list.c revision 6712:79afecec3f3c
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include <mdb/mdb_modapi.h>
29#include <sys/list.h>
30
31typedef struct list_walk_data {
32	uintptr_t lw_head;	/* address of list head */
33	size_t	lw_size;	/* size of list element */
34	size_t	lw_offset;	/* list element linkage offset */
35	void	*lw_obj;	/* buffer of lw_size to hold list element */
36	uintptr_t lw_end;	/* last node in specified range */
37	const char *lw_elem_name;
38	int	(*lw_elem_check)(void *, uintptr_t, void *);
39	void	*lw_elem_check_arg;
40} list_walk_data_t;
41
42/*
43 * Initialize a forward walk through a list.
44 *
45 * begin and end optionally specify objects other than the first and last
46 * objects in the list; either or both may be NULL (defaulting to first and
47 * last).
48 *
49 * list_name and element_name specify command-specific labels other than
50 * "list_t" and "list element" for use in error messages.
51 *
52 * element_check() returns -1, 1, or 0: abort the walk with an error, stop
53 * without an error, or allow the normal callback; arg is an optional user
54 * argument to element_check().
55 */
56int
57list_walk_init_range(mdb_walk_state_t *wsp, uintptr_t begin, uintptr_t end,
58    const char *list_name, const char *element_name,
59    int (*element_check)(void *, uintptr_t, void *), void *arg)
60{
61	list_walk_data_t *lwd;
62	list_t list;
63
64	if (list_name == NULL)
65		list_name = "list_t";
66	if (element_name == NULL)
67		element_name = "list element";
68
69	lwd = mdb_alloc(sizeof (list_walk_data_t), UM_SLEEP);
70	if (mdb_vread(&list, sizeof (list_t), wsp->walk_addr) == -1) {
71		mdb_warn("failed to read %s at %#lx", list_name,
72		    wsp->walk_addr);
73		mdb_free(lwd, sizeof (list_walk_data_t));
74		return (WALK_ERR);
75	}
76
77	lwd->lw_size = list.list_size;
78	lwd->lw_offset = list.list_offset;
79	lwd->lw_obj = mdb_alloc(list.list_size, UM_SLEEP);
80	lwd->lw_head = (uintptr_t)&((list_t *)wsp->walk_addr)->list_head;
81	lwd->lw_end = (end == NULL ? NULL : end + lwd->lw_offset);
82	lwd->lw_elem_name = element_name;
83	lwd->lw_elem_check = element_check;
84	lwd->lw_elem_check_arg = arg;
85
86	wsp->walk_addr = (begin == NULL
87	    ? (uintptr_t)list.list_head.list_next
88	    : begin + lwd->lw_offset);
89	wsp->walk_data = lwd;
90
91	return (WALK_NEXT);
92}
93
94int
95list_walk_init(mdb_walk_state_t *wsp)
96{
97	return (list_walk_init_range(wsp, NULL, NULL, NULL, NULL, NULL, NULL));
98}
99
100int
101list_walk_init_named(mdb_walk_state_t *wsp,
102    const char *list_name, const char *element_name)
103{
104	return (list_walk_init_range(wsp, NULL, NULL, list_name, element_name,
105	    NULL, NULL));
106}
107
108int
109list_walk_init_checked(mdb_walk_state_t *wsp,
110    const char *list_name, const char *element_name,
111    int (*element_check)(void *, uintptr_t, void *), void *arg)
112{
113	return (list_walk_init_range(wsp, NULL, NULL, list_name, element_name,
114	    element_check, arg));
115}
116
117int
118list_walk_step(mdb_walk_state_t *wsp)
119{
120	list_walk_data_t *lwd = wsp->walk_data;
121	uintptr_t addr = wsp->walk_addr - lwd->lw_offset;
122	list_node_t *node;
123	int status;
124
125	if (wsp->walk_addr == lwd->lw_head)
126		return (WALK_DONE);
127
128	if (lwd->lw_end != NULL && wsp->walk_addr == lwd->lw_end)
129		return (WALK_DONE);
130
131	if (mdb_vread(lwd->lw_obj, lwd->lw_size, addr) == -1) {
132		mdb_warn("failed to read %s at %#lx", lwd->lw_elem_name, addr);
133		return (WALK_ERR);
134	}
135
136	if (lwd->lw_elem_check != NULL) {
137		int rc = lwd->lw_elem_check(lwd->lw_obj, addr,
138		    lwd->lw_elem_check_arg);
139		if (rc == -1)
140			return (WALK_ERR);
141		else if (rc == 1)
142			return (WALK_DONE);
143	}
144
145	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
146	node = (list_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
147	wsp->walk_addr = (uintptr_t)node->list_next;
148
149	return (status);
150}
151
152void
153list_walk_fini(mdb_walk_state_t *wsp)
154{
155	list_walk_data_t *lwd = wsp->walk_data;
156
157	mdb_free(lwd->lw_obj, lwd->lw_size);
158	mdb_free(lwd, sizeof (list_walk_data_t));
159}
160