xref: /titanic_41/usr/src/lib/libdtrace/common/dt_print.c (revision a970c705050f9e90c4b6d7982a9b3211719353fb)
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 		/* LINTED - alignment */
204 		(void) fprintf(fp, "%#x", *(uint16_t *)addr);
205 		break;
206 	case sizeof (uint32_t):
207 		/* LINTED - alignment */
208 		(void) fprintf(fp, "%#x", *(uint32_t *)addr);
209 		break;
210 	case sizeof (uint64_t):
211 		(void) fprintf(fp, "%#llx",
212 		    /* LINTED - alignment */
213 		    (unsigned long long)*(uint64_t *)addr);
214 		break;
215 	default:
216 		(void) fprintf(fp, "<invalid size %u>", (uint_t)size);
217 	}
218 }
219 
220 /*
221  * Print an integer type.  Before dumping the contents via dt_print_hex(), we
222  * first check the encoding to see if it's part of a bitfield or a character.
223  */
224 static void
225 dt_print_int(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
226 {
227 	FILE *fp = pap->pa_file;
228 	ctf_file_t *ctfp = pap->pa_ctfp;
229 	ctf_encoding_t e;
230 	size_t size;
231 	caddr_t addr = pap->pa_addr + off / NBBY;
232 
233 	if (ctf_type_encoding(ctfp, base, &e) == CTF_ERR) {
234 		(void) fprintf(fp, "<unknown encoding>");
235 		return;
236 	}
237 
238 	/*
239 	 * This comes from MDB - it's not clear under what circumstances this
240 	 * would be found.
241 	 */
242 	if (e.cte_format & CTF_INT_VARARGS) {
243 		(void) fprintf(fp, "...");
244 		return;
245 	}
246 
247 	/*
248 	 * We print this as a bitfield if the bit encoding indicates it's not
249 	 * an even power of two byte size, or is larger than 8 bytes.
250 	 */
251 	size = e.cte_bits / NBBY;
252 	if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)) != 0) {
253 		print_bitfield(pap, off, &e);
254 		return;
255 	}
256 
257 	/*
258 	 * If this is a character, print it out as such.
259 	 */
260 	if (CTF_IS_CHAR(e)) {
261 		char c = *(char *)addr;
262 		if (isprint(c))
263 			(void) fprintf(fp, "'%c'", c);
264 		else if (c == 0)
265 			(void) fprintf(fp, "'\\0'");
266 		else
267 			(void) fprintf(fp, "'\\%03o'", c);
268 		return;
269 	}
270 
271 	dt_print_hex(fp, addr, size);
272 }
273 
274 /*
275  * Print a floating point (float, double, long double) value.
276  */
277 /* ARGSUSED */
278 static void
279 dt_print_float(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
280 {
281 	FILE *fp = pap->pa_file;
282 	ctf_file_t *ctfp = pap->pa_ctfp;
283 	ctf_encoding_t e;
284 	caddr_t addr = pap->pa_addr + off / NBBY;
285 
286 	if (ctf_type_encoding(ctfp, base, &e) == 0) {
287 		if (e.cte_format == CTF_FP_SINGLE &&
288 		    e.cte_bits == sizeof (float) * NBBY) {
289 			/* LINTED - alignment */
290 			(void) fprintf(fp, "%+.7e", *((float *)addr));
291 		} else if (e.cte_format == CTF_FP_DOUBLE &&
292 		    e.cte_bits == sizeof (double) * NBBY) {
293 			/* LINTED - alignment */
294 			(void) fprintf(fp, "%+.7e", *((double *)addr));
295 		} else if (e.cte_format == CTF_FP_LDOUBLE &&
296 		    e.cte_bits == sizeof (long double) * NBBY) {
297 			/* LINTED - alignment */
298 			(void) fprintf(fp, "%+.16LE", *((long double *)addr));
299 		} else {
300 			(void) fprintf(fp, "<unknown encoding>");
301 		}
302 	}
303 }
304 
305 /*
306  * A pointer is printed as a fixed-size integer.  This is used both for
307  * pointers and functions.
308  */
309 static void
310 dt_print_ptr(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
311 {
312 	FILE *fp = pap->pa_file;
313 	ctf_file_t *ctfp = pap->pa_ctfp;
314 	caddr_t addr = pap->pa_addr + off / NBBY;
315 	size_t size = ctf_type_size(ctfp, base);
316 
317 	dt_print_hex(fp, addr, size);
318 }
319 
320 /*
321  * Print out an array.  This is somewhat complex, as we must manually visit
322  * each member, and recursively invoke ctf_type_visit() for each member.  If
323  * the members are non-structs, then we print them out directly:
324  *
325  * 	[ 0x14, 0x2e, 0 ]
326  *
327  * If they are structs, then we print out the necessary leading and trailing
328  * braces, to end up with:
329  *
330  *	[
331  *	    type {
332  *	    ...
333  *	    },
334  *	    type {
335  *	    ...
336  *	    }
337  *	]
338  *
339  * We also use a heuristic to detect whether the array looks like a character
340  * array.  If the encoding indicates it's a character, and we have all
341  * printable characters followed by a null byte, then we display it as a
342  * string:
343  *
344  *	[ "string" ]
345  */
346 static void
347 dt_print_array(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
348 {
349 	FILE *fp = pap->pa_file;
350 	ctf_file_t *ctfp = pap->pa_ctfp;
351 	caddr_t addr = pap->pa_addr + off / NBBY;
352 	ctf_arinfo_t car;
353 	ssize_t eltsize;
354 	ctf_encoding_t e;
355 	int i;
356 	boolean_t isstring;
357 	int kind;
358 	ctf_id_t rtype;
359 
360 	if (ctf_array_info(ctfp, base, &car) == CTF_ERR) {
361 		(void) fprintf(fp, "0x%p", (void *)addr);
362 		return;
363 	}
364 
365 	if ((eltsize = ctf_type_size(ctfp, car.ctr_contents)) < 0 ||
366 	    (rtype = ctf_type_resolve(ctfp, car.ctr_contents)) == CTF_ERR ||
367 	    (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR) {
368 		(void) fprintf(fp, "<invalid type %lu>", car.ctr_contents);
369 		return;
370 	}
371 
372 	/* 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 */
445 static void
446 dt_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 */
456 static void
457 dt_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 */
475 static void
476 dt_print_tag(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
477 {
478 	(void) fprintf(pap->pa_file, "<forward decl>");
479 }
480 
481 typedef void dt_printarg_f(ctf_id_t, ulong_t, dt_printarg_t *);
482 
483 static dt_printarg_f *const dt_printfuncs[] = {
484 	dt_print_int,		/* CTF_K_INTEGER */
485 	dt_print_float,		/* CTF_K_FLOAT */
486 	dt_print_ptr,		/* CTF_K_POINTER */
487 	dt_print_array,		/* CTF_K_ARRAY */
488 	dt_print_ptr,		/* CTF_K_FUNCTION */
489 	dt_print_structlike,	/* CTF_K_STRUCT */
490 	dt_print_structlike,	/* CTF_K_UNION */
491 	dt_print_enum,		/* CTF_K_ENUM */
492 	dt_print_tag		/* CTF_K_FORWARD */
493 };
494 
495 /*
496  * Print one member of a structure.  This callback is invoked from
497  * ctf_type_visit() recursively.
498  */
499 static int
500 dt_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  */
599 int
600 dtrace_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