1 | /*
|
---|
2 | * CDDL HEADER START
|
---|
3 | *
|
---|
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.
|
---|
7 | *
|
---|
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.
|
---|
12 | *
|
---|
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]
|
---|
18 | *
|
---|
19 | * CDDL HEADER END
|
---|
20 | */
|
---|
21 | /*
|
---|
22 | * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
---|
23 | * Use is subject to license terms.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include <unistd.h>
|
---|
27 | #include <fcntl.h>
|
---|
28 | #include <dlfcn.h>
|
---|
29 | #include <link.h>
|
---|
30 | #include <sys/dtrace.h>
|
---|
31 |
|
---|
32 | #include <stdarg.h>
|
---|
33 | #include <stdio.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 | #include <string.h>
|
---|
36 | #include <errno.h>
|
---|
37 |
|
---|
38 | /*
|
---|
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.
|
---|
46 | *
|
---|
47 | * Users may set the following environment variable to affect the way
|
---|
48 | * helper initialization takes place:
|
---|
49 | *
|
---|
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
|
---|
53 | */
|
---|
54 |
|
---|
55 | static const char *devname = "/dev/dtrace/helper";
|
---|
56 | static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
|
---|
57 |
|
---|
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 */
|
---|
62 |
|
---|
63 | static void
|
---|
64 | dprintf(int debug, const char *fmt, ...)
|
---|
65 | {
|
---|
66 | va_list ap;
|
---|
67 |
|
---|
68 | if (debug && !dof_init_debug)
|
---|
69 | return;
|
---|
70 |
|
---|
71 | va_start(ap, fmt);
|
---|
72 |
|
---|
73 | if (modname == NULL)
|
---|
74 | (void) fprintf(stderr, "dtrace DOF: ");
|
---|
75 | else
|
---|
76 | (void) fprintf(stderr, "dtrace DOF %s: ", modname);
|
---|
77 |
|
---|
78 | (void) vfprintf(stderr, fmt, ap);
|
---|
79 |
|
---|
80 | if (fmt[strlen(fmt) - 1] != '\n')
|
---|
81 | (void) fprintf(stderr, ": %s\n", strerror(errno));
|
---|
82 |
|
---|
83 | va_end(ap);
|
---|
84 | }
|
---|
85 |
|
---|
86 | #pragma init(dtrace_dof_init)
|
---|
87 | static void
|
---|
88 | dtrace_dof_init(void)
|
---|
89 | {
|
---|
90 | dof_hdr_t *dof = &__SUNW_dof;
|
---|
91 | #ifdef _LP64
|
---|
92 | Elf64_Ehdr *elf;
|
---|
93 | #else
|
---|
94 | Elf32_Ehdr *elf;
|
---|
95 | #endif
|
---|
96 | dof_helper_t dh;
|
---|
97 | Link_map *lmp;
|
---|
98 | Lmid_t lmid;
|
---|
99 | int fd;
|
---|
100 | const char *p;
|
---|
101 |
|
---|
102 | if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
|
---|
103 | return;
|
---|
104 |
|
---|
105 | if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
|
---|
106 | dof_init_debug = B_TRUE;
|
---|
107 |
|
---|
108 | if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
|
---|
109 | dprintf(1, "couldn't discover module name or address\n");
|
---|
110 | return;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
|
---|
114 | dprintf(1, "couldn't discover link map ID\n");
|
---|
115 | return;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if ((modname = strrchr(lmp->l_name, '/')) == NULL)
|
---|
119 | modname = lmp->l_name;
|
---|
120 | else
|
---|
121 | modname++;
|
---|
122 |
|
---|
123 | if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
|
---|
124 | dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
|
---|
125 | dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
|
---|
126 | dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
|
---|
127 | dprintf(0, ".SUNW_dof section corrupt\n");
|
---|
128 | return;
|
---|
129 | }
|
---|
130 |
|
---|
131 | elf = (void *)lmp->l_addr;
|
---|
132 |
|
---|
133 | dh.dofhp_dof = (uintptr_t)dof;
|
---|
134 | dh.dofhp_addr = elf->e_type == ET_DYN ? lmp->l_addr : 0;
|
---|
135 |
|
---|
136 | if (lmid == 0) {
|
---|
137 | (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
|
---|
138 | "%s", modname);
|
---|
139 | } else {
|
---|
140 | (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
|
---|
141 | "LM%lu`%s", lmid, modname);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
|
---|
145 | devname = p;
|
---|
146 |
|
---|
147 | if ((fd = open64(devname, O_RDWR)) < 0) {
|
---|
148 | dprintf(1, "failed to open helper device %s", devname);
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * If the device path wasn't explicitly set, try again with
|
---|
152 | * the old device path.
|
---|
153 | */
|
---|
154 | if (p != NULL)
|
---|
155 | return;
|
---|
156 |
|
---|
157 | devname = olddevname;
|
---|
158 |
|
---|
159 | if ((fd = open64(devname, O_RDWR)) < 0) {
|
---|
160 | dprintf(1, "failed to open helper device %s", devname);
|
---|
161 | return;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
|
---|
166 | dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
|
---|
167 | else
|
---|
168 | dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
|
---|
169 |
|
---|
170 | (void) close(fd);
|
---|
171 | }
|
---|
172 |
|
---|
173 | #pragma fini(dtrace_dof_fini)
|
---|
174 | static void
|
---|
175 | dtrace_dof_fini(void)
|
---|
176 | {
|
---|
177 | int fd;
|
---|
178 |
|
---|
179 | if ((fd = open64(devname, O_RDWR)) < 0) {
|
---|
180 | dprintf(1, "failed to open helper device %s", devname);
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
|
---|
185 | dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
|
---|
186 | else
|
---|
187 | dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
|
---|
188 |
|
---|
189 | (void) close(fd);
|
---|
190 | }
|
---|