xref: /titanic_41/usr/src/lib/libdtrace/common/dt_handle.c (revision 4610e4a00999c6d2291b3fc263926b890ec500a5)
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 2005 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 <stddef.h>
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #include <alloca.h>
36 
37 #include <dt_impl.h>
38 #include <dt_program.h>
39 
40 static const char _dt_errprog[] =
41 "dtrace:::ERROR"
42 "{"
43 "	trace(arg1);"
44 "	trace(arg2);"
45 "	trace(arg3);"
46 "	trace(arg4);"
47 "	trace(arg5);"
48 "}";
49 
50 int
51 dtrace_handle_err(dtrace_hdl_t *dtp, dtrace_handle_err_f *hdlr, void *arg)
52 {
53 	dtrace_prog_t *pgp = NULL;
54 	dt_stmt_t *stp;
55 	dtrace_ecbdesc_t *edp;
56 
57 	/*
58 	 * We don't currently support multiple error handlers.
59 	 */
60 	if (dtp->dt_errhdlr != NULL)
61 		return (dt_set_errno(dtp, EALREADY));
62 
63 	/*
64 	 * If the DTRACEOPT_GRABANON is enabled, the anonymous enabling will
65 	 * already have a dtrace:::ERROR probe enabled; save 'hdlr' and 'arg'
66 	 * but do not bother compiling and enabling _dt_errprog.
67 	 */
68 	if (dtp->dt_options[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET)
69 		goto out;
70 
71 	if ((pgp = dtrace_program_strcompile(dtp, _dt_errprog,
72 	    DTRACE_PROBESPEC_NAME, DTRACE_C_ZDEFS, 0, NULL)) == NULL)
73 		return (dt_set_errno(dtp, dtrace_errno(dtp)));
74 
75 	stp = dt_list_next(&pgp->dp_stmts);
76 	assert(stp != NULL);
77 
78 	edp = stp->ds_desc->dtsd_ecbdesc;
79 	assert(edp != NULL);
80 	edp->dted_uarg = DT_ECB_ERROR;
81 
82 out:
83 	dtp->dt_errhdlr = hdlr;
84 	dtp->dt_errarg = arg;
85 	dtp->dt_errprog = pgp;
86 
87 	return (0);
88 }
89 
90 int
91 dtrace_handle_drop(dtrace_hdl_t *dtp, dtrace_handle_drop_f *hdlr, void *arg)
92 {
93 	if (dtp->dt_drophdlr != NULL)
94 		return (dt_set_errno(dtp, EALREADY));
95 
96 	dtp->dt_drophdlr = hdlr;
97 	dtp->dt_droparg = arg;
98 
99 	return (0);
100 }
101 
102 int
103 dtrace_handle_proc(dtrace_hdl_t *dtp, dtrace_handle_proc_f *hdlr, void *arg)
104 {
105 	if (dtp->dt_prochdlr != NULL)
106 		return (dt_set_errno(dtp, EALREADY));
107 
108 	dtp->dt_prochdlr = hdlr;
109 	dtp->dt_procarg = arg;
110 
111 	return (0);
112 }
113 
114 int
115 dtrace_handle_buffered(dtrace_hdl_t *dtp, dtrace_handle_buffered_f *hdlr,
116     void *arg)
117 {
118 	if (dtp->dt_bufhdlr != NULL)
119 		return (dt_set_errno(dtp, EALREADY));
120 
121 	if (hdlr == NULL)
122 		return (dt_set_errno(dtp, EINVAL));
123 
124 	dtp->dt_bufhdlr = hdlr;
125 	dtp->dt_bufarg = arg;
126 
127 	return (0);
128 }
129 
130 int
131 dtrace_handle_setopt(dtrace_hdl_t *dtp, dtrace_handle_setopt_f *hdlr,
132     void *arg)
133 {
134 	if (hdlr == NULL)
135 		return (dt_set_errno(dtp, EINVAL));
136 
137 	dtp->dt_setopthdlr = hdlr;
138 	dtp->dt_setoptarg = arg;
139 
140 	return (0);
141 }
142 
143 #define	DT_REC(type, ndx) *((type *)((uintptr_t)data->dtpda_data + \
144     epd->dtepd_rec[(ndx)].dtrd_offset))
145 
146 static int
147 dt_handle_err(dtrace_hdl_t *dtp, dtrace_probedata_t *data)
148 {
149 	dtrace_eprobedesc_t *epd = data->dtpda_edesc, *errepd;
150 	dtrace_probedesc_t *pd = data->dtpda_pdesc, *errpd;
151 	dtrace_errdata_t err;
152 	dtrace_epid_t epid;
153 
154 	char where[30];
155 	char details[30];
156 	char offinfo[30];
157 	const int slop = 80;
158 	const char *faultstr;
159 	char *str;
160 	int len;
161 
162 	assert(epd->dtepd_uarg == DT_ECB_ERROR);
163 
164 	if (epd->dtepd_nrecs != 5 || strcmp(pd->dtpd_provider, "dtrace") != 0 ||
165 	    strcmp(pd->dtpd_name, "ERROR") != 0)
166 		return (dt_set_errno(dtp, EDT_BADERROR));
167 
168 	/*
169 	 * This is an error.  We have the following items here:  EPID,
170 	 * faulting action, DIF offset, fault code and faulting address.
171 	 */
172 	epid = (uint32_t)DT_REC(uint64_t, 0);
173 
174 	if (dt_epid_lookup(dtp, epid, &errepd, &errpd) != 0)
175 		return (dt_set_errno(dtp, EDT_BADERROR));
176 
177 	err.dteda_edesc = errepd;
178 	err.dteda_pdesc = errpd;
179 	err.dteda_cpu = data->dtpda_cpu;
180 	err.dteda_action = (int)DT_REC(uint64_t, 1);
181 	err.dteda_offset = (int)DT_REC(uint64_t, 2);
182 	err.dteda_fault = (int)DT_REC(uint64_t, 3);
183 	err.dteda_addr = DT_REC(uint64_t, 4);
184 
185 	faultstr = dtrace_faultstr(dtp, err.dteda_fault);
186 	len = sizeof (where) + sizeof (offinfo) + strlen(faultstr) +
187 	    strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) +
188 	    strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) +
189 	    slop;
190 
191 	str = (char *)alloca(len);
192 
193 	if (err.dteda_action == 0) {
194 		(void) sprintf(where, "predicate");
195 	} else {
196 		(void) sprintf(where, "action #%d", err.dteda_action);
197 	}
198 
199 	if (err.dteda_offset != -1) {
200 		(void) sprintf(offinfo, " at DIF offset %d", err.dteda_offset);
201 	} else {
202 		offinfo[0] = 0;
203 	}
204 
205 	switch (err.dteda_fault) {
206 	case DTRACEFLT_BADADDR:
207 	case DTRACEFLT_BADALIGN:
208 		(void) sprintf(details, " (0x%llx)",
209 		    (u_longlong_t)err.dteda_addr);
210 		break;
211 
212 	default:
213 		details[0] = 0;
214 	}
215 
216 	(void) snprintf(str, len, "error on enabled probe ID %u "
217 	    "(ID %u: %s:%s:%s:%s): %s%s in %s%s\n",
218 	    epid, errpd->dtpd_id, errpd->dtpd_provider,
219 	    errpd->dtpd_mod, errpd->dtpd_func,
220 	    errpd->dtpd_name, dtrace_faultstr(dtp, err.dteda_fault),
221 	    details, where, offinfo);
222 
223 	err.dteda_msg = str;
224 
225 	if (dtp->dt_errhdlr == NULL)
226 		return (dt_set_errno(dtp, EDT_ERRABORT));
227 
228 	if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT)
229 		return (dt_set_errno(dtp, EDT_ERRABORT));
230 
231 	return (0);
232 }
233 
234 int
235 dt_handle_liberr(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
236     const char *faultstr)
237 {
238 	dtrace_probedesc_t *errpd = data->dtpda_pdesc;
239 	dtrace_errdata_t err;
240 	const int slop = 80;
241 	char *str;
242 	int len;
243 
244 	err.dteda_edesc = data->dtpda_edesc;
245 	err.dteda_pdesc = errpd;
246 	err.dteda_cpu = data->dtpda_cpu;
247 	err.dteda_action = -1;
248 	err.dteda_offset = -1;
249 	err.dteda_fault = DTRACEFLT_LIBRARY;
250 	err.dteda_addr = NULL;
251 
252 	len = strlen(faultstr) +
253 	    strlen(errpd->dtpd_provider) + strlen(errpd->dtpd_mod) +
254 	    strlen(errpd->dtpd_name) + strlen(errpd->dtpd_func) +
255 	    slop;
256 
257 	str = alloca(len);
258 
259 	(void) snprintf(str, len, "error on enabled probe ID %u "
260 	    "(ID %u: %s:%s:%s:%s): %s\n",
261 	    data->dtpda_edesc->dtepd_epid,
262 	    errpd->dtpd_id, errpd->dtpd_provider,
263 	    errpd->dtpd_mod, errpd->dtpd_func,
264 	    errpd->dtpd_name, faultstr);
265 
266 	err.dteda_msg = str;
267 
268 	if (dtp->dt_errhdlr == NULL)
269 		return (dt_set_errno(dtp, EDT_ERRABORT));
270 
271 	if ((*dtp->dt_errhdlr)(&err, dtp->dt_errarg) == DTRACE_HANDLE_ABORT)
272 		return (dt_set_errno(dtp, EDT_ERRABORT));
273 
274 	return (0);
275 }
276 
277 #define	DROPTAG(x)	x, #x
278 
279 static const struct {
280 	dtrace_dropkind_t dtdrg_kind;
281 	char *dtdrg_tag;
282 } _dt_droptags[] = {
283 	{ DROPTAG(DTRACEDROP_PRINCIPAL) },
284 	{ DROPTAG(DTRACEDROP_AGGREGATION) },
285 	{ DROPTAG(DTRACEDROP_DYNAMIC) },
286 	{ DROPTAG(DTRACEDROP_DYNRINSE) },
287 	{ DROPTAG(DTRACEDROP_DYNDIRTY) },
288 	{ DROPTAG(DTRACEDROP_SPEC) },
289 	{ DROPTAG(DTRACEDROP_SPECBUSY) },
290 	{ DROPTAG(DTRACEDROP_SPECUNAVAIL) },
291 	{ DROPTAG(DTRACEDROP_DBLERROR) },
292 	{ DROPTAG(DTRACEDROP_STKSTROVERFLOW) },
293 	{ 0, NULL }
294 };
295 
296 static const char *
297 dt_droptag(dtrace_dropkind_t kind)
298 {
299 	int i;
300 
301 	for (i = 0; _dt_droptags[i].dtdrg_tag != NULL; i++) {
302 		if (_dt_droptags[i].dtdrg_kind == kind)
303 			return (_dt_droptags[i].dtdrg_tag);
304 	}
305 
306 	return ("DTRACEDROP_UNKNOWN");
307 }
308 
309 int
310 dt_handle_cpudrop(dtrace_hdl_t *dtp, processorid_t cpu,
311     dtrace_dropkind_t what, uint64_t howmany)
312 {
313 	dtrace_dropdata_t drop;
314 	char str[80], *s;
315 	int size;
316 
317 	assert(what == DTRACEDROP_PRINCIPAL || what == DTRACEDROP_AGGREGATION);
318 
319 	bzero(&drop, sizeof (drop));
320 	drop.dtdda_handle = dtp;
321 	drop.dtdda_cpu = cpu;
322 	drop.dtdda_kind = what;
323 	drop.dtdda_drops = howmany;
324 	drop.dtdda_msg = str;
325 
326 	if (dtp->dt_droptags) {
327 		(void) snprintf(str, sizeof (str), "[%s] ", dt_droptag(what));
328 		s = &str[strlen(str)];
329 		size = sizeof (str) - (s - str);
330 	} else {
331 		s = str;
332 		size = sizeof (str);
333 	}
334 
335 	(void) snprintf(s, size, "%llu %sdrop%s on CPU %d\n",
336 	    howmany, what == DTRACEDROP_PRINCIPAL ? "" : "aggregation ",
337 	    howmany > 1 ? "s" : "", cpu);
338 
339 	if (dtp->dt_drophdlr == NULL)
340 		return (dt_set_errno(dtp, EDT_DROPABORT));
341 
342 	if ((*dtp->dt_drophdlr)(&drop, dtp->dt_droparg) == DTRACE_HANDLE_ABORT)
343 		return (dt_set_errno(dtp, EDT_DROPABORT));
344 
345 	return (0);
346 }
347 
348 static const struct {
349 	dtrace_dropkind_t dtdrt_kind;
350 	uintptr_t dtdrt_offset;
351 	const char *dtdrt_str;
352 	const char *dtdrt_msg;
353 } _dt_droptab[] = {
354 	{ DTRACEDROP_DYNAMIC,
355 	    offsetof(dtrace_status_t, dtst_dyndrops),
356 	    "dynamic variable drop" },
357 
358 	{ DTRACEDROP_DYNRINSE,
359 	    offsetof(dtrace_status_t, dtst_dyndrops_rinsing),
360 	    "dynamic variable drop", " with non-empty rinsing list" },
361 
362 	{ DTRACEDROP_DYNDIRTY,
363 	    offsetof(dtrace_status_t, dtst_dyndrops_dirty),
364 	    "dynamic variable drop", " with non-empty dirty list" },
365 
366 	{ DTRACEDROP_SPEC,
367 	    offsetof(dtrace_status_t, dtst_specdrops),
368 	    "speculative drop" },
369 
370 	{ DTRACEDROP_SPECBUSY,
371 	    offsetof(dtrace_status_t, dtst_specdrops_busy),
372 	    "failed speculation", " (available buffer(s) still busy)" },
373 
374 	{ DTRACEDROP_SPECUNAVAIL,
375 	    offsetof(dtrace_status_t, dtst_specdrops_unavail),
376 	    "failed speculation", " (no speculative buffer available)" },
377 
378 	{ DTRACEDROP_STKSTROVERFLOW,
379 	    offsetof(dtrace_status_t, dtst_stkstroverflows),
380 	    "jstack()/ustack() string table overflow" },
381 
382 	{ DTRACEDROP_DBLERROR,
383 	    offsetof(dtrace_status_t, dtst_dblerrors),
384 	    "error", " in ERROR probe enabling" },
385 
386 	{ 0, 0, NULL }
387 };
388 
389 int
390 dt_handle_status(dtrace_hdl_t *dtp, dtrace_status_t *old, dtrace_status_t *new)
391 {
392 	dtrace_dropdata_t drop;
393 	char str[80], *s;
394 	uintptr_t base = (uintptr_t)new, obase = (uintptr_t)old;
395 	int i, size;
396 
397 	bzero(&drop, sizeof (drop));
398 	drop.dtdda_handle = dtp;
399 	drop.dtdda_cpu = DTRACE_CPUALL;
400 	drop.dtdda_msg = str;
401 
402 	/*
403 	 * First, check to see if we've been killed -- in which case we abort.
404 	 */
405 	if (new->dtst_killed && !old->dtst_killed)
406 		return (dt_set_errno(dtp, EDT_BRICKED));
407 
408 	for (i = 0; _dt_droptab[i].dtdrt_str != NULL; i++) {
409 		uintptr_t naddr = base + _dt_droptab[i].dtdrt_offset;
410 		uintptr_t oaddr = obase + _dt_droptab[i].dtdrt_offset;
411 
412 		uint64_t nval = *((uint64_t *)naddr);
413 		uint64_t oval = *((uint64_t *)oaddr);
414 
415 		if (nval == oval)
416 			continue;
417 
418 		if (dtp->dt_droptags) {
419 			(void) snprintf(str, sizeof (str), "[%s] ",
420 			    dt_droptag(_dt_droptab[i].dtdrt_kind));
421 			s = &str[strlen(str)];
422 			size = sizeof (str) - (s - str);
423 		} else {
424 			s = str;
425 			size = sizeof (str);
426 		}
427 
428 		(void) snprintf(s, size, "%llu %s%s%s\n", nval - oval,
429 		    _dt_droptab[i].dtdrt_str, (nval - oval > 1) ? "s" : "",
430 		    _dt_droptab[i].dtdrt_msg != NULL ?
431 		    _dt_droptab[i].dtdrt_msg : "");
432 
433 		drop.dtdda_kind = _dt_droptab[i].dtdrt_kind;
434 		drop.dtdda_total = nval;
435 		drop.dtdda_drops = nval - oval;
436 
437 		if (dtp->dt_drophdlr == NULL)
438 			return (dt_set_errno(dtp, EDT_DROPABORT));
439 
440 		if ((*dtp->dt_drophdlr)(&drop,
441 		    dtp->dt_droparg) == DTRACE_HANDLE_ABORT)
442 			return (dt_set_errno(dtp, EDT_DROPABORT));
443 	}
444 
445 	return (0);
446 }
447 
448 int
449 dt_handle_setopt(dtrace_hdl_t *dtp, dtrace_setoptdata_t *data)
450 {
451 	void *arg = dtp->dt_setoptarg;
452 
453 	if (dtp->dt_setopthdlr == NULL)
454 		return (0);
455 
456 	if ((*dtp->dt_setopthdlr)(data, arg) == DTRACE_HANDLE_ABORT)
457 		return (dt_set_errno(dtp, EDT_DIRABORT));
458 
459 	return (0);
460 }
461 
462 int
463 dt_handle(dtrace_hdl_t *dtp, dtrace_probedata_t *data)
464 {
465 	dtrace_eprobedesc_t *epd = data->dtpda_edesc;
466 	int rval;
467 
468 	switch (epd->dtepd_uarg) {
469 	case DT_ECB_ERROR:
470 		rval = dt_handle_err(dtp, data);
471 		break;
472 
473 	default:
474 		return (DTRACE_CONSUME_THIS);
475 	}
476 
477 	if (rval == 0)
478 		return (DTRACE_CONSUME_NEXT);
479 
480 	return (DTRACE_CONSUME_ERROR);
481 }
482