10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1710Sahl  * Common Development and Distribution License (the "License").
6*1710Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211399Sahl 
220Sstevel@tonic-gate /*
231399Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <assert.h>
341399Sahl #include <ctype.h>
351399Sahl #include <alloca.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <dt_impl.h>
38265Smws #include <dt_program.h>
390Sstevel@tonic-gate #include <dt_printf.h>
401399Sahl #include <dt_provider.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate dtrace_prog_t *
43265Smws dt_program_create(dtrace_hdl_t *dtp)
440Sstevel@tonic-gate {
45265Smws 	dtrace_prog_t *pgp = dt_zalloc(dtp, sizeof (dtrace_prog_t));
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	if (pgp != NULL)
480Sstevel@tonic-gate 		dt_list_append(&dtp->dt_programs, pgp);
490Sstevel@tonic-gate 	else
500Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
510Sstevel@tonic-gate 
52*1710Sahl 	/*
53*1710Sahl 	 * By default, programs start with DOF version 1 so that output files
54*1710Sahl 	 * containing DOF are backward compatible. If a program requires new
55*1710Sahl 	 * DOF features, the version is increased as needed.
56*1710Sahl 	 */
57*1710Sahl 	pgp->dp_dofversion = DOF_VERSION_1;
58*1710Sahl 
590Sstevel@tonic-gate 	return (pgp);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate void
63265Smws dt_program_destroy(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	dt_stmt_t *stp, *next;
66265Smws 	uint_t i;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = next) {
690Sstevel@tonic-gate 		next = dt_list_next(stp);
70265Smws 		dtrace_stmt_destroy(dtp, stp->ds_desc);
71265Smws 		dt_free(dtp, stp);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 
74265Smws 	for (i = 0; i < pgp->dp_xrefslen; i++)
75265Smws 		dt_free(dtp, pgp->dp_xrefs[i]);
76265Smws 
77265Smws 	dt_free(dtp, pgp->dp_xrefs);
780Sstevel@tonic-gate 	dt_list_delete(&dtp->dt_programs, pgp);
79265Smws 	dt_free(dtp, pgp);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*ARGSUSED*/
830Sstevel@tonic-gate void
840Sstevel@tonic-gate dtrace_program_info(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
850Sstevel@tonic-gate     dtrace_proginfo_t *pip)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	dt_stmt_t *stp;
880Sstevel@tonic-gate 	dtrace_actdesc_t *ap;
890Sstevel@tonic-gate 	dtrace_ecbdesc_t *last = NULL;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (pip == NULL)
920Sstevel@tonic-gate 		return;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	bzero(pip, sizeof (dtrace_proginfo_t));
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (dt_list_next(&pgp->dp_stmts) != NULL) {
970Sstevel@tonic-gate 		pip->dpi_descattr = _dtrace_maxattr;
980Sstevel@tonic-gate 		pip->dpi_stmtattr = _dtrace_maxattr;
990Sstevel@tonic-gate 	} else {
1000Sstevel@tonic-gate 		pip->dpi_descattr = _dtrace_defattr;
1010Sstevel@tonic-gate 		pip->dpi_stmtattr = _dtrace_defattr;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	for (stp = dt_list_next(&pgp->dp_stmts); stp; stp = dt_list_next(stp)) {
1050Sstevel@tonic-gate 		dtrace_ecbdesc_t *edp = stp->ds_desc->dtsd_ecbdesc;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		if (edp == last)
1080Sstevel@tonic-gate 			continue;
1090Sstevel@tonic-gate 		last = edp;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		pip->dpi_descattr =
1120Sstevel@tonic-gate 		    dt_attr_min(stp->ds_desc->dtsd_descattr, pip->dpi_descattr);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		pip->dpi_stmtattr =
1150Sstevel@tonic-gate 		    dt_attr_min(stp->ds_desc->dtsd_stmtattr, pip->dpi_stmtattr);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		/*
1180Sstevel@tonic-gate 		 * If there aren't any actions, account for the fact that
1190Sstevel@tonic-gate 		 * recording the epid will generate a record.
1200Sstevel@tonic-gate 		 */
1210Sstevel@tonic-gate 		if (edp->dted_action == NULL)
1220Sstevel@tonic-gate 			pip->dpi_recgens++;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
1250Sstevel@tonic-gate 			if (ap->dtad_kind == DTRACEACT_SPECULATE) {
1260Sstevel@tonic-gate 				pip->dpi_speculations++;
1270Sstevel@tonic-gate 				continue;
1280Sstevel@tonic-gate 			}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 			if (DTRACEACT_ISAGG(ap->dtad_kind)) {
1310Sstevel@tonic-gate 				pip->dpi_recgens -= ap->dtad_arg;
1320Sstevel@tonic-gate 				pip->dpi_aggregates++;
1330Sstevel@tonic-gate 				continue;
1340Sstevel@tonic-gate 			}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 			if (DTRACEACT_ISDESTRUCTIVE(ap->dtad_kind))
1370Sstevel@tonic-gate 				continue;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 			if (ap->dtad_kind == DTRACEACT_DIFEXPR &&
1400Sstevel@tonic-gate 			    ap->dtad_difo->dtdo_rtype.dtdt_kind ==
1410Sstevel@tonic-gate 			    DIF_TYPE_CTF &&
1420Sstevel@tonic-gate 			    ap->dtad_difo->dtdo_rtype.dtdt_size == 0)
1430Sstevel@tonic-gate 				continue;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 			pip->dpi_recgens++;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
1510Sstevel@tonic-gate dtrace_program_exec(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
1520Sstevel@tonic-gate     dtrace_proginfo_t *pip)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	void *dof;
1550Sstevel@tonic-gate 	int n, err;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	dtrace_program_info(dtp, pgp, pip);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if ((dof = dtrace_dof_create(dtp, pgp, DTRACE_D_STRIP)) == NULL)
1600Sstevel@tonic-gate 		return (-1);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	n = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof);
1630Sstevel@tonic-gate 	dtrace_dof_destroy(dtp, dof);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (n == -1) {
1660Sstevel@tonic-gate 		switch (errno) {
1670Sstevel@tonic-gate 		case EINVAL:
1680Sstevel@tonic-gate 			err = EDT_DIFINVAL;
1690Sstevel@tonic-gate 			break;
1700Sstevel@tonic-gate 		case EFAULT:
1710Sstevel@tonic-gate 			err = EDT_DIFFAULT;
1720Sstevel@tonic-gate 			break;
1730Sstevel@tonic-gate 		case E2BIG:
1740Sstevel@tonic-gate 			err = EDT_DIFSIZE;
1750Sstevel@tonic-gate 			break;
1760Sstevel@tonic-gate 		default:
1770Sstevel@tonic-gate 			err = errno;
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		return (dt_set_errno(dtp, err));
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (pip != NULL)
1840Sstevel@tonic-gate 		pip->dpi_matches += n;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	return (0);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
189265Smws static void
190265Smws dt_ecbdesc_hold(dtrace_ecbdesc_t *edp)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	edp->dted_refcnt++;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate void
196265Smws dt_ecbdesc_release(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	if (--edp->dted_refcnt > 0)
1990Sstevel@tonic-gate 		return;
2000Sstevel@tonic-gate 
201265Smws 	dt_difo_free(dtp, edp->dted_pred.dtpdd_difo);
2020Sstevel@tonic-gate 	assert(edp->dted_action == NULL);
203265Smws 	dt_free(dtp, edp);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate dtrace_ecbdesc_t *
207265Smws dt_ecbdesc_create(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp;
2100Sstevel@tonic-gate 
211265Smws 	if ((edp = dt_zalloc(dtp, sizeof (dtrace_ecbdesc_t))) == NULL) {
2120Sstevel@tonic-gate 		(void) dt_set_errno(dtp, EDT_NOMEM);
2130Sstevel@tonic-gate 		return (NULL);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	edp->dted_probe = *pdp;
217265Smws 	dt_ecbdesc_hold(edp);
2180Sstevel@tonic-gate 	return (edp);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate dtrace_stmtdesc_t *
2220Sstevel@tonic-gate dtrace_stmt_create(dtrace_hdl_t *dtp, dtrace_ecbdesc_t *edp)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	dtrace_stmtdesc_t *sdp;
2250Sstevel@tonic-gate 
226265Smws 	if ((sdp = dt_zalloc(dtp, sizeof (dtrace_stmtdesc_t))) == NULL)
2270Sstevel@tonic-gate 		return (NULL);
2280Sstevel@tonic-gate 
229265Smws 	dt_ecbdesc_hold(edp);
2300Sstevel@tonic-gate 	sdp->dtsd_ecbdesc = edp;
2310Sstevel@tonic-gate 	sdp->dtsd_descattr = _dtrace_defattr;
2320Sstevel@tonic-gate 	sdp->dtsd_stmtattr = _dtrace_defattr;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	return (sdp);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate dtrace_actdesc_t *
2380Sstevel@tonic-gate dtrace_stmt_action(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	dtrace_actdesc_t *new;
2410Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
2420Sstevel@tonic-gate 
243265Smws 	if ((new = dt_alloc(dtp, sizeof (dtrace_actdesc_t))) == NULL)
2440Sstevel@tonic-gate 		return (NULL);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (sdp->dtsd_action_last != NULL) {
2470Sstevel@tonic-gate 		assert(sdp->dtsd_action != NULL);
2480Sstevel@tonic-gate 		assert(sdp->dtsd_action_last->dtad_next == NULL);
2490Sstevel@tonic-gate 		sdp->dtsd_action_last->dtad_next = new;
2500Sstevel@tonic-gate 	} else {
2510Sstevel@tonic-gate 		dtrace_actdesc_t *ap = edp->dted_action;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		assert(sdp->dtsd_action == NULL);
2540Sstevel@tonic-gate 		sdp->dtsd_action = new;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		while (ap != NULL && ap->dtad_next != NULL)
2570Sstevel@tonic-gate 			ap = ap->dtad_next;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		if (ap == NULL)
2600Sstevel@tonic-gate 			edp->dted_action = new;
2610Sstevel@tonic-gate 		else
2620Sstevel@tonic-gate 			ap->dtad_next = new;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	sdp->dtsd_action_last = new;
2660Sstevel@tonic-gate 	bzero(new, sizeof (dtrace_actdesc_t));
2670Sstevel@tonic-gate 	new->dtad_uarg = (uintptr_t)sdp;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	return (new);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate int
2730Sstevel@tonic-gate dtrace_stmt_add(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, dtrace_stmtdesc_t *sdp)
2740Sstevel@tonic-gate {
275265Smws 	dt_stmt_t *stp = dt_alloc(dtp, sizeof (dt_stmt_t));
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (stp == NULL)
278265Smws 		return (-1); /* errno is set for us */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	dt_list_append(&pgp->dp_stmts, stp);
2810Sstevel@tonic-gate 	stp->ds_desc = sdp;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate int
2870Sstevel@tonic-gate dtrace_stmt_iter(dtrace_hdl_t *dtp, dtrace_prog_t *pgp,
2880Sstevel@tonic-gate     dtrace_stmt_f *func, void *data)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	dt_stmt_t *stp, *next;
2910Sstevel@tonic-gate 	int status = 0;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = next) {
2940Sstevel@tonic-gate 		next = dt_list_next(stp);
2950Sstevel@tonic-gate 		if ((status = func(dtp, pgp, stp->ds_desc, data)) != 0)
2960Sstevel@tonic-gate 			break;
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (status);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate void
303265Smws dtrace_stmt_destroy(dtrace_hdl_t *dtp, dtrace_stmtdesc_t *sdp)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	dtrace_ecbdesc_t *edp = sdp->dtsd_ecbdesc;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/*
3080Sstevel@tonic-gate 	 * We need to remove any actions that we have on this ECB, and
3090Sstevel@tonic-gate 	 * remove our hold on the ECB itself.
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	if (sdp->dtsd_action != NULL) {
3120Sstevel@tonic-gate 		dtrace_actdesc_t *last = sdp->dtsd_action_last;
3130Sstevel@tonic-gate 		dtrace_actdesc_t *ap, *next;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		assert(last != NULL);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		for (ap = edp->dted_action; ap != NULL; ap = ap->dtad_next) {
3180Sstevel@tonic-gate 			if (ap == sdp->dtsd_action)
3190Sstevel@tonic-gate 				break;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			if (ap->dtad_next == sdp->dtsd_action)
3220Sstevel@tonic-gate 				break;
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		assert(ap != NULL);
3260Sstevel@tonic-gate 
327265Smws 		if (ap == edp->dted_action)
3280Sstevel@tonic-gate 			edp->dted_action = last->dtad_next;
329265Smws 		else
3300Sstevel@tonic-gate 			ap->dtad_next = last->dtad_next;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		/*
3330Sstevel@tonic-gate 		 * We have now removed our action list from its ECB; we can
3340Sstevel@tonic-gate 		 * safely destroy the list.
3350Sstevel@tonic-gate 		 */
3360Sstevel@tonic-gate 		last->dtad_next = NULL;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 		for (ap = sdp->dtsd_action; ap != NULL; ap = next) {
3390Sstevel@tonic-gate 			assert(ap->dtad_uarg == (uintptr_t)sdp);
340265Smws 			dt_difo_free(dtp, ap->dtad_difo);
3410Sstevel@tonic-gate 			next = ap->dtad_next;
342265Smws 			dt_free(dtp, ap);
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (sdp->dtsd_fmtdata != NULL)
3470Sstevel@tonic-gate 		dt_printf_destroy(sdp->dtsd_fmtdata);
3480Sstevel@tonic-gate 
349265Smws 	dt_ecbdesc_release(dtp, sdp->dtsd_ecbdesc);
350265Smws 	dt_free(dtp, sdp);
3510Sstevel@tonic-gate }
3521399Sahl 
3531399Sahl typedef struct dt_header_info {
3541399Sahl 	dtrace_hdl_t *dthi_dtp;	/* consumer handle */
3551399Sahl 	FILE *dthi_out;		/* output file */
3561399Sahl 	char *dthi_pmname;	/* provider macro name */
3571399Sahl 	char *dthi_pfname;	/* provider function name */
3581399Sahl } dt_header_info_t;
3591399Sahl 
3601399Sahl 
3611399Sahl static void
3621399Sahl dt_header_fmt_macro(char *buf, const char *str)
3631399Sahl {
3641399Sahl 	for (;;) {
3651399Sahl 		if (islower(*str)) {
3661399Sahl 			*buf++ = *str++ + 'A' - 'a';
3671399Sahl 		} else if (*str == '-') {
3681399Sahl 			*buf++ = '_';
3691399Sahl 			str++;
3701399Sahl 		} else if (*str == '.') {
3711399Sahl 			*buf++ = '_';
3721399Sahl 			str++;
3731399Sahl 		} else if ((*buf++ = *str++) == '\0') {
3741399Sahl 			break;
3751399Sahl 		}
3761399Sahl 	}
3771399Sahl }
3781399Sahl 
3791399Sahl static void
3801399Sahl dt_header_fmt_func(char *buf, const char *str)
3811399Sahl {
3821399Sahl 	for (;;) {
3831399Sahl 		if (*str == '-') {
3841399Sahl 			*buf++ = '_';
3851399Sahl 			*buf++ = '_';
3861399Sahl 			str++;
3871399Sahl 		} else if ((*buf++ = *str++) == '\0') {
3881399Sahl 			break;
3891399Sahl 		}
3901399Sahl 	}
3911399Sahl }
3921399Sahl 
3931399Sahl /*ARGSUSED*/
3941399Sahl static int
3951399Sahl dt_header_decl(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
3961399Sahl {
3971399Sahl 	dt_header_info_t *infop = data;
3981399Sahl 	dtrace_hdl_t *dtp = infop->dthi_dtp;
3991399Sahl 	dt_probe_t *prp = idp->di_data;
4001399Sahl 	dt_node_t *dnp;
4011399Sahl 	char buf[DT_TYPE_NAMELEN];
4021399Sahl 	char *fname;
4031399Sahl 	const char *p;
4041399Sahl 	int i;
4051399Sahl 
4061399Sahl 	p = prp->pr_name;
4071399Sahl 	for (i = 0; (p = strchr(p, '-')) != NULL; i++)
4081399Sahl 		p++;
4091399Sahl 
4101399Sahl 	fname = alloca(strlen(prp->pr_name) + 1 + i);
4111399Sahl 	dt_header_fmt_func(fname, prp->pr_name);
4121399Sahl 
4131399Sahl 	if (fprintf(infop->dthi_out, "extern void __dtrace_%s___%s(",
4141399Sahl 	    infop->dthi_pfname, fname) < 0)
4151399Sahl 		return (dt_set_errno(dtp, errno));
4161399Sahl 
4171399Sahl 	for (dnp = prp->pr_nargs, i = 0; dnp != NULL; dnp = dnp->dn_list, i++) {
4181399Sahl 		if (fprintf(infop->dthi_out, "%s",
4191399Sahl 		    ctf_type_name(dnp->dn_ctfp, dnp->dn_type,
4201399Sahl 		    buf, sizeof (buf))) < 0)
4211399Sahl 			return (dt_set_errno(dtp, errno));
4221399Sahl 
4231399Sahl 		if (i + 1 != prp->pr_nargc &&
4241399Sahl 		    fprintf(infop->dthi_out, ", ") < 0)
4251399Sahl 			return (dt_set_errno(dtp, errno));
4261399Sahl 	}
4271399Sahl 
4281399Sahl 	if (i == 0 && fprintf(infop->dthi_out, "void") < 0)
4291399Sahl 		return (dt_set_errno(dtp, errno));
4301399Sahl 
4311399Sahl 	if (fprintf(infop->dthi_out, ");\n") < 0)
4321399Sahl 		return (dt_set_errno(dtp, errno));
4331399Sahl 
434*1710Sahl 	if (fprintf(infop->dthi_out, "extern int "
435*1710Sahl 	    "__dtraceenabled_%s___%s(void);\n", infop->dthi_pfname, fname) < 0)
436*1710Sahl 		return (dt_set_errno(dtp, errno));
437*1710Sahl 
4381399Sahl 	return (0);
4391399Sahl }
4401399Sahl 
4411399Sahl /*ARGSUSED*/
4421399Sahl static int
4431399Sahl dt_header_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
4441399Sahl {
4451399Sahl 	dt_header_info_t *infop = data;
4461399Sahl 	dtrace_hdl_t *dtp = infop->dthi_dtp;
4471399Sahl 	dt_probe_t *prp = idp->di_data;
4481399Sahl 	char *mname, *fname;
4491399Sahl 	const char *p;
4501399Sahl 	int i;
4511399Sahl 
4521399Sahl 	p = prp->pr_name;
4531399Sahl 	for (i = 0; (p = strchr(p, '-')) != NULL; i++)
4541399Sahl 		p++;
4551399Sahl 
4561399Sahl 	mname = alloca(strlen(prp->pr_name) + 1);
4571399Sahl 	dt_header_fmt_macro(mname, prp->pr_name);
4581399Sahl 
4591399Sahl 	fname = alloca(strlen(prp->pr_name) + 1 + i);
4601399Sahl 	dt_header_fmt_func(fname, prp->pr_name);
4611399Sahl 
4621399Sahl 	if (fprintf(infop->dthi_out, "#define\t%s_%s(",
4631399Sahl 	    infop->dthi_pmname, mname) < 0)
4641399Sahl 		return (dt_set_errno(dtp, errno));
4651399Sahl 
4661399Sahl 	for (i = 0; i < prp->pr_nargc; i++) {
4671399Sahl 		if (fprintf(infop->dthi_out, "arg%d", i) < 0)
4681399Sahl 			return (dt_set_errno(dtp, errno));
4691399Sahl 
4701399Sahl 		if (i + 1 != prp->pr_nargc &&
4711399Sahl 		    fprintf(infop->dthi_out, ", ") < 0)
4721399Sahl 			return (dt_set_errno(dtp, errno));
4731399Sahl 	}
4741399Sahl 
4751399Sahl 	if (fprintf(infop->dthi_out, ") \\\n\t") < 0)
4761399Sahl 		return (dt_set_errno(dtp, errno));
4771399Sahl 
4781399Sahl 	if (fprintf(infop->dthi_out, "__dtrace_%s___%s(",
4791399Sahl 	    infop->dthi_pfname, fname) < 0)
4801399Sahl 		return (dt_set_errno(dtp, errno));
4811399Sahl 
4821399Sahl 	for (i = 0; i < prp->pr_nargc; i++) {
4831399Sahl 		if (fprintf(infop->dthi_out, "arg%d", i) < 0)
4841399Sahl 			return (dt_set_errno(dtp, errno));
4851399Sahl 
4861399Sahl 		if (i + 1 != prp->pr_nargc &&
4871399Sahl 		    fprintf(infop->dthi_out, ", ") < 0)
4881399Sahl 			return (dt_set_errno(dtp, errno));
4891399Sahl 	}
4901399Sahl 
4911399Sahl 	if (fprintf(infop->dthi_out, ")\n") < 0)
4921399Sahl 		return (dt_set_errno(dtp, errno));
4931399Sahl 
494*1710Sahl 	if (fprintf(infop->dthi_out, "#define\t%s_%s_ENABLED() \\\n",
495*1710Sahl 	    infop->dthi_pmname, mname) < 0)
496*1710Sahl 		return (dt_set_errno(dtp, errno));
497*1710Sahl 
498*1710Sahl 	if (fprintf(infop->dthi_out, "\t__dtraceenabled_%s___%s()\n",
499*1710Sahl 	    infop->dthi_pfname, fname) < 0)
500*1710Sahl 		return (dt_set_errno(dtp, errno));
501*1710Sahl 
5021399Sahl 	return (0);
5031399Sahl }
5041399Sahl 
5051399Sahl static int
5061399Sahl dt_header_provider(dtrace_hdl_t *dtp, dt_provider_t *pvp, FILE *out)
5071399Sahl {
5081399Sahl 	dt_header_info_t info;
5091399Sahl 	const char *p;
5101399Sahl 	int i;
5111399Sahl 
5121399Sahl 	if (pvp->pv_flags & DT_PROVIDER_IMPL)
5131399Sahl 		return (0);
5141399Sahl 
5151399Sahl 	p = pvp->pv_desc.dtvd_name;
5161399Sahl 	for (i = 0; (p = strchr(p, '-')) != NULL; i++)
5171399Sahl 		p++;
5181399Sahl 
5191399Sahl 	info.dthi_dtp = dtp;
5201399Sahl 	info.dthi_out = out;
5211399Sahl 
5221399Sahl 	info.dthi_pmname = alloca(strlen(pvp->pv_desc.dtvd_name) + 1);
5231399Sahl 	dt_header_fmt_macro(info.dthi_pmname, pvp->pv_desc.dtvd_name);
5241399Sahl 
5251399Sahl 	info.dthi_pfname = alloca(strlen(pvp->pv_desc.dtvd_name) + 1 + i);
5261399Sahl 	dt_header_fmt_func(info.dthi_pfname, pvp->pv_desc.dtvd_name);
5271399Sahl 
5281399Sahl 
5291399Sahl 	if (dt_idhash_iter(pvp->pv_probes, dt_header_probe, &info) != 0)
5301399Sahl 		return (-1); /* dt_errno is set for us */
5311399Sahl 	if (fprintf(out, "\n\n") < 0)
5321399Sahl 		return (dt_set_errno(dtp, errno));
5331399Sahl 	if (dt_idhash_iter(pvp->pv_probes, dt_header_decl, &info) != 0)
5341399Sahl 		return (-1); /* dt_errno is set for us */
5351399Sahl 
5361399Sahl 	return (0);
5371399Sahl }
5381399Sahl 
5391399Sahl int
5401399Sahl dtrace_program_header(dtrace_hdl_t *dtp, FILE *out, const char *fname)
5411399Sahl {
5421399Sahl 	dt_provider_t *pvp;
5431399Sahl 	char *mfname, *p;
5441399Sahl 
5451399Sahl 	if (fname != NULL) {
5461399Sahl 		if ((p = strrchr(fname, '/')) != NULL)
5471399Sahl 			fname = p + 1;
5481399Sahl 
5491399Sahl 		mfname = alloca(strlen(fname) + 1);
5501399Sahl 		dt_header_fmt_macro(mfname, fname);
5511399Sahl 		if (fprintf(out, "#ifndef\t_%s\n#define\t_%s\n\n",
5521399Sahl 		    mfname, mfname) < 0)
5531399Sahl 			return (dt_set_errno(dtp, errno));
5541399Sahl 	}
5551399Sahl 
5561399Sahl 	if (fprintf(out, "#ifdef\t__cplusplus\nextern \"C\" {\n#endif\n\n") < 0)
5571399Sahl 		return (-1);
5581399Sahl 
5591399Sahl 	for (pvp = dt_list_next(&dtp->dt_provlist);
5601399Sahl 	    pvp != NULL; pvp = dt_list_next(pvp)) {
5611399Sahl 		if (dt_header_provider(dtp, pvp, out) != 0)
5621399Sahl 			return (-1); /* dt_errno is set for us */
5631399Sahl 	}
5641399Sahl 
5651399Sahl 	if (fprintf(out, "\n#ifdef\t__cplusplus\n}\n#endif\n") < 0)
5661399Sahl 		return (dt_set_errno(dtp, errno));
5671399Sahl 
5681399Sahl 	if (fname != NULL && fprintf(out, "\n#endif\t/* _%s */\n", mfname) < 0)
5691399Sahl 		return (dt_set_errno(dtp, errno));
5701399Sahl 
5711399Sahl 	return (0);
5721399Sahl }
573