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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* In this mode, we generate forthdebug macros as requested by the input
* template, the format of which is given below.
*
* These templates have the following elements:
*
* 1. Macro creation
*
* Given the name of a structure, union, or enum type, a forthdebug macro
* is created that will dump the members of the type. The type can be
* specified as a standalone structure, union, or enum, or it can be
* described relative to another structure or union as "type.member".
*
* By default, all struct members, union members, or enum values, as
* appropriate, will be printed. An alternate form allows specific members
* of struct or union types to be printed. Both forms must be followed by a
* blank line. In the specific-member case, an optional format specifier can
* be provided that will be used to dump the contents of the member.
* Builtins `d' and `x' can be used to dump the member in decimal or
* hexadecimal, respectively. Alternatively, a custom formatter can be
* specified.
*
* 2. Model-specific sections
*
* `model_start' / `model_end' pairs function as an #ifdef for the ctfstabs
* tool. They take, as an argument, either `ilp32' or `lp64'. If a 64-bit
* macro is being generated (if a 64-bit object file is being used), lines
* between `lp64' model pairs will be processed, but lines between `ilp32'
* pairs will be omitted. The reverse is true for 32-bit macros.
*
* 3. Literal sections
*
* Portions of the input template file enclosed within `forth_start' /
* `forth_end' pairs and between `verbatim_begin' / `verbatim_end' pairs
* will be copied as-is to the output file.
*
* 4. Comments
*
* Lines beginning with backslashes are ignored.
*
* Example:
*
* \ dump the `foo' structure
* foo
*
* \ dump the `a' and `b' members of the `bar' structure. dump member `b'
* \ in hexadecimal
* bar
* a
* b x
*
* \ dump the `big' member of the `baz' structure in 64-bit macros, and
* \ the `small' member in 32-bit macros.
* baz
* model_start lp64
* big
* model_end
* model_start ilp32
* small
* model_end
*
* \ copy `literal 1' and `literal 2' to the output file
* verbatim_begin
* literal 1
* verbatim_end
* forth_start
* literal 2
* forth_end
*
* For a more complex example, see common.fdbg.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "ctf_headers.h"
#include "ctfstabs.h"
#include "forth.h"
#include "utils.h"
#include "memory.h"
char *fth_curtype; /* name of the type being processed */
static fth_type_ops_t *fth_type_ops; /* see forth.h */
static char *fth_model; /* the current macro type - for model_start */
static int fth_ignoring; /* in a non-matching model_start/end pair */
static int fth_copying; /* in a verbatim_* or forth_* pair */
static int
fth_init(char *model)
{
fth_model = model;
return (0);
}
/*ARGSUSED*/
static int
fth_null_header(ctf_id_t tid)
{
return (0);
}
/*ARGSUSED*/
static int
fth_null_members(char *memfilter, char *format)
{
return (0);
}
static int
fth_null_trailer(void)
{
return (0);
}
static fth_type_ops_t fth_null_ops = {
fth_null_header,
fth_null_members,
fth_null_trailer
};
/*ARGSUSED2*/
static int
find_member_cb(const char *memname, ctf_id_t tid, ulong_t off, void *arg)
{
char *memtofind = arg;
if (strcmp(memname, memtofind) == 0)
return (tid);
return (0);
}
/* find the tid of a specified member */
static ctf_id_t
find_member(ctf_id_t tid, char *memname)
{
return (ctf_member_iter(ctf, tid, find_member_cb, memname));
}
/*
* Begin a macro.
*
* Once we figure out the type of the thing that we're supposed to dump (struct,
* union, or enum), we select the proper type-specific ops-vector for dumping.
*/
static int
fth_section_init(char *fullname)
{
ctf_id_t ltid = 0, tid;
char *curtype, *lpart, *part, *npart;
int lkind = 0, kind;
curtype = xstrdup(fullname);
lpart = NULL;
part = strtok(fullname, ".");
/*
* First figure out what sort of type we're looking at. Life would be
* simple if we were only going to get type names, but it's not - we
* could also get `type.member'. In that case, we need to figure out
* (and dump) the type of `member' instead.
*/
for (;;) {
if (lpart == NULL) {
/* First part - the struct name */
if ((tid = find_type(part)) == CTF_ERR ||
(tid = ctf_type_resolve(ctf, tid)) == CTF_ERR ||
(kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
free(curtype);
return (parse_warn("Couldn't find %s: %s",
part, ctf_errmsg(ctf_errno(ctf))));
}
} else {
/* Second (or more) part - the member name */
if (lkind != CTF_K_STRUCT && lkind != CTF_K_UNION) {
free(curtype);
return (parse_warn("%s isn't a struct/union",
lpart));
}
if ((tid = find_member(ltid, part)) <= 0) {
free(curtype);
return (parse_warn("%s isn't a member of %s",
part, lpart));
}
if ((kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
free(curtype);
return (parse_warn("Can't get kind for %s",
part));
}
}
/*
* Stop if there aren't any more parts. We use `npart' here
* because we don't want to clobber part - we need it later.
*/
if ((npart = strtok(NULL, ".")) == NULL)
break;
lpart = part;
ltid = tid;
lkind = kind;
part = npart;
}
/*
* Pick the right ops vector for dumping.
*/
switch (kind) {
case CTF_K_STRUCT:
case CTF_K_UNION:
fth_type_ops = &fth_struct_ops;
break;
case CTF_K_ENUM:
fth_type_ops = &fth_enum_ops;
break;
default:
fth_type_ops = &fth_null_ops;
free(curtype);
return (parse_warn("%s isn't a struct, union, or enum", part));
}
fth_curtype = curtype;
return (fth_type_ops->fto_header(tid));
}
static int
fth_section_add_member(char *name, char *format)
{
if (fth_curtype == NULL)
return (fth_section_init(name));
if (fth_type_ops->fto_members(name, format) < 0)
return (-1);
return (0);
}
static int
fth_section_end(void)
{
if (fth_curtype == NULL)
return (0);
if (fth_type_ops->fto_trailer() < 0)
return (-1);
free(fth_curtype);
fth_curtype = NULL;
return (0);
}
static int
fth_process_line(char *line)
{
char *format = NULL;
char *word, *name, *c;
int nblank = 0;
int n;
if (strlen(line) == 0) {
if (fth_section_end() < 0)
return (-1);
if (fth_copying == 1 || nblank++ == 1)
(void) fprintf(out, "\n");
return (0);
} else
nblank = 0;
/* skip comments */
if (line[0] == '\\')
return (0);
if (strcmp(line, "model_end") == 0) {
fth_ignoring = 0;
return (0);
}
if (fth_ignoring == 1)
return (0);
word = "model_start ";
if (strncmp(line, word, strlen(word)) == 0) {
for (c = line + strlen(word); isspace(*c); c++);
if (strlen(c) == strlen(fth_model) &&
strncmp(c, fth_model, strlen(fth_model)) == 0)
/* EMPTY - match */;
else
fth_ignoring = 1;
return (0);
}
if (strcmp(line, "verbatim_end") == 0 ||
strcmp(line, "forth_end") == 0) {
char *start = (strcmp(line, "verbatim_end") == 0 ?
"verbatim_begin" : "forth_start");
if (fth_copying == 0) {
(void) parse_warn("Found %s without matching %s",
line, start);
if (fth_curtype != NULL)
(void) fth_section_end();
return (-1);
}
fth_copying = 0;
return (0);
}
if (fth_copying == 1) {
(void) fprintf(out, "%s\n", line);
return (0);
}
if (strcmp(line, "verbatim_begin") == 0 ||
strcmp(line, "forth_start") == 0) {
if (fth_curtype != NULL) {
(void) parse_warn("Expected blank line between %s "
"macro and %s", fth_curtype, line);
return (fth_section_end());
}
fth_copying = 1;
return (0);
}
for (n = 1, word = strtok(line, " \t"); word != NULL;
word = strtok(NULL, " \t"), n++) {
if (n == 1)
name = word;
else if (n == 2)
format = word;
else
(void) parse_warn("Too many words");
}
return (fth_section_add_member(name, format));
}
static int
fth_fini(void)
{
return (fth_section_end());
}
proc_ops_t fth_ops = {
fth_init,
fth_process_line,
fth_fini
};
|