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