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