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