xref: /titanic_44/usr/src/lib/libdtrace/common/dt_subr.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/sysmacros.h>
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 #include <alloca.h>
40 #include <assert.h>
41 #include <libgen.h>
42 
43 #include <dt_impl.h>
44 
45 static const struct {
46 	size_t dtps_offset;
47 	size_t dtps_len;
48 } dtrace_probespecs[] = {
49 	{ offsetof(dtrace_probedesc_t, dtpd_provider),	DTRACE_PROVNAMELEN },
50 	{ offsetof(dtrace_probedesc_t, dtpd_mod),	DTRACE_MODNAMELEN },
51 	{ offsetof(dtrace_probedesc_t, dtpd_func),	DTRACE_FUNCNAMELEN },
52 	{ offsetof(dtrace_probedesc_t, dtpd_name),	DTRACE_NAMELEN }
53 };
54 
55 int
56 dtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
57     const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
58 {
59 	size_t off, len, vlen;
60 	const char *p, *q, *v;
61 
62 	char buf[32]; /* for id_t as %d (see below) */
63 
64 	if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
65 		return (dt_set_errno(dtp, EINVAL));
66 
67 	bzero(pdp, sizeof (dtrace_probedesc_t));
68 	p = s + strlen(s) - 1;
69 
70 	do {
71 		for (len = 0; p >= s && *p != ':'; len++)
72 			p--; /* move backward until we find a delimiter */
73 
74 		q = p + 1;
75 		vlen = 0;
76 
77 		if ((v = strchr(q, '$')) != NULL && v < q + len) {
78 			/*
79 			 * Set vlen to the length of the variable name and then
80 			 * reset len to the length of the text prior to '$'. If
81 			 * the name begins with a digit, interpret it using the
82 			 * the argv[] array.  Otherwise we look in dt_macros.
83 			 * For the moment, all dt_macros variables are of type
84 			 * id_t (see dtrace_update() for more details on that).
85 			 */
86 			vlen = (size_t)(q + len - v);
87 			len = (size_t)(v - q);
88 
89 			/*
90 			 * If the variable string begins with $$, skip past the
91 			 * leading dollar sign since $ and $$ are equivalent
92 			 * macro reference operators in a probe description.
93 			 */
94 			if (vlen > 2 && v[1] == '$') {
95 				vlen--;
96 				v++;
97 			}
98 
99 			if (isdigit(v[1])) {
100 				char *end;
101 				long i;
102 
103 				errno = 0;
104 				i = strtol(v + 1, &end, 10);
105 
106 				if (i < 0 || i >= argc ||
107 				    errno != 0 || end != v + vlen)
108 					return (dt_set_errno(dtp, EDT_BADSPCV));
109 
110 				v = argv[i];
111 				vlen = strlen(v);
112 
113 				if (yypcb != NULL && yypcb->pcb_sargv == argv)
114 					yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
115 
116 			} else if (vlen > 1) {
117 				char *vstr = alloca(vlen);
118 				dt_ident_t *idp;
119 
120 				(void) strncpy(vstr, v + 1, vlen - 1);
121 				vstr[vlen - 1] = '\0';
122 				idp = dt_idhash_lookup(dtp->dt_macros, vstr);
123 
124 				if (idp == NULL)
125 					return (dt_set_errno(dtp, EDT_BADSPCV));
126 
127 				v = buf;
128 				vlen = snprintf(buf, 32, "%d", idp->di_id);
129 
130 			} else
131 				return (dt_set_errno(dtp, EDT_BADSPCV));
132 		}
133 
134 		if (spec == DTRACE_PROBESPEC_NONE)
135 			return (dt_set_errno(dtp, EDT_BADSPEC));
136 
137 		if (len + vlen >= dtrace_probespecs[spec].dtps_len)
138 			return (dt_set_errno(dtp, ENAMETOOLONG));
139 
140 		off = dtrace_probespecs[spec--].dtps_offset;
141 		bcopy(q, (char *)pdp + off, len);
142 		bcopy(v, (char *)pdp + off + len, vlen);
143 
144 	} while (--p >= s);
145 
146 	pdp->dtpd_id = DTRACE_IDNONE;
147 	return (0);
148 }
149 
150 int
151 dtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
152     const char *s, dtrace_probedesc_t *pdp)
153 {
154 	return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
155 }
156 
157 int
158 dtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
159 {
160 	bzero(pdp, sizeof (dtrace_probedesc_t));
161 	pdp->dtpd_id = id;
162 
163 	if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
164 	    pdp->dtpd_id != id)
165 		return (dt_set_errno(dtp, EDT_BADID));
166 
167 	return (0);
168 }
169 
170 char *
171 dtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
172 {
173 	if (pdp->dtpd_id == 0) {
174 		(void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
175 		    pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
176 	} else
177 		(void) snprintf(buf, len, "%u", pdp->dtpd_id);
178 
179 	return (buf);
180 }
181 
182 char *
183 dtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
184 {
185 	const char *name = dtrace_stability_name(attr.dtat_name);
186 	const char *data = dtrace_stability_name(attr.dtat_data);
187 	const char *class = dtrace_class_name(attr.dtat_class);
188 
189 	if (name == NULL || data == NULL || class == NULL)
190 		return (NULL); /* one or more invalid attributes */
191 
192 	(void) snprintf(buf, len, "%s/%s/%s", name, data, class);
193 	return (buf);
194 }
195 
196 static char *
197 dt_getstrattr(char *p, char **qp)
198 {
199 	char *q;
200 
201 	if (*p == '\0')
202 		return (NULL);
203 
204 	if ((q = strchr(p, '/')) == NULL)
205 		q = p + strlen(p);
206 	else
207 		*q++ = '\0';
208 
209 	*qp = q;
210 	return (p);
211 }
212 
213 int
214 dtrace_str2attr(const char *str, dtrace_attribute_t *attr)
215 {
216 	dtrace_stability_t s;
217 	dtrace_class_t c;
218 	char *p, *q;
219 
220 	if (str == NULL || attr == NULL)
221 		return (-1); /* invalid function arguments */
222 
223 	*attr = _dtrace_maxattr;
224 	p = alloca(strlen(str) + 1);
225 	(void) strcpy(p, str);
226 
227 	if ((p = dt_getstrattr(p, &q)) == NULL)
228 		return (0);
229 
230 	for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
231 		if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
232 			attr->dtat_name = s;
233 			break;
234 		}
235 	}
236 
237 	if (s > DTRACE_STABILITY_MAX)
238 		return (-1);
239 
240 	if ((p = dt_getstrattr(q, &q)) == NULL)
241 		return (0);
242 
243 	for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
244 		if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
245 			attr->dtat_data = s;
246 			break;
247 		}
248 	}
249 
250 	if (s > DTRACE_STABILITY_MAX)
251 		return (-1);
252 
253 	if ((p = dt_getstrattr(q, &q)) == NULL)
254 		return (0);
255 
256 	for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
257 		if (strcasecmp(p, dtrace_class_name(c)) == 0) {
258 			attr->dtat_class = c;
259 			break;
260 		}
261 	}
262 
263 	if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
264 		return (-1);
265 
266 	return (0);
267 }
268 
269 const char *
270 dtrace_stability_name(dtrace_stability_t s)
271 {
272 	switch (s) {
273 	case DTRACE_STABILITY_INTERNAL:	return ("Internal");
274 	case DTRACE_STABILITY_PRIVATE:	return ("Private");
275 	case DTRACE_STABILITY_OBSOLETE:	return ("Obsolete");
276 	case DTRACE_STABILITY_EXTERNAL:	return ("External");
277 	case DTRACE_STABILITY_UNSTABLE:	return ("Unstable");
278 	case DTRACE_STABILITY_EVOLVING:	return ("Evolving");
279 	case DTRACE_STABILITY_STABLE:	return ("Stable");
280 	case DTRACE_STABILITY_STANDARD:	return ("Standard");
281 	default:			return (NULL);
282 	}
283 }
284 
285 const char *
286 dtrace_class_name(dtrace_class_t c)
287 {
288 	switch (c) {
289 	case DTRACE_CLASS_UNKNOWN:	return ("Unknown");
290 	case DTRACE_CLASS_CPU:		return ("CPU");
291 	case DTRACE_CLASS_PLATFORM:	return ("Platform");
292 	case DTRACE_CLASS_GROUP:	return ("Group");
293 	case DTRACE_CLASS_ISA:		return ("ISA");
294 	case DTRACE_CLASS_COMMON:	return ("Common");
295 	default:			return (NULL);
296 	}
297 }
298 
299 dtrace_attribute_t
300 dt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
301 {
302 	dtrace_attribute_t am;
303 
304 	am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
305 	am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
306 	am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
307 
308 	return (am);
309 }
310 
311 dtrace_attribute_t
312 dt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
313 {
314 	dtrace_attribute_t am;
315 
316 	am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
317 	am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
318 	am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
319 
320 	return (am);
321 }
322 
323 /*
324  * Compare two attributes and return an integer value in the following ranges:
325  *
326  * <0 if any of a1's attributes are less than a2's attributes
327  * =0 if all of a1's attributes are equal to a2's attributes
328  * >0 if all of a1's attributes are greater than or equal to a2's attributes
329  *
330  * To implement this function efficiently, we subtract a2's attributes from
331  * a1's to obtain a negative result if an a1 attribute is less than its a2
332  * counterpart.  We then OR the intermediate results together, relying on the
333  * twos-complement property that if any result is negative, the bitwise union
334  * will also be negative since the highest bit will be set in the result.
335  */
336 int
337 dt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
338 {
339 	return (((int)a1.dtat_name - a2.dtat_name) |
340 	    ((int)a1.dtat_data - a2.dtat_data) |
341 	    ((int)a1.dtat_class - a2.dtat_class));
342 }
343 
344 char *
345 dt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
346 {
347 	static const char stability[] = "ipoxuesS";
348 	static const char class[] = "uCpgIc";
349 
350 	if (a.dtat_name < sizeof (stability) &&
351 	    a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
352 		(void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
353 		    stability[a.dtat_data], class[a.dtat_class]);
354 	} else {
355 		(void) snprintf(buf, len, "[%u/%u/%u]",
356 		    a.dtat_name, a.dtat_data, a.dtat_class);
357 	}
358 
359 	return (buf);
360 }
361 
362 char *
363 dt_version_num2str(dt_version_t v, char *buf, size_t len)
364 {
365 	uint_t M = DT_VERSION_MAJOR(v);
366 	uint_t m = DT_VERSION_MINOR(v);
367 	uint_t u = DT_VERSION_MICRO(v);
368 
369 	if (u == 0)
370 		(void) snprintf(buf, len, "%u.%u", M, m);
371 	else
372 		(void) snprintf(buf, len, "%u.%u.%u", M, m, u);
373 
374 	return (buf);
375 }
376 
377 int
378 dt_version_str2num(const char *s, dt_version_t *vp)
379 {
380 	int i = 0, n[3] = { 0, 0, 0 };
381 	char c;
382 
383 	while ((c = *s++) != '\0') {
384 		if (isdigit(c))
385 			n[i] = n[i] * 10 + c - '0';
386 		else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
387 			return (-1);
388 	}
389 
390 	if (n[0] > DT_VERSION_MAJMAX ||
391 	    n[1] > DT_VERSION_MINMAX ||
392 	    n[2] > DT_VERSION_MICMAX)
393 		return (-1);
394 
395 	if (vp != NULL)
396 		*vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
397 
398 	return (0);
399 }
400 
401 int
402 dt_version_defined(dt_version_t v)
403 {
404 	int i;
405 
406 	for (i = 0; _dtrace_versions[i] != 0; i++) {
407 		if (_dtrace_versions[i] == v)
408 			return (1);
409 	}
410 
411 	return (0);
412 }
413 
414 char *
415 dt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
416 {
417 	char *arg;
418 
419 	if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
420 		int olds = dtp->dt_cpp_args;
421 		int news = olds * 2;
422 		char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
423 
424 		if (argv == NULL)
425 			return (NULL);
426 
427 		bzero(&argv[olds], sizeof (char *) * olds);
428 		dtp->dt_cpp_argv = argv;
429 		dtp->dt_cpp_args = news;
430 	}
431 
432 	if ((arg = strdup(str)) == NULL)
433 		return (NULL);
434 
435 	assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
436 	dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
437 	return (arg);
438 }
439 
440 char *
441 dt_cpp_pop_arg(dtrace_hdl_t *dtp)
442 {
443 	char *arg;
444 
445 	if (dtp->dt_cpp_argc <= 1)
446 		return (NULL); /* dt_cpp_argv[0] cannot be popped */
447 
448 	arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
449 	dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
450 
451 	return (arg);
452 }
453 
454 /*PRINTFLIKE1*/
455 void
456 dt_dprintf(const char *format, ...)
457 {
458 	if (_dtrace_debug) {
459 		va_list alist;
460 
461 		va_start(alist, format);
462 		(void) fputs("libdtrace DEBUG: ", stderr);
463 		(void) vfprintf(stderr, format, alist);
464 		va_end(alist);
465 	}
466 }
467 
468 int
469 dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
470 {
471 	const dtrace_vector_t *v = dtp->dt_vector;
472 
473 	if (v != NULL)
474 		return (v->dtv_ioctl(dtp->dt_varg, val, arg));
475 
476 	if (dtp->dt_fd >= 0)
477 		return (ioctl(dtp->dt_fd, val, arg));
478 
479 	errno = EBADF;
480 	return (-1);
481 }
482 
483 int
484 dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
485 {
486 	const dtrace_vector_t *v = dtp->dt_vector;
487 
488 	if (v == NULL)
489 		return (p_online(cpu, P_STATUS));
490 
491 	return (v->dtv_status(dtp->dt_varg, cpu));
492 }
493 
494 long
495 dt_sysconf(dtrace_hdl_t *dtp, int name)
496 {
497 	const dtrace_vector_t *v = dtp->dt_vector;
498 
499 	if (v == NULL)
500 		return (sysconf(name));
501 
502 	return (v->dtv_sysconf(dtp->dt_varg, name));
503 }
504 
505 /*
506  * Wrapper around write(2) to handle partial writes.  For maximum safety of
507  * output files and proper error reporting, we continuing writing in the
508  * face of partial writes until write(2) fails or 'buf' is completely written.
509  * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
510  */
511 ssize_t
512 dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
513 {
514 	ssize_t resid = n;
515 	ssize_t len;
516 
517 	while (resid != 0) {
518 		if ((len = write(fd, buf, resid)) <= 0)
519 			break;
520 
521 		resid -= len;
522 		buf = (char *)buf + len;
523 	}
524 
525 	if (resid == n && n != 0)
526 		return (dt_set_errno(dtp, errno));
527 
528 	return (n - resid);
529 }
530 
531 /*
532  * This function handles all output from libdtrace, as well as the
533  * dtrace_sprintf() case.  If we're here due to dtrace_sprintf(), then
534  * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
535  * specified buffer and return.  Otherwise, if output is buffered (denoted by
536  * a NULL fp), we sprintf the desired output into the buffered buffer
537  * (expanding the buffer if required).  If we don't satisfy either of these
538  * conditions (that is, if we are to actually generate output), then we call
539  * fprintf with the specified fp.  In this case, we need to deal with one of
540  * the more annoying peculiarities of libc's printf routines:  any failed
541  * write persistently sets an error flag inside the FILE causing every
542  * subsequent write to fail, but only the caller that initiated the error gets
543  * the errno.  Since libdtrace clients often intercept SIGINT, this case is
544  * particularly frustrating since we don't want the EINTR on one attempt to
545  * write to the output file to preclude later attempts to write.  This
546  * function therefore does a clearerr() if any error occurred, and saves the
547  * errno for the caller inside the specified dtrace_hdl_t.
548  */
549 /*PRINTFLIKE3*/
550 int
551 dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
552 {
553 	va_list ap;
554 	int n;
555 
556 	va_start(ap, format);
557 
558 	if (dtp->dt_sprintf_buflen != 0) {
559 		int len;
560 		char *buf;
561 
562 		assert(dtp->dt_sprintf_buf != NULL);
563 
564 		buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
565 		len = dtp->dt_sprintf_buflen - len;
566 		assert(len >= 0);
567 
568 		if ((n = vsnprintf(buf, len, format, ap)) < 0)
569 			n = dt_set_errno(dtp, errno);
570 
571 		va_end(ap);
572 
573 		return (n);
574 	}
575 
576 	if (fp == NULL) {
577 		int needed, rval;
578 		size_t avail;
579 
580 		/*
581 		 * It's not legal to use buffered ouput if there is not a
582 		 * handler for buffered output.
583 		 */
584 		if (dtp->dt_bufhdlr == NULL) {
585 			va_end(ap);
586 			return (dt_set_errno(dtp, EDT_NOBUFFERED));
587 		}
588 
589 		if (dtp->dt_buffered_buf == NULL) {
590 			assert(dtp->dt_buffered_size == 0);
591 			dtp->dt_buffered_size = 1;
592 			dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
593 
594 			if (dtp->dt_buffered_buf == NULL) {
595 				va_end(ap);
596 				return (dt_set_errno(dtp, EDT_NOMEM));
597 			}
598 
599 			dtp->dt_buffered_offs = 0;
600 			dtp->dt_buffered_buf[0] = '\0';
601 		}
602 
603 		if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) {
604 			rval = dt_set_errno(dtp, errno);
605 			va_end(ap);
606 			return (rval);
607 		}
608 
609 		if (needed == 0) {
610 			va_end(ap);
611 			return (0);
612 		}
613 
614 		for (;;) {
615 			char *newbuf;
616 
617 			assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
618 			avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
619 
620 			if (needed + 1 < avail)
621 				break;
622 
623 			if ((newbuf = realloc(dtp->dt_buffered_buf,
624 			    dtp->dt_buffered_size << 1)) == NULL) {
625 				va_end(ap);
626 				return (dt_set_errno(dtp, EDT_NOMEM));
627 			}
628 
629 			dtp->dt_buffered_buf = newbuf;
630 			dtp->dt_buffered_size <<= 1;
631 		}
632 
633 		if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
634 		    avail, format, ap) < 0) {
635 			rval = dt_set_errno(dtp, errno);
636 			va_end(ap);
637 			return (rval);
638 		}
639 
640 		dtp->dt_buffered_offs += needed;
641 		assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
642 		return (0);
643 	}
644 
645 	n = vfprintf(fp, format, ap);
646 	va_end(ap);
647 
648 	if (n < 0) {
649 		clearerr(fp);
650 		return (dt_set_errno(dtp, errno));
651 	}
652 
653 	return (n);
654 }
655 
656 int
657 dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
658     dtrace_recdesc_t *rec, dtrace_aggdata_t *agg)
659 {
660 	dtrace_bufdata_t data;
661 
662 	if (dtp->dt_buffered_offs == 0)
663 		return (0);
664 
665 	data.dtbda_handle = dtp;
666 	data.dtbda_buffered = dtp->dt_buffered_buf;
667 	data.dtbda_probe = pdata;
668 	data.dtbda_recdesc = rec;
669 	data.dtbda_aggdata = agg;
670 
671 	if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
672 		return (dt_set_errno(dtp, EDT_DIRABORT));
673 
674 	dtp->dt_buffered_offs = 0;
675 	dtp->dt_buffered_buf[0] = '\0';
676 
677 	return (0);
678 }
679 
680 void
681 dt_buffered_destroy(dtrace_hdl_t *dtp)
682 {
683 	free(dtp->dt_buffered_buf);
684 	dtp->dt_buffered_buf = NULL;
685 	dtp->dt_buffered_offs = 0;
686 	dtp->dt_buffered_size = 0;
687 }
688 
689 void *
690 dt_zalloc(dtrace_hdl_t *dtp, size_t size)
691 {
692 	void *data;
693 
694 	if ((data = malloc(size)) == NULL)
695 		(void) dt_set_errno(dtp, EDT_NOMEM);
696 	else
697 		bzero(data, size);
698 
699 	return (data);
700 }
701 
702 void *
703 dt_alloc(dtrace_hdl_t *dtp, size_t size)
704 {
705 	void *data;
706 
707 	if ((data = malloc(size)) == NULL)
708 		(void) dt_set_errno(dtp, EDT_NOMEM);
709 
710 	return (data);
711 }
712 
713 void
714 dt_free(dtrace_hdl_t *dtp, void *data)
715 {
716 	assert(dtp != NULL); /* ensure sane use of this interface */
717 	free(data);
718 }
719 
720 /*
721  * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
722  * implements the behavior that an empty pattern matches any string.
723  */
724 int
725 dt_gmatch(const char *s, const char *p)
726 {
727 	return (p == NULL || *p == '\0' || gmatch(s, p));
728 }
729 
730 char *
731 dt_basename(char *str)
732 {
733 	char *last = strrchr(str, '/');
734 
735 	if (last == NULL)
736 		return (str);
737 
738 	return (last + 1);
739 }
740 
741 struct _rwlock;
742 struct _lwp_mutex;
743 
744 int
745 dt_rw_read_held(pthread_rwlock_t *lock)
746 {
747 	extern int _rw_read_held(struct _rwlock *);
748 	return (_rw_read_held((struct _rwlock *)lock));
749 }
750 
751 int
752 dt_rw_write_held(pthread_rwlock_t *lock)
753 {
754 	extern int _rw_write_held(struct _rwlock *);
755 	return (_rw_write_held((struct _rwlock *)lock));
756 }
757 
758 int
759 dt_mutex_held(pthread_mutex_t *lock)
760 {
761 	extern int _mutex_held(struct _lwp_mutex *);
762 	return (_mutex_held((struct _lwp_mutex *)lock));
763 }
764