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