summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/modules/libuutil/libuutil.c
blob: aa890302ec8f36a77a26fa3cf92707012b1a1339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <mdb/mdb_modapi.h>

#include <pthread.h>
#include <stddef.h>

#include <libuutil.h>
#include <libuutil_impl.h>

/*ARGSUSED*/
static int
uutil_status(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	pthread_t uu_panic_thread = 0;

	if ((flags & DCMD_ADDRSPEC) || argc != 0)
		return (DCMD_USAGE);

	if (mdb_readvar(&uu_panic_thread, "uu_panic_thread") == -1) {
		mdb_warn("unable to read uu_panic_thread");
	}

	if (uu_panic_thread != 0) {
		mdb_printf("thread %d uu_panicked\n", uu_panic_thread);
	}

	return (DCMD_OK);
}

/*ARGSUSED*/
static int
uutil_listpool(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	uu_list_pool_t ulp;

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("uu_list_pool", "uu_list_pool", argc,
		    argv) == -1) {
			mdb_warn("can't walk uu_list_pool");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}

	if (argc != 0)
		return (DCMD_USAGE);

	if (DCMD_HDRSPEC(flags))
		mdb_printf("%-?s %-30s %?s %5s\n",
		    "ADDR", "NAME", "COMPARE", "FLAGS");

	if (mdb_vread(&ulp, sizeof (uu_list_pool_t), addr) == -1) {
		mdb_warn("failed to read uu_list_pool\n");
		return (DCMD_ERR);
	}

	mdb_printf("%0?p %-30s %08x     %c\n", addr, ulp.ulp_name, ulp.ulp_cmp,
	    ulp.ulp_debug ? 'D' : ' ');

	return (DCMD_OK);
}

/*ARGSUSED*/
static int
uutil_list(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	uu_list_t ul;

	if (!(flags & DCMD_ADDRSPEC) || argc != 0)
		return (DCMD_USAGE);

	if (mdb_vread(&ul, sizeof (uu_list_t), addr) == -1) {
		mdb_warn("failed to read uu_list\n");
		return (DCMD_ERR);
	}

	if (DCMD_HDRSPEC(flags))
		mdb_printf("%-?s %-?s %-?s %6s %5s\n",
		    "ADDR", "POOL", "PARENT", "NODES", "FLAGS");

	mdb_printf("%0?p %0?p %0?p %6u    %c%c\n",
	    addr, ul.ul_pool, UU_PTR_DECODE(ul.ul_parent_enc),
	    ul.ul_numnodes, ul.ul_sorted ? 'S' : ' ', ul.ul_debug? 'D' : ' ');

	return (DCMD_OK);
}

typedef struct uutil_listpool_walk {
	uintptr_t ulpw_final;
	uintptr_t ulpw_current;
} uutil_listpool_walk_t;

int
uutil_listpool_walk_init(mdb_walk_state_t *wsp)
{
	uu_list_pool_t null_lpool;
	uutil_listpool_walk_t *ulpw;
	GElf_Sym sym;

	bzero(&null_lpool, sizeof (uu_list_pool_t));

	if (mdb_lookup_by_obj("libuutil.so.1", "uu_null_lpool", &sym) ==
	    -1) {
		mdb_warn("failed to find 'uu_null_lpool'\n");
		return (WALK_ERR);
	}

	if (mdb_vread(&null_lpool, sym.st_size, (uintptr_t)sym.st_value) ==
	    -1) {
		mdb_warn("failed to read data from 'uu_null_lpool' address\n");
		return (WALK_ERR);
	}

	ulpw = mdb_alloc(sizeof (uutil_listpool_walk_t), UM_SLEEP);

	ulpw->ulpw_final = (uintptr_t)null_lpool.ulp_prev;
	ulpw->ulpw_current = (uintptr_t)null_lpool.ulp_next;
	wsp->walk_data = ulpw;

	return (WALK_NEXT);
}

int
uutil_listpool_walk_step(mdb_walk_state_t *wsp)
{
	uu_list_pool_t ulp;
	uutil_listpool_walk_t *ulpw = wsp->walk_data;
	int status;

	if (mdb_vread(&ulp, sizeof (uu_list_pool_t),
	    ulpw->ulpw_current) == -1) {
		mdb_warn("failed to read uu_list_pool %x", ulpw->ulpw_current);
		return (WALK_ERR);
	}

	status = wsp->walk_callback(ulpw->ulpw_current, &ulp, wsp->walk_cbdata);

	if (ulpw->ulpw_current == ulpw->ulpw_final)
		return (WALK_DONE);

	ulpw->ulpw_current = (uintptr_t)ulp.ulp_next;

	return (status);
}

void
uutil_listpool_walk_fini(mdb_walk_state_t *wsp)
{
	uutil_listpool_walk_t *ulpw = wsp->walk_data;
	mdb_free(ulpw, sizeof (uutil_listpool_walk_t));
}

typedef struct uutil_list_walk {
	uintptr_t ulw_final;
	uintptr_t ulw_current;
} uutil_list_walk_t;

