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