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 * Use is subject to license terms.
30 #include <sys/dtrace.h>
39 * In Solaris 10 GA, the only mechanism for communicating helper information
40 * is through the DTrace helper pseudo-device node in /devices; there is
41 * no /dev link. Because of this, USDT providers and helper actions don't
42 * work inside of non-global zones. This issue was addressed by adding
43 * the /dev and having this initialization code use that /dev link. If the
44 * /dev link doesn't exist it falls back to looking for the /devices node
45 * as this code may be embedded in a binary which runs on Solaris 10 GA.
47 * Users may set the following environment variable to affect the way
48 * helper initialization takes place:
50 * DTRACE_DOF_INIT_DEBUG enable debugging output
51 * DTRACE_DOF_INIT_DISABLE disable helper loading
52 * DTRACE_DOF_INIT_DEVNAME set the path to the helper node
55 static const char *devnamep = "/dev/dtrace/helper";
56 static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
58 static const char *modname; /* Name of this load object */
59 static int gen; /* DOF helper generation */
60 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
61 static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */
64 dprintf(int debug, const char *fmt, ...)
68 if (debug && !dof_init_debug)
74 (void) fprintf(stderr, "dtrace DOF: ");
76 (void) fprintf(stderr, "dtrace DOF %s: ", modname);
78 (void) vfprintf(stderr, fmt, ap);
80 if (fmt[strlen(fmt) - 1] != '\n')
81 (void) fprintf(stderr, ": %s\n", strerror(errno));
87 #pragma init(dtrace_dof_init)
89 static void dtrace_dof_init(void) __attribute__ ((constructor));
95 dof_hdr_t *dof = &__SUNW_dof;
106 struct link_map *lmp;
112 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
115 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
116 dof_init_debug = B_TRUE;
118 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
119 dprintf(1, "couldn't discover module name or address\n");
124 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
125 dprintf(1, "couldn't discover link map ID\n");
130 if ((modname = strrchr(lmp->l_name, '/')) == NULL)
131 modname = lmp->l_name;
135 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
136 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
137 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
138 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
139 dprintf(0, ".SUNW_dof section corrupt\n");
143 elf = (void *)lmp->l_addr;
145 dh.dofhp_dof = (uintptr_t)dof;
146 dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
149 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
152 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
153 "LM%lu`%s", lmid, modname);
156 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
159 if ((fd = open64(devnamep, O_RDWR)) < 0) {
160 dprintf(1, "failed to open helper device %s", devnamep);
163 * If the device path wasn't explicitly set, try again with
164 * the old device path.
169 devnamep = olddevname;
171 if ((fd = open64(devnamep, O_RDWR)) < 0) {
172 dprintf(1, "failed to open helper device %s", devnamep);
177 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
178 dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
180 dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
186 #pragma fini(dtrace_dof_fini)
188 static void dtrace_dof_fini(void) __attribute__ ((destructor));
192 dtrace_dof_fini(void)
196 if ((fd = open64(devnamep, O_RDWR)) < 0) {
197 dprintf(1, "failed to open helper device %s", devnamep);
201 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
202 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
204 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);