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 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <sys/modhash_impl.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
327c478bd9Sstevel@tonic-gate #include <mdb/mdb_ks.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "modhash.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* This is passed to the modent callback; allows caller to get context */
377c478bd9Sstevel@tonic-gate typedef struct modent_step_data_s {
387c478bd9Sstevel@tonic-gate 	struct mod_hash_entry	msd_mhe;	/* must be first */
397c478bd9Sstevel@tonic-gate 	int			msd_hash_index;
407c478bd9Sstevel@tonic-gate 	int			msd_position;	/* entry position in chain */
417c478bd9Sstevel@tonic-gate 	uintptr_t		msd_first_addr;	/* first address in chain */
427c478bd9Sstevel@tonic-gate } modent_step_data_t;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* Context for a walk over a modhash (variable length) */
457c478bd9Sstevel@tonic-gate typedef struct hash_walk_s {
467c478bd9Sstevel@tonic-gate 	modent_step_data_t	hwalk_msd;	/* current entry data */
477c478bd9Sstevel@tonic-gate 	mod_hash_t		hwalk_hash;	/* always last (var. len) */
487c478bd9Sstevel@tonic-gate } hash_walk_t;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Computes number of bytes to allocate for hash_walk_t structure. */
517c478bd9Sstevel@tonic-gate #define	HW_SIZE(n)	(sizeof (modent_step_data_t) + MH_SIZE(n))
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* Used for decoding hash keys for display */
547c478bd9Sstevel@tonic-gate typedef struct hash_type_entry_s {
557c478bd9Sstevel@tonic-gate 	const char *hte_type;		/* name of hash type for ::modent -t */
567c478bd9Sstevel@tonic-gate 	const char *hte_comparator;	/* name of comparator function */
577c478bd9Sstevel@tonic-gate 	void (*hte_format)(const mod_hash_key_t, char *, size_t);
587c478bd9Sstevel@tonic-gate } hash_type_entry_t;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static void format_strhash(const mod_hash_key_t, char *, size_t);
617c478bd9Sstevel@tonic-gate static void format_ptrhash(const mod_hash_key_t, char *, size_t);
627c478bd9Sstevel@tonic-gate static void format_idhash(const mod_hash_key_t, char *, size_t);
637c478bd9Sstevel@tonic-gate static void format_default(const mod_hash_key_t, char *, size_t);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static const hash_type_entry_t hte_table[] = {
667c478bd9Sstevel@tonic-gate 	{ "str", "mod_hash_strkey_cmp", format_strhash },
677c478bd9Sstevel@tonic-gate 	{ "ptr", "mod_hash_ptrkey_cmp", format_ptrhash },
687c478bd9Sstevel@tonic-gate 	{ "id", "mod_hash_idkey_cmp", format_idhash },
697c478bd9Sstevel@tonic-gate 	{ NULL, NULL, format_default }
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static int modent_print(uintptr_t, int, uint_t, const hash_type_entry_t *,
737c478bd9Sstevel@tonic-gate     boolean_t, uint_t, uint_t);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* The information used during a walk */
767c478bd9Sstevel@tonic-gate typedef struct mod_walk_data_s {
777c478bd9Sstevel@tonic-gate 	const hash_type_entry_t	*mwd_hte;	/* pointer to entry type */
787c478bd9Sstevel@tonic-gate 	int			mwd_main_flags;	/* ::modhash flags */
797c478bd9Sstevel@tonic-gate 	int			mwd_flags;	/* DCMD_* flags for looping */
807c478bd9Sstevel@tonic-gate 	uint_t			mwd_opt_e;	/* call-modent mode */
817c478bd9Sstevel@tonic-gate 	uint_t			mwd_opt_c;	/* chain head only mode */
827c478bd9Sstevel@tonic-gate 	uint_t			mwd_opt_h;	/* hash index output */
837c478bd9Sstevel@tonic-gate 	boolean_t		mwd_opt_k_set;	/* key supplied */
847c478bd9Sstevel@tonic-gate 	boolean_t		mwd_opt_v_set;	/* value supplied */
857c478bd9Sstevel@tonic-gate 	uintptr_t		mwd_opt_k;	/* key */
867c478bd9Sstevel@tonic-gate 	uintptr_t		mwd_opt_v;	/* value */
877c478bd9Sstevel@tonic-gate 	int			mwd_maxposn;	/* len of longest chain - 1 */
887c478bd9Sstevel@tonic-gate 	int			mwd_maxidx;	/* hash idx of longest chain */
897c478bd9Sstevel@tonic-gate 	uintptr_t		mwd_maxaddr;	/* addr of 1st elem @ maxidx */
907c478bd9Sstevel@tonic-gate 	uintptr_t		mwd_idxtoprint;	/* desired hash pos to print */
917c478bd9Sstevel@tonic-gate 	uintptr_t		mwd_addr;	/* 1st elem addr @idxtoprint */
927c478bd9Sstevel@tonic-gate } mod_walk_data_t;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Initialize a walk over all the modhashes in the system.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate int
modhash_walk_init(mdb_walk_state_t * wsp)987c478bd9Sstevel@tonic-gate modhash_walk_init(mdb_walk_state_t *wsp)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	mod_hash_t *mh_head;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (mdb_readvar(&mh_head, "mh_head") == -1) {
1037c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mh_head");
1047c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)mh_head;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Step to the next modhash in the system.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate int
modhash_walk_step(mdb_walk_state_t * wsp)1157c478bd9Sstevel@tonic-gate modhash_walk_step(mdb_walk_state_t *wsp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	mod_hash_t mh;
1187c478bd9Sstevel@tonic-gate 	int status;
1197c478bd9Sstevel@tonic-gate 
120*892ad162SToomas Soome 	if (wsp->walk_addr == 0)
1217c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mh, sizeof (mh), wsp->walk_addr) == -1) {
1247c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mod_hash_t at %p", wsp->walk_addr);
1257c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &mh, wsp->walk_cbdata);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)mh.mh_next;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (status);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Initialize a walk over the entries in a given modhash.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate int
modent_walk_init(mdb_walk_state_t * wsp)1397c478bd9Sstevel@tonic-gate modent_walk_init(mdb_walk_state_t *wsp)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	mod_hash_t mh;
1427c478bd9Sstevel@tonic-gate 	hash_walk_t *hwp;
1437c478bd9Sstevel@tonic-gate 	int retv;
1447c478bd9Sstevel@tonic-gate 
145*892ad162SToomas Soome 	if (wsp->walk_addr == 0) {
1467c478bd9Sstevel@tonic-gate 		mdb_warn("mod_hash_t address required\n");
1477c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mh, sizeof (mh), wsp->walk_addr) == -1) {
1517c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mod_hash_t at %p", wsp->walk_addr);
1527c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (mh.mh_nchains <= 1) {
1567c478bd9Sstevel@tonic-gate 		mdb_warn("impossible number of chains in mod_hash_t at %p",
1577c478bd9Sstevel@tonic-gate 		    wsp->walk_addr);
1587c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * If the user presents us with a garbage pointer, and thus the number
1637c478bd9Sstevel@tonic-gate 	 * of chains is just absurd, we don't want to bail out of mdb.  Fail to
1647c478bd9Sstevel@tonic-gate 	 * walk instead.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	hwp = mdb_alloc(HW_SIZE(mh.mh_nchains), UM_NOSLEEP);
1677c478bd9Sstevel@tonic-gate 	if (hwp == NULL) {
1687c478bd9Sstevel@tonic-gate 		mdb_warn("unable to allocate %#x bytes for mod_hash_t at %p",
1697c478bd9Sstevel@tonic-gate 		    HW_SIZE(mh.mh_nchains), wsp->walk_addr);
1707c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	(void) memcpy(&hwp->hwalk_hash, &mh, sizeof (hwp->hwalk_hash));
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	retv = mdb_vread(hwp->hwalk_hash.mh_entries + 1,
1767c478bd9Sstevel@tonic-gate 	    (mh.mh_nchains - 1) * sizeof (struct mod_hash_entry *),
1777c478bd9Sstevel@tonic-gate 	    wsp->walk_addr + sizeof (mh));
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (retv == -1) {
1807c478bd9Sstevel@tonic-gate 		mdb_free(hwp, HW_SIZE(mh.mh_nchains));
1817c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read %#x mod_hash_entry pointers at %p",
1827c478bd9Sstevel@tonic-gate 		    mh.mh_nchains - 1, wsp->walk_addr + sizeof (mh));
1837c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	hwp->hwalk_msd.msd_hash_index = -1;
1877c478bd9Sstevel@tonic-gate 	hwp->hwalk_msd.msd_position = 0;
188*892ad162SToomas Soome 	hwp->hwalk_msd.msd_first_addr = 0;
1897c478bd9Sstevel@tonic-gate 
190*892ad162SToomas Soome 	wsp->walk_addr = 0;
1917c478bd9Sstevel@tonic-gate 	wsp->walk_data = hwp;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Step to the next entry in the modhash.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate int
modent_walk_step(mdb_walk_state_t * wsp)2007c478bd9Sstevel@tonic-gate modent_walk_step(mdb_walk_state_t *wsp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	hash_walk_t *hwp = wsp->walk_data;
2037c478bd9Sstevel@tonic-gate 	int status;
2047c478bd9Sstevel@tonic-gate 
205*892ad162SToomas Soome 	while (wsp->walk_addr == 0) {
2067c478bd9Sstevel@tonic-gate 		hwp->hwalk_msd.msd_position = 0;
2077c478bd9Sstevel@tonic-gate 		if (++hwp->hwalk_msd.msd_hash_index >=
2087c478bd9Sstevel@tonic-gate 		    hwp->hwalk_hash.mh_nchains)
2097c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
2107c478bd9Sstevel@tonic-gate 		wsp->walk_addr = hwp->hwalk_msd.msd_first_addr =
2117c478bd9Sstevel@tonic-gate 		    (uintptr_t)hwp->hwalk_hash.mh_entries[
2127c478bd9Sstevel@tonic-gate 		    hwp->hwalk_msd.msd_hash_index];
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (mdb_vread(&hwp->hwalk_msd.msd_mhe, sizeof (hwp->hwalk_msd.msd_mhe),
2167c478bd9Sstevel@tonic-gate 	    wsp->walk_addr) == -1) {
2177c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mod_hash_entry at %p",
2187c478bd9Sstevel@tonic-gate 		    wsp->walk_addr);
2197c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &hwp->hwalk_msd,
2237c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	hwp->hwalk_msd.msd_position++;
2267c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)hwp->hwalk_msd.msd_mhe.mhe_next;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	return (status);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Clean up after walking the entries in a modhash.
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate void
modent_walk_fini(mdb_walk_state_t * wsp)2357c478bd9Sstevel@tonic-gate modent_walk_fini(mdb_walk_state_t *wsp)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	hash_walk_t *hwp = wsp->walk_data;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	mdb_free(hwp, HW_SIZE(hwp->hwalk_hash.mh_nchains));
2407c478bd9Sstevel@tonic-gate 	wsp->walk_data = NULL;
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * Step to next entry on a hash chain.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate int
modchain_walk_step(mdb_walk_state_t * wsp)2477c478bd9Sstevel@tonic-gate modchain_walk_step(mdb_walk_state_t *wsp)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	struct mod_hash_entry mhe;
2507c478bd9Sstevel@tonic-gate 	int status;
2517c478bd9Sstevel@tonic-gate 
252*892ad162SToomas Soome 	if (wsp->walk_addr == 0)
2537c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mhe, sizeof (mhe), wsp->walk_addr) == -1) {
2567c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mod_hash_entry at %p",
2577c478bd9Sstevel@tonic-gate 		    wsp->walk_addr);
2587c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(wsp->walk_addr, &mhe, wsp->walk_cbdata);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)mhe.mhe_next;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	return (status);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * This is called by ::modhash (via a callback) when gathering data about the
2707c478bd9Sstevel@tonic-gate  * entries in a given modhash.  It keeps track of the longest chain, finds a
2717c478bd9Sstevel@tonic-gate  * specific entry (if the user requested one) and prints out a summary of the
2727c478bd9Sstevel@tonic-gate  * entry or entries.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static int
modent_format(uintptr_t addr,const void * data,void * private)2757c478bd9Sstevel@tonic-gate modent_format(uintptr_t addr, const void *data, void *private)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	const modent_step_data_t *msd = data;
2787c478bd9Sstevel@tonic-gate 	mod_walk_data_t *mwd = private;
2797c478bd9Sstevel@tonic-gate 	int retv = DCMD_OK;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/* If this chain is longest seen, then save start of chain */
2827c478bd9Sstevel@tonic-gate 	if (msd->msd_position > mwd->mwd_maxposn) {
2837c478bd9Sstevel@tonic-gate 		mwd->mwd_maxposn = msd->msd_position;
2847c478bd9Sstevel@tonic-gate 		mwd->mwd_maxidx = msd->msd_hash_index;
2857c478bd9Sstevel@tonic-gate 		mwd->mwd_maxaddr = msd->msd_first_addr;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* If the user specified a particular chain, then ignore others */
2897c478bd9Sstevel@tonic-gate 	if (mwd->mwd_idxtoprint != (uintptr_t)-1) {
2907c478bd9Sstevel@tonic-gate 		/* Save address of *first* entry */
2917c478bd9Sstevel@tonic-gate 		if (mwd->mwd_idxtoprint == msd->msd_hash_index)
2927c478bd9Sstevel@tonic-gate 			mwd->mwd_addr = msd->msd_first_addr;
2937c478bd9Sstevel@tonic-gate 		else
2947c478bd9Sstevel@tonic-gate 			return (retv);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* If the user specified a particular key, ignore others. */
2987c478bd9Sstevel@tonic-gate 	if (mwd->mwd_opt_k_set &&
2997c478bd9Sstevel@tonic-gate 	    (uintptr_t)msd->msd_mhe.mhe_key != mwd->mwd_opt_k)
3007c478bd9Sstevel@tonic-gate 		return (retv);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* If the user specified a particular value, ignore others. */
3037c478bd9Sstevel@tonic-gate 	if (mwd->mwd_opt_v_set &&
3047c478bd9Sstevel@tonic-gate 	    (uintptr_t)msd->msd_mhe.mhe_val != mwd->mwd_opt_v)
3057c478bd9Sstevel@tonic-gate 		return (retv);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* If the user just wants the chain heads, skip intermediate nodes. */
3087c478bd9Sstevel@tonic-gate 	if (mwd->mwd_opt_c && msd->msd_position != 0)
3097c478bd9Sstevel@tonic-gate 		return (retv);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* If the user asked to have the entries printed, then do that. */
3127c478bd9Sstevel@tonic-gate 	if (mwd->mwd_opt_e) {
3137c478bd9Sstevel@tonic-gate 		/* If the output is to a pipeline, just print addresses */
3147c478bd9Sstevel@tonic-gate 		if (mwd->mwd_main_flags & DCMD_PIPE_OUT)
3157c478bd9Sstevel@tonic-gate 			mdb_printf("%p\n", addr);
3167c478bd9Sstevel@tonic-gate 		else
3177c478bd9Sstevel@tonic-gate 			retv = modent_print(addr, msd->msd_hash_index,
3187c478bd9Sstevel@tonic-gate 			    mwd->mwd_flags, mwd->mwd_hte, mwd->mwd_opt_h, 0, 0);
3197c478bd9Sstevel@tonic-gate 		mwd->mwd_flags &= ~DCMD_LOOPFIRST;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	return (retv);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate void
modhash_help(void)3257c478bd9Sstevel@tonic-gate modhash_help(void)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	mdb_printf("Prints information about one or all mod_hash_t databases "
3287c478bd9Sstevel@tonic-gate 	    "in the system.\n"
3297c478bd9Sstevel@tonic-gate 	    "This command has three basic forms, summarized below.\n\n"
3307c478bd9Sstevel@tonic-gate 	    "  ::modhash [-t]\n  <addr>::modhash\n"
3317c478bd9Sstevel@tonic-gate 	    "  <addr>::modhash -e [-ch] [-k key] [-v val] [-i index]\n\n"
3327c478bd9Sstevel@tonic-gate 	    "In the first form, no address is provided, and a summary of all "
3337c478bd9Sstevel@tonic-gate 	    "registered\n"
3347c478bd9Sstevel@tonic-gate 	    "hashes in the system is printed; adding the '-t' option shows"
3357c478bd9Sstevel@tonic-gate 	    " the hash\n"
3367c478bd9Sstevel@tonic-gate 	    "type instead of the limits.  In the second form, the address of a"
3377c478bd9Sstevel@tonic-gate 	    " mod_hash_t\n"
3387c478bd9Sstevel@tonic-gate 	    "is provided, and the output is in a verbose format.  The final "
3397c478bd9Sstevel@tonic-gate 	    "form prints\n"
3407c478bd9Sstevel@tonic-gate 	    "the elements of the hash, optionally selecting just those with a "
3417c478bd9Sstevel@tonic-gate 	    "particular\n"
3427c478bd9Sstevel@tonic-gate 	    "key, value, and/or hash index, or just the chain heads (-c).  "
3437c478bd9Sstevel@tonic-gate 	    "The -h option\n"
3447c478bd9Sstevel@tonic-gate 	    "shows hash indices instead of addresses.\n");
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate int
modhash(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)3487c478bd9Sstevel@tonic-gate modhash(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	mod_hash_t mh;
3517c478bd9Sstevel@tonic-gate 	char name[256];
3527c478bd9Sstevel@tonic-gate 	int len;
3537c478bd9Sstevel@tonic-gate 	mod_walk_data_t mwd;
3547c478bd9Sstevel@tonic-gate 	uint_t opt_s = FALSE;
3557c478bd9Sstevel@tonic-gate 	uint_t opt_t = FALSE;
3567c478bd9Sstevel@tonic-gate 	char kfunc[MDB_SYM_NAMLEN];
3577c478bd9Sstevel@tonic-gate 	const hash_type_entry_t *htep;
3587c478bd9Sstevel@tonic-gate 	boolean_t elem_flags;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	(void) memset(&mwd, 0, sizeof (mwd));
3617c478bd9Sstevel@tonic-gate 	mwd.mwd_main_flags = flags;
3627c478bd9Sstevel@tonic-gate 	mwd.mwd_flags = DCMD_ADDRSPEC | DCMD_LOOP | DCMD_LOOPFIRST;
3637c478bd9Sstevel@tonic-gate 	mwd.mwd_maxposn = -1;
3647c478bd9Sstevel@tonic-gate 	mwd.mwd_idxtoprint = (uintptr_t)-1;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	len = mdb_getopts(argc, argv,
3677c478bd9Sstevel@tonic-gate 	    's', MDB_OPT_SETBITS, TRUE, &opt_s,
3687c478bd9Sstevel@tonic-gate 	    't', MDB_OPT_SETBITS, TRUE, &opt_t,
3697c478bd9Sstevel@tonic-gate 	    'c', MDB_OPT_SETBITS, TRUE, &mwd.mwd_opt_c,
3707c478bd9Sstevel@tonic-gate 	    'e', MDB_OPT_SETBITS, TRUE, &mwd.mwd_opt_e,
3717c478bd9Sstevel@tonic-gate 	    'h', MDB_OPT_SETBITS, TRUE, &mwd.mwd_opt_h,
3727c478bd9Sstevel@tonic-gate 	    'i', MDB_OPT_UINTPTR, &mwd.mwd_idxtoprint,
3737c478bd9Sstevel@tonic-gate 	    'k', MDB_OPT_UINTPTR_SET, &mwd.mwd_opt_k_set, &mwd.mwd_opt_k,
3747c478bd9Sstevel@tonic-gate 	    'v', MDB_OPT_UINTPTR_SET, &mwd.mwd_opt_v_set, &mwd.mwd_opt_v,
3757c478bd9Sstevel@tonic-gate 	    NULL);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (len < argc) {
3787c478bd9Sstevel@tonic-gate 		argv += len;
3797c478bd9Sstevel@tonic-gate 		if (argv->a_type == MDB_TYPE_STRING)
3807c478bd9Sstevel@tonic-gate 			mdb_warn("unexpected argument: %s\n",
3817c478bd9Sstevel@tonic-gate 			    argv->a_un.a_str);
3827c478bd9Sstevel@tonic-gate 		else
3837c478bd9Sstevel@tonic-gate 			mdb_warn("unexpected argument(s)\n");
3847c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* true if any element-related flags are set */
3887c478bd9Sstevel@tonic-gate 	elem_flags = mwd.mwd_opt_c || mwd.mwd_opt_e || mwd.mwd_opt_h ||
3897c478bd9Sstevel@tonic-gate 	    mwd.mwd_opt_k_set || mwd.mwd_opt_v_set ||
3907c478bd9Sstevel@tonic-gate 	    mwd.mwd_idxtoprint != (uintptr_t)-1;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
3937c478bd9Sstevel@tonic-gate 		mdb_arg_t new_argv[1];
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		if (elem_flags) {
3967c478bd9Sstevel@tonic-gate 			/*
3977c478bd9Sstevel@tonic-gate 			 * This isn't allowed so that the output doesn't become
3987c478bd9Sstevel@tonic-gate 			 * a confusing mix of hash table descriptions and
3997c478bd9Sstevel@tonic-gate 			 * element entries.
4007c478bd9Sstevel@tonic-gate 			 */
4017c478bd9Sstevel@tonic-gate 			mdb_warn("printing elements from all hashes is not "
4027c478bd9Sstevel@tonic-gate 			    "permitted\n");
4037c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
4047c478bd9Sstevel@tonic-gate 		}
40548bbca81SDaniel Hoffman 		/* we force short mode here, no matter what it says */
4067c478bd9Sstevel@tonic-gate 		new_argv[0].a_type = MDB_TYPE_STRING;
4077c478bd9Sstevel@tonic-gate 		new_argv[0].a_un.a_str = opt_t ? "-st" : "-s";
4087c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("modhash", "modhash", 1, new_argv) == -1) {
4097c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk mod_hash structures");
4107c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (mwd.mwd_opt_e) {
4167c478bd9Sstevel@tonic-gate 		if (opt_s | opt_t) {
4177c478bd9Sstevel@tonic-gate 			mdb_warn("hash summary options not permitted when "
4187c478bd9Sstevel@tonic-gate 			    "displaying elements\n");
4197c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 	} else {
4227c478bd9Sstevel@tonic-gate 		if (elem_flags) {
4237c478bd9Sstevel@tonic-gate 			/*
4247c478bd9Sstevel@tonic-gate 			 * This isn't allowed so that the output doesn't become
4257c478bd9Sstevel@tonic-gate 			 * a confusing mix of hash table description and
4267c478bd9Sstevel@tonic-gate 			 * element entries.
4277c478bd9Sstevel@tonic-gate 			 */
4287c478bd9Sstevel@tonic-gate 			mdb_warn("printing elements requires -e\n");
4297c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mh, sizeof (mh), addr) == -1) {
4347c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mod_hash_t at %p", addr);
4357c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (mwd.mwd_idxtoprint != (uintptr_t)-1 &&
4397c478bd9Sstevel@tonic-gate 	    mwd.mwd_idxtoprint >= mh.mh_nchains) {
4407c478bd9Sstevel@tonic-gate 		mdb_warn("mod_hash chain index %x out of range 0..%x\n",
4417c478bd9Sstevel@tonic-gate 		    mwd.mwd_idxtoprint, mh.mh_nchains - 1);
4427c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && opt_s) {
4467c478bd9Sstevel@tonic-gate 		if (opt_t != 0) {
4477c478bd9Sstevel@tonic-gate 			mdb_printf("%<u>%?s %6s %5s %?s %s%</u>\n",
4487c478bd9Sstevel@tonic-gate 			    "ADDR", "CHAINS", "ELEMS", "TYPE", "NAME");
4497c478bd9Sstevel@tonic-gate 		} else {
4507c478bd9Sstevel@tonic-gate 			mdb_printf("%<u>%?s %6s %5s %6s %6s %s%</u>\n",
4517c478bd9Sstevel@tonic-gate 			    "ADDR", "CHAINS", "ELEMS", "MAXLEN", "MAXIDX",
4527c478bd9Sstevel@tonic-gate 			    "NAME");
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	len = mdb_readstr(name, sizeof (name), (uintptr_t)mh.mh_name);
4577c478bd9Sstevel@tonic-gate 	if (len < 0)
4587c478bd9Sstevel@tonic-gate 		(void) strcpy(name, "??");
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_addr((uintptr_t)mh.mh_keycmp, MDB_SYM_EXACT, kfunc,
4617c478bd9Sstevel@tonic-gate 	    sizeof (kfunc), NULL) == -1)
4627c478bd9Sstevel@tonic-gate 		kfunc[0] = '\0';
4637c478bd9Sstevel@tonic-gate 	for (htep = hte_table; htep->hte_type != NULL; htep++)
4647c478bd9Sstevel@tonic-gate 		if (strcmp(kfunc, htep->hte_comparator) == 0)
4657c478bd9Sstevel@tonic-gate 			break;
4667c478bd9Sstevel@tonic-gate 	mwd.mwd_hte = htep;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (!mwd.mwd_opt_e && !opt_s) {
4697c478bd9Sstevel@tonic-gate 		mdb_printf("mod_hash_t %?p %s%s:\n", addr, name,
4707c478bd9Sstevel@tonic-gate 		    len == sizeof (name) ? "..." : "");
4717c478bd9Sstevel@tonic-gate 		mdb_printf("\tKey comparator: %?p %s\n",
4727c478bd9Sstevel@tonic-gate 		    mh.mh_keycmp, kfunc);
4737c478bd9Sstevel@tonic-gate 		mdb_printf("\tType: %s\n",
4747c478bd9Sstevel@tonic-gate 		    htep->hte_type == NULL ? "unknown" : htep->hte_type);
4757c478bd9Sstevel@tonic-gate 		mdb_printf("\tSleep flag = %s, alloc failed = %#x\n",
4767c478bd9Sstevel@tonic-gate 		    mh.mh_sleep ? "true" : "false",
4777c478bd9Sstevel@tonic-gate 		    mh.mh_stat.mhs_nomem);
4787c478bd9Sstevel@tonic-gate 		mdb_printf("\tNumber of chains = %#x, elements = %#x\n",
4797c478bd9Sstevel@tonic-gate 		    mh.mh_nchains, mh.mh_stat.mhs_nelems);
4807c478bd9Sstevel@tonic-gate 		mdb_printf("\tHits = %#x, misses = %#x, dups = %#x\n",
4817c478bd9Sstevel@tonic-gate 		    mh.mh_stat.mhs_hit, mh.mh_stat.mhs_miss,
4827c478bd9Sstevel@tonic-gate 		    mh.mh_stat.mhs_coll);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("modent", modent_format, &mwd, addr) == -1) {
4857c478bd9Sstevel@tonic-gate 		mdb_warn("can't walk mod_hash entries");
4867c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	if (opt_s) {
4897c478bd9Sstevel@tonic-gate 		const char *tname;
4907c478bd9Sstevel@tonic-gate 		char tbuf[64];
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (htep->hte_type == NULL) {
4937c478bd9Sstevel@tonic-gate 			(void) mdb_snprintf(tbuf, sizeof (tbuf), "%p",
4947c478bd9Sstevel@tonic-gate 			    mh.mh_keycmp);
4957c478bd9Sstevel@tonic-gate 			tname = tbuf;
4967c478bd9Sstevel@tonic-gate 		} else {
4977c478bd9Sstevel@tonic-gate 			tname = htep->hte_type;
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		mdb_printf("%?p %6x %5x ", addr, mh.mh_nchains,
5007c478bd9Sstevel@tonic-gate 		    mh.mh_stat.mhs_nelems);
5017c478bd9Sstevel@tonic-gate 		if (opt_t != 0) {
5027c478bd9Sstevel@tonic-gate 			mdb_printf("%?s", tname);
5037c478bd9Sstevel@tonic-gate 		} else {
5047c478bd9Sstevel@tonic-gate 			mdb_printf("%6x %6x", mwd.mwd_maxposn + 1,
5057c478bd9Sstevel@tonic-gate 			    mwd.mwd_maxidx);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		mdb_printf(" %s%s\n", name, len == sizeof (name) ? "..." : "");
5087c478bd9Sstevel@tonic-gate 	} else if (!mwd.mwd_opt_e) {
5097c478bd9Sstevel@tonic-gate 		mdb_printf("\tMaximum chain length = %x (at index %x, first "
5107c478bd9Sstevel@tonic-gate 		    "entry %p)\n", mwd.mwd_maxposn + 1, mwd.mwd_maxidx,
5117c478bd9Sstevel@tonic-gate 		    mwd.mwd_maxaddr);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static void
format_strhash(const mod_hash_key_t key,char * keystr,size_t keystrlen)5177c478bd9Sstevel@tonic-gate format_strhash(const mod_hash_key_t key, char *keystr, size_t keystrlen)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	int len;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(keystr, keystrlen, "%?p ", key);
5227c478bd9Sstevel@tonic-gate 	len = strlen(keystr);
5237c478bd9Sstevel@tonic-gate 	(void) mdb_readstr(keystr + len, keystrlen - len, (uintptr_t)key);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static void
format_ptrhash(const mod_hash_key_t key,char * keystr,size_t keystrlen)5277c478bd9Sstevel@tonic-gate format_ptrhash(const mod_hash_key_t key, char *keystr, size_t keystrlen)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	int len;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(keystr, keystrlen, "%?p ", key);
5327c478bd9Sstevel@tonic-gate 	len = strlen(keystr);
5337c478bd9Sstevel@tonic-gate 	(void) mdb_lookup_by_addr((uintptr_t)key, MDB_SYM_EXACT, keystr + len,
5347c478bd9Sstevel@tonic-gate 	    keystrlen - len, NULL);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static void
format_idhash(const mod_hash_key_t key,char * keystr,size_t keystrlen)5387c478bd9Sstevel@tonic-gate format_idhash(const mod_hash_key_t key, char *keystr, size_t keystrlen)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(keystr, keystrlen, "%?x", (uint_t)(uintptr_t)key);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate static void
format_default(const mod_hash_key_t key,char * keystr,size_t keystrlen)5447c478bd9Sstevel@tonic-gate format_default(const mod_hash_key_t key, char *keystr, size_t keystrlen)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(keystr, keystrlen, "%?p", key);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate void
modent_help(void)5507c478bd9Sstevel@tonic-gate modent_help(void)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	mdb_printf("Options are mutually exclusive:\n"
5537c478bd9Sstevel@tonic-gate 	    "  -t <type>  print key in symbolic form; <type> is one of str, "
5547c478bd9Sstevel@tonic-gate 	    "ptr, or id\n"
5557c478bd9Sstevel@tonic-gate 	    "  -v         print value pointer alone\n"
5567c478bd9Sstevel@tonic-gate 	    "  -k         print key pointer alone\n");
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate static int
modent_print(uintptr_t addr,int hidx,uint_t flags,const hash_type_entry_t * htep,boolean_t prtidx,uint_t opt_k,uint_t opt_v)5607c478bd9Sstevel@tonic-gate modent_print(uintptr_t addr, int hidx, uint_t flags,
5617c478bd9Sstevel@tonic-gate     const hash_type_entry_t *htep, boolean_t prtidx, uint_t opt_k,
5627c478bd9Sstevel@tonic-gate     uint_t opt_v)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	char keystr[256];
5657c478bd9Sstevel@tonic-gate 	struct mod_hash_entry mhe;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && opt_k == 0 && opt_v == 0) {
5687c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%?s %?s %?s%</u>\n",
5697c478bd9Sstevel@tonic-gate 		    prtidx ? "HASH_IDX" : "ADDR", "VAL", "KEY");
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mhe, sizeof (mhe), addr) == -1) {
5737c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read mod_hash_entry at %p", addr);
5747c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (opt_k) {
5787c478bd9Sstevel@tonic-gate 		mdb_printf("%p\n", mhe.mhe_key);
5797c478bd9Sstevel@tonic-gate 	} else if (opt_v) {
5807c478bd9Sstevel@tonic-gate 		mdb_printf("%p\n", mhe.mhe_val);
5817c478bd9Sstevel@tonic-gate 	} else {
5827c478bd9Sstevel@tonic-gate 		htep->hte_format(mhe.mhe_key, keystr, sizeof (keystr));
5837c478bd9Sstevel@tonic-gate 		if (prtidx)
5847c478bd9Sstevel@tonic-gate 			mdb_printf("%?x", hidx);
5857c478bd9Sstevel@tonic-gate 		else
5867c478bd9Sstevel@tonic-gate 			mdb_printf("%?p", addr);
5877c478bd9Sstevel@tonic-gate 		mdb_printf(" %?p %s\n", mhe.mhe_val, keystr);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate  * This prints out a single mod_hash element, showing its value and its key.
5957c478bd9Sstevel@tonic-gate  * The key is decoded based on the type of hash keys in use.
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate int
modent(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)5987c478bd9Sstevel@tonic-gate modent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	const char *opt_t = NULL;
6017c478bd9Sstevel@tonic-gate 	const hash_type_entry_t *htep;
6027c478bd9Sstevel@tonic-gate 	int len;
6037c478bd9Sstevel@tonic-gate 	uint_t opt_k = 0;
6047c478bd9Sstevel@tonic-gate 	uint_t opt_v = 0;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
6077c478bd9Sstevel@tonic-gate 		mdb_warn("address of mod_hash_entry must be specified\n");
6087c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	len = mdb_getopts(argc, argv,
6127c478bd9Sstevel@tonic-gate 	    't', MDB_OPT_STR, &opt_t,
6137c478bd9Sstevel@tonic-gate 	    'k', MDB_OPT_SETBITS, 1, &opt_k,
6147c478bd9Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, 1, &opt_v,
6157c478bd9Sstevel@tonic-gate 	    NULL);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/* options are mutually exclusive */
6187c478bd9Sstevel@tonic-gate 	if ((opt_k && opt_v) || (opt_t != NULL && (opt_k || opt_v)) ||
6197c478bd9Sstevel@tonic-gate 	    len < argc) {
6207c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	for (htep = hte_table; htep->hte_type != NULL; htep++)
6247c478bd9Sstevel@tonic-gate 		if (opt_t != NULL && strcmp(opt_t, htep->hte_type) == 0)
6257c478bd9Sstevel@tonic-gate 			break;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	if (opt_t != NULL && htep->hte_type == NULL) {
6287c478bd9Sstevel@tonic-gate 		mdb_warn("unknown hash type %s\n", opt_t);
6297c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	return (modent_print(addr, 0, flags, htep, FALSE, opt_k, opt_v));
6337c478bd9Sstevel@tonic-gate }
634