4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Copyright 2013 Voxer Inc. All rights reserved.
24 * Use is subject to license terms.
31 #include <sys/dtrace.h>
42 * In Solaris 10 GA, the only mechanism for communicating helper information
43 * is through the DTrace helper pseudo-device node in /devices; there is
44 * no /dev link. Because of this, USDT providers and helper actions don't
45 * work inside of non-global zones. This issue was addressed by adding
46 * the /dev and having this initialization code use that /dev link. If the
47 * /dev link doesn't exist it falls back to looking for the /devices node
48 * as this code may be embedded in a binary which runs on Solaris 10 GA.
50 * Users may set the following environment variable to affect the way
51 * helper initialization takes place:
53 * DTRACE_DOF_INIT_DEBUG enable debugging output
54 * DTRACE_DOF_INIT_DISABLE disable helper loading
55 * DTRACE_DOF_INIT_DEVNAME set the path to the helper node
58 static const char *devnamep = "/dev/dtrace/helper";
60 static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
63 static const char *modname; /* Name of this load object */
64 static int gen; /* DOF helper generation */
66 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
68 static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */
71 dprintf(int debug, const char *fmt, ...)
75 if (debug && !dof_init_debug)
81 (void) fprintf(stderr, "dtrace DOF: ");
83 (void) fprintf(stderr, "dtrace DOF %s: ", modname);
85 (void) vfprintf(stderr, fmt, ap);
87 if (fmt[strlen(fmt) - 1] != '\n')
88 (void) fprintf(stderr, ": %s\n", strerror(errno));
95 fixsymbol(Elf *e, Elf_Data *data, size_t idx, int nprobes, char *buf,
96 dof_sec_t *sec, int *fixedprobes, char *dofstrtab)
100 unsigned char *funcname;
105 while (gelf_getsym(data, j++, &sym) != NULL) {
106 prb = (dof_probe_t *)(void *)(buf + sec->dofs_offset);
108 for (ndx = nprobes; ndx; ndx--, prb += 1) {
109 funcname = dofstrtab + prb->dofpr_func;
110 s = elf_strptr(e, idx, sym.st_name);
111 if (strcmp(s, funcname) == 0) {
112 dprintf(1, "fixing %s() symbol\n", s);
113 prb->dofpr_addr = sym.st_value;
117 if (*fixedprobes == nprobes)
124 #pragma init(dtrace_dof_init)
126 static void dtrace_dof_init(void) __attribute__ ((constructor));
130 dtrace_dof_init(void)
133 dof_hdr_t *dof = &__SUNW_dof;
135 dof_hdr_t *dof = NULL;
148 dof_sec_t *sec, *secstart, *dofstrtab, *dofprobes;
149 dof_provider_t *dofprovider;
157 Elf_Data *symtabdata = NULL, *dynsymdata = NULL, *dofdata = NULL;
158 dof_hdr_t *dof_next = NULL;
163 size_t shstridx, symtabidx = 0, dynsymidx = 0;
166 uint64_t aligned_filesz;
169 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
172 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
173 dof_init_debug = B_TRUE;
175 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
176 dprintf(1, "couldn't discover module name or address\n");
181 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
182 dprintf(1, "couldn't discover link map ID\n");
188 if ((modname = strrchr(lmp->l_name, '/')) == NULL)
189 modname = lmp->l_name;
193 elf_version(EV_CURRENT);
194 if ((efd = open(lmp->l_name, O_RDONLY, 0)) < 0) {
195 dprintf(1, "couldn't open file for reading\n");
198 if ((e = elf_begin(efd, ELF_C_READ, NULL)) == NULL) {
199 dprintf(1, "elf_begin failed\n");
203 elf_getshdrstrndx(e, &shstridx);
205 while ((scn = elf_nextscn(e, scn)) != NULL) {
206 gelf_getshdr(scn, &shdr);
207 if (shdr.sh_type == SHT_SYMTAB) {
208 symtabidx = shdr.sh_link;
209 symtabdata = elf_getdata(scn, NULL);
210 } else if (shdr.sh_type == SHT_DYNSYM) {
211 dynsymidx = shdr.sh_link;
212 dynsymdata = elf_getdata(scn, NULL);
213 } else if (shdr.sh_type == SHT_PROGBITS) {
214 s = elf_strptr(e, shstridx, shdr.sh_name);
215 if (s && strcmp(s, ".SUNW_dof") == 0) {
216 dofdata = elf_getdata(scn, NULL);
217 dof = dofdata->d_buf;
222 dprintf(1, "SUNW_dof section not found\n");
228 while ((char *) dof < (char *) dofdata->d_buf + dofdata->d_size) {
230 aligned_filesz = (shdr.sh_addralign == 0 ? dof->dofh_filesz :
231 roundup2(dof->dofh_filesz, shdr.sh_addralign));
232 dof_next = (void *) ((char *) dof + aligned_filesz);
235 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
236 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
237 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
238 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
239 dprintf(0, ".SUNW_dof section corrupt\n");
243 elf = (void *)lmp->l_addr;
245 dh.dofhp_dof = (uintptr_t)dof;
246 dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
249 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
252 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
253 "LM%lu`%s", lmid, modname);
256 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
259 if ((fd = open64(devnamep, O_RDWR)) < 0) {
260 dprintf(1, "failed to open helper device %s", devnamep);
263 * If the device path wasn't explicitly set, try again with
264 * the old device path.
269 devnamep = olddevname;
271 if ((fd = open64(devnamep, O_RDWR)) < 0) {
272 dprintf(1, "failed to open helper device %s", devnamep);
281 * We need to fix the base address of each probe since this wasn't
282 * done by ld(1). (ld(1) needs to grow support for parsing the
285 * The complexity of this is not that great. The first for loop
286 * iterates over the sections inside the DOF file. There are usually
287 * 10 sections here. We asume the STRTAB section comes first and the
288 * PROBES section comes after. Since we are only interested in fixing
289 * data inside the PROBES section we quit the for loop after processing
290 * the PROBES section. It's usually the case that the first section
291 * is the STRTAB section and the second section is the PROBES section,
292 * so this for loop is not meaningful when doing complexity analysis.
294 * After finding the probes section, we iterate over the symbols
295 * in the symtab section. When we find a symbol name that matches
296 * the probe function name, we fix it. If we have fixed all the
297 * probes, we exit all the loops and we are done.
298 * The number of probes is given by the variable 'nprobes' and this
299 * depends entirely on the user, but some optimizations were done.
301 * We are assuming the number of probes is less than the number of
302 * symbols (libc can have 4k symbols, for example).
304 secstart = sec = (dof_sec_t *)(dof + 1);
306 for (i = 0; i < dof->dofh_secnum; i++, sec++) {
307 if (sec->dofs_type != DOF_SECT_PROVIDER)
310 dofprovider = (void *) (buf + sec->dofs_offset);
311 dofstrtab = secstart + dofprovider->dofpv_strtab;
312 dofprobes = secstart + dofprovider->dofpv_probes;
314 if (dofstrtab->dofs_type != DOF_SECT_STRTAB) {
315 fprintf(stderr, "WARNING: expected STRTAB section, but got %d\n",
316 dofstrtab->dofs_type);
319 if (dofprobes->dofs_type != DOF_SECT_PROBES) {
320 fprintf(stderr, "WARNING: expected PROBES section, but got %d\n",
321 dofprobes->dofs_type);
325 dprintf(1, "found provider %p\n", dofprovider);
326 dofstrtabraw = (char *)(buf + dofstrtab->dofs_offset);
327 nprobes = dofprobes->dofs_size / dofprobes->dofs_entsize;
328 fixsymbol(e, symtabdata, symtabidx, nprobes, buf, dofprobes, &fixedprobes,
330 if (fixedprobes != nprobes) {
332 * If we haven't fixed all the probes using the
333 * symtab section, look inside the dynsym
336 fixsymbol(e, dynsymdata, dynsymidx, nprobes, buf, dofprobes,
337 &fixedprobes, dofstrtabraw);
339 if (fixedprobes != nprobes) {
340 fprintf(stderr, "WARNING: number of probes "
341 "fixed does not match the number of "
342 "defined probes (%d != %d, "
343 "respectively)\n", fixedprobes, nprobes);
344 fprintf(stderr, "WARNING: some probes might "
345 "not fire or your program might crash\n");
349 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
350 dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
352 dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
361 /* End of while loop */
371 #pragma fini(dtrace_dof_fini)
373 static void dtrace_dof_fini(void) __attribute__ ((destructor));
377 dtrace_dof_fini(void)
381 if ((fd = open64(devnamep, O_RDWR)) < 0) {
382 dprintf(1, "failed to open helper device %s", devnamep);
386 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
387 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
389 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);