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