1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "umem.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <sys/vmem_impl_user.h>
32*7c478bd9Sstevel@tonic-gate #include <umem_impl.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <alloca.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include "misc.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "umem_pagesize.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #define	UM_ALLOCATED		0x1
41*7c478bd9Sstevel@tonic-gate #define	UM_FREE			0x2
42*7c478bd9Sstevel@tonic-gate #define	UM_BUFCTL		0x4
43*7c478bd9Sstevel@tonic-gate #define	UM_HASH			0x8
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate uint32_t umem_max_ncpus;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate uint32_t umem_stack_depth;
48*7c478bd9Sstevel@tonic-gate size_t umem_pagesize;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #define	UMEM_READVAR(var)				\
51*7c478bd9Sstevel@tonic-gate 	(umem_readvar(&(var), #var) == -1 &&		\
52*7c478bd9Sstevel@tonic-gate 	    ((void) mdb_warn("failed to read "#var), 1))
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate int
55*7c478bd9Sstevel@tonic-gate umem_init(void)
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	size_t pagesize;
58*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	/*
61*7c478bd9Sstevel@tonic-gate 	 * Figure out which type of umem is being used
62*7c478bd9Sstevel@tonic-gate 	 */
63*7c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_obj("libumem.so.1", "umem_alloc", &sym) != 0)
64*7c478bd9Sstevel@tonic-gate 		umem_set_standalone();
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_max_ncpus))
67*7c478bd9Sstevel@tonic-gate 		return (-1);
68*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_stack_depth))
69*7c478bd9Sstevel@tonic-gate 		return (-1);
70*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(pagesize))
71*7c478bd9Sstevel@tonic-gate 		return (-1);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	umem_pagesize = pagesize;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (umem_stack_depth > UMEM_MAX_STACK_DEPTH) {
76*7c478bd9Sstevel@tonic-gate 		mdb_warn("umem_stack_depth corrupted (%d > %d)\n",
77*7c478bd9Sstevel@tonic-gate 		    umem_stack_depth, UMEM_MAX_STACK_DEPTH);
78*7c478bd9Sstevel@tonic-gate 		umem_stack_depth = 0;
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 	return (0);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
84*7c478bd9Sstevel@tonic-gate int
85*7c478bd9Sstevel@tonic-gate umem_init_walkers(uintptr_t addr, const umem_cache_t *c, void *ignored)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	mdb_walker_t w;
88*7c478bd9Sstevel@tonic-gate 	char descr[64];
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(descr, sizeof (descr),
91*7c478bd9Sstevel@tonic-gate 	    "walk the %s cache", c->cache_name);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	w.walk_name = c->cache_name;
94*7c478bd9Sstevel@tonic-gate 	w.walk_descr = descr;
95*7c478bd9Sstevel@tonic-gate 	w.walk_init = umem_walk_init;
96*7c478bd9Sstevel@tonic-gate 	w.walk_step = umem_walk_step;
97*7c478bd9Sstevel@tonic-gate 	w.walk_fini = umem_walk_fini;
98*7c478bd9Sstevel@tonic-gate 	w.walk_init_arg = (void *)addr;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if (mdb_add_walker(&w) == -1)
101*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to add %s walker", c->cache_name);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate int
107*7c478bd9Sstevel@tonic-gate umem_abort_messages(void)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	char *umem_error_buffer;
110*7c478bd9Sstevel@tonic-gate 	uint_t umem_error_begin;
111*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
112*7c478bd9Sstevel@tonic-gate 	size_t bufsize;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_error_begin))
115*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if (umem_lookup_by_name("umem_error_buffer", &sym) == -1) {
118*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to look up umem_error_buffer");
119*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	bufsize = (size_t)sym.st_size;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	umem_error_buffer = mdb_alloc(bufsize+1, UM_SLEEP | UM_GC);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(umem_error_buffer, bufsize, (uintptr_t)sym.st_value)
127*7c478bd9Sstevel@tonic-gate 	    != bufsize) {
128*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to read umem_error_buffer");
129*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 	/* put a zero after the end of the buffer to simplify printing */
132*7c478bd9Sstevel@tonic-gate 	umem_error_buffer[bufsize] = 0;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	if ((umem_error_begin % bufsize) == 0)
135*7c478bd9Sstevel@tonic-gate 		mdb_printf("%s\n", umem_error_buffer);
136*7c478bd9Sstevel@tonic-gate 	else {
137*7c478bd9Sstevel@tonic-gate 		umem_error_buffer[(umem_error_begin % bufsize) - 1] = 0;
138*7c478bd9Sstevel@tonic-gate 		mdb_printf("%s%s\n",
139*7c478bd9Sstevel@tonic-gate 		    &umem_error_buffer[umem_error_begin % bufsize],
140*7c478bd9Sstevel@tonic-gate 		    umem_error_buffer);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate static void
147*7c478bd9Sstevel@tonic-gate umem_log_status(const char *name, umem_log_header_t *val)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	umem_log_header_t my_lh;
150*7c478bd9Sstevel@tonic-gate 	uintptr_t pos = (uintptr_t)val;
151*7c478bd9Sstevel@tonic-gate 	size_t size;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if (pos == NULL)
154*7c478bd9Sstevel@tonic-gate 		return;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&my_lh, sizeof (umem_log_header_t), pos) == -1) {
157*7c478bd9Sstevel@tonic-gate 		mdb_warn("\nunable to read umem_%s_log pointer %p",
158*7c478bd9Sstevel@tonic-gate 		    name, pos);
159*7c478bd9Sstevel@tonic-gate 		return;
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	size = my_lh.lh_chunksize * my_lh.lh_nchunks;
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if (size % (1024 * 1024) == 0)
165*7c478bd9Sstevel@tonic-gate 		mdb_printf("%s=%dm ", name, size / (1024 * 1024));
166*7c478bd9Sstevel@tonic-gate 	else if (size % 1024 == 0)
167*7c478bd9Sstevel@tonic-gate 		mdb_printf("%s=%dk ", name, size / 1024);
168*7c478bd9Sstevel@tonic-gate 	else
169*7c478bd9Sstevel@tonic-gate 		mdb_printf("%s=%d ", name, size);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate typedef struct umem_debug_flags {
173*7c478bd9Sstevel@tonic-gate 	const char	*udf_name;
174*7c478bd9Sstevel@tonic-gate 	uint_t		udf_flags;
175*7c478bd9Sstevel@tonic-gate 	uint_t		udf_clear;	/* if 0, uses udf_flags */
176*7c478bd9Sstevel@tonic-gate } umem_debug_flags_t;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate umem_debug_flags_t umem_status_flags[] = {
179*7c478bd9Sstevel@tonic-gate 	{ "random",	UMF_RANDOMIZE,	UMF_RANDOM },
180*7c478bd9Sstevel@tonic-gate 	{ "default",	UMF_AUDIT | UMF_DEADBEEF | UMF_REDZONE | UMF_CONTENTS },
181*7c478bd9Sstevel@tonic-gate 	{ "audit",	UMF_AUDIT },
182*7c478bd9Sstevel@tonic-gate 	{ "guards",	UMF_DEADBEEF | UMF_REDZONE },
183*7c478bd9Sstevel@tonic-gate 	{ "nosignal",	UMF_CHECKSIGNAL },
184*7c478bd9Sstevel@tonic-gate 	{ "firewall",	UMF_FIREWALL },
185*7c478bd9Sstevel@tonic-gate 	{ "lite",	UMF_LITE },
186*7c478bd9Sstevel@tonic-gate 	{ NULL }
187*7c478bd9Sstevel@tonic-gate };
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
190*7c478bd9Sstevel@tonic-gate int
191*7c478bd9Sstevel@tonic-gate umem_status(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *argv)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate 	int umem_ready;
194*7c478bd9Sstevel@tonic-gate 	int umem_logging;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	umem_log_header_t *umem_transaction_log;
197*7c478bd9Sstevel@tonic-gate 	umem_log_header_t *umem_content_log;
198*7c478bd9Sstevel@tonic-gate 	umem_log_header_t *umem_failure_log;
199*7c478bd9Sstevel@tonic-gate 	umem_log_header_t *umem_slab_log;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_ready))
202*7c478bd9Sstevel@tonic-gate 		goto err;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	mdb_printf("Status:\t\t%s\n",
205*7c478bd9Sstevel@tonic-gate 	    umem_ready == UMEM_READY_INIT_FAILED ? "initialization failed" :
206*7c478bd9Sstevel@tonic-gate 	    umem_ready == UMEM_READY_STARTUP ? "uninitialized" :
207*7c478bd9Sstevel@tonic-gate 	    umem_ready == UMEM_READY_INITING ? "initialization in process" :
208*7c478bd9Sstevel@tonic-gate 	    umem_ready == UMEM_READY ? "ready and active" :
209*7c478bd9Sstevel@tonic-gate 	    "unknown (umem_ready invalid)");
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	mdb_printf("Concurrency:\t%d\n", umem_max_ncpus);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_logging))
214*7c478bd9Sstevel@tonic-gate 		goto err;
215*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_transaction_log))
216*7c478bd9Sstevel@tonic-gate 		goto err;
217*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_content_log))
218*7c478bd9Sstevel@tonic-gate 		goto err;
219*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_failure_log))
220*7c478bd9Sstevel@tonic-gate 		goto err;
221*7c478bd9Sstevel@tonic-gate 	if (UMEM_READVAR(umem_slab_log))
222*7c478bd9Sstevel@tonic-gate 		goto err;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	mdb_printf("Logs:\t\t");
225*7c478bd9Sstevel@tonic-gate 	umem_log_status("transaction", umem_transaction_log);
226*7c478bd9Sstevel@tonic-gate 	umem_log_status("content", umem_content_log);
227*7c478bd9Sstevel@tonic-gate 	umem_log_status("fail", umem_failure_log);
228*7c478bd9Sstevel@tonic-gate 	umem_log_status("slab", umem_slab_log);
229*7c478bd9Sstevel@tonic-gate 	if (!umem_logging)
230*7c478bd9Sstevel@tonic-gate 		mdb_printf("(inactive)");
231*7c478bd9Sstevel@tonic-gate 	mdb_printf("\n");
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	mdb_printf("Message buffer:\n");
234*7c478bd9Sstevel@tonic-gate 	return (umem_abort_messages());
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate err:
237*7c478bd9Sstevel@tonic-gate 	mdb_printf("Message buffer:\n");
238*7c478bd9Sstevel@tonic-gate 	(void) umem_abort_messages();
239*7c478bd9Sstevel@tonic-gate 	return (DCMD_ERR);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate typedef struct {
243*7c478bd9Sstevel@tonic-gate 	uintptr_t ucw_first;
244*7c478bd9Sstevel@tonic-gate 	uintptr_t ucw_current;
245*7c478bd9Sstevel@tonic-gate } umem_cache_walk_t;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate int
248*7c478bd9Sstevel@tonic-gate umem_cache_walk_init(mdb_walk_state_t *wsp)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	umem_cache_walk_t *ucw;
251*7c478bd9Sstevel@tonic-gate 	umem_cache_t c;
252*7c478bd9Sstevel@tonic-gate 	uintptr_t cp;
253*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	if (umem_lookup_by_name("umem_null_cache", &sym) == -1) {
256*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't find umem_null_cache");
257*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	cp = (uintptr_t)sym.st_value;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (umem_cache_t), cp) == -1) {
263*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read cache at %p", cp);
264*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	ucw = mdb_alloc(sizeof (umem_cache_walk_t), UM_SLEEP);
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	ucw->ucw_first = cp;
270*7c478bd9Sstevel@tonic-gate 	ucw->ucw_current = (uintptr_t)c.cache_next;
271*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = ucw;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate int
277*7c478bd9Sstevel@tonic-gate umem_cache_walk_step(mdb_walk_state_t *wsp)
278*7c478bd9Sstevel@tonic-gate {
279*7c478bd9Sstevel@tonic-gate 	umem_cache_walk_t *ucw = wsp->walk_data;
280*7c478bd9Sstevel@tonic-gate 	umem_cache_t c;
281*7c478bd9Sstevel@tonic-gate 	int status;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (umem_cache_t), ucw->ucw_current) == -1) {
284*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read cache at %p", ucw->ucw_current);
285*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
286*7c478bd9Sstevel@tonic-gate 	}
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	status = wsp->walk_callback(ucw->ucw_current, &c, wsp->walk_cbdata);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	if ((ucw->ucw_current = (uintptr_t)c.cache_next) == ucw->ucw_first)
291*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	return (status);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate void
297*7c478bd9Sstevel@tonic-gate umem_cache_walk_fini(mdb_walk_state_t *wsp)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	umem_cache_walk_t *ucw = wsp->walk_data;
300*7c478bd9Sstevel@tonic-gate 	mdb_free(ucw, sizeof (umem_cache_walk_t));
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate typedef struct {
304*7c478bd9Sstevel@tonic-gate 	umem_cpu_t *ucw_cpus;
305*7c478bd9Sstevel@tonic-gate 	uint32_t ucw_current;
306*7c478bd9Sstevel@tonic-gate 	uint32_t ucw_max;
307*7c478bd9Sstevel@tonic-gate } umem_cpu_walk_state_t;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate int
310*7c478bd9Sstevel@tonic-gate umem_cpu_walk_init(mdb_walk_state_t *wsp)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	umem_cpu_t *umem_cpus;
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	umem_cpu_walk_state_t *ucw;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if (umem_readvar(&umem_cpus, "umem_cpus") == -1) {
317*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'umem_cpus'");
318*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
319*7c478bd9Sstevel@tonic-gate 	}
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	ucw = mdb_alloc(sizeof (*ucw), UM_SLEEP);
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	ucw->ucw_cpus = umem_cpus;
324*7c478bd9Sstevel@tonic-gate 	ucw->ucw_current = 0;
325*7c478bd9Sstevel@tonic-gate 	ucw->ucw_max = umem_max_ncpus;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = ucw;
328*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate int
332*7c478bd9Sstevel@tonic-gate umem_cpu_walk_step(mdb_walk_state_t *wsp)
333*7c478bd9Sstevel@tonic-gate {
334*7c478bd9Sstevel@tonic-gate 	umem_cpu_t cpu;
335*7c478bd9Sstevel@tonic-gate 	umem_cpu_walk_state_t *ucw = wsp->walk_data;
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	uintptr_t caddr;
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	if (ucw->ucw_current >= ucw->ucw_max)
340*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	caddr = (uintptr_t)&(ucw->ucw_cpus[ucw->ucw_current]);
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&cpu, sizeof (umem_cpu_t), caddr) == -1) {
345*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read cpu %d", ucw->ucw_current);
346*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	ucw->ucw_current++;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(caddr, &cpu, wsp->walk_cbdata));
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate void
355*7c478bd9Sstevel@tonic-gate umem_cpu_walk_fini(mdb_walk_state_t *wsp)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 	umem_cpu_walk_state_t *ucw = wsp->walk_data;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	mdb_free(ucw, sizeof (*ucw));
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate int
363*7c478bd9Sstevel@tonic-gate umem_cpu_cache_walk_init(mdb_walk_state_t *wsp)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
366*7c478bd9Sstevel@tonic-gate 		mdb_warn("umem_cpu_cache doesn't support global walks");
367*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	if (mdb_layered_walk("umem_cpu", wsp) == -1) {
371*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't walk 'umem_cpu'");
372*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
373*7c478bd9Sstevel@tonic-gate 	}
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = (void *)wsp->walk_addr;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate int
381*7c478bd9Sstevel@tonic-gate umem_cpu_cache_walk_step(mdb_walk_state_t *wsp)
382*7c478bd9Sstevel@tonic-gate {
383*7c478bd9Sstevel@tonic-gate 	uintptr_t caddr = (uintptr_t)wsp->walk_data;
384*7c478bd9Sstevel@tonic-gate 	const umem_cpu_t *cpu = wsp->walk_layer;
385*7c478bd9Sstevel@tonic-gate 	umem_cpu_cache_t cc;
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 	caddr += cpu->cpu_cache_offset;
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&cc, sizeof (umem_cpu_cache_t), caddr) == -1) {
390*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read umem_cpu_cache at %p", caddr);
391*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
392*7c478bd9Sstevel@tonic-gate 	}
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(caddr, &cc, wsp->walk_cbdata));
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate int
398*7c478bd9Sstevel@tonic-gate umem_slab_walk_init(mdb_walk_state_t *wsp)
399*7c478bd9Sstevel@tonic-gate {
400*7c478bd9Sstevel@tonic-gate 	uintptr_t caddr = wsp->walk_addr;
401*7c478bd9Sstevel@tonic-gate 	umem_cache_t c;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	if (caddr == NULL) {
404*7c478bd9Sstevel@tonic-gate 		mdb_warn("umem_slab doesn't support global walks\n");
405*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
406*7c478bd9Sstevel@tonic-gate 	}
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (c), caddr) == -1) {
409*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read umem_cache at %p", caddr);
410*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	wsp->walk_data =
414*7c478bd9Sstevel@tonic-gate 	    (void *)(caddr + offsetof(umem_cache_t, cache_nullslab));
415*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)c.cache_nullslab.slab_next;
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate int
421*7c478bd9Sstevel@tonic-gate umem_slab_walk_partial_init(mdb_walk_state_t *wsp)
422*7c478bd9Sstevel@tonic-gate {
423*7c478bd9Sstevel@tonic-gate 	uintptr_t caddr = wsp->walk_addr;
424*7c478bd9Sstevel@tonic-gate 	umem_cache_t c;
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	if (caddr == NULL) {
427*7c478bd9Sstevel@tonic-gate 		mdb_warn("umem_slab_partial doesn't support global walks\n");
428*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
429*7c478bd9Sstevel@tonic-gate 	}
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (c), caddr) == -1) {
432*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read umem_cache at %p", caddr);
433*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
434*7c478bd9Sstevel@tonic-gate 	}
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	wsp->walk_data =
437*7c478bd9Sstevel@tonic-gate 	    (void *)(caddr + offsetof(umem_cache_t, cache_nullslab));
438*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)c.cache_freelist;
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	/*
441*7c478bd9Sstevel@tonic-gate 	 * Some consumers (umem_walk_step(), in particular) require at
442*7c478bd9Sstevel@tonic-gate 	 * least one callback if there are any buffers in the cache.  So
443*7c478bd9Sstevel@tonic-gate 	 * if there are *no* partial slabs, report the last full slab, if
444*7c478bd9Sstevel@tonic-gate 	 * any.
445*7c478bd9Sstevel@tonic-gate 	 *
446*7c478bd9Sstevel@tonic-gate 	 * Yes, this is ugly, but it's cleaner than the other possibilities.
447*7c478bd9Sstevel@tonic-gate 	 */
448*7c478bd9Sstevel@tonic-gate 	if ((uintptr_t)wsp->walk_data == wsp->walk_addr)
449*7c478bd9Sstevel@tonic-gate 		wsp->walk_addr = (uintptr_t)c.cache_nullslab.slab_prev;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate int
455*7c478bd9Sstevel@tonic-gate umem_slab_walk_step(mdb_walk_state_t *wsp)
456*7c478bd9Sstevel@tonic-gate {
457*7c478bd9Sstevel@tonic-gate 	umem_slab_t s;
458*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
459*7c478bd9Sstevel@tonic-gate 	uintptr_t saddr = (uintptr_t)wsp->walk_data;
460*7c478bd9Sstevel@tonic-gate 	uintptr_t caddr = saddr - offsetof(umem_cache_t, cache_nullslab);
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	if (addr == saddr)
463*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&s, sizeof (s), addr) == -1) {
466*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read slab at %p", wsp->walk_addr);
467*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
468*7c478bd9Sstevel@tonic-gate 	}
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	if ((uintptr_t)s.slab_cache != caddr) {
471*7c478bd9Sstevel@tonic-gate 		mdb_warn("slab %p isn't in cache %p (in cache %p)\n",
472*7c478bd9Sstevel@tonic-gate 		    addr, caddr, s.slab_cache);
473*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
474*7c478bd9Sstevel@tonic-gate 	}
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)s.slab_next;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &s, wsp->walk_cbdata));
479*7c478bd9Sstevel@tonic-gate }
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate int
482*7c478bd9Sstevel@tonic-gate umem_cache(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *argv)
483*7c478bd9Sstevel@tonic-gate {
484*7c478bd9Sstevel@tonic-gate 	umem_cache_t c;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
487*7c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("umem_cache", "umem_cache", ac, argv) == -1) {
488*7c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk umem_cache");
489*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
490*7c478bd9Sstevel@tonic-gate 		}
491*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
492*7c478bd9Sstevel@tonic-gate 	}
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
495*7c478bd9Sstevel@tonic-gate 		mdb_printf("%-?s %-25s %4s %8s %8s %8s\n", "ADDR", "NAME",
496*7c478bd9Sstevel@tonic-gate 		    "FLAG", "CFLAG", "BUFSIZE", "BUFTOTL");
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (c), addr) == -1) {
499*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read umem_cache at %p", addr);
500*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
501*7c478bd9Sstevel@tonic-gate 	}
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	mdb_printf("%0?p %-25s %04x %08x %8ld %8lld\n", addr, c.cache_name,
504*7c478bd9Sstevel@tonic-gate 	    c.cache_flags, c.cache_cflags, c.cache_bufsize, c.cache_buftotal);
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
507*7c478bd9Sstevel@tonic-gate }
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate static int
510*7c478bd9Sstevel@tonic-gate addrcmp(const void *lhs, const void *rhs)
511*7c478bd9Sstevel@tonic-gate {
512*7c478bd9Sstevel@tonic-gate 	uintptr_t p1 = *((uintptr_t *)lhs);
513*7c478bd9Sstevel@tonic-gate 	uintptr_t p2 = *((uintptr_t *)rhs);
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	if (p1 < p2)
516*7c478bd9Sstevel@tonic-gate 		return (-1);
517*7c478bd9Sstevel@tonic-gate 	if (p1 > p2)
518*7c478bd9Sstevel@tonic-gate 		return (1);
519*7c478bd9Sstevel@tonic-gate 	return (0);
520*7c478bd9Sstevel@tonic-gate }
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate static int
523*7c478bd9Sstevel@tonic-gate bufctlcmp(const umem_bufctl_audit_t **lhs, const umem_bufctl_audit_t **rhs)
524*7c478bd9Sstevel@tonic-gate {
525*7c478bd9Sstevel@tonic-gate 	const umem_bufctl_audit_t *bcp1 = *lhs;
526*7c478bd9Sstevel@tonic-gate 	const umem_bufctl_audit_t *bcp2 = *rhs;
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 	if (bcp1->bc_timestamp > bcp2->bc_timestamp)
529*7c478bd9Sstevel@tonic-gate 		return (-1);
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 	if (bcp1->bc_timestamp < bcp2->bc_timestamp)
532*7c478bd9Sstevel@tonic-gate 		return (1);
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	return (0);
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate typedef struct umem_hash_walk {
538*7c478bd9Sstevel@tonic-gate 	uintptr_t *umhw_table;
539*7c478bd9Sstevel@tonic-gate 	size_t umhw_nelems;
540*7c478bd9Sstevel@tonic-gate 	size_t umhw_pos;
541*7c478bd9Sstevel@tonic-gate 	umem_bufctl_t umhw_cur;
542*7c478bd9Sstevel@tonic-gate } umem_hash_walk_t;
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate int
545*7c478bd9Sstevel@tonic-gate umem_hash_walk_init(mdb_walk_state_t *wsp)
546*7c478bd9Sstevel@tonic-gate {
547*7c478bd9Sstevel@tonic-gate 	umem_hash_walk_t *umhw;
548*7c478bd9Sstevel@tonic-gate 	uintptr_t *hash;
549*7c478bd9Sstevel@tonic-gate 	umem_cache_t c;
550*7c478bd9Sstevel@tonic-gate 	uintptr_t haddr, addr = wsp->walk_addr;
551*7c478bd9Sstevel@tonic-gate 	size_t nelems;
552*7c478bd9Sstevel@tonic-gate 	size_t hsize;
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
555*7c478bd9Sstevel@tonic-gate 		mdb_warn("umem_hash doesn't support global walks\n");
556*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&c, sizeof (c), addr) == -1) {
560*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read cache at addr %p", addr);
561*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 	if (!(c.cache_flags & UMF_HASH)) {
565*7c478bd9Sstevel@tonic-gate 		mdb_warn("cache %p doesn't have a hash table\n", addr);
566*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);		/* nothing to do */
567*7c478bd9Sstevel@tonic-gate 	}
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 	umhw = mdb_zalloc(sizeof (umem_hash_walk_t), UM_SLEEP);
570*7c478bd9Sstevel@tonic-gate 	umhw->umhw_cur.bc_next = NULL;
571*7c478bd9Sstevel@tonic-gate 	umhw->umhw_pos = 0;
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	umhw->umhw_nelems = nelems = c.cache_hash_mask + 1;
574*7c478bd9Sstevel@tonic-gate 	hsize = nelems * sizeof (uintptr_t);
575*7c478bd9Sstevel@tonic-gate 	haddr = (uintptr_t)c.cache_hash_table;
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	umhw->umhw_table = hash = mdb_alloc(hsize, UM_SLEEP);
578*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(hash, hsize, haddr) == -1) {
579*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read hash table at %p", haddr);
580*7c478bd9Sstevel@tonic-gate 		mdb_free(hash, hsize);
581*7c478bd9Sstevel@tonic-gate 		mdb_free(umhw, sizeof (umem_hash_walk_t));
582*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
583*7c478bd9Sstevel@tonic-gate 	}
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = umhw;
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
588*7c478bd9Sstevel@tonic-gate }
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate int
591*7c478bd9Sstevel@tonic-gate umem_hash_walk_step(mdb_walk_state_t *wsp)
592*7c478bd9Sstevel@tonic-gate {
593*7c478bd9Sstevel@tonic-gate 	umem_hash_walk_t *umhw = wsp->walk_data;
594*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = NULL;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	if ((addr = (uintptr_t)umhw->umhw_cur.bc_next) == NULL) {
597*7c478bd9Sstevel@tonic-gate 		while (umhw->umhw_pos < umhw->umhw_nelems) {
598*7c478bd9Sstevel@tonic-gate 			if ((addr = umhw->umhw_table[umhw->umhw_pos++]) != NULL)
599*7c478bd9Sstevel@tonic-gate 				break;
600*7c478bd9Sstevel@tonic-gate 		}
601*7c478bd9Sstevel@tonic-gate 	}
602*7c478bd9Sstevel@tonic-gate 	if (addr == NULL)
603*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&umhw->umhw_cur, sizeof (umem_bufctl_t), addr) == -1) {
606*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read umem_bufctl_t at addr %p", addr);
607*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
608*7c478bd9Sstevel@tonic-gate 	}
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(addr, &umhw->umhw_cur, wsp->walk_cbdata));
611*7c478bd9Sstevel@tonic-gate }
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate void
614*7c478bd9Sstevel@tonic-gate umem_hash_walk_fini(mdb_walk_state_t *wsp)
615*7c478bd9Sstevel@tonic-gate {
616*7c478bd9Sstevel@tonic-gate 	umem_hash_walk_t *umhw = wsp->walk_data;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	if (umhw == NULL)
619*7c478bd9Sstevel@tonic-gate 		return;
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 	mdb_free(umhw->umhw_table, umhw->umhw_nelems * sizeof (uintptr_t));
622*7c478bd9Sstevel@tonic-gate 	mdb_free(umhw, sizeof (umem_hash_walk_t));
623*7c478bd9Sstevel@tonic-gate }
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate /*
626*7c478bd9Sstevel@tonic-gate  * Find the address of the bufctl structure for the address 'buf' in cache
627*7c478bd9Sstevel@tonic-gate  * 'cp', which is at address caddr, and place it in *out.
628*7c478bd9Sstevel@tonic-gate  */
629*7c478bd9Sstevel@tonic-gate static int
630*7c478bd9Sstevel@tonic-gate umem_hash_lookup(umem_cache_t *cp, uintptr_t caddr, void *buf, uintptr_t *out)
631*7c478bd9Sstevel@tonic-gate {
632*7c478bd9Sstevel@tonic-gate 	uintptr_t bucket = (uintptr_t)UMEM_HASH(cp, buf);
633*7c478bd9Sstevel@tonic-gate 	umem_bufctl_t *bcp;
634*7c478bd9Sstevel@tonic-gate 	umem_bufctl_t bc;
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&bcp, sizeof (umem_bufctl_t *), bucket) == -1) {
637*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to read hash bucket for %p in cache %p",
638*7c478bd9Sstevel@tonic-gate 		    buf, caddr);
639*7c478bd9Sstevel@tonic-gate 		return (-1);
640*7c478bd9Sstevel@tonic-gate 	}
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate 	while (bcp != NULL) {
643*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&bc, sizeof (umem_bufctl_t),
644*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)bcp) == -1) {
645*7c478bd9Sstevel@tonic-gate 			mdb_warn("unable to read bufctl at %p", bcp);
646*7c478bd9Sstevel@tonic-gate 			return (-1);
647*7c478bd9Sstevel@tonic-gate 		}
648*7c478bd9Sstevel@tonic-gate 		if (bc.bc_addr == buf) {
649*7c478bd9Sstevel@tonic-gate 			*out = (uintptr_t)bcp;
650*7c478bd9Sstevel@tonic-gate 			return (0);
651*7c478bd9Sstevel@tonic-gate 		}
652*7c478bd9Sstevel@tonic-gate 		bcp = bc.bc_next;
653*7c478bd9Sstevel@tonic-gate 	}
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 	mdb_warn("unable to find bufctl for %p in cache %p\n", buf, caddr);
656*7c478bd9Sstevel@tonic-gate 	return (-1);
657*7c478bd9Sstevel@tonic-gate }
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate int
660*7c478bd9Sstevel@tonic-gate umem_get_magsize(const umem_cache_t *cp)
661*7c478bd9Sstevel@tonic-gate {
662*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = (uintptr_t)cp->cache_magtype;
663*7c478bd9Sstevel@tonic-gate 	GElf_Sym mt_sym;
664*7c478bd9Sstevel@tonic-gate 	umem_magtype_t mt;
665*7c478bd9Sstevel@tonic-gate 	int res;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	/*
668*7c478bd9Sstevel@tonic-gate 	 * if cpu 0 has a non-zero magsize, it must be correct.  caches
669*7c478bd9Sstevel@tonic-gate 	 * with UMF_NOMAGAZINE have disabled their magazine layers, so
670*7c478bd9Sstevel@tonic-gate 	 * it is okay to return 0 for them.
671*7c478bd9Sstevel@tonic-gate 	 */
672*7c478bd9Sstevel@tonic-gate 	if ((res = cp->cache_cpu[0].cc_magsize) != 0 ||
673*7c478bd9Sstevel@tonic-gate 	    (cp->cache_flags & UMF_NOMAGAZINE))
674*7c478bd9Sstevel@tonic-gate 		return (res);
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 	if (mdb_lookup_by_name("umem_magtype", &mt_sym) == -1) {
677*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to read 'umem_magtype'");
678*7c478bd9Sstevel@tonic-gate 	} else if (addr < mt_sym.st_value ||
679*7c478bd9Sstevel@tonic-gate 	    addr + sizeof (mt) - 1 > mt_sym.st_value + mt_sym.st_size - 1 ||
680*7c478bd9Sstevel@tonic-gate 	    ((addr - mt_sym.st_value) % sizeof (mt)) != 0) {
681*7c478bd9Sstevel@tonic-gate 		mdb_warn("cache '%s' has invalid magtype pointer (%p)\n",
682*7c478bd9Sstevel@tonic-gate 		    cp->cache_name, addr);
683*7c478bd9Sstevel@tonic-gate 		return (0);
684*7c478bd9Sstevel@tonic-gate 	}
685*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&mt, sizeof (mt), addr) == -1) {
686*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to read magtype at %a", addr);
687*7c478bd9Sstevel@tonic-gate 		return (0);
688*7c478bd9Sstevel@tonic-gate 	}
689*7c478bd9Sstevel@tonic-gate 	return (mt.mt_magsize);
690*7c478bd9Sstevel@tonic-gate }
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
693*7c478bd9Sstevel@tonic-gate static int
694*7c478bd9Sstevel@tonic-gate umem_estimate_slab(uintptr_t addr, const umem_slab_t *sp, size_t *est)
695*7c478bd9Sstevel@tonic-gate {
696*7c478bd9Sstevel@tonic-gate 	*est -= (sp->slab_chunks - sp->slab_refcnt);
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate /*
702*7c478bd9Sstevel@tonic-gate  * Returns an upper bound on the number of allocated buffers in a given
703*7c478bd9Sstevel@tonic-gate  * cache.
704*7c478bd9Sstevel@tonic-gate  */
705*7c478bd9Sstevel@tonic-gate size_t
706*7c478bd9Sstevel@tonic-gate umem_estimate_allocated(uintptr_t addr, const umem_cache_t *cp)
707*7c478bd9Sstevel@tonic-gate {
708*7c478bd9Sstevel@tonic-gate 	int magsize;
709*7c478bd9Sstevel@tonic-gate 	size_t cache_est;
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate 	cache_est = cp->cache_buftotal;
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 	(void) mdb_pwalk("umem_slab_partial",
714*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)umem_estimate_slab, &cache_est, addr);
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	if ((magsize = umem_get_magsize(cp)) != 0) {
717*7c478bd9Sstevel@tonic-gate 		size_t mag_est = cp->cache_full.ml_total * magsize;
718*7c478bd9Sstevel@tonic-gate 
719*7c478bd9Sstevel@tonic-gate 		if (cache_est >= mag_est) {
720*7c478bd9Sstevel@tonic-gate 			cache_est -= mag_est;
721*7c478bd9Sstevel@tonic-gate 		} else {
722*7c478bd9Sstevel@tonic-gate 			mdb_warn("cache %p's magazine layer holds more buffers "
723*7c478bd9Sstevel@tonic-gate 			    "than the slab layer.\n", addr);
724*7c478bd9Sstevel@tonic-gate 		}
725*7c478bd9Sstevel@tonic-gate 	}
726*7c478bd9Sstevel@tonic-gate 	return (cache_est);
727*7c478bd9Sstevel@tonic-gate }
728*7c478bd9Sstevel@tonic-gate 
729*7c478bd9Sstevel@tonic-gate #define	READMAG_ROUNDS(rounds) { \
730*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(mp, magbsize, (uintptr_t)ump) == -1) { \
731*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read magazine at %p", ump); \
732*7c478bd9Sstevel@tonic-gate 		goto fail; \
733*7c478bd9Sstevel@tonic-gate 	} \
734*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < rounds; i++) { \
735*7c478bd9Sstevel@tonic-gate 		maglist[magcnt++] = mp->mag_round[i]; \
736*7c478bd9Sstevel@tonic-gate 		if (magcnt == magmax) { \
737*7c478bd9Sstevel@tonic-gate 			mdb_warn("%d magazines exceeds fudge factor\n", \
738*7c478bd9Sstevel@tonic-gate 			    magcnt); \
739*7c478bd9Sstevel@tonic-gate 			goto fail; \
740*7c478bd9Sstevel@tonic-gate 		} \
741*7c478bd9Sstevel@tonic-gate 	} \
742*7c478bd9Sstevel@tonic-gate }
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate int
745*7c478bd9Sstevel@tonic-gate umem_read_magazines(umem_cache_t *cp, uintptr_t addr, int ncpus,
746*7c478bd9Sstevel@tonic-gate     void ***maglistp, size_t *magcntp, size_t *magmaxp, int alloc_flags)
747*7c478bd9Sstevel@tonic-gate {
748*7c478bd9Sstevel@tonic-gate 	umem_magazine_t *ump, *mp;
749*7c478bd9Sstevel@tonic-gate 	void **maglist = NULL;
750*7c478bd9Sstevel@tonic-gate 	int i, cpu;
751*7c478bd9Sstevel@tonic-gate 	size_t magsize, magmax, magbsize;
752*7c478bd9Sstevel@tonic-gate 	size_t magcnt = 0;
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	/*
755*7c478bd9Sstevel@tonic-gate 	 * Read the magtype out of the cache, after verifying the pointer's
756*7c478bd9Sstevel@tonic-gate 	 * correctness.
757*7c478bd9Sstevel@tonic-gate 	 */
758*7c478bd9Sstevel@tonic-gate 	magsize = umem_get_magsize(cp);
759*7c478bd9Sstevel@tonic-gate 	if (magsize == 0)
760*7c478bd9Sstevel@tonic-gate 		magsize = 1;
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	/*
763*7c478bd9Sstevel@tonic-gate 	 * There are several places where we need to go buffer hunting:
764*7c478bd9Sstevel@tonic-gate 	 * the per-CPU loaded magazine, the per-CPU spare full magazine,
765*7c478bd9Sstevel@tonic-gate 	 * and the full magazine list in the depot.
766*7c478bd9Sstevel@tonic-gate 	 *
767*7c478bd9Sstevel@tonic-gate 	 * For an upper bound on the number of buffers in the magazine
768*7c478bd9Sstevel@tonic-gate 	 * layer, we have the number of magazines on the cache_full
769*7c478bd9Sstevel@tonic-gate 	 * list plus at most two magazines per CPU (the loaded and the
770*7c478bd9Sstevel@tonic-gate 	 * spare).  Toss in 100 magazines as a fudge factor in case this
771*7c478bd9Sstevel@tonic-gate 	 * is live (the number "100" comes from the same fudge factor in
772*7c478bd9Sstevel@tonic-gate 	 * crash(1M)).
773*7c478bd9Sstevel@tonic-gate 	 */
774*7c478bd9Sstevel@tonic-gate 	magmax = (cp->cache_full.ml_total + 2 * ncpus + 100) * magsize;
775*7c478bd9Sstevel@tonic-gate 	magbsize = offsetof(umem_magazine_t, mag_round[magsize]);
776*7c478bd9Sstevel@tonic-gate 
777*7c478bd9Sstevel@tonic-gate 	if (magbsize >= PAGESIZE / 2) {
778*7c478bd9Sstevel@tonic-gate 		mdb_warn("magazine size for cache %p unreasonable (%x)\n",
779*7c478bd9Sstevel@tonic-gate 		    addr, magbsize);
780*7c478bd9Sstevel@tonic-gate 		goto fail;
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	maglist = mdb_alloc(magmax * sizeof (void *), alloc_flags);
784*7c478bd9Sstevel@tonic-gate 	mp = mdb_alloc(magbsize, alloc_flags);
785*7c478bd9Sstevel@tonic-gate 	if (mp == NULL || maglist == NULL)
786*7c478bd9Sstevel@tonic-gate 		goto fail;
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	/*
789*7c478bd9Sstevel@tonic-gate 	 * First up: the magazines in the depot (i.e. on the cache_full list).
790*7c478bd9Sstevel@tonic-gate 	 */
791*7c478bd9Sstevel@tonic-gate 	for (ump = cp->cache_full.ml_list; ump != NULL; ) {
792*7c478bd9Sstevel@tonic-gate 		READMAG_ROUNDS(magsize);
793*7c478bd9Sstevel@tonic-gate 		ump = mp->mag_next;
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 		if (ump == cp->cache_full.ml_list)
796*7c478bd9Sstevel@tonic-gate 			break; /* cache_full list loop detected */
797*7c478bd9Sstevel@tonic-gate 	}
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 	dprintf(("cache_full list done\n"));
800*7c478bd9Sstevel@tonic-gate 
801*7c478bd9Sstevel@tonic-gate 	/*
802*7c478bd9Sstevel@tonic-gate 	 * Now whip through the CPUs, snagging the loaded magazines
803*7c478bd9Sstevel@tonic-gate 	 * and full spares.
804*7c478bd9Sstevel@tonic-gate 	 */
805*7c478bd9Sstevel@tonic-gate 	for (cpu = 0; cpu < ncpus; cpu++) {
806*7c478bd9Sstevel@tonic-gate 		umem_cpu_cache_t *ccp = &cp->cache_cpu[cpu];
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate 		dprintf(("reading cpu cache %p\n",
809*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)ccp - (uintptr_t)cp + addr));
810*7c478bd9Sstevel@tonic-gate 
811*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_rounds > 0 &&
812*7c478bd9Sstevel@tonic-gate 		    (ump = ccp->cc_loaded) != NULL) {
813*7c478bd9Sstevel@tonic-gate 			dprintf(("reading %d loaded rounds\n", ccp->cc_rounds));
814*7c478bd9Sstevel@tonic-gate 			READMAG_ROUNDS(ccp->cc_rounds);
815*7c478bd9Sstevel@tonic-gate 		}
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds > 0 &&
818*7c478bd9Sstevel@tonic-gate 		    (ump = ccp->cc_ploaded) != NULL) {
819*7c478bd9Sstevel@tonic-gate 			dprintf(("reading %d previously loaded rounds\n",
820*7c478bd9Sstevel@tonic-gate 			    ccp->cc_prounds));
821*7c478bd9Sstevel@tonic-gate 			READMAG_ROUNDS(ccp->cc_prounds);
822*7c478bd9Sstevel@tonic-gate 		}
823*7c478bd9Sstevel@tonic-gate 	}
824*7c478bd9Sstevel@tonic-gate 
825*7c478bd9Sstevel@tonic-gate 	dprintf(("magazine layer: %d buffers\n", magcnt));
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate 	if (!(alloc_flags & UM_GC))
828*7c478bd9Sstevel@tonic-gate 		mdb_free(mp, magbsize);
829*7c478bd9Sstevel@tonic-gate 
830*7c478bd9Sstevel@tonic-gate 	*maglistp = maglist;
831*7c478bd9Sstevel@tonic-gate 	*magcntp = magcnt;
832*7c478bd9Sstevel@tonic-gate 	*magmaxp = magmax;
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate fail:
837*7c478bd9Sstevel@tonic-gate 	if (!(alloc_flags & UM_GC)) {
838*7c478bd9Sstevel@tonic-gate 		if (mp)
839*7c478bd9Sstevel@tonic-gate 			mdb_free(mp, magbsize);
840*7c478bd9Sstevel@tonic-gate 		if (maglist)
841*7c478bd9Sstevel@tonic-gate 			mdb_free(maglist, magmax * sizeof (void *));
842*7c478bd9Sstevel@tonic-gate 	}
843*7c478bd9Sstevel@tonic-gate 	return (WALK_ERR);
844*7c478bd9Sstevel@tonic-gate }
845*7c478bd9Sstevel@tonic-gate 
846*7c478bd9Sstevel@tonic-gate static int
847*7c478bd9Sstevel@tonic-gate umem_walk_callback(mdb_walk_state_t *wsp, uintptr_t buf)
848*7c478bd9Sstevel@tonic-gate {
849*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(buf, NULL, wsp->walk_cbdata));
850*7c478bd9Sstevel@tonic-gate }
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate static int
853*7c478bd9Sstevel@tonic-gate bufctl_walk_callback(umem_cache_t *cp, mdb_walk_state_t *wsp, uintptr_t buf)
854*7c478bd9Sstevel@tonic-gate {
855*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t *b;
856*7c478bd9Sstevel@tonic-gate 	UMEM_LOCAL_BUFCTL_AUDIT(&b);
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate 	/*
859*7c478bd9Sstevel@tonic-gate 	 * if UMF_AUDIT is not set, we know that we're looking at a
860*7c478bd9Sstevel@tonic-gate 	 * umem_bufctl_t.
861*7c478bd9Sstevel@tonic-gate 	 */
862*7c478bd9Sstevel@tonic-gate 	if (!(cp->cache_flags & UMF_AUDIT) ||
863*7c478bd9Sstevel@tonic-gate 	    mdb_vread(b, UMEM_BUFCTL_AUDIT_SIZE, buf) == -1) {
864*7c478bd9Sstevel@tonic-gate 		(void) memset(b, 0, UMEM_BUFCTL_AUDIT_SIZE);
865*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(b, sizeof (umem_bufctl_t), buf) == -1) {
866*7c478bd9Sstevel@tonic-gate 			mdb_warn("unable to read bufctl at %p", buf);
867*7c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
868*7c478bd9Sstevel@tonic-gate 		}
869*7c478bd9Sstevel@tonic-gate 	}
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(buf, b, wsp->walk_cbdata));
872*7c478bd9Sstevel@tonic-gate }
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate typedef struct umem_walk {
875*7c478bd9Sstevel@tonic-gate 	int umw_type;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 	int umw_addr;			/* cache address */
878*7c478bd9Sstevel@tonic-gate 	umem_cache_t *umw_cp;
879*7c478bd9Sstevel@tonic-gate 	size_t umw_csize;
880*7c478bd9Sstevel@tonic-gate 
881*7c478bd9Sstevel@tonic-gate 	/*
882*7c478bd9Sstevel@tonic-gate 	 * magazine layer
883*7c478bd9Sstevel@tonic-gate 	 */
884*7c478bd9Sstevel@tonic-gate 	void **umw_maglist;
885*7c478bd9Sstevel@tonic-gate 	size_t umw_max;
886*7c478bd9Sstevel@tonic-gate 	size_t umw_count;
887*7c478bd9Sstevel@tonic-gate 	size_t umw_pos;
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate 	/*
890*7c478bd9Sstevel@tonic-gate 	 * slab layer
891*7c478bd9Sstevel@tonic-gate 	 */
892*7c478bd9Sstevel@tonic-gate 	char *umw_valid;	/* to keep track of freed buffers */
893*7c478bd9Sstevel@tonic-gate 	char *umw_ubase;	/* buffer for slab data */
894*7c478bd9Sstevel@tonic-gate } umem_walk_t;
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate static int
897*7c478bd9Sstevel@tonic-gate umem_walk_init_common(mdb_walk_state_t *wsp, int type)
898*7c478bd9Sstevel@tonic-gate {
899*7c478bd9Sstevel@tonic-gate 	umem_walk_t *umw;
900*7c478bd9Sstevel@tonic-gate 	int ncpus, csize;
901*7c478bd9Sstevel@tonic-gate 	umem_cache_t *cp;
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 	size_t magmax, magcnt;
904*7c478bd9Sstevel@tonic-gate 	void **maglist = NULL;
905*7c478bd9Sstevel@tonic-gate 	uint_t chunksize, slabsize;
906*7c478bd9Sstevel@tonic-gate 	int status = WALK_ERR;
907*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = wsp->walk_addr;
908*7c478bd9Sstevel@tonic-gate 	const char *layered;
909*7c478bd9Sstevel@tonic-gate 
910*7c478bd9Sstevel@tonic-gate 	type &= ~UM_HASH;
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
913*7c478bd9Sstevel@tonic-gate 		mdb_warn("umem walk doesn't support global walks\n");
914*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
915*7c478bd9Sstevel@tonic-gate 	}
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate 	dprintf(("walking %p\n", addr));
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	/*
920*7c478bd9Sstevel@tonic-gate 	 * First we need to figure out how many CPUs are configured in the
921*7c478bd9Sstevel@tonic-gate 	 * system to know how much to slurp out.
922*7c478bd9Sstevel@tonic-gate 	 */
923*7c478bd9Sstevel@tonic-gate 	umem_readvar(&ncpus, "umem_max_ncpus");
924*7c478bd9Sstevel@tonic-gate 
925*7c478bd9Sstevel@tonic-gate 	csize = UMEM_CACHE_SIZE(ncpus);
926*7c478bd9Sstevel@tonic-gate 	cp = mdb_alloc(csize, UM_SLEEP);
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(cp, csize, addr) == -1) {
929*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read cache at addr %p", addr);
930*7c478bd9Sstevel@tonic-gate 		goto out2;
931*7c478bd9Sstevel@tonic-gate 	}
932*7c478bd9Sstevel@tonic-gate 
933*7c478bd9Sstevel@tonic-gate 	dprintf(("buf total is %d\n", cp->cache_buftotal));
934*7c478bd9Sstevel@tonic-gate 
935*7c478bd9Sstevel@tonic-gate 	if (cp->cache_buftotal == 0) {
936*7c478bd9Sstevel@tonic-gate 		mdb_free(cp, csize);
937*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
938*7c478bd9Sstevel@tonic-gate 	}
939*7c478bd9Sstevel@tonic-gate 
940*7c478bd9Sstevel@tonic-gate 	/*
941*7c478bd9Sstevel@tonic-gate 	 * If they ask for bufctls, but it's a small-slab cache,
942*7c478bd9Sstevel@tonic-gate 	 * there is nothing to report.
943*7c478bd9Sstevel@tonic-gate 	 */
944*7c478bd9Sstevel@tonic-gate 	if ((type & UM_BUFCTL) && !(cp->cache_flags & UMF_HASH)) {
945*7c478bd9Sstevel@tonic-gate 		dprintf(("bufctl requested, not UMF_HASH (flags: %p)\n",
946*7c478bd9Sstevel@tonic-gate 		    cp->cache_flags));
947*7c478bd9Sstevel@tonic-gate 		mdb_free(cp, csize);
948*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
949*7c478bd9Sstevel@tonic-gate 	}
950*7c478bd9Sstevel@tonic-gate 
951*7c478bd9Sstevel@tonic-gate 	/*
952*7c478bd9Sstevel@tonic-gate 	 * Read in the contents of the magazine layer
953*7c478bd9Sstevel@tonic-gate 	 */
954*7c478bd9Sstevel@tonic-gate 	if (umem_read_magazines(cp, addr, ncpus, &maglist, &magcnt,
955*7c478bd9Sstevel@tonic-gate 	    &magmax, UM_SLEEP) == WALK_ERR)
956*7c478bd9Sstevel@tonic-gate 		goto out2;
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate 	/*
959*7c478bd9Sstevel@tonic-gate 	 * We have all of the buffers from the magazines;  if we are walking
960*7c478bd9Sstevel@tonic-gate 	 * allocated buffers, sort them so we can bsearch them later.
961*7c478bd9Sstevel@tonic-gate 	 */
962*7c478bd9Sstevel@tonic-gate 	if (type & UM_ALLOCATED)
963*7c478bd9Sstevel@tonic-gate 		qsort(maglist, magcnt, sizeof (void *), addrcmp);
964*7c478bd9Sstevel@tonic-gate 
965*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = umw = mdb_zalloc(sizeof (umem_walk_t), UM_SLEEP);
966*7c478bd9Sstevel@tonic-gate 
967*7c478bd9Sstevel@tonic-gate 	umw->umw_type = type;
968*7c478bd9Sstevel@tonic-gate 	umw->umw_addr = addr;
969*7c478bd9Sstevel@tonic-gate 	umw->umw_cp = cp;
970*7c478bd9Sstevel@tonic-gate 	umw->umw_csize = csize;
971*7c478bd9Sstevel@tonic-gate 	umw->umw_maglist = maglist;
972*7c478bd9Sstevel@tonic-gate 	umw->umw_max = magmax;
973*7c478bd9Sstevel@tonic-gate 	umw->umw_count = magcnt;
974*7c478bd9Sstevel@tonic-gate 	umw->umw_pos = 0;
975*7c478bd9Sstevel@tonic-gate 
976*7c478bd9Sstevel@tonic-gate 	/*
977*7c478bd9Sstevel@tonic-gate 	 * When walking allocated buffers in a UMF_HASH cache, we walk the
978*7c478bd9Sstevel@tonic-gate 	 * hash table instead of the slab layer.
979*7c478bd9Sstevel@tonic-gate 	 */
980*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & UMF_HASH) && (type & UM_ALLOCATED)) {
981*7c478bd9Sstevel@tonic-gate 		layered = "umem_hash";
982*7c478bd9Sstevel@tonic-gate 
983*7c478bd9Sstevel@tonic-gate 		umw->umw_type |= UM_HASH;
984*7c478bd9Sstevel@tonic-gate 	} else {
985*7c478bd9Sstevel@tonic-gate 		/*
986*7c478bd9Sstevel@tonic-gate 		 * If we are walking freed buffers, we only need the
987*7c478bd9Sstevel@tonic-gate 		 * magazine layer plus the partially allocated slabs.
988*7c478bd9Sstevel@tonic-gate 		 * To walk allocated buffers, we need all of the slabs.
989*7c478bd9Sstevel@tonic-gate 		 */
990*7c478bd9Sstevel@tonic-gate 		if (type & UM_ALLOCATED)
991*7c478bd9Sstevel@tonic-gate 			layered = "umem_slab";
992*7c478bd9Sstevel@tonic-gate 		else
993*7c478bd9Sstevel@tonic-gate 			layered = "umem_slab_partial";
994*7c478bd9Sstevel@tonic-gate 
995*7c478bd9Sstevel@tonic-gate 		/*
996*7c478bd9Sstevel@tonic-gate 		 * for small-slab caches, we read in the entire slab.  For
997*7c478bd9Sstevel@tonic-gate 		 * freed buffers, we can just walk the freelist.  For
998*7c478bd9Sstevel@tonic-gate 		 * allocated buffers, we use a 'valid' array to track
999*7c478bd9Sstevel@tonic-gate 		 * the freed buffers.
1000*7c478bd9Sstevel@tonic-gate 		 */
1001*7c478bd9Sstevel@tonic-gate 		if (!(cp->cache_flags & UMF_HASH)) {
1002*7c478bd9Sstevel@tonic-gate 			chunksize = cp->cache_chunksize;
1003*7c478bd9Sstevel@tonic-gate 			slabsize = cp->cache_slabsize;
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 			umw->umw_ubase = mdb_alloc(slabsize +
1006*7c478bd9Sstevel@tonic-gate 			    sizeof (umem_bufctl_t), UM_SLEEP);
1007*7c478bd9Sstevel@tonic-gate 
1008*7c478bd9Sstevel@tonic-gate 			if (type & UM_ALLOCATED)
1009*7c478bd9Sstevel@tonic-gate 				umw->umw_valid =
1010*7c478bd9Sstevel@tonic-gate 				    mdb_alloc(slabsize / chunksize, UM_SLEEP);
1011*7c478bd9Sstevel@tonic-gate 		}
1012*7c478bd9Sstevel@tonic-gate 	}
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate 	status = WALK_NEXT;
1015*7c478bd9Sstevel@tonic-gate 
1016*7c478bd9Sstevel@tonic-gate 	if (mdb_layered_walk(layered, wsp) == -1) {
1017*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to start layered '%s' walk", layered);
1018*7c478bd9Sstevel@tonic-gate 		status = WALK_ERR;
1019*7c478bd9Sstevel@tonic-gate 	}
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate out1:
1022*7c478bd9Sstevel@tonic-gate 	if (status == WALK_ERR) {
1023*7c478bd9Sstevel@tonic-gate 		if (umw->umw_valid)
1024*7c478bd9Sstevel@tonic-gate 			mdb_free(umw->umw_valid, slabsize / chunksize);
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate 		if (umw->umw_ubase)
1027*7c478bd9Sstevel@tonic-gate 			mdb_free(umw->umw_ubase, slabsize +
1028*7c478bd9Sstevel@tonic-gate 			    sizeof (umem_bufctl_t));
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 		mdb_free(umw->umw_maglist, umw->umw_max * sizeof (uintptr_t));
1031*7c478bd9Sstevel@tonic-gate 		mdb_free(umw, sizeof (umem_walk_t));
1032*7c478bd9Sstevel@tonic-gate 		wsp->walk_data = NULL;
1033*7c478bd9Sstevel@tonic-gate 	}
1034*7c478bd9Sstevel@tonic-gate 
1035*7c478bd9Sstevel@tonic-gate out2:
1036*7c478bd9Sstevel@tonic-gate 	if (status == WALK_ERR)
1037*7c478bd9Sstevel@tonic-gate 		mdb_free(cp, csize);
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate 	return (status);
1040*7c478bd9Sstevel@tonic-gate }
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate int
1043*7c478bd9Sstevel@tonic-gate umem_walk_step(mdb_walk_state_t *wsp)
1044*7c478bd9Sstevel@tonic-gate {
1045*7c478bd9Sstevel@tonic-gate 	umem_walk_t *umw = wsp->walk_data;
1046*7c478bd9Sstevel@tonic-gate 	int type = umw->umw_type;
1047*7c478bd9Sstevel@tonic-gate 	umem_cache_t *cp = umw->umw_cp;
1048*7c478bd9Sstevel@tonic-gate 
1049*7c478bd9Sstevel@tonic-gate 	void **maglist = umw->umw_maglist;
1050*7c478bd9Sstevel@tonic-gate 	int magcnt = umw->umw_count;
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate 	uintptr_t chunksize, slabsize;
1053*7c478bd9Sstevel@tonic-gate 	uintptr_t addr;
1054*7c478bd9Sstevel@tonic-gate 	const umem_slab_t *sp;
1055*7c478bd9Sstevel@tonic-gate 	const umem_bufctl_t *bcp;
1056*7c478bd9Sstevel@tonic-gate 	umem_bufctl_t bc;
1057*7c478bd9Sstevel@tonic-gate 
1058*7c478bd9Sstevel@tonic-gate 	int chunks;
1059*7c478bd9Sstevel@tonic-gate 	char *kbase;
1060*7c478bd9Sstevel@tonic-gate 	void *buf;
1061*7c478bd9Sstevel@tonic-gate 	int i, ret;
1062*7c478bd9Sstevel@tonic-gate 
1063*7c478bd9Sstevel@tonic-gate 	char *valid, *ubase;
1064*7c478bd9Sstevel@tonic-gate 
1065*7c478bd9Sstevel@tonic-gate 	/*
1066*7c478bd9Sstevel@tonic-gate 	 * first, handle the 'umem_hash' layered walk case
1067*7c478bd9Sstevel@tonic-gate 	 */
1068*7c478bd9Sstevel@tonic-gate 	if (type & UM_HASH) {
1069*7c478bd9Sstevel@tonic-gate 		/*
1070*7c478bd9Sstevel@tonic-gate 		 * We have a buffer which has been allocated out of the
1071*7c478bd9Sstevel@tonic-gate 		 * global layer. We need to make sure that it's not
1072*7c478bd9Sstevel@tonic-gate 		 * actually sitting in a magazine before we report it as
1073*7c478bd9Sstevel@tonic-gate 		 * an allocated buffer.
1074*7c478bd9Sstevel@tonic-gate 		 */
1075*7c478bd9Sstevel@tonic-gate 		buf = ((const umem_bufctl_t *)wsp->walk_layer)->bc_addr;
1076*7c478bd9Sstevel@tonic-gate 
1077*7c478bd9Sstevel@tonic-gate 		if (magcnt > 0 &&
1078*7c478bd9Sstevel@tonic-gate 		    bsearch(&buf, maglist, magcnt, sizeof (void *),
1079*7c478bd9Sstevel@tonic-gate 		    addrcmp) != NULL)
1080*7c478bd9Sstevel@tonic-gate 			return (WALK_NEXT);
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate 		if (type & UM_BUFCTL)
1083*7c478bd9Sstevel@tonic-gate 			return (bufctl_walk_callback(cp, wsp, wsp->walk_addr));
1084*7c478bd9Sstevel@tonic-gate 
1085*7c478bd9Sstevel@tonic-gate 		return (umem_walk_callback(wsp, (uintptr_t)buf));
1086*7c478bd9Sstevel@tonic-gate 	}
1087*7c478bd9Sstevel@tonic-gate 
1088*7c478bd9Sstevel@tonic-gate 	ret = WALK_NEXT;
1089*7c478bd9Sstevel@tonic-gate 
1090*7c478bd9Sstevel@tonic-gate 	addr = umw->umw_addr;
1091*7c478bd9Sstevel@tonic-gate 
1092*7c478bd9Sstevel@tonic-gate 	/*
1093*7c478bd9Sstevel@tonic-gate 	 * If we're walking freed buffers, report everything in the
1094*7c478bd9Sstevel@tonic-gate 	 * magazine layer before processing the first slab.
1095*7c478bd9Sstevel@tonic-gate 	 */
1096*7c478bd9Sstevel@tonic-gate 	if ((type & UM_FREE) && magcnt != 0) {
1097*7c478bd9Sstevel@tonic-gate 		umw->umw_count = 0;		/* only do this once */
1098*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < magcnt; i++) {
1099*7c478bd9Sstevel@tonic-gate 			buf = maglist[i];
1100*7c478bd9Sstevel@tonic-gate 
1101*7c478bd9Sstevel@tonic-gate 			if (type & UM_BUFCTL) {
1102*7c478bd9Sstevel@tonic-gate 				uintptr_t out;
1103*7c478bd9Sstevel@tonic-gate 
1104*7c478bd9Sstevel@tonic-gate 				if (cp->cache_flags & UMF_BUFTAG) {
1105*7c478bd9Sstevel@tonic-gate 					umem_buftag_t *btp;
1106*7c478bd9Sstevel@tonic-gate 					umem_buftag_t tag;
1107*7c478bd9Sstevel@tonic-gate 
1108*7c478bd9Sstevel@tonic-gate 					/* LINTED - alignment */
1109*7c478bd9Sstevel@tonic-gate 					btp = UMEM_BUFTAG(cp, buf);
1110*7c478bd9Sstevel@tonic-gate 					if (mdb_vread(&tag, sizeof (tag),
1111*7c478bd9Sstevel@tonic-gate 					    (uintptr_t)btp) == -1) {
1112*7c478bd9Sstevel@tonic-gate 						mdb_warn("reading buftag for "
1113*7c478bd9Sstevel@tonic-gate 						    "%p at %p", buf, btp);
1114*7c478bd9Sstevel@tonic-gate 						continue;
1115*7c478bd9Sstevel@tonic-gate 					}
1116*7c478bd9Sstevel@tonic-gate 					out = (uintptr_t)tag.bt_bufctl;
1117*7c478bd9Sstevel@tonic-gate 				} else {
1118*7c478bd9Sstevel@tonic-gate 					if (umem_hash_lookup(cp, addr, buf,
1119*7c478bd9Sstevel@tonic-gate 					    &out) == -1)
1120*7c478bd9Sstevel@tonic-gate 						continue;
1121*7c478bd9Sstevel@tonic-gate 				}
1122*7c478bd9Sstevel@tonic-gate 				ret = bufctl_walk_callback(cp, wsp, out);
1123*7c478bd9Sstevel@tonic-gate 			} else {
1124*7c478bd9Sstevel@tonic-gate 				ret = umem_walk_callback(wsp, (uintptr_t)buf);
1125*7c478bd9Sstevel@tonic-gate 			}
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate 			if (ret != WALK_NEXT)
1128*7c478bd9Sstevel@tonic-gate 				return (ret);
1129*7c478bd9Sstevel@tonic-gate 		}
1130*7c478bd9Sstevel@tonic-gate 	}
1131*7c478bd9Sstevel@tonic-gate 
1132*7c478bd9Sstevel@tonic-gate 	/*
1133*7c478bd9Sstevel@tonic-gate 	 * Handle the buffers in the current slab
1134*7c478bd9Sstevel@tonic-gate 	 */
1135*7c478bd9Sstevel@tonic-gate 	chunksize = cp->cache_chunksize;
1136*7c478bd9Sstevel@tonic-gate 	slabsize = cp->cache_slabsize;
1137*7c478bd9Sstevel@tonic-gate 
1138*7c478bd9Sstevel@tonic-gate 	sp = wsp->walk_layer;
1139*7c478bd9Sstevel@tonic-gate 	chunks = sp->slab_chunks;
1140*7c478bd9Sstevel@tonic-gate 	kbase = sp->slab_base;
1141*7c478bd9Sstevel@tonic-gate 
1142*7c478bd9Sstevel@tonic-gate 	dprintf(("kbase is %p\n", kbase));
1143*7c478bd9Sstevel@tonic-gate 
1144*7c478bd9Sstevel@tonic-gate 	if (!(cp->cache_flags & UMF_HASH)) {
1145*7c478bd9Sstevel@tonic-gate 		valid = umw->umw_valid;
1146*7c478bd9Sstevel@tonic-gate 		ubase = umw->umw_ubase;
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(ubase, chunks * chunksize,
1149*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)kbase) == -1) {
1150*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read slab contents at %p", kbase);
1151*7c478bd9Sstevel@tonic-gate 			return (WALK_ERR);
1152*7c478bd9Sstevel@tonic-gate 		}
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate 		/*
1155*7c478bd9Sstevel@tonic-gate 		 * Set up the valid map as fully allocated -- we'll punch
1156*7c478bd9Sstevel@tonic-gate 		 * out the freelist.
1157*7c478bd9Sstevel@tonic-gate 		 */
1158*7c478bd9Sstevel@tonic-gate 		if (type & UM_ALLOCATED)
1159*7c478bd9Sstevel@tonic-gate 			(void) memset(valid, 1, chunks);
1160*7c478bd9Sstevel@tonic-gate 	} else {
1161*7c478bd9Sstevel@tonic-gate 		valid = NULL;
1162*7c478bd9Sstevel@tonic-gate 		ubase = NULL;
1163*7c478bd9Sstevel@tonic-gate 	}
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 	/*
1166*7c478bd9Sstevel@tonic-gate 	 * walk the slab's freelist
1167*7c478bd9Sstevel@tonic-gate 	 */
1168*7c478bd9Sstevel@tonic-gate 	bcp = sp->slab_head;
1169*7c478bd9Sstevel@tonic-gate 
1170*7c478bd9Sstevel@tonic-gate 	dprintf(("refcnt is %d; chunks is %d\n", sp->slab_refcnt, chunks));
1171*7c478bd9Sstevel@tonic-gate 
1172*7c478bd9Sstevel@tonic-gate 	/*
1173*7c478bd9Sstevel@tonic-gate 	 * since we could be in the middle of allocating a buffer,
1174*7c478bd9Sstevel@tonic-gate 	 * our refcnt could be one higher than it aught.  So we
1175*7c478bd9Sstevel@tonic-gate 	 * check one further on the freelist than the count allows.
1176*7c478bd9Sstevel@tonic-gate 	 */
1177*7c478bd9Sstevel@tonic-gate 	for (i = sp->slab_refcnt; i <= chunks; i++) {
1178*7c478bd9Sstevel@tonic-gate 		uint_t ndx;
1179*7c478bd9Sstevel@tonic-gate 
1180*7c478bd9Sstevel@tonic-gate 		dprintf(("bcp is %p\n", bcp));
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate 		if (bcp == NULL) {
1183*7c478bd9Sstevel@tonic-gate 			if (i == chunks)
1184*7c478bd9Sstevel@tonic-gate 				break;
1185*7c478bd9Sstevel@tonic-gate 			mdb_warn(
1186*7c478bd9Sstevel@tonic-gate 			    "slab %p in cache %p freelist too short by %d\n",
1187*7c478bd9Sstevel@tonic-gate 			    sp, addr, chunks - i);
1188*7c478bd9Sstevel@tonic-gate 			break;
1189*7c478bd9Sstevel@tonic-gate 		}
1190*7c478bd9Sstevel@tonic-gate 
1191*7c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & UMF_HASH) {
1192*7c478bd9Sstevel@tonic-gate 			if (mdb_vread(&bc, sizeof (bc), (uintptr_t)bcp) == -1) {
1193*7c478bd9Sstevel@tonic-gate 				mdb_warn("failed to read bufctl ptr at %p",
1194*7c478bd9Sstevel@tonic-gate 				    bcp);
1195*7c478bd9Sstevel@tonic-gate 				break;
1196*7c478bd9Sstevel@tonic-gate 			}
1197*7c478bd9Sstevel@tonic-gate 			buf = bc.bc_addr;
1198*7c478bd9Sstevel@tonic-gate 		} else {
1199*7c478bd9Sstevel@tonic-gate 			/*
1200*7c478bd9Sstevel@tonic-gate 			 * Otherwise the buffer is in the slab which
1201*7c478bd9Sstevel@tonic-gate 			 * we've read in;  we just need to determine
1202*7c478bd9Sstevel@tonic-gate 			 * its offset in the slab to find the
1203*7c478bd9Sstevel@tonic-gate 			 * umem_bufctl_t.
1204*7c478bd9Sstevel@tonic-gate 			 */
1205*7c478bd9Sstevel@tonic-gate 			bc = *((umem_bufctl_t *)
1206*7c478bd9Sstevel@tonic-gate 			    ((uintptr_t)bcp - (uintptr_t)kbase +
1207*7c478bd9Sstevel@tonic-gate 			    (uintptr_t)ubase));
1208*7c478bd9Sstevel@tonic-gate 
1209*7c478bd9Sstevel@tonic-gate 			buf = UMEM_BUF(cp, bcp);
1210*7c478bd9Sstevel@tonic-gate 		}
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate 		ndx = ((uintptr_t)buf - (uintptr_t)kbase) / chunksize;
1213*7c478bd9Sstevel@tonic-gate 
1214*7c478bd9Sstevel@tonic-gate 		if (ndx > slabsize / cp->cache_bufsize) {
1215*7c478bd9Sstevel@tonic-gate 			/*
1216*7c478bd9Sstevel@tonic-gate 			 * This is very wrong; we have managed to find
1217*7c478bd9Sstevel@tonic-gate 			 * a buffer in the slab which shouldn't
1218*7c478bd9Sstevel@tonic-gate 			 * actually be here.  Emit a warning, and
1219*7c478bd9Sstevel@tonic-gate 			 * try to continue.
1220*7c478bd9Sstevel@tonic-gate 			 */
1221*7c478bd9Sstevel@tonic-gate 			mdb_warn("buf %p is out of range for "
1222*7c478bd9Sstevel@tonic-gate 			    "slab %p, cache %p\n", buf, sp, addr);
1223*7c478bd9Sstevel@tonic-gate 		} else if (type & UM_ALLOCATED) {
1224*7c478bd9Sstevel@tonic-gate 			/*
1225*7c478bd9Sstevel@tonic-gate 			 * we have found a buffer on the slab's freelist;
1226*7c478bd9Sstevel@tonic-gate 			 * clear its entry
1227*7c478bd9Sstevel@tonic-gate 			 */
1228*7c478bd9Sstevel@tonic-gate 			valid[ndx] = 0;
1229*7c478bd9Sstevel@tonic-gate 		} else {
1230*7c478bd9Sstevel@tonic-gate 			/*
1231*7c478bd9Sstevel@tonic-gate 			 * Report this freed buffer
1232*7c478bd9Sstevel@tonic-gate 			 */
1233*7c478bd9Sstevel@tonic-gate 			if (type & UM_BUFCTL) {
1234*7c478bd9Sstevel@tonic-gate 				ret = bufctl_walk_callback(cp, wsp,
1235*7c478bd9Sstevel@tonic-gate 				    (uintptr_t)bcp);
1236*7c478bd9Sstevel@tonic-gate 			} else {
1237*7c478bd9Sstevel@tonic-gate 				ret = umem_walk_callback(wsp, (uintptr_t)buf);
1238*7c478bd9Sstevel@tonic-gate 			}
1239*7c478bd9Sstevel@tonic-gate 			if (ret != WALK_NEXT)
1240*7c478bd9Sstevel@tonic-gate 				return (ret);
1241*7c478bd9Sstevel@tonic-gate 		}
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate 		bcp = bc.bc_next;
1244*7c478bd9Sstevel@tonic-gate 	}
1245*7c478bd9Sstevel@tonic-gate 
1246*7c478bd9Sstevel@tonic-gate 	if (bcp != NULL) {
1247*7c478bd9Sstevel@tonic-gate 		dprintf(("slab %p in cache %p freelist too long (%p)\n",
1248*7c478bd9Sstevel@tonic-gate 		    sp, addr, bcp));
1249*7c478bd9Sstevel@tonic-gate 	}
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 	/*
1252*7c478bd9Sstevel@tonic-gate 	 * If we are walking freed buffers, the loop above handled reporting
1253*7c478bd9Sstevel@tonic-gate 	 * them.
1254*7c478bd9Sstevel@tonic-gate 	 */
1255*7c478bd9Sstevel@tonic-gate 	if (type & UM_FREE)
1256*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1257*7c478bd9Sstevel@tonic-gate 
1258*7c478bd9Sstevel@tonic-gate 	if (type & UM_BUFCTL) {
1259*7c478bd9Sstevel@tonic-gate 		mdb_warn("impossible situation: small-slab UM_BUFCTL walk for "
1260*7c478bd9Sstevel@tonic-gate 		    "cache %p\n", addr);
1261*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1262*7c478bd9Sstevel@tonic-gate 	}
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 	/*
1265*7c478bd9Sstevel@tonic-gate 	 * Report allocated buffers, skipping buffers in the magazine layer.
1266*7c478bd9Sstevel@tonic-gate 	 * We only get this far for small-slab caches.
1267*7c478bd9Sstevel@tonic-gate 	 */
1268*7c478bd9Sstevel@tonic-gate 	for (i = 0; ret == WALK_NEXT && i < chunks; i++) {
1269*7c478bd9Sstevel@tonic-gate 		buf = (char *)kbase + i * chunksize;
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate 		if (!valid[i])
1272*7c478bd9Sstevel@tonic-gate 			continue;		/* on slab freelist */
1273*7c478bd9Sstevel@tonic-gate 
1274*7c478bd9Sstevel@tonic-gate 		if (magcnt > 0 &&
1275*7c478bd9Sstevel@tonic-gate 		    bsearch(&buf, maglist, magcnt, sizeof (void *),
1276*7c478bd9Sstevel@tonic-gate 		    addrcmp) != NULL)
1277*7c478bd9Sstevel@tonic-gate 			continue;		/* in magazine layer */
1278*7c478bd9Sstevel@tonic-gate 
1279*7c478bd9Sstevel@tonic-gate 		ret = umem_walk_callback(wsp, (uintptr_t)buf);
1280*7c478bd9Sstevel@tonic-gate 	}
1281*7c478bd9Sstevel@tonic-gate 	return (ret);
1282*7c478bd9Sstevel@tonic-gate }
1283*7c478bd9Sstevel@tonic-gate 
1284*7c478bd9Sstevel@tonic-gate void
1285*7c478bd9Sstevel@tonic-gate umem_walk_fini(mdb_walk_state_t *wsp)
1286*7c478bd9Sstevel@tonic-gate {
1287*7c478bd9Sstevel@tonic-gate 	umem_walk_t *umw = wsp->walk_data;
1288*7c478bd9Sstevel@tonic-gate 	uintptr_t chunksize;
1289*7c478bd9Sstevel@tonic-gate 	uintptr_t slabsize;
1290*7c478bd9Sstevel@tonic-gate 
1291*7c478bd9Sstevel@tonic-gate 	if (umw == NULL)
1292*7c478bd9Sstevel@tonic-gate 		return;
1293*7c478bd9Sstevel@tonic-gate 
1294*7c478bd9Sstevel@tonic-gate 	if (umw->umw_maglist != NULL)
1295*7c478bd9Sstevel@tonic-gate 		mdb_free(umw->umw_maglist, umw->umw_max * sizeof (void *));
1296*7c478bd9Sstevel@tonic-gate 
1297*7c478bd9Sstevel@tonic-gate 	chunksize = umw->umw_cp->cache_chunksize;
1298*7c478bd9Sstevel@tonic-gate 	slabsize = umw->umw_cp->cache_slabsize;
1299*7c478bd9Sstevel@tonic-gate 
1300*7c478bd9Sstevel@tonic-gate 	if (umw->umw_valid != NULL)
1301*7c478bd9Sstevel@tonic-gate 		mdb_free(umw->umw_valid, slabsize / chunksize);
1302*7c478bd9Sstevel@tonic-gate 	if (umw->umw_ubase != NULL)
1303*7c478bd9Sstevel@tonic-gate 		mdb_free(umw->umw_ubase, slabsize + sizeof (umem_bufctl_t));
1304*7c478bd9Sstevel@tonic-gate 
1305*7c478bd9Sstevel@tonic-gate 	mdb_free(umw->umw_cp, umw->umw_csize);
1306*7c478bd9Sstevel@tonic-gate 	mdb_free(umw, sizeof (umem_walk_t));
1307*7c478bd9Sstevel@tonic-gate }
1308*7c478bd9Sstevel@tonic-gate 
1309*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1310*7c478bd9Sstevel@tonic-gate static int
1311*7c478bd9Sstevel@tonic-gate umem_walk_all(uintptr_t addr, const umem_cache_t *c, mdb_walk_state_t *wsp)
1312*7c478bd9Sstevel@tonic-gate {
1313*7c478bd9Sstevel@tonic-gate 	/*
1314*7c478bd9Sstevel@tonic-gate 	 * Buffers allocated from NOTOUCH caches can also show up as freed
1315*7c478bd9Sstevel@tonic-gate 	 * memory in other caches.  This can be a little confusing, so we
1316*7c478bd9Sstevel@tonic-gate 	 * don't walk NOTOUCH caches when walking all caches (thereby assuring
1317*7c478bd9Sstevel@tonic-gate 	 * that "::walk umem" and "::walk freemem" yield disjoint output).
1318*7c478bd9Sstevel@tonic-gate 	 */
1319*7c478bd9Sstevel@tonic-gate 	if (c->cache_cflags & UMC_NOTOUCH)
1320*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1321*7c478bd9Sstevel@tonic-gate 
1322*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk(wsp->walk_data, wsp->walk_callback,
1323*7c478bd9Sstevel@tonic-gate 	    wsp->walk_cbdata, addr) == -1)
1324*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1325*7c478bd9Sstevel@tonic-gate 
1326*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1327*7c478bd9Sstevel@tonic-gate }
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate #define	UMEM_WALK_ALL(name, wsp) { \
1330*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = (name); \
1331*7c478bd9Sstevel@tonic-gate 	if (mdb_walk("umem_cache", (mdb_walk_cb_t)umem_walk_all, wsp) == -1) \
1332*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR); \
1333*7c478bd9Sstevel@tonic-gate 	return (WALK_DONE); \
1334*7c478bd9Sstevel@tonic-gate }
1335*7c478bd9Sstevel@tonic-gate 
1336*7c478bd9Sstevel@tonic-gate int
1337*7c478bd9Sstevel@tonic-gate umem_walk_init(mdb_walk_state_t *wsp)
1338*7c478bd9Sstevel@tonic-gate {
1339*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_arg != NULL)
1340*7c478bd9Sstevel@tonic-gate 		wsp->walk_addr = (uintptr_t)wsp->walk_arg;
1341*7c478bd9Sstevel@tonic-gate 
1342*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
1343*7c478bd9Sstevel@tonic-gate 		UMEM_WALK_ALL("umem", wsp);
1344*7c478bd9Sstevel@tonic-gate 	return (umem_walk_init_common(wsp, UM_ALLOCATED));
1345*7c478bd9Sstevel@tonic-gate }
1346*7c478bd9Sstevel@tonic-gate 
1347*7c478bd9Sstevel@tonic-gate int
1348*7c478bd9Sstevel@tonic-gate bufctl_walk_init(mdb_walk_state_t *wsp)
1349*7c478bd9Sstevel@tonic-gate {
1350*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
1351*7c478bd9Sstevel@tonic-gate 		UMEM_WALK_ALL("bufctl", wsp);
1352*7c478bd9Sstevel@tonic-gate 	return (umem_walk_init_common(wsp, UM_ALLOCATED | UM_BUFCTL));
1353*7c478bd9Sstevel@tonic-gate }
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate int
1356*7c478bd9Sstevel@tonic-gate freemem_walk_init(mdb_walk_state_t *wsp)
1357*7c478bd9Sstevel@tonic-gate {
1358*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
1359*7c478bd9Sstevel@tonic-gate 		UMEM_WALK_ALL("freemem", wsp);
1360*7c478bd9Sstevel@tonic-gate 	return (umem_walk_init_common(wsp, UM_FREE));
1361*7c478bd9Sstevel@tonic-gate }
1362*7c478bd9Sstevel@tonic-gate 
1363*7c478bd9Sstevel@tonic-gate int
1364*7c478bd9Sstevel@tonic-gate freectl_walk_init(mdb_walk_state_t *wsp)
1365*7c478bd9Sstevel@tonic-gate {
1366*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL)
1367*7c478bd9Sstevel@tonic-gate 		UMEM_WALK_ALL("freectl", wsp);
1368*7c478bd9Sstevel@tonic-gate 	return (umem_walk_init_common(wsp, UM_FREE | UM_BUFCTL));
1369*7c478bd9Sstevel@tonic-gate }
1370*7c478bd9Sstevel@tonic-gate 
1371*7c478bd9Sstevel@tonic-gate typedef struct bufctl_history_walk {
1372*7c478bd9Sstevel@tonic-gate 	void		*bhw_next;
1373*7c478bd9Sstevel@tonic-gate 	umem_cache_t	*bhw_cache;
1374*7c478bd9Sstevel@tonic-gate 	umem_slab_t	*bhw_slab;
1375*7c478bd9Sstevel@tonic-gate 	hrtime_t	bhw_timestamp;
1376*7c478bd9Sstevel@tonic-gate } bufctl_history_walk_t;
1377*7c478bd9Sstevel@tonic-gate 
1378*7c478bd9Sstevel@tonic-gate int
1379*7c478bd9Sstevel@tonic-gate bufctl_history_walk_init(mdb_walk_state_t *wsp)
1380*7c478bd9Sstevel@tonic-gate {
1381*7c478bd9Sstevel@tonic-gate 	bufctl_history_walk_t *bhw;
1382*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t bc;
1383*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t bcn;
1384*7c478bd9Sstevel@tonic-gate 
1385*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
1386*7c478bd9Sstevel@tonic-gate 		mdb_warn("bufctl_history walk doesn't support global walks\n");
1387*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1388*7c478bd9Sstevel@tonic-gate 	}
1389*7c478bd9Sstevel@tonic-gate 
1390*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&bc, sizeof (bc), wsp->walk_addr) == -1) {
1391*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to read bufctl at %p", wsp->walk_addr);
1392*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1393*7c478bd9Sstevel@tonic-gate 	}
1394*7c478bd9Sstevel@tonic-gate 
1395*7c478bd9Sstevel@tonic-gate 	bhw = mdb_zalloc(sizeof (*bhw), UM_SLEEP);
1396*7c478bd9Sstevel@tonic-gate 	bhw->bhw_timestamp = 0;
1397*7c478bd9Sstevel@tonic-gate 	bhw->bhw_cache = bc.bc_cache;
1398*7c478bd9Sstevel@tonic-gate 	bhw->bhw_slab = bc.bc_slab;
1399*7c478bd9Sstevel@tonic-gate 
1400*7c478bd9Sstevel@tonic-gate 	/*
1401*7c478bd9Sstevel@tonic-gate 	 * sometimes the first log entry matches the base bufctl;  in that
1402*7c478bd9Sstevel@tonic-gate 	 * case, skip the base bufctl.
1403*7c478bd9Sstevel@tonic-gate 	 */
1404*7c478bd9Sstevel@tonic-gate 	if (bc.bc_lastlog != NULL &&
1405*7c478bd9Sstevel@tonic-gate 	    mdb_vread(&bcn, sizeof (bcn), (uintptr_t)bc.bc_lastlog) != -1 &&
1406*7c478bd9Sstevel@tonic-gate 	    bc.bc_addr == bcn.bc_addr &&
1407*7c478bd9Sstevel@tonic-gate 	    bc.bc_cache == bcn.bc_cache &&
1408*7c478bd9Sstevel@tonic-gate 	    bc.bc_slab == bcn.bc_slab &&
1409*7c478bd9Sstevel@tonic-gate 	    bc.bc_timestamp == bcn.bc_timestamp &&
1410*7c478bd9Sstevel@tonic-gate 	    bc.bc_thread == bcn.bc_thread)
1411*7c478bd9Sstevel@tonic-gate 		bhw->bhw_next = bc.bc_lastlog;
1412*7c478bd9Sstevel@tonic-gate 	else
1413*7c478bd9Sstevel@tonic-gate 		bhw->bhw_next = (void *)wsp->walk_addr;
1414*7c478bd9Sstevel@tonic-gate 
1415*7c478bd9Sstevel@tonic-gate 	wsp->walk_addr = (uintptr_t)bc.bc_addr;
1416*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = bhw;
1417*7c478bd9Sstevel@tonic-gate 
1418*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1419*7c478bd9Sstevel@tonic-gate }
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate int
1422*7c478bd9Sstevel@tonic-gate bufctl_history_walk_step(mdb_walk_state_t *wsp)
1423*7c478bd9Sstevel@tonic-gate {
1424*7c478bd9Sstevel@tonic-gate 	bufctl_history_walk_t *bhw = wsp->walk_data;
1425*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = (uintptr_t)bhw->bhw_next;
1426*7c478bd9Sstevel@tonic-gate 	uintptr_t baseaddr = wsp->walk_addr;
1427*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t *b;
1428*7c478bd9Sstevel@tonic-gate 	UMEM_LOCAL_BUFCTL_AUDIT(&b);
1429*7c478bd9Sstevel@tonic-gate 
1430*7c478bd9Sstevel@tonic-gate 	if (addr == NULL)
1431*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1432*7c478bd9Sstevel@tonic-gate 
1433*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(b, UMEM_BUFCTL_AUDIT_SIZE, addr) == -1) {
1434*7c478bd9Sstevel@tonic-gate 		mdb_warn("unable to read bufctl at %p", bhw->bhw_next);
1435*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1436*7c478bd9Sstevel@tonic-gate 	}
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 	/*
1439*7c478bd9Sstevel@tonic-gate 	 * The bufctl is only valid if the address, cache, and slab are
1440*7c478bd9Sstevel@tonic-gate 	 * correct.  We also check that the timestamp is decreasing, to
1441*7c478bd9Sstevel@tonic-gate 	 * prevent infinite loops.
1442*7c478bd9Sstevel@tonic-gate 	 */
1443*7c478bd9Sstevel@tonic-gate 	if ((uintptr_t)b->bc_addr != baseaddr ||
1444*7c478bd9Sstevel@tonic-gate 	    b->bc_cache != bhw->bhw_cache ||
1445*7c478bd9Sstevel@tonic-gate 	    b->bc_slab != bhw->bhw_slab ||
1446*7c478bd9Sstevel@tonic-gate 	    (bhw->bhw_timestamp != 0 && b->bc_timestamp >= bhw->bhw_timestamp))
1447*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1448*7c478bd9Sstevel@tonic-gate 
1449*7c478bd9Sstevel@tonic-gate 	bhw->bhw_next = b->bc_lastlog;
1450*7c478bd9Sstevel@tonic-gate 	bhw->bhw_timestamp = b->bc_timestamp;
1451*7c478bd9Sstevel@tonic-gate 
1452*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(addr, b, wsp->walk_cbdata));
1453*7c478bd9Sstevel@tonic-gate }
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate void
1456*7c478bd9Sstevel@tonic-gate bufctl_history_walk_fini(mdb_walk_state_t *wsp)
1457*7c478bd9Sstevel@tonic-gate {
1458*7c478bd9Sstevel@tonic-gate 	bufctl_history_walk_t *bhw = wsp->walk_data;
1459*7c478bd9Sstevel@tonic-gate 
1460*7c478bd9Sstevel@tonic-gate 	mdb_free(bhw, sizeof (*bhw));
1461*7c478bd9Sstevel@tonic-gate }
1462*7c478bd9Sstevel@tonic-gate 
1463*7c478bd9Sstevel@tonic-gate typedef struct umem_log_walk {
1464*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t *ulw_base;
1465*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t **ulw_sorted;
1466*7c478bd9Sstevel@tonic-gate 	umem_log_header_t ulw_lh;
1467*7c478bd9Sstevel@tonic-gate 	size_t ulw_size;
1468*7c478bd9Sstevel@tonic-gate 	size_t ulw_maxndx;
1469*7c478bd9Sstevel@tonic-gate 	size_t ulw_ndx;
1470*7c478bd9Sstevel@tonic-gate } umem_log_walk_t;
1471*7c478bd9Sstevel@tonic-gate 
1472*7c478bd9Sstevel@tonic-gate int
1473*7c478bd9Sstevel@tonic-gate umem_log_walk_init(mdb_walk_state_t *wsp)
1474*7c478bd9Sstevel@tonic-gate {
1475*7c478bd9Sstevel@tonic-gate 	uintptr_t lp = wsp->walk_addr;
1476*7c478bd9Sstevel@tonic-gate 	umem_log_walk_t *ulw;
1477*7c478bd9Sstevel@tonic-gate 	umem_log_header_t *lhp;
1478*7c478bd9Sstevel@tonic-gate 	int maxndx, i, j, k;
1479*7c478bd9Sstevel@tonic-gate 
1480*7c478bd9Sstevel@tonic-gate 	/*
1481*7c478bd9Sstevel@tonic-gate 	 * By default (global walk), walk the umem_transaction_log.  Otherwise
1482*7c478bd9Sstevel@tonic-gate 	 * read the log whose umem_log_header_t is stored at walk_addr.
1483*7c478bd9Sstevel@tonic-gate 	 */
1484*7c478bd9Sstevel@tonic-gate 	if (lp == NULL && umem_readvar(&lp, "umem_transaction_log") == -1) {
1485*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'umem_transaction_log'");
1486*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1487*7c478bd9Sstevel@tonic-gate 	}
1488*7c478bd9Sstevel@tonic-gate 
1489*7c478bd9Sstevel@tonic-gate 	if (lp == NULL) {
1490*7c478bd9Sstevel@tonic-gate 		mdb_warn("log is disabled\n");
1491*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1492*7c478bd9Sstevel@tonic-gate 	}
1493*7c478bd9Sstevel@tonic-gate 
1494*7c478bd9Sstevel@tonic-gate 	ulw = mdb_zalloc(sizeof (umem_log_walk_t), UM_SLEEP);
1495*7c478bd9Sstevel@tonic-gate 	lhp = &ulw->ulw_lh;
1496*7c478bd9Sstevel@tonic-gate 
1497*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(lhp, sizeof (umem_log_header_t), lp) == -1) {
1498*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read log header at %p", lp);
1499*7c478bd9Sstevel@tonic-gate 		mdb_free(ulw, sizeof (umem_log_walk_t));
1500*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1501*7c478bd9Sstevel@tonic-gate 	}
1502*7c478bd9Sstevel@tonic-gate 
1503*7c478bd9Sstevel@tonic-gate 	ulw->ulw_size = lhp->lh_chunksize * lhp->lh_nchunks;
1504*7c478bd9Sstevel@tonic-gate 	ulw->ulw_base = mdb_alloc(ulw->ulw_size, UM_SLEEP);
1505*7c478bd9Sstevel@tonic-gate 	maxndx = lhp->lh_chunksize / UMEM_BUFCTL_AUDIT_SIZE - 1;
1506*7c478bd9Sstevel@tonic-gate 
1507*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(ulw->ulw_base, ulw->ulw_size,
1508*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)lhp->lh_base) == -1) {
1509*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read log at base %p", lhp->lh_base);
1510*7c478bd9Sstevel@tonic-gate 		mdb_free(ulw->ulw_base, ulw->ulw_size);
1511*7c478bd9Sstevel@tonic-gate 		mdb_free(ulw, sizeof (umem_log_walk_t));
1512*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1513*7c478bd9Sstevel@tonic-gate 	}
1514*7c478bd9Sstevel@tonic-gate 
1515*7c478bd9Sstevel@tonic-gate 	ulw->ulw_sorted = mdb_alloc(maxndx * lhp->lh_nchunks *
1516*7c478bd9Sstevel@tonic-gate 	    sizeof (umem_bufctl_audit_t *), UM_SLEEP);
1517*7c478bd9Sstevel@tonic-gate 
1518*7c478bd9Sstevel@tonic-gate 	for (i = 0, k = 0; i < lhp->lh_nchunks; i++) {
1519*7c478bd9Sstevel@tonic-gate 		caddr_t chunk = (caddr_t)
1520*7c478bd9Sstevel@tonic-gate 		    ((uintptr_t)ulw->ulw_base + i * lhp->lh_chunksize);
1521*7c478bd9Sstevel@tonic-gate 
1522*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < maxndx; j++) {
1523*7c478bd9Sstevel@tonic-gate 			/* LINTED align */
1524*7c478bd9Sstevel@tonic-gate 			ulw->ulw_sorted[k++] = (umem_bufctl_audit_t *)chunk;
1525*7c478bd9Sstevel@tonic-gate 			chunk += UMEM_BUFCTL_AUDIT_SIZE;
1526*7c478bd9Sstevel@tonic-gate 		}
1527*7c478bd9Sstevel@tonic-gate 	}
1528*7c478bd9Sstevel@tonic-gate 
1529*7c478bd9Sstevel@tonic-gate 	qsort(ulw->ulw_sorted, k, sizeof (umem_bufctl_audit_t *),
1530*7c478bd9Sstevel@tonic-gate 	    (int(*)(const void *, const void *))bufctlcmp);
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate 	ulw->ulw_maxndx = k;
1533*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = ulw;
1534*7c478bd9Sstevel@tonic-gate 
1535*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1536*7c478bd9Sstevel@tonic-gate }
1537*7c478bd9Sstevel@tonic-gate 
1538*7c478bd9Sstevel@tonic-gate int
1539*7c478bd9Sstevel@tonic-gate umem_log_walk_step(mdb_walk_state_t *wsp)
1540*7c478bd9Sstevel@tonic-gate {
1541*7c478bd9Sstevel@tonic-gate 	umem_log_walk_t *ulw = wsp->walk_data;
1542*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t *bcp;
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate 	if (ulw->ulw_ndx == ulw->ulw_maxndx)
1545*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1546*7c478bd9Sstevel@tonic-gate 
1547*7c478bd9Sstevel@tonic-gate 	bcp = ulw->ulw_sorted[ulw->ulw_ndx++];
1548*7c478bd9Sstevel@tonic-gate 
1549*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback((uintptr_t)bcp - (uintptr_t)ulw->ulw_base +
1550*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)ulw->ulw_lh.lh_base, bcp, wsp->walk_cbdata));
1551*7c478bd9Sstevel@tonic-gate }
1552*7c478bd9Sstevel@tonic-gate 
1553*7c478bd9Sstevel@tonic-gate void
1554*7c478bd9Sstevel@tonic-gate umem_log_walk_fini(mdb_walk_state_t *wsp)
1555*7c478bd9Sstevel@tonic-gate {
1556*7c478bd9Sstevel@tonic-gate 	umem_log_walk_t *ulw = wsp->walk_data;
1557*7c478bd9Sstevel@tonic-gate 
1558*7c478bd9Sstevel@tonic-gate 	mdb_free(ulw->ulw_base, ulw->ulw_size);
1559*7c478bd9Sstevel@tonic-gate 	mdb_free(ulw->ulw_sorted, ulw->ulw_maxndx *
1560*7c478bd9Sstevel@tonic-gate 	    sizeof (umem_bufctl_audit_t *));
1561*7c478bd9Sstevel@tonic-gate 	mdb_free(ulw, sizeof (umem_log_walk_t));
1562*7c478bd9Sstevel@tonic-gate }
1563*7c478bd9Sstevel@tonic-gate 
1564*7c478bd9Sstevel@tonic-gate typedef struct allocdby_bufctl {
1565*7c478bd9Sstevel@tonic-gate 	uintptr_t abb_addr;
1566*7c478bd9Sstevel@tonic-gate 	hrtime_t abb_ts;
1567*7c478bd9Sstevel@tonic-gate } allocdby_bufctl_t;
1568*7c478bd9Sstevel@tonic-gate 
1569*7c478bd9Sstevel@tonic-gate typedef struct allocdby_walk {
1570*7c478bd9Sstevel@tonic-gate 	const char *abw_walk;
1571*7c478bd9Sstevel@tonic-gate 	uintptr_t abw_thread;
1572*7c478bd9Sstevel@tonic-gate 	size_t abw_nbufs;
1573*7c478bd9Sstevel@tonic-gate 	size_t abw_size;
1574*7c478bd9Sstevel@tonic-gate 	allocdby_bufctl_t *abw_buf;
1575*7c478bd9Sstevel@tonic-gate 	size_t abw_ndx;
1576*7c478bd9Sstevel@tonic-gate } allocdby_walk_t;
1577*7c478bd9Sstevel@tonic-gate 
1578*7c478bd9Sstevel@tonic-gate int
1579*7c478bd9Sstevel@tonic-gate allocdby_walk_bufctl(uintptr_t addr, const umem_bufctl_audit_t *bcp,
1580*7c478bd9Sstevel@tonic-gate     allocdby_walk_t *abw)
1581*7c478bd9Sstevel@tonic-gate {
1582*7c478bd9Sstevel@tonic-gate 	if ((uintptr_t)bcp->bc_thread != abw->abw_thread)
1583*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1584*7c478bd9Sstevel@tonic-gate 
1585*7c478bd9Sstevel@tonic-gate 	if (abw->abw_nbufs == abw->abw_size) {
1586*7c478bd9Sstevel@tonic-gate 		allocdby_bufctl_t *buf;
1587*7c478bd9Sstevel@tonic-gate 		size_t oldsize = sizeof (allocdby_bufctl_t) * abw->abw_size;
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate 		buf = mdb_zalloc(oldsize << 1, UM_SLEEP);
1590*7c478bd9Sstevel@tonic-gate 
1591*7c478bd9Sstevel@tonic-gate 		bcopy(abw->abw_buf, buf, oldsize);
1592*7c478bd9Sstevel@tonic-gate 		mdb_free(abw->abw_buf, oldsize);
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 		abw->abw_size <<= 1;
1595*7c478bd9Sstevel@tonic-gate 		abw->abw_buf = buf;
1596*7c478bd9Sstevel@tonic-gate 	}
1597*7c478bd9Sstevel@tonic-gate 
1598*7c478bd9Sstevel@tonic-gate 	abw->abw_buf[abw->abw_nbufs].abb_addr = addr;
1599*7c478bd9Sstevel@tonic-gate 	abw->abw_buf[abw->abw_nbufs].abb_ts = bcp->bc_timestamp;
1600*7c478bd9Sstevel@tonic-gate 	abw->abw_nbufs++;
1601*7c478bd9Sstevel@tonic-gate 
1602*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1603*7c478bd9Sstevel@tonic-gate }
1604*7c478bd9Sstevel@tonic-gate 
1605*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1606*7c478bd9Sstevel@tonic-gate int
1607*7c478bd9Sstevel@tonic-gate allocdby_walk_cache(uintptr_t addr, const umem_cache_t *c, allocdby_walk_t *abw)
1608*7c478bd9Sstevel@tonic-gate {
1609*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk(abw->abw_walk, (mdb_walk_cb_t)allocdby_walk_bufctl,
1610*7c478bd9Sstevel@tonic-gate 	    abw, addr) == -1) {
1611*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't walk bufctl for cache %p", addr);
1612*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1613*7c478bd9Sstevel@tonic-gate 	}
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1616*7c478bd9Sstevel@tonic-gate }
1617*7c478bd9Sstevel@tonic-gate 
1618*7c478bd9Sstevel@tonic-gate static int
1619*7c478bd9Sstevel@tonic-gate allocdby_cmp(const allocdby_bufctl_t *lhs, const allocdby_bufctl_t *rhs)
1620*7c478bd9Sstevel@tonic-gate {
1621*7c478bd9Sstevel@tonic-gate 	if (lhs->abb_ts < rhs->abb_ts)
1622*7c478bd9Sstevel@tonic-gate 		return (1);
1623*7c478bd9Sstevel@tonic-gate 	if (lhs->abb_ts > rhs->abb_ts)
1624*7c478bd9Sstevel@tonic-gate 		return (-1);
1625*7c478bd9Sstevel@tonic-gate 	return (0);
1626*7c478bd9Sstevel@tonic-gate }
1627*7c478bd9Sstevel@tonic-gate 
1628*7c478bd9Sstevel@tonic-gate static int
1629*7c478bd9Sstevel@tonic-gate allocdby_walk_init_common(mdb_walk_state_t *wsp, const char *walk)
1630*7c478bd9Sstevel@tonic-gate {
1631*7c478bd9Sstevel@tonic-gate 	allocdby_walk_t *abw;
1632*7c478bd9Sstevel@tonic-gate 
1633*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
1634*7c478bd9Sstevel@tonic-gate 		mdb_warn("allocdby walk doesn't support global walks\n");
1635*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1636*7c478bd9Sstevel@tonic-gate 	}
1637*7c478bd9Sstevel@tonic-gate 
1638*7c478bd9Sstevel@tonic-gate 	abw = mdb_zalloc(sizeof (allocdby_walk_t), UM_SLEEP);
1639*7c478bd9Sstevel@tonic-gate 
1640*7c478bd9Sstevel@tonic-gate 	abw->abw_thread = wsp->walk_addr;
1641*7c478bd9Sstevel@tonic-gate 	abw->abw_walk = walk;
1642*7c478bd9Sstevel@tonic-gate 	abw->abw_size = 128;	/* something reasonable */
1643*7c478bd9Sstevel@tonic-gate 	abw->abw_buf =
1644*7c478bd9Sstevel@tonic-gate 	    mdb_zalloc(abw->abw_size * sizeof (allocdby_bufctl_t), UM_SLEEP);
1645*7c478bd9Sstevel@tonic-gate 
1646*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = abw;
1647*7c478bd9Sstevel@tonic-gate 
1648*7c478bd9Sstevel@tonic-gate 	if (mdb_walk("umem_cache",
1649*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)allocdby_walk_cache, abw) == -1) {
1650*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't walk umem_cache");
1651*7c478bd9Sstevel@tonic-gate 		allocdby_walk_fini(wsp);
1652*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
1653*7c478bd9Sstevel@tonic-gate 	}
1654*7c478bd9Sstevel@tonic-gate 
1655*7c478bd9Sstevel@tonic-gate 	qsort(abw->abw_buf, abw->abw_nbufs, sizeof (allocdby_bufctl_t),
1656*7c478bd9Sstevel@tonic-gate 	    (int(*)(const void *, const void *))allocdby_cmp);
1657*7c478bd9Sstevel@tonic-gate 
1658*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1659*7c478bd9Sstevel@tonic-gate }
1660*7c478bd9Sstevel@tonic-gate 
1661*7c478bd9Sstevel@tonic-gate int
1662*7c478bd9Sstevel@tonic-gate allocdby_walk_init(mdb_walk_state_t *wsp)
1663*7c478bd9Sstevel@tonic-gate {
1664*7c478bd9Sstevel@tonic-gate 	return (allocdby_walk_init_common(wsp, "bufctl"));
1665*7c478bd9Sstevel@tonic-gate }
1666*7c478bd9Sstevel@tonic-gate 
1667*7c478bd9Sstevel@tonic-gate int
1668*7c478bd9Sstevel@tonic-gate freedby_walk_init(mdb_walk_state_t *wsp)
1669*7c478bd9Sstevel@tonic-gate {
1670*7c478bd9Sstevel@tonic-gate 	return (allocdby_walk_init_common(wsp, "freectl"));
1671*7c478bd9Sstevel@tonic-gate }
1672*7c478bd9Sstevel@tonic-gate 
1673*7c478bd9Sstevel@tonic-gate int
1674*7c478bd9Sstevel@tonic-gate allocdby_walk_step(mdb_walk_state_t *wsp)
1675*7c478bd9Sstevel@tonic-gate {
1676*7c478bd9Sstevel@tonic-gate 	allocdby_walk_t *abw = wsp->walk_data;
1677*7c478bd9Sstevel@tonic-gate 	uintptr_t addr;
1678*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t *bcp;
1679*7c478bd9Sstevel@tonic-gate 	UMEM_LOCAL_BUFCTL_AUDIT(&bcp);
1680*7c478bd9Sstevel@tonic-gate 
1681*7c478bd9Sstevel@tonic-gate 	if (abw->abw_ndx == abw->abw_nbufs)
1682*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1683*7c478bd9Sstevel@tonic-gate 
1684*7c478bd9Sstevel@tonic-gate 	addr = abw->abw_buf[abw->abw_ndx++].abb_addr;
1685*7c478bd9Sstevel@tonic-gate 
1686*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(bcp, UMEM_BUFCTL_AUDIT_SIZE, addr) == -1) {
1687*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read bufctl at %p", addr);
1688*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1689*7c478bd9Sstevel@tonic-gate 	}
1690*7c478bd9Sstevel@tonic-gate 
1691*7c478bd9Sstevel@tonic-gate 	return (wsp->walk_callback(addr, bcp, wsp->walk_cbdata));
1692*7c478bd9Sstevel@tonic-gate }
1693*7c478bd9Sstevel@tonic-gate 
1694*7c478bd9Sstevel@tonic-gate void
1695*7c478bd9Sstevel@tonic-gate allocdby_walk_fini(mdb_walk_state_t *wsp)
1696*7c478bd9Sstevel@tonic-gate {
1697*7c478bd9Sstevel@tonic-gate 	allocdby_walk_t *abw = wsp->walk_data;
1698*7c478bd9Sstevel@tonic-gate 
1699*7c478bd9Sstevel@tonic-gate 	mdb_free(abw->abw_buf, sizeof (allocdby_bufctl_t) * abw->abw_size);
1700*7c478bd9Sstevel@tonic-gate 	mdb_free(abw, sizeof (allocdby_walk_t));
1701*7c478bd9Sstevel@tonic-gate }
1702*7c478bd9Sstevel@tonic-gate 
1703*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1704*7c478bd9Sstevel@tonic-gate int
1705*7c478bd9Sstevel@tonic-gate allocdby_walk(uintptr_t addr, const umem_bufctl_audit_t *bcp, void *ignored)
1706*7c478bd9Sstevel@tonic-gate {
1707*7c478bd9Sstevel@tonic-gate 	char c[MDB_SYM_NAMLEN];
1708*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
1709*7c478bd9Sstevel@tonic-gate 	int i;
1710*7c478bd9Sstevel@tonic-gate 
1711*7c478bd9Sstevel@tonic-gate 	mdb_printf("%0?p %12llx ", addr, bcp->bc_timestamp);
1712*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < bcp->bc_depth; i++) {
1713*7c478bd9Sstevel@tonic-gate 		if (mdb_lookup_by_addr(bcp->bc_stack[i],
1714*7c478bd9Sstevel@tonic-gate 		    MDB_SYM_FUZZY, c, sizeof (c), &sym) == -1)
1715*7c478bd9Sstevel@tonic-gate 			continue;
1716*7c478bd9Sstevel@tonic-gate 		if (is_umem_sym(c, "umem_"))
1717*7c478bd9Sstevel@tonic-gate 			continue;
1718*7c478bd9Sstevel@tonic-gate 		mdb_printf("%s+0x%lx",
1719*7c478bd9Sstevel@tonic-gate 		    c, bcp->bc_stack[i] - (uintptr_t)sym.st_value);
1720*7c478bd9Sstevel@tonic-gate 		break;
1721*7c478bd9Sstevel@tonic-gate 	}
1722*7c478bd9Sstevel@tonic-gate 	mdb_printf("\n");
1723*7c478bd9Sstevel@tonic-gate 
1724*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
1725*7c478bd9Sstevel@tonic-gate }
1726*7c478bd9Sstevel@tonic-gate 
1727*7c478bd9Sstevel@tonic-gate static int
1728*7c478bd9Sstevel@tonic-gate allocdby_common(uintptr_t addr, uint_t flags, const char *w)
1729*7c478bd9Sstevel@tonic-gate {
1730*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
1731*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
1732*7c478bd9Sstevel@tonic-gate 
1733*7c478bd9Sstevel@tonic-gate 	mdb_printf("%-?s %12s %s\n", "BUFCTL", "TIMESTAMP", "CALLER");
1734*7c478bd9Sstevel@tonic-gate 
1735*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk(w, (mdb_walk_cb_t)allocdby_walk, NULL, addr) == -1) {
1736*7c478bd9Sstevel@tonic-gate 		mdb_warn("can't walk '%s' for %p", w, addr);
1737*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1738*7c478bd9Sstevel@tonic-gate 	}
1739*7c478bd9Sstevel@tonic-gate 
1740*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1741*7c478bd9Sstevel@tonic-gate }
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1744*7c478bd9Sstevel@tonic-gate int
1745*7c478bd9Sstevel@tonic-gate allocdby(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1746*7c478bd9Sstevel@tonic-gate {
1747*7c478bd9Sstevel@tonic-gate 	return (allocdby_common(addr, flags, "allocdby"));
1748*7c478bd9Sstevel@tonic-gate }
1749*7c478bd9Sstevel@tonic-gate 
1750*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1751*7c478bd9Sstevel@tonic-gate int
1752*7c478bd9Sstevel@tonic-gate freedby(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1753*7c478bd9Sstevel@tonic-gate {
1754*7c478bd9Sstevel@tonic-gate 	return (allocdby_common(addr, flags, "freedby"));
1755*7c478bd9Sstevel@tonic-gate }
1756*7c478bd9Sstevel@tonic-gate 
1757*7c478bd9Sstevel@tonic-gate typedef struct whatis {
1758*7c478bd9Sstevel@tonic-gate 	uintptr_t w_addr;
1759*7c478bd9Sstevel@tonic-gate 	const umem_cache_t *w_cache;
1760*7c478bd9Sstevel@tonic-gate 	const vmem_t *w_vmem;
1761*7c478bd9Sstevel@tonic-gate 	int w_found;
1762*7c478bd9Sstevel@tonic-gate 	uint_t w_verbose;
1763*7c478bd9Sstevel@tonic-gate 	uint_t w_freemem;
1764*7c478bd9Sstevel@tonic-gate 	uint_t w_all;
1765*7c478bd9Sstevel@tonic-gate 	uint_t w_bufctl;
1766*7c478bd9Sstevel@tonic-gate } whatis_t;
1767*7c478bd9Sstevel@tonic-gate 
1768*7c478bd9Sstevel@tonic-gate static void
1769*7c478bd9Sstevel@tonic-gate whatis_print_umem(uintptr_t addr, uintptr_t baddr, whatis_t *w)
1770*7c478bd9Sstevel@tonic-gate {
1771*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
1772*7c478bd9Sstevel@tonic-gate 	uintptr_t btaddr = (uintptr_t)UMEM_BUFTAG(w->w_cache, addr);
1773*7c478bd9Sstevel@tonic-gate 	intptr_t stat;
1774*7c478bd9Sstevel@tonic-gate 
1775*7c478bd9Sstevel@tonic-gate 	if (w->w_cache->cache_flags & UMF_REDZONE) {
1776*7c478bd9Sstevel@tonic-gate 		umem_buftag_t bt;
1777*7c478bd9Sstevel@tonic-gate 
1778*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&bt, sizeof (bt), btaddr) == -1)
1779*7c478bd9Sstevel@tonic-gate 			goto done;
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate 		stat = (intptr_t)bt.bt_bufctl ^ bt.bt_bxstat;
1782*7c478bd9Sstevel@tonic-gate 
1783*7c478bd9Sstevel@tonic-gate 		if (stat != UMEM_BUFTAG_ALLOC && stat != UMEM_BUFTAG_FREE)
1784*7c478bd9Sstevel@tonic-gate 			goto done;
1785*7c478bd9Sstevel@tonic-gate 
1786*7c478bd9Sstevel@tonic-gate 		/*
1787*7c478bd9Sstevel@tonic-gate 		 * provide the bufctl ptr if it has useful information
1788*7c478bd9Sstevel@tonic-gate 		 */
1789*7c478bd9Sstevel@tonic-gate 		if (baddr == 0 && (w->w_cache->cache_flags & UMF_AUDIT))
1790*7c478bd9Sstevel@tonic-gate 			baddr = (uintptr_t)bt.bt_bufctl;
1791*7c478bd9Sstevel@tonic-gate 	}
1792*7c478bd9Sstevel@tonic-gate 
1793*7c478bd9Sstevel@tonic-gate done:
1794*7c478bd9Sstevel@tonic-gate 	if (baddr == 0)
1795*7c478bd9Sstevel@tonic-gate 		mdb_printf("%p is %p+%p, %s from %s\n",
1796*7c478bd9Sstevel@tonic-gate 		    w->w_addr, addr, w->w_addr - addr,
1797*7c478bd9Sstevel@tonic-gate 		    w->w_freemem == FALSE ? "allocated" : "freed",
1798*7c478bd9Sstevel@tonic-gate 		    w->w_cache->cache_name);
1799*7c478bd9Sstevel@tonic-gate 	else
1800*7c478bd9Sstevel@tonic-gate 		mdb_printf("%p is %p+%p, bufctl %p %s from %s\n",
1801*7c478bd9Sstevel@tonic-gate 		    w->w_addr, addr, w->w_addr - addr, baddr,
1802*7c478bd9Sstevel@tonic-gate 		    w->w_freemem == FALSE ? "allocated" : "freed",
1803*7c478bd9Sstevel@tonic-gate 		    w->w_cache->cache_name);
1804*7c478bd9Sstevel@tonic-gate }
1805*7c478bd9Sstevel@tonic-gate 
1806*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1807*7c478bd9Sstevel@tonic-gate static int
1808*7c478bd9Sstevel@tonic-gate whatis_walk_umem(uintptr_t addr, void *ignored, whatis_t *w)
1809*7c478bd9Sstevel@tonic-gate {
1810*7c478bd9Sstevel@tonic-gate 	if (w->w_addr < addr || w->w_addr >= addr + w->w_cache->cache_bufsize)
1811*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1812*7c478bd9Sstevel@tonic-gate 
1813*7c478bd9Sstevel@tonic-gate 	whatis_print_umem(addr, 0, w);
1814*7c478bd9Sstevel@tonic-gate 	w->w_found++;
1815*7c478bd9Sstevel@tonic-gate 	return (w->w_all == TRUE ? WALK_NEXT : WALK_DONE);
1816*7c478bd9Sstevel@tonic-gate }
1817*7c478bd9Sstevel@tonic-gate 
1818*7c478bd9Sstevel@tonic-gate static int
1819*7c478bd9Sstevel@tonic-gate whatis_walk_seg(uintptr_t addr, const vmem_seg_t *vs, whatis_t *w)
1820*7c478bd9Sstevel@tonic-gate {
1821*7c478bd9Sstevel@tonic-gate 	if (w->w_addr < vs->vs_start || w->w_addr >= vs->vs_end)
1822*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1823*7c478bd9Sstevel@tonic-gate 
1824*7c478bd9Sstevel@tonic-gate 	mdb_printf("%p is %p+%p ", w->w_addr,
1825*7c478bd9Sstevel@tonic-gate 	    vs->vs_start, w->w_addr - vs->vs_start);
1826*7c478bd9Sstevel@tonic-gate 
1827*7c478bd9Sstevel@tonic-gate 	/*
1828*7c478bd9Sstevel@tonic-gate 	 * Always provide the vmem_seg pointer if it has a stack trace.
1829*7c478bd9Sstevel@tonic-gate 	 */
1830*7c478bd9Sstevel@tonic-gate 	if (w->w_bufctl == TRUE ||
1831*7c478bd9Sstevel@tonic-gate 	    (vs->vs_type == VMEM_ALLOC && vs->vs_depth != 0)) {
1832*7c478bd9Sstevel@tonic-gate 		mdb_printf("(vmem_seg %p) ", addr);
1833*7c478bd9Sstevel@tonic-gate 	}
1834*7c478bd9Sstevel@tonic-gate 
1835*7c478bd9Sstevel@tonic-gate 	mdb_printf("%sfrom %s vmem arena\n", w->w_freemem == TRUE ?
1836*7c478bd9Sstevel@tonic-gate 	    "freed " : "", w->w_vmem->vm_name);
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate 	w->w_found++;
1839*7c478bd9Sstevel@tonic-gate 	return (w->w_all == TRUE ? WALK_NEXT : WALK_DONE);
1840*7c478bd9Sstevel@tonic-gate }
1841*7c478bd9Sstevel@tonic-gate 
1842*7c478bd9Sstevel@tonic-gate static int
1843*7c478bd9Sstevel@tonic-gate whatis_walk_vmem(uintptr_t addr, const vmem_t *vmem, whatis_t *w)
1844*7c478bd9Sstevel@tonic-gate {
1845*7c478bd9Sstevel@tonic-gate 	const char *nm = vmem->vm_name;
1846*7c478bd9Sstevel@tonic-gate 	w->w_vmem = vmem;
1847*7c478bd9Sstevel@tonic-gate 	w->w_freemem = FALSE;
1848*7c478bd9Sstevel@tonic-gate 
1849*7c478bd9Sstevel@tonic-gate 	if (w->w_verbose)
1850*7c478bd9Sstevel@tonic-gate 		mdb_printf("Searching vmem arena %s...\n", nm);
1851*7c478bd9Sstevel@tonic-gate 
1852*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("vmem_alloc",
1853*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)whatis_walk_seg, w, addr) == -1) {
1854*7c478bd9Sstevel@tonic-gate 		mdb_warn("can't walk vmem seg for %p", addr);
1855*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1856*7c478bd9Sstevel@tonic-gate 	}
1857*7c478bd9Sstevel@tonic-gate 
1858*7c478bd9Sstevel@tonic-gate 	if (w->w_found && w->w_all == FALSE)
1859*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1860*7c478bd9Sstevel@tonic-gate 
1861*7c478bd9Sstevel@tonic-gate 	if (w->w_verbose)
1862*7c478bd9Sstevel@tonic-gate 		mdb_printf("Searching vmem arena %s for free virtual...\n", nm);
1863*7c478bd9Sstevel@tonic-gate 
1864*7c478bd9Sstevel@tonic-gate 	w->w_freemem = TRUE;
1865*7c478bd9Sstevel@tonic-gate 
1866*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("vmem_free",
1867*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)whatis_walk_seg, w, addr) == -1) {
1868*7c478bd9Sstevel@tonic-gate 		mdb_warn("can't walk vmem seg for %p", addr);
1869*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1870*7c478bd9Sstevel@tonic-gate 	}
1871*7c478bd9Sstevel@tonic-gate 
1872*7c478bd9Sstevel@tonic-gate 	return (w->w_found && w->w_all == FALSE ? WALK_DONE : WALK_NEXT);
1873*7c478bd9Sstevel@tonic-gate }
1874*7c478bd9Sstevel@tonic-gate 
1875*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1876*7c478bd9Sstevel@tonic-gate static int
1877*7c478bd9Sstevel@tonic-gate whatis_walk_bufctl(uintptr_t baddr, const umem_bufctl_t *bcp, whatis_t *w)
1878*7c478bd9Sstevel@tonic-gate {
1879*7c478bd9Sstevel@tonic-gate 	uintptr_t addr;
1880*7c478bd9Sstevel@tonic-gate 
1881*7c478bd9Sstevel@tonic-gate 	if (bcp == NULL)
1882*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1883*7c478bd9Sstevel@tonic-gate 
1884*7c478bd9Sstevel@tonic-gate 	addr = (uintptr_t)bcp->bc_addr;
1885*7c478bd9Sstevel@tonic-gate 
1886*7c478bd9Sstevel@tonic-gate 	if (w->w_addr < addr || w->w_addr >= addr + w->w_cache->cache_bufsize)
1887*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1888*7c478bd9Sstevel@tonic-gate 
1889*7c478bd9Sstevel@tonic-gate 	whatis_print_umem(addr, baddr, w);
1890*7c478bd9Sstevel@tonic-gate 	w->w_found++;
1891*7c478bd9Sstevel@tonic-gate 	return (w->w_all == TRUE ? WALK_NEXT : WALK_DONE);
1892*7c478bd9Sstevel@tonic-gate }
1893*7c478bd9Sstevel@tonic-gate 
1894*7c478bd9Sstevel@tonic-gate static int
1895*7c478bd9Sstevel@tonic-gate whatis_walk_cache(uintptr_t addr, const umem_cache_t *c, whatis_t *w)
1896*7c478bd9Sstevel@tonic-gate {
1897*7c478bd9Sstevel@tonic-gate 	char *walk, *freewalk;
1898*7c478bd9Sstevel@tonic-gate 	mdb_walk_cb_t func;
1899*7c478bd9Sstevel@tonic-gate 
1900*7c478bd9Sstevel@tonic-gate 	if (w->w_bufctl == FALSE) {
1901*7c478bd9Sstevel@tonic-gate 		walk = "umem";
1902*7c478bd9Sstevel@tonic-gate 		freewalk = "freemem";
1903*7c478bd9Sstevel@tonic-gate 		func = (mdb_walk_cb_t)whatis_walk_umem;
1904*7c478bd9Sstevel@tonic-gate 	} else {
1905*7c478bd9Sstevel@tonic-gate 		walk = "bufctl";
1906*7c478bd9Sstevel@tonic-gate 		freewalk = "freectl";
1907*7c478bd9Sstevel@tonic-gate 		func = (mdb_walk_cb_t)whatis_walk_bufctl;
1908*7c478bd9Sstevel@tonic-gate 	}
1909*7c478bd9Sstevel@tonic-gate 
1910*7c478bd9Sstevel@tonic-gate 	if (w->w_verbose)
1911*7c478bd9Sstevel@tonic-gate 		mdb_printf("Searching %s...\n", c->cache_name);
1912*7c478bd9Sstevel@tonic-gate 
1913*7c478bd9Sstevel@tonic-gate 	w->w_cache = c;
1914*7c478bd9Sstevel@tonic-gate 	w->w_freemem = FALSE;
1915*7c478bd9Sstevel@tonic-gate 
1916*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk(walk, func, w, addr) == -1) {
1917*7c478bd9Sstevel@tonic-gate 		mdb_warn("can't find %s walker", walk);
1918*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1919*7c478bd9Sstevel@tonic-gate 	}
1920*7c478bd9Sstevel@tonic-gate 
1921*7c478bd9Sstevel@tonic-gate 	if (w->w_found && w->w_all == FALSE)
1922*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1923*7c478bd9Sstevel@tonic-gate 
1924*7c478bd9Sstevel@tonic-gate 	/*
1925*7c478bd9Sstevel@tonic-gate 	 * We have searched for allocated memory; now search for freed memory.
1926*7c478bd9Sstevel@tonic-gate 	 */
1927*7c478bd9Sstevel@tonic-gate 	if (w->w_verbose)
1928*7c478bd9Sstevel@tonic-gate 		mdb_printf("Searching %s for free memory...\n", c->cache_name);
1929*7c478bd9Sstevel@tonic-gate 
1930*7c478bd9Sstevel@tonic-gate 	w->w_freemem = TRUE;
1931*7c478bd9Sstevel@tonic-gate 
1932*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk(freewalk, func, w, addr) == -1) {
1933*7c478bd9Sstevel@tonic-gate 		mdb_warn("can't find %s walker", freewalk);
1934*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
1935*7c478bd9Sstevel@tonic-gate 	}
1936*7c478bd9Sstevel@tonic-gate 
1937*7c478bd9Sstevel@tonic-gate 	return (w->w_found && w->w_all == FALSE ? WALK_DONE : WALK_NEXT);
1938*7c478bd9Sstevel@tonic-gate }
1939*7c478bd9Sstevel@tonic-gate 
1940*7c478bd9Sstevel@tonic-gate static int
1941*7c478bd9Sstevel@tonic-gate whatis_walk_touch(uintptr_t addr, const umem_cache_t *c, whatis_t *w)
1942*7c478bd9Sstevel@tonic-gate {
1943*7c478bd9Sstevel@tonic-gate 	if (c->cache_cflags & UMC_NOTOUCH)
1944*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1945*7c478bd9Sstevel@tonic-gate 
1946*7c478bd9Sstevel@tonic-gate 	return (whatis_walk_cache(addr, c, w));
1947*7c478bd9Sstevel@tonic-gate }
1948*7c478bd9Sstevel@tonic-gate 
1949*7c478bd9Sstevel@tonic-gate static int
1950*7c478bd9Sstevel@tonic-gate whatis_walk_notouch(uintptr_t addr, const umem_cache_t *c, whatis_t *w)
1951*7c478bd9Sstevel@tonic-gate {
1952*7c478bd9Sstevel@tonic-gate 	if (!(c->cache_cflags & UMC_NOTOUCH))
1953*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
1954*7c478bd9Sstevel@tonic-gate 
1955*7c478bd9Sstevel@tonic-gate 	return (whatis_walk_cache(addr, c, w));
1956*7c478bd9Sstevel@tonic-gate }
1957*7c478bd9Sstevel@tonic-gate 
1958*7c478bd9Sstevel@tonic-gate int
1959*7c478bd9Sstevel@tonic-gate whatis(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1960*7c478bd9Sstevel@tonic-gate {
1961*7c478bd9Sstevel@tonic-gate 	whatis_t w;
1962*7c478bd9Sstevel@tonic-gate 
1963*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
1964*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
1965*7c478bd9Sstevel@tonic-gate 
1966*7c478bd9Sstevel@tonic-gate 	w.w_verbose = FALSE;
1967*7c478bd9Sstevel@tonic-gate 	w.w_bufctl = FALSE;
1968*7c478bd9Sstevel@tonic-gate 	w.w_all = FALSE;
1969*7c478bd9Sstevel@tonic-gate 
1970*7c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
1971*7c478bd9Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &w.w_verbose,
1972*7c478bd9Sstevel@tonic-gate 	    'a', MDB_OPT_SETBITS, TRUE, &w.w_all,
1973*7c478bd9Sstevel@tonic-gate 	    'b', MDB_OPT_SETBITS, TRUE, &w.w_bufctl, NULL) != argc)
1974*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
1975*7c478bd9Sstevel@tonic-gate 
1976*7c478bd9Sstevel@tonic-gate 	w.w_addr = addr;
1977*7c478bd9Sstevel@tonic-gate 	w.w_found = 0;
1978*7c478bd9Sstevel@tonic-gate 
1979*7c478bd9Sstevel@tonic-gate 	/*
1980*7c478bd9Sstevel@tonic-gate 	 * Mappings and threads should eventually be added here.
1981*7c478bd9Sstevel@tonic-gate 	 */
1982*7c478bd9Sstevel@tonic-gate 	if (mdb_walk("umem_cache",
1983*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)whatis_walk_touch, &w) == -1) {
1984*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't find umem_cache walker");
1985*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1986*7c478bd9Sstevel@tonic-gate 	}
1987*7c478bd9Sstevel@tonic-gate 
1988*7c478bd9Sstevel@tonic-gate 	if (w.w_found && w.w_all == FALSE)
1989*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
1990*7c478bd9Sstevel@tonic-gate 
1991*7c478bd9Sstevel@tonic-gate 	if (mdb_walk("umem_cache",
1992*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)whatis_walk_notouch, &w) == -1) {
1993*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't find umem_cache walker");
1994*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1995*7c478bd9Sstevel@tonic-gate 	}
1996*7c478bd9Sstevel@tonic-gate 
1997*7c478bd9Sstevel@tonic-gate 	if (w.w_found && w.w_all == FALSE)
1998*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
1999*7c478bd9Sstevel@tonic-gate 
2000*7c478bd9Sstevel@tonic-gate 	if (mdb_walk("vmem_postfix",
2001*7c478bd9Sstevel@tonic-gate 	    (mdb_walk_cb_t)whatis_walk_vmem, &w) == -1) {
2002*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't find vmem_postfix walker");
2003*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2004*7c478bd9Sstevel@tonic-gate 	}
2005*7c478bd9Sstevel@tonic-gate 
2006*7c478bd9Sstevel@tonic-gate 	if (w.w_found == 0)
2007*7c478bd9Sstevel@tonic-gate 		mdb_printf("%p is unknown\n", addr);
2008*7c478bd9Sstevel@tonic-gate 
2009*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2010*7c478bd9Sstevel@tonic-gate }
2011*7c478bd9Sstevel@tonic-gate 
2012*7c478bd9Sstevel@tonic-gate typedef struct umem_log_cpu {
2013*7c478bd9Sstevel@tonic-gate 	uintptr_t umc_low;
2014*7c478bd9Sstevel@tonic-gate 	uintptr_t umc_high;
2015*7c478bd9Sstevel@tonic-gate } umem_log_cpu_t;
2016*7c478bd9Sstevel@tonic-gate 
2017*7c478bd9Sstevel@tonic-gate int
2018*7c478bd9Sstevel@tonic-gate umem_log_walk(uintptr_t addr, const umem_bufctl_audit_t *b, umem_log_cpu_t *umc)
2019*7c478bd9Sstevel@tonic-gate {
2020*7c478bd9Sstevel@tonic-gate 	int i;
2021*7c478bd9Sstevel@tonic-gate 
2022*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < umem_max_ncpus; i++) {
2023*7c478bd9Sstevel@tonic-gate 		if (addr >= umc[i].umc_low && addr < umc[i].umc_high)
2024*7c478bd9Sstevel@tonic-gate 			break;
2025*7c478bd9Sstevel@tonic-gate 	}
2026*7c478bd9Sstevel@tonic-gate 
2027*7c478bd9Sstevel@tonic-gate 	if (i == umem_max_ncpus)
2028*7c478bd9Sstevel@tonic-gate 		mdb_printf("   ");
2029*7c478bd9Sstevel@tonic-gate 	else
2030*7c478bd9Sstevel@tonic-gate 		mdb_printf("%3d", i);
2031*7c478bd9Sstevel@tonic-gate 
2032*7c478bd9Sstevel@tonic-gate 	mdb_printf(" %0?p %0?p %16llx %0?p\n", addr, b->bc_addr,
2033*7c478bd9Sstevel@tonic-gate 	    b->bc_timestamp, b->bc_thread);
2034*7c478bd9Sstevel@tonic-gate 
2035*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2036*7c478bd9Sstevel@tonic-gate }
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2039*7c478bd9Sstevel@tonic-gate int
2040*7c478bd9Sstevel@tonic-gate umem_log(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2041*7c478bd9Sstevel@tonic-gate {
2042*7c478bd9Sstevel@tonic-gate 	umem_log_header_t lh;
2043*7c478bd9Sstevel@tonic-gate 	umem_cpu_log_header_t clh;
2044*7c478bd9Sstevel@tonic-gate 	uintptr_t lhp, clhp;
2045*7c478bd9Sstevel@tonic-gate 	umem_log_cpu_t *umc;
2046*7c478bd9Sstevel@tonic-gate 	int i;
2047*7c478bd9Sstevel@tonic-gate 
2048*7c478bd9Sstevel@tonic-gate 	if (umem_readvar(&lhp, "umem_transaction_log") == -1) {
2049*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read 'umem_transaction_log'");
2050*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2051*7c478bd9Sstevel@tonic-gate 	}
2052*7c478bd9Sstevel@tonic-gate 
2053*7c478bd9Sstevel@tonic-gate 	if (lhp == NULL) {
2054*7c478bd9Sstevel@tonic-gate 		mdb_warn("no umem transaction log\n");
2055*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2056*7c478bd9Sstevel@tonic-gate 	}
2057*7c478bd9Sstevel@tonic-gate 
2058*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&lh, sizeof (umem_log_header_t), lhp) == -1) {
2059*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read log header at %p", lhp);
2060*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2061*7c478bd9Sstevel@tonic-gate 	}
2062*7c478bd9Sstevel@tonic-gate 
2063*7c478bd9Sstevel@tonic-gate 	clhp = lhp + ((uintptr_t)&lh.lh_cpu[0] - (uintptr_t)&lh);
2064*7c478bd9Sstevel@tonic-gate 
2065*7c478bd9Sstevel@tonic-gate 	umc = mdb_zalloc(sizeof (umem_log_cpu_t) * umem_max_ncpus,
2066*7c478bd9Sstevel@tonic-gate 	    UM_SLEEP | UM_GC);
2067*7c478bd9Sstevel@tonic-gate 
2068*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < umem_max_ncpus; i++) {
2069*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&clh, sizeof (clh), clhp) == -1) {
2070*7c478bd9Sstevel@tonic-gate 			mdb_warn("cannot read cpu %d's log header at %p",
2071*7c478bd9Sstevel@tonic-gate 			    i, clhp);
2072*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2073*7c478bd9Sstevel@tonic-gate 		}
2074*7c478bd9Sstevel@tonic-gate 
2075*7c478bd9Sstevel@tonic-gate 		umc[i].umc_low = clh.clh_chunk * lh.lh_chunksize +
2076*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)lh.lh_base;
2077*7c478bd9Sstevel@tonic-gate 		umc[i].umc_high = (uintptr_t)clh.clh_current;
2078*7c478bd9Sstevel@tonic-gate 
2079*7c478bd9Sstevel@tonic-gate 		clhp += sizeof (umem_cpu_log_header_t);
2080*7c478bd9Sstevel@tonic-gate 	}
2081*7c478bd9Sstevel@tonic-gate 
2082*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags)) {
2083*7c478bd9Sstevel@tonic-gate 		mdb_printf("%3s %-?s %-?s %16s %-?s\n", "CPU", "ADDR",
2084*7c478bd9Sstevel@tonic-gate 		    "BUFADDR", "TIMESTAMP", "THREAD");
2085*7c478bd9Sstevel@tonic-gate 	}
2086*7c478bd9Sstevel@tonic-gate 
2087*7c478bd9Sstevel@tonic-gate 	/*
2088*7c478bd9Sstevel@tonic-gate 	 * If we have been passed an address, we'll just print out that
2089*7c478bd9Sstevel@tonic-gate 	 * log entry.
2090*7c478bd9Sstevel@tonic-gate 	 */
2091*7c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
2092*7c478bd9Sstevel@tonic-gate 		umem_bufctl_audit_t *bp;
2093*7c478bd9Sstevel@tonic-gate 		UMEM_LOCAL_BUFCTL_AUDIT(&bp);
2094*7c478bd9Sstevel@tonic-gate 
2095*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(bp, UMEM_BUFCTL_AUDIT_SIZE, addr) == -1) {
2096*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read bufctl at %p", addr);
2097*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2098*7c478bd9Sstevel@tonic-gate 		}
2099*7c478bd9Sstevel@tonic-gate 
2100*7c478bd9Sstevel@tonic-gate 		(void) umem_log_walk(addr, bp, umc);
2101*7c478bd9Sstevel@tonic-gate 
2102*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2103*7c478bd9Sstevel@tonic-gate 	}
2104*7c478bd9Sstevel@tonic-gate 
2105*7c478bd9Sstevel@tonic-gate 	if (mdb_walk("umem_log", (mdb_walk_cb_t)umem_log_walk, umc) == -1) {
2106*7c478bd9Sstevel@tonic-gate 		mdb_warn("can't find umem log walker");
2107*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2108*7c478bd9Sstevel@tonic-gate 	}
2109*7c478bd9Sstevel@tonic-gate 
2110*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2111*7c478bd9Sstevel@tonic-gate }
2112*7c478bd9Sstevel@tonic-gate 
2113*7c478bd9Sstevel@tonic-gate typedef struct bufctl_history_cb {
2114*7c478bd9Sstevel@tonic-gate 	int		bhc_flags;
2115*7c478bd9Sstevel@tonic-gate 	int		bhc_argc;
2116*7c478bd9Sstevel@tonic-gate 	const mdb_arg_t	*bhc_argv;
2117*7c478bd9Sstevel@tonic-gate 	int		bhc_ret;
2118*7c478bd9Sstevel@tonic-gate } bufctl_history_cb_t;
2119*7c478bd9Sstevel@tonic-gate 
2120*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2121*7c478bd9Sstevel@tonic-gate static int
2122*7c478bd9Sstevel@tonic-gate bufctl_history_callback(uintptr_t addr, const void *ign, void *arg)
2123*7c478bd9Sstevel@tonic-gate {
2124*7c478bd9Sstevel@tonic-gate 	bufctl_history_cb_t *bhc = arg;
2125*7c478bd9Sstevel@tonic-gate 
2126*7c478bd9Sstevel@tonic-gate 	bhc->bhc_ret =
2127*7c478bd9Sstevel@tonic-gate 	    bufctl(addr, bhc->bhc_flags, bhc->bhc_argc, bhc->bhc_argv);
2128*7c478bd9Sstevel@tonic-gate 
2129*7c478bd9Sstevel@tonic-gate 	bhc->bhc_flags &= ~DCMD_LOOPFIRST;
2130*7c478bd9Sstevel@tonic-gate 
2131*7c478bd9Sstevel@tonic-gate 	return ((bhc->bhc_ret == DCMD_OK)? WALK_NEXT : WALK_DONE);
2132*7c478bd9Sstevel@tonic-gate }
2133*7c478bd9Sstevel@tonic-gate 
2134*7c478bd9Sstevel@tonic-gate void
2135*7c478bd9Sstevel@tonic-gate bufctl_help(void)
2136*7c478bd9Sstevel@tonic-gate {
2137*7c478bd9Sstevel@tonic-gate 	mdb_printf("%s\n",
2138*7c478bd9Sstevel@tonic-gate "Display the contents of umem_bufctl_audit_ts, with optional filtering.\n");
2139*7c478bd9Sstevel@tonic-gate 	mdb_dec_indent(2);
2140*7c478bd9Sstevel@tonic-gate 	mdb_printf("%<b>OPTIONS%</b>\n");
2141*7c478bd9Sstevel@tonic-gate 	mdb_inc_indent(2);
2142*7c478bd9Sstevel@tonic-gate 	mdb_printf("%s",
2143*7c478bd9Sstevel@tonic-gate "  -v    Display the full content of the bufctl, including its stack trace\n"
2144*7c478bd9Sstevel@tonic-gate "  -h    retrieve the bufctl's transaction history, if available\n"
2145*7c478bd9Sstevel@tonic-gate "  -a addr\n"
2146*7c478bd9Sstevel@tonic-gate "        filter out bufctls not involving the buffer at addr\n"
2147*7c478bd9Sstevel@tonic-gate "  -c caller\n"
2148*7c478bd9Sstevel@tonic-gate "        filter out bufctls without the function/PC in their stack trace\n"
2149*7c478bd9Sstevel@tonic-gate "  -e earliest\n"
2150*7c478bd9Sstevel@tonic-gate "        filter out bufctls timestamped before earliest\n"
2151*7c478bd9Sstevel@tonic-gate "  -l latest\n"
2152*7c478bd9Sstevel@tonic-gate "        filter out bufctls timestamped after latest\n"
2153*7c478bd9Sstevel@tonic-gate "  -t thread\n"
2154*7c478bd9Sstevel@tonic-gate "        filter out bufctls not involving thread\n");
2155*7c478bd9Sstevel@tonic-gate }
2156*7c478bd9Sstevel@tonic-gate 
2157*7c478bd9Sstevel@tonic-gate int
2158*7c478bd9Sstevel@tonic-gate bufctl(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2159*7c478bd9Sstevel@tonic-gate {
2160*7c478bd9Sstevel@tonic-gate 	uint_t verbose = FALSE;
2161*7c478bd9Sstevel@tonic-gate 	uint_t history = FALSE;
2162*7c478bd9Sstevel@tonic-gate 	uint_t in_history = FALSE;
2163*7c478bd9Sstevel@tonic-gate 	uintptr_t caller = NULL, thread = NULL;
2164*7c478bd9Sstevel@tonic-gate 	uintptr_t laddr, haddr, baddr = NULL;
2165*7c478bd9Sstevel@tonic-gate 	hrtime_t earliest = 0, latest = 0;
2166*7c478bd9Sstevel@tonic-gate 	int i, depth;
2167*7c478bd9Sstevel@tonic-gate 	char c[MDB_SYM_NAMLEN];
2168*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
2169*7c478bd9Sstevel@tonic-gate 	umem_bufctl_audit_t *bcp;
2170*7c478bd9Sstevel@tonic-gate 	UMEM_LOCAL_BUFCTL_AUDIT(&bcp);
2171*7c478bd9Sstevel@tonic-gate 
2172*7c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
2173*7c478bd9Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
2174*7c478bd9Sstevel@tonic-gate 	    'h', MDB_OPT_SETBITS, TRUE, &history,
2175*7c478bd9Sstevel@tonic-gate 	    'H', MDB_OPT_SETBITS, TRUE, &in_history,		/* internal */
2176*7c478bd9Sstevel@tonic-gate 	    'c', MDB_OPT_UINTPTR, &caller,
2177*7c478bd9Sstevel@tonic-gate 	    't', MDB_OPT_UINTPTR, &thread,
2178*7c478bd9Sstevel@tonic-gate 	    'e', MDB_OPT_UINT64, &earliest,
2179*7c478bd9Sstevel@tonic-gate 	    'l', MDB_OPT_UINT64, &latest,
2180*7c478bd9Sstevel@tonic-gate 	    'a', MDB_OPT_UINTPTR, &baddr, NULL) != argc)
2181*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2182*7c478bd9Sstevel@tonic-gate 
2183*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
2184*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2185*7c478bd9Sstevel@tonic-gate 
2186*7c478bd9Sstevel@tonic-gate 	if (in_history && !history)
2187*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2188*7c478bd9Sstevel@tonic-gate 
2189*7c478bd9Sstevel@tonic-gate 	if (history && !in_history) {
2190*7c478bd9Sstevel@tonic-gate 		mdb_arg_t *nargv = mdb_zalloc(sizeof (*nargv) * (argc + 1),
2191*7c478bd9Sstevel@tonic-gate 		    UM_SLEEP | UM_GC);
2192*7c478bd9Sstevel@tonic-gate 		bufctl_history_cb_t bhc;
2193*7c478bd9Sstevel@tonic-gate 
2194*7c478bd9Sstevel@tonic-gate 		nargv[0].a_type = MDB_TYPE_STRING;
2195*7c478bd9Sstevel@tonic-gate 		nargv[0].a_un.a_str = "-H";		/* prevent recursion */
2196*7c478bd9Sstevel@tonic-gate 
2197*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
2198*7c478bd9Sstevel@tonic-gate 			nargv[i + 1] = argv[i];
2199*7c478bd9Sstevel@tonic-gate 
2200*7c478bd9Sstevel@tonic-gate 		/*
2201*7c478bd9Sstevel@tonic-gate 		 * When in history mode, we treat each element as if it
2202*7c478bd9Sstevel@tonic-gate 		 * were in a seperate loop, so that the headers group
2203*7c478bd9Sstevel@tonic-gate 		 * bufctls with similar histories.
2204*7c478bd9Sstevel@tonic-gate 		 */
2205*7c478bd9Sstevel@tonic-gate 		bhc.bhc_flags = flags | DCMD_LOOP | DCMD_LOOPFIRST;
2206*7c478bd9Sstevel@tonic-gate 		bhc.bhc_argc = argc + 1;
2207*7c478bd9Sstevel@tonic-gate 		bhc.bhc_argv = nargv;
2208*7c478bd9Sstevel@tonic-gate 		bhc.bhc_ret = DCMD_OK;
2209*7c478bd9Sstevel@tonic-gate 
2210*7c478bd9Sstevel@tonic-gate 		if (mdb_pwalk("bufctl_history", bufctl_history_callback, &bhc,
2211*7c478bd9Sstevel@tonic-gate 		    addr) == -1) {
2212*7c478bd9Sstevel@tonic-gate 			mdb_warn("unable to walk bufctl_history");
2213*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2214*7c478bd9Sstevel@tonic-gate 		}
2215*7c478bd9Sstevel@tonic-gate 
2216*7c478bd9Sstevel@tonic-gate 		if (bhc.bhc_ret == DCMD_OK && !(flags & DCMD_PIPE_OUT))
2217*7c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
2218*7c478bd9Sstevel@tonic-gate 
2219*7c478bd9Sstevel@tonic-gate 		return (bhc.bhc_ret);
2220*7c478bd9Sstevel@tonic-gate 	}
2221*7c478bd9Sstevel@tonic-gate 
2222*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && !(flags & DCMD_PIPE_OUT)) {
2223*7c478bd9Sstevel@tonic-gate 		if (verbose) {
2224*7c478bd9Sstevel@tonic-gate 			mdb_printf("%16s %16s %16s %16s\n"
2225*7c478bd9Sstevel@tonic-gate 			    "%<u>%16s %16s %16s %16s%</u>\n",
2226*7c478bd9Sstevel@tonic-gate 			    "ADDR", "BUFADDR", "TIMESTAMP", "THREAD",
2227*7c478bd9Sstevel@tonic-gate 			    "", "CACHE", "LASTLOG", "CONTENTS");
2228*7c478bd9Sstevel@tonic-gate 		} else {
2229*7c478bd9Sstevel@tonic-gate 			mdb_printf("%<u>%-?s %-?s %-12s %5s %s%</u>\n",
2230*7c478bd9Sstevel@tonic-gate 			    "ADDR", "BUFADDR", "TIMESTAMP", "THRD", "CALLER");
2231*7c478bd9Sstevel@tonic-gate 		}
2232*7c478bd9Sstevel@tonic-gate 	}
2233*7c478bd9Sstevel@tonic-gate 
2234*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(bcp, UMEM_BUFCTL_AUDIT_SIZE, addr) == -1) {
2235*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read bufctl at %p", addr);
2236*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2237*7c478bd9Sstevel@tonic-gate 	}
2238*7c478bd9Sstevel@tonic-gate 
2239*7c478bd9Sstevel@tonic-gate 	/*
2240*7c478bd9Sstevel@tonic-gate 	 * Guard against bogus bc_depth in case the bufctl is corrupt or
2241*7c478bd9Sstevel@tonic-gate 	 * the address does not really refer to a bufctl.
2242*7c478bd9Sstevel@tonic-gate 	 */
2243*7c478bd9Sstevel@tonic-gate 	depth = MIN(bcp->bc_depth, umem_stack_depth);
2244*7c478bd9Sstevel@tonic-gate 
2245*7c478bd9Sstevel@tonic-gate 	if (caller != NULL) {
2246*7c478bd9Sstevel@tonic-gate 		laddr = caller;
2247*7c478bd9Sstevel@tonic-gate 		haddr = caller + sizeof (caller);
2248*7c478bd9Sstevel@tonic-gate 
2249*7c478bd9Sstevel@tonic-gate 		if (mdb_lookup_by_addr(caller, MDB_SYM_FUZZY, c, sizeof (c),
2250*7c478bd9Sstevel@tonic-gate 		    &sym) != -1 && caller == (uintptr_t)sym.st_value) {
2251*7c478bd9Sstevel@tonic-gate 			/*
2252*7c478bd9Sstevel@tonic-gate 			 * We were provided an exact symbol value; any
2253*7c478bd9Sstevel@tonic-gate 			 * address in the function is valid.
2254*7c478bd9Sstevel@tonic-gate 			 */
2255*7c478bd9Sstevel@tonic-gate 			laddr = (uintptr_t)sym.st_value;
2256*7c478bd9Sstevel@tonic-gate 			haddr = (uintptr_t)sym.st_value + sym.st_size;
2257*7c478bd9Sstevel@tonic-gate 		}
2258*7c478bd9Sstevel@tonic-gate 
2259*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++)
2260*7c478bd9Sstevel@tonic-gate 			if (bcp->bc_stack[i] >= laddr &&
2261*7c478bd9Sstevel@tonic-gate 			    bcp->bc_stack[i] < haddr)
2262*7c478bd9Sstevel@tonic-gate 				break;
2263*7c478bd9Sstevel@tonic-gate 
2264*7c478bd9Sstevel@tonic-gate 		if (i == depth)
2265*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
2266*7c478bd9Sstevel@tonic-gate 	}
2267*7c478bd9Sstevel@tonic-gate 
2268*7c478bd9Sstevel@tonic-gate 	if (thread != NULL && (uintptr_t)bcp->bc_thread != thread)
2269*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2270*7c478bd9Sstevel@tonic-gate 
2271*7c478bd9Sstevel@tonic-gate 	if (earliest != 0 && bcp->bc_timestamp < earliest)
2272*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2273*7c478bd9Sstevel@tonic-gate 
2274*7c478bd9Sstevel@tonic-gate 	if (latest != 0 && bcp->bc_timestamp > latest)
2275*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2276*7c478bd9Sstevel@tonic-gate 
2277*7c478bd9Sstevel@tonic-gate 	if (baddr != 0 && (uintptr_t)bcp->bc_addr != baddr)
2278*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2279*7c478bd9Sstevel@tonic-gate 
2280*7c478bd9Sstevel@tonic-gate 	if (flags & DCMD_PIPE_OUT) {
2281*7c478bd9Sstevel@tonic-gate 		mdb_printf("%#r\n", addr);
2282*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2283*7c478bd9Sstevel@tonic-gate 	}
2284*7c478bd9Sstevel@tonic-gate 
2285*7c478bd9Sstevel@tonic-gate 	if (verbose) {
2286*7c478bd9Sstevel@tonic-gate 		mdb_printf(
2287*7c478bd9Sstevel@tonic-gate 		    "%<b>%16p%</b> %16p %16llx %16d\n"
2288*7c478bd9Sstevel@tonic-gate 		    "%16s %16p %16p %16p\n",
2289*7c478bd9Sstevel@tonic-gate 		    addr, bcp->bc_addr, bcp->bc_timestamp, bcp->bc_thread,
2290*7c478bd9Sstevel@tonic-gate 		    "", bcp->bc_cache, bcp->bc_lastlog, bcp->bc_contents);
2291*7c478bd9Sstevel@tonic-gate 
2292*7c478bd9Sstevel@tonic-gate 		mdb_inc_indent(17);
2293*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++)
2294*7c478bd9Sstevel@tonic-gate 			mdb_printf("%a\n", bcp->bc_stack[i]);
2295*7c478bd9Sstevel@tonic-gate 		mdb_dec_indent(17);
2296*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n");
2297*7c478bd9Sstevel@tonic-gate 	} else {
2298*7c478bd9Sstevel@tonic-gate 		mdb_printf("%0?p %0?p %12llx %5d", addr, bcp->bc_addr,
2299*7c478bd9Sstevel@tonic-gate 		    bcp->bc_timestamp, bcp->bc_thread);
2300*7c478bd9Sstevel@tonic-gate 
2301*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++) {
2302*7c478bd9Sstevel@tonic-gate 			if (mdb_lookup_by_addr(bcp->bc_stack[i],
2303*7c478bd9Sstevel@tonic-gate 			    MDB_SYM_FUZZY, c, sizeof (c), &sym) == -1)
2304*7c478bd9Sstevel@tonic-gate 				continue;
2305*7c478bd9Sstevel@tonic-gate 			if (is_umem_sym(c, "umem_"))
2306*7c478bd9Sstevel@tonic-gate 				continue;
2307*7c478bd9Sstevel@tonic-gate 			mdb_printf(" %a\n", bcp->bc_stack[i]);
2308*7c478bd9Sstevel@tonic-gate 			break;
2309*7c478bd9Sstevel@tonic-gate 		}
2310*7c478bd9Sstevel@tonic-gate 
2311*7c478bd9Sstevel@tonic-gate 		if (i >= depth)
2312*7c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
2313*7c478bd9Sstevel@tonic-gate 	}
2314*7c478bd9Sstevel@tonic-gate 
2315*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2316*7c478bd9Sstevel@tonic-gate }
2317*7c478bd9Sstevel@tonic-gate 
2318*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2319*7c478bd9Sstevel@tonic-gate int
2320*7c478bd9Sstevel@tonic-gate bufctl_audit(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2321*7c478bd9Sstevel@tonic-gate {
2322*7c478bd9Sstevel@tonic-gate 	mdb_arg_t a;
2323*7c478bd9Sstevel@tonic-gate 
2324*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
2325*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2326*7c478bd9Sstevel@tonic-gate 
2327*7c478bd9Sstevel@tonic-gate 	if (argc != 0)
2328*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2329*7c478bd9Sstevel@tonic-gate 
2330*7c478bd9Sstevel@tonic-gate 	a.a_type = MDB_TYPE_STRING;
2331*7c478bd9Sstevel@tonic-gate 	a.a_un.a_str = "-v";
2332*7c478bd9Sstevel@tonic-gate 
2333*7c478bd9Sstevel@tonic-gate 	return (bufctl(addr, flags, 1, &a));
2334*7c478bd9Sstevel@tonic-gate }
2335*7c478bd9Sstevel@tonic-gate 
2336*7c478bd9Sstevel@tonic-gate typedef struct umem_verify {
2337*7c478bd9Sstevel@tonic-gate 	uint64_t *umv_buf;		/* buffer to read cache contents into */
2338*7c478bd9Sstevel@tonic-gate 	size_t umv_size;		/* number of bytes in umv_buf */
2339*7c478bd9Sstevel@tonic-gate 	int umv_corruption;		/* > 0 if corruption found. */
2340*7c478bd9Sstevel@tonic-gate 	int umv_besilent;		/* report actual corruption sites */
2341*7c478bd9Sstevel@tonic-gate 	struct umem_cache umv_cache;	/* the cache we're operating on */
2342*7c478bd9Sstevel@tonic-gate } umem_verify_t;
2343*7c478bd9Sstevel@tonic-gate 
2344*7c478bd9Sstevel@tonic-gate /*
2345*7c478bd9Sstevel@tonic-gate  * verify_pattern()
2346*7c478bd9Sstevel@tonic-gate  *	verify that buf is filled with the pattern pat.
2347*7c478bd9Sstevel@tonic-gate  */
2348*7c478bd9Sstevel@tonic-gate static int64_t
2349*7c478bd9Sstevel@tonic-gate verify_pattern(uint64_t *buf_arg, size_t size, uint64_t pat)
2350*7c478bd9Sstevel@tonic-gate {
2351*7c478bd9Sstevel@tonic-gate 	/*LINTED*/
2352*7c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
2353*7c478bd9Sstevel@tonic-gate 	uint64_t *buf;
2354*7c478bd9Sstevel@tonic-gate 
2355*7c478bd9Sstevel@tonic-gate 	for (buf = buf_arg; buf < bufend; buf++)
2356*7c478bd9Sstevel@tonic-gate 		if (*buf != pat)
2357*7c478bd9Sstevel@tonic-gate 			return ((uintptr_t)buf - (uintptr_t)buf_arg);
2358*7c478bd9Sstevel@tonic-gate 	return (-1);
2359*7c478bd9Sstevel@tonic-gate }
2360*7c478bd9Sstevel@tonic-gate 
2361*7c478bd9Sstevel@tonic-gate /*
2362*7c478bd9Sstevel@tonic-gate  * verify_buftag()
2363*7c478bd9Sstevel@tonic-gate  *	verify that btp->bt_bxstat == (bcp ^ pat)
2364*7c478bd9Sstevel@tonic-gate  */
2365*7c478bd9Sstevel@tonic-gate static int
2366*7c478bd9Sstevel@tonic-gate verify_buftag(umem_buftag_t *btp, uintptr_t pat)
2367*7c478bd9Sstevel@tonic-gate {
2368*7c478bd9Sstevel@tonic-gate 	return (btp->bt_bxstat == ((intptr_t)btp->bt_bufctl ^ pat) ? 0 : -1);
2369*7c478bd9Sstevel@tonic-gate }
2370*7c478bd9Sstevel@tonic-gate 
2371*7c478bd9Sstevel@tonic-gate /*
2372*7c478bd9Sstevel@tonic-gate  * verify_free()
2373*7c478bd9Sstevel@tonic-gate  *	verify the integrity of a free block of memory by checking
2374*7c478bd9Sstevel@tonic-gate  *	that it is filled with 0xdeadbeef and that its buftag is sane.
2375*7c478bd9Sstevel@tonic-gate  */
2376*7c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2377*7c478bd9Sstevel@tonic-gate static int
2378*7c478bd9Sstevel@tonic-gate verify_free(uintptr_t addr, const void *data, void *private)
2379*7c478bd9Sstevel@tonic-gate {
2380*7c478bd9Sstevel@tonic-gate 	umem_verify_t *umv = (umem_verify_t *)private;
2381*7c478bd9Sstevel@tonic-gate 	uint64_t *buf = umv->umv_buf;	/* buf to validate */
2382*7c478bd9Sstevel@tonic-gate 	int64_t corrupt;		/* corruption offset */
2383*7c478bd9Sstevel@tonic-gate 	umem_buftag_t *buftagp;		/* ptr to buftag */
2384*7c478bd9Sstevel@tonic-gate 	umem_cache_t *cp = &umv->umv_cache;
2385*7c478bd9Sstevel@tonic-gate 	int besilent = umv->umv_besilent;
2386*7c478bd9Sstevel@tonic-gate 
2387*7c478bd9Sstevel@tonic-gate 	/*LINTED*/
2388*7c478bd9Sstevel@tonic-gate 	buftagp = UMEM_BUFTAG(cp, buf);
2389*7c478bd9Sstevel@tonic-gate 
2390*7c478bd9Sstevel@tonic-gate 	/*
2391*7c478bd9Sstevel@tonic-gate 	 * Read the buffer to check.
2392*7c478bd9Sstevel@tonic-gate 	 */
2393*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(buf, umv->umv_size, addr) == -1) {
2394*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2395*7c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read %p", addr);
2396*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
2397*7c478bd9Sstevel@tonic-gate 	}
2398*7c478bd9Sstevel@tonic-gate 
2399*7c478bd9Sstevel@tonic-gate 	if ((corrupt = verify_pattern(buf, cp->cache_verify,
2400*7c478bd9Sstevel@tonic-gate 	    UMEM_FREE_PATTERN)) >= 0) {
2401*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2402*7c478bd9Sstevel@tonic-gate 			mdb_printf("buffer %p (free) seems corrupted, at %p\n",
2403*7c478bd9Sstevel@tonic-gate 			    addr, (uintptr_t)addr + corrupt);
2404*7c478bd9Sstevel@tonic-gate 		goto corrupt;
2405*7c478bd9Sstevel@tonic-gate 	}
2406*7c478bd9Sstevel@tonic-gate 
2407*7c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & UMF_HASH) &&
2408*7c478bd9Sstevel@tonic-gate 	    buftagp->bt_redzone != UMEM_REDZONE_PATTERN) {
2409*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2410*7c478bd9Sstevel@tonic-gate 			mdb_printf("buffer %p (free) seems to "
2411*7c478bd9Sstevel@tonic-gate 			    "have a corrupt redzone pattern\n", addr);
2412*7c478bd9Sstevel@tonic-gate 		goto corrupt;
2413*7c478bd9Sstevel@tonic-gate 	}
2414*7c478bd9Sstevel@tonic-gate 
2415*7c478bd9Sstevel@tonic-gate 	/*
2416*7c478bd9Sstevel@tonic-gate 	 * confirm bufctl pointer integrity.
2417*7c478bd9Sstevel@tonic-gate 	 */
2418*7c478bd9Sstevel@tonic-gate 	if (verify_buftag(buftagp, UMEM_BUFTAG_FREE) == -1) {
2419*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2420*7c478bd9Sstevel@tonic-gate 			mdb_printf("buffer %p (free) has a corrupt "
2421*7c478bd9Sstevel@tonic-gate 			    "buftag\n", addr);
2422*7c478bd9Sstevel@tonic-gate 		goto corrupt;
2423*7c478bd9Sstevel@tonic-gate 	}
2424*7c478bd9Sstevel@tonic-gate 
2425*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2426*7c478bd9Sstevel@tonic-gate corrupt:
2427*7c478bd9Sstevel@tonic-gate 	umv->umv_corruption++;
2428*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2429*7c478bd9Sstevel@tonic-gate }
2430*7c478bd9Sstevel@tonic-gate 
2431*7c478bd9Sstevel@tonic-gate /*
2432*7c478bd9Sstevel@tonic-gate  * verify_alloc()
2433*7c478bd9Sstevel@tonic-gate  *	Verify that the buftag of an allocated buffer makes sense with respect
2434*7c478bd9Sstevel@tonic-gate  *	to the buffer.
2435*7c478bd9Sstevel@tonic-gate  */
2436*7c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2437*7c478bd9Sstevel@tonic-gate static int
2438*7c478bd9Sstevel@tonic-gate verify_alloc(uintptr_t addr, const void *data, void *private)
2439*7c478bd9Sstevel@tonic-gate {
2440*7c478bd9Sstevel@tonic-gate 	umem_verify_t *umv = (umem_verify_t *)private;
2441*7c478bd9Sstevel@tonic-gate 	umem_cache_t *cp = &umv->umv_cache;
2442*7c478bd9Sstevel@tonic-gate 	uint64_t *buf = umv->umv_buf;	/* buf to validate */
2443*7c478bd9Sstevel@tonic-gate 	/*LINTED*/
2444*7c478bd9Sstevel@tonic-gate 	umem_buftag_t *buftagp = UMEM_BUFTAG(cp, buf);
2445*7c478bd9Sstevel@tonic-gate 	uint32_t *ip = (uint32_t *)buftagp;
2446*7c478bd9Sstevel@tonic-gate 	uint8_t *bp = (uint8_t *)buf;
2447*7c478bd9Sstevel@tonic-gate 	int looks_ok = 0, size_ok = 1;	/* flags for finding corruption */
2448*7c478bd9Sstevel@tonic-gate 	int besilent = umv->umv_besilent;
2449*7c478bd9Sstevel@tonic-gate 
2450*7c478bd9Sstevel@tonic-gate 	/*
2451*7c478bd9Sstevel@tonic-gate 	 * Read the buffer to check.
2452*7c478bd9Sstevel@tonic-gate 	 */
2453*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(buf, umv->umv_size, addr) == -1) {
2454*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2455*7c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read %p", addr);
2456*7c478bd9Sstevel@tonic-gate 		return (WALK_NEXT);
2457*7c478bd9Sstevel@tonic-gate 	}
2458*7c478bd9Sstevel@tonic-gate 
2459*7c478bd9Sstevel@tonic-gate 	/*
2460*7c478bd9Sstevel@tonic-gate 	 * There are two cases to handle:
2461*7c478bd9Sstevel@tonic-gate 	 * 1. If the buf was alloc'd using umem_cache_alloc, it will have
2462*7c478bd9Sstevel@tonic-gate 	 *    0xfeedfacefeedface at the end of it
2463*7c478bd9Sstevel@tonic-gate 	 * 2. If the buf was alloc'd using umem_alloc, it will have
2464*7c478bd9Sstevel@tonic-gate 	 *    0xbb just past the end of the region in use.  At the buftag,
2465*7c478bd9Sstevel@tonic-gate 	 *    it will have 0xfeedface (or, if the whole buffer is in use,
2466*7c478bd9Sstevel@tonic-gate 	 *    0xfeedface & bb000000 or 0xfeedfacf & 000000bb depending on
2467*7c478bd9Sstevel@tonic-gate 	 *    endianness), followed by 32 bits containing the offset of the
2468*7c478bd9Sstevel@tonic-gate 	 *    0xbb byte in the buffer.
2469*7c478bd9Sstevel@tonic-gate 	 *
2470*7c478bd9Sstevel@tonic-gate 	 * Finally, the two 32-bit words that comprise the second half of the
2471*7c478bd9Sstevel@tonic-gate 	 * buftag should xor to UMEM_BUFTAG_ALLOC
2472*7c478bd9Sstevel@tonic-gate 	 */
2473*7c478bd9Sstevel@tonic-gate 
2474*7c478bd9Sstevel@tonic-gate 	if (buftagp->bt_redzone == UMEM_REDZONE_PATTERN)
2475*7c478bd9Sstevel@tonic-gate 		looks_ok = 1;
2476*7c478bd9Sstevel@tonic-gate 	else if (!UMEM_SIZE_VALID(ip[1]))
2477*7c478bd9Sstevel@tonic-gate 		size_ok = 0;
2478*7c478bd9Sstevel@tonic-gate 	else if (bp[UMEM_SIZE_DECODE(ip[1])] == UMEM_REDZONE_BYTE)
2479*7c478bd9Sstevel@tonic-gate 		looks_ok = 1;
2480*7c478bd9Sstevel@tonic-gate 	else
2481*7c478bd9Sstevel@tonic-gate 		size_ok = 0;
2482*7c478bd9Sstevel@tonic-gate 
2483*7c478bd9Sstevel@tonic-gate 	if (!size_ok) {
2484*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2485*7c478bd9Sstevel@tonic-gate 			mdb_printf("buffer %p (allocated) has a corrupt "
2486*7c478bd9Sstevel@tonic-gate 			    "redzone size encoding\n", addr);
2487*7c478bd9Sstevel@tonic-gate 		goto corrupt;
2488*7c478bd9Sstevel@tonic-gate 	}
2489*7c478bd9Sstevel@tonic-gate 
2490*7c478bd9Sstevel@tonic-gate 	if (!looks_ok) {
2491*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2492*7c478bd9Sstevel@tonic-gate 			mdb_printf("buffer %p (allocated) has a corrupt "
2493*7c478bd9Sstevel@tonic-gate 			    "redzone signature\n", addr);
2494*7c478bd9Sstevel@tonic-gate 		goto corrupt;
2495*7c478bd9Sstevel@tonic-gate 	}
2496*7c478bd9Sstevel@tonic-gate 
2497*7c478bd9Sstevel@tonic-gate 	if (verify_buftag(buftagp, UMEM_BUFTAG_ALLOC) == -1) {
2498*7c478bd9Sstevel@tonic-gate 		if (!besilent)
2499*7c478bd9Sstevel@tonic-gate 			mdb_printf("buffer %p (allocated) has a "
2500*7c478bd9Sstevel@tonic-gate 			    "corrupt buftag\n", addr);
2501*7c478bd9Sstevel@tonic-gate 		goto corrupt;
2502*7c478bd9Sstevel@tonic-gate 	}
2503*7c478bd9Sstevel@tonic-gate 
2504*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2505*7c478bd9Sstevel@tonic-gate corrupt:
2506*7c478bd9Sstevel@tonic-gate 	umv->umv_corruption++;
2507*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2508*7c478bd9Sstevel@tonic-gate }
2509*7c478bd9Sstevel@tonic-gate 
2510*7c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
2511*7c478bd9Sstevel@tonic-gate int
2512*7c478bd9Sstevel@tonic-gate umem_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2513*7c478bd9Sstevel@tonic-gate {
2514*7c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC) {
2515*7c478bd9Sstevel@tonic-gate 		int check_alloc = 0, check_free = 0;
2516*7c478bd9Sstevel@tonic-gate 		umem_verify_t umv;
2517*7c478bd9Sstevel@tonic-gate 
2518*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&umv.umv_cache, sizeof (umv.umv_cache),
2519*7c478bd9Sstevel@tonic-gate 		    addr) == -1) {
2520*7c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read umem_cache %p", addr);
2521*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2522*7c478bd9Sstevel@tonic-gate 		}
2523*7c478bd9Sstevel@tonic-gate 
2524*7c478bd9Sstevel@tonic-gate 		umv.umv_size = umv.umv_cache.cache_buftag +
2525*7c478bd9Sstevel@tonic-gate 		    sizeof (umem_buftag_t);
2526*7c478bd9Sstevel@tonic-gate 		umv.umv_buf = mdb_alloc(umv.umv_size, UM_SLEEP | UM_GC);
2527*7c478bd9Sstevel@tonic-gate 		umv.umv_corruption = 0;
2528*7c478bd9Sstevel@tonic-gate 
2529*7c478bd9Sstevel@tonic-gate 		if ((umv.umv_cache.cache_flags & UMF_REDZONE)) {
2530*7c478bd9Sstevel@tonic-gate 			check_alloc = 1;
2531*7c478bd9Sstevel@tonic-gate 			if (umv.umv_cache.cache_flags & UMF_DEADBEEF)
2532*7c478bd9Sstevel@tonic-gate 				check_free = 1;
2533*7c478bd9Sstevel@tonic-gate 		} else {
2534*7c478bd9Sstevel@tonic-gate 			if (!(flags & DCMD_LOOP)) {
2535*7c478bd9Sstevel@tonic-gate 				mdb_warn("cache %p (%s) does not have "
2536*7c478bd9Sstevel@tonic-gate 				    "redzone checking enabled\n", addr,
2537*7c478bd9Sstevel@tonic-gate 				    umv.umv_cache.cache_name);
2538*7c478bd9Sstevel@tonic-gate 			}
2539*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2540*7c478bd9Sstevel@tonic-gate 		}
2541*7c478bd9Sstevel@tonic-gate 
2542*7c478bd9Sstevel@tonic-gate 		if (flags & DCMD_LOOP) {
2543*7c478bd9Sstevel@tonic-gate 			/*
2544*7c478bd9Sstevel@tonic-gate 			 * table mode, don't print out every corrupt buffer
2545*7c478bd9Sstevel@tonic-gate 			 */
2546*7c478bd9Sstevel@tonic-gate 			umv.umv_besilent = 1;
2547*7c478bd9Sstevel@tonic-gate 		} else {
2548*7c478bd9Sstevel@tonic-gate 			mdb_printf("Summary for cache '%s'\n",
2549*7c478bd9Sstevel@tonic-gate 			    umv.umv_cache.cache_name);
2550*7c478bd9Sstevel@tonic-gate 			mdb_inc_indent(2);
2551*7c478bd9Sstevel@tonic-gate 			umv.umv_besilent = 0;
2552*7c478bd9Sstevel@tonic-gate 		}
2553*7c478bd9Sstevel@tonic-gate 
2554*7c478bd9Sstevel@tonic-gate 		if (check_alloc)
2555*7c478bd9Sstevel@tonic-gate 			(void) mdb_pwalk("umem", verify_alloc, &umv, addr);
2556*7c478bd9Sstevel@tonic-gate 		if (check_free)
2557*7c478bd9Sstevel@tonic-gate 			(void) mdb_pwalk("freemem", verify_free, &umv, addr);
2558*7c478bd9Sstevel@tonic-gate 
2559*7c478bd9Sstevel@tonic-gate 		if (flags & DCMD_LOOP) {
2560*7c478bd9Sstevel@tonic-gate 			if (umv.umv_corruption == 0) {
2561*7c478bd9Sstevel@tonic-gate 				mdb_printf("%-*s %?p clean\n",
2562*7c478bd9Sstevel@tonic-gate 				    UMEM_CACHE_NAMELEN,
2563*7c478bd9Sstevel@tonic-gate 				    umv.umv_cache.cache_name, addr);
2564*7c478bd9Sstevel@tonic-gate 			} else {
2565*7c478bd9Sstevel@tonic-gate 				char *s = "";	/* optional s in "buffer[s]" */
2566*7c478bd9Sstevel@tonic-gate 				if (umv.umv_corruption > 1)
2567*7c478bd9Sstevel@tonic-gate 					s = "s";
2568*7c478bd9Sstevel@tonic-gate 
2569*7c478bd9Sstevel@tonic-gate 				mdb_printf("%-*s %?p %d corrupt buffer%s\n",
2570*7c478bd9Sstevel@tonic-gate 				    UMEM_CACHE_NAMELEN,
2571*7c478bd9Sstevel@tonic-gate 				    umv.umv_cache.cache_name, addr,
2572*7c478bd9Sstevel@tonic-gate 				    umv.umv_corruption, s);
2573*7c478bd9Sstevel@tonic-gate 			}
2574*7c478bd9Sstevel@tonic-gate 		} else {
2575*7c478bd9Sstevel@tonic-gate 			/*
2576*7c478bd9Sstevel@tonic-gate 			 * This is the more verbose mode, when the user has
2577*7c478bd9Sstevel@tonic-gate 			 * type addr::umem_verify.  If the cache was clean,
2578*7c478bd9Sstevel@tonic-gate 			 * nothing will have yet been printed. So say something.
2579*7c478bd9Sstevel@tonic-gate 			 */
2580*7c478bd9Sstevel@tonic-gate 			if (umv.umv_corruption == 0)
2581*7c478bd9Sstevel@tonic-gate 				mdb_printf("clean\n");
2582*7c478bd9Sstevel@tonic-gate 
2583*7c478bd9Sstevel@tonic-gate 			mdb_dec_indent(2);
2584*7c478bd9Sstevel@tonic-gate 		}
2585*7c478bd9Sstevel@tonic-gate 	} else {
2586*7c478bd9Sstevel@tonic-gate 		/*
2587*7c478bd9Sstevel@tonic-gate 		 * If the user didn't specify a cache to verify, we'll walk all
2588*7c478bd9Sstevel@tonic-gate 		 * umem_cache's, specifying ourself as a callback for each...
2589*7c478bd9Sstevel@tonic-gate 		 * this is the equivalent of '::walk umem_cache .::umem_verify'
2590*7c478bd9Sstevel@tonic-gate 		 */
2591*7c478bd9Sstevel@tonic-gate 		mdb_printf("%<u>%-*s %-?s %-20s%</b>\n", UMEM_CACHE_NAMELEN,
2592*7c478bd9Sstevel@tonic-gate 		    "Cache Name", "Addr", "Cache Integrity");
2593*7c478bd9Sstevel@tonic-gate 		(void) (mdb_walk_dcmd("umem_cache", "umem_verify", 0, NULL));
2594*7c478bd9Sstevel@tonic-gate 	}
2595*7c478bd9Sstevel@tonic-gate 
2596*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2597*7c478bd9Sstevel@tonic-gate }
2598*7c478bd9Sstevel@tonic-gate 
2599*7c478bd9Sstevel@tonic-gate typedef struct vmem_node {
2600*7c478bd9Sstevel@tonic-gate 	struct vmem_node *vn_next;
2601*7c478bd9Sstevel@tonic-gate 	struct vmem_node *vn_parent;
2602*7c478bd9Sstevel@tonic-gate 	struct vmem_node *vn_sibling;
2603*7c478bd9Sstevel@tonic-gate 	struct vmem_node *vn_children;
2604*7c478bd9Sstevel@tonic-gate 	uintptr_t vn_addr;
2605*7c478bd9Sstevel@tonic-gate 	int vn_marked;
2606*7c478bd9Sstevel@tonic-gate 	vmem_t vn_vmem;
2607*7c478bd9Sstevel@tonic-gate } vmem_node_t;
2608*7c478bd9Sstevel@tonic-gate 
2609*7c478bd9Sstevel@tonic-gate typedef struct vmem_walk {
2610*7c478bd9Sstevel@tonic-gate 	vmem_node_t *vw_root;
2611*7c478bd9Sstevel@tonic-gate 	vmem_node_t *vw_current;
2612*7c478bd9Sstevel@tonic-gate } vmem_walk_t;
2613*7c478bd9Sstevel@tonic-gate 
2614*7c478bd9Sstevel@tonic-gate int
2615*7c478bd9Sstevel@tonic-gate vmem_walk_init(mdb_walk_state_t *wsp)
2616*7c478bd9Sstevel@tonic-gate {
2617*7c478bd9Sstevel@tonic-gate 	uintptr_t vaddr, paddr;
2618*7c478bd9Sstevel@tonic-gate 	vmem_node_t *head = NULL, *root = NULL, *current = NULL, *parent, *vp;
2619*7c478bd9Sstevel@tonic-gate 	vmem_walk_t *vw;
2620*7c478bd9Sstevel@tonic-gate 
2621*7c478bd9Sstevel@tonic-gate 	if (umem_readvar(&vaddr, "vmem_list") == -1) {
2622*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read 'vmem_list'");
2623*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
2624*7c478bd9Sstevel@tonic-gate 	}
2625*7c478bd9Sstevel@tonic-gate 
2626*7c478bd9Sstevel@tonic-gate 	while (vaddr != NULL) {
2627*7c478bd9Sstevel@tonic-gate 		vp = mdb_zalloc(sizeof (vmem_node_t), UM_SLEEP);
2628*7c478bd9Sstevel@tonic-gate 		vp->vn_addr = vaddr;
2629*7c478bd9Sstevel@tonic-gate 		vp->vn_next = head;
2630*7c478bd9Sstevel@tonic-gate 		head = vp;
2631*7c478bd9Sstevel@tonic-gate 
2632*7c478bd9Sstevel@tonic-gate 		if (vaddr == wsp->walk_addr)
2633*7c478bd9Sstevel@tonic-gate 			current = vp;
2634*7c478bd9Sstevel@tonic-gate 
2635*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&vp->vn_vmem, sizeof (vmem_t), vaddr) == -1) {
2636*7c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't read vmem_t at %p", vaddr);
2637*7c478bd9Sstevel@tonic-gate 			goto err;
2638*7c478bd9Sstevel@tonic-gate 		}
2639*7c478bd9Sstevel@tonic-gate 
2640*7c478bd9Sstevel@tonic-gate 		vaddr = (uintptr_t)vp->vn_vmem.vm_next;
2641*7c478bd9Sstevel@tonic-gate 	}
2642*7c478bd9Sstevel@tonic-gate 
2643*7c478bd9Sstevel@tonic-gate 	for (vp = head; vp != NULL; vp = vp->vn_next) {
2644*7c478bd9Sstevel@tonic-gate 
2645*7c478bd9Sstevel@tonic-gate 		if ((paddr = (uintptr_t)vp->vn_vmem.vm_source) == NULL) {
2646*7c478bd9Sstevel@tonic-gate 			vp->vn_sibling = root;
2647*7c478bd9Sstevel@tonic-gate 			root = vp;
2648*7c478bd9Sstevel@tonic-gate 			continue;
2649*7c478bd9Sstevel@tonic-gate 		}
2650*7c478bd9Sstevel@tonic-gate 
2651*7c478bd9Sstevel@tonic-gate 		for (parent = head; parent != NULL; parent = parent->vn_next) {
2652*7c478bd9Sstevel@tonic-gate 			if (parent->vn_addr != paddr)
2653*7c478bd9Sstevel@tonic-gate 				continue;
2654*7c478bd9Sstevel@tonic-gate 			vp->vn_sibling = parent->vn_children;
2655*7c478bd9Sstevel@tonic-gate 			parent->vn_children = vp;
2656*7c478bd9Sstevel@tonic-gate 			vp->vn_parent = parent;
2657*7c478bd9Sstevel@tonic-gate 			break;
2658*7c478bd9Sstevel@tonic-gate 		}
2659*7c478bd9Sstevel@tonic-gate 
2660*7c478bd9Sstevel@tonic-gate 		if (parent == NULL) {
2661*7c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't find %p's parent (%p)\n",
2662*7c478bd9Sstevel@tonic-gate 			    vp->vn_addr, paddr);
2663*7c478bd9Sstevel@tonic-gate 			goto err;
2664*7c478bd9Sstevel@tonic-gate 		}
2665*7c478bd9Sstevel@tonic-gate 	}
2666*7c478bd9Sstevel@tonic-gate 
2667*7c478bd9Sstevel@tonic-gate 	vw = mdb_zalloc(sizeof (vmem_walk_t), UM_SLEEP);
2668*7c478bd9Sstevel@tonic-gate 	vw->vw_root = root;
2669*7c478bd9Sstevel@tonic-gate 
2670*7c478bd9Sstevel@tonic-gate 	if (current != NULL)
2671*7c478bd9Sstevel@tonic-gate 		vw->vw_current = current;
2672*7c478bd9Sstevel@tonic-gate 	else
2673*7c478bd9Sstevel@tonic-gate 		vw->vw_current = root;
2674*7c478bd9Sstevel@tonic-gate 
2675*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = vw;
2676*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2677*7c478bd9Sstevel@tonic-gate err:
2678*7c478bd9Sstevel@tonic-gate 	for (vp = head; head != NULL; vp = head) {
2679*7c478bd9Sstevel@tonic-gate 		head = vp->vn_next;
2680*7c478bd9Sstevel@tonic-gate 		mdb_free(vp, sizeof (vmem_node_t));
2681*7c478bd9Sstevel@tonic-gate 	}
2682*7c478bd9Sstevel@tonic-gate 
2683*7c478bd9Sstevel@tonic-gate 	return (WALK_ERR);
2684*7c478bd9Sstevel@tonic-gate }
2685*7c478bd9Sstevel@tonic-gate 
2686*7c478bd9Sstevel@tonic-gate int
2687*7c478bd9Sstevel@tonic-gate vmem_walk_step(mdb_walk_state_t *wsp)
2688*7c478bd9Sstevel@tonic-gate {
2689*7c478bd9Sstevel@tonic-gate 	vmem_walk_t *vw = wsp->walk_data;
2690*7c478bd9Sstevel@tonic-gate 	vmem_node_t *vp;
2691*7c478bd9Sstevel@tonic-gate 	int rval;
2692*7c478bd9Sstevel@tonic-gate 
2693*7c478bd9Sstevel@tonic-gate 	if ((vp = vw->vw_current) == NULL)
2694*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
2695*7c478bd9Sstevel@tonic-gate 
2696*7c478bd9Sstevel@tonic-gate 	rval = wsp->walk_callback(vp->vn_addr, &vp->vn_vmem, wsp->walk_cbdata);
2697*7c478bd9Sstevel@tonic-gate 
2698*7c478bd9Sstevel@tonic-gate 	if (vp->vn_children != NULL) {
2699*7c478bd9Sstevel@tonic-gate 		vw->vw_current = vp->vn_children;
2700*7c478bd9Sstevel@tonic-gate 		return (rval);
2701*7c478bd9Sstevel@tonic-gate 	}
2702*7c478bd9Sstevel@tonic-gate 
2703*7c478bd9Sstevel@tonic-gate 	do {
2704*7c478bd9Sstevel@tonic-gate 		vw->vw_current = vp->vn_sibling;
2705*7c478bd9Sstevel@tonic-gate 		vp = vp->vn_parent;
2706*7c478bd9Sstevel@tonic-gate 	} while (vw->vw_current == NULL && vp != NULL);
2707*7c478bd9Sstevel@tonic-gate 
2708*7c478bd9Sstevel@tonic-gate 	return (rval);
2709*7c478bd9Sstevel@tonic-gate }
2710*7c478bd9Sstevel@tonic-gate 
2711*7c478bd9Sstevel@tonic-gate /*
2712*7c478bd9Sstevel@tonic-gate  * The "vmem_postfix" walk walks the vmem arenas in post-fix order; all
2713*7c478bd9Sstevel@tonic-gate  * children are visited before their parent.  We perform the postfix walk
2714*7c478bd9Sstevel@tonic-gate  * iteratively (rather than recursively) to allow mdb to regain control
2715*7c478bd9Sstevel@tonic-gate  * after each callback.
2716*7c478bd9Sstevel@tonic-gate  */
2717*7c478bd9Sstevel@tonic-gate int
2718*7c478bd9Sstevel@tonic-gate vmem_postfix_walk_step(mdb_walk_state_t *wsp)
2719*7c478bd9Sstevel@tonic-gate {
2720*7c478bd9Sstevel@tonic-gate 	vmem_walk_t *vw = wsp->walk_data;
2721*7c478bd9Sstevel@tonic-gate 	vmem_node_t *vp = vw->vw_current;
2722*7c478bd9Sstevel@tonic-gate 	int rval;
2723*7c478bd9Sstevel@tonic-gate 
2724*7c478bd9Sstevel@tonic-gate 	/*
2725*7c478bd9Sstevel@tonic-gate 	 * If this node is marked, then we know that we have already visited
2726*7c478bd9Sstevel@tonic-gate 	 * all of its children.  If the node has any siblings, they need to
2727*7c478bd9Sstevel@tonic-gate 	 * be visited next; otherwise, we need to visit the parent.  Note
2728*7c478bd9Sstevel@tonic-gate 	 * that vp->vn_marked will only be zero on the first invocation of
2729*7c478bd9Sstevel@tonic-gate 	 * the step function.
2730*7c478bd9Sstevel@tonic-gate 	 */
2731*7c478bd9Sstevel@tonic-gate 	if (vp->vn_marked) {
2732*7c478bd9Sstevel@tonic-gate 		if (vp->vn_sibling != NULL)
2733*7c478bd9Sstevel@tonic-gate 			vp = vp->vn_sibling;
2734*7c478bd9Sstevel@tonic-gate 		else if (vp->vn_parent != NULL)
2735*7c478bd9Sstevel@tonic-gate 			vp = vp->vn_parent;
2736*7c478bd9Sstevel@tonic-gate 		else {
2737*7c478bd9Sstevel@tonic-gate 			/*
2738*7c478bd9Sstevel@tonic-gate 			 * We have neither a parent, nor a sibling, and we
2739*7c478bd9Sstevel@tonic-gate 			 * have already been visited; we're done.
2740*7c478bd9Sstevel@tonic-gate 			 */
2741*7c478bd9Sstevel@tonic-gate 			return (WALK_DONE);
2742*7c478bd9Sstevel@tonic-gate 		}
2743*7c478bd9Sstevel@tonic-gate 	}
2744*7c478bd9Sstevel@tonic-gate 
2745*7c478bd9Sstevel@tonic-gate 	/*
2746*7c478bd9Sstevel@tonic-gate 	 * Before we visit this node, visit its children.
2747*7c478bd9Sstevel@tonic-gate 	 */
2748*7c478bd9Sstevel@tonic-gate 	while (vp->vn_children != NULL && !vp->vn_children->vn_marked)
2749*7c478bd9Sstevel@tonic-gate 		vp = vp->vn_children;
2750*7c478bd9Sstevel@tonic-gate 
2751*7c478bd9Sstevel@tonic-gate 	vp->vn_marked = 1;
2752*7c478bd9Sstevel@tonic-gate 	vw->vw_current = vp;
2753*7c478bd9Sstevel@tonic-gate 	rval = wsp->walk_callback(vp->vn_addr, &vp->vn_vmem, wsp->walk_cbdata);
2754*7c478bd9Sstevel@tonic-gate 
2755*7c478bd9Sstevel@tonic-gate 	return (rval);
2756*7c478bd9Sstevel@tonic-gate }
2757*7c478bd9Sstevel@tonic-gate 
2758*7c478bd9Sstevel@tonic-gate void
2759*7c478bd9Sstevel@tonic-gate vmem_walk_fini(mdb_walk_state_t *wsp)
2760*7c478bd9Sstevel@tonic-gate {
2761*7c478bd9Sstevel@tonic-gate 	vmem_walk_t *vw = wsp->walk_data;
2762*7c478bd9Sstevel@tonic-gate 	vmem_node_t *root = vw->vw_root;
2763*7c478bd9Sstevel@tonic-gate 	int done;
2764*7c478bd9Sstevel@tonic-gate 
2765*7c478bd9Sstevel@tonic-gate 	if (root == NULL)
2766*7c478bd9Sstevel@tonic-gate 		return;
2767*7c478bd9Sstevel@tonic-gate 
2768*7c478bd9Sstevel@tonic-gate 	if ((vw->vw_root = root->vn_children) != NULL)
2769*7c478bd9Sstevel@tonic-gate 		vmem_walk_fini(wsp);
2770*7c478bd9Sstevel@tonic-gate 
2771*7c478bd9Sstevel@tonic-gate 	vw->vw_root = root->vn_sibling;
2772*7c478bd9Sstevel@tonic-gate 	done = (root->vn_sibling == NULL && root->vn_parent == NULL);
2773*7c478bd9Sstevel@tonic-gate 	mdb_free(root, sizeof (vmem_node_t));
2774*7c478bd9Sstevel@tonic-gate 
2775*7c478bd9Sstevel@tonic-gate 	if (done) {
2776*7c478bd9Sstevel@tonic-gate 		mdb_free(vw, sizeof (vmem_walk_t));
2777*7c478bd9Sstevel@tonic-gate 	} else {
2778*7c478bd9Sstevel@tonic-gate 		vmem_walk_fini(wsp);
2779*7c478bd9Sstevel@tonic-gate 	}
2780*7c478bd9Sstevel@tonic-gate }
2781*7c478bd9Sstevel@tonic-gate 
2782*7c478bd9Sstevel@tonic-gate typedef struct vmem_seg_walk {
2783*7c478bd9Sstevel@tonic-gate 	uint8_t vsw_type;
2784*7c478bd9Sstevel@tonic-gate 	uintptr_t vsw_start;
2785*7c478bd9Sstevel@tonic-gate 	uintptr_t vsw_current;
2786*7c478bd9Sstevel@tonic-gate } vmem_seg_walk_t;
2787*7c478bd9Sstevel@tonic-gate 
2788*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2789*7c478bd9Sstevel@tonic-gate int
2790*7c478bd9Sstevel@tonic-gate vmem_seg_walk_common_init(mdb_walk_state_t *wsp, uint8_t type, char *name)
2791*7c478bd9Sstevel@tonic-gate {
2792*7c478bd9Sstevel@tonic-gate 	vmem_seg_walk_t *vsw;
2793*7c478bd9Sstevel@tonic-gate 
2794*7c478bd9Sstevel@tonic-gate 	if (wsp->walk_addr == NULL) {
2795*7c478bd9Sstevel@tonic-gate 		mdb_warn("vmem_%s does not support global walks\n", name);
2796*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
2797*7c478bd9Sstevel@tonic-gate 	}
2798*7c478bd9Sstevel@tonic-gate 
2799*7c478bd9Sstevel@tonic-gate 	wsp->walk_data = vsw = mdb_alloc(sizeof (vmem_seg_walk_t), UM_SLEEP);
2800*7c478bd9Sstevel@tonic-gate 
2801*7c478bd9Sstevel@tonic-gate 	vsw->vsw_type = type;
2802*7c478bd9Sstevel@tonic-gate 	vsw->vsw_start = wsp->walk_addr + OFFSETOF(vmem_t, vm_seg0);
2803*7c478bd9Sstevel@tonic-gate 	vsw->vsw_current = vsw->vsw_start;
2804*7c478bd9Sstevel@tonic-gate 
2805*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
2806*7c478bd9Sstevel@tonic-gate }
2807*7c478bd9Sstevel@tonic-gate 
2808*7c478bd9Sstevel@tonic-gate /*
2809*7c478bd9Sstevel@tonic-gate  * vmem segments can't have type 0 (this should be added to vmem_impl.h).
2810*7c478bd9Sstevel@tonic-gate  */
2811*7c478bd9Sstevel@tonic-gate #define	VMEM_NONE	0
2812*7c478bd9Sstevel@tonic-gate 
2813*7c478bd9Sstevel@tonic-gate int
2814*7c478bd9Sstevel@tonic-gate vmem_alloc_walk_init(mdb_walk_state_t *wsp)
2815*7c478bd9Sstevel@tonic-gate {
2816*7c478bd9Sstevel@tonic-gate 	return (vmem_seg_walk_common_init(wsp, VMEM_ALLOC, "alloc"));
2817*7c478bd9Sstevel@tonic-gate }
2818*7c478bd9Sstevel@tonic-gate 
2819*7c478bd9Sstevel@tonic-gate int
2820*7c478bd9Sstevel@tonic-gate vmem_free_walk_init(mdb_walk_state_t *wsp)
2821*7c478bd9Sstevel@tonic-gate {
2822*7c478bd9Sstevel@tonic-gate 	return (vmem_seg_walk_common_init(wsp, VMEM_FREE, "free"));
2823*7c478bd9Sstevel@tonic-gate }
2824*7c478bd9Sstevel@tonic-gate 
2825*7c478bd9Sstevel@tonic-gate int
2826*7c478bd9Sstevel@tonic-gate vmem_span_walk_init(mdb_walk_state_t *wsp)
2827*7c478bd9Sstevel@tonic-gate {
2828*7c478bd9Sstevel@tonic-gate 	return (vmem_seg_walk_common_init(wsp, VMEM_SPAN, "span"));
2829*7c478bd9Sstevel@tonic-gate }
2830*7c478bd9Sstevel@tonic-gate 
2831*7c478bd9Sstevel@tonic-gate int
2832*7c478bd9Sstevel@tonic-gate vmem_seg_walk_init(mdb_walk_state_t *wsp)
2833*7c478bd9Sstevel@tonic-gate {
2834*7c478bd9Sstevel@tonic-gate 	return (vmem_seg_walk_common_init(wsp, VMEM_NONE, "seg"));
2835*7c478bd9Sstevel@tonic-gate }
2836*7c478bd9Sstevel@tonic-gate 
2837*7c478bd9Sstevel@tonic-gate int
2838*7c478bd9Sstevel@tonic-gate vmem_seg_walk_step(mdb_walk_state_t *wsp)
2839*7c478bd9Sstevel@tonic-gate {
2840*7c478bd9Sstevel@tonic-gate 	vmem_seg_t seg;
2841*7c478bd9Sstevel@tonic-gate 	vmem_seg_walk_t *vsw = wsp->walk_data;
2842*7c478bd9Sstevel@tonic-gate 	uintptr_t addr = vsw->vsw_current;
2843*7c478bd9Sstevel@tonic-gate 	static size_t seg_size = 0;
2844*7c478bd9Sstevel@tonic-gate 	int rval;
2845*7c478bd9Sstevel@tonic-gate 
2846*7c478bd9Sstevel@tonic-gate 	if (!seg_size) {
2847*7c478bd9Sstevel@tonic-gate 		if (umem_readvar(&seg_size, "vmem_seg_size") == -1) {
2848*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read 'vmem_seg_size'");
2849*7c478bd9Sstevel@tonic-gate 			seg_size = sizeof (vmem_seg_t);
2850*7c478bd9Sstevel@tonic-gate 		}
2851*7c478bd9Sstevel@tonic-gate 	}
2852*7c478bd9Sstevel@tonic-gate 
2853*7c478bd9Sstevel@tonic-gate 	if (seg_size < sizeof (seg))
2854*7c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&seg + seg_size, sizeof (seg) - seg_size);
2855*7c478bd9Sstevel@tonic-gate 
2856*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&seg, seg_size, addr) == -1) {
2857*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read vmem_seg at %p", addr);
2858*7c478bd9Sstevel@tonic-gate 		return (WALK_ERR);
2859*7c478bd9Sstevel@tonic-gate 	}
2860*7c478bd9Sstevel@tonic-gate 
2861*7c478bd9Sstevel@tonic-gate 	vsw->vsw_current = (uintptr_t)seg.vs_anext;
2862*7c478bd9Sstevel@tonic-gate 	if (vsw->vsw_type != VMEM_NONE && seg.vs_type != vsw->vsw_type) {
2863*7c478bd9Sstevel@tonic-gate 		rval = WALK_NEXT;
2864*7c478bd9Sstevel@tonic-gate 	} else {
2865*7c478bd9Sstevel@tonic-gate 		rval = wsp->walk_callback(addr, &seg, wsp->walk_cbdata);
2866*7c478bd9Sstevel@tonic-gate 	}
2867*7c478bd9Sstevel@tonic-gate 
2868*7c478bd9Sstevel@tonic-gate 	if (vsw->vsw_current == vsw->vsw_start)
2869*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
2870*7c478bd9Sstevel@tonic-gate 
2871*7c478bd9Sstevel@tonic-gate 	return (rval);
2872*7c478bd9Sstevel@tonic-gate }
2873*7c478bd9Sstevel@tonic-gate 
2874*7c478bd9Sstevel@tonic-gate void
2875*7c478bd9Sstevel@tonic-gate vmem_seg_walk_fini(mdb_walk_state_t *wsp)
2876*7c478bd9Sstevel@tonic-gate {
2877*7c478bd9Sstevel@tonic-gate 	vmem_seg_walk_t *vsw = wsp->walk_data;
2878*7c478bd9Sstevel@tonic-gate 
2879*7c478bd9Sstevel@tonic-gate 	mdb_free(vsw, sizeof (vmem_seg_walk_t));
2880*7c478bd9Sstevel@tonic-gate }
2881*7c478bd9Sstevel@tonic-gate 
2882*7c478bd9Sstevel@tonic-gate #define	VMEM_NAMEWIDTH	22
2883*7c478bd9Sstevel@tonic-gate 
2884*7c478bd9Sstevel@tonic-gate int
2885*7c478bd9Sstevel@tonic-gate vmem(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2886*7c478bd9Sstevel@tonic-gate {
2887*7c478bd9Sstevel@tonic-gate 	vmem_t v, parent;
2888*7c478bd9Sstevel@tonic-gate 	uintptr_t paddr;
2889*7c478bd9Sstevel@tonic-gate 	int ident = 0;
2890*7c478bd9Sstevel@tonic-gate 	char c[VMEM_NAMEWIDTH];
2891*7c478bd9Sstevel@tonic-gate 
2892*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2893*7c478bd9Sstevel@tonic-gate 		if (mdb_walk_dcmd("vmem", "vmem", argc, argv) == -1) {
2894*7c478bd9Sstevel@tonic-gate 			mdb_warn("can't walk vmem");
2895*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
2896*7c478bd9Sstevel@tonic-gate 		}
2897*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
2898*7c478bd9Sstevel@tonic-gate 	}
2899*7c478bd9Sstevel@tonic-gate 
2900*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags))
2901*7c478bd9Sstevel@tonic-gate 		mdb_printf("%-?s %-*s %10s %12s %9s %5s\n",
2902*7c478bd9Sstevel@tonic-gate 		    "ADDR", VMEM_NAMEWIDTH, "NAME", "INUSE",
2903*7c478bd9Sstevel@tonic-gate 		    "TOTAL", "SUCCEED", "FAIL");
2904*7c478bd9Sstevel@tonic-gate 
2905*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&v, sizeof (v), addr) == -1) {
2906*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read vmem at %p", addr);
2907*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2908*7c478bd9Sstevel@tonic-gate 	}
2909*7c478bd9Sstevel@tonic-gate 
2910*7c478bd9Sstevel@tonic-gate 	for (paddr = (uintptr_t)v.vm_source; paddr != NULL; ident += 2) {
2911*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
2912*7c478bd9Sstevel@tonic-gate 			mdb_warn("couldn't trace %p's ancestry", addr);
2913*7c478bd9Sstevel@tonic-gate 			ident = 0;
2914*7c478bd9Sstevel@tonic-gate 			break;
2915*7c478bd9Sstevel@tonic-gate 		}
2916*7c478bd9Sstevel@tonic-gate 		paddr = (uintptr_t)parent.vm_source;
2917*7c478bd9Sstevel@tonic-gate 	}
2918*7c478bd9Sstevel@tonic-gate 
2919*7c478bd9Sstevel@tonic-gate 	(void) mdb_snprintf(c, VMEM_NAMEWIDTH, "%*s%s", ident, "", v.vm_name);
2920*7c478bd9Sstevel@tonic-gate 
2921*7c478bd9Sstevel@tonic-gate 	mdb_printf("%0?p %-*s %10llu %12llu %9llu %5llu\n",
2922*7c478bd9Sstevel@tonic-gate 	    addr, VMEM_NAMEWIDTH, c,
2923*7c478bd9Sstevel@tonic-gate 	    v.vm_kstat.vk_mem_inuse, v.vm_kstat.vk_mem_total,
2924*7c478bd9Sstevel@tonic-gate 	    v.vm_kstat.vk_alloc, v.vm_kstat.vk_fail);
2925*7c478bd9Sstevel@tonic-gate 
2926*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
2927*7c478bd9Sstevel@tonic-gate }
2928*7c478bd9Sstevel@tonic-gate 
2929*7c478bd9Sstevel@tonic-gate void
2930*7c478bd9Sstevel@tonic-gate vmem_seg_help(void)
2931*7c478bd9Sstevel@tonic-gate {
2932*7c478bd9Sstevel@tonic-gate 	mdb_printf("%s\n",
2933*7c478bd9Sstevel@tonic-gate "Display the contents of vmem_seg_ts, with optional filtering.\n"
2934*7c478bd9Sstevel@tonic-gate "\n"
2935*7c478bd9Sstevel@tonic-gate "A vmem_seg_t represents a range of addresses (or arbitrary numbers),\n"
2936*7c478bd9Sstevel@tonic-gate "representing a single chunk of data.  Only ALLOC segments have debugging\n"
2937*7c478bd9Sstevel@tonic-gate "information.\n");
2938*7c478bd9Sstevel@tonic-gate 	mdb_dec_indent(2);
2939*7c478bd9Sstevel@tonic-gate 	mdb_printf("%<b>OPTIONS%</b>\n");
2940*7c478bd9Sstevel@tonic-gate 	mdb_inc_indent(2);
2941*7c478bd9Sstevel@tonic-gate 	mdb_printf("%s",
2942*7c478bd9Sstevel@tonic-gate "  -v    Display the full content of the vmem_seg, including its stack trace\n"
2943*7c478bd9Sstevel@tonic-gate "  -s    report the size of the segment, instead of the end address\n"
2944*7c478bd9Sstevel@tonic-gate "  -c caller\n"
2945*7c478bd9Sstevel@tonic-gate "        filter out segments without the function/PC in their stack trace\n"
2946*7c478bd9Sstevel@tonic-gate "  -e earliest\n"
2947*7c478bd9Sstevel@tonic-gate "        filter out segments timestamped before earliest\n"
2948*7c478bd9Sstevel@tonic-gate "  -l latest\n"
2949*7c478bd9Sstevel@tonic-gate "        filter out segments timestamped after latest\n"
2950*7c478bd9Sstevel@tonic-gate "  -m minsize\n"
2951*7c478bd9Sstevel@tonic-gate "        filer out segments smaller than minsize\n"
2952*7c478bd9Sstevel@tonic-gate "  -M maxsize\n"
2953*7c478bd9Sstevel@tonic-gate "        filer out segments larger than maxsize\n"
2954*7c478bd9Sstevel@tonic-gate "  -t thread\n"
2955*7c478bd9Sstevel@tonic-gate "        filter out segments not involving thread\n"
2956*7c478bd9Sstevel@tonic-gate "  -T type\n"
2957*7c478bd9Sstevel@tonic-gate "        filter out segments not of type 'type'\n"
2958*7c478bd9Sstevel@tonic-gate "        type is one of: ALLOC/FREE/SPAN/ROTOR/WALKER\n");
2959*7c478bd9Sstevel@tonic-gate }
2960*7c478bd9Sstevel@tonic-gate 
2961*7c478bd9Sstevel@tonic-gate 
2962*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2963*7c478bd9Sstevel@tonic-gate int
2964*7c478bd9Sstevel@tonic-gate vmem_seg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2965*7c478bd9Sstevel@tonic-gate {
2966*7c478bd9Sstevel@tonic-gate 	vmem_seg_t vs;
2967*7c478bd9Sstevel@tonic-gate 	uintptr_t *stk = vs.vs_stack;
2968*7c478bd9Sstevel@tonic-gate 	uintptr_t sz;
2969*7c478bd9Sstevel@tonic-gate 	uint8_t t;
2970*7c478bd9Sstevel@tonic-gate 	const char *type = NULL;
2971*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
2972*7c478bd9Sstevel@tonic-gate 	char c[MDB_SYM_NAMLEN];
2973*7c478bd9Sstevel@tonic-gate 	int no_debug;
2974*7c478bd9Sstevel@tonic-gate 	int i;
2975*7c478bd9Sstevel@tonic-gate 	int depth;
2976*7c478bd9Sstevel@tonic-gate 	uintptr_t laddr, haddr;
2977*7c478bd9Sstevel@tonic-gate 
2978*7c478bd9Sstevel@tonic-gate 	uintptr_t caller = NULL, thread = NULL;
2979*7c478bd9Sstevel@tonic-gate 	uintptr_t minsize = 0, maxsize = 0;
2980*7c478bd9Sstevel@tonic-gate 
2981*7c478bd9Sstevel@tonic-gate 	hrtime_t earliest = 0, latest = 0;
2982*7c478bd9Sstevel@tonic-gate 
2983*7c478bd9Sstevel@tonic-gate 	uint_t size = 0;
2984*7c478bd9Sstevel@tonic-gate 	uint_t verbose = 0;
2985*7c478bd9Sstevel@tonic-gate 
2986*7c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC))
2987*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2988*7c478bd9Sstevel@tonic-gate 
2989*7c478bd9Sstevel@tonic-gate 	if (mdb_getopts(argc, argv,
2990*7c478bd9Sstevel@tonic-gate 	    'c', MDB_OPT_UINTPTR, &caller,
2991*7c478bd9Sstevel@tonic-gate 	    'e', MDB_OPT_UINT64, &earliest,
2992*7c478bd9Sstevel@tonic-gate 	    'l', MDB_OPT_UINT64, &latest,
2993*7c478bd9Sstevel@tonic-gate 	    's', MDB_OPT_SETBITS, TRUE, &size,
2994*7c478bd9Sstevel@tonic-gate 	    'm', MDB_OPT_UINTPTR, &minsize,
2995*7c478bd9Sstevel@tonic-gate 	    'M', MDB_OPT_UINTPTR, &maxsize,
2996*7c478bd9Sstevel@tonic-gate 	    't', MDB_OPT_UINTPTR, &thread,
2997*7c478bd9Sstevel@tonic-gate 	    'T', MDB_OPT_STR, &type,
2998*7c478bd9Sstevel@tonic-gate 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
2999*7c478bd9Sstevel@tonic-gate 	    NULL) != argc)
3000*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3001*7c478bd9Sstevel@tonic-gate 
3002*7c478bd9Sstevel@tonic-gate 	if (DCMD_HDRSPEC(flags) && !(flags & DCMD_PIPE_OUT)) {
3003*7c478bd9Sstevel@tonic-gate 		if (verbose) {
3004*7c478bd9Sstevel@tonic-gate 			mdb_printf("%16s %4s %16s %16s %16s\n"
3005*7c478bd9Sstevel@tonic-gate 			    "%<u>%16s %4s %16s %16s %16s%</u>\n",
3006*7c478bd9Sstevel@tonic-gate 			    "ADDR", "TYPE", "START", "END", "SIZE",
3007*7c478bd9Sstevel@tonic-gate 			    "", "", "THREAD", "TIMESTAMP", "");
3008*7c478bd9Sstevel@tonic-gate 		} else {
3009*7c478bd9Sstevel@tonic-gate 			mdb_printf("%?s %4s %?s %?s %s\n", "ADDR", "TYPE",
3010*7c478bd9Sstevel@tonic-gate 			    "START", size? "SIZE" : "END", "WHO");
3011*7c478bd9Sstevel@tonic-gate 		}
3012*7c478bd9Sstevel@tonic-gate 	}
3013*7c478bd9Sstevel@tonic-gate 
3014*7c478bd9Sstevel@tonic-gate 	if (mdb_vread(&vs, sizeof (vs), addr) == -1) {
3015*7c478bd9Sstevel@tonic-gate 		mdb_warn("couldn't read vmem_seg at %p", addr);
3016*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3017*7c478bd9Sstevel@tonic-gate 	}
3018*7c478bd9Sstevel@tonic-gate 
3019*7c478bd9Sstevel@tonic-gate 	if (type != NULL) {
3020*7c478bd9Sstevel@tonic-gate 		if (strcmp(type, "ALLC") == 0 || strcmp(type, "ALLOC") == 0)
3021*7c478bd9Sstevel@tonic-gate 			t = VMEM_ALLOC;
3022*7c478bd9Sstevel@tonic-gate 		else if (strcmp(type, "FREE") == 0)
3023*7c478bd9Sstevel@tonic-gate 			t = VMEM_FREE;
3024*7c478bd9Sstevel@tonic-gate 		else if (strcmp(type, "SPAN") == 0)
3025*7c478bd9Sstevel@tonic-gate 			t = VMEM_SPAN;
3026*7c478bd9Sstevel@tonic-gate 		else if (strcmp(type, "ROTR") == 0 ||
3027*7c478bd9Sstevel@tonic-gate 		    strcmp(type, "ROTOR") == 0)
3028*7c478bd9Sstevel@tonic-gate 			t = VMEM_ROTOR;
3029*7c478bd9Sstevel@tonic-gate 		else if (strcmp(type, "WLKR") == 0 ||
3030*7c478bd9Sstevel@tonic-gate 		    strcmp(type, "WALKER") == 0)
3031*7c478bd9Sstevel@tonic-gate 			t = VMEM_WALKER;
3032*7c478bd9Sstevel@tonic-gate 		else {
3033*7c478bd9Sstevel@tonic-gate 			mdb_warn("\"%s\" is not a recognized vmem_seg type\n",
3034*7c478bd9Sstevel@tonic-gate 			    type);
3035*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
3036*7c478bd9Sstevel@tonic-gate 		}
3037*7c478bd9Sstevel@tonic-gate 
3038*7c478bd9Sstevel@tonic-gate 		if (vs.vs_type != t)
3039*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
3040*7c478bd9Sstevel@tonic-gate 	}
3041*7c478bd9Sstevel@tonic-gate 
3042*7c478bd9Sstevel@tonic-gate 	sz = vs.vs_end - vs.vs_start;
3043*7c478bd9Sstevel@tonic-gate 
3044*7c478bd9Sstevel@tonic-gate 	if (minsize != 0 && sz < minsize)
3045*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
3046*7c478bd9Sstevel@tonic-gate 
3047*7c478bd9Sstevel@tonic-gate 	if (maxsize != 0 && sz > maxsize)
3048*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
3049*7c478bd9Sstevel@tonic-gate 
3050*7c478bd9Sstevel@tonic-gate 	t = vs.vs_type;
3051*7c478bd9Sstevel@tonic-gate 	depth = vs.vs_depth;
3052*7c478bd9Sstevel@tonic-gate 
3053*7c478bd9Sstevel@tonic-gate 	/*
3054*7c478bd9Sstevel@tonic-gate 	 * debug info, when present, is only accurate for VMEM_ALLOC segments
3055*7c478bd9Sstevel@tonic-gate 	 */
3056*7c478bd9Sstevel@tonic-gate 	no_debug = (t != VMEM_ALLOC) ||
3057*7c478bd9Sstevel@tonic-gate 	    (depth == 0 || depth > VMEM_STACK_DEPTH);
3058*7c478bd9Sstevel@tonic-gate 
3059*7c478bd9Sstevel@tonic-gate 	if (no_debug) {
3060*7c478bd9Sstevel@tonic-gate 		if (caller != NULL || thread != NULL || earliest != 0 ||
3061*7c478bd9Sstevel@tonic-gate 		    latest != 0)
3062*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);		/* not enough info */
3063*7c478bd9Sstevel@tonic-gate 	} else {
3064*7c478bd9Sstevel@tonic-gate 		if (caller != NULL) {
3065*7c478bd9Sstevel@tonic-gate 			laddr = caller;
3066*7c478bd9Sstevel@tonic-gate 			haddr = caller + sizeof (caller);
3067*7c478bd9Sstevel@tonic-gate 
3068*7c478bd9Sstevel@tonic-gate 			if (mdb_lookup_by_addr(caller, MDB_SYM_FUZZY, c,
3069*7c478bd9Sstevel@tonic-gate 			    sizeof (c), &sym) != -1 &&
3070*7c478bd9Sstevel@tonic-gate 			    caller == (uintptr_t)sym.st_value) {
3071*7c478bd9Sstevel@tonic-gate 				/*
3072*7c478bd9Sstevel@tonic-gate 				 * We were provided an exact symbol value; any
3073*7c478bd9Sstevel@tonic-gate 				 * address in the function is valid.
3074*7c478bd9Sstevel@tonic-gate 				 */
3075*7c478bd9Sstevel@tonic-gate 				laddr = (uintptr_t)sym.st_value;
3076*7c478bd9Sstevel@tonic-gate 				haddr = (uintptr_t)sym.st_value + sym.st_size;
3077*7c478bd9Sstevel@tonic-gate 			}
3078*7c478bd9Sstevel@tonic-gate 
3079*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < depth; i++)
3080*7c478bd9Sstevel@tonic-gate 				if (vs.vs_stack[i] >= laddr &&
3081*7c478bd9Sstevel@tonic-gate 				    vs.vs_stack[i] < haddr)
3082*7c478bd9Sstevel@tonic-gate 					break;
3083*7c478bd9Sstevel@tonic-gate 
3084*7c478bd9Sstevel@tonic-gate 			if (i == depth)
3085*7c478bd9Sstevel@tonic-gate 				return (DCMD_OK);
3086*7c478bd9Sstevel@tonic-gate 		}
3087*7c478bd9Sstevel@tonic-gate 
3088*7c478bd9Sstevel@tonic-gate 		if (thread != NULL && (uintptr_t)vs.vs_thread != thread)
3089*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
3090*7c478bd9Sstevel@tonic-gate 
3091*7c478bd9Sstevel@tonic-gate 		if (earliest != 0 && vs.vs_timestamp < earliest)
3092*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
3093*7c478bd9Sstevel@tonic-gate 
3094*7c478bd9Sstevel@tonic-gate 		if (latest != 0 && vs.vs_timestamp > latest)
3095*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
3096*7c478bd9Sstevel@tonic-gate 	}
3097*7c478bd9Sstevel@tonic-gate 
3098*7c478bd9Sstevel@tonic-gate 	type = (t == VMEM_ALLOC ? "ALLC" :
3099*7c478bd9Sstevel@tonic-gate 	    t == VMEM_FREE ? "FREE" :
3100*7c478bd9Sstevel@tonic-gate 	    t == VMEM_SPAN ? "SPAN" :
3101*7c478bd9Sstevel@tonic-gate 	    t == VMEM_ROTOR ? "ROTR" :
3102*7c478bd9Sstevel@tonic-gate 	    t == VMEM_WALKER ? "WLKR" :
3103*7c478bd9Sstevel@tonic-gate 	    "????");
3104*7c478bd9Sstevel@tonic-gate 
3105*7c478bd9Sstevel@tonic-gate 	if (flags & DCMD_PIPE_OUT) {
3106*7c478bd9Sstevel@tonic-gate 		mdb_printf("%#r\n", addr);
3107*7c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
3108*7c478bd9Sstevel@tonic-gate 	}
3109*7c478bd9Sstevel@tonic-gate 
3110*7c478bd9Sstevel@tonic-gate 	if (verbose) {
3111*7c478bd9Sstevel@tonic-gate 		mdb_printf("%<b>%16p%</b> %4s %16p %16p %16d\n",
3112*7c478bd9Sstevel@tonic-gate 		    addr, type, vs.vs_start, vs.vs_end, sz);
3113*7c478bd9Sstevel@tonic-gate 
3114*7c478bd9Sstevel@tonic-gate 		if (no_debug)
3115*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
3116*7c478bd9Sstevel@tonic-gate 
3117*7c478bd9Sstevel@tonic-gate 		mdb_printf("%16s %4s %16d %16llx\n",
3118*7c478bd9Sstevel@tonic-gate 		    "", "", vs.vs_thread, vs.vs_timestamp);
3119*7c478bd9Sstevel@tonic-gate 
3120*7c478bd9Sstevel@tonic-gate 		mdb_inc_indent(17);
3121*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++) {
3122*7c478bd9Sstevel@tonic-gate 			mdb_printf("%a\n", stk[i]);
3123*7c478bd9Sstevel@tonic-gate 		}
3124*7c478bd9Sstevel@tonic-gate 		mdb_dec_indent(17);
3125*7c478bd9Sstevel@tonic-gate 		mdb_printf("\n");
3126*7c478bd9Sstevel@tonic-gate 	} else {
3127*7c478bd9Sstevel@tonic-gate 		mdb_printf("%0?p %4s %0?p %0?p", addr, type,
3128*7c478bd9Sstevel@tonic-gate 		    vs.vs_start, size? sz : vs.vs_end);
3129*7c478bd9Sstevel@tonic-gate 
3130*7c478bd9Sstevel@tonic-gate 		if (no_debug) {
3131*7c478bd9Sstevel@tonic-gate 			mdb_printf("\n");
3132*7c478bd9Sstevel@tonic-gate 			return (DCMD_OK);
3133*7c478bd9Sstevel@tonic-gate 		}
3134*7c478bd9Sstevel@tonic-gate 
3135*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++) {
3136*7c478bd9Sstevel@tonic-gate 			if (mdb_lookup_by_addr(stk[i], MDB_SYM_FUZZY,
3137*7c478bd9Sstevel@tonic-gate 			    c, sizeof (c), &sym) == -1)
3138*7c478bd9Sstevel@tonic-gate 				continue;
3139*7c478bd9Sstevel@tonic-gate 			if (is_umem_sym(c, "vmem_"))
3140*7c478bd9Sstevel@tonic-gate 				continue;
3141*7c478bd9Sstevel@tonic-gate 			break;
3142*7c478bd9Sstevel@tonic-gate 		}
3143*7c478bd9Sstevel@tonic-gate 		mdb_printf(" %a\n", stk[i]);
3144*7c478bd9Sstevel@tonic-gate 	}
3145*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3146*7c478bd9Sstevel@tonic-gate }
3147*7c478bd9Sstevel@tonic-gate 
3148*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3149*7c478bd9Sstevel@tonic-gate static int
3150*7c478bd9Sstevel@tonic-gate showbc(uintptr_t addr, const umem_bufctl_audit_t *bcp, hrtime_t *newest)
3151*7c478bd9Sstevel@tonic-gate {
3152*7c478bd9Sstevel@tonic-gate 	char name[UMEM_CACHE_NAMELEN + 1];
3153*7c478bd9Sstevel@tonic-gate 	hrtime_t delta;
3154*7c478bd9Sstevel@tonic-gate 	int i, depth;
3155*7c478bd9Sstevel@tonic-gate 
3156*7c478bd9Sstevel@tonic-gate 	if (bcp->bc_timestamp == 0)
3157*7c478bd9Sstevel@tonic-gate 		return (WALK_DONE);
3158*7c478bd9Sstevel@tonic-gate 
3159*7c478bd9Sstevel@tonic-gate 	if (*newest == 0)
3160*7c478bd9Sstevel@tonic-gate 		*newest = bcp->bc_timestamp;
3161*7c478bd9Sstevel@tonic-gate 
3162*7c478bd9Sstevel@tonic-gate 	delta = *newest - bcp->bc_timestamp;
3163*7c478bd9Sstevel@tonic-gate 	depth = MIN(bcp->bc_depth, umem_stack_depth);
3164*7c478bd9Sstevel@tonic-gate 
3165*7c478bd9Sstevel@tonic-gate 	if (mdb_readstr(name, sizeof (name), (uintptr_t)
3166*7c478bd9Sstevel@tonic-gate 	    &bcp->bc_cache->cache_name) <= 0)
3167*7c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(name, sizeof (name), "%a", bcp->bc_cache);
3168*7c478bd9Sstevel@tonic-gate 
3169*7c478bd9Sstevel@tonic-gate 	mdb_printf("\nT-%lld.%09lld  addr=%p  %s\n",
3170*7c478bd9Sstevel@tonic-gate 	    delta / NANOSEC, delta % NANOSEC, bcp->bc_addr, name);
3171*7c478bd9Sstevel@tonic-gate 
3172*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
3173*7c478bd9Sstevel@tonic-gate 		mdb_printf("\t %a\n", bcp->bc_stack[i]);
3174*7c478bd9Sstevel@tonic-gate 
3175*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
3176*7c478bd9Sstevel@tonic-gate }
3177*7c478bd9Sstevel@tonic-gate 
3178*7c478bd9Sstevel@tonic-gate int
3179*7c478bd9Sstevel@tonic-gate umalog(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3180*7c478bd9Sstevel@tonic-gate {
3181*7c478bd9Sstevel@tonic-gate 	const char *logname = "umem_transaction_log";
3182*7c478bd9Sstevel@tonic-gate 	hrtime_t newest = 0;
3183*7c478bd9Sstevel@tonic-gate 
3184*7c478bd9Sstevel@tonic-gate 	if ((flags & DCMD_ADDRSPEC) || argc > 1)
3185*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3186*7c478bd9Sstevel@tonic-gate 
3187*7c478bd9Sstevel@tonic-gate 	if (argc > 0) {
3188*7c478bd9Sstevel@tonic-gate 		if (argv->a_type != MDB_TYPE_STRING)
3189*7c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
3190*7c478bd9Sstevel@tonic-gate 		if (strcmp(argv->a_un.a_str, "fail") == 0)
3191*7c478bd9Sstevel@tonic-gate 			logname = "umem_failure_log";
3192*7c478bd9Sstevel@tonic-gate 		else if (strcmp(argv->a_un.a_str, "slab") == 0)
3193*7c478bd9Sstevel@tonic-gate 			logname = "umem_slab_log";
3194*7c478bd9Sstevel@tonic-gate 		else
3195*7c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
3196*7c478bd9Sstevel@tonic-gate 	}
3197*7c478bd9Sstevel@tonic-gate 
3198*7c478bd9Sstevel@tonic-gate 	if (umem_readvar(&addr, logname) == -1) {
3199*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read %s log header pointer");
3200*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3201*7c478bd9Sstevel@tonic-gate 	}
3202*7c478bd9Sstevel@tonic-gate 
3203*7c478bd9Sstevel@tonic-gate 	if (mdb_pwalk("umem_log", (mdb_walk_cb_t)showbc, &newest, addr) == -1) {
3204*7c478bd9Sstevel@tonic-gate 		mdb_warn("failed to walk umem log");
3205*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3206*7c478bd9Sstevel@tonic-gate 	}
3207*7c478bd9Sstevel@tonic-gate 
3208*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3209*7c478bd9Sstevel@tonic-gate }
3210*7c478bd9Sstevel@tonic-gate 
3211*7c478bd9Sstevel@tonic-gate /*
3212*7c478bd9Sstevel@tonic-gate  * As the final lure for die-hard crash(1M) users, we provide ::umausers here.
3213*7c478bd9Sstevel@tonic-gate  * The first piece is a structure which we use to accumulate umem_cache_t
3214*7c478bd9Sstevel@tonic-gate  * addresses of interest.  The umc_add is used as a callback for the umem_cache
3215*7c478bd9Sstevel@tonic-gate  * walker; we either add all caches, or ones named explicitly as arguments.
3216*7c478bd9Sstevel@tonic-gate  */
3217*7c478bd9Sstevel@tonic-gate 
3218*7c478bd9Sstevel@tonic-gate typedef struct umclist {
3219*7c478bd9Sstevel@tonic-gate 	const char *umc_name;			/* Name to match (or NULL) */
3220*7c478bd9Sstevel@tonic-gate 	uintptr_t *umc_caches;			/* List of umem_cache_t addrs */
3221*7c478bd9Sstevel@tonic-gate 	int umc_nelems;				/* Num entries in umc_caches */
3222*7c478bd9Sstevel@tonic-gate 	int umc_size;				/* Size of umc_caches array */
3223*7c478bd9Sstevel@tonic-gate } umclist_t;
3224*7c478bd9Sstevel@tonic-gate 
3225*7c478bd9Sstevel@tonic-gate static int
3226*7c478bd9Sstevel@tonic-gate umc_add(uintptr_t addr, const umem_cache_t *cp, umclist_t *umc)
3227*7c478bd9Sstevel@tonic-gate {
3228*7c478bd9Sstevel@tonic-gate 	void *p;
3229*7c478bd9Sstevel@tonic-gate 	int s;
3230*7c478bd9Sstevel@tonic-gate 
3231*7c478bd9Sstevel@tonic-gate 	if (umc->umc_name == NULL ||
3232*7c478bd9Sstevel@tonic-gate 	    strcmp(cp->cache_name, umc->umc_name) == 0) {
3233*7c478bd9Sstevel@tonic-gate 		/*
3234*7c478bd9Sstevel@tonic-gate 		 * If we have a match, grow our array (if necessary), and then
3235*7c478bd9Sstevel@tonic-gate 		 * add the virtual address of the matching cache to our list.
3236*7c478bd9Sstevel@tonic-gate 		 */
3237*7c478bd9Sstevel@tonic-gate 		if (umc->umc_nelems >= umc->umc_size) {
3238*7c478bd9Sstevel@tonic-gate 			s = umc->umc_size ? umc->umc_size * 2 : 256;
3239*7c478bd9Sstevel@tonic-gate 			p = mdb_alloc(sizeof (uintptr_t) * s, UM_SLEEP | UM_GC);
3240*7c478bd9Sstevel@tonic-gate 
3241*7c478bd9Sstevel@tonic-gate 			bcopy(umc->umc_caches, p,
3242*7c478bd9Sstevel@tonic-gate 			    sizeof (uintptr_t) * umc->umc_size);
3243*7c478bd9Sstevel@tonic-gate 
3244*7c478bd9Sstevel@tonic-gate 			umc->umc_caches = p;
3245*7c478bd9Sstevel@tonic-gate 			umc->umc_size = s;
3246*7c478bd9Sstevel@tonic-gate 		}
3247*7c478bd9Sstevel@tonic-gate 
3248*7c478bd9Sstevel@tonic-gate 		umc->umc_caches[umc->umc_nelems++] = addr;
3249*7c478bd9Sstevel@tonic-gate 		return (umc->umc_name ? WALK_DONE : WALK_NEXT);
3250*7c478bd9Sstevel@tonic-gate 	}
3251*7c478bd9Sstevel@tonic-gate 
3252*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
3253*7c478bd9Sstevel@tonic-gate }
3254*7c478bd9Sstevel@tonic-gate 
3255*7c478bd9Sstevel@tonic-gate /*
3256*7c478bd9Sstevel@tonic-gate  * The second piece of ::umausers is a hash table of allocations.  Each
3257*7c478bd9Sstevel@tonic-gate  * allocation owner is identified by its stack trace and data_size.  We then
3258*7c478bd9Sstevel@tonic-gate  * track the total bytes of all such allocations, and the number of allocations
3259*7c478bd9Sstevel@tonic-gate  * to report at the end.  Once we have a list of caches, we walk through the
3260*7c478bd9Sstevel@tonic-gate  * allocated bufctls of each, and update our hash table accordingly.
3261*7c478bd9Sstevel@tonic-gate  */
3262*7c478bd9Sstevel@tonic-gate 
3263*7c478bd9Sstevel@tonic-gate typedef struct umowner {
3264*7c478bd9Sstevel@tonic-gate 	struct umowner *umo_head;		/* First hash elt in bucket */
3265*7c478bd9Sstevel@tonic-gate 	struct umowner *umo_next;		/* Next hash elt in chain */
3266*7c478bd9Sstevel@tonic-gate 	size_t umo_signature;			/* Hash table signature */
3267*7c478bd9Sstevel@tonic-gate 	uint_t umo_num;				/* Number of allocations */
3268*7c478bd9Sstevel@tonic-gate 	size_t umo_data_size;			/* Size of each allocation */
3269*7c478bd9Sstevel@tonic-gate 	size_t umo_total_size;			/* Total bytes of allocation */
3270*7c478bd9Sstevel@tonic-gate 	int umo_depth;				/* Depth of stack trace */
3271*7c478bd9Sstevel@tonic-gate 	uintptr_t *umo_stack;			/* Stack trace */
3272*7c478bd9Sstevel@tonic-gate } umowner_t;
3273*7c478bd9Sstevel@tonic-gate 
3274*7c478bd9Sstevel@tonic-gate typedef struct umusers {
3275*7c478bd9Sstevel@tonic-gate 	const umem_cache_t *umu_cache;		/* Current umem cache */
3276*7c478bd9Sstevel@tonic-gate 	umowner_t *umu_hash;			/* Hash table of owners */
3277*7c478bd9Sstevel@tonic-gate 	uintptr_t *umu_stacks;			/* stacks for owners */
3278*7c478bd9Sstevel@tonic-gate 	int umu_nelems;				/* Number of entries in use */
3279*7c478bd9Sstevel@tonic-gate 	int umu_size;				/* Total number of entries */
3280*7c478bd9Sstevel@tonic-gate } umusers_t;
3281*7c478bd9Sstevel@tonic-gate 
3282*7c478bd9Sstevel@tonic-gate static void
3283*7c478bd9Sstevel@tonic-gate umu_add(umusers_t *umu, const umem_bufctl_audit_t *bcp,
3284*7c478bd9Sstevel@tonic-gate     size_t size, size_t data_size)
3285*7c478bd9Sstevel@tonic-gate {
3286*7c478bd9Sstevel@tonic-gate 	int i, depth = MIN(bcp->bc_depth, umem_stack_depth);
3287*7c478bd9Sstevel@tonic-gate 	size_t bucket, signature = data_size;
3288*7c478bd9Sstevel@tonic-gate 	umowner_t *umo, *umoend;
3289*7c478bd9Sstevel@tonic-gate 
3290*7c478bd9Sstevel@tonic-gate 	/*
3291*7c478bd9Sstevel@tonic-gate 	 * If the hash table is full, double its size and rehash everything.
3292*7c478bd9Sstevel@tonic-gate 	 */
3293*7c478bd9Sstevel@tonic-gate 	if (umu->umu_nelems >= umu->umu_size) {
3294*7c478bd9Sstevel@tonic-gate 		int s = umu->umu_size ? umu->umu_size * 2 : 1024;
3295*7c478bd9Sstevel@tonic-gate 		size_t umowner_size = sizeof (umowner_t);
3296*7c478bd9Sstevel@tonic-gate 		size_t trace_size = umem_stack_depth * sizeof (uintptr_t);
3297*7c478bd9Sstevel@tonic-gate 		uintptr_t *new_stacks;
3298*7c478bd9Sstevel@tonic-gate 
3299*7c478bd9Sstevel@tonic-gate 		umo = mdb_alloc(umowner_size * s, UM_SLEEP | UM_GC);
3300*7c478bd9Sstevel@tonic-gate 		new_stacks = mdb_alloc(trace_size * s, UM_SLEEP | UM_GC);
3301*7c478bd9Sstevel@tonic-gate 
3302*7c478bd9Sstevel@tonic-gate 		bcopy(umu->umu_hash, umo, umowner_size * umu->umu_size);
3303*7c478bd9Sstevel@tonic-gate 		bcopy(umu->umu_stacks, new_stacks, trace_size * umu->umu_size);
3304*7c478bd9Sstevel@tonic-gate 		umu->umu_hash = umo;
3305*7c478bd9Sstevel@tonic-gate 		umu->umu_stacks = new_stacks;
3306*7c478bd9Sstevel@tonic-gate 		umu->umu_size = s;
3307*7c478bd9Sstevel@tonic-gate 
3308*7c478bd9Sstevel@tonic-gate 		umoend = umu->umu_hash + umu->umu_size;
3309*7c478bd9Sstevel@tonic-gate 		for (umo = umu->umu_hash; umo < umoend; umo++) {
3310*7c478bd9Sstevel@tonic-gate 			umo->umo_head = NULL;
3311*7c478bd9Sstevel@tonic-gate 			umo->umo_stack = &umu->umu_stacks[
3312*7c478bd9Sstevel@tonic-gate 			    umem_stack_depth * (umo - umu->umu_hash)];
3313*7c478bd9Sstevel@tonic-gate 		}
3314*7c478bd9Sstevel@tonic-gate 
3315*7c478bd9Sstevel@tonic-gate 		umoend = umu->umu_hash + umu->umu_nelems;
3316*7c478bd9Sstevel@tonic-gate 		for (umo = umu->umu_hash; umo < umoend; umo++) {
3317*7c478bd9Sstevel@tonic-gate 			bucket = umo->umo_signature & (umu->umu_size - 1);
3318*7c478bd9Sstevel@tonic-gate 			umo->umo_next = umu->umu_hash[bucket].umo_head;
3319*7c478bd9Sstevel@tonic-gate 			umu->umu_hash[bucket].umo_head = umo;
3320*7c478bd9Sstevel@tonic-gate 		}
3321*7c478bd9Sstevel@tonic-gate 	}
3322*7c478bd9Sstevel@tonic-gate 
3323*7c478bd9Sstevel@tonic-gate 	/*
3324*7c478bd9Sstevel@tonic-gate 	 * Finish computing the hash signature from the stack trace, and then
3325*7c478bd9Sstevel@tonic-gate 	 * see if the owner is in the hash table.  If so, update our stats.
3326*7c478bd9Sstevel@tonic-gate 	 */
3327*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
3328*7c478bd9Sstevel@tonic-gate 		signature += bcp->bc_stack[i];
3329*7c478bd9Sstevel@tonic-gate 
3330*7c478bd9Sstevel@tonic-gate 	bucket = signature & (umu->umu_size - 1);
3331*7c478bd9Sstevel@tonic-gate 
3332*7c478bd9Sstevel@tonic-gate 	for (umo = umu->umu_hash[bucket].umo_head; umo; umo = umo->umo_next) {
3333*7c478bd9Sstevel@tonic-gate 		if (umo->umo_signature == signature) {
3334*7c478bd9Sstevel@tonic-gate 			size_t difference = 0;
3335*7c478bd9Sstevel@tonic-gate 
3336*7c478bd9Sstevel@tonic-gate 			difference |= umo->umo_data_size - data_size;
3337*7c478bd9Sstevel@tonic-gate 			difference |= umo->umo_depth - depth;
3338*7c478bd9Sstevel@tonic-gate 
3339*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < depth; i++) {
3340*7c478bd9Sstevel@tonic-gate 				difference |= umo->umo_stack[i] -
3341*7c478bd9Sstevel@tonic-gate 				    bcp->bc_stack[i];
3342*7c478bd9Sstevel@tonic-gate 			}
3343*7c478bd9Sstevel@tonic-gate 
3344*7c478bd9Sstevel@tonic-gate 			if (difference == 0) {
3345*7c478bd9Sstevel@tonic-gate 				umo->umo_total_size += size;
3346*7c478bd9Sstevel@tonic-gate 				umo->umo_num++;
3347*7c478bd9Sstevel@tonic-gate 				return;
3348*7c478bd9Sstevel@tonic-gate 			}
3349*7c478bd9Sstevel@tonic-gate 		}
3350*7c478bd9Sstevel@tonic-gate 	}
3351*7c478bd9Sstevel@tonic-gate 
3352*7c478bd9Sstevel@tonic-gate 	/*
3353*7c478bd9Sstevel@tonic-gate 	 * If the owner is not yet hashed, grab the next element and fill it
3354*7c478bd9Sstevel@tonic-gate 	 * in based on the allocation information.
3355*7c478bd9Sstevel@tonic-gate 	 */
3356*7c478bd9Sstevel@tonic-gate 	umo = &umu->umu_hash[umu->umu_nelems++];
3357*7c478bd9Sstevel@tonic-gate 	umo->umo_next = umu->umu_hash[bucket].umo_head;
3358*7c478bd9Sstevel@tonic-gate 	umu->umu_hash[bucket].umo_head = umo;
3359*7c478bd9Sstevel@tonic-gate 
3360*7c478bd9Sstevel@tonic-gate 	umo->umo_signature = signature;
3361*7c478bd9Sstevel@tonic-gate 	umo->umo_num = 1;
3362*7c478bd9Sstevel@tonic-gate 	umo->umo_data_size = data_size;
3363*7c478bd9Sstevel@tonic-gate 	umo->umo_total_size = size;
3364*7c478bd9Sstevel@tonic-gate 	umo->umo_depth = depth;
3365*7c478bd9Sstevel@tonic-gate 
3366*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
3367*7c478bd9Sstevel@tonic-gate 		umo->umo_stack[i] = bcp->bc_stack[i];
3368*7c478bd9Sstevel@tonic-gate }
3369*7c478bd9Sstevel@tonic-gate 
3370*7c478bd9Sstevel@tonic-gate /*
3371*7c478bd9Sstevel@tonic-gate  * When ::umausers is invoked without the -f flag, we simply update our hash
3372*7c478bd9Sstevel@tonic-gate  * table with the information from each allocated bufctl.
3373*7c478bd9Sstevel@tonic-gate  */
3374*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3375*7c478bd9Sstevel@tonic-gate static int
3376*7c478bd9Sstevel@tonic-gate umause1(uintptr_t addr, const umem_bufctl_audit_t *bcp, umusers_t *umu)
3377*7c478bd9Sstevel@tonic-gate {
3378*7c478bd9Sstevel@tonic-gate 	const umem_cache_t *cp = umu->umu_cache;
3379*7c478bd9Sstevel@tonic-gate 
3380*7c478bd9Sstevel@tonic-gate 	umu_add(umu, bcp, cp->cache_bufsize, cp->cache_bufsize);
3381*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
3382*7c478bd9Sstevel@tonic-gate }
3383*7c478bd9Sstevel@tonic-gate 
3384*7c478bd9Sstevel@tonic-gate /*
3385*7c478bd9Sstevel@tonic-gate  * When ::umausers is invoked with the -f flag, we print out the information
3386*7c478bd9Sstevel@tonic-gate  * for each bufctl as well as updating the hash table.
3387*7c478bd9Sstevel@tonic-gate  */
3388*7c478bd9Sstevel@tonic-gate static int
3389*7c478bd9Sstevel@tonic-gate umause2(uintptr_t addr, const umem_bufctl_audit_t *bcp, umusers_t *umu)
3390*7c478bd9Sstevel@tonic-gate {
3391*7c478bd9Sstevel@tonic-gate 	int i, depth = MIN(bcp->bc_depth, umem_stack_depth);
3392*7c478bd9Sstevel@tonic-gate 	const umem_cache_t *cp = umu->umu_cache;
3393*7c478bd9Sstevel@tonic-gate 
3394*7c478bd9Sstevel@tonic-gate 	mdb_printf("size %d, addr %p, thread %p, cache %s\n",
3395*7c478bd9Sstevel@tonic-gate 	    cp->cache_bufsize, addr, bcp->bc_thread, cp->cache_name);
3396*7c478bd9Sstevel@tonic-gate 
3397*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
3398*7c478bd9Sstevel@tonic-gate 		mdb_printf("\t %a\n", bcp->bc_stack[i]);
3399*7c478bd9Sstevel@tonic-gate 
3400*7c478bd9Sstevel@tonic-gate 	umu_add(umu, bcp, cp->cache_bufsize, cp->cache_bufsize);
3401*7c478bd9Sstevel@tonic-gate 	return (WALK_NEXT);
3402*7c478bd9Sstevel@tonic-gate }
3403*7c478bd9Sstevel@tonic-gate 
3404*7c478bd9Sstevel@tonic-gate /*
3405*7c478bd9Sstevel@tonic-gate  * We sort our results by allocation size before printing them.
3406*7c478bd9Sstevel@tonic-gate  */
3407*7c478bd9Sstevel@tonic-gate static int
3408*7c478bd9Sstevel@tonic-gate umownercmp(const void *lp, const void *rp)
3409*7c478bd9Sstevel@tonic-gate {
3410*7c478bd9Sstevel@tonic-gate 	const umowner_t *lhs = lp;
3411*7c478bd9Sstevel@tonic-gate 	const umowner_t *rhs = rp;
3412*7c478bd9Sstevel@tonic-gate 
3413*7c478bd9Sstevel@tonic-gate 	return (rhs->umo_total_size - lhs->umo_total_size);
3414*7c478bd9Sstevel@tonic-gate }
3415*7c478bd9Sstevel@tonic-gate 
3416*7c478bd9Sstevel@tonic-gate /*
3417*7c478bd9Sstevel@tonic-gate  * The main engine of ::umausers is relatively straightforward: First we
3418*7c478bd9Sstevel@tonic-gate  * accumulate our list of umem_cache_t addresses into the umclist_t. Next we
3419*7c478bd9Sstevel@tonic-gate  * iterate over the allocated bufctls of each cache in the list.  Finally,
3420*7c478bd9Sstevel@tonic-gate  * we sort and print our results.
3421*7c478bd9Sstevel@tonic-gate  */
3422*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3423*7c478bd9Sstevel@tonic-gate int
3424*7c478bd9Sstevel@tonic-gate umausers(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3425*7c478bd9Sstevel@tonic-gate {
3426*7c478bd9Sstevel@tonic-gate 	int mem_threshold = 8192;	/* Minimum # bytes for printing */
3427*7c478bd9Sstevel@tonic-gate 	int cnt_threshold = 100;	/* Minimum # blocks for printing */
3428*7c478bd9Sstevel@tonic-gate 	int audited_caches = 0;		/* Number of UMF_AUDIT caches found */
3429*7c478bd9Sstevel@tonic-gate 	int do_all_caches = 1;		/* Do all caches (no arguments) */
3430*7c478bd9Sstevel@tonic-gate 	int opt_e = FALSE;		/* Include "small" users */
3431*7c478bd9Sstevel@tonic-gate 	int opt_f = FALSE;		/* Print stack traces */
3432*7c478bd9Sstevel@tonic-gate 
3433*7c478bd9Sstevel@tonic-gate 	mdb_walk_cb_t callback = (mdb_walk_cb_t)umause1;
3434*7c478bd9Sstevel@tonic-gate 	umowner_t *umo, *umoend;
3435*7c478bd9Sstevel@tonic-gate 	int i, oelems;
3436*7c478bd9Sstevel@tonic-gate 
3437*7c478bd9Sstevel@tonic-gate 	umclist_t umc;
3438*7c478bd9Sstevel@tonic-gate 	umusers_t umu;
3439*7c478bd9Sstevel@tonic-gate 
3440*7c478bd9Sstevel@tonic-gate 	if (flags & DCMD_ADDRSPEC)
3441*7c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
3442*7c478bd9Sstevel@tonic-gate 
3443*7c478bd9Sstevel@tonic-gate 	bzero(&umc, sizeof (umc));
3444*7c478bd9Sstevel@tonic-gate 	bzero(&umu, sizeof (umu));
3445*7c478bd9Sstevel@tonic-gate 
3446*7c478bd9Sstevel@tonic-gate 	while ((i = mdb_getopts(argc, argv,
3447*7c478bd9Sstevel@tonic-gate 	    'e', MDB_OPT_SETBITS, TRUE, &opt_e,
3448*7c478bd9Sstevel@tonic-gate 	    'f', MDB_OPT_SETBITS, TRUE, &opt_f, NULL)) != argc) {
3449*7c478bd9Sstevel@tonic-gate 
3450*7c478bd9Sstevel@tonic-gate 		argv += i;	/* skip past options we just processed */
3451*7c478bd9Sstevel@tonic-gate 		argc -= i;	/* adjust argc */
3452*7c478bd9Sstevel@tonic-gate 
3453*7c478bd9Sstevel@tonic-gate 		if (argv->a_type != MDB_TYPE_STRING || *argv->a_un.a_str == '-')
3454*7c478bd9Sstevel@tonic-gate 			return (DCMD_USAGE);
3455*7c478bd9Sstevel@tonic-gate 
3456*7c478bd9Sstevel@tonic-gate 		oelems = umc.umc_nelems;
3457*7c478bd9Sstevel@tonic-gate 		umc.umc_name = argv->a_un.a_str;
3458*7c478bd9Sstevel@tonic-gate 		(void) mdb_walk("umem_cache", (mdb_walk_cb_t)umc_add, &umc);
3459*7c478bd9Sstevel@tonic-gate 
3460*7c478bd9Sstevel@tonic-gate 		if (umc.umc_nelems == oelems) {
3461*7c478bd9Sstevel@tonic-gate 			mdb_warn("unknown umem cache: %s\n", umc.umc_name);
3462*7c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
3463*7c478bd9Sstevel@tonic-gate 		}
3464*7c478bd9Sstevel@tonic-gate 
3465*7c478bd9Sstevel@tonic-gate 		do_all_caches = 0;
3466*7c478bd9Sstevel@tonic-gate 		argv++;
3467*7c478bd9Sstevel@tonic-gate 		argc--;
3468*7c478bd9Sstevel@tonic-gate 	}
3469*7c478bd9Sstevel@tonic-gate 
3470*7c478bd9Sstevel@tonic-gate 	if (opt_e)
3471*7c478bd9Sstevel@tonic-gate 		mem_threshold = cnt_threshold = 0;
3472*7c478bd9Sstevel@tonic-gate 
3473*7c478bd9Sstevel@tonic-gate 	if (opt_f)
3474*7c478bd9Sstevel@tonic-gate 		callback = (mdb_walk_cb_t)umause2;
3475*7c478bd9Sstevel@tonic-gate 
3476*7c478bd9Sstevel@tonic-gate 	if (do_all_caches) {
3477*7c478bd9Sstevel@tonic-gate 		umc.umc_name = NULL; /* match all cache names */
3478*7c478bd9Sstevel@tonic-gate 		(void) mdb_walk("umem_cache", (mdb_walk_cb_t)umc_add, &umc);
3479*7c478bd9Sstevel@tonic-gate 	}
3480*7c478bd9Sstevel@tonic-gate 
3481*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < umc.umc_nelems; i++) {
3482*7c478bd9Sstevel@tonic-gate 		uintptr_t cp = umc.umc_caches[i];
3483*7c478bd9Sstevel@tonic-gate 		umem_cache_t c;
3484*7c478bd9Sstevel@tonic-gate 
3485*7c478bd9Sstevel@tonic-gate 		if (mdb_vread(&c, sizeof (c), cp) == -1) {
3486*7c478bd9Sstevel@tonic-gate 			mdb_warn("failed to read cache at %p", cp);
3487*7c478bd9Sstevel@tonic-gate 			continue;
3488*7c478bd9Sstevel@tonic-gate 		}
3489*7c478bd9Sstevel@tonic-gate 
3490*7c478bd9Sstevel@tonic-gate 		if (!(c.cache_flags & UMF_AUDIT)) {
3491*7c478bd9Sstevel@tonic-gate 			if (!do_all_caches) {
3492*7c478bd9Sstevel@tonic-gate 				mdb_warn("UMF_AUDIT is not enabled for %s\n",
3493*7c478bd9Sstevel@tonic-gate 				    c.cache_name);
3494*7c478bd9Sstevel@tonic-gate 			}
3495*7c478bd9Sstevel@tonic-gate 			continue;
3496*7c478bd9Sstevel@tonic-gate 		}
3497*7c478bd9Sstevel@tonic-gate 
3498*7c478bd9Sstevel@tonic-gate 		umu.umu_cache = &c;
3499*7c478bd9Sstevel@tonic-gate 		(void) mdb_pwalk("bufctl", callback, &umu, cp);
3500*7c478bd9Sstevel@tonic-gate 		audited_caches++;
3501*7c478bd9Sstevel@tonic-gate 	}
3502*7c478bd9Sstevel@tonic-gate 
3503*7c478bd9Sstevel@tonic-gate 	if (audited_caches == 0 && do_all_caches) {
3504*7c478bd9Sstevel@tonic-gate 		mdb_warn("UMF_AUDIT is not enabled for any caches\n");
3505*7c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
3506*7c478bd9Sstevel@tonic-gate 	}
3507*7c478bd9Sstevel@tonic-gate 
3508*7c478bd9Sstevel@tonic-gate 	qsort(umu.umu_hash, umu.umu_nelems, sizeof (umowner_t), umownercmp);
3509*7c478bd9Sstevel@tonic-gate 	umoend = umu.umu_hash + umu.umu_nelems;
3510*7c478bd9Sstevel@tonic-gate 
3511*7c478bd9Sstevel@tonic-gate 	for (umo = umu.umu_hash; umo < umoend; umo++) {
3512*7c478bd9Sstevel@tonic-gate 		if (umo->umo_total_size < mem_threshold &&
3513*7c478bd9Sstevel@tonic-gate 		    umo->umo_num < cnt_threshold)
3514*7c478bd9Sstevel@tonic-gate 			continue;
3515*7c478bd9Sstevel@tonic-gate 		mdb_printf("%lu bytes for %u allocations with data size %lu:\n",
3516*7c478bd9Sstevel@tonic-gate 		    umo->umo_total_size, umo->umo_num, umo->umo_data_size);
3517*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < umo->umo_depth; i++)
3518*7c478bd9Sstevel@tonic-gate 			mdb_printf("\t %a\n", umo->umo_stack[i]);
3519*7c478bd9Sstevel@tonic-gate 	}
3520*7c478bd9Sstevel@tonic-gate 
3521*7c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
3522*7c478bd9Sstevel@tonic-gate }
3523