xref: /illumos-gate/usr/src/cmd/mdb/common/modules/cpc/cpc.c (revision 09e2ab34f6c69b170fe7478e8b011d6bb505e0d9)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/mdb_modapi.h>
28 #include <sys/kcpc.h>
29 #include <sys/cpc_impl.h>
30 
31 #define	 KCPC_HASH_BUCKETS	(1l << KCPC_LOG2_HASH_BUCKETS)
32 
33 /*
34  * Assume 64-bit kernel address max is 100000000000 - 1.
35  */
36 #ifdef _LP64
37 #define	ADDR_WIDTH 11
38 #else
39 #define	ADDR_WIDTH 8
40 #endif
41 
42 struct cpc_ctx_aux {
43 	uintptr_t  cca_hash[KCPC_HASH_BUCKETS];
44 	int	   cca_bucket;
45 };
46 
47 /*ARGSUSED*/
48 static int
49 cpc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
50 {
51 	kcpc_ctx_t	ctx;
52 	kcpc_set_t	set;
53 	kcpc_request_t	*reqs;
54 	uint64_t	*data;
55 	kcpc_attr_t	*attr;
56 	int		i;
57 	int		j;
58 	uint_t		opt_v = FALSE;
59 
60 	if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v) != argc)
61 		return (DCMD_USAGE);
62 
63 	if ((flags & DCMD_ADDRSPEC) == 0) {
64 		/*
65 		 * We weren't given the address of any specific cpc ctx, so
66 		 * invoke the walker to find them all.
67 		 */
68 		mdb_walk_dcmd("cpc_ctx", "cpc", argc, argv);
69 		return (DCMD_OK);
70 	}
71 
72 	if (mdb_vread(&ctx, sizeof (ctx), addr) == -1) {
73 		mdb_warn("failed to read kcpc_ctx_t at %p", addr);
74 		return (DCMD_ABORT);
75 	}
76 
77 	if (mdb_vread(&set, sizeof (set), (uintptr_t)ctx.kc_set) == -1) {
78 		mdb_warn("failed to read kcpc_set_t at %p", ctx.kc_set);
79 		return (DCMD_ABORT);
80 	}
81 
82 	reqs = mdb_alloc(set.ks_nreqs * sizeof (*reqs), UM_GC);
83 	data = mdb_alloc(set.ks_nreqs * sizeof (*data), UM_GC);
84 
85 	if (mdb_vread(reqs, set.ks_nreqs * sizeof (*reqs),
86 	    (uintptr_t)set.ks_req) == -1) {
87 		mdb_warn("failed to read requests at %p", set.ks_req);
88 		return (DCMD_ABORT);
89 	}
90 
91 	if (mdb_vread(data, set.ks_nreqs * sizeof (*data),
92 	    (uintptr_t)set.ks_data) == -1) {
93 		mdb_warn("failed to read set data at %p", set.ks_data);
94 		return (DCMD_ABORT);
95 	}
96 
97 	if (DCMD_HDRSPEC(flags))
98 		mdb_printf("N PIC NDX %16s FLG %16s %*s EVENT\n", "VAL",
99 		    "PRESET", ADDR_WIDTH, "CFG");
100 	mdb_printf("-----------------------------------------------------------"
101 	    "---------------------\n");
102 	if (opt_v)
103 		mdb_printf("Set: %p\t%d requests. Flags = %x\n", ctx.kc_set,
104 		    set.ks_nreqs, set.ks_flags);
105 
106 	for (i = 0; i < set.ks_nreqs; i++) {
107 		mdb_printf("%d %3d %3d %16llx %1s%1s%1s %16llx %8p %s\n", i,
108 		    reqs[i].kr_picnum, reqs[i].kr_index,
109 		    data[reqs[i].kr_index],
110 		    (reqs[i].kr_flags & CPC_OVF_NOTIFY_EMT)	? "O" : "",
111 		    (reqs[i].kr_flags & CPC_COUNT_USER)		? "U" : "",
112 		    (reqs[i].kr_flags & CPC_COUNT_SYSTEM)	? "S" : "",
113 		    reqs[i].kr_preset, reqs[i].kr_config,
114 		    reqs[i].kr_event);
115 		if (opt_v == 0)
116 			continue;
117 		if (reqs[i].kr_nattrs > 0) {
118 			attr = mdb_alloc(reqs[i].kr_nattrs * sizeof (*attr),
119 			    UM_GC);
120 			if (mdb_vread(attr, reqs[i].kr_nattrs * sizeof (*attr),
121 			    (uintptr_t)reqs[i].kr_attr) == -1) {
122 				mdb_warn("failed to read attributes at %p",
123 				    reqs[i].kr_attr);
124 				return (DCMD_ABORT);
125 			}
126 			for (j = 0; j < reqs[i].kr_nattrs; j++)
127 				mdb_printf("\t%s = %llx", attr[j].ka_name,
128 				    attr[j].ka_val);
129 			mdb_printf("\n");
130 		}
131 	}
132 
133 	return (DCMD_OK);
134 }
135 
136 static void
137 cpc_help(void)
138 {
139 	mdb_printf("Displays the contents of the CPC context at the supplied "
140 	    "address.  If no address is given, displays contents of all active "
141 	    "CPC contexts.\n");
142 	mdb_printf("Flag codes: \n"
143 	    "O = overflow notify     U = count user events     "
144 	    "S = count system events\n");
145 }
146 
147 
148 /*
149  * Initialize the global walk by grabbing the hash table in the
150  * cpc module.
151  */
152 static int
153 cpc_ctx_walk_init(mdb_walk_state_t *wsp)
154 {
155 	struct cpc_ctx_aux *cca;
156 
157 	if (wsp->walk_addr != 0) {
158 		mdb_warn("only global cpc_ctx walk supported\n");
159 		return (WALK_ERR);
160 	}
161 
162 	cca = mdb_zalloc(sizeof (*cca), UM_SLEEP);
163 
164 	if (mdb_readsym(&cca->cca_hash, sizeof (cca->cca_hash),
165 	    "kcpc_ctx_list") == -1) {
166 		mdb_warn("cannot read cpc_ctx hash table");
167 		mdb_free(cca, sizeof (*cca));
168 		return (WALK_ERR);
169 	}
170 
171 	wsp->walk_data = cca;
172 	wsp->walk_addr = 0;
173 	return (WALK_NEXT);
174 }
175 
176 static int
177 cpc_ctx_walk_step(mdb_walk_state_t *wsp)
178 {
179 	int status;
180 	kcpc_ctx_t ctx;
181 	struct cpc_ctx_aux *cca = wsp->walk_data;
182 
183 	while (wsp->walk_addr == 0) {
184 		if (cca->cca_bucket == KCPC_HASH_BUCKETS)
185 			return (WALK_DONE);
186 		wsp->walk_addr = cca->cca_hash[cca->cca_bucket++];
187 	}
188 
189 	if (mdb_vread(&ctx, sizeof (ctx), wsp->walk_addr) == -1) {
190 		mdb_warn("failed to read cpc_ctx at %p", wsp->walk_addr);
191 		return (WALK_ERR);
192 	}
193 
194 	status = wsp->walk_callback(wsp->walk_addr, &ctx,
195 	    wsp->walk_cbdata);
196 
197 	wsp->walk_addr = (uintptr_t)ctx.kc_next;
198 	return (status);
199 }
200 
201 static void
202 cpc_ctx_walk_fini(mdb_walk_state_t *wsp)
203 {
204 	mdb_free(wsp->walk_data, sizeof (struct cpc_ctx_aux));
205 }
206 
207 static const mdb_walker_t walkers[] = {
208 	{ "cpc_ctx", "walk global list of cpc contexts",
209 		cpc_ctx_walk_init, cpc_ctx_walk_step, cpc_ctx_walk_fini },
210 	{ NULL }
211 };
212 
213 static const mdb_dcmd_t dcmds[] = {
214 	{ "cpc", "?[-v]", "Display contents of CPC context", cpc, cpc_help },
215 	{ NULL }
216 };
217 
218 static const mdb_modinfo_t modinfo = {
219 	MDB_API_VERSION, dcmds, walkers
220 };
221 
222 const mdb_modinfo_t *
223 _mdb_init(void)
224 {
225 	return (&modinfo);
226 }
227