Unleashed v1.4
[unleashed.git] / usr / src / cmd / mdb / common / modules / mdb_ks / mdb_ks.c
blob99807187b0599b7abf6af6fe07d08ac67674c5a2
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2017 Joyent, Inc.
27 * Mdb kernel support module. This module is loaded automatically when the
28 * kvm target is initialized. Any global functions declared here are exported
29 * for the resolution of symbols in subsequently loaded modules.
31 * WARNING: Do not assume that static variables in mdb_ks will be initialized
32 * to zero.
35 #include <mdb/mdb_target.h>
36 #include <mdb/mdb_param.h>
37 #include <mdb/mdb_modapi.h>
38 #include <mdb/mdb_ks.h>
40 #include <sys/types.h>
41 #include <sys/procfs.h>
42 #include <sys/proc.h>
43 #include <sys/dnlc.h>
44 #include <sys/autoconf.h>
45 #include <sys/machelf.h>
46 #include <sys/modctl.h>
47 #include <sys/hwconf.h>
48 #include <sys/kobj.h>
49 #include <sys/fs/autofs.h>
50 #include <sys/ddi_impldefs.h>
51 #include <sys/refstr_impl.h>
52 #include <sys/cpuvar.h>
53 #include <sys/dlpi.h>
54 #include <sys/clock_impl.h>
55 #include <sys/swap.h>
56 #include <errno.h>
58 #include <vm/seg_vn.h>
59 #include <vm/page.h>
61 #define MDB_PATH_NELEM 256 /* Maximum path components */
63 typedef struct mdb_path {
64 size_t mdp_nelem; /* Number of components */
65 uint_t mdp_complete; /* Path completely resolved? */
66 uintptr_t mdp_vnode[MDB_PATH_NELEM]; /* Array of vnode_t addresses */
67 char *mdp_name[MDB_PATH_NELEM]; /* Array of name components */
68 } mdb_path_t;
70 static int mdb_autonode2path(uintptr_t, mdb_path_t *);
71 static int mdb_sprintpath(char *, size_t, mdb_path_t *);
74 * Kernel parameters from <sys/param.h> which we keep in-core:
76 unsigned long _mdb_ks_pagesize;
77 unsigned int _mdb_ks_pageshift;
78 unsigned long _mdb_ks_pageoffset;
79 unsigned long long _mdb_ks_pagemask;
80 unsigned long _mdb_ks_mmu_pagesize;
81 unsigned int _mdb_ks_mmu_pageshift;
82 unsigned long _mdb_ks_mmu_pageoffset;
83 unsigned long _mdb_ks_mmu_pagemask;
84 uintptr_t _mdb_ks_kernelbase;
85 uintptr_t _mdb_ks_userlimit;
86 uintptr_t _mdb_ks_userlimit32;
87 uintptr_t _mdb_ks_argsbase;
88 unsigned long _mdb_ks_msg_bsize;
89 unsigned long _mdb_ks_defaultstksz;
90 int _mdb_ks_ncpu;
91 int _mdb_ks_ncpu_log2;
92 int _mdb_ks_ncpu_p2;
95 * In-core copy of DNLC information:
97 #define MDB_DNLC_HSIZE 1024
98 #define MDB_DNLC_HASH(vp) (((uintptr_t)(vp) >> 3) & (MDB_DNLC_HSIZE - 1))
99 #define MDB_DNLC_NCACHE_SZ(ncp) (sizeof (ncache_t) + (ncp)->namlen)
100 #define MDB_DNLC_MAX_RETRY 4
102 static ncache_t **dnlc_hash; /* mdbs hash array of dnlc entries */
105 * STREAMS queue registrations:
107 typedef struct mdb_qinfo {
108 const mdb_qops_t *qi_ops; /* Address of ops vector */
109 uintptr_t qi_addr; /* Address of qinit structure (key) */
110 struct mdb_qinfo *qi_next; /* Next qinfo in list */
111 } mdb_qinfo_t;
113 static mdb_qinfo_t *qi_head; /* Head of qinfo chain */
116 * Device naming callback structure:
118 typedef struct nm_query {
119 const char *nm_name; /* Device driver name [in/out] */
120 major_t nm_major; /* Device major number [in/out] */
121 ushort_t nm_found; /* Did we find a match? [out] */
122 } nm_query_t;
125 * Address-to-modctl callback structure:
127 typedef struct a2m_query {
128 uintptr_t a2m_addr; /* Virtual address [in] */
129 uintptr_t a2m_where; /* Modctl address [out] */
130 } a2m_query_t;
133 * Segment-to-mdb_map callback structure:
135 typedef struct {
136 struct seg_ops *asm_segvn_ops; /* Address of segvn ops [in] */
137 void (*asm_callback)(const struct mdb_map *, void *); /* Callb [in] */
138 void *asm_cbdata; /* Callback data [in] */
139 } asmap_arg_t;
141 static void
142 dnlc_free(void)
144 ncache_t *ncp, *next;
145 int i;
147 if (dnlc_hash == NULL) {
148 return;
152 * Free up current dnlc entries
154 for (i = 0; i < MDB_DNLC_HSIZE; i++) {
155 for (ncp = dnlc_hash[i]; ncp; ncp = next) {
156 next = ncp->hash_next;
157 mdb_free(ncp, MDB_DNLC_NCACHE_SZ(ncp));
160 mdb_free(dnlc_hash, MDB_DNLC_HSIZE * sizeof (ncache_t *));
161 dnlc_hash = NULL;
164 char bad_dnlc[] = "inconsistent dnlc chain: %d, ncache va: %p"
165 " - continuing with the rest\n";
167 static int
168 dnlc_load(void)
170 int i; /* hash index */
171 int retry_cnt = 0;
172 int skip_bad_chains = 0;
173 int nc_hashsz; /* kernel hash array size */
174 uintptr_t nc_hash_addr; /* kernel va of ncache hash array */
175 uintptr_t head; /* kernel va of head of hash chain */
178 * If we've already cached the DNLC and we're looking at a dump,
179 * our cache is good forever, so don't bother re-loading.
181 if (dnlc_hash && mdb_prop_postmortem) {
182 return (0);
186 * For a core dump, retries wont help.
187 * Just print and skip any bad chains.
189 if (mdb_prop_postmortem) {
190 skip_bad_chains = 1;
192 retry:
193 if (retry_cnt++ >= MDB_DNLC_MAX_RETRY) {
195 * Give up retrying the rapidly changing dnlc.
196 * Just print and skip any bad chains
198 skip_bad_chains = 1;
201 dnlc_free(); /* Free up the mdb hashed dnlc - if any */
204 * Although nc_hashsz and the location of nc_hash doesn't currently
205 * change, it may do in the future with a more dynamic dnlc.
206 * So always read these values afresh.
208 if (mdb_readvar(&nc_hashsz, "nc_hashsz") == -1) {
209 mdb_warn("failed to read nc_hashsz");
210 return (-1);
212 if (mdb_readvar(&nc_hash_addr, "nc_hash") == -1) {
213 mdb_warn("failed to read nc_hash");
214 return (-1);
218 * Allocate the mdb dnlc hash array
220 dnlc_hash = mdb_zalloc(MDB_DNLC_HSIZE * sizeof (ncache_t *), UM_SLEEP);
222 /* for each kernel hash chain */
223 for (i = 0, head = nc_hash_addr; i < nc_hashsz;
224 i++, head += sizeof (nc_hash_t)) {
225 nc_hash_t nch; /* kernel hash chain header */
226 ncache_t *ncp; /* name cache pointer */
227 int hash; /* mdb hash value */
228 uintptr_t nc_va; /* kernel va of next ncache */
229 uintptr_t ncprev_va; /* kernel va of previous ncache */
230 int khash; /* kernel dnlc hash value */
231 uchar_t namelen; /* name length */
232 ncache_t nc; /* name cache entry */
233 int nc_size; /* size of a name cache entry */
236 * We read each element of the nc_hash array individually
237 * just before we process the entries in its chain. This is
238 * because the chain can change so rapidly on a running system.
240 if (mdb_vread(&nch, sizeof (nc_hash_t), head) == -1) {
241 mdb_warn("failed to read nc_hash chain header %d", i);
242 dnlc_free();
243 return (-1);
246 ncprev_va = head;
247 nc_va = (uintptr_t)(nch.hash_next);
248 /* for each entry in the chain */
249 while (nc_va != head) {
251 * The size of the ncache entries varies
252 * because the name is appended to the structure.
253 * So we read in the structure then re-read
254 * for the structure plus name.
256 if (mdb_vread(&nc, sizeof (ncache_t), nc_va) == -1) {
257 if (skip_bad_chains) {
258 mdb_warn(bad_dnlc, i, nc_va);
259 break;
261 goto retry;
263 nc_size = MDB_DNLC_NCACHE_SZ(&nc);
264 ncp = mdb_alloc(nc_size, UM_SLEEP);
265 if (mdb_vread(ncp, nc_size - 1, nc_va) == -1) {
266 mdb_free(ncp, nc_size);
267 if (skip_bad_chains) {
268 mdb_warn(bad_dnlc, i, nc_va);
269 break;
271 goto retry;
275 * Check for chain consistency
277 if ((uintptr_t)ncp->hash_prev != ncprev_va) {
278 mdb_free(ncp, nc_size);
279 if (skip_bad_chains) {
280 mdb_warn(bad_dnlc, i, nc_va);
281 break;
283 goto retry;
286 * Terminate the new name with a null.
287 * Note, we allowed space for this null when
288 * allocating space for the entry.
290 ncp->name[ncp->namlen] = '\0';
293 * Validate new entry by re-hashing using the
294 * kernel dnlc hash function and comparing the hash
296 DNLCHASH(ncp->name, ncp->dp, khash, namelen);
297 if ((namelen != ncp->namlen) ||
298 (khash != ncp->hash)) {
299 mdb_free(ncp, nc_size);
300 if (skip_bad_chains) {
301 mdb_warn(bad_dnlc, i, nc_va);
302 break;
304 goto retry;
308 * Finally put the validated entry into the mdb
309 * hash chains. Reuse the kernel next hash field
310 * for the mdb hash chain pointer.
312 hash = MDB_DNLC_HASH(ncp->vp);
313 ncprev_va = nc_va;
314 nc_va = (uintptr_t)(ncp->hash_next);
315 ncp->hash_next = dnlc_hash[hash];
316 dnlc_hash[hash] = ncp;
319 return (0);
322 /*ARGSUSED*/
324 dnlcdump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
326 ncache_t *ent;
327 int i;
329 if ((flags & DCMD_ADDRSPEC) || argc != 0)
330 return (DCMD_USAGE);
332 if (dnlc_load() == -1)
333 return (DCMD_ERR);
335 mdb_printf("%<u>%-?s %-?s %-32s%</u>\n", "VP", "DVP", "NAME");
337 for (i = 0; i < MDB_DNLC_HSIZE; i++) {
338 for (ent = dnlc_hash[i]; ent != NULL; ent = ent->hash_next) {
339 mdb_printf("%0?p %0?p %s\n",
340 ent->vp, ent->dp, ent->name);
344 return (DCMD_OK);
347 static int
348 mdb_sprintpath(char *buf, size_t len, mdb_path_t *path)
350 char *s = buf;
351 int i;
353 if (len < sizeof ("/..."))
354 return (-1);
356 if (!path->mdp_complete) {
357 (void) strcpy(s, "??");
358 s += 2;
360 if (path->mdp_nelem == 0)
361 return (-1);
364 if (path->mdp_nelem == 0) {
365 (void) strcpy(s, "/");
366 return (0);
369 for (i = path->mdp_nelem - 1; i >= 0; i--) {
371 * Number of bytes left is the distance from where we
372 * are to the end, minus 2 for '/' and '\0'
374 ssize_t left = (ssize_t)(&buf[len] - s) - 2;
376 if (left <= 0)
377 break;
379 *s++ = '/';
380 (void) strncpy(s, path->mdp_name[i], left);
381 s[left - 1] = '\0';
382 s += strlen(s);
384 if (left < strlen(path->mdp_name[i]))
385 break;
388 if (i >= 0)
389 (void) strcpy(&buf[len - 4], "...");
391 return (0);
394 static int
395 mdb_autonode2path(uintptr_t addr, mdb_path_t *path)
397 GElf_Sym sym;
398 fninfo_t fni;
399 fnnode_t fn;
401 vnode_t vn;
402 vfs_t vfs;
405 * We want to look it up each time we access it since autofs could
406 * (in theory) be unloaded and reloaded.
408 if (mdb_lookup_by_name("auto_vnodeops", &sym) != 0)
409 return (-1);
411 if (vn.v_op != (struct vnodeops *)(uintptr_t)sym.st_value)
412 return (-1);
414 addr = (uintptr_t)vn.v_data;
416 if (mdb_vread(&vfs, sizeof (vfs), (uintptr_t)vn.v_vfsp) == -1 ||
417 mdb_vread(&fni, sizeof (fni), (uintptr_t)vfs.vfs_data) == -1 ||
418 mdb_vread(&vn, sizeof (vn), (uintptr_t)fni.fi_rootvp) == -1)
419 return (-1);
421 for (;;) {
422 size_t elem = path->mdp_nelem++;
423 char elemstr[MAXNAMELEN];
424 char *c, *p;
426 if (elem == MDB_PATH_NELEM) {
427 path->mdp_nelem--;
428 return (-1);
431 if (mdb_vread(&fn, sizeof (fn), addr) != sizeof (fn)) {
432 path->mdp_nelem--;
433 return (-1);
436 if (mdb_readstr(elemstr, sizeof (elemstr),
437 (uintptr_t)fn.fn_name) <= 0) {
438 (void) strcpy(elemstr, "?");
441 c = mdb_alloc(strlen(elemstr) + 1, UM_SLEEP | UM_GC);
442 (void) strcpy(c, elemstr);
444 path->mdp_vnode[elem] = (uintptr_t)fn.fn_vnode;
446 if (addr == (uintptr_t)fn.fn_parent) {
447 path->mdp_name[elem] = &c[1];
448 path->mdp_complete = TRUE;
449 break;
452 if ((p = strrchr(c, '/')) != NULL)
453 path->mdp_name[elem] = p + 1;
454 else
455 path->mdp_name[elem] = c;
457 addr = (uintptr_t)fn.fn_parent;
460 return (0);
464 mdb_vnode2path(uintptr_t addr, char *buf, size_t buflen)
466 uintptr_t rootdir;
467 ncache_t *ent;
468 vnode_t vp;
469 mdb_path_t path;
472 * Check to see if we have a cached value for this vnode
474 if (mdb_vread(&vp, sizeof (vp), addr) != -1 &&
475 vp.v_path != NULL &&
476 mdb_readstr(buf, buflen, (uintptr_t)vp.v_path) != -1)
477 return (0);
479 if (dnlc_load() == -1)
480 return (-1);
482 if (mdb_readvar(&rootdir, "rootdir") == -1) {
483 mdb_warn("failed to read 'rootdir'");
484 return (-1);
487 bzero(&path, sizeof (mdb_path_t));
488 again:
489 if ((addr == (uintptr_t)NULL) && (path.mdp_nelem == 0)) {
491 * 0 elems && complete tells sprintpath to just print "/"
493 path.mdp_complete = TRUE;
494 goto out;
497 if (addr == rootdir) {
498 path.mdp_complete = TRUE;
499 goto out;
502 for (ent = dnlc_hash[MDB_DNLC_HASH(addr)]; ent; ent = ent->hash_next) {
503 if ((uintptr_t)ent->vp == addr) {
504 if (strcmp(ent->name, "..") == 0 ||
505 strcmp(ent->name, ".") == 0)
506 continue;
508 path.mdp_vnode[path.mdp_nelem] = (uintptr_t)ent->vp;
509 path.mdp_name[path.mdp_nelem] = ent->name;
510 path.mdp_nelem++;
512 if (path.mdp_nelem == MDB_PATH_NELEM) {
513 path.mdp_nelem--;
514 mdb_warn("path exceeded maximum expected "
515 "elements\n");
516 return (-1);
519 addr = (uintptr_t)ent->dp;
520 goto again;
524 (void) mdb_autonode2path(addr, &path);
526 out:
527 return (mdb_sprintpath(buf, buflen, &path));
531 uintptr_t
532 mdb_pid2proc(pid_t pid, proc_t *proc)
534 int pid_hashsz, hash;
535 uintptr_t paddr, pidhash, procdir;
536 struct pid pidp;
538 if (mdb_readvar(&pidhash, "pidhash") == -1)
539 return (0);
541 if (mdb_readvar(&pid_hashsz, "pid_hashsz") == -1)
542 return (0);
544 if (mdb_readvar(&procdir, "procdir") == -1)
545 return (0);
547 hash = pid & (pid_hashsz - 1);
549 if (mdb_vread(&paddr, sizeof (paddr),
550 pidhash + (hash * sizeof (paddr))) == -1)
551 return (0);
553 while (paddr != (uintptr_t)NULL) {
554 if (mdb_vread(&pidp, sizeof (pidp), paddr) == -1)
555 return (0);
557 if (pidp.pid_id == pid) {
558 uintptr_t procp;
560 if (mdb_vread(&procp, sizeof (procp), procdir +
561 (pidp.pid_prslot * sizeof (procp))) == -1)
562 return (0);
564 if (proc != NULL)
565 (void) mdb_vread(proc, sizeof (proc_t), procp);
567 return (procp);
569 paddr = (uintptr_t)pidp.pid_link;
571 return (0);
575 mdb_cpu2cpuid(uintptr_t cpup)
577 cpu_t cpu;
579 if (mdb_vread(&cpu, sizeof (cpu_t), cpup) != sizeof (cpu_t))
580 return (-1);
582 return (cpu.cpu_id);
586 mdb_cpuset_find(uintptr_t cpusetp)
588 ulong_t *cpuset;
589 size_t nr_words = BT_BITOUL(NCPU);
590 size_t sz = nr_words * sizeof (ulong_t);
591 size_t i;
592 int cpu = -1;
594 cpuset = mdb_alloc(sz, UM_SLEEP);
596 if (mdb_vread((void *)cpuset, sz, cpusetp) != sz)
597 goto out;
599 for (i = 0; i < nr_words; i++) {
600 size_t j;
601 ulong_t m;
603 for (j = 0, m = 1; j < BT_NBIPUL; j++, m <<= 1) {
604 if (cpuset[i] & m) {
605 cpu = i * BT_NBIPUL + j;
606 goto out;
611 out:
612 mdb_free(cpuset, sz);
613 return (cpu);
616 struct page_lookup_state {
617 /* input */
618 uintptr_t vnode;
619 uoff_t offset;
621 /* output */
622 uintptr_t page;
625 static int
626 __page_lookup(uintptr_t addr, const void *data, void *private)
628 struct page_lookup_state *state = private;
629 const page_t *page = data;
631 if ((uintptr_t)page->p_vnode != state->vnode) {
632 mdb_warn("unexpected vnode pointer (got: %p, expected: %p)",
633 page->p_vnode, state->vnode);
634 return (WALK_ERR);
637 if (page->p_offset != state->offset)
638 return (WALK_NEXT);
640 state->page = addr;
641 return (WALK_DONE);
644 uintptr_t
645 mdb_page_lookup(uintptr_t vp, uoff_t offset)
647 uintptr_t addr = vp + OFFSETOF(vnode_t, v_object.tree);
648 struct page_lookup_state state = {
649 .vnode = vp,
650 .offset = offset,
653 if (mdb_pwalk("avl", __page_lookup, &state, addr) != 0)
654 return 0;
656 return state.page;
659 char
660 mdb_vtype2chr(vtype_t type, mode_t mode)
662 static const char vttab[] = {
663 ' ', /* VNON */
664 ' ', /* VREG */
665 '/', /* VDIR */
666 ' ', /* VBLK */
667 ' ', /* VCHR */
668 '@', /* VLNK */
669 '|', /* VFIFO */
670 '>', /* VDOOR */
671 ' ', /* VPROC */
672 '=', /* VSOCK */
673 ' ', /* VBAD */
676 if (type < 0 || type >= sizeof (vttab) / sizeof (vttab[0]))
677 return ('?');
679 if (type == VREG && (mode & 0111) != 0)
680 return ('*');
682 return (vttab[type]);
685 struct pfn2page {
686 pfn_t pfn;
687 page_t *pp;
690 /*ARGSUSED*/
691 static int
692 pfn2page_cb(uintptr_t addr, const struct memseg *msp, void *data)
694 struct pfn2page *p = data;
696 if (p->pfn >= msp->pages_base && p->pfn < msp->pages_end) {
697 p->pp = msp->pages + (p->pfn - msp->pages_base);
698 return (WALK_DONE);
701 return (WALK_NEXT);
704 uintptr_t
705 mdb_pfn2page(pfn_t pfn)
707 struct pfn2page arg;
708 struct page page;
710 arg.pfn = pfn;
711 arg.pp = NULL;
713 if (mdb_walk("memseg", (mdb_walk_cb_t)pfn2page_cb, &arg) == -1) {
714 mdb_warn("pfn2page: can't walk memsegs");
715 return (0);
717 if (arg.pp == NULL) {
718 mdb_warn("pfn2page: unable to find page_t for pfn %lx\n",
719 pfn);
720 return (0);
723 if (mdb_vread(&page, sizeof (page_t), (uintptr_t)arg.pp) == -1) {
724 mdb_warn("pfn2page: can't read page 0x%lx at %p", pfn, arg.pp);
725 return (0);
727 if (page.p_pagenum != pfn) {
728 mdb_warn("pfn2page: page_t 0x%p should have PFN 0x%lx, "
729 "but actually has 0x%lx\n", arg.pp, pfn, page.p_pagenum);
730 return (0);
733 return ((uintptr_t)arg.pp);
736 pfn_t
737 mdb_page2pfn(uintptr_t addr)
739 struct page page;
741 if (mdb_vread(&page, sizeof (page_t), addr) == -1) {
742 mdb_warn("pp2pfn: can't read page at %p", addr);
743 return ((pfn_t)(-1));
746 return (page.p_pagenum);
749 static int
750 a2m_walk_modctl(uintptr_t addr, const struct modctl *m, a2m_query_t *a2m)
752 struct module mod;
754 if (m->mod_mp == NULL)
755 return (0);
757 if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) {
758 mdb_warn("couldn't read modctl %p's module", addr);
759 return (0);
762 if (a2m->a2m_addr >= (uintptr_t)mod.text &&
763 a2m->a2m_addr < (uintptr_t)mod.text + mod.text_size)
764 goto found;
766 if (a2m->a2m_addr >= (uintptr_t)mod.data &&
767 a2m->a2m_addr < (uintptr_t)mod.data + mod.data_size)
768 goto found;
770 return (0);
772 found:
773 a2m->a2m_where = addr;
774 return (-1);
777 uintptr_t
778 mdb_addr2modctl(uintptr_t addr)
780 a2m_query_t a2m;
782 a2m.a2m_addr = addr;
783 a2m.a2m_where = (uintptr_t)NULL;
785 (void) mdb_walk("modctl", (mdb_walk_cb_t)a2m_walk_modctl, &a2m);
786 return (a2m.a2m_where);
789 static mdb_qinfo_t *
790 qi_lookup(uintptr_t qinit_addr)
792 mdb_qinfo_t *qip;
794 for (qip = qi_head; qip != NULL; qip = qip->qi_next) {
795 if (qip->qi_addr == qinit_addr)
796 return (qip);
799 return (NULL);
802 void
803 mdb_qops_install(const mdb_qops_t *qops, uintptr_t qinit_addr)
805 mdb_qinfo_t *qip = qi_lookup(qinit_addr);
807 if (qip != NULL) {
808 qip->qi_ops = qops;
809 return;
812 qip = mdb_alloc(sizeof (mdb_qinfo_t), UM_SLEEP);
814 qip->qi_ops = qops;
815 qip->qi_addr = qinit_addr;
816 qip->qi_next = qi_head;
818 qi_head = qip;
821 void
822 mdb_qops_remove(const mdb_qops_t *qops, uintptr_t qinit_addr)
824 mdb_qinfo_t *qip, *p = NULL;
826 for (qip = qi_head; qip != NULL; p = qip, qip = qip->qi_next) {
827 if (qip->qi_addr == qinit_addr && qip->qi_ops == qops) {
828 if (qi_head == qip)
829 qi_head = qip->qi_next;
830 else
831 p->qi_next = qip->qi_next;
832 mdb_free(qip, sizeof (mdb_qinfo_t));
833 return;
838 char *
839 mdb_qname(const queue_t *q, char *buf, size_t nbytes)
841 struct module_info mi;
842 struct qinit qi;
844 if (mdb_vread(&qi, sizeof (qi), (uintptr_t)q->q_qinfo) == -1) {
845 mdb_warn("failed to read qinit at %p", q->q_qinfo);
846 goto err;
849 if (mdb_vread(&mi, sizeof (mi), (uintptr_t)qi.qi_minfo) == -1) {
850 mdb_warn("failed to read module_info at %p", qi.qi_minfo);
851 goto err;
854 if (mdb_readstr(buf, nbytes, (uintptr_t)mi.mi_idname) <= 0) {
855 mdb_warn("failed to read mi_idname at %p", mi.mi_idname);
856 goto err;
859 return (buf);
861 err:
862 (void) mdb_snprintf(buf, nbytes, "???");
863 return (buf);
866 void
867 mdb_qinfo(const queue_t *q, char *buf, size_t nbytes)
869 mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo);
870 buf[0] = '\0';
872 if (qip != NULL)
873 qip->qi_ops->q_info(q, buf, nbytes);
876 uintptr_t
877 mdb_qrnext(const queue_t *q)
879 mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo);
881 if (qip != NULL)
882 return (qip->qi_ops->q_rnext(q));
884 return ((uintptr_t)NULL);
887 uintptr_t
888 mdb_qwnext(const queue_t *q)
890 mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo);
892 if (qip != NULL)
893 return (qip->qi_ops->q_wnext(q));
895 return ((uintptr_t)NULL);
898 uintptr_t
899 mdb_qrnext_default(const queue_t *q)
901 return ((uintptr_t)q->q_next);
904 uintptr_t
905 mdb_qwnext_default(const queue_t *q)
907 return ((uintptr_t)q->q_next);
911 * The following three routines borrowed from modsubr.c
913 static int
914 nm_hash(const char *name)
916 char c;
917 int hash = 0;
919 for (c = *name++; c; c = *name++)
920 hash ^= c;
922 return (hash & MOD_BIND_HASHMASK);
925 static uintptr_t
926 find_mbind(const char *name, uintptr_t *hashtab)
928 int hashndx;
929 uintptr_t mb;
930 struct bind mb_local;
931 char node_name[MAXPATHLEN + 1];
933 hashndx = nm_hash(name);
934 mb = hashtab[hashndx];
935 while (mb) {
936 if (mdb_vread(&mb_local, sizeof (mb_local), mb) == -1) {
937 mdb_warn("failed to read struct bind at %p", mb);
938 return ((uintptr_t)NULL);
940 if (mdb_readstr(node_name, sizeof (node_name),
941 (uintptr_t)mb_local.b_name) == -1) {
942 mdb_warn("failed to read node name string at %p",
943 mb_local.b_name);
944 return ((uintptr_t)NULL);
947 if (strcmp(name, node_name) == 0)
948 break;
950 mb = (uintptr_t)mb_local.b_next;
952 return (mb);
956 mdb_name_to_major(const char *name, major_t *major)
958 uintptr_t mbind;
959 uintptr_t mb_hashtab[MOD_BIND_HASHSIZE];
960 struct bind mbind_local;
963 if (mdb_readsym(mb_hashtab, sizeof (mb_hashtab), "mb_hashtab") == -1) {
964 mdb_warn("failed to read symbol 'mb_hashtab'");
965 return (-1);
968 if ((mbind = find_mbind(name, mb_hashtab)) != (uintptr_t)NULL) {
969 if (mdb_vread(&mbind_local, sizeof (mbind_local), mbind) ==
970 -1) {
971 mdb_warn("failed to read mbind struct at %p", mbind);
972 return (-1);
975 *major = (major_t)mbind_local.b_num;
976 return (0);
978 return (-1);
981 const char *
982 mdb_major_to_name(major_t major)
984 static char name[MODMAXNAMELEN + 1];
986 uintptr_t devnamesp;
987 struct devnames dn;
988 uint_t devcnt;
990 if (mdb_readvar(&devcnt, "devcnt") == -1 || major >= devcnt ||
991 mdb_readvar(&devnamesp, "devnamesp") == -1)
992 return (NULL);
994 if (mdb_vread(&dn, sizeof (struct devnames), devnamesp +
995 major * sizeof (struct devnames)) != sizeof (struct devnames))
996 return (NULL);
998 if (mdb_readstr(name, MODMAXNAMELEN + 1, (uintptr_t)dn.dn_name) == -1)
999 return (NULL);
1001 return ((const char *)name);
1005 * Return the name of the driver attached to the dip in drivername.
1008 mdb_devinfo2driver(uintptr_t dip_addr, char *drivername, size_t namebufsize)
1010 struct dev_info devinfo;
1011 char bind_name[MAXPATHLEN + 1];
1012 major_t major;
1013 const char *namestr;
1016 if (mdb_vread(&devinfo, sizeof (devinfo), dip_addr) == -1) {
1017 mdb_warn("failed to read devinfo at %p", dip_addr);
1018 return (-1);
1021 if (mdb_readstr(bind_name, sizeof (bind_name),
1022 (uintptr_t)devinfo.devi_binding_name) == -1) {
1023 mdb_warn("failed to read binding name at %p",
1024 devinfo.devi_binding_name);
1025 return (-1);
1029 * Many->one relation: various names to one major number
1031 if (mdb_name_to_major(bind_name, &major) == -1) {
1032 mdb_warn("failed to translate bind name to major number\n");
1033 return (-1);
1037 * One->one relation: one major number corresponds to one driver
1039 if ((namestr = mdb_major_to_name(major)) == NULL) {
1040 (void) strncpy(drivername, "???", namebufsize);
1041 return (-1);
1044 (void) strncpy(drivername, namestr, namebufsize);
1045 return (0);
1049 * Find the name of the driver attached to this dip (if any), given:
1050 * - the address of a dip (in core)
1051 * - the NAME of the global pointer to the driver's i_ddi_soft_state struct
1052 * - pointer to a pointer to receive the address
1055 mdb_devinfo2statep(uintptr_t dip_addr, char *soft_statep_name,
1056 uintptr_t *statep)
1058 struct dev_info dev_info;
1061 if (mdb_vread(&dev_info, sizeof (dev_info), dip_addr) == -1) {
1062 mdb_warn("failed to read devinfo at %p", dip_addr);
1063 return (-1);
1066 return (mdb_get_soft_state_byname(soft_statep_name,
1067 dev_info.devi_instance, statep, NULL, 0));
1071 * Returns a pointer to the top of the soft state struct for the instance
1072 * specified (in state_addr), given the address of the global soft state
1073 * pointer and size of the struct. Also fills in the buffer pointed to by
1074 * state_buf_p (if non-NULL) with the contents of the state struct.
1077 mdb_get_soft_state_byaddr(uintptr_t ssaddr, uint_t instance,
1078 uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state)
1080 struct i_ddi_soft_state ss;
1081 void *statep;
1084 if (mdb_vread(&ss, sizeof (ss), ssaddr) == -1)
1085 return (-1);
1087 if (instance >= ss.n_items)
1088 return (-1);
1090 if (mdb_vread(&statep, sizeof (statep), (uintptr_t)ss.array +
1091 (sizeof (statep) * instance)) == -1)
1092 return (-1);
1094 if (state_addr != NULL)
1095 *state_addr = (uintptr_t)statep;
1097 if (statep == NULL) {
1098 errno = ENOENT;
1099 return (-1);
1102 if (state_buf_p != NULL) {
1104 /* Read the state struct into the buffer in local space. */
1105 if (mdb_vread(state_buf_p, sizeof_state,
1106 (uintptr_t)statep) == -1)
1107 return (-1);
1110 return (0);
1115 * Returns a pointer to the top of the soft state struct for the instance
1116 * specified (in state_addr), given the name of the global soft state pointer
1117 * and size of the struct. Also fills in the buffer pointed to by
1118 * state_buf_p (if non-NULL) with the contents of the state struct.
1121 mdb_get_soft_state_byname(char *softstatep_name, uint_t instance,
1122 uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state)
1124 uintptr_t ssaddr;
1126 if (mdb_readvar((void *)&ssaddr, softstatep_name) == -1)
1127 return (-1);
1129 return (mdb_get_soft_state_byaddr(ssaddr, instance, state_addr,
1130 state_buf_p, sizeof_state));
1133 static const mdb_dcmd_t dcmds[] = {
1134 { "dnlc", NULL, "print DNLC contents", dnlcdump },
1135 { NULL }
1138 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds };
1140 /*ARGSUSED*/
1141 static void
1142 update_vars(void *arg)
1144 (void) mdb_readvar(&_mdb_ks_pagesize, "_pagesize");
1145 (void) mdb_readvar(&_mdb_ks_pageshift, "_pageshift");
1146 (void) mdb_readvar(&_mdb_ks_pageoffset, "_pageoffset");
1147 (void) mdb_readvar(&_mdb_ks_pagemask, "_pagemask");
1148 (void) mdb_readvar(&_mdb_ks_mmu_pagesize, "_mmu_pagesize");
1149 (void) mdb_readvar(&_mdb_ks_mmu_pageshift, "_mmu_pageshift");
1150 (void) mdb_readvar(&_mdb_ks_mmu_pageoffset, "_mmu_pageoffset");
1151 (void) mdb_readvar(&_mdb_ks_mmu_pagemask, "_mmu_pagemask");
1152 (void) mdb_readvar(&_mdb_ks_kernelbase, "_kernelbase");
1154 (void) mdb_readvar(&_mdb_ks_userlimit, "_userlimit");
1155 (void) mdb_readvar(&_mdb_ks_userlimit32, "_userlimit32");
1156 (void) mdb_readvar(&_mdb_ks_argsbase, "_argsbase");
1157 (void) mdb_readvar(&_mdb_ks_msg_bsize, "_msg_bsize");
1158 (void) mdb_readvar(&_mdb_ks_defaultstksz, "_defaultstksz");
1159 (void) mdb_readvar(&_mdb_ks_ncpu, "_ncpu");
1160 (void) mdb_readvar(&_mdb_ks_ncpu_log2, "_ncpu_log2");
1161 (void) mdb_readvar(&_mdb_ks_ncpu_p2, "_ncpu_p2");
1164 const mdb_modinfo_t *
1165 _mdb_init(void)
1168 * When used with mdb, mdb_ks is a separate dmod. With kmdb, however,
1169 * mdb_ks is compiled into the debugger module. kmdb cannot
1170 * automatically modunload itself when it exits. If it restarts after
1171 * debugger fault, static variables may not be initialized to zero.
1172 * They must be manually reinitialized here.
1174 dnlc_hash = NULL;
1175 qi_head = NULL;
1177 mdb_callback_add(MDB_CALLBACK_STCHG, update_vars, NULL);
1179 update_vars(NULL);
1181 return (&modinfo);
1184 void
1185 _mdb_fini(void)
1187 dnlc_free();
1188 while (qi_head != NULL) {
1189 mdb_qinfo_t *qip = qi_head;
1190 qi_head = qip->qi_next;
1191 mdb_free(qip, sizeof (mdb_qinfo_t));
1196 * Interface between MDB kproc target and mdb_ks. The kproc target relies
1197 * on looking up and invoking these functions in mdb_ks so that dependencies
1198 * on the current kernel implementation are isolated in mdb_ks.
1202 * Given the address of a proc_t, return the p.p_as pointer; return NULL
1203 * if we were unable to read a proc structure from the given address.
1205 uintptr_t
1206 mdb_kproc_as(uintptr_t proc_addr)
1208 proc_t p;
1210 if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p))
1211 return ((uintptr_t)p.p_as);
1213 return ((uintptr_t)NULL);
1217 * Given the address of a proc_t, return the p.p_model value; return
1218 * PR_MODEL_UNKNOWN if we were unable to read a proc structure or if
1219 * the model value does not match one of the two known values.
1221 uint_t
1222 mdb_kproc_model(uintptr_t proc_addr)
1224 proc_t p;
1226 if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p)) {
1227 switch (p.p_model) {
1228 case DATAMODEL_ILP32:
1229 return (PR_MODEL_ILP32);
1230 case DATAMODEL_LP64:
1231 return (PR_MODEL_LP64);
1235 return (PR_MODEL_UNKNOWN);
1239 * Callback function for walking process's segment list. For each segment,
1240 * we fill in an mdb_map_t describing its properties, and then invoke
1241 * the callback function provided by the kproc target.
1243 static int
1244 asmap_step(uintptr_t addr, const struct seg *seg, asmap_arg_t *asmp)
1246 struct segvn_data svd;
1247 mdb_map_t map;
1249 if (seg->s_ops == asmp->asm_segvn_ops && mdb_vread(&svd,
1250 sizeof (svd), (uintptr_t)seg->s_data) == sizeof (svd)) {
1252 if (svd.vp != NULL) {
1253 if (mdb_vnode2path((uintptr_t)svd.vp, map.map_name,
1254 MDB_TGT_MAPSZ) != 0) {
1255 (void) mdb_snprintf(map.map_name,
1256 MDB_TGT_MAPSZ, "[ vnode %p ]", svd.vp);
1258 } else
1259 (void) strcpy(map.map_name, "[ anon ]");
1261 } else {
1262 (void) mdb_snprintf(map.map_name, MDB_TGT_MAPSZ,
1263 "[ seg %p ]", addr);
1266 map.map_base = (uintptr_t)seg->s_base;
1267 map.map_size = seg->s_size;
1268 map.map_flags = 0;
1270 asmp->asm_callback((const struct mdb_map *)&map, asmp->asm_cbdata);
1271 return (WALK_NEXT);
1275 * Given a process address space, walk its segment list using the seg walker,
1276 * convert the segment data to an mdb_map_t, and pass this information
1277 * back to the kproc target via the given callback function.
1280 mdb_kproc_asiter(uintptr_t as,
1281 void (*func)(const struct mdb_map *, void *), void *p)
1283 asmap_arg_t arg;
1284 GElf_Sym sym;
1286 arg.asm_segvn_ops = NULL;
1287 arg.asm_callback = func;
1288 arg.asm_cbdata = p;
1290 if (mdb_lookup_by_name("segvn_ops", &sym) == 0)
1291 arg.asm_segvn_ops = (struct seg_ops *)(uintptr_t)sym.st_value;
1293 return (mdb_pwalk("seg", (mdb_walk_cb_t)asmap_step, &arg, as));
1297 * Copy the auxv array from the given process's u-area into the provided
1298 * buffer. If the buffer is NULL, only return the size of the auxv array
1299 * so the caller knows how much space will be required.
1302 mdb_kproc_auxv(uintptr_t proc, auxv_t *auxv)
1304 if (auxv != NULL) {
1305 proc_t p;
1307 if (mdb_vread(&p, sizeof (p), proc) != sizeof (p))
1308 return (-1);
1310 bcopy(p.p_user.u_auxv, auxv,
1311 sizeof (auxv_t) * __KERN_NAUXV_IMPL);
1314 return (__KERN_NAUXV_IMPL);
1318 * Given a process address, return the PID.
1320 pid_t
1321 mdb_kproc_pid(uintptr_t proc_addr)
1323 struct pid pid;
1324 proc_t p;
1326 if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p) &&
1327 mdb_vread(&pid, sizeof (pid), (uintptr_t)p.p_pidp) == sizeof (pid))
1328 return (pid.pid_id);
1330 return (-1);
1334 * Interface between the MDB kvm target and mdb_ks. The kvm target relies
1335 * on looking up and invoking these functions in mdb_ks so that dependencies
1336 * on the current kernel implementation are isolated in mdb_ks.
1340 * Determine whether or not the thread that panicked the given kernel was a
1341 * kernel thread (panic_thread->t_procp == &p0).
1343 void
1344 mdb_dump_print_content(dumphdr_t *dh, pid_t content)
1346 GElf_Sym sym;
1347 uintptr_t pt;
1348 uintptr_t procp;
1349 int expcont = 0;
1350 int actcont;
1352 (void) mdb_readvar(&expcont, "dump_conflags");
1353 actcont = dh->dump_flags & DF_CONTENT;
1355 if (actcont == DF_ALL) {
1356 mdb_printf("dump content: all kernel and user pages\n");
1357 return;
1358 } else if (actcont == DF_CURPROC) {
1359 mdb_printf("dump content: kernel pages and pages from "
1360 "PID %d", content);
1361 return;
1364 mdb_printf("dump content: kernel pages only\n");
1365 if (!(expcont & DF_CURPROC))
1366 return;
1368 if (mdb_readvar(&pt, "panic_thread") != sizeof (pt) ||
1369 pt == (uintptr_t)NULL)
1370 goto kthreadpanic_err;
1372 if (mdb_vread(&procp, sizeof (procp), pt + OFFSETOF(kthread_t,
1373 t_procp)) == -1 || procp == (uintptr_t)NULL)
1374 goto kthreadpanic_err;
1376 if (mdb_lookup_by_name("p0", &sym) != 0)
1377 goto kthreadpanic_err;
1379 if (procp == (uintptr_t)sym.st_value) {
1380 mdb_printf(" (curproc requested, but a kernel thread "
1381 "panicked)\n");
1382 } else {
1383 mdb_printf(" (curproc requested, but the process that "
1384 "panicked could not be dumped)\n");
1387 return;
1389 kthreadpanic_err:
1390 mdb_printf(" (curproc requested, but the process that panicked could "
1391 "not be found)\n");
1395 * Determine the process that was saved in a `curproc' dump. This process will
1396 * be recorded as the first element in dump_pids[].
1399 mdb_dump_find_curproc(void)
1401 uintptr_t pidp;
1402 pid_t pid = -1;
1404 if (mdb_readvar(&pidp, "dump_pids") == sizeof (pidp) &&
1405 mdb_vread(&pid, sizeof (pid), pidp) == sizeof (pid) &&
1406 pid > 0)
1407 return (pid);
1408 else
1409 return (-1);
1414 * Following three funcs extracted from sunddi.c
1418 * Return core address of root node of devinfo tree
1420 static uintptr_t
1421 mdb_ddi_root_node(void)
1423 uintptr_t top_devinfo_addr;
1425 /* return (top_devinfo); */
1426 if (mdb_readvar(&top_devinfo_addr, "top_devinfo") == -1) {
1427 mdb_warn("failed to read top_devinfo");
1428 return ((uintptr_t)NULL);
1430 return (top_devinfo_addr);
1434 * Return the name of the devinfo node pointed at by 'dip_addr' in the buffer
1435 * pointed at by 'name.'
1437 * - dip_addr is a pointer to a dev_info struct in core.
1439 static char *
1440 mdb_ddi_deviname(uintptr_t dip_addr, char *name, size_t name_size)
1442 uintptr_t addrname;
1443 ssize_t length;
1444 char *local_namep = name;
1445 size_t local_name_size = name_size;
1446 struct dev_info local_dip;
1449 if (dip_addr == mdb_ddi_root_node()) {
1450 if (name_size < 1) {
1451 mdb_warn("failed to get node name: buf too small\n");
1452 return (NULL);
1455 *name = '\0';
1456 return (name);
1459 if (name_size < 2) {
1460 mdb_warn("failed to get node name: buf too small\n");
1461 return (NULL);
1464 local_namep = name;
1465 *local_namep++ = '/';
1466 *local_namep = '\0';
1467 local_name_size--;
1469 if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) {
1470 mdb_warn("failed to read devinfo struct");
1473 length = mdb_readstr(local_namep, local_name_size,
1474 (uintptr_t)local_dip.devi_node_name);
1475 if (length == -1) {
1476 mdb_warn("failed to read node name");
1477 return (NULL);
1479 local_namep += length;
1480 local_name_size -= length;
1481 addrname = (uintptr_t)local_dip.devi_addr;
1483 if (addrname != (uintptr_t)NULL) {
1485 if (local_name_size < 2) {
1486 mdb_warn("not enough room for node address string");
1487 return (name);
1489 *local_namep++ = '@';
1490 *local_namep = '\0';
1491 local_name_size--;
1493 length = mdb_readstr(local_namep, local_name_size, addrname);
1494 if (length == -1) {
1495 mdb_warn("failed to read name");
1496 return (NULL);
1500 return (name);
1504 * Generate the full path under the /devices dir to the device entry.
1506 * dip is a pointer to a devinfo struct in core (not in local memory).
1508 char *
1509 mdb_ddi_pathname(uintptr_t dip_addr, char *path, size_t pathlen)
1511 struct dev_info local_dip;
1512 uintptr_t parent_dip;
1513 char *bp;
1514 size_t buf_left;
1517 if (dip_addr == mdb_ddi_root_node()) {
1518 *path = '\0';
1519 return (path);
1523 if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) {
1524 mdb_warn("failed to read devinfo struct");
1527 parent_dip = (uintptr_t)local_dip.devi_parent;
1528 (void) mdb_ddi_pathname(parent_dip, path, pathlen);
1530 bp = path + strlen(path);
1531 buf_left = pathlen - strlen(path);
1532 (void) mdb_ddi_deviname(dip_addr, bp, buf_left);
1533 return (path);
1538 * Read in the string value of a refstr, which is appended to the end of
1539 * the structure.
1541 ssize_t
1542 mdb_read_refstr(uintptr_t refstr_addr, char *str, size_t nbytes)
1544 struct refstr *r = (struct refstr *)refstr_addr;
1546 return (mdb_readstr(str, nbytes, (uintptr_t)r->rs_string));
1550 * Chase an mblk list by b_next and return the length.
1553 mdb_mblk_count(const mblk_t *mb)
1555 int count;
1556 mblk_t mblk;
1558 if (mb == NULL)
1559 return (0);
1561 count = 1;
1562 while (mb->b_next != NULL) {
1563 count++;
1564 if (mdb_vread(&mblk, sizeof (mblk), (uintptr_t)mb->b_next) ==
1566 break;
1567 mb = &mblk;
1569 return (count);
1573 * Write the given MAC address as a printable string in the usual colon-
1574 * separated format. Assumes that buflen is at least 2.
1576 void
1577 mdb_mac_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
1579 int slen;
1581 if (alen == 0 || buflen < 4) {
1582 (void) strcpy(buf, "?");
1583 return;
1585 for (;;) {
1587 * If there are more MAC address bytes available, but we won't
1588 * have any room to print them, then add "..." to the string
1589 * instead. See below for the 'magic number' explanation.
1591 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
1592 (void) strcpy(buf, "...");
1593 break;
1595 slen = mdb_snprintf(buf, buflen, "%02x", *addr++);
1596 buf += slen;
1597 if (--alen == 0)
1598 break;
1599 *buf++ = ':';
1600 buflen -= slen + 1;
1602 * At this point, based on the first 'if' statement above,
1603 * either alen == 1 and buflen >= 3, or alen > 1 and
1604 * buflen >= 4. The first case leaves room for the final "xx"
1605 * number and trailing NUL byte. The second leaves room for at
1606 * least "...". Thus the apparently 'magic' numbers chosen for
1607 * that statement.
1613 * Produce a string that represents a DLPI primitive, or NULL if no such string
1614 * is possible.
1616 const char *
1617 mdb_dlpi_prim(int prim)
1619 switch (prim) {
1620 case DL_INFO_REQ: return ("DL_INFO_REQ");
1621 case DL_INFO_ACK: return ("DL_INFO_ACK");
1622 case DL_ATTACH_REQ: return ("DL_ATTACH_REQ");
1623 case DL_DETACH_REQ: return ("DL_DETACH_REQ");
1624 case DL_BIND_REQ: return ("DL_BIND_REQ");
1625 case DL_BIND_ACK: return ("DL_BIND_ACK");
1626 case DL_UNBIND_REQ: return ("DL_UNBIND_REQ");
1627 case DL_OK_ACK: return ("DL_OK_ACK");
1628 case DL_ERROR_ACK: return ("DL_ERROR_ACK");
1629 case DL_ENABMULTI_REQ: return ("DL_ENABMULTI_REQ");
1630 case DL_DISABMULTI_REQ: return ("DL_DISABMULTI_REQ");
1631 case DL_PROMISCON_REQ: return ("DL_PROMISCON_REQ");
1632 case DL_PROMISCOFF_REQ: return ("DL_PROMISCOFF_REQ");
1633 case DL_UNITDATA_REQ: return ("DL_UNITDATA_REQ");
1634 case DL_UNITDATA_IND: return ("DL_UNITDATA_IND");
1635 case DL_UDERROR_IND: return ("DL_UDERROR_IND");
1636 case DL_PHYS_ADDR_REQ: return ("DL_PHYS_ADDR_REQ");
1637 case DL_PHYS_ADDR_ACK: return ("DL_PHYS_ADDR_ACK");
1638 case DL_SET_PHYS_ADDR_REQ: return ("DL_SET_PHYS_ADDR_REQ");
1639 case DL_NOTIFY_REQ: return ("DL_NOTIFY_REQ");
1640 case DL_NOTIFY_ACK: return ("DL_NOTIFY_ACK");
1641 case DL_NOTIFY_IND: return ("DL_NOTIFY_IND");
1642 case DL_NOTIFY_CONF: return ("DL_NOTIFY_CONF");
1643 case DL_CAPABILITY_REQ: return ("DL_CAPABILITY_REQ");
1644 case DL_CAPABILITY_ACK: return ("DL_CAPABILITY_ACK");
1645 case DL_CONTROL_REQ: return ("DL_CONTROL_REQ");
1646 case DL_CONTROL_ACK: return ("DL_CONTROL_ACK");
1647 case DL_PASSIVE_REQ: return ("DL_PASSIVE_REQ");
1648 default: return (NULL);
1653 * mdb_gethrtime() returns the hires system time. This will be the timestamp at
1654 * which we dropped into, if called from, kmdb(1); the core dump's hires time
1655 * if inspecting one; or the running system's hires time if we're inspecting
1656 * a live kernel.
1658 hrtime_t
1659 mdb_gethrtime(void)
1661 uintptr_t ptr;
1662 GElf_Sym sym;
1663 lbolt_info_t lbi;
1664 hrtime_t ts;
1667 * We first check whether the lbolt info structure has been allocated
1668 * and initialized. If not, lbolt_hybrid will be pointing at
1669 * lbolt_bootstrap.
1671 if (mdb_lookup_by_name("lbolt_bootstrap", &sym) == -1)
1672 return (0);
1674 if (mdb_readvar(&ptr, "lbolt_hybrid") == -1)
1675 return (0);
1677 if (ptr == (uintptr_t)sym.st_value)
1678 return (0);
1680 #ifdef _KMDB
1681 if (mdb_readvar(&ptr, "lb_info") == -1)
1682 return (0);
1684 if (mdb_vread(&lbi, sizeof (lbolt_info_t), ptr) !=
1685 sizeof (lbolt_info_t))
1686 return (0);
1688 ts = lbi.lbi_debug_ts;
1689 #else
1690 if (mdb_prop_postmortem) {
1691 if (mdb_readvar(&ptr, "lb_info") == -1)
1692 return (0);
1694 if (mdb_vread(&lbi, sizeof (lbolt_info_t), ptr) !=
1695 sizeof (lbolt_info_t))
1696 return (0);
1698 ts = lbi.lbi_debug_ts;
1699 } else {
1700 ts = gethrtime();
1702 #endif
1703 return (ts);
1707 * mdb_get_lbolt() returns the number of clock ticks since system boot.
1708 * Depending on the context in which it's called, the value will be derived
1709 * from different sources per mdb_gethrtime(). If inspecting a panicked
1710 * system, the routine returns the 'panic_lbolt64' variable from the core file.
1712 int64_t
1713 mdb_get_lbolt(void)
1715 lbolt_info_t lbi;
1716 uintptr_t ptr;
1717 int64_t pl;
1718 hrtime_t ts;
1719 int nsec;
1721 if (mdb_readvar(&pl, "panic_lbolt64") != -1 && pl > 0)
1722 return (pl);
1725 * mdb_gethrtime() will return zero if the lbolt info structure hasn't
1726 * been allocated and initialized yet, or if it fails to read it.
1728 if ((ts = mdb_gethrtime()) <= 0)
1729 return (0);
1732 * Load the time spent in kmdb, if any.
1734 if (mdb_readvar(&ptr, "lb_info") == -1)
1735 return (0);
1737 if (mdb_vread(&lbi, sizeof (lbolt_info_t), ptr) !=
1738 sizeof (lbolt_info_t))
1739 return (0);
1741 if (mdb_readvar(&nsec, "nsec_per_tick") == -1 || nsec == 0) {
1742 mdb_warn("failed to read 'nsec_per_tick'");
1743 return (-1);
1746 return ((ts/nsec) - lbi.lbi_debug_time);