diff --git a/cddl/contrib/opensolaris/lib/libdtrace/common/dt_dof.c b/cddl/contrib/opensolaris/lib/libdtrace/common/dt_dof.c index 075001157618..f35a386c5d7d 100644 --- a/cddl/contrib/opensolaris/lib/libdtrace/common/dt_dof.c +++ b/cddl/contrib/opensolaris/lib/libdtrace/common/dt_dof.c @@ -1,965 +1,969 @@ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" #include +#if defined(sun) #include +#endif #include +#if defined(sun) #include +#endif #include #include #include #include #include #include #include #include #include #include void dt_dof_init(dtrace_hdl_t *dtp) { dt_dof_t *ddo = &dtp->dt_dof; ddo->ddo_hdl = dtp; ddo->ddo_nsecs = 0; ddo->ddo_strsec = DOF_SECIDX_NONE; ddo->ddo_xlimport = NULL; ddo->ddo_xlexport = NULL; dt_buf_create(dtp, &ddo->ddo_secs, "section headers", 0); dt_buf_create(dtp, &ddo->ddo_strs, "string table", 0); dt_buf_create(dtp, &ddo->ddo_ldata, "loadable data", 0); dt_buf_create(dtp, &ddo->ddo_udata, "unloadable data", 0); dt_buf_create(dtp, &ddo->ddo_probes, "probe data", 0); dt_buf_create(dtp, &ddo->ddo_args, "probe args", 0); dt_buf_create(dtp, &ddo->ddo_offs, "probe offs", 0); dt_buf_create(dtp, &ddo->ddo_enoffs, "probe is-enabled offs", 0); dt_buf_create(dtp, &ddo->ddo_rels, "probe rels", 0); dt_buf_create(dtp, &ddo->ddo_xlms, "xlate members", 0); } void dt_dof_fini(dtrace_hdl_t *dtp) { dt_dof_t *ddo = &dtp->dt_dof; dt_free(dtp, ddo->ddo_xlimport); dt_free(dtp, ddo->ddo_xlexport); dt_buf_destroy(dtp, &ddo->ddo_secs); dt_buf_destroy(dtp, &ddo->ddo_strs); dt_buf_destroy(dtp, &ddo->ddo_ldata); dt_buf_destroy(dtp, &ddo->ddo_udata); dt_buf_destroy(dtp, &ddo->ddo_probes); dt_buf_destroy(dtp, &ddo->ddo_args); dt_buf_destroy(dtp, &ddo->ddo_offs); dt_buf_destroy(dtp, &ddo->ddo_enoffs); dt_buf_destroy(dtp, &ddo->ddo_rels); dt_buf_destroy(dtp, &ddo->ddo_xlms); } static int dt_dof_reset(dtrace_hdl_t *dtp, dtrace_prog_t *pgp) { dt_dof_t *ddo = &dtp->dt_dof; uint_t i, nx = dtp->dt_xlatorid; assert(ddo->ddo_hdl == dtp); ddo->ddo_pgp = pgp; ddo->ddo_nsecs = 0; ddo->ddo_strsec = DOF_SECIDX_NONE; dt_free(dtp, ddo->ddo_xlimport); dt_free(dtp, ddo->ddo_xlexport); ddo->ddo_xlimport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx); ddo->ddo_xlexport = dt_alloc(dtp, sizeof (dof_secidx_t) * nx); if (nx != 0 && (ddo->ddo_xlimport == NULL || ddo->ddo_xlexport == NULL)) return (-1); /* errno is set for us */ for (i = 0; i < nx; i++) { ddo->ddo_xlimport[i] = DOF_SECIDX_NONE; ddo->ddo_xlexport[i] = DOF_SECIDX_NONE; } dt_buf_reset(dtp, &ddo->ddo_secs); dt_buf_reset(dtp, &ddo->ddo_strs); dt_buf_reset(dtp, &ddo->ddo_ldata); dt_buf_reset(dtp, &ddo->ddo_udata); dt_buf_reset(dtp, &ddo->ddo_probes); dt_buf_reset(dtp, &ddo->ddo_args); dt_buf_reset(dtp, &ddo->ddo_offs); dt_buf_reset(dtp, &ddo->ddo_enoffs); dt_buf_reset(dtp, &ddo->ddo_rels); dt_buf_reset(dtp, &ddo->ddo_xlms); return (0); } /* * Add a loadable DOF section to the file using the specified data buffer and * the specified DOF section attributes. DOF_SECF_LOAD must be set in flags. * If 'data' is NULL, the caller is responsible for manipulating the ldata buf. */ static dof_secidx_t dof_add_lsect(dt_dof_t *ddo, const void *data, uint32_t type, uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size) { dtrace_hdl_t *dtp = ddo->ddo_hdl; dof_sec_t s; s.dofs_type = type; s.dofs_align = align; s.dofs_flags = flags | DOF_SECF_LOAD; s.dofs_entsize = entsize; s.dofs_offset = dt_buf_offset(&ddo->ddo_ldata, align); s.dofs_size = size; dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t)); if (data != NULL) dt_buf_write(dtp, &ddo->ddo_ldata, data, size, align); return (ddo->ddo_nsecs++); } /* * Add an unloadable DOF section to the file using the specified data buffer * and DOF section attributes. DOF_SECF_LOAD must *not* be set in flags. * If 'data' is NULL, the caller is responsible for manipulating the udata buf. */ static dof_secidx_t dof_add_usect(dt_dof_t *ddo, const void *data, uint32_t type, uint32_t align, uint32_t flags, uint32_t entsize, uint64_t size) { dtrace_hdl_t *dtp = ddo->ddo_hdl; dof_sec_t s; s.dofs_type = type; s.dofs_align = align; s.dofs_flags = flags & ~DOF_SECF_LOAD; s.dofs_entsize = entsize; s.dofs_offset = dt_buf_offset(&ddo->ddo_udata, align); s.dofs_size = size; dt_buf_write(dtp, &ddo->ddo_secs, &s, sizeof (s), sizeof (uint64_t)); if (data != NULL) dt_buf_write(dtp, &ddo->ddo_udata, data, size, align); return (ddo->ddo_nsecs++); } /* * Add a string to the global string table associated with the DOF. The offset * of the string is returned as an index into the string table. */ static dof_stridx_t dof_add_string(dt_dof_t *ddo, const char *s) { dt_buf_t *bp = &ddo->ddo_strs; dof_stridx_t i = dt_buf_len(bp); if (i != 0 && (s == NULL || *s == '\0')) return (0); /* string table has \0 at offset 0 */ dt_buf_write(ddo->ddo_hdl, bp, s, strlen(s) + 1, sizeof (char)); return (i); } static dof_attr_t dof_attr(const dtrace_attribute_t *ap) { return (DOF_ATTR(ap->dtat_name, ap->dtat_data, ap->dtat_class)); } static dof_secidx_t dof_add_difo(dt_dof_t *ddo, const dtrace_difo_t *dp) { dof_secidx_t dsecs[5]; /* enough for all possible DIFO sections */ uint_t nsecs = 0; dof_difohdr_t *dofd; dof_relohdr_t dofr; dof_secidx_t relsec; dof_secidx_t strsec = DOF_SECIDX_NONE; dof_secidx_t intsec = DOF_SECIDX_NONE; dof_secidx_t hdrsec = DOF_SECIDX_NONE; if (dp->dtdo_buf != NULL) { dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_buf, DOF_SECT_DIF, sizeof (dif_instr_t), 0, sizeof (dif_instr_t), sizeof (dif_instr_t) * dp->dtdo_len); } if (dp->dtdo_inttab != NULL) { dsecs[nsecs++] = intsec = dof_add_lsect(ddo, dp->dtdo_inttab, DOF_SECT_INTTAB, sizeof (uint64_t), 0, sizeof (uint64_t), sizeof (uint64_t) * dp->dtdo_intlen); } if (dp->dtdo_strtab != NULL) { dsecs[nsecs++] = strsec = dof_add_lsect(ddo, dp->dtdo_strtab, DOF_SECT_STRTAB, sizeof (char), 0, 0, dp->dtdo_strlen); } if (dp->dtdo_vartab != NULL) { dsecs[nsecs++] = dof_add_lsect(ddo, dp->dtdo_vartab, DOF_SECT_VARTAB, sizeof (uint_t), 0, sizeof (dtrace_difv_t), sizeof (dtrace_difv_t) * dp->dtdo_varlen); } if (dp->dtdo_xlmtab != NULL) { dof_xlref_t *xlt, *xlp; dt_node_t **pnp; xlt = alloca(sizeof (dof_xlref_t) * dp->dtdo_xlmlen); pnp = dp->dtdo_xlmtab; /* * dtdo_xlmtab contains pointers to the translator members. * The translator itself is in sect ddo_xlimport[dxp->dx_id]. * The XLMEMBERS entries are in order by their dn_membid, so * the member section offset is the population count of bits * in ddo_pgp->dp_xlrefs[] up to and not including dn_membid. */ for (xlp = xlt; xlp < xlt + dp->dtdo_xlmlen; xlp++) { dt_node_t *dnp = *pnp++; dt_xlator_t *dxp = dnp->dn_membexpr->dn_xlator; xlp->dofxr_xlator = ddo->ddo_xlimport[dxp->dx_id]; xlp->dofxr_member = dt_popcb( ddo->ddo_pgp->dp_xrefs[dxp->dx_id], dnp->dn_membid); xlp->dofxr_argn = (uint32_t)dxp->dx_arg; } dsecs[nsecs++] = dof_add_lsect(ddo, xlt, DOF_SECT_XLTAB, sizeof (dof_secidx_t), 0, sizeof (dof_xlref_t), sizeof (dof_xlref_t) * dp->dtdo_xlmlen); } /* * Copy the return type and the array of section indices that form the * DIFO into a single dof_difohdr_t and then add DOF_SECT_DIFOHDR. */ assert(nsecs <= sizeof (dsecs) / sizeof (dsecs[0])); dofd = alloca(sizeof (dtrace_diftype_t) + sizeof (dsecs)); bcopy(&dp->dtdo_rtype, &dofd->dofd_rtype, sizeof (dtrace_diftype_t)); bcopy(dsecs, &dofd->dofd_links, sizeof (dof_secidx_t) * nsecs); hdrsec = dof_add_lsect(ddo, dofd, DOF_SECT_DIFOHDR, sizeof (dof_secidx_t), 0, 0, sizeof (dtrace_diftype_t) + sizeof (dof_secidx_t) * nsecs); /* * Add any other sections related to dtrace_difo_t. These are not * referenced in dof_difohdr_t because they are not used by emulation. */ if (dp->dtdo_kreltab != NULL) { relsec = dof_add_lsect(ddo, dp->dtdo_kreltab, DOF_SECT_RELTAB, sizeof (uint64_t), 0, sizeof (dof_relodesc_t), sizeof (dof_relodesc_t) * dp->dtdo_krelen); /* * This code assumes the target of all relocations is the * integer table 'intsec' (DOF_SECT_INTTAB). If other sections * need relocation in the future this will need to change. */ dofr.dofr_strtab = strsec; dofr.dofr_relsec = relsec; dofr.dofr_tgtsec = intsec; (void) dof_add_lsect(ddo, &dofr, DOF_SECT_KRELHDR, sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t)); } if (dp->dtdo_ureltab != NULL) { relsec = dof_add_lsect(ddo, dp->dtdo_ureltab, DOF_SECT_RELTAB, sizeof (uint64_t), 0, sizeof (dof_relodesc_t), sizeof (dof_relodesc_t) * dp->dtdo_urelen); /* * This code assumes the target of all relocations is the * integer table 'intsec' (DOF_SECT_INTTAB). If other sections * need relocation in the future this will need to change. */ dofr.dofr_strtab = strsec; dofr.dofr_relsec = relsec; dofr.dofr_tgtsec = intsec; (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR, sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t)); } return (hdrsec); } static void dof_add_translator(dt_dof_t *ddo, const dt_xlator_t *dxp, uint_t type) { dtrace_hdl_t *dtp = ddo->ddo_hdl; dof_xlmember_t dofxm; dof_xlator_t dofxl; dof_secidx_t *xst; char buf[DT_TYPE_NAMELEN]; dt_node_t *dnp; uint_t i = 0; assert(type == DOF_SECT_XLIMPORT || type == DOF_SECT_XLEXPORT); xst = type == DOF_SECT_XLIMPORT ? ddo->ddo_xlimport : ddo->ddo_xlexport; if (xst[dxp->dx_id] != DOF_SECIDX_NONE) return; /* translator has already been emitted */ dt_buf_reset(dtp, &ddo->ddo_xlms); /* * Generate an array of dof_xlmember_t's into ddo_xlms. If we are * importing the translator, add only those members referenced by the * program and set the dofxm_difo reference of each member to NONE. If * we're exporting the translator, add all members and a DIFO for each. */ for (dnp = dxp->dx_members; dnp != NULL; dnp = dnp->dn_list, i++) { if (type == DOF_SECT_XLIMPORT) { if (!BT_TEST(ddo->ddo_pgp->dp_xrefs[dxp->dx_id], i)) continue; /* member is not referenced */ dofxm.dofxm_difo = DOF_SECIDX_NONE; } else { dofxm.dofxm_difo = dof_add_difo(ddo, dxp->dx_membdif[dnp->dn_membid]); } dofxm.dofxm_name = dof_add_string(ddo, dnp->dn_membname); dt_node_diftype(dtp, dnp, &dofxm.dofxm_type); dt_buf_write(dtp, &ddo->ddo_xlms, &dofxm, sizeof (dofxm), sizeof (uint32_t)); } dofxl.dofxl_members = dof_add_lsect(ddo, NULL, DOF_SECT_XLMEMBERS, sizeof (uint32_t), 0, sizeof (dofxm), dt_buf_len(&ddo->ddo_xlms)); dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_xlms, sizeof (uint32_t)); dofxl.dofxl_strtab = ddo->ddo_strsec; dofxl.dofxl_argv = dof_add_string(ddo, ctf_type_name( dxp->dx_src_ctfp, dxp->dx_src_type, buf, sizeof (buf))); dofxl.dofxl_argc = 1; dofxl.dofxl_type = dof_add_string(ddo, ctf_type_name( dxp->dx_dst_ctfp, dxp->dx_dst_type, buf, sizeof (buf))); dofxl.dofxl_attr = dof_attr(&dxp->dx_souid.di_attr); xst[dxp->dx_id] = dof_add_lsect(ddo, &dofxl, type, sizeof (uint32_t), 0, 0, sizeof (dofxl)); } /*ARGSUSED*/ static int dof_add_probe(dt_idhash_t *dhp, dt_ident_t *idp, void *data) { dt_dof_t *ddo = data; dtrace_hdl_t *dtp = ddo->ddo_hdl; dt_probe_t *prp = idp->di_data; dof_probe_t dofpr; dof_relodesc_t dofr; dt_probe_instance_t *pip; dt_node_t *dnp; char buf[DT_TYPE_NAMELEN]; uint_t i; dofpr.dofpr_addr = 0; dofpr.dofpr_name = dof_add_string(ddo, prp->pr_name); dofpr.dofpr_nargv = dt_buf_len(&ddo->ddo_strs); for (dnp = prp->pr_nargs; dnp != NULL; dnp = dnp->dn_list) { (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp, dnp->dn_type, buf, sizeof (buf))); } dofpr.dofpr_xargv = dt_buf_len(&ddo->ddo_strs); for (dnp = prp->pr_xargs; dnp != NULL; dnp = dnp->dn_list) { (void) dof_add_string(ddo, ctf_type_name(dnp->dn_ctfp, dnp->dn_type, buf, sizeof (buf))); } dofpr.dofpr_argidx = dt_buf_len(&ddo->ddo_args) / sizeof (uint8_t); for (i = 0; i < prp->pr_xargc; i++) { dt_buf_write(dtp, &ddo->ddo_args, &prp->pr_mapping[i], sizeof (uint8_t), sizeof (uint8_t)); } dofpr.dofpr_nargc = prp->pr_nargc; dofpr.dofpr_xargc = prp->pr_xargc; dofpr.dofpr_pad1 = 0; dofpr.dofpr_pad2 = 0; for (pip = prp->pr_inst; pip != NULL; pip = pip->pi_next) { dt_dprintf("adding probe for %s:%s\n", pip->pi_fname, prp->pr_name); dofpr.dofpr_func = dof_add_string(ddo, pip->pi_fname); /* * There should be one probe offset or is-enabled probe offset * or else this probe instance won't have been created. The * kernel will reject DOF which has a probe with no offsets. */ assert(pip->pi_noffs + pip->pi_nenoffs > 0); dofpr.dofpr_offidx = dt_buf_len(&ddo->ddo_offs) / sizeof (uint32_t); dofpr.dofpr_noffs = pip->pi_noffs; dt_buf_write(dtp, &ddo->ddo_offs, pip->pi_offs, pip->pi_noffs * sizeof (uint32_t), sizeof (uint32_t)); dofpr.dofpr_enoffidx = dt_buf_len(&ddo->ddo_enoffs) / sizeof (uint32_t); dofpr.dofpr_nenoffs = pip->pi_nenoffs; dt_buf_write(dtp, &ddo->ddo_enoffs, pip->pi_enoffs, pip->pi_nenoffs * sizeof (uint32_t), sizeof (uint32_t)); /* * If pi_rname isn't set, the relocation will be against the * function name. If it is, the relocation will be against * pi_rname. This will be used if the function is scoped * locally so an alternate symbol is added for the purpose * of this relocation. */ if (pip->pi_rname[0] == '\0') dofr.dofr_name = dofpr.dofpr_func; else dofr.dofr_name = dof_add_string(ddo, pip->pi_rname); dofr.dofr_type = DOF_RELO_SETX; dofr.dofr_offset = dt_buf_len(&ddo->ddo_probes); dofr.dofr_data = 0; dt_buf_write(dtp, &ddo->ddo_rels, &dofr, sizeof (dofr), sizeof (uint64_t)); dt_buf_write(dtp, &ddo->ddo_probes, &dofpr, sizeof (dofpr), sizeof (uint64_t)); } return (0); } static void dof_add_provider(dt_dof_t *ddo, const dt_provider_t *pvp) { dtrace_hdl_t *dtp = ddo->ddo_hdl; dof_provider_t dofpv; dof_relohdr_t dofr; dof_secidx_t *dofs; ulong_t xr, nxr; size_t sz; id_t i; if (pvp->pv_flags & DT_PROVIDER_IMPL) return; /* ignore providers that are exported by dtrace(7D) */ nxr = dt_popcb(pvp->pv_xrefs, pvp->pv_xrmax); dofs = alloca(sizeof (dof_secidx_t) * (nxr + 1)); xr = 1; /* reserve dofs[0] for the provider itself */ /* * For each translator referenced by the provider (pv_xrefs), emit an * exported translator section for it if one hasn't been created yet. */ for (i = 0; i < pvp->pv_xrmax; i++) { if (BT_TEST(pvp->pv_xrefs, i) && dtp->dt_xlatemode == DT_XL_DYNAMIC) { dof_add_translator(ddo, dt_xlator_lookup_id(dtp, i), DOF_SECT_XLEXPORT); dofs[xr++] = ddo->ddo_xlexport[i]; } } dt_buf_reset(dtp, &ddo->ddo_probes); dt_buf_reset(dtp, &ddo->ddo_args); dt_buf_reset(dtp, &ddo->ddo_offs); dt_buf_reset(dtp, &ddo->ddo_enoffs); dt_buf_reset(dtp, &ddo->ddo_rels); (void) dt_idhash_iter(pvp->pv_probes, dof_add_probe, ddo); dofpv.dofpv_probes = dof_add_lsect(ddo, NULL, DOF_SECT_PROBES, sizeof (uint64_t), 0, sizeof (dof_probe_t), dt_buf_len(&ddo->ddo_probes)); dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_probes, sizeof (uint64_t)); dofpv.dofpv_prargs = dof_add_lsect(ddo, NULL, DOF_SECT_PRARGS, sizeof (uint8_t), 0, sizeof (uint8_t), dt_buf_len(&ddo->ddo_args)); dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_args, sizeof (uint8_t)); dofpv.dofpv_proffs = dof_add_lsect(ddo, NULL, DOF_SECT_PROFFS, sizeof (uint_t), 0, sizeof (uint_t), dt_buf_len(&ddo->ddo_offs)); dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_offs, sizeof (uint_t)); if ((sz = dt_buf_len(&ddo->ddo_enoffs)) != 0) { dofpv.dofpv_prenoffs = dof_add_lsect(ddo, NULL, DOF_SECT_PRENOFFS, sizeof (uint_t), 0, sizeof (uint_t), sz); } else { dofpv.dofpv_prenoffs = DOF_SECT_NONE; } dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_enoffs, sizeof (uint_t)); dofpv.dofpv_strtab = ddo->ddo_strsec; dofpv.dofpv_name = dof_add_string(ddo, pvp->pv_desc.dtvd_name); dofpv.dofpv_provattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_provider); dofpv.dofpv_modattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_mod); dofpv.dofpv_funcattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_func); dofpv.dofpv_nameattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_name); dofpv.dofpv_argsattr = dof_attr(&pvp->pv_desc.dtvd_attr.dtpa_args); dofs[0] = dof_add_lsect(ddo, &dofpv, DOF_SECT_PROVIDER, sizeof (dof_secidx_t), 0, 0, sizeof (dof_provider_t)); dofr.dofr_strtab = dofpv.dofpv_strtab; dofr.dofr_tgtsec = dofpv.dofpv_probes; dofr.dofr_relsec = dof_add_lsect(ddo, NULL, DOF_SECT_RELTAB, sizeof (uint64_t), 0, sizeof (dof_relodesc_t), dt_buf_len(&ddo->ddo_rels)); dt_buf_concat(dtp, &ddo->ddo_ldata, &ddo->ddo_rels, sizeof (uint64_t)); (void) dof_add_lsect(ddo, &dofr, DOF_SECT_URELHDR, sizeof (dof_secidx_t), 0, 0, sizeof (dof_relohdr_t)); if (nxr != 0 && dtp->dt_xlatemode == DT_XL_DYNAMIC) { (void) dof_add_lsect(ddo, dofs, DOF_SECT_PREXPORT, sizeof (dof_secidx_t), 0, sizeof (dof_secidx_t), sizeof (dof_secidx_t) * (nxr + 1)); } } static int dof_hdr(dtrace_hdl_t *dtp, uint8_t dofversion, dof_hdr_t *hp) { /* * If our config values cannot fit in a uint8_t, we can't generate a * DOF header since the values won't fit. This can only happen if the * user forcibly compiles a program with an artificial configuration. */ if (dtp->dt_conf.dtc_difversion > UINT8_MAX || dtp->dt_conf.dtc_difintregs > UINT8_MAX || dtp->dt_conf.dtc_diftupregs > UINT8_MAX) return (dt_set_errno(dtp, EOVERFLOW)); bzero(hp, sizeof (dof_hdr_t)); hp->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; hp->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; hp->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; hp->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_LP64; else hp->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_ILP32; hp->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; hp->dofh_ident[DOF_ID_VERSION] = dofversion; hp->dofh_ident[DOF_ID_DIFVERS] = dtp->dt_conf.dtc_difversion; hp->dofh_ident[DOF_ID_DIFIREG] = dtp->dt_conf.dtc_difintregs; hp->dofh_ident[DOF_ID_DIFTREG] = dtp->dt_conf.dtc_diftupregs; hp->dofh_hdrsize = sizeof (dof_hdr_t); hp->dofh_secsize = sizeof (dof_sec_t); hp->dofh_secoff = sizeof (dof_hdr_t); return (0); } void * dtrace_dof_create(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t flags) { dt_dof_t *ddo = &dtp->dt_dof; const dtrace_ecbdesc_t *edp, *last; const dtrace_probedesc_t *pdp; const dtrace_actdesc_t *ap; const dt_stmt_t *stp; uint_t maxacts = 0; uint_t maxfmt = 0; dt_provider_t *pvp; dt_xlator_t *dxp; dof_actdesc_t *dofa; dof_sec_t *sp; size_t ssize, lsize; dof_hdr_t h; dt_buf_t dof; char *fmt; uint_t i; if (flags & ~DTRACE_D_MASK) { (void) dt_set_errno(dtp, EINVAL); return (NULL); } flags |= dtp->dt_dflags; if (dof_hdr(dtp, pgp->dp_dofversion, &h) != 0) return (NULL); if (dt_dof_reset(dtp, pgp) != 0) return (NULL); /* * Iterate through the statement list computing the maximum number of * actions and the maximum format string for allocating local buffers. */ for (last = NULL, stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = dt_list_next(stp), last = edp) { dtrace_stmtdesc_t *sdp = stp->ds_desc; dtrace_actdesc_t *ap = sdp->dtsd_action; if (sdp->dtsd_fmtdata != NULL) { i = dtrace_printf_format(dtp, sdp->dtsd_fmtdata, NULL, 0); maxfmt = MAX(maxfmt, i); } if ((edp = sdp->dtsd_ecbdesc) == last) continue; /* same ecb as previous statement */ for (i = 0, ap = edp->dted_action; ap; ap = ap->dtad_next) i++; maxacts = MAX(maxacts, i); } dofa = alloca(sizeof (dof_actdesc_t) * maxacts); fmt = alloca(maxfmt + 1); ddo->ddo_strsec = dof_add_lsect(ddo, NULL, DOF_SECT_STRTAB, 1, 0, 0, 0); (void) dof_add_string(ddo, ""); /* * If there are references to dynamic translators in the program, add * an imported translator table entry for each referenced translator. */ if (pgp->dp_xrefslen != 0) { for (dxp = dt_list_next(&dtp->dt_xlators); dxp != NULL; dxp = dt_list_next(dxp)) { if (dxp->dx_id < pgp->dp_xrefslen && pgp->dp_xrefs[dxp->dx_id] != NULL) dof_add_translator(ddo, dxp, DOF_SECT_XLIMPORT); } } /* * Now iterate through the statement list, creating the DOF section * headers and data for each one and adding them to our buffers. */ for (last = NULL, stp = dt_list_next(&pgp->dp_stmts); stp != NULL; stp = dt_list_next(stp), last = edp) { dof_secidx_t probesec = DOF_SECIDX_NONE; dof_secidx_t prdsec = DOF_SECIDX_NONE; dof_secidx_t actsec = DOF_SECIDX_NONE; const dt_stmt_t *next = stp; dtrace_stmtdesc_t *sdp = stp->ds_desc; dof_stridx_t strndx = 0; dof_probedesc_t dofp; dof_ecbdesc_t dofe; uint_t i; if ((edp = stp->ds_desc->dtsd_ecbdesc) == last) continue; /* same ecb as previous statement */ pdp = &edp->dted_probe; /* * Add a DOF_SECT_PROBEDESC for the ECB's probe description, * and copy the probe description strings into the string table. */ dofp.dofp_strtab = ddo->ddo_strsec; dofp.dofp_provider = dof_add_string(ddo, pdp->dtpd_provider); dofp.dofp_mod = dof_add_string(ddo, pdp->dtpd_mod); dofp.dofp_func = dof_add_string(ddo, pdp->dtpd_func); dofp.dofp_name = dof_add_string(ddo, pdp->dtpd_name); dofp.dofp_id = pdp->dtpd_id; probesec = dof_add_lsect(ddo, &dofp, DOF_SECT_PROBEDESC, sizeof (dof_secidx_t), 0, sizeof (dof_probedesc_t), sizeof (dof_probedesc_t)); /* * If there is a predicate DIFO associated with the ecbdesc, * write out the DIFO sections and save the DIFO section index. */ if (edp->dted_pred.dtpdd_difo != NULL) prdsec = dof_add_difo(ddo, edp->dted_pred.dtpdd_difo); /* * Now iterate through the action list generating DIFOs as * referenced therein and adding action descriptions to 'dofa'. */ for (i = 0, ap = edp->dted_action; ap != NULL; ap = ap->dtad_next, i++) { if (ap->dtad_difo != NULL) { dofa[i].dofa_difo = dof_add_difo(ddo, ap->dtad_difo); } else dofa[i].dofa_difo = DOF_SECIDX_NONE; /* * If the first action in a statement has format data, * add the format string to the global string table. */ if (sdp != NULL && ap == sdp->dtsd_action) { if (sdp->dtsd_fmtdata != NULL) { (void) dtrace_printf_format(dtp, sdp->dtsd_fmtdata, fmt, maxfmt + 1); strndx = dof_add_string(ddo, fmt); } else strndx = 0; /* use dtad_arg instead */ if ((next = dt_list_next(next)) != NULL) sdp = next->ds_desc; else sdp = NULL; } if (strndx != 0) { dofa[i].dofa_arg = strndx; dofa[i].dofa_strtab = ddo->ddo_strsec; } else { dofa[i].dofa_arg = ap->dtad_arg; dofa[i].dofa_strtab = DOF_SECIDX_NONE; } dofa[i].dofa_kind = ap->dtad_kind; dofa[i].dofa_ntuple = ap->dtad_ntuple; dofa[i].dofa_uarg = ap->dtad_uarg; } if (i > 0) { actsec = dof_add_lsect(ddo, dofa, DOF_SECT_ACTDESC, sizeof (uint64_t), 0, sizeof (dof_actdesc_t), sizeof (dof_actdesc_t) * i); } /* * Now finally, add the DOF_SECT_ECBDESC referencing all the * previously created sub-sections. */ dofe.dofe_probes = probesec; dofe.dofe_pred = prdsec; dofe.dofe_actions = actsec; dofe.dofe_pad = 0; dofe.dofe_uarg = edp->dted_uarg; (void) dof_add_lsect(ddo, &dofe, DOF_SECT_ECBDESC, sizeof (uint64_t), 0, 0, sizeof (dof_ecbdesc_t)); } /* * If any providers are user-defined, output DOF sections corresponding * to the providers and the probes and arguments that they define. */ if (flags & DTRACE_D_PROBES) { for (pvp = dt_list_next(&dtp->dt_provlist); pvp != NULL; pvp = dt_list_next(pvp)) dof_add_provider(ddo, pvp); } /* * If we're not stripping unloadable sections, generate compiler * comments and any other unloadable miscellany. */ if (!(flags & DTRACE_D_STRIP)) { (void) dof_add_usect(ddo, _dtrace_version, DOF_SECT_COMMENTS, sizeof (char), 0, 0, strlen(_dtrace_version) + 1); (void) dof_add_usect(ddo, &dtp->dt_uts, DOF_SECT_UTSNAME, sizeof (char), 0, 0, sizeof (struct utsname)); } /* * Compute and fill in the appropriate values for the dof_hdr_t's * dofh_secnum, dofh_loadsz, and dofh_filez values. */ h.dofh_secnum = ddo->ddo_nsecs; ssize = sizeof (h) + dt_buf_len(&ddo->ddo_secs); assert(ssize == sizeof (h) + sizeof (dof_sec_t) * ddo->ddo_nsecs); h.dofh_loadsz = ssize + dt_buf_len(&ddo->ddo_ldata) + dt_buf_len(&ddo->ddo_strs); if (dt_buf_len(&ddo->ddo_udata) != 0) { lsize = roundup(h.dofh_loadsz, sizeof (uint64_t)); h.dofh_filesz = lsize + dt_buf_len(&ddo->ddo_udata); } else { lsize = h.dofh_loadsz; h.dofh_filesz = lsize; } /* * Set the global DOF_SECT_STRTAB's offset to be after the header, * section headers, and other loadable data. Since we're going to * iterate over the buffer data directly, we must check for errors. */ if ((i = dt_buf_error(&ddo->ddo_secs)) != 0) { (void) dt_set_errno(dtp, i); return (NULL); } sp = dt_buf_ptr(&ddo->ddo_secs); assert(sp[ddo->ddo_strsec].dofs_type == DOF_SECT_STRTAB); sp[ddo->ddo_strsec].dofs_offset = ssize + dt_buf_len(&ddo->ddo_ldata); sp[ddo->ddo_strsec].dofs_size = dt_buf_len(&ddo->ddo_strs); /* * Now relocate all the other section headers by adding the appropriate * delta to their respective dofs_offset values. */ for (i = 0; i < ddo->ddo_nsecs; i++, sp++) { if (i == ddo->ddo_strsec) continue; /* already relocated above */ if (sp->dofs_flags & DOF_SECF_LOAD) sp->dofs_offset += ssize; else sp->dofs_offset += lsize; } /* * Finally, assemble the complete in-memory DOF buffer by writing the * header and then concatenating all our buffers. dt_buf_concat() will * propagate any errors and cause dt_buf_claim() to return NULL. */ dt_buf_create(dtp, &dof, "dof", h.dofh_filesz); dt_buf_write(dtp, &dof, &h, sizeof (h), sizeof (uint64_t)); dt_buf_concat(dtp, &dof, &ddo->ddo_secs, sizeof (uint64_t)); dt_buf_concat(dtp, &dof, &ddo->ddo_ldata, sizeof (uint64_t)); dt_buf_concat(dtp, &dof, &ddo->ddo_strs, sizeof (char)); dt_buf_concat(dtp, &dof, &ddo->ddo_udata, sizeof (uint64_t)); return (dt_buf_claim(dtp, &dof)); } void dtrace_dof_destroy(dtrace_hdl_t *dtp, void *dof) { dt_free(dtp, dof); } void * dtrace_getopt_dof(dtrace_hdl_t *dtp) { dof_hdr_t *dof; dof_sec_t *sec; dof_optdesc_t *dofo; int i, nopts = 0, len = sizeof (dof_hdr_t) + roundup(sizeof (dof_sec_t), sizeof (uint64_t)); for (i = 0; i < DTRACEOPT_MAX; i++) { if (dtp->dt_options[i] != DTRACEOPT_UNSET) nopts++; } len += sizeof (dof_optdesc_t) * nopts; if ((dof = dt_zalloc(dtp, len)) == NULL || dof_hdr(dtp, DOF_VERSION, dof) != 0) { dt_free(dtp, dof); return (NULL); } dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ dof->dofh_loadsz = len; dof->dofh_filesz = len; /* * Fill in the option section header... */ sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); sec->dofs_type = DOF_SECT_OPTDESC; sec->dofs_align = sizeof (uint64_t); sec->dofs_flags = DOF_SECF_LOAD; sec->dofs_entsize = sizeof (dof_optdesc_t); dofo = (dof_optdesc_t *)((uintptr_t)sec + roundup(sizeof (dof_sec_t), sizeof (uint64_t))); sec->dofs_offset = (uintptr_t)dofo - (uintptr_t)dof; sec->dofs_size = sizeof (dof_optdesc_t) * nopts; for (i = 0; i < DTRACEOPT_MAX; i++) { if (dtp->dt_options[i] == DTRACEOPT_UNSET) continue; dofo->dofo_option = i; dofo->dofo_strtab = DOF_SECIDX_NONE; dofo->dofo_value = dtp->dt_options[i]; dofo++; } return (dof); } void * dtrace_geterr_dof(dtrace_hdl_t *dtp) { if (dtp->dt_errprog != NULL) return (dtrace_dof_create(dtp, dtp->dt_errprog, 0)); (void) dt_set_errno(dtp, EDT_BADERROR); return (NULL); } diff --git a/cddl/contrib/opensolaris/lib/libdtrace/common/dt_ident.c b/cddl/contrib/opensolaris/lib/libdtrace/common/dt_ident.c index c437e0ab031a..13adbb45e1a7 100644 --- a/cddl/contrib/opensolaris/lib/libdtrace/common/dt_ident.c +++ b/cddl/contrib/opensolaris/lib/libdtrace/common/dt_ident.c @@ -1,1041 +1,1047 @@ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" +#if defined(sun) #include +#endif #include #include +#if defined(sun) #include +#endif #include #include #include +#if defined(sun) #include +#endif #include #include #include #include #include #include /* * Common code for cooking an identifier that uses a typed signature list (we * use this for associative arrays and functions). If the argument list is * of the same length and types, then return the return type. Otherwise * print an appropriate compiler error message and abort the compile. */ static void dt_idcook_sign(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args, const char *prefix, const char *suffix) { dt_idsig_t *isp = idp->di_data; int i, compat, mismatch, arglimit, iskey; char n1[DT_TYPE_NAMELEN]; char n2[DT_TYPE_NAMELEN]; iskey = idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_AGG; if (isp->dis_varargs >= 0) { mismatch = argc < isp->dis_varargs; arglimit = isp->dis_varargs; } else if (isp->dis_optargs >= 0) { mismatch = (argc < isp->dis_optargs || argc > isp->dis_argc); arglimit = argc; } else { mismatch = argc != isp->dis_argc; arglimit = isp->dis_argc; } if (mismatch) { xyerror(D_PROTO_LEN, "%s%s%s prototype mismatch: %d %s%s" "passed, %s%d expected\n", prefix, idp->di_name, suffix, argc, iskey ? "key" : "arg", argc == 1 ? " " : "s ", isp->dis_optargs >= 0 ? "at least " : "", isp->dis_optargs >= 0 ? isp->dis_optargs : arglimit); } for (i = 0; i < arglimit; i++, args = args->dn_list) { if (isp->dis_args[i].dn_ctfp != NULL) compat = dt_node_is_argcompat(&isp->dis_args[i], args); else compat = 1; /* "@" matches any type */ if (!compat) { xyerror(D_PROTO_ARG, "%s%s%s %s #%d is incompatible with " "prototype:\n\tprototype: %s\n\t%9s: %s\n", prefix, idp->di_name, suffix, iskey ? "key" : "argument", i + 1, dt_node_type_name(&isp->dis_args[i], n1, sizeof (n1)), iskey ? "key" : "argument", dt_node_type_name(args, n2, sizeof (n2))); } } dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type); } /* * Cook an associative array identifier. If this is the first time we are * cooking this array, create its signature based on the argument list. * Otherwise validate the argument list against the existing signature. */ static void dt_idcook_assc(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) { if (idp->di_data == NULL) { dt_idsig_t *isp = idp->di_data = malloc(sizeof (dt_idsig_t)); char n[DT_TYPE_NAMELEN]; int i; if (isp == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); isp->dis_varargs = -1; isp->dis_optargs = -1; isp->dis_argc = argc; isp->dis_args = NULL; isp->dis_auxinfo = 0; if (argc != 0 && (isp->dis_args = calloc(argc, sizeof (dt_node_t))) == NULL) { idp->di_data = NULL; free(isp); longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); } /* * If this identifier has not been explicitly declared earlier, * set the identifier's base type to be our special type . * If this ident is an aggregation, it will remain as is. If * this ident is an associative array, it will be reassigned * based on the result type of the first assignment statement. */ if (!(idp->di_flags & DT_IDFLG_DECL)) { idp->di_ctfp = DT_DYN_CTFP(yypcb->pcb_hdl); idp->di_type = DT_DYN_TYPE(yypcb->pcb_hdl); } for (i = 0; i < argc; i++, args = args->dn_list) { if (dt_node_is_dynamic(args) || dt_node_is_void(args)) { xyerror(D_KEY_TYPE, "%s expression may not be " "used as %s index: key #%d\n", dt_node_type_name(args, n, sizeof (n)), dt_idkind_name(idp->di_kind), i + 1); } dt_node_type_propagate(args, &isp->dis_args[i]); isp->dis_args[i].dn_list = &isp->dis_args[i + 1]; } if (argc != 0) isp->dis_args[argc - 1].dn_list = NULL; dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type); } else { dt_idcook_sign(dnp, idp, argc, args, idp->di_kind == DT_IDENT_AGG ? "@" : "", "[ ]"); } } /* * Cook a function call. If this is the first time we are cooking this * identifier, create its type signature based on predefined prototype stored * in di_iarg. We then validate the argument list against this signature. */ static void dt_idcook_func(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) { if (idp->di_data == NULL) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; dtrace_typeinfo_t dtt; dt_idsig_t *isp; char *s, *p1, *p2; int i = 0; assert(idp->di_iarg != NULL); s = alloca(strlen(idp->di_iarg) + 1); (void) strcpy(s, idp->di_iarg); if ((p2 = strrchr(s, ')')) != NULL) *p2 = '\0'; /* mark end of parameter list string */ if ((p1 = strchr(s, '(')) != NULL) *p1++ = '\0'; /* mark end of return type string */ if (p1 == NULL || p2 == NULL) { xyerror(D_UNKNOWN, "internal error: malformed entry " "for built-in function %s\n", idp->di_name); } for (p2 = p1; *p2 != '\0'; p2++) { if (!isspace(*p2)) { i++; break; } } for (p2 = strchr(p2, ','); p2++ != NULL; i++) p2 = strchr(p2, ','); /* * We first allocate a new ident signature structure with the * appropriate number of argument entries, and then look up * the return type and store its CTF data in di_ctfp/type. */ if ((isp = idp->di_data = malloc(sizeof (dt_idsig_t))) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); isp->dis_varargs = -1; isp->dis_optargs = -1; isp->dis_argc = i; isp->dis_args = NULL; isp->dis_auxinfo = 0; if (i != 0 && (isp->dis_args = calloc(i, sizeof (dt_node_t))) == NULL) { idp->di_data = NULL; free(isp); longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); } if (dt_type_lookup(s, &dtt) == -1) { xyerror(D_UNKNOWN, "failed to resolve type of %s (%s):" " %s\n", idp->di_name, s, dtrace_errmsg(dtp, dtrace_errno(dtp))); } if (idp->di_kind == DT_IDENT_AGGFUNC) { idp->di_ctfp = DT_DYN_CTFP(dtp); idp->di_type = DT_DYN_TYPE(dtp); } else { idp->di_ctfp = dtt.dtt_ctfp; idp->di_type = dtt.dtt_type; } /* * For each comma-delimited parameter in the prototype string, * we look up the corresponding type and store its CTF data in * the corresponding location in dis_args[]. We also recognize * the special type string "@" to indicate that the specified * parameter may be a D expression of *any* type (represented * as a dis_args[] element with ctfp = NULL, type == CTF_ERR). * If a varargs "..." is present, we record the argument index * in dis_varargs for the benefit of dt_idcook_sign(), above. * If the type of an argument is enclosed in square brackets * (e.g. "[int]"), the argument is considered optional: the * argument may be absent, but if it is present, it must be of * the specified type. Note that varargs may not optional, * optional arguments may not follow varargs, and non-optional * arguments may not follow optional arguments. */ for (i = 0; i < isp->dis_argc; i++, p1 = p2) { while (isspace(*p1)) p1++; /* skip leading whitespace */ if ((p2 = strchr(p1, ',')) == NULL) p2 = p1 + strlen(p1); else *p2++ = '\0'; if (strcmp(p1, "@") == 0 || strcmp(p1, "...") == 0) { isp->dis_args[i].dn_ctfp = NULL; isp->dis_args[i].dn_type = CTF_ERR; if (*p1 == '.') isp->dis_varargs = i; continue; } if (*p1 == '[' && p1[strlen(p1) - 1] == ']') { if (isp->dis_varargs != -1) { xyerror(D_UNKNOWN, "optional arg#%d " "may not follow variable arg#%d\n", i + 1, isp->dis_varargs + 1); } if (isp->dis_optargs == -1) isp->dis_optargs = i; p1[strlen(p1) - 1] = '\0'; p1++; } else if (isp->dis_optargs != -1) { xyerror(D_UNKNOWN, "required arg#%d may not " "follow optional arg#%d\n", i + 1, isp->dis_optargs + 1); } if (dt_type_lookup(p1, &dtt) == -1) { xyerror(D_UNKNOWN, "failed to resolve type of " "%s arg#%d (%s): %s\n", idp->di_name, i + 1, p1, dtrace_errmsg(dtp, dtrace_errno(dtp))); } dt_node_type_assign(&isp->dis_args[i], dtt.dtt_ctfp, dtt.dtt_type); } } dt_idcook_sign(dnp, idp, argc, args, "", "( )"); } /* * Cook a reference to the dynamically typed args[] array. We verify that the * reference is using a single integer constant, and then construct a new ident * representing the appropriate type or translation specifically for this node. */ static void dt_idcook_args(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; dt_probe_t *prp = yypcb->pcb_probe; dt_node_t tag, *nnp, *xnp; dt_xlator_t *dxp; dt_ident_t *xidp; char n1[DT_TYPE_NAMELEN]; char n2[DT_TYPE_NAMELEN]; if (argc != 1) { xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s" "passed, 1 expected\n", idp->di_name, argc, argc == 1 ? " " : "s "); } if (ap->dn_kind != DT_NODE_INT) { xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with " "prototype:\n\tprototype: %s\n\t argument: %s\n", idp->di_name, "integer constant", dt_type_name(ap->dn_ctfp, ap->dn_type, n1, sizeof (n1))); } if (yypcb->pcb_pdesc == NULL) { xyerror(D_ARGS_NONE, "%s[ ] may not be referenced outside " "of a probe clause\n", idp->di_name); } if (prp == NULL) { xyerror(D_ARGS_MULTI, "%s[ ] may not be referenced because probe description %s " "matches an unstable set of probes\n", idp->di_name, dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1))); } if (ap->dn_value >= prp->pr_argc) { xyerror(D_ARGS_IDX, "index %lld is out of range for %s %s[ ]\n", (longlong_t)ap->dn_value, dtrace_desc2str(yypcb->pcb_pdesc, n1, sizeof (n1)), idp->di_name); } /* * Look up the native and translated argument types for the probe. * If no translation is needed, these will be the same underlying node. * If translation is needed, look up the appropriate translator. Once * we have the appropriate node, create a new dt_ident_t for this node, * assign it the appropriate attributes, and set the type of 'dnp'. */ xnp = prp->pr_xargv[ap->dn_value]; nnp = prp->pr_nargv[prp->pr_mapping[ap->dn_value]]; if (xnp->dn_type == CTF_ERR) { xyerror(D_ARGS_TYPE, "failed to resolve translated type for " "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value); } if (nnp->dn_type == CTF_ERR) { xyerror(D_ARGS_TYPE, "failed to resolve native type for " "%s[%lld]\n", idp->di_name, (longlong_t)ap->dn_value); } if (dtp->dt_xlatemode == DT_XL_STATIC && ( nnp == xnp || dt_node_is_argcompat(nnp, xnp))) { dnp->dn_ident = dt_ident_create(idp->di_name, idp->di_kind, idp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr, idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen); if (dnp->dn_ident == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); dt_node_type_assign(dnp, prp->pr_argv[ap->dn_value].dtt_ctfp, prp->pr_argv[ap->dn_value].dtt_type); } else if ((dxp = dt_xlator_lookup(dtp, nnp, xnp, DT_XLATE_FUZZY)) != NULL || ( dxp = dt_xlator_lookup(dtp, dt_probe_tag(prp, ap->dn_value, &tag), xnp, DT_XLATE_EXACT | DT_XLATE_EXTERN)) != NULL) { xidp = dt_xlator_ident(dxp, xnp->dn_ctfp, xnp->dn_type); dnp->dn_ident = dt_ident_create(idp->di_name, xidp->di_kind, xidp->di_flags | DT_IDFLG_ORPHAN, idp->di_id, idp->di_attr, idp->di_vers, idp->di_ops, idp->di_iarg, idp->di_gen); if (dnp->dn_ident == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); if (dt_xlator_dynamic(dxp)) dxp->dx_arg = (int)ap->dn_value; /* * Propagate relevant members from the translator's internal * dt_ident_t. This code must be kept in sync with the state * that is initialized for idents in dt_xlator_create(). */ dnp->dn_ident->di_data = xidp->di_data; dnp->dn_ident->di_ctfp = xidp->di_ctfp; dnp->dn_ident->di_type = xidp->di_type; dt_node_type_assign(dnp, DT_DYN_CTFP(dtp), DT_DYN_TYPE(dtp)); } else { xyerror(D_ARGS_XLATOR, "translator for %s[%lld] from %s to %s " "is not defined\n", idp->di_name, (longlong_t)ap->dn_value, dt_node_type_name(nnp, n1, sizeof (n1)), dt_node_type_name(xnp, n2, sizeof (n2))); } assert(dnp->dn_ident->di_flags & DT_IDFLG_ORPHAN); assert(dnp->dn_ident->di_id == idp->di_id); } static void dt_idcook_regs(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *ap) { dtrace_typeinfo_t dtt; dtrace_hdl_t *dtp = yypcb->pcb_hdl; char n[DT_TYPE_NAMELEN]; if (argc != 1) { xyerror(D_PROTO_LEN, "%s[ ] prototype mismatch: %d arg%s" "passed, 1 expected\n", idp->di_name, argc, argc == 1 ? " " : "s "); } if (ap->dn_kind != DT_NODE_INT) { xyerror(D_PROTO_ARG, "%s[ ] argument #1 is incompatible with " "prototype:\n\tprototype: %s\n\t argument: %s\n", idp->di_name, "integer constant", dt_type_name(ap->dn_ctfp, ap->dn_type, n, sizeof (n))); } if ((ap->dn_flags & DT_NF_SIGNED) && (int64_t)ap->dn_value < 0) { xyerror(D_REGS_IDX, "index %lld is out of range for array %s\n", (longlong_t)ap->dn_value, idp->di_name); } if (dt_type_lookup("uint64_t", &dtt) == -1) { xyerror(D_UNKNOWN, "failed to resolve type of %s: %s\n", idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp))); } idp->di_ctfp = dtt.dtt_ctfp; idp->di_type = dtt.dtt_type; dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type); } /*ARGSUSED*/ static void dt_idcook_type(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) { if (idp->di_type == CTF_ERR) { dtrace_hdl_t *dtp = yypcb->pcb_hdl; dtrace_typeinfo_t dtt; if (dt_type_lookup(idp->di_iarg, &dtt) == -1) { xyerror(D_UNKNOWN, "failed to resolve type %s for identifier %s: %s\n", (const char *)idp->di_iarg, idp->di_name, dtrace_errmsg(dtp, dtrace_errno(dtp))); } idp->di_ctfp = dtt.dtt_ctfp; idp->di_type = dtt.dtt_type; } dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type); } /*ARGSUSED*/ static void dt_idcook_thaw(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) { if (idp->di_ctfp != NULL && idp->di_type != CTF_ERR) dt_node_type_assign(dnp, idp->di_ctfp, idp->di_type); } static void dt_idcook_inline(dt_node_t *dnp, dt_ident_t *idp, int argc, dt_node_t *args) { if (idp->di_kind == DT_IDENT_ARRAY) dt_idcook_assc(dnp, idp, argc, args); else dt_idcook_thaw(dnp, idp, argc, args); } static void dt_iddtor_sign(dt_ident_t *idp) { if (idp->di_data != NULL) free(((dt_idsig_t *)idp->di_data)->dis_args); free(idp->di_data); } static void dt_iddtor_free(dt_ident_t *idp) { free(idp->di_data); } static void dt_iddtor_inline(dt_ident_t *idp) { dt_idnode_t *inp = idp->di_iarg; if (inp != NULL) { dt_node_link_free(&inp->din_list); if (inp->din_hash != NULL) dt_idhash_destroy(inp->din_hash); free(inp->din_argv); free(inp); } if (idp->di_kind == DT_IDENT_ARRAY) dt_iddtor_sign(idp); else dt_iddtor_free(idp); } /*ARGSUSED*/ static void dt_iddtor_none(dt_ident_t *idp) { /* do nothing */ } static void dt_iddtor_probe(dt_ident_t *idp) { if (idp->di_data != NULL) dt_probe_destroy(idp->di_data); } static size_t dt_idsize_type(dt_ident_t *idp) { return (ctf_type_size(idp->di_ctfp, idp->di_type)); } /*ARGSUSED*/ static size_t dt_idsize_none(dt_ident_t *idp) { return (0); } const dt_idops_t dt_idops_assc = { dt_idcook_assc, dt_iddtor_sign, dt_idsize_none, }; const dt_idops_t dt_idops_func = { dt_idcook_func, dt_iddtor_sign, dt_idsize_none, }; const dt_idops_t dt_idops_args = { dt_idcook_args, dt_iddtor_none, dt_idsize_none, }; const dt_idops_t dt_idops_regs = { dt_idcook_regs, dt_iddtor_free, dt_idsize_none, }; const dt_idops_t dt_idops_type = { dt_idcook_type, dt_iddtor_free, dt_idsize_type, }; const dt_idops_t dt_idops_thaw = { dt_idcook_thaw, dt_iddtor_free, dt_idsize_type, }; const dt_idops_t dt_idops_inline = { dt_idcook_inline, dt_iddtor_inline, dt_idsize_type, }; const dt_idops_t dt_idops_probe = { dt_idcook_thaw, dt_iddtor_probe, dt_idsize_none, }; static void dt_idhash_populate(dt_idhash_t *dhp) { const dt_ident_t *idp = dhp->dh_tmpl; dhp->dh_tmpl = NULL; /* clear dh_tmpl first to avoid recursion */ dt_dprintf("populating %s idhash from %p\n", dhp->dh_name, (void *)idp); for (; idp->di_name != NULL; idp++) { if (dt_idhash_insert(dhp, idp->di_name, idp->di_kind, idp->di_flags, idp->di_id, idp->di_attr, idp->di_vers, idp->di_ops ? idp->di_ops : &dt_idops_thaw, idp->di_iarg, 0) == NULL) longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM); } } dt_idhash_t * dt_idhash_create(const char *name, const dt_ident_t *tmpl, uint_t min, uint_t max) { dt_idhash_t *dhp; size_t size; assert(min <= max); size = sizeof (dt_idhash_t) + sizeof (dt_ident_t *) * (_dtrace_strbuckets - 1); if ((dhp = malloc(size)) == NULL) return (NULL); bzero(dhp, size); dhp->dh_name = name; dhp->dh_tmpl = tmpl; dhp->dh_nextid = min; dhp->dh_minid = min; dhp->dh_maxid = max; dhp->dh_hashsz = _dtrace_strbuckets; return (dhp); } /* * Destroy an entire identifier hash. This must be done using two passes with * an inlined version of dt_ident_destroy() to avoid referencing freed memory. * In the first pass di_dtor() is called for all identifiers; then the second * pass frees the actual dt_ident_t's. These must be done separately because * a di_dtor() may operate on data structures which contain references to other * identifiers inside of this hash itself (e.g. a global inline definition * which contains a parse tree that refers to another global variable). */ void dt_idhash_destroy(dt_idhash_t *dhp) { dt_ident_t *idp, *next; ulong_t i; for (i = 0; i < dhp->dh_hashsz; i++) { for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) { next = idp->di_next; idp->di_ops->di_dtor(idp); } } for (i = 0; i < dhp->dh_hashsz; i++) { for (idp = dhp->dh_hash[i]; idp != NULL; idp = next) { next = idp->di_next; free(idp->di_name); free(idp); } } free(dhp); } void dt_idhash_update(dt_idhash_t *dhp) { uint_t nextid = dhp->dh_minid; dt_ident_t *idp; ulong_t i; for (i = 0; i < dhp->dh_hashsz; i++) { for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) { /* * Right now we're hard coding which types need to be * reset, but ideally this would be done dynamically. */ if (idp->di_kind == DT_IDENT_ARRAY || idp->di_kind == DT_IDENT_SCALAR || idp->di_kind == DT_IDENT_AGG) nextid = MAX(nextid, idp->di_id + 1); } } dhp->dh_nextid = nextid; } dt_ident_t * dt_idhash_lookup(dt_idhash_t *dhp, const char *name) { size_t len; ulong_t h = dt_strtab_hash(name, &len) % dhp->dh_hashsz; dt_ident_t *idp; if (dhp->dh_tmpl != NULL) dt_idhash_populate(dhp); /* fill hash w/ initial population */ for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) { if (strcmp(idp->di_name, name) == 0) return (idp); } return (NULL); } int dt_idhash_nextid(dt_idhash_t *dhp, uint_t *p) { if (dhp->dh_nextid >= dhp->dh_maxid) return (-1); /* no more id's are free to allocate */ *p = dhp->dh_nextid++; return (0); } ulong_t dt_idhash_size(const dt_idhash_t *dhp) { return (dhp->dh_nelems); } const char * dt_idhash_name(const dt_idhash_t *dhp) { return (dhp->dh_name); } dt_ident_t * dt_idhash_insert(dt_idhash_t *dhp, const char *name, ushort_t kind, ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers, const dt_idops_t *ops, void *iarg, ulong_t gen) { dt_ident_t *idp; ulong_t h; if (dhp->dh_tmpl != NULL) dt_idhash_populate(dhp); /* fill hash w/ initial population */ idp = dt_ident_create(name, kind, flags, id, attr, vers, ops, iarg, gen); if (idp == NULL) return (NULL); h = dt_strtab_hash(name, NULL) % dhp->dh_hashsz; idp->di_next = dhp->dh_hash[h]; dhp->dh_hash[h] = idp; dhp->dh_nelems++; if (dhp->dh_defer != NULL) dhp->dh_defer(dhp, idp); return (idp); } void dt_idhash_xinsert(dt_idhash_t *dhp, dt_ident_t *idp) { ulong_t h; if (dhp->dh_tmpl != NULL) dt_idhash_populate(dhp); /* fill hash w/ initial population */ h = dt_strtab_hash(idp->di_name, NULL) % dhp->dh_hashsz; idp->di_next = dhp->dh_hash[h]; idp->di_flags &= ~DT_IDFLG_ORPHAN; dhp->dh_hash[h] = idp; dhp->dh_nelems++; if (dhp->dh_defer != NULL) dhp->dh_defer(dhp, idp); } void dt_idhash_delete(dt_idhash_t *dhp, dt_ident_t *key) { size_t len; ulong_t h = dt_strtab_hash(key->di_name, &len) % dhp->dh_hashsz; dt_ident_t **pp = &dhp->dh_hash[h]; dt_ident_t *idp; for (idp = dhp->dh_hash[h]; idp != NULL; idp = idp->di_next) { if (idp == key) break; else pp = &idp->di_next; } assert(idp == key); *pp = idp->di_next; assert(dhp->dh_nelems != 0); dhp->dh_nelems--; if (!(idp->di_flags & DT_IDFLG_ORPHAN)) dt_ident_destroy(idp); } static int dt_idhash_comp(const void *lp, const void *rp) { const dt_ident_t *lhs = *((const dt_ident_t **)lp); const dt_ident_t *rhs = *((const dt_ident_t **)rp); if (lhs->di_id != rhs->di_id) return ((int)(lhs->di_id - rhs->di_id)); else return (strcmp(lhs->di_name, rhs->di_name)); } int dt_idhash_iter(dt_idhash_t *dhp, dt_idhash_f *func, void *data) { dt_ident_t **ids; dt_ident_t *idp; ulong_t i, j, n; int rv; if (dhp->dh_tmpl != NULL) dt_idhash_populate(dhp); /* fill hash w/ initial population */ n = dhp->dh_nelems; ids = alloca(sizeof (dt_ident_t *) * n); for (i = 0, j = 0; i < dhp->dh_hashsz; i++) { for (idp = dhp->dh_hash[i]; idp != NULL; idp = idp->di_next) ids[j++] = idp; } qsort(ids, dhp->dh_nelems, sizeof (dt_ident_t *), dt_idhash_comp); for (i = 0; i < n; i++) { if ((rv = func(dhp, ids[i], data)) != 0) return (rv); } return (0); } dt_ident_t * dt_idstack_lookup(dt_idstack_t *sp, const char *name) { dt_idhash_t *dhp; dt_ident_t *idp; for (dhp = dt_list_prev(&sp->dids_list); dhp != NULL; dhp = dt_list_prev(dhp)) { if ((idp = dt_idhash_lookup(dhp, name)) != NULL) return (idp); } return (NULL); } void dt_idstack_push(dt_idstack_t *sp, dt_idhash_t *dhp) { dt_list_append(&sp->dids_list, dhp); } void dt_idstack_pop(dt_idstack_t *sp, dt_idhash_t *dhp) { assert(dt_list_prev(&sp->dids_list) == dhp); dt_list_delete(&sp->dids_list, dhp); } dt_ident_t * dt_ident_create(const char *name, ushort_t kind, ushort_t flags, uint_t id, dtrace_attribute_t attr, uint_t vers, const dt_idops_t *ops, void *iarg, ulong_t gen) { dt_ident_t *idp; char *s = NULL; if ((name != NULL && (s = strdup(name)) == NULL) || (idp = malloc(sizeof (dt_ident_t))) == NULL) { free(s); return (NULL); } idp->di_name = s; idp->di_kind = kind; idp->di_flags = flags; idp->di_id = id; idp->di_attr = attr; idp->di_vers = vers; idp->di_ops = ops; idp->di_iarg = iarg; idp->di_data = NULL; idp->di_ctfp = NULL; idp->di_type = CTF_ERR; idp->di_next = NULL; idp->di_gen = gen; idp->di_lineno = yylineno; return (idp); } /* * Destroy an individual identifier. This code must be kept in sync with the * dt_idhash_destroy() function below, which separates out the call to di_dtor. */ void dt_ident_destroy(dt_ident_t *idp) { idp->di_ops->di_dtor(idp); free(idp->di_name); free(idp); } void dt_ident_morph(dt_ident_t *idp, ushort_t kind, const dt_idops_t *ops, void *iarg) { idp->di_ops->di_dtor(idp); idp->di_kind = kind; idp->di_ops = ops; idp->di_iarg = iarg; idp->di_data = NULL; } dtrace_attribute_t dt_ident_cook(dt_node_t *dnp, dt_ident_t *idp, dt_node_t **pargp) { dtrace_attribute_t attr; dt_node_t *args, *argp; int argc = 0; attr = dt_node_list_cook(pargp, DT_IDFLG_REF); args = pargp ? *pargp : NULL; for (argp = args; argp != NULL; argp = argp->dn_list) argc++; idp->di_ops->di_cook(dnp, idp, argc, args); if (idp->di_flags & DT_IDFLG_USER) dnp->dn_flags |= DT_NF_USERLAND; return (dt_attr_min(attr, idp->di_attr)); } void dt_ident_type_assign(dt_ident_t *idp, ctf_file_t *fp, ctf_id_t type) { idp->di_ctfp = fp; idp->di_type = type; } dt_ident_t * dt_ident_resolve(dt_ident_t *idp) { while (idp->di_flags & DT_IDFLG_INLINE) { const dt_node_t *dnp = ((dt_idnode_t *)idp->di_iarg)->din_root; if (dnp == NULL) break; /* can't resolve any further yet */ switch (dnp->dn_kind) { case DT_NODE_VAR: case DT_NODE_SYM: case DT_NODE_FUNC: case DT_NODE_AGG: case DT_NODE_INLINE: case DT_NODE_PROBE: idp = dnp->dn_ident; continue; } if (dt_node_is_dynamic(dnp)) idp = dnp->dn_ident; else break; } return (idp); } size_t dt_ident_size(dt_ident_t *idp) { idp = dt_ident_resolve(idp); return (idp->di_ops->di_size(idp)); } int dt_ident_unref(const dt_ident_t *idp) { return (idp->di_gen == yypcb->pcb_hdl->dt_gen && (idp->di_flags & (DT_IDFLG_REF|DT_IDFLG_MOD|DT_IDFLG_DECL)) == 0); } const char * dt_idkind_name(uint_t kind) { switch (kind) { case DT_IDENT_ARRAY: return ("associative array"); case DT_IDENT_SCALAR: return ("scalar"); case DT_IDENT_PTR: return ("pointer"); case DT_IDENT_FUNC: return ("function"); case DT_IDENT_AGG: return ("aggregation"); case DT_IDENT_AGGFUNC: return ("aggregating function"); case DT_IDENT_ACTFUNC: return ("tracing function"); case DT_IDENT_XLSOU: return ("translated data"); case DT_IDENT_XLPTR: return ("pointer to translated data"); case DT_IDENT_SYMBOL: return ("external symbol reference"); case DT_IDENT_ENUM: return ("enumerator"); case DT_IDENT_PRAGAT: return ("#pragma attributes"); case DT_IDENT_PRAGBN: return ("#pragma binding"); case DT_IDENT_PROBE: return ("probe definition"); default: return (""); } }