17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27bc54f855SJohn Levon /*
28*3b442230SJordan Paige Hendricks  * Copyright 2019 Joyent, Inc.
29bc54f855SJohn Levon  */
30bc54f855SJohn Levon 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
347c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddipropdefs.h>
367c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunldi_impl.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb_ks.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "ldi.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * ldi handle walker structure
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate typedef struct lh_walk {
49892ad162SToomas Soome 	struct ldi_handle	**hash;	/* current bucket pointer	*/
507c478bd9Sstevel@tonic-gate 	struct ldi_handle	*lhp;	/* ldi handle pointer		*/
51892ad162SToomas Soome 	size_t			index;	/* hash table index		*/
527c478bd9Sstevel@tonic-gate 	struct ldi_handle	buf;	/* buffer used for handle reads */
537c478bd9Sstevel@tonic-gate } lh_walk_t;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * ldi identifier walker structure
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate typedef struct li_walk {
59892ad162SToomas Soome 	struct ldi_ident	**hash;	/* current bucket pointer	*/
607c478bd9Sstevel@tonic-gate 	struct ldi_ident	*lip;	/* ldi handle pointer		*/
61892ad162SToomas Soome 	size_t			index;	/* hash table index		*/
627c478bd9Sstevel@tonic-gate 	struct ldi_ident	buf;	/* buffer used for ident reads */
637c478bd9Sstevel@tonic-gate } li_walk_t;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Options for ldi_handles dcmd
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	LH_IDENTINFO	0x1
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * LDI walkers
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate int
ldi_handle_walk_init(mdb_walk_state_t * wsp)747c478bd9Sstevel@tonic-gate ldi_handle_walk_init(mdb_walk_state_t *wsp)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	lh_walk_t	*lhwp;
777c478bd9Sstevel@tonic-gate 	GElf_Sym	sym;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/* get the address of the hash table */
807c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_name("ldi_handle_hash", &sym) == -1) {
817c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't find ldi_handle_hash");
827c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	lhwp = mdb_alloc(sizeof (lh_walk_t), UM_SLEEP|UM_GC);
867c478bd9Sstevel@tonic-gate 	lhwp->hash = (struct ldi_handle **)(uintptr_t)sym.st_value;
877c478bd9Sstevel@tonic-gate 	lhwp->index = 0;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/* get the address of the first element in the first hash bucket */
907c478bd9Sstevel@tonic-gate 	if ((mdb_vread(&lhwp->lhp, sizeof (struct ldi_handle *),
917c478bd9Sstevel@tonic-gate 	    (uintptr_t)lhwp->hash)) == -1) {
927c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read ldi handle hash at %p", lhwp->hash);
937c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)lhwp->lhp;
977c478bd9Sstevel@tonic-gate 	wsp->walk_data = lhwp;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate int
ldi_handle_walk_step(mdb_walk_state_t * wsp)1037c478bd9Sstevel@tonic-gate ldi_handle_walk_step(mdb_walk_state_t *wsp)
1047c478bd9Sstevel@tonic-gate {
105892ad162SToomas Soome 	lh_walk_t	*lhwp = (lh_walk_t *)wsp->walk_data;
1067c478bd9Sstevel@tonic-gate 	int		status;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/* check if we need to go to the next hash bucket */
109892ad162SToomas Soome 	while (wsp->walk_addr == 0) {
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		/* advance to the next bucket */
1127c478bd9Sstevel@tonic-gate 		if (++(lhwp->index) >= LH_HASH_SZ)
1137c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		/* get handle address from the hash bucket */
1167c478bd9Sstevel@tonic-gate 		if ((mdb_vread(&lhwp->lhp, sizeof (struct ldi_handle *),
1177c478bd9Sstevel@tonic-gate 		    (uintptr_t)(lhwp->hash + lhwp->index))) == -1) {
1187c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read ldi handle hash at %p",
1197c478bd9Sstevel@tonic-gate 			    (uintptr_t)lhwp->hash + lhwp->index);
1207c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		wsp->walk_addr = (uintptr_t)lhwp->lhp;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* invoke the walker callback for this hash element */
1277c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1287c478bd9Sstevel@tonic-gate 	if (status != WALK_NEXT)
1297c478bd9Sstevel@tonic-gate 		return (status);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* get a pointer to the next hash element */
1327c478bd9Sstevel@tonic-gate 	if (mdb_vread(&lhwp->buf, sizeof (struct ldi_handle),
1337c478bd9Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
1347c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read ldi handle at %p", wsp->walk_addr);
1357c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)lhwp->buf.lh_next;
1387c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate int
ldi_ident_walk_init(mdb_walk_state_t * wsp)1427c478bd9Sstevel@tonic-gate ldi_ident_walk_init(mdb_walk_state_t *wsp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	li_walk_t	*liwp;
1457c478bd9Sstevel@tonic-gate 	GElf_Sym	sym;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* get the address of the hash table */
1487c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_name("ldi_ident_hash", &sym) == -1) {
1497c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't find ldi_ident_hash");
1507c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	liwp = mdb_alloc(sizeof (li_walk_t), UM_SLEEP|UM_GC);
1547c478bd9Sstevel@tonic-gate 	liwp->hash = (struct ldi_ident **)(uintptr_t)sym.st_value;
1557c478bd9Sstevel@tonic-gate 	liwp->index = 0;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/* get the address of the first element in the first hash bucket */
1587c478bd9Sstevel@tonic-gate 	if ((mdb_vread(&liwp->lip, sizeof (struct ldi_ident *),
1597c478bd9Sstevel@tonic-gate 	    (uintptr_t)liwp->hash)) == -1) {
1607c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read ldi ident hash at %p", liwp->hash);
1617c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)liwp->lip;
1657c478bd9Sstevel@tonic-gate 	wsp->walk_data = liwp;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate int
ldi_ident_walk_step(mdb_walk_state_t * wsp)1717c478bd9Sstevel@tonic-gate ldi_ident_walk_step(mdb_walk_state_t *wsp)
1727c478bd9Sstevel@tonic-gate {
173892ad162SToomas Soome 	li_walk_t	*liwp = (li_walk_t *)wsp->walk_data;
1747c478bd9Sstevel@tonic-gate 	int		status;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* check if we need to go to the next hash bucket */
177892ad162SToomas Soome 	while (wsp->walk_addr == 0) {
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		/* advance to the next bucket */
1807c478bd9Sstevel@tonic-gate 		if (++(liwp->index) >= LI_HASH_SZ)
1817c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		/* get handle address from the hash bucket */
1847c478bd9Sstevel@tonic-gate 		if ((mdb_vread(&liwp->lip, sizeof (struct ldi_ident *),
1857c478bd9Sstevel@tonic-gate 		    (uintptr_t)(liwp->hash + liwp->index))) == -1) {
1867c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read ldi ident hash at %p",
1877c478bd9Sstevel@tonic-gate 			    (uintptr_t)liwp->hash + liwp->index);
1887c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		wsp->walk_addr = (uintptr_t)liwp->lip;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* invoke the walker callback for this hash element */
1957c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata);
1967c478bd9Sstevel@tonic-gate 	if (status != WALK_NEXT)
1977c478bd9Sstevel@tonic-gate 		return (status);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* get a pointer to the next hash element */
2007c478bd9Sstevel@tonic-gate 	if (mdb_vread(&liwp->buf, sizeof (struct ldi_ident),
2017c478bd9Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
2027c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read ldi ident at %p", wsp->walk_addr);
2037c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)liwp->buf.li_next;
2067c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * LDI dcmds
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static void
ldi_ident_header(int start,int refs)2137c478bd9Sstevel@tonic-gate ldi_ident_header(int start, int refs)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	if (start) {
2167c478bd9Sstevel@tonic-gate 		mdb_printf("%-?s ", "IDENT");
2177c478bd9Sstevel@tonic-gate 	} else {
2187c478bd9Sstevel@tonic-gate 		mdb_printf("%?s ", "IDENT");
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (refs)
2227c478bd9Sstevel@tonic-gate 		mdb_printf("%4s ", "REFS");
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	mdb_printf("%?s %5s %5s %s\n", "DIP", "MINOR", "MODID", "MODULE NAME");
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static int
ldi_ident_print(uintptr_t addr,int refs)2287c478bd9Sstevel@tonic-gate ldi_ident_print(uintptr_t addr, int refs)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	struct ldi_ident	li;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* read the ldi ident */
2337c478bd9Sstevel@tonic-gate 	if (mdb_vread(&li, sizeof (struct ldi_ident), addr) == -1) {
2347c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read ldi ident at %p", addr);
2357c478bd9Sstevel@tonic-gate 		return (1);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/* display the ident address */
2397c478bd9Sstevel@tonic-gate 	mdb_printf("%0?p ", addr);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* display the ref count */
2427c478bd9Sstevel@tonic-gate 	if (refs)
2437c478bd9Sstevel@tonic-gate 		mdb_printf("%4u ", li.li_ref);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* display the dip (if any) */
2467c478bd9Sstevel@tonic-gate 	if (li.li_dip != NULL) {
2477c478bd9Sstevel@tonic-gate 		mdb_printf("%0?p ", li.li_dip);
2487c478bd9Sstevel@tonic-gate 	} else {
2497c478bd9Sstevel@tonic-gate 		mdb_printf("%?s ", "-");
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/* display the minor node (if any) */
2537c478bd9Sstevel@tonic-gate 	if (li.li_dev != DDI_DEV_T_NONE) {
2547c478bd9Sstevel@tonic-gate 		mdb_printf("%5u ", getminor(li.li_dev));
2557c478bd9Sstevel@tonic-gate 	} else {
2567c478bd9Sstevel@tonic-gate 		mdb_printf("%5s ", "-");
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/* display the module info */
2607c478bd9Sstevel@tonic-gate 	mdb_printf("%5d %s\n", li.li_modid, li.li_modname);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (0);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate int
ldi_ident(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2667c478bd9Sstevel@tonic-gate ldi_ident(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int	start = 1;
2697c478bd9Sstevel@tonic-gate 	int	refs = 1;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* Determine if there is an ldi identifier address */
2727c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2737c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("ldi_ident", "ldi_ident",
2747c478bd9Sstevel@tonic-gate 		    argc, argv) == -1) {
2757c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk ldi idents");
2767c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/* display the header line */
2827c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
2837c478bd9Sstevel@tonic-gate 		ldi_ident_header(start, refs);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/* display the ldi ident */
2867c478bd9Sstevel@tonic-gate 	if (ldi_ident_print(addr, refs))
2877c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate static void
ldi_handle_header(int refs,int ident)293*3b442230SJordan Paige Hendricks ldi_handle_header(int refs, int ident)
294*3b442230SJordan Paige Hendricks {
2957c478bd9Sstevel@tonic-gate 	mdb_printf("%-?s ", "HANDLE");
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (refs)
2987c478bd9Sstevel@tonic-gate 		mdb_printf("%4s ", "REFS");
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	mdb_printf("%?s %10s %5s %?s ", "VNODE", "DRV", "MINOR", "EVENTS");
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (!ident) {
3037c478bd9Sstevel@tonic-gate 		mdb_printf("%?s\n", "IDENT");
3047c478bd9Sstevel@tonic-gate 	} else {
3057c478bd9Sstevel@tonic-gate 		ldi_ident_header(0, 0);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate static int
ldi_handle_print(uintptr_t addr,int ident,int refs)3107c478bd9Sstevel@tonic-gate ldi_handle_print(uintptr_t addr, int ident, int refs)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	vnode_t			vnode;
3137c478bd9Sstevel@tonic-gate 	struct ldi_handle	lh;
3147c478bd9Sstevel@tonic-gate 	const char		*name;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* read in the ldi handle */
3177c478bd9Sstevel@tonic-gate 	if (mdb_vread(&lh, sizeof (struct ldi_handle), addr) == -1) {
3187c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read ldi handle at %p", addr);
3197c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* display the handle address */
3237c478bd9Sstevel@tonic-gate 	mdb_printf("%0?p ", addr);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/* display the ref count */
3267c478bd9Sstevel@tonic-gate 	if (refs)
3277c478bd9Sstevel@tonic-gate 		mdb_printf("%4u ", lh.lh_ref);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/* display the vnode */
3307c478bd9Sstevel@tonic-gate 	mdb_printf("%0?p ", lh.lh_vp);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/* read in the vnode associated with the handle */
3337c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)lh.lh_vp;
3347c478bd9Sstevel@tonic-gate 	if (mdb_vread(&vnode, sizeof (vnode_t), addr) == -1) {
3357c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read vnode at %p", addr);
3367c478bd9Sstevel@tonic-gate 		return (1);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/* display the driver name */
3407c478bd9Sstevel@tonic-gate 	if ((name = mdb_major_to_name(getmajor(vnode.v_rdev))) == NULL) {
3417c478bd9Sstevel@tonic-gate 		mdb_warn("failed to convert major number to name\n");
3427c478bd9Sstevel@tonic-gate 		return (1);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	mdb_printf("%10s ", name);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* display the minor number */
3477c478bd9Sstevel@tonic-gate 	mdb_printf("%5d ", getminor(vnode.v_rdev));
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* display the event pointer (if any) */
3507c478bd9Sstevel@tonic-gate 	if (lh.lh_events != NULL) {
3517c478bd9Sstevel@tonic-gate 		mdb_printf("%0?p ", lh.lh_events);
3527c478bd9Sstevel@tonic-gate 	} else {
3537c478bd9Sstevel@tonic-gate 		mdb_printf("%?s ", "-");
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (!ident) {
3577c478bd9Sstevel@tonic-gate 		/* display the ident address */
3587c478bd9Sstevel@tonic-gate 		mdb_printf("%0?p\n", lh.lh_ident);
3597c478bd9Sstevel@tonic-gate 		return (0);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* display the entire ident  */
3637c478bd9Sstevel@tonic-gate 	return (ldi_ident_print((uintptr_t)lh.lh_ident, refs));
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate int
ldi_handle(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)3677c478bd9Sstevel@tonic-gate ldi_handle(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	int			ident = 0;
3707c478bd9Sstevel@tonic-gate 	int			refs = 1;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
373*3b442230SJordan Paige Hendricks 	    'i', MDB_OPT_SETBITS, TRUE, &ident, NULL) != argc)
3747c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if (ident)
3777c478bd9Sstevel@tonic-gate 		refs = 0;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* Determine if there is an ldi handle address */
3807c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
3817c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("ldi_handle", "ldi_handle",
3827c478bd9Sstevel@tonic-gate 		    argc, argv) == -1) {
3837c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk ldi handles");
3847c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
385bc54f855SJohn Levon 		}
386bc54f855SJohn Levon 		return (DCMD_OK);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/* display the header line */
3907c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
3917c478bd9Sstevel@tonic-gate 		ldi_handle_header(refs, ident);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* display the ldi handle */
3947c478bd9Sstevel@tonic-gate 	if (ldi_handle_print(addr, ident, refs))
3957c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate void
ldi_ident_help(void)4017c478bd9Sstevel@tonic-gate ldi_ident_help(void)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	mdb_printf("Displays an ldi identifier.\n"
4047c478bd9Sstevel@tonic-gate 	    "Without the address of an \"ldi_ident_t\", "
4057c478bd9Sstevel@tonic-gate 	    "print all identifiers.\n"
4067c478bd9Sstevel@tonic-gate 	    "With an address, print the specified identifier.\n");
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate void
ldi_handle_help(void)4107c478bd9Sstevel@tonic-gate ldi_handle_help(void)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	mdb_printf("Displays an ldi handle.\n"
4137c478bd9Sstevel@tonic-gate 	    "Without the address of an \"ldi_handle_t\", "
4147c478bd9Sstevel@tonic-gate 	    "print all handles.\n"
4157c478bd9Sstevel@tonic-gate 	    "With an address, print the specified handle.\n\n"
4167c478bd9Sstevel@tonic-gate 	    "Switches:\n"
4177c478bd9Sstevel@tonic-gate 	    "  -i  print the module identifier information\n");
4187c478bd9Sstevel@tonic-gate }
419