8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / stabs / common / ctfstabs.c
blob3b628d52a595b702be1ee7114eac0606efecbecf
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This program has two modes.
32 * In the first, or genassym, mode, it generates a header file containing
33 * #define'd values for offsets and other information about requested
34 * structures and arrays. This header file can then be used by assembly
35 * source files to access those structures without having to hard-code the
36 * offsets. The offsets and values in the header file are derived from the
37 * CTF data in a provided object file.
39 * The second mode creates forthdebug macros for specified structures and
40 * members from an object file. The macros are created using the CTF data in
41 * the object file.
43 * Forthdebug macros and offsets header files are generated using the same
44 * tool for historical reasons.
46 * The input and output files, and their interaction with the tool are
47 * shown below:
49 * --------------- ----------- cc -c -g ------------------
50 * |#includes | -----> |#includes| ------------> |object file with|
51 * |mode-specific| ----------- ctfconvert | CTF data |
52 * | directives| ------------------
53 * --------------- |
54 * | | obj_file
55 * | V
56 * | ------------ ----------
57 * \-------------> |directives| ---------------> |ctfstabs|
58 * ------------ input_template ----------
59 * |
60 * V
61 * ---------------
62 * Mode-specific input and output formats are |mode-specific|
63 * described in forth.c and genassym.c | output |
64 * ---------------
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <stdarg.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <unistd.h>
73 #include <libgen.h>
74 #include <string.h>
75 #include <ctype.h>
76 #include <sys/types.h>
77 #include <sys/stat.h>
79 #include "ctf_headers.h"
80 #include "utils.h"
81 #include "memory.h"
82 #include "ctfstabs.h"
84 #define WORD_LEN 256
86 static int lineno;
88 FILE *out;
89 ctf_file_t *ctf;
91 static void
92 usage(void)
94 (void) fprintf(stderr, "Usage: %s -t genassym [-m model] "
95 "[-i input_template] [-o output] obj_file\n", getpname());
96 (void) fprintf(stderr, " %s -t forth [-m model] "
97 "[-i input_template] [-o output] obj_file\n", getpname());
98 exit(2);
101 /*PRINTFLIKE1*/
103 parse_warn(char *format, ...)
105 va_list alist;
107 (void) fprintf(stderr, "%s: Line %d: ", getpname(), lineno);
109 va_start(alist, format);
110 (void) vfprintf(stderr, format, alist);
111 va_end(alist);
113 (void) fprintf(stderr, "\n");
115 return (-1);
118 #define READLINE_BUF_INCR 2
121 * Read a line of input into a statically-allocated buffer. If the line
122 * is larger than the buffer, the buffer will be dynamically resized.
123 * Subsequent calls will overwrite the buffer.
125 static char *
126 readline(FILE *fp)
128 static char *buf, *bptr;
129 static int buflen;
131 if (buflen == 0) {
132 buf = xmalloc(READLINE_BUF_INCR);
133 buflen = READLINE_BUF_INCR;
136 bptr = buf;
137 for (;;) {
138 size_t len, off;
140 if (fgets(bptr, buflen - (size_t)(bptr - buf), fp) == NULL)
141 return (NULL);
143 len = strlen(bptr);
145 if (bptr[len - 1] == '\n')
146 return (buf);
148 off = (size_t)((bptr + len) - buf);
149 buflen += READLINE_BUF_INCR;
150 buf = xrealloc(buf, buflen);
151 bptr = buf + off;
156 * We're only given a type name. Even if it's a struct or a union, we
157 * still only get the struct or union name. We therefore iterate through
158 * the possible prefixes, trying to find the right type.
160 ctf_id_t
161 find_type(char *name)
163 char fullname[WORD_LEN];
164 ctf_id_t id;
166 if ((id = ctf_lookup_by_name(ctf, name)) != CTF_ERR)
167 return (id);
169 (void) snprintf(fullname, WORD_LEN, "struct %s", name);
170 if ((id = ctf_lookup_by_name(ctf, fullname)) != CTF_ERR)
171 return (id);
173 (void) snprintf(fullname, WORD_LEN, "union %s", name);
174 if ((id = ctf_lookup_by_name(ctf, fullname)) != CTF_ERR)
175 return (id);
177 (void) snprintf(fullname, WORD_LEN, "enum %s", name);
178 if ((id = ctf_lookup_by_name(ctf, fullname)) != CTF_ERR)
179 return (id);
181 return (CTF_ERR);
184 static int
185 process_ifile(FILE *tmpl, proc_ops_t *ops)
187 char *line;
188 int skipping;
189 size_t len;
190 int err = 0;
192 for (lineno = skipping = 0; (line = readline(tmpl)) != NULL; lineno++) {
193 len = strlen(line) - 1;
194 line[len] = '\0';
196 if (len == 0)
197 skipping = 0;
199 if (skipping == 1)
200 continue;
202 if (ops->po_line(line) < 0) {
203 (void) parse_warn("Error found: skipping to the next "
204 "blank line");
205 err++;
206 skipping = 1;
207 continue;
211 return (err > 0 ? -1 : 0);
214 static char *
215 get_model(ctf_file_t *ctf)
217 ssize_t lsz;
218 ctf_id_t lid;
220 /* Neither of these should fail */
221 if ((lid = ctf_lookup_by_name(ctf, "long")) == CTF_ERR ||
222 (lsz = ctf_type_size(ctf, lid)) == CTF_ERR)
223 die("Couldn't get size of long in object file");
225 if (lsz == 8)
226 return ("lp64");
227 else if (lsz == 4)
228 return ("ilp32");
229 else
230 die("Unexpected size of long: %d bytes\n", lsz);
232 return (NULL);
236 main(int argc, char **argv)
238 char *model = NULL, *objfile = NULL, *outfile = NULL, *tmplfile = NULL;
239 proc_ops_t *ops = &ga_ops;
240 FILE *tmpl;
241 int ctferr, c;
243 while ((c = getopt(argc, argv, "i:m:o:t:")) != EOF) {
244 switch (c) {
245 case 'i':
246 tmplfile = optarg;
247 break;
248 case 'm':
249 model = optarg;
250 break;
251 case 't':
252 if (strcmp(optarg, "genassym") == 0)
253 ops = &ga_ops;
254 else if (strcmp(optarg, "forth") == 0)
255 ops = &fth_ops;
256 else
257 usage();
258 break;
260 case 'o':
261 outfile = optarg;
262 break;
263 default:
264 usage();
268 if (argc - optind != 1)
269 usage();
270 objfile = argv[optind];
272 if (tmplfile == NULL || strcmp(tmplfile, "-") == 0)
273 tmpl = stdin;
274 else if ((tmpl = fopen(tmplfile, "r")) == NULL)
275 die("Couldn't open template file %s", tmplfile);
278 * this can fail if ENOENT or if there's no CTF data in the file.
280 if ((ctf = ctf_open(objfile, &ctferr)) == NULL) {
281 die("Couldn't open object file %s: %s\n", objfile,
282 ctf_errmsg(ctferr));
285 if (model == NULL)
286 model = get_model(ctf);
287 else if (strcmp(model, get_model(ctf)) != 0)
288 die("Model argument %s doesn't match the object file\n", model);
290 if (outfile == NULL || strcmp(outfile, "-") == 0)
291 out = stdout;
292 else if ((out = fopen(outfile, "w")) == NULL)
293 die("Couldn't open output file %s for writing", outfile);
295 if ((ops->po_init != NULL && ops->po_init(model) < 0) ||
296 (process_ifile(tmpl, ops) < 0) ||
297 (ops->po_fini != NULL && ops->po_fini() < 0)) {
298 (void) fclose(out);
299 (void) unlink(outfile);
300 return (1);
303 return (0);