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