xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_print.c (revision e44e85a7f9935f0428e188393e3da61b17e83884)
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 #include <mdb/mdb_modapi.h>
27 #include <mdb/mdb_target.h>
28 #include <mdb/mdb_argvec.h>
29 #include <mdb/mdb_string.h>
30 #include <mdb/mdb_stdlib.h>
31 #include <mdb/mdb_err.h>
32 #include <mdb/mdb_debug.h>
33 #include <mdb/mdb_fmt.h>
34 #include <mdb/mdb_ctf.h>
35 #include <mdb/mdb_ctf_impl.h>
36 #include <mdb/mdb.h>
37 
38 #include <sys/isa_defs.h>
39 #include <sys/param.h>
40 #include <sys/sysmacros.h>
41 #include <strings.h>
42 #include <libctf.h>
43 #include <ctype.h>
44 
45 typedef struct holeinfo {
46 	ulong_t hi_offset;		/* expected offset */
47 	uchar_t hi_isunion;		/* represents a union */
48 } holeinfo_t;
49 
50 typedef struct printarg {
51 	mdb_tgt_t *pa_tgt;		/* current target */
52 	mdb_tgt_t *pa_realtgt;		/* real target (for -i) */
53 	mdb_tgt_t *pa_immtgt;		/* immediate target (for -i) */
54 	mdb_tgt_as_t pa_as;		/* address space to use for i/o */
55 	mdb_tgt_addr_t pa_addr;		/* base address for i/o */
56 	ulong_t pa_armemlim;		/* limit on array elements to print */
57 	ulong_t pa_arstrlim;		/* limit on array chars to print */
58 	const char *pa_delim;		/* element delimiter string */
59 	const char *pa_prefix;		/* element prefix string */
60 	const char *pa_suffix;		/* element suffix string */
61 	holeinfo_t *pa_holes;		/* hole detection information */
62 	int pa_nholes;			/* size of holes array */
63 	int pa_flags;			/* formatting flags (see below) */
64 	int pa_depth;			/* previous depth */
65 	int pa_nest;			/* array nesting depth */
66 	int pa_tab;			/* tabstop width */
67 	uint_t pa_maxdepth;		/* Limit max depth */
68 } printarg_t;
69 
70 #define	PA_SHOWTYPE	0x001		/* print type name */
71 #define	PA_SHOWBASETYPE	0x002		/* print base type name */
72 #define	PA_SHOWNAME	0x004		/* print member name */
73 #define	PA_SHOWADDR	0x008		/* print address */
74 #define	PA_SHOWVAL	0x010		/* print value */
75 #define	PA_SHOWHOLES	0x020		/* print holes in structs */
76 #define	PA_INTHEX	0x040		/* print integer values in hex */
77 #define	PA_INTDEC	0x080		/* print integer values in decimal */
78 #define	PA_NOSYMBOLIC	0x100		/* don't print ptrs as func+offset */
79 
80 #define	IS_CHAR(e) \
81 	(((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) == \
82 	(CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
83 
84 #define	COMPOSITE_MASK	((1 << CTF_K_STRUCT) | \
85 			(1 << CTF_K_UNION) | (1 << CTF_K_ARRAY))
86 #define	IS_COMPOSITE(k)	(((1 << k) & COMPOSITE_MASK) != 0)
87 
88 #define	SOU_MASK	((1 << CTF_K_STRUCT) | (1 << CTF_K_UNION))
89 #define	IS_SOU(k)	(((1 << k) & SOU_MASK) != 0)
90 
91 #define	MEMBER_DELIM_ERR	-1
92 #define	MEMBER_DELIM_DONE	0
93 #define	MEMBER_DELIM_PTR	1
94 #define	MEMBER_DELIM_DOT	2
95 #define	MEMBER_DELIM_LBR	3
96 
97 typedef int printarg_f(const char *, const char *,
98     mdb_ctf_id_t, mdb_ctf_id_t, ulong_t, printarg_t *);
99 
100 static int elt_print(const char *, mdb_ctf_id_t, mdb_ctf_id_t, ulong_t, int,
101     void *);
102 static void print_close_sou(printarg_t *, int);
103 
104 /*
105  * Given an address, look up the symbol ID of the specified symbol in its
106  * containing module.  We only support lookups for exact matches.
107  */
108 static const char *
109 addr_to_sym(mdb_tgt_t *t, uintptr_t addr, char *name, size_t namelen,
110     GElf_Sym *symp, mdb_syminfo_t *sip)
111 {
112 	const mdb_map_t *mp;
113 	const char *p;
114 
115 	if (mdb_tgt_lookup_by_addr(t, addr, MDB_TGT_SYM_EXACT, name,
116 	    namelen, NULL, NULL) == -1)
117 		return (NULL); /* address does not exactly match a symbol */
118 
119 	if ((p = strrsplit(name, '`')) != NULL) {
120 		if (mdb_tgt_lookup_by_name(t, name, p, symp, sip) == -1)
121 			return (NULL);
122 		return (p);
123 	}
124 
125 	if ((mp = mdb_tgt_addr_to_map(t, addr)) == NULL)
126 		return (NULL); /* address does not fall within a mapping */
127 
128 	if (mdb_tgt_lookup_by_name(t, mp->map_name, name, symp, sip) == -1)
129 		return (NULL);
130 
131 	return (name);
132 }
133 
134 /*
135  * This lets dcmds be a little fancy with their processing of type arguments
136  * while still treating them more or less as a single argument.
137  * For example, if a command is invokes like this:
138  *
139  *   ::<dcmd> proc_t ...
140  *
141  * this function will just copy "proc_t" into the provided buffer. If the
142  * command is instead invoked like this:
143  *
144  *   ::<dcmd> struct proc ...
145  *
146  * this function will place the string "struct proc" into the provided buffer
147  * and increment the caller's argv and argc. This allows the caller to still
148  * treat the type argument logically as it would an other atomic argument.
149  */
150 int
151 args_to_typename(int *argcp, const mdb_arg_t **argvp, char *buf, size_t len)
152 {
153 	int argc = *argcp;
154 	const mdb_arg_t *argv = *argvp;
155 
156 	if (argc < 1 || argv->a_type != MDB_TYPE_STRING)
157 		return (DCMD_USAGE);
158 
159 	if (strcmp(argv->a_un.a_str, "struct") == 0 ||
160 	    strcmp(argv->a_un.a_str, "enum") == 0 ||
161 	    strcmp(argv->a_un.a_str, "union") == 0) {
162 		if (argc <= 1) {
163 			mdb_warn("%s is not a valid type\n", argv->a_un.a_str);
164 			return (DCMD_ABORT);
165 		}
166 
167 		if (argv[1].a_type != MDB_TYPE_STRING)
168 			return (DCMD_USAGE);
169 
170 		(void) mdb_snprintf(buf, len, "%s %s",
171 		    argv[0].a_un.a_str, argv[1].a_un.a_str);
172 
173 		*argcp = argc - 1;
174 		*argvp = argv + 1;
175 	} else {
176 		(void) mdb_snprintf(buf, len, "%s", argv[0].a_un.a_str);
177 	}
178 
179 	return (0);
180 }
181 
182 /*ARGSUSED*/
183 int
184 cmd_sizeof(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
185 {
186 	mdb_ctf_id_t id;
187 	char tn[MDB_SYM_NAMLEN];
188 	int ret;
189 
190 	if (flags & DCMD_ADDRSPEC)
191 		return (DCMD_USAGE);
192 
193 	if ((ret = args_to_typename(&argc, &argv, tn, sizeof (tn))) != 0)
194 		return (ret);
195 
196 	if (argc != 1)
197 		return (DCMD_USAGE);
198 
199 	if (mdb_ctf_lookup_by_name(tn, &id) != 0) {
200 		mdb_warn("failed to look up type %s", tn);
201 		return (DCMD_ERR);
202 	}
203 
204 	if (flags & DCMD_PIPE_OUT)
205 		mdb_printf("%#lr\n", mdb_ctf_type_size(id));
206 	else
207 		mdb_printf("sizeof (%s) = %#lr\n", tn, mdb_ctf_type_size(id));
208 
209 	return (DCMD_OK);
210 }
211 
212 /*ARGSUSED*/
213 int
214 cmd_offsetof(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
215 {
216 	const char *member;
217 	mdb_ctf_id_t id;
218 	ulong_t off;
219 	char tn[MDB_SYM_NAMLEN];
220 	int ret;
221 
222 	if (flags & DCMD_ADDRSPEC)
223 		return (DCMD_USAGE);
224 
225 	if ((ret = args_to_typename(&argc, &argv, tn, sizeof (tn))) != 0)
226 		return (ret);
227 
228 	if (argc != 2 || argv[1].a_type != MDB_TYPE_STRING)
229 		return (DCMD_USAGE);
230 
231 	if (mdb_ctf_lookup_by_name(tn, &id) != 0) {
232 		mdb_warn("failed to look up type %s", tn);
233 		return (DCMD_ERR);
234 	}
235 
236 	member = argv[1].a_un.a_str;
237 
238 	if (mdb_ctf_offsetof(id, member, &off) != 0) {
239 		mdb_warn("failed to find member %s of type %s", member, tn);
240 		return (DCMD_ERR);
241 	}
242 
243 	if (off % NBBY == 0)
244 		mdb_printf("offsetof (%s, %s) = %#lr\n",
245 		    tn, member, off / NBBY);
246 	else
247 		mdb_printf("offsetof (%s, %s) = %#lr bits\n",
248 		    tn, member, off);
249 
250 	return (DCMD_OK);
251 }
252 
253 struct enum_p2_info {
254 	int	e_value;
255 	char	*e_buf;
256 	size_t	e_size;
257 	uint_t	e_bits;
258 	uint8_t	e_found;
259 	uint8_t	e_zero;
260 };
261 
262 static int
263 enum_p2_cb(const char *name, int bit_arg, void *arg)
264 {
265 	struct enum_p2_info *eiip = arg;
266 	uint_t bit = bit_arg;
267 
268 	if (bit != 0 && !ISP2(bit))
269 		return (1);	/* non-power-of-2; abort processing */
270 
271 	if ((bit == 0 && eiip->e_zero) ||
272 	    (bit != 0 && (eiip->e_bits & bit) != 0)) {
273 		return (0);	/* already seen this value */
274 	}
275 
276 	if (bit == 0)
277 		eiip->e_zero = 1;
278 	else
279 		eiip->e_bits |= bit;
280 
281 	if (eiip->e_buf != NULL && (eiip->e_value & bit) != 0) {
282 		if (eiip->e_found)
283 			(void) strlcat(eiip->e_buf, "|", eiip->e_size);
284 
285 		if (strlcat(eiip->e_buf, name, eiip->e_size) >=
286 		    eiip->e_size)
287 			return (1);	/* overflowed */
288 
289 		eiip->e_found = 1;
290 	}
291 	return (0);
292 }
293 
294 static int
295 enum_value_to_name_p2(mdb_ctf_id_t id, int v, char *buf, size_t size)
296 {
297 	struct enum_p2_info eii;
298 
299 	bzero(&eii, sizeof (eii));
300 
301 	eii.e_value = v;
302 	eii.e_buf = buf;
303 	eii.e_size = size;
304 
305 	if (buf != NULL && size > 0)
306 		buf[0] = '\0';
307 
308 	if (mdb_ctf_type_kind(id) != CTF_K_ENUM ||
309 	    mdb_ctf_enum_iter(id, enum_p2_cb, &eii) != 0 ||
310 	    eii.e_bits == 0)
311 		return (-1);
312 
313 	if (buf != NULL && (!eii.e_found || (v & ~eii.e_bits) != 0)) {
314 		char val[16];
315 
316 		(void) mdb_snprintf(val, sizeof (val),
317 		    "0x%x", (v & ~eii.e_bits));
318 
319 		if (eii.e_found)
320 			(void) strlcat(buf, "|", size);
321 		if (strlcat(buf, val, size) >= size)
322 			return (-1);
323 	}
324 
325 	return (0);
326 }
327 
328 struct enum_cbinfo {
329 	uint_t		e_flags;
330 	const char	*e_string;	/* NULL for value searches */
331 	int		e_value;
332 	uint_t		e_found;
333 };
334 #define	E_PRETTY		0x1
335 #define	E_HEX			0x2
336 #define	E_SEARCH_STRING		0x4
337 #define	E_SEARCH_VALUE		0x8
338 
339 static void
340 enum_print(struct enum_cbinfo *info, const char *name, int value)
341 {
342 	uint_t flags = info->e_flags;
343 
344 	if (flags & E_PRETTY) {
345 		if (flags & E_HEX)
346 			mdb_printf("%-8x %s\n", value, name);
347 		else
348 			mdb_printf("%-11d %s\n", value, name);
349 	} else {
350 		mdb_printf("%#r\n", value);
351 	}
352 }
353 
354 static int
355 enum_cb(const char *name, int value, void *arg)
356 {
357 	struct enum_cbinfo *info = arg;
358 	uint_t flags = info->e_flags;
359 
360 	if (flags & E_SEARCH_STRING) {
361 		if (strcmp(name, info->e_string) != 0)
362 			return (0);
363 
364 	} else if (flags & E_SEARCH_VALUE) {
365 		if (value != info->e_value)
366 			return (0);
367 	}
368 
369 	enum_print(info, name, value);
370 
371 	info->e_found = 1;
372 	return (0);
373 }
374 
375 /*ARGSUSED*/
376 int
377 cmd_enum(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
378 {
379 	struct enum_cbinfo info;
380 
381 	char type[MDB_SYM_NAMLEN + sizeof ("enum ")];
382 	char tn2[MDB_SYM_NAMLEN + sizeof ("enum ")];
383 	mdb_ctf_id_t id;
384 	mdb_ctf_id_t idr;
385 
386 	int i;
387 	intmax_t search;
388 
389 	info.e_flags = (flags & DCMD_PIPE_OUT)? 0 : E_PRETTY;
390 	info.e_string = NULL;
391 	info.e_value = 0;
392 	info.e_found = 0;
393 
394 	i = mdb_getopts(argc, argv,
395 	    'x', MDB_OPT_SETBITS, E_HEX, &info.e_flags,
396 	    NULL);
397 
398 	argc -= i;
399 	argv += i;
400 
401 	if ((i = args_to_typename(&argc, &argv, type, MDB_SYM_NAMLEN)) != 0)
402 		return (i);
403 
404 	if (strchr(type, ' ') == NULL) {
405 		/*
406 		 * Check as an enumeration tag first, and fall back
407 		 * to checking for a typedef.  Yes, this means that
408 		 * anonymous enumerations whose typedefs conflict with
409 		 * an enum tag can't be accessed.  Don't do that.
410 		 */
411 		(void) mdb_snprintf(tn2, sizeof (tn2), "enum %s", type);
412 
413 		if (mdb_ctf_lookup_by_name(tn2, &id) == 0) {
414 			strcpy(type, tn2);
415 		} else if (mdb_ctf_lookup_by_name(type, &id) != 0) {
416 			mdb_warn("types '%s', '%s'", tn2, type);
417 			return (DCMD_ERR);
418 		}
419 	} else {
420 		if (mdb_ctf_lookup_by_name(type, &id) != 0) {
421 			mdb_warn("'%s'", type);
422 			return (DCMD_ERR);
423 		}
424 	}
425 
426 	/* resolve it, and make sure we're looking at an enumeration */
427 	if (mdb_ctf_type_resolve(id, &idr) == -1) {
428 		mdb_warn("unable to resolve '%s'", type);
429 		return (DCMD_ERR);
430 	}
431 	if (mdb_ctf_type_kind(idr) != CTF_K_ENUM) {
432 		mdb_warn("'%s': not an enumeration\n", type);
433 		return (DCMD_ERR);
434 	}
435 
436 	if (argc > 2)
437 		return (DCMD_USAGE);
438 
439 	if (argc == 2) {
440 		if (flags & DCMD_ADDRSPEC) {
441 			mdb_warn("may only specify one of: name, address\n");
442 			return (DCMD_USAGE);
443 		}
444 
445 		if (argv[1].a_type == MDB_TYPE_STRING) {
446 			info.e_flags |= E_SEARCH_STRING;
447 			info.e_string = argv[1].a_un.a_str;
448 		} else if (argv[1].a_type == MDB_TYPE_IMMEDIATE) {
449 			info.e_flags |= E_SEARCH_VALUE;
450 			search = argv[1].a_un.a_val;
451 		} else {
452 			return (DCMD_USAGE);
453 		}
454 	}
455 
456 	if (flags & DCMD_ADDRSPEC) {
457 		info.e_flags |= E_SEARCH_VALUE;
458 		search = mdb_get_dot();
459 	}
460 
461 	if (info.e_flags & E_SEARCH_VALUE) {
462 		if ((int)search != search) {
463 			mdb_warn("value '%lld' out of enumeration range\n",
464 			    search);
465 			return (DCMD_ERR);
466 		}
467 		info.e_value = search;
468 	}
469 
470 	if (DCMD_HDRSPEC(flags) && (info.e_flags & E_PRETTY)) {
471 		if (info.e_flags & E_HEX)
472 			mdb_printf("%<b>%-8s %s%</b>\n", "VALUE", "NAME");
473 		else
474 			mdb_printf("%<b>%-11s %s%</b>\n", "VALUE", "NAME");
475 	}
476 
477 	/* if the enum is a power-of-two one, process it that way */
478 	if ((info.e_flags & E_SEARCH_VALUE) &&
479 	    enum_value_to_name_p2(idr, info.e_value, tn2, sizeof (tn2)) == 0) {
480 		enum_print(&info, tn2, info.e_value);
481 		return (DCMD_OK);
482 	}
483 
484 	if (mdb_ctf_enum_iter(idr, enum_cb, &info) == -1) {
485 		mdb_warn("cannot walk '%s' as enum", type);
486 		return (DCMD_ERR);
487 	}
488 
489 	if (info.e_found == 0 &&
490 	    (info.e_flags & (E_SEARCH_STRING | E_SEARCH_VALUE)) != 0) {
491 		if (info.e_flags & E_SEARCH_STRING)
492 			mdb_warn("name \"%s\" not in '%s'\n", info.e_string,
493 			    type);
494 		else
495 			mdb_warn("value %#d not in '%s'\n", info.e_value, type);
496 
497 		return (DCMD_ERR);
498 	}
499 
500 	return (DCMD_OK);
501 }
502 
503 static int
504 setup_vcb(const char *name, uintptr_t addr)
505 {
506 	const char *p;
507 	mdb_var_t *v;
508 
509 	if ((v = mdb_nv_lookup(&mdb.m_nv, name)) == NULL) {
510 		if ((p = strbadid(name)) != NULL) {
511 			mdb_warn("'%c' may not be used in a variable "
512 			    "name\n", *p);
513 			return (DCMD_ABORT);
514 		}
515 
516 		if ((v = mdb_nv_insert(&mdb.m_nv, name, NULL, addr, 0)) == NULL)
517 			return (DCMD_ERR);
518 	} else {
519 		if (v->v_flags & MDB_NV_RDONLY) {
520 			mdb_warn("variable %s is read-only\n", name);
521 			return (DCMD_ABORT);
522 		}
523 	}
524 
525 	/*
526 	 * If there already exists a vcb for this variable, we may be
527 	 * calling the dcmd in a loop.  We only create a vcb for this
528 	 * variable on the first invocation.
529 	 */
530 	if (mdb_vcb_find(v, mdb.m_frame) == NULL)
531 		mdb_vcb_insert(mdb_vcb_create(v), mdb.m_frame);
532 
533 	return (0);
534 }
535 
536 /*ARGSUSED*/
537 int
538 cmd_list(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
539 {
540 	mdb_ctf_id_t id;
541 	ulong_t offset;
542 	uintptr_t a, tmp;
543 	int ret;
544 
545 	if (!(flags & DCMD_ADDRSPEC) || argc == 0)
546 		return (DCMD_USAGE);
547 
548 	if (argv->a_type != MDB_TYPE_STRING) {
549 		/*
550 		 * We are being given a raw offset in lieu of a type and
551 		 * member; confirm the arguments.
552 		 */
553 		if (argv->a_type != MDB_TYPE_IMMEDIATE)
554 			return (DCMD_USAGE);
555 
556 		offset = argv->a_un.a_val;
557 
558 		argv++;
559 		argc--;
560 
561 		if (offset % sizeof (uintptr_t)) {
562 			mdb_warn("offset must fall on a word boundary\n");
563 			return (DCMD_ABORT);
564 		}
565 	} else {
566 		const char *member;
567 		char buf[MDB_SYM_NAMLEN];
568 		int ret;
569 
570 		ret = args_to_typename(&argc, &argv, buf, sizeof (buf));
571 		if (ret != 0)
572 			return (ret);
573 
574 		if (mdb_ctf_lookup_by_name(buf, &id) != 0) {
575 			mdb_warn("failed to look up type %s", buf);
576 			return (DCMD_ABORT);
577 		}
578 
579 		argv++;
580 		argc--;
581 
582 		if (argc < 1 || argv->a_type != MDB_TYPE_STRING)
583 			return (DCMD_USAGE);
584 
585 		member = argv->a_un.a_str;
586 
587 		argv++;
588 		argc--;
589 
590 		if (mdb_ctf_offsetof(id, member, &offset) != 0) {
591 			mdb_warn("failed to find member %s of type %s",
592 			    member, buf);
593 			return (DCMD_ABORT);
594 		}
595 
596 		if (offset % (sizeof (uintptr_t) * NBBY) != 0) {
597 			mdb_warn("%s is not a word-aligned member\n", member);
598 			return (DCMD_ABORT);
599 		}
600 
601 		offset /= NBBY;
602 	}
603 
604 	/*
605 	 * If we have any unchewed arguments, a variable name must be present.
606 	 */
607 	if (argc == 1) {
608 		if (argv->a_type != MDB_TYPE_STRING)
609 			return (DCMD_USAGE);
610 
611 		if ((ret = setup_vcb(argv->a_un.a_str, addr)) != 0)
612 			return (ret);
613 
614 	} else if (argc != 0) {
615 		return (DCMD_USAGE);
616 	}
617 
618 	a = addr;
619 
620 	do {
621 		mdb_printf("%lr\n", a);
622 
623 		if (mdb_vread(&tmp, sizeof (tmp), a + offset) == -1) {
624 			mdb_warn("failed to read next pointer from object %p",
625 			    a);
626 			return (DCMD_ERR);
627 		}
628 
629 		a = tmp;
630 	} while (a != addr && a != NULL);
631 
632 	return (DCMD_OK);
633 }
634 
635 int
636 cmd_array(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
637 {
638 	mdb_ctf_id_t id;
639 	ssize_t elemsize = 0;
640 	char tn[MDB_SYM_NAMLEN];
641 	int ret, nelem = -1;
642 
643 	mdb_tgt_t *t = mdb.m_target;
644 	GElf_Sym sym;
645 	mdb_ctf_arinfo_t ar;
646 	mdb_syminfo_t s_info;
647 
648 	if (!(flags & DCMD_ADDRSPEC))
649 		return (DCMD_USAGE);
650 
651 	if (argc >= 2) {
652 		ret = args_to_typename(&argc, &argv, tn, sizeof (tn));
653 		if (ret != 0)
654 			return (ret);
655 
656 		if (argc == 1)	/* unquoted compound type without count */
657 			return (DCMD_USAGE);
658 
659 		if (mdb_ctf_lookup_by_name(tn, &id) != 0) {
660 			mdb_warn("failed to look up type %s", tn);
661 			return (DCMD_ABORT);
662 		}
663 
664 		if (argv[1].a_type == MDB_TYPE_IMMEDIATE)
665 			nelem = argv[1].a_un.a_val;
666 		else
667 			nelem = mdb_strtoull(argv[1].a_un.a_str);
668 
669 		elemsize = mdb_ctf_type_size(id);
670 	} else if (addr_to_sym(t, addr, tn, sizeof (tn), &sym, &s_info)
671 	    != NULL && mdb_ctf_lookup_by_symbol(&sym, &s_info, &id)
672 	    == 0 && mdb_ctf_type_kind(id) == CTF_K_ARRAY &&
673 	    mdb_ctf_array_info(id, &ar) != -1) {
674 		elemsize = mdb_ctf_type_size(id) / ar.mta_nelems;
675 		nelem = ar.mta_nelems;
676 	} else {
677 		mdb_warn("no symbol information for %a", addr);
678 		return (DCMD_ERR);
679 	}
680 
681 	if (argc == 3 || argc == 1) {
682 		if (argv[argc - 1].a_type != MDB_TYPE_STRING)
683 			return (DCMD_USAGE);
684 
685 		if ((ret = setup_vcb(argv[argc - 1].a_un.a_str, addr)) != 0)
686 			return (ret);
687 
688 	} else if (argc > 3) {
689 		return (DCMD_USAGE);
690 	}
691 
692 	for (; nelem > 0; nelem--) {
693 		mdb_printf("%lr\n", addr);
694 		addr = addr + elemsize;
695 	}
696 
697 	return (DCMD_OK);
698 }
699 
700 /*
701  * Print an integer bitfield in hexadecimal by reading the enclosing byte(s)
702  * and then shifting and masking the data in the lower bits of a uint64_t.
703  */
704 static int
705 print_bitfield(ulong_t off, printarg_t *pap, ctf_encoding_t *ep)
706 {
707 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
708 	size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
709 	uint64_t mask = (1ULL << ep->cte_bits) - 1;
710 	uint64_t value = 0;
711 	uint8_t *buf = (uint8_t *)&value;
712 	uint8_t shift;
713 
714 	const char *format;
715 
716 	if (!(pap->pa_flags & PA_SHOWVAL))
717 		return (0);
718 
719 	if (ep->cte_bits > sizeof (value) * NBBY - 1) {
720 		mdb_printf("??? (invalid bitfield size %u)", ep->cte_bits);
721 		return (0);
722 	}
723 
724 	/*
725 	 * On big-endian machines, we need to adjust the buf pointer to refer
726 	 * to the lowest 'size' bytes in 'value', and we need shift based on
727 	 * the offset from the end of the data, not the offset of the start.
728 	 */
729 #ifdef _BIG_ENDIAN
730 	buf += sizeof (value) - size;
731 	off += ep->cte_bits;
732 #endif
733 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, buf, size, addr) != size) {
734 		mdb_warn("failed to read %lu bytes at %llx",
735 		    (ulong_t)size, addr);
736 		return (1);
737 	}
738 
739 	shift = off % NBBY;
740 
741 	/*
742 	 * Offsets are counted from opposite ends on little- and
743 	 * big-endian machines.
744 	 */
745 #ifdef _BIG_ENDIAN
746 	shift = NBBY - shift;
747 #endif
748 
749 	/*
750 	 * If the bits we want do not begin on a byte boundary, shift the data
751 	 * right so that the value is in the lowest 'cte_bits' of 'value'.
752 	 */
753 	if (off % NBBY != 0)
754 		value >>= shift;
755 	value &= mask;
756 
757 	/*
758 	 * We default to printing signed bitfields as decimals,
759 	 * and unsigned bitfields in hexadecimal.  If they specify
760 	 * hexadecimal, we treat the field as unsigned.
761 	 */
762 	if ((pap->pa_flags & PA_INTHEX) ||
763 	    !(ep->cte_format & CTF_INT_SIGNED)) {
764 		format = (pap->pa_flags & PA_INTDEC)? "%#llu" : "%#llx";
765 	} else {
766 		int sshift = sizeof (value) * NBBY - ep->cte_bits;
767 
768 		/* sign-extend value, and print as a signed decimal */
769 		value = ((int64_t)value << sshift) >> sshift;
770 		format = "%#lld";
771 	}
772 	mdb_printf(format, value);
773 
774 	return (0);
775 }
776 
777 /*
778  * Print out a character or integer value.  We use some simple heuristics,
779  * described below, to determine the appropriate radix to use for output.
780  */
781 static int
782 print_int_val(const char *type, ctf_encoding_t *ep, ulong_t off,
783     printarg_t *pap)
784 {
785 	static const char *const sformat[] = { "%#d", "%#d", "%#d", "%#lld" };
786 	static const char *const uformat[] = { "%#u", "%#u", "%#u", "%#llu" };
787 	static const char *const xformat[] = { "%#x", "%#x", "%#x", "%#llx" };
788 
789 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
790 	const char *const *fsp;
791 	size_t size;
792 
793 	union {
794 		uint64_t i8;
795 		uint32_t i4;
796 		uint16_t i2;
797 		uint8_t i1;
798 		time_t t;
799 	} u;
800 
801 	if (!(pap->pa_flags & PA_SHOWVAL))
802 		return (0);
803 
804 	if (ep->cte_format & CTF_INT_VARARGS) {
805 		mdb_printf("...\n");
806 		return (0);
807 	}
808 
809 	/*
810 	 * If the size is not a power-of-two number of bytes in the range 1-8
811 	 * then we assume it is a bitfield and print it as such.
812 	 */
813 	size = ep->cte_bits / NBBY;
814 	if (size > 8 || (ep->cte_bits % NBBY) != 0 || (size & (size - 1)) != 0)
815 		return (print_bitfield(off, pap, ep));
816 
817 	if (IS_CHAR(*ep)) {
818 		mdb_printf("'");
819 		if (mdb_fmt_print(pap->pa_tgt, pap->pa_as,
820 		    addr, 1, 'C') == addr)
821 			return (1);
822 		mdb_printf("'");
823 		return (0);
824 	}
825 
826 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.i8, size, addr) != size) {
827 		mdb_warn("failed to read %lu bytes at %llx",
828 		    (ulong_t)size, addr);
829 		return (1);
830 	}
831 
832 	/*
833 	 * We pretty-print time_t values as a calendar date and time.
834 	 */
835 	if (!(pap->pa_flags & (PA_INTHEX | PA_INTDEC)) &&
836 	    strcmp(type, "time_t") == 0 && u.t != 0) {
837 		mdb_printf("%Y", u.t);
838 		return (0);
839 	}
840 
841 	/*
842 	 * The default format is hexadecimal.
843 	 */
844 	if (!(pap->pa_flags & PA_INTDEC))
845 		fsp = xformat;
846 	else if (ep->cte_format & CTF_INT_SIGNED)
847 		fsp = sformat;
848 	else
849 		fsp = uformat;
850 
851 	switch (size) {
852 	case sizeof (uint8_t):
853 		mdb_printf(fsp[0], u.i1);
854 		break;
855 	case sizeof (uint16_t):
856 		mdb_printf(fsp[1], u.i2);
857 		break;
858 	case sizeof (uint32_t):
859 		mdb_printf(fsp[2], u.i4);
860 		break;
861 	case sizeof (uint64_t):
862 		mdb_printf(fsp[3], u.i8);
863 		break;
864 	}
865 	return (0);
866 }
867 
868 /*ARGSUSED*/
869 static int
870 print_int(const char *type, const char *name, mdb_ctf_id_t id,
871     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
872 {
873 	ctf_encoding_t e;
874 
875 	if (!(pap->pa_flags & PA_SHOWVAL))
876 		return (0);
877 
878 	if (mdb_ctf_type_encoding(base, &e) != 0) {
879 		mdb_printf("??? (%s)", mdb_strerror(errno));
880 		return (0);
881 	}
882 
883 	return (print_int_val(type, &e, off, pap));
884 }
885 
886 /*
887  * Print out a floating point value.  We only provide support for floats in
888  * the ANSI-C float, double, and long double formats.
889  */
890 /*ARGSUSED*/
891 static int
892 print_float(const char *type, const char *name, mdb_ctf_id_t id,
893     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
894 {
895 #ifndef _KMDB
896 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
897 	ctf_encoding_t e;
898 
899 	union {
900 		float f;
901 		double d;
902 		long double ld;
903 	} u;
904 
905 	if (!(pap->pa_flags & PA_SHOWVAL))
906 		return (0);
907 
908 	if (mdb_ctf_type_encoding(base, &e) == 0) {
909 		if (e.cte_format == CTF_FP_SINGLE &&
910 		    e.cte_bits == sizeof (float) * NBBY) {
911 			if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.f,
912 			    sizeof (u.f), addr) != sizeof (u.f)) {
913 				mdb_warn("failed to read float at %llx", addr);
914 				return (1);
915 			}
916 			mdb_printf("%s", doubletos(u.f, 7, 'e'));
917 
918 		} else if (e.cte_format == CTF_FP_DOUBLE &&
919 		    e.cte_bits == sizeof (double) * NBBY) {
920 			if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.d,
921 			    sizeof (u.d), addr) != sizeof (u.d)) {
922 				mdb_warn("failed to read float at %llx", addr);
923 				return (1);
924 			}
925 			mdb_printf("%s", doubletos(u.d, 7, 'e'));
926 
927 		} else if (e.cte_format == CTF_FP_LDOUBLE &&
928 		    e.cte_bits == sizeof (long double) * NBBY) {
929 			if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.ld,
930 			    sizeof (u.ld), addr) != sizeof (u.ld)) {
931 				mdb_warn("failed to read float at %llx", addr);
932 				return (1);
933 			}
934 			mdb_printf("%s", longdoubletos(&u.ld, 16, 'e'));
935 
936 		} else {
937 			mdb_printf("??? (unsupported FP format %u / %u bits\n",
938 			    e.cte_format, e.cte_bits);
939 		}
940 	} else
941 		mdb_printf("??? (%s)", mdb_strerror(errno));
942 #else
943 	mdb_printf("<FLOAT>");
944 #endif
945 	return (0);
946 }
947 
948 
949 /*
950  * Print out a pointer value as a symbol name + offset or a hexadecimal value.
951  * If the pointer itself is a char *, we attempt to read a bit of the data
952  * referenced by the pointer and display it if it is a printable ASCII string.
953  */
954 /*ARGSUSED*/
955 static int
956 print_ptr(const char *type, const char *name, mdb_ctf_id_t id,
957     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
958 {
959 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
960 	ctf_encoding_t e;
961 	uintptr_t value;
962 	char buf[256];
963 	ssize_t len;
964 
965 	if (!(pap->pa_flags & PA_SHOWVAL))
966 		return (0);
967 
968 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as,
969 	    &value, sizeof (value), addr) != sizeof (value)) {
970 		mdb_warn("failed to read %s pointer at %llx", name, addr);
971 		return (1);
972 	}
973 
974 	if (pap->pa_flags & PA_NOSYMBOLIC) {
975 		mdb_printf("%#lx", value);
976 		return (0);
977 	}
978 
979 	mdb_printf("%a", value);
980 
981 	if (value == NULL || strcmp(type, "caddr_t") == 0)
982 		return (0);
983 
984 	if (mdb_ctf_type_kind(base) == CTF_K_POINTER &&
985 	    mdb_ctf_type_reference(base, &base) != -1 &&
986 	    mdb_ctf_type_resolve(base, &base) != -1 &&
987 	    mdb_ctf_type_encoding(base, &e) == 0 && IS_CHAR(e)) {
988 		if ((len = mdb_tgt_readstr(pap->pa_realtgt, pap->pa_as,
989 		    buf, sizeof (buf), value)) >= 0 && strisprint(buf)) {
990 			if (len == sizeof (buf))
991 				(void) strabbr(buf, sizeof (buf));
992 			mdb_printf(" \"%s\"", buf);
993 		}
994 	}
995 
996 	return (0);
997 }
998 
999 
1000 /*
1001  * Print out a fixed-size array.  We special-case arrays of characters
1002  * and attempt to print them out as ASCII strings if possible.  For other
1003  * arrays, we iterate over a maximum of pa_armemlim members and call
1004  * mdb_ctf_type_visit() again on each element to print its value.
1005  */
1006 /*ARGSUSED*/
1007 static int
1008 print_array(const char *type, const char *name, mdb_ctf_id_t id,
1009     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1010 {
1011 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
1012 	printarg_t pa = *pap;
1013 	ssize_t eltsize;
1014 	mdb_ctf_arinfo_t r;
1015 	ctf_encoding_t e;
1016 	uint_t i, kind, limit;
1017 	int d, sou;
1018 	char buf[8];
1019 	char *str;
1020 
1021 	if (!(pap->pa_flags & PA_SHOWVAL))
1022 		return (0);
1023 
1024 	if (pap->pa_depth == pap->pa_maxdepth) {
1025 		mdb_printf("[ ... ]");
1026 		return (0);
1027 	}
1028 
1029 	/*
1030 	 * Determine the base type and size of the array's content.  If this
1031 	 * fails, we cannot print anything and just give up.
1032 	 */
1033 	if (mdb_ctf_array_info(base, &r) == -1 ||
1034 	    mdb_ctf_type_resolve(r.mta_contents, &base) == -1 ||
1035 	    (eltsize = mdb_ctf_type_size(base)) == -1) {
1036 		mdb_printf("[ ??? ] (%s)", mdb_strerror(errno));
1037 		return (0);
1038 	}
1039 
1040 	/*
1041 	 * Read a few bytes and determine if the content appears to be
1042 	 * printable ASCII characters.  If so, read the entire array and
1043 	 * attempt to display it as a string if it is printable.
1044 	 */
1045 	if ((pap->pa_arstrlim == MDB_ARR_NOLIMIT ||
1046 	    r.mta_nelems <= pap->pa_arstrlim) &&
1047 	    mdb_ctf_type_encoding(base, &e) == 0 && IS_CHAR(e) &&
1048 	    mdb_tgt_readstr(pap->pa_tgt, pap->pa_as, buf,
1049 	    MIN(sizeof (buf), r.mta_nelems), addr) > 0 && strisprint(buf)) {
1050 
1051 		str = mdb_alloc(r.mta_nelems + 1, UM_SLEEP | UM_GC);
1052 		str[r.mta_nelems] = '\0';
1053 
1054 		if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, str,
1055 		    r.mta_nelems, addr) != r.mta_nelems) {
1056 			mdb_warn("failed to read char array at %llx", addr);
1057 			return (1);
1058 		}
1059 
1060 		if (strisprint(str)) {
1061 			mdb_printf("[ \"%s\" ]", str);
1062 			return (0);
1063 		}
1064 	}
1065 
1066 	if (pap->pa_armemlim != MDB_ARR_NOLIMIT)
1067 		limit = MIN(r.mta_nelems, pap->pa_armemlim);
1068 	else
1069 		limit = r.mta_nelems;
1070 
1071 	if (limit == 0) {
1072 		mdb_printf("[ ... ]");
1073 		return (0);
1074 	}
1075 
1076 	kind = mdb_ctf_type_kind(base);
1077 	sou = IS_COMPOSITE(kind);
1078 
1079 	pa.pa_addr = addr;		/* set base address to start of array */
1080 	pa.pa_maxdepth = pa.pa_maxdepth - pa.pa_depth - 1;
1081 	pa.pa_nest += pa.pa_depth + 1;	/* nesting level is current depth + 1 */
1082 	pa.pa_depth = 0;		/* reset depth to 0 for new scope */
1083 	pa.pa_prefix = NULL;
1084 
1085 	if (sou) {
1086 		pa.pa_delim = "\n";
1087 		mdb_printf("[\n");
1088 	} else {
1089 		pa.pa_flags &= ~(PA_SHOWTYPE | PA_SHOWNAME | PA_SHOWADDR);
1090 		pa.pa_delim = ", ";
1091 		mdb_printf("[ ");
1092 	}
1093 
1094 	for (i = 0; i < limit; i++, pa.pa_addr += eltsize) {
1095 		if (i == limit - 1 && !sou) {
1096 			if (limit < r.mta_nelems)
1097 				pa.pa_delim = ", ... ]";
1098 			else
1099 				pa.pa_delim = " ]";
1100 		}
1101 
1102 		if (mdb_ctf_type_visit(r.mta_contents, elt_print, &pa) == -1) {
1103 			mdb_warn("failed to print array data");
1104 			return (1);
1105 		}
1106 	}
1107 
1108 	if (sou) {
1109 		for (d = pa.pa_depth - 1; d >= 0; d--)
1110 			print_close_sou(&pa, d);
1111 
1112 		if (limit < r.mta_nelems) {
1113 			mdb_printf("%*s... ]",
1114 			    (pap->pa_depth + pap->pa_nest) * pap->pa_tab, "");
1115 		} else {
1116 			mdb_printf("%*s]",
1117 			    (pap->pa_depth + pap->pa_nest) * pap->pa_tab, "");
1118 		}
1119 	}
1120 
1121 	/* copy the hole array info, since it may have been grown */
1122 	pap->pa_holes = pa.pa_holes;
1123 	pap->pa_nholes = pa.pa_nholes;
1124 
1125 	return (0);
1126 }
1127 
1128 /*
1129  * Print out a struct or union header.  We need only print the open brace
1130  * because mdb_ctf_type_visit() itself will automatically recurse through
1131  * all members of the given struct or union.
1132  */
1133 /*ARGSUSED*/
1134 static int
1135 print_sou(const char *type, const char *name, mdb_ctf_id_t id,
1136     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1137 {
1138 	if (pap->pa_depth == pap->pa_maxdepth)
1139 		mdb_printf("{ ... }");
1140 	else
1141 		mdb_printf("{");
1142 	pap->pa_delim = "\n";
1143 	return (0);
1144 }
1145 
1146 /*
1147  * Print an enum value.  We attempt to convert the value to the corresponding
1148  * enum name and print that if possible.
1149  */
1150 /*ARGSUSED*/
1151 static int
1152 print_enum(const char *type, const char *name, mdb_ctf_id_t id,
1153     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1154 {
1155 	mdb_tgt_addr_t addr = pap->pa_addr + off / NBBY;
1156 	char vname[MDB_SYM_NAMLEN];
1157 	const char *ename;
1158 	int value;
1159 
1160 	if (!(pap->pa_flags & PA_SHOWVAL))
1161 		return (0);
1162 
1163 	if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as,
1164 	    &value, sizeof (value), addr) != sizeof (value)) {
1165 		mdb_warn("failed to read %s integer at %llx", name, addr);
1166 		return (1);
1167 	}
1168 
1169 	if (pap->pa_flags & PA_INTHEX)
1170 		mdb_printf("%#x", value);
1171 	else
1172 		mdb_printf("%#d", value);
1173 
1174 	ename = mdb_ctf_enum_name(base, value);
1175 
1176 	/* If it wasn't an exact match, check if we can do P2 matching */
1177 	if (ename == NULL &&
1178 	    enum_value_to_name_p2(base, value, vname, sizeof (vname)) == 0)
1179 		ename = vname;
1180 
1181 	mdb_printf(" (%s)", (ename != NULL)? ename : "???");
1182 
1183 	return (0);
1184 }
1185 
1186 /*
1187  * This will only get called if the structure isn't found in any available CTF
1188  * data.
1189  */
1190 /*ARGSUSED*/
1191 static int
1192 print_tag(const char *type, const char *name, mdb_ctf_id_t id,
1193     mdb_ctf_id_t base, ulong_t off, printarg_t *pap)
1194 {
1195 	char basename[MDB_SYM_NAMLEN];
1196 
1197 	if (pap->pa_flags & PA_SHOWVAL)
1198 		mdb_printf("; ");
1199 
1200 	if (mdb_ctf_type_name(base, basename, sizeof (basename)) != NULL)
1201 		mdb_printf("<forward declaration of %s>", basename);
1202 	else
1203 		mdb_printf("<forward declaration of unknown type>");
1204 
1205 	return (0);
1206 }
1207 
1208 static void
1209 print_hole(printarg_t *pap, int depth, ulong_t off, ulong_t endoff)
1210 {
1211 	ulong_t bits = endoff - off;
1212 	ulong_t size = bits / NBBY;
1213 	ctf_encoding_t e;
1214 
1215 	static const char *const name = "<<HOLE>>";
1216 	char type[MDB_SYM_NAMLEN];
1217 
1218 	int bitfield =
1219 	    (off % NBBY != 0 ||
1220 	    bits % NBBY != 0 ||
1221 	    size > 8 ||
1222 	    (size & (size - 1)) != 0);
1223 
1224 	ASSERT(off < endoff);
1225 
1226 	if (bits > NBBY * sizeof (uint64_t)) {
1227 		ulong_t end;
1228 
1229 		/*
1230 		 * The hole is larger than the largest integer type.  To
1231 		 * handle this, we split up the hole at 8-byte-aligned
1232 		 * boundaries, recursing to print each subsection.  For
1233 		 * normal C structures, we'll loop at most twice.
1234 		 */
1235 		for (; off < endoff; off = end) {
1236 			end = P2END(off, NBBY * sizeof (uint64_t));
1237 			if (end > endoff)
1238 				end = endoff;
1239 
1240 			ASSERT((end - off) <= NBBY * sizeof (uint64_t));
1241 			print_hole(pap, depth, off, end);
1242 		}
1243 		ASSERT(end == endoff);
1244 
1245 		return;
1246 	}
1247 
1248 	if (bitfield)
1249 		(void) mdb_snprintf(type, sizeof (type), "unsigned");
1250 	else
1251 		(void) mdb_snprintf(type, sizeof (type), "uint%d_t", bits);
1252 
1253 	if (pap->pa_flags & (PA_SHOWTYPE | PA_SHOWNAME | PA_SHOWADDR))
1254 		mdb_printf("%*s", (depth + pap->pa_nest) * pap->pa_tab, "");
1255 
1256 	if (pap->pa_flags & PA_SHOWADDR) {
1257 		if (off % NBBY == 0)
1258 			mdb_printf("%llx ", pap->pa_addr + off / NBBY);
1259 		else
1260 			mdb_printf("%llx.%lx ",
1261 			    pap->pa_addr + off / NBBY, off % NBBY);
1262 	}
1263 
1264 	if (pap->pa_flags & PA_SHOWTYPE)
1265 		mdb_printf("%s ", type);
1266 
1267 	if (pap->pa_flags & PA_SHOWNAME)
1268 		mdb_printf("%s", name);
1269 
1270 	if (bitfield && (pap->pa_flags & PA_SHOWTYPE))
1271 		mdb_printf(" :%d", bits);
1272 
1273 	mdb_printf("%s ", (pap->pa_flags & PA_SHOWVAL)? " =" : "");
1274 
1275 	/*
1276 	 * We fake up a ctf_encoding_t, and use print_int_val() to print
1277 	 * the value.  Holes are always processed as unsigned integers.
1278 	 */
1279 	bzero(&e, sizeof (e));
1280 	e.cte_format = 0;
1281 	e.cte_offset = 0;
1282 	e.cte_bits = bits;
1283 
1284 	if (print_int_val(type, &e, off, pap) != 0)
1285 		mdb_iob_discard(mdb.m_out);
1286 	else
1287 		mdb_iob_puts(mdb.m_out, pap->pa_delim);
1288 }
1289 
1290 /*
1291  * The print_close_sou() function is called for each structure or union
1292  * which has been completed.  For structures, we detect and print any holes
1293  * before printing the closing brace.
1294  */
1295 static void
1296 print_close_sou(printarg_t *pap, int newdepth)
1297 {
1298 	int d = newdepth + pap->pa_nest;
1299 
1300 	if ((pap->pa_flags & PA_SHOWHOLES) && !pap->pa_holes[d].hi_isunion) {
1301 		ulong_t end = pap->pa_holes[d + 1].hi_offset;
1302 		ulong_t expected = pap->pa_holes[d].hi_offset;
1303 
1304 		if (end < expected)
1305 			print_hole(pap, newdepth + 1, end, expected);
1306 	}
1307 	/* if the struct is an array element, print a comma after the } */
1308 	mdb_printf("%*s}%s\n", d * pap->pa_tab, "",
1309 	    (newdepth == 0 && pap->pa_nest > 0)? "," : "");
1310 }
1311 
1312 static printarg_f *const printfuncs[] = {
1313 	print_int,	/* CTF_K_INTEGER */
1314 	print_float,	/* CTF_K_FLOAT */
1315 	print_ptr,	/* CTF_K_POINTER */
1316 	print_array,	/* CTF_K_ARRAY */
1317 	print_ptr,	/* CTF_K_FUNCTION */
1318 	print_sou,	/* CTF_K_STRUCT */
1319 	print_sou,	/* CTF_K_UNION */
1320 	print_enum,	/* CTF_K_ENUM */
1321 	print_tag	/* CTF_K_FORWARD */
1322 };
1323 
1324 /*
1325  * The elt_print function is used as the mdb_ctf_type_visit callback.  For
1326  * each element, we print an appropriate name prefix and then call the
1327  * print subroutine for this type class in the array above.
1328  */
1329 static int
1330 elt_print(const char *name, mdb_ctf_id_t id, mdb_ctf_id_t base,
1331     ulong_t off, int depth, void *data)
1332 {
1333 	char type[MDB_SYM_NAMLEN + sizeof (" <<12345678...>>")];
1334 	int kind, rc, d;
1335 	printarg_t *pap = data;
1336 
1337 	for (d = pap->pa_depth - 1; d >= depth; d--)
1338 		print_close_sou(pap, d);
1339 
1340 	if (depth > pap->pa_maxdepth)
1341 		return (0);
1342 
1343 	if (!mdb_ctf_type_valid(base) ||
1344 	    (kind = mdb_ctf_type_kind(base)) == -1)
1345 		return (-1); /* errno is set for us */
1346 
1347 	if (mdb_ctf_type_name(id, type, MDB_SYM_NAMLEN) == NULL)
1348 		(void) strcpy(type, "(?)");
1349 
1350 	if (pap->pa_flags & PA_SHOWBASETYPE) {
1351 		/*
1352 		 * If basetype is different and informative, concatenate
1353 		 * <<basetype>> (or <<baset...>> if it doesn't fit)
1354 		 *
1355 		 * We just use the end of the buffer to store the type name, and
1356 		 * only connect it up if that's necessary.
1357 		 */
1358 
1359 		char *type_end = type + strlen(type);
1360 		char *basetype;
1361 		size_t sz;
1362 
1363 		(void) strlcat(type, " <<", sizeof (type));
1364 
1365 		basetype = type + strlen(type);
1366 		sz = sizeof (type) - (basetype - type);
1367 
1368 		*type_end = '\0'; /* restore the end of type for strcmp() */
1369 
1370 		if (mdb_ctf_type_name(base, basetype, sz) != NULL &&
1371 		    strcmp(basetype, type) != 0 &&
1372 		    strcmp(basetype, "struct ") != 0 &&
1373 		    strcmp(basetype, "enum ") != 0 &&
1374 		    strcmp(basetype, "union ") != 0) {
1375 			type_end[0] = ' ';	/* reconnect */
1376 			if (strlcat(type, ">>", sizeof (type)) >= sizeof (type))
1377 				(void) strlcpy(
1378 				    type + sizeof (type) - 6, "...>>", 6);
1379 		}
1380 	}
1381 
1382 	if (pap->pa_flags & PA_SHOWHOLES) {
1383 		ctf_encoding_t e;
1384 		ssize_t nsize;
1385 		ulong_t newoff;
1386 		holeinfo_t *hole;
1387 		int extra = IS_COMPOSITE(kind)? 1 : 0;
1388 
1389 		/*
1390 		 * grow the hole array, if necessary
1391 		 */
1392 		if (pap->pa_nest + depth + extra >= pap->pa_nholes) {
1393 			int new = MAX(MAX(8, pap->pa_nholes * 2),
1394 			    pap->pa_nest + depth + extra + 1);
1395 
1396 			holeinfo_t *nhi = mdb_zalloc(
1397 			    sizeof (*nhi) * new, UM_NOSLEEP | UM_GC);
1398 
1399 			bcopy(pap->pa_holes, nhi,
1400 			    pap->pa_nholes * sizeof (*nhi));
1401 
1402 			pap->pa_holes = nhi;
1403 			pap->pa_nholes = new;
1404 		}
1405 
1406 		hole = &pap->pa_holes[depth + pap->pa_nest];
1407 
1408 		if (depth != 0 && off > hole->hi_offset)
1409 			print_hole(pap, depth, hole->hi_offset, off);
1410 
1411 		/* compute the next expected offset */
1412 		if (kind == CTF_K_INTEGER &&
1413 		    mdb_ctf_type_encoding(base, &e) == 0)
1414 			newoff = off + e.cte_bits;
1415 		else if ((nsize = mdb_ctf_type_size(base)) >= 0)
1416 			newoff = off + nsize * NBBY;
1417 		else {
1418 			/* something bad happened, disable hole checking */
1419 			newoff = -1UL;		/* ULONG_MAX */
1420 		}
1421 
1422 		hole->hi_offset = newoff;
1423 
1424 		if (IS_COMPOSITE(kind)) {
1425 			hole->hi_isunion = (kind == CTF_K_UNION);
1426 			hole++;
1427 			hole->hi_offset = off;
1428 		}
1429 	}
1430 
1431 	if (pap->pa_flags & (PA_SHOWTYPE | PA_SHOWNAME | PA_SHOWADDR))
1432 		mdb_printf("%*s", (depth + pap->pa_nest) * pap->pa_tab, "");
1433 
1434 	if (pap->pa_flags & PA_SHOWADDR) {
1435 		if (off % NBBY == 0)
1436 			mdb_printf("%llx ", pap->pa_addr + off / NBBY);
1437 		else
1438 			mdb_printf("%llx.%lx ",
1439 			    pap->pa_addr + off / NBBY, off % NBBY);
1440 	}
1441 
1442 	if ((pap->pa_flags & PA_SHOWTYPE)) {
1443 		mdb_printf("%s", type);
1444 		/*
1445 		 * We want to avoid printing a trailing space when
1446 		 * dealing with pointers in a structure, so we end
1447 		 * up with:
1448 		 *
1449 		 *	label_t *t_onfault = 0
1450 		 *
1451 		 * If depth is zero, always print the trailing space unless
1452 		 * we also have a prefix.
1453 		 */
1454 		if (type[strlen(type) - 1] != '*' ||
1455 		    (depth == 0 && (!(pap->pa_flags & PA_SHOWNAME) ||
1456 		    pap->pa_prefix == NULL)))
1457 			mdb_printf(" ");
1458 	}
1459 
1460 	if (pap->pa_flags & PA_SHOWNAME) {
1461 		if (pap->pa_prefix != NULL && depth <= 1)
1462 			mdb_printf("%s%s", pap->pa_prefix,
1463 			    (depth == 0) ? "" : pap->pa_suffix);
1464 		mdb_printf("%s", name);
1465 	}
1466 
1467 	if ((pap->pa_flags & PA_SHOWTYPE) && kind == CTF_K_INTEGER) {
1468 		ctf_encoding_t e;
1469 
1470 		if (mdb_ctf_type_encoding(base, &e) == 0) {
1471 			ulong_t bits = e.cte_bits;
1472 			ulong_t size = bits / NBBY;
1473 
1474 			if (bits % NBBY != 0 ||
1475 			    off % NBBY != 0 ||
1476 			    size > 8 ||
1477 			    size != mdb_ctf_type_size(base))
1478 				mdb_printf(" :%d", bits);
1479 		}
1480 	}
1481 
1482 	if (depth != 0 ||
1483 	    ((pap->pa_flags & PA_SHOWNAME) && pap->pa_prefix != NULL))
1484 		mdb_printf("%s ", pap->pa_flags & PA_SHOWVAL ? " =" : "");
1485 
1486 	if (depth == 0 && pap->pa_prefix != NULL)
1487 		name = pap->pa_prefix;
1488 
1489 	pap->pa_depth = depth;
1490 	if (kind <= CTF_K_UNKNOWN || kind >= CTF_K_TYPEDEF) {
1491 		mdb_warn("unknown ctf for %s type %s kind %d\n",
1492 		    name, type, kind);
1493 		return (-1);
1494 	}
1495 	rc = printfuncs[kind - 1](type, name, id, base, off, pap);
1496 
1497 	if (rc != 0)
1498 		mdb_iob_discard(mdb.m_out);
1499 	else
1500 		mdb_iob_puts(mdb.m_out, pap->pa_delim);
1501 
1502 	return (rc);
1503 }
1504 
1505 /*
1506  * Special semantics for pipelines.
1507  */
1508 static int
1509 pipe_print(mdb_ctf_id_t id, ulong_t off, void *data)
1510 {
1511 	printarg_t *pap = data;
1512 	ssize_t size;
1513 	static const char *const fsp[] = { "%#r", "%#r", "%#r", "%#llr" };
1514 	uintptr_t value;
1515 	uintptr_t addr = pap->pa_addr + off / NBBY;
1516 	mdb_ctf_id_t base;
1517 	ctf_encoding_t e;
1518 
1519 	union {
1520 		uint64_t i8;
1521 		uint32_t i4;
1522 		uint16_t i2;
1523 		uint8_t i1;
1524 	} u;
1525 
1526 	if (mdb_ctf_type_resolve(id, &base) == -1) {
1527 		mdb_warn("could not resolve type\n");
1528 		return (-1);
1529 	}
1530 
1531 	/*
1532 	 * If the user gives -a, then always print out the address of the
1533 	 * member.
1534 	 */
1535 	if ((pap->pa_flags & PA_SHOWADDR)) {
1536 		mdb_printf("%#lr\n", addr);
1537 		return (0);
1538 	}
1539 
1540 again:
1541 	switch (mdb_ctf_type_kind(base)) {
1542 	case CTF_K_POINTER:
1543 		if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as,
1544 		    &value, sizeof (value), addr) != sizeof (value)) {
1545 			mdb_warn("failed to read pointer at %p", addr);
1546 			return (-1);
1547 		}
1548 		mdb_printf("%#lr\n", value);
1549 		break;
1550 
1551 	case CTF_K_INTEGER:
1552 	case CTF_K_ENUM:
1553 		if (mdb_ctf_type_encoding(base, &e) != 0) {
1554 			mdb_printf("could not get type encoding\n");
1555 			return (-1);
1556 		}
1557 
1558 		/*
1559 		 * For immediate values, we just print out the value.
1560 		 */
1561 		size = e.cte_bits / NBBY;
1562 		if (size > 8 || (e.cte_bits % NBBY) != 0 ||
1563 		    (size & (size - 1)) != 0) {
1564 			return (print_bitfield(off, pap, &e));
1565 		}
1566 
1567 		if (mdb_tgt_aread(pap->pa_tgt, pap->pa_as, &u.i8, size,
1568 		    addr) != size) {
1569 			mdb_warn("failed to read %lu bytes at %p",
1570 			    (ulong_t)size, pap->pa_addr);
1571 			return (-1);
1572 		}
1573 
1574 		switch (size) {
1575 		case sizeof (uint8_t):
1576 			mdb_printf(fsp[0], u.i1);
1577 			break;
1578 		case sizeof (uint16_t):
1579 			mdb_printf(fsp[1], u.i2);
1580 			break;
1581 		case sizeof (uint32_t):
1582 			mdb_printf(fsp[2], u.i4);
1583 			break;
1584 		case sizeof (uint64_t):
1585 			mdb_printf(fsp[3], u.i8);
1586 			break;
1587 		}
1588 		mdb_printf("\n");
1589 		break;
1590 
1591 	case CTF_K_FUNCTION:
1592 	case CTF_K_FLOAT:
1593 	case CTF_K_ARRAY:
1594 	case CTF_K_UNKNOWN:
1595 	case CTF_K_STRUCT:
1596 	case CTF_K_UNION:
1597 	case CTF_K_FORWARD:
1598 		/*
1599 		 * For these types, always print the address of the member
1600 		 */
1601 		mdb_printf("%#lr\n", addr);
1602 		break;
1603 
1604 	default:
1605 		mdb_warn("unknown type %d", mdb_ctf_type_kind(base));
1606 		break;
1607 	}
1608 
1609 	return (0);
1610 }
1611 
1612 static int
1613 parse_delimiter(char **strp)
1614 {
1615 	switch (**strp) {
1616 	case '\0':
1617 		return (MEMBER_DELIM_DONE);
1618 
1619 	case '.':
1620 		*strp = *strp + 1;
1621 		return (MEMBER_DELIM_DOT);
1622 
1623 	case '[':
1624 		*strp = *strp + 1;
1625 		return (MEMBER_DELIM_LBR);
1626 
1627 	case '-':
1628 		*strp = *strp + 1;
1629 		if (**strp == '>') {
1630 			*strp = *strp + 1;
1631 			return (MEMBER_DELIM_PTR);
1632 		}
1633 		*strp = *strp - 1;
1634 		/*FALLTHROUGH*/
1635 	default:
1636 		return (MEMBER_DELIM_ERR);
1637 	}
1638 }
1639 
1640 static int
1641 deref(printarg_t *pap, size_t size)
1642 {
1643 	uint32_t a32;
1644 	mdb_tgt_as_t as = pap->pa_as;
1645 	mdb_tgt_addr_t *ap = &pap->pa_addr;
1646 
1647 	if (size == sizeof (mdb_tgt_addr_t)) {
1648 		if (mdb_tgt_aread(mdb.m_target, as, ap, size, *ap) == -1) {
1649 			mdb_warn("could not dereference pointer %llx\n", *ap);
1650 			return (-1);
1651 		}
1652 	} else {
1653 		if (mdb_tgt_aread(mdb.m_target, as, &a32, size, *ap) == -1) {
1654 			mdb_warn("could not dereference pointer %x\n", *ap);
1655 			return (-1);
1656 		}
1657 
1658 		*ap = (mdb_tgt_addr_t)a32;
1659 	}
1660 
1661 	/*
1662 	 * We've dereferenced at least once, we must be on the real
1663 	 * target. If we were in the immediate target, reset to the real
1664 	 * target; it's reset as needed when we return to the print
1665 	 * routines.
1666 	 */
1667 	if (pap->pa_tgt == pap->pa_immtgt)
1668 		pap->pa_tgt = pap->pa_realtgt;
1669 
1670 	return (0);
1671 }
1672 
1673 static int
1674 parse_member(printarg_t *pap, const char *str, mdb_ctf_id_t id,
1675     mdb_ctf_id_t *idp, ulong_t *offp, int *last_deref)
1676 {
1677 	int delim;
1678 	char member[64];
1679 	char buf[128];
1680 	uint_t index;
1681 	char *start = (char *)str;
1682 	char *end;
1683 	ulong_t off = 0;
1684 	mdb_ctf_arinfo_t ar;
1685 	mdb_ctf_id_t rid;
1686 	int kind;
1687 	ssize_t size;
1688 	int non_array = FALSE;
1689 
1690 	/*
1691 	 * id always has the unresolved type for printing error messages
1692 	 * that include the type; rid always has the resolved type for
1693 	 * use in mdb_ctf_* calls.  It is possible for this command to fail,
1694 	 * however, if the resolved type is in the parent and it is currently
1695 	 * unavailable.  Note that we also can't print out the name of the
1696 	 * type, since that would also rely on looking up the resolved name.
1697 	 */
1698 	if (mdb_ctf_type_resolve(id, &rid) != 0) {
1699 		mdb_warn("failed to resolve type");
1700 		return (-1);
1701 	}
1702 
1703 	delim = parse_delimiter(&start);
1704 	/*
1705 	 * If the user fails to specify an initial delimiter, guess -> for
1706 	 * pointer types and . for non-pointer types.
1707 	 */
1708 	if (delim == MEMBER_DELIM_ERR)
1709 		delim = (mdb_ctf_type_kind(rid) == CTF_K_POINTER) ?
1710 		    MEMBER_DELIM_PTR : MEMBER_DELIM_DOT;
1711 
1712 	*last_deref = FALSE;
1713 
1714 	while (delim != MEMBER_DELIM_DONE) {
1715 		switch (delim) {
1716 		case MEMBER_DELIM_PTR:
1717 			kind = mdb_ctf_type_kind(rid);
1718 			if (kind != CTF_K_POINTER) {
1719 				mdb_warn("%s is not a pointer type\n",
1720 				    mdb_ctf_type_name(id, buf, sizeof (buf)));
1721 				return (-1);
1722 			}
1723 
1724 			size = mdb_ctf_type_size(id);
1725 			if (deref(pap, size) != 0)
1726 				return (-1);
1727 
1728 			(void) mdb_ctf_type_reference(rid, &id);
1729 			(void) mdb_ctf_type_resolve(id, &rid);
1730 
1731 			off = 0;
1732 			break;
1733 
1734 		case MEMBER_DELIM_DOT:
1735 			kind = mdb_ctf_type_kind(rid);
1736 			if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) {
1737 				mdb_warn("%s is not a struct or union type\n",
1738 				    mdb_ctf_type_name(id, buf, sizeof (buf)));
1739 				return (-1);
1740 			}
1741 			break;
1742 
1743 		case MEMBER_DELIM_LBR:
1744 			end = strchr(start, ']');
1745 			if (end == NULL) {
1746 				mdb_warn("no trailing ']'\n");
1747 				return (-1);
1748 			}
1749 
1750 			(void) mdb_snprintf(member, end - start + 1, start);
1751 
1752 			index = mdb_strtoull(member);
1753 
1754 			switch (mdb_ctf_type_kind(rid)) {
1755 			case CTF_K_POINTER:
1756 				size = mdb_ctf_type_size(rid);
1757 
1758 				if (deref(pap, size) != 0)
1759 					return (-1);
1760 
1761 				(void) mdb_ctf_type_reference(rid, &id);
1762 				(void) mdb_ctf_type_resolve(id, &rid);
1763 
1764 				size = mdb_ctf_type_size(id);
1765 				if (size <= 0) {
1766 					mdb_warn("cannot dereference void "
1767 					    "type\n");
1768 					return (-1);
1769 				}
1770 
1771 				pap->pa_addr += index * size;
1772 				off = 0;
1773 
1774 				if (index == 0 && non_array)
1775 					*last_deref = TRUE;
1776 				break;
1777 
1778 			case CTF_K_ARRAY:
1779 				(void) mdb_ctf_array_info(rid, &ar);
1780 
1781 				if (index >= ar.mta_nelems) {
1782 					mdb_warn("index %r is outside of "
1783 					    "array bounds [0 .. %r]\n",
1784 					    index, ar.mta_nelems - 1);
1785 				}
1786 
1787 				id = ar.mta_contents;
1788 				(void) mdb_ctf_type_resolve(id, &rid);
1789 
1790 				size = mdb_ctf_type_size(id);
1791 				if (size <= 0) {
1792 					mdb_warn("cannot dereference void "
1793 					    "type\n");
1794 					return (-1);
1795 				}
1796 
1797 				pap->pa_addr += index * size;
1798 				off = 0;
1799 				break;
1800 
1801 			default:
1802 				mdb_warn("cannot index into non-array, "
1803 				    "non-pointer type\n");
1804 				return (-1);
1805 			}
1806 
1807 			start = end + 1;
1808 			delim = parse_delimiter(&start);
1809 			continue;
1810 
1811 		case MEMBER_DELIM_ERR:
1812 		default:
1813 			mdb_warn("'%c' is not a valid delimiter\n", *start);
1814 			return (-1);
1815 		}
1816 
1817 		*last_deref = FALSE;
1818 		non_array = TRUE;
1819 
1820 		/*
1821 		 * Find the end of the member name; assume that a member
1822 		 * name is at least one character long.
1823 		 */
1824 		for (end = start + 1; isalnum(*end) || *end == '_'; end++)
1825 			continue;
1826 
1827 		(void) mdb_snprintf(member, end - start + 1, start);
1828 
1829 		if (mdb_ctf_member_info(rid, member, &off, &id) != 0) {
1830 			mdb_warn("failed to find member %s of %s", member,
1831 			    mdb_ctf_type_name(id, buf, sizeof (buf)));
1832 			return (-1);
1833 		}
1834 		(void) mdb_ctf_type_resolve(id, &rid);
1835 
1836 		pap->pa_addr += off / NBBY;
1837 
1838 		start = end;
1839 		delim = parse_delimiter(&start);
1840 	}
1841 
1842 
1843 	*idp = id;
1844 	*offp = off;
1845 
1846 	return (0);
1847 }
1848 
1849 /*
1850  * Recursively descend a print a given data structure.  We create a struct of
1851  * the relevant print arguments and then call mdb_ctf_type_visit() to do the
1852  * traversal, using elt_print() as the callback for each element.
1853  */
1854 /*ARGSUSED*/
1855 int
1856 cmd_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1857 {
1858 	uintptr_t opt_c = MDB_ARR_NOLIMIT, opt_l = MDB_ARR_NOLIMIT;
1859 	uint_t opt_C = FALSE, opt_L = FALSE, opt_p = FALSE, opt_i = FALSE;
1860 	uintptr_t opt_s = (uintptr_t)-1ul;
1861 	int uflags = (flags & DCMD_ADDRSPEC) ? PA_SHOWVAL : 0;
1862 	mdb_ctf_id_t id;
1863 	int err = DCMD_OK;
1864 
1865 	mdb_tgt_t *t = mdb.m_target;
1866 	printarg_t pa;
1867 	int d, i;
1868 
1869 	char s_name[MDB_SYM_NAMLEN];
1870 	mdb_syminfo_t s_info;
1871 	GElf_Sym sym;
1872 
1873 	i = mdb_getopts(argc, argv,
1874 	    'a', MDB_OPT_SETBITS, PA_SHOWADDR, &uflags,
1875 	    'C', MDB_OPT_SETBITS, TRUE, &opt_C,
1876 	    'c', MDB_OPT_UINTPTR, &opt_c,
1877 	    'd', MDB_OPT_SETBITS, PA_INTDEC, &uflags,
1878 	    'h', MDB_OPT_SETBITS, PA_SHOWHOLES, &uflags,
1879 	    'i', MDB_OPT_SETBITS, TRUE, &opt_i,
1880 	    'L', MDB_OPT_SETBITS, TRUE, &opt_L,
1881 	    'l', MDB_OPT_UINTPTR, &opt_l,
1882 	    'n', MDB_OPT_SETBITS, PA_NOSYMBOLIC, &uflags,
1883 	    'p', MDB_OPT_SETBITS, TRUE, &opt_p,
1884 	    's', MDB_OPT_UINTPTR, &opt_s,
1885 	    'T', MDB_OPT_SETBITS, PA_SHOWTYPE | PA_SHOWBASETYPE, &uflags,
1886 	    't', MDB_OPT_SETBITS, PA_SHOWTYPE, &uflags,
1887 	    'x', MDB_OPT_SETBITS, PA_INTHEX, &uflags,
1888 	    NULL);
1889 
1890 	if (uflags & PA_INTHEX)
1891 		uflags &= ~PA_INTDEC;	/* -x and -d are mutually exclusive */
1892 
1893 	uflags |= PA_SHOWNAME;
1894 
1895 	if (opt_p && opt_i) {
1896 		mdb_warn("-p and -i options are incompatible\n");
1897 		return (DCMD_ERR);
1898 	}
1899 
1900 	argc -= i;
1901 	argv += i;
1902 
1903 	if (argc != 0 && argv->a_type == MDB_TYPE_STRING) {
1904 		const char *t_name = s_name;
1905 		int ret;
1906 
1907 		if (strchr("+-", argv->a_un.a_str[0]) != NULL)
1908 			return (DCMD_USAGE);
1909 
1910 		if ((ret = args_to_typename(&argc, &argv, s_name,
1911 		    sizeof (s_name))) != 0)
1912 			return (ret);
1913 
1914 		if (mdb_ctf_lookup_by_name(t_name, &id) != 0) {
1915 			if (!(flags & DCMD_ADDRSPEC) || opt_i ||
1916 			    addr_to_sym(t, addr, s_name, sizeof (s_name),
1917 			    &sym, &s_info) == NULL ||
1918 			    mdb_ctf_lookup_by_symbol(&sym, &s_info, &id) != 0) {
1919 
1920 				mdb_warn("failed to look up type %s", t_name);
1921 				return (DCMD_ABORT);
1922 			}
1923 		} else {
1924 			argc--;
1925 			argv++;
1926 		}
1927 
1928 	} else if (!(flags & DCMD_ADDRSPEC) || opt_i) {
1929 		return (DCMD_USAGE);
1930 
1931 	} else if (addr_to_sym(t, addr, s_name, sizeof (s_name),
1932 	    &sym, &s_info) == NULL) {
1933 		mdb_warn("no symbol information for %a", addr);
1934 		return (DCMD_ERR);
1935 
1936 	} else if (mdb_ctf_lookup_by_symbol(&sym, &s_info, &id) != 0) {
1937 		mdb_warn("no type data available for %a [%u]", addr,
1938 		    s_info.sym_id);
1939 		return (DCMD_ERR);
1940 	}
1941 
1942 	pa.pa_tgt = mdb.m_target;
1943 	pa.pa_realtgt = pa.pa_tgt;
1944 	pa.pa_immtgt = NULL;
1945 	pa.pa_as = opt_p ? MDB_TGT_AS_PHYS : MDB_TGT_AS_VIRT;
1946 	pa.pa_armemlim = mdb.m_armemlim;
1947 	pa.pa_arstrlim = mdb.m_arstrlim;
1948 	pa.pa_delim = "\n";
1949 	pa.pa_flags = uflags;
1950 	pa.pa_nest = 0;
1951 	pa.pa_tab = 4;
1952 	pa.pa_prefix = NULL;
1953 	pa.pa_suffix = NULL;
1954 	pa.pa_holes = NULL;
1955 	pa.pa_nholes = 0;
1956 	pa.pa_depth = 0;
1957 	pa.pa_maxdepth = opt_s;
1958 
1959 	if ((flags & DCMD_ADDRSPEC) && !opt_i)
1960 		pa.pa_addr = opt_p ? mdb_get_dot() : addr;
1961 	else
1962 		pa.pa_addr = NULL;
1963 
1964 	if (opt_i) {
1965 		const char *vargv[2];
1966 		uintmax_t dot = mdb_get_dot();
1967 		size_t outsize = mdb_ctf_type_size(id);
1968 		vargv[0] = (const char *)&dot;
1969 		vargv[1] = (const char *)&outsize;
1970 		pa.pa_immtgt = mdb_tgt_create(mdb_value_tgt_create,
1971 		    0, 2, vargv);
1972 		pa.pa_tgt = pa.pa_immtgt;
1973 	}
1974 
1975 	if (opt_c != MDB_ARR_NOLIMIT)
1976 		pa.pa_arstrlim = opt_c;
1977 	if (opt_C)
1978 		pa.pa_arstrlim = MDB_ARR_NOLIMIT;
1979 	if (opt_l != MDB_ARR_NOLIMIT)
1980 		pa.pa_armemlim = opt_l;
1981 	if (opt_L)
1982 		pa.pa_armemlim = MDB_ARR_NOLIMIT;
1983 
1984 	if (argc > 0) {
1985 		for (i = 0; i < argc; i++) {
1986 			mdb_ctf_id_t mid;
1987 			int last_deref;
1988 			ulong_t off;
1989 			int kind;
1990 			char buf[MDB_SYM_NAMLEN];
1991 
1992 			mdb_tgt_t *oldtgt = pa.pa_tgt;
1993 			mdb_tgt_as_t oldas = pa.pa_as;
1994 			mdb_tgt_addr_t oldaddr = pa.pa_addr;
1995 
1996 			if (argv->a_type == MDB_TYPE_STRING) {
1997 				const char *member = argv[i].a_un.a_str;
1998 				mdb_ctf_id_t rid;
1999 
2000 				if (parse_member(&pa, member, id, &mid,
2001 				    &off, &last_deref) != 0) {
2002 					err = DCMD_ABORT;
2003 					goto out;
2004 				}
2005 
2006 				/*
2007 				 * If the member string ends with a "[0]"
2008 				 * (last_deref * is true) and the type is a
2009 				 * structure or union, * print "->" rather
2010 				 * than "[0]." in elt_print.
2011 				 */
2012 				(void) mdb_ctf_type_resolve(mid, &rid);
2013 				kind = mdb_ctf_type_kind(rid);
2014 				if (last_deref && IS_SOU(kind)) {
2015 					char *end;
2016 					(void) mdb_snprintf(buf, sizeof (buf),
2017 					    "%s", member);
2018 					end = strrchr(buf, '[');
2019 					*end = '\0';
2020 					pa.pa_suffix = "->";
2021 					member = &buf[0];
2022 				} else if (IS_SOU(kind)) {
2023 					pa.pa_suffix = ".";
2024 				} else {
2025 					pa.pa_suffix = "";
2026 				}
2027 
2028 				pa.pa_prefix = member;
2029 			} else {
2030 				ulong_t moff;
2031 
2032 				moff = (ulong_t)argv[i].a_un.a_val;
2033 
2034 				if (mdb_ctf_offset_to_name(id, moff * NBBY,
2035 				    buf, sizeof (buf), 0, &mid, &off) == -1) {
2036 					mdb_warn("invalid offset %lx\n", moff);
2037 					err = DCMD_ABORT;
2038 					goto out;
2039 				}
2040 
2041 				pa.pa_prefix = buf;
2042 				pa.pa_addr += moff - off / NBBY;
2043 				pa.pa_suffix = strlen(buf) == 0 ? "" : ".";
2044 			}
2045 
2046 			off %= NBBY;
2047 			if (flags & DCMD_PIPE_OUT) {
2048 				if (pipe_print(mid, off, &pa) != 0) {
2049 					mdb_warn("failed to print type");
2050 					err = DCMD_ERR;
2051 					goto out;
2052 				}
2053 			} else if (off != 0) {
2054 				mdb_ctf_id_t base;
2055 				(void) mdb_ctf_type_resolve(mid, &base);
2056 
2057 				if (elt_print("", mid, base, off, 0,
2058 				    &pa) != 0) {
2059 					mdb_warn("failed to print type");
2060 					err = DCMD_ERR;
2061 					goto out;
2062 				}
2063 			} else {
2064 				if (mdb_ctf_type_visit(mid, elt_print,
2065 				    &pa) == -1) {
2066 					mdb_warn("failed to print type");
2067 					err = DCMD_ERR;
2068 					goto out;
2069 				}
2070 
2071 				for (d = pa.pa_depth - 1; d >= 0; d--)
2072 					print_close_sou(&pa, d);
2073 			}
2074 
2075 			pa.pa_depth = 0;
2076 			pa.pa_tgt = oldtgt;
2077 			pa.pa_as = oldas;
2078 			pa.pa_addr = oldaddr;
2079 			pa.pa_delim = "\n";
2080 		}
2081 
2082 	} else if (flags & DCMD_PIPE_OUT) {
2083 		if (pipe_print(id, 0, &pa) != 0) {
2084 			mdb_warn("failed to print type");
2085 			err = DCMD_ERR;
2086 			goto out;
2087 		}
2088 	} else {
2089 		if (mdb_ctf_type_visit(id, elt_print, &pa) == -1) {
2090 			mdb_warn("failed to print type");
2091 			err = DCMD_ERR;
2092 			goto out;
2093 		}
2094 
2095 		for (d = pa.pa_depth - 1; d >= 0; d--)
2096 			print_close_sou(&pa, d);
2097 	}
2098 
2099 	mdb_set_dot(addr + mdb_ctf_type_size(id));
2100 	err = DCMD_OK;
2101 out:
2102 	if (pa.pa_immtgt)
2103 		mdb_tgt_destroy(pa.pa_immtgt);
2104 	return (err);
2105 }
2106 
2107 void
2108 print_help(void)
2109 {
2110 	mdb_printf(
2111 	    "-a         show address of object\n"
2112 	    "-C         unlimit the length of character arrays\n"
2113 	    "-c limit   limit the length of character arrays\n"
2114 	    "-d         output values in decimal\n"
2115 	    "-h         print holes in structures\n"
2116 	    "-i         interpret address as data of the given type\n"
2117 	    "-L         unlimit the length of standard arrays\n"
2118 	    "-l limit   limit the length of standard arrays\n"
2119 	    "-n         don't print pointers as symbol offsets\n"
2120 	    "-p         interpret address as a physical memory address\n"
2121 	    "-s depth   limit the recursion depth\n"
2122 	    "-T         show type and <<base type>> of object\n"
2123 	    "-t         show type of object\n"
2124 	    "-x         output values in hexadecimal\n"
2125 	    "\n"
2126 	    "type may be omitted if the C type of addr can be inferred.\n"
2127 	    "\n"
2128 	    "Members may be specified with standard C syntax using the\n"
2129 	    "array indexing operator \"[index]\", structure member\n"
2130 	    "operator \".\", or structure pointer operator \"->\".\n"
2131 	    "\n"
2132 	    "Offsets must use the $[ expression ] syntax\n");
2133 }
2134