xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_subr.c (revision 9a30c8d347bf9aaa89277b6e5a275f737be8edce)
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 
55 static 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 
65 int
dtrace_xstr2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,int argc,char * const argv[],dtrace_probedesc_t * pdp)66 dtrace_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 
162 int
dtrace_str2desc(dtrace_hdl_t * dtp,dtrace_probespec_t spec,const char * s,dtrace_probedesc_t * pdp)163 dtrace_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 
169 int
dtrace_id2desc(dtrace_hdl_t * dtp,dtrace_id_t id,dtrace_probedesc_t * pdp)170 dtrace_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 
182 char *
dtrace_desc2str(const dtrace_probedesc_t * pdp,char * buf,size_t len)183 dtrace_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 
194 char *
dtrace_attr2str(dtrace_attribute_t attr,char * buf,size_t len)195 dtrace_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 
208 static char *
dt_getstrattr(char * p,char ** qp)209 dt_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 
225 int
dtrace_str2attr(const char * str,dtrace_attribute_t * attr)226 dtrace_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 
281 const char *
dtrace_stability_name(dtrace_stability_t s)282 dtrace_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 
297 const char *
dtrace_class_name(dtrace_class_t c)298 dtrace_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 
311 dtrace_attribute_t
dt_attr_min(dtrace_attribute_t a1,dtrace_attribute_t a2)312 dt_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 
323 dtrace_attribute_t
dt_attr_max(dtrace_attribute_t a1,dtrace_attribute_t a2)324 dt_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  */
348 int
dt_attr_cmp(dtrace_attribute_t a1,dtrace_attribute_t a2)349 dt_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 
356 char *
dt_attr_str(dtrace_attribute_t a,char * buf,size_t len)357 dt_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 
374 char *
dt_version_num2str(dt_version_t v,char * buf,size_t len)375 dt_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 
389 int
dt_version_str2num(const char * s,dt_version_t * vp)390 dt_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 
413 int
dt_version_defined(dt_version_t v)414 dt_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 
426 char *
dt_cpp_add_arg(dtrace_hdl_t * dtp,const char * str)427 dt_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 
452 char *
dt_cpp_pop_arg(dtrace_hdl_t * dtp)453 dt_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 int
dt_cpu_maxid(dtrace_hdl_t * dtp)467 dt_cpu_maxid(dtrace_hdl_t *dtp)
468 {
469 	size_t len;
470 	u_int count;
471 	int error;
472 
473 	len = sizeof(count);
474 	error = sysctlbyname("kern.smp.maxid", &count, &len, NULL, 0);
475 	if (error != 0)
476 		return (dt_set_errno(dtp, errno));
477 	return (count);
478 }
479 
480 /*PRINTFLIKE1*/
481 void
dt_dprintf(const char * format,...)482 dt_dprintf(const char *format, ...)
483 {
484 	if (_dtrace_debug) {
485 		va_list alist;
486 
487 		va_start(alist, format);
488 		(void) fputs("libdtrace DEBUG: ", stderr);
489 		(void) vfprintf(stderr, format, alist);
490 		va_end(alist);
491 	}
492 }
493 
494 int
495 #ifdef illumos
dt_ioctl(dtrace_hdl_t * dtp,int val,void * arg)496 dt_ioctl(dtrace_hdl_t *dtp, int val, void *arg)
497 #else
498 dt_ioctl(dtrace_hdl_t *dtp, u_long val, void *arg)
499 #endif
500 {
501 	const dtrace_vector_t *v = dtp->dt_vector;
502 
503 #ifndef illumos
504 	/* Avoid sign extension. */
505 	val &= 0xffffffff;
506 #endif
507 
508 	if (v != NULL)
509 		return (v->dtv_ioctl(dtp->dt_varg, val, arg));
510 
511 	if (dtp->dt_fd >= 0)
512 		return (ioctl(dtp->dt_fd, val, arg));
513 
514 	errno = EBADF;
515 	return (-1);
516 }
517 
518 int
dt_status(dtrace_hdl_t * dtp,processorid_t cpu)519 dt_status(dtrace_hdl_t *dtp, processorid_t cpu)
520 {
521 	const dtrace_vector_t *v = dtp->dt_vector;
522 
523 	if (v == NULL) {
524 #ifdef illumos
525 		return (p_online(cpu, P_STATUS));
526 #else
527 		int maxid = 0;
528 		size_t len = sizeof(maxid);
529 		if (sysctlbyname("kern.smp.maxid", &maxid, &len, NULL, 0) != 0)
530 			return (cpu == 0 ? 1 : -1);
531 		else
532 			return (cpu <= maxid ? 1 : -1);
533 #endif
534 	}
535 
536 	return (v->dtv_status(dtp->dt_varg, cpu));
537 }
538 
539 long
dt_sysconf(dtrace_hdl_t * dtp,int name)540 dt_sysconf(dtrace_hdl_t *dtp, int name)
541 {
542 	const dtrace_vector_t *v = dtp->dt_vector;
543 
544 	if (v == NULL)
545 		return (sysconf(name));
546 
547 	return (v->dtv_sysconf(dtp->dt_varg, name));
548 }
549 
550 /*
551  * Wrapper around write(2) to handle partial writes.  For maximum safety of
552  * output files and proper error reporting, we continuing writing in the
553  * face of partial writes until write(2) fails or 'buf' is completely written.
554  * We also record any errno in the specified dtrace_hdl_t as well as 'errno'.
555  */
556 ssize_t
dt_write(dtrace_hdl_t * dtp,int fd,const void * buf,size_t n)557 dt_write(dtrace_hdl_t *dtp, int fd, const void *buf, size_t n)
558 {
559 	ssize_t resid = n;
560 	ssize_t len;
561 
562 	while (resid != 0) {
563 		if ((len = write(fd, buf, resid)) <= 0)
564 			break;
565 
566 		resid -= len;
567 		buf = (char *)buf + len;
568 	}
569 
570 	if (resid == n && n != 0)
571 		return (dt_set_errno(dtp, errno));
572 
573 	return (n - resid);
574 }
575 
576 /*
577  * This function handles all output from libdtrace, as well as the
578  * dtrace_sprintf() case.  If we're here due to dtrace_sprintf(), then
579  * dt_sprintf_buflen will be non-zero; in this case, we sprintf into the
580  * specified buffer and return.  Otherwise, if output is buffered (denoted by
581  * a NULL fp), we sprintf the desired output into the buffered buffer
582  * (expanding the buffer if required).  If we don't satisfy either of these
583  * conditions (that is, if we are to actually generate output), then we call
584  * fprintf with the specified fp.  In this case, we need to deal with one of
585  * the more annoying peculiarities of libc's printf routines:  any failed
586  * write persistently sets an error flag inside the FILE causing every
587  * subsequent write to fail, but only the caller that initiated the error gets
588  * the errno.  Since libdtrace clients often intercept SIGINT, this case is
589  * particularly frustrating since we don't want the EINTR on one attempt to
590  * write to the output file to preclude later attempts to write.  This
591  * function therefore does a clearerr() if any error occurred, and saves the
592  * errno for the caller inside the specified dtrace_hdl_t.
593  */
594 /*PRINTFLIKE3*/
595 int
dt_printf(dtrace_hdl_t * dtp,FILE * fp,const char * format,...)596 dt_printf(dtrace_hdl_t *dtp, FILE *fp, const char *format, ...)
597 {
598 	va_list ap;
599 	va_list ap2;
600 	int n;
601 
602 #ifndef illumos
603 	/*
604 	 * On FreeBSD, check if output is currently being re-directed
605 	 * to another file. If so, output to that file instead of the
606 	 * one the caller has specified.
607 	 */
608 	if (dtp->dt_freopen_fp != NULL)
609 		fp = dtp->dt_freopen_fp;
610 #endif
611 
612 	va_start(ap, format);
613 
614 	if (dtp->dt_sprintf_buflen != 0) {
615 		int len;
616 		char *buf;
617 
618 		assert(dtp->dt_sprintf_buf != NULL);
619 
620 		buf = &dtp->dt_sprintf_buf[len = strlen(dtp->dt_sprintf_buf)];
621 		len = dtp->dt_sprintf_buflen - len;
622 		assert(len >= 0);
623 
624 		va_copy(ap2, ap);
625 		if ((n = vsnprintf(buf, len, format, ap2)) < 0)
626 			n = dt_set_errno(dtp, errno);
627 
628 		va_end(ap2);
629 		va_end(ap);
630 
631 		return (n);
632 	}
633 
634 	if (fp == NULL) {
635 		int needed, rval;
636 		size_t avail;
637 
638 		/*
639 		 * Using buffered output is not allowed if a handler has
640 		 * not been installed.
641 		 */
642 		if (dtp->dt_bufhdlr == NULL) {
643 			va_end(ap);
644 			return (dt_set_errno(dtp, EDT_NOBUFFERED));
645 		}
646 
647 		if (dtp->dt_buffered_buf == NULL) {
648 			assert(dtp->dt_buffered_size == 0);
649 			dtp->dt_buffered_size = 1;
650 			dtp->dt_buffered_buf = malloc(dtp->dt_buffered_size);
651 
652 			if (dtp->dt_buffered_buf == NULL) {
653 				va_end(ap);
654 				return (dt_set_errno(dtp, EDT_NOMEM));
655 			}
656 
657 			dtp->dt_buffered_offs = 0;
658 			dtp->dt_buffered_buf[0] = '\0';
659 		}
660 
661 		va_copy(ap2, ap);
662 		if ((needed = vsnprintf(NULL, 0, format, ap2)) < 0) {
663 			rval = dt_set_errno(dtp, errno);
664 			va_end(ap2);
665 			va_end(ap);
666 			return (rval);
667 		}
668 		va_end(ap2);
669 
670 		if (needed == 0) {
671 			va_end(ap);
672 			return (0);
673 		}
674 
675 		for (;;) {
676 			char *newbuf;
677 
678 			assert(dtp->dt_buffered_offs < dtp->dt_buffered_size);
679 			avail = dtp->dt_buffered_size - dtp->dt_buffered_offs;
680 
681 			if (needed + 1 < avail)
682 				break;
683 
684 			if ((newbuf = realloc(dtp->dt_buffered_buf,
685 			    dtp->dt_buffered_size << 1)) == NULL) {
686 				va_end(ap);
687 				return (dt_set_errno(dtp, EDT_NOMEM));
688 			}
689 
690 			dtp->dt_buffered_buf = newbuf;
691 			dtp->dt_buffered_size <<= 1;
692 		}
693 
694 		va_copy(ap2, ap);
695 		if (vsnprintf(&dtp->dt_buffered_buf[dtp->dt_buffered_offs],
696 		    avail, format, ap2) < 0) {
697 			rval = dt_set_errno(dtp, errno);
698 			va_end(ap2);
699 			va_end(ap);
700 			return (rval);
701 		}
702 		va_end(ap2);
703 
704 		dtp->dt_buffered_offs += needed;
705 		assert(dtp->dt_buffered_buf[dtp->dt_buffered_offs] == '\0');
706 		va_end(ap);
707 		return (0);
708 	}
709 
710 	va_copy(ap2, ap);
711 	n = vfprintf(fp, format, ap2);
712 	va_end(ap2);
713 	va_end(ap);
714 
715 	if (n < 0) {
716 		clearerr(fp);
717 		return (dt_set_errno(dtp, errno));
718 	}
719 
720 	return (n);
721 }
722 
723 int
dt_buffered_flush(dtrace_hdl_t * dtp,dtrace_probedata_t * pdata,const dtrace_recdesc_t * rec,const dtrace_aggdata_t * agg,uint32_t flags)724 dt_buffered_flush(dtrace_hdl_t *dtp, dtrace_probedata_t *pdata,
725     const dtrace_recdesc_t *rec, const dtrace_aggdata_t *agg, uint32_t flags)
726 {
727 	dtrace_bufdata_t data;
728 
729 	if (dtp->dt_buffered_offs == 0)
730 		return (0);
731 
732 	data.dtbda_handle = dtp;
733 	data.dtbda_buffered = dtp->dt_buffered_buf;
734 	data.dtbda_probe = pdata;
735 	data.dtbda_recdesc = rec;
736 	data.dtbda_aggdata = agg;
737 	data.dtbda_flags = flags;
738 
739 	if ((*dtp->dt_bufhdlr)(&data, dtp->dt_bufarg) == DTRACE_HANDLE_ABORT)
740 		return (dt_set_errno(dtp, EDT_DIRABORT));
741 
742 	dtp->dt_buffered_offs = 0;
743 	dtp->dt_buffered_buf[0] = '\0';
744 
745 	return (0);
746 }
747 
748 void
dt_buffered_destroy(dtrace_hdl_t * dtp)749 dt_buffered_destroy(dtrace_hdl_t *dtp)
750 {
751 	free(dtp->dt_buffered_buf);
752 	dtp->dt_buffered_buf = NULL;
753 	dtp->dt_buffered_offs = 0;
754 	dtp->dt_buffered_size = 0;
755 }
756 
757 void *
dt_zalloc(dtrace_hdl_t * dtp,size_t size)758 dt_zalloc(dtrace_hdl_t *dtp, size_t size)
759 {
760 	void *data;
761 
762 	if ((data = malloc(size)) == NULL)
763 		(void) dt_set_errno(dtp, EDT_NOMEM);
764 	else
765 		bzero(data, size);
766 
767 	return (data);
768 }
769 
770 void *
dt_alloc(dtrace_hdl_t * dtp,size_t size)771 dt_alloc(dtrace_hdl_t *dtp, size_t size)
772 {
773 	void *data;
774 
775 	if ((data = malloc(size)) == NULL)
776 		(void) dt_set_errno(dtp, EDT_NOMEM);
777 
778 	return (data);
779 }
780 
781 void
dt_free(dtrace_hdl_t * dtp,void * data)782 dt_free(dtrace_hdl_t *dtp, void *data)
783 {
784 	assert(dtp != NULL); /* ensure sane use of this interface */
785 	free(data);
786 }
787 
788 void
dt_difo_free(dtrace_hdl_t * dtp,dtrace_difo_t * dp)789 dt_difo_free(dtrace_hdl_t *dtp, dtrace_difo_t *dp)
790 {
791 	if (dp == NULL)
792 		return; /* simplify caller code */
793 
794 	dt_free(dtp, dp->dtdo_buf);
795 	dt_free(dtp, dp->dtdo_inttab);
796 	dt_free(dtp, dp->dtdo_strtab);
797 	dt_free(dtp, dp->dtdo_vartab);
798 	dt_free(dtp, dp->dtdo_kreltab);
799 	dt_free(dtp, dp->dtdo_ureltab);
800 	dt_free(dtp, dp->dtdo_xlmtab);
801 
802 	dt_free(dtp, dp);
803 }
804 
805 /*
806  * dt_gmatch() is similar to gmatch(3GEN) and dtrace(7D) globbing, but also
807  * implements the behavior that an empty pattern matches any string.
808  */
809 int
dt_gmatch(const char * s,const char * p)810 dt_gmatch(const char *s, const char *p)
811 {
812 	return (p == NULL || *p == '\0' || gmatch(s, p));
813 }
814 
815 char *
dt_basename(char * str)816 dt_basename(char *str)
817 {
818 	char *last = strrchr(str, '/');
819 
820 	if (last == NULL)
821 		return (str);
822 
823 	return (last + 1);
824 }
825 
826 /*
827  * dt_popc() is a fast implementation of population count.  The algorithm is
828  * from "Hacker's Delight" by Henry Warren, Jr with a 64-bit equivalent added.
829  */
830 ulong_t
dt_popc(ulong_t x)831 dt_popc(ulong_t x)
832 {
833 #if defined(_ILP32)
834 	x = x - ((x >> 1) & 0x55555555UL);
835 	x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
836 	x = (x + (x >> 4)) & 0x0F0F0F0FUL;
837 	x = x + (x >> 8);
838 	x = x + (x >> 16);
839 	return (x & 0x3F);
840 #elif defined(_LP64)
841 	x = x - ((x >> 1) & 0x5555555555555555ULL);
842 	x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
843 	x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
844 	x = x + (x >> 8);
845 	x = x + (x >> 16);
846 	x = x + (x >> 32);
847 	return (x & 0x7F);
848 #else
849 /* This should be a #warning but for now ignore error. Err: "need td_popc() implementation" */
850 #endif
851 }
852 
853 /*
854  * dt_popcb() is a bitmap-based version of population count that returns the
855  * number of one bits in the specified bitmap 'bp' at bit positions below 'n'.
856  */
857 ulong_t
dt_popcb(const ulong_t * bp,ulong_t n)858 dt_popcb(const ulong_t *bp, ulong_t n)
859 {
860 	ulong_t maxb = n & BT_ULMASK;
861 	ulong_t maxw = n >> BT_ULSHIFT;
862 	ulong_t w, popc = 0;
863 
864 	if (n == 0)
865 		return (0);
866 
867 	for (w = 0; w < maxw; w++)
868 		popc += dt_popc(bp[w]);
869 
870 	return (popc + dt_popc(bp[maxw] & ((1UL << maxb) - 1)));
871 }
872 
873 #ifdef illumos
874 struct _rwlock;
875 struct _lwp_mutex;
876 
877 int
dt_rw_read_held(pthread_rwlock_t * lock)878 dt_rw_read_held(pthread_rwlock_t *lock)
879 {
880 	extern int _rw_read_held(struct _rwlock *);
881 	return (_rw_read_held((struct _rwlock *)lock));
882 }
883 
884 int
dt_rw_write_held(pthread_rwlock_t * lock)885 dt_rw_write_held(pthread_rwlock_t *lock)
886 {
887 	extern int _rw_write_held(struct _rwlock *);
888 	return (_rw_write_held((struct _rwlock *)lock));
889 }
890 #endif
891 
892 int
dt_mutex_held(pthread_mutex_t * lock)893 dt_mutex_held(pthread_mutex_t *lock)
894 {
895 #ifdef illumos
896 	extern int _mutex_held(struct _lwp_mutex *);
897 	return (_mutex_held((struct _lwp_mutex *)lock));
898 #else
899 	return (1);
900 #endif
901 }
902 
903 static int
dt_string2str(char * s,char * str,int nbytes)904 dt_string2str(char *s, char *str, int nbytes)
905 {
906 	int len = strlen(s);
907 
908 	if (nbytes == 0) {
909 		/*
910 		 * Like snprintf(3C), we don't check the value of str if the
911 		 * number of bytes is 0.
912 		 */
913 		return (len);
914 	}
915 
916 	if (nbytes <= len) {
917 		(void) strncpy(str, s, nbytes - 1);
918 		/*
919 		 * Like snprintf(3C) (and unlike strncpy(3C)), we guarantee
920 		 * that the string is null-terminated.
921 		 */
922 		str[nbytes - 1] = '\0';
923 	} else {
924 		(void) strcpy(str, s);
925 	}
926 
927 	return (len);
928 }
929 
930 int
dtrace_addr2str(dtrace_hdl_t * dtp,uint64_t addr,char * str,int nbytes)931 dtrace_addr2str(dtrace_hdl_t *dtp, uint64_t addr, char *str, int nbytes)
932 {
933 	dtrace_syminfo_t dts;
934 	GElf_Sym sym;
935 
936 	size_t n = 20; /* for 0x%llx\0 */
937 	char *s;
938 	int err;
939 
940 	if ((err = dtrace_lookup_by_addr(dtp, addr, &sym, &dts)) == 0)
941 		n += strlen(dts.dts_object) + strlen(dts.dts_name) + 2; /* +` */
942 
943 	s = alloca(n);
944 
945 	if (err == 0 && addr != sym.st_value) {
946 		(void) snprintf(s, n, "%s`%s+0x%llx", dts.dts_object,
947 		    dts.dts_name, (u_longlong_t)addr - sym.st_value);
948 	} else if (err == 0) {
949 		(void) snprintf(s, n, "%s`%s",
950 		    dts.dts_object, dts.dts_name);
951 	} else {
952 		/*
953 		 * We'll repeat the lookup, but this time we'll specify a NULL
954 		 * GElf_Sym -- indicating that we're only interested in the
955 		 * containing module.
956 		 */
957 		if (dtrace_lookup_by_addr(dtp, addr, NULL, &dts) == 0) {
958 			(void) snprintf(s, n, "%s`0x%llx", dts.dts_object,
959 			    (u_longlong_t)addr);
960 		} else {
961 			(void) snprintf(s, n, "0x%llx", (u_longlong_t)addr);
962 		}
963 	}
964 
965 	return (dt_string2str(s, str, nbytes));
966 }
967 
968 int
dtrace_uaddr2str(dtrace_hdl_t * dtp,pid_t pid,uint64_t addr,char * str,int nbytes)969 dtrace_uaddr2str(dtrace_hdl_t *dtp, pid_t pid,
970     uint64_t addr, char *str, int nbytes)
971 {
972 	char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
973 	struct ps_prochandle *P = NULL;
974 	GElf_Sym sym;
975 	char *obj;
976 
977 	if (pid != 0)
978 		P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
979 
980 	if (P == NULL) {
981 	  (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr);
982 		return (dt_string2str(c, str, nbytes));
983 	}
984 
985 	dt_proc_lock(dtp, P);
986 
987 	if (Plookup_by_addr(P, addr, name, sizeof (name), &sym) == 0) {
988 		(void) Pobjname(P, addr, objname, sizeof (objname));
989 
990 		obj = dt_basename(objname);
991 
992 		if (addr > sym.st_value) {
993 			(void) snprintf(c, sizeof (c), "%s`%s+0x%llx", obj,
994 			    name, (u_longlong_t)(addr - sym.st_value));
995 		} else {
996 			(void) snprintf(c, sizeof (c), "%s`%s", obj, name);
997 		}
998 	} else if (Pobjname(P, addr, objname, sizeof (objname)) != 0) {
999 		(void) snprintf(c, sizeof (c), "%s`0x%jx",
1000 				dt_basename(objname), (uintmax_t)addr);
1001 	} else {
1002 	  (void) snprintf(c, sizeof (c), "0x%jx", (uintmax_t)addr);
1003 	}
1004 
1005 	dt_proc_unlock(dtp, P);
1006 	dt_proc_release(dtp, P);
1007 
1008 	return (dt_string2str(c, str, nbytes));
1009 }
1010 
1011 int
dtrace_oformat_configure(dtrace_hdl_t * dtp)1012 dtrace_oformat_configure(dtrace_hdl_t *dtp)
1013 {
1014 
1015 	dtp->dt_oformat = xo_get_style(NULL) == XO_STYLE_TEXT ?
1016 	    DTRACE_OFORMAT_TEXT :
1017 	    DTRACE_OFORMAT_STRUCTURED;
1018 	xo_set_flags(NULL, XOF_DTRT);
1019 	return (0);
1020 }
1021 
1022 int
dtrace_oformat(dtrace_hdl_t * dtp)1023 dtrace_oformat(dtrace_hdl_t *dtp)
1024 {
1025 
1026 	return (dtp->dt_oformat != DTRACE_OFORMAT_TEXT);
1027 }
1028 
1029 void
dtrace_set_outfp(const FILE * ofp)1030 dtrace_set_outfp(const FILE *ofp)
1031 {
1032 
1033 	xo_set_file((FILE *)ofp);
1034 }
1035 
1036 void
dtrace_oformat_setup(dtrace_hdl_t * dtp)1037 dtrace_oformat_setup(dtrace_hdl_t *dtp)
1038 {
1039 
1040 	xo_open_container("dtrace");
1041 	xo_open_list("probes");
1042 }
1043 
1044 void
dtrace_oformat_teardown(dtrace_hdl_t * dtp)1045 dtrace_oformat_teardown(dtrace_hdl_t *dtp)
1046 {
1047 
1048 	xo_close_list("probes");
1049 	xo_close_container("dtrace");
1050 }
1051