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 * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 27 */ 28 29 #include <mdb/mdb_modapi.h> 30 #include <sys/list.h> 31 32 typedef struct list_walk_data { 33 uintptr_t lw_head; /* address of list head */ 34 size_t lw_size; /* size of list element */ 35 size_t lw_offset; /* list element linkage offset */ 36 void *lw_obj; /* buffer of lw_size to hold list element */ 37 uintptr_t lw_end; /* last node in specified range */ 38 const char *lw_elem_name; 39 int (*lw_elem_check)(void *, uintptr_t, void *); 40 void *lw_elem_check_arg; 41 } list_walk_data_t; 42 43 /* 44 * Initialize a forward walk through a list. 45 * 46 * begin and end optionally specify objects other than the first and last 47 * objects in the list; either or both may be NULL (defaulting to first and 48 * last). 49 * 50 * list_name and element_name specify command-specific labels other than 51 * "list_t" and "list element" for use in error messages. 52 * 53 * element_check() returns -1, 1, or 0: abort the walk with an error, stop 54 * without an error, or allow the normal callback; arg is an optional user 55 * argument to element_check(). 56 */ 57 int 58 list_walk_init_range(mdb_walk_state_t *wsp, uintptr_t begin, uintptr_t end, 59 const char *list_name, const char *element_name, 60 int (*element_check)(void *, uintptr_t, void *), void *arg) 61 { 62 list_walk_data_t *lwd; 63 list_t list; 64 65 if (list_name == NULL) 66 list_name = "list_t"; 67 if (element_name == NULL) 68 element_name = "list element"; 69 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 return (WALK_ERR); 74 } 75 76 if (list.list_size < list.list_offset + sizeof (list_node_t)) { 77 mdb_warn("invalid or uninitialized %s at %#lx\n", list_name, 78 wsp->walk_addr); 79 return (WALK_ERR); 80 } 81 82 lwd = mdb_alloc(sizeof (list_walk_data_t), UM_SLEEP); 83 84 lwd->lw_size = list.list_size; 85 lwd->lw_offset = list.list_offset; 86 lwd->lw_obj = mdb_alloc(list.list_size, UM_SLEEP); 87 lwd->lw_head = (uintptr_t)&((list_t *)wsp->walk_addr)->list_head; 88 lwd->lw_end = (end == NULL ? NULL : end + lwd->lw_offset); 89 lwd->lw_elem_name = element_name; 90 lwd->lw_elem_check = element_check; 91 lwd->lw_elem_check_arg = arg; 92 93 wsp->walk_addr = (begin == NULL 94 ? (uintptr_t)list.list_head.list_next 95 : begin + lwd->lw_offset); 96 wsp->walk_data = lwd; 97 98 return (WALK_NEXT); 99 } 100 101 int 102 list_walk_init(mdb_walk_state_t *wsp) 103 { 104 return (list_walk_init_range(wsp, NULL, NULL, NULL, NULL, NULL, NULL)); 105 } 106 107 int 108 list_walk_init_named(mdb_walk_state_t *wsp, 109 const char *list_name, const char *element_name) 110 { 111 return (list_walk_init_range(wsp, NULL, NULL, list_name, element_name, 112 NULL, NULL)); 113 } 114 115 int 116 list_walk_init_checked(mdb_walk_state_t *wsp, 117 const char *list_name, const char *element_name, 118 int (*element_check)(void *, uintptr_t, void *), void *arg) 119 { 120 return (list_walk_init_range(wsp, NULL, NULL, list_name, element_name, 121 element_check, arg)); 122 } 123 124 int 125 list_walk_step(mdb_walk_state_t *wsp) 126 { 127 list_walk_data_t *lwd = wsp->walk_data; 128 uintptr_t addr = wsp->walk_addr - lwd->lw_offset; 129 list_node_t *node; 130 int status; 131 132 if (wsp->walk_addr == lwd->lw_head) 133 return (WALK_DONE); 134 135 if (lwd->lw_end != NULL && wsp->walk_addr == lwd->lw_end) 136 return (WALK_DONE); 137 138 if (mdb_vread(lwd->lw_obj, lwd->lw_size, addr) == -1) { 139 mdb_warn("failed to read %s at %#lx", lwd->lw_elem_name, addr); 140 return (WALK_ERR); 141 } 142 143 if (lwd->lw_elem_check != NULL) { 144 int rc = lwd->lw_elem_check(lwd->lw_obj, addr, 145 lwd->lw_elem_check_arg); 146 if (rc == -1) 147 return (WALK_ERR); 148 else if (rc == 1) 149 return (WALK_DONE); 150 } 151 152 status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata); 153 node = (list_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset); 154 wsp->walk_addr = (uintptr_t)node->list_next; 155 156 return (status); 157 } 158 159 void 160 list_walk_fini(mdb_walk_state_t *wsp) 161 { 162 list_walk_data_t *lwd = wsp->walk_data; 163 164 mdb_free(lwd->lw_obj, lwd->lw_size); 165 mdb_free(lwd, sizeof (list_walk_data_t)); 166 } 167