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