Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright (c) 2011 by Delphix. All rights reserved.
 */
/*
 * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
 */

/*
 * DTrace print() action
 *
 * This file contains the post-processing logic for the print() action.  The
 * print action behaves identically to trace() in that it generates a
 * DTRACEACT_DIFEXPR action, but the action argument field refers to a CTF type
 * string stored in the DOF string table (similar to printf formats).  We
 * take the result of the trace action and post-process it in the fashion of
 * MDB's ::print dcmd.
 *
 * This implementation differs from MDB's in the following ways:
 *
 * 	- We do not expose any options or flags.  The behavior of print() is
 *	  equivalent to "::print -tn".
 *
 * 	- MDB will display "holes" in structures (unused padding between
 *	  members).
 *
 * 	- When printing arrays of structures, MDB will leave a trailing ','
 *	  after the last element.
 *
 *	- MDB will print time_t types as date and time.
 *
 *	- MDB will detect when an enum is actually the OR of several flags,
 *	  and print it out with the constituent flags separated.
 *
 *	- For large arrays, MDB will print the first few members and then
 *	  print a "..." continuation line.
 *
 *	- MDB will break and wrap arrays at 80 columns.
 *
 *	- MDB prints out floats and doubles by hand, as it must run in kmdb
 *	  context.  We're able to leverage the printf() format strings,
 *	  but the result is a slightly different format.
 */

#include <sys/sysmacros.h>
#include <strings.h>
#include <stdlib.h>
#include <alloca.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <dt_module.h>
#include <dt_printf.h>
#include <dt_string.h>
#include <dt_impl.h>

