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