xref: /titanic_50/usr/src/cmd/mdb/common/mdb/mdb_nv.c (revision 065c692a88e4dcdd0c6eadc2476c046d6ee9dd1c)
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  */
26*065c692aSJosef 'Jeff' Sipek /*
27*065c692aSJosef 'Jeff' Sipek  * Copyright (c) 2013 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
28*065c692aSJosef 'Jeff' Sipek  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
317c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
327c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
337c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
347c478bd9Sstevel@tonic-gate #include <mdb/mdb_nv.h>
357c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	NV_NAME(v) \
387c478bd9Sstevel@tonic-gate 	(((v)->v_flags & MDB_NV_EXTNAME) ? (v)->v_ename : (v)->v_lname)
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	NV_SIZE(v) \
417c478bd9Sstevel@tonic-gate 	(((v)->v_flags & MDB_NV_EXTNAME) ? sizeof (mdb_var_t) : \
42*065c692aSJosef 'Jeff' Sipek 	sizeof (mdb_var_t) + strlen((v)->v_lname))
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	NV_HASHSZ	211
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static size_t
nv_hashstring(const char * key)477c478bd9Sstevel@tonic-gate nv_hashstring(const char *key)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	size_t g, h = 0;
507c478bd9Sstevel@tonic-gate 	const char *p;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	ASSERT(key != NULL);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	for (p = key; *p != '\0'; p++) {
557c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
587c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
597c478bd9Sstevel@tonic-gate 			h ^= g;
607c478bd9Sstevel@tonic-gate 		}
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	return (h);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static mdb_var_t *
nv_var_alloc(const char * name,const mdb_nv_disc_t * disc,uintmax_t value,uint_t flags,uint_t um_flags,mdb_var_t * next)677c478bd9Sstevel@tonic-gate nv_var_alloc(const char *name, const mdb_nv_disc_t *disc,
687c478bd9Sstevel@tonic-gate 	uintmax_t value, uint_t flags, uint_t um_flags, mdb_var_t *next)
697c478bd9Sstevel@tonic-gate {
70*065c692aSJosef 'Jeff' Sipek 	size_t nbytes;
71*065c692aSJosef 'Jeff' Sipek 	mdb_var_t *v;
727c478bd9Sstevel@tonic-gate 
73*065c692aSJosef 'Jeff' Sipek 	if (flags & MDB_NV_EXTNAME)
74*065c692aSJosef 'Jeff' Sipek 		nbytes = sizeof (mdb_var_t);
75*065c692aSJosef 'Jeff' Sipek 	else
76*065c692aSJosef 'Jeff' Sipek 		nbytes = sizeof (mdb_var_t) + strlen(name);
77*065c692aSJosef 'Jeff' Sipek 
78*065c692aSJosef 'Jeff' Sipek 	v = mdb_alloc(nbytes, um_flags);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (v == NULL)
817c478bd9Sstevel@tonic-gate 		return (NULL);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (flags & MDB_NV_EXTNAME) {
847c478bd9Sstevel@tonic-gate 		v->v_ename = name;
85*065c692aSJosef 'Jeff' Sipek 		v->v_lname[0] = '\0';
867c478bd9Sstevel@tonic-gate 	} else {
87*065c692aSJosef 'Jeff' Sipek 		/*
88*065c692aSJosef 'Jeff' Sipek 		 * We don't overflow here since the mdb_var_t itself has
89*065c692aSJosef 'Jeff' Sipek 		 * room for the trailing \0.
90*065c692aSJosef 'Jeff' Sipek 		 */
91*065c692aSJosef 'Jeff' Sipek 		(void) strcpy(v->v_lname, name);
927c478bd9Sstevel@tonic-gate 		v->v_ename = NULL;
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	v->v_uvalue = value;
967c478bd9Sstevel@tonic-gate 	v->v_flags = flags & ~(MDB_NV_SILENT | MDB_NV_INTERPOS);
977c478bd9Sstevel@tonic-gate 	v->v_disc = disc;
987c478bd9Sstevel@tonic-gate 	v->v_next = next;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	return (v);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static void
nv_var_free(mdb_var_t * v,uint_t um_flags)1047c478bd9Sstevel@tonic-gate nv_var_free(mdb_var_t *v, uint_t um_flags)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	if (um_flags & UM_GC)
1077c478bd9Sstevel@tonic-gate 		return;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (v->v_flags & MDB_NV_OVERLOAD) {
1107c478bd9Sstevel@tonic-gate 		mdb_var_t *w, *nw;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		for (w = v->v_ndef; w != NULL; w = nw) {
1137c478bd9Sstevel@tonic-gate 			nw = w->v_ndef;
1147c478bd9Sstevel@tonic-gate 			mdb_free(w, NV_SIZE(w));
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	mdb_free(v, NV_SIZE(v));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Can return NULL only if the nv's memory allocation flags include UM_NOSLEEP
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate mdb_nv_t *
mdb_nv_create(mdb_nv_t * nv,uint_t um_flags)1257c478bd9Sstevel@tonic-gate mdb_nv_create(mdb_nv_t *nv, uint_t um_flags)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	nv->nv_hash = mdb_zalloc(sizeof (mdb_var_t *) * NV_HASHSZ, um_flags);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (nv->nv_hash == NULL)
1307c478bd9Sstevel@tonic-gate 		return (NULL);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	nv->nv_hashsz = NV_HASHSZ;
1337c478bd9Sstevel@tonic-gate 	nv->nv_nelems = 0;
1347c478bd9Sstevel@tonic-gate 	nv->nv_iter_elt = NULL;
1357c478bd9Sstevel@tonic-gate 	nv->nv_iter_bucket = 0;
1367c478bd9Sstevel@tonic-gate 	nv->nv_um_flags = um_flags;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	return (nv);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
mdb_nv_destroy(mdb_nv_t * nv)1427c478bd9Sstevel@tonic-gate mdb_nv_destroy(mdb_nv_t *nv)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	mdb_var_t *v, *w;
1457c478bd9Sstevel@tonic-gate 	size_t i;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (nv->nv_um_flags & UM_GC)
1487c478bd9Sstevel@tonic-gate 		return;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	for (i = 0; i < nv->nv_hashsz; i++) {
1517c478bd9Sstevel@tonic-gate 		for (v = nv->nv_hash[i]; v != NULL; v = w) {
1527c478bd9Sstevel@tonic-gate 			w = v->v_next;
1537c478bd9Sstevel@tonic-gate 			nv_var_free(v, nv->nv_um_flags);
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	mdb_free(nv->nv_hash, sizeof (mdb_var_t *) * NV_HASHSZ);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate mdb_var_t *
mdb_nv_lookup(mdb_nv_t * nv,const char * name)1617c478bd9Sstevel@tonic-gate mdb_nv_lookup(mdb_nv_t *nv, const char *name)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	size_t i = nv_hashstring(name) % nv->nv_hashsz;
1647c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	for (v = nv->nv_hash[i]; v != NULL; v = v->v_next) {
1677c478bd9Sstevel@tonic-gate 		if (strcmp(NV_NAME(v), name) == 0)
1687c478bd9Sstevel@tonic-gate 			return (v);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (NULL);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Interpose W in place of V.  We replace V with W in nv_hash, and then
1767c478bd9Sstevel@tonic-gate  * set W's v_ndef overload chain to point at V.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static mdb_var_t *
nv_var_interpos(mdb_nv_t * nv,size_t i,mdb_var_t * v,mdb_var_t * w)1797c478bd9Sstevel@tonic-gate nv_var_interpos(mdb_nv_t *nv, size_t i, mdb_var_t *v, mdb_var_t *w)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	mdb_var_t **pvp = &nv->nv_hash[i];
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	while (*pvp != v) {
1847c478bd9Sstevel@tonic-gate 		mdb_var_t *vp = *pvp;
1857c478bd9Sstevel@tonic-gate 		ASSERT(vp != NULL);
1867c478bd9Sstevel@tonic-gate 		pvp = &vp->v_next;
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	*pvp = w;
1907c478bd9Sstevel@tonic-gate 	w->v_next = v->v_next;
1917c478bd9Sstevel@tonic-gate 	w->v_ndef = v;
1927c478bd9Sstevel@tonic-gate 	v->v_next = NULL;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (w);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Add W to the end of V's overload chain.  We simply follow v_ndef to the
1997c478bd9Sstevel@tonic-gate  * end, and then append W.  We don't expect these chains to grow very long.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate static mdb_var_t *
nv_var_overload(mdb_var_t * v,mdb_var_t * w)2027c478bd9Sstevel@tonic-gate nv_var_overload(mdb_var_t *v, mdb_var_t *w)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	while (v->v_ndef != NULL)
2057c478bd9Sstevel@tonic-gate 		v = v->v_ndef;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	v->v_ndef = w;
2087c478bd9Sstevel@tonic-gate 	return (w);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * Can return NULL only if the nv's memory allocation flags include UM_NOSLEEP
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate mdb_var_t *
mdb_nv_insert(mdb_nv_t * nv,const char * name,const mdb_nv_disc_t * disc,uintmax_t value,uint_t flags)2157c478bd9Sstevel@tonic-gate mdb_nv_insert(mdb_nv_t *nv, const char *name, const mdb_nv_disc_t *disc,
2167c478bd9Sstevel@tonic-gate     uintmax_t value, uint_t flags)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	size_t i = nv_hashstring(name) % nv->nv_hashsz;
2197c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & MDB_NV_EXTNAME) || !(flags & MDB_NV_OVERLOAD));
2227c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & MDB_NV_RDONLY) || !(flags & MDB_NV_OVERLOAD));
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * If the specified name is already hashed,
2267c478bd9Sstevel@tonic-gate 	 * and MDB_NV_OVERLOAD is set:	insert new var into overload chain
2277c478bd9Sstevel@tonic-gate 	 * and MDB_NV_RDONLY is set:	leave var unchanged, issue warning
2287c478bd9Sstevel@tonic-gate 	 * otherwise:			update var with new value
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	for (v = nv->nv_hash[i]; v != NULL; v = v->v_next) {
2317c478bd9Sstevel@tonic-gate 		if (strcmp(NV_NAME(v), name) == 0) {
2327c478bd9Sstevel@tonic-gate 			if (v->v_flags & MDB_NV_OVERLOAD) {
2337c478bd9Sstevel@tonic-gate 				mdb_var_t *w = nv_var_alloc(NV_NAME(v), disc,
2347c478bd9Sstevel@tonic-gate 				    value, flags, nv->nv_um_flags, NULL);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 				if (w == NULL) {
2377c478bd9Sstevel@tonic-gate 					ASSERT(nv->nv_um_flags & UM_NOSLEEP);
2387c478bd9Sstevel@tonic-gate 					return (NULL);
2397c478bd9Sstevel@tonic-gate 				}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 				if (flags & MDB_NV_INTERPOS)
2427c478bd9Sstevel@tonic-gate 					v = nv_var_interpos(nv, i, v, w);
2437c478bd9Sstevel@tonic-gate 				else
2447c478bd9Sstevel@tonic-gate 					v = nv_var_overload(v, w);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			} else if (v->v_flags & MDB_NV_RDONLY) {
2477c478bd9Sstevel@tonic-gate 				if (!(flags & MDB_NV_SILENT)) {
2487c478bd9Sstevel@tonic-gate 					warn("cannot modify read-only "
2497c478bd9Sstevel@tonic-gate 					    "variable '%s'\n", NV_NAME(v));
2507c478bd9Sstevel@tonic-gate 				}
2517c478bd9Sstevel@tonic-gate 			} else
2527c478bd9Sstevel@tonic-gate 				v->v_uvalue = value;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 			ASSERT(v != NULL);
2557c478bd9Sstevel@tonic-gate 			return (v);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * If the specified name was not found, initialize a new element
2617c478bd9Sstevel@tonic-gate 	 * and add it to the hash table at the beginning of this chain:
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	v = nv_var_alloc(name, disc, value, flags, nv->nv_um_flags,
2647c478bd9Sstevel@tonic-gate 	    nv->nv_hash[i]);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (v == NULL) {
2677c478bd9Sstevel@tonic-gate 		ASSERT(nv->nv_um_flags & UM_NOSLEEP);
2687c478bd9Sstevel@tonic-gate 		return (NULL);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	nv->nv_hash[i] = v;
2727c478bd9Sstevel@tonic-gate 	nv->nv_nelems++;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (v);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static void
nv_var_defn_remove(mdb_var_t * v,mdb_var_t * corpse,uint_t um_flags)2787c478bd9Sstevel@tonic-gate nv_var_defn_remove(mdb_var_t *v, mdb_var_t *corpse, uint_t um_flags)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	mdb_var_t *w = v;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	while (v->v_ndef != NULL && v->v_ndef != corpse)
2837c478bd9Sstevel@tonic-gate 		v = v->v_ndef;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (v == NULL) {
2867c478bd9Sstevel@tonic-gate 		fail("var %p ('%s') not found on defn chain of %p\n",
2877c478bd9Sstevel@tonic-gate 		    (void *)corpse, NV_NAME(corpse), (void *)w);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	v->v_ndef = corpse->v_ndef;
2917c478bd9Sstevel@tonic-gate 	corpse->v_ndef = NULL;
2927c478bd9Sstevel@tonic-gate 	nv_var_free(corpse, um_flags);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate void
mdb_nv_remove(mdb_nv_t * nv,mdb_var_t * corpse)2967c478bd9Sstevel@tonic-gate mdb_nv_remove(mdb_nv_t *nv, mdb_var_t *corpse)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	const char *cname = NV_NAME(corpse);
2997c478bd9Sstevel@tonic-gate 	size_t i = nv_hashstring(cname) % nv->nv_hashsz;
3007c478bd9Sstevel@tonic-gate 	mdb_var_t *v = nv->nv_hash[i];
3017c478bd9Sstevel@tonic-gate 	mdb_var_t **pvp;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (corpse->v_flags & MDB_NV_PERSIST) {
3047c478bd9Sstevel@tonic-gate 		warn("cannot remove persistent variable '%s'\n", cname);
3057c478bd9Sstevel@tonic-gate 		return;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (v != corpse) {
3097c478bd9Sstevel@tonic-gate 		do {
3107c478bd9Sstevel@tonic-gate 			if (strcmp(NV_NAME(v), cname) == 0) {
3117c478bd9Sstevel@tonic-gate 				if (corpse->v_flags & MDB_NV_OVERLOAD) {
3127c478bd9Sstevel@tonic-gate 					nv_var_defn_remove(v, corpse,
3137c478bd9Sstevel@tonic-gate 					    nv->nv_um_flags);
3147c478bd9Sstevel@tonic-gate 					return; /* No v_next changes needed */
3157c478bd9Sstevel@tonic-gate 				} else
3167c478bd9Sstevel@tonic-gate 					goto notfound;
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 			if (v->v_next == corpse)
3207c478bd9Sstevel@tonic-gate 				break; /* Corpse is next on the chain */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		} while ((v = v->v_next) != NULL);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		if (v == NULL)
3257c478bd9Sstevel@tonic-gate 			goto notfound;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		pvp = &v->v_next;
3287c478bd9Sstevel@tonic-gate 	} else
3297c478bd9Sstevel@tonic-gate 		pvp = &nv->nv_hash[i];
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if ((corpse->v_flags & MDB_NV_OVERLOAD) && corpse->v_ndef != NULL) {
3327c478bd9Sstevel@tonic-gate 		corpse->v_ndef->v_next = corpse->v_next;
3337c478bd9Sstevel@tonic-gate 		*pvp = corpse->v_ndef;
3347c478bd9Sstevel@tonic-gate 		corpse->v_ndef = NULL;
3357c478bd9Sstevel@tonic-gate 	} else {
3367c478bd9Sstevel@tonic-gate 		*pvp = corpse->v_next;
3377c478bd9Sstevel@tonic-gate 		nv->nv_nelems--;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	nv_var_free(corpse, nv->nv_um_flags);
3417c478bd9Sstevel@tonic-gate 	return;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate notfound:
3447c478bd9Sstevel@tonic-gate 	fail("var %p ('%s') not found on hash chain: nv=%p [%lu]\n",
3457c478bd9Sstevel@tonic-gate 	    (void *)corpse, cname, (void *)nv, (ulong_t)i);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate void
mdb_nv_rewind(mdb_nv_t * nv)3497c478bd9Sstevel@tonic-gate mdb_nv_rewind(mdb_nv_t *nv)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	size_t i;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	for (i = 0; i < nv->nv_hashsz; i++) {
3547c478bd9Sstevel@tonic-gate 		if (nv->nv_hash[i] != NULL)
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	nv->nv_iter_elt = i < nv->nv_hashsz ? nv->nv_hash[i] : NULL;
3597c478bd9Sstevel@tonic-gate 	nv->nv_iter_bucket = i;
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate mdb_var_t *
mdb_nv_advance(mdb_nv_t * nv)3637c478bd9Sstevel@tonic-gate mdb_nv_advance(mdb_nv_t *nv)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	mdb_var_t *v = nv->nv_iter_elt;
3667c478bd9Sstevel@tonic-gate 	size_t i;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (v == NULL)
3697c478bd9Sstevel@tonic-gate 		return (NULL);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (v->v_next != NULL) {
3727c478bd9Sstevel@tonic-gate 		nv->nv_iter_elt = v->v_next;
3737c478bd9Sstevel@tonic-gate 		return (v);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	for (i = nv->nv_iter_bucket + 1; i < nv->nv_hashsz; i++) {
3777c478bd9Sstevel@tonic-gate 		if (nv->nv_hash[i] != NULL)
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	nv->nv_iter_elt = i < nv->nv_hashsz ? nv->nv_hash[i] : NULL;
3827c478bd9Sstevel@tonic-gate 	nv->nv_iter_bucket = i;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (v);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate mdb_var_t *
mdb_nv_peek(mdb_nv_t * nv)3887c478bd9Sstevel@tonic-gate mdb_nv_peek(mdb_nv_t *nv)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	return (nv->nv_iter_elt);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate size_t
mdb_nv_size(mdb_nv_t * nv)3947c478bd9Sstevel@tonic-gate mdb_nv_size(mdb_nv_t *nv)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	return (nv->nv_nelems);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static int
nv_compare(const mdb_var_t ** lp,const mdb_var_t ** rp)4007c478bd9Sstevel@tonic-gate nv_compare(const mdb_var_t **lp, const mdb_var_t **rp)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	return (strcmp(mdb_nv_get_name(*lp), mdb_nv_get_name(*rp)));
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate void
mdb_nv_sort_iter(mdb_nv_t * nv,int (* func)(mdb_var_t *,void *),void * private,uint_t um_flags)4067c478bd9Sstevel@tonic-gate mdb_nv_sort_iter(mdb_nv_t *nv, int (*func)(mdb_var_t *, void *),
4077c478bd9Sstevel@tonic-gate     void *private, uint_t um_flags)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	mdb_var_t **vps =
4107c478bd9Sstevel@tonic-gate 	    mdb_alloc(nv->nv_nelems * sizeof (mdb_var_t *), um_flags);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (nv->nv_nelems != 0 && vps != NULL) {
4137c478bd9Sstevel@tonic-gate 		mdb_var_t *v, **vpp = vps;
4147c478bd9Sstevel@tonic-gate 		size_t i;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		for (mdb_nv_rewind(nv); (v = mdb_nv_advance(nv)) != NULL; )
4177c478bd9Sstevel@tonic-gate 			*vpp++ = v;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		qsort(vps, nv->nv_nelems, sizeof (mdb_var_t *),
4207c478bd9Sstevel@tonic-gate 		    (int (*)(const void *, const void *))nv_compare);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		for (vpp = vps, i = 0; i < nv->nv_nelems; i++) {
4237c478bd9Sstevel@tonic-gate 			if (func(*vpp++, private) == -1)
4247c478bd9Sstevel@tonic-gate 				break;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		if (!(um_flags & UM_GC))
4287c478bd9Sstevel@tonic-gate 			mdb_free(vps, nv->nv_nelems * sizeof (mdb_var_t *));
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate void
mdb_nv_defn_iter(mdb_var_t * v,int (* func)(mdb_var_t *,void *),void * private)4337c478bd9Sstevel@tonic-gate mdb_nv_defn_iter(mdb_var_t *v, int (*func)(mdb_var_t *, void *), void *private)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	if (func(v, private) == -1 || !(v->v_flags & MDB_NV_OVERLOAD))
4367c478bd9Sstevel@tonic-gate 		return;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (v = v->v_ndef; v != NULL; v = v->v_ndef) {
4397c478bd9Sstevel@tonic-gate 		if (func(v, private) == -1)
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate uintmax_t
mdb_nv_get_value(const mdb_var_t * v)4457c478bd9Sstevel@tonic-gate mdb_nv_get_value(const mdb_var_t *v)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	if (v->v_disc)
4487c478bd9Sstevel@tonic-gate 		return (v->v_disc->disc_get(v));
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	return (v->v_uvalue);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate void
mdb_nv_set_value(mdb_var_t * v,uintmax_t l)4547c478bd9Sstevel@tonic-gate mdb_nv_set_value(mdb_var_t *v, uintmax_t l)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	if (v->v_flags & MDB_NV_RDONLY) {
4577c478bd9Sstevel@tonic-gate 		warn("cannot modify read-only variable '%s'\n", NV_NAME(v));
4587c478bd9Sstevel@tonic-gate 		return;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (v->v_disc)
4627c478bd9Sstevel@tonic-gate 		v->v_disc->disc_set(v, l);
4637c478bd9Sstevel@tonic-gate 	else
4647c478bd9Sstevel@tonic-gate 		v->v_uvalue = l;
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate void *
mdb_nv_get_cookie(const mdb_var_t * v)4687c478bd9Sstevel@tonic-gate mdb_nv_get_cookie(const mdb_var_t *v)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	if (v->v_disc)
4717c478bd9Sstevel@tonic-gate 		return ((void *)(uintptr_t)v->v_disc->disc_get(v));
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	return (MDB_NV_COOKIE(v));
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate void
mdb_nv_set_cookie(mdb_var_t * v,void * cookie)4777c478bd9Sstevel@tonic-gate mdb_nv_set_cookie(mdb_var_t *v, void *cookie)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	mdb_nv_set_value(v, (uintmax_t)(uintptr_t)cookie);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate const char *
mdb_nv_get_name(const mdb_var_t * v)4837c478bd9Sstevel@tonic-gate mdb_nv_get_name(const mdb_var_t *v)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	return (NV_NAME(v));
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate mdb_var_t *
mdb_nv_get_ndef(const mdb_var_t * v)4897c478bd9Sstevel@tonic-gate mdb_nv_get_ndef(const mdb_var_t *v)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	if (v->v_flags & MDB_NV_OVERLOAD)
4927c478bd9Sstevel@tonic-gate 		return (v->v_ndef);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	return (NULL);
4957c478bd9Sstevel@tonic-gate }
496