Deleted Added
full compact
dt_subr.c (210767) dt_subr.c (211554)
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/*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#if defined(sun)
28#include <sys/sysmacros.h>
29#endif
30
31#include <strings.h>
32#include <unistd.h>
33#include <stdarg.h>
34#include <stddef.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <errno.h>
38#include <ctype.h>
39#if defined(sun)
40#include <alloca.h>
41#else
42#include <sys/sysctl.h>
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/*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#if defined(sun)
28#include <sys/sysmacros.h>
29#endif
30
31#include <strings.h>
32#include <unistd.h>
33#include <stdarg.h>
34#include <stddef.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <errno.h>
38#include <ctype.h>
39#if defined(sun)
40#include <alloca.h>
41#else
42#include <sys/sysctl.h>
43#include <libproc_compat.h>
43#endif
44#include <assert.h>
45#include <libgen.h>
46#include <limits.h>
47
48#include <dt_impl.h>
49
50static const struct {
51 size_t dtps_offset;
52 size_t dtps_len;
53} dtrace_probespecs[] = {
54 { offsetof(dtrace_probedesc_t, dtpd_provider), DTRACE_PROVNAMELEN },
55 { offsetof(dtrace_probedesc_t, dtpd_mod), DTRACE_MODNAMELEN },
56 { offsetof(dtrace_probedesc_t, dtpd_func), DTRACE_FUNCNAMELEN },
57 { offsetof(dtrace_probedesc_t, dtpd_name), DTRACE_NAMELEN }
58};
59
60int
61dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
62 const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
63{
64 size_t off, len, vlen, wlen;
65 const char *p, *q, *v, *w;
66
67 char buf[32]; /* for id_t as %d (see below) */
68
69 if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
70 return (dt_set_errno(dtp, EINVAL));
71
72 bzero(pdp, sizeof (dtrace_probedesc_t));
73 p = s + strlen(s) - 1;
74
75 do {
76 for (len = 0; p >= s && *p != ':'; len++)
77 p--; /* move backward until we find a delimiter */
78
79 q = p + 1;
80 vlen = 0;
81 w = NULL;
82 wlen = 0;
83
84 if ((v = strchr(q, '$')) != NULL && v < q + len) {
85 /*
86 * Set vlen to the length of the variable name and then
87 * reset len to the length of the text prior to '$'. If
88 * the name begins with a digit, interpret it using the
89 * the argv[] array. Otherwise we look in dt_macros.
90 * For the moment, all dt_macros variables are of type
91 * id_t (see dtrace_update() for more details on that).
92 */
93 vlen = (size_t)(q + len - v);
94 len = (size_t)(v - q);
95
96 /*
97 * If the variable string begins with $$, skip past the
98 * leading dollar sign since $ and $$ are equivalent
99 * macro reference operators in a probe description.
100 */
101 if (vlen > 2 && v[1] == '$') {
102 vlen--;
103 v++;
104 }
105
106 if (isdigit(v[1])) {
107 long i;
108
109 errno = 0;
110 i = strtol(v + 1, (char **)&w, 10);
111
112 wlen = vlen - (w - v);
113
114 if (i < 0 || i >= argc || errno != 0)
115 return (dt_set_errno(dtp, EDT_BADSPCV));
116
117 v = argv[i];
118 vlen = strlen(v);
119
120 if (yypcb != NULL && yypcb->pcb_sargv == argv)
121 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
122
123 } else if (vlen > 1) {
124 char *vstr = alloca(vlen);
125 dt_ident_t *idp;
126
127 (void) strncpy(vstr, v + 1, vlen - 1);
128 vstr[vlen - 1] = '\0';
129 idp = dt_idhash_lookup(dtp->dt_macros, vstr);
130
131 if (idp == NULL)
132 return (dt_set_errno(dtp, EDT_BADSPCV));
133
134 v = buf;
135 vlen = snprintf(buf, 32, "%d", idp->di_id);
136
137 } else
138 return (dt_set_errno(dtp, EDT_BADSPCV));
139 }
140
141 if (spec == DTRACE_PROBESPEC_NONE)
142 return (dt_set_errno(dtp, EDT_BADSPEC));
143
144 if (len + vlen >= dtrace_probespecs[spec].dtps_len)
145 return (dt_set_errno(dtp, ENAMETOOLONG));
146
147 off = dtrace_probespecs[spec--].dtps_offset;
148 bcopy(q, (char *)pdp + off, len);
149 bcopy(v, (char *)pdp + off + len, vlen);
150 bcopy(w, (char *)pdp + off + len + vlen, wlen);
151 } while (--p >= s);
152
153 pdp->dtpd_id = DTRACE_IDNONE;
154 return (0);
155}
156
157int
158dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
159 const char *s, dtrace_probedesc_t *pdp)
160{
161 return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
162}
163
164int
165dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
166{
167 bzero(pdp, sizeof (dtrace_probedesc_t));
168 pdp->dtpd_id = id;
169
170 if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
171 pdp->dtpd_id != id)
172 return (dt_set_errno(dtp, EDT_BADID));
173
174 return (0);
175}
176
177char *
178dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
179{
180 if (pdp->dtpd_id == 0) {
181 (void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
182 pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
183 } else
184 (void) snprintf(buf, len, "%u", pdp->dtpd_id);
185
186 return (buf);
187}
188
189char *
190dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
191{
192 const char *name = dtrace_stability_name(attr.dtat_name);
193 const char *data = dtrace_stability_name(attr.dtat_data);
194 const char *class = dtrace_class_name(attr.dtat_class);
195
196 if (name == NULL || data == NULL || class == NULL)
197 return (NULL); /* one or more invalid attributes */
198
199 (void) snprintf(buf, len, "%s/%s/%s", name, data, class);
200 return (buf);
201}
202
203static char *
204dt_getstrattr(char *p, char **qp)
205{
206 char *q;
207
208 if (*p == '\0')
209 return (NULL);
210
211 if ((q = strchr(p, '/')) == NULL)
212 q = p + strlen(p);
213 else
214 *q++ = '\0';
215
216 *qp = q;
217 return (p);
218}
219
220int
221dtrace_str2attr(const char *str, dtrace_attribute_t *attr)
222{
223 dtrace_stability_t s;
224 dtrace_class_t c;
225 char *p, *q;
226
227 if (str == NULL || attr == NULL)
228 return (-1); /* invalid function arguments */
229
230 *attr = _dtrace_maxattr;
231 p = alloca(strlen(str) + 1);
232 (void) strcpy(p, str);
233
234 if ((p = dt_getstrattr(p, &q)) == NULL)
235 return (0);
236
237 for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
238 if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
239 attr->dtat_name = s;
240 break;
241 }
242 }
243
244 if (s > DTRACE_STABILITY_MAX)
245 return (-1);
246
247 if ((p = dt_getstrattr(q, &q)) == NULL)
248 return (0);
249
250 for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
251 if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
252 attr->dtat_data = s;
253 break;
254 }
255 }
256
257 if (s > DTRACE_STABILITY_MAX)
258 return (-1);
259
260 if ((p = dt_getstrattr(q, &q)) == NULL)
261 return (0);
262
263 for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
264 if (strcasecmp(p, dtrace_class_name(c)) == 0) {
265 attr->dtat_class = c;
266 break;
267 }
268 }
269
270 if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
271 return (-1);
272
273 return (0);
274}
275
276const char *
277dtrace_stability_name(dtrace_stability_t s)
278{
279 switch (s) {
280 case DTRACE_STABILITY_INTERNAL: return ("Internal");
281 case DTRACE_STABILITY_PRIVATE: return ("Private");
282 case DTRACE_STABILITY_OBSOLETE: return ("Obsolete");
283 case DTRACE_STABILITY_EXTERNAL: return ("External");
284 case DTRACE_STABILITY_UNSTABLE: return ("Unstable");
285 case DTRACE_STABILITY_EVOLVING: return ("Evolving");
286 case DTRACE_STABILITY_STABLE: return ("Stable");
287 case DTRACE_STABILITY_STANDARD: return ("Standard");
288 default: return (NULL);
289 }
290}
291
292const char *
293dtrace_class_name(dtrace_class_t c)
294{
295 switch (c) {
296 case DTRACE_CLASS_UNKNOWN: return ("Unknown");
297 case DTRACE_CLASS_CPU: return ("CPU");
298 case DTRACE_CLASS_PLATFORM: return ("Platform");
299 case DTRACE_CLASS_GROUP: return ("Group");
300 case DTRACE_CLASS_ISA: return ("ISA");
301 case DTRACE_CLASS_COMMON: return ("Common");
302 default: return (NULL);
303 }
304}
305
306dtrace_attribute_t
307dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
308{
309 dtrace_attribute_t am;
310
311 am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
312 am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
313 am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
314
315 return (am);
316}
317
318dtrace_attribute_t
319dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
320{
321 dtrace_attribute_t am;
322
323 am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
324 am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
325 am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
326
327 return (am);
328}
329
330/*
331 * Compare two attributes and return an integer value in the following ranges:
332 *
333 * <0 if any of a1's attributes are less than a2's attributes
334 * =0 if all of a1's attributes are equal to a2's attributes
335 * >0 if all of a1's attributes are greater than or equal to a2's attributes
336 *
337 * To implement this function efficiently, we subtract a2's attributes from
338 * a1's to obtain a negative result if an a1 attribute is less than its a2
339 * counterpart. We then OR the intermediate results together, relying on the
340 * twos-complement property that if any result is negative, the bitwise union
341 * will also be negative since the highest bit will be set in the result.
342 */
343int
344dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
345{
346 return (((int)a1.dtat_name - a2.dtat_name) |
347 ((int)a1.dtat_data - a2.dtat_data) |
348 ((int)a1.dtat_class - a2.dtat_class));
349}
350
351char *
352dt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
353{
354 static const char stability[] = "ipoxuesS";
355 static const char class[] = "uCpgIc";
356
357 if (a.dtat_name < sizeof (stability) &&
358 a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
359 (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
360 stability[a.dtat_data], class[a.dtat_class]);
361 } else {
362 (void) snprintf(buf, len, "[%u/%u/%u]",
363 a.dtat_name, a.dtat_data, a.dtat_class);
364 }
365
366 return (buf);
367}
368
369char *
370dt_version_num2str(dt_version_t v, char *buf, size_t len)
371{
372 uint_t M = DT_VERSION_MAJOR(v);
373 uint_t m = DT_VERSION_MINOR(v);
374 uint_t u = DT_VERSION_MICRO(v);
375
376 if (u == 0)
377 (void) snprintf(buf, len, "%u.%u", M, m);
378 else
379 (void) snprintf(buf, len, "%u.%u.%u", M, m, u);
380
381 return (buf);
382}
383
384int
385dt_version_str2num(const char *s, dt_version_t *vp)
386{
387 int i = 0, n[3] = { 0, 0, 0 };
388 char c;
389
390 while ((c = *s++) != '\0') {
391 if (isdigit(c))
392 n[i] = n[i] * 10 + c - '0';
393 else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
394 return (-1);
395 }
396
397 if (n[0] > DT_VERSION_MAJMAX ||
398 n[1] > DT_VERSION_MINMAX ||
399 n[2] > DT_VERSION_MICMAX)
400 return (-1);
401
402 if (vp != NULL)
403 *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
404
405 return (0);
406}
407
408int
409dt_version_defined(dt_version_t v)
410{
411 int i;
412
413 for (i = 0; _dtrace_versions[i] != 0; i++) {
414 if (_dtrace_versions[i] == v)
415 return (1);
416 }
417
418 return (0);
419}
420
421char *
422dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
423{
424 char *arg;
425
426 if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
427 int olds = dtp->dt_cpp_args;
428 int news = olds * 2;
429 char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
430
431 if (argv == NULL)
432 return (NULL);
433
434 bzero(&argv[olds], sizeof (char *) * olds);
435 dtp->dt_cpp_argv = argv;
436 dtp->dt_cpp_args = news;
437 }
438
439 if ((arg = strdup(str)) == NULL)
440 return (NULL);
441
442 assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
443 dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
444 return (arg);
445}
446
447char *
448dt_cpp_pop_arg(dtrace_hdl_t *dtp)
449{
450 char *arg;
451
452 if (dtp->dt_cpp_argc <= 1)
453 return (NULL); /* dt_cpp_argv[0] cannot be popped */
454
455 arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
456 dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
457
458 return (arg);
459}
460
461/*PRINTFLIKE1*/
462void
463dt_dprintf(const char *format, ...)
464{
465 if (_dtrace_debug) {
466 va_list alist;
467
468 va_start(alist, format);
469 (void) fputs("libdtrace DEBUG: ", stderr);
470 (void) vfprintf(stderr, format, alist);
471 va_end(alist);
472 }
473}
474
475int
476#if defined(sun)
477dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
478#else
479dt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg)
480#endif
481{
482 const dtrace_vector_t *v = dtp->dt_vector;
483
484#if !defined(sun)
485 /* Avoid sign extension. */
486 val &= 0xffffffff;
487#endif
488
489 if (v != NULL)
490 return (v->dtv_ioctl(dtp->dt_varg, val, arg));
491
492 if (dtp->dt_fd >= 0)
493 return (ioctl(dtp->dt_fd, val, arg));
494
495 errno = EBADF;
496 return (-1);
497}
498
499int
500dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
501{
502 const dtrace_vector_t *v = dtp->dt_vector;
503
504 if (v == NULL) {
505#if defined(sun)
506 return (p_online(cpu, P_STATUS));
507#else
508 int maxid = 0;
509 size_t len = sizeof(maxid);
510 if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0)
511 return (cpu == 0 ? 1 : -1);
512 else
513 return (cpu <= maxid ? 1 : -1);
514#endif
515 }
516
517 return (v->dtv_status(dtp->dt_varg, cpu));
518}
519
520long
521dt_sysconf(dtrace_hdl_t *dtp, int name)
522{
523 const dtrace_vector_t *v = dtp->dt_vector;
524
525 if (v == NULL)
526 return (sysconf(name));
527
528 return (v->dtv_sysconf(dtp->dt_varg, name));
529}
530
531/*
532 * Wrapper around write(2) to handle partial writes. For maximum safety of
533 * output files and proper error reporting, we continuing writing in the
534 * face of partial writes until write(2) fails or 'buf' is completely written.
535 * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
536 */
537ssize_t
538dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
539{
540 ssize_t resid = n;
541 ssize_t len;
542
543 while (resid != 0) {
544 if ((len = write(fd, buf, resid)) <= 0)
545 break;
546
547 resid -= len;
548 buf = (char *)buf + len;
549 }
550
551 if (resid == n && n != 0)
552 return (dt_set_errno(dtp, errno));
553
554 return (n - resid);
555}
556
557/*
558 * This function handles all output from libdtrace, as well as the
559 * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then
560 * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
561 * specified buffer and return. Otherwise, if output is buffered (denoted by
562 * a NULL fp), we sprintf the desired output into the buffered buffer
563 * (expanding the buffer if required). If we don't satisfy either of these
564 * conditions (that is, if we are to actually generate output), then we call
565 * fprintf with the specified fp. In this case, we need to deal with one of
566 * the more annoying peculiarities of libc's printf routines: any failed
567 * write persistently sets an error flag inside the FILE causing every
568 * subsequent write to fail, but only the caller that initiated the error gets
569 * the errno. Since libdtrace clients often intercept SIGINT, this case is
570 * particularly frustrating since we don't want the EINTR on one attempt to
571 * write to the output file to preclude later attempts to write. This
572 * function therefore does a clearerr() if any error occurred, and saves the
573 * errno for the caller inside the specified dtrace_hdl_t.
574 */
575/*PRINTFLIKE3*/
576int
577dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
578{
579 va_list ap;
580 int n;
581
582#if !defined(sun)
583 /*
584 * On FreeBSD, check if output is currently being re-directed
585 * to another file. If so, output to that file instead of the
586 * one the caller has specified.
587 */
588 if (dtp->dt_freopen_fp != NULL)
589 fp = dtp->dt_freopen_fp;
590#endif
591
592 va_start(ap, format);
593
594 if (dtp->dt_sprintf_buflen != 0) {
595 int len;
596 char *buf;
597
598 assert(dtp->dt_sprintf_buf != NULL);
599
600 buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
601 len = dtp->dt_sprintf_buflen - len;
602 assert(len >= 0);
603
604 if ((n = vsnprintf(buf, len, format, ap)) < 0)
605 n = dt_set_errno(dtp, errno);
606
607 va_end(ap);
608
609 return (n);
610 }
611
612 if (fp == NULL) {
613 int needed, rval;
614 size_t avail;
615
616 /*
617 * It's not legal to use buffered ouput if there is not a
618 * handler for buffered output.
619 */
620 if (dtp->dt_bufhdlr == NULL) {
621 va_end(ap);
622 return (dt_set_errno(dtp, EDT_NOBUFFERED));
623 }
624
625 if (dtp->dt_buffered_buf == NULL) {
626 assert(dtp->dt_buffered_size == 0);
627 dtp->dt_buffered_size = 1;
628 dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
629
630 if (dtp->dt_buffered_buf == NULL) {
631 va_end(ap);
632 return (dt_set_errno(dtp, EDT_NOMEM));
633 }
634
635 dtp->dt_buffered_offs = 0;
636 dtp->dt_buffered_buf[0] = '\0';
637 }
638
639 if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) {
640 rval = dt_set_errno(dtp, errno);
641 va_end(ap);
642 return (rval);
643 }
644
645 if (needed == 0) {
646 va_end(ap);
647 return (0);
648 }
649
650 for (;;) {
651 char *newbuf;
652
653 assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
654 avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
655
656 if (needed + 1 < avail)
657 break;
658
659 if ((newbuf = realloc(dtp->dt_buffered_buf,
660 dtp->dt_buffered_size << 1)) == NULL) {
661 va_end(ap);
662 return (dt_set_errno(dtp, EDT_NOMEM));
663 }
664
665 dtp->dt_buffered_buf = newbuf;
666 dtp->dt_buffered_size <<= 1;
667 }
668
669 if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
670 avail, format, ap) < 0) {
671 rval = dt_set_errno(dtp, errno);
672 va_end(ap);
673 return (rval);
674 }
675
676 dtp->dt_buffered_offs += needed;
677 assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
678 return (0);
679 }
680
681 n = vfprintf(fp, format, ap);
682 fflush(fp);
683 va_end(ap);
684
685 if (n < 0) {
686 clearerr(fp);
687 return (dt_set_errno(dtp, errno));
688 }
689
690 return (n);
691}
692
693int
694dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
695 const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
696{
697 dtrace_bufdata_t data;
698
699 if (dtp->dt_buffered_offs == 0)
700 return (0);
701
702 data.dtbda_handle = dtp;
703 data.dtbda_buffered = dtp->dt_buffered_buf;
704 data.dtbda_probe = pdata;
705 data.dtbda_recdesc = rec;
706 data.dtbda_aggdata = agg;
707 data.dtbda_flags = flags;
708
709 if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
710 return (dt_set_errno(dtp, EDT_DIRABORT));
711
712 dtp->dt_buffered_offs = 0;
713 dtp->dt_buffered_buf[0] = '\0';
714
715 return (0);
716}
717
718void
719dt_buffered_destroy(dtrace_hdl_t *dtp)
720{
721 free(dtp->dt_buffered_buf);
722 dtp->dt_buffered_buf = NULL;
723 dtp->dt_buffered_offs = 0;
724 dtp->dt_buffered_size = 0;
725}
726
727void *
728dt_zalloc(dtrace_hdl_t *dtp, size_t size)
729{
730 void *data;
731
732 if (size > 16 * 1024 * 1024) {
733 (void) dt_set_errno(dtp, EDT_NOMEM);
734 return (NULL);
735 }
736
737 if ((data = malloc(size)) == NULL)
738 (void) dt_set_errno(dtp, EDT_NOMEM);
739 else
740 bzero(data, size);
741
742 return (data);
743}
744
745void *
746dt_alloc(dtrace_hdl_t *dtp, size_t size)
747{
748 void *data;
749
750 if (size > 16 * 1024 * 1024) {
751 (void) dt_set_errno(dtp, EDT_NOMEM);
752 return (NULL);
753 }
754
755 if ((data = malloc(size)) == NULL)
756 (void) dt_set_errno(dtp, EDT_NOMEM);
757
758 return (data);
759}
760
761void
762dt_free(dtrace_hdl_t *dtp, void *data)
763{
764 assert(dtp != NULL); /* ensure sane use of this interface */
765 free(data);
766}
767
768void
769dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
770{
771 if (dp == NULL)
772 return; /* simplify caller code */
773
774 dt_free(dtp, dp->dtdo_buf);
775 dt_free(dtp, dp->dtdo_inttab);
776 dt_free(dtp, dp->dtdo_strtab);
777 dt_free(dtp, dp->dtdo_vartab);
778 dt_free(dtp, dp->dtdo_kreltab);
779 dt_free(dtp, dp->dtdo_ureltab);
780 dt_free(dtp, dp->dtdo_xlmtab);
781
782 dt_free(dtp, dp);
783}
784
785/*
786 * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
787 * implements the behavior that an empty pattern matches any string.
788 */
789int
790dt_gmatch(const char *s, const char *p)
791{
792 return (p == NULL || *p == '\0' || gmatch(s, p));
793}
794
795char *
796dt_basename(char *str)
797{
798 char *last = strrchr(str, '/');
799
800 if (last == NULL)
801 return (str);
802
803 return (last + 1);
804}
805
806/*
807 * dt_popc() is a fast implementation of population count. The algorithm is
808 * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
809 */
810ulong_t
811dt_popc(ulong_t x)
812{
813#ifdef _ILP32
814 x = x - ((x >> 1) & 0x55555555UL);
815 x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
816 x = (x + (x >> 4)) & 0x0F0F0F0FUL;
817 x = x + (x >> 8);
818 x = x + (x >> 16);
819 return (x & 0x3F);
820#endif
821#ifdef _LP64
822 x = x - ((x >> 1) & 0x5555555555555555ULL);
823 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
824 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
825 x = x + (x >> 8);
826 x = x + (x >> 16);
827 x = x + (x >> 32);
828 return (x & 0x7F);
829#endif
830}
831
832/*
833 * dt_popcb() is a bitmap-based version of population count that returns the
834 * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
835 */
836ulong_t
837dt_popcb(const ulong_t *bp, ulong_t n)
838{
839 ulong_t maxb = n & BT_ULMASK;
840 ulong_t maxw = n >> BT_ULSHIFT;
841 ulong_t w, popc = 0;
842
843 if (n == 0)
844 return (0);
845
846 for (w = 0; w < maxw; w++)
847 popc += dt_popc(bp[w]);
848
849 return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
850}
851
852#if defined(sun)
853struct _rwlock;
854struct _lwp_mutex;
855
856int
857dt_rw_read_held(pthread_rwlock_t *lock)
858{
859 extern int _rw_read_held(struct _rwlock *);
860 return (_rw_read_held((struct _rwlock *)lock));
861}
862
863int
864dt_rw_write_held(pthread_rwlock_t *lock)
865{
866 extern int _rw_write_held(struct _rwlock *);
867 return (_rw_write_held((struct _rwlock *)lock));
868}
869#endif
870
871int
872dt_mutex_held(pthread_mutex_t *lock)
873{
874#if defined(sun)
875 extern int _mutex_held(struct _lwp_mutex *);
876 return (_mutex_held((struct _lwp_mutex *)lock));
877#else
878 return (1);
879#endif
880}
881
882static int
883dt_string2str(char *s, char *str, int nbytes)
884{
885 int len = strlen(s);
886
887 if (nbytes == 0) {
888 /*
889 * Like snprintf(3C), we don't check the value of str if the
890 * number of bytes is 0.
891 */
892 return (len);
893 }
894
895 if (nbytes <= len) {
896 (void) strncpy(str, s, nbytes - 1);
897 /*
898 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
899 * that the string is null-terminated.
900 */
901 str[nbytes - 1] = '\0';
902 } else {
903 (void) strcpy(str, s);
904 }
905
906 return (len);
907}
908
909int
910dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
911{
912 dtrace_syminfo_t dts;
913 GElf_Sym sym;
914
915 size_t n = 20; /* for 0x%llx\0 */
916 char *s;
917 int err;
918
919 if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
920 n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
921
922 s = alloca(n);
923
924 if (err == 0 && addr != sym.st_value) {
925 (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
926 dts.dts_name, (u_longlong_t)addr - sym.st_value);
927 } else if (err == 0) {
928 (void) snprintf(s, n, "%s`%s",
929 dts.dts_object, dts.dts_name);
930 } else {
931 /*
932 * We'll repeat the lookup, but this time we'll specify a NULL
933 * GElf_Sym -- indicating that we're only interested in the
934 * containing module.
935 */
936 if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
937 (void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
938 (u_longlong_t)addr);
939 } else {
940 (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
941 }
942 }
943
944 return (dt_string2str(s, str, nbytes));
945}
946
947int
948dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
949 uint64_t addr, char *str, int nbytes)
950{
951 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
952 struct ps_prochandle *P = NULL;
953 GElf_Sym sym;
954 char *obj;
955
956 if (pid != 0)
957 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
958
959 if (P == NULL) {
960 (void) snprintf(c, sizeof (c), "0x%llx", addr);
961 return (dt_string2str(c, str, nbytes));
962 }
963
964 dt_proc_lock(dtp, P);
965
44#endif
45#include <assert.h>
46#include <libgen.h>
47#include <limits.h>
48
49#include <dt_impl.h>
50
51static const struct {
52 size_t dtps_offset;
53 size_t dtps_len;
54} dtrace_probespecs[] = {
55 { offsetof(dtrace_probedesc_t, dtpd_provider), DTRACE_PROVNAMELEN },
56 { offsetof(dtrace_probedesc_t, dtpd_mod), DTRACE_MODNAMELEN },
57 { offsetof(dtrace_probedesc_t, dtpd_func), DTRACE_FUNCNAMELEN },
58 { offsetof(dtrace_probedesc_t, dtpd_name), DTRACE_NAMELEN }
59};
60
61int
62dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
63 const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
64{
65 size_t off, len, vlen, wlen;
66 const char *p, *q, *v, *w;
67
68 char buf[32]; /* for id_t as %d (see below) */
69
70 if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
71 return (dt_set_errno(dtp, EINVAL));
72
73 bzero(pdp, sizeof (dtrace_probedesc_t));
74 p = s + strlen(s) - 1;
75
76 do {
77 for (len = 0; p >= s && *p != ':'; len++)
78 p--; /* move backward until we find a delimiter */
79
80 q = p + 1;
81 vlen = 0;
82 w = NULL;
83 wlen = 0;
84
85 if ((v = strchr(q, '$')) != NULL && v < q + len) {
86 /*
87 * Set vlen to the length of the variable name and then
88 * reset len to the length of the text prior to '$'. If
89 * the name begins with a digit, interpret it using the
90 * the argv[] array. Otherwise we look in dt_macros.
91 * For the moment, all dt_macros variables are of type
92 * id_t (see dtrace_update() for more details on that).
93 */
94 vlen = (size_t)(q + len - v);
95 len = (size_t)(v - q);
96
97 /*
98 * If the variable string begins with $$, skip past the
99 * leading dollar sign since $ and $$ are equivalent
100 * macro reference operators in a probe description.
101 */
102 if (vlen > 2 && v[1] == '$') {
103 vlen--;
104 v++;
105 }
106
107 if (isdigit(v[1])) {
108 long i;
109
110 errno = 0;
111 i = strtol(v + 1, (char **)&w, 10);
112
113 wlen = vlen - (w - v);
114
115 if (i < 0 || i >= argc || errno != 0)
116 return (dt_set_errno(dtp, EDT_BADSPCV));
117
118 v = argv[i];
119 vlen = strlen(v);
120
121 if (yypcb != NULL && yypcb->pcb_sargv == argv)
122 yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
123
124 } else if (vlen > 1) {
125 char *vstr = alloca(vlen);
126 dt_ident_t *idp;
127
128 (void) strncpy(vstr, v + 1, vlen - 1);
129 vstr[vlen - 1] = '\0';
130 idp = dt_idhash_lookup(dtp->dt_macros, vstr);
131
132 if (idp == NULL)
133 return (dt_set_errno(dtp, EDT_BADSPCV));
134
135 v = buf;
136 vlen = snprintf(buf, 32, "%d", idp->di_id);
137
138 } else
139 return (dt_set_errno(dtp, EDT_BADSPCV));
140 }
141
142 if (spec == DTRACE_PROBESPEC_NONE)
143 return (dt_set_errno(dtp, EDT_BADSPEC));
144
145 if (len + vlen >= dtrace_probespecs[spec].dtps_len)
146 return (dt_set_errno(dtp, ENAMETOOLONG));
147
148 off = dtrace_probespecs[spec--].dtps_offset;
149 bcopy(q, (char *)pdp + off, len);
150 bcopy(v, (char *)pdp + off + len, vlen);
151 bcopy(w, (char *)pdp + off + len + vlen, wlen);
152 } while (--p >= s);
153
154 pdp->dtpd_id = DTRACE_IDNONE;
155 return (0);
156}
157
158int
159dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
160 const char *s, dtrace_probedesc_t *pdp)
161{
162 return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
163}
164
165int
166dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
167{
168 bzero(pdp, sizeof (dtrace_probedesc_t));
169 pdp->dtpd_id = id;
170
171 if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
172 pdp->dtpd_id != id)
173 return (dt_set_errno(dtp, EDT_BADID));
174
175 return (0);
176}
177
178char *
179dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
180{
181 if (pdp->dtpd_id == 0) {
182 (void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
183 pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
184 } else
185 (void) snprintf(buf, len, "%u", pdp->dtpd_id);
186
187 return (buf);
188}
189
190char *
191dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
192{
193 const char *name = dtrace_stability_name(attr.dtat_name);
194 const char *data = dtrace_stability_name(attr.dtat_data);
195 const char *class = dtrace_class_name(attr.dtat_class);
196
197 if (name == NULL || data == NULL || class == NULL)
198 return (NULL); /* one or more invalid attributes */
199
200 (void) snprintf(buf, len, "%s/%s/%s", name, data, class);
201 return (buf);
202}
203
204static char *
205dt_getstrattr(char *p, char **qp)
206{
207 char *q;
208
209 if (*p == '\0')
210 return (NULL);
211
212 if ((q = strchr(p, '/')) == NULL)
213 q = p + strlen(p);
214 else
215 *q++ = '\0';
216
217 *qp = q;
218 return (p);
219}
220
221int
222dtrace_str2attr(const char *str, dtrace_attribute_t *attr)
223{
224 dtrace_stability_t s;
225 dtrace_class_t c;
226 char *p, *q;
227
228 if (str == NULL || attr == NULL)
229 return (-1); /* invalid function arguments */
230
231 *attr = _dtrace_maxattr;
232 p = alloca(strlen(str) + 1);
233 (void) strcpy(p, str);
234
235 if ((p = dt_getstrattr(p, &q)) == NULL)
236 return (0);
237
238 for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
239 if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
240 attr->dtat_name = s;
241 break;
242 }
243 }
244
245 if (s > DTRACE_STABILITY_MAX)
246 return (-1);
247
248 if ((p = dt_getstrattr(q, &q)) == NULL)
249 return (0);
250
251 for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
252 if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
253 attr->dtat_data = s;
254 break;
255 }
256 }
257
258 if (s > DTRACE_STABILITY_MAX)
259 return (-1);
260
261 if ((p = dt_getstrattr(q, &q)) == NULL)
262 return (0);
263
264 for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
265 if (strcasecmp(p, dtrace_class_name(c)) == 0) {
266 attr->dtat_class = c;
267 break;
268 }
269 }
270
271 if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
272 return (-1);
273
274 return (0);
275}
276
277const char *
278dtrace_stability_name(dtrace_stability_t s)
279{
280 switch (s) {
281 case DTRACE_STABILITY_INTERNAL: return ("Internal");
282 case DTRACE_STABILITY_PRIVATE: return ("Private");
283 case DTRACE_STABILITY_OBSOLETE: return ("Obsolete");
284 case DTRACE_STABILITY_EXTERNAL: return ("External");
285 case DTRACE_STABILITY_UNSTABLE: return ("Unstable");
286 case DTRACE_STABILITY_EVOLVING: return ("Evolving");
287 case DTRACE_STABILITY_STABLE: return ("Stable");
288 case DTRACE_STABILITY_STANDARD: return ("Standard");
289 default: return (NULL);
290 }
291}
292
293const char *
294dtrace_class_name(dtrace_class_t c)
295{
296 switch (c) {
297 case DTRACE_CLASS_UNKNOWN: return ("Unknown");
298 case DTRACE_CLASS_CPU: return ("CPU");
299 case DTRACE_CLASS_PLATFORM: return ("Platform");
300 case DTRACE_CLASS_GROUP: return ("Group");
301 case DTRACE_CLASS_ISA: return ("ISA");
302 case DTRACE_CLASS_COMMON: return ("Common");
303 default: return (NULL);
304 }
305}
306
307dtrace_attribute_t
308dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
309{
310 dtrace_attribute_t am;
311
312 am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
313 am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
314 am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
315
316 return (am);
317}
318
319dtrace_attribute_t
320dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
321{
322 dtrace_attribute_t am;
323
324 am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
325 am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
326 am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
327
328 return (am);
329}
330
331/*
332 * Compare two attributes and return an integer value in the following ranges:
333 *
334 * <0 if any of a1's attributes are less than a2's attributes
335 * =0 if all of a1's attributes are equal to a2's attributes
336 * >0 if all of a1's attributes are greater than or equal to a2's attributes
337 *
338 * To implement this function efficiently, we subtract a2's attributes from
339 * a1's to obtain a negative result if an a1 attribute is less than its a2
340 * counterpart. We then OR the intermediate results together, relying on the
341 * twos-complement property that if any result is negative, the bitwise union
342 * will also be negative since the highest bit will be set in the result.
343 */
344int
345dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
346{
347 return (((int)a1.dtat_name - a2.dtat_name) |
348 ((int)a1.dtat_data - a2.dtat_data) |
349 ((int)a1.dtat_class - a2.dtat_class));
350}
351
352char *
353dt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
354{
355 static const char stability[] = "ipoxuesS";
356 static const char class[] = "uCpgIc";
357
358 if (a.dtat_name < sizeof (stability) &&
359 a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
360 (void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
361 stability[a.dtat_data], class[a.dtat_class]);
362 } else {
363 (void) snprintf(buf, len, "[%u/%u/%u]",
364 a.dtat_name, a.dtat_data, a.dtat_class);
365 }
366
367 return (buf);
368}
369
370char *
371dt_version_num2str(dt_version_t v, char *buf, size_t len)
372{
373 uint_t M = DT_VERSION_MAJOR(v);
374 uint_t m = DT_VERSION_MINOR(v);
375 uint_t u = DT_VERSION_MICRO(v);
376
377 if (u == 0)
378 (void) snprintf(buf, len, "%u.%u", M, m);
379 else
380 (void) snprintf(buf, len, "%u.%u.%u", M, m, u);
381
382 return (buf);
383}
384
385int
386dt_version_str2num(const char *s, dt_version_t *vp)
387{
388 int i = 0, n[3] = { 0, 0, 0 };
389 char c;
390
391 while ((c = *s++) != '\0') {
392 if (isdigit(c))
393 n[i] = n[i] * 10 + c - '0';
394 else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
395 return (-1);
396 }
397
398 if (n[0] > DT_VERSION_MAJMAX ||
399 n[1] > DT_VERSION_MINMAX ||
400 n[2] > DT_VERSION_MICMAX)
401 return (-1);
402
403 if (vp != NULL)
404 *vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
405
406 return (0);
407}
408
409int
410dt_version_defined(dt_version_t v)
411{
412 int i;
413
414 for (i = 0; _dtrace_versions[i] != 0; i++) {
415 if (_dtrace_versions[i] == v)
416 return (1);
417 }
418
419 return (0);
420}
421
422char *
423dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
424{
425 char *arg;
426
427 if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
428 int olds = dtp->dt_cpp_args;
429 int news = olds * 2;
430 char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
431
432 if (argv == NULL)
433 return (NULL);
434
435 bzero(&argv[olds], sizeof (char *) * olds);
436 dtp->dt_cpp_argv = argv;
437 dtp->dt_cpp_args = news;
438 }
439
440 if ((arg = strdup(str)) == NULL)
441 return (NULL);
442
443 assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
444 dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
445 return (arg);
446}
447
448char *
449dt_cpp_pop_arg(dtrace_hdl_t *dtp)
450{
451 char *arg;
452
453 if (dtp->dt_cpp_argc <= 1)
454 return (NULL); /* dt_cpp_argv[0] cannot be popped */
455
456 arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
457 dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
458
459 return (arg);
460}
461
462/*PRINTFLIKE1*/
463void
464dt_dprintf(const char *format, ...)
465{
466 if (_dtrace_debug) {
467 va_list alist;
468
469 va_start(alist, format);
470 (void) fputs("libdtrace DEBUG: ", stderr);
471 (void) vfprintf(stderr, format, alist);
472 va_end(alist);
473 }
474}
475
476int
477#if defined(sun)
478dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
479#else
480dt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg)
481#endif
482{
483 const dtrace_vector_t *v = dtp->dt_vector;
484
485#if !defined(sun)
486 /* Avoid sign extension. */
487 val &= 0xffffffff;
488#endif
489
490 if (v != NULL)
491 return (v->dtv_ioctl(dtp->dt_varg, val, arg));
492
493 if (dtp->dt_fd >= 0)
494 return (ioctl(dtp->dt_fd, val, arg));
495
496 errno = EBADF;
497 return (-1);
498}
499
500int
501dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
502{
503 const dtrace_vector_t *v = dtp->dt_vector;
504
505 if (v == NULL) {
506#if defined(sun)
507 return (p_online(cpu, P_STATUS));
508#else
509 int maxid = 0;
510 size_t len = sizeof(maxid);
511 if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0)
512 return (cpu == 0 ? 1 : -1);
513 else
514 return (cpu <= maxid ? 1 : -1);
515#endif
516 }
517
518 return (v->dtv_status(dtp->dt_varg, cpu));
519}
520
521long
522dt_sysconf(dtrace_hdl_t *dtp, int name)
523{
524 const dtrace_vector_t *v = dtp->dt_vector;
525
526 if (v == NULL)
527 return (sysconf(name));
528
529 return (v->dtv_sysconf(dtp->dt_varg, name));
530}
531
532/*
533 * Wrapper around write(2) to handle partial writes. For maximum safety of
534 * output files and proper error reporting, we continuing writing in the
535 * face of partial writes until write(2) fails or 'buf' is completely written.
536 * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
537 */
538ssize_t
539dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
540{
541 ssize_t resid = n;
542 ssize_t len;
543
544 while (resid != 0) {
545 if ((len = write(fd, buf, resid)) <= 0)
546 break;
547
548 resid -= len;
549 buf = (char *)buf + len;
550 }
551
552 if (resid == n && n != 0)
553 return (dt_set_errno(dtp, errno));
554
555 return (n - resid);
556}
557
558/*
559 * This function handles all output from libdtrace, as well as the
560 * dtrace_sprintf() case. If we're here due to dtrace_sprintf(), then
561 * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
562 * specified buffer and return. Otherwise, if output is buffered (denoted by
563 * a NULL fp), we sprintf the desired output into the buffered buffer
564 * (expanding the buffer if required). If we don't satisfy either of these
565 * conditions (that is, if we are to actually generate output), then we call
566 * fprintf with the specified fp. In this case, we need to deal with one of
567 * the more annoying peculiarities of libc's printf routines: any failed
568 * write persistently sets an error flag inside the FILE causing every
569 * subsequent write to fail, but only the caller that initiated the error gets
570 * the errno. Since libdtrace clients often intercept SIGINT, this case is
571 * particularly frustrating since we don't want the EINTR on one attempt to
572 * write to the output file to preclude later attempts to write. This
573 * function therefore does a clearerr() if any error occurred, and saves the
574 * errno for the caller inside the specified dtrace_hdl_t.
575 */
576/*PRINTFLIKE3*/
577int
578dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
579{
580 va_list ap;
581 int n;
582
583#if !defined(sun)
584 /*
585 * On FreeBSD, check if output is currently being re-directed
586 * to another file. If so, output to that file instead of the
587 * one the caller has specified.
588 */
589 if (dtp->dt_freopen_fp != NULL)
590 fp = dtp->dt_freopen_fp;
591#endif
592
593 va_start(ap, format);
594
595 if (dtp->dt_sprintf_buflen != 0) {
596 int len;
597 char *buf;
598
599 assert(dtp->dt_sprintf_buf != NULL);
600
601 buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
602 len = dtp->dt_sprintf_buflen - len;
603 assert(len >= 0);
604
605 if ((n = vsnprintf(buf, len, format, ap)) < 0)
606 n = dt_set_errno(dtp, errno);
607
608 va_end(ap);
609
610 return (n);
611 }
612
613 if (fp == NULL) {
614 int needed, rval;
615 size_t avail;
616
617 /*
618 * It's not legal to use buffered ouput if there is not a
619 * handler for buffered output.
620 */
621 if (dtp->dt_bufhdlr == NULL) {
622 va_end(ap);
623 return (dt_set_errno(dtp, EDT_NOBUFFERED));
624 }
625
626 if (dtp->dt_buffered_buf == NULL) {
627 assert(dtp->dt_buffered_size == 0);
628 dtp->dt_buffered_size = 1;
629 dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
630
631 if (dtp->dt_buffered_buf == NULL) {
632 va_end(ap);
633 return (dt_set_errno(dtp, EDT_NOMEM));
634 }
635
636 dtp->dt_buffered_offs = 0;
637 dtp->dt_buffered_buf[0] = '\0';
638 }
639
640 if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) {
641 rval = dt_set_errno(dtp, errno);
642 va_end(ap);
643 return (rval);
644 }
645
646 if (needed == 0) {
647 va_end(ap);
648 return (0);
649 }
650
651 for (;;) {
652 char *newbuf;
653
654 assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
655 avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
656
657 if (needed + 1 < avail)
658 break;
659
660 if ((newbuf = realloc(dtp->dt_buffered_buf,
661 dtp->dt_buffered_size << 1)) == NULL) {
662 va_end(ap);
663 return (dt_set_errno(dtp, EDT_NOMEM));
664 }
665
666 dtp->dt_buffered_buf = newbuf;
667 dtp->dt_buffered_size <<= 1;
668 }
669
670 if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
671 avail, format, ap) < 0) {
672 rval = dt_set_errno(dtp, errno);
673 va_end(ap);
674 return (rval);
675 }
676
677 dtp->dt_buffered_offs += needed;
678 assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
679 return (0);
680 }
681
682 n = vfprintf(fp, format, ap);
683 fflush(fp);
684 va_end(ap);
685
686 if (n < 0) {
687 clearerr(fp);
688 return (dt_set_errno(dtp, errno));
689 }
690
691 return (n);
692}
693
694int
695dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
696 const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
697{
698 dtrace_bufdata_t data;
699
700 if (dtp->dt_buffered_offs == 0)
701 return (0);
702
703 data.dtbda_handle = dtp;
704 data.dtbda_buffered = dtp->dt_buffered_buf;
705 data.dtbda_probe = pdata;
706 data.dtbda_recdesc = rec;
707 data.dtbda_aggdata = agg;
708 data.dtbda_flags = flags;
709
710 if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
711 return (dt_set_errno(dtp, EDT_DIRABORT));
712
713 dtp->dt_buffered_offs = 0;
714 dtp->dt_buffered_buf[0] = '\0';
715
716 return (0);
717}
718
719void
720dt_buffered_destroy(dtrace_hdl_t *dtp)
721{
722 free(dtp->dt_buffered_buf);
723 dtp->dt_buffered_buf = NULL;
724 dtp->dt_buffered_offs = 0;
725 dtp->dt_buffered_size = 0;
726}
727
728void *
729dt_zalloc(dtrace_hdl_t *dtp, size_t size)
730{
731 void *data;
732
733 if (size > 16 * 1024 * 1024) {
734 (void) dt_set_errno(dtp, EDT_NOMEM);
735 return (NULL);
736 }
737
738 if ((data = malloc(size)) == NULL)
739 (void) dt_set_errno(dtp, EDT_NOMEM);
740 else
741 bzero(data, size);
742
743 return (data);
744}
745
746void *
747dt_alloc(dtrace_hdl_t *dtp, size_t size)
748{
749 void *data;
750
751 if (size > 16 * 1024 * 1024) {
752 (void) dt_set_errno(dtp, EDT_NOMEM);
753 return (NULL);
754 }
755
756 if ((data = malloc(size)) == NULL)
757 (void) dt_set_errno(dtp, EDT_NOMEM);
758
759 return (data);
760}
761
762void
763dt_free(dtrace_hdl_t *dtp, void *data)
764{
765 assert(dtp != NULL); /* ensure sane use of this interface */
766 free(data);
767}
768
769void
770dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
771{
772 if (dp == NULL)
773 return; /* simplify caller code */
774
775 dt_free(dtp, dp->dtdo_buf);
776 dt_free(dtp, dp->dtdo_inttab);
777 dt_free(dtp, dp->dtdo_strtab);
778 dt_free(dtp, dp->dtdo_vartab);
779 dt_free(dtp, dp->dtdo_kreltab);
780 dt_free(dtp, dp->dtdo_ureltab);
781 dt_free(dtp, dp->dtdo_xlmtab);
782
783 dt_free(dtp, dp);
784}
785
786/*
787 * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
788 * implements the behavior that an empty pattern matches any string.
789 */
790int
791dt_gmatch(const char *s, const char *p)
792{
793 return (p == NULL || *p == '\0' || gmatch(s, p));
794}
795
796char *
797dt_basename(char *str)
798{
799 char *last = strrchr(str, '/');
800
801 if (last == NULL)
802 return (str);
803
804 return (last + 1);
805}
806
807/*
808 * dt_popc() is a fast implementation of population count. The algorithm is
809 * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
810 */
811ulong_t
812dt_popc(ulong_t x)
813{
814#ifdef _ILP32
815 x = x - ((x >> 1) & 0x55555555UL);
816 x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
817 x = (x + (x >> 4)) & 0x0F0F0F0FUL;
818 x = x + (x >> 8);
819 x = x + (x >> 16);
820 return (x & 0x3F);
821#endif
822#ifdef _LP64
823 x = x - ((x >> 1) & 0x5555555555555555ULL);
824 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
825 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
826 x = x + (x >> 8);
827 x = x + (x >> 16);
828 x = x + (x >> 32);
829 return (x & 0x7F);
830#endif
831}
832
833/*
834 * dt_popcb() is a bitmap-based version of population count that returns the
835 * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
836 */
837ulong_t
838dt_popcb(const ulong_t *bp, ulong_t n)
839{
840 ulong_t maxb = n & BT_ULMASK;
841 ulong_t maxw = n >> BT_ULSHIFT;
842 ulong_t w, popc = 0;
843
844 if (n == 0)
845 return (0);
846
847 for (w = 0; w < maxw; w++)
848 popc += dt_popc(bp[w]);
849
850 return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
851}
852
853#if defined(sun)
854struct _rwlock;
855struct _lwp_mutex;
856
857int
858dt_rw_read_held(pthread_rwlock_t *lock)
859{
860 extern int _rw_read_held(struct _rwlock *);
861 return (_rw_read_held((struct _rwlock *)lock));
862}
863
864int
865dt_rw_write_held(pthread_rwlock_t *lock)
866{
867 extern int _rw_write_held(struct _rwlock *);
868 return (_rw_write_held((struct _rwlock *)lock));
869}
870#endif
871
872int
873dt_mutex_held(pthread_mutex_t *lock)
874{
875#if defined(sun)
876 extern int _mutex_held(struct _lwp_mutex *);
877 return (_mutex_held((struct _lwp_mutex *)lock));
878#else
879 return (1);
880#endif
881}
882
883static int
884dt_string2str(char *s, char *str, int nbytes)
885{
886 int len = strlen(s);
887
888 if (nbytes == 0) {
889 /*
890 * Like snprintf(3C), we don't check the value of str if the
891 * number of bytes is 0.
892 */
893 return (len);
894 }
895
896 if (nbytes <= len) {
897 (void) strncpy(str, s, nbytes - 1);
898 /*
899 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
900 * that the string is null-terminated.
901 */
902 str[nbytes - 1] = '\0';
903 } else {
904 (void) strcpy(str, s);
905 }
906
907 return (len);
908}
909
910int
911dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
912{
913 dtrace_syminfo_t dts;
914 GElf_Sym sym;
915
916 size_t n = 20; /* for 0x%llx\0 */
917 char *s;
918 int err;
919
920 if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
921 n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
922
923 s = alloca(n);
924
925 if (err == 0 && addr != sym.st_value) {
926 (void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
927 dts.dts_name, (u_longlong_t)addr - sym.st_value);
928 } else if (err == 0) {
929 (void) snprintf(s, n, "%s`%s",
930 dts.dts_object, dts.dts_name);
931 } else {
932 /*
933 * We'll repeat the lookup, but this time we'll specify a NULL
934 * GElf_Sym -- indicating that we're only interested in the
935 * containing module.
936 */
937 if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
938 (void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
939 (u_longlong_t)addr);
940 } else {
941 (void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
942 }
943 }
944
945 return (dt_string2str(s, str, nbytes));
946}
947
948int
949dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
950 uint64_t addr, char *str, int nbytes)
951{
952 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
953 struct ps_prochandle *P = NULL;
954 GElf_Sym sym;
955 char *obj;
956
957 if (pid != 0)
958 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
959
960 if (P == NULL) {
961 (void) snprintf(c, sizeof (c), "0x%llx", addr);
962 return (dt_string2str(c, str, nbytes));
963 }
964
965 dt_proc_lock(dtp, P);
966
966#if defined(sun)
967 if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
968 (void) Pobjname(P, addr, objname, sizeof (objname));
967 if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
968 (void) Pobjname(P, addr, objname, sizeof (objname));
969#else
970 if (proc_addr2sym(P, addr, name, sizeof (name), &sym) == 0) {
971 (void) proc_objname(P, addr, objname, sizeof (objname));
972#endif
973
974 obj = dt_basename(objname);
975
976 if (addr > sym.st_value) {
977 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
978 name, (u_longlong_t)(addr - sym.st_value));
979 } else {
980 (void) snprintf(c, sizeof (c), "%s`%s", obj, name);
981 }
969
970 obj = dt_basename(objname);
971
972 if (addr > sym.st_value) {
973 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
974 name, (u_longlong_t)(addr - sym.st_value));
975 } else {
976 (void) snprintf(c, sizeof (c), "%s`%s", obj, name);
977 }
982#if defined(sun)
983 } else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) {
978 } else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) {
984#else
985 } else if (proc_objname(P, addr, objname, sizeof (objname)) != 0) {
986#endif
987 (void) snprintf(c, sizeof (c), "%s`0x%llx",
988 dt_basename(objname), addr);
989 } else {
990 (void) snprintf(c, sizeof (c), "0x%llx", addr);
991 }
992
993 dt_proc_unlock(dtp, P);
994 dt_proc_release(dtp, P);
995
996 return (dt_string2str(c, str, nbytes));
997}
979 (void) snprintf(c, sizeof (c), "%s`0x%llx",
980 dt_basename(objname), addr);
981 } else {
982 (void) snprintf(c, sizeof (c), "0x%llx", addr);
983 }
984
985 dt_proc_unlock(dtp, P);
986 dt_proc_release(dtp, P);
987
988 return (dt_string2str(c, str, nbytes));
989}