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