xref: /illumos-gate/usr/src/cmd/mdb/common/modules/genunix/contract.c (revision e121b61f5e8ffbeb2f6b373c967c80351333ee21)
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 #include <mdb/mdb_modapi.h>
28 #include <sys/types.h>
29 #include <sys/contract_impl.h>
30 
31 int
32 ct_walk_init(mdb_walk_state_t *wsp)
33 {
34 	if (wsp->walk_addr != 0) {
35 		wsp->walk_addr = wsp->walk_addr +
36 		    OFFSETOF(ct_type_t, ct_type_avl);
37 	} else {
38 		GElf_Sym sym;
39 		if (mdb_lookup_by_name("contract_avl", &sym)) {
40 			mdb_warn("failed to read contract_avl");
41 			return (WALK_ERR);
42 		}
43 		wsp->walk_addr = sym.st_value;
44 	}
45 
46 	if (mdb_layered_walk("avl", wsp) == -1)
47 		return (WALK_ERR);
48 
49 	return (WALK_NEXT);
50 }
51 
52 int
53 ct_event_walk_init(mdb_walk_state_t *wsp)
54 {
55 	if (wsp->walk_addr == 0) {
56 		mdb_warn("ct_event walker requires ct_equeue address\n");
57 		return (WALK_ERR);
58 	}
59 
60 	wsp->walk_addr = wsp->walk_addr +
61 	    OFFSETOF(ct_equeue_t, ctq_events);
62 
63 	if (mdb_layered_walk("list", wsp) == -1)
64 		return (WALK_ERR);
65 
66 	return (WALK_NEXT);
67 }
68 
69 int
70 ct_listener_walk_init(mdb_walk_state_t *wsp)
71 {
72 	if (wsp->walk_addr == 0) {
73 		mdb_warn("ct_listener walker requires ct_equeue address\n");
74 		return (WALK_ERR);
75 	}
76 
77 	wsp->walk_addr = wsp->walk_addr +
78 	    OFFSETOF(ct_equeue_t, ctq_listeners);
79 
80 	if (mdb_layered_walk("list", wsp) == -1)
81 		return (WALK_ERR);
82 
83 	return (WALK_NEXT);
84 }
85 
86 
87 /* ARGSUSED */
88 int
89 cmd_contract(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
90 {
91 	contract_t ct;
92 	ct_type_t ctt;
93 	char str[32];
94 
95 	if (!(flags & DCMD_ADDRSPEC)) {
96 		if (mdb_walk_dcmd("contract", "contract", argc, argv) == -1) {
97 			mdb_warn("can't walk 'contract'");
98 			return (DCMD_ERR);
99 		}
100 		return (DCMD_OK);
101 	}
102 
103 	if (DCMD_HDRSPEC(flags))
104 		mdb_printf("%<u>%?s %8s %8s %8s %?s %?s%</u>\n",
105 		    "ADDR", "ID", "TYPE", "STATE", "OWNER", "REGENT");
106 
107 	if (mdb_vread(&ct, sizeof (ct), addr) != sizeof (ct)) {
108 		mdb_warn("error reading contract_t at %p", addr);
109 		return (DCMD_ERR);
110 	}
111 	if (mdb_vread(&ctt, sizeof (ctt), (uintptr_t)ct.ct_type) !=
112 	    sizeof (ctt)) {
113 		mdb_warn("error reading ct_type_t at %p", ct.ct_type);
114 		return (DCMD_ERR);
115 	}
116 	if (mdb_readstr(str, sizeof (str), (uintptr_t)ctt.ct_type_name) == -1) {
117 		mdb_warn("error reading contract type name at %p",
118 		    ctt.ct_type_name);
119 		return (DCMD_ERR);
120 	}
121 
122 	mdb_printf("%0?p %8d %8s %8s %?p %?p\n", addr, ct.ct_id, str,
123 	    (ct.ct_state == CTS_OWNED) ? "owned" :
124 	    (ct.ct_state == CTS_INHERITED) ? "inherit" :
125 	    (ct.ct_state == CTS_ORPHAN) ? "orphan" : "dead",
126 	    ct.ct_owner, ct.ct_regent);
127 
128 	return (DCMD_OK);
129 }
130 
131 const mdb_bitmask_t ct_event_flags[] = {
132 	{ "ACK", CTE_ACK, CTE_ACK },
133 	{ "INFO", CTE_INFO, CTE_INFO },
134 	{ "NEG", CTE_NEG, CTE_NEG },
135 	{ NULL }
136 };
137 
138 
139 /* ARGSUSED */
140 int
141 cmd_ctevent(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
142 {
143 	ct_kevent_t cte;
144 
145 	if (!(flags & DCMD_ADDRSPEC))
146 		return (DCMD_USAGE);
147 
148 	if (DCMD_HDRSPEC(flags))
149 		mdb_printf("%<u>%12s %8s %12s %6s %12s %12s %s%</u>\n",
150 		    "ADDR", "ID", "CONTRACT", "TYPE", "DATA", "GDATA", "FLAGS");
151 
152 	if (mdb_vread(&cte, sizeof (cte), addr) != sizeof (cte)) {
153 		mdb_warn("error reading ct_kevent_t at %p", addr);
154 		return (DCMD_ERR);
155 	}
156 
157 	mdb_printf("%12p %8llu %12p %6d %12p %12p %b\n", addr, cte.cte_id,
158 	    cte.cte_contract, cte.cte_type, cte.cte_data, cte.cte_gdata,
159 	    cte.cte_flags, ct_event_flags);
160 
161 	return (DCMD_OK);
162 }
163 
164 typedef struct findct_data {
165 	uintptr_t fc_ctid;
166 	uintptr_t fc_addr;
167 	boolean_t fc_found;
168 } findct_data_t;
169 
170 static int
171 findct(uintptr_t addr, contract_t *ct, findct_data_t *arg)
172 {
173 	if (ct->ct_id == arg->fc_ctid) {
174 		arg->fc_found = B_TRUE;
175 		arg->fc_addr = addr;
176 		return (WALK_DONE);
177 	}
178 
179 	return (WALK_NEXT);
180 }
181 
182 /* ARGSUSED */
183 int
184 cmd_ctid(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
185 {
186 	findct_data_t fcdata;
187 
188 	if (!(flags & DCMD_ADDRSPEC))
189 		return (DCMD_USAGE);
190 
191 	fcdata.fc_ctid = addr;
192 	fcdata.fc_found = B_FALSE;
193 	if (mdb_walk("contract", (mdb_walk_cb_t)findct, &fcdata) == -1 ||
194 	    !fcdata.fc_found)
195 		return (DCMD_ERR);
196 
197 	mdb_printf("%lr", fcdata.fc_addr);
198 
199 	return (DCMD_OK);
200 }
201