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 2011 Nexenta Systems, Inc.  All rights reserved.
  23  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2019 Joyent, Inc.
  25  * Copyright (c) 2013 by Delphix. All rights reserved.
  26  */
  27 
  28 #include <mdb/mdb_param.h>
  29 #include <mdb/mdb_modapi.h>
  30 #include <mdb/mdb_ks.h>
  31 #include <mdb/mdb_ctf.h>
  32 
  33 #include <sys/types.h>
  34 #include <sys/thread.h>
  35 #include <sys/session.h>
  36 #include <sys/user.h>
  37 #include <sys/proc.h>
  38 #include <sys/var.h>
  39 #include <sys/t_lock.h>
  40 #include <sys/callo.h>
  41 #include <sys/priocntl.h>
  42 #include <sys/class.h>
  43 #include <sys/regset.h>
  44 #include <sys/stack.h>
  45 #include <sys/cpuvar.h>
  46 #include <sys/vnode.h>
  47 #include <sys/vfs.h>
  48 #include <sys/flock_impl.h>
  49 #include <sys/kmem_impl.h>
  50 #include <sys/vmem_impl.h>
  51 #include <sys/kstat.h>
  52 #include <sys/dditypes.h>
  53 #include <sys/ddi_impldefs.h>
  54 #include <sys/sysmacros.h>
  55 #include <sys/sysconf.h>
  56 #include <sys/task.h>
  57 #include <sys/project.h>
  58 #include <sys/errorq_impl.h>
  59 #include <sys/cred_impl.h>
  60 #include <sys/zone.h>
  61 #include <sys/panic.h>
  62 #include <regex.h>
  63 #include <sys/port_impl.h>
  64 
  65 #include "avl.h"
  66 #include "bio.h"
  67 #include "bitset.h"
  68 #include "combined.h"
  69 #include "contract.h"
  70 #include "cpupart_mdb.h"
  71 #include "cred.h"
  72 #include "ctxop.h"
  73 #include "cyclic.h"
  74 #include "damap.h"
  75 #include "ddi_periodic.h"
  76 #include "devinfo.h"
  77 #include "dnlc.h"
  78 #include "findstack.h"
  79 #include "fm.h"
  80 #include "gcore.h"
  81 #include "group.h"
  82 #include "irm.h"
  83 #include "kgrep.h"
  84 #include "kmem.h"
  85 #include "ldi.h"
  86 #include "leaky.h"
  87 #include "lgrp.h"
  88 #include "list.h"
  89 #include "log.h"
  90 #include "mdi.h"
  91 #include "memory.h"
  92 #include "mmd.h"
  93 #include "modhash.h"
  94 #include "ndievents.h"
  95 #include "net.h"
  96 #include "netstack.h"
  97 #include "nvpair.h"
  98 #include "pci.h"
  99 #include "pg.h"
 100 #include "rctl.h"
 101 #include "sobj.h"
 102 #include "streams.h"
 103 #include "sysevent.h"
 104 #include "taskq.h"
 105 #include "thread.h"
 106 #include "tsd.h"
 107 #include "tsol.h"
 108 #include "typegraph.h"
 109 #include "vfs.h"
 110 #include "zone.h"
 111 #include "hotplug.h"
 112 
 113 /*
 114  * Surely this is defined somewhere...
 115  */
 116 #define NINTR           16
 117 
 118 #define KILOS           10
 119 #define MEGS            20
 120 #define GIGS            30
 121 
 122 #ifndef STACK_BIAS
 123 #define STACK_BIAS      0
 124 #endif
 125 
 126 static char
 127 pstat2ch(uchar_t state)
 128 {
 129         switch (state) {
 130                 case SSLEEP: return ('S');
 131                 case SRUN: return ('R');
 132                 case SZOMB: return ('Z');
 133                 case SIDL: return ('I');
 134                 case SONPROC: return ('O');
 135                 case SSTOP: return ('T');
 136                 case SWAIT: return ('W');
 137                 default: return ('?');
 138         }
 139 }
 140 
 141 #define PS_PRTTHREADS   0x1
 142 #define PS_PRTLWPS      0x2
 143 #define PS_PSARGS       0x4
 144 #define PS_TASKS        0x8
 145 #define PS_PROJECTS     0x10
 146 #define PS_ZONES        0x20
 147 
 148 static int
 149 ps_threadprint(uintptr_t addr, const void *data, void *private)
 150 {
 151         const kthread_t *t = (const kthread_t *)data;
 152         uint_t prt_flags = *((uint_t *)private);
 153 
 154         static const mdb_bitmask_t t_state_bits[] = {
 155                 { "TS_FREE",    UINT_MAX,       TS_FREE         },
 156                 { "TS_SLEEP",   TS_SLEEP,       TS_SLEEP        },
 157                 { "TS_RUN",     TS_RUN,         TS_RUN          },
 158                 { "TS_ONPROC",  TS_ONPROC,      TS_ONPROC       },
 159                 { "TS_ZOMB",    TS_ZOMB,        TS_ZOMB         },
 160                 { "TS_STOPPED", TS_STOPPED,     TS_STOPPED      },
 161                 { "TS_WAIT",    TS_WAIT,        TS_WAIT         },
 162                 { NULL,         0,              0               }
 163         };
 164 
 165         if (prt_flags & PS_PRTTHREADS)
 166                 mdb_printf("\tT  %?a <%b>\n", addr, t->t_state, t_state_bits);
 167 
 168         if (prt_flags & PS_PRTLWPS) {
 169                 char desc[128] = "";
 170 
 171                 (void) thread_getdesc(addr, B_FALSE, desc, sizeof (desc));
 172 
 173                 mdb_printf("\tL  %?a ID: %s\n", t->t_lwp, desc);
 174         }
 175 
 176         return (WALK_NEXT);
 177 }
 178 
 179 typedef struct mdb_pflags_proc {
 180         struct pid      *p_pidp;
 181         ushort_t        p_pidflag;
 182         uint_t          p_proc_flag;
 183         uint_t          p_flag;
 184 } mdb_pflags_proc_t;
 185 
 186 static int
 187 pflags(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 188 {
 189         mdb_pflags_proc_t pr;
 190         struct pid pid;
 191 
 192         static const mdb_bitmask_t p_flag_bits[] = {
 193                 { "SSYS",               SSYS,           SSYS            },
 194                 { "SEXITING",           SEXITING,       SEXITING        },
 195                 { "SITBUSY",            SITBUSY,        SITBUSY         },
 196                 { "SFORKING",           SFORKING,       SFORKING        },
 197                 { "SWATCHOK",           SWATCHOK,       SWATCHOK        },
 198                 { "SKILLED",            SKILLED,        SKILLED         },
 199                 { "SSCONT",             SSCONT,         SSCONT          },
 200                 { "SZONETOP",           SZONETOP,       SZONETOP        },
 201                 { "SEXTKILLED",         SEXTKILLED,     SEXTKILLED      },
 202                 { "SUGID",              SUGID,          SUGID           },
 203                 { "SEXECED",            SEXECED,        SEXECED         },
 204                 { "SJCTL",              SJCTL,          SJCTL           },
 205                 { "SNOWAIT",            SNOWAIT,        SNOWAIT         },
 206                 { "SVFORK",             SVFORK,         SVFORK          },
 207                 { "SVFWAIT",            SVFWAIT,        SVFWAIT         },
 208                 { "SEXITLWPS",          SEXITLWPS,      SEXITLWPS       },
 209                 { "SHOLDFORK",          SHOLDFORK,      SHOLDFORK       },
 210                 { "SHOLDFORK1",         SHOLDFORK1,     SHOLDFORK1      },
 211                 { "SCOREDUMP",          SCOREDUMP,      SCOREDUMP       },
 212                 { "SMSACCT",            SMSACCT,        SMSACCT         },
 213                 { "SLWPWRAP",           SLWPWRAP,       SLWPWRAP        },
 214                 { "SAUTOLPG",           SAUTOLPG,       SAUTOLPG        },
 215                 { "SNOCD",              SNOCD,          SNOCD           },
 216                 { "SHOLDWATCH",         SHOLDWATCH,     SHOLDWATCH      },
 217                 { "SMSFORK",            SMSFORK,        SMSFORK         },
 218                 { "SDOCORE",            SDOCORE,        SDOCORE         },
 219                 { NULL,                 0,              0               }
 220         };
 221 
 222         static const mdb_bitmask_t p_pidflag_bits[] = {
 223                 { "CLDPEND",            CLDPEND,        CLDPEND         },
 224                 { "CLDCONT",            CLDCONT,        CLDCONT         },
 225                 { "CLDNOSIGCHLD",       CLDNOSIGCHLD,   CLDNOSIGCHLD    },
 226                 { "CLDWAITPID",         CLDWAITPID,     CLDWAITPID      },
 227                 { NULL,                 0,              0               }
 228         };
 229 
 230         static const mdb_bitmask_t p_proc_flag_bits[] = {
 231                 { "P_PR_TRACE",         P_PR_TRACE,     P_PR_TRACE      },
 232                 { "P_PR_PTRACE",        P_PR_PTRACE,    P_PR_PTRACE     },
 233                 { "P_PR_FORK",          P_PR_FORK,      P_PR_FORK       },
 234                 { "P_PR_LOCK",          P_PR_LOCK,      P_PR_LOCK       },
 235                 { "P_PR_ASYNC",         P_PR_ASYNC,     P_PR_ASYNC      },
 236                 { "P_PR_EXEC",          P_PR_EXEC,      P_PR_EXEC       },
 237                 { "P_PR_BPTADJ",        P_PR_BPTADJ,    P_PR_BPTADJ     },
 238                 { "P_PR_RUNLCL",        P_PR_RUNLCL,    P_PR_RUNLCL     },
 239                 { "P_PR_KILLCL",        P_PR_KILLCL,    P_PR_KILLCL     },
 240                 { NULL,                 0,              0               }
 241         };
 242 
 243         if (!(flags & DCMD_ADDRSPEC)) {
 244                 if (mdb_walk_dcmd("proc", "pflags", argc, argv) == -1) {
 245                         mdb_warn("can't walk 'proc'");
 246                         return (DCMD_ERR);
 247                 }
 248                 return (DCMD_OK);
 249         }
 250 
 251         if (mdb_ctf_vread(&pr, "proc_t", "mdb_pflags_proc_t", addr, 0) == -1 ||
 252             mdb_vread(&pid, sizeof (pid), (uintptr_t)pr.p_pidp) == -1) {
 253                 mdb_warn("cannot read proc_t or pid");
 254                 return (DCMD_ERR);
 255         }
 256 
 257         mdb_printf("%p [pid %d]:\n", addr, pid.pid_id);
 258         mdb_printf("\tp_flag:      %08x <%b>\n", pr.p_flag, pr.p_flag,
 259             p_flag_bits);
 260         mdb_printf("\tp_pidflag:   %08x <%b>\n", pr.p_pidflag, pr.p_pidflag,
 261             p_pidflag_bits);
 262         mdb_printf("\tp_proc_flag: %08x <%b>\n", pr.p_proc_flag, pr.p_proc_flag,
 263             p_proc_flag_bits);
 264 
 265         return (DCMD_OK);
 266 }
 267 
 268 typedef struct mdb_ps_proc {
 269         char            p_stat;
 270         struct pid      *p_pidp;
 271         struct pid      *p_pgidp;
 272         struct cred     *p_cred;
 273         struct sess     *p_sessp;
 274         struct task     *p_task;
 275         struct zone     *p_zone;
 276         pid_t           p_ppid;
 277         uint_t          p_flag;
 278         struct {
 279                 char            u_comm[MAXCOMLEN + 1];
 280                 char            u_psargs[PSARGSZ];
 281         } p_user;
 282 } mdb_ps_proc_t;
 283 
 284 int
 285 ps(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 286 {
 287         uint_t prt_flags = 0;
 288         mdb_ps_proc_t pr;
 289         struct pid pid, pgid, sid;
 290         sess_t session;
 291         cred_t cred;
 292         task_t tk;
 293         kproject_t pj;
 294         zone_t zn;
 295 
 296         if (!(flags & DCMD_ADDRSPEC)) {
 297                 if (mdb_walk_dcmd("proc", "ps", argc, argv) == -1) {
 298                         mdb_warn("can't walk 'proc'");
 299                         return (DCMD_ERR);
 300                 }
 301                 return (DCMD_OK);
 302         }
 303 
 304         if (mdb_getopts(argc, argv,
 305             'f', MDB_OPT_SETBITS, PS_PSARGS, &prt_flags,
 306             'l', MDB_OPT_SETBITS, PS_PRTLWPS, &prt_flags,
 307             'T', MDB_OPT_SETBITS, PS_TASKS, &prt_flags,
 308             'P', MDB_OPT_SETBITS, PS_PROJECTS, &prt_flags,
 309             'z', MDB_OPT_SETBITS, PS_ZONES, &prt_flags,
 310             't', MDB_OPT_SETBITS, PS_PRTTHREADS, &prt_flags, NULL) != argc)
 311                 return (DCMD_USAGE);
 312 
 313         if (DCMD_HDRSPEC(flags)) {
 314                 mdb_printf("%<u>%1s %6s %6s %6s %6s ",
 315                     "S", "PID", "PPID", "PGID", "SID");
 316                 if (prt_flags & PS_TASKS)
 317                         mdb_printf("%5s ", "TASK");
 318                 if (prt_flags & PS_PROJECTS)
 319                         mdb_printf("%5s ", "PROJ");
 320                 if (prt_flags & PS_ZONES)
 321                         mdb_printf("%5s ", "ZONE");
 322                 mdb_printf("%6s %10s %?s %s%</u>\n",
 323                     "UID", "FLAGS", "ADDR", "NAME");
 324         }
 325 
 326         if (mdb_ctf_vread(&pr, "proc_t", "mdb_ps_proc_t", addr, 0) == -1)
 327                 return (DCMD_ERR);
 328 
 329         mdb_vread(&pid, sizeof (pid), (uintptr_t)pr.p_pidp);
 330         mdb_vread(&pgid, sizeof (pgid), (uintptr_t)pr.p_pgidp);
 331         mdb_vread(&cred, sizeof (cred), (uintptr_t)pr.p_cred);
 332         mdb_vread(&session, sizeof (session), (uintptr_t)pr.p_sessp);
 333         mdb_vread(&sid, sizeof (sid), (uintptr_t)session.s_sidp);
 334         if (prt_flags & (PS_TASKS | PS_PROJECTS))
 335                 mdb_vread(&tk, sizeof (tk), (uintptr_t)pr.p_task);
 336         if (prt_flags & PS_PROJECTS)
 337                 mdb_vread(&pj, sizeof (pj), (uintptr_t)tk.tk_proj);
 338         if (prt_flags & PS_ZONES)
 339                 mdb_vread(&zn, sizeof (zn), (uintptr_t)pr.p_zone);
 340 
 341         mdb_printf("%c %6d %6d %6d %6d ",
 342             pstat2ch(pr.p_stat), pid.pid_id, pr.p_ppid, pgid.pid_id,
 343             sid.pid_id);
 344         if (prt_flags & PS_TASKS)
 345                 mdb_printf("%5d ", tk.tk_tkid);
 346         if (prt_flags & PS_PROJECTS)
 347                 mdb_printf("%5d ", pj.kpj_id);
 348         if (prt_flags & PS_ZONES)
 349                 mdb_printf("%5d ", zn.zone_id);
 350         mdb_printf("%6d 0x%08x %0?p %s\n",
 351             cred.cr_uid, pr.p_flag, addr,
 352             (prt_flags & PS_PSARGS) ? pr.p_user.u_psargs : pr.p_user.u_comm);
 353 
 354         if (prt_flags & ~PS_PSARGS)
 355                 (void) mdb_pwalk("thread", ps_threadprint, &prt_flags, addr);
 356 
 357         return (DCMD_OK);
 358 }
 359 
 360 static void
 361 ps_help(void)
 362 {
 363         mdb_printf("Display processes.\n\n"
 364             "Options:\n"
 365             "    -f\tDisplay command arguments\n"
 366             "    -l\tDisplay LWPs\n"
 367             "    -T\tDisplay tasks\n"
 368             "    -P\tDisplay projects\n"
 369             "    -z\tDisplay zones\n"
 370             "    -t\tDisplay threads\n\n");
 371 
 372         mdb_printf("The resulting output is a table of the processes on the "
 373             "system.  The\n"
 374             "columns in the output consist of a combination of the "
 375             "following fields:\n\n");
 376         mdb_printf("S\tProcess state.  Possible states are:\n"
 377             "\tS\tSleeping (SSLEEP)\n"
 378             "\tR\tRunnable (SRUN)\n"
 379             "\tZ\tZombie (SZOMB)\n"
 380             "\tI\tIdle (SIDL)\n"
 381             "\tO\tOn Cpu (SONPROC)\n"
 382             "\tT\tStopped (SSTOP)\n"
 383             "\tW\tWaiting (SWAIT)\n");
 384 
 385         mdb_printf("PID\tProcess id.\n");
 386         mdb_printf("PPID\tParent process id.\n");
 387         mdb_printf("PGID\tProcess group id.\n");
 388         mdb_printf("SID\tProcess id of the session leader.\n");
 389         mdb_printf("TASK\tThe task id of the process.\n");
 390         mdb_printf("PROJ\tThe project id of the process.\n");
 391         mdb_printf("ZONE\tThe zone id of the process.\n");
 392         mdb_printf("UID\tThe user id of the process.\n");
 393         mdb_printf("FLAGS\tThe process flags (see ::pflags).\n");
 394         mdb_printf("ADDR\tThe kernel address of the proc_t structure of the "
 395             "process\n");
 396         mdb_printf("NAME\tThe name (p_user.u_comm field) of the process.  If "
 397             "the -f flag\n"
 398             "\tis specified, the arguments of the process are displayed.\n");
 399 }
 400 
 401 #define PG_NEWEST       0x0001
 402 #define PG_OLDEST       0x0002
 403 #define PG_PIPE_OUT     0x0004
 404 #define PG_EXACT_MATCH  0x0008
 405 
 406 typedef struct pgrep_data {
 407         uint_t pg_flags;
 408         uint_t pg_psflags;
 409         uintptr_t pg_xaddr;
 410         hrtime_t pg_xstart;
 411         const char *pg_pat;
 412 #ifndef _KMDB
 413         regex_t pg_reg;
 414 #endif
 415 } pgrep_data_t;
 416 
 417 typedef struct mdb_pgrep_proc {
 418         struct {
 419                 timestruc_t     u_start;
 420                 char            u_comm[MAXCOMLEN + 1];
 421         } p_user;
 422 } mdb_pgrep_proc_t;
 423 
 424 /*ARGSUSED*/
 425 static int
 426 pgrep_cb(uintptr_t addr, const void *ignored, void *data)
 427 {
 428         mdb_pgrep_proc_t p;
 429         pgrep_data_t *pgp = data;
 430 #ifndef _KMDB
 431         regmatch_t pmatch;
 432 #endif
 433 
 434         if (mdb_ctf_vread(&p, "proc_t", "mdb_pgrep_proc_t", addr, 0) == -1)
 435                 return (WALK_ERR);
 436 
 437         /*
 438          * kmdb doesn't have access to the reg* functions, so we fall back
 439          * to strstr/strcmp.
 440          */
 441 #ifdef _KMDB
 442         if ((pgp->pg_flags & PG_EXACT_MATCH) ?
 443             (strcmp(p.p_user.u_comm, pgp->pg_pat) != 0) :
 444             (strstr(p.p_user.u_comm, pgp->pg_pat) == NULL))
 445                 return (WALK_NEXT);
 446 #else
 447         if (regexec(&pgp->pg_reg, p.p_user.u_comm, 1, &pmatch, 0) != 0)
 448                 return (WALK_NEXT);
 449 
 450         if ((pgp->pg_flags & PG_EXACT_MATCH) &&
 451             (pmatch.rm_so != 0 || p.p_user.u_comm[pmatch.rm_eo] != '\0'))
 452                 return (WALK_NEXT);
 453 #endif
 454 
 455         if (pgp->pg_flags & (PG_NEWEST | PG_OLDEST)) {
 456                 hrtime_t start;
 457 
 458                 start = (hrtime_t)p.p_user.u_start.tv_sec * NANOSEC +
 459                     p.p_user.u_start.tv_nsec;
 460 
 461                 if (pgp->pg_flags & PG_NEWEST) {
 462                         if (pgp->pg_xaddr == 0 || start > pgp->pg_xstart) {
 463                                 pgp->pg_xaddr = addr;
 464                                 pgp->pg_xstart = start;
 465                         }
 466                 } else {
 467                         if (pgp->pg_xaddr == 0 || start < pgp->pg_xstart) {
 468                                 pgp->pg_xaddr = addr;
 469                                 pgp->pg_xstart = start;
 470                         }
 471                 }
 472 
 473         } else if (pgp->pg_flags & PG_PIPE_OUT) {
 474                 mdb_printf("%p\n", addr);
 475 
 476         } else {
 477                 if (mdb_call_dcmd("ps", addr, pgp->pg_psflags, 0, NULL) != 0) {
 478                         mdb_warn("can't invoke 'ps'");
 479                         return (WALK_DONE);
 480                 }
 481                 pgp->pg_psflags &= ~DCMD_LOOPFIRST;
 482         }
 483 
 484         return (WALK_NEXT);
 485 }
 486 
 487 /*ARGSUSED*/
 488 int
 489 pgrep(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 490 {
 491         pgrep_data_t pg;
 492         int i;
 493 #ifndef _KMDB
 494         int err;
 495 #endif
 496 
 497         if (flags & DCMD_ADDRSPEC)
 498                 return (DCMD_USAGE);
 499 
 500         pg.pg_flags = 0;
 501         pg.pg_xaddr = 0;
 502 
 503         i = mdb_getopts(argc, argv,
 504             'n', MDB_OPT_SETBITS, PG_NEWEST, &pg.pg_flags,
 505             'o', MDB_OPT_SETBITS, PG_OLDEST, &pg.pg_flags,
 506             'x', MDB_OPT_SETBITS, PG_EXACT_MATCH, &pg.pg_flags,
 507             NULL);
 508 
 509         argc -= i;
 510         argv += i;
 511 
 512         if (argc != 1)
 513                 return (DCMD_USAGE);
 514 
 515         /*
 516          * -n and -o are mutually exclusive.
 517          */
 518         if ((pg.pg_flags & PG_NEWEST) && (pg.pg_flags & PG_OLDEST))
 519                 return (DCMD_USAGE);
 520 
 521         if (argv->a_type != MDB_TYPE_STRING)
 522                 return (DCMD_USAGE);
 523 
 524         if (flags & DCMD_PIPE_OUT)
 525                 pg.pg_flags |= PG_PIPE_OUT;
 526 
 527         pg.pg_pat = argv->a_un.a_str;
 528         if (DCMD_HDRSPEC(flags))
 529                 pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP | DCMD_LOOPFIRST;
 530         else
 531                 pg.pg_psflags = DCMD_ADDRSPEC | DCMD_LOOP;
 532 
 533 #ifndef _KMDB
 534         if ((err = regcomp(&pg.pg_reg, pg.pg_pat, REG_EXTENDED)) != 0) {
 535                 size_t nbytes;
 536                 char *buf;
 537 
 538                 nbytes = regerror(err, &pg.pg_reg, NULL, 0);
 539                 buf = mdb_alloc(nbytes + 1, UM_SLEEP | UM_GC);
 540                 (void) regerror(err, &pg.pg_reg, buf, nbytes);
 541                 mdb_warn("%s\n", buf);
 542 
 543                 return (DCMD_ERR);
 544         }
 545 #endif
 546 
 547         if (mdb_walk("proc", pgrep_cb, &pg) != 0) {
 548                 mdb_warn("can't walk 'proc'");
 549                 return (DCMD_ERR);
 550         }
 551 
 552         if (pg.pg_xaddr != 0 && (pg.pg_flags & (PG_NEWEST | PG_OLDEST))) {
 553                 if (pg.pg_flags & PG_PIPE_OUT) {
 554                         mdb_printf("%p\n", pg.pg_xaddr);
 555                 } else {
 556                         if (mdb_call_dcmd("ps", pg.pg_xaddr, pg.pg_psflags,
 557                             0, NULL) != 0) {
 558                                 mdb_warn("can't invoke 'ps'");
 559                                 return (DCMD_ERR);
 560                         }
 561                 }
 562         }
 563 
 564         return (DCMD_OK);
 565 }
 566 
 567 int
 568 task(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 569 {
 570         task_t tk;
 571         kproject_t pj;
 572 
 573         if (!(flags & DCMD_ADDRSPEC)) {
 574                 if (mdb_walk_dcmd("task_cache", "task", argc, argv) == -1) {
 575                         mdb_warn("can't walk task_cache");
 576                         return (DCMD_ERR);
 577                 }
 578                 return (DCMD_OK);
 579         }
 580         if (DCMD_HDRSPEC(flags)) {
 581                 mdb_printf("%<u>%?s %6s %6s %6s %6s %10s%</u>\n",
 582                     "ADDR", "TASKID", "PROJID", "ZONEID", "REFCNT", "FLAGS");
 583         }
 584         if (mdb_vread(&tk, sizeof (task_t), addr) == -1) {
 585                 mdb_warn("can't read task_t structure at %p", addr);
 586                 return (DCMD_ERR);
 587         }
 588         if (mdb_vread(&pj, sizeof (kproject_t), (uintptr_t)tk.tk_proj) == -1) {
 589                 mdb_warn("can't read project_t structure at %p", addr);
 590                 return (DCMD_ERR);
 591         }
 592         mdb_printf("%0?p %6d %6d %6d %6u 0x%08x\n",
 593             addr, tk.tk_tkid, pj.kpj_id, pj.kpj_zoneid, tk.tk_hold_count,
 594             tk.tk_flags);
 595         return (DCMD_OK);
 596 }
 597 
 598 int
 599 project(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
 600 {
 601         kproject_t pj;
 602 
 603         if (!(flags & DCMD_ADDRSPEC)) {
 604                 if (mdb_walk_dcmd("projects", "project", argc, argv) == -1) {
 605                         mdb_warn("can't walk projects");
 606                         return (DCMD_ERR);
 607                 }
 608                 return (DCMD_OK);
 609         }
 610         if (DCMD_HDRSPEC(flags)) {
 611                 mdb_printf("%<u>%?s %6s %6s %6s%</u>\n",
 612                     "ADDR", "PROJID", "ZONEID", "REFCNT");
 613         }
 614         if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
 615                 mdb_warn("can't read kproject_t structure at %p", addr);
 616                 return (DCMD_ERR);
 617         }
 618         mdb_printf("%0?p %6d %6d %6u\n", addr, pj.kpj_id, pj.kpj_zoneid,
 619             pj.kpj_count);
 620         return (DCMD_OK);
 621 }
 622 
 623 /* walk callouts themselves, either by list or id hash. */
 624 int
 625 callout_walk_init(mdb_walk_state_t *wsp)
 626 {
 627         if (wsp->walk_addr == 0) {
 628                 mdb_warn("callout doesn't support global walk");
 629                 return (WALK_ERR);
 630         }
 631         wsp->walk_data = mdb_alloc(sizeof (callout_t), UM_SLEEP);
 632         return (WALK_NEXT);
 633 }
 634 
 635 #define CALLOUT_WALK_BYLIST     0
 636 #define CALLOUT_WALK_BYID       1
 637 
 638 /* the walker arg switches between walking by list (0) and walking by id (1). */
 639 int
 640 callout_walk_step(mdb_walk_state_t *wsp)
 641 {
 642         int retval;
 643 
 644         if (wsp->walk_addr == 0) {
 645                 return (WALK_DONE);
 646         }
 647         if (mdb_vread(wsp->walk_data, sizeof (callout_t),
 648             wsp->walk_addr) == -1) {
 649                 mdb_warn("failed to read callout at %p", wsp->walk_addr);
 650                 return (WALK_DONE);
 651         }
 652         retval = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
 653             wsp->walk_cbdata);
 654 
 655         if ((ulong_t)wsp->walk_arg == CALLOUT_WALK_BYID) {
 656                 wsp->walk_addr =
 657                     (uintptr_t)(((callout_t *)wsp->walk_data)->c_idnext);
 658         } else {
 659                 wsp->walk_addr =
 660                     (uintptr_t)(((callout_t *)wsp->walk_data)->c_clnext);
 661         }
 662 
 663         return (retval);
 664 }
 665 
 666 void
 667 callout_walk_fini(mdb_walk_state_t *wsp)
 668 {
 669         mdb_free(wsp->walk_data, sizeof (callout_t));
 670 }
 671 
 672 /*
 673  * walker for callout lists. This is different from hashes and callouts.
 674  * Thankfully, it's also simpler.
 675  */
 676 int
 677 callout_list_walk_init(mdb_walk_state_t *wsp)
 678 {
 679         if (wsp->walk_addr == 0) {
 680                 mdb_warn("callout list doesn't support global walk");
 681                 return (WALK_ERR);
 682         }
 683         wsp->walk_data = mdb_alloc(sizeof (callout_list_t), UM_SLEEP);
 684         return (WALK_NEXT);
 685 }
 686 
 687 int
 688 callout_list_walk_step(mdb_walk_state_t *wsp)
 689 {
 690         int retval;
 691 
 692         if (wsp->walk_addr == 0) {
 693                 return (WALK_DONE);
 694         }
 695         if (mdb_vread(wsp->walk_data, sizeof (callout_list_t),
 696             wsp->walk_addr) != sizeof (callout_list_t)) {
 697                 mdb_warn("failed to read callout_list at %p", wsp->walk_addr);
 698                 return (WALK_ERR);
 699         }
 700         retval = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
 701             wsp->walk_cbdata);
 702 
 703         wsp->walk_addr = (uintptr_t)
 704             (((callout_list_t *)wsp->walk_data)->cl_next);
 705 
 706         return (retval);
 707 }
 708 
 709 void
 710 callout_list_walk_fini(mdb_walk_state_t *wsp)
 711 {
 712         mdb_free(wsp->walk_data, sizeof (callout_list_t));
 713 }
 714 
 715 /* routines/structs to walk callout table(s) */
 716 typedef struct cot_data {
 717         callout_table_t *ct0;
 718         callout_table_t ct;
 719         callout_hash_t cot_idhash[CALLOUT_BUCKETS];
 720         callout_hash_t cot_clhash[CALLOUT_BUCKETS];
 721         kstat_named_t ct_kstat_data[CALLOUT_NUM_STATS];
 722         int cotndx;
 723         int cotsize;
 724 } cot_data_t;
 725 
 726 int
 727 callout_table_walk_init(mdb_walk_state_t *wsp)
 728 {
 729         int max_ncpus;
 730         cot_data_t *cot_walk_data;
 731 
 732         cot_walk_data = mdb_alloc(sizeof (cot_data_t), UM_SLEEP);
 733 
 734         if (wsp->walk_addr == 0) {
 735                 if (mdb_readvar(&cot_walk_data->ct0, "callout_table") == -1) {
 736                         mdb_warn("failed to read 'callout_table'");
 737                         return (WALK_ERR);
 738                 }
 739                 if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
 740                         mdb_warn("failed to get callout_table array size");
 741                         return (WALK_ERR);
 742                 }
 743                 cot_walk_data->cotsize = CALLOUT_NTYPES * max_ncpus;
 744                 wsp->walk_addr = (uintptr_t)cot_walk_data->ct0;
 745         } else {
 746                 /* not a global walk */
 747                 cot_walk_data->cotsize = 1;
 748         }
 749 
 750         cot_walk_data->cotndx = 0;
 751         wsp->walk_data = cot_walk_data;
 752 
 753         return (WALK_NEXT);
 754 }
 755 
 756 int
 757 callout_table_walk_step(mdb_walk_state_t *wsp)
 758 {
 759         int retval;
 760         cot_data_t *cotwd = (cot_data_t *)wsp->walk_data;
 761         size_t size;
 762 
 763         if (cotwd->cotndx >= cotwd->cotsize) {
 764                 return (WALK_DONE);
 765         }
 766         if (mdb_vread(&(cotwd->ct), sizeof (callout_table_t),
 767             wsp->walk_addr) != sizeof (callout_table_t)) {
 768                 mdb_warn("failed to read callout_table at %p", wsp->walk_addr);
 769                 return (WALK_ERR);
 770         }
 771 
 772         size = sizeof (callout_hash_t) * CALLOUT_BUCKETS;
 773         if (cotwd->ct.ct_idhash != NULL) {
 774                 if (mdb_vread(cotwd->cot_idhash, size,
 775                     (uintptr_t)(cotwd->ct.ct_idhash)) != size) {
 776                         mdb_warn("failed to read id_hash at %p",
 777                             cotwd->ct.ct_idhash);
 778                         return (WALK_ERR);
 779                 }
 780         }
 781         if (cotwd->ct.ct_clhash != NULL) {
 782                 if (mdb_vread(&(cotwd->cot_clhash), size,
 783                     (uintptr_t)cotwd->ct.ct_clhash) == -1) {
 784                         mdb_warn("failed to read cl_hash at %p",
 785                             cotwd->ct.ct_clhash);
 786                         return (WALK_ERR);
 787                 }
 788         }
 789         size = sizeof (kstat_named_t) * CALLOUT_NUM_STATS;
 790         if (cotwd->ct.ct_kstat_data != NULL) {
 791                 if (mdb_vread(&(cotwd->ct_kstat_data), size,
 792                     (uintptr_t)cotwd->ct.ct_kstat_data) == -1) {
 793                         mdb_warn("failed to read kstats at %p",
 794                             cotwd->ct.ct_kstat_data);
 795                         return (WALK_ERR);
 796                 }
 797         }
 798         retval = wsp->walk_callback(wsp->walk_addr, (void *)cotwd,
 799             wsp->walk_cbdata);
 800 
 801         cotwd->cotndx++;
 802         if (cotwd->cotndx >= cotwd->cotsize) {
 803                 return (WALK_DONE);
 804         }
 805         wsp->walk_addr = (uintptr_t)((char *)wsp->walk_addr +
 806             sizeof (callout_table_t));
 807 
 808         return (retval);
 809 }
 810 
 811 void
 812 callout_table_walk_fini(mdb_walk_state_t *wsp)
 813 {
 814         mdb_free(wsp->walk_data, sizeof (cot_data_t));
 815 }
 816 
 817 static const char *co_typenames[] = { "R", "N" };
 818 
 819 #define CO_PLAIN_ID(xid)        ((xid) & CALLOUT_ID_MASK)
 820 
 821 #define TABLE_TO_SEQID(x)       ((x) >> CALLOUT_TYPE_BITS)
 822 
 823 /* callout flags, in no particular order */
 824 #define COF_REAL        0x00000001
 825 #define COF_NORM        0x00000002
 826 #define COF_LONG        0x00000004
 827 #define COF_SHORT       0x00000008
 828 #define COF_EMPTY       0x00000010
 829 #define COF_TIME        0x00000020
 830 #define COF_BEFORE      0x00000040
 831 #define COF_AFTER       0x00000080
 832 #define COF_SEQID       0x00000100
 833 #define COF_FUNC        0x00000200
 834 #define COF_ADDR        0x00000400
 835 #define COF_EXEC        0x00000800
 836 #define COF_HIRES       0x00001000
 837 #define COF_ABS         0x00002000
 838 #define COF_TABLE       0x00004000
 839 #define COF_BYIDH       0x00008000
 840 #define COF_FREE        0x00010000
 841 #define COF_LIST        0x00020000
 842 #define COF_EXPREL      0x00040000
 843 #define COF_HDR         0x00080000
 844 #define COF_VERBOSE     0x00100000
 845 #define COF_LONGLIST    0x00200000
 846 #define COF_THDR        0x00400000
 847 #define COF_LHDR        0x00800000
 848 #define COF_CHDR        0x01000000
 849 #define COF_PARAM       0x02000000
 850 #define COF_DECODE      0x04000000
 851 #define COF_HEAP        0x08000000
 852 #define COF_QUEUE       0x10000000
 853 
 854 /* show real and normal, short and long, expired and unexpired. */
 855 #define COF_DEFAULT     (COF_REAL | COF_NORM | COF_LONG | COF_SHORT)
 856 
 857 #define COF_LIST_FLAGS  \
 858         (CALLOUT_LIST_FLAG_HRESTIME | CALLOUT_LIST_FLAG_ABSOLUTE)
 859 
 860 /* private callout data for callback functions */
 861 typedef struct callout_data {
 862         uint_t flags;           /* COF_* */
 863         cpu_t *cpu;             /* cpu pointer if given */
 864         int seqid;              /* cpu seqid, or -1 */
 865         hrtime_t time;          /* expiration time value */
 866         hrtime_t atime;         /* expiration before value */
 867         hrtime_t btime;         /* expiration after value */
 868         uintptr_t funcaddr;     /* function address or NULL */
 869         uintptr_t param;        /* parameter to function or NULL */
 870         hrtime_t now;           /* current system time */
 871         int nsec_per_tick;      /* for conversions */
 872         ulong_t ctbits;         /* for decoding xid */
 873         callout_table_t *co_table;      /* top of callout table array */
 874         int ndx;                /* table index. */
 875         int bucket;             /* which list/id bucket are we in */
 876         hrtime_t exp;           /* expire time */
 877         int list_flags;         /* copy of cl_flags */
 878 } callout_data_t;
 879 
 880 /* this callback does the actual callback itself (finally). */
 881 /*ARGSUSED*/
 882 static int
 883 callouts_cb(uintptr_t addr, const void *data, void *priv)
 884 {
 885         callout_data_t *coargs = (callout_data_t *)priv;
 886         callout_t *co = (callout_t *)data;
 887         int tableid, list_flags;
 888         callout_id_t coid;
 889 
 890         if ((coargs == NULL) || (co == NULL)) {
 891                 return (WALK_ERR);
 892         }
 893 
 894         if ((coargs->flags & COF_FREE) && !(co->c_xid & CALLOUT_ID_FREE)) {
 895                 /*
 896                  * The callout must have been reallocated. No point in
 897                  * walking any more.
 898                  */
 899                 return (WALK_DONE);
 900         }
 901         if (!(coargs->flags & COF_FREE) && (co->c_xid & CALLOUT_ID_FREE)) {
 902                 /*
 903                  * The callout must have been freed. No point in
 904                  * walking any more.
 905                  */
 906                 return (WALK_DONE);
 907         }
 908         if ((coargs->flags & COF_FUNC) &&
 909             (coargs->funcaddr != (uintptr_t)co->c_func)) {
 910                 return (WALK_NEXT);
 911         }
 912         if ((coargs->flags & COF_PARAM) &&
 913             (coargs->param != (uintptr_t)co->c_arg)) {
 914                 return (WALK_NEXT);
 915         }
 916         if (!(coargs->flags & COF_LONG) && (co->c_xid & CALLOUT_LONGTERM)) {
 917                 return (WALK_NEXT);
 918         }
 919         if (!(coargs->flags & COF_SHORT) && !(co->c_xid & CALLOUT_LONGTERM)) {
 920                 return (WALK_NEXT);
 921         }
 922         if ((coargs->flags & COF_EXEC) && !(co->c_xid & CALLOUT_EXECUTING)) {
 923                 return (WALK_NEXT);
 924         }
 925         /* it is possible we don't have the exp time or flags */
 926         if (coargs->flags & COF_BYIDH) {
 927                 if (!(coargs->flags & COF_FREE)) {
 928                         /* we have to fetch the expire time ourselves. */
 929                         if (mdb_vread(&coargs->exp, sizeof (hrtime_t),
 930                             (uintptr_t)co->c_list + offsetof(callout_list_t,
 931                             cl_expiration)) == -1) {
 932                                 mdb_warn("failed to read expiration "
 933                                     "time from %p", co->c_list);
 934                                 coargs->exp = 0;
 935                         }
 936                         /* and flags. */
 937                         if (mdb_vread(&coargs->list_flags, sizeof (int),
 938                             (uintptr_t)co->c_list + offsetof(callout_list_t,
 939                             cl_flags)) == -1) {
 940                                 mdb_warn("failed to read list flags"
 941                                     "from %p", co->c_list);
 942                                 coargs->list_flags = 0;
 943                         }
 944                 } else {
 945                         /* free callouts can't use list pointer. */
 946                         coargs->exp = 0;
 947                         coargs->list_flags = 0;
 948                 }
 949                 if (coargs->exp != 0) {
 950                         if ((coargs->flags & COF_TIME) &&
 951                             (coargs->exp != coargs->time)) {
 952                                 return (WALK_NEXT);
 953                         }
 954                         if ((coargs->flags & COF_BEFORE) &&
 955                             (coargs->exp > coargs->btime)) {
 956                                 return (WALK_NEXT);
 957                         }
 958                         if ((coargs->flags & COF_AFTER) &&
 959                             (coargs->exp < coargs->atime)) {
 960                                 return (WALK_NEXT);
 961                         }
 962                 }
 963                 /* tricky part, since both HIRES and ABS can be set */
 964                 list_flags = coargs->list_flags;
 965                 if ((coargs->flags & COF_HIRES) && (coargs->flags & COF_ABS)) {
 966                         /* both flags are set, only skip "regular" ones */
 967                         if (! (list_flags & COF_LIST_FLAGS)) {
 968                                 return (WALK_NEXT);
 969                         }
 970                 } else {
 971                         /* individual flags, or no flags */
 972                         if ((coargs->flags & COF_HIRES) &&
 973                             !(list_flags & CALLOUT_LIST_FLAG_HRESTIME)) {
 974                                 return (WALK_NEXT);
 975                         }
 976                         if ((coargs->flags & COF_ABS) &&
 977                             !(list_flags & CALLOUT_LIST_FLAG_ABSOLUTE)) {
 978                                 return (WALK_NEXT);
 979                         }
 980                 }
 981                 /*
 982                  * We do the checks for COF_HEAP and COF_QUEUE here only if we
 983                  * are traversing BYIDH. If the traversal is by callout list,
 984                  * we do this check in callout_list_cb() to be more
 985                  * efficient.
 986                  */
 987                 if ((coargs->flags & COF_HEAP) &&
 988                     !(list_flags & CALLOUT_LIST_FLAG_HEAPED)) {
 989                         return (WALK_NEXT);
 990                 }
 991 
 992                 if ((coargs->flags & COF_QUEUE) &&
 993                     !(list_flags & CALLOUT_LIST_FLAG_QUEUED)) {
 994                         return (WALK_NEXT);
 995                 }
 996         }
 997 
 998 #define callout_table_mask      ((1 << coargs->ctbits) - 1)
 999         tableid = CALLOUT_ID_TO_TABLE(co->c_xid);
1000 #undef  callout_table_mask
1001         coid = CO_PLAIN_ID(co->c_xid);
1002 
1003         if ((coargs->flags & COF_CHDR) && !(coargs->flags & COF_ADDR)) {
1004                 /*
1005                  * We need to print the headers. If walking by id, then
1006                  * the list header isn't printed, so we must include
1007                  * that info here.
1008                  */
1009                 if (!(coargs->flags & COF_VERBOSE)) {
1010                         mdb_printf("%<u>%3s %-1s %-14s %</u>",
1011                             "SEQ", "T", "EXP");
1012                 } else if (coargs->flags & COF_BYIDH) {
1013                         mdb_printf("%<u>%-14s %</u>", "EXP");
1014                 }
1015                 mdb_printf("%<u>%-4s %-?s %-20s%</u>",
1016                     "XHAL", "XID", "FUNC(ARG)");
1017                 if (coargs->flags & COF_LONGLIST) {
1018                         mdb_printf("%<u> %-?s %-?s %-?s %-?s%</u>",
1019                             "PREVID", "NEXTID", "PREVL", "NEXTL");
1020                         mdb_printf("%<u> %-?s %-4s %-?s%</u>",
1021                             "DONE", "UTOS", "THREAD");
1022                 }
1023                 mdb_printf("\n");
1024                 coargs->flags &= ~COF_CHDR;
1025                 coargs->flags |= (COF_THDR | COF_LHDR);
1026         }
1027 
1028         if (!(coargs->flags & COF_ADDR)) {
1029                 if (!(coargs->flags & COF_VERBOSE)) {
1030                         mdb_printf("%-3d %1s %-14llx ",
1031                             TABLE_TO_SEQID(tableid),
1032                             co_typenames[tableid & CALLOUT_TYPE_MASK],
1033                             (coargs->flags & COF_EXPREL) ?
1034                             coargs->exp - coargs->now : coargs->exp);
1035                 } else if (coargs->flags & COF_BYIDH) {
1036                         mdb_printf("%-14x ",
1037                             (coargs->flags & COF_EXPREL) ?
1038                             coargs->exp - coargs->now : coargs->exp);
1039                 }
1040                 list_flags = coargs->list_flags;
1041                 mdb_printf("%1s%1s%1s%1s %-?llx %a(%p)",
1042                     (co->c_xid & CALLOUT_EXECUTING) ? "X" : " ",
1043                     (list_flags & CALLOUT_LIST_FLAG_HRESTIME) ? "H" : " ",
1044                     (list_flags & CALLOUT_LIST_FLAG_ABSOLUTE) ? "A" : " ",
1045                     (co->c_xid & CALLOUT_LONGTERM) ? "L" : " ",
1046                     (long long)coid, co->c_func, co->c_arg);
1047                 if (coargs->flags & COF_LONGLIST) {
1048                         mdb_printf(" %-?p %-?p %-?p %-?p",
1049                             co->c_idprev, co->c_idnext, co->c_clprev,
1050                             co->c_clnext);
1051                         mdb_printf(" %-?p %-4d %-0?p",
1052                             co->c_done, co->c_waiting, co->c_executor);
1053                 }
1054         } else {
1055                 /* address only */
1056                 mdb_printf("%-0p", addr);
1057         }
1058         mdb_printf("\n");
1059         return (WALK_NEXT);
1060 }
1061 
1062 /* this callback is for callout list handling. idhash is done by callout_t_cb */
1063 /*ARGSUSED*/
1064 static int
1065 callout_list_cb(uintptr_t addr, const void *data, void *priv)
1066 {
1067         callout_data_t *coargs = (callout_data_t *)priv;
1068         callout_list_t *cl = (callout_list_t *)data;
1069         callout_t *coptr;
1070         int list_flags;
1071 
1072         if ((coargs == NULL) || (cl == NULL)) {
1073                 return (WALK_ERR);
1074         }
1075 
1076         coargs->exp = cl->cl_expiration;
1077         coargs->list_flags = cl->cl_flags;
1078         if ((coargs->flags & COF_FREE) &&
1079             !(cl->cl_flags & CALLOUT_LIST_FLAG_FREE)) {
1080                 /*
1081                  * The callout list must have been reallocated. No point in
1082                  * walking any more.
1083                  */
1084                 return (WALK_DONE);
1085         }
1086         if (!(coargs->flags & COF_FREE) &&
1087             (cl->cl_flags & CALLOUT_LIST_FLAG_FREE)) {
1088                 /*
1089                  * The callout list must have been freed. No point in
1090                  * walking any more.
1091                  */
1092                 return (WALK_DONE);
1093         }
1094         if ((coargs->flags & COF_TIME) &&
1095             (cl->cl_expiration != coargs->time)) {
1096                 return (WALK_NEXT);
1097         }
1098         if ((coargs->flags & COF_BEFORE) &&
1099             (cl->cl_expiration > coargs->btime)) {
1100                 return (WALK_NEXT);
1101         }
1102         if ((coargs->flags & COF_AFTER) &&
1103             (cl->cl_expiration < coargs->atime)) {
1104                 return (WALK_NEXT);
1105         }
1106         if (!(coargs->flags & COF_EMPTY) &&
1107             (cl->cl_callouts.ch_head == NULL)) {
1108                 return (WALK_NEXT);
1109         }
1110         /* FOUR cases, each different, !A!B, !AB, A!B, AB */
1111         if ((coargs->flags & COF_HIRES) && (coargs->flags & COF_ABS)) {
1112                 /* both flags are set, only skip "regular" ones */
1113                 if (! (cl->cl_flags & COF_LIST_FLAGS)) {
1114                         return (WALK_NEXT);
1115                 }
1116         } else {
1117                 if ((coargs->flags & COF_HIRES) &&
1118                     !(cl->cl_flags & CALLOUT_LIST_FLAG_HRESTIME)) {
1119                         return (WALK_NEXT);
1120                 }
1121                 if ((coargs->flags & COF_ABS) &&
1122                     !(cl->cl_flags & CALLOUT_LIST_FLAG_ABSOLUTE)) {
1123                         return (WALK_NEXT);
1124                 }
1125         }
1126 
1127         if ((coargs->flags & COF_HEAP) &&
1128             !(coargs->list_flags & CALLOUT_LIST_FLAG_HEAPED)) {
1129                 return (WALK_NEXT);
1130         }
1131 
1132         if ((coargs->flags & COF_QUEUE) &&
1133             !(coargs->list_flags & CALLOUT_LIST_FLAG_QUEUED)) {
1134                 return (WALK_NEXT);
1135         }
1136 
1137         if ((coargs->flags & COF_LHDR) && !(coargs->flags & COF_ADDR) &&
1138             (coargs->flags & (COF_LIST | COF_VERBOSE))) {
1139                 if (!(coargs->flags & COF_VERBOSE)) {
1140                         /* don't be redundant again */
1141                         mdb_printf("%<u>SEQ T %</u>");
1142                 }
1143                 mdb_printf("%<u>EXP            HA BUCKET "
1144                     "CALLOUTS         %</u>");
1145 
1146                 if (coargs->flags & COF_LONGLIST) {
1147                         mdb_printf("%<u> %-?s %-?s%</u>",
1148                             "PREV", "NEXT");
1149                 }
1150                 mdb_printf("\n");
1151                 coargs->flags &= ~COF_LHDR;
1152                 coargs->flags |= (COF_THDR | COF_CHDR);
1153         }
1154         if (coargs->flags & (COF_LIST | COF_VERBOSE)) {
1155                 if (!(coargs->flags & COF_ADDR)) {
1156                         if (!(coargs->flags & COF_VERBOSE)) {
1157                                 mdb_printf("%3d %1s ",
1158                                     TABLE_TO_SEQID(coargs->ndx),
1159                                     co_typenames[coargs->ndx &
1160                                     CALLOUT_TYPE_MASK]);
1161                         }
1162 
1163                         list_flags = coargs->list_flags;
1164                         mdb_printf("%-14llx %1s%1s %-6d %-0?p ",
1165                             (coargs->flags & COF_EXPREL) ?
1166                             coargs->exp - coargs->now : coargs->exp,
1167                             (list_flags & CALLOUT_LIST_FLAG_HRESTIME) ?
1168                             "H" : " ",
1169                             (list_flags & CALLOUT_LIST_FLAG_ABSOLUTE) ?
1170                             "A" : " ",
1171                             coargs->bucket, cl->cl_callouts.ch_head);
1172 
1173                         if (coargs->flags & COF_LONGLIST) {
1174                                 mdb_printf(" %-?p %-?p",
1175                                     cl->cl_prev, cl->cl_next);
1176                         }
1177                 } else {
1178                         /* address only */
1179                         mdb_printf("%-0p", addr);
1180                 }
1181                 mdb_printf("\n");
1182                 if (coargs->flags & COF_LIST) {
1183                         return (WALK_NEXT);
1184                 }
1185         }
1186         /* yet another layer as we walk the actual callouts via list. */
1187         if (cl->cl_callouts.ch_head == NULL) {
1188                 return (WALK_NEXT);
1189         }
1190         /* free list structures do not have valid callouts off of them. */
1191         if (coargs->flags & COF_FREE) {
1192                 return (WALK_NEXT);
1193         }
1194         coptr = (callout_t *)cl->cl_callouts.ch_head;
1195 
1196         if (coargs->flags & COF_VERBOSE) {
1197                 mdb_inc_indent(4);
1198         }
1199         /*
1200          * walk callouts using yet another callback routine.
1201          * we use callouts_bytime because id hash is handled via
1202          * the callout_t_cb callback.
1203          */
1204         if (mdb_pwalk("callouts_bytime", callouts_cb, coargs,
1205             (uintptr_t)coptr) == -1) {
1206                 mdb_warn("cannot walk callouts at %p", coptr);
1207                 return (WALK_ERR);
1208         }
1209         if (coargs->flags & COF_VERBOSE) {
1210                 mdb_dec_indent(4);
1211         }
1212 
1213         return (WALK_NEXT);
1214 }
1215 
1216 /* this callback handles the details of callout table walking. */
1217 static int
1218 callout_t_cb(uintptr_t addr, const void *data, void *priv)
1219 {
1220         callout_data_t *coargs = (callout_data_t *)priv;
1221         cot_data_t *cotwd = (cot_data_t *)data;
1222         callout_table_t *ct = &(cotwd->ct);
1223         int index, seqid, cotype;
1224         int i;
1225         callout_list_t *clptr;
1226         callout_t *coptr;
1227 
1228         if ((coargs == NULL) || (ct == NULL) || (coargs->co_table == NULL)) {
1229                 return (WALK_ERR);
1230         }
1231 
1232         index =  ((char *)addr - (char *)coargs->co_table) /
1233             sizeof (callout_table_t);
1234         cotype = index & CALLOUT_TYPE_MASK;
1235         seqid = TABLE_TO_SEQID(index);
1236 
1237         if ((coargs->flags & COF_SEQID) && (coargs->seqid != seqid)) {
1238                 return (WALK_NEXT);
1239         }
1240 
1241         if (!(coargs->flags & COF_REAL) && (cotype == CALLOUT_REALTIME)) {
1242                 return (WALK_NEXT);
1243         }
1244 
1245         if (!(coargs->flags & COF_NORM) && (cotype == CALLOUT_NORMAL)) {
1246                 return (WALK_NEXT);
1247         }
1248 
1249         if (!(coargs->flags & COF_EMPTY) && (
1250             (ct->ct_heap == NULL) || (ct->ct_cyclic == 0))) {
1251                 return (WALK_NEXT);
1252         }
1253 
1254         if ((coargs->flags & COF_THDR) && !(coargs->flags & COF_ADDR) &&
1255             (coargs->flags & (COF_TABLE | COF_VERBOSE))) {
1256                 /* print table hdr */
1257                 mdb_printf("%<u>%-3s %-1s %-?s %-?s %-?s %-?s%</u>",
1258                     "SEQ", "T", "FREE", "LFREE", "CYCLIC", "HEAP");
1259                 coargs->flags &= ~COF_THDR;
1260                 coargs->flags |= (COF_LHDR | COF_CHDR);
1261                 if (coargs->flags & COF_LONGLIST) {
1262                         /* more info! */
1263                         mdb_printf("%<u> %-T%-7s %-7s %-?s %-?s %-?s"
1264                             " %-?s %-?s %-?s%</u>",
1265                             "HEAPNUM", "HEAPMAX", "TASKQ", "EXPQ", "QUE",
1266                             "PEND", "FREE", "LOCK");
1267                 }
1268                 mdb_printf("\n");
1269         }
1270         if (coargs->flags & (COF_TABLE | COF_VERBOSE)) {
1271                 if (!(coargs->flags & COF_ADDR)) {
1272                         mdb_printf("%-3d %-1s %-0?p %-0?p %-0?p %-?p",
1273                             seqid, co_typenames[cotype],
1274                             ct->ct_free, ct->ct_lfree, ct->ct_cyclic,
1275                             ct->ct_heap);
1276                         if (coargs->flags & COF_LONGLIST)  {
1277                                 /* more info! */
1278                                 mdb_printf(" %-7d %-7d %-?p %-?p %-?p"
1279                                     " %-?lld %-?lld %-?p",
1280                                     ct->ct_heap_num,  ct->ct_heap_max,
1281                                     ct->ct_taskq, ct->ct_expired.ch_head,
1282                                     ct->ct_queue.ch_head,
1283                                     cotwd->ct_timeouts_pending,
1284                                     cotwd->ct_allocations -
1285                                     cotwd->ct_timeouts_pending,
1286                                     ct->ct_mutex);
1287                         }
1288                 } else {
1289                         /* address only */
1290                         mdb_printf("%-0?p", addr);
1291                 }
1292                 mdb_printf("\n");
1293                 if (coargs->flags & COF_TABLE) {
1294                         return (WALK_NEXT);
1295                 }
1296         }
1297 
1298         coargs->ndx = index;
1299         if (coargs->flags & COF_VERBOSE) {
1300                 mdb_inc_indent(4);
1301         }
1302         /* keep digging. */
1303         if (!(coargs->flags & COF_BYIDH)) {
1304                 /* walk the list hash table */
1305                 if (coargs->flags & COF_FREE) {
1306                         clptr = ct->ct_lfree;
1307                         coargs->bucket = 0;
1308                         if (clptr == NULL) {
1309                                 return (WALK_NEXT);
1310                         }
1311                         if (mdb_pwalk("callout_list", callout_list_cb, coargs,
1312                             (uintptr_t)clptr) == -1) {
1313                                 mdb_warn("cannot walk callout free list at %p",
1314                                     clptr);
1315                                 return (WALK_ERR);
1316                         }
1317                 } else {
1318                         /* first print the expired list. */
1319                         clptr = (callout_list_t *)ct->ct_expired.ch_head;
1320                         if (clptr != NULL) {
1321                                 coargs->bucket = -1;
1322                                 if (mdb_pwalk("callout_list", callout_list_cb,
1323                                     coargs, (uintptr_t)clptr) == -1) {
1324                                         mdb_warn("cannot walk callout_list"
1325                                             " at %p", clptr);
1326                                         return (WALK_ERR);
1327                                 }
1328                         }
1329                         /* then, print the callout queue */
1330                         clptr = (callout_list_t *)ct->ct_queue.ch_head;
1331                         if (clptr != NULL) {
1332                                 coargs->bucket = -1;
1333                                 if (mdb_pwalk("callout_list", callout_list_cb,
1334                                     coargs, (uintptr_t)clptr) == -1) {
1335                                         mdb_warn("cannot walk callout_list"
1336                                             " at %p", clptr);
1337                                         return (WALK_ERR);
1338                                 }
1339                         }
1340                         for (i = 0; i < CALLOUT_BUCKETS; i++) {
1341                                 if (ct->ct_clhash == NULL) {
1342                                         /* nothing to do */
1343                                         break;
1344                                 }
1345                                 if (cotwd->cot_clhash[i].ch_head == NULL) {
1346                                         continue;
1347                                 }
1348                                 clptr = (callout_list_t *)
1349                                     cotwd->cot_clhash[i].ch_head;
1350                                 coargs->bucket = i;
1351                                 /* walk list with callback routine. */
1352                                 if (mdb_pwalk("callout_list", callout_list_cb,
1353                                     coargs, (uintptr_t)clptr) == -1) {
1354                                         mdb_warn("cannot walk callout_list"
1355                                             " at %p", clptr);
1356                                         return (WALK_ERR);
1357                                 }
1358                         }
1359                 }
1360         } else {
1361                 /* walk the id hash table. */
1362                 if (coargs->flags & COF_FREE) {
1363                         coptr = ct->ct_free;
1364                         coargs->bucket = 0;
1365                         if (coptr == NULL) {
1366                                 return (WALK_NEXT);
1367                         }
1368                         if (mdb_pwalk("callouts_byid", callouts_cb, coargs,
1369                             (uintptr_t)coptr) == -1) {
1370                                 mdb_warn("cannot walk callout id free list"
1371                                     " at %p", coptr);
1372                                 return (WALK_ERR);
1373                         }
1374                 } else {
1375                         for (i = 0; i < CALLOUT_BUCKETS; i++) {
1376                                 if (ct->ct_idhash == NULL) {
1377                                         break;
1378                                 }
1379                                 coptr = (callout_t *)
1380                                     cotwd->cot_idhash[i].ch_head;
1381                                 if (coptr == NULL) {
1382                                         continue;
1383                                 }
1384                                 coargs->bucket = i;
1385 
1386                                 /*
1387                                  * walk callouts directly by id. For id
1388                                  * chain, the callout list is just a header,
1389                                  * so there's no need to walk it.
1390                                  */
1391                                 if (mdb_pwalk("callouts_byid", callouts_cb,
1392                                     coargs, (uintptr_t)coptr) == -1) {
1393                                         mdb_warn("cannot walk callouts at %p",
1394                                             coptr);
1395                                         return (WALK_ERR);
1396                                 }
1397                         }
1398                 }
1399         }
1400         if (coargs->flags & COF_VERBOSE) {
1401                 mdb_dec_indent(4);
1402         }
1403         return (WALK_NEXT);
1404 }
1405 
1406 /*
1407  * initialize some common info for both callout dcmds.
1408  */
1409 int
1410 callout_common_init(callout_data_t *coargs)
1411 {
1412         /* we need a couple of things */
1413         if (mdb_readvar(&(coargs->co_table), "callout_table") == -1) {
1414                 mdb_warn("failed to read 'callout_table'");
1415                 return (DCMD_ERR);
1416         }
1417         /* need to get now in nsecs. Approximate with hrtime vars */
1418         if (mdb_readsym(&(coargs->now), sizeof (hrtime_t), "hrtime_last") !=
1419             sizeof (hrtime_t)) {
1420                 if (mdb_readsym(&(coargs->now), sizeof (hrtime_t),
1421                     "hrtime_base") != sizeof (hrtime_t)) {
1422                         mdb_warn("Could not determine current system time");
1423                         return (DCMD_ERR);
1424                 }
1425         }
1426 
1427         if (mdb_readvar(&(coargs->ctbits), "callout_table_bits") == -1) {
1428                 mdb_warn("failed to read 'callout_table_bits'");
1429                 return (DCMD_ERR);
1430         }
1431         if (mdb_readvar(&(coargs->nsec_per_tick), "nsec_per_tick") == -1) {
1432                 mdb_warn("failed to read 'nsec_per_tick'");
1433                 return (DCMD_ERR);
1434         }
1435         return (DCMD_OK);
1436 }
1437 
1438 /*
1439  * dcmd to print callouts.  Optional addr limits to specific table.
1440  * Parses lots of options that get passed to callbacks for walkers.
1441  * Has it's own help function.
1442  */
1443 /*ARGSUSED*/
1444 int
1445 callout(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1446 {
1447         callout_data_t coargs;
1448         /* getopts doesn't help much with stuff like this */
1449         boolean_t Sflag, Cflag, tflag, aflag, bflag, dflag, kflag;
1450         char *funcname = NULL;
1451         char *paramstr = NULL;
1452         uintptr_t Stmp, Ctmp;   /* for getopt. */
1453         int retval;
1454 
1455         coargs.flags = COF_DEFAULT;
1456         Sflag = Cflag = tflag = bflag = aflag = dflag = kflag = FALSE;
1457         coargs.seqid = -1;
1458 
1459         if (mdb_getopts(argc, argv,
1460             'r', MDB_OPT_CLRBITS, COF_NORM, &coargs.flags,
1461             'n', MDB_OPT_CLRBITS, COF_REAL, &coargs.flags,
1462             'l', MDB_OPT_CLRBITS, COF_SHORT, &coargs.flags,
1463             's', MDB_OPT_CLRBITS, COF_LONG, &coargs.flags,
1464             'x', MDB_OPT_SETBITS, COF_EXEC, &coargs.flags,
1465             'h', MDB_OPT_SETBITS, COF_HIRES, &coargs.flags,
1466             'B', MDB_OPT_SETBITS, COF_ABS, &coargs.flags,
1467             'E', MDB_OPT_SETBITS, COF_EMPTY, &coargs.flags,
1468             'd', MDB_OPT_SETBITS, 1, &dflag,
1469             'C', MDB_OPT_UINTPTR_SET, &Cflag, &Ctmp,
1470             'S', MDB_OPT_UINTPTR_SET, &Sflag, &Stmp,
1471             't', MDB_OPT_UINTPTR_SET, &tflag, (uintptr_t *)&coargs.time,
1472             'a', MDB_OPT_UINTPTR_SET, &aflag, (uintptr_t *)&coargs.atime,
1473             'b', MDB_OPT_UINTPTR_SET, &bflag, (uintptr_t *)&coargs.btime,
1474             'k', MDB_OPT_SETBITS, 1, &kflag,
1475             'f', MDB_OPT_STR, &funcname,
1476             'p', MDB_OPT_STR, &paramstr,
1477             'T', MDB_OPT_SETBITS, COF_TABLE, &coargs.flags,
1478             'D', MDB_OPT_SETBITS, COF_EXPREL, &coargs.flags,
1479             'L', MDB_OPT_SETBITS, COF_LIST, &coargs.flags,
1480             'V', MDB_OPT_SETBITS, COF_VERBOSE, &coargs.flags,
1481             'v', MDB_OPT_SETBITS, COF_LONGLIST, &coargs.flags,
1482             'i', MDB_OPT_SETBITS, COF_BYIDH, &coargs.flags,
1483             'F', MDB_OPT_SETBITS, COF_FREE, &coargs.flags,
1484             'H', MDB_OPT_SETBITS, COF_HEAP, &coargs.flags,
1485             'Q', MDB_OPT_SETBITS, COF_QUEUE, &coargs.flags,
1486             'A', MDB_OPT_SETBITS, COF_ADDR, &coargs.flags,
1487             NULL) != argc) {
1488                 return (DCMD_USAGE);
1489         }
1490 
1491         /* initialize from kernel variables */
1492         if ((retval = callout_common_init(&coargs)) != DCMD_OK) {
1493                 return (retval);
1494         }
1495 
1496         /* do some option post-processing */
1497         if (kflag) {
1498                 coargs.time *= coargs.nsec_per_tick;
1499                 coargs.atime *= coargs.nsec_per_tick;
1500                 coargs.btime *= coargs.nsec_per_tick;
1501         }
1502 
1503         if (dflag) {
1504                 coargs.time += coargs.now;
1505                 coargs.atime += coargs.now;
1506                 coargs.btime += coargs.now;
1507         }
1508         if (Sflag) {
1509                 if (flags & DCMD_ADDRSPEC) {
1510                         mdb_printf("-S option conflicts with explicit"
1511                             " address\n");
1512                         return (DCMD_USAGE);
1513                 }
1514                 coargs.flags |= COF_SEQID;
1515                 coargs.seqid = (int)Stmp;
1516         }
1517         if (Cflag) {
1518                 if (flags & DCMD_ADDRSPEC) {
1519                         mdb_printf("-C option conflicts with explicit"
1520                             " address\n");
1521                         return (DCMD_USAGE);
1522                 }
1523                 if (coargs.flags & COF_SEQID) {
1524                         mdb_printf("-C and -S are mutually exclusive\n");
1525                         return (DCMD_USAGE);
1526                 }
1527                 coargs.cpu = (cpu_t *)Ctmp;
1528                 if (mdb_vread(&coargs.seqid, sizeof (processorid_t),
1529                     (uintptr_t)&(coargs.cpu->cpu_seqid)) == -1) {
1530                         mdb_warn("failed to read cpu_t at %p", Ctmp);
1531                         return (DCMD_ERR);
1532                 }
1533                 coargs.flags |= COF_SEQID;
1534         }
1535         /* avoid null outputs. */
1536         if (!(coargs.flags & (COF_REAL | COF_NORM))) {
1537                 coargs.flags |= COF_REAL | COF_NORM;
1538         }
1539         if (!(coargs.flags & (COF_LONG | COF_SHORT))) {
1540                 coargs.flags |= COF_LONG | COF_SHORT;
1541         }
1542         if (tflag) {
1543                 if (aflag || bflag) {
1544                         mdb_printf("-t and -a|b are mutually exclusive\n");
1545                         return (DCMD_USAGE);
1546                 }
1547                 coargs.flags |= COF_TIME;
1548         }
1549         if (aflag) {
1550                 coargs.flags |= COF_AFTER;
1551         }
1552         if (bflag) {
1553                 coargs.flags |= COF_BEFORE;
1554         }
1555         if ((aflag && bflag) && (coargs.btime <= coargs.atime)) {
1556                 mdb_printf("value for -a must be earlier than the value"
1557                     " for -b.\n");
1558                 return (DCMD_USAGE);
1559         }
1560 
1561         if ((coargs.flags & COF_HEAP) && (coargs.flags & COF_QUEUE)) {
1562                 mdb_printf("-H and -Q are mutually exclusive\n");
1563                 return (DCMD_USAGE);
1564         }
1565 
1566         if (funcname != NULL) {
1567                 GElf_Sym sym;
1568 
1569                 if (mdb_lookup_by_name(funcname, &sym) != 0) {
1570                         coargs.funcaddr = mdb_strtoull(funcname);
1571                 } else {
1572                         coargs.funcaddr = sym.st_value;
1573                 }
1574                 coargs.flags |= COF_FUNC;
1575         }
1576 
1577         if (paramstr != NULL) {
1578                 GElf_Sym sym;
1579 
1580                 if (mdb_lookup_by_name(paramstr, &sym) != 0) {
1581                         coargs.param = mdb_strtoull(paramstr);
1582                 } else {
1583                         coargs.param = sym.st_value;
1584                 }
1585                 coargs.flags |= COF_PARAM;
1586         }
1587 
1588         if (!(flags & DCMD_ADDRSPEC)) {
1589                 /* don't pass "dot" if no addr. */
1590                 addr = 0;
1591         }
1592         if (addr != 0) {
1593                 /*
1594                  * a callout table was specified. Ignore -r|n option
1595                  * to avoid null output.
1596                  */
1597                 coargs.flags |= (COF_REAL | COF_NORM);
1598         }
1599 
1600         if (DCMD_HDRSPEC(flags) || (coargs.flags & COF_VERBOSE)) {
1601                 coargs.flags |= COF_THDR | COF_LHDR | COF_CHDR;
1602         }
1603         if (coargs.flags & COF_FREE) {
1604                 coargs.flags |= COF_EMPTY;
1605                 /* -F = free callouts, -FL = free lists */
1606                 if (!(coargs.flags & COF_LIST)) {
1607                         coargs.flags |= COF_BYIDH;
1608                 }
1609         }
1610 
1611         /* walk table, using specialized callback routine. */
1612         if (mdb_pwalk("callout_table", callout_t_cb, &coargs, addr) == -1) {
1613                 mdb_warn("cannot walk callout_table");
1614                 return (DCMD_ERR);
1615         }
1616         return (DCMD_OK);
1617 }
1618 
1619 
1620 /*
1621  * Given an extended callout id, dump its information.
1622  */
1623 /*ARGSUSED*/
1624 int
1625 calloutid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1626 {
1627         callout_data_t coargs;
1628         callout_table_t *ctptr;
1629         callout_table_t ct;
1630         callout_id_t coid;
1631         callout_t *coptr;
1632         int tableid;
1633         callout_id_t xid;
1634         ulong_t idhash;
1635         int i, retval;
1636         const mdb_arg_t *arg;
1637         size_t size;
1638         callout_hash_t cot_idhash[CALLOUT_BUCKETS];
1639 
1640         coargs.flags = COF_DEFAULT | COF_BYIDH;
1641         i = mdb_getopts(argc, argv,
1642             'd', MDB_OPT_SETBITS, COF_DECODE, &coargs.flags,
1643             'v', MDB_OPT_SETBITS, COF_LONGLIST, &coargs.flags,
1644             NULL);
1645         argc -= i;
1646         argv += i;
1647 
1648         if (argc != 1) {
1649                 return (DCMD_USAGE);
1650         }
1651         arg = &argv[0];
1652 
1653         if (arg->a_type == MDB_TYPE_IMMEDIATE) {
1654                 xid = arg->a_un.a_val;
1655         } else {
1656                 xid = (callout_id_t)mdb_strtoull(arg->a_un.a_str);
1657         }
1658 
1659         if (DCMD_HDRSPEC(flags)) {
1660                 coargs.flags |= COF_CHDR;
1661         }
1662 
1663 
1664         /* initialize from kernel variables */
1665         if ((retval = callout_common_init(&coargs)) != DCMD_OK) {
1666                 return (retval);
1667         }
1668 
1669         /* we must massage the environment so that the macros will play nice */
1670 #define callout_table_mask      ((1 << coargs.ctbits) - 1)
1671 #define callout_table_bits      coargs.ctbits
1672 #define nsec_per_tick           coargs.nsec_per_tick
1673         tableid = CALLOUT_ID_TO_TABLE(xid);
1674         idhash = CALLOUT_IDHASH(xid);
1675 #undef  callouts_table_bits
1676 #undef  callout_table_mask
1677 #undef  nsec_per_tick
1678         coid = CO_PLAIN_ID(xid);
1679 
1680         if (flags & DCMD_ADDRSPEC) {
1681                 mdb_printf("calloutid does not accept explicit address.\n");
1682                 return (DCMD_USAGE);
1683         }
1684 
1685         if (coargs.flags & COF_DECODE) {
1686                 if (DCMD_HDRSPEC(flags)) {
1687                         mdb_printf("%<u>%3s %1s %2s %-?s %-6s %</u>\n",
1688                             "SEQ", "T", "XL", "XID", "IDHASH");
1689                 }
1690                 mdb_printf("%-3d %1s %1s%1s %-?llx %-6d\n",
1691                     TABLE_TO_SEQID(tableid),
1692                     co_typenames[tableid & CALLOUT_TYPE_MASK],
1693                     (xid & CALLOUT_EXECUTING) ? "X" : " ",
1694                     (xid & CALLOUT_LONGTERM) ? "L" : " ",
1695                     (long long)coid, idhash);
1696                 return (DCMD_OK);
1697         }
1698 
1699         /* get our table. Note this relies on the types being correct */
1700         ctptr = coargs.co_table + tableid;
1701         if (mdb_vread(&ct, sizeof (callout_table_t), (uintptr_t)ctptr) == -1) {
1702                 mdb_warn("failed to read callout_table at %p", ctptr);
1703                 return (DCMD_ERR);
1704         }
1705         size = sizeof (callout_hash_t) * CALLOUT_BUCKETS;
1706         if (ct.ct_idhash != NULL) {
1707                 if (mdb_vread(&(cot_idhash), size,
1708                     (uintptr_t)ct.ct_idhash) == -1) {
1709                         mdb_warn("failed to read id_hash at %p",
1710                             ct.ct_idhash);
1711                         return (WALK_ERR);
1712                 }
1713         }
1714 
1715         /* callout at beginning of hash chain */
1716         if (ct.ct_idhash == NULL) {
1717                 mdb_printf("id hash chain for this xid is empty\n");
1718                 return (DCMD_ERR);
1719         }
1720         coptr = (callout_t *)cot_idhash[idhash].ch_head;
1721         if (coptr == NULL) {
1722                 mdb_printf("id hash chain for this xid is empty\n");
1723                 return (DCMD_ERR);
1724         }
1725 
1726         coargs.ndx = tableid;
1727         coargs.bucket = idhash;
1728 
1729         /* use the walker, luke */
1730         if (mdb_pwalk("callouts_byid", callouts_cb, &coargs,
1731             (uintptr_t)coptr) == -1) {
1732                 mdb_warn("cannot walk callouts at %p", coptr);
1733                 return (WALK_ERR);
1734         }
1735 
1736         return (DCMD_OK);
1737 }
1738 
1739 void
1740 callout_help(void)
1741 {
1742         mdb_printf("callout: display callouts.\n"
1743             "Given a callout table address, display callouts from table.\n"
1744             "Without an address, display callouts from all tables.\n"
1745             "options:\n"
1746             " -r|n : limit display to (r)ealtime or (n)ormal type callouts\n"
1747             " -s|l : limit display to (s)hort-term ids or (l)ong-term ids\n"
1748             " -x : limit display to callouts which are executing\n"
1749             " -h : limit display to callouts based on hrestime\n"
1750             " -B : limit display to callouts based on absolute time\n"
1751             " -t|a|b nsec: limit display to callouts that expire a(t) time,"
1752             " (a)fter time,\n     or (b)efore time. Use -a and -b together "
1753             " to specify a range.\n     For \"now\", use -d[t|a|b] 0.\n"
1754             " -d : interpret time option to -t|a|b as delta from current time\n"
1755             " -k : use ticks instead of nanoseconds as arguments to"
1756             " -t|a|b. Note that\n     ticks are less accurate and may not"
1757             " match other tick times (ie: lbolt).\n"
1758             " -D : display exiration time as delta from current time\n"
1759             " -S seqid : limit display to callouts for this cpu sequence id\n"
1760             " -C addr :  limit display to callouts for this cpu pointer\n"
1761             " -f name|addr : limit display to callouts with this function\n"
1762             " -p name|addr : limit display to callouts functions with this"
1763             " parameter\n"
1764             " -T : display the callout table itself, instead of callouts\n"
1765             " -L : display callout lists instead of callouts\n"
1766             " -E : with -T or L, display empty data structures.\n"
1767             " -i : traverse callouts by id hash instead of list hash\n"
1768             " -F : walk free callout list (free list with -i) instead\n"
1769             " -v : display more info for each item\n"
1770             " -V : show details of each level of info as it is traversed\n"
1771             " -H : limit display to callouts in the callout heap\n"
1772             " -Q : limit display to callouts in the callout queue\n"
1773             " -A : show only addresses. Useful for pipelines.\n");
1774 }
1775 
1776 void
1777 calloutid_help(void)
1778 {
1779         mdb_printf("calloutid: display callout by id.\n"
1780             "Given an extended callout id, display the callout infomation.\n"
1781             "options:\n"
1782             " -d : do not dereference callout, just decode the id.\n"
1783             " -v : verbose display more info about the callout\n");
1784 }
1785 
1786 /*ARGSUSED*/
1787 int
1788 class(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1789 {
1790         long num_classes, i;
1791         sclass_t *class_tbl;
1792         GElf_Sym g_sclass;
1793         char class_name[PC_CLNMSZ];
1794         size_t tbl_size;
1795 
1796         if (mdb_lookup_by_name("sclass", &g_sclass) == -1) {
1797                 mdb_warn("failed to find symbol sclass\n");
1798                 return (DCMD_ERR);
1799         }
1800 
1801         tbl_size = (size_t)g_sclass.st_size;
1802         num_classes = tbl_size / (sizeof (sclass_t));
1803         class_tbl = mdb_alloc(tbl_size, UM_SLEEP | UM_GC);
1804 
1805         if (mdb_readsym(class_tbl, tbl_size, "sclass") == -1) {
1806                 mdb_warn("failed to read sclass");
1807                 return (DCMD_ERR);
1808         }
1809 
1810         mdb_printf("%<u>%4s %-10s %-24s %-24s%</u>\n", "SLOT", "NAME",
1811             "INIT FCN", "CLASS FCN");
1812 
1813         for (i = 0; i < num_classes; i++) {
1814                 if (mdb_vread(class_name, sizeof (class_name),
1815                     (uintptr_t)class_tbl[i].cl_name) == -1)
1816                         (void) strcpy(class_name, "???");
1817 
1818                 mdb_printf("%4ld %-10s %-24a %-24a\n", i, class_name,
1819                     class_tbl[i].cl_init, class_tbl[i].cl_funcs);
1820         }
1821 
1822         return (DCMD_OK);
1823 }
1824 
1825 #define FSNAMELEN       32      /* Max len of FS name we read from vnodeops */
1826 
1827 int
1828 vnode2path(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1829 {
1830         uintptr_t rootdir;
1831         vnode_t vn;
1832         char buf[MAXPATHLEN];
1833 
1834         uint_t opt_F = FALSE;
1835 
1836         if (mdb_getopts(argc, argv,
1837             'F', MDB_OPT_SETBITS, TRUE, &opt_F, NULL) != argc)
1838                 return (DCMD_USAGE);
1839 
1840         if (!(flags & DCMD_ADDRSPEC)) {
1841                 mdb_warn("expected explicit vnode_t address before ::\n");
1842                 return (DCMD_USAGE);
1843         }
1844 
1845         if (mdb_readvar(&rootdir, "rootdir") == -1) {
1846                 mdb_warn("failed to read rootdir");
1847                 return (DCMD_ERR);
1848         }
1849 
1850         if (mdb_vnode2path(addr, buf, sizeof (buf)) == -1)
1851                 return (DCMD_ERR);
1852 
1853         if (*buf == '\0') {
1854                 mdb_printf("??\n");
1855                 return (DCMD_OK);
1856         }
1857 
1858         mdb_printf("%s", buf);
1859         if (opt_F && buf[strlen(buf)-1] != '/' &&
1860             mdb_vread(&vn, sizeof (vn), addr) == sizeof (vn))
1861                 mdb_printf("%c", mdb_vtype2chr(vn.v_type, 0));
1862         mdb_printf("\n");
1863 
1864         return (DCMD_OK);
1865 }
1866 
1867 int
1868 ld_walk_init(mdb_walk_state_t *wsp)
1869 {
1870         wsp->walk_data = (void *)wsp->walk_addr;
1871         return (WALK_NEXT);
1872 }
1873 
1874 int
1875 ld_walk_step(mdb_walk_state_t *wsp)
1876 {
1877         int status;
1878         lock_descriptor_t ld;
1879 
1880         if (mdb_vread(&ld, sizeof (lock_descriptor_t), wsp->walk_addr) == -1) {
1881                 mdb_warn("couldn't read lock_descriptor_t at %p\n",
1882                     wsp->walk_addr);
1883                 return (WALK_ERR);
1884         }
1885 
1886         status = wsp->walk_callback(wsp->walk_addr, &ld, wsp->walk_cbdata);
1887         if (status == WALK_ERR)
1888                 return (WALK_ERR);
1889 
1890         wsp->walk_addr = (uintptr_t)ld.l_next;
1891         if (wsp->walk_addr == (uintptr_t)wsp->walk_data)
1892                 return (WALK_DONE);
1893 
1894         return (status);
1895 }
1896 
1897 int
1898 lg_walk_init(mdb_walk_state_t *wsp)
1899 {
1900         GElf_Sym sym;
1901 
1902         if (mdb_lookup_by_name("lock_graph", &sym) == -1) {
1903                 mdb_warn("failed to find symbol 'lock_graph'\n");
1904                 return (WALK_ERR);
1905         }
1906 
1907         wsp->walk_addr = (uintptr_t)sym.st_value;
1908         wsp->walk_data = (void *)(uintptr_t)(sym.st_value + sym.st_size);
1909 
1910         return (WALK_NEXT);
1911 }
1912 
1913 typedef struct lg_walk_data {
1914         uintptr_t startaddr;
1915         mdb_walk_cb_t callback;
1916         void *data;
1917 } lg_walk_data_t;
1918 
1919 /*
1920  * We can't use ::walk lock_descriptor directly, because the head of each graph
1921  * is really a dummy lock.  Rather than trying to dynamically determine if this
1922  * is a dummy node or not, we just filter out the initial element of the
1923  * list.
1924  */
1925 static int
1926 lg_walk_cb(uintptr_t addr, const void *data, void *priv)
1927 {
1928         lg_walk_data_t *lw = priv;
1929 
1930         if (addr != lw->startaddr)
1931                 return (lw->callback(addr, data, lw->data));
1932 
1933         return (WALK_NEXT);
1934 }
1935 
1936 int
1937 lg_walk_step(mdb_walk_state_t *wsp)
1938 {
1939         graph_t *graph;
1940         lg_walk_data_t lw;
1941 
1942         if (wsp->walk_addr >= (uintptr_t)wsp->walk_data)
1943                 return (WALK_DONE);
1944 
1945         if (mdb_vread(&graph, sizeof (graph), wsp->walk_addr) == -1) {
1946                 mdb_warn("failed to read graph_t at %p", wsp->walk_addr);
1947                 return (WALK_ERR);
1948         }
1949 
1950         wsp->walk_addr += sizeof (graph);
1951 
1952         if (graph == NULL)
1953                 return (WALK_NEXT);
1954 
1955         lw.callback = wsp->walk_callback;
1956         lw.data = wsp->walk_cbdata;
1957 
1958         lw.startaddr = (uintptr_t)&(graph->active_locks);
1959         if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
1960                 mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
1961                 return (WALK_ERR);
1962         }
1963 
1964         lw.startaddr = (uintptr_t)&(graph->sleeping_locks);
1965         if (mdb_pwalk("lock_descriptor", lg_walk_cb, &lw, lw.startaddr)) {
1966                 mdb_warn("couldn't walk lock_descriptor at %p\n", lw.startaddr);
1967                 return (WALK_ERR);
1968         }
1969 
1970         return (WALK_NEXT);
1971 }
1972 
1973 /*
1974  * The space available for the path corresponding to the locked vnode depends
1975  * on whether we are printing 32- or 64-bit addresses.
1976  */
1977 #ifdef _LP64
1978 #define LM_VNPATHLEN    20
1979 #else
1980 #define LM_VNPATHLEN    30
1981 #endif
1982 
1983 typedef struct mdb_lminfo_proc {
1984         struct {
1985                 char            u_comm[MAXCOMLEN + 1];
1986         } p_user;
1987 } mdb_lminfo_proc_t;
1988 
1989 /*ARGSUSED*/
1990 static int
1991 lminfo_cb(uintptr_t addr, const void *data, void *priv)
1992 {
1993         const lock_descriptor_t *ld = data;
1994         char buf[LM_VNPATHLEN];
1995         mdb_lminfo_proc_t p;
1996         uintptr_t paddr = 0;
1997 
1998         if (ld->l_flock.l_pid != 0)
1999                 paddr = mdb_pid2proc(ld->l_flock.l_pid, NULL);
2000 
2001         if (paddr != 0)
2002                 mdb_ctf_vread(&p, "proc_t", "mdb_lminfo_proc_t", paddr, 0);
2003 
2004         mdb_printf("%-?p %2s %04x %6d %-16s %-?p ",
2005             addr, ld->l_type == F_RDLCK ? "RD" :
2006             ld->l_type == F_WRLCK ? "WR" : "??",
2007             ld->l_state, ld->l_flock.l_pid,
2008             ld->l_flock.l_pid == 0 ? "<kernel>" :
2009             paddr == 0 ? "<defunct>" : p.p_user.u_comm, ld->l_vnode);
2010 
2011         mdb_vnode2path((uintptr_t)ld->l_vnode, buf,
2012             sizeof (buf));
2013         mdb_printf("%s\n", buf);
2014 
2015         return (WALK_NEXT);
2016 }
2017 
2018 /*ARGSUSED*/
2019 int
2020 lminfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2021 {
2022         if (DCMD_HDRSPEC(flags))
2023                 mdb_printf("%<u>%-?s %2s %4s %6s %-16s %-?s %s%</u>\n",
2024                     "ADDR", "TP", "FLAG", "PID", "COMM", "VNODE", "PATH");
2025 
2026         return (mdb_pwalk("lock_graph", lminfo_cb, NULL, 0));
2027 }
2028 
2029 /*ARGSUSED*/
2030 int
2031 whereopen_fwalk(uintptr_t addr, struct file *f, uintptr_t *target)
2032 {
2033         if ((uintptr_t)f->f_vnode == *target) {
2034                 mdb_printf("file %p\n", addr);
2035                 *target = 0;
2036         }
2037 
2038         return (WALK_NEXT);
2039 }
2040 
2041 /*ARGSUSED*/
2042 int
2043 whereopen_pwalk(uintptr_t addr, void *ignored, uintptr_t *target)
2044 {
2045         uintptr_t t = *target;
2046 
2047         if (mdb_pwalk("file", (mdb_walk_cb_t)whereopen_fwalk, &t, addr) == -1) {
2048                 mdb_warn("couldn't file walk proc %p", addr);
2049                 return (WALK_ERR);
2050         }
2051 
2052         if (t == 0)
2053                 mdb_printf("%p\n", addr);
2054 
2055         return (WALK_NEXT);
2056 }
2057 
2058 /*ARGSUSED*/
2059 int
2060 whereopen(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2061 {
2062         uintptr_t target = addr;
2063 
2064         if (!(flags & DCMD_ADDRSPEC) || addr == 0)
2065                 return (DCMD_USAGE);
2066 
2067         if (mdb_walk("proc", (mdb_walk_cb_t)whereopen_pwalk, &target) == -1) {
2068                 mdb_warn("can't proc walk");
2069                 return (DCMD_ERR);
2070         }
2071 
2072         return (DCMD_OK);
2073 }
2074 
2075 typedef struct datafmt {
2076         char    *hdr1;
2077         char    *hdr2;
2078         char    *dashes;
2079         char    *fmt;
2080 } datafmt_t;
2081 
2082 static datafmt_t kmemfmt[] = {
2083         { "cache                    ", "name                     ",
2084         "-------------------------", "%-25s "                           },
2085         { "   buf",     "  size",       "------",       "%6u "          },
2086         { "   buf",     "in use",       "------",       "%6u "          },
2087         { "   buf",     " total",       "------",       "%6u "          },
2088         { "   memory",  "   in use",    "----------",   "%10lu%c "      },
2089         { "    alloc",  "  succeed",    "---------",    "%9u "          },
2090         { "alloc",      " fail",        "-----",        "%5u "          },
2091         { NULL,         NULL,           NULL,           NULL            }
2092 };
2093 
2094 static datafmt_t vmemfmt[] = {
2095         { "vmem                     ", "name                     ",
2096         "-------------------------", "%-*s "                            },
2097         { "   memory",  "   in use",    "----------",   "%9llu%c "      },
2098         { "    memory", "     total",   "-----------",  "%10llu%c "     },
2099         { "   memory",  "   import",    "----------",   "%9llu%c "      },
2100         { "    alloc",  "  succeed",    "---------",    "%9llu "        },
2101         { "alloc",      " fail",        "-----",        "%5llu "        },
2102         { NULL,         NULL,           NULL,           NULL            }
2103 };
2104 
2105 /*ARGSUSED*/
2106 static int
2107 kmastat_cpu_avail(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *avail)
2108 {
2109         short rounds, prounds;
2110 
2111         if (KMEM_DUMPCC(ccp)) {
2112                 rounds = ccp->cc_dump_rounds;
2113                 prounds = ccp->cc_dump_prounds;
2114         } else {
2115                 rounds = ccp->cc_rounds;
2116                 prounds = ccp->cc_prounds;
2117         }
2118         if (rounds > 0)
2119                 *avail += rounds;
2120         if (prounds > 0)
2121                 *avail += prounds;
2122 
2123         return (WALK_NEXT);
2124 }
2125 
2126 /*ARGSUSED*/
2127 static int
2128 kmastat_cpu_alloc(uintptr_t addr, const kmem_cpu_cache_t *ccp, int *alloc)
2129 {
2130         *alloc += ccp->cc_alloc;
2131 
2132         return (WALK_NEXT);
2133 }
2134 
2135 /*ARGSUSED*/
2136 static int
2137 kmastat_slab_avail(uintptr_t addr, const kmem_slab_t *sp, int *avail)
2138 {
2139         *avail += sp->slab_chunks - sp->slab_refcnt;
2140 
2141         return (WALK_NEXT);
2142 }
2143 
2144 typedef struct kmastat_vmem {
2145         uintptr_t kv_addr;
2146         struct kmastat_vmem *kv_next;
2147         size_t kv_meminuse;
2148         int kv_alloc;
2149         int kv_fail;
2150 } kmastat_vmem_t;
2151 
2152 typedef struct kmastat_args {
2153         kmastat_vmem_t **ka_kvpp;
2154         uint_t ka_shift;
2155 } kmastat_args_t;
2156 
2157 static int
2158 kmastat_cache(uintptr_t addr, const kmem_cache_t *cp, kmastat_args_t *kap)
2159 {
2160         kmastat_vmem_t **kvpp = kap->ka_kvpp;
2161         kmastat_vmem_t *kv;
2162         datafmt_t *dfp = kmemfmt;
2163         int magsize;
2164 
2165         int avail, alloc, total;
2166         size_t meminuse = (cp->cache_slab_create - cp->cache_slab_destroy) *
2167             cp->cache_slabsize;
2168 
2169         mdb_walk_cb_t cpu_avail = (mdb_walk_cb_t)kmastat_cpu_avail;
2170         mdb_walk_cb_t cpu_alloc = (mdb_walk_cb_t)kmastat_cpu_alloc;
2171         mdb_walk_cb_t slab_avail = (mdb_walk_cb_t)kmastat_slab_avail;
2172 
2173         magsize = kmem_get_magsize(cp);
2174 
2175         alloc = cp->cache_slab_alloc + cp->cache_full.ml_alloc;
2176         avail = cp->cache_full.ml_total * magsize;
2177         total = cp->cache_buftotal;
2178 
2179         (void) mdb_pwalk("kmem_cpu_cache", cpu_alloc, &alloc, addr);
2180         (void) mdb_pwalk("kmem_cpu_cache", cpu_avail, &avail, addr);
2181         (void) mdb_pwalk("kmem_slab_partial", slab_avail, &avail, addr);
2182 
2183         for (kv = *kvpp; kv != NULL; kv = kv->kv_next) {
2184                 if (kv->kv_addr == (uintptr_t)cp->cache_arena)
2185                         goto out;
2186         }
2187 
2188         kv = mdb_zalloc(sizeof (kmastat_vmem_t), UM_SLEEP | UM_GC);
2189         kv->kv_next = *kvpp;
2190         kv->kv_addr = (uintptr_t)cp->cache_arena;
2191         *kvpp = kv;
2192 out:
2193         kv->kv_meminuse += meminuse;
2194         kv->kv_alloc += alloc;
2195         kv->kv_fail += cp->cache_alloc_fail;
2196 
2197         mdb_printf((dfp++)->fmt, cp->cache_name);
2198         mdb_printf((dfp++)->fmt, cp->cache_bufsize);
2199         mdb_printf((dfp++)->fmt, total - avail);
2200         mdb_printf((dfp++)->fmt, total);
2201         mdb_printf((dfp++)->fmt, meminuse >> kap->ka_shift,
2202             kap->ka_shift == GIGS ? 'G' : kap->ka_shift == MEGS ? 'M' :
2203             kap->ka_shift == KILOS ? 'K' : 'B');
2204         mdb_printf((dfp++)->fmt, alloc);
2205         mdb_printf((dfp++)->fmt, cp->cache_alloc_fail);
2206         mdb_printf("\n");
2207 
2208         return (WALK_NEXT);
2209 }
2210 
2211 static int
2212 kmastat_vmem_totals(uintptr_t addr, const vmem_t *v, kmastat_args_t *kap)
2213 {
2214         kmastat_vmem_t *kv = *kap->ka_kvpp;
2215         size_t len;
2216 
2217         while (kv != NULL && kv->kv_addr != addr)
2218                 kv = kv->kv_next;
2219 
2220         if (kv == NULL || kv->kv_alloc == 0)
2221                 return (WALK_NEXT);
2222 
2223         len = MIN(17, strlen(v->vm_name));
2224 
2225         mdb_printf("Total [%s]%*s %6s %6s %6s %10lu%c %9u %5u\n", v->vm_name,
2226             17 - len, "", "", "", "",
2227             kv->kv_meminuse >> kap->ka_shift,
2228             kap->ka_shift == GIGS ? 'G' : kap->ka_shift == MEGS ? 'M' :
2229             kap->ka_shift == KILOS ? 'K' : 'B', kv->kv_alloc, kv->kv_fail);
2230 
2231         return (WALK_NEXT);
2232 }
2233 
2234 /*ARGSUSED*/
2235 static int
2236 kmastat_vmem(uintptr_t addr, const vmem_t *v, const uint_t *shiftp)
2237 {
2238         datafmt_t *dfp = vmemfmt;
2239         const vmem_kstat_t *vkp = &v->vm_kstat;
2240         uintptr_t paddr;
2241         vmem_t parent;
2242         int ident = 0;
2243 
2244         for (paddr = (uintptr_t)v->vm_source; paddr != 0; ident += 4) {
2245                 if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
2246                         mdb_warn("couldn't trace %p's ancestry", addr);
2247                         ident = 0;
2248                         break;
2249                 }
2250                 paddr = (uintptr_t)parent.vm_source;
2251         }
2252 
2253         mdb_printf("%*s", ident, "");
2254         mdb_printf((dfp++)->fmt, 25 - ident, v->vm_name);
2255         mdb_printf((dfp++)->fmt, vkp->vk_mem_inuse.value.ui64 >> *shiftp,
2256             *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
2257             *shiftp == KILOS ? 'K' : 'B');
2258         mdb_printf((dfp++)->fmt, vkp->vk_mem_total.value.ui64 >> *shiftp,
2259             *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
2260             *shiftp == KILOS ? 'K' : 'B');
2261         mdb_printf((dfp++)->fmt, vkp->vk_mem_import.value.ui64 >> *shiftp,
2262             *shiftp == GIGS ? 'G' : *shiftp == MEGS ? 'M' :
2263             *shiftp == KILOS ? 'K' : 'B');
2264         mdb_printf((dfp++)->fmt, vkp->vk_alloc.value.ui64);
2265         mdb_printf((dfp++)->fmt, vkp->vk_fail.value.ui64);
2266 
2267         mdb_printf("\n");
2268 
2269         return (WALK_NEXT);
2270 }
2271 
2272 /*ARGSUSED*/
2273 int
2274 kmastat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2275 {
2276         kmastat_vmem_t *kv = NULL;
2277         datafmt_t *dfp;
2278         kmastat_args_t ka;
2279 
2280         ka.ka_shift = 0;
2281         if (mdb_getopts(argc, argv,
2282             'k', MDB_OPT_SETBITS, KILOS, &ka.ka_shift,
2283             'm', MDB_OPT_SETBITS, MEGS, &ka.ka_shift,
2284             'g', MDB_OPT_SETBITS, GIGS, &ka.ka_shift, NULL) != argc)
2285                 return (DCMD_USAGE);
2286 
2287         for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2288                 mdb_printf("%s ", dfp->hdr1);
2289         mdb_printf("\n");
2290 
2291         for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2292                 mdb_printf("%s ", dfp->hdr2);
2293         mdb_printf("\n");
2294 
2295         for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2296                 mdb_printf("%s ", dfp->dashes);
2297         mdb_printf("\n");
2298 
2299         ka.ka_kvpp = &kv;
2300         if (mdb_walk("kmem_cache", (mdb_walk_cb_t)kmastat_cache, &ka) == -1) {
2301                 mdb_warn("can't walk 'kmem_cache'");
2302                 return (DCMD_ERR);
2303         }
2304 
2305         for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2306                 mdb_printf("%s ", dfp->dashes);
2307         mdb_printf("\n");
2308 
2309         if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem_totals, &ka) == -1) {
2310                 mdb_warn("can't walk 'vmem'");
2311                 return (DCMD_ERR);
2312         }
2313 
2314         for (dfp = kmemfmt; dfp->hdr1 != NULL; dfp++)
2315                 mdb_printf("%s ", dfp->dashes);
2316         mdb_printf("\n");
2317 
2318         mdb_printf("\n");
2319 
2320         for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2321                 mdb_printf("%s ", dfp->hdr1);
2322         mdb_printf("\n");
2323 
2324         for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2325                 mdb_printf("%s ", dfp->hdr2);
2326         mdb_printf("\n");
2327 
2328         for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2329                 mdb_printf("%s ", dfp->dashes);
2330         mdb_printf("\n");
2331 
2332         if (mdb_walk("vmem", (mdb_walk_cb_t)kmastat_vmem, &ka.ka_shift) == -1) {
2333                 mdb_warn("can't walk 'vmem'");
2334                 return (DCMD_ERR);
2335         }
2336 
2337         for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
2338                 mdb_printf("%s ", dfp->dashes);
2339         mdb_printf("\n");
2340         return (DCMD_OK);
2341 }
2342 
2343 /*
2344  * Our ::kgrep callback scans the entire kernel VA space (kas).  kas is made
2345  * up of a set of 'struct seg's.  We could just scan each seg en masse, but
2346  * unfortunately, a few of the segs are both large and sparse, so we could
2347  * spend quite a bit of time scanning VAs which have no backing pages.
2348  *
2349  * So for the few very sparse segs, we skip the segment itself, and scan
2350  * the allocated vmem_segs in the vmem arena which manages that part of kas.
2351  * Currently, we do this for:
2352  *
2353  *      SEG             VMEM ARENA
2354  *      kvseg           heap_arena
2355  *      kvseg32         heap32_arena
2356  *      kvseg_core      heap_core_arena
2357  *
2358  * In addition, we skip the segkpm segment in its entirety, since it is very
2359  * sparse, and contains no new kernel data.
2360  */
2361 typedef struct kgrep_walk_data {
2362         kgrep_cb_func *kg_cb;
2363         void *kg_cbdata;
2364         uintptr_t kg_kvseg;
2365         uintptr_t kg_kvseg32;
2366         uintptr_t kg_kvseg_core;
2367         uintptr_t kg_segkpm;
2368         uintptr_t kg_heap_lp_base;
2369         uintptr_t kg_heap_lp_end;
2370 } kgrep_walk_data_t;
2371 
2372 static int
2373 kgrep_walk_seg(uintptr_t addr, const struct seg *seg, kgrep_walk_data_t *kg)
2374 {
2375         uintptr_t base = (uintptr_t)seg->s_base;
2376 
2377         if (addr == kg->kg_kvseg || addr == kg->kg_kvseg32 ||
2378             addr == kg->kg_kvseg_core)
2379                 return (WALK_NEXT);
2380 
2381         if ((uintptr_t)seg->s_ops == kg->kg_segkpm)
2382                 return (WALK_NEXT);
2383 
2384         return (kg->kg_cb(base, base + seg->s_size, kg->kg_cbdata));
2385 }
2386 
2387 /*ARGSUSED*/
2388 static int
2389 kgrep_walk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
2390 {
2391         /*
2392          * skip large page heap address range - it is scanned by walking
2393          * allocated vmem_segs in the heap_lp_arena
2394          */
2395         if (seg->vs_start == kg->kg_heap_lp_base &&
2396             seg->vs_end == kg->kg_heap_lp_end)
2397                 return (WALK_NEXT);
2398 
2399         return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
2400 }
2401 
2402 /*ARGSUSED*/
2403 static int
2404 kgrep_xwalk_vseg(uintptr_t addr, const vmem_seg_t *seg, kgrep_walk_data_t *kg)
2405 {
2406         return (kg->kg_cb(seg->vs_start, seg->vs_end, kg->kg_cbdata));
2407 }
2408 
2409 static int
2410 kgrep_walk_vmem(uintptr_t addr, const vmem_t *vmem, kgrep_walk_data_t *kg)
2411 {
2412         mdb_walk_cb_t walk_vseg = (mdb_walk_cb_t)kgrep_walk_vseg;
2413 
2414         if (strcmp(vmem->vm_name, "heap") != 0 &&
2415             strcmp(vmem->vm_name, "heap32") != 0 &&
2416             strcmp(vmem->vm_name, "heap_core") != 0 &&
2417             strcmp(vmem->vm_name, "heap_lp") != 0)
2418                 return (WALK_NEXT);
2419 
2420         if (strcmp(vmem->vm_name, "heap_lp") == 0)
2421                 walk_vseg = (mdb_walk_cb_t)kgrep_xwalk_vseg;
2422 
2423         if (mdb_pwalk("vmem_alloc", walk_vseg, kg, addr) == -1) {
2424                 mdb_warn("couldn't walk vmem_alloc for vmem %p", addr);
2425                 return (WALK_ERR);
2426         }
2427 
2428         return (WALK_NEXT);
2429 }
2430 
2431 int
2432 kgrep_subr(kgrep_cb_func *cb, void *cbdata)
2433 {
2434         GElf_Sym kas, kvseg, kvseg32, kvseg_core, segkpm;
2435         kgrep_walk_data_t kg;
2436 
2437         if (mdb_get_state() == MDB_STATE_RUNNING) {
2438                 mdb_warn("kgrep can only be run on a system "
2439                     "dump or under kmdb; see dumpadm(1M)\n");
2440                 return (DCMD_ERR);
2441         }
2442 
2443         if (mdb_lookup_by_name("kas", &kas) == -1) {
2444                 mdb_warn("failed to locate 'kas' symbol\n");
2445                 return (DCMD_ERR);
2446         }
2447 
2448         if (mdb_lookup_by_name("kvseg", &kvseg) == -1) {
2449                 mdb_warn("failed to locate 'kvseg' symbol\n");
2450                 return (DCMD_ERR);
2451         }
2452 
2453         if (mdb_lookup_by_name("kvseg32", &kvseg32) == -1) {
2454                 mdb_warn("failed to locate 'kvseg32' symbol\n");
2455                 return (DCMD_ERR);
2456         }
2457 
2458         if (mdb_lookup_by_name("kvseg_core", &kvseg_core) == -1) {
2459                 mdb_warn("failed to locate 'kvseg_core' symbol\n");
2460                 return (DCMD_ERR);
2461         }
2462 
2463         if (mdb_lookup_by_name("segkpm_ops", &segkpm) == -1) {
2464                 mdb_warn("failed to locate 'segkpm_ops' symbol\n");
2465                 return (DCMD_ERR);
2466         }
2467 
2468         if (mdb_readvar(&kg.kg_heap_lp_base, "heap_lp_base") == -1) {
2469                 mdb_warn("failed to read 'heap_lp_base'\n");
2470                 return (DCMD_ERR);
2471         }
2472 
2473         if (mdb_readvar(&kg.kg_heap_lp_end, "heap_lp_end") == -1) {
2474                 mdb_warn("failed to read 'heap_lp_end'\n");
2475                 return (DCMD_ERR);
2476         }
2477 
2478         kg.kg_cb = cb;
2479         kg.kg_cbdata = cbdata;
2480         kg.kg_kvseg = (uintptr_t)kvseg.st_value;
2481         kg.kg_kvseg32 = (uintptr_t)kvseg32.st_value;
2482         kg.kg_kvseg_core = (uintptr_t)kvseg_core.st_value;
2483         kg.kg_segkpm = (uintptr_t)segkpm.st_value;
2484 
2485         if (mdb_pwalk("seg", (mdb_walk_cb_t)kgrep_walk_seg,
2486             &kg, kas.st_value) == -1) {
2487                 mdb_warn("failed to walk kas segments");
2488                 return (DCMD_ERR);
2489         }
2490 
2491         if (mdb_walk("vmem", (mdb_walk_cb_t)kgrep_walk_vmem, &kg) == -1) {
2492                 mdb_warn("failed to walk heap/heap32 vmem arenas");
2493                 return (DCMD_ERR);
2494         }
2495 
2496         return (DCMD_OK);
2497 }
2498 
2499 size_t
2500 kgrep_subr_pagesize(void)
2501 {
2502         return (PAGESIZE);
2503 }
2504 
2505 typedef struct file_walk_data {
2506         struct uf_entry *fw_flist;
2507         int fw_flistsz;
2508         int fw_ndx;
2509         int fw_nofiles;
2510 } file_walk_data_t;
2511 
2512 typedef struct mdb_file_proc {
2513         struct {
2514                 struct {
2515                         int                     fi_nfiles;
2516                         uf_entry_t *volatile    fi_list;
2517                 } u_finfo;
2518         } p_user;
2519 } mdb_file_proc_t;
2520 
2521 int
2522 file_walk_init(mdb_walk_state_t *wsp)
2523 {
2524         file_walk_data_t *fw;
2525         mdb_file_proc_t p;
2526 
2527         if (wsp->walk_addr == 0) {
2528                 mdb_warn("file walk doesn't support global walks\n");
2529                 return (WALK_ERR);
2530         }
2531 
2532         fw = mdb_alloc(sizeof (file_walk_data_t), UM_SLEEP);
2533 
2534         if (mdb_ctf_vread(&p, "proc_t", "mdb_file_proc_t",
2535             wsp->walk_addr, 0) == -1) {
2536                 mdb_free(fw, sizeof (file_walk_data_t));
2537                 mdb_warn("failed to read proc structure at %p", wsp->walk_addr);
2538                 return (WALK_ERR);
2539         }
2540 
2541         if (p.p_user.u_finfo.fi_nfiles == 0) {
2542                 mdb_free(fw, sizeof (file_walk_data_t));
2543                 return (WALK_DONE);
2544         }
2545 
2546         fw->fw_nofiles = p.p_user.u_finfo.fi_nfiles;
2547         fw->fw_flistsz = sizeof (struct uf_entry) * fw->fw_nofiles;
2548         fw->fw_flist = mdb_alloc(fw->fw_flistsz, UM_SLEEP);
2549 
2550         if (mdb_vread(fw->fw_flist, fw->fw_flistsz,
2551             (uintptr_t)p.p_user.u_finfo.fi_list) == -1) {
2552                 mdb_warn("failed to read file array at %p",
2553                     p.p_user.u_finfo.fi_list);
2554                 mdb_free(fw->fw_flist, fw->fw_flistsz);
2555                 mdb_free(fw, sizeof (file_walk_data_t));
2556                 return (WALK_ERR);
2557         }
2558 
2559         fw->fw_ndx = 0;
2560         wsp->walk_data = fw;
2561 
2562         return (WALK_NEXT);
2563 }
2564 
2565 int
2566 file_walk_step(mdb_walk_state_t *wsp)
2567 {
2568         file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
2569         struct file file;
2570         uintptr_t fp;
2571 
2572 again:
2573         if (fw->fw_ndx == fw->fw_nofiles)
2574                 return (WALK_DONE);
2575 
2576         if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) == 0)
2577                 goto again;
2578 
2579         (void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
2580         return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
2581 }
2582 
2583 int
2584 allfile_walk_step(mdb_walk_state_t *wsp)
2585 {
2586         file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
2587         struct file file;
2588         uintptr_t fp;
2589 
2590         if (fw->fw_ndx == fw->fw_nofiles)
2591                 return (WALK_DONE);
2592 
2593         if ((fp = (uintptr_t)fw->fw_flist[fw->fw_ndx++].uf_file) != 0)
2594                 (void) mdb_vread(&file, sizeof (file), (uintptr_t)fp);
2595         else
2596                 bzero(&file, sizeof (file));
2597 
2598         return (wsp->walk_callback(fp, &file, wsp->walk_cbdata));
2599 }
2600 
2601 void
2602 file_walk_fini(mdb_walk_state_t *wsp)
2603 {
2604         file_walk_data_t *fw = (file_walk_data_t *)wsp->walk_data;
2605 
2606         mdb_free(fw->fw_flist, fw->fw_flistsz);
2607         mdb_free(fw, sizeof (file_walk_data_t));
2608 }
2609 
2610 int
2611 port_walk_init(mdb_walk_state_t *wsp)
2612 {
2613         if (wsp->walk_addr == 0) {
2614                 mdb_warn("port walk doesn't support global walks\n");
2615                 return (WALK_ERR);
2616         }
2617 
2618         if (mdb_layered_walk("file", wsp) == -1) {
2619                 mdb_warn("couldn't walk 'file'");
2620                 return (WALK_ERR);
2621         }
2622         return (WALK_NEXT);
2623 }
2624 
2625 int
2626 port_walk_step(mdb_walk_state_t *wsp)
2627 {
2628         struct vnode    vn;
2629         uintptr_t       vp;
2630         uintptr_t       pp;
2631         struct port     port;
2632 
2633         vp = (uintptr_t)((struct file *)wsp->walk_layer)->f_vnode;
2634         if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
2635                 mdb_warn("failed to read vnode_t at %p", vp);
2636                 return (WALK_ERR);
2637         }
2638         if (vn.v_type != VPORT)
2639                 return (WALK_NEXT);
2640 
2641         pp = (uintptr_t)vn.v_data;
2642         if (mdb_vread(&port, sizeof (port), pp) == -1) {
2643                 mdb_warn("failed to read port_t at %p", pp);
2644                 return (WALK_ERR);
2645         }
2646         return (wsp->walk_callback(pp, &port, wsp->walk_cbdata));
2647 }
2648 
2649 typedef struct portev_walk_data {
2650         list_node_t     *pev_node;
2651         list_node_t     *pev_last;
2652         size_t          pev_offset;
2653 } portev_walk_data_t;
2654 
2655 int
2656 portev_walk_init(mdb_walk_state_t *wsp)
2657 {
2658         portev_walk_data_t *pevd;
2659         struct port     port;
2660         struct vnode    vn;
2661         struct list     *list;
2662         uintptr_t       vp;
2663 
2664         if (wsp->walk_addr == 0) {
2665                 mdb_warn("portev walk doesn't support global walks\n");
2666                 return (WALK_ERR);
2667         }
2668 
2669         pevd = mdb_alloc(sizeof (portev_walk_data_t), UM_SLEEP);
2670 
2671         if (mdb_vread(&port, sizeof (port), wsp->walk_addr) == -1) {
2672                 mdb_free(pevd, sizeof (portev_walk_data_t));
2673                 mdb_warn("failed to read port structure at %p", wsp->walk_addr);
2674                 return (WALK_ERR);
2675         }
2676 
2677         vp = (uintptr_t)port.port_vnode;
2678         if (mdb_vread(&vn, sizeof (vn), vp) == -1) {
2679                 mdb_free(pevd, sizeof (portev_walk_data_t));
2680                 mdb_warn("failed to read vnode_t at %p", vp);
2681                 return (WALK_ERR);
2682         }
2683 
2684         if (vn.v_type != VPORT) {
2685                 mdb_free(pevd, sizeof (portev_walk_data_t));
2686                 mdb_warn("input address (%p) does not point to an event port",
2687                     wsp->walk_addr);
2688                 return (WALK_ERR);
2689         }
2690 
2691         if (port.port_queue.portq_nent == 0) {
2692                 mdb_free(pevd, sizeof (portev_walk_data_t));
2693                 return (WALK_DONE);
2694         }
2695         list = &port.port_queue.portq_list;
2696         pevd->pev_offset = list->list_offset;
2697         pevd->pev_last = list->list_head.list_prev;
2698         pevd->pev_node = list->list_head.list_next;
2699         wsp->walk_data = pevd;
2700         return (WALK_NEXT);
2701 }
2702 
2703 int
2704 portev_walk_step(mdb_walk_state_t *wsp)
2705 {
2706         portev_walk_data_t      *pevd;
2707         struct port_kevent      ev;
2708         uintptr_t               evp;
2709 
2710         pevd = (portev_walk_data_t *)wsp->walk_data;
2711 
2712         if (pevd->pev_last == NULL)
2713                 return (WALK_DONE);
2714         if (pevd->pev_node == pevd->pev_last)
2715                 pevd->pev_last = NULL;               /* last round */
2716 
2717         evp = ((uintptr_t)(((char *)pevd->pev_node) - pevd->pev_offset));
2718         if (mdb_vread(&ev, sizeof (ev), evp) == -1) {
2719                 mdb_warn("failed to read port_kevent at %p", evp);
2720                 return (WALK_DONE);
2721         }
2722         pevd->pev_node = ev.portkev_node.list_next;
2723         return (wsp->walk_callback(evp, &ev, wsp->walk_cbdata));
2724 }
2725 
2726 void
2727 portev_walk_fini(mdb_walk_state_t *wsp)
2728 {
2729         portev_walk_data_t *pevd = (portev_walk_data_t *)wsp->walk_data;
2730 
2731         if (pevd != NULL)
2732                 mdb_free(pevd, sizeof (portev_walk_data_t));
2733 }
2734 
2735 typedef struct proc_walk_data {
2736         uintptr_t *pw_stack;
2737         int pw_depth;
2738         int pw_max;
2739 } proc_walk_data_t;
2740 
2741 int
2742 proc_walk_init(mdb_walk_state_t *wsp)
2743 {
2744         GElf_Sym sym;
2745         proc_walk_data_t *pw;
2746 
2747         if (wsp->walk_addr == 0) {
2748                 if (mdb_lookup_by_name("p0", &sym) == -1) {
2749                         mdb_warn("failed to read 'practive'");
2750                         return (WALK_ERR);
2751                 }
2752                 wsp->walk_addr = (uintptr_t)sym.st_value;
2753         }
2754 
2755         pw = mdb_zalloc(sizeof (proc_walk_data_t), UM_SLEEP);
2756 
2757         if (mdb_readvar(&pw->pw_max, "nproc") == -1) {
2758                 mdb_warn("failed to read 'nproc'");
2759                 mdb_free(pw, sizeof (pw));
2760                 return (WALK_ERR);
2761         }
2762 
2763         pw->pw_stack = mdb_alloc(pw->pw_max * sizeof (uintptr_t), UM_SLEEP);
2764         wsp->walk_data = pw;
2765 
2766         return (WALK_NEXT);
2767 }
2768 
2769 typedef struct mdb_walk_proc {
2770         struct proc     *p_child;
2771         struct proc     *p_sibling;
2772 } mdb_walk_proc_t;
2773 
2774 int
2775 proc_walk_step(mdb_walk_state_t *wsp)
2776 {
2777         proc_walk_data_t *pw = wsp->walk_data;
2778         uintptr_t addr = wsp->walk_addr;
2779         uintptr_t cld, sib;
2780         int status;
2781         mdb_walk_proc_t pr;
2782 
2783         if (mdb_ctf_vread(&pr, "proc_t", "mdb_walk_proc_t",
2784             addr, 0) == -1) {
2785                 mdb_warn("failed to read proc at %p", addr);
2786                 return (WALK_DONE);
2787         }
2788 
2789         cld = (uintptr_t)pr.p_child;
2790         sib = (uintptr_t)pr.p_sibling;
2791 
2792         if (pw->pw_depth > 0 && addr == pw->pw_stack[pw->pw_depth - 1]) {
2793                 pw->pw_depth--;
2794                 goto sib;
2795         }
2796 
2797         /*
2798          * Always pass NULL as the local copy pointer. Consumers
2799          * should use mdb_ctf_vread() to read their own minimal
2800          * version of proc_t. Thus minimizing the chance of breakage
2801          * with older crash dumps.
2802          */
2803         status = wsp->walk_callback(addr, NULL, wsp->walk_cbdata);
2804 
2805         if (status != WALK_NEXT)
2806                 return (status);
2807 
2808         if ((wsp->walk_addr = cld) != 0) {
2809                 if (mdb_ctf_vread(&pr, "proc_t", "mdb_walk_proc_t",
2810                     cld, 0) == -1) {
2811                         mdb_warn("proc %p has invalid p_child %p; skipping\n",
2812                             addr, cld);
2813                         goto sib;
2814                 }
2815 
2816                 pw->pw_stack[pw->pw_depth++] = addr;
2817 
2818                 if (pw->pw_depth == pw->pw_max) {
2819                         mdb_warn("depth %d exceeds max depth; try again\n",
2820                             pw->pw_depth);
2821                         return (WALK_DONE);
2822                 }
2823                 return (WALK_NEXT);
2824         }
2825 
2826 sib:
2827         /*
2828          * We know that p0 has no siblings, and if another starting proc
2829          * was given, we don't want to walk its siblings anyway.
2830          */
2831         if (pw->pw_depth == 0)
2832                 return (WALK_DONE);
2833 
2834         if (sib != 0 && mdb_ctf_vread(&pr, "proc_t", "mdb_walk_proc_t",
2835             sib, 0) == -1) {
2836                 mdb_warn("proc %p has invalid p_sibling %p; skipping\n",
2837                     addr, sib);
2838                 sib = 0;
2839         }
2840 
2841         if ((wsp->walk_addr = sib) == 0) {
2842                 if (pw->pw_depth > 0) {
2843                         wsp->walk_addr = pw->pw_stack[pw->pw_depth - 1];
2844                         return (WALK_NEXT);
2845                 }
2846                 return (WALK_DONE);
2847         }
2848 
2849         return (WALK_NEXT);
2850 }
2851 
2852 void
2853 proc_walk_fini(mdb_walk_state_t *wsp)
2854 {
2855         proc_walk_data_t *pw = wsp->walk_data;
2856 
2857         mdb_free(pw->pw_stack, pw->pw_max * sizeof (uintptr_t));
2858         mdb_free(pw, sizeof (proc_walk_data_t));
2859 }
2860 
2861 int
2862 task_walk_init(mdb_walk_state_t *wsp)
2863 {
2864         task_t task;
2865 
2866         if (mdb_vread(&task, sizeof (task_t), wsp->walk_addr) == -1) {
2867                 mdb_warn("failed to read task at %p", wsp->walk_addr);
2868                 return (WALK_ERR);
2869         }
2870         wsp->walk_addr = (uintptr_t)task.tk_memb_list;
2871         wsp->walk_data = task.tk_memb_list;
2872         return (WALK_NEXT);
2873 }
2874 
2875 typedef struct mdb_task_proc {
2876         struct proc     *p_tasknext;
2877 } mdb_task_proc_t;
2878 
2879 int
2880 task_walk_step(mdb_walk_state_t *wsp)
2881 {
2882         mdb_task_proc_t proc;
2883         int status;
2884 
2885         if (mdb_ctf_vread(&proc, "proc_t", "mdb_task_proc_t",
2886             wsp->walk_addr, 0) == -1) {
2887                 mdb_warn("failed to read proc at %p", wsp->walk_addr);
2888                 return (WALK_DONE);
2889         }
2890 
2891         status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
2892 
2893         if (proc.p_tasknext == wsp->walk_data)
2894                 return (WALK_DONE);
2895 
2896         wsp->walk_addr = (uintptr_t)proc.p_tasknext;
2897         return (status);
2898 }
2899 
2900 int
2901 project_walk_init(mdb_walk_state_t *wsp)
2902 {
2903         if (wsp->walk_addr == 0) {
2904                 if (mdb_readvar(&wsp->walk_addr, "proj0p") == -1) {
2905                         mdb_warn("failed to read 'proj0p'");
2906                         return (WALK_ERR);
2907                 }
2908         }
2909         wsp->walk_data = (void *)wsp->walk_addr;
2910         return (WALK_NEXT);
2911 }
2912 
2913 int
2914 project_walk_step(mdb_walk_state_t *wsp)
2915 {
2916         uintptr_t addr = wsp->walk_addr;
2917         kproject_t pj;
2918         int status;
2919 
2920         if (mdb_vread(&pj, sizeof (kproject_t), addr) == -1) {
2921                 mdb_warn("failed to read project at %p", addr);
2922                 return (WALK_DONE);
2923         }
2924         status = wsp->walk_callback(addr, &pj, wsp->walk_cbdata);
2925         if (status != WALK_NEXT)
2926                 return (status);
2927         wsp->walk_addr = (uintptr_t)pj.kpj_next;
2928         if ((void *)wsp->walk_addr == wsp->walk_data)
2929                 return (WALK_DONE);
2930         return (WALK_NEXT);
2931 }
2932 
2933 static int
2934 generic_walk_step(mdb_walk_state_t *wsp)
2935 {
2936         return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
2937             wsp->walk_cbdata));
2938 }
2939 
2940 static int
2941 cpu_walk_cmp(const void *l, const void *r)
2942 {
2943         uintptr_t lhs = *((uintptr_t *)l);
2944         uintptr_t rhs = *((uintptr_t *)r);
2945         cpu_t lcpu, rcpu;
2946 
2947         (void) mdb_vread(&lcpu, sizeof (lcpu), lhs);
2948         (void) mdb_vread(&rcpu, sizeof (rcpu), rhs);
2949 
2950         if (lcpu.cpu_id < rcpu.cpu_id)
2951                 return (-1);
2952 
2953         if (lcpu.cpu_id > rcpu.cpu_id)
2954                 return (1);
2955 
2956         return (0);
2957 }
2958 
2959 typedef struct cpu_walk {
2960         uintptr_t *cw_array;
2961         int cw_ndx;
2962 } cpu_walk_t;
2963 
2964 int
2965 cpu_walk_init(mdb_walk_state_t *wsp)
2966 {
2967         cpu_walk_t *cw;
2968         int max_ncpus, i = 0;
2969         uintptr_t current, first;
2970         cpu_t cpu, panic_cpu;
2971         uintptr_t panicstr, addr;
2972         GElf_Sym sym;
2973 
2974         cw = mdb_zalloc(sizeof (cpu_walk_t), UM_SLEEP | UM_GC);
2975 
2976         if (mdb_readvar(&max_ncpus, "max_ncpus") == -1) {
2977                 mdb_warn("failed to read 'max_ncpus'");
2978                 return (WALK_ERR);
2979         }
2980 
2981         if (mdb_readvar(&panicstr, "panicstr") == -1) {
2982                 mdb_warn("failed to read 'panicstr'");
2983                 return (WALK_ERR);
2984         }
2985 
2986         if (panicstr != 0) {
2987                 if (mdb_lookup_by_name("panic_cpu", &sym) == -1) {
2988                         mdb_warn("failed to find 'panic_cpu'");
2989                         return (WALK_ERR);
2990                 }
2991 
2992                 addr = (uintptr_t)sym.st_value;
2993 
2994                 if (mdb_vread(&panic_cpu, sizeof (cpu_t), addr) == -1) {
2995                         mdb_warn("failed to read 'panic_cpu'");
2996                         return (WALK_ERR);
2997                 }
2998         }
2999 
3000         /*
3001          * Unfortunately, there is no platform-independent way to walk
3002          * CPUs in ID order.  We therefore loop through in cpu_next order,
3003          * building an array of CPU pointers which will subsequently be
3004          * sorted.
3005          */
3006         cw->cw_array =
3007             mdb_zalloc((max_ncpus + 1) * sizeof (uintptr_t), UM_SLEEP | UM_GC);
3008 
3009         if (mdb_readvar(&first, "cpu_list") == -1) {
3010                 mdb_warn("failed to read 'cpu_list'");
3011                 return (WALK_ERR);
3012         }
3013 
3014         current = first;
3015         do {
3016                 if (mdb_vread(&cpu, sizeof (cpu), current) == -1) {
3017                         mdb_warn("failed to read cpu at %p", current);
3018                         return (WALK_ERR);
3019                 }
3020 
3021                 if (panicstr != 0 && panic_cpu.cpu_id == cpu.cpu_id) {
3022                         cw->cw_array[i++] = addr;
3023                 } else {
3024                         cw->cw_array[i++] = current;
3025                 }
3026         } while ((current = (uintptr_t)cpu.cpu_next) != first);
3027 
3028         qsort(cw->cw_array, i, sizeof (uintptr_t), cpu_walk_cmp);
3029         wsp->walk_data = cw;
3030 
3031         return (WALK_NEXT);
3032 }
3033 
3034 int
3035 cpu_walk_step(mdb_walk_state_t *wsp)
3036 {
3037         cpu_walk_t *cw = wsp->walk_data;
3038         cpu_t cpu;
3039         uintptr_t addr = cw->cw_array[cw->cw_ndx++];
3040 
3041         if (addr == 0)
3042                 return (WALK_DONE);
3043 
3044         if (mdb_vread(&cpu, sizeof (cpu), addr) == -1) {
3045                 mdb_warn("failed to read cpu at %p", addr);
3046                 return (WALK_DONE);
3047         }
3048 
3049         return (wsp->walk_callback(addr, &cpu, wsp->walk_cbdata));
3050 }
3051 
3052 typedef struct cpuinfo_data {
3053         intptr_t cid_cpu;
3054         uintptr_t **cid_ithr;
3055         char    cid_print_head;
3056         char    cid_print_thr;
3057         char    cid_print_ithr;
3058         char    cid_print_flags;
3059 } cpuinfo_data_t;
3060 
3061 int
3062 cpuinfo_walk_ithread(uintptr_t addr, const kthread_t *thr, cpuinfo_data_t *cid)
3063 {
3064         cpu_t c;
3065         int id;
3066         uint8_t pil;
3067 
3068         if (!(thr->t_flag & T_INTR_THREAD) || thr->t_state == TS_FREE)
3069                 return (WALK_NEXT);
3070 
3071         if (thr->t_bound_cpu == NULL) {
3072                 mdb_warn("thr %p is intr thread w/out a CPU\n", addr);
3073                 return (WALK_NEXT);
3074         }
3075 
3076         (void) mdb_vread(&c, sizeof (c), (uintptr_t)thr->t_bound_cpu);
3077 
3078         if ((id = c.cpu_id) >= NCPU) {
3079                 mdb_warn("CPU %p has id (%d) greater than NCPU (%d)\n",
3080                     thr->t_bound_cpu, id, NCPU);
3081                 return (WALK_NEXT);
3082         }
3083 
3084         if ((pil = thr->t_pil) >= NINTR) {
3085                 mdb_warn("thread %p has pil (%d) greater than %d\n",
3086                     addr, pil, NINTR);
3087                 return (WALK_NEXT);
3088         }
3089 
3090         if (cid->cid_ithr[id][pil] != 0) {
3091                 mdb_warn("CPU %d has multiple threads at pil %d (at least "
3092                     "%p and %p)\n", id, pil, addr, cid->cid_ithr[id][pil]);
3093                 return (WALK_NEXT);
3094         }
3095 
3096         cid->cid_ithr[id][pil] = addr;
3097 
3098         return (WALK_NEXT);
3099 }
3100 
3101 #define CPUINFO_IDWIDTH         3
3102 #define CPUINFO_FLAGWIDTH       9
3103 
3104 #ifdef _LP64
3105 #if defined(__amd64)
3106 #define CPUINFO_TWIDTH          16
3107 #define CPUINFO_CPUWIDTH        16
3108 #else
3109 #define CPUINFO_CPUWIDTH        11
3110 #define CPUINFO_TWIDTH          11
3111 #endif
3112 #else
3113 #define CPUINFO_CPUWIDTH        8
3114 #define CPUINFO_TWIDTH          8
3115 #endif
3116 
3117 #define CPUINFO_THRDELT         (CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 9)
3118 #define CPUINFO_FLAGDELT        (CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH + 4)
3119 #define CPUINFO_ITHRDELT        4
3120 
3121 #define CPUINFO_INDENT  mdb_printf("%*s", CPUINFO_THRDELT, \
3122     flagline < nflaglines ? flagbuf[flagline++] : "")
3123 
3124 typedef struct mdb_cpuinfo_proc {
3125         struct {
3126                 char            u_comm[MAXCOMLEN + 1];
3127         } p_user;
3128 } mdb_cpuinfo_proc_t;
3129 
3130 int
3131 cpuinfo_walk_cpu(uintptr_t addr, const cpu_t *cpu, cpuinfo_data_t *cid)
3132 {
3133         kthread_t t;
3134         disp_t disp;
3135         mdb_cpuinfo_proc_t p;
3136         uintptr_t pinned;
3137         char **flagbuf;
3138         int nflaglines = 0, flagline = 0, bspl, rval = WALK_NEXT;
3139 
3140         const char *flags[] = {
3141             "RUNNING", "READY", "QUIESCED", "EXISTS",
3142             "ENABLE", "OFFLINE", "POWEROFF", "FROZEN",
3143             "SPARE", "FAULTED", "DISABLED", NULL
3144         };
3145 
3146         if (cid->cid_cpu != -1) {
3147                 if (addr != cid->cid_cpu && cpu->cpu_id != cid->cid_cpu)
3148                         return (WALK_NEXT);
3149 
3150                 /*
3151                  * Set cid_cpu to -1 to indicate that we found a matching CPU.
3152                  */
3153                 cid->cid_cpu = -1;
3154                 rval = WALK_DONE;
3155         }
3156 
3157         if (cid->cid_print_head) {
3158                 mdb_printf("%3s %-*s %3s %4s %4s %3s %4s %5s %-6s %-*s %s\n",
3159                     "ID", CPUINFO_CPUWIDTH, "ADDR", "FLG", "NRUN", "BSPL",
3160                     "PRI", "RNRN", "KRNRN", "SWITCH", CPUINFO_TWIDTH, "THREAD",
3161                     "PROC");
3162                 cid->cid_print_head = FALSE;
3163         }
3164 
3165         bspl = cpu->cpu_base_spl;
3166 
3167         if (mdb_vread(&disp, sizeof (disp_t), (uintptr_t)cpu->cpu_disp) == -1) {
3168                 mdb_warn("failed to read disp_t at %p", cpu->cpu_disp);
3169                 return (WALK_ERR);
3170         }
3171 
3172         mdb_printf("%3d %0*p %3x %4d %4d ",
3173             cpu->cpu_id, CPUINFO_CPUWIDTH, addr, cpu->cpu_flags,
3174             disp.disp_nrunnable, bspl);
3175 
3176         if (mdb_vread(&t, sizeof (t), (uintptr_t)cpu->cpu_thread) != -1) {
3177                 mdb_printf("%3d ", t.t_pri);
3178         } else {
3179                 mdb_printf("%3s ", "-");
3180         }
3181 
3182         mdb_printf("%4s %5s ", cpu->cpu_runrun ? "yes" : "no",
3183             cpu->cpu_kprunrun ? "yes" : "no");
3184 
3185         if (cpu->cpu_last_swtch) {
3186                 mdb_printf("t-%-4d ",
3187                     (clock_t)mdb_get_lbolt() - cpu->cpu_last_swtch);
3188         } else {
3189                 mdb_printf("%-6s ", "-");
3190         }
3191 
3192         mdb_printf("%0*p", CPUINFO_TWIDTH, cpu->cpu_thread);
3193 
3194         if (cpu->cpu_thread == cpu->cpu_idle_thread)
3195                 mdb_printf(" (idle)\n");
3196         else if (cpu->cpu_thread == NULL)
3197                 mdb_printf(" -\n");
3198         else {
3199                 if (mdb_ctf_vread(&p, "proc_t", "mdb_cpuinfo_proc_t",
3200                     (uintptr_t)t.t_procp, 0) != -1) {
3201                         mdb_printf(" %s\n", p.p_user.u_comm);
3202                 } else {
3203                         mdb_printf(" ?\n");
3204                 }
3205         }
3206 
3207         flagbuf = mdb_zalloc(sizeof (flags), UM_SLEEP | UM_GC);
3208 
3209         if (cid->cid_print_flags) {
3210                 int first = 1, i, j, k;
3211                 char *s;
3212 
3213                 cid->cid_print_head = TRUE;
3214 
3215                 for (i = 1, j = 0; flags[j] != NULL; i <<= 1, j++) {
3216                         if (!(cpu->cpu_flags & i))
3217                                 continue;
3218 
3219                         if (first) {
3220                                 s = mdb_alloc(CPUINFO_THRDELT + 1,
3221                                     UM_GC | UM_SLEEP);
3222 
3223                                 (void) mdb_snprintf(s, CPUINFO_THRDELT + 1,
3224                                     "%*s|%*s", CPUINFO_FLAGDELT, "",
3225                                     CPUINFO_THRDELT - 1 - CPUINFO_FLAGDELT, "");
3226                                 flagbuf[nflaglines++] = s;
3227                         }
3228 
3229                         s = mdb_alloc(CPUINFO_THRDELT + 1, UM_GC | UM_SLEEP);
3230                         (void) mdb_snprintf(s, CPUINFO_THRDELT + 1, "%*s%*s %s",
3231                             CPUINFO_IDWIDTH + CPUINFO_CPUWIDTH -
3232                             CPUINFO_FLAGWIDTH, "", CPUINFO_FLAGWIDTH, flags[j],
3233                             first ? "<--+" : "");
3234 
3235                         for (k = strlen(s); k < CPUINFO_THRDELT; k++)
3236                                 s[k] = ' ';
3237                         s[k] = '\0';
3238 
3239                         flagbuf[nflaglines++] = s;
3240                         first = 0;
3241                 }
3242         }
3243 
3244         if (cid->cid_print_ithr) {
3245                 int i, found_one = FALSE;
3246                 int print_thr = disp.disp_nrunnable && cid->cid_print_thr;
3247 
3248                 for (i = NINTR - 1; i >= 0; i--) {
3249                         uintptr_t iaddr = cid->cid_ithr[cpu->cpu_id][i];
3250 
3251                         if (iaddr == 0)
3252                                 continue;
3253 
3254                         if (!found_one) {
3255                                 found_one = TRUE;
3256 
3257                                 CPUINFO_INDENT;
3258                                 mdb_printf("%c%*s|\n", print_thr ? '|' : ' ',
3259                                     CPUINFO_ITHRDELT, "");
3260 
3261                                 CPUINFO_INDENT;
3262                                 mdb_printf("%c%*s+--> %3s %s\n",
3263                                     print_thr ? '|' : ' ', CPUINFO_ITHRDELT,
3264                                     "", "PIL", "THREAD");
3265                         }
3266 
3267                         if (mdb_vread(&t, sizeof (t), iaddr) == -1) {
3268                                 mdb_warn("failed to read kthread_t at %p",
3269                                     iaddr);
3270                                 return (WALK_ERR);
3271                         }
3272 
3273                         CPUINFO_INDENT;
3274                         mdb_printf("%c%*s     %3d %0*p\n",
3275                             print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "",
3276                             t.t_pil, CPUINFO_TWIDTH, iaddr);
3277 
3278                         pinned = (uintptr_t)t.t_intr;
3279                 }
3280 
3281                 if (found_one && pinned != 0) {
3282                         cid->cid_print_head = TRUE;
3283                         (void) strcpy(p.p_user.u_comm, "?");
3284 
3285                         if (mdb_vread(&t, sizeof (t),
3286                             (uintptr_t)pinned) == -1) {
3287                                 mdb_warn("failed to read kthread_t at %p",
3288                                     pinned);
3289                                 return (WALK_ERR);
3290                         }
3291                         if (mdb_ctf_vread(&p, "proc_t", "mdb_cpuinfo_proc_t",
3292                             (uintptr_t)t.t_procp, 0) == -1) {
3293                                 mdb_warn("failed to read proc_t at %p",
3294                                     t.t_procp);
3295                                 return (WALK_ERR);
3296                         }
3297 
3298                         CPUINFO_INDENT;
3299                         mdb_printf("%c%*s     %3s %0*p %s\n",
3300                             print_thr ? '|' : ' ', CPUINFO_ITHRDELT, "", "-",
3301                             CPUINFO_TWIDTH, pinned,
3302                             pinned == (uintptr_t)cpu->cpu_idle_thread ?
3303                             "(idle)" : p.p_user.u_comm);
3304                 }
3305         }
3306 
3307         if (disp.disp_nrunnable && cid->cid_print_thr) {
3308                 dispq_t *dq;
3309 
3310                 int i, npri = disp.disp_npri;
3311 
3312                 dq = mdb_alloc(sizeof (dispq_t) * npri, UM_SLEEP | UM_GC);
3313 
3314                 if (mdb_vread(dq, sizeof (dispq_t) * npri,
3315                     (uintptr_t)disp.disp_q) == -1) {
3316                         mdb_warn("failed to read dispq_t at %p", disp.disp_q);
3317                         return (WALK_ERR);
3318                 }
3319 
3320                 CPUINFO_INDENT;
3321                 mdb_printf("|\n");
3322 
3323                 CPUINFO_INDENT;
3324                 mdb_printf("+-->  %3s %-*s %s\n", "PRI",
3325                     CPUINFO_TWIDTH, "THREAD", "PROC");
3326 
3327                 for (i = npri - 1; i >= 0; i--) {
3328                         uintptr_t taddr = (uintptr_t)dq[i].dq_first;
3329 
3330                         while (taddr != 0) {
3331                                 if (mdb_vread(&t, sizeof (t), taddr) == -1) {
3332                                         mdb_warn("failed to read kthread_t "
3333                                             "at %p", taddr);
3334                                         return (WALK_ERR);
3335                                 }
3336                                 if (mdb_ctf_vread(&p, "proc_t",
3337                                     "mdb_cpuinfo_proc_t",
3338                                     (uintptr_t)t.t_procp, 0) == -1) {
3339                                         mdb_warn("failed to read proc_t at %p",
3340                                             t.t_procp);
3341                                         return (WALK_ERR);
3342                                 }
3343 
3344                                 CPUINFO_INDENT;
3345                                 mdb_printf("      %3d %0*p %s\n", t.t_pri,
3346                                     CPUINFO_TWIDTH, taddr, p.p_user.u_comm);
3347 
3348                                 taddr = (uintptr_t)t.t_link;
3349                         }
3350                 }
3351                 cid->cid_print_head = TRUE;
3352         }
3353 
3354         while (flagline < nflaglines)
3355                 mdb_printf("%s\n", flagbuf[flagline++]);
3356 
3357         if (cid->cid_print_head)
3358                 mdb_printf("\n");
3359 
3360         return (rval);
3361 }
3362 
3363 int
3364 cpuinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3365 {
3366         uint_t verbose = FALSE;
3367         cpuinfo_data_t cid;
3368 
3369         cid.cid_print_ithr = FALSE;
3370         cid.cid_print_thr = FALSE;
3371         cid.cid_print_flags = FALSE;
3372         cid.cid_print_head = DCMD_HDRSPEC(flags) ? TRUE : FALSE;
3373         cid.cid_cpu = -1;
3374 
3375         if (flags & DCMD_ADDRSPEC)
3376                 cid.cid_cpu = addr;
3377 
3378         if (mdb_getopts(argc, argv,
3379             'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
3380                 return (DCMD_USAGE);
3381 
3382         if (verbose) {
3383                 cid.cid_print_ithr = TRUE;
3384                 cid.cid_print_thr = TRUE;
3385                 cid.cid_print_flags = TRUE;
3386                 cid.cid_print_head = TRUE;
3387         }
3388 
3389         if (cid.cid_print_ithr) {
3390                 int i;
3391 
3392                 cid.cid_ithr = mdb_alloc(sizeof (uintptr_t **)
3393                     * NCPU, UM_SLEEP | UM_GC);
3394 
3395                 for (i = 0; i < NCPU; i++)
3396                         cid.cid_ithr[i] = mdb_zalloc(sizeof (uintptr_t *) *
3397                             NINTR, UM_SLEEP | UM_GC);
3398 
3399                 if (mdb_walk("thread", (mdb_walk_cb_t)cpuinfo_walk_ithread,
3400                     &cid) == -1) {
3401                         mdb_warn("couldn't walk thread");
3402                         return (DCMD_ERR);
3403                 }
3404         }
3405 
3406         if (mdb_walk("cpu", (mdb_walk_cb_t)cpuinfo_walk_cpu, &cid) == -1) {
3407                 mdb_warn("can't walk cpus");
3408                 return (DCMD_ERR);
3409         }
3410 
3411         if (cid.cid_cpu != -1) {
3412                 /*
3413                  * We didn't find this CPU when we walked through the CPUs
3414                  * (i.e. the address specified doesn't show up in the "cpu"
3415                  * walk).  However, the specified address may still correspond
3416                  * to a valid cpu_t (for example, if the specified address is
3417                  * the actual panicking cpu_t and not the cached panic_cpu).
3418                  * Point is:  even if we didn't find it, we still want to try
3419                  * to print the specified address as a cpu_t.
3420                  */
3421                 cpu_t cpu;
3422 
3423                 if (mdb_vread(&cpu, sizeof (cpu), cid.cid_cpu) == -1) {
3424                         mdb_warn("%p is neither a valid CPU ID nor a "
3425                             "valid cpu_t address\n", cid.cid_cpu);
3426                         return (DCMD_ERR);
3427                 }
3428 
3429                 (void) cpuinfo_walk_cpu(cid.cid_cpu, &cpu, &cid);
3430         }
3431 
3432         return (DCMD_OK);
3433 }
3434 
3435 /*ARGSUSED*/
3436 int
3437 flipone(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3438 {
3439         int i;
3440 
3441         if (!(flags & DCMD_ADDRSPEC))
3442                 return (DCMD_USAGE);
3443 
3444         for (i = 0; i < sizeof (addr) * NBBY; i++)
3445                 mdb_printf("%p\n", addr ^ (1UL << i));
3446 
3447         return (DCMD_OK);
3448 }
3449 
3450 typedef struct mdb_as2proc_proc {
3451         struct as *p_as;
3452 } mdb_as2proc_proc_t;
3453 
3454 /*ARGSUSED*/
3455 int
3456 as2proc_walk(uintptr_t addr, const void *ignored, struct as **asp)
3457 {
3458         mdb_as2proc_proc_t p;
3459 
3460         mdb_ctf_vread(&p, "proc_t", "mdb_as2proc_proc_t", addr, 0);
3461 
3462         if (p.p_as == *asp)
3463                 mdb_printf("%p\n", addr);
3464         return (WALK_NEXT);
3465 }
3466 
3467 /*ARGSUSED*/
3468 int
3469 as2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3470 {
3471         if (!(flags & DCMD_ADDRSPEC) || argc != 0)
3472                 return (DCMD_USAGE);
3473 
3474         if (mdb_walk("proc", (mdb_walk_cb_t)as2proc_walk, &addr) == -1) {
3475                 mdb_warn("failed to walk proc");
3476                 return (DCMD_ERR);
3477         }
3478 
3479         return (DCMD_OK);
3480 }
3481 
3482 typedef struct mdb_ptree_proc {
3483         struct proc     *p_parent;
3484         struct {
3485                 char            u_comm[MAXCOMLEN + 1];
3486         } p_user;
3487 } mdb_ptree_proc_t;
3488 
3489 /*ARGSUSED*/
3490 int
3491 ptree_walk(uintptr_t addr, const void *ignored, void *data)
3492 {
3493         mdb_ptree_proc_t proc;
3494         mdb_ptree_proc_t parent;
3495         int ident = 0;
3496         uintptr_t paddr;
3497 
3498         mdb_ctf_vread(&proc, "proc_t", "mdb_ptree_proc_t", addr, 0);
3499 
3500         for (paddr = (uintptr_t)proc.p_parent; paddr != 0; ident += 5) {
3501                 mdb_ctf_vread(&parent, "proc_t", "mdb_ptree_proc_t", paddr, 0);
3502                 paddr = (uintptr_t)parent.p_parent;
3503         }
3504 
3505         mdb_inc_indent(ident);
3506         mdb_printf("%0?p  %s\n", addr, proc.p_user.u_comm);
3507         mdb_dec_indent(ident);
3508 
3509         return (WALK_NEXT);
3510 }
3511 
3512 void
3513 ptree_ancestors(uintptr_t addr, uintptr_t start)
3514 {
3515         mdb_ptree_proc_t p;
3516 
3517         if (mdb_ctf_vread(&p, "proc_t", "mdb_ptree_proc_t", addr, 0) == -1) {
3518                 mdb_warn("couldn't read ancestor at %p", addr);
3519                 return;
3520         }
3521 
3522         if (p.p_parent != NULL)
3523                 ptree_ancestors((uintptr_t)p.p_parent, start);
3524 
3525         if (addr != start)
3526                 (void) ptree_walk(addr, &p, NULL);
3527 }
3528 
3529 /*ARGSUSED*/
3530 int
3531 ptree(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3532 {
3533         if (!(flags & DCMD_ADDRSPEC))
3534                 addr = 0;
3535         else
3536                 ptree_ancestors(addr, addr);
3537 
3538         if (mdb_pwalk("proc", (mdb_walk_cb_t)ptree_walk, NULL, addr) == -1) {
3539                 mdb_warn("couldn't walk 'proc'");
3540                 return (DCMD_ERR);
3541         }
3542 
3543         return (DCMD_OK);
3544 }
3545 
3546 typedef struct mdb_fd_proc {
3547         struct {
3548                 struct {
3549                         int                     fi_nfiles;
3550                         uf_entry_t *volatile    fi_list;
3551                 } u_finfo;
3552         } p_user;
3553 } mdb_fd_proc_t;
3554 
3555 /*ARGSUSED*/
3556 static int
3557 fd(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3558 {
3559         int fdnum;
3560         const mdb_arg_t *argp = &argv[0];
3561         mdb_fd_proc_t p;
3562         uf_entry_t uf;
3563 
3564         if ((flags & DCMD_ADDRSPEC) == 0) {
3565                 mdb_warn("fd doesn't give global information\n");
3566                 return (DCMD_ERR);
3567         }
3568         if (argc != 1)
3569                 return (DCMD_USAGE);
3570 
3571         if (argp->a_type == MDB_TYPE_IMMEDIATE)
3572                 fdnum = argp->a_un.a_val;
3573         else
3574                 fdnum = mdb_strtoull(argp->a_un.a_str);
3575 
3576         if (mdb_ctf_vread(&p, "proc_t", "mdb_fd_proc_t", addr, 0) == -1) {
3577                 mdb_warn("couldn't read proc_t at %p", addr);
3578                 return (DCMD_ERR);
3579         }
3580         if (fdnum > p.p_user.u_finfo.fi_nfiles) {
3581                 mdb_warn("process %p only has %d files open.\n",
3582                     addr, p.p_user.u_finfo.fi_nfiles);
3583                 return (DCMD_ERR);
3584         }
3585         if (mdb_vread(&uf, sizeof (uf_entry_t),
3586             (uintptr_t)&p.p_user.u_finfo.fi_list[fdnum]) == -1) {
3587                 mdb_warn("couldn't read uf_entry_t at %p",
3588                     &p.p_user.u_finfo.fi_list[fdnum]);
3589                 return (DCMD_ERR);
3590         }
3591 
3592         mdb_printf("%p\n", uf.uf_file);
3593         return (DCMD_OK);
3594 }
3595 
3596 /*ARGSUSED*/
3597 static int
3598 pid2proc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3599 {
3600         pid_t pid = (pid_t)addr;
3601 
3602         if (argc != 0)
3603                 return (DCMD_USAGE);
3604 
3605         if ((addr = mdb_pid2proc(pid, NULL)) == 0) {
3606                 mdb_warn("PID 0t%d not found\n", pid);
3607                 return (DCMD_ERR);
3608         }
3609 
3610         mdb_printf("%p\n", addr);
3611         return (DCMD_OK);
3612 }
3613 
3614 static char *sysfile_cmd[] = {
3615         "exclude:",
3616         "include:",
3617         "forceload:",
3618         "rootdev:",
3619         "rootfs:",
3620         "swapdev:",
3621         "swapfs:",
3622         "moddir:",
3623         "set",
3624         "unknown",
3625 };
3626 
3627 static char *sysfile_ops[] = { "", "=", "&", "|" };
3628 
3629 /*ARGSUSED*/
3630 static int
3631 sysfile_vmem_seg(uintptr_t addr, const vmem_seg_t *vsp, void **target)
3632 {
3633         if (vsp->vs_type == VMEM_ALLOC && (void *)vsp->vs_start == *target) {
3634                 *target = NULL;
3635                 return (WALK_DONE);
3636         }
3637         return (WALK_NEXT);
3638 }
3639 
3640 /*ARGSUSED*/
3641 static int
3642 sysfile(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3643 {
3644         struct sysparam *sysp, sys;
3645         char var[256];
3646         char modname[256];
3647         char val[256];
3648         char strval[256];
3649         vmem_t *mod_sysfile_arena;
3650         void *straddr;
3651 
3652         if (mdb_readvar(&sysp, "sysparam_hd") == -1) {
3653                 mdb_warn("failed to read sysparam_hd");
3654                 return (DCMD_ERR);
3655         }
3656 
3657         if (mdb_readvar(&mod_sysfile_arena, "mod_sysfile_arena") == -1) {
3658                 mdb_warn("failed to read mod_sysfile_arena");
3659                 return (DCMD_ERR);
3660         }
3661 
3662         while (sysp != NULL) {
3663                 var[0] = '\0';
3664                 val[0] = '\0';
3665                 modname[0] = '\0';
3666                 if (mdb_vread(&sys, sizeof (sys), (uintptr_t)sysp) == -1) {
3667                         mdb_warn("couldn't read sysparam %p", sysp);
3668                         return (DCMD_ERR);
3669                 }
3670                 if (sys.sys_modnam != NULL &&
3671                     mdb_readstr(modname, 256,
3672                     (uintptr_t)sys.sys_modnam) == -1) {
3673                         mdb_warn("couldn't read modname in %p", sysp);
3674                         return (DCMD_ERR);
3675                 }
3676                 if (sys.sys_ptr != NULL &&
3677                     mdb_readstr(var, 256, (uintptr_t)sys.sys_ptr) == -1) {
3678                         mdb_warn("couldn't read ptr in %p", sysp);
3679                         return (DCMD_ERR);
3680                 }
3681                 if (sys.sys_op != SETOP_NONE) {
3682                         /*
3683                          * Is this an int or a string?  We determine this
3684                          * by checking whether straddr is contained in
3685                          * mod_sysfile_arena.  If so, the walker will set
3686                          * straddr to NULL.
3687                          */
3688                         straddr = (void *)(uintptr_t)sys.sys_info;
3689                         if (sys.sys_op == SETOP_ASSIGN &&
3690                             sys.sys_info != 0 &&
3691                             mdb_pwalk("vmem_seg",
3692                             (mdb_walk_cb_t)sysfile_vmem_seg, &straddr,
3693                             (uintptr_t)mod_sysfile_arena) == 0 &&
3694                             straddr == NULL &&
3695                             mdb_readstr(strval, 256,
3696                             (uintptr_t)sys.sys_info) != -1) {
3697                                 (void) mdb_snprintf(val, sizeof (val), "\"%s\"",
3698                                     strval);
3699                         } else {
3700                                 (void) mdb_snprintf(val, sizeof (val),
3701                                     "0x%llx [0t%llu]", sys.sys_info,
3702                                     sys.sys_info);
3703                         }
3704                 }
3705                 mdb_printf("%s %s%s%s%s%s\n", sysfile_cmd[sys.sys_type],
3706                     modname, modname[0] == '\0' ? "" : ":",
3707                     var, sysfile_ops[sys.sys_op], val);
3708 
3709                 sysp = sys.sys_next;
3710         }
3711 
3712         return (DCMD_OK);
3713 }
3714 
3715 int
3716 didmatch(uintptr_t addr, const kthread_t *thr, kt_did_t *didp)
3717 {
3718 
3719         if (*didp == thr->t_did) {
3720                 mdb_printf("%p\n", addr);
3721                 return (WALK_DONE);
3722         } else
3723                 return (WALK_NEXT);
3724 }
3725 
3726 /*ARGSUSED*/
3727 int
3728 did2thread(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3729 {
3730         const mdb_arg_t *argp = &argv[0];
3731         kt_did_t        did;
3732 
3733         if (argc != 1)
3734                 return (DCMD_USAGE);
3735 
3736         did = (kt_did_t)mdb_strtoull(argp->a_un.a_str);
3737 
3738         if (mdb_walk("thread", (mdb_walk_cb_t)didmatch, (void *)&did) == -1) {
3739                 mdb_warn("failed to walk thread");
3740                 return (DCMD_ERR);
3741 
3742         }
3743         return (DCMD_OK);
3744 
3745 }
3746 
3747 static int
3748 errorq_walk_init(mdb_walk_state_t *wsp)
3749 {
3750         if (wsp->walk_addr == 0 &&
3751             mdb_readvar(&wsp->walk_addr, "errorq_list") == -1) {
3752                 mdb_warn("failed to read errorq_list");
3753                 return (WALK_ERR);
3754         }
3755 
3756         return (WALK_NEXT);
3757 }
3758 
3759 static int
3760 errorq_walk_step(mdb_walk_state_t *wsp)
3761 {
3762         uintptr_t addr = wsp->walk_addr;
3763         errorq_t eq;
3764 
3765         if (addr == 0)
3766                 return (WALK_DONE);
3767 
3768         if (mdb_vread(&eq, sizeof (eq), addr) == -1) {
3769                 mdb_warn("failed to read errorq at %p", addr);
3770                 return (WALK_ERR);
3771         }
3772 
3773         wsp->walk_addr = (uintptr_t)eq.eq_next;
3774         return (wsp->walk_callback(addr, &eq, wsp->walk_cbdata));
3775 }
3776 
3777 typedef struct eqd_walk_data {
3778         uintptr_t *eqd_stack;
3779         void *eqd_buf;
3780         ulong_t eqd_qpos;
3781         ulong_t eqd_qlen;
3782         size_t eqd_size;
3783 } eqd_walk_data_t;
3784 
3785 /*
3786  * In order to walk the list of pending error queue elements, we push the
3787  * addresses of the corresponding data buffers in to the eqd_stack array.
3788  * The error lists are in reverse chronological order when iterating using
3789  * eqe_prev, so we then pop things off the top in eqd_walk_step so that the
3790  * walker client gets addresses in order from oldest error to newest error.
3791  */
3792 static void
3793 eqd_push_list(eqd_walk_data_t *eqdp, uintptr_t addr)
3794 {
3795         errorq_elem_t eqe;
3796 
3797         while (addr != 0) {
3798                 if (mdb_vread(&eqe, sizeof (eqe), addr) != sizeof (eqe)) {
3799                         mdb_warn("failed to read errorq element at %p", addr);
3800                         break;
3801                 }
3802 
3803                 if (eqdp->eqd_qpos == eqdp->eqd_qlen) {
3804                         mdb_warn("errorq is overfull -- more than %lu "
3805                             "elems found\n", eqdp->eqd_qlen);
3806                         break;
3807                 }
3808 
3809                 eqdp->eqd_stack[eqdp->eqd_qpos++] = (uintptr_t)eqe.eqe_data;
3810                 addr = (uintptr_t)eqe.eqe_prev;
3811         }
3812 }
3813 
3814 static int
3815 eqd_walk_init(mdb_walk_state_t *wsp)
3816 {
3817         eqd_walk_data_t *eqdp;
3818         errorq_elem_t eqe, *addr;
3819         errorq_t eq;
3820         ulong_t i;
3821 
3822         if (mdb_vread(&eq, sizeof (eq), wsp->walk_addr) == -1) {
3823                 mdb_warn("failed to read errorq at %p", wsp->walk_addr);
3824                 return (WALK_ERR);
3825         }
3826 
3827         if (eq.eq_ptail != NULL &&
3828             mdb_vread(&eqe, sizeof (eqe), (uintptr_t)eq.eq_ptail) == -1) {
3829                 mdb_warn("failed to read errorq element at %p", eq.eq_ptail);
3830                 return (WALK_ERR);
3831         }
3832 
3833         eqdp = mdb_alloc(sizeof (eqd_walk_data_t), UM_SLEEP);
3834         wsp->walk_data = eqdp;
3835 
3836         eqdp->eqd_stack = mdb_zalloc(sizeof (uintptr_t) * eq.eq_qlen, UM_SLEEP);
3837         eqdp->eqd_buf = mdb_alloc(eq.eq_size, UM_SLEEP);
3838         eqdp->eqd_qlen = eq.eq_qlen;
3839         eqdp->eqd_qpos = 0;
3840         eqdp->eqd_size = eq.eq_size;
3841 
3842         /*
3843          * The newest elements in the queue are on the pending list, so we
3844          * push those on to our stack first.
3845          */
3846         eqd_push_list(eqdp, (uintptr_t)eq.eq_pend);
3847 
3848         /*
3849          * If eq_ptail is set, it may point to a subset of the errors on the
3850          * pending list in the event a atomic_cas_ptr() failed; if ptail's
3851          * data is already in our stack, NULL out eq_ptail and ignore it.
3852          */
3853         if (eq.eq_ptail != NULL) {
3854                 for (i = 0; i < eqdp->eqd_qpos; i++) {
3855                         if (eqdp->eqd_stack[i] == (uintptr_t)eqe.eqe_data) {
3856                                 eq.eq_ptail = NULL;
3857                                 break;
3858                         }
3859                 }
3860         }
3861 
3862         /*
3863          * If eq_phead is set, it has the processing list in order from oldest
3864          * to newest.  Use this to recompute eq_ptail as best we can and then
3865          * we nicely fall into eqd_push_list() of eq_ptail below.
3866          */
3867         for (addr = eq.eq_phead; addr != NULL && mdb_vread(&eqe, sizeof (eqe),
3868             (uintptr_t)addr) == sizeof (eqe); addr = eqe.eqe_next)
3869                 eq.eq_ptail = addr;
3870 
3871         /*
3872          * The oldest elements in the queue are on the processing list, subject
3873          * to machinations in the if-clauses above.  Push any such elements.
3874          */
3875         eqd_push_list(eqdp, (uintptr_t)eq.eq_ptail);
3876         return (WALK_NEXT);
3877 }
3878 
3879 static int
3880 eqd_walk_step(mdb_walk_state_t *wsp)
3881 {
3882         eqd_walk_data_t *eqdp = wsp->walk_data;
3883         uintptr_t addr;
3884 
3885         if (eqdp->eqd_qpos == 0)
3886                 return (WALK_DONE);
3887 
3888         addr = eqdp->eqd_stack[--eqdp->eqd_qpos];
3889 
3890         if (mdb_vread(eqdp->eqd_buf, eqdp->eqd_size, addr) != eqdp->eqd_size) {
3891                 mdb_warn("failed to read errorq data at %p", addr);
3892                 return (WALK_ERR);
3893         }
3894 
3895         return (wsp->walk_callback(addr, eqdp->eqd_buf, wsp->walk_cbdata));
3896 }
3897 
3898 static void
3899 eqd_walk_fini(mdb_walk_state_t *wsp)
3900 {
3901         eqd_walk_data_t *eqdp = wsp->walk_data;
3902 
3903         mdb_free(eqdp->eqd_stack, sizeof (uintptr_t) * eqdp->eqd_qlen);
3904         mdb_free(eqdp->eqd_buf, eqdp->eqd_size);
3905         mdb_free(eqdp, sizeof (eqd_walk_data_t));
3906 }
3907 
3908 #define EQKSVAL(eqv, what) (eqv.eq_kstat.what.value.ui64)
3909 
3910 static int
3911 errorq(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3912 {
3913         int i;
3914         errorq_t eq;
3915         uint_t opt_v = FALSE;
3916 
3917         if (!(flags & DCMD_ADDRSPEC)) {
3918                 if (mdb_walk_dcmd("errorq", "errorq", argc, argv) == -1) {
3919                         mdb_warn("can't walk 'errorq'");
3920                         return (DCMD_ERR);
3921                 }
3922                 return (DCMD_OK);
3923         }
3924 
3925         i = mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL);
3926         argc -= i;
3927         argv += i;
3928 
3929         if (argc != 0)
3930                 return (DCMD_USAGE);
3931 
3932         if (opt_v || DCMD_HDRSPEC(flags)) {
3933                 mdb_printf("%<u>%-11s %-16s %1s %1s %1s ",
3934                     "ADDR", "NAME", "S", "V", "N");
3935                 if (!opt_v) {
3936                         mdb_printf("%7s %7s %7s%</u>\n",
3937                             "ACCEPT", "DROP", "LOG");
3938                 } else {
3939                         mdb_printf("%5s %6s %6s %3s %16s%</u>\n",
3940                             "KSTAT", "QLEN", "SIZE", "IPL", "FUNC");
3941                 }
3942         }
3943 
3944         if (mdb_vread(&eq, sizeof (eq), addr) != sizeof (eq)) {
3945                 mdb_warn("failed to read errorq at %p", addr);
3946                 return (DCMD_ERR);
3947         }
3948 
3949         mdb_printf("%-11p %-16s %c %c %c ", addr, eq.eq_name,
3950             (eq.eq_flags & ERRORQ_ACTIVE) ? '+' : '-',
3951             (eq.eq_flags & ERRORQ_VITAL) ? '!' : ' ',
3952             (eq.eq_flags & ERRORQ_NVLIST) ? '*' : ' ');
3953 
3954         if (!opt_v) {
3955                 mdb_printf("%7llu %7llu %7llu\n",
3956                     EQKSVAL(eq, eqk_dispatched) + EQKSVAL(eq, eqk_committed),
3957                     EQKSVAL(eq, eqk_dropped) + EQKSVAL(eq, eqk_reserve_fail) +
3958                     EQKSVAL(eq, eqk_commit_fail), EQKSVAL(eq, eqk_logged));
3959         } else {
3960                 mdb_printf("%5s %6lu %6lu %3u %a\n",
3961                     "  |  ", eq.eq_qlen, eq.eq_size, eq.eq_ipl, eq.eq_func);
3962                 mdb_printf("%38s\n%41s"
3963                     "%12s %llu\n"
3964                     "%53s %llu\n"
3965                     "%53s %llu\n"
3966                     "%53s %llu\n"
3967                     "%53s %llu\n"
3968                     "%53s %llu\n"
3969                     "%53s %llu\n"
3970                     "%53s %llu\n\n",
3971                     "|", "+-> ",
3972                     "DISPATCHED",       EQKSVAL(eq, eqk_dispatched),
3973                     "DROPPED",          EQKSVAL(eq, eqk_dropped),
3974                     "LOGGED",           EQKSVAL(eq, eqk_logged),
3975                     "RESERVED",         EQKSVAL(eq, eqk_reserved),
3976                     "RESERVE FAIL",     EQKSVAL(eq, eqk_reserve_fail),
3977                     "COMMITTED",        EQKSVAL(eq, eqk_committed),
3978                     "COMMIT FAIL",      EQKSVAL(eq, eqk_commit_fail),
3979                     "CANCELLED",        EQKSVAL(eq, eqk_cancelled));
3980         }
3981 
3982         return (DCMD_OK);
3983 }
3984 
3985 /*ARGSUSED*/
3986 static int
3987 panicinfo(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3988 {
3989         cpu_t panic_cpu;
3990         kthread_t *panic_thread;
3991         void *buf;
3992         panic_data_t *pd;
3993         int i, n;
3994 
3995         if (!mdb_prop_postmortem) {
3996                 mdb_warn("panicinfo can only be run on a system "
3997                     "dump; see dumpadm(1M)\n");
3998                 return (DCMD_ERR);
3999         }
4000 
4001         if (flags & DCMD_ADDRSPEC || argc != 0)
4002                 return (DCMD_USAGE);
4003 
4004         if (mdb_readsym(&panic_cpu, sizeof (cpu_t), "panic_cpu") == -1)
4005                 mdb_warn("failed to read 'panic_cpu'");
4006         else
4007                 mdb_printf("%16s %?d\n", "cpu", panic_cpu.cpu_id);
4008 
4009         if (mdb_readvar(&panic_thread, "panic_thread") == -1)
4010                 mdb_warn("failed to read 'panic_thread'");
4011         else
4012                 mdb_printf("%16s %?p\n", "thread", panic_thread);
4013 
4014         buf = mdb_alloc(PANICBUFSIZE, UM_SLEEP);
4015         pd = (panic_data_t *)buf;
4016 
4017         if (mdb_readsym(buf, PANICBUFSIZE, "panicbuf") == -1 ||
4018             pd->pd_version != PANICBUFVERS) {
4019                 mdb_warn("failed to read 'panicbuf'");
4020                 mdb_free(buf, PANICBUFSIZE);
4021                 return (DCMD_ERR);
4022         }
4023 
4024         mdb_printf("%16s %s\n", "message",  (char *)buf + pd->pd_msgoff);
4025 
4026         n = (pd->pd_msgoff - (sizeof (panic_data_t) -
4027             sizeof (panic_nv_t))) / sizeof (panic_nv_t);
4028 
4029         for (i = 0; i < n; i++)
4030                 mdb_printf("%16s %?llx\n",
4031                     pd->pd_nvdata[i].pnv_name, pd->pd_nvdata[i].pnv_value);
4032 
4033         mdb_free(buf, PANICBUFSIZE);
4034         return (DCMD_OK);
4035 }
4036 
4037 /*
4038  * ::time dcmd, which will print a hires timestamp of when we entered the
4039  * debugger, or the lbolt value if used with the -l option.
4040  *
4041  */
4042 /*ARGSUSED*/
4043 static int
4044 time(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4045 {
4046         uint_t opt_dec = FALSE;
4047         uint_t opt_lbolt = FALSE;
4048         uint_t opt_hex = FALSE;
4049         const char *fmt;
4050         hrtime_t result;
4051 
4052         if (mdb_getopts(argc, argv,
4053             'd', MDB_OPT_SETBITS, TRUE, &opt_dec,
4054             'l', MDB_OPT_SETBITS, TRUE, &opt_lbolt,
4055             'x', MDB_OPT_SETBITS, TRUE, &opt_hex,
4056             NULL) != argc)
4057                 return (DCMD_USAGE);
4058 
4059         if (opt_dec && opt_hex)
4060                 return (DCMD_USAGE);
4061 
4062         result = opt_lbolt ? mdb_get_lbolt() : mdb_gethrtime();
4063         fmt =
4064             opt_hex ? "0x%llx\n" :
4065             opt_dec ? "0t%lld\n" : "%#llr\n";
4066 
4067         mdb_printf(fmt, result);
4068         return (DCMD_OK);
4069 }
4070 
4071 void
4072 time_help(void)
4073 {
4074         mdb_printf("Prints the system time in nanoseconds.\n\n"
4075             "::time will return the timestamp at which we dropped into, \n"
4076             "if called from, kmdb(1); the core dump's high resolution \n"
4077             "time if inspecting one; or the running hires time if we're \n"
4078             "looking at a live system.\n\n"
4079             "Switches:\n"
4080             "  -d   report times in decimal\n"
4081             "  -l   prints the number of clock ticks since system boot\n"
4082             "  -x   report times in hexadecimal\n");
4083 }
4084 
4085 extern int cmd_refstr(uintptr_t, uint_t, int, const mdb_arg_t *);
4086 
4087 static const mdb_dcmd_t dcmds[] = {
4088 
4089         /* from genunix.c */
4090         { "as2proc", ":", "convert as to proc_t address", as2proc },
4091         { "binding_hash_entry", ":", "print driver names hash table entry",
4092                 binding_hash_entry },
4093         { "callout", "?[-r|n] [-s|l] [-xhB] [-t | -ab nsec [-dkD]]"
4094             " [-C addr | -S seqid] [-f name|addr] [-p name| addr] [-T|L [-E]]"
4095             " [-FivVA]",
4096             "display callouts", callout, callout_help },
4097         { "calloutid", "[-d|v] xid", "print callout by extended id",
4098             calloutid, calloutid_help },
4099         { "class", NULL, "print process scheduler classes", class },
4100         { "cpuinfo", "?[-v]", "print CPUs and runnable threads", cpuinfo },
4101         { "did2thread", "? kt_did", "find kernel thread for this id",
4102                 did2thread },
4103         { "errorq", "?[-v]", "display kernel error queues", errorq },
4104         { "fd", ":[fd num]", "get a file pointer from an fd", fd },
4105         { "flipone", ":", "the vik_rev_level 2 special", flipone },
4106         { "lminfo", NULL, "print lock manager information", lminfo },
4107         { "ndi_event_hdl", "?", "print ndi_event_hdl", ndi_event_hdl },
4108         { "panicinfo", NULL, "print panic information", panicinfo },
4109         { "pid2proc", "?", "convert PID to proc_t address", pid2proc },
4110         { "project", NULL, "display kernel project(s)", project },
4111         { "ps", "[-fltzTP]", "list processes (and associated thr,lwp)", ps,
4112             ps_help },
4113         { "pflags", NULL, "display various proc_t flags", pflags },
4114         { "pgrep", "[-x] [-n | -o] pattern",
4115                 "pattern match against all processes", pgrep },
4116         { "ptree", NULL, "print process tree", ptree },
4117         { "refstr", NULL, "print string from a refstr_t", cmd_refstr, NULL },
4118         { "sysevent", "?[-sv]", "print sysevent pending or sent queue",
4119                 sysevent},
4120         { "sysevent_channel", "?", "print sysevent channel database",
4121                 sysevent_channel},
4122         { "sysevent_class_list", ":", "print sysevent class list",
4123                 sysevent_class_list},
4124         { "sysevent_subclass_list", ":",
4125                 "print sysevent subclass list", sysevent_subclass_list},
4126         { "system", NULL, "print contents of /etc/system file", sysfile },
4127         { "task", NULL, "display kernel task(s)", task },
4128         { "time", "[-dlx]", "display system time", time, time_help },
4129         { "vnode2path", ":[-F]", "vnode address to pathname", vnode2path },
4130         { "whereopen", ":", "given a vnode, dumps procs which have it open",
4131             whereopen },
4132 
4133         /* from bio.c */
4134         { "bufpagefind", ":addr", "find page_t on buf_t list", bufpagefind },
4135 
4136         /* from bitset.c */
4137         { "bitset", ":", "display a bitset", bitset, bitset_help },
4138 
4139         /* from contract.c */
4140         { "contract", "?", "display a contract", cmd_contract },
4141         { "ctevent", ":", "display a contract event", cmd_ctevent },
4142         { "ctid", ":", "convert id to a contract pointer", cmd_ctid },
4143 
4144         /* from cpupart.c */
4145         { "cpupart", "?[-v]", "print cpu partition info", cpupart },
4146 
4147         /* from cred.c */
4148         { "cred", ":[-v]", "display a credential", cmd_cred },
4149         { "credgrp", ":[-v]", "display cred_t groups", cmd_credgrp },
4150         { "credsid", ":[-v]", "display a credsid_t", cmd_credsid },
4151         { "ksidlist", ":[-v]", "display a ksidlist_t", cmd_ksidlist },
4152 
4153         /* from cyclic.c */
4154         { "cyccover", NULL, "dump cyclic coverage information", cyccover },
4155         { "cycid", "?", "dump a cyclic id", cycid },
4156         { "cycinfo", "?", "dump cyc_cpu info", cycinfo },
4157         { "cyclic", ":", "developer information", cyclic },
4158         { "cyctrace", "?", "dump cyclic trace buffer", cyctrace },
4159 
4160         /* from damap.c */
4161         { "damap", ":", "display a damap_t", damap, damap_help },
4162 
4163         /* from ddi_periodic.c */
4164         { "ddi_periodic", "?[-v]", "dump ddi_periodic_impl_t info", dprinfo },
4165 
4166         /* from devinfo.c */
4167         { "devbindings", "?[-qs] [device-name | major-num]",
4168             "print devinfo nodes bound to device-name or major-num",
4169             devbindings, devinfo_help },
4170         { "devinfo", ":[-qsd] [-b bus]", "detailed devinfo of one node",
4171             devinfo, devinfo_help },
4172         { "devinfo_audit", ":[-v]", "devinfo configuration audit record",
4173             devinfo_audit },
4174         { "devinfo_audit_log", "?[-v]", "system wide devinfo configuration log",
4175             devinfo_audit_log },
4176         { "devinfo_audit_node", ":[-v]", "devinfo node configuration history",
4177             devinfo_audit_node },
4178         { "devinfo2driver", ":", "find driver name for this devinfo node",
4179             devinfo2driver },
4180         { "devnames", "?[-vm] [num]", "print devnames array", devnames },
4181         { "dev2major", "?<dev_t>", "convert dev_t to a major number",
4182             dev2major },
4183         { "dev2minor", "?<dev_t>", "convert dev_t to a minor number",
4184             dev2minor },
4185         { "devt", "?<dev_t>", "display a dev_t's major and minor numbers",
4186             devt },
4187         { "major2name", "?<major-num>", "convert major number to dev name",
4188             major2name },
4189         { "minornodes", ":", "given a devinfo node, print its minor nodes",
4190             minornodes },
4191         { "modctl2devinfo", ":", "given a modctl, list its devinfos",
4192             modctl2devinfo },
4193         { "name2major", "<dev-name>", "convert dev name to major number",
4194             name2major },
4195         { "prtconf", "?[-vpc] [-d driver] [-i inst]", "print devinfo tree",
4196             prtconf, prtconf_help },
4197         { "softstate", ":<instance>", "retrieve soft-state pointer",
4198             softstate },
4199         { "devinfo_fm", ":", "devinfo fault managment configuration",
4200             devinfo_fm },
4201         { "devinfo_fmce", ":", "devinfo fault managment cache entry",
4202             devinfo_fmce},
4203 
4204         /* from findstack.c */
4205         { "findstack", ":[-v]", "find kernel thread stack", findstack },
4206         { "findstack_debug", NULL, "toggle findstack debugging",
4207                 findstack_debug },
4208         { "stacks", "?[-afiv] [-c func] [-C func] [-m module] [-M module] "
4209                 "[-s sobj | -S sobj] [-t tstate | -T tstate]",
4210                 "print unique kernel thread stacks",
4211                 stacks, stacks_help },
4212 
4213         /* from fm.c */
4214         { "ereport", "[-v]", "print ereports logged in dump",
4215             ereport },
4216 
4217         /* from group.c */
4218         { "group", "?[-q]", "display a group", group},
4219 
4220         /* from hotplug.c */
4221         { "hotplug", "?[-p]", "display a registered hotplug attachment",
4222             hotplug, hotplug_help },
4223 
4224         /* from irm.c */
4225         { "irmpools", NULL, "display interrupt pools", irmpools_dcmd },
4226         { "irmreqs", NULL, "display interrupt requests in an interrupt pool",
4227             irmreqs_dcmd },
4228         { "irmreq", NULL, "display an interrupt request", irmreq_dcmd },
4229 
4230         /* from kgrep.c + genunix.c */
4231         { "kgrep", KGREP_USAGE, "search kernel as for a pointer", kgrep,
4232                 kgrep_help },
4233 
4234         /* from kmem.c */
4235         { "allocdby", ":", "given a thread, print its allocated buffers",
4236                 allocdby },
4237         { "bufctl", ":[-vh] [-a addr] [-c caller] [-e earliest] [-l latest] "
4238                 "[-t thd]", "print or filter a bufctl", bufctl, bufctl_help },
4239         { "freedby", ":", "given a thread, print its freed buffers", freedby },
4240         { "kmalog", "?[ fail | slab ]",
4241             "display kmem transaction log and stack traces", kmalog },
4242         { "kmastat", "[-kmg]", "kernel memory allocator stats",
4243             kmastat },
4244         { "kmausers", "?[-ef] [cache ...]", "current medium and large users "
4245                 "of the kmem allocator", kmausers, kmausers_help },
4246         { "kmem_cache", "?[-n name]",
4247                 "print kernel memory caches", kmem_cache, kmem_cache_help},
4248         { "kmem_slabs", "?[-v] [-n cache] [-N cache] [-b maxbins] "
4249                 "[-B minbinsize]", "display slab usage per kmem cache",
4250                 kmem_slabs, kmem_slabs_help },
4251         { "kmem_debug", NULL, "toggle kmem dcmd/walk debugging", kmem_debug },
4252         { "kmem_log", "?[-b]", "dump kmem transaction log", kmem_log },
4253         { "kmem_verify", "?", "check integrity of kmem-managed memory",
4254                 kmem_verify },
4255         { "vmem", "?", "print a vmem_t", vmem },
4256         { "vmem_seg", ":[-sv] [-c caller] [-e earliest] [-l latest] "
4257                 "[-m minsize] [-M maxsize] [-t thread] [-T type]",
4258                 "print or filter a vmem_seg", vmem_seg, vmem_seg_help },
4259         { "whatthread", ":[-v]", "print threads whose stack contains the "
4260                 "given address", whatthread },
4261 
4262         /* from ldi.c */
4263         { "ldi_handle", "?[-i]", "display a layered driver handle",
4264             ldi_handle, ldi_handle_help },
4265         { "ldi_ident", NULL, "display a layered driver identifier",
4266             ldi_ident, ldi_ident_help },
4267 
4268         /* from leaky.c + leaky_subr.c */
4269         { "findleaks", FINDLEAKS_USAGE,
4270             "search for potential kernel memory leaks", findleaks,
4271             findleaks_help },
4272 
4273         /* from lgrp.c */
4274         { "lgrp", "?[-q] [-p | -Pih]", "display an lgrp", lgrp},
4275         { "lgrp_set", "", "display bitmask of lgroups as a list", lgrp_set},
4276 
4277         /* from log.c */
4278         { "msgbuf", "?[-v]", "print most recent console messages", msgbuf },
4279 
4280         /* from mdi.c */
4281         { "mdipi", NULL, "given a path, dump mdi_pathinfo "
4282                 "and detailed pi_prop list", mdipi },
4283         { "mdiprops", NULL, "given a pi_prop, dump the pi_prop list",
4284                 mdiprops },
4285         { "mdiphci", NULL, "given a phci, dump mdi_phci and "
4286                 "list all paths", mdiphci },
4287         { "mdivhci", NULL, "given a vhci, dump mdi_vhci and list "
4288                 "all phcis", mdivhci },
4289         { "mdiclient_paths", NULL, "given a path, walk mdi_pathinfo "
4290                 "client links", mdiclient_paths },
4291         { "mdiphci_paths", NULL, "given a path, walk through mdi_pathinfo "
4292                 "phci links", mdiphci_paths },
4293         { "mdiphcis", NULL, "given a phci, walk through mdi_phci ph_next links",
4294                 mdiphcis },
4295 
4296         /* from memory.c */
4297         { "addr2smap", ":[offset]", "translate address to smap", addr2smap },
4298         { "memlist", "?[-iav]", "display a struct memlist", memlist },
4299         { "memstat", NULL, "display memory usage summary", memstat },
4300         { "page", "?", "display a summarized page_t", page },
4301         { "pagelookup", "?[-v vp] [-o offset]",
4302                 "find the page_t with the name {vp, offset}",
4303                 pagelookup, pagelookup_help },
4304         { "page_num2pp", ":", "find the page_t for a given page frame number",
4305                 page_num2pp },
4306         { "pmap", ":[-q]", "print process memory map", pmap },
4307         { "seg", ":", "print address space segment", seg },
4308         { "swapinfo", "?", "display a struct swapinfo", swapinfof },
4309         { "vnode2smap", ":[offset]", "translate vnode to smap", vnode2smap },
4310 
4311         /* from mmd.c */
4312         { "multidata", ":[-sv]", "display a summarized multidata_t",
4313                 multidata },
4314         { "pattbl", ":", "display a summarized multidata attribute table",
4315                 pattbl },
4316         { "pattr2multidata", ":", "print multidata pointer from pattr_t",
4317                 pattr2multidata },
4318         { "pdesc2slab", ":", "print pdesc slab pointer from pdesc_t",
4319                 pdesc2slab },
4320         { "pdesc_verify", ":", "verify integrity of a pdesc_t", pdesc_verify },
4321         { "slab2multidata", ":", "print multidata pointer from pdesc_slab_t",
4322                 slab2multidata },
4323 
4324         /* from modhash.c */
4325         { "modhash", "?[-ceht] [-k key] [-v val] [-i index]",
4326                 "display information about one or all mod_hash structures",
4327                 modhash, modhash_help },
4328         { "modent", ":[-k | -v | -t type]",
4329                 "display information about a mod_hash_entry", modent,
4330                 modent_help },
4331 
4332         /* from net.c */
4333         { "dladm", "?<sub-command> [flags]", "show data link information",
4334                 dladm, dladm_help },
4335         { "mi", ":[-p] [-d | -m]", "filter and display MI object or payload",
4336                 mi },
4337         { "netstat", "[-arv] [-f inet | inet6 | unix] [-P tcp | udp | icmp]",
4338                 "show network statistics", netstat },
4339         { "sonode", "?[-f inet | inet6 | unix | #] "
4340                 "[-t stream | dgram | raw | #] [-p #]",
4341                 "filter and display sonode", sonode },
4342 
4343         /* from netstack.c */
4344         { "netstack", "", "show stack instances", netstack },
4345         { "netstackid2netstack", ":",
4346                 "translate a netstack id to its netstack_t",
4347                 netstackid2netstack },
4348 
4349         /* from nvpair.c */
4350         { NVPAIR_DCMD_NAME, NVPAIR_DCMD_USAGE, NVPAIR_DCMD_DESCR,
4351                 nvpair_print },
4352         { NVLIST_DCMD_NAME, NVLIST_DCMD_USAGE, NVLIST_DCMD_DESCR,
4353                 print_nvlist },
4354 
4355         /* from pg.c */
4356         { "pg", "?[-q]", "display a pg", pg},
4357 
4358         /* from rctl.c */
4359         { "rctl_dict", "?", "print systemwide default rctl definitions",
4360                 rctl_dict },
4361         { "rctl_list", ":[handle]", "print rctls for the given proc",
4362                 rctl_list },
4363         { "rctl", ":[handle]", "print a rctl_t, only if it matches the handle",
4364                 rctl },
4365         { "rctl_validate", ":[-v] [-n #]", "test resource control value "
4366                 "sequence", rctl_validate },
4367 
4368         /* from sobj.c */
4369         { "rwlock", ":", "dump out a readers/writer lock", rwlock },
4370         { "mutex", ":[-f]", "dump out an adaptive or spin mutex", mutex,
4371                 mutex_help },
4372         { "sobj2ts", ":", "perform turnstile lookup on synch object", sobj2ts },
4373         { "wchaninfo", "?[-v]", "dump condition variable", wchaninfo },
4374         { "turnstile", "?", "display a turnstile", turnstile },
4375 
4376         /* from stream.c */
4377         { "mblk", ":[-q|v] [-f|F flag] [-t|T type] [-l|L|B len] [-d dbaddr]",
4378                 "print an mblk", mblk_prt, mblk_help },
4379         { "mblk_verify", "?", "verify integrity of an mblk", mblk_verify },
4380         { "mblk2dblk", ":", "convert mblk_t address to dblk_t address",
4381                 mblk2dblk },
4382         { "q2otherq", ":", "print peer queue for a given queue", q2otherq },
4383         { "q2rdq", ":", "print read queue for a given queue", q2rdq },
4384         { "q2syncq", ":", "print syncq for a given queue", q2syncq },
4385         { "q2stream", ":", "print stream pointer for a given queue", q2stream },
4386         { "q2wrq", ":", "print write queue for a given queue", q2wrq },
4387         { "queue", ":[-q|v] [-m mod] [-f flag] [-F flag] [-s syncq_addr]",
4388                 "filter and display STREAM queue", queue, queue_help },
4389         { "stdata", ":[-q|v] [-f flag] [-F flag]",
4390                 "filter and display STREAM head", stdata, stdata_help },
4391         { "str2mate", ":", "print mate of this stream", str2mate },
4392         { "str2wrq", ":", "print write queue of this stream", str2wrq },
4393         { "stream", ":", "display STREAM", stream },
4394         { "strftevent", ":", "print STREAMS flow trace event", strftevent },
4395         { "syncq", ":[-q|v] [-f flag] [-F flag] [-t type] [-T type]",
4396                 "filter and display STREAM sync queue", syncq, syncq_help },
4397         { "syncq2q", ":", "print queue for a given syncq", syncq2q },
4398 
4399         /* from taskq.c */
4400         { "taskq", ":[-atT] [-m min_maxq] [-n name]",
4401             "display a taskq", taskq, taskq_help },
4402         { "taskq_entry", ":", "display a taskq_ent_t", taskq_ent },
4403 
4404         /* from thread.c */
4405         { "thread", "?[-bdfimps]", "display a summarized kthread_t", thread,
4406                 thread_help },
4407         { "threadlist", "?[-t] [-v [count]]",
4408                 "display threads and associated C stack traces", threadlist,
4409                 threadlist_help },
4410         { "stackinfo", "?[-h|-a]", "display kthread_t stack usage", stackinfo,
4411                 stackinfo_help },
4412 
4413         /* from tsd.c */
4414         { "tsd", ":-k key", "print tsd[key-1] for this thread", ttotsd },
4415         { "tsdtot", ":", "find thread with this tsd", tsdtot },
4416 
4417         /*
4418          * typegraph does not work under kmdb, as it requires too much memory
4419          * for its internal data structures.
4420          */
4421 #ifndef _KMDB
4422         /* from typegraph.c */
4423         { "findlocks", ":", "find locks held by specified thread", findlocks },
4424         { "findfalse", "?[-v]", "find potentially falsely shared structures",
4425                 findfalse },
4426         { "typegraph", NULL, "build type graph", typegraph },
4427         { "istype", ":type", "manually set object type", istype },
4428         { "notype", ":", "manually clear object type", notype },
4429         { "whattype", ":", "determine object type", whattype },
4430 #endif
4431 
4432         /* from vfs.c */
4433         { "fsinfo", "?[-v]", "print mounted filesystems", fsinfo },
4434         { "pfiles", ":[-fp]", "print process file information", pfiles,
4435                 pfiles_help },
4436 
4437         /* from zone.c */
4438         { "zid2zone", ":", "find the zone_t with the given zone id",
4439                 zid2zone },
4440         { "zone", "?[-r [-v]]", "display kernel zone(s)", zoneprt },
4441         { "zsd", ":[-v] [zsd_key]", "display zone-specific-data entries for "
4442             "selected zones", zsd },
4443 
4444 #ifndef _KMDB
4445         { "gcore", NULL, "generate a user core for the given process",
4446             gcore_dcmd },
4447 #endif
4448 
4449         { NULL }
4450 };
4451 
4452 static const mdb_walker_t walkers[] = {
4453 
4454         /* from genunix.c */
4455         { "callouts_bytime", "walk callouts by list chain (expiration time)",
4456                 callout_walk_init, callout_walk_step, callout_walk_fini,
4457                 (void *)CALLOUT_WALK_BYLIST },
4458         { "callouts_byid", "walk callouts by id hash chain",
4459                 callout_walk_init, callout_walk_step, callout_walk_fini,
4460                 (void *)CALLOUT_WALK_BYID },
4461         { "callout_list", "walk a callout list", callout_list_walk_init,
4462                 callout_list_walk_step, callout_list_walk_fini },
4463         { "callout_table", "walk callout table array", callout_table_walk_init,
4464                 callout_table_walk_step, callout_table_walk_fini },
4465         { "cpu", "walk cpu structures", cpu_walk_init, cpu_walk_step },
4466         { "dnlc", "walk dnlc entries",
4467                 dnlc_walk_init, dnlc_walk_step, dnlc_walk_fini },
4468         { "ereportq_dump", "walk list of ereports in dump error queue",
4469                 ereportq_dump_walk_init, ereportq_dump_walk_step, NULL },
4470         { "ereportq_pend", "walk list of ereports in pending error queue",
4471                 ereportq_pend_walk_init, ereportq_pend_walk_step, NULL },
4472         { "errorq", "walk list of system error queues",
4473                 errorq_walk_init, errorq_walk_step, NULL },
4474         { "errorq_data", "walk pending error queue data buffers",
4475                 eqd_walk_init, eqd_walk_step, eqd_walk_fini },
4476         { "allfile", "given a proc pointer, list all file pointers",
4477                 file_walk_init, allfile_walk_step, file_walk_fini },
4478         { "file", "given a proc pointer, list of open file pointers",
4479                 file_walk_init, file_walk_step, file_walk_fini },
4480         { "lock_descriptor", "walk lock_descriptor_t structures",
4481                 ld_walk_init, ld_walk_step, NULL },
4482         { "lock_graph", "walk lock graph",
4483                 lg_walk_init, lg_walk_step, NULL },
4484         { "port", "given a proc pointer, list of created event ports",
4485                 port_walk_init, port_walk_step, NULL },
4486         { "portev", "given a port pointer, list of events in the queue",
4487                 portev_walk_init, portev_walk_step, portev_walk_fini },
4488         { "proc", "list of active proc_t structures",
4489                 proc_walk_init, proc_walk_step, proc_walk_fini },
4490         { "projects", "walk a list of kernel projects",
4491                 project_walk_init, project_walk_step, NULL },
4492         { "sysevent_pend", "walk sysevent pending queue",
4493                 sysevent_pend_walk_init, sysevent_walk_step,
4494                 sysevent_walk_fini},
4495         { "sysevent_sent", "walk sysevent sent queue", sysevent_sent_walk_init,
4496                 sysevent_walk_step, sysevent_walk_fini},
4497         { "sysevent_channel", "walk sysevent channel subscriptions",
4498                 sysevent_channel_walk_init, sysevent_channel_walk_step,
4499                 sysevent_channel_walk_fini},
4500         { "sysevent_class_list", "walk sysevent subscription's class list",
4501                 sysevent_class_list_walk_init, sysevent_class_list_walk_step,
4502                 sysevent_class_list_walk_fini},
4503         { "sysevent_subclass_list",
4504                 "walk sysevent subscription's subclass list",
4505                 sysevent_subclass_list_walk_init,
4506                 sysevent_subclass_list_walk_step,
4507                 sysevent_subclass_list_walk_fini},
4508         { "task", "given a task pointer, walk its processes",
4509                 task_walk_init, task_walk_step, NULL },
4510 
4511         /* from avl.c */
4512         { AVL_WALK_NAME, AVL_WALK_DESC,
4513                 avl_walk_init, avl_walk_step, avl_walk_fini },
4514 
4515         /* from bio.c */
4516         { "buf", "walk the bio buf hash",
4517                 buf_walk_init, buf_walk_step, buf_walk_fini },
4518 
4519         /* from contract.c */
4520         { "contract", "walk all contracts, or those of the specified type",
4521                 ct_walk_init, generic_walk_step, NULL },
4522         { "ct_event", "walk events on a contract event queue",
4523                 ct_event_walk_init, generic_walk_step, NULL },
4524         { "ct_listener", "walk contract event queue listeners",
4525                 ct_listener_walk_init, generic_walk_step, NULL },
4526 
4527         /* from cpupart.c */
4528         { "cpupart_cpulist", "given an cpupart_t, walk cpus in partition",
4529                 cpupart_cpulist_walk_init, cpupart_cpulist_walk_step,
4530                 NULL },
4531         { "cpupart_walk", "walk the set of cpu partitions",
4532                 cpupart_walk_init, cpupart_walk_step, NULL },
4533 
4534         /* from ctxop.c */
4535         { "ctxop", "walk list of context ops on a thread",
4536                 ctxop_walk_init, ctxop_walk_step, ctxop_walk_fini },
4537 
4538         /* from cyclic.c */
4539         { "cyccpu", "walk per-CPU cyc_cpu structures",
4540                 cyccpu_walk_init, cyccpu_walk_step, NULL },
4541         { "cycomni", "for an omnipresent cyclic, walk cyc_omni_cpu list",
4542                 cycomni_walk_init, cycomni_walk_step, NULL },
4543         { "cyctrace", "walk cyclic trace buffer",
4544                 cyctrace_walk_init, cyctrace_walk_step, cyctrace_walk_fini },
4545 
4546         /* from devinfo.c */
4547         { "binding_hash", "walk all entries in binding hash table",
4548                 binding_hash_walk_init, binding_hash_walk_step, NULL },
4549         { "devinfo", "walk devinfo tree or subtree",
4550                 devinfo_walk_init, devinfo_walk_step, devinfo_walk_fini },
4551         { "devinfo_audit_log", "walk devinfo audit system-wide log",
4552                 devinfo_audit_log_walk_init, devinfo_audit_log_walk_step,
4553                 devinfo_audit_log_walk_fini},
4554         { "devinfo_audit_node", "walk per-devinfo audit history",
4555                 devinfo_audit_node_walk_init, devinfo_audit_node_walk_step,
4556                 devinfo_audit_node_walk_fini},
4557         { "devinfo_children", "walk children of devinfo node",
4558                 devinfo_children_walk_init, devinfo_children_walk_step,
4559                 devinfo_children_walk_fini },
4560         { "devinfo_parents", "walk ancestors of devinfo node",
4561                 devinfo_parents_walk_init, devinfo_parents_walk_step,
4562                 devinfo_parents_walk_fini },
4563         { "devinfo_siblings", "walk siblings of devinfo node",
4564                 devinfo_siblings_walk_init, devinfo_siblings_walk_step, NULL },
4565         { "devi_next", "walk devinfo list",
4566                 NULL, devi_next_walk_step, NULL },
4567         { "devnames", "walk devnames array",
4568                 devnames_walk_init, devnames_walk_step, devnames_walk_fini },
4569         { "minornode", "given a devinfo node, walk minor nodes",
4570                 minornode_walk_init, minornode_walk_step, NULL },
4571         { "softstate",
4572                 "given an i_ddi_soft_state*, list all in-use driver stateps",
4573                 soft_state_walk_init, soft_state_walk_step,
4574                 NULL, NULL },
4575         { "softstate_all",
4576                 "given an i_ddi_soft_state*, list all driver stateps",
4577                 soft_state_walk_init, soft_state_all_walk_step,
4578                 NULL, NULL },
4579         { "devinfo_fmc",
4580                 "walk a fault management handle cache active list",
4581                 devinfo_fmc_walk_init, devinfo_fmc_walk_step, NULL },
4582 
4583         /* from group.c */
4584         { "group", "walk all elements of a group",
4585                 group_walk_init, group_walk_step, NULL },
4586 
4587         /* from irm.c */
4588         { "irmpools", "walk global list of interrupt pools",
4589             irmpools_walk_init, list_walk_step, list_walk_fini },
4590         { "irmreqs", "walk list of interrupt requests in an interrupt pool",
4591             irmreqs_walk_init, list_walk_step, list_walk_fini },
4592 
4593         /* from kmem.c */
4594         { "allocdby", "given a thread, walk its allocated bufctls",
4595                 allocdby_walk_init, allocdby_walk_step, allocdby_walk_fini },
4596         { "bufctl", "walk a kmem cache's bufctls",
4597                 bufctl_walk_init, kmem_walk_step, kmem_walk_fini },
4598         { "bufctl_history", "walk the available history of a bufctl",
4599                 bufctl_history_walk_init, bufctl_history_walk_step,
4600                 bufctl_history_walk_fini },
4601         { "freedby", "given a thread, walk its freed bufctls",
4602                 freedby_walk_init, allocdby_walk_step, allocdby_walk_fini },
4603         { "freectl", "walk a kmem cache's free bufctls",
4604                 freectl_walk_init, kmem_walk_step, kmem_walk_fini },
4605         { "freectl_constructed", "walk a kmem cache's constructed free bufctls",
4606                 freectl_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
4607         { "freemem", "walk a kmem cache's free memory",
4608                 freemem_walk_init, kmem_walk_step, kmem_walk_fini },
4609         { "freemem_constructed", "walk a kmem cache's constructed free memory",
4610                 freemem_constructed_walk_init, kmem_walk_step, kmem_walk_fini },
4611         { "kmem", "walk a kmem cache",
4612                 kmem_walk_init, kmem_walk_step, kmem_walk_fini },
4613         { "kmem_cpu_cache", "given a kmem cache, walk its per-CPU caches",
4614                 kmem_cpu_cache_walk_init, kmem_cpu_cache_walk_step, NULL },
4615         { "kmem_hash", "given a kmem cache, walk its allocated hash table",
4616                 kmem_hash_walk_init, kmem_hash_walk_step, kmem_hash_walk_fini },
4617         { "kmem_log", "walk the kmem transaction log",
4618                 kmem_log_walk_init, kmem_log_walk_step, kmem_log_walk_fini },
4619         { "kmem_slab", "given a kmem cache, walk its slabs",
4620                 kmem_slab_walk_init, combined_walk_step, combined_walk_fini },
4621         { "kmem_slab_partial",
4622             "given a kmem cache, walk its partially allocated slabs (min 1)",
4623                 kmem_slab_walk_partial_init, combined_walk_step,
4624                 combined_walk_fini },
4625         { "vmem", "walk vmem structures in pre-fix, depth-first order",
4626                 vmem_walk_init, vmem_walk_step, vmem_walk_fini },
4627         { "vmem_alloc", "given a vmem_t, walk its allocated vmem_segs",
4628                 vmem_alloc_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4629         { "vmem_free", "given a vmem_t, walk its free vmem_segs",
4630                 vmem_free_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4631         { "vmem_postfix", "walk vmem structures in post-fix, depth-first order",
4632                 vmem_walk_init, vmem_postfix_walk_step, vmem_walk_fini },
4633         { "vmem_seg", "given a vmem_t, walk all of its vmem_segs",
4634                 vmem_seg_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4635         { "vmem_span", "given a vmem_t, walk its spanning vmem_segs",
4636                 vmem_span_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
4637 
4638         /* from ldi.c */
4639         { "ldi_handle", "walk the layered driver handle hash",
4640                 ldi_handle_walk_init, ldi_handle_walk_step, NULL },
4641         { "ldi_ident", "walk the layered driver identifier hash",
4642                 ldi_ident_walk_init, ldi_ident_walk_step, NULL },
4643 
4644         /* from leaky.c + leaky_subr.c */
4645         { "leak", "given a leaked bufctl or vmem_seg, find leaks w/ same "
4646             "stack trace",
4647                 leaky_walk_init, leaky_walk_step, leaky_walk_fini },
4648         { "leakbuf", "given a leaked bufctl or vmem_seg, walk buffers for "
4649             "leaks w/ same stack trace",
4650                 leaky_walk_init, leaky_buf_walk_step, leaky_walk_fini },
4651 
4652         /* from lgrp.c */
4653         { "lgrp_cpulist", "walk CPUs in a given lgroup",
4654                 lgrp_cpulist_walk_init, lgrp_cpulist_walk_step, NULL },
4655         { "lgrptbl", "walk lgroup table",
4656                 lgrp_walk_init, lgrp_walk_step, NULL },
4657         { "lgrp_parents", "walk up lgroup lineage from given lgroup",
4658                 lgrp_parents_walk_init, lgrp_parents_walk_step, NULL },
4659         { "lgrp_rsrc_mem", "walk lgroup memory resources of given lgroup",
4660                 lgrp_rsrc_mem_walk_init, lgrp_set_walk_step, NULL },
4661         { "lgrp_rsrc_cpu", "walk lgroup CPU resources of given lgroup",
4662                 lgrp_rsrc_cpu_walk_init, lgrp_set_walk_step, NULL },
4663 
4664         /* from list.c */
4665         { LIST_WALK_NAME, LIST_WALK_DESC,
4666                 list_walk_init, list_walk_step, list_walk_fini },
4667 
4668         /* from mdi.c */
4669         { "mdipi_client_list", "Walker for mdi_pathinfo pi_client_link",
4670                 mdi_pi_client_link_walk_init,
4671                 mdi_pi_client_link_walk_step,
4672                 mdi_pi_client_link_walk_fini },
4673         { "mdipi_phci_list", "Walker for mdi_pathinfo pi_phci_link",
4674                 mdi_pi_phci_link_walk_init,
4675                 mdi_pi_phci_link_walk_step,
4676                 mdi_pi_phci_link_walk_fini },
4677         { "mdiphci_list", "Walker for mdi_phci ph_next link",
4678                 mdi_phci_ph_next_walk_init,
4679                 mdi_phci_ph_next_walk_step,
4680                 mdi_phci_ph_next_walk_fini },
4681 
4682         /* from memory.c */
4683         { "allpages", "walk all pages, including free pages",
4684                 allpages_walk_init, allpages_walk_step, allpages_walk_fini },
4685         { "anon", "given an amp, list allocated anon structures",
4686                 anon_walk_init, anon_walk_step, anon_walk_fini,
4687                 ANON_WALK_ALLOC },
4688         { "anon_all", "given an amp, list contents of all anon slots",
4689                 anon_walk_init, anon_walk_step, anon_walk_fini,
4690                 ANON_WALK_ALL },
4691         { "memlist", "walk specified memlist",
4692                 NULL, memlist_walk_step, NULL },
4693         { "page", "walk all pages, or those from the specified vnode",
4694                 page_walk_init, page_walk_step, page_walk_fini },
4695         { "seg", "given an as, list of segments",
4696                 seg_walk_init, avl_walk_step, avl_walk_fini },
4697         { "segvn_anon",
4698                 "given a struct segvn_data, list allocated anon structures",
4699                 segvn_anon_walk_init, anon_walk_step, anon_walk_fini,
4700                 ANON_WALK_ALLOC },
4701         { "segvn_anon_all",
4702                 "given a struct segvn_data, list contents of all anon slots",
4703                 segvn_anon_walk_init, anon_walk_step, anon_walk_fini,
4704                 ANON_WALK_ALL },
4705         { "segvn_pages",
4706                 "given a struct segvn_data, list resident pages in "
4707                 "offset order",
4708                 segvn_pages_walk_init, segvn_pages_walk_step,
4709                 segvn_pages_walk_fini, SEGVN_PAGES_RESIDENT },
4710         { "segvn_pages_all",
4711                 "for each offset in a struct segvn_data, give page_t pointer "
4712                 "(if resident), or NULL.",
4713                 segvn_pages_walk_init, segvn_pages_walk_step,
4714                 segvn_pages_walk_fini, SEGVN_PAGES_ALL },
4715         { "swapinfo", "walk swapinfo structures",
4716                 swap_walk_init, swap_walk_step, NULL },
4717 
4718         /* from mmd.c */
4719         { "pattr", "walk pattr_t structures", pattr_walk_init,
4720                 mmdq_walk_step, mmdq_walk_fini },
4721         { "pdesc", "walk pdesc_t structures",
4722                 pdesc_walk_init, mmdq_walk_step, mmdq_walk_fini },
4723         { "pdesc_slab", "walk pdesc_slab_t structures",
4724                 pdesc_slab_walk_init, mmdq_walk_step, mmdq_walk_fini },
4725 
4726         /* from modhash.c */
4727         { "modhash", "walk list of mod_hash structures", modhash_walk_init,
4728                 modhash_walk_step, NULL },
4729         { "modent", "walk list of entries in a given mod_hash",
4730                 modent_walk_init, modent_walk_step, modent_walk_fini },
4731         { "modchain", "walk list of entries in a given mod_hash_entry",
4732                 NULL, modchain_walk_step, NULL },
4733 
4734         /* from net.c */
4735         { "icmp", "walk ICMP control structures using MI for all stacks",
4736                 mi_payload_walk_init, mi_payload_walk_step, NULL,
4737                 &mi_icmp_arg },
4738         { "mi", "given a MI_O, walk the MI",
4739                 mi_walk_init, mi_walk_step, mi_walk_fini, NULL },
4740         { "sonode", "given a sonode, walk its children",
4741                 sonode_walk_init, sonode_walk_step, sonode_walk_fini, NULL },
4742         { "icmp_stacks", "walk all the icmp_stack_t",
4743                 icmp_stacks_walk_init, icmp_stacks_walk_step, NULL },
4744         { "tcp_stacks", "walk all the tcp_stack_t",
4745                 tcp_stacks_walk_init, tcp_stacks_walk_step, NULL },
4746         { "udp_stacks", "walk all the udp_stack_t",
4747                 udp_stacks_walk_init, udp_stacks_walk_step, NULL },
4748 
4749         /* from netstack.c */
4750         { "netstack", "walk a list of kernel netstacks",
4751                 netstack_walk_init, netstack_walk_step, NULL },
4752 
4753         /* from nvpair.c */
4754         { NVPAIR_WALKER_NAME, NVPAIR_WALKER_DESCR,
4755                 nvpair_walk_init, nvpair_walk_step, NULL },
4756 
4757         /* from pci.c */
4758         { "pcie_bus", "walk all pcie_bus_t's", pcie_bus_walk_init,
4759                 pcie_bus_walk_step, NULL },
4760 
4761         /* from rctl.c */
4762         { "rctl_dict_list", "walk all rctl_dict_entry_t's from rctl_lists",
4763                 rctl_dict_walk_init, rctl_dict_walk_step, NULL },
4764         { "rctl_set", "given a rctl_set, walk all rctls", rctl_set_walk_init,
4765                 rctl_set_walk_step, NULL },
4766         { "rctl_val", "given a rctl_t, walk all rctl_val entries associated",
4767                 rctl_val_walk_init, rctl_val_walk_step },
4768 
4769         /* from sobj.c */
4770         { "blocked", "walk threads blocked on a given sobj",
4771                 blocked_walk_init, blocked_walk_step, NULL },
4772         { "wchan", "given a wchan, list of blocked threads",
4773                 wchan_walk_init, wchan_walk_step, wchan_walk_fini },
4774 
4775         /* from stream.c */
4776         { "b_cont", "walk mblk_t list using b_cont",
4777                 mblk_walk_init, b_cont_step, mblk_walk_fini },
4778         { "b_next", "walk mblk_t list using b_next",
4779                 mblk_walk_init, b_next_step, mblk_walk_fini },
4780         { "qlink", "walk queue_t list using q_link",
4781                 queue_walk_init, queue_link_step, queue_walk_fini },
4782         { "qnext", "walk queue_t list using q_next",
4783                 queue_walk_init, queue_next_step, queue_walk_fini },
4784         { "strftblk", "given a dblk_t, walk STREAMS flow trace event list",
4785                 strftblk_walk_init, strftblk_step, strftblk_walk_fini },
4786         { "readq", "walk read queue side of stdata",
4787                 str_walk_init, strr_walk_step, str_walk_fini },
4788         { "writeq", "walk write queue side of stdata",
4789                 str_walk_init, strw_walk_step, str_walk_fini },
4790 
4791         /* from taskq.c */
4792         { "taskq_thread", "given a taskq_t, list all of its threads",
4793                 taskq_thread_walk_init,
4794                 taskq_thread_walk_step,
4795                 taskq_thread_walk_fini },
4796         { "taskq_entry", "given a taskq_t*, list all taskq_ent_t in the list",
4797                 taskq_ent_walk_init, taskq_ent_walk_step, NULL },
4798 
4799         /* from thread.c */
4800         { "deathrow", "walk threads on both lwp_ and thread_deathrow",
4801                 deathrow_walk_init, deathrow_walk_step, NULL },
4802         { "cpu_dispq", "given a cpu_t, walk threads in dispatcher queues",
4803                 cpu_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
4804         { "cpupart_dispq",
4805                 "given a cpupart_t, walk threads in dispatcher queues",
4806                 cpupart_dispq_walk_init, dispq_walk_step, dispq_walk_fini },
4807         { "lwp_deathrow", "walk lwp_deathrow",
4808                 lwp_deathrow_walk_init, deathrow_walk_step, NULL },
4809         { "thread", "global or per-process kthread_t structures",
4810                 thread_walk_init, thread_walk_step, thread_walk_fini },
4811         { "thread_deathrow", "walk threads on thread_deathrow",
4812                 thread_deathrow_walk_init, deathrow_walk_step, NULL },
4813 
4814         /* from tsd.c */
4815         { "tsd", "walk list of thread-specific data",
4816                 tsd_walk_init, tsd_walk_step, tsd_walk_fini },
4817 
4818         /* from tsol.c */
4819         { "tnrh", "walk remote host cache structures",
4820             tnrh_walk_init, tnrh_walk_step, tnrh_walk_fini },
4821         { "tnrhtp", "walk remote host template structures",
4822             tnrhtp_walk_init, tnrhtp_walk_step, tnrhtp_walk_fini },
4823 
4824         /*
4825          * typegraph does not work under kmdb, as it requires too much memory
4826          * for its internal data structures.
4827          */
4828 #ifndef _KMDB
4829         /* from typegraph.c */
4830         { "typeconflict", "walk buffers with conflicting type inferences",
4831                 typegraph_walk_init, typeconflict_walk_step },
4832         { "typeunknown", "walk buffers with unknown types",
4833                 typegraph_walk_init, typeunknown_walk_step },
4834 #endif
4835 
4836         /* from vfs.c */
4837         { "vfs", "walk file system list",
4838                 vfs_walk_init, vfs_walk_step },
4839 
4840         /* from zone.c */
4841         { "zone", "walk a list of kernel zones",
4842                 zone_walk_init, zone_walk_step, NULL },
4843         { "zsd", "walk list of zsd entries for a zone",
4844                 zsd_walk_init, zsd_walk_step, NULL },
4845 
4846         { NULL }
4847 };
4848 
4849 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
4850 
4851 /*ARGSUSED*/
4852 static void
4853 genunix_statechange_cb(void *ignored)
4854 {
4855         /*
4856          * Force ::findleaks and ::stacks to let go any cached state.
4857          */
4858         leaky_cleanup(1);
4859         stacks_cleanup(1);
4860 
4861         kmem_statechange();     /* notify kmem */
4862 }
4863 
4864 const mdb_modinfo_t *
4865 _mdb_init(void)
4866 {
4867         kmem_init();
4868 
4869         (void) mdb_callback_add(MDB_CALLBACK_STCHG,
4870             genunix_statechange_cb, NULL);
4871 
4872 #ifndef _KMDB
4873         gcore_init();
4874 #endif
4875 
4876         return (&modinfo);
4877 }
4878 
4879 void
4880 _mdb_fini(void)
4881 {
4882         leaky_cleanup(1);
4883         stacks_cleanup(1);
4884 }