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