1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * Active walks are tracked using a WCB (Walk Control Block), which is a simple
31*7c478bd9Sstevel@tonic-gate * data structure that contains the mdb_walk_state_t passed to the various
32*7c478bd9Sstevel@tonic-gate * walker functions for this particular walk, as well as links to other walk
33*7c478bd9Sstevel@tonic-gate * layers if this is a layered walk. The control block is kept in a list
34*7c478bd9Sstevel@tonic-gate * associated with the MDB frame, so that we can clean up all the walks and
35*7c478bd9Sstevel@tonic-gate * call their respective fini routines at the end of command processing.
36*7c478bd9Sstevel@tonic-gate */
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_module.h>
39*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
40*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
41*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_frame.h>
42*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
43*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_wcb.h>
44*7c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate mdb_wcb_t *
mdb_wcb_from_state(mdb_walk_state_t * wsp)47*7c478bd9Sstevel@tonic-gate mdb_wcb_from_state(mdb_walk_state_t *wsp)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate * The walk state passed to a walker sits at the start of the
51*7c478bd9Sstevel@tonic-gate * walk control block, so we can ask the walker to pass this
52*7c478bd9Sstevel@tonic-gate * back to us and quickly obtain the control block:
53*7c478bd9Sstevel@tonic-gate */
54*7c478bd9Sstevel@tonic-gate mdb_wcb_t *wcb = (mdb_wcb_t *)wsp;
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate if (wcb->w_buftag != WCB_TAG_ACTIVE && wcb->w_buftag != WCB_TAG_INITIAL)
57*7c478bd9Sstevel@tonic-gate fail("walk state %p is corrupt or not active\n", (void *)wcb);
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate return (wcb);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate mdb_wcb_t *
mdb_wcb_create(mdb_iwalker_t * iwp,mdb_walk_cb_t cb,void * data,uintptr_t addr)63*7c478bd9Sstevel@tonic-gate mdb_wcb_create(mdb_iwalker_t *iwp, mdb_walk_cb_t cb, void *data, uintptr_t addr)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate mdb_wcb_t *wcb = mdb_zalloc(sizeof (mdb_wcb_t), UM_SLEEP);
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate wcb->w_buftag = WCB_TAG_INITIAL;
68*7c478bd9Sstevel@tonic-gate wcb->w_walker = iwp;
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate wcb->w_state.walk_callback = cb;
71*7c478bd9Sstevel@tonic-gate wcb->w_state.walk_cbdata = data;
72*7c478bd9Sstevel@tonic-gate wcb->w_state.walk_addr = addr;
73*7c478bd9Sstevel@tonic-gate wcb->w_state.walk_arg = iwp->iwlk_init_arg;
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate return (wcb);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate void
mdb_wcb_destroy(mdb_wcb_t * wcb)79*7c478bd9Sstevel@tonic-gate mdb_wcb_destroy(mdb_wcb_t *wcb)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate mdb_wcb_t *p, *q;
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate for (p = wcb->w_lyr_head; p != NULL; p = q) {
84*7c478bd9Sstevel@tonic-gate q = wcb->w_lyr_link;
85*7c478bd9Sstevel@tonic-gate mdb_wcb_destroy(p);
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate if (wcb->w_inited)
89*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_fini(&wcb->w_state);
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate mdb_free(wcb, sizeof (mdb_wcb_t));
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate void
mdb_wcb_insert(mdb_wcb_t * wcb,mdb_frame_t * fp)95*7c478bd9Sstevel@tonic-gate mdb_wcb_insert(mdb_wcb_t *wcb, mdb_frame_t *fp)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate mdb_dprintf(MDB_DBG_WALK, "activate walk %s`%s wcb %p\n",
98*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_modp->mod_name,
99*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_name, (void *)wcb);
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate wcb->w_buftag = WCB_TAG_ACTIVE;
102*7c478bd9Sstevel@tonic-gate wcb->w_link = fp->f_wcbs;
103*7c478bd9Sstevel@tonic-gate fp->f_wcbs = wcb;
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate void
mdb_wcb_delete(mdb_wcb_t * wcb,mdb_frame_t * fp)107*7c478bd9Sstevel@tonic-gate mdb_wcb_delete(mdb_wcb_t *wcb, mdb_frame_t *fp)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate mdb_wcb_t **pp = &fp->f_wcbs;
110*7c478bd9Sstevel@tonic-gate mdb_wcb_t *w;
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate mdb_dprintf(MDB_DBG_WALK, "deactivate walk %s`%s wcb %p\n",
113*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_modp->mod_name,
114*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_name, (void *)wcb);
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate for (w = fp->f_wcbs; w != NULL; pp = &w->w_link, w = w->w_link) {
117*7c478bd9Sstevel@tonic-gate if (w == wcb) {
118*7c478bd9Sstevel@tonic-gate w->w_buftag = WCB_TAG_PASSIVE;
119*7c478bd9Sstevel@tonic-gate *pp = w->w_link;
120*7c478bd9Sstevel@tonic-gate return;
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate fail("attempted to remove wcb not on list: %p\n", (void *)wcb);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate void
mdb_wcb_purge(mdb_wcb_t ** wcbpp)128*7c478bd9Sstevel@tonic-gate mdb_wcb_purge(mdb_wcb_t **wcbpp)
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate mdb_wcb_t *n, *wcb = *wcbpp;
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate while (wcb != NULL) {
133*7c478bd9Sstevel@tonic-gate mdb_dprintf(MDB_DBG_WALK, "purge walk %s`%s wcb %p\n",
134*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_modp->mod_name,
135*7c478bd9Sstevel@tonic-gate wcb->w_walker->iwlk_name, (void *)wcb);
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate n = wcb->w_link;
138*7c478bd9Sstevel@tonic-gate mdb_wcb_destroy(wcb);
139*7c478bd9Sstevel@tonic-gate wcb = n;
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate *wcbpp = NULL;
143*7c478bd9Sstevel@tonic-gate }
144