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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 #include <sys/types.h>
30 #include <sys/sysmacros.h>
43 #include <dt_strtab.h>
44 #include <dt_program.h>
45 #include <dt_provider.h>
46 #include <dt_xlator.h>
50 dt_dof_init(dtrace_hdl_t *dtp)
52 dt_dof_t *ddo = &dtp->dt_dof;
56 ddo->ddo_strsec = DOF_SECIDX_NONE;
57 ddo->ddo_xlimport = NULL;
58 ddo->ddo_xlexport = NULL;
60 dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
61 dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
62 dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
63 dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
65 dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
66 dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
67 dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
68 dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
69 dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
71 dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
75 dt_dof_fini(dtrace_hdl_t *dtp)
77 dt_dof_t *ddo = &dtp->dt_dof;
79 dt_free(dtp, ddo->ddo_xlimport);
80 dt_free(dtp, ddo->ddo_xlexport);
82 dt_buf_destroy(dtp, &ddo->ddo_secs);
83 dt_buf_destroy(dtp, &ddo->ddo_strs);
84 dt_buf_destroy(dtp, &ddo->ddo_ldata);
85 dt_buf_destroy(dtp, &ddo->ddo_udata);
87 dt_buf_destroy(dtp, &ddo->ddo_probes);
88 dt_buf_destroy(dtp, &ddo->ddo_args);
89 dt_buf_destroy(dtp, &ddo->ddo_offs);
90 dt_buf_destroy(dtp, &ddo->ddo_enoffs);
91 dt_buf_destroy(dtp, &ddo->ddo_rels);
93 dt_buf_destroy(dtp, &ddo->ddo_xlms);
97 dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
99 dt_dof_t *ddo = &dtp->dt_dof;
100 uint_t i, nx = dtp->dt_xlatorid;
102 assert(ddo->ddo_hdl == dtp);
106 ddo->ddo_strsec = DOF_SECIDX_NONE;
108 dt_free(dtp, ddo->ddo_xlimport);
109 dt_free(dtp, ddo->ddo_xlexport);
111 ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
112 ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
114 if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
115 return (-1); /* errno is set for us */
117 for (i = 0; i < nx; i++) {
118 ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
119 ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
122 dt_buf_reset(dtp, &ddo->ddo_secs);
123 dt_buf_reset(dtp, &ddo->ddo_strs);
124 dt_buf_reset(dtp, &ddo->ddo_ldata);
125 dt_buf_reset(dtp, &ddo->ddo_udata);
127 dt_buf_reset(dtp, &ddo->ddo_probes);
128 dt_buf_reset(dtp, &ddo->ddo_args);
129 dt_buf_reset(dtp, &ddo->ddo_offs);
130 dt_buf_reset(dtp, &ddo->ddo_enoffs);
131 dt_buf_reset(dtp, &ddo->ddo_rels);
133 dt_buf_reset(dtp, &ddo->ddo_xlms);
138 * Add a loadable DOF section to the file using the specified data buffer and
139 * the specified DOF section attributes. DOF_SECF_LOAD must be set in flags.
140 * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
143 dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
144 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
146 dtrace_hdl_t *dtp = ddo->ddo_hdl;
150 s.dofs_align = align;
151 s.dofs_flags = flags | DOF_SECF_LOAD;
152 s.dofs_entsize = entsize;
153 s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
156 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
159 dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
161 return (ddo->ddo_nsecs++);
165 * Add an unloadable DOF section to the file using the specified data buffer
166 * and DOF section attributes. DOF_SECF_LOAD must *not* be set in flags.
167 * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
170 dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
171 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
173 dtrace_hdl_t *dtp = ddo->ddo_hdl;
177 s.dofs_align = align;
178 s.dofs_flags = flags & ~DOF_SECF_LOAD;
179 s.dofs_entsize = entsize;
180 s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
183 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
186 dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
188 return (ddo->ddo_nsecs++);
192 * Add a string to the global string table associated with the DOF. The offset
193 * of the string is returned as an index into the string table.
196 dof_add_string(dt_dof_t *ddo, const char *s)
198 dt_buf_t *bp = &ddo->ddo_strs;
199 dof_stridx_t i = dt_buf_len(bp);
201 if (i != 0 && (s == NULL || *s == '\0'))
202 return (0); /* string table has \0 at offset 0 */
204 dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
209 dof_attr(const dtrace_attribute_t *ap)
211 return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
215 dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
217 dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
224 dof_secidx_t strsec = DOF_SECIDX_NONE;
225 dof_secidx_t intsec = DOF_SECIDX_NONE;
226 dof_secidx_t hdrsec = DOF_SECIDX_NONE;
228 if (dp->dtdo_buf != NULL) {
229 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
230 DOF_SECT_DIF, sizeof (dif_instr_t), 0,
231 sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
234 if (dp->dtdo_inttab != NULL) {
235 dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
236 DOF_SECT_INTTAB, sizeof (uint64_t), 0,
237 sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
240 if (dp->dtdo_strtab != NULL) {
241 dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
242 DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
245 if (dp->dtdo_vartab != NULL) {
246 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
247 DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
248 sizeof (dtrace_difv_t) * dp->dtdo_varlen);
251 if (dp->dtdo_xlmtab != NULL) {
252 dof_xlref_t *xlt, *xlp;
255 xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
256 pnp = dp->dtdo_xlmtab;
259 * dtdo_xlmtab contains pointers to the translator members.
260 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
261 * The XLMEMBERS entries are in order by their dn_membid, so
262 * the member section offset is the population count of bits
263 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
265 for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
266 dt_node_t *dnp = *pnp++;
267 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
269 xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
270 xlp->dofxr_member = dt_popcb(
271 ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
272 xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
275 dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
276 sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
277 sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
281 * Copy the return type and the array of section indices that form the
282 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
284 assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
285 dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
286 bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
287 bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
289 hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
290 sizeof (dof_secidx_t), 0, 0,
291 sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
294 * Add any other sections related to dtrace_difo_t. These are not
295 * referenced in dof_difohdr_t because they are not used by emulation.
297 if (dp->dtdo_kreltab != NULL) {
298 relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
299 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
300 sizeof (dof_relodesc_t) * dp->dtdo_krelen);
303 * This code assumes the target of all relocations is the
304 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
305 * need relocation in the future this will need to change.
307 dofr.dofr_strtab = strsec;
308 dofr.dofr_relsec = relsec;
309 dofr.dofr_tgtsec = intsec;
311 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
312 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
315 if (dp->dtdo_ureltab != NULL) {
316 relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
317 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
318 sizeof (dof_relodesc_t) * dp->dtdo_urelen);
321 * This code assumes the target of all relocations is the
322 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
323 * need relocation in the future this will need to change.
325 dofr.dofr_strtab = strsec;
326 dofr.dofr_relsec = relsec;
327 dofr.dofr_tgtsec = intsec;
329 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
330 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
337 dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
339 dtrace_hdl_t *dtp = ddo->ddo_hdl;
340 dof_xlmember_t dofxm;
344 char buf[DT_TYPE_NAMELEN];
348 assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
349 xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
351 if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
352 return; /* translator has already been emitted */
354 dt_buf_reset(dtp, &ddo->ddo_xlms);
357 * Generate an array of dof_xlmember_t's into ddo_xlms. If we are
358 * importing the translator, add only those members referenced by the
359 * program and set the dofxm_difo reference of each member to NONE. If
360 * we're exporting the translator, add all members and a DIFO for each.
362 for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
363 if (type == DOF_SECT_XLIMPORT) {
364 if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
365 continue; /* member is not referenced */
366 dofxm.dofxm_difo = DOF_SECIDX_NONE;
368 dofxm.dofxm_difo = dof_add_difo(ddo,
369 dxp->dx_membdif[dnp->dn_membid]);
372 dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
373 dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
375 dt_buf_write(dtp, &ddo->ddo_xlms,
376 &dofxm, sizeof (dofxm), sizeof (uint32_t));
379 dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
380 sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
382 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
384 dofxl.dofxl_strtab = ddo->ddo_strsec;
385 dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
386 dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
387 dofxl.dofxl_argc = 1;
388 dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
389 dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
390 dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
392 xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
393 sizeof (uint32_t), 0, 0, sizeof (dofxl));
398 dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
400 dt_dof_t *ddo = data;
401 dtrace_hdl_t *dtp = ddo->ddo_hdl;
402 dt_probe_t *prp = idp->di_data;
406 dt_probe_instance_t *pip;
409 char buf[DT_TYPE_NAMELEN];
412 dofpr.dofpr_addr = 0;
413 dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
414 dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
416 for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
417 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
418 dnp->dn_type, buf, sizeof (buf)));
421 dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
423 for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
424 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
425 dnp->dn_type, buf, sizeof (buf)));
428 dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
430 for (i = 0; i < prp->pr_xargc; i++) {
431 dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
432 sizeof (uint8_t), sizeof (uint8_t));
435 dofpr.dofpr_nargc = prp->pr_nargc;
436 dofpr.dofpr_xargc = prp->pr_xargc;
437 dofpr.dofpr_pad1 = 0;
438 dofpr.dofpr_pad2 = 0;
440 for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
441 dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
444 dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
447 * There should be one probe offset or is-enabled probe offset
448 * or else this probe instance won't have been created. The
449 * kernel will reject DOF which has a probe with no offsets.
451 assert(pip->pi_noffs + pip->pi_nenoffs > 0);
454 dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
455 dofpr.dofpr_noffs = pip->pi_noffs;
456 dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
457 pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
459 dofpr.dofpr_enoffidx =
460 dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
461 dofpr.dofpr_nenoffs = pip->pi_nenoffs;
462 dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
463 pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
466 * If pi_rname isn't set, the relocation will be against the
467 * function name. If it is, the relocation will be against
468 * pi_rname. This will be used if the function is scoped
469 * locally so an alternate symbol is added for the purpose
470 * of this relocation.
472 if (pip->pi_rname[0] == '\0')
473 dofr.dofr_name = dofpr.dofpr_func;
475 dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
476 dofr.dofr_type = DOF_RELO_SETX;
477 dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
480 dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
481 sizeof (dofr), sizeof (uint64_t));
483 dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
484 sizeof (dofpr), sizeof (uint64_t));
491 dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
493 dtrace_hdl_t *dtp = ddo->ddo_hdl;
494 dof_provider_t dofpv;
501 if (pvp->pv_flags & DT_PROVIDER_IMPL) {
503 * ignore providers that are exported by dtrace(7D)
508 nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
509 dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
510 xr = 1; /* reserve dofs[0] for the provider itself */
513 * For each translator referenced by the provider (pv_xrefs), emit an
514 * exported translator section for it if one hasn't been created yet.
516 for (i = 0; i < pvp->pv_xrmax; i++) {
517 if (BT_TEST(pvp->pv_xrefs, i) &&
518 dtp->dt_xlatemode == DT_XL_DYNAMIC) {
519 dof_add_translator(ddo,
520 dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
521 dofs[xr++] = ddo->ddo_xlexport[i];
525 dt_buf_reset(dtp, &ddo->ddo_probes);
526 dt_buf_reset(dtp, &ddo->ddo_args);
527 dt_buf_reset(dtp, &ddo->ddo_offs);
528 dt_buf_reset(dtp, &ddo->ddo_enoffs);
529 dt_buf_reset(dtp, &ddo->ddo_rels);
531 (void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
533 if (dt_buf_len(&ddo->ddo_probes) == 0)
534 return (dt_set_errno(dtp, EDT_NOPROBES));
536 dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
537 sizeof (uint64_t), 0, sizeof (dof_probe_t),
538 dt_buf_len(&ddo->ddo_probes));
540 dt_buf_concat(dtp, &ddo->ddo_ldata,
541 &ddo->ddo_probes, sizeof (uint64_t));
543 dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
544 sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
546 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
548 dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
549 sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
551 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
553 if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
554 dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
555 DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
557 dofpv.dofpv_prenoffs = DOF_SECT_NONE;
560 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
562 dofpv.dofpv_strtab = ddo->ddo_strsec;
563 dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
565 dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
566 dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
567 dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
568 dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
569 dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
571 dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
572 sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
574 dofr.dofr_strtab = dofpv.dofpv_strtab;
575 dofr.dofr_tgtsec = dofpv.dofpv_probes;
576 dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
577 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
578 dt_buf_len(&ddo->ddo_rels));
580 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
582 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
583 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
585 if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
586 (void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
587 sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
588 sizeof (dof_secidx_t) * (nxr + 1));
595 dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
598 * If our config values cannot fit in a uint8_t, we can't generate a
599 * DOF header since the values won't fit. This can only happen if the
600 * user forcibly compiles a program with an artificial configuration.
602 if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
603 dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
604 dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
605 return (dt_set_errno(dtp, EOVERFLOW));
607 bzero(hp, sizeof (dof_hdr_t));
609 hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
610 hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
611 hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
612 hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
614 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
615 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
617 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
619 hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
620 hp->dofh_ident[DOF_ID_VERSION] = dofversion;
621 hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
622 hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
623 hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
625 hp->dofh_hdrsize = sizeof (dof_hdr_t);
626 hp->dofh_secsize = sizeof (dof_sec_t);
627 hp->dofh_secoff = sizeof (dof_hdr_t);
633 dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
635 dt_dof_t *ddo = &dtp->dt_dof;
637 const dtrace_ecbdesc_t *edp, *last;
638 const dtrace_probedesc_t *pdp;
639 const dtrace_actdesc_t *ap;
640 const dt_stmt_t *stp;
656 if (flags & ~DTRACE_D_MASK) {
657 (void) dt_set_errno(dtp, EINVAL);
661 flags |= dtp->dt_dflags;
663 if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
666 if (dt_dof_reset(dtp, pgp) != 0)
670 * Iterate through the statement list computing the maximum number of
671 * actions and the maximum format string for allocating local buffers.
673 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
674 stp != NULL; stp = dt_list_next(stp), last = edp) {
676 dtrace_stmtdesc_t *sdp = stp->ds_desc;
677 dtrace_actdesc_t *ap = sdp->dtsd_action;
679 if (sdp->dtsd_fmtdata != NULL) {
680 i = dtrace_printf_format(dtp,
681 sdp->dtsd_fmtdata, NULL, 0);
682 maxfmt = MAX(maxfmt, i);
685 if ((edp = sdp->dtsd_ecbdesc) == last)
686 continue; /* same ecb as previous statement */
688 for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
691 maxacts = MAX(maxacts, i);
694 dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
695 fmt = alloca(maxfmt + 1);
697 ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
698 (void) dof_add_string(ddo, "");
701 * If there are references to dynamic translators in the program, add
702 * an imported translator table entry for each referenced translator.
704 if (pgp->dp_xrefslen != 0) {
705 for (dxp = dt_list_next(&dtp->dt_xlators);
706 dxp != NULL; dxp = dt_list_next(dxp)) {
707 if (dxp->dx_id < pgp->dp_xrefslen &&
708 pgp->dp_xrefs[dxp->dx_id] != NULL)
709 dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
714 * Now iterate through the statement list, creating the DOF section
715 * headers and data for each one and adding them to our buffers.
717 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
718 stp != NULL; stp = dt_list_next(stp), last = edp) {
720 dof_secidx_t probesec = DOF_SECIDX_NONE;
721 dof_secidx_t prdsec = DOF_SECIDX_NONE;
722 dof_secidx_t actsec = DOF_SECIDX_NONE;
724 const dt_stmt_t *next = stp;
725 dtrace_stmtdesc_t *sdp = stp->ds_desc;
726 dof_stridx_t strndx = 0;
727 dof_probedesc_t dofp;
731 if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
732 continue; /* same ecb as previous statement */
734 pdp = &edp->dted_probe;
737 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
738 * and copy the probe description strings into the string table.
740 dofp.dofp_strtab = ddo->ddo_strsec;
741 dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
742 dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
743 dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
744 dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
745 dofp.dofp_id = pdp->dtpd_id;
747 probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
748 sizeof (dof_secidx_t), 0,
749 sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
752 * If there is a predicate DIFO associated with the ecbdesc,
753 * write out the DIFO sections and save the DIFO section index.
755 if (edp->dted_pred.dtpdd_difo != NULL)
756 prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
759 * Now iterate through the action list generating DIFOs as
760 * referenced therein and adding action descriptions to 'dofa'.
762 for (i = 0, ap = edp->dted_action;
763 ap != NULL; ap = ap->dtad_next, i++) {
765 if (ap->dtad_difo != NULL) {
767 dof_add_difo(ddo, ap->dtad_difo);
769 dofa[i].dofa_difo = DOF_SECIDX_NONE;
772 * If the first action in a statement has string data,
773 * add the string to the global string table. This can
774 * be due either to a printf() format string
775 * (dtsd_fmtdata) or a print() type string
778 if (sdp != NULL && ap == sdp->dtsd_action) {
779 if (sdp->dtsd_fmtdata != NULL) {
780 (void) dtrace_printf_format(dtp,
781 sdp->dtsd_fmtdata, fmt, maxfmt + 1);
782 strndx = dof_add_string(ddo, fmt);
783 } else if (sdp->dtsd_strdata != NULL) {
784 strndx = dof_add_string(ddo,
787 strndx = 0; /* use dtad_arg instead */
790 if ((next = dt_list_next(next)) != NULL)
797 dofa[i].dofa_arg = strndx;
798 dofa[i].dofa_strtab = ddo->ddo_strsec;
800 dofa[i].dofa_arg = ap->dtad_arg;
801 dofa[i].dofa_strtab = DOF_SECIDX_NONE;
804 dofa[i].dofa_kind = ap->dtad_kind;
805 dofa[i].dofa_ntuple = ap->dtad_ntuple;
806 dofa[i].dofa_uarg = ap->dtad_uarg;
810 actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
811 sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
812 sizeof (dof_actdesc_t) * i);
816 * Now finally, add the DOF_SECT_ECBDESC referencing all the
817 * previously created sub-sections.
819 dofe.dofe_probes = probesec;
820 dofe.dofe_pred = prdsec;
821 dofe.dofe_actions = actsec;
823 dofe.dofe_uarg = edp->dted_uarg;
825 (void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
826 sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
830 * If any providers are user-defined, output DOF sections corresponding
831 * to the providers and the probes and arguments that they define.
833 if (flags & DTRACE_D_PROBES) {
834 for (pvp = dt_list_next(&dtp->dt_provlist);
835 pvp != NULL; pvp = dt_list_next(pvp)) {
836 if (dof_add_provider(ddo, pvp) != 0)
842 * If we're not stripping unloadable sections, generate compiler
843 * comments and any other unloadable miscellany.
845 if (!(flags & DTRACE_D_STRIP)) {
846 (void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
847 sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
848 (void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
849 sizeof (char), 0, 0, sizeof (struct utsname));
853 * Compute and fill in the appropriate values for the dof_hdr_t's
854 * dofh_secnum, dofh_loadsz, and dofh_filez values.
856 h.dofh_secnum = ddo->ddo_nsecs;
857 ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
859 h.dofh_loadsz = ssize +
860 dt_buf_len(&ddo->ddo_ldata) +
861 dt_buf_len(&ddo->ddo_strs);
863 if (dt_buf_len(&ddo->ddo_udata) != 0) {
864 lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
865 h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
867 lsize = h.dofh_loadsz;
868 h.dofh_filesz = lsize;
872 * Set the global DOF_SECT_STRTAB's offset to be after the header,
873 * section headers, and other loadable data. Since we're going to
874 * iterate over the buffer data directly, we must check for errors.
876 if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
877 (void) dt_set_errno(dtp, i);
881 sp = dt_buf_ptr(&ddo->ddo_secs);
882 assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
883 assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
885 sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
886 sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
889 * Now relocate all the other section headers by adding the appropriate
890 * delta to their respective dofs_offset values.
892 for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
893 if (i == ddo->ddo_strsec)
894 continue; /* already relocated above */
896 if (sp->dofs_flags & DOF_SECF_LOAD)
897 sp->dofs_offset += ssize;
899 sp->dofs_offset += lsize;
903 * Finally, assemble the complete in-memory DOF buffer by writing the
904 * header and then concatenating all our buffers. dt_buf_concat() will
905 * propagate any errors and cause dt_buf_claim() to return NULL.
907 dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
909 dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
910 dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
911 dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
912 dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
913 dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
915 return (dt_buf_claim(dtp, &dof));
919 dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
925 dtrace_getopt_dof(dtrace_hdl_t *dtp)
930 int i, nopts = 0, len = sizeof (dof_hdr_t) +
931 roundup(sizeof (dof_sec_t), sizeof (uint64_t));
933 for (i = 0; i < DTRACEOPT_MAX; i++) {
934 if (dtp->dt_options[i] != DTRACEOPT_UNSET)
938 len += sizeof (dof_optdesc_t) * nopts;
940 if ((dof = dt_zalloc(dtp, len)) == NULL ||
941 dof_hdr(dtp, DOF_VERSION, dof) != 0) {
946 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
947 dof->dofh_loadsz = len;
948 dof->dofh_filesz = len;
951 * Fill in the option section header...
953 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
954 sec->dofs_type = DOF_SECT_OPTDESC;
955 sec->dofs_align = sizeof (uint64_t);
956 sec->dofs_flags = DOF_SECF_LOAD;
957 sec->dofs_entsize = sizeof (dof_optdesc_t);
959 dofo = (dof_optdesc_t *)((uintptr_t)sec +
960 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
962 sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
963 sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
965 for (i = 0; i < DTRACEOPT_MAX; i++) {
966 if (dtp->dt_options[i] == DTRACEOPT_UNSET)
969 dofo->dofo_option = i;
970 dofo->dofo_strtab = DOF_SECIDX_NONE;
971 dofo->dofo_value = dtp->dt_options[i];
979 dtrace_geterr_dof(dtrace_hdl_t *dtp)
981 if (dtp->dt_errprog != NULL)
982 return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
984 (void) dt_set_errno(dtp, EDT_BADERROR);