xref: /illumos-gate/usr/src/cmd/mdb/common/modules/mdb_ks/mdb_ks.c (revision d3d50737e566cade9a08d73d2af95105ac7cd960)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
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.
30  *
31  * WARNING: Do not assume that static variables in mdb_ks will be initialized
32  * to zero.
33  */
34 
35 
36 #include <mdb/mdb_target.h>
37 #include <mdb/mdb_param.h>
38 #include <mdb/mdb_modapi.h>
39 #include <mdb/mdb_ks.h>
40 
41 #include <sys/types.h>
42 #include <sys/procfs.h>
43 #include <sys/proc.h>
44 #include <sys/dnlc.h>
45 #include <sys/autoconf.h>
46 #include <sys/machelf.h>
47 #include <sys/modctl.h>
48 #include <sys/hwconf.h>
49 #include <sys/kobj.h>
50 #include <sys/fs/autofs.h>
51 #include <sys/ddi_impldefs.h>
52 #include <sys/refstr_impl.h>
53 #include <sys/cpuvar.h>
54 #include <sys/dlpi.h>
55 #include <sys/clock_impl.h>
56 #include <errno.h>
57 
58 #include <vm/seg_vn.h>
59 #include <vm/page.h>
60 
61 #define	MDB_PATH_NELEM	256			/* Maximum path components */
62 
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;
69 
70 static int mdb_autonode2path(uintptr_t, mdb_path_t *);
71 static int mdb_sprintpath(char *, size_t, mdb_path_t *);
72 
73 /*
74  * Kernel parameters from <sys/param.h> which we keep in-core:
75  */
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 
92 /*
93  * In-core copy of DNLC information:
94  */
95 #define	MDB_DNLC_HSIZE	1024
96 #define	MDB_DNLC_HASH(vp)	(((uintptr_t)(vp) >> 3) & (MDB_DNLC_HSIZE - 1))
97 #define	MDB_DNLC_NCACHE_SZ(ncp) (sizeof (ncache_t) + (ncp)->namlen)
98 #define	MDB_DNLC_MAX_RETRY 4
99 
100 
101 static ncache_t **dnlc_hash;	/* mdbs hash array of dnlc entries */
102 
103 /*
104  * This will be the location of the vnodeops pointer for "autofs_vnodeops"
105  * The pointer still needs to be read with mdb_vread() to get the location
106  * of the vnodeops structure for autofs.
107  */
108 static struct vnodeops *autofs_vnops_ptr;
109 
110 /*
111  * STREAMS queue registrations:
112  */
113 typedef struct mdb_qinfo {
114 	const mdb_qops_t *qi_ops;	/* Address of ops vector */
115 	uintptr_t qi_addr;		/* Address of qinit structure (key) */
116 	struct mdb_qinfo *qi_next;	/* Next qinfo in list */
117 } mdb_qinfo_t;
118 
119 static mdb_qinfo_t *qi_head;		/* Head of qinfo chain */
120 
121 /*
122  * Device naming callback structure:
123  */
124 typedef struct nm_query {
125 	const char *nm_name;		/* Device driver name [in/out] */
126 	major_t nm_major;		/* Device major number [in/out] */
127 	ushort_t nm_found;		/* Did we find a match? [out] */
128 } nm_query_t;
129 
130 /*
131  * Address-to-modctl callback structure:
132  */
133 typedef struct a2m_query {
134 	uintptr_t a2m_addr;		/* Virtual address [in] */
135 	uintptr_t a2m_where;		/* Modctl address [out] */
136 } a2m_query_t;
137 
138 /*
139  * Segment-to-mdb_map callback structure:
140  */
141 typedef struct {
142 	struct seg_ops *asm_segvn_ops;	/* Address of segvn ops [in] */
143 	void (*asm_callback)(const struct mdb_map *, void *); /* Callb [in] */
144 	void *asm_cbdata;		/* Callback data [in] */
145 } asmap_arg_t;
146 
147 static void
148 dnlc_free(void)
149 {
150 	ncache_t *ncp, *next;
151 	int i;
152 
153 	if (dnlc_hash == NULL) {
154 		return;
155 	}
156 
157 	/*
158 	 * Free up current dnlc entries
159 	 */
160 	for (i = 0; i < MDB_DNLC_HSIZE; i++) {
161 		for (ncp = dnlc_hash[i]; ncp; ncp = next) {
162 			next = ncp->hash_next;
163 			mdb_free(ncp, MDB_DNLC_NCACHE_SZ(ncp));
164 		}
165 	}
166 	mdb_free(dnlc_hash, MDB_DNLC_HSIZE * sizeof (ncache_t *));
167 	dnlc_hash = NULL;
168 }
169 
170 char bad_dnlc[] = "inconsistent dnlc chain: %d, ncache va: %p"
171 	" - continuing with the rest\n";
172 
173 static int
174 dnlc_load(void)
175 {
176 	int i; /* hash index */
177 	int retry_cnt = 0;
178 	int skip_bad_chains = 0;
179 	int nc_hashsz; /* kernel hash array size */
180 	uintptr_t nc_hash_addr; /* kernel va of ncache hash array */
181 	uintptr_t head; /* kernel va of head of hash chain */
182 
183 	/*
184 	 * If we've already cached the DNLC and we're looking at a dump,
185 	 * our cache is good forever, so don't bother re-loading.
186 	 */
187 	if (dnlc_hash && mdb_prop_postmortem) {
188 		return (0);
189 	}
190 
191 	/*
192 	 * For a core dump, retries wont help.
193 	 * Just print and skip any bad chains.
194 	 */
195 	if (mdb_prop_postmortem) {
196 		skip_bad_chains = 1;
197 	}
198 retry:
199 	if (retry_cnt++ >= MDB_DNLC_MAX_RETRY) {
200 		/*
201 		 * Give up retrying the rapidly changing dnlc.
202 		 * Just print and skip any bad chains
203 		 */
204 		skip_bad_chains = 1;
205 	}
206 
207 	dnlc_free(); /* Free up the mdb hashed dnlc - if any */
208 
209 	/*
210 	 * Although nc_hashsz and the location of nc_hash doesn't currently
211 	 * change, it may do in the future with a more dynamic dnlc.
212 	 * So always read these values afresh.
213 	 */
214 	if (mdb_readvar(&nc_hashsz, "nc_hashsz") == -1) {
215 		mdb_warn("failed to read nc_hashsz");
216 		return (-1);
217 	}
218 	if (mdb_readvar(&nc_hash_addr, "nc_hash") == -1) {
219 		mdb_warn("failed to read nc_hash");
220 		return (-1);
221 	}
222 
223 	/*
224 	 * Allocate the mdb dnlc hash array
225 	 */
226 	dnlc_hash = mdb_zalloc(MDB_DNLC_HSIZE * sizeof (ncache_t *), UM_SLEEP);
227 
228 	/* for each kernel hash chain */
229 	for (i = 0, head = nc_hash_addr; i < nc_hashsz;
230 	    i++, head += sizeof (nc_hash_t)) {
231 		nc_hash_t nch; /* kernel hash chain header */
232 		ncache_t *ncp; /* name cache pointer */
233 		int hash; /* mdb hash value */
234 		uintptr_t nc_va; /* kernel va of next ncache */
235 		uintptr_t ncprev_va; /* kernel va of previous ncache */
236 		int khash; /* kernel dnlc hash value */
237 		uchar_t namelen; /* name length */
238 		ncache_t nc; /* name cache entry */
239 		int nc_size; /* size of a name cache entry */
240 
241 		/*
242 		 * We read each element of the nc_hash array individually
243 		 * just before we process the entries in its chain. This is
244 		 * because the chain can change so rapidly on a running system.
245 		 */
246 		if (mdb_vread(&nch, sizeof (nc_hash_t), head) == -1) {
247 			mdb_warn("failed to read nc_hash chain header %d", i);
248 			dnlc_free();
249 			return (-1);
250 		}
251 
252 		ncprev_va = head;
253 		nc_va = (uintptr_t)(nch.hash_next);
254 		/* for each entry in the chain */
255 		while (nc_va != head) {
256 			/*
257 			 * The size of the ncache entries varies
258 			 * because the name is appended to the structure.
259 			 * So we read in the structure then re-read
260 			 * for the structure plus name.
261 			 */
262 			if (mdb_vread(&nc, sizeof (ncache_t), nc_va) == -1) {
263 				if (skip_bad_chains) {
264 					mdb_warn(bad_dnlc, i, nc_va);
265 					break;
266 				}
267 				goto retry;
268 			}
269 			nc_size = MDB_DNLC_NCACHE_SZ(&nc);
270 			ncp = mdb_alloc(nc_size, UM_SLEEP);
271 			if (mdb_vread(ncp, nc_size - 1, nc_va) == -1) {
272 				mdb_free(ncp, nc_size);
273 				if (skip_bad_chains) {
274 					mdb_warn(bad_dnlc, i, nc_va);
275 					break;
276 				}
277 				goto retry;
278 			}
279 
280 			/*
281 			 * Check for chain consistency
282 			 */
283 			if ((uintptr_t)ncp->hash_prev != ncprev_va) {
284 				mdb_free(ncp, nc_size);
285 				if (skip_bad_chains) {
286 					mdb_warn(bad_dnlc, i, nc_va);
287 					break;
288 				}
289 				goto retry;
290 			}
291 			/*
292 			 * Terminate the new name with a null.
293 			 * Note, we allowed space for this null when
294 			 * allocating space for the entry.
295 			 */
296 			ncp->name[ncp->namlen] = '\0';
297 
298 			/*
299 			 * Validate new entry by re-hashing using the
300 			 * kernel dnlc hash function and comparing the hash
301 			 */
302 			DNLCHASH(ncp->name, ncp->dp, khash, namelen);
303 			if ((namelen != ncp->namlen) ||
304 			    (khash != ncp->hash)) {
305 				mdb_free(ncp, nc_size);
306 				if (skip_bad_chains) {
307 					mdb_warn(bad_dnlc, i, nc_va);
308 					break;
309 				}
310 				goto retry;
311 			}
312 
313 			/*
314 			 * Finally put the validated entry into the mdb
315 			 * hash chains. Reuse the kernel next hash field
316 			 * for the mdb hash chain pointer.
317 			 */
318 			hash = MDB_DNLC_HASH(ncp->vp);
319 			ncprev_va = nc_va;
320 			nc_va = (uintptr_t)(ncp->hash_next);
321 			ncp->hash_next = dnlc_hash[hash];
322 			dnlc_hash[hash] = ncp;
323 		}
324 	}
325 	return (0);
326 }
327 
328 /*ARGSUSED*/
329 int
330 dnlcdump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
331 {
332 	ncache_t *ent;
333 	int i;
334 
335 	if ((flags & DCMD_ADDRSPEC) || argc != 0)
336 		return (DCMD_USAGE);
337 
338 	if (dnlc_load() == -1)
339 		return (DCMD_ERR);
340 
341 	mdb_printf("%<u>%-?s %-?s %-32s%</u>\n", "VP", "DVP", "NAME");
342 
343 	for (i = 0; i < MDB_DNLC_HSIZE; i++) {
344 		for (ent = dnlc_hash[i]; ent != NULL; ent = ent->hash_next) {
345 			mdb_printf("%0?p %0?p %s\n",
346 			    ent->vp, ent->dp, ent->name);
347 		}
348 	}
349 
350 	return (DCMD_OK);
351 }
352 
353 static int
354 mdb_sprintpath(char *buf, size_t len, mdb_path_t *path)
355 {
356 	char *s = buf;
357 	int i;
358 
359 	if (len < sizeof ("/..."))
360 		return (-1);
361 
362 	if (!path->mdp_complete) {
363 		(void) strcpy(s, "??");
364 		s += 2;
365 
366 		if (path->mdp_nelem == 0)
367 			return (-1);
368 	}
369 
370 	if (path->mdp_nelem == 0) {
371 		(void) strcpy(s, "/");
372 		return (0);
373 	}
374 
375 	for (i = path->mdp_nelem - 1; i >= 0; i--) {
376 		/*
377 		 * Number of bytes left is the distance from where we
378 		 * are to the end, minus 2 for '/' and '\0'
379 		 */
380 		ssize_t left = (ssize_t)(&buf[len] - s) - 2;
381 
382 		if (left <= 0)
383 			break;
384 
385 		*s++ = '/';
386 		(void) strncpy(s, path->mdp_name[i], left);
387 		s[left - 1] = '\0';
388 		s += strlen(s);
389 
390 		if (left < strlen(path->mdp_name[i]))
391 			break;
392 	}
393 
394 	if (i >= 0)
395 		(void) strcpy(&buf[len - 4], "...");
396 
397 	return (0);
398 }
399 
400 static int
401 mdb_autonode2path(uintptr_t addr, mdb_path_t *path)
402 {
403 	fninfo_t fni;
404 	fnnode_t fn;
405 
406 	vnode_t vn;
407 	vfs_t vfs;
408 	struct vnodeops *autofs_vnops = NULL;
409 
410 	/*
411 	 * "autofs_vnops_ptr" is the address of the pointer to the vnodeops
412 	 * structure for autofs.  We want to read it each time we access
413 	 * it since autofs could (in theory) be unloaded and reloaded.
414 	 */
415 	if (mdb_vread(&autofs_vnops, sizeof (autofs_vnops),
416 	    (uintptr_t)autofs_vnops_ptr) == -1)
417 		return (-1);
418 
419 	if (mdb_vread(&vn, sizeof (vn), addr) == -1)
420 		return (-1);
421 
422 	if (autofs_vnops == NULL || vn.v_op != autofs_vnops)
423 		return (-1);
424 
425 	addr = (uintptr_t)vn.v_data;
426 
427 	if (mdb_vread(&vfs, sizeof (vfs), (uintptr_t)vn.v_vfsp) == -1 ||
428 	    mdb_vread(&fni, sizeof (fni), (uintptr_t)vfs.vfs_data) == -1 ||
429 	    mdb_vread(&vn, sizeof (vn), (uintptr_t)fni.fi_rootvp) == -1)
430 		return (-1);
431 
432 	for (;;) {
433 		size_t elem = path->mdp_nelem++;
434 		char elemstr[MAXNAMELEN];
435 		char *c, *p;
436 
437 		if (elem == MDB_PATH_NELEM) {
438 			path->mdp_nelem--;
439 			return (-1);
440 		}
441 
442 		if (mdb_vread(&fn, sizeof (fn), addr) != sizeof (fn)) {
443 			path->mdp_nelem--;
444 			return (-1);
445 		}
446 
447 		if (mdb_readstr(elemstr, sizeof (elemstr),
448 		    (uintptr_t)fn.fn_name) <= 0) {
449 			(void) strcpy(elemstr, "?");
450 		}
451 
452 		c = mdb_alloc(strlen(elemstr) + 1, UM_SLEEP | UM_GC);
453 		(void) strcpy(c, elemstr);
454 
455 		path->mdp_vnode[elem] = (uintptr_t)fn.fn_vnode;
456 
457 		if (addr == (uintptr_t)fn.fn_parent) {
458 			path->mdp_name[elem] = &c[1];
459 			path->mdp_complete = TRUE;
460 			break;
461 		}
462 
463 		if ((p = strrchr(c, '/')) != NULL)
464 			path->mdp_name[elem] = p + 1;
465 		else
466 			path->mdp_name[elem] = c;
467 
468 		addr = (uintptr_t)fn.fn_parent;
469 	}
470 
471 	return (0);
472 }
473 
474 int
475 mdb_vnode2path(uintptr_t addr, char *buf, size_t buflen)
476 {
477 	uintptr_t rootdir;
478 	ncache_t *ent;
479 	vnode_t vp;
480 	mdb_path_t path;
481 
482 	/*
483 	 * Check to see if we have a cached value for this vnode
484 	 */
485 	if (mdb_vread(&vp, sizeof (vp), addr) != -1 &&
486 	    vp.v_path != NULL &&
487 	    mdb_readstr(buf, buflen, (uintptr_t)vp.v_path) != -1)
488 		return (0);
489 
490 	if (dnlc_load() == -1)
491 		return (-1);
492 
493 	if (mdb_readvar(&rootdir, "rootdir") == -1) {
494 		mdb_warn("failed to read 'rootdir'");
495 		return (-1);
496 	}
497 
498 	bzero(&path, sizeof (mdb_path_t));
499 again:
500 	if ((addr == NULL) && (path.mdp_nelem == 0)) {
501 		/*
502 		 * 0 elems && complete tells sprintpath to just print "/"
503 		 */
504 		path.mdp_complete = TRUE;
505 		goto out;
506 	}
507 
508 	if (addr == rootdir) {
509 		path.mdp_complete = TRUE;
510 		goto out;
511 	}
512 
513 	for (ent = dnlc_hash[MDB_DNLC_HASH(addr)]; ent; ent = ent->hash_next) {
514 		if ((uintptr_t)ent->vp == addr) {
515 			if (strcmp(ent->name, "..") == 0 ||
516 			    strcmp(ent->name, ".") == 0)
517 				continue;
518 
519 			path.mdp_vnode[path.mdp_nelem] = (uintptr_t)ent->vp;
520 			path.mdp_name[path.mdp_nelem] = ent->name;
521 			path.mdp_nelem++;
522 
523 			if (path.mdp_nelem == MDB_PATH_NELEM) {
524 				path.mdp_nelem--;
525 				mdb_warn("path exceeded maximum expected "
526 				    "elements\n");
527 				return (-1);
528 			}
529 
530 			addr = (uintptr_t)ent->dp;
531 			goto again;
532 		}
533 	}
534 
535 	(void) mdb_autonode2path(addr, &path);
536 
537 out:
538 	return (mdb_sprintpath(buf, buflen, &path));
539 }
540 
541 
542 uintptr_t
543 mdb_pid2proc(pid_t pid, proc_t *proc)
544 {
545 	int pid_hashsz, hash;
546 	uintptr_t paddr, pidhash, procdir;
547 	struct pid pidp;
548 
549 	if (mdb_readvar(&pidhash, "pidhash") == -1)
550 		return (NULL);
551 
552 	if (mdb_readvar(&pid_hashsz, "pid_hashsz") == -1)
553 		return (NULL);
554 
555 	if (mdb_readvar(&procdir, "procdir") == -1)
556 		return (NULL);
557 
558 	hash = pid & (pid_hashsz - 1);
559 
560 	if (mdb_vread(&paddr, sizeof (paddr),
561 	    pidhash + (hash * sizeof (paddr))) == -1)
562 		return (NULL);
563 
564 	while (paddr != 0) {
565 		if (mdb_vread(&pidp, sizeof (pidp), paddr) == -1)
566 			return (NULL);
567 
568 		if (pidp.pid_id == pid) {
569 			uintptr_t procp;
570 
571 			if (mdb_vread(&procp, sizeof (procp), procdir +
572 			    (pidp.pid_prslot * sizeof (procp))) == -1)
573 				return (NULL);
574 
575 			if (proc != NULL)
576 				(void) mdb_vread(proc, sizeof (proc_t), procp);
577 
578 			return (procp);
579 		}
580 		paddr = (uintptr_t)pidp.pid_link;
581 	}
582 	return (NULL);
583 }
584 
585 int
586 mdb_cpu2cpuid(uintptr_t cpup)
587 {
588 	cpu_t cpu;
589 
590 	if (mdb_vread(&cpu, sizeof (cpu_t), cpup) != sizeof (cpu_t))
591 		return (-1);
592 
593 	return (cpu.cpu_id);
594 }
595 
596 int
597 mdb_cpuset_find(uintptr_t cpusetp)
598 {
599 	ulong_t	*cpuset;
600 	size_t nr_words = BT_BITOUL(NCPU);
601 	size_t sz = nr_words * sizeof (ulong_t);
602 	size_t	i;
603 	int cpu = -1;
604 
605 	cpuset = mdb_alloc(sz, UM_SLEEP);
606 
607 	if (mdb_vread((void *)cpuset, sz, cpusetp) != sz)
608 		goto out;
609 
610 	for (i = 0; i < nr_words; i++) {
611 		size_t j;
612 		ulong_t m;
613 
614 		for (j = 0, m = 1; j < BT_NBIPUL; j++, m <<= 1) {
615 			if (cpuset[i] & m) {
616 				cpu = i * BT_NBIPUL + j;
617 				goto out;
618 			}
619 		}
620 	}
621 
622 out:
623 	mdb_free(cpuset, sz);
624 	return (cpu);
625 }
626 
627 uintptr_t
628 mdb_vnode2page(uintptr_t vp, uintptr_t offset)
629 {
630 	long page_hashsz, ndx;
631 	uintptr_t page_hash, pp;
632 
633 	if (mdb_readvar(&page_hashsz, "page_hashsz") == -1 ||
634 	    mdb_readvar(&page_hash, "page_hash") == -1)
635 		return (NULL);
636 
637 	ndx = PAGE_HASH_FUNC(vp, offset);
638 	page_hash += ndx * sizeof (uintptr_t);
639 
640 	mdb_vread(&pp, sizeof (pp), page_hash);
641 
642 	while (pp != NULL) {
643 		page_t page;
644 
645 		mdb_vread(&page, sizeof (page), pp);
646 
647 		if ((uintptr_t)page.p_vnode == vp &&
648 		    (uintptr_t)page.p_offset == offset)
649 			return (pp);
650 
651 		pp = (uintptr_t)page.p_hash;
652 	}
653 
654 	return (NULL);
655 }
656 
657 char
658 mdb_vtype2chr(vtype_t type, mode_t mode)
659 {
660 	static const char vttab[] = {
661 		' ',	/* VNON */
662 		' ',	/* VREG */
663 		'/',	/* VDIR */
664 		' ',	/* VBLK */
665 		' ',	/* VCHR */
666 		'@',	/* VLNK */
667 		'|',	/* VFIFO */
668 		'>',	/* VDOOR */
669 		' ',	/* VPROC */
670 		'=',	/* VSOCK */
671 		' ',	/* VBAD */
672 	};
673 
674 	if (type < 0 || type >= sizeof (vttab) / sizeof (vttab[0]))
675 		return ('?');
676 
677 	if (type == VREG && (mode & 0111) != 0)
678 		return ('*');
679 
680 	return (vttab[type]);
681 }
682 
683 static int
684 a2m_walk_modctl(uintptr_t addr, const struct modctl *m, a2m_query_t *a2m)
685 {
686 	struct module mod;
687 
688 	if (m->mod_mp == NULL)
689 		return (0);
690 
691 	if (mdb_vread(&mod, sizeof (mod), (uintptr_t)m->mod_mp) == -1) {
692 		mdb_warn("couldn't read modctl %p's module", addr);
693 		return (0);
694 	}
695 
696 	if (a2m->a2m_addr >= (uintptr_t)mod.text &&
697 	    a2m->a2m_addr < (uintptr_t)mod.text + mod.text_size)
698 		goto found;
699 
700 	if (a2m->a2m_addr >= (uintptr_t)mod.data &&
701 	    a2m->a2m_addr < (uintptr_t)mod.data + mod.data_size)
702 		goto found;
703 
704 	return (0);
705 
706 found:
707 	a2m->a2m_where = addr;
708 	return (-1);
709 }
710 
711 uintptr_t
712 mdb_addr2modctl(uintptr_t addr)
713 {
714 	a2m_query_t a2m;
715 
716 	a2m.a2m_addr = addr;
717 	a2m.a2m_where = NULL;
718 
719 	(void) mdb_walk("modctl", (mdb_walk_cb_t)a2m_walk_modctl, &a2m);
720 	return (a2m.a2m_where);
721 }
722 
723 static mdb_qinfo_t *
724 qi_lookup(uintptr_t qinit_addr)
725 {
726 	mdb_qinfo_t *qip;
727 
728 	for (qip = qi_head; qip != NULL; qip = qip->qi_next) {
729 		if (qip->qi_addr == qinit_addr)
730 			return (qip);
731 	}
732 
733 	return (NULL);
734 }
735 
736 void
737 mdb_qops_install(const mdb_qops_t *qops, uintptr_t qinit_addr)
738 {
739 	mdb_qinfo_t *qip = qi_lookup(qinit_addr);
740 
741 	if (qip != NULL) {
742 		qip->qi_ops = qops;
743 		return;
744 	}
745 
746 	qip = mdb_alloc(sizeof (mdb_qinfo_t), UM_SLEEP);
747 
748 	qip->qi_ops = qops;
749 	qip->qi_addr = qinit_addr;
750 	qip->qi_next = qi_head;
751 
752 	qi_head = qip;
753 }
754 
755 void
756 mdb_qops_remove(const mdb_qops_t *qops, uintptr_t qinit_addr)
757 {
758 	mdb_qinfo_t *qip, *p = NULL;
759 
760 	for (qip = qi_head; qip != NULL; p = qip, qip = qip->qi_next) {
761 		if (qip->qi_addr == qinit_addr && qip->qi_ops == qops) {
762 			if (qi_head == qip)
763 				qi_head = qip->qi_next;
764 			else
765 				p->qi_next = qip->qi_next;
766 			mdb_free(qip, sizeof (mdb_qinfo_t));
767 			return;
768 		}
769 	}
770 }
771 
772 char *
773 mdb_qname(const queue_t *q, char *buf, size_t nbytes)
774 {
775 	struct module_info mi;
776 	struct qinit qi;
777 
778 	if (mdb_vread(&qi, sizeof (qi), (uintptr_t)q->q_qinfo) == -1) {
779 		mdb_warn("failed to read qinit at %p", q->q_qinfo);
780 		goto err;
781 	}
782 
783 	if (mdb_vread(&mi, sizeof (mi), (uintptr_t)qi.qi_minfo) == -1) {
784 		mdb_warn("failed to read module_info at %p", qi.qi_minfo);
785 		goto err;
786 	}
787 
788 	if (mdb_readstr(buf, nbytes, (uintptr_t)mi.mi_idname) <= 0) {
789 		mdb_warn("failed to read mi_idname at %p", mi.mi_idname);
790 		goto err;
791 	}
792 
793 	return (buf);
794 
795 err:
796 	(void) mdb_snprintf(buf, nbytes, "???");
797 	return (buf);
798 }
799 
800 void
801 mdb_qinfo(const queue_t *q, char *buf, size_t nbytes)
802 {
803 	mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo);
804 	buf[0] = '\0';
805 
806 	if (qip != NULL)
807 		qip->qi_ops->q_info(q, buf, nbytes);
808 }
809 
810 uintptr_t
811 mdb_qrnext(const queue_t *q)
812 {
813 	mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo);
814 
815 	if (qip != NULL)
816 		return (qip->qi_ops->q_rnext(q));
817 
818 	return (NULL);
819 }
820 
821 uintptr_t
822 mdb_qwnext(const queue_t *q)
823 {
824 	mdb_qinfo_t *qip = qi_lookup((uintptr_t)q->q_qinfo);
825 
826 	if (qip != NULL)
827 		return (qip->qi_ops->q_wnext(q));
828 
829 	return (NULL);
830 }
831 
832 uintptr_t
833 mdb_qrnext_default(const queue_t *q)
834 {
835 	return ((uintptr_t)q->q_next);
836 }
837 
838 uintptr_t
839 mdb_qwnext_default(const queue_t *q)
840 {
841 	return ((uintptr_t)q->q_next);
842 }
843 
844 /*
845  * The following three routines borrowed from modsubr.c
846  */
847 static int
848 nm_hash(const char *name)
849 {
850 	char c;
851 	int hash = 0;
852 
853 	for (c = *name++; c; c = *name++)
854 		hash ^= c;
855 
856 	return (hash & MOD_BIND_HASHMASK);
857 }
858 
859 static uintptr_t
860 find_mbind(const char *name, uintptr_t *hashtab)
861 {
862 	int hashndx;
863 	uintptr_t mb;
864 	struct bind mb_local;
865 	char node_name[MAXPATHLEN + 1];
866 
867 	hashndx = nm_hash(name);
868 	mb = hashtab[hashndx];
869 	while (mb) {
870 		if (mdb_vread(&mb_local, sizeof (mb_local), mb) == -1) {
871 			mdb_warn("failed to read struct bind at %p", mb);
872 			return (NULL);
873 		}
874 		if (mdb_readstr(node_name, sizeof (node_name),
875 		    (uintptr_t)mb_local.b_name) == -1) {
876 			mdb_warn("failed to read node name string at %p",
877 			    mb_local.b_name);
878 			return (NULL);
879 		}
880 
881 		if (strcmp(name, node_name) == 0)
882 			break;
883 
884 		mb = (uintptr_t)mb_local.b_next;
885 	}
886 	return (mb);
887 }
888 
889 int
890 mdb_name_to_major(const char *name, major_t *major)
891 {
892 	uintptr_t	mbind;
893 	uintptr_t	mb_hashtab[MOD_BIND_HASHSIZE];
894 	struct bind 	mbind_local;
895 
896 
897 	if (mdb_readsym(mb_hashtab, sizeof (mb_hashtab), "mb_hashtab") == -1) {
898 		mdb_warn("failed to read symbol 'mb_hashtab'");
899 		return (-1);
900 	}
901 
902 	if ((mbind = find_mbind(name, mb_hashtab)) != NULL) {
903 		if (mdb_vread(&mbind_local, sizeof (mbind_local), mbind) ==
904 		    -1) {
905 			mdb_warn("failed to read mbind struct at %p", mbind);
906 			return (-1);
907 		}
908 
909 		*major = (major_t)mbind_local.b_num;
910 		return (0);
911 	}
912 	return (-1);
913 }
914 
915 const char *
916 mdb_major_to_name(major_t major)
917 {
918 	static char name[MODMAXNAMELEN + 1];
919 
920 	uintptr_t devnamesp;
921 	struct devnames dn;
922 	uint_t devcnt;
923 
924 	if (mdb_readvar(&devcnt, "devcnt") == -1 || major >= devcnt ||
925 	    mdb_readvar(&devnamesp, "devnamesp") == -1)
926 		return (NULL);
927 
928 	if (mdb_vread(&dn, sizeof (struct devnames), devnamesp +
929 	    major * sizeof (struct devnames)) != sizeof (struct devnames))
930 		return (NULL);
931 
932 	if (mdb_readstr(name, MODMAXNAMELEN + 1, (uintptr_t)dn.dn_name) == -1)
933 		return (NULL);
934 
935 	return ((const char *)name);
936 }
937 
938 /*
939  * Return the name of the driver attached to the dip in drivername.
940  */
941 int
942 mdb_devinfo2driver(uintptr_t dip_addr, char *drivername, size_t namebufsize)
943 {
944 	struct dev_info	devinfo;
945 	char bind_name[MAXPATHLEN + 1];
946 	major_t	major;
947 	const char *namestr;
948 
949 
950 	if (mdb_vread(&devinfo, sizeof (devinfo), dip_addr) == -1) {
951 		mdb_warn("failed to read devinfo at %p", dip_addr);
952 		return (-1);
953 	}
954 
955 	if (mdb_readstr(bind_name, sizeof (bind_name),
956 	    (uintptr_t)devinfo.devi_binding_name) == -1) {
957 		mdb_warn("failed to read binding name at %p",
958 		    devinfo.devi_binding_name);
959 		return (-1);
960 	}
961 
962 	/*
963 	 * Many->one relation: various names to one major number
964 	 */
965 	if (mdb_name_to_major(bind_name, &major) == -1) {
966 		mdb_warn("failed to translate bind name to major number\n");
967 		return (-1);
968 	}
969 
970 	/*
971 	 * One->one relation: one major number corresponds to one driver
972 	 */
973 	if ((namestr = mdb_major_to_name(major)) == NULL) {
974 		(void) strncpy(drivername, "???", namebufsize);
975 		return (-1);
976 	}
977 
978 	(void) strncpy(drivername, namestr, namebufsize);
979 	return (0);
980 }
981 
982 /*
983  * Find the name of the driver attached to this dip (if any), given:
984  * - the address of a dip (in core)
985  * - the NAME of the global pointer to the driver's i_ddi_soft_state struct
986  * - pointer to a pointer to receive the address
987  */
988 int
989 mdb_devinfo2statep(uintptr_t dip_addr, char *soft_statep_name,
990     uintptr_t *statep)
991 {
992 	struct dev_info	dev_info;
993 
994 
995 	if (mdb_vread(&dev_info, sizeof (dev_info), dip_addr) == -1) {
996 		mdb_warn("failed to read devinfo at %p", dip_addr);
997 		return (-1);
998 	}
999 
1000 	return (mdb_get_soft_state_byname(soft_statep_name,
1001 	    dev_info.devi_instance, statep, NULL, 0));
1002 }
1003 
1004 /*
1005  * Returns a pointer to the top of the soft state struct for the instance
1006  * specified (in state_addr), given the address of the global soft state
1007  * pointer and size of the struct.  Also fills in the buffer pointed to by
1008  * state_buf_p (if non-NULL) with the contents of the state struct.
1009  */
1010 int
1011 mdb_get_soft_state_byaddr(uintptr_t ssaddr, uint_t instance,
1012     uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state)
1013 {
1014 	struct i_ddi_soft_state ss;
1015 	void *statep;
1016 
1017 
1018 	if (mdb_vread(&ss, sizeof (ss), ssaddr) == -1)
1019 		return (-1);
1020 
1021 	if (instance >= ss.n_items)
1022 		return (-1);
1023 
1024 	if (mdb_vread(&statep, sizeof (statep), (uintptr_t)ss.array +
1025 	    (sizeof (statep) * instance)) == -1)
1026 		return (-1);
1027 
1028 	if (state_addr != NULL)
1029 		*state_addr = (uintptr_t)statep;
1030 
1031 	if (statep == NULL) {
1032 		errno = ENOENT;
1033 		return (-1);
1034 	}
1035 
1036 	if (state_buf_p != NULL) {
1037 
1038 		/* Read the state struct into the buffer in local space. */
1039 		if (mdb_vread(state_buf_p, sizeof_state,
1040 		    (uintptr_t)statep) == -1)
1041 			return (-1);
1042 	}
1043 
1044 	return (0);
1045 }
1046 
1047 
1048 /*
1049  * Returns a pointer to the top of the soft state struct for the instance
1050  * specified (in state_addr), given the name of the global soft state pointer
1051  * and size of the struct.  Also fills in the buffer pointed to by
1052  * state_buf_p (if non-NULL) with the contents of the state struct.
1053  */
1054 int
1055 mdb_get_soft_state_byname(char *softstatep_name, uint_t instance,
1056     uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state)
1057 {
1058 	uintptr_t ssaddr;
1059 
1060 	if (mdb_readvar((void *)&ssaddr, softstatep_name) == -1)
1061 		return (-1);
1062 
1063 	return (mdb_get_soft_state_byaddr(ssaddr, instance, state_addr,
1064 	    state_buf_p, sizeof_state));
1065 }
1066 
1067 static const mdb_dcmd_t dcmds[] = {
1068 	{ "dnlc", NULL, "print DNLC contents", dnlcdump },
1069 	{ NULL }
1070 };
1071 
1072 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds };
1073 
1074 /*ARGSUSED*/
1075 static void
1076 update_vars(void *arg)
1077 {
1078 	GElf_Sym sym;
1079 
1080 	if (mdb_lookup_by_name("auto_vnodeops", &sym) == 0)
1081 		autofs_vnops_ptr = (struct vnodeops *)(uintptr_t)sym.st_value;
1082 	else
1083 		autofs_vnops_ptr = NULL;
1084 
1085 	(void) mdb_readvar(&_mdb_ks_pagesize, "_pagesize");
1086 	(void) mdb_readvar(&_mdb_ks_pageshift, "_pageshift");
1087 	(void) mdb_readvar(&_mdb_ks_pageoffset, "_pageoffset");
1088 	(void) mdb_readvar(&_mdb_ks_pagemask, "_pagemask");
1089 	(void) mdb_readvar(&_mdb_ks_mmu_pagesize, "_mmu_pagesize");
1090 	(void) mdb_readvar(&_mdb_ks_mmu_pageshift, "_mmu_pageshift");
1091 	(void) mdb_readvar(&_mdb_ks_mmu_pageoffset, "_mmu_pageoffset");
1092 	(void) mdb_readvar(&_mdb_ks_mmu_pagemask, "_mmu_pagemask");
1093 	(void) mdb_readvar(&_mdb_ks_kernelbase, "_kernelbase");
1094 
1095 	(void) mdb_readvar(&_mdb_ks_userlimit, "_userlimit");
1096 	(void) mdb_readvar(&_mdb_ks_userlimit32, "_userlimit32");
1097 	(void) mdb_readvar(&_mdb_ks_argsbase, "_argsbase");
1098 	(void) mdb_readvar(&_mdb_ks_msg_bsize, "_msg_bsize");
1099 	(void) mdb_readvar(&_mdb_ks_defaultstksz, "_defaultstksz");
1100 	(void) mdb_readvar(&_mdb_ks_ncpu, "_ncpu");
1101 }
1102 
1103 const mdb_modinfo_t *
1104 _mdb_init(void)
1105 {
1106 	/*
1107 	 * When used with mdb, mdb_ks is a separate dmod.  With kmdb, however,
1108 	 * mdb_ks is compiled into the debugger module.  kmdb cannot
1109 	 * automatically modunload itself when it exits.  If it restarts after
1110 	 * debugger fault, static variables may not be initialized to zero.
1111 	 * They must be manually reinitialized here.
1112 	 */
1113 	dnlc_hash = NULL;
1114 	qi_head = NULL;
1115 
1116 	mdb_callback_add(MDB_CALLBACK_STCHG, update_vars, NULL);
1117 
1118 	update_vars(NULL);
1119 
1120 	return (&modinfo);
1121 }
1122 
1123 void
1124 _mdb_fini(void)
1125 {
1126 	dnlc_free();
1127 	while (qi_head != NULL) {
1128 		mdb_qinfo_t *qip = qi_head;
1129 		qi_head = qip->qi_next;
1130 		mdb_free(qip, sizeof (mdb_qinfo_t));
1131 	}
1132 }
1133 
1134 /*
1135  * Interface between MDB kproc target and mdb_ks.  The kproc target relies
1136  * on looking up and invoking these functions in mdb_ks so that dependencies
1137  * on the current kernel implementation are isolated in mdb_ks.
1138  */
1139 
1140 /*
1141  * Given the address of a proc_t, return the p.p_as pointer; return NULL
1142  * if we were unable to read a proc structure from the given address.
1143  */
1144 uintptr_t
1145 mdb_kproc_as(uintptr_t proc_addr)
1146 {
1147 	proc_t p;
1148 
1149 	if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p))
1150 		return ((uintptr_t)p.p_as);
1151 
1152 	return (NULL);
1153 }
1154 
1155 /*
1156  * Given the address of a proc_t, return the p.p_model value; return
1157  * PR_MODEL_UNKNOWN if we were unable to read a proc structure or if
1158  * the model value does not match one of the two known values.
1159  */
1160 uint_t
1161 mdb_kproc_model(uintptr_t proc_addr)
1162 {
1163 	proc_t p;
1164 
1165 	if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p)) {
1166 		switch (p.p_model) {
1167 		case DATAMODEL_ILP32:
1168 			return (PR_MODEL_ILP32);
1169 		case DATAMODEL_LP64:
1170 			return (PR_MODEL_LP64);
1171 		}
1172 	}
1173 
1174 	return (PR_MODEL_UNKNOWN);
1175 }
1176 
1177 /*
1178  * Callback function for walking process's segment list.  For each segment,
1179  * we fill in an mdb_map_t describing its properties, and then invoke
1180  * the callback function provided by the kproc target.
1181  */
1182 static int
1183 asmap_step(uintptr_t addr, const struct seg *seg, asmap_arg_t *asmp)
1184 {
1185 	struct segvn_data svd;
1186 	mdb_map_t map;
1187 
1188 	if (seg->s_ops == asmp->asm_segvn_ops && mdb_vread(&svd,
1189 	    sizeof (svd), (uintptr_t)seg->s_data) == sizeof (svd)) {
1190 
1191 		if (svd.vp != NULL) {
1192 			if (mdb_vnode2path((uintptr_t)svd.vp, map.map_name,
1193 			    MDB_TGT_MAPSZ) != 0) {
1194 				(void) mdb_snprintf(map.map_name,
1195 				    MDB_TGT_MAPSZ, "[ vnode %p ]", svd.vp);
1196 			}
1197 		} else
1198 			(void) strcpy(map.map_name, "[ anon ]");
1199 
1200 	} else {
1201 		(void) mdb_snprintf(map.map_name, MDB_TGT_MAPSZ,
1202 		    "[ seg %p ]", addr);
1203 	}
1204 
1205 	map.map_base = (uintptr_t)seg->s_base;
1206 	map.map_size = seg->s_size;
1207 	map.map_flags = 0;
1208 
1209 	asmp->asm_callback((const struct mdb_map *)&map, asmp->asm_cbdata);
1210 	return (WALK_NEXT);
1211 }
1212 
1213 /*
1214  * Given a process address space, walk its segment list using the seg walker,
1215  * convert the segment data to an mdb_map_t, and pass this information
1216  * back to the kproc target via the given callback function.
1217  */
1218 int
1219 mdb_kproc_asiter(uintptr_t as,
1220     void (*func)(const struct mdb_map *, void *), void *p)
1221 {
1222 	asmap_arg_t arg;
1223 	GElf_Sym sym;
1224 
1225 	arg.asm_segvn_ops = NULL;
1226 	arg.asm_callback = func;
1227 	arg.asm_cbdata = p;
1228 
1229 	if (mdb_lookup_by_name("segvn_ops", &sym) == 0)
1230 		arg.asm_segvn_ops = (struct seg_ops *)(uintptr_t)sym.st_value;
1231 
1232 	return (mdb_pwalk("seg", (mdb_walk_cb_t)asmap_step, &arg, as));
1233 }
1234 
1235 /*
1236  * Copy the auxv array from the given process's u-area into the provided
1237  * buffer.  If the buffer is NULL, only return the size of the auxv array
1238  * so the caller knows how much space will be required.
1239  */
1240 int
1241 mdb_kproc_auxv(uintptr_t proc, auxv_t *auxv)
1242 {
1243 	if (auxv != NULL) {
1244 		proc_t p;
1245 
1246 		if (mdb_vread(&p, sizeof (p), proc) != sizeof (p))
1247 			return (-1);
1248 
1249 		bcopy(p.p_user.u_auxv, auxv,
1250 		    sizeof (auxv_t) * __KERN_NAUXV_IMPL);
1251 	}
1252 
1253 	return (__KERN_NAUXV_IMPL);
1254 }
1255 
1256 /*
1257  * Given a process address, return the PID.
1258  */
1259 pid_t
1260 mdb_kproc_pid(uintptr_t proc_addr)
1261 {
1262 	struct pid pid;
1263 	proc_t p;
1264 
1265 	if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p) &&
1266 	    mdb_vread(&pid, sizeof (pid), (uintptr_t)p.p_pidp) == sizeof (pid))
1267 		return (pid.pid_id);
1268 
1269 	return (-1);
1270 }
1271 
1272 /*
1273  * Interface between the MDB kvm target and mdb_ks.  The kvm target relies
1274  * on looking up and invoking these functions in mdb_ks so that dependencies
1275  * on the current kernel implementation are isolated in mdb_ks.
1276  */
1277 
1278 /*
1279  * Determine whether or not the thread that panicked the given kernel was a
1280  * kernel thread (panic_thread->t_procp == &p0).
1281  */
1282 void
1283 mdb_dump_print_content(dumphdr_t *dh, pid_t content)
1284 {
1285 	GElf_Sym sym;
1286 	uintptr_t pt;
1287 	uintptr_t procp;
1288 	int expcont = 0;
1289 	int actcont;
1290 
1291 	(void) mdb_readvar(&expcont, "dump_conflags");
1292 	actcont = dh->dump_flags & DF_CONTENT;
1293 
1294 	if (actcont == DF_ALL) {
1295 		mdb_printf("dump content: all kernel and user pages\n");
1296 		return;
1297 	} else if (actcont == DF_CURPROC) {
1298 		mdb_printf("dump content: kernel pages and pages from "
1299 		    "PID %d", content);
1300 		return;
1301 	}
1302 
1303 	mdb_printf("dump content: kernel pages only\n");
1304 	if (!(expcont & DF_CURPROC))
1305 		return;
1306 
1307 	if (mdb_readvar(&pt, "panic_thread") != sizeof (pt) || pt == NULL)
1308 		goto kthreadpanic_err;
1309 
1310 	if (mdb_vread(&procp, sizeof (procp), pt + OFFSETOF(kthread_t,
1311 	    t_procp)) == -1 || procp == NULL)
1312 		goto kthreadpanic_err;
1313 
1314 	if (mdb_lookup_by_name("p0", &sym) != 0)
1315 		goto kthreadpanic_err;
1316 
1317 	if (procp == (uintptr_t)sym.st_value) {
1318 		mdb_printf("  (curproc requested, but a kernel thread "
1319 		    "panicked)\n");
1320 	} else {
1321 		mdb_printf("  (curproc requested, but the process that "
1322 		    "panicked could not be dumped)\n");
1323 	}
1324 
1325 	return;
1326 
1327 kthreadpanic_err:
1328 	mdb_printf("  (curproc requested, but the process that panicked could "
1329 	    "not be found)\n");
1330 }
1331 
1332 /*
1333  * Determine the process that was saved in a `curproc' dump.  This process will
1334  * be recorded as the first element in dump_pids[].
1335  */
1336 int
1337 mdb_dump_find_curproc(void)
1338 {
1339 	uintptr_t pidp;
1340 	pid_t pid = -1;
1341 
1342 	if (mdb_readvar(&pidp, "dump_pids") == sizeof (pidp) &&
1343 	    mdb_vread(&pid, sizeof (pid), pidp) == sizeof (pid) &&
1344 	    pid > 0)
1345 		return (pid);
1346 	else
1347 		return (-1);
1348 }
1349 
1350 
1351 /*
1352  * Following three funcs extracted from sunddi.c
1353  */
1354 
1355 /*
1356  * Return core address of root node of devinfo tree
1357  */
1358 static uintptr_t
1359 mdb_ddi_root_node(void)
1360 {
1361 	uintptr_t	top_devinfo_addr;
1362 
1363 	/* return (top_devinfo);   */
1364 	if (mdb_readvar(&top_devinfo_addr, "top_devinfo") == -1) {
1365 		mdb_warn("failed to read top_devinfo");
1366 		return (NULL);
1367 	}
1368 	return (top_devinfo_addr);
1369 }
1370 
1371 /*
1372  * Return the name of the devinfo node pointed at by 'dip_addr' in the buffer
1373  * pointed at by 'name.'
1374  *
1375  * - dip_addr is a pointer to a dev_info struct in core.
1376  */
1377 static char *
1378 mdb_ddi_deviname(uintptr_t dip_addr, char *name, size_t name_size)
1379 {
1380 	uintptr_t addrname;
1381 	ssize_t	length;
1382 	char *local_namep = name;
1383 	size_t local_name_size = name_size;
1384 	struct dev_info	local_dip;
1385 
1386 
1387 	if (dip_addr == mdb_ddi_root_node()) {
1388 		if (name_size < 1) {
1389 			mdb_warn("failed to get node name: buf too small\n");
1390 			return (NULL);
1391 		}
1392 
1393 		*name = '\0';
1394 		return (name);
1395 	}
1396 
1397 	if (name_size < 2) {
1398 		mdb_warn("failed to get node name: buf too small\n");
1399 		return (NULL);
1400 	}
1401 
1402 	local_namep = name;
1403 	*local_namep++ = '/';
1404 	*local_namep = '\0';
1405 	local_name_size--;
1406 
1407 	if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) {
1408 		mdb_warn("failed to read devinfo struct");
1409 	}
1410 
1411 	length = mdb_readstr(local_namep, local_name_size,
1412 	    (uintptr_t)local_dip.devi_node_name);
1413 	if (length == -1) {
1414 		mdb_warn("failed to read node name");
1415 		return (NULL);
1416 	}
1417 	local_namep += length;
1418 	local_name_size -= length;
1419 	addrname = (uintptr_t)local_dip.devi_addr;
1420 
1421 	if (addrname != NULL) {
1422 
1423 		if (local_name_size < 2) {
1424 			mdb_warn("not enough room for node address string");
1425 			return (name);
1426 		}
1427 		*local_namep++ = '@';
1428 		*local_namep = '\0';
1429 		local_name_size--;
1430 
1431 		length = mdb_readstr(local_namep, local_name_size, addrname);
1432 		if (length == -1) {
1433 			mdb_warn("failed to read name");
1434 			return (NULL);
1435 		}
1436 	}
1437 
1438 	return (name);
1439 }
1440 
1441 /*
1442  * Generate the full path under the /devices dir to the device entry.
1443  *
1444  * dip is a pointer to a devinfo struct in core (not in local memory).
1445  */
1446 char *
1447 mdb_ddi_pathname(uintptr_t dip_addr, char *path, size_t pathlen)
1448 {
1449 	struct dev_info local_dip;
1450 	uintptr_t	parent_dip;
1451 	char		*bp;
1452 	size_t		buf_left;
1453 
1454 
1455 	if (dip_addr == mdb_ddi_root_node()) {
1456 		*path = '\0';
1457 		return (path);
1458 	}
1459 
1460 
1461 	if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) {
1462 		mdb_warn("failed to read devinfo struct");
1463 	}
1464 
1465 	parent_dip = (uintptr_t)local_dip.devi_parent;
1466 	(void) mdb_ddi_pathname(parent_dip, path, pathlen);
1467 
1468 	bp = path + strlen(path);
1469 	buf_left = pathlen - strlen(path);
1470 	(void) mdb_ddi_deviname(dip_addr, bp, buf_left);
1471 	return (path);
1472 }
1473 
1474 
1475 /*
1476  * Read in the string value of a refstr, which is appended to the end of
1477  * the structure.
1478  */
1479 ssize_t
1480 mdb_read_refstr(uintptr_t refstr_addr, char *str, size_t nbytes)
1481 {
1482 	struct refstr *r = (struct refstr *)refstr_addr;
1483 
1484 	return (mdb_readstr(str, nbytes, (uintptr_t)r->rs_string));
1485 }
1486 
1487 /*
1488  * Chase an mblk list by b_next and return the length.
1489  */
1490 int
1491 mdb_mblk_count(const mblk_t *mb)
1492 {
1493 	int count;
1494 	mblk_t mblk;
1495 
1496 	if (mb == NULL)
1497 		return (0);
1498 
1499 	count = 1;
1500 	while (mb->b_next != NULL) {
1501 		count++;
1502 		if (mdb_vread(&mblk, sizeof (mblk), (uintptr_t)mb->b_next) ==
1503 		    -1)
1504 			break;
1505 		mb = &mblk;
1506 	}
1507 	return (count);
1508 }
1509 
1510 /*
1511  * Write the given MAC address as a printable string in the usual colon-
1512  * separated format.  Assumes that buflen is at least 2.
1513  */
1514 void
1515 mdb_mac_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
1516 {
1517 	int slen;
1518 
1519 	if (alen == 0 || buflen < 4) {
1520 		(void) strcpy(buf, "?");
1521 		return;
1522 	}
1523 	for (;;) {
1524 		/*
1525 		 * If there are more MAC address bytes available, but we won't
1526 		 * have any room to print them, then add "..." to the string
1527 		 * instead.  See below for the 'magic number' explanation.
1528 		 */
1529 		if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
1530 			(void) strcpy(buf, "...");
1531 			break;
1532 		}
1533 		slen = mdb_snprintf(buf, buflen, "%02x", *addr++);
1534 		buf += slen;
1535 		if (--alen == 0)
1536 			break;
1537 		*buf++ = ':';
1538 		buflen -= slen + 1;
1539 		/*
1540 		 * At this point, based on the first 'if' statement above,
1541 		 * either alen == 1 and buflen >= 3, or alen > 1 and
1542 		 * buflen >= 4.  The first case leaves room for the final "xx"
1543 		 * number and trailing NUL byte.  The second leaves room for at
1544 		 * least "...".  Thus the apparently 'magic' numbers chosen for
1545 		 * that statement.
1546 		 */
1547 	}
1548 }
1549 
1550 /*
1551  * Produce a string that represents a DLPI primitive, or NULL if no such string
1552  * is possible.
1553  */
1554 const char *
1555 mdb_dlpi_prim(int prim)
1556 {
1557 	switch (prim) {
1558 	case DL_INFO_REQ:	return ("DL_INFO_REQ");
1559 	case DL_INFO_ACK:	return ("DL_INFO_ACK");
1560 	case DL_ATTACH_REQ:	return ("DL_ATTACH_REQ");
1561 	case DL_DETACH_REQ:	return ("DL_DETACH_REQ");
1562 	case DL_BIND_REQ:	return ("DL_BIND_REQ");
1563 	case DL_BIND_ACK:	return ("DL_BIND_ACK");
1564 	case DL_UNBIND_REQ:	return ("DL_UNBIND_REQ");
1565 	case DL_OK_ACK:		return ("DL_OK_ACK");
1566 	case DL_ERROR_ACK:	return ("DL_ERROR_ACK");
1567 	case DL_ENABMULTI_REQ:	return ("DL_ENABMULTI_REQ");
1568 	case DL_DISABMULTI_REQ:	return ("DL_DISABMULTI_REQ");
1569 	case DL_PROMISCON_REQ:	return ("DL_PROMISCON_REQ");
1570 	case DL_PROMISCOFF_REQ:	return ("DL_PROMISCOFF_REQ");
1571 	case DL_UNITDATA_REQ:	return ("DL_UNITDATA_REQ");
1572 	case DL_UNITDATA_IND:	return ("DL_UNITDATA_IND");
1573 	case DL_UDERROR_IND:	return ("DL_UDERROR_IND");
1574 	case DL_PHYS_ADDR_REQ:	return ("DL_PHYS_ADDR_REQ");
1575 	case DL_PHYS_ADDR_ACK:	return ("DL_PHYS_ADDR_ACK");
1576 	case DL_SET_PHYS_ADDR_REQ:	return ("DL_SET_PHYS_ADDR_REQ");
1577 	case DL_NOTIFY_REQ:	return ("DL_NOTIFY_REQ");
1578 	case DL_NOTIFY_ACK:	return ("DL_NOTIFY_ACK");
1579 	case DL_NOTIFY_IND:	return ("DL_NOTIFY_IND");
1580 	case DL_NOTIFY_CONF:	return ("DL_NOTIFY_CONF");
1581 	case DL_CAPABILITY_REQ:	return ("DL_CAPABILITY_REQ");
1582 	case DL_CAPABILITY_ACK:	return ("DL_CAPABILITY_ACK");
1583 	case DL_CONTROL_REQ:	return ("DL_CONTROL_REQ");
1584 	case DL_CONTROL_ACK:	return ("DL_CONTROL_ACK");
1585 	case DL_PASSIVE_REQ:	return ("DL_PASSIVE_REQ");
1586 	default:		return (NULL);
1587 	}
1588 }
1589 
1590 /*
1591  * mdb_gethrtime() returns the hires system time. This will be the timestamp at
1592  * which we dropped into, if called from, kmdb(1); the core dump's hires time
1593  * if inspecting one; or the running system's hires time if we're inspecting
1594  * a live kernel.
1595  */
1596 hrtime_t
1597 mdb_gethrtime(void)
1598 {
1599 	uintptr_t ptr;
1600 	lbolt_info_t lbi;
1601 	hrtime_t ts;
1602 
1603 #ifdef _KMDB
1604 	if (mdb_readvar(&ptr, "lb_info") == -1)
1605 		return (0);
1606 
1607 	if (mdb_vread(&lbi, sizeof (lbolt_info_t), ptr) !=
1608 	    sizeof (lbolt_info_t))
1609 		return (0);
1610 
1611 	ts = lbi.lbi_debug_ts;
1612 #else
1613 	if (mdb_prop_postmortem) {
1614 		if (mdb_readvar(&ptr, "lb_info") == -1)
1615 			return (0);
1616 
1617 		if (mdb_vread(&lbi, sizeof (lbolt_info_t), ptr) !=
1618 		    sizeof (lbolt_info_t))
1619 			return (0);
1620 
1621 		ts = lbi.lbi_debug_ts;
1622 	} else {
1623 		ts = gethrtime();
1624 	}
1625 #endif
1626 	return (ts);
1627 }
1628 
1629 /*
1630  * mdb_get_lbolt() returns the number of clock ticks since system boot.
1631  * Depending on the context in which it's called, the value will be derived
1632  * from different sources per mdb_gethrtime(). If inspecting a panicked
1633  * system, the routine returns the 'panic_lbolt64' variable from the core file.
1634  */
1635 int64_t
1636 mdb_get_lbolt(void)
1637 {
1638 	lbolt_info_t lbi;
1639 	uintptr_t ptr;
1640 	int64_t pl;
1641 	hrtime_t ts;
1642 	int nsec;
1643 
1644 	if (mdb_readvar(&pl, "panic_lbolt64") != -1 && pl > 0)
1645 		return (pl);
1646 
1647 	/*
1648 	 * Load the time spent in kmdb, if any.
1649 	 */
1650 	if (mdb_readvar(&ptr, "lb_info") == -1)
1651 		return (0);
1652 
1653 	if (mdb_vread(&lbi, sizeof (lbolt_info_t), ptr) !=
1654 	    sizeof (lbolt_info_t))
1655 		return (0);
1656 
1657 	if ((ts = mdb_gethrtime()) <= 0)
1658 		return (0);
1659 
1660 	if (mdb_readvar(&nsec, "nsec_per_tick") == -1 || nsec == 0) {
1661 		mdb_warn("failed to read 'nsec_per_tick'");
1662 		return (-1);
1663 	}
1664 
1665 	return ((ts/nsec) - lbi.lbi_debug_time);
1666 }
1667