/* determines whether the given integer CTF encoding is a character */
#define	CTF_IS_CHAR(e) \
	(((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
	(CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
/* determines whether the given CTF kind is a struct or union */
#define	CTF_IS_STRUCTLIKE(k) \
	((k) == CTF_K_STRUCT || (k) == CTF_K_UNION)

/*
 * Print structure passed down recursively through printing algorithm.
 */
typedef struct dt_printarg {
	dtrace_hdl_t	*pa_dtp;	/* libdtrace handle */
	caddr_t		pa_addr;	/* base address of trace data */
	ctf_file_t	*pa_ctfp;	/* CTF container */
	int		pa_depth;	/* member depth */
	int		pa_nest;	/* nested array depth */
	FILE		*pa_file;	/* output file */
} dt_printarg_t;

static int dt_print_member(const char *, ctf_id_t, ulong_t, int, void *);

/*
 * Safe version of ctf_type_name() that will fall back to just "<ctfid>" if it
 * can't resolve the type.
 */
static void
dt_print_type_name(ctf_file_t *ctfp, ctf_id_t id, char *buf, size_t buflen)
{
	if (ctf_type_name(ctfp, id, buf, buflen) == NULL)
		(void) snprintf(buf, buflen, "<%ld>", id);
}

/*
 * Print any necessary trailing braces for structures or unions.  We don't get
 * invoked when a struct or union ends, so we infer the need to print braces
 * based on the depth the last time we printed something and the new depth.
 */
static void
dt_print_trailing_braces(dt_printarg_t *pap, int depth)
{
	int d;

	for (d = pap->pa_depth; d > depth; d--) {
		(void) fprintf(pap->pa_file, "%*s}%s",
		    (d + pap->pa_nest - 1) * 4, "",
		    d == depth + 1 ? "" : "\n");
	}
}

/*
 * Print the appropriate amount of indentation given the current depth and
 * array nesting.
 */
static void
dt_print_indent(dt_printarg_t *pap)
{
	(void) fprintf(pap->pa_file, "%*s",
	    (pap->pa_depth + pap->pa_nest) * 4, "");
}

/*
 * Print a bitfield.  It's worth noting that the D compiler support for
 * bitfields is currently broken; printing "D`user_desc_t" (pulled in by the
 * various D provider files) will produce incorrect results compared to
 * "genunix`user_desc_t".
 */
static void
print_bitfield(dt_printarg_t *pap, ulong_t off, ctf_encoding_t *ep)
{
	FILE *fp = pap->pa_file;
	caddr_t addr = pap->pa_addr + off / NBBY;
	uint64_t mask = (1ULL << ep->cte_bits) - 1;
	uint64_t value = 0;
	size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
	uint8_t *buf = (uint8_t *)&value;
	uint8_t shift;

	/*
	 * On big-endian machines, we need to adjust the buf pointer to refer
	 * to the lowest 'size' bytes in 'value', and we need to shift based on
	 * the offset from the end of the data, not the offset of the start.
	 */
#if BYTE_ORDER == _BIG_ENDIAN
	buf += sizeof (value) - size;
	off += ep->cte_bits;
#endif
	bcopy(addr, buf, size);
	shift = off % NBBY;

	/*
	 * Offsets are counted from opposite ends on little- and
	 * big-endian machines.
	 */
#if BYTE_ORDER == _BIG_ENDIAN
	shift = NBBY - shift;
#endif

	/*
	 * If the bits we want do not begin on a byte boundary, shift the data
	 * right so that the value is in the lowest 'cte_bits' of 'value'.
	 */
	if (off % NBBY != 0)
		value >>= shift;
	value &= mask;

	(void) fprintf(fp, "%#llx", (u_longlong_t)value);
}

/*
 * Dump the contents of memory as a fixed-size integer in hex.
 */
static void
dt_print_hex(FILE *fp, caddr_t addr, size_t size)
{
	switch (size) {
	case sizeof (uint8_t):
		(void) fprintf(fp, "%#x", *(uint8_t *)addr);
		break;
	case sizeof (uint16_t):
		/* LINTED - alignment */
		(void) fprintf(fp, "%#x", *(uint16_t *)addr);
		break;
	case sizeof (uint32_t):
		/* LINTED - alignment */
		(void) fprintf(fp, "%#x", *(uint32_t *)addr);
		break;
	case sizeof (uint64_t):
		(void) fprintf(fp, "%#llx",
		    /* LINTED - alignment */
		    (unsigned long long)*(uint64_t *)addr);
		break;
	default:
		(void) fprintf(fp, "<invalid size %u>", (uint_t)size);
	}
}

/*
 * Print an integer type.  Before dumping the contents via dt_print_hex(), we
 * first check the encoding to see if it's part of a bitfield or a character.
 */
static void
dt_print_int(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
{
	FILE *fp = pap->pa_file;
	ctf_file_t *ctfp = pap->pa_ctfp;
	ctf_encoding_t e;
	size_t size;
	caddr_t addr = pap->pa_addr + off / NBBY;

	if (ctf_type_encoding(ctfp, base, &e) == CTF_ERR) {
		(void) fprintf(fp, "<unknown encoding>");
		return;
	}

	/*
	 * This comes from MDB - it's not clear under what circumstances this
	 * would be found.
	 */
	if (e.cte_format & CTF_INT_VARARGS) {
		(void) fprintf(fp, "...");
		return;
	}

	/*
	 * We print this as a bitfield if the bit encoding indicates it's not
	 * an even power of two byte size, or is larger than 8 bytes.
	 */
	size = e.cte_bits / NBBY;
	if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)) != 0) {
		print_bitfield(pap, off, &e);
		return;
	}

	/*
	 * If this is a character, print it out as such.
	 */
	if (CTF_IS_CHAR(e)) {
		char c = *(char *)addr;
		if (isprint(c))
			(void) fprintf(fp, "'%c'", c);
		else if (c == 0)
			(void) fprintf(fp, "'\\0'");
		else
			(void) fprintf(fp, "'\\%03o'", c);
		return;
	}

	dt_print_hex(fp, addr, size);
}

/*
 * Print a floating point (float, double, long double) value.
 */
/* ARGSUSED */
static void
dt_print_float(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
{
	FILE *fp = pap->pa_file;
	ctf_file_t *ctfp = pap->pa_ctfp;
	ctf_encoding_t e;
	caddr_t addr = pap->pa_addr + off / NBBY;

	if (ctf_type_encoding(ctfp, base, &e) == 0) {
		if (e.cte_format == CTF_FP_SINGLE &&
		    e.cte_bits == sizeof (float) * NBBY) {
			/* LINTED - alignment */
			(void) fprintf(fp, "%+.7e", *((float *)addr));
		} else if (e.cte_format == CTF_FP_DOUBLE &&
		    e.cte_bits == sizeof (double) * NBBY) {
			/* LINTED - alignment */
			(void) fprintf(fp, "%+.7e", *((double *)addr));
		} else if (e.cte_format == CTF_FP_LDOUBLE &&
		    e.cte_bits == sizeof (long double) * NBBY) {
			/* LINTED - alignment */
			(void) fprintf(fp, "%+.16LE", *((long double *)addr));
		} else {
			(void) fprintf(fp, "<unknown encoding>");
		}
	}
}

/*
 * A pointer is generally printed as a fixed-size integer.  If we have a
 * function pointer, we try to look up its name.
 */
static void
dt_print_ptr(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
{
	FILE *fp = pap->pa_file;
	ctf_file_t *ctfp = pap->pa_ctfp;
	caddr_t addr = pap->pa_addr + off / NBBY;
	size_t size = ctf_type_size(ctfp, base);
	ctf_id_t bid = ctf_type_reference(ctfp, base);
	uint64_t pc;
	dtrace_syminfo_t dts;
	GElf_Sym sym;

	if (bid == CTF_ERR || ctf_type_kind(ctfp, bid) != CTF_K_FUNCTION) {
		dt_print_hex(fp, addr, size);
	} else {
		/* LINTED - alignment */
		pc = *((uint64_t *)addr);
		if (dtrace_lookup_by_addr(pap->pa_dtp, pc, &sym, &dts) != 0) {
			dt_print_hex(fp, addr, size);
		} else {
			(void) fprintf(fp, "%s`%s", dts.dts_object,
			    dts.dts_name);
		}
	}
}

/*
 * Print out an array.  This is somewhat complex, as we must manually visit
 * each member, and recursively invoke ctf_type_visit() for each member.  If
 * the members are non-structs, then we print them out directly:
 *
 * 	[ 0x14, 0x2e, 0 ]
 *
 * If they are structs, then we print out the necessary leading and trailing
 * braces, to end up with:
 *
 *	[
 *	    type {
 *	    ...
 *	    },
 *	    type {
 *	    ...
 *	    }
 *	]
 *
 * We also use a heuristic to detect whether the array looks like a character
 * array.  If the encoding indicates it's a character, and we have all
 * printable characters followed by a null byte, then we display it as a
 * string:
 *
 *	[ "string" ]
 */
static void
dt_print_array(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
{
	FILE *fp = pap->pa_file;
	ctf_file_t *ctfp = pap->pa_ctfp;
	caddr_t addr = pap->pa_addr + off / NBBY;
	ctf_arinfo_t car;
	ssize_t eltsize;
	ctf_encoding_t e;
	int i;
	boolean_t isstring;
	int kind;
	ctf_id_t rtype;

	if (ctf_array_info(ctfp, base, &car) == CTF_ERR) {
		(void) fprintf(fp, "%p", (void *)addr);
		return;
	}

	if ((eltsize = ctf_type_size(ctfp, car.ctr_contents)) < 0 ||
	    (rtype = ctf_type_resolve(ctfp, car.ctr_contents)) == CTF_ERR ||
	    (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR) {
		(void) fprintf(fp, "<invalid type %lu>", car.ctr_contents);
		return;
	}

	/* see if this looks like a string */
	isstring = B_FALSE;
	if (kind == CTF_K_INTEGER &&
	    ctf_type_encoding(ctfp, rtype, &e) != CTF_ERR && CTF_IS_CHAR(e)) {
		char c;
		for (i = 0; i < car.ctr_nelems; i++) {
			c = *((char *)addr + eltsize * i);
			if (!isprint(c) || c == '\0')
				break;
		}

		if (i != car.ctr_nelems && c == '\0')
			isstring = B_TRUE;
	}

	/*
	 * As a slight aesthetic optimization, if we are a top-level type, then
	 * don't bother printing out the brackets.  This lets print("foo") look
	 * like:
	 *
	 * 	string "foo"
	 *
	 * As D will internally represent this as a char[256] array.
	 */
	if (!isstring || pap->pa_depth != 0)
		(void) fprintf(fp, "[ ");

	if (isstring)
		(void) fprintf(fp, "\"");

	for (i = 0; i < car.ctr_nelems; i++) {
		if (isstring) {
			char c = *((char *)addr + eltsize * i);
			if (c == '\0')
				break;
			(void) fprintf(fp, "%c", c);
		} else {
			/*
			 * Recursively invoke ctf_type_visit() on each member.
			 * We setup a new printarg struct with 'pa_nest' set to
			 * indicate that we are within a nested array.
			 */
			dt_printarg_t pa = *pap;
			pa.pa_nest += pap->pa_depth + 1;
			pa.pa_depth = 0;
			pa.pa_addr = addr + eltsize * i;
			(void) ctf_type_visit(ctfp, car.ctr_contents,
			    dt_print_member, &pa);

			dt_print_trailing_braces(&pa, 0);
			if (i != car.ctr_nelems - 1)
				(void) fprintf(fp, ", ");
			else if (CTF_IS_STRUCTLIKE(kind))
				(void) fprintf(fp, "\n");
		}
	}

	if (isstring)
		(void) fprintf(fp, "\"");

	if (!isstring || pap->pa_depth != 0) {
		if (CTF_IS_STRUCTLIKE(kind))
			dt_print_indent(pap);
		else
			(void) fprintf(fp, " ");
		(void) fprintf(fp, "]");
	}
}

/*
 * This isued by both structs and unions to print the leading brace.
 */
/* ARGSUSED */
static void
dt_print_structlike(ctf_id_t id, ulong_t off, dt_printarg_t *pap)
{
	(void) fprintf(pap->pa_file, "{");
}

/*
 * For enums, we try to print the enum name, and fall back to the value if it
 * can't be determined.  We do not do any fancy flag processing like mdb.
 */
/* ARGSUSED */
static void
dt_print_enum(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
{
	FILE *fp = pap->pa_file;
	ctf_file_t *ctfp = pap->pa_ctfp;
	const char *ename;
	ssize_t size;
	caddr_t addr = pap->pa_addr + off / NBBY;
	int value = 0;

	/*
	 * The C standard says that an enum will be at most the sizeof (int).
	 * But if all the values are less than that, the compiler can use a
	 * smaller size. Thanks standards.
	 */
	size = ctf_type_size(ctfp, base);
	switch (size) {
	case sizeof (uint8_t):
		value = *(uint8_t *)addr;
		break;
	case sizeof (uint16_t):
		value = *(uint16_t *)addr;
		break;
	case sizeof (int32_t):
		value = *(int32_t *)addr;
		break;
	default:
		(void) fprintf(fp, "<invalid enum size %u>", (uint_t)size);
		return;
	}

	if ((ename = ctf_enum_name(ctfp, base, value)) != NULL)
		(void) fprintf(fp, "%s", ename);
	else
		(void) fprintf(fp, "%d", value);
}

/*
 * Forward declaration.  There's not much to do here without the complete
 * type information, so just print out this fact and drive on.
 */
/* ARGSUSED */
static void
dt_print_tag(ctf_id_t base, ulong_t off, dt_printarg_t *pap)
{
	(void) fprintf(pap->pa_file, "<forward decl>");
}

typedef void dt_printarg_f(ctf_id_t, ulong_t, dt_printarg_t *);

static dt_printarg_f *const dt_printfuncs[] = {
	dt_print_int,		/* CTF_K_INTEGER */
	dt_print_float,		/* CTF_K_FLOAT */
	dt_print_ptr,		/* CTF_K_POINTER */
	dt_print_array,		/* CTF_K_ARRAY */
	dt_print_ptr,		/* CTF_K_FUNCTION */
	dt_print_structlike,	/* CTF_K_STRUCT */
	dt_print_structlike,	/* CTF_K_UNION */
	dt_print_enum,		/* CTF_K_ENUM */
	dt_print_tag		/* CTF_K_FORWARD */
};

/*
 * Print one member of a structure.  This callback is invoked from
 * ctf_type_visit() recursively.
 */
static int
dt_print_member(const char *name, ctf_id_t id, ulong_t off, int depth,
    void *data)
{
	char type[DT_TYPE_NAMELEN];
	int kind;
	dt_printarg_t *pap = data;
	FILE *fp = pap->pa_file;
	ctf_file_t *ctfp = pap->pa_ctfp;
	boolean_t arraymember;
	boolean_t brief;
	ctf_encoding_t e;
	ctf_id_t rtype;

	dt_print_trailing_braces(pap, depth);
	/*
	 * dt_print_trailing_braces() doesn't include the trailing newline; add
	 * it here if necessary.
	 */
	if (depth < pap->pa_depth)
		(void) fprintf(fp, "\n");
	pap->pa_depth = depth;

	if ((rtype = ctf_type_resolve(ctfp, id)) == CTF_ERR ||
	    (kind = ctf_type_kind(ctfp, rtype)) == CTF_ERR ||
	    kind < CTF_K_INTEGER || kind > CTF_K_FORWARD) {
		dt_print_indent(pap);
		(void) fprintf(fp, "%s = <invalid type %lu>", name, id);
		return (0);
	}

	dt_print_type_name(ctfp, id, type, sizeof (type));

	arraymember = (pap->pa_nest != 0 && depth == 0);
	brief = (arraymember && !CTF_IS_STRUCTLIKE(kind));

	if (!brief) {
		/*
		 * If this is a direct array member and a struct (otherwise
		 * brief would be true), then print a trailing newline, as the
		 * array printing code doesn't include it because it might be a
		 * simple type.
		 */
		if (arraymember)
			(void) fprintf(fp, "\n");
		dt_print_indent(pap);

		/* always print the type */
		(void) fprintf(fp, "%s", type);
		if (name[0] != '\0') {
			/*
			 * For aesthetics, we don't include a space between the
			 * type name and member name if the type is a pointer.
			 * This will give us "void *foo =" instead of "void *
			 * foo =".  Unions also have the odd behavior that the
			 * type name is returned as "union ", with a trailing
			 * space, so we also avoid printing a space if the type
			 * name already ends with a space.
			 */
			if (type[strlen(type) - 1] != '*' &&
			    type[strlen(type) -1] != ' ') {
				(void) fprintf(fp, " ");
			}
			(void) fprintf(fp, "%s", name);

			/*
			 * If this looks like a bitfield, or is an integer not
			 * aligned on a byte boundary, print the number of
			 * bits after the name.
			 */
			if (kind == CTF_K_INTEGER &&
			    ctf_type_encoding(ctfp, id, &e) == 0) {
				ulong_t bits = e.cte_bits;
				ulong_t size = bits / NBBY;

				if (bits % NBBY != 0 ||
				    off % NBBY != 0 ||
				    size > 8 ||
				    size != ctf_type_size(ctfp, id)) {
					(void) fprintf(fp, " :%lu", bits);
				}
			}

			(void) fprintf(fp, " =");
		}
		(void) fprintf(fp, " ");
	}

	dt_printfuncs[kind - 1](rtype, off, pap);

	/* direct simple array members are not separated by newlines */
	if (!brief)
		(void) fprintf(fp, "\n");

	return (0);
}

/*
 * Main print function invoked by dt_consume_cpu().
 */
int
dtrace_print(dtrace_hdl_t *dtp, FILE *fp, const char *typename,
    caddr_t addr, size_t len)
{
	const char *s;
	char *object;
	dt_printarg_t pa;
	ctf_id_t id;
	dt_module_t *dmp;
	ctf_file_t *ctfp;
	int libid;

	/*
	 * Split the fully-qualified type ID (module`id).  This should
	 * always be the format, but if for some reason we don't find the
	 * expected value, return 0 to fall back to the generic trace()
	 * behavior. In the case of userland CTF modules this will actually be
	 * of the format (module`lib`id). This is due to the fact that those
	 * modules have multiple CTF containers which `lib` identifies.
	 */
	for (s = typename; *s != '\0' && *s != '`'; s++)
		;

	if (*s != '`')
		return (0);

	object = alloca(s - typename + 1);
	bcopy(typename, object, s - typename);
	object[s - typename] = '\0';
	dmp = dt_module_lookup_by_name(dtp, object);
	if (dmp == NULL)
		return (0);

	if (dmp->dm_pid != 0) {
		libid = atoi(s + 1);
		s = strchr(s + 1, '`');
		if (s == NULL || libid > dmp->dm_nctflibs)
			return (0);
		ctfp = dmp->dm_libctfp[libid];
	} else {
		ctfp = dt_module_getctf(dtp, dmp);
	}

	id = atoi(s + 1);

	/*
	 * Try to get the CTF kind for this id.  If something has gone horribly
	 * wrong and we can't resolve the ID, bail out and let trace() do the
	 * work.
	 */
	if (ctfp == NULL || ctf_type_kind(ctfp, id) == CTF_ERR)
		return (0);

	/* setup the print structure and kick off the main print routine */
	pa.pa_dtp = dtp;
	pa.pa_addr = addr;
	pa.pa_ctfp = ctfp;
	pa.pa_nest = 0;
	pa.pa_depth = 0;
	pa.pa_file = fp;
	(void) ctf_type_visit(pa.pa_ctfp, id, dt_print_member, &pa);

	dt_print_trailing_braces(&pa, 0);

	return (len);
}