1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <strings.h>
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <assert.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <dt_impl.h>
36*0Sstevel@tonic-gate #include <dt_printf.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static int
39*0Sstevel@tonic-gate dt_epid_add(dtrace_hdl_t *dtp, dtrace_epid_t id)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	dtrace_id_t max;
42*0Sstevel@tonic-gate 	int rval, i, maxformat;
43*0Sstevel@tonic-gate 	dtrace_eprobedesc_t *enabled, *nenabled;
44*0Sstevel@tonic-gate 	dtrace_probedesc_t *probe;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	while (id >= (max = dtp->dt_maxprobe) || dtp->dt_pdesc == NULL) {
47*0Sstevel@tonic-gate 		dtrace_id_t new_max = max ? (max << 1) : 1;
48*0Sstevel@tonic-gate 		size_t nsize = new_max * sizeof (void *);
49*0Sstevel@tonic-gate 		dtrace_probedesc_t **new_pdesc;
50*0Sstevel@tonic-gate 		dtrace_eprobedesc_t **new_edesc;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 		if ((new_pdesc = malloc(nsize)) == NULL ||
53*0Sstevel@tonic-gate 		    (new_edesc = malloc(nsize)) == NULL) {
54*0Sstevel@tonic-gate 			free(new_pdesc);
55*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
56*0Sstevel@tonic-gate 		}
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 		bzero(new_pdesc, nsize);
59*0Sstevel@tonic-gate 		bzero(new_edesc, nsize);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 		if (dtp->dt_pdesc != NULL) {
62*0Sstevel@tonic-gate 			size_t osize = max * sizeof (void *);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 			bcopy(dtp->dt_pdesc, new_pdesc, osize);
65*0Sstevel@tonic-gate 			free(dtp->dt_pdesc);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 			bcopy(dtp->dt_edesc, new_edesc, osize);
68*0Sstevel@tonic-gate 			free(dtp->dt_edesc);
69*0Sstevel@tonic-gate 		}
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 		dtp->dt_pdesc = new_pdesc;
72*0Sstevel@tonic-gate 		dtp->dt_edesc = new_edesc;
73*0Sstevel@tonic-gate 		dtp->dt_maxprobe = new_max;
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (dtp->dt_pdesc[id] != NULL)
77*0Sstevel@tonic-gate 		return (0);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	if ((enabled = malloc(sizeof (dtrace_eprobedesc_t))) == NULL)
80*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	bzero(enabled, sizeof (dtrace_eprobedesc_t));
83*0Sstevel@tonic-gate 	enabled->dtepd_epid = id;
84*0Sstevel@tonic-gate 	enabled->dtepd_nrecs = 1;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_EPROBE, enabled) == -1) {
87*0Sstevel@tonic-gate 		rval = dt_set_errno(dtp, errno);
88*0Sstevel@tonic-gate 		free(enabled);
89*0Sstevel@tonic-gate 		return (rval);
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	if (DTRACE_SIZEOF_EPROBEDESC(enabled) != sizeof (*enabled)) {
93*0Sstevel@tonic-gate 		/*
94*0Sstevel@tonic-gate 		 * There must be more than one action.  Allocate the
95*0Sstevel@tonic-gate 		 * appropriate amount of space and try again.
96*0Sstevel@tonic-gate 		 */
97*0Sstevel@tonic-gate 		if ((nenabled =
98*0Sstevel@tonic-gate 		    malloc(DTRACE_SIZEOF_EPROBEDESC(enabled))) != NULL)
99*0Sstevel@tonic-gate 			bcopy(enabled, nenabled, sizeof (*enabled));
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		free(enabled);
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		if ((enabled = nenabled) == NULL)
104*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 		rval = dt_ioctl(dtp, DTRACEIOC_EPROBE, enabled);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		if (rval == -1) {
109*0Sstevel@tonic-gate 			rval = dt_set_errno(dtp, errno);
110*0Sstevel@tonic-gate 			free(enabled);
111*0Sstevel@tonic-gate 			return (rval);
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if ((probe = malloc(sizeof (dtrace_probedesc_t))) == NULL) {
116*0Sstevel@tonic-gate 		free(enabled);
117*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	probe->dtpd_id = enabled->dtepd_probeid;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_PROBES, probe) == -1) {
123*0Sstevel@tonic-gate 		rval = dt_set_errno(dtp, errno);
124*0Sstevel@tonic-gate 		goto err;
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	for (i = 0; i < enabled->dtepd_nrecs; i++) {
128*0Sstevel@tonic-gate 		dtrace_fmtdesc_t fmt;
129*0Sstevel@tonic-gate 		dtrace_recdesc_t *rec = &enabled->dtepd_rec[i];
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 		if (!DTRACEACT_ISPRINTFLIKE(rec->dtrd_action))
132*0Sstevel@tonic-gate 			continue;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		if (rec->dtrd_format == 0)
135*0Sstevel@tonic-gate 			continue;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		if (rec->dtrd_format <= dtp->dt_maxformat &&
138*0Sstevel@tonic-gate 		    dtp->dt_formats[rec->dtrd_format - 1] != NULL)
139*0Sstevel@tonic-gate 			continue;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		bzero(&fmt, sizeof (fmt));
142*0Sstevel@tonic-gate 		fmt.dtfd_format = rec->dtrd_format;
143*0Sstevel@tonic-gate 		fmt.dtfd_string = NULL;
144*0Sstevel@tonic-gate 		fmt.dtfd_length = 0;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 		if (dt_ioctl(dtp, DTRACEIOC_FORMAT, &fmt) == -1) {
147*0Sstevel@tonic-gate 			rval = dt_set_errno(dtp, errno);
148*0Sstevel@tonic-gate 			goto err;
149*0Sstevel@tonic-gate 		}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 		if ((fmt.dtfd_string = malloc(fmt.dtfd_length)) == NULL) {
152*0Sstevel@tonic-gate 			rval = dt_set_errno(dtp, EDT_NOMEM);
153*0Sstevel@tonic-gate 			goto err;
154*0Sstevel@tonic-gate 		}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 		if (dt_ioctl(dtp, DTRACEIOC_FORMAT, &fmt) == -1) {
157*0Sstevel@tonic-gate 			rval = dt_set_errno(dtp, errno);
158*0Sstevel@tonic-gate 			free(fmt.dtfd_string);
159*0Sstevel@tonic-gate 			goto err;
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		while (rec->dtrd_format > (maxformat = dtp->dt_maxformat)) {
163*0Sstevel@tonic-gate 			int new_max = maxformat ? (maxformat << 1) : 1;
164*0Sstevel@tonic-gate 			size_t nsize = new_max * sizeof (void *);
165*0Sstevel@tonic-gate 			size_t osize = maxformat * sizeof (void *);
166*0Sstevel@tonic-gate 			void **new_formats = malloc(nsize);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 			if (new_formats == NULL) {
169*0Sstevel@tonic-gate 				rval = dt_set_errno(dtp, EDT_NOMEM);
170*0Sstevel@tonic-gate 				free(fmt.dtfd_string);
171*0Sstevel@tonic-gate 				goto err;
172*0Sstevel@tonic-gate 			}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 			bzero(new_formats, nsize);
175*0Sstevel@tonic-gate 			bcopy(dtp->dt_formats, new_formats, osize);
176*0Sstevel@tonic-gate 			free(dtp->dt_formats);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 			dtp->dt_formats = new_formats;
179*0Sstevel@tonic-gate 			dtp->dt_maxformat = new_max;
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		dtp->dt_formats[rec->dtrd_format - 1] =
183*0Sstevel@tonic-gate 		    rec->dtrd_action == DTRACEACT_PRINTA ?
184*0Sstevel@tonic-gate 		    dtrace_printa_create(dtp, fmt.dtfd_string) :
185*0Sstevel@tonic-gate 		    dtrace_printf_create(dtp, fmt.dtfd_string);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		free(fmt.dtfd_string);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		if (dtp->dt_formats[rec->dtrd_format - 1] == NULL) {
190*0Sstevel@tonic-gate 			rval = -1; /* dt_errno is set for us */
191*0Sstevel@tonic-gate 			goto err;
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	dtp->dt_pdesc[id] = probe;
196*0Sstevel@tonic-gate 	dtp->dt_edesc[id] = enabled;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	return (0);
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate err:
201*0Sstevel@tonic-gate 	/*
202*0Sstevel@tonic-gate 	 * If we failed, free our allocated probes.  Note that if we failed
203*0Sstevel@tonic-gate 	 * while allocating formats, we aren't going to free formats that
204*0Sstevel@tonic-gate 	 * we have already allocated.  This is okay; these formats are
205*0Sstevel@tonic-gate 	 * hanging off of dt_formats and will therefore not be leaked.
206*0Sstevel@tonic-gate 	 */
207*0Sstevel@tonic-gate 	free(enabled);
208*0Sstevel@tonic-gate 	free(probe);
209*0Sstevel@tonic-gate 	return (rval);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate int
213*0Sstevel@tonic-gate dt_epid_lookup(dtrace_hdl_t *dtp, dtrace_epid_t epid,
214*0Sstevel@tonic-gate     dtrace_eprobedesc_t **epdp, dtrace_probedesc_t **pdp)
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	int rval;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	if (epid >= dtp->dt_maxprobe || dtp->dt_pdesc[epid] == NULL) {
219*0Sstevel@tonic-gate 		if ((rval = dt_epid_add(dtp, epid)) != 0)
220*0Sstevel@tonic-gate 			return (rval);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	assert(epid < dtp->dt_maxprobe);
224*0Sstevel@tonic-gate 	assert(dtp->dt_edesc[epid] != NULL);
225*0Sstevel@tonic-gate 	assert(dtp->dt_pdesc[epid] != NULL);
226*0Sstevel@tonic-gate 	*epdp = dtp->dt_edesc[epid];
227*0Sstevel@tonic-gate 	*pdp = dtp->dt_pdesc[epid];
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	return (0);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate void
233*0Sstevel@tonic-gate dt_epid_destroy(dtrace_hdl_t *dtp)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	size_t i;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	assert((dtp->dt_pdesc != NULL && dtp->dt_edesc != NULL &&
238*0Sstevel@tonic-gate 	    dtp->dt_maxprobe > 0) || (dtp->dt_pdesc == NULL &&
239*0Sstevel@tonic-gate 	    dtp->dt_edesc == NULL && dtp->dt_maxprobe == 0));
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (dtp->dt_pdesc == NULL)
242*0Sstevel@tonic-gate 		return;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	for (i = 0; i < dtp->dt_maxprobe; i++) {
245*0Sstevel@tonic-gate 		if (dtp->dt_edesc[i] == NULL) {
246*0Sstevel@tonic-gate 			assert(dtp->dt_pdesc[i] == NULL);
247*0Sstevel@tonic-gate 			continue;
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 		assert(dtp->dt_pdesc[i] != NULL);
251*0Sstevel@tonic-gate 		free(dtp->dt_edesc[i]);
252*0Sstevel@tonic-gate 		free(dtp->dt_pdesc[i]);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	free(dtp->dt_pdesc);
256*0Sstevel@tonic-gate 	dtp->dt_pdesc = NULL;
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	free(dtp->dt_edesc);
259*0Sstevel@tonic-gate 	dtp->dt_edesc = NULL;
260*0Sstevel@tonic-gate 	dtp->dt_maxprobe = 0;
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate void *
264*0Sstevel@tonic-gate dt_format_lookup(dtrace_hdl_t *dtp, int format)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	if (format == 0 || format > dtp->dt_maxformat)
267*0Sstevel@tonic-gate 		return (NULL);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	if (dtp->dt_formats == NULL)
270*0Sstevel@tonic-gate 		return (NULL);
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	return (dtp->dt_formats[format - 1]);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate void
276*0Sstevel@tonic-gate dt_format_destroy(dtrace_hdl_t *dtp)
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate 	int i;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	for (i = 0; i < dtp->dt_maxformat; i++) {
281*0Sstevel@tonic-gate 		if (dtp->dt_formats[i] != NULL)
282*0Sstevel@tonic-gate 			dt_printf_destroy(dtp->dt_formats[i]);
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	free(dtp->dt_formats);
286*0Sstevel@tonic-gate 	dtp->dt_formats = NULL;
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate static int
290*0Sstevel@tonic-gate dt_aggid_add(dtrace_hdl_t *dtp, dtrace_aggid_t id)
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate 	dtrace_id_t max;
293*0Sstevel@tonic-gate 	dtrace_epid_t epid;
294*0Sstevel@tonic-gate 	int rval;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	while (id >= (max = dtp->dt_maxagg) || dtp->dt_aggdesc == NULL) {
297*0Sstevel@tonic-gate 		dtrace_id_t new_max = max ? (max << 1) : 1;
298*0Sstevel@tonic-gate 		size_t nsize = new_max * sizeof (void *);
299*0Sstevel@tonic-gate 		dtrace_aggdesc_t **new_aggdesc;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		if ((new_aggdesc = malloc(nsize)) == NULL)
302*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		bzero(new_aggdesc, nsize);
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		if (dtp->dt_aggdesc != NULL) {
307*0Sstevel@tonic-gate 			bcopy(dtp->dt_aggdesc, new_aggdesc,
308*0Sstevel@tonic-gate 			    max * sizeof (void *));
309*0Sstevel@tonic-gate 			free(dtp->dt_aggdesc);
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		dtp->dt_aggdesc = new_aggdesc;
313*0Sstevel@tonic-gate 		dtp->dt_maxagg = new_max;
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	if (dtp->dt_aggdesc[id] == NULL) {
317*0Sstevel@tonic-gate 		dtrace_aggdesc_t *agg, *nagg;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		if ((agg = malloc(sizeof (dtrace_aggdesc_t))) == NULL)
320*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 		bzero(agg, sizeof (dtrace_aggdesc_t));
323*0Sstevel@tonic-gate 		agg->dtagd_id = id;
324*0Sstevel@tonic-gate 		agg->dtagd_nrecs = 1;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 		if (dt_ioctl(dtp, DTRACEIOC_AGGDESC, agg) == -1) {
327*0Sstevel@tonic-gate 			rval = dt_set_errno(dtp, errno);
328*0Sstevel@tonic-gate 			free(agg);
329*0Sstevel@tonic-gate 			return (rval);
330*0Sstevel@tonic-gate 		}
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		if (DTRACE_SIZEOF_AGGDESC(agg) != sizeof (*agg)) {
333*0Sstevel@tonic-gate 			/*
334*0Sstevel@tonic-gate 			 * There must be more than one action.  Allocate the
335*0Sstevel@tonic-gate 			 * appropriate amount of space and try again.
336*0Sstevel@tonic-gate 			 */
337*0Sstevel@tonic-gate 			if ((nagg = malloc(DTRACE_SIZEOF_AGGDESC(agg))) != NULL)
338*0Sstevel@tonic-gate 				bcopy(agg, nagg, sizeof (*agg));
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 			free(agg);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 			if ((agg = nagg) == NULL)
343*0Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_NOMEM));
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 			rval = dt_ioctl(dtp, DTRACEIOC_AGGDESC, agg);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 			if (rval == -1) {
348*0Sstevel@tonic-gate 				rval = dt_set_errno(dtp, errno);
349*0Sstevel@tonic-gate 				free(agg);
350*0Sstevel@tonic-gate 				return (rval);
351*0Sstevel@tonic-gate 			}
352*0Sstevel@tonic-gate 		}
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 		if (agg->dtagd_rec[0].dtrd_uarg) {
355*0Sstevel@tonic-gate 			dtrace_stmtdesc_t *sdp;
356*0Sstevel@tonic-gate 			dt_ident_t *aid;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 			sdp = (dtrace_stmtdesc_t *)agg->dtagd_rec[0].dtrd_uarg;
359*0Sstevel@tonic-gate 			aid = sdp->dtsd_aggdata;
360*0Sstevel@tonic-gate 			agg->dtagd_name = aid->di_name;
361*0Sstevel@tonic-gate 		}
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 		if ((epid = agg->dtagd_epid) >= dtp->dt_maxprobe ||
364*0Sstevel@tonic-gate 		    dtp->dt_pdesc[epid] == NULL) {
365*0Sstevel@tonic-gate 			if ((rval = dt_epid_add(dtp, epid)) != 0) {
366*0Sstevel@tonic-gate 				free(agg);
367*0Sstevel@tonic-gate 				return (rval);
368*0Sstevel@tonic-gate 			}
369*0Sstevel@tonic-gate 		}
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 		dtp->dt_aggdesc[id] = agg;
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	return (0);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate int
378*0Sstevel@tonic-gate dt_aggid_lookup(dtrace_hdl_t *dtp, dtrace_aggid_t aggid,
379*0Sstevel@tonic-gate     dtrace_aggdesc_t **adp)
380*0Sstevel@tonic-gate {
381*0Sstevel@tonic-gate 	int rval;
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	if (aggid >= dtp->dt_maxagg || dtp->dt_aggdesc[aggid] == NULL) {
384*0Sstevel@tonic-gate 		if ((rval = dt_aggid_add(dtp, aggid)) != 0)
385*0Sstevel@tonic-gate 			return (rval);
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	assert(aggid < dtp->dt_maxagg);
389*0Sstevel@tonic-gate 	assert(dtp->dt_aggdesc[aggid] != NULL);
390*0Sstevel@tonic-gate 	*adp = dtp->dt_aggdesc[aggid];
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	return (0);
393*0Sstevel@tonic-gate }
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate void
396*0Sstevel@tonic-gate dt_aggid_destroy(dtrace_hdl_t *dtp)
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate 	size_t i;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	assert((dtp->dt_aggdesc != NULL && dtp->dt_maxagg != 0) ||
401*0Sstevel@tonic-gate 	    (dtp->dt_aggdesc == NULL && dtp->dt_maxagg == 0));
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	if (dtp->dt_aggdesc == NULL)
404*0Sstevel@tonic-gate 		return;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	for (i = 0; i < dtp->dt_maxagg; i++) {
407*0Sstevel@tonic-gate 		if (dtp->dt_aggdesc[i] != NULL)
408*0Sstevel@tonic-gate 			free(dtp->dt_aggdesc[i]);
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	free(dtp->dt_aggdesc);
412*0Sstevel@tonic-gate 	dtp->dt_aggdesc = NULL;
413*0Sstevel@tonic-gate 	dtp->dt_maxagg = 0;
414*0Sstevel@tonic-gate }
415