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