xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_print.c (revision 28e4da25922bdfc5cba7ab29f47de911bbd78009)
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 /*
27  * Copyright (c) 2012 by Delphix. All rights reserved.
28  * Copyright (c) 2012 Joyent, Inc. All rights reserved.
29  */
30 
31 #include <mdb/mdb_modapi.h>
32 #include <mdb/mdb_target.h>
33 #include <mdb/mdb_argvec.h>
34 #include <mdb/mdb_string.h>
35 #include <mdb/mdb_stdlib.h>
36 #include <mdb/mdb_err.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_fmt.h>
39 #include <mdb/mdb_ctf.h>
40 #include <mdb/mdb_ctf_impl.h>
41 #include <mdb/mdb.h>
42 #include <mdb/mdb_tab.h>
43 
44 #include <sys/isa_defs.h>
45 #include <sys/param.h>
46 #include <sys/sysmacros.h>
47 #include <netinet/in.h>
48 #include <strings.h>
49 #include <libctf.h>
50 #include <ctype.h>
51 
52 typedef struct holeinfo {
53 	ulong_t hi_offset;		/* expected offset */
54 	uchar_t hi_isunion;		/* represents a union */
55 } holeinfo_t;
56 
57 typedef struct printarg {
58 	mdb_tgt_t *pa_tgt;		/* current target */
59 	mdb_tgt_t *pa_realtgt;		/* real target (for -i) */
60 	mdb_tgt_t *pa_immtgt;		/* immediate target (for -i) */
61 	mdb_tgt_as_t pa_as;		/* address space to use for i/o */
62 	mdb_tgt_addr_t pa_addr;		/* base address for i/o */
63 	ulong_t pa_armemlim;		/* limit on array elements to print */
64 	ulong_t pa_arstrlim;		/* limit on array chars to print */
65 	const char *pa_delim;		/* element delimiter string */
66 	const char *pa_prefix;		/* element prefix string */
67 	const char *pa_suffix;		/* element suffix string */
68 	holeinfo_t *pa_holes;		/* hole detection information */
69 	int pa_nholes;			/* size of holes array */
70 	int pa_flags;			/* formatting flags (see below) */
71 	int pa_depth;			/* previous depth */
72 	int pa_nest;			/* array nesting depth */
73 	int pa_tab;			/* tabstop width */
74 	uint_t pa_maxdepth;		/* Limit max depth */
75 } printarg_t;
76 
77 #define	PA_SHOWTYPE	0x001		/* print type name */
78 #define	PA_SHOWBASETYPE	0x002		/* print base type name */
79 #define	PA_SHOWNAME	0x004		/* print member name */
80 #define	PA_SHOWADDR	0x008		/* print address */
81 #define	PA_SHOWVAL	0x010		/* print value */
82 #define	PA_SHOWHOLES	0x020		/* print holes in structs */
83 #define	PA_INTHEX	0x040		/* print integer values in hex */
84 #define	PA_INTDEC	0x080		/* print integer values in decimal */
85 #define	PA_NOSYMBOLIC	0x100		/* don't print ptrs as func+offset */
86 
87 #define	IS_CHAR(e) \
88 	(((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
89 	(CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
90 
91 #define	COMPOSITE_MASK	((1 << CTF_K_STRUCT) | \
92 			(1 << CTF_K_UNION) | (1 << CTF_K_ARRAY))
93 #define	IS_COMPOSITE(k)	(((1 << k) & COMPOSITE_MASK) != 0)
94 
95 #define	SOU_MASK	((1 << CTF_K_STRUCT) | (1 << CTF_K_UNION))
96 #define	IS_SOU(k)	(((1 << k) & SOU_MASK) != 0)
97 
98 #define	MEMBER_DELIM_ERR	-1
99 #define	MEMBER_DELIM_DONE	0
100 #define	MEMBER_DELIM_PTR	1
101 #define	MEMBER_DELIM_DOT	2
102 #define	MEMBER_DELIM_LBR	3
103 
104 typedef int printarg_f(const char *, const char *,
105     mdb_ctf_id_t, mdb_ctf_id_t, ulong_t, printarg_t *);
106 
107 static int elt_print(const char *, mdb_ctf_id_t, mdb_ctf_id_t, ulong_t, int,
108     void *);
109 static void print_close_sou(printarg_t *, int);
110 
111 /*
112  * Given an address, look up the symbol ID of the specified symbol in its
113  * containing module.  We only support lookups for exact matches.
114  */
115 static const char *
116 addr_to_sym(mdb_tgt_t *t, uintptr_t addr, char *name, size_t namelen,
117     GElf_Sym *symp, mdb_syminfo_t *sip)
118 {
119 	const mdb_map_t *mp;
120 	const char *p;
121 
122 	if (mdb_tgt_lookup_by_addr(t, addr, MDB_TGT_SYM_EXACT, name,
123 	    namelen, NULL, NULL) == -1)
124 		return (NULL); /* address does not exactly match a symbol */
125 
126 	if ((p = strrsplit(name, '`')) != NULL) {
127 		if (mdb_tgt_lookup_by_name(t, name, p, symp, sip) == -1)
128 			return (NULL);
129 		return (p);
130 	}
131 
132 	if ((mp = mdb_tgt_addr_to_map(t, addr)) == NULL)
133 		return (NULL); /* address does not fall within a mapping */
134 
135 	if (mdb_tgt_lookup_by_name(t, mp->map_name, name, symp, sip) == -1)
136 		return (NULL);
137 
138 	return (name);
139 }
140 
141 /*
142  * This lets dcmds be a little fancy with their processing of type arguments
143  * while still treating them more or less as a single argument.
144  * For example, if a command is invokes like this:
145  *
146  *   ::<dcmd> proc_t ...
147  *
148  * this function will just copy "proc_t" into the provided buffer. If the
149  * command is instead invoked like this:
150  *
151  *   ::<dcmd> struct proc ...
152  *
153  * this function will place the string "struct proc" into the provided buffer
154  * and increment the caller's argv and argc. This allows the caller to still
155  * treat the type argument logically as it would an other atomic argument.
156  */
157 int
158 args_to_typename(int *argcp, const mdb_arg_t **argvp, char *buf, size_t len)
159 {
160 	int argc = *argcp;
161 	const mdb_arg_t *argv = *argvp;
162 
163 	if (argc < 1 || argv->a_type != MDB_TYPE_STRING)
164 		return (DCMD_USAGE);
165 
166 	if (strcmp(argv->a_un.a_str, "struct") == 0 ||
167 	    strcmp(argv->a_un.a_str, "enum") == 0 ||
168 	    strcmp(argv->a_un.a_str, "union") == 0) {
169 		if (argc <= 1) {
170 			mdb_warn("%s is not a valid type\n", argv->a_un.a_str);
171 			return (DCMD_ABORT);
172 		}
173 
174 		if (argv[1].a_type != MDB_TYPE_STRING)
175 			return (DCMD_USAGE);
176 
177 		(void) mdb_snprintf(buf, len, "%s %s",
178 		    argv[0].a_un.a_str, argv[1].a_un.a_str);
179 
180 		*argcp = argc - 1;
181 		*argvp = argv + 1;
182 	} else {
183 		(void) mdb_snprintf(buf, len, "%s", argv[0].a_un.a_str);
184 	}
185 
186 	return (0);
187 }
188 
189 /*ARGSUSED*/
190 int
191 cmd_sizeof(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
192 {
193 	mdb_ctf_id_t id;
194 	char tn[MDB_SYM_NAMLEN];
195 	int ret;
196 
197 	if (flags & DCMD_ADDRSPEC)
198 		return (DCMD_USAGE);
199 
200 	if ((ret = args_to_typename(&argc, &argv, tn, sizeof (tn))) != 0)
201 		return (ret);
202 
203 	if (argc != 1)
204 		return (DCMD_USAGE);
205 
206 	if (mdb_ctf_lookup_by_name(tn, &id) != 0) {
207 		mdb_warn("failed to look up type %s", tn);
208 		return (DCMD_ERR);
209 	}
210 
211 	if (flags & DCMD_PIPE_OUT)
212 		mdb_printf("%#lr\n", mdb_ctf_type_size(id));
213 	else
214 		mdb_printf("sizeof (%s) = %#lr\n", tn, mdb_ctf_type_size(id));
215 
216 	return (DCMD_OK);
217 }
218 
219 int
220 cmd_sizeof_tab(mdb_tab_cookie_t *mcp, uint_t flags, int argc,
221     const mdb_arg_t *argv)
222 {
223 	char tn[MDB_SYM_NAMLEN];
224 	int ret;
225 
226 	if (argc == 0 && !(flags & DCMD_TAB_SPACE))
227 		return (0);
228 
229 	if (argc == 0 && (flags & DCMD_TAB_SPACE))
230 		return (mdb_tab_complete_type(mcp, NULL, MDB_TABC_NOPOINT));
231 
232 	if ((ret = mdb_tab_typename(&argc, &argv, tn, sizeof (tn))) < 0)
233 		return (ret);
234 
235 	if (argc == 1)
236 		return (mdb_tab_complete_type(mcp, tn, MDB_TABC_NOPOINT));
237 
238 	return (0);
239 }
240 
241 /*ARGSUSED*/
242 int
243 cmd_offsetof(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
244 {
245 	const char *member;
246 	mdb_ctf_id_t id;
247 	ulong_t off;
248 	char tn[MDB_SYM_NAMLEN];
249 	ssize_t sz;
250 	int ret;
251 
252 	if (flags & DCMD_ADDRSPEC)
253 		return (DCMD_USAGE);
254 
255 	if ((ret = args_to_typename(&argc, &argv, tn, sizeof (tn))) != 0)
256 		return (ret);
257 
258 	if (argc != 2 || argv[1].a_type != MDB_TYPE_STRING)
259 		return (DCMD_USAGE);
260 
261 	if (mdb_ctf_lookup_by_name(tn, &id) != 0) {
262 		mdb_warn("failed to look up type %s", tn);
263 		return (DCMD_ERR);
264 	}
265 
266 	member = argv[1].a_un.a_str;
267 
268 	if (mdb_ctf_member_info(id, member, &off, &id) != 0) {
269 		mdb_warn("failed to find member %s of type %s", member, tn);
270 		return (DCMD_ERR);
271 	}
272 
273 	if (flags & DCMD_PIPE_OUT) {
274 		if (off % NBBY != 0) {
275 			mdb_warn("member %s of type %s is not byte-aligned\n",
276 			    member, tn);
277 			return (DCMD_ERR);
278 		}
279 		mdb_printf("%#lr", off / NBBY);
280 		return (DCMD_OK);
281 	}
282 
283 	mdb_printf("offsetof (%s, %s) = %#lr",
284 	    tn, member, off / NBBY);
285 	if (off % NBBY != 0)
286 		mdb_printf(".%lr", off % NBBY);
287 
288 	if ((sz = mdb_ctf_type_size(id)) > 0)
289 		mdb_printf(", sizeof (...->%s) = %#lr", member, sz);
290 
291 	mdb_printf("\n");
292 
293 	return (DCMD_OK);
294 }
295 
296 /*ARGSUSED*/
297 static int
298 enum_prefix_scan_cb(const char *name, int value, void *arg)
299 {
300 	char *str = arg;
301 
302 	/*
303 	 * This function is called with every name in the enum.  We make
304 	 * "arg" be the common prefix, if any.
305 	 */
306 	if (str[0] == 0) {
307 		if (strlcpy(arg, name, MDB_SYM_NAMLEN) >= MDB_SYM_NAMLEN)
308 			return (1);
309 		return (0);
310 	}
311 
312 	while (*name == *str) {
313 		if (*str == 0) {
314 			if (str != arg) {
315 				str--;	/* don't smother a name completely */
316 			}
317 			break;
318 		}
319 		name++;
320 		str++;
321 	}
322 	*str = 0;
323 
324 	return (str == arg);	/* only continue if prefix is non-empty */
325 }
326 
327 struct enum_p2_info {
328 	intmax_t e_value;	/* value we're processing */
329 	char	*e_buf;		/* buffer for holding names */
330 	size_t	e_size;		/* size of buffer */
331 	size_t	e_prefix;	/* length of initial prefix */
332 	uint_t	e_allprefix;	/* apply prefix to first guy, too */
333 	uint_t	e_bits;		/* bits seen */
334 	uint8_t	e_found;	/* have we seen anything? */
335 	uint8_t	e_first;	/* does buf contain the first one? */
336 	uint8_t	e_zero;		/* have we seen a zero value? */
337 };
338 
339 static int
340 enum_p2_cb(const char *name, int bit_arg, void *arg)
341 {
342 	struct enum_p2_info *eiip = arg;
343 	uintmax_t bit = bit_arg;
344 
345 	if (bit != 0 && !ISP2(bit))
346 		return (1);	/* non-power-of-2; abort processing */
347 
348 	if ((bit == 0 && eiip->e_zero) ||
349 	    (bit != 0 && (eiip->e_bits & bit) != 0)) {
350 		return (0);	/* already seen this value */
351 	}
352 
353 	if (bit == 0)
354 		eiip->e_zero = 1;
355 	else
356 		eiip->e_bits |= bit;
357 
358 	if (eiip->e_buf != NULL && (eiip->e_value & bit) != 0) {
359 		char *buf = eiip->e_buf;
360 		size_t prefix = eiip->e_prefix;
361 
362 		if (eiip->e_found) {
363 			(void) strlcat(buf, "|", eiip->e_size);
364 
365 			if (eiip->e_first && !eiip->e_allprefix && prefix > 0) {
366 				char c1 = buf[prefix];
367 				char c2 = buf[prefix + 1];
368 				buf[prefix] = '{';
369 				buf[prefix + 1] = 0;
370 				mdb_printf("%s", buf);
371 				buf[prefix] = c1;
372 				buf[prefix + 1] = c2;
373 				mdb_printf("%s", buf + prefix);
374 			} else {
375 				mdb_printf("%s", buf);
376 			}
377 
378 		}
379 		/* skip the common prefix as necessary */
380 		if ((eiip->e_found || eiip->e_allprefix) &&
381 		    strlen(name) > prefix)
382 			name += prefix;
383 
384 		(void) strlcpy(eiip->e_buf, name, eiip->e_size);
385 		eiip->e_first = !eiip->e_found;
386 		eiip->e_found = 1;
387 	}
388 	return (0);
389 }
390 
391 static int
392 enum_is_p2(mdb_ctf_id_t id)
393 {
394 	struct enum_p2_info eii;
395 	bzero(&eii, sizeof (eii));
396 
397 	return (mdb_ctf_type_kind(id) == CTF_K_ENUM &&
398 	    mdb_ctf_enum_iter(id, enum_p2_cb, &eii) == 0 &&
399 	    eii.e_bits != 0);
400 }
401 
402 static int
403 enum_value_print_p2(mdb_ctf_id_t id, intmax_t value, uint_t allprefix)
404 {
405 	struct enum_p2_info eii;
406 	char prefix[MDB_SYM_NAMLEN + 2];
407 	intmax_t missed;
408 
409 	bzero(&eii, sizeof (eii));
410 
411 	eii.e_value = value;
412 	eii.e_buf = prefix;
413 	eii.e_size = sizeof (prefix);
414 	eii.e_allprefix = allprefix;
415 
416 	prefix[0] = 0;
417 	if (mdb_ctf_enum_iter(id, enum_prefix_scan_cb, prefix) == 0)
418 		eii.e_prefix = strlen(prefix);
419 
420 	if (mdb_ctf_enum_iter(id, enum_p2_cb, &eii) != 0 || eii.e_bits == 0)
421 		return (-1);
422 
423 	missed = (value & ~(intmax_t)eii.e_bits);
424 
425 	if (eii.e_found) {
426 		/* push out any final value, with a | if we missed anything */
427 		if (!eii.e_first)
428 			(void) strlcat(prefix, "}", sizeof (prefix));
429 		if (missed != 0)
430 			(void) strlcat(prefix, "|", sizeof (prefix));
431 
432 		mdb_printf("%s", prefix);
433 	}
434 
435 	if (!eii.e_found || missed) {
436 		mdb_printf("%#llx", missed);
437 	}
438 
439 	return (0);
440 }
441 
442 struct enum_cbinfo {
443 	uint_t		e_flags;
444 	const char	*e_string;	/* NULL for value searches */
445 	size_t		e_prefix;
446 	intmax_t	e_value;
447 	uint_t		e_found;
448 	mdb_ctf_id_t	e_id;
449 };
450 #define	E_PRETTY		0x01
451 #define	E_HEX			0x02
452 #define	E_SEARCH_STRING		0x04
453 #define	E_SEARCH_VALUE		0x08
454 #define	E_ELIDE_PREFIX		0x10
455 
456 static void
457 enum_print(struct enum_cbinfo *info, const char *name, int value)
458 {
459 	uint_t flags = info->e_flags;
460 	uint_t elide_prefix = (info->e_flags & E_ELIDE_PREFIX);
461 
462 	if (name != NULL && info->e_prefix && strlen(name) > info->e_prefix)
463 		name += info->e_prefix;
464 
465 	if (flags & E_PRETTY) {
466 		uint_t indent = 5 + ((flags & E_HEX) ? 8 : 11);
467 
468 		mdb_printf((flags & E_HEX)? "%8x " : "%11d ", value);
469 		(void) mdb_inc_indent(indent);
470 		if (name != NULL) {
471 			mdb_iob_puts(mdb.m_out, name);
472 		} else {
473 			(void) enum_value_print_p2(info->e_id, value,
474 			    elide_prefix);
475 		}
476 		(void) mdb_dec_indent(indent);
477 		mdb_printf("\n");
478 	} else {
479 		mdb_printf("%#r\n", value);
480 	}
481 }
482 
483 static int
484 enum_cb(const char *name, int value, void *arg)
485 {
486 	struct enum_cbinfo *info = arg;
487 	uint_t flags = info->e_flags;
488 
489 	if (flags & E_SEARCH_STRING) {
490 		if (strcmp(name, info->e_string) != 0)
491 			return (0);
492 
493 	} else if (flags & E_SEARCH_VALUE) {
494 		if (value != info->e_value)
495 			return (0);
496 	}
497 
498 	enum_print(info, name, value);
499 
500 	info->e_found = 1;
501 	return (0);
502 }
503 
504 void
505 enum_help(void)
506 {
507 	mdb_printf("%s",
508 "Without an address and name, print all values for the enumeration \"enum\".\n"
509 "With an address, look up a particular value in \"enum\".  With a name, look\n"
510 "up a particular name in \"enum\".\n");
511 
512 	(void) mdb_dec_indent(2);
513 	mdb_printf("\n%<b>OPTIONS%</b>\n");
514 	(void) mdb_inc_indent(2);
515 
516 	mdb_printf("%s",
517 "   -e    remove common prefixes from enum names\n"
518 "   -x    report enum values in hexadecimal\n");
519 }
520 
521 /*ARGSUSED*/
522 int
523 cmd_enum(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
524 {
525 	struct enum_cbinfo info;
526 
527 	char type[MDB_SYM_NAMLEN + sizeof ("enum ")];
528 	char tn2[MDB_SYM_NAMLEN + sizeof ("enum ")];
529 	char prefix[MDB_SYM_NAMLEN];
530 	mdb_ctf_id_t id;
531 	mdb_ctf_id_t idr;
532 
533 	int i;
534 	intmax_t search;
535 	uint_t isp2;
536 
537 	info.e_flags = (flags & DCMD_PIPE_OUT)? 0 : E_PRETTY;
538 	info.e_string = NULL;
539 	info.e_value = 0;
540 	info.e_found = 0;
541 
542 	i = mdb_getopts(argc, argv,
543 	    'e', MDB_OPT_SETBITS, E_ELIDE_PREFIX, &info.e_flags,
544 	    'x', MDB_OPT_SETBITS, E_HEX, &info.e_flags,
545 	    NULL);
546 
547 	argc -= i;
548 	argv += i;
549 
550 	if ((i = args_to_typename(&argc, &argv, type, MDB_SYM_NAMLEN)) != 0)
551 		return (i);
552 
553 	if (strchr(type, ' ') == NULL) {
554 		/*
555 		 * Check as an enumeration tag first, and fall back
556 		 * to checking for a typedef.  Yes, this means that
557 		 * anonymous enumerations whose typedefs conflict with
558 		 * an enum tag can't be accessed.  Don't do that.
559 		 */
560 		(void) mdb_snprintf(tn2, sizeof (tn2), "enum %s", type);
561 
562 		if (mdb_ctf_lookup_by_name(tn2, &id) == 0) {
563 			(void) strcpy(type, tn2);
564 		} else if (mdb_ctf_lookup_by_name(type, &id) != 0) {
565 			mdb_warn("types '%s', '%s'", tn2, type);
566 			return (DCMD_ERR);
567 		}
568 	} else {
569 		if (mdb_ctf_lookup_by_name(type, &id) != 0) {
570 			mdb_warn("'%s'", type);
571 			return (DCMD_ERR);
572 		}
573 	}
574 
575 	/* resolve it, and make sure we're looking at an enumeration */
576 	if (mdb_ctf_type_resolve(id, &idr) == -1) {
577 		mdb_warn("unable to resolve '%s'", type);
578 		return (DCMD_ERR);
579 	}
580 	if (mdb_ctf_type_kind(idr) != CTF_K_ENUM) {
581 		mdb_warn("'%s': not an enumeration\n", type);
582 		return (DCMD_ERR);
583 	}
584 
585 	info.e_id = idr;
586 
587 	if (argc > 2)
588 		return (DCMD_USAGE);
589 
590 	if (argc == 2) {
591 		if (flags & DCMD_ADDRSPEC) {
592 			mdb_warn("may only specify one of: name, address\n");
593 			return (DCMD_USAGE);
594 		}
595 
596 		if (argv[1].a_type == MDB_TYPE_STRING) {
597 			info.e_flags |= E_SEARCH_STRING;
598 			info.e_string = argv[1].a_un.a_str;
599 		} else if (argv[1].a_type == MDB_TYPE_IMMEDIATE) {
600 			info.e_flags |= E_SEARCH_VALUE;
601 			search = argv[1].a_un.a_val;
602 		} else {
603 			return (DCMD_USAGE);
604 		}
605 	}
606 
607 	if (flags & DCMD_ADDRSPEC) {
608 		info.e_flags |= E_SEARCH_VALUE;
609 		search = mdb_get_dot();
610 	}
611 
612 	if (info.e_flags & E_SEARCH_VALUE) {
613 		if ((int)search != search) {
614 			mdb_warn("value '%lld' out of enumeration range\n",
615 			    search);
616 		}
617 		info.e_value = search;
618 	}
619 
620 	isp2 = enum_is_p2(idr);
621 	if (isp2)
622 		info.e_flags |= E_HEX;
623 
624 	if (DCMD_HDRSPEC(flags) && (info.e_flags & E_PRETTY)) {
625 		if (info.e_flags & E_HEX)
626 			mdb_printf("%<u>%8s %-64s%</u>\n", "VALUE", "NAME");
627 		else
628 			mdb_printf("%<u>%11s %-64s%</u>\n", "VALUE", "NAME");
629 	}
630 
631 	/* if the enum is a power-of-two one, process it that way */
632 	if ((info.e_flags & E_SEARCH_VALUE) && isp2) {
633 		enum_print(&info, NULL, info.e_value);
634 		return (DCMD_OK);
635 	}
636 
637 	prefix[0] = 0;
638 	if ((info.e_flags & E_ELIDE_PREFIX) &&
639 	    mdb_ctf_enum_iter(id, enum_prefix_scan_cb, prefix) == 0)
640 		info.e_prefix = strlen(prefix);
641 
642 	if (mdb_ctf_enum_iter(idr, enum_cb, &info) == -1) {
643 		mdb_warn("cannot walk '%s' as enum", type);
644 		return (DCMD_ERR);
645 	}
646 
647 	if (info.e_found == 0 &&
648 	    (info.e_flags & (E_SEARCH_STRING | E_SEARCH_VALUE)) != 0) {
649 		if (info.e_flags & E_SEARCH_STRING)
650 			mdb_warn("name \"%s\" not in '%s'\n", info.e_string,
651 			    type);
652 		else
653 			mdb_warn("value %#lld not in '%s'\n", info.e_value,
654 			    type);
655 
656 		return (DCMD_ERR);
657 	}
658 
659 	return (DCMD_OK);
660 }
661 
662 static int
663 setup_vcb(const char *name, uintptr_t addr)
664 {
665 	const char *p;
666 	mdb_var_t *v;
667 
668 	if ((v = mdb_nv_lookup(&mdb.m_nv, name)) == NULL) {
669 		if ((p = strbadid(name)) != NULL) {
670 			mdb_warn("'%c' may not be used in a variable "
671 			    "name\n", *p);
672 			return (DCMD_ABORT);
673 		}
674 
675 		if ((v = mdb_nv_insert(&mdb.m_nv, name, NULL, addr, 0)) == NULL)
676 			return (DCMD_ERR);
677 	} else {
678 		if (v->v_flags & MDB_NV_RDONLY) {
679 			mdb_warn("variable %s is read-only\n", name);
680 			return (DCMD_ABORT);
681 		}
682 	}
683 
684 	/*
685 	 * If there already exists a vcb for this variable, we may be
686 	 * calling the dcmd in a loop.  We only create a vcb for this
687 	 * variable on the first invocation.
688 	 */
689 	if (mdb_vcb_find(v, mdb.m_frame) == NULL)
690 		mdb_vcb_insert(mdb_vcb_create(v), mdb.m_frame);
691 
692 	return (0);
693 }
694 
695 /*ARGSUSED*/
696 int
697 cmd_list(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
698 {
699 	int offset;
700 	uintptr_t a, tmp;
701 	int ret;
702 
703 	if (!(flags & DCMD_ADDRSPEC) || argc == 0)
704 		return (DCMD_USAGE);
705 
706 	if (argv->a_type != MDB_TYPE_STRING) {
707 		/*
708 		 * We are being given a raw offset in lieu of a type and
709 		 * member; confirm the arguments.
710 		 */
711 		if (argv->a_type != MDB_TYPE_IMMEDIATE)
712 			return (DCMD_USAGE);
713 
714 		offset = argv->a_un.a_val;
715 
716 		argv++;
717 		argc--;
718 
719 		if (offset % sizeof (uintptr_t)) {
720 			mdb_warn("offset must fall on a word boundary\n");
721 			return (DCMD_ABORT);
722 		}
723 	} else {
724 		const char *member;
725 		char buf[MDB_SYM_NAMLEN];
726 		int ret;
727 
728 		ret = args_to_typename(&argc, &argv, buf, sizeof (buf));
729 		if (ret != 0)
730 			return (ret);
731 
732 		argv++;
733 		argc--;
734 
735 		member = argv->a_un.a_str;
736 		offset = mdb_ctf_offsetof_by_name(buf, member);
737 		if (offset == -1)
738 			return (DCMD_ABORT);
739 
740 		argv++;
741 		argc--;
742 
743 		if (offset % (sizeof (uintptr_t)) != 0) {
744 			mdb_warn("%s is not a word-aligned member\n", member);
745 			return (DCMD_ABORT);
746 		}
747 	}
748 
749 	/*
750 	 * If we have any unchewed arguments, a variable name must be present.
751 	 */
752 	if (argc == 1) {
753 		if (argv->a_type != MDB_TYPE_STRING)
754 			return (DCMD_USAGE);
755 
756 		if ((ret = setup_vcb(argv->a_un.a_str, addr)) != 0)
757 			return (ret);
758 
759 	} else if (argc != 0) {
760 		return (DCMD_USAGE);
761 	}
762 
763 	a = addr;
764 
765 	do {
766 		mdb_printf("%lr\n", a);
767 
768 		if (mdb_vread(&tmp, sizeof (tmp), a + offset) == -1) {
769 			mdb_warn("failed to read next pointer from object %p",
770 			    a);
771 			return (DCMD_ERR);
772 		}
773 
774 		a = tmp;
775 	} while (a != addr && a != NULL);
776 
777 	return (DCMD_OK);
778 }
779 
780 int
781 cmd_array(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
782 {
783 	mdb_ctf_id_t id;
784 	ssize_t elemsize = 0;
785 	char tn[MDB_SYM_NAMLEN];
786 	int ret, nelem = -1;
787 
788 	mdb_tgt_t *t = mdb.m_target;
789 	GElf_Sym sym;
790 	mdb_ctf_arinfo_t ar;
791 	mdb_syminfo_t s_info;
792 
793 	if (!(flags & DCMD_ADDRSPEC))
794 		return (DCMD_USAGE);
795 
796 	if (argc >= 2) {
797 		ret = args_to_typename(&argc, &argv, tn, sizeof (tn));
798 		if (ret != 0)
799 			return (ret);
800 
801 		if (argc == 1)	/* unquoted compound type without count */
802 			return (DCMD_USAGE);
803 
804 		if (mdb_ctf_lookup_by_name(tn, &id) != 0) {
805 			mdb_warn("failed to look up type %s", tn);
806 			return (DCMD_ABORT);
807 		}
808 
809 		if (argv[1].a_type == MDB_TYPE_IMMEDIATE)
810 			nelem = argv[1].a_un.a_val;
811 		else
812 			nelem = mdb_strtoull(argv[1].a_un.a_str);
813 
814 		elemsize = mdb_ctf_type_size(id);
815 	} else if (addr_to_sym(t, addr, tn, sizeof (tn), &sym, &s_info)
816 	    != NULL && mdb_ctf_lookup_by_symbol(&sym, &s_info, &id)
817 	    == 0 && mdb_ctf_type_kind(id) == CTF_K_ARRAY &&
818 	    mdb_ctf_array_info(id, &ar) != -1) {
819 		elemsize = mdb_ctf_type_size(id) / ar.mta_nelems;
820 		nelem = ar.mta_nelems;
821 	} else {
822 		mdb_warn("no symbol information for %a", addr);
823 		return (DCMD_ERR);
824 	}
825 
826 	if (argc == 3 || argc == 1) {
827 		if (argv[argc - 1].a_type != MDB_TYPE_STRING)
828 			return (DCMD_USAGE);
829 
830 		if ((ret = setup_vcb(argv[argc - 1].a_un.a_str, addr)) != 0)
831 			return (ret);
832 
833 	} else if (argc > 3) {
834 		return (DCMD_USAGE);
835 	}
836 
837 	for (; nelem > 0; nelem--) {
838 		mdb_printf("%lr\n", addr);
839 		addr = addr + elemsize;
840 	}
841 
842 	return (DCMD_OK);
843 }
844 
845 /*
846  * Print an integer bitfield in hexadecimal by reading the enclosing byte(s)
847  * and then shifting and masking the data in the lower bits of a uint64_t.
848  */
849 static int
850 print_bitfield(ulong_t off, printarg_t *pap, ctf_encoding_t *ep)
851 {
852 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
853 	size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
854 	uint64_t mask = (1ULL << ep->cte_bits) - 1;
855 	uint64_t value = 0;
856 	uint8_t *buf = (uint8_t *)&value;
857 	uint8_t shift;
858 
859 	const char *format;
860 
861 	if (!(pap->pa_flags & PA_SHOWVAL))
862 		return (0);
863 
864 	if (ep->cte_bits > sizeof (value) * NBBY - 1) {
865 		mdb_printf("??? (invalid bitfield size %u)", ep->cte_bits);
866 		return (0);
867 	}
868 
869 	/*
870 	 * On big-endian machines, we need to adjust the buf pointer to refer
871 	 * to the lowest 'size' bytes in 'value', and we need shift based on
872 	 * the offset from the end of the data, not the offset of the start.
873 	 */
874 #ifdef _BIG_ENDIAN
875 	buf += sizeof (value) - size;
876 	off += ep->cte_bits;
877 #endif
878 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, buf, size, addr) != size) {
879 		mdb_warn("failed to read %lu bytes at %llx",
880 		    (ulong_t)size, addr);
881 		return (1);
882 	}
883 
884 	shift = off % NBBY;
885 
886 	/*
887 	 * Offsets are counted from opposite ends on little- and
888 	 * big-endian machines.
889 	 */
890 #ifdef _BIG_ENDIAN
891 	shift = NBBY - shift;
892 #endif
893 
894 	/*
895 	 * If the bits we want do not begin on a byte boundary, shift the data
896 	 * right so that the value is in the lowest 'cte_bits' of 'value'.
897 	 */
898 	if (off % NBBY != 0)
899 		value >>= shift;
900 	value &= mask;
901 
902 	/*
903 	 * We default to printing signed bitfields as decimals,
904 	 * and unsigned bitfields in hexadecimal.  If they specify
905 	 * hexadecimal, we treat the field as unsigned.
906 	 */
907 	if ((pap->pa_flags & PA_INTHEX) ||
908 	    !(ep->cte_format & CTF_INT_SIGNED)) {
909 		format = (pap->pa_flags & PA_INTDEC)? "%#llu" : "%#llx";
910 	} else {
911 		int sshift = sizeof (value) * NBBY - ep->cte_bits;
912 
913 		/* sign-extend value, and print as a signed decimal */
914 		value = ((int64_t)value << sshift) >> sshift;
915 		format = "%#lld";
916 	}
917 	mdb_printf(format, value);
918 
919 	return (0);
920 }
921 
922 /*
923  * Print out a character or integer value.  We use some simple heuristics,
924  * described below, to determine the appropriate radix to use for output.
925  */
926 static int
927 print_int_val(const char *type, ctf_encoding_t *ep, ulong_t off,
928     printarg_t *pap)
929 {
930 	static const char *const sformat[] = { "%#d", "%#d", "%#d", "%#lld" };
931 	static const char *const uformat[] = { "%#u", "%#u", "%#u", "%#llu" };
932 	static const char *const xformat[] = { "%#x", "%#x", "%#x", "%#llx" };
933 
934 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
935 	const char *const *fsp;
936 	size_t size;
937 
938 	union {
939 		uint64_t i8;
940 		uint32_t i4;
941 		uint16_t i2;
942 		uint8_t i1;
943 		time_t t;
944 	} u;
945 
946 	if (!(pap->pa_flags & PA_SHOWVAL))
947 		return (0);
948 
949 	if (ep->cte_format & CTF_INT_VARARGS) {
950 		mdb_printf("...\n");
951 		return (0);
952 	}
953 
954 	/*
955 	 * If the size is not a power-of-two number of bytes in the range 1-8
956 	 * then we assume it is a bitfield and print it as such.
957 	 */
958 	size = ep->cte_bits / NBBY;
959 	if (size > 8 || (ep->cte_bits % NBBY) != 0 || (size & (size - 1)) != 0)
960 		return (print_bitfield(off, pap, ep));
961 
962 	if (IS_CHAR(*ep)) {
963 		mdb_printf("'");
964 		if (mdb_fmt_print(pap->pa_tgt, pap->pa_as,
965 		    addr, 1, 'C') == addr)
966 			return (1);
967 		mdb_printf("'");
968 		return (0);
969 	}
970 
971 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.i8, size, addr) != size) {
972 		mdb_warn("failed to read %lu bytes at %llx",
973 		    (ulong_t)size, addr);
974 		return (1);
975 	}
976 
977 	/*
978 	 * We pretty-print time_t values as a calendar date and time.
979 	 */
980 	if (!(pap->pa_flags & (PA_INTHEX | PA_INTDEC)) &&
981 	    strcmp(type, "time_t") == 0 && u.t != 0) {
982 		mdb_printf("%Y", u.t);
983 		return (0);
984 	}
985 
986 	/*
987 	 * The default format is hexadecimal.
988 	 */
989 	if (!(pap->pa_flags & PA_INTDEC))
990 		fsp = xformat;
991 	else if (ep->cte_format & CTF_INT_SIGNED)
992 		fsp = sformat;
993 	else
994 		fsp = uformat;
995 
996 	switch (size) {
997 	case sizeof (uint8_t):
998 		mdb_printf(fsp[0], u.i1);
999 		break;
1000 	case sizeof (uint16_t):
1001 		mdb_printf(fsp[1], u.i2);
1002 		break;
1003 	case sizeof (uint32_t):
1004 		mdb_printf(fsp[2], u.i4);
1005 		break;
1006 	case sizeof (uint64_t):
1007 		mdb_printf(fsp[3], u.i8);
1008 		break;
1009 	}
1010 	return (0);
1011 }
1012 
1013 /*ARGSUSED*/
1014 static int
1015 print_int(const char *type, const char *name, mdb_ctf_id_t id,
1016     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1017 {
1018 	ctf_encoding_t e;
1019 
1020 	if (!(pap->pa_flags & PA_SHOWVAL))
1021 		return (0);
1022 
1023 	if (mdb_ctf_type_encoding(base, &e) != 0) {
1024 		mdb_printf("??? (%s)", mdb_strerror(errno));
1025 		return (0);
1026 	}
1027 
1028 	return (print_int_val(type, &e, off, pap));
1029 }
1030 
1031 /*
1032  * Print out a floating point value.  We only provide support for floats in
1033  * the ANSI-C float, double, and long double formats.
1034  */
1035 /*ARGSUSED*/
1036 static int
1037 print_float(const char *type, const char *name, mdb_ctf_id_t id,
1038     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1039 {
1040 #ifndef _KMDB
1041 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
1042 	ctf_encoding_t e;
1043 
1044 	union {
1045 		float f;
1046 		double d;
1047 		long double ld;
1048 	} u;
1049 
1050 	if (!(pap->pa_flags & PA_SHOWVAL))
1051 		return (0);
1052 
1053 	if (mdb_ctf_type_encoding(base, &e) == 0) {
1054 		if (e.cte_format == CTF_FP_SINGLE &&
1055 		    e.cte_bits == sizeof (float) * NBBY) {
1056 			if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.f,
1057 			    sizeof (u.f), addr) != sizeof (u.f)) {
1058 				mdb_warn("failed to read float at %llx", addr);
1059 				return (1);
1060 			}
1061 			mdb_printf("%s", doubletos(u.f, 7, 'e'));
1062 
1063 		} else if (e.cte_format == CTF_FP_DOUBLE &&
1064 		    e.cte_bits == sizeof (double) * NBBY) {
1065 			if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.d,
1066 			    sizeof (u.d), addr) != sizeof (u.d)) {
1067 				mdb_warn("failed to read float at %llx", addr);
1068 				return (1);
1069 			}
1070 			mdb_printf("%s", doubletos(u.d, 7, 'e'));
1071 
1072 		} else if (e.cte_format == CTF_FP_LDOUBLE &&
1073 		    e.cte_bits == sizeof (long double) * NBBY) {
1074 			if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.ld,
1075 			    sizeof (u.ld), addr) != sizeof (u.ld)) {
1076 				mdb_warn("failed to read float at %llx", addr);
1077 				return (1);
1078 			}
1079 			mdb_printf("%s", longdoubletos(&u.ld, 16, 'e'));
1080 
1081 		} else {
1082 			mdb_printf("??? (unsupported FP format %u / %u bits\n",
1083 			    e.cte_format, e.cte_bits);
1084 		}
1085 	} else
1086 		mdb_printf("??? (%s)", mdb_strerror(errno));
1087 #else
1088 	mdb_printf("<FLOAT>");
1089 #endif
1090 	return (0);
1091 }
1092 
1093 
1094 /*
1095  * Print out a pointer value as a symbol name + offset or a hexadecimal value.
1096  * If the pointer itself is a char *, we attempt to read a bit of the data
1097  * referenced by the pointer and display it if it is a printable ASCII string.
1098  */
1099 /*ARGSUSED*/
1100 static int
1101 print_ptr(const char *type, const char *name, mdb_ctf_id_t id,
1102     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1103 {
1104 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
1105 	ctf_encoding_t e;
1106 	uintptr_t value;
1107 	char buf[256];
1108 	ssize_t len;
1109 
1110 	if (!(pap->pa_flags & PA_SHOWVAL))
1111 		return (0);
1112 
1113 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as,
1114 	    &value, sizeof (value), addr) != sizeof (value)) {
1115 		mdb_warn("failed to read %s pointer at %llx", name, addr);
1116 		return (1);
1117 	}
1118 
1119 	if (pap->pa_flags & PA_NOSYMBOLIC) {
1120 		mdb_printf("%#lx", value);
1121 		return (0);
1122 	}
1123 
1124 	mdb_printf("%a", value);
1125 
1126 	if (value == NULL || strcmp(type, "caddr_t") == 0)
1127 		return (0);
1128 
1129 	if (mdb_ctf_type_kind(base) == CTF_K_POINTER &&
1130 	    mdb_ctf_type_reference(base, &base) != -1 &&
1131 	    mdb_ctf_type_resolve(base, &base) != -1 &&
1132 	    mdb_ctf_type_encoding(base, &e) == 0 && IS_CHAR(e)) {
1133 		if ((len = mdb_tgt_readstr(pap->pa_realtgt, pap->pa_as,
1134 		    buf, sizeof (buf), value)) >= 0 && strisprint(buf)) {
1135 			if (len == sizeof (buf))
1136 				(void) strabbr(buf, sizeof (buf));
1137 			mdb_printf(" \"%s\"", buf);
1138 		}
1139 	}
1140 
1141 	return (0);
1142 }
1143 
1144 
1145 /*
1146  * Print out a fixed-size array.  We special-case arrays of characters
1147  * and attempt to print them out as ASCII strings if possible.  For other
1148  * arrays, we iterate over a maximum of pa_armemlim members and call
1149  * mdb_ctf_type_visit() again on each element to print its value.
1150  */
1151 /*ARGSUSED*/
1152 static int
1153 print_array(const char *type, const char *name, mdb_ctf_id_t id,
1154     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1155 {
1156 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
1157 	printarg_t pa = *pap;
1158 	ssize_t eltsize;
1159 	mdb_ctf_arinfo_t r;
1160 	ctf_encoding_t e;
1161 	uint_t i, kind, limit;
1162 	int d, sou;
1163 	char buf[8];
1164 	char *str;
1165 
1166 	if (!(pap->pa_flags & PA_SHOWVAL))
1167 		return (0);
1168 
1169 	if (pap->pa_depth == pap->pa_maxdepth) {
1170 		mdb_printf("[ ... ]");
1171 		return (0);
1172 	}
1173 
1174 	/*
1175 	 * Determine the base type and size of the array's content.  If this
1176 	 * fails, we cannot print anything and just give up.
1177 	 */
1178 	if (mdb_ctf_array_info(base, &r) == -1 ||
1179 	    mdb_ctf_type_resolve(r.mta_contents, &base) == -1 ||
1180 	    (eltsize = mdb_ctf_type_size(base)) == -1) {
1181 		mdb_printf("[ ??? ] (%s)", mdb_strerror(errno));
1182 		return (0);
1183 	}
1184 
1185 	/*
1186 	 * Read a few bytes and determine if the content appears to be
1187 	 * printable ASCII characters.  If so, read the entire array and
1188 	 * attempt to display it as a string if it is printable.
1189 	 */
1190 	if ((pap->pa_arstrlim == MDB_ARR_NOLIMIT ||
1191 	    r.mta_nelems <= pap->pa_arstrlim) &&
1192 	    mdb_ctf_type_encoding(base, &e) == 0 && IS_CHAR(e) &&
1193 	    mdb_tgt_readstr(pap->pa_tgt, pap->pa_as, buf,
1194 	    MIN(sizeof (buf), r.mta_nelems), addr) > 0 && strisprint(buf)) {
1195 
1196 		str = mdb_alloc(r.mta_nelems + 1, UM_SLEEP | UM_GC);
1197 		str[r.mta_nelems] = '\0';
1198 
1199 		if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, str,
1200 		    r.mta_nelems, addr) != r.mta_nelems) {
1201 			mdb_warn("failed to read char array at %llx", addr);
1202 			return (1);
1203 		}
1204 
1205 		if (strisprint(str)) {
1206 			mdb_printf("[ \"%s\" ]", str);
1207 			return (0);
1208 		}
1209 	}
1210 
1211 	if (pap->pa_armemlim != MDB_ARR_NOLIMIT)
1212 		limit = MIN(r.mta_nelems, pap->pa_armemlim);
1213 	else
1214 		limit = r.mta_nelems;
1215 
1216 	if (limit == 0) {
1217 		mdb_printf("[ ... ]");
1218 		return (0);
1219 	}
1220 
1221 	kind = mdb_ctf_type_kind(base);
1222 	sou = IS_COMPOSITE(kind);
1223 
1224 	pa.pa_addr = addr;		/* set base address to start of array */
1225 	pa.pa_maxdepth = pa.pa_maxdepth - pa.pa_depth - 1;
1226 	pa.pa_nest += pa.pa_depth + 1;	/* nesting level is current depth + 1 */
1227 	pa.pa_depth = 0;		/* reset depth to 0 for new scope */
1228 	pa.pa_prefix = NULL;
1229 
1230 	if (sou) {
1231 		pa.pa_delim = "\n";
1232 		mdb_printf("[\n");
1233 	} else {
1234 		pa.pa_flags &= ~(PA_SHOWTYPE | PA_SHOWNAME | PA_SHOWADDR);
1235 		pa.pa_delim = ", ";
1236 		mdb_printf("[ ");
1237 	}
1238 
1239 	for (i = 0; i < limit; i++, pa.pa_addr += eltsize) {
1240 		if (i == limit - 1 && !sou) {
1241 			if (limit < r.mta_nelems)
1242 				pa.pa_delim = ", ... ]";
1243 			else
1244 				pa.pa_delim = " ]";
1245 		}
1246 
1247 		if (mdb_ctf_type_visit(r.mta_contents, elt_print, &pa) == -1) {
1248 			mdb_warn("failed to print array data");
1249 			return (1);
1250 		}
1251 	}
1252 
1253 	if (sou) {
1254 		for (d = pa.pa_depth - 1; d >= 0; d--)
1255 			print_close_sou(&pa, d);
1256 
1257 		if (limit < r.mta_nelems) {
1258 			mdb_printf("%*s... ]",
1259 			    (pap->pa_depth + pap->pa_nest) * pap->pa_tab, "");
1260 		} else {
1261 			mdb_printf("%*s]",
1262 			    (pap->pa_depth + pap->pa_nest) * pap->pa_tab, "");
1263 		}
1264 	}
1265 
1266 	/* copy the hole array info, since it may have been grown */
1267 	pap->pa_holes = pa.pa_holes;
1268 	pap->pa_nholes = pa.pa_nholes;
1269 
1270 	return (0);
1271 }
1272 
1273 /*
1274  * Print out a struct or union header.  We need only print the open brace
1275  * because mdb_ctf_type_visit() itself will automatically recurse through
1276  * all members of the given struct or union.
1277  */
1278 /*ARGSUSED*/
1279 static int
1280 print_sou(const char *type, const char *name, mdb_ctf_id_t id,
1281     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1282 {
1283 	if (pap->pa_depth == pap->pa_maxdepth)
1284 		mdb_printf("{ ... }");
1285 	else
1286 		mdb_printf("{");
1287 	pap->pa_delim = "\n";
1288 	return (0);
1289 }
1290 
1291 /*
1292  * Print an enum value.  We attempt to convert the value to the corresponding
1293  * enum name and print that if possible.
1294  */
1295 /*ARGSUSED*/
1296 static int
1297 print_enum(const char *type, const char *name, mdb_ctf_id_t id,
1298     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1299 {
1300 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
1301 	const char *ename;
1302 	int value;
1303 	int isp2 = enum_is_p2(base);
1304 	int flags = pap->pa_flags | (isp2 ? PA_INTHEX : 0);
1305 
1306 	if (!(flags & PA_SHOWVAL))
1307 		return (0);
1308 
1309 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as,
1310 	    &value, sizeof (value), addr) != sizeof (value)) {
1311 		mdb_warn("failed to read %s integer at %llx", name, addr);
1312 		return (1);
1313 	}
1314 
1315 	if (flags & PA_INTHEX)
1316 		mdb_printf("%#x", value);
1317 	else
1318 		mdb_printf("%#d", value);
1319 
1320 	(void) mdb_inc_indent(8);
1321 	mdb_printf(" (");
1322 
1323 	if (!isp2 || enum_value_print_p2(base, value, 0) != 0) {
1324 		ename = mdb_ctf_enum_name(base, value);
1325 		if (ename == NULL) {
1326 			ename = "???";
1327 		}
1328 		mdb_printf("%s", ename);
1329 	}
1330 	mdb_printf(")");
1331 	(void) mdb_dec_indent(8);
1332 
1333 	return (0);
1334 }
1335 
1336 /*
1337  * This will only get called if the structure isn't found in any available CTF
1338  * data.
1339  */
1340 /*ARGSUSED*/
1341 static int
1342 print_tag(const char *type, const char *name, mdb_ctf_id_t id,
1343     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1344 {
1345 	char basename[MDB_SYM_NAMLEN];
1346 
1347 	if (pap->pa_flags & PA_SHOWVAL)
1348 		mdb_printf("; ");
1349 
1350 	if (mdb_ctf_type_name(base, basename, sizeof (basename)) != NULL)
1351 		mdb_printf("<forward declaration of %s>", basename);
1352 	else
1353 		mdb_printf("<forward declaration of unknown type>");
1354 
1355 	return (0);
1356 }
1357 
1358 static void
1359 print_hole(printarg_t *pap, int depth, ulong_t off, ulong_t endoff)
1360 {
1361 	ulong_t bits = endoff - off;
1362 	ulong_t size = bits / NBBY;
1363 	ctf_encoding_t e;
1364 
1365 	static const char *const name = "<<HOLE>>";
1366 	char type[MDB_SYM_NAMLEN];
1367 
1368 	int bitfield =
1369 	    (off % NBBY != 0 ||
1370 	    bits % NBBY != 0 ||
1371 	    size > 8 ||
1372 	    (size & (size - 1)) != 0);
1373 
1374 	ASSERT(off < endoff);
1375 
1376 	if (bits > NBBY * sizeof (uint64_t)) {
1377 		ulong_t end;
1378 
1379 		/*
1380 		 * The hole is larger than the largest integer type.  To
1381 		 * handle this, we split up the hole at 8-byte-aligned
1382 		 * boundaries, recursing to print each subsection.  For
1383 		 * normal C structures, we'll loop at most twice.
1384 		 */
1385 		for (; off < endoff; off = end) {
1386 			end = P2END(off, NBBY * sizeof (uint64_t));
1387 			if (end > endoff)
1388 				end = endoff;
1389 
1390 			ASSERT((end - off) <= NBBY * sizeof (uint64_t));
1391 			print_hole(pap, depth, off, end);
1392 		}
1393 		ASSERT(end == endoff);
1394 
1395 		return;
1396 	}
1397 
1398 	if (bitfield)
1399 		(void) mdb_snprintf(type, sizeof (type), "unsigned");
1400 	else
1401 		(void) mdb_snprintf(type, sizeof (type), "uint%d_t", bits);
1402 
1403 	if (pap->pa_flags & (PA_SHOWTYPE | PA_SHOWNAME | PA_SHOWADDR))
1404 		mdb_printf("%*s", (depth + pap->pa_nest) * pap->pa_tab, "");
1405 
1406 	if (pap->pa_flags & PA_SHOWADDR) {
1407 		if (off % NBBY == 0)
1408 			mdb_printf("%llx ", pap->pa_addr + off / NBBY);
1409 		else
1410 			mdb_printf("%llx.%lx ",
1411 			    pap->pa_addr + off / NBBY, off % NBBY);
1412 	}
1413 
1414 	if (pap->pa_flags & PA_SHOWTYPE)
1415 		mdb_printf("%s ", type);
1416 
1417 	if (pap->pa_flags & PA_SHOWNAME)
1418 		mdb_printf("%s", name);
1419 
1420 	if (bitfield && (pap->pa_flags & PA_SHOWTYPE))
1421 		mdb_printf(" :%d", bits);
1422 
1423 	mdb_printf("%s ", (pap->pa_flags & PA_SHOWVAL)? " =" : "");
1424 
1425 	/*
1426 	 * We fake up a ctf_encoding_t, and use print_int_val() to print
1427 	 * the value.  Holes are always processed as unsigned integers.
1428 	 */
1429 	bzero(&e, sizeof (e));
1430 	e.cte_format = 0;
1431 	e.cte_offset = 0;
1432 	e.cte_bits = bits;
1433 
1434 	if (print_int_val(type, &e, off, pap) != 0)
1435 		mdb_iob_discard(mdb.m_out);
1436 	else
1437 		mdb_iob_puts(mdb.m_out, pap->pa_delim);
1438 }
1439 
1440 /*
1441  * The print_close_sou() function is called for each structure or union
1442  * which has been completed.  For structures, we detect and print any holes
1443  * before printing the closing brace.
1444  */
1445 static void
1446 print_close_sou(printarg_t *pap, int newdepth)
1447 {
1448 	int d = newdepth + pap->pa_nest;
1449 
1450 	if ((pap->pa_flags & PA_SHOWHOLES) && !pap->pa_holes[d].hi_isunion) {
1451 		ulong_t end = pap->pa_holes[d + 1].hi_offset;
1452 		ulong_t expected = pap->pa_holes[d].hi_offset;
1453 
1454 		if (end < expected)
1455 			print_hole(pap, newdepth + 1, end, expected);
1456 	}
1457 	/* if the struct is an array element, print a comma after the } */
1458 	mdb_printf("%*s}%s\n", d * pap->pa_tab, "",
1459 	    (newdepth == 0 && pap->pa_nest > 0)? "," : "");
1460 }
1461 
1462 static printarg_f *const printfuncs[] = {
1463 	print_int,	/* CTF_K_INTEGER */
1464 	print_float,	/* CTF_K_FLOAT */
1465 	print_ptr,	/* CTF_K_POINTER */
1466 	print_array,	/* CTF_K_ARRAY */
1467 	print_ptr,	/* CTF_K_FUNCTION */
1468 	print_sou,	/* CTF_K_STRUCT */
1469 	print_sou,	/* CTF_K_UNION */
1470 	print_enum,	/* CTF_K_ENUM */
1471 	print_tag	/* CTF_K_FORWARD */
1472 };
1473 
1474 /*
1475  * The elt_print function is used as the mdb_ctf_type_visit callback.  For
1476  * each element, we print an appropriate name prefix and then call the
1477  * print subroutine for this type class in the array above.
1478  */
1479 static int
1480 elt_print(const char *name, mdb_ctf_id_t id, mdb_ctf_id_t base,
1481     ulong_t off, int depth, void *data)
1482 {
1483 	char type[MDB_SYM_NAMLEN + sizeof (" <<12345678...>>")];
1484 	int kind, rc, d;
1485 	printarg_t *pap = data;
1486 
1487 	for (d = pap->pa_depth - 1; d >= depth; d--)
1488 		print_close_sou(pap, d);
1489 
1490 	if (depth > pap->pa_maxdepth)
1491 		return (0);
1492 
1493 	if (!mdb_ctf_type_valid(base) ||
1494 	    (kind = mdb_ctf_type_kind(base)) == -1)
1495 		return (-1); /* errno is set for us */
1496 
1497 	if (mdb_ctf_type_name(id, type, MDB_SYM_NAMLEN) == NULL)
1498 		(void) strcpy(type, "(?)");
1499 
1500 	if (pap->pa_flags & PA_SHOWBASETYPE) {
1501 		/*
1502 		 * If basetype is different and informative, concatenate
1503 		 * <<basetype>> (or <<baset...>> if it doesn't fit)
1504 		 *
1505 		 * We just use the end of the buffer to store the type name, and
1506 		 * only connect it up if that's necessary.
1507 		 */
1508 
1509 		char *type_end = type + strlen(type);
1510 		char *basetype;
1511 		size_t sz;
1512 
1513 		(void) strlcat(type, " <<", sizeof (type));
1514 
1515 		basetype = type + strlen(type);
1516 		sz = sizeof (type) - (basetype - type);
1517 
1518 		*type_end = '\0'; /* restore the end of type for strcmp() */
1519 
1520 		if (mdb_ctf_type_name(base, basetype, sz) != NULL &&
1521 		    strcmp(basetype, type) != 0 &&
1522 		    strcmp(basetype, "struct ") != 0 &&
1523 		    strcmp(basetype, "enum ") != 0 &&
1524 		    strcmp(basetype, "union ") != 0) {
1525 			type_end[0] = ' ';	/* reconnect */
1526 			if (strlcat(type, ">>", sizeof (type)) >= sizeof (type))
1527 				(void) strlcpy(
1528 				    type + sizeof (type) - 6, "...>>", 6);
1529 		}
1530 	}
1531 
1532 	if (pap->pa_flags & PA_SHOWHOLES) {
1533 		ctf_encoding_t e;
1534 		ssize_t nsize;
1535 		ulong_t newoff;
1536 		holeinfo_t *hole;
1537 		int extra = IS_COMPOSITE(kind)? 1 : 0;
1538 
1539 		/*
1540 		 * grow the hole array, if necessary
1541 		 */
1542 		if (pap->pa_nest + depth + extra >= pap->pa_nholes) {
1543 			int new = MAX(MAX(8, pap->pa_nholes * 2),
1544 			    pap->pa_nest + depth + extra + 1);
1545 
1546 			holeinfo_t *nhi = mdb_zalloc(
1547 			    sizeof (*nhi) * new, UM_NOSLEEP | UM_GC);
1548 
1549 			bcopy(pap->pa_holes, nhi,
1550 			    pap->pa_nholes * sizeof (*nhi));
1551 
1552 			pap->pa_holes = nhi;
1553 			pap->pa_nholes = new;
1554 		}
1555 
1556 		hole = &pap->pa_holes[depth + pap->pa_nest];
1557 
1558 		if (depth != 0 && off > hole->hi_offset)
1559 			print_hole(pap, depth, hole->hi_offset, off);
1560 
1561 		/* compute the next expected offset */
1562 		if (kind == CTF_K_INTEGER &&
1563 		    mdb_ctf_type_encoding(base, &e) == 0)
1564 			newoff = off + e.cte_bits;
1565 		else if ((nsize = mdb_ctf_type_size(base)) >= 0)
1566 			newoff = off + nsize * NBBY;
1567 		else {
1568 			/* something bad happened, disable hole checking */
1569 			newoff = -1UL;		/* ULONG_MAX */
1570 		}
1571 
1572 		hole->hi_offset = newoff;
1573 
1574 		if (IS_COMPOSITE(kind)) {
1575 			hole->hi_isunion = (kind == CTF_K_UNION);
1576 			hole++;
1577 			hole->hi_offset = off;
1578 		}
1579 	}
1580 
1581 	if (pap->pa_flags & (PA_SHOWTYPE | PA_SHOWNAME | PA_SHOWADDR))
1582 		mdb_printf("%*s", (depth + pap->pa_nest) * pap->pa_tab, "");
1583 
1584 	if (pap->pa_flags & PA_SHOWADDR) {
1585 		if (off % NBBY == 0)
1586 			mdb_printf("%llx ", pap->pa_addr + off / NBBY);
1587 		else
1588 			mdb_printf("%llx.%lx ",
1589 			    pap->pa_addr + off / NBBY, off % NBBY);
1590 	}
1591 
1592 	if ((pap->pa_flags & PA_SHOWTYPE)) {
1593 		mdb_printf("%s", type);
1594 		/*
1595 		 * We want to avoid printing a trailing space when
1596 		 * dealing with pointers in a structure, so we end
1597 		 * up with:
1598 		 *
1599 		 *	label_t *t_onfault = 0
1600 		 *
1601 		 * If depth is zero, always print the trailing space unless
1602 		 * we also have a prefix.
1603 		 */
1604 		if (type[strlen(type) - 1] != '*' ||
1605 		    (depth == 0 && (!(pap->pa_flags & PA_SHOWNAME) ||
1606 		    pap->pa_prefix == NULL)))
1607 			mdb_printf(" ");
1608 	}
1609 
1610 	if (pap->pa_flags & PA_SHOWNAME) {
1611 		if (pap->pa_prefix != NULL && depth <= 1)
1612 			mdb_printf("%s%s", pap->pa_prefix,
1613 			    (depth == 0) ? "" : pap->pa_suffix);
1614 		mdb_printf("%s", name);
1615 	}
1616 
1617 	if ((pap->pa_flags & PA_SHOWTYPE) && kind == CTF_K_INTEGER) {
1618 		ctf_encoding_t e;
1619 
1620 		if (mdb_ctf_type_encoding(base, &e) == 0) {
1621 			ulong_t bits = e.cte_bits;
1622 			ulong_t size = bits / NBBY;
1623 
1624 			if (bits % NBBY != 0 ||
1625 			    off % NBBY != 0 ||
1626 			    size > 8 ||
1627 			    size != mdb_ctf_type_size(base))
1628 				mdb_printf(" :%d", bits);
1629 		}
1630 	}
1631 
1632 	if (depth != 0 ||
1633 	    ((pap->pa_flags & PA_SHOWNAME) && pap->pa_prefix != NULL))
1634 		mdb_printf("%s ", pap->pa_flags & PA_SHOWVAL ? " =" : "");
1635 
1636 	if (depth == 0 && pap->pa_prefix != NULL)
1637 		name = pap->pa_prefix;
1638 
1639 	pap->pa_depth = depth;
1640 	if (kind <= CTF_K_UNKNOWN || kind >= CTF_K_TYPEDEF) {
1641 		mdb_warn("unknown ctf for %s type %s kind %d\n",
1642 		    name, type, kind);
1643 		return (-1);
1644 	}
1645 	rc = printfuncs[kind - 1](type, name, id, base, off, pap);
1646 
1647 	if (rc != 0)
1648 		mdb_iob_discard(mdb.m_out);
1649 	else
1650 		mdb_iob_puts(mdb.m_out, pap->pa_delim);
1651 
1652 	return (rc);
1653 }
1654 
1655 /*
1656  * Special semantics for pipelines.
1657  */
1658 static int
1659 pipe_print(mdb_ctf_id_t id, ulong_t off, void *data)
1660 {
1661 	printarg_t *pap = data;
1662 	ssize_t size;
1663 	static const char *const fsp[] = { "%#r", "%#r", "%#r", "%#llr" };
1664 	uintptr_t value;
1665 	uintptr_t addr = pap->pa_addr + off / NBBY;
1666 	mdb_ctf_id_t base;
1667 	ctf_encoding_t e;
1668 
1669 	union {
1670 		uint64_t i8;
1671 		uint32_t i4;
1672 		uint16_t i2;
1673 		uint8_t i1;
1674 	} u;
1675 
1676 	if (mdb_ctf_type_resolve(id, &base) == -1) {
1677 		mdb_warn("could not resolve type");
1678 		return (-1);
1679 	}
1680 
1681 	/*
1682 	 * If the user gives -a, then always print out the address of the
1683 	 * member.
1684 	 */
1685 	if ((pap->pa_flags & PA_SHOWADDR)) {
1686 		mdb_printf("%#lr\n", addr);
1687 		return (0);
1688 	}
1689 
1690 again:
1691 	switch (mdb_ctf_type_kind(base)) {
1692 	case CTF_K_POINTER:
1693 		if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as,
1694 		    &value, sizeof (value), addr) != sizeof (value)) {
1695 			mdb_warn("failed to read pointer at %p", addr);
1696 			return (-1);
1697 		}
1698 		mdb_printf("%#lr\n", value);
1699 		break;
1700 
1701 	case CTF_K_INTEGER:
1702 	case CTF_K_ENUM:
1703 		if (mdb_ctf_type_encoding(base, &e) != 0) {
1704 			mdb_printf("could not get type encoding\n");
1705 			return (-1);
1706 		}
1707 
1708 		/*
1709 		 * For immediate values, we just print out the value.
1710 		 */
1711 		size = e.cte_bits / NBBY;
1712 		if (size > 8 || (e.cte_bits % NBBY) != 0 ||
1713 		    (size & (size - 1)) != 0) {
1714 			return (print_bitfield(off, pap, &e));
1715 		}
1716 
1717 		if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.i8, size,
1718 		    addr) != size) {
1719 			mdb_warn("failed to read %lu bytes at %p",
1720 			    (ulong_t)size, pap->pa_addr);
1721 			return (-1);
1722 		}
1723 
1724 		switch (size) {
1725 		case sizeof (uint8_t):
1726 			mdb_printf(fsp[0], u.i1);
1727 			break;
1728 		case sizeof (uint16_t):
1729 			mdb_printf(fsp[1], u.i2);
1730 			break;
1731 		case sizeof (uint32_t):
1732 			mdb_printf(fsp[2], u.i4);
1733 			break;
1734 		case sizeof (uint64_t):
1735 			mdb_printf(fsp[3], u.i8);
1736 			break;
1737 		}
1738 		mdb_printf("\n");
1739 		break;
1740 
1741 	case CTF_K_FUNCTION:
1742 	case CTF_K_FLOAT:
1743 	case CTF_K_ARRAY:
1744 	case CTF_K_UNKNOWN:
1745 	case CTF_K_STRUCT:
1746 	case CTF_K_UNION:
1747 	case CTF_K_FORWARD:
1748 		/*
1749 		 * For these types, always print the address of the member
1750 		 */
1751 		mdb_printf("%#lr\n", addr);
1752 		break;
1753 
1754 	default:
1755 		mdb_warn("unknown type %d", mdb_ctf_type_kind(base));
1756 		break;
1757 	}
1758 
1759 	return (0);
1760 }
1761 
1762 static int
1763 parse_delimiter(char **strp)
1764 {
1765 	switch (**strp) {
1766 	case '\0':
1767 		return (MEMBER_DELIM_DONE);
1768 
1769 	case '.':
1770 		*strp = *strp + 1;
1771 		return (MEMBER_DELIM_DOT);
1772 
1773 	case '[':
1774 		*strp = *strp + 1;
1775 		return (MEMBER_DELIM_LBR);
1776 
1777 	case '-':
1778 		*strp = *strp + 1;
1779 		if (**strp == '>') {
1780 			*strp = *strp + 1;
1781 			return (MEMBER_DELIM_PTR);
1782 		}
1783 		*strp = *strp - 1;
1784 		/*FALLTHROUGH*/
1785 	default:
1786 		return (MEMBER_DELIM_ERR);
1787 	}
1788 }
1789 
1790 static int
1791 deref(printarg_t *pap, size_t size)
1792 {
1793 	uint32_t a32;
1794 	mdb_tgt_as_t as = pap->pa_as;
1795 	mdb_tgt_addr_t *ap = &pap->pa_addr;
1796 
1797 	if (size == sizeof (mdb_tgt_addr_t)) {
1798 		if (mdb_tgt_aread(mdb.m_target, as, ap, size, *ap) == -1) {
1799 			mdb_warn("could not dereference pointer %llx\n", *ap);
1800 			return (-1);
1801 		}
1802 	} else {
1803 		if (mdb_tgt_aread(mdb.m_target, as, &a32, size, *ap) == -1) {
1804 			mdb_warn("could not dereference pointer %x\n", *ap);
1805 			return (-1);
1806 		}
1807 
1808 		*ap = (mdb_tgt_addr_t)a32;
1809 	}
1810 
1811 	/*
1812 	 * We've dereferenced at least once, we must be on the real
1813 	 * target. If we were in the immediate target, reset to the real
1814 	 * target; it's reset as needed when we return to the print
1815 	 * routines.
1816 	 */
1817 	if (pap->pa_tgt == pap->pa_immtgt)
1818 		pap->pa_tgt = pap->pa_realtgt;
1819 
1820 	return (0);
1821 }
1822 
1823 static int
1824 parse_member(printarg_t *pap, const char *str, mdb_ctf_id_t id,
1825     mdb_ctf_id_t *idp, ulong_t *offp, int *last_deref)
1826 {
1827 	int delim;
1828 	char member[64];
1829 	char buf[128];
1830 	uint_t index;
1831 	char *start = (char *)str;
1832 	char *end;
1833 	ulong_t off = 0;
1834 	mdb_ctf_arinfo_t ar;
1835 	mdb_ctf_id_t rid;
1836 	int kind;
1837 	ssize_t size;
1838 	int non_array = FALSE;
1839 
1840 	/*
1841 	 * id always has the unresolved type for printing error messages
1842 	 * that include the type; rid always has the resolved type for
1843 	 * use in mdb_ctf_* calls.  It is possible for this command to fail,
1844 	 * however, if the resolved type is in the parent and it is currently
1845 	 * unavailable.  Note that we also can't print out the name of the
1846 	 * type, since that would also rely on looking up the resolved name.
1847 	 */
1848 	if (mdb_ctf_type_resolve(id, &rid) != 0) {
1849 		mdb_warn("failed to resolve type");
1850 		return (-1);
1851 	}
1852 
1853 	delim = parse_delimiter(&start);
1854 	/*
1855 	 * If the user fails to specify an initial delimiter, guess -> for
1856 	 * pointer types and . for non-pointer types.
1857 	 */
1858 	if (delim == MEMBER_DELIM_ERR)
1859 		delim = (mdb_ctf_type_kind(rid) == CTF_K_POINTER) ?
1860 		    MEMBER_DELIM_PTR : MEMBER_DELIM_DOT;
1861 
1862 	*last_deref = FALSE;
1863 
1864 	while (delim != MEMBER_DELIM_DONE) {
1865 		switch (delim) {
1866 		case MEMBER_DELIM_PTR:
1867 			kind = mdb_ctf_type_kind(rid);
1868 			if (kind != CTF_K_POINTER) {
1869 				mdb_warn("%s is not a pointer type\n",
1870 				    mdb_ctf_type_name(id, buf, sizeof (buf)));
1871 				return (-1);
1872 			}
1873 
1874 			size = mdb_ctf_type_size(id);
1875 			if (deref(pap, size) != 0)
1876 				return (-1);
1877 
1878 			(void) mdb_ctf_type_reference(rid, &id);
1879 			(void) mdb_ctf_type_resolve(id, &rid);
1880 
1881 			off = 0;
1882 			break;
1883 
1884 		case MEMBER_DELIM_DOT:
1885 			kind = mdb_ctf_type_kind(rid);
1886 			if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1887 				mdb_warn("%s is not a struct or union type\n",
1888 				    mdb_ctf_type_name(id, buf, sizeof (buf)));
1889 				return (-1);
1890 			}
1891 			break;
1892 
1893 		case MEMBER_DELIM_LBR:
1894 			end = strchr(start, ']');
1895 			if (end == NULL) {
1896 				mdb_warn("no trailing ']'\n");
1897 				return (-1);
1898 			}
1899 
1900 			(void) mdb_snprintf(member, end - start + 1, "%s",
1901 			    start);
1902 
1903 			index = mdb_strtoull(member);
1904 
1905 			switch (mdb_ctf_type_kind(rid)) {
1906 			case CTF_K_POINTER:
1907 				size = mdb_ctf_type_size(rid);
1908 
1909 				if (deref(pap, size) != 0)
1910 					return (-1);
1911 
1912 				(void) mdb_ctf_type_reference(rid, &id);
1913 				(void) mdb_ctf_type_resolve(id, &rid);
1914 
1915 				size = mdb_ctf_type_size(id);
1916 				if (size <= 0) {
1917 					mdb_warn("cannot dereference void "
1918 					    "type\n");
1919 					return (-1);
1920 				}
1921 
1922 				pap->pa_addr += index * size;
1923 				off = 0;
1924 
1925 				if (index == 0 && non_array)
1926 					*last_deref = TRUE;
1927 				break;
1928 
1929 			case CTF_K_ARRAY:
1930 				(void) mdb_ctf_array_info(rid, &ar);
1931 
1932 				if (index >= ar.mta_nelems) {
1933 					mdb_warn("index %r is outside of "
1934 					    "array bounds [0 .. %r]\n",
1935 					    index, ar.mta_nelems - 1);
1936 				}
1937 
1938 				id = ar.mta_contents;
1939 				(void) mdb_ctf_type_resolve(id, &rid);
1940 
1941 				size = mdb_ctf_type_size(id);
1942 				if (size <= 0) {
1943 					mdb_warn("cannot dereference void "
1944 					    "type\n");
1945 					return (-1);
1946 				}
1947 
1948 				pap->pa_addr += index * size;
1949 				off = 0;
1950 				break;
1951 
1952 			default:
1953 				mdb_warn("cannot index into non-array, "
1954 				    "non-pointer type\n");
1955 				return (-1);
1956 			}
1957 
1958 			start = end + 1;
1959 			delim = parse_delimiter(&start);
1960 			continue;
1961 
1962 		case MEMBER_DELIM_ERR:
1963 		default:
1964 			mdb_warn("'%c' is not a valid delimiter\n", *start);
1965 			return (-1);
1966 		}
1967 
1968 		*last_deref = FALSE;
1969 		non_array = TRUE;
1970 
1971 		/*
1972 		 * Find the end of the member name; assume that a member
1973 		 * name is at least one character long.
1974 		 */
1975 		for (end = start + 1; isalnum(*end) || *end == '_'; end++)
1976 			continue;
1977 
1978 		(void) mdb_snprintf(member, end - start + 1, "%s", start);
1979 
1980 		if (mdb_ctf_member_info(rid, member, &off, &id) != 0) {
1981 			mdb_warn("failed to find member %s of %s", member,
1982 			    mdb_ctf_type_name(id, buf, sizeof (buf)));
1983 			return (-1);
1984 		}
1985 		(void) mdb_ctf_type_resolve(id, &rid);
1986 
1987 		pap->pa_addr += off / NBBY;
1988 
1989 		start = end;
1990 		delim = parse_delimiter(&start);
1991 	}
1992 
1993 	*idp = id;
1994 	*offp = off;
1995 
1996 	return (0);
1997 }
1998 
1999 int
2000 cmd_print_tab(mdb_tab_cookie_t *mcp, uint_t flags, int argc,
2001     const mdb_arg_t *argv)
2002 {
2003 	char tn[MDB_SYM_NAMLEN];
2004 	char member[64];
2005 	int i, dummy, delim, kind;
2006 	int ret = 0;
2007 	mdb_ctf_id_t id, rid;
2008 	mdb_ctf_arinfo_t ar;
2009 	char *start, *end;
2010 	ulong_t dul;
2011 
2012 	/*
2013 	 * This getopts is only here to make the tab completion work better when
2014 	 * including options in the ::print arguments. None of the values should
2015 	 * be used. This should only be updated with additional arguments, if
2016 	 * they are added to cmd_print.
2017 	 */
2018 	i = mdb_getopts(argc, argv,
2019 	    'a', MDB_OPT_SETBITS, PA_SHOWADDR, &dummy,
2020 	    'C', MDB_OPT_SETBITS, TRUE, &dummy,
2021 	    'c', MDB_OPT_UINTPTR, &dummy,
2022 	    'd', MDB_OPT_SETBITS, PA_INTDEC, &dummy,
2023 	    'h', MDB_OPT_SETBITS, PA_SHOWHOLES, &dummy,
2024 	    'i', MDB_OPT_SETBITS, TRUE, &dummy,
2025 	    'L', MDB_OPT_SETBITS, TRUE, &dummy,
2026 	    'l', MDB_OPT_UINTPTR, &dummy,
2027 	    'n', MDB_OPT_SETBITS, PA_NOSYMBOLIC, &dummy,
2028 	    'p', MDB_OPT_SETBITS, TRUE, &dummy,
2029 	    's', MDB_OPT_UINTPTR, &dummy,
2030 	    'T', MDB_OPT_SETBITS, PA_SHOWTYPE | PA_SHOWBASETYPE, &dummy,
2031 	    't', MDB_OPT_SETBITS, PA_SHOWTYPE, &dummy,
2032 	    'x', MDB_OPT_SETBITS, PA_INTHEX, &dummy,
2033 	    NULL);
2034 
2035 	argc -= i;
2036 	argv += i;
2037 
2038 	if (argc == 0 && !(flags & DCMD_TAB_SPACE))
2039 		return (0);
2040 
2041 	if (argc == 0 && (flags & DCMD_TAB_SPACE))
2042 		return (mdb_tab_complete_type(mcp, NULL, MDB_TABC_NOPOINT |
2043 		    MDB_TABC_NOARRAY));
2044 
2045 	if ((ret = mdb_tab_typename(&argc, &argv, tn, sizeof (tn))) < 0)
2046 		return (ret);
2047 
2048 	if (argc == 1 && (!(flags & DCMD_TAB_SPACE) || ret == 1))
2049 		return (mdb_tab_complete_type(mcp, tn, MDB_TABC_NOPOINT |
2050 		    MDB_TABC_NOARRAY));
2051 
2052 	if (argc == 1 && (flags & DCMD_TAB_SPACE))
2053 		return (mdb_tab_complete_member(mcp, tn, NULL));
2054 
2055 	/*
2056 	 * This is the reason that tab completion was created. We're going to go
2057 	 * along and walk the delimiters until we find something a member that
2058 	 * we don't recognize, at which point we'll try and tab complete it.
2059 	 * Note that ::print takes multiple args, so this is going to operate on
2060 	 * whatever the last arg that we have is.
2061 	 */
2062 	if (mdb_ctf_lookup_by_name(tn, &id) != 0)
2063 		return (1);
2064 
2065 	(void) mdb_ctf_type_resolve(id, &rid);
2066 	start = (char *)argv[argc-1].a_un.a_str;
2067 	delim = parse_delimiter(&start);
2068 
2069 	/*
2070 	 * If we hit the case where we actually have no delimiters, than we need
2071 	 * to make sure that we properly set up the fields the loops would.
2072 	 */
2073 	if (delim == MEMBER_DELIM_DONE)
2074 		(void) mdb_snprintf(member, sizeof (member), "%s", start);
2075 
2076 	while (delim != MEMBER_DELIM_DONE) {
2077 		switch (delim) {
2078 		case MEMBER_DELIM_PTR:
2079 			kind = mdb_ctf_type_kind(rid);
2080 			if (kind != CTF_K_POINTER)
2081 				return (1);
2082 
2083 			(void) mdb_ctf_type_reference(rid, &id);
2084 			(void) mdb_ctf_type_resolve(id, &rid);
2085 			break;
2086 		case MEMBER_DELIM_DOT:
2087 			kind = mdb_ctf_type_kind(rid);
2088 			if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
2089 				return (1);
2090 			break;
2091 		case MEMBER_DELIM_LBR:
2092 			end = strchr(start, ']');
2093 			/*
2094 			 * We're not going to try and tab complete the indexes
2095 			 * here. So for now, punt on it. Also, we're not going
2096 			 * to try and validate you're within the bounds, just
2097 			 * that you get the type you asked for.
2098 			 */
2099 			if (end == NULL)
2100 				return (1);
2101 
2102 			switch (mdb_ctf_type_kind(rid)) {
2103 			case CTF_K_POINTER:
2104 				(void) mdb_ctf_type_reference(rid, &id);
2105 				(void) mdb_ctf_type_resolve(id, &rid);
2106 				break;
2107 			case CTF_K_ARRAY:
2108 				(void) mdb_ctf_array_info(rid, &ar);
2109 				id = ar.mta_contents;
2110 				(void) mdb_ctf_type_resolve(id, &rid);
2111 				break;
2112 			default:
2113 				return (1);
2114 			}
2115 
2116 			start = end + 1;
2117 			delim = parse_delimiter(&start);
2118 			break;
2119 		case MEMBER_DELIM_ERR:
2120 		default:
2121 			break;
2122 		}
2123 
2124 		for (end = start + 1; isalnum(*end) || *end == '_'; end++)
2125 			continue;
2126 
2127 		(void) mdb_snprintf(member, end - start + 1, start);
2128 
2129 		/*
2130 		 * We are going to try to resolve this name as a member. There
2131 		 * are a few two different questions that we need to answer. The
2132 		 * first is do we recognize this member. The second is are we at
2133 		 * the end of the string. If we encounter a member that we don't
2134 		 * recognize before the end, then we have to error out and can't
2135 		 * complete it. But if there are no more delimiters then we can
2136 		 * try and complete it.
2137 		 */
2138 		ret = mdb_ctf_member_info(rid, member, &dul, &id);
2139 		start = end;
2140 		delim = parse_delimiter(&start);
2141 		if (ret != 0 && errno == EMDB_CTFNOMEMB) {
2142 			if (delim != MEMBER_DELIM_DONE)
2143 				return (1);
2144 			continue;
2145 		} else if (ret != 0)
2146 			return (1);
2147 
2148 		if (delim == MEMBER_DELIM_DONE)
2149 			return (mdb_tab_complete_member_by_id(mcp, rid,
2150 			    member));
2151 
2152 		(void) mdb_ctf_type_resolve(id, &rid);
2153 	}
2154 
2155 	/*
2156 	 * If we've reached here, then we need to try and tab complete the last
2157 	 * field, which is currently member, based on the ctf type id that we
2158 	 * already have in rid.
2159 	 */
2160 	return (mdb_tab_complete_member_by_id(mcp, rid, member));
2161 }
2162 
2163 /*
2164  * Recursively descend a print a given data structure.  We create a struct of
2165  * the relevant print arguments and then call mdb_ctf_type_visit() to do the
2166  * traversal, using elt_print() as the callback for each element.
2167  */
2168 /*ARGSUSED*/
2169 int
2170 cmd_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2171 {
2172 	uintptr_t opt_c = MDB_ARR_NOLIMIT, opt_l = MDB_ARR_NOLIMIT;
2173 	uint_t opt_C = FALSE, opt_L = FALSE, opt_p = FALSE, opt_i = FALSE;
2174 	uintptr_t opt_s = (uintptr_t)-1ul;
2175 	int uflags = (flags & DCMD_ADDRSPEC) ? PA_SHOWVAL : 0;
2176 	mdb_ctf_id_t id;
2177 	int err = DCMD_OK;
2178 
2179 	mdb_tgt_t *t = mdb.m_target;
2180 	printarg_t pa;
2181 	int d, i;
2182 
2183 	char s_name[MDB_SYM_NAMLEN];
2184 	mdb_syminfo_t s_info;
2185 	GElf_Sym sym;
2186 
2187 	/*
2188 	 * If a new option is added, make sure the getopts above in
2189 	 * cmd_print_tab is also updated.
2190 	 */
2191 	i = mdb_getopts(argc, argv,
2192 	    'a', MDB_OPT_SETBITS, PA_SHOWADDR, &uflags,
2193 	    'C', MDB_OPT_SETBITS, TRUE, &opt_C,
2194 	    'c', MDB_OPT_UINTPTR, &opt_c,
2195 	    'd', MDB_OPT_SETBITS, PA_INTDEC, &uflags,
2196 	    'h', MDB_OPT_SETBITS, PA_SHOWHOLES, &uflags,
2197 	    'i', MDB_OPT_SETBITS, TRUE, &opt_i,
2198 	    'L', MDB_OPT_SETBITS, TRUE, &opt_L,
2199 	    'l', MDB_OPT_UINTPTR, &opt_l,
2200 	    'n', MDB_OPT_SETBITS, PA_NOSYMBOLIC, &uflags,
2201 	    'p', MDB_OPT_SETBITS, TRUE, &opt_p,
2202 	    's', MDB_OPT_UINTPTR, &opt_s,
2203 	    'T', MDB_OPT_SETBITS, PA_SHOWTYPE | PA_SHOWBASETYPE, &uflags,
2204 	    't', MDB_OPT_SETBITS, PA_SHOWTYPE, &uflags,
2205 	    'x', MDB_OPT_SETBITS, PA_INTHEX, &uflags,
2206 	    NULL);
2207 
2208 	if (uflags & PA_INTHEX)
2209 		uflags &= ~PA_INTDEC;	/* -x and -d are mutually exclusive */
2210 
2211 	uflags |= PA_SHOWNAME;
2212 
2213 	if (opt_p && opt_i) {
2214 		mdb_warn("-p and -i options are incompatible\n");
2215 		return (DCMD_ERR);
2216 	}
2217 
2218 	argc -= i;
2219 	argv += i;
2220 
2221 	if (argc != 0 && argv->a_type == MDB_TYPE_STRING) {
2222 		const char *t_name = s_name;
2223 		int ret;
2224 
2225 		if (strchr("+-", argv->a_un.a_str[0]) != NULL)
2226 			return (DCMD_USAGE);
2227 
2228 		if ((ret = args_to_typename(&argc, &argv, s_name,
2229 		    sizeof (s_name))) != 0)
2230 			return (ret);
2231 
2232 		if (mdb_ctf_lookup_by_name(t_name, &id) != 0) {
2233 			if (!(flags & DCMD_ADDRSPEC) || opt_i ||
2234 			    addr_to_sym(t, addr, s_name, sizeof (s_name),
2235 			    &sym, &s_info) == NULL ||
2236 			    mdb_ctf_lookup_by_symbol(&sym, &s_info, &id) != 0) {
2237 
2238 				mdb_warn("failed to look up type %s", t_name);
2239 				return (DCMD_ABORT);
2240 			}
2241 		} else {
2242 			argc--;
2243 			argv++;
2244 		}
2245 
2246 	} else if (!(flags & DCMD_ADDRSPEC) || opt_i) {
2247 		return (DCMD_USAGE);
2248 
2249 	} else if (addr_to_sym(t, addr, s_name, sizeof (s_name),
2250 	    &sym, &s_info) == NULL) {
2251 		mdb_warn("no symbol information for %a", addr);
2252 		return (DCMD_ERR);
2253 
2254 	} else if (mdb_ctf_lookup_by_symbol(&sym, &s_info, &id) != 0) {
2255 		mdb_warn("no type data available for %a [%u]", addr,
2256 		    s_info.sym_id);
2257 		return (DCMD_ERR);
2258 	}
2259 
2260 	pa.pa_tgt = mdb.m_target;
2261 	pa.pa_realtgt = pa.pa_tgt;
2262 	pa.pa_immtgt = NULL;
2263 	pa.pa_as = opt_p ? MDB_TGT_AS_PHYS : MDB_TGT_AS_VIRT;
2264 	pa.pa_armemlim = mdb.m_armemlim;
2265 	pa.pa_arstrlim = mdb.m_arstrlim;
2266 	pa.pa_delim = "\n";
2267 	pa.pa_flags = uflags;
2268 	pa.pa_nest = 0;
2269 	pa.pa_tab = 4;
2270 	pa.pa_prefix = NULL;
2271 	pa.pa_suffix = NULL;
2272 	pa.pa_holes = NULL;
2273 	pa.pa_nholes = 0;
2274 	pa.pa_depth = 0;
2275 	pa.pa_maxdepth = opt_s;
2276 
2277 	if ((flags & DCMD_ADDRSPEC) && !opt_i)
2278 		pa.pa_addr = opt_p ? mdb_get_dot() : addr;
2279 	else
2280 		pa.pa_addr = NULL;
2281 
2282 	if (opt_i) {
2283 		const char *vargv[2];
2284 		uintmax_t dot = mdb_get_dot();
2285 		size_t outsize = mdb_ctf_type_size(id);
2286 		vargv[0] = (const char *)&dot;
2287 		vargv[1] = (const char *)&outsize;
2288 		pa.pa_immtgt = mdb_tgt_create(mdb_value_tgt_create,
2289 		    0, 2, vargv);
2290 		pa.pa_tgt = pa.pa_immtgt;
2291 	}
2292 
2293 	if (opt_c != MDB_ARR_NOLIMIT)
2294 		pa.pa_arstrlim = opt_c;
2295 	if (opt_C)
2296 		pa.pa_arstrlim = MDB_ARR_NOLIMIT;
2297 	if (opt_l != MDB_ARR_NOLIMIT)
2298 		pa.pa_armemlim = opt_l;
2299 	if (opt_L)
2300 		pa.pa_armemlim = MDB_ARR_NOLIMIT;
2301 
2302 	if (argc > 0) {
2303 		for (i = 0; i < argc; i++) {
2304 			mdb_ctf_id_t mid;
2305 			int last_deref;
2306 			ulong_t off;
2307 			int kind;
2308 			char buf[MDB_SYM_NAMLEN];
2309 
2310 			mdb_tgt_t *oldtgt = pa.pa_tgt;
2311 			mdb_tgt_as_t oldas = pa.pa_as;
2312 			mdb_tgt_addr_t oldaddr = pa.pa_addr;
2313 
2314 			if (argv->a_type == MDB_TYPE_STRING) {
2315 				const char *member = argv[i].a_un.a_str;
2316 				mdb_ctf_id_t rid;
2317 
2318 				if (parse_member(&pa, member, id, &mid,
2319 				    &off, &last_deref) != 0) {
2320 					err = DCMD_ABORT;
2321 					goto out;
2322 				}
2323 
2324 				/*
2325 				 * If the member string ends with a "[0]"
2326 				 * (last_deref * is true) and the type is a
2327 				 * structure or union, * print "->" rather
2328 				 * than "[0]." in elt_print.
2329 				 */
2330 				(void) mdb_ctf_type_resolve(mid, &rid);
2331 				kind = mdb_ctf_type_kind(rid);
2332 				if (last_deref && IS_SOU(kind)) {
2333 					char *end;
2334 					(void) mdb_snprintf(buf, sizeof (buf),
2335 					    "%s", member);
2336 					end = strrchr(buf, '[');
2337 					*end = '\0';
2338 					pa.pa_suffix = "->";
2339 					member = &buf[0];
2340 				} else if (IS_SOU(kind)) {
2341 					pa.pa_suffix = ".";
2342 				} else {
2343 					pa.pa_suffix = "";
2344 				}
2345 
2346 				pa.pa_prefix = member;
2347 			} else {
2348 				ulong_t moff;
2349 
2350 				moff = (ulong_t)argv[i].a_un.a_val;
2351 
2352 				if (mdb_ctf_offset_to_name(id, moff * NBBY,
2353 				    buf, sizeof (buf), 0, &mid, &off) == -1) {
2354 					mdb_warn("invalid offset %lx\n", moff);
2355 					err = DCMD_ABORT;
2356 					goto out;
2357 				}
2358 
2359 				pa.pa_prefix = buf;
2360 				pa.pa_addr += moff - off / NBBY;
2361 				pa.pa_suffix = strlen(buf) == 0 ? "" : ".";
2362 			}
2363 
2364 			off %= NBBY;
2365 			if (flags & DCMD_PIPE_OUT) {
2366 				if (pipe_print(mid, off, &pa) != 0) {
2367 					mdb_warn("failed to print type");
2368 					err = DCMD_ERR;
2369 					goto out;
2370 				}
2371 			} else if (off != 0) {
2372 				mdb_ctf_id_t base;
2373 				(void) mdb_ctf_type_resolve(mid, &base);
2374 
2375 				if (elt_print("", mid, base, off, 0,
2376 				    &pa) != 0) {
2377 					mdb_warn("failed to print type");
2378 					err = DCMD_ERR;
2379 					goto out;
2380 				}
2381 			} else {
2382 				if (mdb_ctf_type_visit(mid, elt_print,
2383 				    &pa) == -1) {
2384 					mdb_warn("failed to print type");
2385 					err = DCMD_ERR;
2386 					goto out;
2387 				}
2388 
2389 				for (d = pa.pa_depth - 1; d >= 0; d--)
2390 					print_close_sou(&pa, d);
2391 			}
2392 
2393 			pa.pa_depth = 0;
2394 			pa.pa_tgt = oldtgt;
2395 			pa.pa_as = oldas;
2396 			pa.pa_addr = oldaddr;
2397 			pa.pa_delim = "\n";
2398 		}
2399 
2400 	} else if (flags & DCMD_PIPE_OUT) {
2401 		if (pipe_print(id, 0, &pa) != 0) {
2402 			mdb_warn("failed to print type");
2403 			err = DCMD_ERR;
2404 			goto out;
2405 		}
2406 	} else {
2407 		if (mdb_ctf_type_visit(id, elt_print, &pa) == -1) {
2408 			mdb_warn("failed to print type");
2409 			err = DCMD_ERR;
2410 			goto out;
2411 		}
2412 
2413 		for (d = pa.pa_depth - 1; d >= 0; d--)
2414 			print_close_sou(&pa, d);
2415 	}
2416 
2417 	mdb_set_dot(addr + mdb_ctf_type_size(id));
2418 	err = DCMD_OK;
2419 out:
2420 	if (pa.pa_immtgt)
2421 		mdb_tgt_destroy(pa.pa_immtgt);
2422 	return (err);
2423 }
2424 
2425 void
2426 print_help(void)
2427 {
2428 	mdb_printf(
2429 	    "-a         show address of object\n"
2430 	    "-C         unlimit the length of character arrays\n"
2431 	    "-c limit   limit the length of character arrays\n"
2432 	    "-d         output values in decimal\n"
2433 	    "-h         print holes in structures\n"
2434 	    "-i         interpret address as data of the given type\n"
2435 	    "-L         unlimit the length of standard arrays\n"
2436 	    "-l limit   limit the length of standard arrays\n"
2437 	    "-n         don't print pointers as symbol offsets\n"
2438 	    "-p         interpret address as a physical memory address\n"
2439 	    "-s depth   limit the recursion depth\n"
2440 	    "-T         show type and <<base type>> of object\n"
2441 	    "-t         show type of object\n"
2442 	    "-x         output values in hexadecimal\n"
2443 	    "\n"
2444 	    "type may be omitted if the C type of addr can be inferred.\n"
2445 	    "\n"
2446 	    "Members may be specified with standard C syntax using the\n"
2447 	    "array indexing operator \"[index]\", structure member\n"
2448 	    "operator \".\", or structure pointer operator \"->\".\n"
2449 	    "\n"
2450 	    "Offsets must use the $[ expression ] syntax\n");
2451 }
2452 
2453 static int
2454 printf_signed(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt,
2455     boolean_t sign)
2456 {
2457 	ssize_t size;
2458 	mdb_ctf_id_t base;
2459 	ctf_encoding_t e;
2460 
2461 	union {
2462 		uint64_t ui8;
2463 		uint32_t ui4;
2464 		uint16_t ui2;
2465 		uint8_t ui1;
2466 		int64_t i8;
2467 		int32_t i4;
2468 		int16_t i2;
2469 		int8_t i1;
2470 	} u;
2471 
2472 	if (mdb_ctf_type_resolve(id, &base) == -1) {
2473 		mdb_warn("could not resolve type");
2474 		return (DCMD_ABORT);
2475 	}
2476 
2477 	if (mdb_ctf_type_kind(base) != CTF_K_INTEGER) {
2478 		mdb_warn("expected integer type\n");
2479 		return (DCMD_ABORT);
2480 	}
2481 
2482 	if (mdb_ctf_type_encoding(base, &e) != 0) {
2483 		mdb_warn("could not get type encoding");
2484 		return (DCMD_ABORT);
2485 	}
2486 
2487 	if (sign)
2488 		sign = e.cte_format & CTF_INT_SIGNED;
2489 
2490 	size = e.cte_bits / NBBY;
2491 
2492 	/*
2493 	 * Check to see if our life has been complicated by the presence of
2494 	 * a bitfield.  If it has, we will print it using logic that is only
2495 	 * slightly different than that found in print_bitfield(), above.  (In
2496 	 * particular, see the comments there for an explanation of the
2497 	 * endianness differences in this code.)
2498 	 */
2499 	if (size > 8 || (e.cte_bits % NBBY) != 0 ||
2500 	    (size & (size - 1)) != 0) {
2501 		uint64_t mask = (1ULL << e.cte_bits) - 1;
2502 		uint64_t value = 0;
2503 		uint8_t *buf = (uint8_t *)&value;
2504 		uint8_t shift;
2505 
2506 		/*
2507 		 * Round our size up one byte.
2508 		 */
2509 		size = (e.cte_bits + (NBBY - 1)) / NBBY;
2510 
2511 		if (e.cte_bits > sizeof (value) * NBBY - 1) {
2512 			mdb_printf("invalid bitfield size %u", e.cte_bits);
2513 			return (DCMD_ABORT);
2514 		}
2515 
2516 #ifdef _BIG_ENDIAN
2517 		buf += sizeof (value) - size;
2518 		off += e.cte_bits;
2519 #endif
2520 
2521 		if (mdb_vread(buf, size, addr) == -1) {
2522 			mdb_warn("failed to read %lu bytes at %p", size, addr);
2523 			return (DCMD_ERR);
2524 		}
2525 
2526 		shift = off % NBBY;
2527 #ifdef _BIG_ENDIAN
2528 		shift = NBBY - shift;
2529 #endif
2530 
2531 		/*
2532 		 * If we have a bit offset within the byte, shift it down.
2533 		 */
2534 		if (off % NBBY != 0)
2535 			value >>= shift;
2536 		value &= mask;
2537 
2538 		if (sign) {
2539 			int sshift = sizeof (value) * NBBY - e.cte_bits;
2540 			value = ((int64_t)value << sshift) >> sshift;
2541 		}
2542 
2543 		mdb_printf(fmt, value);
2544 		return (0);
2545 	}
2546 
2547 	if (mdb_vread(&u.i8, size, addr) == -1) {
2548 		mdb_warn("failed to read %lu bytes at %p", (ulong_t)size, addr);
2549 		return (DCMD_ERR);
2550 	}
2551 
2552 	switch (size) {
2553 	case sizeof (uint8_t):
2554 		mdb_printf(fmt, (uint64_t)(sign ? u.i1 : u.ui1));
2555 		break;
2556 	case sizeof (uint16_t):
2557 		mdb_printf(fmt, (uint64_t)(sign ? u.i2 : u.ui2));
2558 		break;
2559 	case sizeof (uint32_t):
2560 		mdb_printf(fmt, (uint64_t)(sign ? u.i4 : u.ui4));
2561 		break;
2562 	case sizeof (uint64_t):
2563 		mdb_printf(fmt, (uint64_t)(sign ? u.i8 : u.ui8));
2564 		break;
2565 	}
2566 
2567 	return (0);
2568 }
2569 
2570 static int
2571 printf_int(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt)
2572 {
2573 	return (printf_signed(id, addr, off, fmt, B_TRUE));
2574 }
2575 
2576 static int
2577 printf_uint(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt)
2578 {
2579 	return (printf_signed(id, addr, off, fmt, B_FALSE));
2580 }
2581 
2582 /*ARGSUSED*/
2583 static int
2584 printf_uint32(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt)
2585 {
2586 	mdb_ctf_id_t base;
2587 	ctf_encoding_t e;
2588 	uint32_t value;
2589 
2590 	if (mdb_ctf_type_resolve(id, &base) == -1) {
2591 		mdb_warn("could not resolve type\n");
2592 		return (DCMD_ABORT);
2593 	}
2594 
2595 	if (mdb_ctf_type_kind(base) != CTF_K_INTEGER ||
2596 	    mdb_ctf_type_encoding(base, &e) != 0 ||
2597 	    e.cte_bits / NBBY != sizeof (value)) {
2598 		mdb_warn("expected 32-bit integer type\n");
2599 		return (DCMD_ABORT);
2600 	}
2601 
2602 	if (mdb_vread(&value, sizeof (value), addr) == -1) {
2603 		mdb_warn("failed to read 32-bit value at %p", addr);
2604 		return (DCMD_ERR);
2605 	}
2606 
2607 	mdb_printf(fmt, value);
2608 
2609 	return (0);
2610 }
2611 
2612 /*ARGSUSED*/
2613 static int
2614 printf_ptr(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt)
2615 {
2616 	uintptr_t value;
2617 	mdb_ctf_id_t base;
2618 
2619 	if (mdb_ctf_type_resolve(id, &base) == -1) {
2620 		mdb_warn("could not resolve type\n");
2621 		return (DCMD_ABORT);
2622 	}
2623 
2624 	if (mdb_ctf_type_kind(base) != CTF_K_POINTER) {
2625 		mdb_warn("expected pointer type\n");
2626 		return (DCMD_ABORT);
2627 	}
2628 
2629 	if (mdb_vread(&value, sizeof (value), addr) == -1) {
2630 		mdb_warn("failed to read pointer at %llx", addr);
2631 		return (DCMD_ERR);
2632 	}
2633 
2634 	mdb_printf(fmt, value);
2635 
2636 	return (0);
2637 }
2638 
2639 /*ARGSUSED*/
2640 static int
2641 printf_string(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt)
2642 {
2643 	mdb_ctf_id_t base;
2644 	mdb_ctf_arinfo_t r;
2645 	char buf[1024];
2646 	ssize_t size;
2647 
2648 	if (mdb_ctf_type_resolve(id, &base) == -1) {
2649 		mdb_warn("could not resolve type");
2650 		return (DCMD_ABORT);
2651 	}
2652 
2653 	if (mdb_ctf_type_kind(base) == CTF_K_POINTER) {
2654 		uintptr_t value;
2655 
2656 		if (mdb_vread(&value, sizeof (value), addr) == -1) {
2657 			mdb_warn("failed to read pointer at %llx", addr);
2658 			return (DCMD_ERR);
2659 		}
2660 
2661 		if (mdb_readstr(buf, sizeof (buf) - 1, value) < 0) {
2662 			mdb_warn("failed to read string at %llx", value);
2663 			return (DCMD_ERR);
2664 		}
2665 
2666 		mdb_printf(fmt, buf);
2667 		return (0);
2668 	}
2669 
2670 	if (mdb_ctf_type_kind(base) != CTF_K_ARRAY) {
2671 		mdb_warn("exepected pointer or array type\n");
2672 		return (DCMD_ABORT);
2673 	}
2674 
2675 	if (mdb_ctf_array_info(base, &r) == -1 ||
2676 	    mdb_ctf_type_resolve(r.mta_contents, &base) == -1 ||
2677 	    (size = mdb_ctf_type_size(base)) == -1) {
2678 		mdb_warn("can't determine array type");
2679 		return (DCMD_ABORT);
2680 	}
2681 
2682 	if (size != 1) {
2683 		mdb_warn("string format specifier requires "
2684 		    "an array of characters\n");
2685 		return (DCMD_ABORT);
2686 	}
2687 
2688 	bzero(buf, sizeof (buf));
2689 
2690 	if (mdb_vread(buf, MIN(r.mta_nelems, sizeof (buf) - 1), addr) == -1) {
2691 		mdb_warn("failed to read array at %p", addr);
2692 		return (DCMD_ERR);
2693 	}
2694 
2695 	mdb_printf(fmt, buf);
2696 
2697 	return (0);
2698 }
2699 
2700 /*ARGSUSED*/
2701 static int
2702 printf_ipv6(mdb_ctf_id_t id, uintptr_t addr, ulong_t off, char *fmt)
2703 {
2704 	mdb_ctf_id_t base;
2705 	mdb_ctf_id_t ipv6_type, ipv6_base;
2706 	in6_addr_t ipv6;
2707 
2708 	if (mdb_ctf_lookup_by_name("in6_addr_t", &ipv6_type) == -1) {
2709 		mdb_warn("could not resolve in6_addr_t type\n");
2710 		return (DCMD_ABORT);
2711 	}
2712 
2713 	if (mdb_ctf_type_resolve(id, &base) == -1) {
2714 		mdb_warn("could not resolve type\n");
2715 		return (DCMD_ABORT);
2716 	}
2717 
2718 	if (mdb_ctf_type_resolve(ipv6_type, &ipv6_base) == -1) {
2719 		mdb_warn("could not resolve in6_addr_t type\n");
2720 		return (DCMD_ABORT);
2721 	}
2722 
2723 	if (mdb_ctf_type_cmp(base, ipv6_base) != 0) {
2724 		mdb_warn("requires argument of type in6_addr_t\n");
2725 		return (DCMD_ABORT);
2726 	}
2727 
2728 	if (mdb_vread(&ipv6, sizeof (ipv6), addr) == -1) {
2729 		mdb_warn("couldn't read in6_addr_t at %p", addr);
2730 		return (DCMD_ERR);
2731 	}
2732 
2733 	mdb_printf(fmt, &ipv6);
2734 
2735 	return (0);
2736 }
2737 
2738 /*
2739  * To validate the format string specified to ::printf, we run the format
2740  * string through a very simple state machine that restricts us to a subset
2741  * of mdb_printf() functionality.
2742  */
2743 enum {
2744 	PRINTF_NOFMT = 1,		/* no current format specifier */
2745 	PRINTF_PERC,			/* processed '%' */
2746 	PRINTF_FMT,			/* processing format specifier */
2747 	PRINTF_LEFT,			/* processed '-', expecting width */
2748 	PRINTF_WIDTH,			/* processing width */
2749 	PRINTF_QUES			/* processed '?', expecting format */
2750 };
2751 
2752 int
2753 cmd_printf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2754 {
2755 	char type[MDB_SYM_NAMLEN];
2756 	int i, nfmts = 0, ret;
2757 	mdb_ctf_id_t id;
2758 	const char *fmt, *member;
2759 	char **fmts, *last, *dest, f;
2760 	int (**funcs)(mdb_ctf_id_t, uintptr_t, ulong_t, char *);
2761 	int state = PRINTF_NOFMT;
2762 	printarg_t pa;
2763 
2764 	if (!(flags & DCMD_ADDRSPEC))
2765 		return (DCMD_USAGE);
2766 
2767 	bzero(&pa, sizeof (pa));
2768 	pa.pa_as = MDB_TGT_AS_VIRT;
2769 	pa.pa_realtgt = pa.pa_tgt = mdb.m_target;
2770 
2771 	if (argc == 0 || argv[0].a_type != MDB_TYPE_STRING) {
2772 		mdb_warn("expected a format string\n");
2773 		return (DCMD_USAGE);
2774 	}
2775 
2776 	/*
2777 	 * Our first argument is a format string; rip it apart and run it
2778 	 * through our state machine to validate that our input is within the
2779 	 * subset of mdb_printf() format strings that we allow.
2780 	 */
2781 	fmt = argv[0].a_un.a_str;
2782 	/*
2783 	 * 'dest' must be large enough to hold a copy of the format string,
2784 	 * plus a NUL and up to 2 additional characters for each conversion
2785 	 * in the format string.  This gives us a bloat factor of 5/2 ~= 3.
2786 	 *   e.g. "%d" (strlen of 2) --> "%lld\0" (need 5 bytes)
2787 	 */
2788 	dest = mdb_zalloc(strlen(fmt) * 3, UM_SLEEP | UM_GC);
2789 	fmts = mdb_zalloc(strlen(fmt) * sizeof (char *), UM_SLEEP | UM_GC);
2790 	funcs = mdb_zalloc(strlen(fmt) * sizeof (void *), UM_SLEEP | UM_GC);
2791 	last = dest;
2792 
2793 	for (i = 0; fmt[i] != '\0'; i++) {
2794 		*dest++ = f = fmt[i];
2795 
2796 		switch (state) {
2797 		case PRINTF_NOFMT:
2798 			state = f == '%' ? PRINTF_PERC : PRINTF_NOFMT;
2799 			break;
2800 
2801 		case PRINTF_PERC:
2802 			state = f == '-' ? PRINTF_LEFT :
2803 			    f >= '0' && f <= '9' ? PRINTF_WIDTH :
2804 			    f == '?' ? PRINTF_QUES :
2805 			    f == '%' ? PRINTF_NOFMT : PRINTF_FMT;
2806 			break;
2807 
2808 		case PRINTF_LEFT:
2809 			state = f >= '0' && f <= '9' ? PRINTF_WIDTH :
2810 			    f == '?' ? PRINTF_QUES : PRINTF_FMT;
2811 			break;
2812 
2813 		case PRINTF_WIDTH:
2814 			state = f >= '0' && f <= '9' ? PRINTF_WIDTH :
2815 			    PRINTF_FMT;
2816 			break;
2817 
2818 		case PRINTF_QUES:
2819 			state = PRINTF_FMT;
2820 			break;
2821 		}
2822 
2823 		if (state != PRINTF_FMT)
2824 			continue;
2825 
2826 		dest--;
2827 
2828 		/*
2829 		 * Now check that we have one of our valid format characters.
2830 		 */
2831 		switch (f) {
2832 		case 'a':
2833 		case 'A':
2834 		case 'p':
2835 			funcs[nfmts] = printf_ptr;
2836 			break;
2837 
2838 		case 'd':
2839 		case 'q':
2840 		case 'R':
2841 			funcs[nfmts] = printf_int;
2842 			*dest++ = 'l';
2843 			*dest++ = 'l';
2844 			break;
2845 
2846 		case 'I':
2847 			funcs[nfmts] = printf_uint32;
2848 			break;
2849 
2850 		case 'N':
2851 			funcs[nfmts] = printf_ipv6;
2852 			break;
2853 
2854 		case 'o':
2855 		case 'r':
2856 		case 'u':
2857 		case 'x':
2858 		case 'X':
2859 			funcs[nfmts] = printf_uint;
2860 			*dest++ = 'l';
2861 			*dest++ = 'l';
2862 			break;
2863 
2864 		case 's':
2865 			funcs[nfmts] = printf_string;
2866 			break;
2867 
2868 		case 'Y':
2869 			funcs[nfmts] = sizeof (time_t) == sizeof (int) ?
2870 			    printf_uint32 : printf_uint;
2871 			break;
2872 
2873 		default:
2874 			mdb_warn("illegal format string at or near "
2875 			    "'%c' (position %d)\n", f, i + 1);
2876 			return (DCMD_ABORT);
2877 		}
2878 
2879 		*dest++ = f;
2880 		*dest++ = '\0';
2881 		fmts[nfmts++] = last;
2882 		last = dest;
2883 		state = PRINTF_NOFMT;
2884 	}
2885 
2886 	argc--;
2887 	argv++;
2888 
2889 	/*
2890 	 * Now we expect a type name.
2891 	 */
2892 	if ((ret = args_to_typename(&argc, &argv, type, sizeof (type))) != 0)
2893 		return (ret);
2894 
2895 	argv++;
2896 	argc--;
2897 
2898 	if (mdb_ctf_lookup_by_name(type, &id) != 0) {
2899 		mdb_warn("failed to look up type %s", type);
2900 		return (DCMD_ABORT);
2901 	}
2902 
2903 	if (argc == 0) {
2904 		mdb_warn("at least one member must be specified\n");
2905 		return (DCMD_USAGE);
2906 	}
2907 
2908 	if (argc != nfmts) {
2909 		mdb_warn("%s format specifiers (found %d, expected %d)\n",
2910 		    argc > nfmts ? "missing" : "extra", nfmts, argc);
2911 		return (DCMD_ABORT);
2912 	}
2913 
2914 	for (i = 0; i < argc; i++) {
2915 		mdb_ctf_id_t mid;
2916 		ulong_t off;
2917 		int ignored;
2918 
2919 		if (argv[i].a_type != MDB_TYPE_STRING) {
2920 			mdb_warn("expected only type member arguments\n");
2921 			return (DCMD_ABORT);
2922 		}
2923 
2924 		if (strcmp((member = argv[i].a_un.a_str), ".") == 0) {
2925 			/*
2926 			 * We allow "." to be specified to denote the current
2927 			 * value of dot.
2928 			 */
2929 			if (funcs[i] != printf_ptr && funcs[i] != printf_uint &&
2930 			    funcs[i] != printf_int) {
2931 				mdb_warn("expected integer or pointer format "
2932 				    "specifier for '.'\n");
2933 				return (DCMD_ABORT);
2934 			}
2935 
2936 			mdb_printf(fmts[i], mdb_get_dot());
2937 			continue;
2938 		}
2939 
2940 		pa.pa_addr = addr;
2941 
2942 		if (parse_member(&pa, member, id, &mid, &off, &ignored) != 0)
2943 			return (DCMD_ABORT);
2944 
2945 		if ((ret = funcs[i](mid, pa.pa_addr, off, fmts[i])) != 0) {
2946 			mdb_warn("failed to print member '%s'\n", member);
2947 			return (ret);
2948 		}
2949 	}
2950 
2951 	mdb_printf("%s", last);
2952 
2953 	return (DCMD_OK);
2954 }
2955 
2956 static char _mdb_printf_help[] =
2957 "The format string argument is a printf(3C)-like format string that is a\n"
2958 "subset of the format strings supported by mdb_printf().  The type argument\n"
2959 "is the name of a type to be used to interpret the memory referenced by dot.\n"
2960 "The member should either be a field in the specified structure, or the\n"
2961 "special member '.', denoting the value of dot (and treated as a pointer).\n"
2962 "The number of members must match the number of format specifiers in the\n"
2963 "format string.\n"
2964 "\n"
2965 "The following format specifiers are recognized by ::printf:\n"
2966 "\n"
2967 "  %%    Prints the '%' symbol.\n"
2968 "  %a    Prints the member in symbolic form.\n"
2969 "  %d    Prints the member as a decimal integer.  If the member is a signed\n"
2970 "        integer type, the output will be signed.\n"
2971 "  %I    Prints the member a IPv4 address (must be a 32-bit integer type).\n"
2972 "  %N    Prints the member an IPv6 address (must be of type in6_addr_t).\n"
2973 "  %o    Prints the member as an unsigned octal integer.\n"
2974 "  %p    Prints the member as a pointer, in hexadecimal.\n"
2975 "  %q    Prints the member in signed octal.  Honk if you ever use this!\n"
2976 "  %r    Prints the member as an unsigned value in the current output radix.\n"
2977 "  %R    Prints the member as a signed value in the current output radix.\n"
2978 "  %s    Prints the member as a string (requires a pointer or an array of\n"
2979 "        characters).\n"
2980 "  %u    Prints the member as an unsigned decimal integer.\n"
2981 "  %x    Prints the member in hexadecimal.\n"
2982 "  %X    Prints the member in hexadecimal, using the characters A-F as the\n"
2983 "        digits for the values 10-15.\n"
2984 "  %Y    Prints the member as a time_t as the string "
2985 	    "'year month day HH:MM:SS'.\n"
2986 "\n"
2987 "The following field width specifiers are recognized by ::printf:\n"
2988 "\n"
2989 "  %n    Field width is set to the specified decimal value.\n"
2990 "  %?    Field width is set to the maximum width of a hexadecimal pointer\n"
2991 "        value.  This is 8 in an ILP32 environment, and 16 in an LP64\n"
2992 "        environment.\n"
2993 "\n"
2994 "The following flag specifers are recognized by ::printf:\n"
2995 "\n"
2996 "  %-    Left-justify the output within the specified field width.  If the\n"
2997 "        width of the output is less than the specified field width, the\n"
2998 "        output will be padded with blanks on the right-hand side.  Without\n"
2999 "        %-, values are right-justified by default.\n"
3000 "\n"
3001 "  %0    Zero-fill the output field if the output is right-justified and the\n"
3002 "        width of the output is less than the specified field width.  Without\n"
3003 "        %0, right-justified values are prepended with blanks in order to\n"
3004 "        fill the field.\n"
3005 "\n"
3006 "Examples: \n"
3007 "\n"
3008 "  ::walk proc | "
3009 	"::printf \"%-6d %s\\n\" proc_t p_pidp->pid_id p_user.u_psargs\n"
3010 "  ::walk thread | "
3011 	"::printf \"%?p %3d %a\\n\" kthread_t . t_pri t_startpc\n"
3012 "  ::walk zone | "
3013 	"::printf \"%-40s %20s\\n\" zone_t zone_name zone_nodename\n"
3014 "  ::walk ire | "
3015 	"::printf \"%Y %I\\n\" ire_t ire_create_time ire_u.ire4_u.ire4_addr\n"
3016 "\n";
3017 
3018 void
3019 printf_help(void)
3020 {
3021 	mdb_printf("%s", _mdb_printf_help);
3022 }
3023