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