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.
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.
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]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <dt_printf.h>
38 dt_epid_add(dtrace_hdl_t *dtp, dtrace_epid_t id)
41 int rval, i, maxformat;
42 dtrace_eprobedesc_t *enabled, *nenabled;
43 dtrace_probedesc_t *probe;
45 while (id >= (max = dtp->dt_maxprobe) || dtp->dt_pdesc == NULL) {
46 dtrace_id_t new_max = max ? (max << 1) : 1;
47 size_t nsize = new_max * sizeof (void *);
48 dtrace_probedesc_t **new_pdesc;
49 dtrace_eprobedesc_t **new_edesc;
51 if ((new_pdesc = malloc(nsize)) == NULL ||
52 (new_edesc = malloc(nsize)) == NULL) {
54 return (dt_set_errno(dtp, EDT_NOMEM));
57 bzero(new_pdesc, nsize);
58 bzero(new_edesc, nsize);
60 if (dtp->dt_pdesc != NULL) {
61 size_t osize = max * sizeof (void *);
63 bcopy(dtp->dt_pdesc, new_pdesc, osize);
66 bcopy(dtp->dt_edesc, new_edesc, osize);
70 dtp->dt_pdesc = new_pdesc;
71 dtp->dt_edesc = new_edesc;
72 dtp->dt_maxprobe = new_max;
75 if (dtp->dt_pdesc[id] != NULL)
78 if ((enabled = malloc(sizeof (dtrace_eprobedesc_t))) == NULL)
79 return (dt_set_errno(dtp, EDT_NOMEM));
81 bzero(enabled, sizeof (dtrace_eprobedesc_t));
82 enabled->dtepd_epid = id;
83 enabled->dtepd_nrecs = 1;
86 if (dt_ioctl(dtp, DTRACEIOC_EPROBE, enabled) == -1) {
88 if (dt_ioctl(dtp, DTRACEIOC_EPROBE, &enabled) == -1) {
90 rval = dt_set_errno(dtp, errno);
95 if (DTRACE_SIZEOF_EPROBEDESC(enabled) != sizeof (*enabled)) {
97 * There must be more than one action. Allocate the
98 * appropriate amount of space and try again.
101 malloc(DTRACE_SIZEOF_EPROBEDESC(enabled))) != NULL)
102 bcopy(enabled, nenabled, sizeof (*enabled));
106 if ((enabled = nenabled) == NULL)
107 return (dt_set_errno(dtp, EDT_NOMEM));
110 rval = dt_ioctl(dtp, DTRACEIOC_EPROBE, enabled);
112 rval = dt_ioctl(dtp, DTRACEIOC_EPROBE, &enabled);
116 rval = dt_set_errno(dtp, errno);
122 if ((probe = malloc(sizeof (dtrace_probedesc_t))) == NULL) {
124 return (dt_set_errno(dtp, EDT_NOMEM));
127 probe->dtpd_id = enabled->dtepd_probeid;
129 if (dt_ioctl(dtp, DTRACEIOC_PROBES, probe) == -1) {
130 rval = dt_set_errno(dtp, errno);
134 for (i = 0; i < enabled->dtepd_nrecs; i++) {
135 dtrace_fmtdesc_t fmt;
136 dtrace_recdesc_t *rec = &enabled->dtepd_rec[i];
138 if (!DTRACEACT_ISPRINTFLIKE(rec->dtrd_action))
141 if (rec->dtrd_format == 0)
144 if (rec->dtrd_format <= dtp->dt_maxformat &&
145 dtp->dt_formats[rec->dtrd_format - 1] != NULL)
148 bzero(&fmt, sizeof (fmt));
149 fmt.dtfd_format = rec->dtrd_format;
150 fmt.dtfd_string = NULL;
153 if (dt_ioctl(dtp, DTRACEIOC_FORMAT, &fmt) == -1) {
154 rval = dt_set_errno(dtp, errno);
158 if ((fmt.dtfd_string = malloc(fmt.dtfd_length)) == NULL) {
159 rval = dt_set_errno(dtp, EDT_NOMEM);
163 if (dt_ioctl(dtp, DTRACEIOC_FORMAT, &fmt) == -1) {
164 rval = dt_set_errno(dtp, errno);
165 free(fmt.dtfd_string);
169 while (rec->dtrd_format > (maxformat = dtp->dt_maxformat)) {
170 int new_max = maxformat ? (maxformat << 1) : 1;
171 size_t nsize = new_max * sizeof (void *);
172 size_t osize = maxformat * sizeof (void *);
173 void **new_formats = malloc(nsize);
175 if (new_formats == NULL) {
176 rval = dt_set_errno(dtp, EDT_NOMEM);
177 free(fmt.dtfd_string);
181 bzero(new_formats, nsize);
182 bcopy(dtp->dt_formats, new_formats, osize);
183 free(dtp->dt_formats);
185 dtp->dt_formats = new_formats;
186 dtp->dt_maxformat = new_max;
189 dtp->dt_formats[rec->dtrd_format - 1] =
190 rec->dtrd_action == DTRACEACT_PRINTA ?
191 dtrace_printa_create(dtp, fmt.dtfd_string) :
192 dtrace_printf_create(dtp, fmt.dtfd_string);
194 free(fmt.dtfd_string);
196 if (dtp->dt_formats[rec->dtrd_format - 1] == NULL) {
197 rval = -1; /* dt_errno is set for us */
202 dtp->dt_pdesc[id] = probe;
203 dtp->dt_edesc[id] = enabled;
209 * If we failed, free our allocated probes. Note that if we failed
210 * while allocating formats, we aren't going to free formats that
211 * we have already allocated. This is okay; these formats are
212 * hanging off of dt_formats and will therefore not be leaked.
220 dt_epid_lookup(dtrace_hdl_t *dtp, dtrace_epid_t epid,
221 dtrace_eprobedesc_t **epdp, dtrace_probedesc_t **pdp)
225 if (epid >= dtp->dt_maxprobe || dtp->dt_pdesc[epid] == NULL) {
226 if ((rval = dt_epid_add(dtp, epid)) != 0)
230 assert(epid < dtp->dt_maxprobe);
231 assert(dtp->dt_edesc[epid] != NULL);
232 assert(dtp->dt_pdesc[epid] != NULL);
233 *epdp = dtp->dt_edesc[epid];
234 *pdp = dtp->dt_pdesc[epid];
240 dt_epid_destroy(dtrace_hdl_t *dtp)
244 assert((dtp->dt_pdesc != NULL && dtp->dt_edesc != NULL &&
245 dtp->dt_maxprobe > 0) || (dtp->dt_pdesc == NULL &&
246 dtp->dt_edesc == NULL && dtp->dt_maxprobe == 0));
248 if (dtp->dt_pdesc == NULL)
251 for (i = 0; i < dtp->dt_maxprobe; i++) {
252 if (dtp->dt_edesc[i] == NULL) {
253 assert(dtp->dt_pdesc[i] == NULL);
257 assert(dtp->dt_pdesc[i] != NULL);
258 free(dtp->dt_edesc[i]);
259 free(dtp->dt_pdesc[i]);
263 dtp->dt_pdesc = NULL;
266 dtp->dt_edesc = NULL;
267 dtp->dt_maxprobe = 0;
271 dt_format_lookup(dtrace_hdl_t *dtp, int format)
273 if (format == 0 || format > dtp->dt_maxformat)
276 if (dtp->dt_formats == NULL)
279 return (dtp->dt_formats[format - 1]);
283 dt_format_destroy(dtrace_hdl_t *dtp)
287 for (i = 0; i < dtp->dt_maxformat; i++) {
288 if (dtp->dt_formats[i] != NULL)
289 dt_printf_destroy(dtp->dt_formats[i]);
292 free(dtp->dt_formats);
293 dtp->dt_formats = NULL;
297 dt_aggid_add(dtrace_hdl_t *dtp, dtrace_aggid_t id)
303 while (id >= (max = dtp->dt_maxagg) || dtp->dt_aggdesc == NULL) {
304 dtrace_id_t new_max = max ? (max << 1) : 1;
305 size_t nsize = new_max * sizeof (void *);
306 dtrace_aggdesc_t **new_aggdesc;
308 if ((new_aggdesc = malloc(nsize)) == NULL)
309 return (dt_set_errno(dtp, EDT_NOMEM));
311 bzero(new_aggdesc, nsize);
313 if (dtp->dt_aggdesc != NULL) {
314 bcopy(dtp->dt_aggdesc, new_aggdesc,
315 max * sizeof (void *));
316 free(dtp->dt_aggdesc);
319 dtp->dt_aggdesc = new_aggdesc;
320 dtp->dt_maxagg = new_max;
323 if (dtp->dt_aggdesc[id] == NULL) {
324 dtrace_aggdesc_t *agg, *nagg;
326 if ((agg = malloc(sizeof (dtrace_aggdesc_t))) == NULL)
327 return (dt_set_errno(dtp, EDT_NOMEM));
329 bzero(agg, sizeof (dtrace_aggdesc_t));
331 agg->dtagd_nrecs = 1;
334 if (dt_ioctl(dtp, DTRACEIOC_AGGDESC, agg) == -1) {
336 if (dt_ioctl(dtp, DTRACEIOC_AGGDESC, &agg) == -1) {
338 rval = dt_set_errno(dtp, errno);
343 if (DTRACE_SIZEOF_AGGDESC(agg) != sizeof (*agg)) {
345 * There must be more than one action. Allocate the
346 * appropriate amount of space and try again.
348 if ((nagg = malloc(DTRACE_SIZEOF_AGGDESC(agg))) != NULL)
349 bcopy(agg, nagg, sizeof (*agg));
353 if ((agg = nagg) == NULL)
354 return (dt_set_errno(dtp, EDT_NOMEM));
357 rval = dt_ioctl(dtp, DTRACEIOC_AGGDESC, agg);
359 rval = dt_ioctl(dtp, DTRACEIOC_AGGDESC, &agg);
363 rval = dt_set_errno(dtp, errno);
370 * If we have a uarg, it's a pointer to the compiler-generated
371 * statement; we'll use this value to get the name and
372 * compiler-generated variable ID for the aggregation. If
373 * we're grabbing an anonymous enabling, this pointer value
374 * is obviously meaningless -- and in this case, we can't
375 * provide the compiler-generated aggregation information.
377 if (dtp->dt_options[DTRACEOPT_GRABANON] == DTRACEOPT_UNSET &&
378 agg->dtagd_rec[0].dtrd_uarg != 0) {
379 dtrace_stmtdesc_t *sdp;
382 sdp = (dtrace_stmtdesc_t *)(uintptr_t)
383 agg->dtagd_rec[0].dtrd_uarg;
384 aid = sdp->dtsd_aggdata;
385 agg->dtagd_name = aid->di_name;
386 agg->dtagd_varid = aid->di_id;
388 agg->dtagd_varid = DTRACE_AGGVARIDNONE;
391 if ((epid = agg->dtagd_epid) >= dtp->dt_maxprobe ||
392 dtp->dt_pdesc[epid] == NULL) {
393 if ((rval = dt_epid_add(dtp, epid)) != 0) {
399 dtp->dt_aggdesc[id] = agg;
406 dt_aggid_lookup(dtrace_hdl_t *dtp, dtrace_aggid_t aggid,
407 dtrace_aggdesc_t **adp)
411 if (aggid >= dtp->dt_maxagg || dtp->dt_aggdesc[aggid] == NULL) {
412 if ((rval = dt_aggid_add(dtp, aggid)) != 0)
416 assert(aggid < dtp->dt_maxagg);
417 assert(dtp->dt_aggdesc[aggid] != NULL);
418 *adp = dtp->dt_aggdesc[aggid];
424 dt_aggid_destroy(dtrace_hdl_t *dtp)
428 assert((dtp->dt_aggdesc != NULL && dtp->dt_maxagg != 0) ||
429 (dtp->dt_aggdesc == NULL && dtp->dt_maxagg == 0));
431 if (dtp->dt_aggdesc == NULL)
434 for (i = 0; i < dtp->dt_maxagg; i++) {
435 if (dtp->dt_aggdesc[i] != NULL)
436 free(dtp->dt_aggdesc[i]);
439 free(dtp->dt_aggdesc);
440 dtp->dt_aggdesc = NULL;