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