xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_callb.c (revision 2a8bcb4e)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Callback facility designed to allow interested parties (dmods, targets, or
29  * even the core debugger framework) to register for notification when certain
30  * "interesting" events occur.
31  */
32 
33 #include <mdb/mdb_list.h>
34 #include <mdb/mdb_debug.h>
35 #include <mdb/mdb_callb.h>
36 #include <mdb/mdb_module.h>
37 #include <mdb/mdb.h>
38 
39 mdb_callb_t *
mdb_callb_add(mdb_module_t * m,int class,mdb_callb_f fp,void * arg)40 mdb_callb_add(mdb_module_t *m, int class, mdb_callb_f fp, void *arg)
41 {
42 	mdb_callb_t *new = mdb_zalloc(sizeof (mdb_callb_t), UM_SLEEP);
43 
44 	ASSERT(class == MDB_CALLB_STCHG || class == MDB_CALLB_PROMPT);
45 
46 	new->cb_mod = m;
47 	new->cb_class = class;
48 	new->cb_func = fp;
49 	new->cb_arg = arg;
50 
51 	if (m == NULL) {
52 		mdb_list_prepend(&mdb.m_cblist, new);
53 	} else {
54 		mdb_list_insert(&mdb.m_cblist, m->mod_cb, new);
55 		if (m->mod_cb == NULL)
56 			m->mod_cb = new;
57 	}
58 
59 	return (new);
60 }
61 
62 void
mdb_callb_remove(mdb_callb_t * cb)63 mdb_callb_remove(mdb_callb_t *cb)
64 {
65 	if (cb->cb_mod != NULL) {
66 		mdb_callb_t *next = mdb_list_next(cb);
67 		mdb_module_t *mod = cb->cb_mod;
68 
69 		if (mod->mod_cb == cb) {
70 			if (next == NULL || next->cb_mod != mod)
71 				mod->mod_cb = NULL;
72 			else
73 				mod->mod_cb = next;
74 		}
75 	}
76 
77 	mdb_list_delete(&mdb.m_cblist, cb);
78 
79 	mdb_free(cb, sizeof (mdb_callb_t));
80 }
81 
82 void
mdb_callb_remove_by_mod(mdb_module_t * m)83 mdb_callb_remove_by_mod(mdb_module_t *m)
84 {
85 	while (m->mod_cb != NULL)
86 		mdb_callb_remove(m->mod_cb);
87 }
88 
89 void
mdb_callb_remove_all(void)90 mdb_callb_remove_all(void)
91 {
92 	mdb_callb_t *cb;
93 
94 	while ((cb = mdb_list_next(&mdb.m_cblist)) != NULL)
95 		mdb_callb_remove(cb);
96 }
97 
98 void
mdb_callb_fire(int class)99 mdb_callb_fire(int class)
100 {
101 	mdb_callb_t *cb, *next;
102 
103 	ASSERT(class == MDB_CALLB_STCHG || class == MDB_CALLB_PROMPT);
104 
105 	mdb_dprintf(MDB_DBG_CALLB, "invoking %s callbacks\n",
106 	    (class == MDB_CALLB_STCHG ? "state change" : "prompt"));
107 
108 	for (cb = mdb_list_next(&mdb.m_cblist); cb != NULL; cb = next) {
109 		next = mdb_list_next(cb);
110 		if (cb->cb_class == class)
111 			cb->cb_func(cb->cb_arg);
112 	}
113 }
114