xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_callb.c (revision 2a8bcb4e)
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 2005 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 /*
28*7c478bd9Sstevel@tonic-gate  * Callback facility designed to allow interested parties (dmods, targets, or
29*7c478bd9Sstevel@tonic-gate  * even the core debugger framework) to register for notification when certain
30*7c478bd9Sstevel@tonic-gate  * "interesting" events occur.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_list.h>
34*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
35*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_callb.h>
36*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_module.h>
37*7c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate mdb_callb_t *
mdb_callb_add(mdb_module_t * m,int class,mdb_callb_f fp,void * arg)40*7c478bd9Sstevel@tonic-gate mdb_callb_add(mdb_module_t *m, int class, mdb_callb_f fp, void *arg)
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	mdb_callb_t *new = mdb_zalloc(sizeof (mdb_callb_t), UM_SLEEP);
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	ASSERT(class == MDB_CALLB_STCHG || class == MDB_CALLB_PROMPT);
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate 	new->cb_mod = m;
47*7c478bd9Sstevel@tonic-gate 	new->cb_class = class;
48*7c478bd9Sstevel@tonic-gate 	new->cb_func = fp;
49*7c478bd9Sstevel@tonic-gate 	new->cb_arg = arg;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	if (m == NULL) {
52*7c478bd9Sstevel@tonic-gate 		mdb_list_prepend(&mdb.m_cblist, new);
53*7c478bd9Sstevel@tonic-gate 	} else {
54*7c478bd9Sstevel@tonic-gate 		mdb_list_insert(&mdb.m_cblist, m->mod_cb, new);
55*7c478bd9Sstevel@tonic-gate 		if (m->mod_cb == NULL)
56*7c478bd9Sstevel@tonic-gate 			m->mod_cb = new;
57*7c478bd9Sstevel@tonic-gate 	}
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	return (new);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate void
mdb_callb_remove(mdb_callb_t * cb)63*7c478bd9Sstevel@tonic-gate mdb_callb_remove(mdb_callb_t *cb)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	if (cb->cb_mod != NULL) {
66*7c478bd9Sstevel@tonic-gate 		mdb_callb_t *next = mdb_list_next(cb);
67*7c478bd9Sstevel@tonic-gate 		mdb_module_t *mod = cb->cb_mod;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 		if (mod->mod_cb == cb) {
70*7c478bd9Sstevel@tonic-gate 			if (next == NULL || next->cb_mod != mod)
71*7c478bd9Sstevel@tonic-gate 				mod->mod_cb = NULL;
72*7c478bd9Sstevel@tonic-gate 			else
73*7c478bd9Sstevel@tonic-gate 				mod->mod_cb = next;
74*7c478bd9Sstevel@tonic-gate 		}
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	mdb_list_delete(&mdb.m_cblist, cb);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	mdb_free(cb, sizeof (mdb_callb_t));
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate void
mdb_callb_remove_by_mod(mdb_module_t * m)83*7c478bd9Sstevel@tonic-gate mdb_callb_remove_by_mod(mdb_module_t *m)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	while (m->mod_cb != NULL)
86*7c478bd9Sstevel@tonic-gate 		mdb_callb_remove(m->mod_cb);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate void
mdb_callb_remove_all(void)90*7c478bd9Sstevel@tonic-gate mdb_callb_remove_all(void)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	mdb_callb_t *cb;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	while ((cb = mdb_list_next(&mdb.m_cblist)) != NULL)
95*7c478bd9Sstevel@tonic-gate 		mdb_callb_remove(cb);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate void
mdb_callb_fire(int class)99*7c478bd9Sstevel@tonic-gate mdb_callb_fire(int class)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	mdb_callb_t *cb, *next;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	ASSERT(class == MDB_CALLB_STCHG || class == MDB_CALLB_PROMPT);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_CALLB, "invoking %s callbacks\n",
106*7c478bd9Sstevel@tonic-gate 	    (class == MDB_CALLB_STCHG ? "state change" : "prompt"));
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	for (cb = mdb_list_next(&mdb.m_cblist); cb != NULL; cb = next) {
109*7c478bd9Sstevel@tonic-gate 		next = mdb_list_next(cb);
110*7c478bd9Sstevel@tonic-gate 		if (cb->cb_class == class)
111*7c478bd9Sstevel@tonic-gate 			cb->cb_func(cb->cb_arg);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate }
114