4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2011 by Delphix. All rights reserved.
30 * DTrace print() action
32 * This file contains the post-processing logic for the print() action. The
33 * print action behaves identically to trace() in that it generates a
34 * DTRACEACT_DIFEXPR action, but the action argument field refers to a CTF type
35 * string stored in the DOF string table (similar to printf formats). We
36 * take the result of the trace action and post-process it in the fashion of
39 * This implementation differs from MDB's in the following ways:
41 * - We do not expose any options or flags. The behavior of print() is
42 * equivalent to "::print -tn".
44 * - MDB will display "holes" in structures (unused padding between
47 * - When printing arrays of structures, MDB will leave a trailing ','
48 * after the last element.
50 * - MDB will print time_t types as date and time.
52 * - MDB will detect when an enum is actually the OR of several flags,
53 * and print it out with the constituent flags separated.
55 * - For large arrays, MDB will print the first few members and then
56 * print a "..." continuation line.
58 * - MDB will break and wrap arrays at 80 columns.
60 * - MDB prints out floats and doubles by hand, as it must run in kmdb
61 * context. We're able to leverage the printf() format strings,
62 * but the result is a slightly different format.
65 #include <sys/sysmacros.h>
73 #include <sys/socket.h>
75 #include <netinet/in.h>
76 #include <arpa/inet.h>
77 #include <arpa/nameser.h>
79 #include <dt_module.h>
80 #include <dt_printf.h>
81 #include <dt_string.h>
84 /* determines whether the given integer CTF encoding is a character */
85 #define CTF_IS_CHAR(e) \
86 (((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
87 (CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
88 /* determines whether the given CTF kind is a struct or union */
89 #define CTF_IS_STRUCTLIKE(k) \
90 ((k) == CTF_K_STRUCT || (k) == CTF_K_UNION)
93 * Print structure passed down recursively through printing algorithm.
95 typedef struct dt_printarg {
96 caddr_t pa_addr; /* base address of trace data */
97 ctf_file_t *pa_ctfp; /* CTF container */
98 int pa_depth; /* member depth */
99 int pa_nest; /* nested array depth */
100 FILE *pa_file; /* output file */
103 static int dt_print_member(const char *, ctf_id_t, ulong_t, int, void *);
106 * Safe version of ctf_type_name() that will fall back to just "<ctfid>" if it
107 * can't resolve the type.
110 dt_print_type_name(ctf_file_t *ctfp, ctf_id_t id, char *buf, size_t buflen)
112 if (ctf_type_name(ctfp, id, buf, buflen) == NULL)
113 (void) snprintf(buf, buflen, "<%ld>", id);
117 * Print any necessary trailing braces for structures or unions. We don't get
118 * invoked when a struct or union ends, so we infer the need to print braces
119 * based on the depth the last time we printed something and the new depth.
122 dt_print_trailing_braces(dt_printarg_t *pap, int depth)
126 for (d = pap->pa_depth; d > depth; d--) {
127 (void) fprintf(pap->pa_file, "%*s}%s",
128 (d + pap->pa_nest - 1) * 4, "",
129 d == depth + 1 ? "" : "\n");
134 * Print the appropriate amount of indentation given the current depth and
138 dt_print_indent(dt_printarg_t *pap)
140 (void) fprintf(pap->pa_file, "%*s",
141 (pap->pa_depth + pap->pa_nest) * 4, "");
145 * Print a bitfield. It's worth noting that the D compiler support for
146 * bitfields is currently broken; printing "D`user_desc_t" (pulled in by the
147 * various D provider files) will produce incorrect results compared to
148 * "genunix`user_desc_t".
151 print_bitfield(dt_printarg_t *pap, ulong_t off, ctf_encoding_t *ep)
153 FILE *fp = pap->pa_file;
154 caddr_t addr = pap->pa_addr + off / NBBY;
155 uint64_t mask = (1ULL << ep->cte_bits) - 1;
157 size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
158 uint8_t *buf = (uint8_t *)&value;
162 * On big-endian machines, we need to adjust the buf pointer to refer
163 * to the lowest 'size' bytes in 'value', and we need to shift based on
164 * the offset from the end of the data, not the offset of the start.
167 buf += sizeof (value) - size;
170 bcopy(addr, buf, size);
174 * Offsets are counted from opposite ends on little- and
175 * big-endian machines.
178 shift = NBBY - shift;
182 * If the bits we want do not begin on a byte boundary, shift the data
183 * right so that the value is in the lowest 'cte_bits' of 'value'.
189 (void) fprintf(fp, "%#llx", (u_longlong_t)value);
193 * Dump the contents of memory as a fixed-size integer in hex.
196 dt_print_hex(FILE *fp, caddr_t addr, size_t size)
199 case sizeof (uint8_t):
200 (void) fprintf(fp, "%#x", *(uint8_t *)addr);
202 case sizeof (uint16_t):
203 /* LINTED - alignment */
204 (void) fprintf(fp, "%#x", *(uint16_t *)addr);
206 case sizeof (uint32_t):
207 /* LINTED - alignment */
208 (void) fprintf(fp, "%#x", *(uint32_t *)addr);
210 case sizeof (uint64_t):
211 (void) fprintf(fp, "%#llx",
212 /* LINTED - alignment */
213 (unsigned long long)*(uint64_t *)addr);
216 (void) fprintf(fp, "<invalid size %u>", (uint_t)size);
221 * Print an integer type. Before dumping the contents via dt_print_hex(), we
222 * first check the encoding to see if it's part of a bitfield or a character.
225 dt_print_int(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
227 FILE *fp = pap->pa_file;
228 ctf_file_t *ctfp = pap->pa_ctfp;
231 caddr_t addr = pap->pa_addr + off / NBBY;
233 if (ctf_type_encoding(ctfp, base, &e) == CTF_ERR) {
234 (void) fprintf(fp, "<unknown encoding>");
239 * This comes from MDB - it's not clear under what circumstances this
242 if (e.cte_format & CTF_INT_VARARGS) {
243 (void) fprintf(fp, "...");
248 * We print this as a bitfield if the bit encoding indicates it's not
249 * an even power of two byte size, or is larger than 8 bytes.
251 size = e.cte_bits / NBBY;
252 if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)) != 0) {
253 print_bitfield(pap, off, &e);
258 * If this is a character, print it out as such.
260 if (CTF_IS_CHAR(e)) {
261 char c = *(char *)addr;
263 (void) fprintf(fp, "'%c'", c);
265 (void) fprintf(fp, "'\\0'");
267 (void) fprintf(fp, "'\\%03o'", c);
271 dt_print_hex(fp, addr, size);
275 * Print a floating point (float, double, long double) value.
279 dt_print_float(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
281 FILE *fp = pap->pa_file;
282 ctf_file_t *ctfp = pap->pa_ctfp;
284 caddr_t addr = pap->pa_addr + off / NBBY;
286 if (ctf_type_encoding(ctfp, base, &e) == 0) {
287 if (e.cte_format == CTF_FP_SINGLE &&
288 e.cte_bits == sizeof (float) * NBBY) {
289 /* LINTED - alignment */
290 (void) fprintf(fp, "%+.7e", *((float *)addr));
291 } else if (e.cte_format == CTF_FP_DOUBLE &&
292 e.cte_bits == sizeof (double) * NBBY) {
293 /* LINTED - alignment */
294 (void) fprintf(fp, "%+.7e", *((double *)addr));
295 } else if (e.cte_format == CTF_FP_LDOUBLE &&
296 e.cte_bits == sizeof (long double) * NBBY) {
297 /* LINTED - alignment */
298 (void) fprintf(fp, "%+.16LE", *((long double *)addr));
300 (void) fprintf(fp, "<unknown encoding>");
306 * A pointer is printed as a fixed-size integer. This is used both for
307 * pointers and functions.
310 dt_print_ptr(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
312 FILE *fp = pap->pa_file;
313 ctf_file_t *ctfp = pap->pa_ctfp;
314 caddr_t addr = pap->pa_addr + off / NBBY;
315 size_t size = ctf_type_size(ctfp, base);
317 dt_print_hex(fp, addr, size);
321 * Print out an array. This is somewhat complex, as we must manually visit
322 * each member, and recursively invoke ctf_type_visit() for each member. If
323 * the members are non-structs, then we print them out directly:
327 * If they are structs, then we print out the necessary leading and trailing
328 * braces, to end up with:
339 * We also use a heuristic to detect whether the array looks like a character
340 * array. If the encoding indicates it's a character, and we have all
341 * printable characters followed by a null byte, then we display it as a
347 dt_print_array(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
349 FILE *fp = pap->pa_file;
350 ctf_file_t *ctfp = pap->pa_ctfp;
351 caddr_t addr = pap->pa_addr + off / NBBY;
360 if (ctf_array_info(ctfp, base, &car) == CTF_ERR) {
361 (void) fprintf(fp, "0x%p", (void *)addr);
365 if ((eltsize = ctf_type_size(ctfp, car.ctr_contents)) < 0 ||
366 (rtype = ctf_type_resolve(ctfp, car.ctr_contents)) == CTF_ERR ||
367 (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR) {
368 (void) fprintf(fp, "<invalid type %lu>", car.ctr_contents);
372 /* see if this looks like a string */
374 if (kind == CTF_K_INTEGER &&
375 ctf_type_encoding(ctfp, rtype, &e) != CTF_ERR && CTF_IS_CHAR(e)) {
377 for (i = 0; i < car.ctr_nelems; i++) {
378 c = *((char *)addr + eltsize * i);
379 if (!isprint(c) || c == '\0')
383 if (i != car.ctr_nelems && c == '\0')
388 * As a slight aesthetic optimization, if we are a top-level type, then
389 * don't bother printing out the brackets. This lets print("foo") look
394 * As D will internally represent this as a char[256] array.
396 if (!isstring || pap->pa_depth != 0)
397 (void) fprintf(fp, "[ ");
400 (void) fprintf(fp, "\"");
402 for (i = 0; i < car.ctr_nelems; i++) {
404 char c = *((char *)addr + eltsize * i);
407 (void) fprintf(fp, "%c", c);
410 * Recursively invoke ctf_type_visit() on each member.
411 * We setup a new printarg struct with 'pa_nest' set to
412 * indicate that we are within a nested array.
414 dt_printarg_t pa = *pap;
415 pa.pa_nest += pap->pa_depth + 1;
417 pa.pa_addr = addr + eltsize * i;
418 (void) ctf_type_visit(ctfp, car.ctr_contents,
419 dt_print_member, &pa);
421 dt_print_trailing_braces(&pa, 0);
422 if (i != car.ctr_nelems - 1)
423 (void) fprintf(fp, ", ");
424 else if (CTF_IS_STRUCTLIKE(kind))
425 (void) fprintf(fp, "\n");
430 (void) fprintf(fp, "\"");
432 if (!isstring || pap->pa_depth != 0) {
433 if (CTF_IS_STRUCTLIKE(kind))
434 dt_print_indent(pap);
436 (void) fprintf(fp, " ");
437 (void) fprintf(fp, "]");
442 * This isued by both structs and unions to print the leading brace.
446 dt_print_structlike(ctf_id_t id, ulong_t off, dt_printarg_t *pap)
448 (void) fprintf(pap->pa_file, "{");
452 * For enums, we try to print the enum name, and fall back to the value if it
453 * can't be determined. We do not do any fancy flag processing like mdb.
457 dt_print_enum(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
459 FILE *fp = pap->pa_file;
460 ctf_file_t *ctfp = pap->pa_ctfp;
464 if ((ename = ctf_enum_name(ctfp, base, value)) != NULL)
465 (void) fprintf(fp, "%s", ename);
467 (void) fprintf(fp, "%d", value);
471 * Forward declaration. There's not much to do here without the complete
472 * type information, so just print out this fact and drive on.
476 dt_print_tag(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
478 (void) fprintf(pap->pa_file, "<forward decl>");
481 typedef void dt_printarg_f(ctf_id_t, ulong_t, dt_printarg_t *);
483 static dt_printarg_f *const dt_printfuncs[] = {
484 dt_print_int, /* CTF_K_INTEGER */
485 dt_print_float, /* CTF_K_FLOAT */
486 dt_print_ptr, /* CTF_K_POINTER */
487 dt_print_array, /* CTF_K_ARRAY */
488 dt_print_ptr, /* CTF_K_FUNCTION */
489 dt_print_structlike, /* CTF_K_STRUCT */
490 dt_print_structlike, /* CTF_K_UNION */
491 dt_print_enum, /* CTF_K_ENUM */
492 dt_print_tag /* CTF_K_FORWARD */
496 * Print one member of a structure. This callback is invoked from
497 * ctf_type_visit() recursively.
500 dt_print_member(const char *name, ctf_id_t id, ulong_t off, int depth,
503 char type[DT_TYPE_NAMELEN];
505 dt_printarg_t *pap = data;
506 FILE *fp = pap->pa_file;
507 ctf_file_t *ctfp = pap->pa_ctfp;
508 boolean_t arraymember;
513 dt_print_trailing_braces(pap, depth);
515 * dt_print_trailing_braces() doesn't include the trailing newline; add
516 * it here if necessary.
518 if (depth < pap->pa_depth)
519 (void) fprintf(fp, "\n");
520 pap->pa_depth = depth;
522 if ((rtype = ctf_type_resolve(ctfp, id)) == CTF_ERR ||
523 (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR ||
524 kind < CTF_K_INTEGER || kind > CTF_K_FORWARD) {
525 dt_print_indent(pap);
526 (void) fprintf(fp, "%s = <invalid type %lu>", name, id);
530 dt_print_type_name(ctfp, id, type, sizeof (type));
532 arraymember = (pap->pa_nest != 0 && depth == 0);
533 brief = (arraymember && !CTF_IS_STRUCTLIKE(kind));
537 * If this is a direct array member and a struct (otherwise
538 * brief would be true), then print a trailing newline, as the
539 * array printing code doesn't include it because it might be a
543 (void) fprintf(fp, "\n");
544 dt_print_indent(pap);
546 /* always print the type */
547 (void) fprintf(fp, "%s", type);
548 if (name[0] != '\0') {
550 * For aesthetics, we don't include a space between the
551 * type name and member name if the type is a pointer.
552 * This will give us "void *foo =" instead of "void *
553 * foo =". Unions also have the odd behavior that the
554 * type name is returned as "union ", with a trailing
555 * space, so we also avoid printing a space if the type
556 * name already ends with a space.
558 if (type[strlen(type) - 1] != '*' &&
559 type[strlen(type) -1] != ' ') {
560 (void) fprintf(fp, " ");
562 (void) fprintf(fp, "%s", name);
565 * If this looks like a bitfield, or is an integer not
566 * aligned on a byte boundary, print the number of
567 * bits after the name.
569 if (kind == CTF_K_INTEGER &&
570 ctf_type_encoding(ctfp, id, &e) == 0) {
571 ulong_t bits = e.cte_bits;
572 ulong_t size = bits / NBBY;
574 if (bits % NBBY != 0 ||
577 size != ctf_type_size(ctfp, id)) {
578 (void) fprintf(fp, " :%lu", bits);
582 (void) fprintf(fp, " =");
584 (void) fprintf(fp, " ");
587 dt_printfuncs[kind - 1](rtype, off, pap);
589 /* direct simple array members are not separated by newlines */
591 (void) fprintf(fp, "\n");
597 * Main print function invoked by dt_consume_cpu().
600 dtrace_print(dtrace_hdl_t *dtp, FILE *fp, const char *typename,
601 caddr_t addr, size_t len)
610 * Split the fully-qualified type ID (module`id). This should
611 * always be the format, but if for some reason we don't find the
612 * expected value, return 0 to fall back to the generic trace()
615 for (s = typename; *s != '\0' && *s != '`'; s++)
621 object = alloca(s - typename + 1);
622 bcopy(typename, object, s - typename);
623 object[s - typename] = '\0';
627 * Try to get the CTF kind for this id. If something has gone horribly
628 * wrong and we can't resolve the ID, bail out and let trace() do the
631 dmp = dt_module_lookup_by_name(dtp, object);
632 if (dmp == NULL || ctf_type_kind(dt_module_getctf(dtp, dmp),
637 /* setup the print structure and kick off the main print routine */
639 pa.pa_ctfp = dt_module_getctf(dtp, dmp);
643 (void) ctf_type_visit(pa.pa_ctfp, id, dt_print_member, &pa);
645 dt_print_trailing_braces(&pa, 0);