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