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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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.
32 * The template has the following elements
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.
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
48 * 2. Literal cpp directives
50 * Lines beginning with "\#" are copied directly to the output file.
54 * Lines beginning with backslashes (excluding the literal cpp directives
55 * described above) are ignored.
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
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'
72 * #define FOO_SIZE 0x4
73 * #define FOO_SHIFT 0x2
75 * #define FRED_INCR 0x1
83 #include <sys/types.h>
85 #include "ctf_headers.h"
90 ga_parse_tokens(char *line
, int max
, char ***wret
)
99 for (n
= 1, word
= strtok(line
, " \t"); word
!= NULL
;
100 word
= strtok(NULL
, " \t"), n
++) {
104 *(wret
[n
- 1]) = word
;
111 ga_parse_common(char *line
, int min
, int max
, char **w1
, char **w2
, char **w3
)
120 if ((nread
= ga_parse_tokens(line
, max
, wret
)) < min
)
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
;
134 * Valid format: typename [sizedefname [shiftdefname]]
137 ga_parse_name(char *line
, char **cnp
, char **szdp
, char **shdp
)
139 return (ga_parse_common(line
, 1, 3, cnp
, szdp
, shdp
));
143 * Valid format: memname [offdefname]
146 ga_parse_member(char *line
, char **mnp
, char **offp
)
148 return (ga_parse_common(line
, 1, 2, mnp
, offp
, NULL
));
152 * Used to begin a new structure/union block, and to print the optional size
153 * and optional shift constants.
156 ga_process_name(char *line
)
158 char *curname
, *sizedef
, *shdef
;
162 if (ga_parse_name(line
, &curname
, &sizedef
, &shdef
) < 0)
163 return (parse_warn("Couldn't parse name"));
165 if ((curtype
= find_type(curname
)) == CTF_ERR
)
166 return (parse_warn("Couldn't find type %s", curname
));
168 if (sizedef
!= NULL
) {
169 if ((sz
= ctf_type_size(ctf
, curtype
)) < 0) {
170 return (parse_warn("Couldn't get size for type %s",
172 } else if (sz
== 0) {
173 return (parse_warn("Invalid type size 0 for %s",
177 (void) fprintf(out
, "#define\t%s\t0x%x\n", sizedef
, sz
);
183 for (shift
= -1, tsz
= sz
; tsz
> 0; tsz
>>= 1, shift
++)
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
));
190 (void) fprintf(out
, "#define\t%s\t0x%x\n", shdef
, shift
);
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.
204 typedef struct ga_member_cb_data
{
209 } ga_member_cb_data_t
;
211 static int ga_member_find(ctf_id_t
, ga_member_cb_data_t
*);
214 ga_member_cb(const char *name
, ctf_id_t type
, ulong_t off
, void *arg
)
216 ga_member_cb_data_t
*md
= arg
;
220 if (strcmp(name
, md
->gmcb_memname
) != 0)
223 md
->gmcb_off
+= off
/ 8; /* off is in bits */
225 if (md
->gmcb_submem
!= NULL
) {
227 * The user requested foo.bar. We've found foo, and now need to
228 * recurse down to bar.
230 ga_member_cb_data_t smd
;
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
;
237 return (ga_member_find(type
, &smd
));
240 if (md
->gmcb_offdef
== NULL
) {
243 label
= md
->gmcb_memname
;
244 for (i
= 0; i
< strlen(label
); i
++)
245 label
[i
] = toupper(label
[i
]);
247 label
= md
->gmcb_offdef
;
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
);
253 if ((type
= ctf_type_resolve(ctf
, type
)) == CTF_ERR
)
254 return (parse_warn("Couldn't resolve type %s", name
));
256 if (ctf_array_info(ctf
, type
, &arinfo
) == 0) {
259 if ((sz
= ctf_type_size(ctf
, arinfo
.ctr_contents
)) < 0)
260 return (parse_warn("Couldn't get array elem size"));
262 (void) fprintf(out
, "#define\t%s_INCR\t0x%x\n", label
, sz
);
269 ga_member_find(ctf_id_t curtype
, ga_member_cb_data_t
*md
)
274 if ((c
= strchr(md
->gmcb_memname
, '.')) != NULL
)
278 if ((rc
= ctf_member_iter(ctf
, curtype
, ga_member_cb
, md
)) == 0) {
279 return (parse_warn("Couldn't find member named %s",
282 return (parse_warn("Can't parse"));
288 ga_process_member(ctf_id_t curtype
, char *line
)
290 ga_member_cb_data_t md
= { 0 };
292 if (ga_parse_member(line
, &md
.gmcb_memname
, &md
.gmcb_offdef
) < 0)
293 return (parse_warn("Couldn't parse member"));
295 return (ga_member_find(curtype
, &md
));
299 ga_process_line(char *line
)
301 static int curtype
= -1;
302 static int blanks
= 0;
304 if (strlen(line
) == 0) {
308 if (!isspace(line
[0]))
313 if (line
[0] == '\\') {
314 if (line
[1] == '#') {
315 /* dump, verbatim, lines that begin with "\#" */
316 (void) fprintf(out
, "%s\n", line
+ 1);
320 } else if (line
[0] == '#') {
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.
328 if (isdigit(line
[2])) {
329 /* line number, terminate type */
335 return ((curtype
= ga_process_name(line
)));
337 return (ga_process_member(curtype
, line
));
340 proc_ops_t ga_ops
= {