xref: /illumos-gate/usr/src/cmd/mdb/common/modules/mdb_ks/mdb_ks.c (revision 560f878bce5cdf0661659001415019ca5c8a01b4)
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 2006 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 <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(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[MODMAXNAMELEN + 1];
866 
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[MODMAXNAMELEN + 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