1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * In this mode, we generate forthdebug macros as requested by the input
31*0Sstevel@tonic-gate * template, the format of which is given below.
32*0Sstevel@tonic-gate *
33*0Sstevel@tonic-gate * These templates have the following elements:
34*0Sstevel@tonic-gate *
35*0Sstevel@tonic-gate * 1. Macro creation
36*0Sstevel@tonic-gate *
37*0Sstevel@tonic-gate * Given the name of a structure, union, or enum type, a forthdebug macro
38*0Sstevel@tonic-gate * is created that will dump the members of the type. The type can be
39*0Sstevel@tonic-gate * specified as a standalone structure, union, or enum, or it can be
40*0Sstevel@tonic-gate * described relative to another structure or union as "type.member".
41*0Sstevel@tonic-gate *
42*0Sstevel@tonic-gate * By default, all struct members, union members, or enum values, as
43*0Sstevel@tonic-gate * appropriate, will be printed. An alternate form allows specific members
44*0Sstevel@tonic-gate * of struct or union types to be printed. Both forms must be followed by a
45*0Sstevel@tonic-gate * blank line. In the specific-member case, an optional format specifier can
46*0Sstevel@tonic-gate * be provided that will be used to dump the contents of the member.
47*0Sstevel@tonic-gate * Builtins `d' and `x' can be used to dump the member in decimal or
48*0Sstevel@tonic-gate * hexadecimal, respectively. Alternatively, a custom formatter can be
49*0Sstevel@tonic-gate * specified.
50*0Sstevel@tonic-gate *
51*0Sstevel@tonic-gate * 2. Model-specific sections
52*0Sstevel@tonic-gate *
53*0Sstevel@tonic-gate * `model_start' / `model_end' pairs function as an #ifdef for the ctfstabs
54*0Sstevel@tonic-gate * tool. They take, as an argument, either `ilp32' or `lp64'. If a 64-bit
55*0Sstevel@tonic-gate * macro is being generated (if a 64-bit object file is being used), lines
56*0Sstevel@tonic-gate * between `lp64' model pairs will be processed, but lines between `ilp32'
57*0Sstevel@tonic-gate * pairs will be omitted. The reverse is true for 32-bit macros.
58*0Sstevel@tonic-gate *
59*0Sstevel@tonic-gate * 3. Literal sections
60*0Sstevel@tonic-gate *
61*0Sstevel@tonic-gate * Portions of the input template file enclosed within `forth_start' /
62*0Sstevel@tonic-gate * `forth_end' pairs and between `verbatim_begin' / `verbatim_end' pairs
63*0Sstevel@tonic-gate * will be copied as-is to the output file.
64*0Sstevel@tonic-gate *
65*0Sstevel@tonic-gate * 4. Comments
66*0Sstevel@tonic-gate *
67*0Sstevel@tonic-gate * Lines beginning with backslashes are ignored.
68*0Sstevel@tonic-gate *
69*0Sstevel@tonic-gate * Example:
70*0Sstevel@tonic-gate *
71*0Sstevel@tonic-gate * \ dump the `foo' structure
72*0Sstevel@tonic-gate * foo
73*0Sstevel@tonic-gate *
74*0Sstevel@tonic-gate * \ dump the `a' and `b' members of the `bar' structure. dump member `b'
75*0Sstevel@tonic-gate * \ in hexadecimal
76*0Sstevel@tonic-gate * bar
77*0Sstevel@tonic-gate * a
78*0Sstevel@tonic-gate * b x
79*0Sstevel@tonic-gate *
80*0Sstevel@tonic-gate * \ dump the `big' member of the `baz' structure in 64-bit macros, and
81*0Sstevel@tonic-gate * \ the `small' member in 32-bit macros.
82*0Sstevel@tonic-gate * baz
83*0Sstevel@tonic-gate * model_start lp64
84*0Sstevel@tonic-gate * big
85*0Sstevel@tonic-gate * model_end
86*0Sstevel@tonic-gate * model_start ilp32
87*0Sstevel@tonic-gate * small
88*0Sstevel@tonic-gate * model_end
89*0Sstevel@tonic-gate *
90*0Sstevel@tonic-gate * \ copy `literal 1' and `literal 2' to the output file
91*0Sstevel@tonic-gate * verbatim_begin
92*0Sstevel@tonic-gate * literal 1
93*0Sstevel@tonic-gate * verbatim_end
94*0Sstevel@tonic-gate * forth_start
95*0Sstevel@tonic-gate * literal 2
96*0Sstevel@tonic-gate * forth_end
97*0Sstevel@tonic-gate *
98*0Sstevel@tonic-gate * For a more complex example, see common.fdbg.
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate #include <stdio.h>
102*0Sstevel@tonic-gate #include <stdlib.h>
103*0Sstevel@tonic-gate #include <string.h>
104*0Sstevel@tonic-gate #include <ctype.h>
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate #include "ctf_headers.h"
107*0Sstevel@tonic-gate #include "ctfstabs.h"
108*0Sstevel@tonic-gate #include "forth.h"
109*0Sstevel@tonic-gate #include "utils.h"
110*0Sstevel@tonic-gate #include "memory.h"
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate char *fth_curtype; /* name of the type being processed */
113*0Sstevel@tonic-gate static fth_type_ops_t *fth_type_ops; /* see forth.h */
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate static char *fth_model; /* the current macro type - for model_start */
116*0Sstevel@tonic-gate static int fth_ignoring; /* in a non-matching model_start/end pair */
117*0Sstevel@tonic-gate static int fth_copying; /* in a verbatim_* or forth_* pair */
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate static int
fth_init(char * model)120*0Sstevel@tonic-gate fth_init(char *model)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate fth_model = model;
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate return (0);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate /*ARGSUSED*/
128*0Sstevel@tonic-gate static int
fth_null_header(ctf_id_t tid)129*0Sstevel@tonic-gate fth_null_header(ctf_id_t tid)
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate return (0);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate /*ARGSUSED*/
135*0Sstevel@tonic-gate static int
fth_null_members(char * memfilter,char * format)136*0Sstevel@tonic-gate fth_null_members(char *memfilter, char *format)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate return (0);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate static int
fth_null_trailer(void)142*0Sstevel@tonic-gate fth_null_trailer(void)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate return (0);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate static fth_type_ops_t fth_null_ops = {
148*0Sstevel@tonic-gate fth_null_header,
149*0Sstevel@tonic-gate fth_null_members,
150*0Sstevel@tonic-gate fth_null_trailer
151*0Sstevel@tonic-gate };
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate /*ARGSUSED2*/
154*0Sstevel@tonic-gate static int
find_member_cb(const char * memname,ctf_id_t tid,ulong_t off,void * arg)155*0Sstevel@tonic-gate find_member_cb(const char *memname, ctf_id_t tid, ulong_t off, void *arg)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate char *memtofind = arg;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate if (strcmp(memname, memtofind) == 0)
160*0Sstevel@tonic-gate return (tid);
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate return (0);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate /* find the tid of a specified member */
166*0Sstevel@tonic-gate static ctf_id_t
find_member(ctf_id_t tid,char * memname)167*0Sstevel@tonic-gate find_member(ctf_id_t tid, char *memname)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate return (ctf_member_iter(ctf, tid, find_member_cb, memname));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate * Begin a macro.
174*0Sstevel@tonic-gate *
175*0Sstevel@tonic-gate * Once we figure out the type of the thing that we're supposed to dump (struct,
176*0Sstevel@tonic-gate * union, or enum), we select the proper type-specific ops-vector for dumping.
177*0Sstevel@tonic-gate */
178*0Sstevel@tonic-gate static int
fth_section_init(char * fullname)179*0Sstevel@tonic-gate fth_section_init(char *fullname)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate ctf_id_t ltid = 0, tid;
182*0Sstevel@tonic-gate char *curtype, *lpart, *part, *npart;
183*0Sstevel@tonic-gate int lkind = 0, kind;
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate curtype = xstrdup(fullname);
186*0Sstevel@tonic-gate lpart = NULL;
187*0Sstevel@tonic-gate part = strtok(fullname, ".");
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate * First figure out what sort of type we're looking at. Life would be
191*0Sstevel@tonic-gate * simple if we were only going to get type names, but it's not - we
192*0Sstevel@tonic-gate * could also get `type.member'. In that case, we need to figure out
193*0Sstevel@tonic-gate * (and dump) the type of `member' instead.
194*0Sstevel@tonic-gate */
195*0Sstevel@tonic-gate for (;;) {
196*0Sstevel@tonic-gate if (lpart == NULL) {
197*0Sstevel@tonic-gate /* First part - the struct name */
198*0Sstevel@tonic-gate if ((tid = find_type(part)) == CTF_ERR ||
199*0Sstevel@tonic-gate (tid = ctf_type_resolve(ctf, tid)) == CTF_ERR ||
200*0Sstevel@tonic-gate (kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
201*0Sstevel@tonic-gate free(curtype);
202*0Sstevel@tonic-gate return (parse_warn("Couldn't find %s: %s",
203*0Sstevel@tonic-gate part, ctf_errmsg(ctf_errno(ctf))));
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate } else {
206*0Sstevel@tonic-gate /* Second (or more) part - the member name */
207*0Sstevel@tonic-gate if (lkind != CTF_K_STRUCT && lkind != CTF_K_UNION) {
208*0Sstevel@tonic-gate free(curtype);
209*0Sstevel@tonic-gate return (parse_warn("%s isn't a struct/union",
210*0Sstevel@tonic-gate lpart));
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate if ((tid = find_member(ltid, part)) <= 0) {
214*0Sstevel@tonic-gate free(curtype);
215*0Sstevel@tonic-gate return (parse_warn("%s isn't a member of %s",
216*0Sstevel@tonic-gate part, lpart));
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate if ((kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
220*0Sstevel@tonic-gate free(curtype);
221*0Sstevel@tonic-gate return (parse_warn("Can't get kind for %s",
222*0Sstevel@tonic-gate part));
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate /*
227*0Sstevel@tonic-gate * Stop if there aren't any more parts. We use `npart' here
228*0Sstevel@tonic-gate * because we don't want to clobber part - we need it later.
229*0Sstevel@tonic-gate */
230*0Sstevel@tonic-gate if ((npart = strtok(NULL, ".")) == NULL)
231*0Sstevel@tonic-gate break;
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate lpart = part;
234*0Sstevel@tonic-gate ltid = tid;
235*0Sstevel@tonic-gate lkind = kind;
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate part = npart;
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate /*
241*0Sstevel@tonic-gate * Pick the right ops vector for dumping.
242*0Sstevel@tonic-gate */
243*0Sstevel@tonic-gate switch (kind) {
244*0Sstevel@tonic-gate case CTF_K_STRUCT:
245*0Sstevel@tonic-gate case CTF_K_UNION:
246*0Sstevel@tonic-gate fth_type_ops = &fth_struct_ops;
247*0Sstevel@tonic-gate break;
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate case CTF_K_ENUM:
250*0Sstevel@tonic-gate fth_type_ops = &fth_enum_ops;
251*0Sstevel@tonic-gate break;
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate default:
254*0Sstevel@tonic-gate fth_type_ops = &fth_null_ops;
255*0Sstevel@tonic-gate free(curtype);
256*0Sstevel@tonic-gate return (parse_warn("%s isn't a struct, union, or enum", part));
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate fth_curtype = curtype;
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate return (fth_type_ops->fto_header(tid));
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate static int
fth_section_add_member(char * name,char * format)265*0Sstevel@tonic-gate fth_section_add_member(char *name, char *format)
266*0Sstevel@tonic-gate {
267*0Sstevel@tonic-gate if (fth_curtype == NULL)
268*0Sstevel@tonic-gate return (fth_section_init(name));
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate if (fth_type_ops->fto_members(name, format) < 0)
271*0Sstevel@tonic-gate return (-1);
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate return (0);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gate static int
fth_section_end(void)277*0Sstevel@tonic-gate fth_section_end(void)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate if (fth_curtype == NULL)
280*0Sstevel@tonic-gate return (0);
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate if (fth_type_ops->fto_trailer() < 0)
283*0Sstevel@tonic-gate return (-1);
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate free(fth_curtype);
286*0Sstevel@tonic-gate fth_curtype = NULL;
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate return (0);
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate static int
fth_process_line(char * line)292*0Sstevel@tonic-gate fth_process_line(char *line)
293*0Sstevel@tonic-gate {
294*0Sstevel@tonic-gate char *format = NULL;
295*0Sstevel@tonic-gate char *word, *name, *c;
296*0Sstevel@tonic-gate int nblank = 0;
297*0Sstevel@tonic-gate int n;
298*0Sstevel@tonic-gate
299*0Sstevel@tonic-gate if (strlen(line) == 0) {
300*0Sstevel@tonic-gate if (fth_section_end() < 0)
301*0Sstevel@tonic-gate return (-1);
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate if (fth_copying == 1 || nblank++ == 1)
304*0Sstevel@tonic-gate (void) fprintf(out, "\n");
305*0Sstevel@tonic-gate return (0);
306*0Sstevel@tonic-gate } else
307*0Sstevel@tonic-gate nblank = 0;
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate /* skip comments */
310*0Sstevel@tonic-gate if (line[0] == '\\')
311*0Sstevel@tonic-gate return (0);
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate if (strcmp(line, "model_end") == 0) {
314*0Sstevel@tonic-gate fth_ignoring = 0;
315*0Sstevel@tonic-gate return (0);
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate if (fth_ignoring == 1)
319*0Sstevel@tonic-gate return (0);
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate word = "model_start ";
322*0Sstevel@tonic-gate if (strncmp(line, word, strlen(word)) == 0) {
323*0Sstevel@tonic-gate for (c = line + strlen(word); isspace(*c); c++);
324*0Sstevel@tonic-gate if (strlen(c) == strlen(fth_model) &&
325*0Sstevel@tonic-gate strncmp(c, fth_model, strlen(fth_model)) == 0)
326*0Sstevel@tonic-gate /* EMPTY - match */;
327*0Sstevel@tonic-gate else
328*0Sstevel@tonic-gate fth_ignoring = 1;
329*0Sstevel@tonic-gate return (0);
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate if (strcmp(line, "verbatim_end") == 0 ||
333*0Sstevel@tonic-gate strcmp(line, "forth_end") == 0) {
334*0Sstevel@tonic-gate char *start = (strcmp(line, "verbatim_end") == 0 ?
335*0Sstevel@tonic-gate "verbatim_begin" : "forth_start");
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate if (fth_copying == 0) {
338*0Sstevel@tonic-gate (void) parse_warn("Found %s without matching %s",
339*0Sstevel@tonic-gate line, start);
340*0Sstevel@tonic-gate if (fth_curtype != NULL)
341*0Sstevel@tonic-gate (void) fth_section_end();
342*0Sstevel@tonic-gate return (-1);
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate fth_copying = 0;
345*0Sstevel@tonic-gate return (0);
346*0Sstevel@tonic-gate }
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate if (fth_copying == 1) {
349*0Sstevel@tonic-gate (void) fprintf(out, "%s\n", line);
350*0Sstevel@tonic-gate return (0);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate if (strcmp(line, "verbatim_begin") == 0 ||
354*0Sstevel@tonic-gate strcmp(line, "forth_start") == 0) {
355*0Sstevel@tonic-gate if (fth_curtype != NULL) {
356*0Sstevel@tonic-gate (void) parse_warn("Expected blank line between %s "
357*0Sstevel@tonic-gate "macro and %s", fth_curtype, line);
358*0Sstevel@tonic-gate return (fth_section_end());
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate fth_copying = 1;
362*0Sstevel@tonic-gate return (0);
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate for (n = 1, word = strtok(line, " \t"); word != NULL;
366*0Sstevel@tonic-gate word = strtok(NULL, " \t"), n++) {
367*0Sstevel@tonic-gate if (n == 1)
368*0Sstevel@tonic-gate name = word;
369*0Sstevel@tonic-gate else if (n == 2)
370*0Sstevel@tonic-gate format = word;
371*0Sstevel@tonic-gate else
372*0Sstevel@tonic-gate (void) parse_warn("Too many words");
373*0Sstevel@tonic-gate }
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gate return (fth_section_add_member(name, format));
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate static int
fth_fini(void)379*0Sstevel@tonic-gate fth_fini(void)
380*0Sstevel@tonic-gate {
381*0Sstevel@tonic-gate return (fth_section_end());
382*0Sstevel@tonic-gate }
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate proc_ops_t fth_ops = {
385*0Sstevel@tonic-gate fth_init,
386*0Sstevel@tonic-gate fth_process_line,
387*0Sstevel@tonic-gate fth_fini
388*0Sstevel@tonic-gate };
389