xref: /illumos-gate/usr/src/cmd/mdb/common/modules/mdb_ks/mdb_ks.c (revision 60a3f738d56f92ae8b80e4b62a2331c6e1f2311f)
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 <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(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[MODMAXNAMELEN + 1];
867 
868 
869 	hashndx = nm_hash(name);
870 	mb = hashtab[hashndx];
871 	while (mb) {
872 		if (mdb_vread(&mb_local, sizeof (mb_local), mb) == -1) {
873 			mdb_warn("failed to read struct bind at %p", mb);
874 			return (NULL);
875 		}
876 		if (mdb_readstr(node_name, sizeof (node_name),
877 		    (uintptr_t)mb_local.b_name) == -1) {
878 			mdb_warn("failed to read node name string at %p",
879 				mb_local.b_name);
880 			return (NULL);
881 		}
882 
883 		if (strcmp(name, node_name) == 0)
884 			break;
885 
886 		mb = (uintptr_t)mb_local.b_next;
887 	}
888 	return (mb);
889 }
890 
891 int
892 mdb_name_to_major(const char *name, major_t *major)
893 {
894 	uintptr_t	mbind;
895 	uintptr_t	mb_hashtab[MOD_BIND_HASHSIZE];
896 	struct bind 	mbind_local;
897 
898 
899 	if (mdb_readsym(mb_hashtab, sizeof (mb_hashtab), "mb_hashtab") == -1) {
900 		mdb_warn("failed to read symbol 'mb_hashtab'");
901 		return (-1);
902 	}
903 
904 	if ((mbind = find_mbind(name, mb_hashtab)) != NULL) {
905 		if (mdb_vread(&mbind_local, sizeof (mbind_local), mbind) ==
906 		    -1) {
907 			mdb_warn("failed to read mbind struct at %p", mbind);
908 			return (-1);
909 		}
910 
911 		*major = (major_t)mbind_local.b_num;
912 		return (0);
913 	}
914 	return (-1);
915 }
916 
917 const char *
918 mdb_major_to_name(major_t major)
919 {
920 	static char name[MODMAXNAMELEN + 1];
921 
922 	uintptr_t devnamesp;
923 	struct devnames dn;
924 	uint_t devcnt;
925 
926 	if (mdb_readvar(&devcnt, "devcnt") == -1 || major >= devcnt ||
927 	    mdb_readvar(&devnamesp, "devnamesp") == -1)
928 		return (NULL);
929 
930 	if (mdb_vread(&dn, sizeof (struct devnames), devnamesp +
931 	    major * sizeof (struct devnames)) != sizeof (struct devnames))
932 		return (NULL);
933 
934 	if (mdb_readstr(name, MODMAXNAMELEN + 1, (uintptr_t)dn.dn_name) == -1)
935 		return (NULL);
936 
937 	return ((const char *)name);
938 }
939 
940 /*
941  * Return the name of the driver attached to the dip in drivername.
942  */
943 int
944 mdb_devinfo2driver(uintptr_t dip_addr, char *drivername, size_t namebufsize)
945 {
946 	struct dev_info	devinfo;
947 	char bind_name[MODMAXNAMELEN + 1];
948 	major_t	major;
949 	const char *namestr;
950 
951 
952 	if (mdb_vread(&devinfo, sizeof (devinfo), dip_addr) == -1) {
953 		mdb_warn("failed to read devinfo at %p", dip_addr);
954 		return (-1);
955 	}
956 
957 	if (mdb_readstr(bind_name, sizeof (bind_name),
958 	    (uintptr_t)devinfo.devi_binding_name) == -1) {
959 		mdb_warn("failed to read binding name at %p",
960 		    devinfo.devi_binding_name);
961 		return (-1);
962 	}
963 
964 	/*
965 	 * Many->one relation: various names to one major number
966 	 */
967 	if (mdb_name_to_major(bind_name, &major) == -1) {
968 		mdb_warn("failed to translate bind name to major number\n");
969 		return (-1);
970 	}
971 
972 	/*
973 	 * One->one relation: one major number corresponds to one driver
974 	 */
975 	if ((namestr = mdb_major_to_name(major)) == NULL) {
976 		(void) strncpy(drivername, "???", namebufsize);
977 		return (-1);
978 	}
979 
980 	(void) strncpy(drivername, namestr, namebufsize);
981 	return (0);
982 }
983 
984 /*
985  * Find the name of the driver attached to this dip (if any), given:
986  * - the address of a dip (in core)
987  * - the NAME of the global pointer to the driver's i_ddi_soft_state struct
988  * - pointer to a pointer to receive the address
989  */
990 int
991 mdb_devinfo2statep(uintptr_t dip_addr, char *soft_statep_name,
992     uintptr_t *statep)
993 {
994 	struct dev_info	dev_info;
995 
996 
997 	if (mdb_vread(&dev_info, sizeof (dev_info), dip_addr) == -1) {
998 		mdb_warn("failed to read devinfo at %p", dip_addr);
999 		return (-1);
1000 	}
1001 
1002 	return (mdb_get_soft_state_byname(soft_statep_name,
1003 	    dev_info.devi_instance, statep, NULL, 0));
1004 }
1005 
1006 /*
1007  * Returns a pointer to the top of the soft state struct for the instance
1008  * specified (in state_addr), given the address of the global soft state
1009  * pointer and size of the struct.  Also fills in the buffer pointed to by
1010  * state_buf_p (if non-NULL) with the contents of the state struct.
1011  */
1012 int
1013 mdb_get_soft_state_byaddr(uintptr_t ssaddr, uint_t instance,
1014     uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state)
1015 {
1016 	struct i_ddi_soft_state ss;
1017 	void *statep;
1018 
1019 
1020 	if (mdb_vread(&ss, sizeof (ss), ssaddr) == -1)
1021 		return (-1);
1022 
1023 	if (instance >= ss.n_items)
1024 		return (-1);
1025 
1026 	if (mdb_vread(&statep, sizeof (statep), (uintptr_t)ss.array +
1027 	    (sizeof (statep) * instance)) == -1)
1028 		return (-1);
1029 
1030 	if (state_addr != NULL)
1031 		*state_addr = (uintptr_t)statep;
1032 
1033 	if (statep == NULL) {
1034 		errno = ENOENT;
1035 		return (-1);
1036 	}
1037 
1038 	if (state_buf_p != NULL) {
1039 
1040 		/* Read the state struct into the buffer in local space. */
1041 		if (mdb_vread(state_buf_p, sizeof_state,
1042 		    (uintptr_t)statep) == -1)
1043 			return (-1);
1044 	}
1045 
1046 	return (0);
1047 }
1048 
1049 
1050 /*
1051  * Returns a pointer to the top of the soft state struct for the instance
1052  * specified (in state_addr), given the name of the global soft state pointer
1053  * and size of the struct.  Also fills in the buffer pointed to by
1054  * state_buf_p (if non-NULL) with the contents of the state struct.
1055  */
1056 int
1057 mdb_get_soft_state_byname(char *softstatep_name, uint_t instance,
1058     uintptr_t *state_addr, void *state_buf_p, size_t sizeof_state)
1059 {
1060 	uintptr_t ssaddr;
1061 
1062 	if (mdb_readvar((void *)&ssaddr, softstatep_name) == -1)
1063 		return (-1);
1064 
1065 	return (mdb_get_soft_state_byaddr(ssaddr, instance, state_addr,
1066 	    state_buf_p, sizeof_state));
1067 }
1068 
1069 static const mdb_dcmd_t dcmds[] = {
1070 	{ "dnlc", NULL, "print DNLC contents", dnlcdump },
1071 	{ NULL }
1072 };
1073 
1074 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds };
1075 
1076 /*ARGSUSED*/
1077 static void
1078 update_vars(void *arg)
1079 {
1080 	GElf_Sym sym;
1081 
1082 	if (mdb_lookup_by_name("auto_vnodeops", &sym) == 0)
1083 		autofs_vnops_ptr = (struct vnodeops *)(uintptr_t)sym.st_value;
1084 	else
1085 		autofs_vnops_ptr = NULL;
1086 
1087 	(void) mdb_readvar(&_mdb_ks_pagesize, "_pagesize");
1088 	(void) mdb_readvar(&_mdb_ks_pageshift, "_pageshift");
1089 	(void) mdb_readvar(&_mdb_ks_pageoffset, "_pageoffset");
1090 	(void) mdb_readvar(&_mdb_ks_pagemask, "_pagemask");
1091 	(void) mdb_readvar(&_mdb_ks_mmu_pagesize, "_mmu_pagesize");
1092 	(void) mdb_readvar(&_mdb_ks_mmu_pageshift, "_mmu_pageshift");
1093 	(void) mdb_readvar(&_mdb_ks_mmu_pageoffset, "_mmu_pageoffset");
1094 	(void) mdb_readvar(&_mdb_ks_mmu_pagemask, "_mmu_pagemask");
1095 	(void) mdb_readvar(&_mdb_ks_kernelbase, "_kernelbase");
1096 
1097 	(void) mdb_readvar(&_mdb_ks_userlimit, "_userlimit");
1098 	(void) mdb_readvar(&_mdb_ks_userlimit32, "_userlimit32");
1099 	(void) mdb_readvar(&_mdb_ks_argsbase, "_argsbase");
1100 	(void) mdb_readvar(&_mdb_ks_msg_bsize, "_msg_bsize");
1101 	(void) mdb_readvar(&_mdb_ks_defaultstksz, "_defaultstksz");
1102 	(void) mdb_readvar(&_mdb_ks_ncpu, "_ncpu");
1103 }
1104 
1105 const mdb_modinfo_t *
1106 _mdb_init(void)
1107 {
1108 	/*
1109 	 * When used with mdb, mdb_ks is a separate dmod.  With kmdb, however,
1110 	 * mdb_ks is compiled into the debugger module.  kmdb cannot
1111 	 * automatically modunload itself when it exits.  If it restarts after
1112 	 * debugger fault, static variables may not be initialized to zero.
1113 	 * They must be manually reinitialized here.
1114 	 */
1115 	dnlc_hash = NULL;
1116 	qi_head = NULL;
1117 
1118 	mdb_callback_add(MDB_CALLBACK_STCHG, update_vars, NULL);
1119 
1120 	update_vars(NULL);
1121 
1122 	return (&modinfo);
1123 }
1124 
1125 void
1126 _mdb_fini(void)
1127 {
1128 	dnlc_free();
1129 	while (qi_head != NULL) {
1130 		mdb_qinfo_t *qip = qi_head;
1131 		qi_head = qip->qi_next;
1132 		mdb_free(qip, sizeof (mdb_qinfo_t));
1133 	}
1134 }
1135 
1136 /*
1137  * Interface between MDB kproc target and mdb_ks.  The kproc target relies
1138  * on looking up and invoking these functions in mdb_ks so that dependencies
1139  * on the current kernel implementation are isolated in mdb_ks.
1140  */
1141 
1142 /*
1143  * Given the address of a proc_t, return the p.p_as pointer; return NULL
1144  * if we were unable to read a proc structure from the given address.
1145  */
1146 uintptr_t
1147 mdb_kproc_as(uintptr_t proc_addr)
1148 {
1149 	proc_t p;
1150 
1151 	if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p))
1152 		return ((uintptr_t)p.p_as);
1153 
1154 	return (NULL);
1155 }
1156 
1157 /*
1158  * Given the address of a proc_t, return the p.p_model value; return
1159  * PR_MODEL_UNKNOWN if we were unable to read a proc structure or if
1160  * the model value does not match one of the two known values.
1161  */
1162 uint_t
1163 mdb_kproc_model(uintptr_t proc_addr)
1164 {
1165 	proc_t p;
1166 
1167 	if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p)) {
1168 		switch (p.p_model) {
1169 		case DATAMODEL_ILP32:
1170 			return (PR_MODEL_ILP32);
1171 		case DATAMODEL_LP64:
1172 			return (PR_MODEL_LP64);
1173 		}
1174 	}
1175 
1176 	return (PR_MODEL_UNKNOWN);
1177 }
1178 
1179 /*
1180  * Callback function for walking process's segment list.  For each segment,
1181  * we fill in an mdb_map_t describing its properties, and then invoke
1182  * the callback function provided by the kproc target.
1183  */
1184 static int
1185 asmap_step(uintptr_t addr, const struct seg *seg, asmap_arg_t *asmp)
1186 {
1187 	struct segvn_data svd;
1188 	mdb_map_t map;
1189 
1190 	if (seg->s_ops == asmp->asm_segvn_ops && mdb_vread(&svd,
1191 	    sizeof (svd), (uintptr_t)seg->s_data) == sizeof (svd)) {
1192 
1193 		if (svd.vp != NULL) {
1194 			if (mdb_vnode2path((uintptr_t)svd.vp, map.map_name,
1195 				    MDB_TGT_MAPSZ) != 0) {
1196 				(void) mdb_snprintf(map.map_name,
1197 				    MDB_TGT_MAPSZ, "[ vnode %p ]", svd.vp);
1198 			}
1199 		} else
1200 			(void) strcpy(map.map_name, "[ anon ]");
1201 
1202 	} else {
1203 		(void) mdb_snprintf(map.map_name, MDB_TGT_MAPSZ,
1204 		    "[ seg %p ]", addr);
1205 	}
1206 
1207 	map.map_base = (uintptr_t)seg->s_base;
1208 	map.map_size = seg->s_size;
1209 	map.map_flags = 0;
1210 
1211 	asmp->asm_callback((const struct mdb_map *)&map, asmp->asm_cbdata);
1212 	return (WALK_NEXT);
1213 }
1214 
1215 /*
1216  * Given a process address space, walk its segment list using the seg walker,
1217  * convert the segment data to an mdb_map_t, and pass this information
1218  * back to the kproc target via the given callback function.
1219  */
1220 int
1221 mdb_kproc_asiter(uintptr_t as,
1222     void (*func)(const struct mdb_map *, void *), void *p)
1223 {
1224 	asmap_arg_t arg;
1225 	GElf_Sym sym;
1226 
1227 	arg.asm_segvn_ops = NULL;
1228 	arg.asm_callback = func;
1229 	arg.asm_cbdata = p;
1230 
1231 	if (mdb_lookup_by_name("segvn_ops", &sym) == 0)
1232 		arg.asm_segvn_ops = (struct seg_ops *)(uintptr_t)sym.st_value;
1233 
1234 	return (mdb_pwalk("seg", (mdb_walk_cb_t)asmap_step, &arg, as));
1235 }
1236 
1237 /*
1238  * Copy the auxv array from the given process's u-area into the provided
1239  * buffer.  If the buffer is NULL, only return the size of the auxv array
1240  * so the caller knows how much space will be required.
1241  */
1242 int
1243 mdb_kproc_auxv(uintptr_t proc, auxv_t *auxv)
1244 {
1245 	if (auxv != NULL) {
1246 		proc_t p;
1247 
1248 		if (mdb_vread(&p, sizeof (p), proc) != sizeof (p))
1249 			return (-1);
1250 
1251 		bcopy(p.p_user.u_auxv, auxv,
1252 		    sizeof (auxv_t) * __KERN_NAUXV_IMPL);
1253 	}
1254 
1255 	return (__KERN_NAUXV_IMPL);
1256 }
1257 
1258 /*
1259  * Given a process address, return the PID.
1260  */
1261 pid_t
1262 mdb_kproc_pid(uintptr_t proc_addr)
1263 {
1264 	struct pid pid;
1265 	proc_t p;
1266 
1267 	if (mdb_vread(&p, sizeof (p), proc_addr) == sizeof (p) &&
1268 	    mdb_vread(&pid, sizeof (pid), (uintptr_t)p.p_pidp) == sizeof (pid))
1269 		return (pid.pid_id);
1270 
1271 	return (-1);
1272 }
1273 
1274 /*
1275  * Interface between the MDB kvm target and mdb_ks.  The kvm target relies
1276  * on looking up and invoking these functions in mdb_ks so that dependencies
1277  * on the current kernel implementation are isolated in mdb_ks.
1278  */
1279 
1280 /*
1281  * Determine whether or not the thread that panicked the given kernel was a
1282  * kernel thread (panic_thread->t_procp == &p0).
1283  */
1284 void
1285 mdb_dump_print_content(dumphdr_t *dh, pid_t content)
1286 {
1287 	GElf_Sym sym;
1288 	uintptr_t pt;
1289 	uintptr_t procp;
1290 	int expcont = 0;
1291 	int actcont;
1292 
1293 	(void) mdb_readvar(&expcont, "dump_conflags");
1294 	actcont = dh->dump_flags & DF_CONTENT;
1295 
1296 	if (actcont == DF_ALL) {
1297 		mdb_printf("dump content: all kernel and user pages\n");
1298 		return;
1299 	} else if (actcont == DF_CURPROC) {
1300 		mdb_printf("dump content: kernel pages and pages from "
1301 		    "PID %d", content);
1302 		return;
1303 	}
1304 
1305 	mdb_printf("dump content: kernel pages only\n");
1306 	if (!(expcont & DF_CURPROC))
1307 		return;
1308 
1309 	if (mdb_readvar(&pt, "panic_thread") != sizeof (pt) || pt == NULL)
1310 		goto kthreadpanic_err;
1311 
1312 	if (mdb_vread(&procp, sizeof (procp), pt + OFFSETOF(kthread_t,
1313 	    t_procp)) == -1 || procp == NULL)
1314 		goto kthreadpanic_err;
1315 
1316 	if (mdb_lookup_by_name("p0", &sym) != 0)
1317 		goto kthreadpanic_err;
1318 
1319 	if (procp == (uintptr_t)sym.st_value) {
1320 		mdb_printf("  (curproc requested, but a kernel thread "
1321 		    "panicked)\n");
1322 	} else {
1323 		mdb_printf("  (curproc requested, but the process that "
1324 		    "panicked could not be dumped)\n");
1325 	}
1326 
1327 	return;
1328 
1329 kthreadpanic_err:
1330 	mdb_printf("  (curproc requested, but the process that panicked could "
1331 	    "not be found)\n");
1332 }
1333 
1334 /*
1335  * Determine the process that was saved in a `curproc' dump.  This process will
1336  * be recorded as the first element in dump_pids[].
1337  */
1338 int
1339 mdb_dump_find_curproc(void)
1340 {
1341 	uintptr_t pidp;
1342 	pid_t pid = -1;
1343 
1344 	if (mdb_readvar(&pidp, "dump_pids") == sizeof (pidp) &&
1345 	    mdb_vread(&pid, sizeof (pid), pidp) == sizeof (pid) &&
1346 	    pid > 0)
1347 		return (pid);
1348 	else
1349 		return (-1);
1350 }
1351 
1352 
1353 /*
1354  * Following three funcs extracted from sunddi.c
1355  */
1356 
1357 /*
1358  * Return core address of root node of devinfo tree
1359  */
1360 static uintptr_t
1361 mdb_ddi_root_node(void)
1362 {
1363 	uintptr_t	top_devinfo_addr;
1364 
1365 	/* return (top_devinfo);   */
1366 	if (mdb_readvar(&top_devinfo_addr, "top_devinfo") == -1) {
1367 		mdb_warn("failed to read top_devinfo");
1368 		return (NULL);
1369 	}
1370 	return (top_devinfo_addr);
1371 }
1372 
1373 /*
1374  * Return the name of the devinfo node pointed at by 'dip_addr' in the buffer
1375  * pointed at by 'name.'
1376  *
1377  * - dip_addr is a pointer to a dev_info struct in core.
1378  */
1379 static char *
1380 mdb_ddi_deviname(uintptr_t dip_addr, char *name, size_t name_size)
1381 {
1382 	uintptr_t addrname;
1383 	ssize_t	length;
1384 	char *local_namep = name;
1385 	size_t local_name_size = name_size;
1386 	struct dev_info	local_dip;
1387 
1388 
1389 	if (dip_addr == mdb_ddi_root_node()) {
1390 		if (name_size < 1) {
1391 			mdb_warn("failed to get node name: buf too small\n");
1392 			return (NULL);
1393 		}
1394 
1395 		*name = '\0';
1396 		return (name);
1397 	}
1398 
1399 	if (name_size < 2) {
1400 		mdb_warn("failed to get node name: buf too small\n");
1401 		return (NULL);
1402 	}
1403 
1404 	local_namep = name;
1405 	*local_namep++ = '/';
1406 	*local_namep = '\0';
1407 	local_name_size--;
1408 
1409 	if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) {
1410 		mdb_warn("failed to read devinfo struct");
1411 	}
1412 
1413 	length = mdb_readstr(local_namep, local_name_size,
1414 	    (uintptr_t)local_dip.devi_node_name);
1415 	if (length == -1) {
1416 		mdb_warn("failed to read node name");
1417 		return (NULL);
1418 	}
1419 	local_namep += length;
1420 	local_name_size -= length;
1421 	addrname = (uintptr_t)local_dip.devi_addr;
1422 
1423 	if (addrname != NULL) {
1424 
1425 		if (local_name_size < 2) {
1426 			mdb_warn("not enough room for node address string");
1427 			return (name);
1428 		}
1429 		*local_namep++ = '@';
1430 		*local_namep = '\0';
1431 		local_name_size--;
1432 
1433 		length = mdb_readstr(local_namep, local_name_size, addrname);
1434 		if (length == -1) {
1435 			mdb_warn("failed to read name");
1436 			return (NULL);
1437 		}
1438 	}
1439 
1440 	return (name);
1441 }
1442 
1443 /*
1444  * Generate the full path under the /devices dir to the device entry.
1445  *
1446  * dip is a pointer to a devinfo struct in core (not in local memory).
1447  */
1448 char *
1449 mdb_ddi_pathname(uintptr_t dip_addr, char *path, size_t pathlen)
1450 {
1451 	struct dev_info local_dip;
1452 	uintptr_t	parent_dip;
1453 	char		*bp;
1454 	size_t		buf_left;
1455 
1456 
1457 	if (dip_addr == mdb_ddi_root_node()) {
1458 		*path = '\0';
1459 		return (path);
1460 	}
1461 
1462 
1463 	if (mdb_vread(&local_dip, sizeof (struct dev_info), dip_addr) == -1) {
1464 		mdb_warn("failed to read devinfo struct");
1465 	}
1466 
1467 	parent_dip = (uintptr_t)local_dip.devi_parent;
1468 	(void) mdb_ddi_pathname(parent_dip, path, pathlen);
1469 
1470 	bp = path + strlen(path);
1471 	buf_left = pathlen - strlen(path);
1472 	(void) mdb_ddi_deviname(dip_addr, bp, buf_left);
1473 	return (path);
1474 }
1475 
1476 
1477 /*
1478  * Read in the string value of a refstr, which is appended to the end of
1479  * the structure.
1480  */
1481 ssize_t
1482 mdb_read_refstr(uintptr_t refstr_addr, char *str, size_t nbytes)
1483 {
1484 	struct refstr *r = (struct refstr *)refstr_addr;
1485 
1486 	return (mdb_readstr(str, nbytes, (uintptr_t)r->rs_string));
1487 }
1488 
1489 /*
1490  * Chase an mblk list by b_next and return the length.
1491  */
1492 int
1493 mdb_mblk_count(const mblk_t *mb)
1494 {
1495 	int count;
1496 	mblk_t mblk;
1497 
1498 	if (mb == NULL)
1499 		return (0);
1500 
1501 	count = 1;
1502 	while (mb->b_next != NULL) {
1503 		count++;
1504 		if (mdb_vread(&mblk, sizeof (mblk), (uintptr_t)mb->b_next) ==
1505 		    -1)
1506 			break;
1507 		mb = &mblk;
1508 	}
1509 	return (count);
1510 }
1511 
1512 /*
1513  * Write the given MAC address as a printable string in the usual colon-
1514  * separated format.  Assumes that buflen is at least 2.
1515  */
1516 void
1517 mdb_mac_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
1518 {
1519 	int slen;
1520 
1521 	if (alen == 0 || buflen < 4) {
1522 		(void) strcpy(buf, "?");
1523 		return;
1524 	}
1525 	for (;;) {
1526 		/*
1527 		 * If there are more MAC address bytes available, but we won't
1528 		 * have any room to print them, then add "..." to the string
1529 		 * instead.  See below for the 'magic number' explanation.
1530 		 */
1531 		if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
1532 			(void) strcpy(buf, "...");
1533 			break;
1534 		}
1535 		slen = mdb_snprintf(buf, buflen, "%02x", *addr++);
1536 		buf += slen;
1537 		if (--alen == 0)
1538 			break;
1539 		*buf++ = ':';
1540 		buflen -= slen + 1;
1541 		/*
1542 		 * At this point, based on the first 'if' statement above,
1543 		 * either alen == 1 and buflen >= 3, or alen > 1 and
1544 		 * buflen >= 4.  The first case leaves room for the final "xx"
1545 		 * number and trailing NUL byte.  The second leaves room for at
1546 		 * least "...".  Thus the apparently 'magic' numbers chosen for
1547 		 * that statement.
1548 		 */
1549 	}
1550 }
1551 
1552 /*
1553  * Produce a string that represents a DLPI primitive, or NULL if no such string
1554  * is possible.
1555  */
1556 const char *
1557 mdb_dlpi_prim(int prim)
1558 {
1559 	switch (prim) {
1560 	case DL_INFO_REQ:	return ("DL_INFO_REQ");
1561 	case DL_INFO_ACK:	return ("DL_INFO_ACK");
1562 	case DL_ATTACH_REQ:	return ("DL_ATTACH_REQ");
1563 	case DL_DETACH_REQ:	return ("DL_DETACH_REQ");
1564 	case DL_BIND_REQ:	return ("DL_BIND_REQ");
1565 	case DL_BIND_ACK:	return ("DL_BIND_ACK");
1566 	case DL_UNBIND_REQ:	return ("DL_UNBIND_REQ");
1567 	case DL_OK_ACK:		return ("DL_OK_ACK");
1568 	case DL_ERROR_ACK:	return ("DL_ERROR_ACK");
1569 	case DL_ENABMULTI_REQ:	return ("DL_ENABMULTI_REQ");
1570 	case DL_DISABMULTI_REQ:	return ("DL_DISABMULTI_REQ");
1571 	case DL_PROMISCON_REQ:	return ("DL_PROMISCON_REQ");
1572 	case DL_PROMISCOFF_REQ:	return ("DL_PROMISCOFF_REQ");
1573 	case DL_UNITDATA_REQ:	return ("DL_UNITDATA_REQ");
1574 	case DL_UNITDATA_IND:	return ("DL_UNITDATA_IND");
1575 	case DL_UDERROR_IND:	return ("DL_UDERROR_IND");
1576 	case DL_PHYS_ADDR_REQ:	return ("DL_PHYS_ADDR_REQ");
1577 	case DL_PHYS_ADDR_ACK:	return ("DL_PHYS_ADDR_ACK");
1578 	case DL_SET_PHYS_ADDR_REQ:	return ("DL_SET_PHYS_ADDR_REQ");
1579 	case DL_NOTIFY_REQ:	return ("DL_NOTIFY_REQ");
1580 	case DL_NOTIFY_ACK:	return ("DL_NOTIFY_ACK");
1581 	case DL_NOTIFY_IND:	return ("DL_NOTIFY_IND");
1582 	case DL_CAPABILITY_REQ:	return ("DL_CAPABILITY_REQ");
1583 	case DL_CAPABILITY_ACK:	return ("DL_CAPABILITY_ACK");
1584 	case DL_CONTROL_REQ:	return ("DL_CONTROL_REQ");
1585 	case DL_CONTROL_ACK:	return ("DL_CONTROL_ACK");
1586 	case DL_PASSIVE_REQ:	return ("DL_PASSIVE_REQ");
1587 	default:		return (NULL);
1588 	}
1589 }
1590