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