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