Print this page
4474 DTrace Userland CTF Support
4475 DTrace userland Keyword
4476 DTrace tests should be better citizens
4479 pid provider types
4480 dof emulation missing checks
Reviewed by: Bryan Cantrill <bryan@joyent.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdtrace/common/dt_print.c
+++ new/usr/src/lib/libdtrace/common/dt_print.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25 /*
26 26 * Copyright (c) 2011 by Delphix. All rights reserved.
27 27 */
28 28 /*
29 29 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
30 30 */
31 31
32 32 /*
33 33 * DTrace print() action
34 34 *
35 35 * This file contains the post-processing logic for the print() action. The
36 36 * print action behaves identically to trace() in that it generates a
37 37 * DTRACEACT_DIFEXPR action, but the action argument field refers to a CTF type
38 38 * string stored in the DOF string table (similar to printf formats). We
39 39 * take the result of the trace action and post-process it in the fashion of
40 40 * MDB's ::print dcmd.
41 41 *
42 42 * This implementation differs from MDB's in the following ways:
43 43 *
44 44 * - We do not expose any options or flags. The behavior of print() is
45 45 * equivalent to "::print -tn".
46 46 *
47 47 * - MDB will display "holes" in structures (unused padding between
48 48 * members).
49 49 *
50 50 * - When printing arrays of structures, MDB will leave a trailing ','
51 51 * after the last element.
52 52 *
53 53 * - MDB will print time_t types as date and time.
54 54 *
55 55 * - MDB will detect when an enum is actually the OR of several flags,
56 56 * and print it out with the constituent flags separated.
57 57 *
58 58 * - For large arrays, MDB will print the first few members and then
59 59 * print a "..." continuation line.
60 60 *
61 61 * - MDB will break and wrap arrays at 80 columns.
62 62 *
63 63 * - MDB prints out floats and doubles by hand, as it must run in kmdb
64 64 * context. We're able to leverage the printf() format strings,
65 65 * but the result is a slightly different format.
66 66 */
67 67
68 68 #include <sys/sysmacros.h>
69 69 #include <strings.h>
70 70 #include <stdlib.h>
71 71 #include <alloca.h>
72 72 #include <assert.h>
73 73 #include <ctype.h>
74 74 #include <errno.h>
75 75 #include <limits.h>
76 76 #include <sys/socket.h>
77 77 #include <netdb.h>
78 78 #include <netinet/in.h>
79 79 #include <arpa/inet.h>
80 80 #include <arpa/nameser.h>
81 81
82 82 #include <dt_module.h>
83 83 #include <dt_printf.h>
84 84 #include <dt_string.h>
85 85 #include <dt_impl.h>
86 86
87 87 /* determines whether the given integer CTF encoding is a character */
88 88 #define CTF_IS_CHAR(e) \
89 89 (((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
90 90 (CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
91 91 /* determines whether the given CTF kind is a struct or union */
92 92 #define CTF_IS_STRUCTLIKE(k) \
93 93 ((k) == CTF_K_STRUCT || (k) == CTF_K_UNION)
94 94
95 95 /*
96 96 * Print structure passed down recursively through printing algorithm.
97 97 */
98 98 typedef struct dt_printarg {
99 99 dtrace_hdl_t *pa_dtp; /* libdtrace handle */
100 100 caddr_t pa_addr; /* base address of trace data */
101 101 ctf_file_t *pa_ctfp; /* CTF container */
102 102 int pa_depth; /* member depth */
103 103 int pa_nest; /* nested array depth */
104 104 FILE *pa_file; /* output file */
105 105 } dt_printarg_t;
106 106
107 107 static int dt_print_member(const char *, ctf_id_t, ulong_t, int, void *);
108 108
109 109 /*
110 110 * Safe version of ctf_type_name() that will fall back to just "<ctfid>" if it
111 111 * can't resolve the type.
112 112 */
113 113 static void
114 114 dt_print_type_name(ctf_file_t *ctfp, ctf_id_t id, char *buf, size_t buflen)
115 115 {
116 116 if (ctf_type_name(ctfp, id, buf, buflen) == NULL)
117 117 (void) snprintf(buf, buflen, "<%ld>", id);
118 118 }
119 119
120 120 /*
121 121 * Print any necessary trailing braces for structures or unions. We don't get
122 122 * invoked when a struct or union ends, so we infer the need to print braces
123 123 * based on the depth the last time we printed something and the new depth.
124 124 */
125 125 static void
126 126 dt_print_trailing_braces(dt_printarg_t *pap, int depth)
127 127 {
128 128 int d;
129 129
130 130 for (d = pap->pa_depth; d > depth; d--) {
131 131 (void) fprintf(pap->pa_file, "%*s}%s",
132 132 (d + pap->pa_nest - 1) * 4, "",
133 133 d == depth + 1 ? "" : "\n");
134 134 }
135 135 }
136 136
137 137 /*
138 138 * Print the appropriate amount of indentation given the current depth and
139 139 * array nesting.
140 140 */
141 141 static void
142 142 dt_print_indent(dt_printarg_t *pap)
143 143 {
144 144 (void) fprintf(pap->pa_file, "%*s",
145 145 (pap->pa_depth + pap->pa_nest) * 4, "");
146 146 }
147 147
148 148 /*
149 149 * Print a bitfield. It's worth noting that the D compiler support for
150 150 * bitfields is currently broken; printing "D`user_desc_t" (pulled in by the
151 151 * various D provider files) will produce incorrect results compared to
152 152 * "genunix`user_desc_t".
153 153 */
154 154 static void
155 155 print_bitfield(dt_printarg_t *pap, ulong_t off, ctf_encoding_t *ep)
156 156 {
157 157 FILE *fp = pap->pa_file;
158 158 caddr_t addr = pap->pa_addr + off / NBBY;
159 159 uint64_t mask = (1ULL << ep->cte_bits) - 1;
160 160 uint64_t value = 0;
161 161 size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
162 162 uint8_t *buf = (uint8_t *)&value;
163 163 uint8_t shift;
164 164
165 165 /*
166 166 * On big-endian machines, we need to adjust the buf pointer to refer
167 167 * to the lowest 'size' bytes in 'value', and we need to shift based on
168 168 * the offset from the end of the data, not the offset of the start.
169 169 */
170 170 #ifdef _BIG_ENDIAN
171 171 buf += sizeof (value) - size;
172 172 off += ep->cte_bits;
173 173 #endif
174 174 bcopy(addr, buf, size);
175 175 shift = off % NBBY;
176 176
177 177 /*
178 178 * Offsets are counted from opposite ends on little- and
179 179 * big-endian machines.
180 180 */
181 181 #ifdef _BIG_ENDIAN
182 182 shift = NBBY - shift;
183 183 #endif
184 184
185 185 /*
186 186 * If the bits we want do not begin on a byte boundary, shift the data
187 187 * right so that the value is in the lowest 'cte_bits' of 'value'.
188 188 */
189 189 if (off % NBBY != 0)
190 190 value >>= shift;
191 191 value &= mask;
192 192
193 193 (void) fprintf(fp, "%#llx", (u_longlong_t)value);
194 194 }
195 195
196 196 /*
197 197 * Dump the contents of memory as a fixed-size integer in hex.
198 198 */
199 199 static void
200 200 dt_print_hex(FILE *fp, caddr_t addr, size_t size)
201 201 {
202 202 switch (size) {
203 203 case sizeof (uint8_t):
204 204 (void) fprintf(fp, "%#x", *(uint8_t *)addr);
205 205 break;
206 206 case sizeof (uint16_t):
207 207 /* LINTED - alignment */
208 208 (void) fprintf(fp, "%#x", *(uint16_t *)addr);
209 209 break;
210 210 case sizeof (uint32_t):
211 211 /* LINTED - alignment */
212 212 (void) fprintf(fp, "%#x", *(uint32_t *)addr);
213 213 break;
214 214 case sizeof (uint64_t):
215 215 (void) fprintf(fp, "%#llx",
216 216 /* LINTED - alignment */
217 217 (unsigned long long)*(uint64_t *)addr);
218 218 break;
219 219 default:
220 220 (void) fprintf(fp, "<invalid size %u>", (uint_t)size);
221 221 }
222 222 }
223 223
224 224 /*
225 225 * Print an integer type. Before dumping the contents via dt_print_hex(), we
226 226 * first check the encoding to see if it's part of a bitfield or a character.
227 227 */
228 228 static void
229 229 dt_print_int(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
230 230 {
231 231 FILE *fp = pap->pa_file;
232 232 ctf_file_t *ctfp = pap->pa_ctfp;
233 233 ctf_encoding_t e;
234 234 size_t size;
235 235 caddr_t addr = pap->pa_addr + off / NBBY;
236 236
237 237 if (ctf_type_encoding(ctfp, base, &e) == CTF_ERR) {
238 238 (void) fprintf(fp, "<unknown encoding>");
239 239 return;
240 240 }
241 241
242 242 /*
243 243 * This comes from MDB - it's not clear under what circumstances this
244 244 * would be found.
245 245 */
246 246 if (e.cte_format & CTF_INT_VARARGS) {
247 247 (void) fprintf(fp, "...");
248 248 return;
249 249 }
250 250
251 251 /*
252 252 * We print this as a bitfield if the bit encoding indicates it's not
253 253 * an even power of two byte size, or is larger than 8 bytes.
254 254 */
255 255 size = e.cte_bits / NBBY;
256 256 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)) != 0) {
257 257 print_bitfield(pap, off, &e);
258 258 return;
259 259 }
260 260
261 261 /*
262 262 * If this is a character, print it out as such.
263 263 */
264 264 if (CTF_IS_CHAR(e)) {
265 265 char c = *(char *)addr;
266 266 if (isprint(c))
267 267 (void) fprintf(fp, "'%c'", c);
268 268 else if (c == 0)
269 269 (void) fprintf(fp, "'\\0'");
270 270 else
271 271 (void) fprintf(fp, "'\\%03o'", c);
272 272 return;
273 273 }
274 274
275 275 dt_print_hex(fp, addr, size);
276 276 }
277 277
278 278 /*
279 279 * Print a floating point (float, double, long double) value.
280 280 */
281 281 /* ARGSUSED */
282 282 static void
283 283 dt_print_float(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
284 284 {
285 285 FILE *fp = pap->pa_file;
286 286 ctf_file_t *ctfp = pap->pa_ctfp;
287 287 ctf_encoding_t e;
288 288 caddr_t addr = pap->pa_addr + off / NBBY;
289 289
290 290 if (ctf_type_encoding(ctfp, base, &e) == 0) {
291 291 if (e.cte_format == CTF_FP_SINGLE &&
292 292 e.cte_bits == sizeof (float) * NBBY) {
293 293 /* LINTED - alignment */
294 294 (void) fprintf(fp, "%+.7e", *((float *)addr));
295 295 } else if (e.cte_format == CTF_FP_DOUBLE &&
296 296 e.cte_bits == sizeof (double) * NBBY) {
297 297 /* LINTED - alignment */
298 298 (void) fprintf(fp, "%+.7e", *((double *)addr));
299 299 } else if (e.cte_format == CTF_FP_LDOUBLE &&
300 300 e.cte_bits == sizeof (long double) * NBBY) {
301 301 /* LINTED - alignment */
302 302 (void) fprintf(fp, "%+.16LE", *((long double *)addr));
303 303 } else {
304 304 (void) fprintf(fp, "<unknown encoding>");
305 305 }
306 306 }
307 307 }
308 308
309 309 /*
310 310 * A pointer is generally printed as a fixed-size integer. If we have a
311 311 * function pointer, we try to look up its name.
312 312 */
313 313 static void
314 314 dt_print_ptr(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
315 315 {
316 316 FILE *fp = pap->pa_file;
317 317 ctf_file_t *ctfp = pap->pa_ctfp;
318 318 caddr_t addr = pap->pa_addr + off / NBBY;
319 319 size_t size = ctf_type_size(ctfp, base);
320 320 ctf_id_t bid = ctf_type_reference(ctfp, base);
321 321 uint64_t pc;
322 322 dtrace_syminfo_t dts;
323 323 GElf_Sym sym;
324 324
325 325 if (bid == CTF_ERR || ctf_type_kind(ctfp, bid) != CTF_K_FUNCTION) {
326 326 dt_print_hex(fp, addr, size);
327 327 } else {
328 328 /* LINTED - alignment */
329 329 pc = *((uint64_t *)addr);
330 330 if (dtrace_lookup_by_addr(pap->pa_dtp, pc, &sym, &dts) != 0) {
331 331 dt_print_hex(fp, addr, size);
332 332 } else {
333 333 (void) fprintf(fp, "%s`%s", dts.dts_object,
334 334 dts.dts_name);
335 335 }
336 336 }
337 337 }
338 338
339 339 /*
340 340 * Print out an array. This is somewhat complex, as we must manually visit
341 341 * each member, and recursively invoke ctf_type_visit() for each member. If
342 342 * the members are non-structs, then we print them out directly:
343 343 *
344 344 * [ 0x14, 0x2e, 0 ]
345 345 *
346 346 * If they are structs, then we print out the necessary leading and trailing
347 347 * braces, to end up with:
348 348 *
349 349 * [
350 350 * type {
351 351 * ...
352 352 * },
353 353 * type {
354 354 * ...
355 355 * }
356 356 * ]
357 357 *
358 358 * We also use a heuristic to detect whether the array looks like a character
359 359 * array. If the encoding indicates it's a character, and we have all
360 360 * printable characters followed by a null byte, then we display it as a
361 361 * string:
362 362 *
363 363 * [ "string" ]
364 364 */
365 365 static void
366 366 dt_print_array(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
367 367 {
368 368 FILE *fp = pap->pa_file;
369 369 ctf_file_t *ctfp = pap->pa_ctfp;
370 370 caddr_t addr = pap->pa_addr + off / NBBY;
371 371 ctf_arinfo_t car;
372 372 ssize_t eltsize;
373 373 ctf_encoding_t e;
374 374 int i;
375 375 boolean_t isstring;
376 376 int kind;
377 377 ctf_id_t rtype;
378 378
379 379 if (ctf_array_info(ctfp, base, &car) == CTF_ERR) {
380 380 (void) fprintf(fp, "0x%p", (void *)addr);
381 381 return;
382 382 }
383 383
384 384 if ((eltsize = ctf_type_size(ctfp, car.ctr_contents)) < 0 ||
385 385 (rtype = ctf_type_resolve(ctfp, car.ctr_contents)) == CTF_ERR ||
386 386 (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR) {
387 387 (void) fprintf(fp, "<invalid type %lu>", car.ctr_contents);
388 388 return;
389 389 }
390 390
391 391 /* see if this looks like a string */
392 392 isstring = B_FALSE;
393 393 if (kind == CTF_K_INTEGER &&
394 394 ctf_type_encoding(ctfp, rtype, &e) != CTF_ERR && CTF_IS_CHAR(e)) {
395 395 char c;
396 396 for (i = 0; i < car.ctr_nelems; i++) {
397 397 c = *((char *)addr + eltsize * i);
398 398 if (!isprint(c) || c == '\0')
399 399 break;
400 400 }
401 401
402 402 if (i != car.ctr_nelems && c == '\0')
403 403 isstring = B_TRUE;
404 404 }
405 405
406 406 /*
407 407 * As a slight aesthetic optimization, if we are a top-level type, then
408 408 * don't bother printing out the brackets. This lets print("foo") look
409 409 * like:
410 410 *
411 411 * string "foo"
412 412 *
413 413 * As D will internally represent this as a char[256] array.
414 414 */
415 415 if (!isstring || pap->pa_depth != 0)
416 416 (void) fprintf(fp, "[ ");
417 417
418 418 if (isstring)
419 419 (void) fprintf(fp, "\"");
420 420
421 421 for (i = 0; i < car.ctr_nelems; i++) {
422 422 if (isstring) {
423 423 char c = *((char *)addr + eltsize * i);
424 424 if (c == '\0')
425 425 break;
426 426 (void) fprintf(fp, "%c", c);
427 427 } else {
428 428 /*
429 429 * Recursively invoke ctf_type_visit() on each member.
430 430 * We setup a new printarg struct with 'pa_nest' set to
431 431 * indicate that we are within a nested array.
432 432 */
433 433 dt_printarg_t pa = *pap;
434 434 pa.pa_nest += pap->pa_depth + 1;
435 435 pa.pa_depth = 0;
436 436 pa.pa_addr = addr + eltsize * i;
437 437 (void) ctf_type_visit(ctfp, car.ctr_contents,
438 438 dt_print_member, &pa);
439 439
440 440 dt_print_trailing_braces(&pa, 0);
441 441 if (i != car.ctr_nelems - 1)
442 442 (void) fprintf(fp, ", ");
443 443 else if (CTF_IS_STRUCTLIKE(kind))
444 444 (void) fprintf(fp, "\n");
445 445 }
446 446 }
447 447
448 448 if (isstring)
449 449 (void) fprintf(fp, "\"");
450 450
451 451 if (!isstring || pap->pa_depth != 0) {
452 452 if (CTF_IS_STRUCTLIKE(kind))
453 453 dt_print_indent(pap);
454 454 else
455 455 (void) fprintf(fp, " ");
456 456 (void) fprintf(fp, "]");
457 457 }
458 458 }
459 459
460 460 /*
461 461 * This isued by both structs and unions to print the leading brace.
462 462 */
463 463 /* ARGSUSED */
464 464 static void
465 465 dt_print_structlike(ctf_id_t id, ulong_t off, dt_printarg_t *pap)
466 466 {
467 467 (void) fprintf(pap->pa_file, "{");
468 468 }
469 469
470 470 /*
471 471 * For enums, we try to print the enum name, and fall back to the value if it
472 472 * can't be determined. We do not do any fancy flag processing like mdb.
473 473 */
474 474 /* ARGSUSED */
475 475 static void
476 476 dt_print_enum(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
477 477 {
478 478 FILE *fp = pap->pa_file;
479 479 ctf_file_t *ctfp = pap->pa_ctfp;
480 480 const char *ename;
481 481 ssize_t size;
482 482 caddr_t addr = pap->pa_addr + off / NBBY;
483 483 int value = 0;
484 484
485 485 /*
486 486 * The C standard says that an enum will be at most the sizeof (int).
487 487 * But if all the values are less than that, the compiler can use a
488 488 * smaller size. Thanks standards.
489 489 */
490 490 size = ctf_type_size(ctfp, base);
491 491 switch (size) {
492 492 case sizeof (uint8_t):
493 493 value = *(uint8_t *)addr;
494 494 break;
495 495 case sizeof (uint16_t):
496 496 /* LINTED - alignment */
497 497 value = *(uint16_t *)addr;
498 498 break;
499 499 case sizeof (int32_t):
500 500 /* LINTED - alignment */
501 501 value = *(int32_t *)addr;
502 502 break;
503 503 default:
504 504 (void) fprintf(fp, "<invalid enum size %u>", (uint_t)size);
505 505 return;
506 506 }
507 507
508 508 if ((ename = ctf_enum_name(ctfp, base, value)) != NULL)
509 509 (void) fprintf(fp, "%s", ename);
510 510 else
511 511 (void) fprintf(fp, "%d", value);
512 512 }
513 513
514 514 /*
515 515 * Forward declaration. There's not much to do here without the complete
516 516 * type information, so just print out this fact and drive on.
517 517 */
518 518 /* ARGSUSED */
519 519 static void
520 520 dt_print_tag(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
521 521 {
522 522 (void) fprintf(pap->pa_file, "<forward decl>");
523 523 }
524 524
525 525 typedef void dt_printarg_f(ctf_id_t, ulong_t, dt_printarg_t *);
526 526
527 527 static dt_printarg_f *const dt_printfuncs[] = {
528 528 dt_print_int, /* CTF_K_INTEGER */
529 529 dt_print_float, /* CTF_K_FLOAT */
530 530 dt_print_ptr, /* CTF_K_POINTER */
531 531 dt_print_array, /* CTF_K_ARRAY */
532 532 dt_print_ptr, /* CTF_K_FUNCTION */
533 533 dt_print_structlike, /* CTF_K_STRUCT */
534 534 dt_print_structlike, /* CTF_K_UNION */
535 535 dt_print_enum, /* CTF_K_ENUM */
536 536 dt_print_tag /* CTF_K_FORWARD */
537 537 };
538 538
539 539 /*
540 540 * Print one member of a structure. This callback is invoked from
541 541 * ctf_type_visit() recursively.
542 542 */
543 543 static int
544 544 dt_print_member(const char *name, ctf_id_t id, ulong_t off, int depth,
545 545 void *data)
546 546 {
547 547 char type[DT_TYPE_NAMELEN];
548 548 int kind;
549 549 dt_printarg_t *pap = data;
550 550 FILE *fp = pap->pa_file;
551 551 ctf_file_t *ctfp = pap->pa_ctfp;
552 552 boolean_t arraymember;
553 553 boolean_t brief;
554 554 ctf_encoding_t e;
555 555 ctf_id_t rtype;
556 556
557 557 dt_print_trailing_braces(pap, depth);
558 558 /*
559 559 * dt_print_trailing_braces() doesn't include the trailing newline; add
560 560 * it here if necessary.
561 561 */
562 562 if (depth < pap->pa_depth)
563 563 (void) fprintf(fp, "\n");
564 564 pap->pa_depth = depth;
565 565
566 566 if ((rtype = ctf_type_resolve(ctfp, id)) == CTF_ERR ||
567 567 (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR ||
568 568 kind < CTF_K_INTEGER || kind > CTF_K_FORWARD) {
569 569 dt_print_indent(pap);
570 570 (void) fprintf(fp, "%s = <invalid type %lu>", name, id);
571 571 return (0);
572 572 }
573 573
574 574 dt_print_type_name(ctfp, id, type, sizeof (type));
575 575
576 576 arraymember = (pap->pa_nest != 0 && depth == 0);
577 577 brief = (arraymember && !CTF_IS_STRUCTLIKE(kind));
578 578
579 579 if (!brief) {
580 580 /*
581 581 * If this is a direct array member and a struct (otherwise
582 582 * brief would be true), then print a trailing newline, as the
583 583 * array printing code doesn't include it because it might be a
584 584 * simple type.
585 585 */
586 586 if (arraymember)
587 587 (void) fprintf(fp, "\n");
588 588 dt_print_indent(pap);
589 589
590 590 /* always print the type */
591 591 (void) fprintf(fp, "%s", type);
592 592 if (name[0] != '\0') {
593 593 /*
594 594 * For aesthetics, we don't include a space between the
595 595 * type name and member name if the type is a pointer.
596 596 * This will give us "void *foo =" instead of "void *
597 597 * foo =". Unions also have the odd behavior that the
598 598 * type name is returned as "union ", with a trailing
599 599 * space, so we also avoid printing a space if the type
600 600 * name already ends with a space.
601 601 */
602 602 if (type[strlen(type) - 1] != '*' &&
603 603 type[strlen(type) -1] != ' ') {
604 604 (void) fprintf(fp, " ");
605 605 }
606 606 (void) fprintf(fp, "%s", name);
607 607
608 608 /*
609 609 * If this looks like a bitfield, or is an integer not
610 610 * aligned on a byte boundary, print the number of
611 611 * bits after the name.
612 612 */
613 613 if (kind == CTF_K_INTEGER &&
614 614 ctf_type_encoding(ctfp, id, &e) == 0) {
615 615 ulong_t bits = e.cte_bits;
616 616 ulong_t size = bits / NBBY;
617 617
618 618 if (bits % NBBY != 0 ||
619 619 off % NBBY != 0 ||
620 620 size > 8 ||
621 621 size != ctf_type_size(ctfp, id)) {
622 622 (void) fprintf(fp, " :%lu", bits);
623 623 }
624 624 }
625 625
626 626 (void) fprintf(fp, " =");
627 627 }
628 628 (void) fprintf(fp, " ");
629 629 }
630 630
631 631 dt_printfuncs[kind - 1](rtype, off, pap);
632 632
633 633 /* direct simple array members are not separated by newlines */
634 634 if (!brief)
635 635 (void) fprintf(fp, "\n");
636 636
637 637 return (0);
638 638 }
639 639
640 640 /*
641 641 * Main print function invoked by dt_consume_cpu().
↓ open down ↓ |
641 lines elided |
↑ open up ↑ |
642 642 */
643 643 int
644 644 dtrace_print(dtrace_hdl_t *dtp, FILE *fp, const char *typename,
645 645 caddr_t addr, size_t len)
646 646 {
647 647 const char *s;
648 648 char *object;
649 649 dt_printarg_t pa;
650 650 ctf_id_t id;
651 651 dt_module_t *dmp;
652 + ctf_file_t *ctfp;
653 + int libid;
652 654
653 655 /*
654 656 * Split the fully-qualified type ID (module`id). This should
655 657 * always be the format, but if for some reason we don't find the
656 658 * expected value, return 0 to fall back to the generic trace()
657 - * behavior.
659 + * behavior. In the case of userland CTF modules this will actually be
660 + * of the format (module`lib`id). This is due to the fact that those
661 + * modules have multiple CTF containers which `lib` identifies.
658 662 */
659 663 for (s = typename; *s != '\0' && *s != '`'; s++)
660 664 ;
661 665
662 666 if (*s != '`')
663 667 return (0);
664 668
665 669 object = alloca(s - typename + 1);
666 670 bcopy(typename, object, s - typename);
667 671 object[s - typename] = '\0';
672 + dmp = dt_module_lookup_by_name(dtp, object);
673 + if (dmp == NULL)
674 + return (0);
675 +
676 + if (dmp->dm_pid != 0) {
677 + libid = atoi(s + 1);
678 + s = strchr(s + 1, '`');
679 + if (s == NULL || libid > dmp->dm_nctflibs)
680 + return (0);
681 + ctfp = dmp->dm_libctfp[libid];
682 + } else {
683 + ctfp = dt_module_getctf(dtp, dmp);
684 + }
685 +
668 686 id = atoi(s + 1);
669 687
670 688 /*
671 689 * Try to get the CTF kind for this id. If something has gone horribly
672 690 * wrong and we can't resolve the ID, bail out and let trace() do the
673 691 * work.
674 692 */
675 - dmp = dt_module_lookup_by_name(dtp, object);
676 - if (dmp == NULL || ctf_type_kind(dt_module_getctf(dtp, dmp),
677 - id) == CTF_ERR) {
693 + if (ctfp == NULL || ctf_type_kind(ctfp, id) == CTF_ERR)
678 694 return (0);
679 - }
680 695
681 696 /* setup the print structure and kick off the main print routine */
682 697 pa.pa_dtp = dtp;
683 698 pa.pa_addr = addr;
684 - pa.pa_ctfp = dt_module_getctf(dtp, dmp);
699 + pa.pa_ctfp = ctfp;
685 700 pa.pa_nest = 0;
686 701 pa.pa_depth = 0;
687 702 pa.pa_file = fp;
688 703 (void) ctf_type_visit(pa.pa_ctfp, id, dt_print_member, &pa);
689 704
690 705 dt_print_trailing_braces(&pa, 0);
691 706
692 707 return (len);
693 708 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX