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;
168 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
171 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
172 dof_init_debug = B_TRUE;
174 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
175 dprintf(1, "couldn't discover module name or address\n");
180 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
181 dprintf(1, "couldn't discover link map ID\n");
187 if ((modname = strrchr(lmp->l_name, '/')) == NULL)
188 modname = lmp->l_name;
192 elf_version(EV_CURRENT);
193 if ((efd = open(lmp->l_name, O_RDONLY, 0)) < 0) {
194 dprintf(1, "couldn't open file for reading\n");
197 if ((e = elf_begin(efd, ELF_C_READ, NULL)) == NULL) {
198 dprintf(1, "elf_begin failed\n");
202 elf_getshdrstrndx(e, &shstridx);
204 while ((scn = elf_nextscn(e, scn)) != NULL) {
205 gelf_getshdr(scn, &shdr);
206 if (shdr.sh_type == SHT_SYMTAB) {
207 symtabidx = shdr.sh_link;
208 symtabdata = elf_getdata(scn, NULL);
209 } else if (shdr.sh_type == SHT_DYNSYM) {
210 dynsymidx = shdr.sh_link;
211 dynsymdata = elf_getdata(scn, NULL);
212 } else if (shdr.sh_type == SHT_PROGBITS) {
213 s = elf_strptr(e, shstridx, shdr.sh_name);
214 if (s && strcmp(s, ".SUNW_dof") == 0) {
215 dofdata = elf_getdata(scn, NULL);
216 dof = dofdata->d_buf;
221 dprintf(1, "SUNW_dof section not found\n");
227 while ((char *) dof < (char *) dofdata->d_buf + dofdata->d_size) {
229 dof_next = (void *) ((char *) dof + dof->dofh_filesz);
232 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
233 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
234 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
235 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
236 dprintf(0, ".SUNW_dof section corrupt\n");
240 elf = (void *)lmp->l_addr;
242 dh.dofhp_dof = (uintptr_t)dof;
243 dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
246 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
249 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
250 "LM%lu`%s", lmid, modname);
253 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
256 if ((fd = open64(devnamep, O_RDWR)) < 0) {
257 dprintf(1, "failed to open helper device %s", devnamep);
260 * If the device path wasn't explicitly set, try again with
261 * the old device path.
266 devnamep = olddevname;
268 if ((fd = open64(devnamep, O_RDWR)) < 0) {
269 dprintf(1, "failed to open helper device %s", devnamep);
278 * We need to fix the base address of each probe since this wasn't
279 * done by ld(1). (ld(1) needs to grow support for parsing the
282 * The complexity of this is not that great. The first for loop
283 * iterates over the sections inside the DOF file. There are usually
284 * 10 sections here. We asume the STRTAB section comes first and the
285 * PROBES section comes after. Since we are only interested in fixing
286 * data inside the PROBES section we quit the for loop after processing
287 * the PROBES section. It's usually the case that the first section
288 * is the STRTAB section and the second section is the PROBES section,
289 * so this for loop is not meaningful when doing complexity analysis.
291 * After finding the probes section, we iterate over the symbols
292 * in the symtab section. When we find a symbol name that matches
293 * the probe function name, we fix it. If we have fixed all the
294 * probes, we exit all the loops and we are done.
295 * The number of probes is given by the variable 'nprobes' and this
296 * depends entirely on the user, but some optimizations were done.
298 * We are assuming the number of probes is less than the number of
299 * symbols (libc can have 4k symbols, for example).
301 secstart = sec = (dof_sec_t *)(dof + 1);
303 for (i = 0; i < dof->dofh_secnum; i++, sec++) {
304 if (sec->dofs_type != DOF_SECT_PROVIDER)
307 dofprovider = (void *) (buf + sec->dofs_offset);
308 dofstrtab = secstart + dofprovider->dofpv_strtab;
309 dofprobes = secstart + dofprovider->dofpv_probes;
311 if (dofstrtab->dofs_type != DOF_SECT_STRTAB) {
312 fprintf(stderr, "WARNING: expected STRTAB section, but got %d\n",
313 dofstrtab->dofs_type);
316 if (dofprobes->dofs_type != DOF_SECT_PROBES) {
317 fprintf(stderr, "WARNING: expected PROBES section, but got %d\n",
318 dofprobes->dofs_type);
322 dprintf(1, "found provider %p\n", dofprovider);
323 dofstrtabraw = (char *)(buf + dofstrtab->dofs_offset);
324 nprobes = dofprobes->dofs_size / dofprobes->dofs_entsize;
325 fixsymbol(e, symtabdata, symtabidx, nprobes, buf, dofprobes, &fixedprobes,
327 if (fixedprobes != nprobes) {
329 * If we haven't fixed all the probes using the
330 * symtab section, look inside the dynsym
333 fixsymbol(e, dynsymdata, dynsymidx, nprobes, buf, dofprobes,
334 &fixedprobes, dofstrtabraw);
336 if (fixedprobes != nprobes) {
337 fprintf(stderr, "WARNING: number of probes "
338 "fixed does not match the number of "
339 "defined probes (%d != %d, "
340 "respectively)\n", fixedprobes, nprobes);
341 fprintf(stderr, "WARNING: some probes might "
342 "not fire or your program might crash\n");
346 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
347 dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
349 dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
358 /* End of while loop */
368 #pragma fini(dtrace_dof_fini)
370 static void dtrace_dof_fini(void) __attribute__ ((destructor));
374 dtrace_dof_fini(void)
378 if ((fd = open64(devnamep, O_RDWR)) < 0) {
379 dprintf(1, "failed to open helper device %s", devnamep);
383 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
384 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
386 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);