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