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