xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_subr.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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 	va_list ap2;
585 	int n;
586 
587 #ifndef illumos
588 	/*
589 	 * On FreeBSD, check if output is currently being re-directed
590 	 * to another file. If so, output to that file instead of the
591 	 * one the caller has specified.
592 	 */
593 	if (dtp->dt_freopen_fp != NULL)
594 		fp = dtp->dt_freopen_fp;
595 #endif
596 
597 	va_start(ap, format);
598 
599 	if (dtp->dt_sprintf_buflen != 0) {
600 		int len;
601 		char *buf;
602 
603 		assert(dtp->dt_sprintf_buf != NULL);
604 
605 		buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
606 		len = dtp->dt_sprintf_buflen - len;
607 		assert(len >= 0);
608 
609 		va_copy(ap2, ap);
610 		if ((n = vsnprintf(buf, len, format, ap2)) < 0)
611 			n = dt_set_errno(dtp, errno);
612 
613 		va_end(ap2);
614 		va_end(ap);
615 
616 		return (n);
617 	}
618 
619 	if (fp == NULL) {
620 		int needed, rval;
621 		size_t avail;
622 
623 		/*
624 		 * Using buffered output is not allowed if a handler has
625 		 * not been installed.
626 		 */
627 		if (dtp->dt_bufhdlr == NULL) {
628 			va_end(ap);
629 			return (dt_set_errno(dtp, EDT_NOBUFFERED));
630 		}
631 
632 		if (dtp->dt_buffered_buf == NULL) {
633 			assert(dtp->dt_buffered_size == 0);
634 			dtp->dt_buffered_size = 1;
635 			dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
636 
637 			if (dtp->dt_buffered_buf == NULL) {
638 				va_end(ap);
639 				return (dt_set_errno(dtp, EDT_NOMEM));
640 			}
641 
642 			dtp->dt_buffered_offs = 0;
643 			dtp->dt_buffered_buf[0] = '\0';
644 		}
645 
646 		va_copy(ap2, ap);
647 		if ((needed = vsnprintf(NULL, 0, format, ap2)) < 0) {
648 			rval = dt_set_errno(dtp, errno);
649 			va_end(ap2);
650 			va_end(ap);
651 			return (rval);
652 		}
653 		va_end(ap2);
654 
655 		if (needed == 0) {
656 			va_end(ap);
657 			return (0);
658 		}
659 
660 		for (;;) {
661 			char *newbuf;
662 
663 			assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
664 			avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
665 
666 			if (needed + 1 < avail)
667 				break;
668 
669 			if ((newbuf = realloc(dtp->dt_buffered_buf,
670 			    dtp->dt_buffered_size << 1)) == NULL) {
671 				va_end(ap);
672 				return (dt_set_errno(dtp, EDT_NOMEM));
673 			}
674 
675 			dtp->dt_buffered_buf = newbuf;
676 			dtp->dt_buffered_size <<= 1;
677 		}
678 
679 		va_copy(ap2, ap);
680 		if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
681 		    avail, format, ap2) < 0) {
682 			rval = dt_set_errno(dtp, errno);
683 			va_end(ap2);
684 			va_end(ap);
685 			return (rval);
686 		}
687 		va_end(ap2);
688 
689 		dtp->dt_buffered_offs += needed;
690 		assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
691 		va_end(ap);
692 		return (0);
693 	}
694 
695 	va_copy(ap2, ap);
696 	n = vfprintf(fp, format, ap2);
697 	va_end(ap2);
698 	va_end(ap);
699 
700 	if (n < 0) {
701 		clearerr(fp);
702 		return (dt_set_errno(dtp, errno));
703 	}
704 
705 	return (n);
706 }
707 
708 int
709 dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
710     const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
711 {
712 	dtrace_bufdata_t data;
713 
714 	if (dtp->dt_buffered_offs == 0)
715 		return (0);
716 
717 	data.dtbda_handle = dtp;
718 	data.dtbda_buffered = dtp->dt_buffered_buf;
719 	data.dtbda_probe = pdata;
720 	data.dtbda_recdesc = rec;
721 	data.dtbda_aggdata = agg;
722 	data.dtbda_flags = flags;
723 
724 	if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
725 		return (dt_set_errno(dtp, EDT_DIRABORT));
726 
727 	dtp->dt_buffered_offs = 0;
728 	dtp->dt_buffered_buf[0] = '\0';
729 
730 	return (0);
731 }
732 
733 void
734 dt_buffered_destroy(dtrace_hdl_t *dtp)
735 {
736 	free(dtp->dt_buffered_buf);
737 	dtp->dt_buffered_buf = NULL;
738 	dtp->dt_buffered_offs = 0;
739 	dtp->dt_buffered_size = 0;
740 }
741 
742 void *
743 dt_zalloc(dtrace_hdl_t *dtp, size_t size)
744 {
745 	void *data;
746 
747 	if ((data = malloc(size)) == NULL)
748 		(void) dt_set_errno(dtp, EDT_NOMEM);
749 	else
750 		bzero(data, size);
751 
752 	return (data);
753 }
754 
755 void *
756 dt_alloc(dtrace_hdl_t *dtp, size_t size)
757 {
758 	void *data;
759 
760 	if ((data = malloc(size)) == NULL)
761 		(void) dt_set_errno(dtp, EDT_NOMEM);
762 
763 	return (data);
764 }
765 
766 void
767 dt_free(dtrace_hdl_t *dtp, void *data)
768 {
769 	assert(dtp != NULL); /* ensure sane use of this interface */
770 	free(data);
771 }
772 
773 void
774 dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
775 {
776 	if (dp == NULL)
777 		return; /* simplify caller code */
778 
779 	dt_free(dtp, dp->dtdo_buf);
780 	dt_free(dtp, dp->dtdo_inttab);
781 	dt_free(dtp, dp->dtdo_strtab);
782 	dt_free(dtp, dp->dtdo_vartab);
783 	dt_free(dtp, dp->dtdo_kreltab);
784 	dt_free(dtp, dp->dtdo_ureltab);
785 	dt_free(dtp, dp->dtdo_xlmtab);
786 
787 	dt_free(dtp, dp);
788 }
789 
790 /*
791  * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
792  * implements the behavior that an empty pattern matches any string.
793  */
794 int
795 dt_gmatch(const char *s, const char *p)
796 {
797 	return (p == NULL || *p == '\0' || gmatch(s, p));
798 }
799 
800 char *
801 dt_basename(char *str)
802 {
803 	char *last = strrchr(str, '/');
804 
805 	if (last == NULL)
806 		return (str);
807 
808 	return (last + 1);
809 }
810 
811 /*
812  * dt_popc() is a fast implementation of population count.  The algorithm is
813  * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
814  */
815 ulong_t
816 dt_popc(ulong_t x)
817 {
818 #if defined(_ILP32)
819 	x = x - ((x >> 1) & 0x55555555UL);
820 	x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
821 	x = (x + (x >> 4)) & 0x0F0F0F0FUL;
822 	x = x + (x >> 8);
823 	x = x + (x >> 16);
824 	return (x & 0x3F);
825 #elif defined(_LP64)
826 	x = x - ((x >> 1) & 0x5555555555555555ULL);
827 	x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
828 	x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
829 	x = x + (x >> 8);
830 	x = x + (x >> 16);
831 	x = x + (x >> 32);
832 	return (x & 0x7F);
833 #else
834 /* This should be a #warning but for now ignore error. Err: "need td_popc() implementation" */
835 #endif
836 }
837 
838 /*
839  * dt_popcb() is a bitmap-based version of population count that returns the
840  * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
841  */
842 ulong_t
843 dt_popcb(const ulong_t *bp, ulong_t n)
844 {
845 	ulong_t maxb = n & BT_ULMASK;
846 	ulong_t maxw = n >> BT_ULSHIFT;
847 	ulong_t w, popc = 0;
848 
849 	if (n == 0)
850 		return (0);
851 
852 	for (w = 0; w < maxw; w++)
853 		popc += dt_popc(bp[w]);
854 
855 	return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
856 }
857 
858 #ifdef illumos
859 struct _rwlock;
860 struct _lwp_mutex;
861 
862 int
863 dt_rw_read_held(pthread_rwlock_t *lock)
864 {
865 	extern int _rw_read_held(struct _rwlock *);
866 	return (_rw_read_held((struct _rwlock *)lock));
867 }
868 
869 int
870 dt_rw_write_held(pthread_rwlock_t *lock)
871 {
872 	extern int _rw_write_held(struct _rwlock *);
873 	return (_rw_write_held((struct _rwlock *)lock));
874 }
875 #endif
876 
877 int
878 dt_mutex_held(pthread_mutex_t *lock)
879 {
880 #ifdef illumos
881 	extern int _mutex_held(struct _lwp_mutex *);
882 	return (_mutex_held((struct _lwp_mutex *)lock));
883 #else
884 	return (1);
885 #endif
886 }
887 
888 static int
889 dt_string2str(char *s, char *str, int nbytes)
890 {
891 	int len = strlen(s);
892 
893 	if (nbytes == 0) {
894 		/*
895 		 * Like snprintf(3C), we don't check the value of str if the
896 		 * number of bytes is 0.
897 		 */
898 		return (len);
899 	}
900 
901 	if (nbytes <= len) {
902 		(void) strncpy(str, s, nbytes - 1);
903 		/*
904 		 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
905 		 * that the string is null-terminated.
906 		 */
907 		str[nbytes - 1] = '\0';
908 	} else {
909 		(void) strcpy(str, s);
910 	}
911 
912 	return (len);
913 }
914 
915 int
916 dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
917 {
918 	dtrace_syminfo_t dts;
919 	GElf_Sym sym;
920 
921 	size_t n = 20; /* for 0x%llx\0 */
922 	char *s;
923 	int err;
924 
925 	if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
926 		n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
927 
928 	s = alloca(n);
929 
930 	if (err == 0 && addr != sym.st_value) {
931 		(void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
932 		    dts.dts_name, (u_longlong_t)addr - sym.st_value);
933 	} else if (err == 0) {
934 		(void) snprintf(s, n, "%s`%s",
935 		    dts.dts_object, dts.dts_name);
936 	} else {
937 		/*
938 		 * We'll repeat the lookup, but this time we'll specify a NULL
939 		 * GElf_Sym -- indicating that we're only interested in the
940 		 * containing module.
941 		 */
942 		if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
943 			(void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
944 			    (u_longlong_t)addr);
945 		} else {
946 			(void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
947 		}
948 	}
949 
950 	return (dt_string2str(s, str, nbytes));
951 }
952 
953 int
954 dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
955     uint64_t addr, char *str, int nbytes)
956 {
957 	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
958 	struct ps_prochandle *P = NULL;
959 	GElf_Sym sym;
960 	char *obj;
961 
962 	if (pid != 0)
963 		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
964 
965 	if (P == NULL) {
966 	  (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr);
967 		return (dt_string2str(c, str, nbytes));
968 	}
969 
970 	dt_proc_lock(dtp, P);
971 
972 	if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
973 		(void) Pobjname(P, addr, objname, sizeof (objname));
974 
975 		obj = dt_basename(objname);
976 
977 		if (addr > sym.st_value) {
978 			(void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
979 			    name, (u_longlong_t)(addr - sym.st_value));
980 		} else {
981 			(void) snprintf(c, sizeof (c), "%s`%s", obj, name);
982 		}
983 	} else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) {
984 		(void) snprintf(c, sizeof (c), "%s`0x%jx",
985 				dt_basename(objname), (uintmax_t)addr);
986 	} else {
987 	  (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr);
988 	}
989 
990 	dt_proc_unlock(dtp, P);
991 	dt_proc_release(dtp, P);
992 
993 	return (dt_string2str(c, str, nbytes));
994 }
995