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