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