xref: /illumos-gate/usr/src/lib/libdtrace/common/dt_program.c (revision 2b6e762c557496a41438c0b105d604f60c593682)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <unistd.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <alloca.h>
36 
37 #include <dt_impl.h>
38 #include <dt_program.h>
39 #include <dt_printf.h>
40 #include <dt_provider.h>
41 
42 dtrace_prog_t *
43 dt_program_create(dtrace_hdl_t *dtp)
44 {
45 	dtrace_prog_t *pgp = dt_zalloc(dtp, sizeof (dtrace_prog_t));
46 
47 	if (pgp != NULL)
48 		dt_list_append(&dtp->dt_programs, pgp);
49 	else
50 		(void) dt_set_errno(dtp, EDT_NOMEM);
51 
52 	/*
53 	 * By default, programs start with DOF version 1 so that output files
54 	 * containing DOF are backward compatible. If a program requires new
55 	 * DOF features, the version is increased as needed.
56 	 */
57 	pgp->dp_dofversion = DOF_VERSION_1;
58 
59 	return (pgp);
60 }
61 
62 void
63 dt_program_destroy(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
64 {
65 	dt_stmt_t *stp, *next;
66 	uint_t i;
67 
68 	for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = next) {
69 		next = dt_list_next(stp);
70 		dtrace_stmt_destroy(dtp, stp->ds_desc);
71 		dt_free(dtp, stp);
72 	}
73 
74 	for (i = 0; i < pgp->dp_xrefslen; i++)
75 		dt_free(dtp, pgp->dp_xrefs[i]);
76 
77 	dt_free(dtp, pgp->dp_xrefs);
78 	dt_list_delete(&dtp->dt_programs, pgp);
79 	dt_free(dtp, pgp);
80 }
81 
82 /*ARGSUSED*/
83 void
84 dtrace_program_info(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
85     dtrace_proginfo_t *pip)
86 {
87 	dt_stmt_t *stp;
88 	dtrace_actdesc_t *ap;
89 	dtrace_ecbdesc_t *last = NULL;
90 
91 	if (pip == NULL)
92 		return;
93 
94 	bzero(pip, sizeof (dtrace_proginfo_t));
95 
96 	if (dt_list_next(&pgp->dp_stmts) != NULL) {
97 		pip->dpi_descattr = _dtrace_maxattr;
98 		pip->dpi_stmtattr = _dtrace_maxattr;
99 	} else {
100 		pip->dpi_descattr = _dtrace_defattr;
101 		pip->dpi_stmtattr = _dtrace_defattr;
102 	}
103 
104 	for (stp = dt_list_next(&pgp->dp_stmts); stp; stp = dt_list_next(stp)) {
105 		dtrace_ecbdesc_t *edp = stp->ds_desc->dtsd_ecbdesc;
106 
107 		if (edp == last)
108 			continue;
109 		last = edp;
110 
111 		pip->dpi_descattr =
112 		    dt_attr_min(stp->ds_desc->dtsd_descattr, pip->dpi_descattr);
113 
114 		pip->dpi_stmtattr =
115 		    dt_attr_min(stp->ds_desc->dtsd_stmtattr, pip->dpi_stmtattr);
116 
117 		/*
118 		 * If there aren't any actions, account for the fact that
119 		 * recording the epid will generate a record.
120 		 */
121 		if (edp->dted_action == NULL)
122 			pip->dpi_recgens++;
123 
124 		for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
125 			if (ap->dtad_kind == DTRACEACT_SPECULATE) {
126 				pip->dpi_speculations++;
127 				continue;
128 			}
129 
130 			if (DTRACEACT_ISAGG(ap->dtad_kind)) {
131 				pip->dpi_recgens -= ap->dtad_arg;
132 				pip->dpi_aggregates++;
133 				continue;
134 			}
135 
136 			if (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind))
137 				continue;
138 
139 			if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
140 			    ap->dtad_difo->dtdo_rtype.dtdt_kind ==
141 			    DIF_TYPE_CTF &&
142 			    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
143 				continue;
144 
145 			pip->dpi_recgens++;
146 		}
147 	}
148 }
149 
150 int
151 dtrace_program_exec(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
152     dtrace_proginfo_t *pip)
153 {
154 	void *dof;
155 	int n, err;
156 
157 	dtrace_program_info(dtp, pgp, pip);
158 
159 	if ((dof = dtrace_dof_create(dtp, pgp, DTRACE_D_STRIP)) == NULL)
160 		return (-1);
161 
162 	n = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof);
163 	dtrace_dof_destroy(dtp, dof);
164 
165 	if (n == -1) {
166 		switch (errno) {
167 		case EINVAL:
168 			err = EDT_DIFINVAL;
169 			break;
170 		case EFAULT:
171 			err = EDT_DIFFAULT;
172 			break;
173 		case E2BIG:
174 			err = EDT_DIFSIZE;
175 			break;
176 		default:
177 			err = errno;
178 		}
179 
180 		return (dt_set_errno(dtp, err));
181 	}
182 
183 	if (pip != NULL)
184 		pip->dpi_matches += n;
185 
186 	return (0);
187 }
188 
189 static void
190 dt_ecbdesc_hold(dtrace_ecbdesc_t *edp)
191 {
192 	edp->dted_refcnt++;
193 }
194 
195 void
196 dt_ecbdesc_release(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp)
197 {
198 	if (--edp->dted_refcnt > 0)
199 		return;
200 
201 	dt_difo_free(dtp, edp->dted_pred.dtpdd_difo);
202 	assert(edp->dted_action == NULL);
203 	dt_free(dtp, edp);
204 }
205 
206 dtrace_ecbdesc_t *
207 dt_ecbdesc_create(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp)
208 {
209 	dtrace_ecbdesc_t *edp;
210 
211 	if ((edp = dt_zalloc(dtp, sizeof (dtrace_ecbdesc_t))) == NULL) {
212 		(void) dt_set_errno(dtp, EDT_NOMEM);
213 		return (NULL);
214 	}
215 
216 	edp->dted_probe = *pdp;
217 	dt_ecbdesc_hold(edp);
218 	return (edp);
219 }
220 
221 dtrace_stmtdesc_t *
222 dtrace_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp)
223 {
224 	dtrace_stmtdesc_t *sdp;
225 
226 	if ((sdp = dt_zalloc(dtp, sizeof (dtrace_stmtdesc_t))) == NULL)
227 		return (NULL);
228 
229 	dt_ecbdesc_hold(edp);
230 	sdp->dtsd_ecbdesc = edp;
231 	sdp->dtsd_descattr = _dtrace_defattr;
232 	sdp->dtsd_stmtattr = _dtrace_defattr;
233 
234 	return (sdp);
235 }
236 
237 dtrace_actdesc_t *
238 dtrace_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
239 {
240 	dtrace_actdesc_t *new;
241 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
242 
243 	if ((new = dt_alloc(dtp, sizeof (dtrace_actdesc_t))) == NULL)
244 		return (NULL);
245 
246 	if (sdp->dtsd_action_last != NULL) {
247 		assert(sdp->dtsd_action != NULL);
248 		assert(sdp->dtsd_action_last->dtad_next == NULL);
249 		sdp->dtsd_action_last->dtad_next = new;
250 	} else {
251 		dtrace_actdesc_t *ap = edp->dted_action;
252 
253 		assert(sdp->dtsd_action == NULL);
254 		sdp->dtsd_action = new;
255 
256 		while (ap != NULL && ap->dtad_next != NULL)
257 			ap = ap->dtad_next;
258 
259 		if (ap == NULL)
260 			edp->dted_action = new;
261 		else
262 			ap->dtad_next = new;
263 	}
264 
265 	sdp->dtsd_action_last = new;
266 	bzero(new, sizeof (dtrace_actdesc_t));
267 	new->dtad_uarg = (uintptr_t)sdp;
268 
269 	return (new);
270 }
271 
272 int
273 dtrace_stmt_add(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, dtrace_stmtdesc_t *sdp)
274 {
275 	dt_stmt_t *stp = dt_alloc(dtp, sizeof (dt_stmt_t));
276 
277 	if (stp == NULL)
278 		return (-1); /* errno is set for us */
279 
280 	dt_list_append(&pgp->dp_stmts, stp);
281 	stp->ds_desc = sdp;
282 
283 	return (0);
284 }
285 
286 int
287 dtrace_stmt_iter(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
288     dtrace_stmt_f *func, void *data)
289 {
290 	dt_stmt_t *stp, *next;
291 	int status = 0;
292 
293 	for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = next) {
294 		next = dt_list_next(stp);
295 		if ((status = func(dtp, pgp, stp->ds_desc, data)) != 0)
296 			break;
297 	}
298 
299 	return (status);
300 }
301 
302 void
303 dtrace_stmt_destroy(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
304 {
305 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
306 
307 	/*
308 	 * We need to remove any actions that we have on this ECB, and
309 	 * remove our hold on the ECB itself.
310 	 */
311 	if (sdp->dtsd_action != NULL) {
312 		dtrace_actdesc_t *last = sdp->dtsd_action_last;
313 		dtrace_actdesc_t *ap, *next;
314 
315 		assert(last != NULL);
316 
317 		for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
318 			if (ap == sdp->dtsd_action)
319 				break;
320 
321 			if (ap->dtad_next == sdp->dtsd_action)
322 				break;
323 		}
324 
325 		assert(ap != NULL);
326 
327 		if (ap == edp->dted_action)
328 			edp->dted_action = last->dtad_next;
329 		else
330 			ap->dtad_next = last->dtad_next;
331 
332 		/*
333 		 * We have now removed our action list from its ECB; we can
334 		 * safely destroy the list.
335 		 */
336 		last->dtad_next = NULL;
337 
338 		for (ap = sdp->dtsd_action; ap != NULL; ap = next) {
339 			assert(ap->dtad_uarg == (uintptr_t)sdp);
340 			dt_difo_free(dtp, ap->dtad_difo);
341 			next = ap->dtad_next;
342 			dt_free(dtp, ap);
343 		}
344 	}
345 
346 	if (sdp->dtsd_fmtdata != NULL)
347 		dt_printf_destroy(sdp->dtsd_fmtdata);
348 
349 	dt_ecbdesc_release(dtp, sdp->dtsd_ecbdesc);
350 	dt_free(dtp, sdp);
351 }
352 
353 typedef struct dt_header_info {
354 	dtrace_hdl_t *dthi_dtp;	/* consumer handle */
355 	FILE *dthi_out;		/* output file */
356 	char *dthi_pmname;	/* provider macro name */
357 	char *dthi_pfname;	/* provider function name */
358 	int dthi_empty;		/* should we generate empty macros */
359 } dt_header_info_t;
360 
361 static void
362 dt_header_fmt_macro(char *buf, const char *str)
363 {
364 	for (;;) {
365 		if (islower(*str)) {
366 			*buf++ = *str++ + 'A' - 'a';
367 		} else if (*str == '-') {
368 			*buf++ = '_';
369 			str++;
370 		} else if (*str == '.') {
371 			*buf++ = '_';
372 			str++;
373 		} else if ((*buf++ = *str++) == '\0') {
374 			break;
375 		}
376 	}
377 }
378 
379 static void
380 dt_header_fmt_func(char *buf, const char *str)
381 {
382 	for (;;) {
383 		if (*str == '-') {
384 			*buf++ = '_';
385 			*buf++ = '_';
386 			str++;
387 		} else if ((*buf++ = *str++) == '\0') {
388 			break;
389 		}
390 	}
391 }
392 
393 /*ARGSUSED*/
394 static int
395 dt_header_decl(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
396 {
397 	dt_header_info_t *infop = data;
398 	dtrace_hdl_t *dtp = infop->dthi_dtp;
399 	dt_probe_t *prp = idp->di_data;
400 	dt_node_t *dnp;
401 	char buf[DT_TYPE_NAMELEN];
402 	char *fname;
403 	const char *p;
404 	int i;
405 
406 	p = prp->pr_name;
407 	for (i = 0; (p = strchr(p, '-')) != NULL; i++)
408 		p++;
409 
410 	fname = alloca(strlen(prp->pr_name) + 1 + i);
411 	dt_header_fmt_func(fname, prp->pr_name);
412 
413 	if (fprintf(infop->dthi_out, "extern void __dtrace_%s___%s(",
414 	    infop->dthi_pfname, fname) < 0)
415 		return (dt_set_errno(dtp, errno));
416 
417 	for (dnp = prp->pr_nargs, i = 0; dnp != NULL; dnp = dnp->dn_list, i++) {
418 		if (fprintf(infop->dthi_out, "%s",
419 		    ctf_type_name(dnp->dn_ctfp, dnp->dn_type,
420 		    buf, sizeof (buf))) < 0)
421 			return (dt_set_errno(dtp, errno));
422 
423 		if (i + 1 != prp->pr_nargc &&
424 		    fprintf(infop->dthi_out, ", ") < 0)
425 			return (dt_set_errno(dtp, errno));
426 	}
427 
428 	if (i == 0 && fprintf(infop->dthi_out, "void") < 0)
429 		return (dt_set_errno(dtp, errno));
430 
431 	if (fprintf(infop->dthi_out, ");\n") < 0)
432 		return (dt_set_errno(dtp, errno));
433 
434 	if (fprintf(infop->dthi_out, "extern int "
435 	    "__dtraceenabled_%s___%s(void);\n", infop->dthi_pfname, fname) < 0)
436 		return (dt_set_errno(dtp, errno));
437 
438 	return (0);
439 }
440 
441 /*ARGSUSED*/
442 static int
443 dt_header_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
444 {
445 	dt_header_info_t *infop = data;
446 	dtrace_hdl_t *dtp = infop->dthi_dtp;
447 	dt_probe_t *prp = idp->di_data;
448 	char *mname, *fname;
449 	const char *p;
450 	int i;
451 
452 	p = prp->pr_name;
453 	for (i = 0; (p = strchr(p, '-')) != NULL; i++)
454 		p++;
455 
456 	mname = alloca(strlen(prp->pr_name) + 1);
457 	dt_header_fmt_macro(mname, prp->pr_name);
458 
459 	fname = alloca(strlen(prp->pr_name) + 1 + i);
460 	dt_header_fmt_func(fname, prp->pr_name);
461 
462 	if (fprintf(infop->dthi_out, "#define\t%s_%s(",
463 	    infop->dthi_pmname, mname) < 0)
464 		return (dt_set_errno(dtp, errno));
465 
466 	for (i = 0; i < prp->pr_nargc; i++) {
467 		if (fprintf(infop->dthi_out, "arg%d", i) < 0)
468 			return (dt_set_errno(dtp, errno));
469 
470 		if (i + 1 != prp->pr_nargc &&
471 		    fprintf(infop->dthi_out, ", ") < 0)
472 			return (dt_set_errno(dtp, errno));
473 	}
474 
475 	if (!infop->dthi_empty) {
476 		if (fprintf(infop->dthi_out, ") \\\n\t") < 0)
477 			return (dt_set_errno(dtp, errno));
478 
479 		if (fprintf(infop->dthi_out, "__dtrace_%s___%s(",
480 		    infop->dthi_pfname, fname) < 0)
481 			return (dt_set_errno(dtp, errno));
482 
483 		for (i = 0; i < prp->pr_nargc; i++) {
484 			if (fprintf(infop->dthi_out, "arg%d", i) < 0)
485 				return (dt_set_errno(dtp, errno));
486 
487 			if (i + 1 != prp->pr_nargc &&
488 			    fprintf(infop->dthi_out, ", ") < 0)
489 				return (dt_set_errno(dtp, errno));
490 		}
491 	}
492 
493 	if (fprintf(infop->dthi_out, ")\n") < 0)
494 		return (dt_set_errno(dtp, errno));
495 
496 	if (!infop->dthi_empty) {
497 		if (fprintf(infop->dthi_out, "#define\t%s_%s_ENABLED() \\\n",
498 		    infop->dthi_pmname, mname) < 0)
499 			return (dt_set_errno(dtp, errno));
500 
501 		if (fprintf(infop->dthi_out, "\t__dtraceenabled_%s___%s()\n",
502 		    infop->dthi_pfname, fname) < 0)
503 			return (dt_set_errno(dtp, errno));
504 	} else {
505 		if (fprintf(infop->dthi_out, "#define\t%s_%s_ENABLED() (0)\n",
506 		    infop->dthi_pmname, mname) < 0)
507 			return (dt_set_errno(dtp, errno));
508 	}
509 
510 	return (0);
511 }
512 
513 static int
514 dt_header_provider(dtrace_hdl_t *dtp, dt_provider_t *pvp, FILE *out)
515 {
516 	dt_header_info_t info;
517 	const char *p;
518 	int i;
519 
520 	if (pvp->pv_flags & DT_PROVIDER_IMPL)
521 		return (0);
522 
523 	/*
524 	 * Count the instances of the '-' character since we'll need to double
525 	 * those up.
526 	 */
527 	p = pvp->pv_desc.dtvd_name;
528 	for (i = 0; (p = strchr(p, '-')) != NULL; i++)
529 		p++;
530 
531 	info.dthi_dtp = dtp;
532 	info.dthi_out = out;
533 	info.dthi_empty = 0;
534 
535 	info.dthi_pmname = alloca(strlen(pvp->pv_desc.dtvd_name) + 1);
536 	dt_header_fmt_macro(info.dthi_pmname, pvp->pv_desc.dtvd_name);
537 
538 	info.dthi_pfname = alloca(strlen(pvp->pv_desc.dtvd_name) + 1 + i);
539 	dt_header_fmt_func(info.dthi_pfname, pvp->pv_desc.dtvd_name);
540 
541 	if (fprintf(out, "#if _DTRACE_VERSION\n\n") < 0)
542 		return (dt_set_errno(dtp, errno));
543 
544 	if (dt_idhash_iter(pvp->pv_probes, dt_header_probe, &info) != 0)
545 		return (-1); /* dt_errno is set for us */
546 	if (fprintf(out, "\n\n") < 0)
547 		return (dt_set_errno(dtp, errno));
548 	if (dt_idhash_iter(pvp->pv_probes, dt_header_decl, &info) != 0)
549 		return (-1); /* dt_errno is set for us */
550 
551 	if (fprintf(out, "\n#else\n\n") < 0)
552 		return (dt_set_errno(dtp, errno));
553 
554 	info.dthi_empty = 1;
555 
556 	if (dt_idhash_iter(pvp->pv_probes, dt_header_probe, &info) != 0)
557 		return (-1); /* dt_errno is set for us */
558 
559 	if (fprintf(out, "\n#endif\n\n") < 0)
560 		return (dt_set_errno(dtp, errno));
561 
562 	return (0);
563 }
564 
565 int
566 dtrace_program_header(dtrace_hdl_t *dtp, FILE *out, const char *fname)
567 {
568 	dt_provider_t *pvp;
569 	char *mfname, *p;
570 
571 	if (fname != NULL) {
572 		if ((p = strrchr(fname, '/')) != NULL)
573 			fname = p + 1;
574 
575 		mfname = alloca(strlen(fname) + 1);
576 		dt_header_fmt_macro(mfname, fname);
577 		if (fprintf(out, "#ifndef\t_%s\n#define\t_%s\n\n",
578 		    mfname, mfname) < 0)
579 			return (dt_set_errno(dtp, errno));
580 	}
581 
582 	if (fprintf(out, "#include <unistd.h>\n\n") < 0)
583 		return (-1);
584 
585 	if (fprintf(out, "#ifdef\t__cplusplus\nextern \"C\" {\n#endif\n\n") < 0)
586 		return (-1);
587 
588 	for (pvp = dt_list_next(&dtp->dt_provlist);
589 	    pvp != NULL; pvp = dt_list_next(pvp)) {
590 		if (dt_header_provider(dtp, pvp, out) != 0)
591 			return (-1); /* dt_errno is set for us */
592 	}
593 
594 	if (fprintf(out, "\n#ifdef\t__cplusplus\n}\n#endif\n") < 0)
595 		return (dt_set_errno(dtp, errno));
596 
597 	if (fname != NULL && fprintf(out, "\n#endif\t/* _%s */\n", mfname) < 0)
598 		return (dt_set_errno(dtp, errno));
599 
600 	return (0);
601 }
602