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]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/types.h>
31 #include <sys/sysmacros.h>
44 #include <dt_strtab.h>
45 #include <dt_program.h>
46 #include <dt_provider.h>
47 #include <dt_xlator.h>
51 dt_dof_init(dtrace_hdl_t *dtp)
53 dt_dof_t *ddo = &dtp->dt_dof;
57 ddo->ddo_strsec = DOF_SECIDX_NONE;
58 ddo->ddo_xlimport = NULL;
59 ddo->ddo_xlexport = NULL;
61 dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
62 dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
63 dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
64 dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
66 dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
67 dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
68 dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
69 dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
70 dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
72 dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
76 dt_dof_fini(dtrace_hdl_t *dtp)
78 dt_dof_t *ddo = &dtp->dt_dof;
80 dt_free(dtp, ddo->ddo_xlimport);
81 dt_free(dtp, ddo->ddo_xlexport);
83 dt_buf_destroy(dtp, &ddo->ddo_secs);
84 dt_buf_destroy(dtp, &ddo->ddo_strs);
85 dt_buf_destroy(dtp, &ddo->ddo_ldata);
86 dt_buf_destroy(dtp, &ddo->ddo_udata);
88 dt_buf_destroy(dtp, &ddo->ddo_probes);
89 dt_buf_destroy(dtp, &ddo->ddo_args);
90 dt_buf_destroy(dtp, &ddo->ddo_offs);
91 dt_buf_destroy(dtp, &ddo->ddo_enoffs);
92 dt_buf_destroy(dtp, &ddo->ddo_rels);
94 dt_buf_destroy(dtp, &ddo->ddo_xlms);
98 dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
100 dt_dof_t *ddo = &dtp->dt_dof;
101 uint_t i, nx = dtp->dt_xlatorid;
103 assert(ddo->ddo_hdl == dtp);
107 ddo->ddo_strsec = DOF_SECIDX_NONE;
109 dt_free(dtp, ddo->ddo_xlimport);
110 dt_free(dtp, ddo->ddo_xlexport);
112 ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
113 ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
115 if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
116 return (-1); /* errno is set for us */
118 for (i = 0; i < nx; i++) {
119 ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
120 ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
123 dt_buf_reset(dtp, &ddo->ddo_secs);
124 dt_buf_reset(dtp, &ddo->ddo_strs);
125 dt_buf_reset(dtp, &ddo->ddo_ldata);
126 dt_buf_reset(dtp, &ddo->ddo_udata);
128 dt_buf_reset(dtp, &ddo->ddo_probes);
129 dt_buf_reset(dtp, &ddo->ddo_args);
130 dt_buf_reset(dtp, &ddo->ddo_offs);
131 dt_buf_reset(dtp, &ddo->ddo_enoffs);
132 dt_buf_reset(dtp, &ddo->ddo_rels);
134 dt_buf_reset(dtp, &ddo->ddo_xlms);
139 * Add a loadable DOF section to the file using the specified data buffer and
140 * the specified DOF section attributes. DOF_SECF_LOAD must be set in flags.
141 * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
144 dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
145 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
147 dtrace_hdl_t *dtp = ddo->ddo_hdl;
151 s.dofs_align = align;
152 s.dofs_flags = flags | DOF_SECF_LOAD;
153 s.dofs_entsize = entsize;
154 s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
157 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
160 dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
162 return (ddo->ddo_nsecs++);
166 * Add an unloadable DOF section to the file using the specified data buffer
167 * and DOF section attributes. DOF_SECF_LOAD must *not* be set in flags.
168 * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
171 dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
172 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
174 dtrace_hdl_t *dtp = ddo->ddo_hdl;
178 s.dofs_align = align;
179 s.dofs_flags = flags & ~DOF_SECF_LOAD;
180 s.dofs_entsize = entsize;
181 s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
184 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
187 dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
189 return (ddo->ddo_nsecs++);
193 * Add a string to the global string table associated with the DOF. The offset
194 * of the string is returned as an index into the string table.
197 dof_add_string(dt_dof_t *ddo, const char *s)
199 dt_buf_t *bp = &ddo->ddo_strs;
200 dof_stridx_t i = dt_buf_len(bp);
202 if (i != 0 && (s == NULL || *s == '\0'))
203 return (0); /* string table has \0 at offset 0 */
205 dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
210 dof_attr(const dtrace_attribute_t *ap)
212 return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
216 dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
218 dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
225 dof_secidx_t strsec = DOF_SECIDX_NONE;
226 dof_secidx_t intsec = DOF_SECIDX_NONE;
227 dof_secidx_t hdrsec = DOF_SECIDX_NONE;
229 if (dp->dtdo_buf != NULL) {
230 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
231 DOF_SECT_DIF, sizeof (dif_instr_t), 0,
232 sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
235 if (dp->dtdo_inttab != NULL) {
236 dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
237 DOF_SECT_INTTAB, sizeof (uint64_t), 0,
238 sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
241 if (dp->dtdo_strtab != NULL) {
242 dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
243 DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
246 if (dp->dtdo_vartab != NULL) {
247 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
248 DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
249 sizeof (dtrace_difv_t) * dp->dtdo_varlen);
252 if (dp->dtdo_xlmtab != NULL) {
253 dof_xlref_t *xlt, *xlp;
256 xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
257 pnp = dp->dtdo_xlmtab;
260 * dtdo_xlmtab contains pointers to the translator members.
261 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
262 * The XLMEMBERS entries are in order by their dn_membid, so
263 * the member section offset is the population count of bits
264 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
266 for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
267 dt_node_t *dnp = *pnp++;
268 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
270 xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
271 xlp->dofxr_member = dt_popcb(
272 ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
273 xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
276 dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
277 sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
278 sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
282 * Copy the return type and the array of section indices that form the
283 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
285 assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
286 dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
287 bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
288 bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
290 hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
291 sizeof (dof_secidx_t), 0, 0,
292 sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
295 * Add any other sections related to dtrace_difo_t. These are not
296 * referenced in dof_difohdr_t because they are not used by emulation.
298 if (dp->dtdo_kreltab != NULL) {
299 relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
300 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
301 sizeof (dof_relodesc_t) * dp->dtdo_krelen);
304 * This code assumes the target of all relocations is the
305 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
306 * need relocation in the future this will need to change.
308 dofr.dofr_strtab = strsec;
309 dofr.dofr_relsec = relsec;
310 dofr.dofr_tgtsec = intsec;
312 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
313 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
316 if (dp->dtdo_ureltab != NULL) {
317 relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
318 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
319 sizeof (dof_relodesc_t) * dp->dtdo_urelen);
322 * This code assumes the target of all relocations is the
323 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
324 * need relocation in the future this will need to change.
326 dofr.dofr_strtab = strsec;
327 dofr.dofr_relsec = relsec;
328 dofr.dofr_tgtsec = intsec;
330 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
331 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
338 dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
340 dtrace_hdl_t *dtp = ddo->ddo_hdl;
341 dof_xlmember_t dofxm;
345 char buf[DT_TYPE_NAMELEN];
349 assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
350 xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
352 if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
353 return; /* translator has already been emitted */
355 dt_buf_reset(dtp, &ddo->ddo_xlms);
358 * Generate an array of dof_xlmember_t's into ddo_xlms. If we are
359 * importing the translator, add only those members referenced by the
360 * program and set the dofxm_difo reference of each member to NONE. If
361 * we're exporting the translator, add all members and a DIFO for each.
363 for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
364 if (type == DOF_SECT_XLIMPORT) {
365 if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
366 continue; /* member is not referenced */
367 dofxm.dofxm_difo = DOF_SECIDX_NONE;
369 dofxm.dofxm_difo = dof_add_difo(ddo,
370 dxp->dx_membdif[dnp->dn_membid]);
373 dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
374 dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
376 dt_buf_write(dtp, &ddo->ddo_xlms,
377 &dofxm, sizeof (dofxm), sizeof (uint32_t));
380 dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
381 sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
383 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
385 dofxl.dofxl_strtab = ddo->ddo_strsec;
386 dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
387 dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
388 dofxl.dofxl_argc = 1;
389 dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
390 dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
391 dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
393 xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
394 sizeof (uint32_t), 0, 0, sizeof (dofxl));
399 dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
401 dt_dof_t *ddo = data;
402 dtrace_hdl_t *dtp = ddo->ddo_hdl;
403 dt_probe_t *prp = idp->di_data;
407 dt_probe_instance_t *pip;
410 char buf[DT_TYPE_NAMELEN];
413 dofpr.dofpr_addr = 0;
414 dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
415 dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
417 for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
418 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
419 dnp->dn_type, buf, sizeof (buf)));
422 dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
424 for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
425 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
426 dnp->dn_type, buf, sizeof (buf)));
429 dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
431 for (i = 0; i < prp->pr_xargc; i++) {
432 dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
433 sizeof (uint8_t), sizeof (uint8_t));
436 dofpr.dofpr_nargc = prp->pr_nargc;
437 dofpr.dofpr_xargc = prp->pr_xargc;
438 dofpr.dofpr_pad1 = 0;
439 dofpr.dofpr_pad2 = 0;
441 for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
442 dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
445 dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
448 * There should be one probe offset or is-enabled probe offset
449 * or else this probe instance won't have been created. The
450 * kernel will reject DOF which has a probe with no offsets.
452 assert(pip->pi_noffs + pip->pi_nenoffs > 0);
455 dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
456 dofpr.dofpr_noffs = pip->pi_noffs;
457 dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
458 pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
460 dofpr.dofpr_enoffidx =
461 dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
462 dofpr.dofpr_nenoffs = pip->pi_nenoffs;
463 dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
464 pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
467 * If pi_rname isn't set, the relocation will be against the
468 * function name. If it is, the relocation will be against
469 * pi_rname. This will be used if the function is scoped
470 * locally so an alternate symbol is added for the purpose
471 * of this relocation.
473 if (pip->pi_rname[0] == '\0')
474 dofr.dofr_name = dofpr.dofpr_func;
476 dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
477 dofr.dofr_type = DOF_RELO_SETX;
478 dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
481 dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
482 sizeof (dofr), sizeof (uint64_t));
484 dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
485 sizeof (dofpr), sizeof (uint64_t));
492 dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
494 dtrace_hdl_t *dtp = ddo->ddo_hdl;
495 dof_provider_t dofpv;
502 if (pvp->pv_flags & DT_PROVIDER_IMPL)
503 return; /* ignore providers that are exported by dtrace(7D) */
505 nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
506 dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
507 xr = 1; /* reserve dofs[0] for the provider itself */
510 * For each translator referenced by the provider (pv_xrefs), emit an
511 * exported translator section for it if one hasn't been created yet.
513 for (i = 0; i < pvp->pv_xrmax; i++) {
514 if (BT_TEST(pvp->pv_xrefs, i) &&
515 dtp->dt_xlatemode == DT_XL_DYNAMIC) {
516 dof_add_translator(ddo,
517 dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
518 dofs[xr++] = ddo->ddo_xlexport[i];
522 dt_buf_reset(dtp, &ddo->ddo_probes);
523 dt_buf_reset(dtp, &ddo->ddo_args);
524 dt_buf_reset(dtp, &ddo->ddo_offs);
525 dt_buf_reset(dtp, &ddo->ddo_enoffs);
526 dt_buf_reset(dtp, &ddo->ddo_rels);
528 (void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
530 dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
531 sizeof (uint64_t), 0, sizeof (dof_probe_t),
532 dt_buf_len(&ddo->ddo_probes));
534 dt_buf_concat(dtp, &ddo->ddo_ldata,
535 &ddo->ddo_probes, sizeof (uint64_t));
537 dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
538 sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
540 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
542 dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
543 sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
545 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
547 if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
548 dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
549 DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
551 dofpv.dofpv_prenoffs = DOF_SECT_NONE;
554 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
556 dofpv.dofpv_strtab = ddo->ddo_strsec;
557 dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
559 dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
560 dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
561 dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
562 dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
563 dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
565 dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
566 sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
568 dofr.dofr_strtab = dofpv.dofpv_strtab;
569 dofr.dofr_tgtsec = dofpv.dofpv_probes;
570 dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
571 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
572 dt_buf_len(&ddo->ddo_rels));
574 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
576 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
577 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
579 if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
580 (void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
581 sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
582 sizeof (dof_secidx_t) * (nxr + 1));
587 dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
590 * If our config values cannot fit in a uint8_t, we can't generate a
591 * DOF header since the values won't fit. This can only happen if the
592 * user forcibly compiles a program with an artificial configuration.
594 if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
595 dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
596 dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
597 return (dt_set_errno(dtp, EOVERFLOW));
599 bzero(hp, sizeof (dof_hdr_t));
601 hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
602 hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
603 hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
604 hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
606 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
607 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
609 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
611 hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
612 hp->dofh_ident[DOF_ID_VERSION] = dofversion;
613 hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
614 hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
615 hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
617 hp->dofh_hdrsize = sizeof (dof_hdr_t);
618 hp->dofh_secsize = sizeof (dof_sec_t);
619 hp->dofh_secoff = sizeof (dof_hdr_t);
625 dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
627 dt_dof_t *ddo = &dtp->dt_dof;
629 const dtrace_ecbdesc_t *edp, *last;
630 const dtrace_probedesc_t *pdp;
631 const dtrace_actdesc_t *ap;
632 const dt_stmt_t *stp;
648 if (flags & ~DTRACE_D_MASK) {
649 (void) dt_set_errno(dtp, EINVAL);
653 flags |= dtp->dt_dflags;
655 if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
658 if (dt_dof_reset(dtp, pgp) != 0)
662 * Iterate through the statement list computing the maximum number of
663 * actions and the maximum format string for allocating local buffers.
665 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
666 stp != NULL; stp = dt_list_next(stp), last = edp) {
668 dtrace_stmtdesc_t *sdp = stp->ds_desc;
669 dtrace_actdesc_t *ap = sdp->dtsd_action;
671 if (sdp->dtsd_fmtdata != NULL) {
672 i = dtrace_printf_format(dtp,
673 sdp->dtsd_fmtdata, NULL, 0);
674 maxfmt = MAX(maxfmt, i);
677 if ((edp = sdp->dtsd_ecbdesc) == last)
678 continue; /* same ecb as previous statement */
680 for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
683 maxacts = MAX(maxacts, i);
686 dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
687 fmt = alloca(maxfmt + 1);
689 ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
690 (void) dof_add_string(ddo, "");
693 * If there are references to dynamic translators in the program, add
694 * an imported translator table entry for each referenced translator.
696 if (pgp->dp_xrefslen != 0) {
697 for (dxp = dt_list_next(&dtp->dt_xlators);
698 dxp != NULL; dxp = dt_list_next(dxp)) {
699 if (dxp->dx_id < pgp->dp_xrefslen &&
700 pgp->dp_xrefs[dxp->dx_id] != NULL)
701 dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
706 * Now iterate through the statement list, creating the DOF section
707 * headers and data for each one and adding them to our buffers.
709 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
710 stp != NULL; stp = dt_list_next(stp), last = edp) {
712 dof_secidx_t probesec = DOF_SECIDX_NONE;
713 dof_secidx_t prdsec = DOF_SECIDX_NONE;
714 dof_secidx_t actsec = DOF_SECIDX_NONE;
716 const dt_stmt_t *next = stp;
717 dtrace_stmtdesc_t *sdp = stp->ds_desc;
718 dof_stridx_t strndx = 0;
719 dof_probedesc_t dofp;
723 if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
724 continue; /* same ecb as previous statement */
726 pdp = &edp->dted_probe;
729 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
730 * and copy the probe description strings into the string table.
732 dofp.dofp_strtab = ddo->ddo_strsec;
733 dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
734 dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
735 dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
736 dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
737 dofp.dofp_id = pdp->dtpd_id;
739 probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
740 sizeof (dof_secidx_t), 0,
741 sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
744 * If there is a predicate DIFO associated with the ecbdesc,
745 * write out the DIFO sections and save the DIFO section index.
747 if (edp->dted_pred.dtpdd_difo != NULL)
748 prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
751 * Now iterate through the action list generating DIFOs as
752 * referenced therein and adding action descriptions to 'dofa'.
754 for (i = 0, ap = edp->dted_action;
755 ap != NULL; ap = ap->dtad_next, i++) {
757 if (ap->dtad_difo != NULL) {
759 dof_add_difo(ddo, ap->dtad_difo);
761 dofa[i].dofa_difo = DOF_SECIDX_NONE;
764 * If the first action in a statement has format data,
765 * add the format string to the global string table.
767 if (sdp != NULL && ap == sdp->dtsd_action) {
768 if (sdp->dtsd_fmtdata != NULL) {
769 (void) dtrace_printf_format(dtp,
770 sdp->dtsd_fmtdata, fmt, maxfmt + 1);
771 strndx = dof_add_string(ddo, fmt);
773 strndx = 0; /* use dtad_arg instead */
775 if ((next = dt_list_next(next)) != NULL)
782 dofa[i].dofa_arg = strndx;
783 dofa[i].dofa_strtab = ddo->ddo_strsec;
785 dofa[i].dofa_arg = ap->dtad_arg;
786 dofa[i].dofa_strtab = DOF_SECIDX_NONE;
789 dofa[i].dofa_kind = ap->dtad_kind;
790 dofa[i].dofa_ntuple = ap->dtad_ntuple;
791 dofa[i].dofa_uarg = ap->dtad_uarg;
795 actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
796 sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
797 sizeof (dof_actdesc_t) * i);
801 * Now finally, add the DOF_SECT_ECBDESC referencing all the
802 * previously created sub-sections.
804 dofe.dofe_probes = probesec;
805 dofe.dofe_pred = prdsec;
806 dofe.dofe_actions = actsec;
808 dofe.dofe_uarg = edp->dted_uarg;
810 (void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
811 sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
815 * If any providers are user-defined, output DOF sections corresponding
816 * to the providers and the probes and arguments that they define.
818 if (flags & DTRACE_D_PROBES) {
819 for (pvp = dt_list_next(&dtp->dt_provlist);
820 pvp != NULL; pvp = dt_list_next(pvp))
821 dof_add_provider(ddo, pvp);
825 * If we're not stripping unloadable sections, generate compiler
826 * comments and any other unloadable miscellany.
828 if (!(flags & DTRACE_D_STRIP)) {
829 (void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
830 sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
831 (void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
832 sizeof (char), 0, 0, sizeof (struct utsname));
836 * Compute and fill in the appropriate values for the dof_hdr_t's
837 * dofh_secnum, dofh_loadsz, and dofh_filez values.
839 h.dofh_secnum = ddo->ddo_nsecs;
840 ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
841 assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
843 h.dofh_loadsz = ssize +
844 dt_buf_len(&ddo->ddo_ldata) +
845 dt_buf_len(&ddo->ddo_strs);
847 if (dt_buf_len(&ddo->ddo_udata) != 0) {
848 lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
849 h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
851 lsize = h.dofh_loadsz;
852 h.dofh_filesz = lsize;
856 * Set the global DOF_SECT_STRTAB's offset to be after the header,
857 * section headers, and other loadable data. Since we're going to
858 * iterate over the buffer data directly, we must check for errors.
860 if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
861 (void) dt_set_errno(dtp, i);
865 sp = dt_buf_ptr(&ddo->ddo_secs);
866 assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
868 sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
869 sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
872 * Now relocate all the other section headers by adding the appropriate
873 * delta to their respective dofs_offset values.
875 for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
876 if (i == ddo->ddo_strsec)
877 continue; /* already relocated above */
879 if (sp->dofs_flags & DOF_SECF_LOAD)
880 sp->dofs_offset += ssize;
882 sp->dofs_offset += lsize;
886 * Finally, assemble the complete in-memory DOF buffer by writing the
887 * header and then concatenating all our buffers. dt_buf_concat() will
888 * propagate any errors and cause dt_buf_claim() to return NULL.
890 dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
892 dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
893 dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
894 dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
895 dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
896 dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
898 return (dt_buf_claim(dtp, &dof));
902 dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
908 dtrace_getopt_dof(dtrace_hdl_t *dtp)
913 int i, nopts = 0, len = sizeof (dof_hdr_t) +
914 roundup(sizeof (dof_sec_t), sizeof (uint64_t));
916 for (i = 0; i < DTRACEOPT_MAX; i++) {
917 if (dtp->dt_options[i] != DTRACEOPT_UNSET)
921 len += sizeof (dof_optdesc_t) * nopts;
923 if ((dof = dt_zalloc(dtp, len)) == NULL ||
924 dof_hdr(dtp, DOF_VERSION, dof) != 0) {
929 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
930 dof->dofh_loadsz = len;
931 dof->dofh_filesz = len;
934 * Fill in the option section header...
936 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
937 sec->dofs_type = DOF_SECT_OPTDESC;
938 sec->dofs_align = sizeof (uint64_t);
939 sec->dofs_flags = DOF_SECF_LOAD;
940 sec->dofs_entsize = sizeof (dof_optdesc_t);
942 dofo = (dof_optdesc_t *)((uintptr_t)sec +
943 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
945 sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
946 sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
948 for (i = 0; i < DTRACEOPT_MAX; i++) {
949 if (dtp->dt_options[i] == DTRACEOPT_UNSET)
952 dofo->dofo_option = i;
953 dofo->dofo_strtab = DOF_SECIDX_NONE;
954 dofo->dofo_value = dtp->dt_options[i];
962 dtrace_geterr_dof(dtrace_hdl_t *dtp)
964 if (dtp->dt_errprog != NULL)
965 return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
967 (void) dt_set_errno(dtp, EDT_BADERROR);