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