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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/*
* 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 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Used to dump structures and unions in forth mode.
*
* structures and unions are a bit more complicated than enums. To make things
* just that much more interesting, we have to dump the members in reverse
* order, which is nice. But wait! It gets better! For compatibility reasons,
* we need to dump the members in reverse-offset order, even if member-specific
* mode was used to request the members in something other than that order.
*
* The header op prints the macro header and saves the type being printed.
*
* In member-specific mode, the member op will be invoked for each structure
* or union member. The member op adds the member name, format, type ID,
* and offset to a list, sorted in reverse order by offset.
*
* The trailer op is called when the structure or enum is complete. If no
* members were specifically requested, then the trailer iterates through all
* of the members of the structure, pretending they were. Each member is thus
* added, in reverse-offset order, to the list used in specific-member mode.
* Either way, we then proceed through the list, dumping each member out with
* fth_print_member. Structure and union members are printed out differently,
* depending on member type, as follows:
*
* Integer:
* Normal integers: ' <format> <offset> <type>-field <name>
* <format> defaults to ".d" for enums, ".x" for others
* <offset> is the member offset, in bytes.
* <type> is "byte", "short", "long", or "ext" for 8-, 16-, 32-, and
* 64-bit integers, respectively.
* <name> is the name of the member being printed
*
* Bitfields: ' <format> <shift> <mask> <offset> bits-field <name>
* <format> defaults to ".x"
* <shift> is the number of times to right-shift the masked value
* <mask> use to extract the bit-field value from the read value
* <offset> is the member offset, in bytes
* <name> is the name of the member being printed
*
* Float: Ignored
*
* Pointer: ' <format> <offset> ptr-field <name>
* <format> defaults to .x
* <offset> is in bytes
* <name> is the name of the member being printed
*
* Array:
* Arrays have a content-type-specific prefix, followed by an array
* suffix. The resulting line looks like this if the array contents
* type is an integer, a pointer, or an enum:
*
* ' <fldc> ' <fmt> <sz> <elsz> <off> array-field <name>
*
* The following is printed for array contents that are arrays:
*
* ' noop ' .x <sz> <elsz> <off> array-field <name>
*
* The following is printed for array contents that are structs:
*
* ' noop ' <fmt> <sz> <elsz> <off> array-field <name>
*
* <fldc> is "c@", "w@", "l@", or "x@", depending on whether array
* elements are 8, 16, 32 or 64 bits wide.
* <fmt> defaults to ".x"
* <sz> is the size of the array, in bytes
* <elsz> is the size of the array elements
* <off> is the member offset, in bytes
* <name> is the nam eof the member being printed
*
* Struct/Union: ' <format> <offset> struct-field <name>
* <format> defaults to ".x"
* <offset> is the member offset, in bytes
* <name> is the name of the member being printed
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ctf_headers.h"
#include "forth.h"
#include "list.h"
#include "memory.h"
static ctf_id_t fth_str_curtid;
static list_t *fth_str_curmems;
/*
* Node type for the member-storage list (fth_str_curmems) built by
* fth_struct_members()
*/
typedef struct fth_str_mem {
char *fsm_memname;
char *fsm_format;
ctf_id_t fsm_tid;
ulong_t fsm_off;
} fth_str_mem_t;
typedef struct fth_struct_members_data {
char *fsmd_strname;
char *fsmd_memfilter;
char *fsmd_format;
int fsmd_matched;
} fth_struct_members_data_t;
static int fth_print_member(fth_str_mem_t *, int);
/* Comparison routined used to insert members into the fth_str_curmems list */
static int
fth_struct_memcmp(void *m1, void *m2)
{
fth_str_mem_t *mem1 = m1, *mem2 = m2;
if (mem1->fsm_off < mem2->fsm_off)
return (1);
else if (mem1->fsm_off > mem2->fsm_off)
return (-1);
else
return (0);
}
static void
fth_free_str_mem(fth_str_mem_t *mem)
{
free(mem->fsm_memname);
if (mem->fsm_format)
free(mem->fsm_format);
free(mem);
}
static int
fth_struct_header(ctf_id_t tid)
{
ssize_t sz;
fth_str_curtid = tid;
fth_str_curmems = NULL;
if ((sz = ctf_type_size(ctf, fth_str_curtid)) == CTF_ERR)
return (parse_warn("Can't get size for %s", fth_curtype));
(void) fprintf(out, "\n");
(void) fprintf(out, "vocabulary %s-words\n", fth_curtype);
(void) fprintf(out, "h# %x constant %s-sz\n", sz, fth_curtype);
(void) fprintf(out, "%x ' %s-words c-struct .%s\n", sz, fth_curtype,
fth_curtype);
(void) fprintf(out, "also %s-words definitions\n\n", fth_curtype);
return (0);
}
/* Print the array prefix for integer and pointer members */
static int
fth_print_level(uint_t bits, char *format)
{
if ((bits & (bits - 1)) != 0 ||(bits % 8) != 0 || bits > 64) {
return (parse_warn("Unexpected bit size %d in %s",
bits, fth_curtype));
}
(void) fprintf(out, "' %c@ ' %s", " cw l x"[bits / 8], format);
return (0);
}
/*
* Return the format to be used to print the member. If one of the builtin
* formats "d" or "x" were specified, return ".d" or ".x", respectively.
* Otherwise, use the user-provided format as is, or use the default if none
* was provided.
*/
static char *
fth_convert_format(char *format, char *def)
{
static char dot[3] = ".";
if (format == NULL)
return (def);
else if (strlen(format) == 1) {
dot[1] = *format;
return (dot);
} else
return (format);
}
static int
fth_print_integer(const char *memname, ulong_t off, uint_t bits, char *format,
int level)
{
format = fth_convert_format(format, ".x");
if (bits > 64) {
return (parse_warn("%s.%s is too large (>8 bytes)",
fth_curtype, memname));
}
if (level != 0)
return (fth_print_level(bits, format));
if ((bits % NBBY) != 0 || (bits & (bits - 1)) != 0) {
/* bit field */
uint_t offset, shift, mask;
offset = (off / 32) * 4;
shift = 32 - ((off % 32) + bits);
mask = ((1 << bits) - 1) << shift;
(void) fprintf(out, "' %s %x %x %x bits-field %s\n",
format, shift, mask, offset, memname);
} else {
char *type[] = {
NULL, "byte", "short", NULL, "long",
NULL, NULL, NULL, "ext"
};
(void) fprintf(out, "' %s %lx %s-field %s\n", format, off / 8,
type[bits / 8], memname);
}
return (0);
}
static int
fth_print_pointer(const char *memname, ulong_t off, uint_t bits, char *format,
int level)
{
format = fth_convert_format(format, ".x");
if (level != 0)
return (fth_print_level(bits, format));
(void) fprintf(out, "' %s %lx ptr-field %s\n", format, off / 8,
memname);
return (0);
}
static int
fth_print_struct(char *memname, ulong_t off, char *format,
int level)
{
format = fth_convert_format(format, ".x");
if (level != 0)
(void) fprintf(out, "' noop ' %s", format);
else {
(void) fprintf(out, "' %s %lx struct-field %s\n", format,
off / 8, memname);
}
return (0);
}
static int
fth_print_enum(char *memname, ulong_t off, char *format,
int level)
{
format = fth_convert_format(format, ".d");
if (level != 0)
(void) fprintf(out, "' l@ ' %s", format);
else {
(void) fprintf(out, "' %s %lx long-field %s\n", format, off / 8,
memname);
}
return (0);
}
static int
fth_print_array(char *memname, ctf_id_t tid, ulong_t off, ssize_t sz,
char *format, int level)
{
if (level != 0)
(void) fprintf(out, "' noop ' .x");
else {
fth_str_mem_t mem;
ctf_arinfo_t ar;
/*
* print the prefix for the array contents type, then print
* the array macro
*/
if (ctf_array_info(ctf, tid, &ar) == CTF_ERR) {
return (parse_warn("Can't read array in %s.%s",
fth_curtype, memname));
}
mem.fsm_memname = memname;
mem.fsm_format = format;
mem.fsm_tid = ar.ctr_contents;
mem.fsm_off = off;
if (fth_print_member(&mem, level + 1) < 0)
return (-1);
(void) fprintf(out, " %x %x %lx array-field %s\n", sz,
(sz / ar.ctr_nelems), off / 8, memname);
}
return (0);
}
/* dump a structure or union member */
static int
fth_print_member(fth_str_mem_t *mem, int level)
{
ctf_encoding_t e;
ctf_id_t tid;
int kind;
ssize_t sz;
if ((tid = ctf_type_resolve(ctf, mem->fsm_tid)) == CTF_ERR) {
return (parse_warn("Can't resolve %s.%s", fth_curtype,
mem->fsm_memname));
}
if ((kind = ctf_type_kind(ctf, tid)) == CTF_ERR) {
return (parse_warn("Can't get kind for %s.%s",
fth_curtype, mem->fsm_memname));
}
if ((sz = ctf_type_size(ctf, tid)) == CTF_ERR) {
return (parse_warn("Can't get size for %s.%s",
fth_curtype, mem->fsm_memname));
}
switch (kind) {
case CTF_K_INTEGER:
if (ctf_type_encoding(ctf, tid, &e) == CTF_ERR)
return (parse_warn("Can't get encoding for %ld", tid));
return (fth_print_integer(mem->fsm_memname, mem->fsm_off,
e.cte_bits, mem->fsm_format, level));
case CTF_K_FLOAT:
(void) parse_warn("Ignoring floating point member %s.%s",
fth_curtype, mem->fsm_memname);
return (0);
case CTF_K_POINTER:
return (fth_print_pointer(mem->fsm_memname, mem->fsm_off,
sz * 8, mem->fsm_format, level));
case CTF_K_ARRAY:
return (fth_print_array(mem->fsm_memname, tid, mem->fsm_off, sz,
mem->fsm_format, level));
case CTF_K_STRUCT:
case CTF_K_UNION:
return (fth_print_struct(mem->fsm_memname, mem->fsm_off,
mem->fsm_format, level));
case CTF_K_ENUM:
return (fth_print_enum(mem->fsm_memname, mem->fsm_off,
mem->fsm_format, level));
case CTF_K_FORWARD:
return (parse_warn("Type %ld in %s.%s is undefined", tid,
fth_curtype, mem->fsm_memname));
default:
return (parse_warn("Unexpected kind %d for %s.%s", kind,
fth_curtype, mem->fsm_memname));
}
}
/*
* Add a member to list of members to be printed (fth_str_curmems). If
* fsmd_memfilter is non-null, only add this member if its name matches that
* in the filter.
*/
static int
fth_struct_members_cb(const char *memname, ctf_id_t tid, ulong_t off, void *arg)
{
fth_struct_members_data_t *fsmd = arg;
fth_str_mem_t *mem;
if (fsmd->fsmd_memfilter != NULL && strcmp(fsmd->fsmd_memfilter,
memname) != 0)
return (0);
fsmd->fsmd_matched = 1;
mem = xcalloc(sizeof (fth_str_mem_t));
mem->fsm_memname = xstrdup(memname);
if (fsmd->fsmd_format)
mem->fsm_format = xstrdup(fsmd->fsmd_format);
mem->fsm_tid = tid;
mem->fsm_off = off;
slist_add(&fth_str_curmems, mem, fth_struct_memcmp);
return (0);
}
/*
* If memfilter is non-null, iterate through the members of this type, causing
* every member to be added to the list. Otherwise, use the iterator and
* the callback to add only the specified member.
*/
static int
fth_struct_members(char *memfilter, char *format)
{
fth_struct_members_data_t fsmd;
fsmd.fsmd_strname = fth_curtype;
fsmd.fsmd_memfilter = memfilter;
fsmd.fsmd_format = format;
fsmd.fsmd_matched = 0;
if (ctf_member_iter(ctf, fth_str_curtid, fth_struct_members_cb,
&fsmd) != 0)
return (-1);
if (memfilter != NULL && fsmd.fsmd_matched == 0) {
return (parse_warn("Invalid member %s.%s", fth_curtype,
memfilter));
}
return (0);
}
static int
fth_struct_trailer(void)
{
if (list_count(fth_str_curmems) == 0) {
if (fth_struct_members(NULL, NULL) < 0)
return (-1);
}
while (!list_empty(fth_str_curmems)) {
fth_str_mem_t *mem = list_remove(&fth_str_curmems,
list_first(fth_str_curmems), NULL, NULL);
if (fth_print_member(mem, 0) < 0)
return (-1);
fth_free_str_mem(mem);
}
(void) fprintf(out, "\n");
(void) fprintf(out, "kdbg-words definitions\n");
(void) fprintf(out, "previous\n");
(void) fprintf(out, "\n");
(void) fprintf(out, "\\ end %s section\n", fth_curtype);
(void) fprintf(out, "\n");
return (0);
}
fth_type_ops_t fth_struct_ops = {
fth_struct_header,
fth_struct_members,
fth_struct_trailer
};
|