dt_print.c revision 248708
118334Speter/*
290075Sobrien * CDDL HEADER START
3169689Skan *
418334Speter * The contents of this file are subject to the terms of the
590075Sobrien * Common Development and Distribution License (the "License").
618334Speter * You may not use this file except in compliance with the License.
790075Sobrien *
890075Sobrien * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
990075Sobrien * or http://www.opensolaris.org/os/licensing.
1090075Sobrien * See the License for the specific language governing permissions
1118334Speter * and limitations under the License.
1290075Sobrien *
1390075Sobrien * When distributing Covered Code, include this CDDL HEADER in each
1490075Sobrien * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1590075Sobrien * If applicable, add the following below this CDDL HEADER, with the
1618334Speter * fields enclosed by brackets "[]" replaced with your own identifying
1718334Speter * information: Portions Copyright [yyyy] [name of copyright owner]
1890075Sobrien *
19169689Skan * CDDL HEADER END
20169689Skan */
2118334Speter/*
2218334Speter * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2318334Speter * Use is subject to license terms.
2418334Speter */
2518334Speter/*
2618334Speter * Copyright (c) 2011 by Delphix. All rights reserved.
2718334Speter */
2818334Speter
2918334Speter/*
3018334Speter * DTrace print() action
3118334Speter *
3218334Speter * This file contains the post-processing logic for the print() action.  The
3350397Sobrien * print action behaves identically to trace() in that it generates a
3450397Sobrien * DTRACEACT_DIFEXPR action, but the action argument field refers to a CTF type
3550397Sobrien * string stored in the DOF string table (similar to printf formats).  We
3650397Sobrien * take the result of the trace action and post-process it in the fashion of
3750397Sobrien * MDB's ::print dcmd.
38132718Skan *
3950397Sobrien * This implementation differs from MDB's in the following ways:
4018334Speter *
4118334Speter * 	- We do not expose any options or flags.  The behavior of print() is
4218334Speter *	  equivalent to "::print -tn".
4318334Speter *
4418334Speter * 	- MDB will display "holes" in structures (unused padding between
4518334Speter *	  members).
4618334Speter *
4718334Speter * 	- When printing arrays of structures, MDB will leave a trailing ','
4818334Speter *	  after the last element.
4918334Speter *
5018334Speter *	- MDB will print time_t types as date and time.
5118334Speter *
5250397Sobrien *	- MDB will detect when an enum is actually the OR of several flags,
5350397Sobrien *	  and print it out with the constituent flags separated.
5450397Sobrien *
5518334Speter *	- For large arrays, MDB will print the first few members and then
5618334Speter *	  print a "..." continuation line.
5718334Speter *
5818334Speter *	- MDB will break and wrap arrays at 80 columns.
5918334Speter *
6018334Speter *	- MDB prints out floats and doubles by hand, as it must run in kmdb
6118334Speter *	  context.  We're able to leverage the printf() format strings,
62132718Skan *	  but the result is a slightly different format.
6318334Speter */
6418334Speter
6518334Speter#include <sys/sysmacros.h>
6618334Speter#include <strings.h>
6718334Speter#include <stdlib.h>
6818334Speter#include <alloca.h>
69132718Skan#include <assert.h>
7050397Sobrien#include <ctype.h>
7150397Sobrien#include <errno.h>
7218334Speter#include <limits.h>
7318334Speter#include <sys/socket.h>
7418334Speter#include <netdb.h>
7518334Speter#include <netinet/in.h>
7690075Sobrien#include <arpa/inet.h>
7790075Sobrien#include <arpa/nameser.h>
7890075Sobrien
7990075Sobrien#include <dt_module.h>
8090075Sobrien#include <dt_printf.h>
8190075Sobrien#include <dt_string.h>
8290075Sobrien#include <dt_impl.h>
8390075Sobrien
8490075Sobrien/* determines whether the given integer CTF encoding is a character */
8518334Speter#define	CTF_IS_CHAR(e) \
8690075Sobrien	(((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
8790075Sobrien	(CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
8890075Sobrien/* determines whether the given CTF kind is a struct or union */
8990075Sobrien#define	CTF_IS_STRUCTLIKE(k) \
9090075Sobrien	((k) == CTF_K_STRUCT || (k) == CTF_K_UNION)
9190075Sobrien
9290075Sobrien/*
9390075Sobrien * Print structure passed down recursively through printing algorithm.
9490075Sobrien */
9590075Sobrientypedef struct dt_printarg {
9690075Sobrien	caddr_t		pa_addr;	/* base address of trace data */
9790075Sobrien	ctf_file_t	*pa_ctfp;	/* CTF container */
9890075Sobrien	int		pa_depth;	/* member depth */
9990075Sobrien	int		pa_nest;	/* nested array depth */
10090075Sobrien	FILE		*pa_file;	/* output file */
10190075Sobrien} dt_printarg_t;
10290075Sobrien
10390075Sobrienstatic int dt_print_member(const char *, ctf_id_t, ulong_t, int, void *);
10490075Sobrien
10590075Sobrien/*
10690075Sobrien * Safe version of ctf_type_name() that will fall back to just "<ctfid>" if it
10790075Sobrien * can't resolve the type.
10890075Sobrien */
10990075Sobrienstatic void
11090075Sobriendt_print_type_name(ctf_file_t *ctfp, ctf_id_t id, char *buf, size_t buflen)
11190075Sobrien{
11290075Sobrien	if (ctf_type_name(ctfp, id, buf, buflen) == NULL)
11390075Sobrien		(void) snprintf(buf, buflen, "<%ld>", id);
11490075Sobrien}
11590075Sobrien
11690075Sobrien/*
11790075Sobrien * Print any necessary trailing braces for structures or unions.  We don't get
11890075Sobrien * invoked when a struct or union ends, so we infer the need to print braces
11990075Sobrien * based on the depth the last time we printed something and the new depth.
12090075Sobrien */
12190075Sobrienstatic void
12290075Sobriendt_print_trailing_braces(dt_printarg_t *pap, int depth)
12390075Sobrien{
12490075Sobrien	int d;
12590075Sobrien
12690075Sobrien	for (d = pap->pa_depth; d > depth; d--) {
12790075Sobrien		(void) fprintf(pap->pa_file, "%*s}%s",
12890075Sobrien		    (d + pap->pa_nest - 1) * 4, "",
12990075Sobrien		    d == depth + 1 ? "" : "\n");
13090075Sobrien	}
13190075Sobrien}
13290075Sobrien
13390075Sobrien/*
13490075Sobrien * Print the appropriate amount of indentation given the current depth and
13590075Sobrien * array nesting.
13690075Sobrien */
13790075Sobrienstatic void
13890075Sobriendt_print_indent(dt_printarg_t *pap)
13990075Sobrien{
14090075Sobrien	(void) fprintf(pap->pa_file, "%*s",
141132718Skan	    (pap->pa_depth + pap->pa_nest) * 4, "");
14290075Sobrien}
14390075Sobrien
14490075Sobrien/*
14590075Sobrien * Print a bitfield.  It's worth noting that the D compiler support for
14690075Sobrien * bitfields is currently broken; printing "D`user_desc_t" (pulled in by the
14790075Sobrien * various D provider files) will produce incorrect results compared to
14890075Sobrien * "genunix`user_desc_t".
14990075Sobrien */
15090075Sobrienstatic void
15190075Sobrienprint_bitfield(dt_printarg_t *pap, ulong_t off, ctf_encoding_t *ep)
15290075Sobrien{
15390075Sobrien	FILE *fp = pap->pa_file;
15490075Sobrien	caddr_t addr = pap->pa_addr + off / NBBY;
15590075Sobrien	uint64_t mask = (1ULL << ep->cte_bits) - 1;
15690075Sobrien	uint64_t value = 0;
15790075Sobrien	size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
158169689Skan	uint8_t *buf = (uint8_t *)&value;
15918334Speter	uint8_t shift;
160169689Skan
16118334Speter	/*
16218334Speter	 * On big-endian machines, we need to adjust the buf pointer to refer
16318334Speter	 * to the lowest 'size' bytes in 'value', and we need to shift based on
164169689Skan	 * the offset from the end of the data, not the offset of the start.
16518334Speter	 */
166169689Skan#ifdef _BIG_ENDIAN
167169689Skan	buf += sizeof (value) - size;
168169689Skan	off += ep->cte_bits;
169169689Skan#endif
170169689Skan	bcopy(addr, buf, size);
171169689Skan	shift = off % NBBY;
172169689Skan
17318334Speter	/*
17418334Speter	 * Offsets are counted from opposite ends on little- and
17518334Speter	 * big-endian machines.
17618334Speter	 */
17718334Speter#ifdef _BIG_ENDIAN
17818334Speter	shift = NBBY - shift;
17918334Speter#endif
18018334Speter
18118334Speter	/*
18218334Speter	 * If the bits we want do not begin on a byte boundary, shift the data
18318334Speter	 * right so that the value is in the lowest 'cte_bits' of 'value'.
18418334Speter	 */
18518334Speter	if (off % NBBY != 0)
18618334Speter		value >>= shift;
18790075Sobrien	value &= mask;
18818334Speter
18918334Speter	(void) fprintf(fp, "%#llx", (u_longlong_t)value);
19018334Speter}
19118334Speter
19218334Speter/*
19318334Speter * Dump the contents of memory as a fixed-size integer in hex.
19452284Sobrien */
19552284Sobrienstatic void
19652284Sobriendt_print_hex(FILE *fp, caddr_t addr, size_t size)
19752284Sobrien{
19852284Sobrien	switch (size) {
19952284Sobrien	case sizeof (uint8_t):
200132718Skan		(void) fprintf(fp, "%#x", *(uint8_t *)addr);
20152284Sobrien		break;
202117395Skan	case sizeof (uint16_t):
20352284Sobrien		/* LINTED - alignment */
20452284Sobrien		(void) fprintf(fp, "%#x", *(uint16_t *)addr);
20552284Sobrien		break;
20652284Sobrien	case sizeof (uint32_t):
20752284Sobrien		/* LINTED - alignment */
20852284Sobrien		(void) fprintf(fp, "%#x", *(uint32_t *)addr);
20952284Sobrien		break;
21052284Sobrien	case sizeof (uint64_t):
21152284Sobrien		(void) fprintf(fp, "%#llx",
21252284Sobrien		    /* LINTED - alignment */
21352284Sobrien		    (unsigned long long)*(uint64_t *)addr);
21490075Sobrien		break;
21590075Sobrien	default:
21690075Sobrien		(void) fprintf(fp, "<invalid size %u>", (uint_t)size);
21752284Sobrien	}
21890075Sobrien}
21990075Sobrien
22090075Sobrien/*
22152284Sobrien * Print an integer type.  Before dumping the contents via dt_print_hex(), we
22252284Sobrien * first check the encoding to see if it's part of a bitfield or a character.
22352284Sobrien */
22452284Sobrienstatic void
22552284Sobriendt_print_int(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
22652284Sobrien{
22752284Sobrien	FILE *fp = pap->pa_file;
22852284Sobrien	ctf_file_t *ctfp = pap->pa_ctfp;
22952284Sobrien	ctf_encoding_t e;
23052284Sobrien	size_t size;
23152284Sobrien	caddr_t addr = pap->pa_addr + off / NBBY;
23252284Sobrien
23352284Sobrien	if (ctf_type_encoding(ctfp, base, &e) == CTF_ERR) {
23452284Sobrien		(void) fprintf(fp, "<unknown encoding>");
23552284Sobrien		return;
23652284Sobrien	}
23752284Sobrien
23852284Sobrien	/*
23952284Sobrien	 * This comes from MDB - it's not clear under what circumstances this
24052284Sobrien	 * would be found.
241132718Skan	 */
24252284Sobrien	if (e.cte_format & CTF_INT_VARARGS) {
243132718Skan		(void) fprintf(fp, "...");
24452284Sobrien		return;
24552284Sobrien	}
24618334Speter
24718334Speter	/*
248169689Skan	 * We print this as a bitfield if the bit encoding indicates it's not
249169689Skan	 * an even power of two byte size, or is larger than 8 bytes.
250169689Skan	 */
251169689Skan	size = e.cte_bits / NBBY;
252169689Skan	if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)) != 0) {
253169689Skan		print_bitfield(pap, off, &e);
254169689Skan		return;
255132718Skan	}
25618334Speter
25718334Speter	/*
258132718Skan	 * If this is a character, print it out as such.
25918334Speter	 */
26018334Speter	if (CTF_IS_CHAR(e)) {
261132718Skan		char c = *(char *)addr;
26218334Speter		if (isprint(c))
26318334Speter			(void) fprintf(fp, "'%c'", c);
26418334Speter		else if (c == 0)
265132718Skan			(void) fprintf(fp, "'\\0'");
26618334Speter		else
26752284Sobrien			(void) fprintf(fp, "'\\%03o'", c);
26852284Sobrien		return;
269132718Skan	}
270132718Skan
271132718Skan	dt_print_hex(fp, addr, size);
27218334Speter}
27318334Speter
27418334Speter/*
27518334Speter * Print a floating point (float, double, long double) value.
276132718Skan */
27718334Speter/* ARGSUSED */
27890075Sobrienstatic void
279132718Skandt_print_float(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
28018334Speter{
28118334Speter	FILE *fp = pap->pa_file;
28218334Speter	ctf_file_t *ctfp = pap->pa_ctfp;
28318334Speter	ctf_encoding_t e;
284132718Skan	caddr_t addr = pap->pa_addr + off / NBBY;
28518334Speter
28618334Speter	if (ctf_type_encoding(ctfp, base, &e) == 0) {
28718334Speter		if (e.cte_format == CTF_FP_SINGLE &&
28818334Speter		    e.cte_bits == sizeof (float) * NBBY) {
28918334Speter			/* LINTED - alignment */
290132718Skan			(void) fprintf(fp, "%+.7e", *((float *)addr));
29118334Speter		} else if (e.cte_format == CTF_FP_DOUBLE &&
29218334Speter		    e.cte_bits == sizeof (double) * NBBY) {
29318334Speter			/* LINTED - alignment */
294132718Skan			(void) fprintf(fp, "%+.7e", *((double *)addr));
29518334Speter		} else if (e.cte_format == CTF_FP_LDOUBLE &&
29618334Speter		    e.cte_bits == sizeof (long double) * NBBY) {
29718334Speter			/* LINTED - alignment */
29818334Speter			(void) fprintf(fp, "%+.16LE", *((long double *)addr));
299132718Skan		} else {
30018334Speter			(void) fprintf(fp, "<unknown encoding>");
30150397Sobrien		}
302132718Skan	}
30350397Sobrien}
30418334Speter
30518334Speter/*
306132718Skan * A pointer is printed as a fixed-size integer.  This is used both for
30718334Speter * pointers and functions.
30818334Speter */
309132718Skanstatic void
31018334Speterdt_print_ptr(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
31118334Speter{
31218334Speter	FILE *fp = pap->pa_file;
313132718Skan	ctf_file_t *ctfp = pap->pa_ctfp;
314132718Skan	caddr_t addr = pap->pa_addr + off / NBBY;
31518334Speter	size_t size = ctf_type_size(ctfp, base);
31618334Speter
317132718Skan	dt_print_hex(fp, addr, size);
31818334Speter}
31990075Sobrien
320132718Skan/*
32190075Sobrien * Print out an array.  This is somewhat complex, as we must manually visit
32290075Sobrien * each member, and recursively invoke ctf_type_visit() for each member.  If
323132718Skan * the members are non-structs, then we print them out directly:
324132718Skan *
325132718Skan * 	[ 0x14, 0x2e, 0 ]
32690075Sobrien *
327132718Skan * If they are structs, then we print out the necessary leading and trailing
328132718Skan * braces, to end up with:
329132718Skan *
33018334Speter *	[
33118334Speter *	    type {
33218334Speter *	    ...
333132718Skan *	    },
33418334Speter *	    type {
33518334Speter *	    ...
336132718Skan *	    }
33718334Speter *	]
33818334Speter *
33918334Speter * We also use a heuristic to detect whether the array looks like a character
340132718Skan * array.  If the encoding indicates it's a character, and we have all
34118334Speter * printable characters followed by a null byte, then we display it as a
34218334Speter * string:
34318334Speter *
344132718Skan *	[ "string" ]
34518334Speter */
34652284Sobrienstatic void
347132718Skandt_print_array(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
34852284Sobrien{
34918334Speter	FILE *fp = pap->pa_file;
35018334Speter	ctf_file_t *ctfp = pap->pa_ctfp;
35118334Speter	caddr_t addr = pap->pa_addr + off / NBBY;
352132718Skan	ctf_arinfo_t car;
35318334Speter	ssize_t eltsize;
35418334Speter	ctf_encoding_t e;
355132718Skan	int i;
35618334Speter	boolean_t isstring;
35718334Speter	int kind;
358132718Skan	ctf_id_t rtype;
35918334Speter
36018334Speter	if (ctf_array_info(ctfp, base, &car) == CTF_ERR) {
361132718Skan		(void) fprintf(fp, "0x%p", (void *)addr);
36252284Sobrien		return;
36352284Sobrien	}
364132718Skan
36570635Sobrien	if ((eltsize = ctf_type_size(ctfp, car.ctr_contents)) < 0 ||
36690075Sobrien	    (rtype = ctf_type_resolve(ctfp, car.ctr_contents)) == CTF_ERR ||
367132718Skan	    (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR) {
368132718Skan		(void) fprintf(fp, "<invalid type %lu>", car.ctr_contents);
369132718Skan		return;
370132718Skan	}
371132718Skan
372132718Skan	/* see if this looks like a string */
373	isstring = B_FALSE;
374	if (kind == CTF_K_INTEGER &&
375	    ctf_type_encoding(ctfp, rtype, &e) != CTF_ERR && CTF_IS_CHAR(e)) {
376		char c;
377		for (i = 0; i < car.ctr_nelems; i++) {
378			c = *((char *)addr + eltsize * i);
379			if (!isprint(c) || c == '\0')
380				break;
381		}
382
383		if (i != car.ctr_nelems && c == '\0')
384			isstring = B_TRUE;
385	}
386
387	/*
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
390	 * like:
391	 *
392	 * 	string "foo"
393	 *
394	 * As D will internally represent this as a char[256] array.
395	 */
396	if (!isstring || pap->pa_depth != 0)
397		(void) fprintf(fp, "[ ");
398
399	if (isstring)
400		(void) fprintf(fp, "\"");
401
402	for (i = 0; i < car.ctr_nelems; i++) {
403		if (isstring) {
404			char c = *((char *)addr + eltsize * i);
405			if (c == '\0')
406				break;
407			(void) fprintf(fp, "%c", c);
408		} else {
409			/*
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.
413			 */
414			dt_printarg_t pa = *pap;
415			pa.pa_nest += pap->pa_depth + 1;
416			pa.pa_depth = 0;
417			pa.pa_addr = addr + eltsize * i;
418			(void) ctf_type_visit(ctfp, car.ctr_contents,
419			    dt_print_member, &pa);
420
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");
426		}
427	}
428
429	if (isstring)
430		(void) fprintf(fp, "\"");
431
432	if (!isstring || pap->pa_depth != 0) {
433		if (CTF_IS_STRUCTLIKE(kind))
434			dt_print_indent(pap);
435		else
436			(void) fprintf(fp, " ");
437		(void) fprintf(fp, "]");
438	}
439}
440
441/*
442 * This isued by both structs and unions to print the leading brace.
443 */
444/* ARGSUSED */
445static void
446dt_print_structlike(ctf_id_t id, ulong_t off, dt_printarg_t *pap)
447{
448	(void) fprintf(pap->pa_file, "{");
449}
450
451/*
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.
454 */
455/* ARGSUSED */
456static void
457dt_print_enum(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
458{
459	FILE *fp = pap->pa_file;
460	ctf_file_t *ctfp = pap->pa_ctfp;
461	const char *ename;
462	int value = 0;
463
464	if ((ename = ctf_enum_name(ctfp, base, value)) != NULL)
465		(void) fprintf(fp, "%s", ename);
466	else
467		(void) fprintf(fp, "%d", value);
468}
469
470/*
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.
473 */
474/* ARGSUSED */
475static void
476dt_print_tag(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
477{
478	(void) fprintf(pap->pa_file, "<forward decl>");
479}
480
481typedef void dt_printarg_f(ctf_id_t, ulong_t, dt_printarg_t *);
482
483static 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 */
493};
494
495/*
496 * Print one member of a structure.  This callback is invoked from
497 * ctf_type_visit() recursively.
498 */
499static int
500dt_print_member(const char *name, ctf_id_t id, ulong_t off, int depth,
501    void *data)
502{
503	char type[DT_TYPE_NAMELEN];
504	int kind;
505	dt_printarg_t *pap = data;
506	FILE *fp = pap->pa_file;
507	ctf_file_t *ctfp = pap->pa_ctfp;
508	boolean_t arraymember;
509	boolean_t brief;
510	ctf_encoding_t e;
511	ctf_id_t rtype;
512
513	dt_print_trailing_braces(pap, depth);
514	/*
515	 * dt_print_trailing_braces() doesn't include the trailing newline; add
516	 * it here if necessary.
517	 */
518	if (depth < pap->pa_depth)
519		(void) fprintf(fp, "\n");
520	pap->pa_depth = depth;
521
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);
527		return (0);
528	}
529
530	dt_print_type_name(ctfp, id, type, sizeof (type));
531
532	arraymember = (pap->pa_nest != 0 && depth == 0);
533	brief = (arraymember && !CTF_IS_STRUCTLIKE(kind));
534
535	if (!brief) {
536		/*
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
540		 * simple type.
541		 */
542		if (arraymember)
543			(void) fprintf(fp, "\n");
544		dt_print_indent(pap);
545
546		/* always print the type */
547		(void) fprintf(fp, "%s", type);
548		if (name[0] != '\0') {
549			/*
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.
557			 */
558			if (type[strlen(type) - 1] != '*' &&
559			    type[strlen(type) -1] != ' ') {
560				(void) fprintf(fp, " ");
561			}
562			(void) fprintf(fp, "%s", name);
563
564			/*
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.
568			 */
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;
573
574				if (bits % NBBY != 0 ||
575				    off % NBBY != 0 ||
576				    size > 8 ||
577				    size != ctf_type_size(ctfp, id)) {
578					(void) fprintf(fp, " :%lu", bits);
579				}
580			}
581
582			(void) fprintf(fp, " =");
583		}
584		(void) fprintf(fp, " ");
585	}
586
587	dt_printfuncs[kind - 1](rtype, off, pap);
588
589	/* direct simple array members are not separated by newlines */
590	if (!brief)
591		(void) fprintf(fp, "\n");
592
593	return (0);
594}
595
596/*
597 * Main print function invoked by dt_consume_cpu().
598 */
599int
600dtrace_print(dtrace_hdl_t *dtp, FILE *fp, const char *typename,
601    caddr_t addr, size_t len)
602{
603	const char *s;
604	char *object;
605	dt_printarg_t pa;
606	ctf_id_t id;
607	dt_module_t *dmp;
608
609	/*
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()
613	 * behavior.
614	 */
615	for (s = typename; *s != '\0' && *s != '`'; s++)
616		;
617
618	if (*s != '`')
619		return (0);
620
621	object = alloca(s - typename + 1);
622	bcopy(typename, object, s - typename);
623	object[s - typename] = '\0';
624	id = atoi(s + 1);
625
626	/*
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
629	 * work.
630	 */
631	dmp = dt_module_lookup_by_name(dtp, object);
632	if (dmp == NULL || ctf_type_kind(dt_module_getctf(dtp, dmp),
633	    id) == CTF_ERR) {
634		return (0);
635	}
636
637	/* setup the print structure and kick off the main print routine */
638	pa.pa_addr = addr;
639	pa.pa_ctfp = dt_module_getctf(dtp, dmp);
640	pa.pa_nest = 0;
641	pa.pa_depth = 0;
642	pa.pa_file = fp;
643	(void) ctf_type_visit(pa.pa_ctfp, id, dt_print_member, &pa);
644
645	dt_print_trailing_braces(&pa, 0);
646
647	return (len);
648}
649