summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/modules/cpc/cpc.c
blob: 9fc783518628249a6a0a745bd03b26dbe7ecbc08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2019 Joyent, Inc.
 */

#include <sys/mdb_modapi.h>
#include <sys/kcpc.h>
#include <sys/cpc_impl.h>

#define	 KCPC_HASH_BUCKETS	(1l << KCPC_LOG2_HASH_BUCKETS)

/*
 * Assume 64-bit kernel address max is 100000000000 - 1.
 */
#ifdef _LP64
#define	ADDR_WIDTH 11
#else
#define	ADDR_WIDTH 8
#endif

struct cpc_ctx_aux {
	uintptr_t  cca_hash[KCPC_HASH_BUCKETS];
	int	   cca_bucket;
};

/*ARGSUSED*/
static int
cpc(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	kcpc_ctx_t	ctx;
	kcpc_set_t	set;
	kcpc_request_t	*reqs;
	uint64_t	*data;
	kcpc_attr_t	*attr;
	int		i;
	int		j;
	uint_t		opt_v = FALSE;

	if (mdb_getopts(argc, argv, 'v', MDB_OPT_SETBITS, TRUE, &opt_v, NULL) !=
	    argc)
		return (DCMD_USAGE);

	if ((flags & DCMD_ADDRSPEC) == 0) {
		/*
		 * We weren't given the address of any specific cpc ctx, so
		 * invoke the walker to find them all.
		 */
		mdb_walk_dcmd("cpc_ctx", "cpc", argc, argv);
		return (DCMD_OK);
	}

	if (mdb_vread(&ctx, sizeof (ctx), addr) == -1) {
		mdb_warn("failed to read kcpc_ctx_t at %p", addr);
		return (DCMD_ABORT);
	}

	if (mdb_vread(&set, sizeof (set), (uintptr_t)ctx.kc_set) == -1) {
		mdb_warn("failed to read kcpc_set_t at %p", ctx.kc_set);
		return (DCMD_ABORT);
	}

	reqs = mdb_alloc(set.ks_nreqs * sizeof (*reqs), UM_GC);
	data = mdb_alloc(set.ks_nreqs * sizeof (*data), UM_GC);

	if (mdb_vread(reqs, set.ks_nreqs * sizeof (*reqs),
	    (uintptr_t)set.ks_req) == -1) {
		mdb_warn("failed to read requests at %p", set.ks_req);
		return (DCMD_ABORT);
	}

	if (mdb_vread(data, set.ks_nreqs * sizeof (*data),
	    (uintptr_t)set.ks_data) == -1) {
		mdb_warn("failed to read set data at %p", set.ks_data);
		return (DCMD_ABORT);
	}

	if (DCMD_HDRSPEC(flags))
		mdb_printf("N PIC NDX %16s FLG %16s %*s EVENT\n", "VAL",
		    "PRESET", ADDR_WIDTH, "CFG");
	mdb_printf("-----------------------------------------------------------"
	    "---------------------\n");
	if (opt_v)
		mdb_printf("Set: %p\t%d requests. Flags = %x\n", ctx.kc_set,
		    set.ks_nreqs, set.ks_flags);

	for (i = 0; i < set.ks_nreqs; i++) {
		mdb_printf("%d %3d %3d %16llx %1s%1s%1s %16llx %8p %s\n", i,
		    reqs[i].kr_picnum, reqs[i].kr_index,
		    data[reqs[i].kr_index],
		    (reqs[i].kr_flags & CPC_OVF_NOTIFY_EMT)	? "O" : "",
		    (reqs[i].kr_flags & CPC_COUNT_USER)		? "U" : "",
		    (reqs[i].kr_flags & CPC_COUNT_SYSTEM)	? "S" : "",
		    reqs[i].kr_preset, reqs[i].kr_config,
		    reqs[i].kr_event);
		if (opt_v == 0)
			continue;
		if (reqs[i].kr_nattrs > 0) {
			attr = mdb_alloc(reqs[i].kr_nattrs * sizeof (*attr),
			    UM_GC);
			if (mdb_vread(attr, reqs[i].kr_nattrs * sizeof (*attr),
			    (uintptr_t)reqs[i].kr_attr) == -1) {
				mdb_warn("failed to read attributes at %p",
				    reqs[i].kr_attr);
				return (DCMD_ABORT);
			}
			for (j = 0; j < reqs[i].kr_nattrs; j++)
				mdb_printf("\t%s = %llx", attr[j].ka_name,
				    attr[j].ka_val);
			mdb_printf("\n");
		}
	}

	return (DCMD_OK);
}

static void
cpc_help(void)
{
	mdb_printf("Displays the contents of the CPC context at the supplied "
	    "address.  If no address is given, displays contents of all active "
	    "CPC contexts.\n");
	mdb_printf("Flag codes: \n"
	    "O = overflow notify     U = count user events     "
	    "S = count system events\n");
}


/*
 * Initialize the global walk by grabbing the hash table in the
 * cpc module.
 */
static int
cpc_ctx_walk_init(mdb_walk_state_t *wsp)
{
	struct cpc_ctx_aux *cca;

	if (wsp->walk_addr != 0) {
		mdb_warn("only global cpc_ctx walk supported\n");
		return (WALK_ERR);
	}

	cca = mdb_zalloc(sizeof (*cca), UM_SLEEP);

	if (mdb_readsym(&cca->cca_hash, sizeof (cca->cca_hash),
	    "kcpc_ctx_list") == -1) {
		mdb_warn("cannot read cpc_ctx hash table");
		mdb_free(cca, sizeof (*cca));
		return (WALK_ERR);
	}

	wsp->walk_data = cca;
	wsp->walk_addr = 0;
	return (WALK_NEXT);
}

static int
cpc_ctx_walk_step(mdb_walk_state_t *wsp)
{
	int status;
	kcpc_ctx_t ctx;
	struct cpc_ctx_aux *cca = wsp->walk_data;

	while (wsp->walk_addr == 0) {
		if (cca->cca_bucket == KCPC_HASH_BUCKETS)
			return (WALK_DONE);
		wsp->walk_addr = cca->cca_hash[cca->cca_bucket++];
	}

	if (mdb_vread(&ctx, sizeof (ctx), wsp->walk_addr) == -1) {
		mdb_warn("failed to read cpc_ctx at %p", wsp->walk_addr);
		return (WALK_ERR);
	}

	status = wsp->walk_callback(wsp->walk_addr, &ctx,
	    wsp->walk_cbdata);

	wsp->walk_addr = (uintptr_t)ctx.kc_next;
	return (status);
}

static void
cpc_ctx_walk_fini(mdb_walk_state_t *wsp)
{
	mdb_free(wsp->walk_data, sizeof (struct cpc_ctx_aux));
}

static const mdb_walker_t walkers[] = {
	{ "cpc_ctx", "walk global list of cpc contexts",
		cpc_ctx_walk_init, cpc_ctx_walk_step, cpc_ctx_walk_fini },
	{ NULL }
};

static const mdb_dcmd_t dcmds[] = {
	{ "cpc", "?[-v]", "Display contents of CPC context", cpc, cpc_help },
	{ NULL }
};

static const mdb_modinfo_t modinfo = {
	MDB_API_VERSION, dcmds, walkers
};

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}