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