xref: /illumos-gate/usr/src/tools/ctf/stabs/common/genassym.c (revision 3ba944265c4ae1fcf23ef758537c2e4f4feec16e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * In this mode, we generate header files containg various #defines which can
28  * be used to access members of various structures, and to walk through arrays.
29  * The input template specifies the structures and members for whom #defines
30  * are to be generated.
31  *
32  * The template has the following elements
33  *
34  * 1. Given the name of a structure or union, #defines can be generated that
35  *    describe the type.  If requested, #defines that give the size and the
36  *    log2 (shift) of the structure will be generated.  The latter can only
37  *    be requested for structures whose size is a power of two.
38  *
39  *    Per-member #defines are also generated.  The value of these defines will
40  *    be the offsets necessary to access the members they describe.  By
41  *    default, the name of the #define will be the name of the member, in upper
42  *    case, but a user-supplied version can be used instead.  If the member is
43  *    an array, an extra #define will be generated that will give the increment
44  *    needed to access individual array elements.  The name of the increment
45  *    #define will be identical to that of the member #define, but with an
46  *    "_INCR" suffix.
47  *
48  * 2. Literal cpp directives
49  *
50  *    Lines beginning with "\#" are copied directly to the output file.
51  *
52  * 3. Comments
53  *
54  *    Lines beginning with backslashes (excluding the literal cpp directives
55  *    described above) are ignored.
56  *
57  * Example input:
58  *
59  *    \ Dump the `foo' structure, creating a size #define called FOO_SIZE, and a
60  *    \ shift #define called FOO_SHIFT.  `foo' has one member called `mem'.
61  *    foo FOO_SIZE FOO_SHIFT
62  *
63  *    \ Dump the `a' and `b' members of the `bar' structure.  the offset
64  *    \ #defines for these members should be `FRED' and `BOB', respectively.
65  *    \ Both members are of type `char'
66  *    bar
67  *	a	FRED
68  *	b	BOB
69  *
70  * Example output:
71  *
72  *    #define FOO_SIZE	0x4
73  *    #define FOO_SHIFT	0x2
74  *    #define FRED	0x0
75  *    #define FRED_INCR	0x1
76  *    #define BOB	0x4
77  */
78 
79 #include <string.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <ctype.h>
83 #include <sys/types.h>
84 
85 #include "ctf_headers.h"
86 #include "utils.h"
87 #include "ctfstabs.h"
88 
89 static int
90 ga_parse_tokens(char *line, int max, char ***wret)
91 {
92 	char *c = line;
93 	char *word;
94 	int n;
95 
96 	while (isspace(*c))
97 		c++;
98 
99 	for (n = 1, word = strtok(line, " \t"); word != NULL;
100 	    word = strtok(NULL, " \t"), n++) {
101 		if (n > max)
102 			return (-1);
103 
104 		*(wret[n - 1]) = word;
105 	}
106 
107 	return (n - 1);
108 }
109 
110 static int
111 ga_parse_common(char *line, int min, int max, char **w1, char **w2, char **w3)
112 {
113 	char **wret[3];
114 	int nread;
115 
116 	wret[0] = w1;
117 	wret[1] = w2;
118 	wret[2] = w3;
119 
120 	if ((nread = ga_parse_tokens(line, max, wret)) < min)
121 		return (-1);
122 
123 	if (nread < 3 && wret[2] != NULL)
124 		*wret[2] = (char *)NULL;
125 	if (nread < 2 && wret[1] != NULL)
126 		*wret[1] = (char *)NULL;
127 	if (nread < 1 && wret[0] != NULL)
128 		*wret[0] = (char *)NULL;
129 
130 	return (nread);
131 }
132 
133 /*
134  * Valid format:	typename [sizedefname [shiftdefname]]
135  */
136 static int
137 ga_parse_name(char *line, char **cnp, char **szdp, char **shdp)
138 {
139 	return (ga_parse_common(line, 1, 3, cnp, szdp, shdp));
140 }
141 
142 /*
143  * Valid format:	memname [offdefname]
144  */
145 static int
146 ga_parse_member(char *line, char **mnp, char **offp)
147 {
148 	return (ga_parse_common(line, 1, 2, mnp, offp, NULL));
149 }
150 
151 /*
152  * Used to begin a new structure/union block, and to print the optional size
153  * and optional shift constants.
154  */
155 static int
156 ga_process_name(char *line)
157 {
158 	char *curname, *sizedef, *shdef;
159 	ctf_id_t curtype;
160 	ssize_t sz, shift;
161 
162 	if (ga_parse_name(line, &curname, &sizedef, &shdef) < 0)
163 		return (parse_warn("Couldn't parse name"));
164 
165 	if ((curtype = find_type(curname)) == CTF_ERR)
166 		return (parse_warn("Couldn't find type %s", curname));
167 
168 	if (sizedef != NULL) {
169 		if ((sz = ctf_type_size(ctf, curtype)) < 0) {
170 			return (parse_warn("Couldn't get size for type %s",
171 			    curname));
172 		} else if (sz == 0) {
173 			return (parse_warn("Invalid type size 0 for %s",
174 			    curname));
175 		}
176 
177 		(void) fprintf(out, "#define\t%s\t0x%x\n", sizedef, sz);
178 	}
179 
180 	if (shdef != NULL) {
181 		ssize_t tsz;
182 
183 		for (shift = -1, tsz = sz; tsz > 0; tsz >>= 1, shift++)
184 			;
185 		if (shift < 0 || 1 << shift != sz) {
186 			return (parse_warn("Can't make shift #define: %s size "
187 			    "(%d) isn't a power of 2", curname, sz));
188 		}
189 
190 		(void) fprintf(out, "#define\t%s\t0x%x\n", shdef, shift);
191 	}
192 
193 	return (curtype);
194 }
195 
196 /*
197  * ga_process_member() and ga_member_cb() are used to print the offset and
198  * possibly array increment values for a given structure member.  A specific
199  * member is requested via ga_process_member(), and ga_member_cb() is used
200  * to iterate through the members of the current structure type, looking for
201  * that member.  This is not the most efficient way to do things, but the
202  * lists involved are generally short.
203  */
204 typedef struct ga_member_cb_data {
205 	char *gmcb_memname;
206 	char *gmcb_submem;
207 	char *gmcb_offdef;
208 	size_t gmcb_off;
209 } ga_member_cb_data_t;
210 
211 static int ga_member_find(ctf_id_t, ga_member_cb_data_t *);
212 
213 static int
214 ga_member_cb(const char *name, ctf_id_t type, ulong_t off, void *arg)
215 {
216 	ga_member_cb_data_t *md = arg;
217 	ctf_arinfo_t arinfo;
218 	char *label;
219 
220 	if (strcmp(name, md->gmcb_memname) != 0)
221 		return (0);
222 
223 	md->gmcb_off += off / 8;	/* off is in bits */
224 
225 	if (md->gmcb_submem != NULL) {
226 		/*
227 		 * The user requested foo.bar.  We've found foo, and now need to
228 		 * recurse down to bar.
229 		 */
230 		ga_member_cb_data_t smd;
231 
232 		smd.gmcb_memname = md->gmcb_submem;
233 		smd.gmcb_submem = NULL;
234 		smd.gmcb_offdef = md->gmcb_offdef;
235 		smd.gmcb_off = md->gmcb_off;
236 
237 		return (ga_member_find(type, &smd));
238 	}
239 
240 	if (md->gmcb_offdef == NULL) {
241 		int i;
242 
243 		label = md->gmcb_memname;
244 		for (i = 0; i < strlen(label); i++)
245 			label[i] = toupper(label[i]);
246 	} else
247 		label = md->gmcb_offdef;
248 
249 	/* offsets are in bits - we need bytes */
250 	(void) fprintf(out, "#define\t%s\t0x%lx\n", label,
251 	    (ulong_t)md->gmcb_off);
252 
253 	if ((type = ctf_type_resolve(ctf, type)) == CTF_ERR)
254 		return (parse_warn("Couldn't resolve type %s", name));
255 
256 	if (ctf_array_info(ctf, type, &arinfo) == 0) {
257 		ssize_t sz;
258 
259 		if ((sz = ctf_type_size(ctf, arinfo.ctr_contents)) < 0)
260 			return (parse_warn("Couldn't get array elem size"));
261 
262 		(void) fprintf(out, "#define\t%s_INCR\t0x%x\n", label, sz);
263 	}
264 
265 	return (1);
266 }
267 
268 static int
269 ga_member_find(ctf_id_t curtype, ga_member_cb_data_t *md)
270 {
271 	char *c;
272 	int rc;
273 
274 	if ((c = strchr(md->gmcb_memname, '.')) != NULL)
275 		*c++ = '\0';
276 	md->gmcb_submem = c;
277 
278 	if ((rc = ctf_member_iter(ctf, curtype, ga_member_cb, md)) == 0) {
279 		return (parse_warn("Couldn't find member named %s",
280 		    md->gmcb_memname));
281 	} else if (rc != 1)
282 		return (parse_warn("Can't parse"));
283 
284 	return (1);
285 }
286 
287 static int
288 ga_process_member(ctf_id_t curtype, char *line)
289 {
290 	ga_member_cb_data_t md = { 0 };
291 
292 	if (ga_parse_member(line, &md.gmcb_memname, &md.gmcb_offdef) < 0)
293 		return (parse_warn("Couldn't parse member"));
294 
295 	return (ga_member_find(curtype, &md));
296 }
297 
298 static int
299 ga_process_line(char *line)
300 {
301 	static int curtype = -1;
302 	static int blanks = 0;
303 
304 	if (strlen(line) == 0) {
305 		blanks++;
306 		return (1);
307 	} else if (blanks) {
308 		if (!isspace(line[0]))
309 			curtype = -1;
310 		blanks = 0;
311 	}
312 
313 	if (line[0] == '\\') {
314 		if (line[1] == '#') {
315 			/* dump, verbatim, lines that begin with "\#" */
316 			(void) fprintf(out, "%s\n", line + 1);
317 		}
318 		return (1);
319 
320 	} else if (line[0] == '#') {
321 		/*
322 		 * This is a comment of some sort; is it a line number
323 		 * comment?  Those look like '# 53 "filename.c"'.  GCC
324 		 * sometimes inserts them and removes all other vertical
325 		 * whitespace, so they should be treated as a "type
326 		 * terminator" like a blank line is.
327 		 */
328 		if (isdigit(line[2])) {
329 			/* line number, terminate type */
330 			curtype = -1;
331 		}
332 		return (1);
333 	}
334 	if (curtype == -1)
335 		return ((curtype = ga_process_name(line)));
336 	else
337 		return (ga_process_member(curtype, line));
338 }
339 
340 proc_ops_t ga_ops = {
341 	NULL,
342 	ga_process_line,
343 	NULL
344 };
345