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