dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / modules / libuutil / libuutil.c
blobaa890302ec8f36a77a26fa3cf92707012b1a1339
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <mdb/mdb_modapi.h>
31 #include <pthread.h>
32 #include <stddef.h>
34 #include <libuutil.h>
35 #include <libuutil_impl.h>
37 /*ARGSUSED*/
38 static int
39 uutil_status(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
41 pthread_t uu_panic_thread = 0;
43 if ((flags & DCMD_ADDRSPEC) || argc != 0)
44 return (DCMD_USAGE);
46 if (mdb_readvar(&uu_panic_thread, "uu_panic_thread") == -1) {
47 mdb_warn("unable to read uu_panic_thread");
50 if (uu_panic_thread != 0) {
51 mdb_printf("thread %d uu_panicked\n", uu_panic_thread);
54 return (DCMD_OK);
57 /*ARGSUSED*/
58 static int
59 uutil_listpool(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
61 uu_list_pool_t ulp;
63 if (!(flags & DCMD_ADDRSPEC)) {
64 if (mdb_walk_dcmd("uu_list_pool", "uu_list_pool", argc,
65 argv) == -1) {
66 mdb_warn("can't walk uu_list_pool");
67 return (DCMD_ERR);
69 return (DCMD_OK);
72 if (argc != 0)
73 return (DCMD_USAGE);
75 if (DCMD_HDRSPEC(flags))
76 mdb_printf("%-?s %-30s %?s %5s\n",
77 "ADDR", "NAME", "COMPARE", "FLAGS");
79 if (mdb_vread(&ulp, sizeof (uu_list_pool_t), addr) == -1) {
80 mdb_warn("failed to read uu_list_pool\n");
81 return (DCMD_ERR);
84 mdb_printf("%0?p %-30s %08x %c\n", addr, ulp.ulp_name, ulp.ulp_cmp,
85 ulp.ulp_debug ? 'D' : ' ');
87 return (DCMD_OK);
90 /*ARGSUSED*/
91 static int
92 uutil_list(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
94 uu_list_t ul;
96 if (!(flags & DCMD_ADDRSPEC) || argc != 0)
97 return (DCMD_USAGE);
99 if (mdb_vread(&ul, sizeof (uu_list_t), addr) == -1) {
100 mdb_warn("failed to read uu_list\n");
101 return (DCMD_ERR);
104 if (DCMD_HDRSPEC(flags))
105 mdb_printf("%-?s %-?s %-?s %6s %5s\n",
106 "ADDR", "POOL", "PARENT", "NODES", "FLAGS");
108 mdb_printf("%0?p %0?p %0?p %6u %c%c\n",
109 addr, ul.ul_pool, UU_PTR_DECODE(ul.ul_parent_enc),
110 ul.ul_numnodes, ul.ul_sorted ? 'S' : ' ', ul.ul_debug? 'D' : ' ');
112 return (DCMD_OK);
115 typedef struct uutil_listpool_walk {
116 uintptr_t ulpw_final;
117 uintptr_t ulpw_current;
118 } uutil_listpool_walk_t;
121 uutil_listpool_walk_init(mdb_walk_state_t *wsp)
123 uu_list_pool_t null_lpool;
124 uutil_listpool_walk_t *ulpw;
125 GElf_Sym sym;
127 bzero(&null_lpool, sizeof (uu_list_pool_t));
129 if (mdb_lookup_by_obj("libuutil.so.1", "uu_null_lpool", &sym) ==
130 -1) {
131 mdb_warn("failed to find 'uu_null_lpool'\n");
132 return (WALK_ERR);
135 if (mdb_vread(&null_lpool, sym.st_size, (uintptr_t)sym.st_value) ==
136 -1) {
137 mdb_warn("failed to read data from 'uu_null_lpool' address\n");
138 return (WALK_ERR);
141 ulpw = mdb_alloc(sizeof (uutil_listpool_walk_t), UM_SLEEP);
143 ulpw->ulpw_final = (uintptr_t)null_lpool.ulp_prev;
144 ulpw->ulpw_current = (uintptr_t)null_lpool.ulp_next;
145 wsp->walk_data = ulpw;
147 return (WALK_NEXT);
151 uutil_listpool_walk_step(mdb_walk_state_t *wsp)
153 uu_list_pool_t ulp;
154 uutil_listpool_walk_t *ulpw = wsp->walk_data;
155 int status;
157 if (mdb_vread(&ulp, sizeof (uu_list_pool_t),
158 ulpw->ulpw_current) == -1) {
159 mdb_warn("failed to read uu_list_pool %x", ulpw->ulpw_current);
160 return (WALK_ERR);
163 status = wsp->walk_callback(ulpw->ulpw_current, &ulp, wsp->walk_cbdata);
165 if (ulpw->ulpw_current == ulpw->ulpw_final)
166 return (WALK_DONE);
168 ulpw->ulpw_current = (uintptr_t)ulp.ulp_next;
170 return (status);
173 void
174 uutil_listpool_walk_fini(mdb_walk_state_t *wsp)
176 uutil_listpool_walk_t *ulpw = wsp->walk_data;
177 mdb_free(ulpw, sizeof (uutil_listpool_walk_t));
180 typedef struct uutil_list_walk {
181 uintptr_t ulw_final;
182 uintptr_t ulw_current;
183 } uutil_list_walk_t;
186 uutil_list_walk_init(mdb_walk_state_t *wsp)
188 uutil_list_walk_t *ulw;
189 uu_list_pool_t ulp;
191 if (mdb_vread(&ulp, sizeof (uu_list_pool_t), wsp->walk_addr) == -1) {
192 mdb_warn("failed to read uu_list_pool_t at given address\n");
193 return (WALK_ERR);
196 if (UU_LIST_PTR(ulp.ulp_null_list.ul_next_enc) ==
197 &((uu_list_pool_t *)wsp->walk_addr)->ulp_null_list)
198 return (WALK_DONE);
200 ulw = mdb_alloc(sizeof (uutil_list_walk_t), UM_SLEEP);
202 ulw->ulw_final = (uintptr_t)UU_LIST_PTR(ulp.ulp_null_list.ul_prev_enc);
203 ulw->ulw_current =
204 (uintptr_t)UU_LIST_PTR(ulp.ulp_null_list.ul_next_enc);
205 wsp->walk_data = ulw;
207 return (WALK_NEXT);
211 uutil_list_walk_step(mdb_walk_state_t *wsp)
213 uu_list_t ul;
214 uutil_list_walk_t *ulw = wsp->walk_data;
215 int status;
217 if (mdb_vread(&ul, sizeof (uu_list_t), ulw->ulw_current) == -1) {
218 mdb_warn("failed to read uu_list %x", ulw->ulw_current);
219 return (WALK_ERR);
222 status = wsp->walk_callback(ulw->ulw_current, &ul, wsp->walk_cbdata);
224 if (ulw->ulw_current == ulw->ulw_final)
225 return (WALK_DONE);
227 ulw->ulw_current = (uintptr_t)UU_LIST_PTR(ul.ul_next_enc);
229 return (status);
232 void
233 uutil_list_walk_fini(mdb_walk_state_t *wsp)
235 uutil_list_walk_t *ulw = wsp->walk_data;
236 mdb_free(ulw, sizeof (uutil_list_walk_t));
239 typedef struct uutil_list_node_walk {
240 size_t ulnw_offset;
241 uintptr_t ulnw_final;
242 uintptr_t ulnw_current;
243 void *ulnw_buf;
244 size_t ulnw_bufsz;
245 } uutil_list_node_walk_t;
248 uutil_list_node_walk_init(mdb_walk_state_t *wsp)
250 uutil_list_node_walk_t *ulnw;
251 uu_list_t ul;
252 uu_list_pool_t ulp;
254 if (mdb_vread(&ul, sizeof (uu_list_t), wsp->walk_addr) == -1) {
255 mdb_warn("failed to read uu_list_t at given address\n");
256 return (WALK_ERR);
259 if (mdb_vread(&ulp, sizeof (uu_list_pool_t), (uintptr_t)ul.ul_pool) ==
260 -1) {
261 mdb_warn("failed to read supporting uu_list_pool_t\n");
262 return (WALK_ERR);
265 ulnw = mdb_alloc(sizeof (uutil_list_node_walk_t), UM_SLEEP);
267 ulnw->ulnw_offset = ul.ul_offset;
268 ulnw->ulnw_final = wsp->walk_addr + offsetof(uu_list_t, ul_null_node);
269 ulnw->ulnw_current = (uintptr_t)ul.ul_null_node.uln_next;
270 ulnw->ulnw_buf = mdb_alloc(ulp.ulp_objsize, UM_SLEEP);
271 ulnw->ulnw_bufsz = ulp.ulp_objsize;
273 wsp->walk_data = ulnw;
275 return (WALK_NEXT);
279 uutil_list_node_walk_step(mdb_walk_state_t *wsp)
281 uu_list_node_impl_t uln;
282 uutil_list_node_walk_t *ulnw = wsp->walk_data;
283 int status;
284 uintptr_t diff;
286 if (ulnw->ulnw_current == ulnw->ulnw_final)
287 return (WALK_DONE);
289 if (mdb_vread(&uln, sizeof (uu_list_node_impl_t), ulnw->ulnw_current) ==
290 -1) {
291 mdb_warn("failed to read uu_list_node %x", ulnw->ulnw_current);
292 return (WALK_ERR);
295 diff = ulnw->ulnw_current - ulnw->ulnw_offset;
297 if (mdb_vread(ulnw->ulnw_buf, ulnw->ulnw_bufsz, diff) == -1) {
298 mdb_warn("failed to read enclosing structure %x", diff);
299 return (WALK_ERR);
302 * Correct for offset; we return the address of the included structure.
304 status = wsp->walk_callback(diff, ulnw->ulnw_buf, wsp->walk_cbdata);
306 ulnw->ulnw_current = (uintptr_t)uln.uln_next;
308 return (status);
311 void
312 uutil_list_node_walk_fini(mdb_walk_state_t *wsp)
314 uutil_list_node_walk_t *ulnw = wsp->walk_data;
316 mdb_free(ulnw->ulnw_buf, ulnw->ulnw_bufsz);
317 mdb_free(ulnw, sizeof (uutil_list_node_walk_t));
320 static const mdb_dcmd_t dcmds[] = {
321 { "uutil_status", NULL, "libuutil status summary", uutil_status },
322 { "uu_list_pool", NULL, "display uu_list_pool information",
323 uutil_listpool },
324 { "uu_list", NULL, "display uu_list information",
325 uutil_list },
326 { NULL }
329 static const mdb_walker_t walkers[] = {
330 { "uu_list_pool", "walk uu_list_pools",
331 uutil_listpool_walk_init, uutil_listpool_walk_step,
332 uutil_listpool_walk_fini },
333 { "uu_list", "given a uu_list_pool, walk its uu_lists",
334 uutil_list_walk_init, uutil_list_walk_step,
335 uutil_list_walk_fini },
336 { "uu_list_node",
337 "given a uu_list, walk its nodes, returning container addr",
338 uutil_list_node_walk_init, uutil_list_node_walk_step,
339 uutil_list_node_walk_fini },
340 { NULL }
343 static const mdb_modinfo_t modinfo = {
344 MDB_API_VERSION, dcmds, walkers
347 const mdb_modinfo_t *
348 _mdb_init(void)
350 return (&modinfo);