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