dt_subr.c revision 210767
1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5210767Srpaulo * Common Development and Distribution License (the "License").
6210767Srpaulo * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21210767Srpaulo
22178479Sjb/*
23210767Srpaulo * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24178479Sjb * Use is subject to license terms.
25178479Sjb */
26178479Sjb
27178556Sjb#if defined(sun)
28178479Sjb#include <sys/sysmacros.h>
29178556Sjb#endif
30178479Sjb
31178479Sjb#include <strings.h>
32178479Sjb#include <unistd.h>
33178479Sjb#include <stdarg.h>
34178479Sjb#include <stddef.h>
35178479Sjb#include <stdlib.h>
36178479Sjb#include <stdio.h>
37178479Sjb#include <errno.h>
38178479Sjb#include <ctype.h>
39178556Sjb#if defined(sun)
40178479Sjb#include <alloca.h>
41178556Sjb#else
42178556Sjb#include <sys/sysctl.h>
43178556Sjb#endif
44178479Sjb#include <assert.h>
45178479Sjb#include <libgen.h>
46178479Sjb#include <limits.h>
47178479Sjb
48178479Sjb#include <dt_impl.h>
49178479Sjb
50178479Sjbstatic const struct {
51178479Sjb	size_t dtps_offset;
52178479Sjb	size_t dtps_len;
53178479Sjb} dtrace_probespecs[] = {
54178479Sjb	{ offsetof(dtrace_probedesc_t, dtpd_provider),	DTRACE_PROVNAMELEN },
55178479Sjb	{ offsetof(dtrace_probedesc_t, dtpd_mod),	DTRACE_MODNAMELEN },
56178479Sjb	{ offsetof(dtrace_probedesc_t, dtpd_func),	DTRACE_FUNCNAMELEN },
57178479Sjb	{ offsetof(dtrace_probedesc_t, dtpd_name),	DTRACE_NAMELEN }
58178479Sjb};
59178479Sjb
60178479Sjbint
61178479Sjbdtrace_xstr2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
62178479Sjb    const char *s, int argc, char *const argv[], dtrace_probedesc_t *pdp)
63178479Sjb{
64210767Srpaulo	size_t off, len, vlen, wlen;
65210767Srpaulo	const char *p, *q, *v, *w;
66178479Sjb
67178479Sjb	char buf[32]; /* for id_t as %d (see below) */
68178479Sjb
69178479Sjb	if (spec < DTRACE_PROBESPEC_NONE || spec > DTRACE_PROBESPEC_NAME)
70178479Sjb		return (dt_set_errno(dtp, EINVAL));
71178479Sjb
72178479Sjb	bzero(pdp, sizeof (dtrace_probedesc_t));
73178479Sjb	p = s + strlen(s) - 1;
74178479Sjb
75178479Sjb	do {
76178479Sjb		for (len = 0; p >= s && *p != ':'; len++)
77178479Sjb			p--; /* move backward until we find a delimiter */
78178479Sjb
79178479Sjb		q = p + 1;
80178479Sjb		vlen = 0;
81210767Srpaulo		w = NULL;
82210767Srpaulo		wlen = 0;
83178479Sjb
84178479Sjb		if ((v = strchr(q, '$')) != NULL && v < q + len) {
85178479Sjb			/*
86178479Sjb			 * Set vlen to the length of the variable name and then
87178479Sjb			 * reset len to the length of the text prior to '$'. If
88178479Sjb			 * the name begins with a digit, interpret it using the
89178479Sjb			 * the argv[] array.  Otherwise we look in dt_macros.
90178479Sjb			 * For the moment, all dt_macros variables are of type
91178479Sjb			 * id_t (see dtrace_update() for more details on that).
92178479Sjb			 */
93178479Sjb			vlen = (size_t)(q + len - v);
94178479Sjb			len = (size_t)(v - q);
95178479Sjb
96178479Sjb			/*
97178479Sjb			 * If the variable string begins with $$, skip past the
98178479Sjb			 * leading dollar sign since $ and $$ are equivalent
99178479Sjb			 * macro reference operators in a probe description.
100178479Sjb			 */
101178479Sjb			if (vlen > 2 && v[1] == '$') {
102178479Sjb				vlen--;
103178479Sjb				v++;
104178479Sjb			}
105178479Sjb
106178479Sjb			if (isdigit(v[1])) {
107178479Sjb				long i;
108178479Sjb
109178479Sjb				errno = 0;
110210767Srpaulo				i = strtol(v + 1, (char **)&w, 10);
111178479Sjb
112210767Srpaulo				wlen = vlen - (w - v);
113210767Srpaulo
114210767Srpaulo				if (i < 0 || i >= argc || errno != 0)
115178479Sjb					return (dt_set_errno(dtp, EDT_BADSPCV));
116178479Sjb
117178479Sjb				v = argv[i];
118178479Sjb				vlen = strlen(v);
119178479Sjb
120178479Sjb				if (yypcb != NULL && yypcb->pcb_sargv == argv)
121178479Sjb					yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
122178479Sjb
123178479Sjb			} else if (vlen > 1) {
124178479Sjb				char *vstr = alloca(vlen);
125178479Sjb				dt_ident_t *idp;
126178479Sjb
127178479Sjb				(void) strncpy(vstr, v + 1, vlen - 1);
128178479Sjb				vstr[vlen - 1] = '\0';
129178479Sjb				idp = dt_idhash_lookup(dtp->dt_macros, vstr);
130178479Sjb
131178479Sjb				if (idp == NULL)
132178479Sjb					return (dt_set_errno(dtp, EDT_BADSPCV));
133178479Sjb
134178479Sjb				v = buf;
135178479Sjb				vlen = snprintf(buf, 32, "%d", idp->di_id);
136178479Sjb
137178479Sjb			} else
138178479Sjb				return (dt_set_errno(dtp, EDT_BADSPCV));
139178479Sjb		}
140178479Sjb
141178479Sjb		if (spec == DTRACE_PROBESPEC_NONE)
142178479Sjb			return (dt_set_errno(dtp, EDT_BADSPEC));
143178479Sjb
144178479Sjb		if (len + vlen >= dtrace_probespecs[spec].dtps_len)
145178479Sjb			return (dt_set_errno(dtp, ENAMETOOLONG));
146178479Sjb
147178479Sjb		off = dtrace_probespecs[spec--].dtps_offset;
148178479Sjb		bcopy(q, (char *)pdp + off, len);
149178479Sjb		bcopy(v, (char *)pdp + off + len, vlen);
150210767Srpaulo		bcopy(w, (char *)pdp + off + len + vlen, wlen);
151178479Sjb	} while (--p >= s);
152178479Sjb
153178479Sjb	pdp->dtpd_id = DTRACE_IDNONE;
154178479Sjb	return (0);
155178479Sjb}
156178479Sjb
157178479Sjbint
158178479Sjbdtrace_str2desc(dtrace_hdl_t *dtp, dtrace_probespec_t spec,
159178479Sjb    const char *s, dtrace_probedesc_t *pdp)
160178479Sjb{
161178479Sjb	return (dtrace_xstr2desc(dtp, spec, s, 0, NULL, pdp));
162178479Sjb}
163178479Sjb
164178479Sjbint
165178479Sjbdtrace_id2desc(dtrace_hdl_t *dtp, dtrace_id_t id, dtrace_probedesc_t *pdp)
166178479Sjb{
167178479Sjb	bzero(pdp, sizeof (dtrace_probedesc_t));
168178479Sjb	pdp->dtpd_id = id;
169178479Sjb
170178479Sjb	if (dt_ioctl(dtp, DTRACEIOC_PROBES, pdp) == -1 ||
171178479Sjb	    pdp->dtpd_id != id)
172178479Sjb		return (dt_set_errno(dtp, EDT_BADID));
173178479Sjb
174178479Sjb	return (0);
175178479Sjb}
176178479Sjb
177178479Sjbchar *
178178479Sjbdtrace_desc2str(const dtrace_probedesc_t *pdp, char *buf, size_t len)
179178479Sjb{
180178479Sjb	if (pdp->dtpd_id == 0) {
181178479Sjb		(void) snprintf(buf, len, "%s:%s:%s:%s", pdp->dtpd_provider,
182178479Sjb		    pdp->dtpd_mod, pdp->dtpd_func, pdp->dtpd_name);
183178479Sjb	} else
184178479Sjb		(void) snprintf(buf, len, "%u", pdp->dtpd_id);
185178479Sjb
186178479Sjb	return (buf);
187178479Sjb}
188178479Sjb
189178479Sjbchar *
190178479Sjbdtrace_attr2str(dtrace_attribute_t attr, char *buf, size_t len)
191178479Sjb{
192178479Sjb	const char *name = dtrace_stability_name(attr.dtat_name);
193178479Sjb	const char *data = dtrace_stability_name(attr.dtat_data);
194178479Sjb	const char *class = dtrace_class_name(attr.dtat_class);
195178479Sjb
196178479Sjb	if (name == NULL || data == NULL || class == NULL)
197178479Sjb		return (NULL); /* one or more invalid attributes */
198178479Sjb
199178479Sjb	(void) snprintf(buf, len, "%s/%s/%s", name, data, class);
200178479Sjb	return (buf);
201178479Sjb}
202178479Sjb
203178479Sjbstatic char *
204178479Sjbdt_getstrattr(char *p, char **qp)
205178479Sjb{
206178479Sjb	char *q;
207178479Sjb
208178479Sjb	if (*p == '\0')
209178479Sjb		return (NULL);
210178479Sjb
211178479Sjb	if ((q = strchr(p, '/')) == NULL)
212178479Sjb		q = p + strlen(p);
213178479Sjb	else
214178479Sjb		*q++ = '\0';
215178479Sjb
216178479Sjb	*qp = q;
217178479Sjb	return (p);
218178479Sjb}
219178479Sjb
220178479Sjbint
221178479Sjbdtrace_str2attr(const char *str, dtrace_attribute_t *attr)
222178479Sjb{
223178479Sjb	dtrace_stability_t s;
224178479Sjb	dtrace_class_t c;
225178479Sjb	char *p, *q;
226178479Sjb
227178479Sjb	if (str == NULL || attr == NULL)
228178479Sjb		return (-1); /* invalid function arguments */
229178479Sjb
230178479Sjb	*attr = _dtrace_maxattr;
231178479Sjb	p = alloca(strlen(str) + 1);
232178479Sjb	(void) strcpy(p, str);
233178479Sjb
234178479Sjb	if ((p = dt_getstrattr(p, &q)) == NULL)
235178479Sjb		return (0);
236178479Sjb
237178479Sjb	for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
238178479Sjb		if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
239178479Sjb			attr->dtat_name = s;
240178479Sjb			break;
241178479Sjb		}
242178479Sjb	}
243178479Sjb
244178479Sjb	if (s > DTRACE_STABILITY_MAX)
245178479Sjb		return (-1);
246178479Sjb
247178479Sjb	if ((p = dt_getstrattr(q, &q)) == NULL)
248178479Sjb		return (0);
249178479Sjb
250178479Sjb	for (s = 0; s <= DTRACE_STABILITY_MAX; s++) {
251178479Sjb		if (strcasecmp(p, dtrace_stability_name(s)) == 0) {
252178479Sjb			attr->dtat_data = s;
253178479Sjb			break;
254178479Sjb		}
255178479Sjb	}
256178479Sjb
257178479Sjb	if (s > DTRACE_STABILITY_MAX)
258178479Sjb		return (-1);
259178479Sjb
260178479Sjb	if ((p = dt_getstrattr(q, &q)) == NULL)
261178479Sjb		return (0);
262178479Sjb
263178479Sjb	for (c = 0; c <= DTRACE_CLASS_MAX; c++) {
264178479Sjb		if (strcasecmp(p, dtrace_class_name(c)) == 0) {
265178479Sjb			attr->dtat_class = c;
266178479Sjb			break;
267178479Sjb		}
268178479Sjb	}
269178479Sjb
270178479Sjb	if (c > DTRACE_CLASS_MAX || (p = dt_getstrattr(q, &q)) != NULL)
271178479Sjb		return (-1);
272178479Sjb
273178479Sjb	return (0);
274178479Sjb}
275178479Sjb
276178479Sjbconst char *
277178479Sjbdtrace_stability_name(dtrace_stability_t s)
278178479Sjb{
279178479Sjb	switch (s) {
280178479Sjb	case DTRACE_STABILITY_INTERNAL:	return ("Internal");
281178479Sjb	case DTRACE_STABILITY_PRIVATE:	return ("Private");
282178479Sjb	case DTRACE_STABILITY_OBSOLETE:	return ("Obsolete");
283178479Sjb	case DTRACE_STABILITY_EXTERNAL:	return ("External");
284178479Sjb	case DTRACE_STABILITY_UNSTABLE:	return ("Unstable");
285178479Sjb	case DTRACE_STABILITY_EVOLVING:	return ("Evolving");
286178479Sjb	case DTRACE_STABILITY_STABLE:	return ("Stable");
287178479Sjb	case DTRACE_STABILITY_STANDARD:	return ("Standard");
288178479Sjb	default:			return (NULL);
289178479Sjb	}
290178479Sjb}
291178479Sjb
292178479Sjbconst char *
293178479Sjbdtrace_class_name(dtrace_class_t c)
294178479Sjb{
295178479Sjb	switch (c) {
296178479Sjb	case DTRACE_CLASS_UNKNOWN:	return ("Unknown");
297178479Sjb	case DTRACE_CLASS_CPU:		return ("CPU");
298178479Sjb	case DTRACE_CLASS_PLATFORM:	return ("Platform");
299178479Sjb	case DTRACE_CLASS_GROUP:	return ("Group");
300178479Sjb	case DTRACE_CLASS_ISA:		return ("ISA");
301178479Sjb	case DTRACE_CLASS_COMMON:	return ("Common");
302178479Sjb	default:			return (NULL);
303178479Sjb	}
304178479Sjb}
305178479Sjb
306178479Sjbdtrace_attribute_t
307178479Sjbdt_attr_min(dtrace_attribute_t a1, dtrace_attribute_t a2)
308178479Sjb{
309178479Sjb	dtrace_attribute_t am;
310178479Sjb
311178479Sjb	am.dtat_name = MIN(a1.dtat_name, a2.dtat_name);
312178479Sjb	am.dtat_data = MIN(a1.dtat_data, a2.dtat_data);
313178479Sjb	am.dtat_class = MIN(a1.dtat_class, a2.dtat_class);
314178479Sjb
315178479Sjb	return (am);
316178479Sjb}
317178479Sjb
318178479Sjbdtrace_attribute_t
319178479Sjbdt_attr_max(dtrace_attribute_t a1, dtrace_attribute_t a2)
320178479Sjb{
321178479Sjb	dtrace_attribute_t am;
322178479Sjb
323178479Sjb	am.dtat_name = MAX(a1.dtat_name, a2.dtat_name);
324178479Sjb	am.dtat_data = MAX(a1.dtat_data, a2.dtat_data);
325178479Sjb	am.dtat_class = MAX(a1.dtat_class, a2.dtat_class);
326178479Sjb
327178479Sjb	return (am);
328178479Sjb}
329178479Sjb
330178479Sjb/*
331178479Sjb * Compare two attributes and return an integer value in the following ranges:
332178479Sjb *
333178479Sjb * <0 if any of a1's attributes are less than a2's attributes
334178479Sjb * =0 if all of a1's attributes are equal to a2's attributes
335178479Sjb * >0 if all of a1's attributes are greater than or equal to a2's attributes
336178479Sjb *
337178479Sjb * To implement this function efficiently, we subtract a2's attributes from
338178479Sjb * a1's to obtain a negative result if an a1 attribute is less than its a2
339178479Sjb * counterpart.  We then OR the intermediate results together, relying on the
340178479Sjb * twos-complement property that if any result is negative, the bitwise union
341178479Sjb * will also be negative since the highest bit will be set in the result.
342178479Sjb */
343178479Sjbint
344178479Sjbdt_attr_cmp(dtrace_attribute_t a1, dtrace_attribute_t a2)
345178479Sjb{
346178479Sjb	return (((int)a1.dtat_name - a2.dtat_name) |
347178479Sjb	    ((int)a1.dtat_data - a2.dtat_data) |
348178479Sjb	    ((int)a1.dtat_class - a2.dtat_class));
349178479Sjb}
350178479Sjb
351178479Sjbchar *
352178479Sjbdt_attr_str(dtrace_attribute_t a, char *buf, size_t len)
353178479Sjb{
354178479Sjb	static const char stability[] = "ipoxuesS";
355178479Sjb	static const char class[] = "uCpgIc";
356178479Sjb
357178479Sjb	if (a.dtat_name < sizeof (stability) &&
358178479Sjb	    a.dtat_data < sizeof (stability) && a.dtat_class < sizeof (class)) {
359178479Sjb		(void) snprintf(buf, len, "[%c/%c/%c]", stability[a.dtat_name],
360178479Sjb		    stability[a.dtat_data], class[a.dtat_class]);
361178479Sjb	} else {
362178479Sjb		(void) snprintf(buf, len, "[%u/%u/%u]",
363178479Sjb		    a.dtat_name, a.dtat_data, a.dtat_class);
364178479Sjb	}
365178479Sjb
366178479Sjb	return (buf);
367178479Sjb}
368178479Sjb
369178479Sjbchar *
370178479Sjbdt_version_num2str(dt_version_t v, char *buf, size_t len)
371178479Sjb{
372178479Sjb	uint_t M = DT_VERSION_MAJOR(v);
373178479Sjb	uint_t m = DT_VERSION_MINOR(v);
374178479Sjb	uint_t u = DT_VERSION_MICRO(v);
375178479Sjb
376178479Sjb	if (u == 0)
377178479Sjb		(void) snprintf(buf, len, "%u.%u", M, m);
378178479Sjb	else
379178479Sjb		(void) snprintf(buf, len, "%u.%u.%u", M, m, u);
380178479Sjb
381178479Sjb	return (buf);
382178479Sjb}
383178479Sjb
384178479Sjbint
385178479Sjbdt_version_str2num(const char *s, dt_version_t *vp)
386178479Sjb{
387178479Sjb	int i = 0, n[3] = { 0, 0, 0 };
388178479Sjb	char c;
389178479Sjb
390178479Sjb	while ((c = *s++) != '\0') {
391178479Sjb		if (isdigit(c))
392178479Sjb			n[i] = n[i] * 10 + c - '0';
393178479Sjb		else if (c != '.' || i++ >= sizeof (n) / sizeof (n[0]) - 1)
394178479Sjb			return (-1);
395178479Sjb	}
396178479Sjb
397178479Sjb	if (n[0] > DT_VERSION_MAJMAX ||
398178479Sjb	    n[1] > DT_VERSION_MINMAX ||
399178479Sjb	    n[2] > DT_VERSION_MICMAX)
400178479Sjb		return (-1);
401178479Sjb
402178479Sjb	if (vp != NULL)
403178479Sjb		*vp = DT_VERSION_NUMBER(n[0], n[1], n[2]);
404178479Sjb
405178479Sjb	return (0);
406178479Sjb}
407178479Sjb
408178479Sjbint
409178479Sjbdt_version_defined(dt_version_t v)
410178479Sjb{
411178479Sjb	int i;
412178479Sjb
413178479Sjb	for (i = 0; _dtrace_versions[i] != 0; i++) {
414178479Sjb		if (_dtrace_versions[i] == v)
415178479Sjb			return (1);
416178479Sjb	}
417178479Sjb
418178479Sjb	return (0);
419178479Sjb}
420178479Sjb
421178479Sjbchar *
422178479Sjbdt_cpp_add_arg(dtrace_hdl_t *dtp, const char *str)
423178479Sjb{
424178479Sjb	char *arg;
425178479Sjb
426178479Sjb	if (dtp->dt_cpp_argc == dtp->dt_cpp_args) {
427178479Sjb		int olds = dtp->dt_cpp_args;
428178479Sjb		int news = olds * 2;
429178479Sjb		char **argv = realloc(dtp->dt_cpp_argv, sizeof (char *) * news);
430178479Sjb
431178479Sjb		if (argv == NULL)
432178479Sjb			return (NULL);
433178479Sjb
434178479Sjb		bzero(&argv[olds], sizeof (char *) * olds);
435178479Sjb		dtp->dt_cpp_argv = argv;
436178479Sjb		dtp->dt_cpp_args = news;
437178479Sjb	}
438178479Sjb
439178479Sjb	if ((arg = strdup(str)) == NULL)
440178479Sjb		return (NULL);
441178479Sjb
442178479Sjb	assert(dtp->dt_cpp_argc < dtp->dt_cpp_args);
443178479Sjb	dtp->dt_cpp_argv[dtp->dt_cpp_argc++] = arg;
444178479Sjb	return (arg);
445178479Sjb}
446178479Sjb
447178479Sjbchar *
448178479Sjbdt_cpp_pop_arg(dtrace_hdl_t *dtp)
449178479Sjb{
450178479Sjb	char *arg;
451178479Sjb
452178479Sjb	if (dtp->dt_cpp_argc <= 1)
453178479Sjb		return (NULL); /* dt_cpp_argv[0] cannot be popped */
454178479Sjb
455178479Sjb	arg = dtp->dt_cpp_argv[--dtp->dt_cpp_argc];
456178479Sjb	dtp->dt_cpp_argv[dtp->dt_cpp_argc] = NULL;
457178479Sjb
458178479Sjb	return (arg);
459178479Sjb}
460178479Sjb
461178479Sjb/*PRINTFLIKE1*/
462178479Sjbvoid
463178479Sjbdt_dprintf(const char *format, ...)
464178479Sjb{
465178479Sjb	if (_dtrace_debug) {
466178479Sjb		va_list alist;
467178479Sjb
468178479Sjb		va_start(alist, format);
469178479Sjb		(void) fputs("libdtrace DEBUG: ", stderr);
470178479Sjb		(void) vfprintf(stderr, format, alist);
471178479Sjb		va_end(alist);
472178479Sjb	}
473178479Sjb}
474178479Sjb
475178479Sjbint
476178556Sjb#if defined(sun)
477178479Sjbdt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
478178556Sjb#else
479178556Sjbdt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg)
480178556Sjb#endif
481178479Sjb{
482178479Sjb	const dtrace_vector_t *v = dtp->dt_vector;
483178479Sjb
484178556Sjb#if !defined(sun)
485178556Sjb	/* Avoid sign extension. */
486178556Sjb	val &= 0xffffffff;
487178556Sjb#endif
488178556Sjb
489178479Sjb	if (v != NULL)
490178479Sjb		return (v->dtv_ioctl(dtp->dt_varg, val, arg));
491178479Sjb
492178479Sjb	if (dtp->dt_fd >= 0)
493178479Sjb		return (ioctl(dtp->dt_fd, val, arg));
494178479Sjb
495178479Sjb	errno = EBADF;
496178479Sjb	return (-1);
497178479Sjb}
498178479Sjb
499178479Sjbint
500178479Sjbdt_status(dtrace_hdl_t *dtp, processorid_t cpu)
501178479Sjb{
502178479Sjb	const dtrace_vector_t *v = dtp->dt_vector;
503178479Sjb
504178556Sjb	if (v == NULL) {
505178556Sjb#if defined(sun)
506178479Sjb		return (p_online(cpu, P_STATUS));
507178556Sjb#else
508178556Sjb		int maxid = 0;
509178556Sjb		size_t len = sizeof(maxid);
510178556Sjb		if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0)
511178556Sjb			return (cpu == 0 ? 1 : -1);
512178556Sjb		else
513178556Sjb			return (cpu <= maxid ? 1 : -1);
514178556Sjb#endif
515178556Sjb	}
516178479Sjb
517178479Sjb	return (v->dtv_status(dtp->dt_varg, cpu));
518178479Sjb}
519178479Sjb
520178479Sjblong
521178479Sjbdt_sysconf(dtrace_hdl_t *dtp, int name)
522178479Sjb{
523178479Sjb	const dtrace_vector_t *v = dtp->dt_vector;
524178479Sjb
525178479Sjb	if (v == NULL)
526178479Sjb		return (sysconf(name));
527178479Sjb
528178479Sjb	return (v->dtv_sysconf(dtp->dt_varg, name));
529178479Sjb}
530178479Sjb
531178479Sjb/*
532178479Sjb * Wrapper around write(2) to handle partial writes.  For maximum safety of
533178479Sjb * output files and proper error reporting, we continuing writing in the
534178479Sjb * face of partial writes until write(2) fails or 'buf' is completely written.
535178479Sjb * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
536178479Sjb */
537178479Sjbssize_t
538178479Sjbdt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
539178479Sjb{
540178479Sjb	ssize_t resid = n;
541178479Sjb	ssize_t len;
542178479Sjb
543178479Sjb	while (resid != 0) {
544178479Sjb		if ((len = write(fd, buf, resid)) <= 0)
545178479Sjb			break;
546178479Sjb
547178479Sjb		resid -= len;
548178479Sjb		buf = (char *)buf + len;
549178479Sjb	}
550178479Sjb
551178479Sjb	if (resid == n && n != 0)
552178479Sjb		return (dt_set_errno(dtp, errno));
553178479Sjb
554178479Sjb	return (n - resid);
555178479Sjb}
556178479Sjb
557178479Sjb/*
558178479Sjb * This function handles all output from libdtrace, as well as the
559178479Sjb * dtrace_sprintf() case.  If we're here due to dtrace_sprintf(), then
560178479Sjb * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
561178479Sjb * specified buffer and return.  Otherwise, if output is buffered (denoted by
562178479Sjb * a NULL fp), we sprintf the desired output into the buffered buffer
563178479Sjb * (expanding the buffer if required).  If we don't satisfy either of these
564178479Sjb * conditions (that is, if we are to actually generate output), then we call
565178479Sjb * fprintf with the specified fp.  In this case, we need to deal with one of
566178479Sjb * the more annoying peculiarities of libc's printf routines:  any failed
567178479Sjb * write persistently sets an error flag inside the FILE causing every
568178479Sjb * subsequent write to fail, but only the caller that initiated the error gets
569178479Sjb * the errno.  Since libdtrace clients often intercept SIGINT, this case is
570178479Sjb * particularly frustrating since we don't want the EINTR on one attempt to
571178479Sjb * write to the output file to preclude later attempts to write.  This
572178479Sjb * function therefore does a clearerr() if any error occurred, and saves the
573178479Sjb * errno for the caller inside the specified dtrace_hdl_t.
574178479Sjb */
575178479Sjb/*PRINTFLIKE3*/
576178479Sjbint
577178479Sjbdt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
578178479Sjb{
579178479Sjb	va_list ap;
580178479Sjb	int n;
581178479Sjb
582178556Sjb#if !defined(sun)
583178556Sjb	/*
584178556Sjb	 * On FreeBSD, check if output is currently being re-directed
585178556Sjb	 * to another file. If so, output to that file instead of the
586178556Sjb	 * one the caller has specified.
587178556Sjb	 */
588178556Sjb	if (dtp->dt_freopen_fp != NULL)
589178556Sjb		fp = dtp->dt_freopen_fp;
590178556Sjb#endif
591178556Sjb
592178479Sjb	va_start(ap, format);
593178479Sjb
594178479Sjb	if (dtp->dt_sprintf_buflen != 0) {
595178479Sjb		int len;
596178479Sjb		char *buf;
597178479Sjb
598178479Sjb		assert(dtp->dt_sprintf_buf != NULL);
599178479Sjb
600178479Sjb		buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
601178479Sjb		len = dtp->dt_sprintf_buflen - len;
602178479Sjb		assert(len >= 0);
603178479Sjb
604178479Sjb		if ((n = vsnprintf(buf, len, format, ap)) < 0)
605178479Sjb			n = dt_set_errno(dtp, errno);
606178479Sjb
607178479Sjb		va_end(ap);
608178479Sjb
609178479Sjb		return (n);
610178479Sjb	}
611178479Sjb
612178479Sjb	if (fp == NULL) {
613178479Sjb		int needed, rval;
614178479Sjb		size_t avail;
615178479Sjb
616178479Sjb		/*
617178479Sjb		 * It's not legal to use buffered ouput if there is not a
618178479Sjb		 * handler for buffered output.
619178479Sjb		 */
620178479Sjb		if (dtp->dt_bufhdlr == NULL) {
621178479Sjb			va_end(ap);
622178479Sjb			return (dt_set_errno(dtp, EDT_NOBUFFERED));
623178479Sjb		}
624178479Sjb
625178479Sjb		if (dtp->dt_buffered_buf == NULL) {
626178479Sjb			assert(dtp->dt_buffered_size == 0);
627178479Sjb			dtp->dt_buffered_size = 1;
628178479Sjb			dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
629178479Sjb
630178479Sjb			if (dtp->dt_buffered_buf == NULL) {
631178479Sjb				va_end(ap);
632178479Sjb				return (dt_set_errno(dtp, EDT_NOMEM));
633178479Sjb			}
634178479Sjb
635178479Sjb			dtp->dt_buffered_offs = 0;
636178479Sjb			dtp->dt_buffered_buf[0] = '\0';
637178479Sjb		}
638178479Sjb
639178479Sjb		if ((needed = vsnprintf(NULL, 0, format, ap)) < 0) {
640178479Sjb			rval = dt_set_errno(dtp, errno);
641178479Sjb			va_end(ap);
642178479Sjb			return (rval);
643178479Sjb		}
644178479Sjb
645178479Sjb		if (needed == 0) {
646178479Sjb			va_end(ap);
647178479Sjb			return (0);
648178479Sjb		}
649178479Sjb
650178479Sjb		for (;;) {
651178479Sjb			char *newbuf;
652178479Sjb
653178479Sjb			assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
654178479Sjb			avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
655178479Sjb
656178479Sjb			if (needed + 1 < avail)
657178479Sjb				break;
658178479Sjb
659178479Sjb			if ((newbuf = realloc(dtp->dt_buffered_buf,
660178479Sjb			    dtp->dt_buffered_size << 1)) == NULL) {
661178479Sjb				va_end(ap);
662178479Sjb				return (dt_set_errno(dtp, EDT_NOMEM));
663178479Sjb			}
664178479Sjb
665178479Sjb			dtp->dt_buffered_buf = newbuf;
666178479Sjb			dtp->dt_buffered_size <<= 1;
667178479Sjb		}
668178479Sjb
669178479Sjb		if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
670178479Sjb		    avail, format, ap) < 0) {
671178479Sjb			rval = dt_set_errno(dtp, errno);
672178479Sjb			va_end(ap);
673178479Sjb			return (rval);
674178479Sjb		}
675178479Sjb
676178479Sjb		dtp->dt_buffered_offs += needed;
677178479Sjb		assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
678178479Sjb		return (0);
679178479Sjb	}
680178479Sjb
681178479Sjb	n = vfprintf(fp, format, ap);
682178556Sjb	fflush(fp);
683178479Sjb	va_end(ap);
684178479Sjb
685178479Sjb	if (n < 0) {
686178479Sjb		clearerr(fp);
687178479Sjb		return (dt_set_errno(dtp, errno));
688178479Sjb	}
689178479Sjb
690178479Sjb	return (n);
691178479Sjb}
692178479Sjb
693178479Sjbint
694178479Sjbdt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
695178479Sjb    const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
696178479Sjb{
697178479Sjb	dtrace_bufdata_t data;
698178479Sjb
699178479Sjb	if (dtp->dt_buffered_offs == 0)
700178479Sjb		return (0);
701178479Sjb
702178479Sjb	data.dtbda_handle = dtp;
703178479Sjb	data.dtbda_buffered = dtp->dt_buffered_buf;
704178479Sjb	data.dtbda_probe = pdata;
705178479Sjb	data.dtbda_recdesc = rec;
706178479Sjb	data.dtbda_aggdata = agg;
707178479Sjb	data.dtbda_flags = flags;
708178479Sjb
709178479Sjb	if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
710178479Sjb		return (dt_set_errno(dtp, EDT_DIRABORT));
711178479Sjb
712178479Sjb	dtp->dt_buffered_offs = 0;
713178479Sjb	dtp->dt_buffered_buf[0] = '\0';
714178479Sjb
715178479Sjb	return (0);
716178479Sjb}
717178479Sjb
718178479Sjbvoid
719178479Sjbdt_buffered_destroy(dtrace_hdl_t *dtp)
720178479Sjb{
721178479Sjb	free(dtp->dt_buffered_buf);
722178479Sjb	dtp->dt_buffered_buf = NULL;
723178479Sjb	dtp->dt_buffered_offs = 0;
724178479Sjb	dtp->dt_buffered_size = 0;
725178479Sjb}
726178479Sjb
727178479Sjbvoid *
728178479Sjbdt_zalloc(dtrace_hdl_t *dtp, size_t size)
729178479Sjb{
730178479Sjb	void *data;
731178479Sjb
732178556Sjb	if (size > 16 * 1024 * 1024) {
733178556Sjb		(void) dt_set_errno(dtp, EDT_NOMEM);
734178556Sjb		return (NULL);
735178556Sjb	}
736178556Sjb
737178479Sjb	if ((data = malloc(size)) == NULL)
738178479Sjb		(void) dt_set_errno(dtp, EDT_NOMEM);
739178479Sjb	else
740178479Sjb		bzero(data, size);
741178479Sjb
742178479Sjb	return (data);
743178479Sjb}
744178479Sjb
745178479Sjbvoid *
746178479Sjbdt_alloc(dtrace_hdl_t *dtp, size_t size)
747178479Sjb{
748178479Sjb	void *data;
749178479Sjb
750178556Sjb	if (size > 16 * 1024 * 1024) {
751178556Sjb		(void) dt_set_errno(dtp, EDT_NOMEM);
752178556Sjb		return (NULL);
753178556Sjb	}
754178556Sjb
755178479Sjb	if ((data = malloc(size)) == NULL)
756178479Sjb		(void) dt_set_errno(dtp, EDT_NOMEM);
757178479Sjb
758178479Sjb	return (data);
759178479Sjb}
760178479Sjb
761178479Sjbvoid
762178479Sjbdt_free(dtrace_hdl_t *dtp, void *data)
763178479Sjb{
764178479Sjb	assert(dtp != NULL); /* ensure sane use of this interface */
765178479Sjb	free(data);
766178479Sjb}
767178479Sjb
768178479Sjbvoid
769178479Sjbdt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
770178479Sjb{
771178479Sjb	if (dp == NULL)
772178479Sjb		return; /* simplify caller code */
773178479Sjb
774178479Sjb	dt_free(dtp, dp->dtdo_buf);
775178479Sjb	dt_free(dtp, dp->dtdo_inttab);
776178479Sjb	dt_free(dtp, dp->dtdo_strtab);
777178479Sjb	dt_free(dtp, dp->dtdo_vartab);
778178479Sjb	dt_free(dtp, dp->dtdo_kreltab);
779178479Sjb	dt_free(dtp, dp->dtdo_ureltab);
780178479Sjb	dt_free(dtp, dp->dtdo_xlmtab);
781178479Sjb
782178479Sjb	dt_free(dtp, dp);
783178479Sjb}
784178479Sjb
785178479Sjb/*
786178479Sjb * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
787178479Sjb * implements the behavior that an empty pattern matches any string.
788178479Sjb */
789178479Sjbint
790178479Sjbdt_gmatch(const char *s, const char *p)
791178479Sjb{
792178479Sjb	return (p == NULL || *p == '\0' || gmatch(s, p));
793178479Sjb}
794178479Sjb
795178479Sjbchar *
796178479Sjbdt_basename(char *str)
797178479Sjb{
798178479Sjb	char *last = strrchr(str, '/');
799178479Sjb
800178479Sjb	if (last == NULL)
801178479Sjb		return (str);
802178479Sjb
803178479Sjb	return (last + 1);
804178479Sjb}
805178479Sjb
806178479Sjb/*
807178479Sjb * dt_popc() is a fast implementation of population count.  The algorithm is
808178479Sjb * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
809178479Sjb */
810178479Sjbulong_t
811178479Sjbdt_popc(ulong_t x)
812178479Sjb{
813178479Sjb#ifdef _ILP32
814178479Sjb	x = x - ((x >> 1) & 0x55555555UL);
815178479Sjb	x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
816178479Sjb	x = (x + (x >> 4)) & 0x0F0F0F0FUL;
817178479Sjb	x = x + (x >> 8);
818178479Sjb	x = x + (x >> 16);
819178479Sjb	return (x & 0x3F);
820178479Sjb#endif
821178479Sjb#ifdef _LP64
822178479Sjb	x = x - ((x >> 1) & 0x5555555555555555ULL);
823178479Sjb	x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
824178479Sjb	x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
825178479Sjb	x = x + (x >> 8);
826178479Sjb	x = x + (x >> 16);
827178479Sjb	x = x + (x >> 32);
828178479Sjb	return (x & 0x7F);
829178479Sjb#endif
830178479Sjb}
831178479Sjb
832178479Sjb/*
833178479Sjb * dt_popcb() is a bitmap-based version of population count that returns the
834178479Sjb * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
835178479Sjb */
836178479Sjbulong_t
837178479Sjbdt_popcb(const ulong_t *bp, ulong_t n)
838178479Sjb{
839178479Sjb	ulong_t maxb = n & BT_ULMASK;
840178479Sjb	ulong_t maxw = n >> BT_ULSHIFT;
841178479Sjb	ulong_t w, popc = 0;
842178479Sjb
843178479Sjb	if (n == 0)
844178479Sjb		return (0);
845178479Sjb
846178479Sjb	for (w = 0; w < maxw; w++)
847178479Sjb		popc += dt_popc(bp[w]);
848178479Sjb
849178479Sjb	return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
850178479Sjb}
851178479Sjb
852178556Sjb#if defined(sun)
853178479Sjbstruct _rwlock;
854178479Sjbstruct _lwp_mutex;
855178479Sjb
856178479Sjbint
857178479Sjbdt_rw_read_held(pthread_rwlock_t *lock)
858178479Sjb{
859178479Sjb	extern int _rw_read_held(struct _rwlock *);
860178479Sjb	return (_rw_read_held((struct _rwlock *)lock));
861178479Sjb}
862178479Sjb
863178479Sjbint
864178479Sjbdt_rw_write_held(pthread_rwlock_t *lock)
865178479Sjb{
866178479Sjb	extern int _rw_write_held(struct _rwlock *);
867178479Sjb	return (_rw_write_held((struct _rwlock *)lock));
868178479Sjb}
869178556Sjb#endif
870178479Sjb
871178479Sjbint
872178479Sjbdt_mutex_held(pthread_mutex_t *lock)
873178479Sjb{
874178556Sjb#if defined(sun)
875178479Sjb	extern int _mutex_held(struct _lwp_mutex *);
876178479Sjb	return (_mutex_held((struct _lwp_mutex *)lock));
877178556Sjb#else
878178556Sjb	return (1);
879178556Sjb#endif
880178479Sjb}
881178479Sjb
882178479Sjbstatic int
883178479Sjbdt_string2str(char *s, char *str, int nbytes)
884178479Sjb{
885178479Sjb	int len = strlen(s);
886178479Sjb
887178479Sjb	if (nbytes == 0) {
888178479Sjb		/*
889178479Sjb		 * Like snprintf(3C), we don't check the value of str if the
890178479Sjb		 * number of bytes is 0.
891178479Sjb		 */
892178479Sjb		return (len);
893178479Sjb	}
894178479Sjb
895178479Sjb	if (nbytes <= len) {
896178479Sjb		(void) strncpy(str, s, nbytes - 1);
897178479Sjb		/*
898178479Sjb		 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
899178479Sjb		 * that the string is null-terminated.
900178479Sjb		 */
901178479Sjb		str[nbytes - 1] = '\0';
902178479Sjb	} else {
903178479Sjb		(void) strcpy(str, s);
904178479Sjb	}
905178479Sjb
906178479Sjb	return (len);
907178479Sjb}
908178479Sjb
909178479Sjbint
910178479Sjbdtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
911178479Sjb{
912178479Sjb	dtrace_syminfo_t dts;
913178479Sjb	GElf_Sym sym;
914178479Sjb
915178479Sjb	size_t n = 20; /* for 0x%llx\0 */
916178479Sjb	char *s;
917178479Sjb	int err;
918178479Sjb
919178479Sjb	if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
920178479Sjb		n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
921178479Sjb
922178479Sjb	s = alloca(n);
923178479Sjb
924178479Sjb	if (err == 0 && addr != sym.st_value) {
925178479Sjb		(void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
926178479Sjb		    dts.dts_name, (u_longlong_t)addr - sym.st_value);
927178479Sjb	} else if (err == 0) {
928178479Sjb		(void) snprintf(s, n, "%s`%s",
929178479Sjb		    dts.dts_object, dts.dts_name);
930178479Sjb	} else {
931178479Sjb		/*
932178479Sjb		 * We'll repeat the lookup, but this time we'll specify a NULL
933178479Sjb		 * GElf_Sym -- indicating that we're only interested in the
934178479Sjb		 * containing module.
935178479Sjb		 */
936178479Sjb		if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
937178479Sjb			(void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
938178479Sjb			    (u_longlong_t)addr);
939178479Sjb		} else {
940178479Sjb			(void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
941178479Sjb		}
942178479Sjb	}
943178479Sjb
944178479Sjb	return (dt_string2str(s, str, nbytes));
945178479Sjb}
946178479Sjb
947178479Sjbint
948178479Sjbdtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
949178479Sjb    uint64_t addr, char *str, int nbytes)
950178479Sjb{
951178479Sjb	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
952178479Sjb	struct ps_prochandle *P = NULL;
953178479Sjb	GElf_Sym sym;
954178479Sjb	char *obj;
955178479Sjb
956178479Sjb	if (pid != 0)
957178479Sjb		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
958178479Sjb
959178479Sjb	if (P == NULL) {
960178479Sjb		(void) snprintf(c, sizeof (c), "0x%llx", addr);
961178479Sjb		return (dt_string2str(c, str, nbytes));
962178479Sjb	}
963178479Sjb
964178479Sjb	dt_proc_lock(dtp, P);
965178479Sjb
966178556Sjb#if defined(sun)
967178479Sjb	if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
968178479Sjb		(void) Pobjname(P, addr, objname, sizeof (objname));
969178556Sjb#else
970178556Sjb	if (proc_addr2sym(P, addr, name, sizeof (name), &sym) == 0) {
971178556Sjb		(void) proc_objname(P, addr, objname, sizeof (objname));
972178556Sjb#endif
973178479Sjb
974178479Sjb		obj = dt_basename(objname);
975178479Sjb
976178479Sjb		if (addr > sym.st_value) {
977178479Sjb			(void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
978178479Sjb			    name, (u_longlong_t)(addr - sym.st_value));
979178479Sjb		} else {
980178479Sjb			(void) snprintf(c, sizeof (c), "%s`%s", obj, name);
981178479Sjb		}
982178556Sjb#if defined(sun)
983178556Sjb	} else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) {
984178556Sjb#else
985178556Sjb	} else if (proc_objname(P, addr, objname, sizeof (objname)) != 0) {
986178556Sjb#endif
987178479Sjb		(void) snprintf(c, sizeof (c), "%s`0x%llx",
988178479Sjb		    dt_basename(objname), addr);
989178479Sjb	} else {
990178479Sjb		(void) snprintf(c, sizeof (c), "0x%llx", addr);
991178479Sjb	}
992178479Sjb
993178479Sjb	dt_proc_unlock(dtp, P);
994178479Sjb	dt_proc_release(dtp, P);
995178479Sjb
996178479Sjb	return (dt_string2str(c, str, nbytes));
997178479Sjb}
998