4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
33 #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";
59 static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
61 static const char *modname; /* Name of this load object */
62 static int gen; /* DOF helper generation */
63 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
66 dprintf(int debug, const char *fmt, ...)
70 if (debug && getenv("DTRACE_DOF_INIT_DEBUG") == NULL)
76 (void) fprintf(stderr, "dtrace DOF: ");
78 (void) fprintf(stderr, "dtrace DOF %s: ", modname);
80 (void) vfprintf(stderr, fmt, ap);
82 if (fmt[strlen(fmt) - 1] != '\n')
83 (void) fprintf(stderr, ": %s\n", strerror(errno));
89 #pragma init(dtrace_dof_init)
91 static void dtrace_dof_init(void) __attribute__ ((constructor));
97 dof_hdr_t *dof = &__SUNW_dof;
108 struct link_map *lmp;
114 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
117 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
118 dprintf(1, "couldn't discover module name or address\n");
123 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
124 dprintf(1, "couldn't discover link map ID\n");
129 if ((modname = strrchr(lmp->l_name, '/')) == NULL)
130 modname = lmp->l_name;
134 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
135 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
136 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
137 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
138 dprintf(0, ".SUNW_dof section corrupt\n");
142 elf = (void *)lmp->l_addr;
144 dh.dofhp_dof = (uintptr_t)dof;
145 dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
148 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
151 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
152 "LM%lu`%s", lmid, modname);
155 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
158 if ((fd = open64(devnamep, O_RDWR)) < 0) {
159 dprintf(1, "failed to open helper device %s", devnamep);
162 * If the device path wasn't explicitly set, try again with
163 * the old device path.
168 devnamep = olddevname;
170 if ((fd = open64(devnamep, O_RDWR)) < 0) {
171 dprintf(1, "failed to open helper device %s", devnamep);
176 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
177 dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
179 dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
185 #pragma fini(dtrace_dof_fini)
187 static void dtrace_dof_fini(void) __attribute__ ((destructor));
191 dtrace_dof_fini(void)
195 if ((fd = open64(devnamep, O_RDWR)) < 0) {
196 dprintf(1, "failed to open helper device %s", devnamep);
200 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
201 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
203 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);