17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5b5fca8f8Stomee * Common Development and Distribution License (the "License").
6b5fca8f8Stomee * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22b5fca8f8Stomee * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
259889d1c6SMarcel Telka /*
269889d1c6SMarcel Telka * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
279889d1c6SMarcel Telka */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
307c478bd9Sstevel@tonic-gate #include <sys/list.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate typedef struct list_walk_data {
33b5fca8f8Stomee uintptr_t lw_head; /* address of list head */
34b5fca8f8Stomee size_t lw_size; /* size of list element */
35b5fca8f8Stomee size_t lw_offset; /* list element linkage offset */
36b5fca8f8Stomee void *lw_obj; /* buffer of lw_size to hold list element */
37b5fca8f8Stomee uintptr_t lw_end; /* last node in specified range */
38b5fca8f8Stomee const char *lw_elem_name;
39b5fca8f8Stomee int (*lw_elem_check)(void *, uintptr_t, void *);
40b5fca8f8Stomee void *lw_elem_check_arg;
417c478bd9Sstevel@tonic-gate } list_walk_data_t;
427c478bd9Sstevel@tonic-gate
43b5fca8f8Stomee /*
44b5fca8f8Stomee * Initialize a forward walk through a list.
45b5fca8f8Stomee *
46b5fca8f8Stomee * begin and end optionally specify objects other than the first and last
47b5fca8f8Stomee * objects in the list; either or both may be NULL (defaulting to first and
48b5fca8f8Stomee * last).
49b5fca8f8Stomee *
50b5fca8f8Stomee * list_name and element_name specify command-specific labels other than
51b5fca8f8Stomee * "list_t" and "list element" for use in error messages.
52b5fca8f8Stomee *
53b5fca8f8Stomee * element_check() returns -1, 1, or 0: abort the walk with an error, stop
54b5fca8f8Stomee * without an error, or allow the normal callback; arg is an optional user
55b5fca8f8Stomee * argument to element_check().
56b5fca8f8Stomee */
577c478bd9Sstevel@tonic-gate int
list_walk_init_range(mdb_walk_state_t * wsp,uintptr_t begin,uintptr_t end,const char * list_name,const char * element_name,int (* element_check)(void *,uintptr_t,void *),void * arg)58b5fca8f8Stomee list_walk_init_range(mdb_walk_state_t *wsp, uintptr_t begin, uintptr_t end,
59b5fca8f8Stomee const char *list_name, const char *element_name,
60b5fca8f8Stomee int (*element_check)(void *, uintptr_t, void *), void *arg)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate list_walk_data_t *lwd;
637c478bd9Sstevel@tonic-gate list_t list;
647c478bd9Sstevel@tonic-gate
65b5fca8f8Stomee if (list_name == NULL)
66b5fca8f8Stomee list_name = "list_t";
67b5fca8f8Stomee if (element_name == NULL)
68b5fca8f8Stomee element_name = "list element";
69b5fca8f8Stomee
707c478bd9Sstevel@tonic-gate if (mdb_vread(&list, sizeof (list_t), wsp->walk_addr) == -1) {
71b5fca8f8Stomee mdb_warn("failed to read %s at %#lx", list_name,
72b5fca8f8Stomee wsp->walk_addr);
737c478bd9Sstevel@tonic-gate return (WALK_ERR);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
769889d1c6SMarcel Telka if (list.list_size < list.list_offset + sizeof (list_node_t)) {
779889d1c6SMarcel Telka mdb_warn("invalid or uninitialized %s at %#lx\n", list_name,
789889d1c6SMarcel Telka wsp->walk_addr);
799889d1c6SMarcel Telka return (WALK_ERR);
809889d1c6SMarcel Telka }
819889d1c6SMarcel Telka
829889d1c6SMarcel Telka lwd = mdb_alloc(sizeof (list_walk_data_t), UM_SLEEP);
839889d1c6SMarcel Telka
847c478bd9Sstevel@tonic-gate lwd->lw_size = list.list_size;
857c478bd9Sstevel@tonic-gate lwd->lw_offset = list.list_offset;
867c478bd9Sstevel@tonic-gate lwd->lw_obj = mdb_alloc(list.list_size, UM_SLEEP);
87b5fca8f8Stomee lwd->lw_head = (uintptr_t)&((list_t *)wsp->walk_addr)->list_head;
88*892ad162SToomas Soome lwd->lw_end = (end == 0 ? 0 : end + lwd->lw_offset);
89b5fca8f8Stomee lwd->lw_elem_name = element_name;
90b5fca8f8Stomee lwd->lw_elem_check = element_check;
91b5fca8f8Stomee lwd->lw_elem_check_arg = arg;
927c478bd9Sstevel@tonic-gate
93*892ad162SToomas Soome wsp->walk_addr = (begin == 0
94b5fca8f8Stomee ? (uintptr_t)list.list_head.list_next
95b5fca8f8Stomee : begin + lwd->lw_offset);
967c478bd9Sstevel@tonic-gate wsp->walk_data = lwd;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate return (WALK_NEXT);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
101b5fca8f8Stomee int
list_walk_init(mdb_walk_state_t * wsp)102b5fca8f8Stomee list_walk_init(mdb_walk_state_t *wsp)
103b5fca8f8Stomee {
104*892ad162SToomas Soome return (list_walk_init_range(wsp, 0, 0, NULL, NULL, NULL, NULL));
105b5fca8f8Stomee }
106b5fca8f8Stomee
107b5fca8f8Stomee int
list_walk_init_named(mdb_walk_state_t * wsp,const char * list_name,const char * element_name)108b5fca8f8Stomee list_walk_init_named(mdb_walk_state_t *wsp,
109b5fca8f8Stomee const char *list_name, const char *element_name)
110b5fca8f8Stomee {
111*892ad162SToomas Soome return (list_walk_init_range(wsp, 0, 0, list_name, element_name,
112b5fca8f8Stomee NULL, NULL));
113b5fca8f8Stomee }
114b5fca8f8Stomee
115b5fca8f8Stomee int
list_walk_init_checked(mdb_walk_state_t * wsp,const char * list_name,const char * element_name,int (* element_check)(void *,uintptr_t,void *),void * arg)116b5fca8f8Stomee list_walk_init_checked(mdb_walk_state_t *wsp,
117b5fca8f8Stomee const char *list_name, const char *element_name,
118b5fca8f8Stomee int (*element_check)(void *, uintptr_t, void *), void *arg)
119b5fca8f8Stomee {
120*892ad162SToomas Soome return (list_walk_init_range(wsp, 0, 0, list_name, element_name,
121b5fca8f8Stomee element_check, arg));
122b5fca8f8Stomee }
123b5fca8f8Stomee
1247c478bd9Sstevel@tonic-gate int
list_walk_step(mdb_walk_state_t * wsp)1257c478bd9Sstevel@tonic-gate list_walk_step(mdb_walk_state_t *wsp)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate list_walk_data_t *lwd = wsp->walk_data;
1287c478bd9Sstevel@tonic-gate uintptr_t addr = wsp->walk_addr - lwd->lw_offset;
1297c478bd9Sstevel@tonic-gate list_node_t *node;
1307c478bd9Sstevel@tonic-gate int status;
1317c478bd9Sstevel@tonic-gate
132b5fca8f8Stomee if (wsp->walk_addr == lwd->lw_head)
133b5fca8f8Stomee return (WALK_DONE);
134b5fca8f8Stomee
135*892ad162SToomas Soome if (lwd->lw_end != 0 && wsp->walk_addr == lwd->lw_end)
1367c478bd9Sstevel@tonic-gate return (WALK_DONE);
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (mdb_vread(lwd->lw_obj, lwd->lw_size, addr) == -1) {
139b5fca8f8Stomee mdb_warn("failed to read %s at %#lx", lwd->lw_elem_name, addr);
1407c478bd9Sstevel@tonic-gate return (WALK_ERR);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
143b5fca8f8Stomee if (lwd->lw_elem_check != NULL) {
144b5fca8f8Stomee int rc = lwd->lw_elem_check(lwd->lw_obj, addr,
145b5fca8f8Stomee lwd->lw_elem_check_arg);
146b5fca8f8Stomee if (rc == -1)
147b5fca8f8Stomee return (WALK_ERR);
148b5fca8f8Stomee else if (rc == 1)
149b5fca8f8Stomee return (WALK_DONE);
150b5fca8f8Stomee }
151b5fca8f8Stomee
1527c478bd9Sstevel@tonic-gate status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
1537c478bd9Sstevel@tonic-gate node = (list_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
1547c478bd9Sstevel@tonic-gate wsp->walk_addr = (uintptr_t)node->list_next;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate return (status);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate void
list_walk_fini(mdb_walk_state_t * wsp)1607c478bd9Sstevel@tonic-gate list_walk_fini(mdb_walk_state_t *wsp)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate list_walk_data_t *lwd = wsp->walk_data;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate mdb_free(lwd->lw_obj, lwd->lw_size);
1657c478bd9Sstevel@tonic-gate mdb_free(lwd, sizeof (list_walk_data_t));
1667c478bd9Sstevel@tonic-gate }
167