int
uutil_list_walk_init(mdb_walk_state_t *wsp)
{
	uutil_list_walk_t *ulw;
	uu_list_pool_t ulp;

	if (mdb_vread(&ulp, sizeof (uu_list_pool_t), wsp->walk_addr) == -1) {
		mdb_warn("failed to read uu_list_pool_t at given address\n");
		return (WALK_ERR);
	}

	if (UU_LIST_PTR(ulp.ulp_null_list.ul_next_enc) ==
	    &((uu_list_pool_t *)wsp->walk_addr)->ulp_null_list)
		return (WALK_DONE);

	ulw = mdb_alloc(sizeof (uutil_list_walk_t), UM_SLEEP);

	ulw->ulw_final = (uintptr_t)UU_LIST_PTR(ulp.ulp_null_list.ul_prev_enc);
	ulw->ulw_current =
	    (uintptr_t)UU_LIST_PTR(ulp.ulp_null_list.ul_next_enc);
	wsp->walk_data = ulw;

	return (WALK_NEXT);
}

int
uutil_list_walk_step(mdb_walk_state_t *wsp)
{
	uu_list_t ul;
	uutil_list_walk_t *ulw = wsp->walk_data;
	int status;

	if (mdb_vread(&ul, sizeof (uu_list_t), ulw->ulw_current) == -1) {
		mdb_warn("failed to read uu_list %x", ulw->ulw_current);
		return (WALK_ERR);
	}

	status = wsp->walk_callback(ulw->ulw_current, &ul, wsp->walk_cbdata);

	if (ulw->ulw_current == ulw->ulw_final)
		return (WALK_DONE);

	ulw->ulw_current = (uintptr_t)UU_LIST_PTR(ul.ul_next_enc);

	return (status);
}

void
uutil_list_walk_fini(mdb_walk_state_t *wsp)
{
	uutil_list_walk_t *ulw = wsp->walk_data;
	mdb_free(ulw, sizeof (uutil_list_walk_t));
}

typedef struct uutil_list_node_walk {
	size_t ulnw_offset;
	uintptr_t ulnw_final;
	uintptr_t ulnw_current;
	void *ulnw_buf;
	size_t ulnw_bufsz;
} uutil_list_node_walk_t;

int
uutil_list_node_walk_init(mdb_walk_state_t *wsp)
{
	uutil_list_node_walk_t *ulnw;
	uu_list_t ul;
	uu_list_pool_t ulp;

	if (mdb_vread(&ul, sizeof (uu_list_t), wsp->walk_addr) == -1) {
		mdb_warn("failed to read uu_list_t at given address\n");
		return (WALK_ERR);
	}

	if (mdb_vread(&ulp, sizeof (uu_list_pool_t), (uintptr_t)ul.ul_pool) ==
	    -1) {
		mdb_warn("failed to read supporting uu_list_pool_t\n");
		return (WALK_ERR);
	}

	ulnw = mdb_alloc(sizeof (uutil_list_node_walk_t), UM_SLEEP);

	ulnw->ulnw_offset = ul.ul_offset;
	ulnw->ulnw_final = wsp->walk_addr + offsetof(uu_list_t, ul_null_node);
	ulnw->ulnw_current = (uintptr_t)ul.ul_null_node.uln_next;
	ulnw->ulnw_buf = mdb_alloc(ulp.ulp_objsize, UM_SLEEP);
	ulnw->ulnw_bufsz = ulp.ulp_objsize;

	wsp->walk_data = ulnw;

	return (WALK_NEXT);
}

int
uutil_list_node_walk_step(mdb_walk_state_t *wsp)
{
	uu_list_node_impl_t uln;
	uutil_list_node_walk_t *ulnw = wsp->walk_data;
	int status;
	uintptr_t diff;

	if (ulnw->ulnw_current == ulnw->ulnw_final)
		return (WALK_DONE);

	if (mdb_vread(&uln, sizeof (uu_list_node_impl_t), ulnw->ulnw_current) ==
	    -1) {
		mdb_warn("failed to read uu_list_node %x", ulnw->ulnw_current);
		return (WALK_ERR);
	}

	diff = ulnw->ulnw_current - ulnw->ulnw_offset;

	if (mdb_vread(ulnw->ulnw_buf, ulnw->ulnw_bufsz, diff) == -1) {
		mdb_warn("failed to read enclosing structure %x", diff);
		return (WALK_ERR);
	}
	/*
	 * Correct for offset; we return the address of the included structure.
	 */
	status = wsp->walk_callback(diff, ulnw->ulnw_buf, wsp->walk_cbdata);

	ulnw->ulnw_current = (uintptr_t)uln.uln_next;

	return (status);
}

void
uutil_list_node_walk_fini(mdb_walk_state_t *wsp)
{
	uutil_list_node_walk_t *ulnw = wsp->walk_data;

	mdb_free(ulnw->ulnw_buf, ulnw->ulnw_bufsz);
	mdb_free(ulnw, sizeof (uutil_list_node_walk_t));
}

static const mdb_dcmd_t dcmds[] = {
	{ "uutil_status", NULL, "libuutil status summary", uutil_status },
	{ "uu_list_pool", NULL, "display uu_list_pool information",
		uutil_listpool },
	{ "uu_list", NULL, "display uu_list information",
		uutil_list },
	{ NULL }
};

static const mdb_walker_t walkers[] = {
	{ "uu_list_pool", "walk uu_list_pools",
		uutil_listpool_walk_init, uutil_listpool_walk_step,
		uutil_listpool_walk_fini },
	{ "uu_list", "given a uu_list_pool, walk its uu_lists",
		uutil_list_walk_init, uutil_list_walk_step,
		uutil_list_walk_fini },
	{ "uu_list_node",
		"given a uu_list, walk its nodes, returning container addr",
		uutil_list_node_walk_init, uutil_list_node_walk_step,
		uutil_list_node_walk_fini },
	{ NULL }
};

static const mdb_modinfo_t modinfo = {
	MDB_API_VERSION, dcmds, walkers
};

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}