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.
26 #include <sys/types.h>
28 #include <sys/sysmacros.h>
41 #include <dt_strtab.h>
42 #include <dt_program.h>
43 #include <dt_provider.h>
44 #include <dt_xlator.h>
48 dt_dof_init(dtrace_hdl_t *dtp)
50 dt_dof_t *ddo = &dtp->dt_dof;
54 ddo->ddo_strsec = DOF_SECIDX_NONE;
55 ddo->ddo_xlimport = NULL;
56 ddo->ddo_xlexport = NULL;
58 dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0);
59 dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0);
60 dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0);
61 dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0);
63 dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0);
64 dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0);
65 dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0);
66 dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0);
67 dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0);
69 dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0);
73 dt_dof_fini(dtrace_hdl_t *dtp)
75 dt_dof_t *ddo = &dtp->dt_dof;
77 dt_free(dtp, ddo->ddo_xlimport);
78 dt_free(dtp, ddo->ddo_xlexport);
80 dt_buf_destroy(dtp, &ddo->ddo_secs);
81 dt_buf_destroy(dtp, &ddo->ddo_strs);
82 dt_buf_destroy(dtp, &ddo->ddo_ldata);
83 dt_buf_destroy(dtp, &ddo->ddo_udata);
85 dt_buf_destroy(dtp, &ddo->ddo_probes);
86 dt_buf_destroy(dtp, &ddo->ddo_args);
87 dt_buf_destroy(dtp, &ddo->ddo_offs);
88 dt_buf_destroy(dtp, &ddo->ddo_enoffs);
89 dt_buf_destroy(dtp, &ddo->ddo_rels);
91 dt_buf_destroy(dtp, &ddo->ddo_xlms);
95 dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp)
97 dt_dof_t *ddo = &dtp->dt_dof;
98 uint_t i, nx = dtp->dt_xlatorid;
100 assert(ddo->ddo_hdl == dtp);
104 ddo->ddo_strsec = DOF_SECIDX_NONE;
106 dt_free(dtp, ddo->ddo_xlimport);
107 dt_free(dtp, ddo->ddo_xlexport);
109 ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
110 ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx);
112 if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL))
113 return (-1); /* errno is set for us */
115 for (i = 0; i < nx; i++) {
116 ddo->ddo_xlimport[i] = DOF_SECIDX_NONE;
117 ddo->ddo_xlexport[i] = DOF_SECIDX_NONE;
120 dt_buf_reset(dtp, &ddo->ddo_secs);
121 dt_buf_reset(dtp, &ddo->ddo_strs);
122 dt_buf_reset(dtp, &ddo->ddo_ldata);
123 dt_buf_reset(dtp, &ddo->ddo_udata);
125 dt_buf_reset(dtp, &ddo->ddo_probes);
126 dt_buf_reset(dtp, &ddo->ddo_args);
127 dt_buf_reset(dtp, &ddo->ddo_offs);
128 dt_buf_reset(dtp, &ddo->ddo_enoffs);
129 dt_buf_reset(dtp, &ddo->ddo_rels);
131 dt_buf_reset(dtp, &ddo->ddo_xlms);
136 * Add a loadable DOF section to the file using the specified data buffer and
137 * the specified DOF section attributes. DOF_SECF_LOAD must be set in flags.
138 * If 'data' is NULL, the caller is responsible for manipulating the ldata buf.
141 dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type,
142 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
144 dtrace_hdl_t *dtp = ddo->ddo_hdl;
148 s.dofs_align = align;
149 s.dofs_flags = flags | DOF_SECF_LOAD;
150 s.dofs_entsize = entsize;
151 s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align);
154 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
157 dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align);
159 return (ddo->ddo_nsecs++);
163 * Add an unloadable DOF section to the file using the specified data buffer
164 * and DOF section attributes. DOF_SECF_LOAD must *not* be set in flags.
165 * If 'data' is NULL, the caller is responsible for manipulating the udata buf.
168 dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type,
169 uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size)
171 dtrace_hdl_t *dtp = ddo->ddo_hdl;
175 s.dofs_align = align;
176 s.dofs_flags = flags & ~DOF_SECF_LOAD;
177 s.dofs_entsize = entsize;
178 s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align);
181 dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t));
184 dt_buf_write(dtp, &ddo->ddo_udata, data, size, align);
186 return (ddo->ddo_nsecs++);
190 * Add a string to the global string table associated with the DOF. The offset
191 * of the string is returned as an index into the string table.
194 dof_add_string(dt_dof_t *ddo, const char *s)
196 dt_buf_t *bp = &ddo->ddo_strs;
197 dof_stridx_t i = dt_buf_len(bp);
199 if (i != 0 && (s == NULL || *s == '\0'))
200 return (0); /* string table has \0 at offset 0 */
202 dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char));
207 dof_attr(const dtrace_attribute_t *ap)
209 return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class));
213 dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp)
215 dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */
222 dof_secidx_t strsec = DOF_SECIDX_NONE;
223 dof_secidx_t intsec = DOF_SECIDX_NONE;
224 dof_secidx_t hdrsec = DOF_SECIDX_NONE;
226 if (dp->dtdo_buf != NULL) {
227 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf,
228 DOF_SECT_DIF, sizeof (dif_instr_t), 0,
229 sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len);
232 if (dp->dtdo_inttab != NULL) {
233 dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab,
234 DOF_SECT_INTTAB, sizeof (uint64_t), 0,
235 sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen);
238 if (dp->dtdo_strtab != NULL) {
239 dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab,
240 DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen);
243 if (dp->dtdo_vartab != NULL) {
244 dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab,
245 DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t),
246 sizeof (dtrace_difv_t) * dp->dtdo_varlen);
249 if (dp->dtdo_xlmtab != NULL) {
250 dof_xlref_t *xlt, *xlp;
253 xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
254 pnp = dp->dtdo_xlmtab;
257 * dtdo_xlmtab contains pointers to the translator members.
258 * The translator itself is in sect ddo_xlimport[dxp->dx_id].
259 * The XLMEMBERS entries are in order by their dn_membid, so
260 * the member section offset is the population count of bits
261 * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid.
263 for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) {
264 dt_node_t *dnp = *pnp++;
265 dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator;
267 xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id];
268 xlp->dofxr_member = dt_popcb(
269 ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid);
270 xlp->dofxr_argn = (uint32_t)dxp->dx_arg;
273 dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB,
274 sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t),
275 sizeof (dof_xlref_t) * dp->dtdo_xlmlen);
279 * Copy the return type and the array of section indices that form the
280 * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR.
282 assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0]));
283 dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs));
284 bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t));
285 bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs);
287 hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR,
288 sizeof (dof_secidx_t), 0, 0,
289 sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs);
292 * Add any other sections related to dtrace_difo_t. These are not
293 * referenced in dof_difohdr_t because they are not used by emulation.
295 if (dp->dtdo_kreltab != NULL) {
296 relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB,
297 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
298 sizeof (dof_relodesc_t) * dp->dtdo_krelen);
301 * This code assumes the target of all relocations is the
302 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
303 * need relocation in the future this will need to change.
305 dofr.dofr_strtab = strsec;
306 dofr.dofr_relsec = relsec;
307 dofr.dofr_tgtsec = intsec;
309 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR,
310 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
313 if (dp->dtdo_ureltab != NULL) {
314 relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB,
315 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
316 sizeof (dof_relodesc_t) * dp->dtdo_urelen);
319 * This code assumes the target of all relocations is the
320 * integer table 'intsec' (DOF_SECT_INTTAB). If other sections
321 * need relocation in the future this will need to change.
323 dofr.dofr_strtab = strsec;
324 dofr.dofr_relsec = relsec;
325 dofr.dofr_tgtsec = intsec;
327 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
328 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
335 dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type)
337 dtrace_hdl_t *dtp = ddo->ddo_hdl;
338 dof_xlmember_t dofxm;
342 char buf[DT_TYPE_NAMELEN];
346 assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT);
347 xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport;
349 if (xst[dxp->dx_id] != DOF_SECIDX_NONE)
350 return; /* translator has already been emitted */
352 dt_buf_reset(dtp, &ddo->ddo_xlms);
355 * Generate an array of dof_xlmember_t's into ddo_xlms. If we are
356 * importing the translator, add only those members referenced by the
357 * program and set the dofxm_difo reference of each member to NONE. If
358 * we're exporting the translator, add all members and a DIFO for each.
360 for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) {
361 if (type == DOF_SECT_XLIMPORT) {
362 if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i))
363 continue; /* member is not referenced */
364 dofxm.dofxm_difo = DOF_SECIDX_NONE;
366 dofxm.dofxm_difo = dof_add_difo(ddo,
367 dxp->dx_membdif[dnp->dn_membid]);
370 dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname);
371 dt_node_diftype(dtp, dnp, &dofxm.dofxm_type);
373 dt_buf_write(dtp, &ddo->ddo_xlms,
374 &dofxm, sizeof (dofxm), sizeof (uint32_t));
377 dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS,
378 sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms));
380 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t));
382 dofxl.dofxl_strtab = ddo->ddo_strsec;
383 dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name(
384 dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf)));
385 dofxl.dofxl_argc = 1;
386 dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name(
387 dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf)));
388 dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr);
390 xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type,
391 sizeof (uint32_t), 0, 0, sizeof (dofxl));
396 dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data)
398 dt_dof_t *ddo = data;
399 dtrace_hdl_t *dtp = ddo->ddo_hdl;
400 dt_probe_t *prp = idp->di_data;
404 dt_probe_instance_t *pip;
407 char buf[DT_TYPE_NAMELEN];
410 dofpr.dofpr_addr = 0;
411 dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name);
412 dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs);
414 for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) {
415 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
416 dnp->dn_type, buf, sizeof (buf)));
419 dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs);
421 for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) {
422 (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp,
423 dnp->dn_type, buf, sizeof (buf)));
426 dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t);
428 for (i = 0; i < prp->pr_xargc; i++) {
429 dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i],
430 sizeof (uint8_t), sizeof (uint8_t));
433 dofpr.dofpr_nargc = prp->pr_nargc;
434 dofpr.dofpr_xargc = prp->pr_xargc;
435 dofpr.dofpr_pad1 = 0;
436 dofpr.dofpr_pad2 = 0;
438 for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) {
439 dt_dprintf("adding probe for %s:%s\n", pip->pi_fname,
442 dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname);
445 * There should be one probe offset or is-enabled probe offset
446 * or else this probe instance won't have been created. The
447 * kernel will reject DOF which has a probe with no offsets.
449 assert(pip->pi_noffs + pip->pi_nenoffs > 0);
452 dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t);
453 dofpr.dofpr_noffs = pip->pi_noffs;
454 dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs,
455 pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t));
457 dofpr.dofpr_enoffidx =
458 dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t);
459 dofpr.dofpr_nenoffs = pip->pi_nenoffs;
460 dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs,
461 pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t));
464 * If pi_rname isn't set, the relocation will be against the
465 * function name. If it is, the relocation will be against
466 * pi_rname. This will be used if the function is scoped
467 * locally so an alternate symbol is added for the purpose
468 * of this relocation.
470 if (pip->pi_rname[0] == '\0')
471 dofr.dofr_name = dofpr.dofpr_func;
473 dofr.dofr_name = dof_add_string(ddo, pip->pi_rname);
474 dofr.dofr_type = DOF_RELO_SETX;
475 dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes);
478 dt_buf_write(dtp, &ddo->ddo_rels, &dofr,
479 sizeof (dofr), sizeof (uint64_t));
481 dt_buf_write(dtp, &ddo->ddo_probes, &dofpr,
482 sizeof (dofpr), sizeof (uint64_t));
489 dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp)
491 dtrace_hdl_t *dtp = ddo->ddo_hdl;
492 dof_provider_t dofpv;
499 if (pvp->pv_flags & DT_PROVIDER_IMPL)
500 return; /* ignore providers that are exported by dtrace(7D) */
502 nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax);
503 dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1));
504 xr = 1; /* reserve dofs[0] for the provider itself */
507 * For each translator referenced by the provider (pv_xrefs), emit an
508 * exported translator section for it if one hasn't been created yet.
510 for (i = 0; i < pvp->pv_xrmax; i++) {
511 if (BT_TEST(pvp->pv_xrefs, i) &&
512 dtp->dt_xlatemode == DT_XL_DYNAMIC) {
513 dof_add_translator(ddo,
514 dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT);
515 dofs[xr++] = ddo->ddo_xlexport[i];
519 dt_buf_reset(dtp, &ddo->ddo_probes);
520 dt_buf_reset(dtp, &ddo->ddo_args);
521 dt_buf_reset(dtp, &ddo->ddo_offs);
522 dt_buf_reset(dtp, &ddo->ddo_enoffs);
523 dt_buf_reset(dtp, &ddo->ddo_rels);
525 (void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo);
527 dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES,
528 sizeof (uint64_t), 0, sizeof (dof_probe_t),
529 dt_buf_len(&ddo->ddo_probes));
531 dt_buf_concat(dtp, &ddo->ddo_ldata,
532 &ddo->ddo_probes, sizeof (uint64_t));
534 dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS,
535 sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args));
537 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t));
539 dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS,
540 sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs));
542 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t));
544 if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) {
545 dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL,
546 DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz);
548 dofpv.dofpv_prenoffs = DOF_SECT_NONE;
551 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t));
553 dofpv.dofpv_strtab = ddo->ddo_strsec;
554 dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name);
556 dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider);
557 dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod);
558 dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func);
559 dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name);
560 dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args);
562 dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER,
563 sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t));
565 dofr.dofr_strtab = dofpv.dofpv_strtab;
566 dofr.dofr_tgtsec = dofpv.dofpv_probes;
567 dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB,
568 sizeof (uint64_t), 0, sizeof (dof_relodesc_t),
569 dt_buf_len(&ddo->ddo_rels));
571 dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t));
573 (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR,
574 sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t));
576 if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) {
577 (void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT,
578 sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t),
579 sizeof (dof_secidx_t) * (nxr + 1));
584 dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp)
587 * If our config values cannot fit in a uint8_t, we can't generate a
588 * DOF header since the values won't fit. This can only happen if the
589 * user forcibly compiles a program with an artificial configuration.
591 if (dtp->dt_conf.dtc_difversion > UINT8_MAX ||
592 dtp->dt_conf.dtc_difintregs > UINT8_MAX ||
593 dtp->dt_conf.dtc_diftupregs > UINT8_MAX)
594 return (dt_set_errno(dtp, EOVERFLOW));
596 bzero(hp, sizeof (dof_hdr_t));
598 hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0;
599 hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1;
600 hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2;
601 hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3;
603 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
604 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64;
606 hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32;
608 hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE;
609 hp->dofh_ident[DOF_ID_VERSION] = dofversion;
610 hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion;
611 hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs;
612 hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs;
614 hp->dofh_hdrsize = sizeof (dof_hdr_t);
615 hp->dofh_secsize = sizeof (dof_sec_t);
616 hp->dofh_secoff = sizeof (dof_hdr_t);
622 dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags)
624 dt_dof_t *ddo = &dtp->dt_dof;
626 const dtrace_ecbdesc_t *edp, *last;
627 const dtrace_probedesc_t *pdp;
628 const dtrace_actdesc_t *ap;
629 const dt_stmt_t *stp;
645 if (flags & ~DTRACE_D_MASK) {
646 (void) dt_set_errno(dtp, EINVAL);
650 flags |= dtp->dt_dflags;
652 if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0)
655 if (dt_dof_reset(dtp, pgp) != 0)
659 * Iterate through the statement list computing the maximum number of
660 * actions and the maximum format string for allocating local buffers.
662 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
663 stp != NULL; stp = dt_list_next(stp), last = edp) {
665 dtrace_stmtdesc_t *sdp = stp->ds_desc;
666 dtrace_actdesc_t *ap = sdp->dtsd_action;
668 if (sdp->dtsd_fmtdata != NULL) {
669 i = dtrace_printf_format(dtp,
670 sdp->dtsd_fmtdata, NULL, 0);
671 maxfmt = MAX(maxfmt, i);
674 if ((edp = sdp->dtsd_ecbdesc) == last)
675 continue; /* same ecb as previous statement */
677 for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next)
680 maxacts = MAX(maxacts, i);
683 dofa = alloca(sizeof (dof_actdesc_t) * maxacts);
684 fmt = alloca(maxfmt + 1);
686 ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0);
687 (void) dof_add_string(ddo, "");
690 * If there are references to dynamic translators in the program, add
691 * an imported translator table entry for each referenced translator.
693 if (pgp->dp_xrefslen != 0) {
694 for (dxp = dt_list_next(&dtp->dt_xlators);
695 dxp != NULL; dxp = dt_list_next(dxp)) {
696 if (dxp->dx_id < pgp->dp_xrefslen &&
697 pgp->dp_xrefs[dxp->dx_id] != NULL)
698 dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT);
703 * Now iterate through the statement list, creating the DOF section
704 * headers and data for each one and adding them to our buffers.
706 for (last = NULL, stp = dt_list_next(&pgp->dp_stmts);
707 stp != NULL; stp = dt_list_next(stp), last = edp) {
709 dof_secidx_t probesec = DOF_SECIDX_NONE;
710 dof_secidx_t prdsec = DOF_SECIDX_NONE;
711 dof_secidx_t actsec = DOF_SECIDX_NONE;
713 const dt_stmt_t *next = stp;
714 dtrace_stmtdesc_t *sdp = stp->ds_desc;
715 dof_stridx_t strndx = 0;
716 dof_probedesc_t dofp;
720 if ((edp = stp->ds_desc->dtsd_ecbdesc) == last)
721 continue; /* same ecb as previous statement */
723 pdp = &edp->dted_probe;
726 * Add a DOF_SECT_PROBEDESC for the ECB's probe description,
727 * and copy the probe description strings into the string table.
729 dofp.dofp_strtab = ddo->ddo_strsec;
730 dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider);
731 dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod);
732 dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func);
733 dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name);
734 dofp.dofp_id = pdp->dtpd_id;
736 probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC,
737 sizeof (dof_secidx_t), 0,
738 sizeof (dof_probedesc_t), sizeof (dof_probedesc_t));
741 * If there is a predicate DIFO associated with the ecbdesc,
742 * write out the DIFO sections and save the DIFO section index.
744 if (edp->dted_pred.dtpdd_difo != NULL)
745 prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo);
748 * Now iterate through the action list generating DIFOs as
749 * referenced therein and adding action descriptions to 'dofa'.
751 for (i = 0, ap = edp->dted_action;
752 ap != NULL; ap = ap->dtad_next, i++) {
754 if (ap->dtad_difo != NULL) {
756 dof_add_difo(ddo, ap->dtad_difo);
758 dofa[i].dofa_difo = DOF_SECIDX_NONE;
761 * If the first action in a statement has format data,
762 * add the format string to the global string table.
764 if (sdp != NULL && ap == sdp->dtsd_action) {
765 if (sdp->dtsd_fmtdata != NULL) {
766 (void) dtrace_printf_format(dtp,
767 sdp->dtsd_fmtdata, fmt, maxfmt + 1);
768 strndx = dof_add_string(ddo, fmt);
770 strndx = 0; /* use dtad_arg instead */
772 if ((next = dt_list_next(next)) != NULL)
779 dofa[i].dofa_arg = strndx;
780 dofa[i].dofa_strtab = ddo->ddo_strsec;
782 dofa[i].dofa_arg = ap->dtad_arg;
783 dofa[i].dofa_strtab = DOF_SECIDX_NONE;
786 dofa[i].dofa_kind = ap->dtad_kind;
787 dofa[i].dofa_ntuple = ap->dtad_ntuple;
788 dofa[i].dofa_uarg = ap->dtad_uarg;
792 actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC,
793 sizeof (uint64_t), 0, sizeof (dof_actdesc_t),
794 sizeof (dof_actdesc_t) * i);
798 * Now finally, add the DOF_SECT_ECBDESC referencing all the
799 * previously created sub-sections.
801 dofe.dofe_probes = probesec;
802 dofe.dofe_pred = prdsec;
803 dofe.dofe_actions = actsec;
805 dofe.dofe_uarg = edp->dted_uarg;
807 (void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC,
808 sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t));
812 * If any providers are user-defined, output DOF sections corresponding
813 * to the providers and the probes and arguments that they define.
815 if (flags & DTRACE_D_PROBES) {
816 for (pvp = dt_list_next(&dtp->dt_provlist);
817 pvp != NULL; pvp = dt_list_next(pvp))
818 dof_add_provider(ddo, pvp);
822 * If we're not stripping unloadable sections, generate compiler
823 * comments and any other unloadable miscellany.
825 if (!(flags & DTRACE_D_STRIP)) {
826 (void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS,
827 sizeof (char), 0, 0, strlen(_dtrace_version) + 1);
828 (void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME,
829 sizeof (char), 0, 0, sizeof (struct utsname));
833 * Compute and fill in the appropriate values for the dof_hdr_t's
834 * dofh_secnum, dofh_loadsz, and dofh_filez values.
836 h.dofh_secnum = ddo->ddo_nsecs;
837 ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs);
839 h.dofh_loadsz = ssize +
840 dt_buf_len(&ddo->ddo_ldata) +
841 dt_buf_len(&ddo->ddo_strs);
843 if (dt_buf_len(&ddo->ddo_udata) != 0) {
844 lsize = roundup(h.dofh_loadsz, sizeof (uint64_t));
845 h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata);
847 lsize = h.dofh_loadsz;
848 h.dofh_filesz = lsize;
852 * Set the global DOF_SECT_STRTAB's offset to be after the header,
853 * section headers, and other loadable data. Since we're going to
854 * iterate over the buffer data directly, we must check for errors.
856 if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) {
857 (void) dt_set_errno(dtp, i);
861 sp = dt_buf_ptr(&ddo->ddo_secs);
862 assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB);
863 assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs);
865 sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata);
866 sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs);
869 * Now relocate all the other section headers by adding the appropriate
870 * delta to their respective dofs_offset values.
872 for (i = 0; i < ddo->ddo_nsecs; i++, sp++) {
873 if (i == ddo->ddo_strsec)
874 continue; /* already relocated above */
876 if (sp->dofs_flags & DOF_SECF_LOAD)
877 sp->dofs_offset += ssize;
879 sp->dofs_offset += lsize;
883 * Finally, assemble the complete in-memory DOF buffer by writing the
884 * header and then concatenating all our buffers. dt_buf_concat() will
885 * propagate any errors and cause dt_buf_claim() to return NULL.
887 dt_buf_create(dtp, &dof, "dof", h.dofh_filesz);
889 dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t));
890 dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t));
891 dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t));
892 dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char));
893 dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t));
895 return (dt_buf_claim(dtp, &dof));
899 dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof)
905 dtrace_getopt_dof(dtrace_hdl_t *dtp)
910 int i, nopts = 0, len = sizeof (dof_hdr_t) +
911 roundup(sizeof (dof_sec_t), sizeof (uint64_t));
913 for (i = 0; i < DTRACEOPT_MAX; i++) {
914 if (dtp->dt_options[i] != DTRACEOPT_UNSET)
918 len += sizeof (dof_optdesc_t) * nopts;
920 if ((dof = dt_zalloc(dtp, len)) == NULL ||
921 dof_hdr(dtp, DOF_VERSION, dof) != 0) {
926 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */
927 dof->dofh_loadsz = len;
928 dof->dofh_filesz = len;
931 * Fill in the option section header...
933 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t));
934 sec->dofs_type = DOF_SECT_OPTDESC;
935 sec->dofs_align = sizeof (uint64_t);
936 sec->dofs_flags = DOF_SECF_LOAD;
937 sec->dofs_entsize = sizeof (dof_optdesc_t);
939 dofo = (dof_optdesc_t *)((uintptr_t)sec +
940 roundup(sizeof (dof_sec_t), sizeof (uint64_t)));
942 sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof;
943 sec->dofs_size = sizeof (dof_optdesc_t) * nopts;
945 for (i = 0; i < DTRACEOPT_MAX; i++) {
946 if (dtp->dt_options[i] == DTRACEOPT_UNSET)
949 dofo->dofo_option = i;
950 dofo->dofo_strtab = DOF_SECIDX_NONE;
951 dofo->dofo_value = dtp->dt_options[i];
959 dtrace_geterr_dof(dtrace_hdl_t *dtp)
961 if (dtp->dt_errprog != NULL)
962 return (dtrace_dof_create(dtp, dtp->dt_errprog, 0));
964 (void) dt_set_errno(dtp, EDT_BADERROR);