xref: /illumos-gate/usr/src/cmd/mdb/common/modules/libsysevent/libsysevent.c (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/sysevent.h>
28 #include <sys/sysevent_impl.h>
29 #include <libsysevent.h>
30 #include <libsysevent_impl.h>
31 #include "../genunix/sysevent.h"
32 
33 int
34 sysevent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
35 {
36 	if ((flags & DCMD_ADDRSPEC) == 0)
37 		return (DCMD_USAGE);
38 
39 	if ((flags & DCMD_LOOP) == 0) {
40 		if (mdb_pwalk_dcmd("sysevent", "sysevent", argc, argv,
41 		    addr) == -1) {
42 			mdb_warn("can't walk sysevent queue");
43 			return (DCMD_ERR);
44 		}
45 		return (DCMD_OK);
46 	}
47 
48 	return (sysevent_buf(addr, flags, 0));
49 }
50 
51 /*ARGSUSED*/
52 int
53 sysevent_handle(uintptr_t addr, uint_t flags, int argc,
54     const mdb_arg_t *argv)
55 {
56 	ssize_t channel_name_sz;
57 	char channel_name[CLASS_LIST_FIELD_MAX];
58 	subscriber_priv_t sub;
59 	sysevent_impl_hdl_t sysevent_hdl;
60 
61 	if ((flags & DCMD_ADDRSPEC) == 0)
62 		return (DCMD_USAGE);
63 
64 
65 	if (argc != 0)
66 		return (DCMD_USAGE);
67 
68 
69 	if (mdb_vread(&sysevent_hdl, sizeof (sysevent_hdl),
70 	    (uintptr_t)addr) == -1) {
71 		mdb_warn("failed to read sysevent handle at %p", addr);
72 		return (DCMD_ERR);
73 	}
74 	if ((channel_name_sz = mdb_readstr(channel_name, CLASS_LIST_FIELD_MAX,
75 	    (uintptr_t)sysevent_hdl.sh_channel_name)) == -1) {
76 		mdb_warn("failed to read channel name at %p",
77 		    sysevent_hdl.sh_channel_name);
78 		return (DCMD_ERR);
79 	}
80 	if (channel_name_sz >= CLASS_LIST_FIELD_MAX - 1)
81 		(void) strcpy(&channel_name[CLASS_LIST_FIELD_MAX - 4], "...");
82 
83 	if (sysevent_hdl.sh_type == SUBSCRIBER) {
84 		if (mdb_vread(&sub, sizeof (sub),
85 		    (uintptr_t)sysevent_hdl.sh_priv_data) == -1) {
86 			mdb_warn("failed to read sysevent handle at %p", addr);
87 			return (DCMD_ERR);
88 		}
89 
90 		if (DCMD_HDRSPEC(flags))
91 			mdb_printf("%<u>%-?s %-24s %-13s %-5s %-?s"
92 			    "%</u>\n", "ADDR", "NAME", "TYPE", "ID",
93 			    "EVENT QUEUE ADDR");
94 
95 		mdb_printf("%-?p %-24s %-13s %-5lu %-?p\n",
96 		    addr, channel_name, "SUBSCRIBER", sysevent_hdl.sh_id,
97 		    (uintptr_t)sub.sp_evq_head);
98 
99 	} else {
100 		if (DCMD_HDRSPEC(flags))
101 			mdb_printf("%<u>%-?s %-24s %-13s %-5s %-?s"
102 			    "%</u>\n", "ADDR", "NAME",
103 			    "TYPE", "ID", "CLASS LIST ADDR");
104 
105 		mdb_printf("%-?p %-24s %-13s %-5lu %-?p\n",
106 		    addr, channel_name, "PUBLISHER", sysevent_hdl.sh_id,
107 		    (uintptr_t)sysevent_hdl.sh_priv_data +
108 		    offsetof(publisher_priv_t, pp_class_hash));
109 	}
110 
111 	return (DCMD_OK);
112 }
113 
114 static const mdb_dcmd_t dcmds[] = {
115 	{ "sysevent", "?[-v]", "print the contents of a sysevent queue",
116 		sysevent},
117 	{ "sysevent_handle", ":", "print sysevent subscriber/publisher handle",
118 		sysevent_handle},
119 	{ "sysevent_class_list", ":", "print sysevent class list",
120 		sysevent_class_list },
121 	{ "sysevent_subclass_list", ":", "print sysevent subclass list",
122 		sysevent_subclass_list },
123 	{ NULL }
124 };
125 
126 int
127 sysevent_walk_init(mdb_walk_state_t *wsp)
128 {
129 	if (wsp->walk_addr == 0) {
130 		mdb_warn("sysevent does not support global walks");
131 		return (WALK_ERR);
132 	}
133 
134 	return (WALK_NEXT);
135 }
136 
137 int
138 sysevent_walk_step(mdb_walk_state_t *wsp)
139 {
140 	int status;
141 	sysevent_queue_t se_q;
142 
143 	if (wsp->walk_addr == 0)
144 		return (WALK_DONE);
145 
146 	if (mdb_vread(&se_q, sizeof (se_q), wsp->walk_addr) == -1) {
147 		mdb_warn("failed to read sysevent queue at %p", wsp->walk_addr);
148 		return (WALK_ERR);
149 	}
150 
151 	status = wsp->walk_callback((uintptr_t)se_q.sq_ev, NULL,
152 	    wsp->walk_cbdata);
153 
154 	wsp->walk_addr = (uintptr_t)se_q.sq_next;
155 
156 	return (status);
157 }
158 
159 static const mdb_walker_t walkers[] = {
160 	{ "sysevent", "walk sysevent buffer queue",
161 		sysevent_walk_init, sysevent_walk_step,
162 		NULL },
163 	{ "sysevent_class_list", "walk sysevent subsccription class list",
164 		sysevent_class_list_walk_init, sysevent_class_list_walk_step,
165 		sysevent_class_list_walk_fini },
166 	{ "sysevent_subclass_list", "walk sysevent subsccription subclass list",
167 		sysevent_subclass_list_walk_init,
168 		sysevent_subclass_list_walk_step, NULL },
169 	{ NULL }
170 };
171 
172 static const mdb_modinfo_t modinfo = {
173 	MDB_API_VERSION, dcmds, walkers
174 };
175 
176 const mdb_modinfo_t *
177 _mdb_init(void)
178 {
179 	return (&modinfo);
180 }
181