xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/drti.c (revision d65af1e7b1ff4806881f2c46d684d004b70e5cc9)
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  * Copyright 2013 Voxer Inc. All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <dlfcn.h>
30 #include <link.h>
31 #include <sys/dtrace.h>
32 
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <libelf.h>
39 #include <gelf.h>
40 
41 /*
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.
49  *
50  * Users may set the following environment variable to affect the way
51  * helper initialization takes place:
52  *
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
56  */
57 
58 static const char *devnamep = "/dev/dtrace/helper";
59 #if defined(sun)
60 static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
61 #endif
62 
63 static const char *modname;	/* Name of this load object */
64 static int gen;			/* DOF helper generation */
65 #if defined(sun)
66 extern dof_hdr_t __SUNW_dof;	/* DOF defined in the .SUNW_dof section */
67 #endif
68 static boolean_t dof_init_debug = B_FALSE;	/* From DTRACE_DOF_INIT_DEBUG */
69 
70 static void
71 dprintf(int debug, const char *fmt, ...)
72 {
73 	va_list ap;
74 
75 	if (debug && !dof_init_debug)
76 		return;
77 
78 	va_start(ap, fmt);
79 
80 	if (modname == NULL)
81 		(void) fprintf(stderr, "dtrace DOF: ");
82 	else
83 		(void) fprintf(stderr, "dtrace DOF %s: ", modname);
84 
85 	(void) vfprintf(stderr, fmt, ap);
86 
87 	if (fmt[strlen(fmt) - 1] != '\n')
88 		(void) fprintf(stderr, ": %s\n", strerror(errno));
89 
90 	va_end(ap);
91 }
92 
93 #if defined(sun)
94 #pragma init(dtrace_dof_init)
95 #else
96 static void dtrace_dof_init(void) __attribute__ ((constructor));
97 #endif
98 
99 static void
100 dtrace_dof_init(void)
101 {
102 #if defined(sun)
103 	dof_hdr_t *dof = &__SUNW_dof;
104 #else
105 	dof_hdr_t *dof = NULL;
106 #endif
107 #ifdef _LP64
108 	Elf64_Ehdr *elf;
109 #else
110 	Elf32_Ehdr *elf;
111 #endif
112 	dof_helper_t dh;
113 	Link_map *lmp;
114 #if defined(sun)
115 	Lmid_t lmid;
116 #else
117 	u_long lmid = 0;
118 #endif
119 	int fd;
120 	const char *p;
121 #if !defined(sun)
122 	Elf *e;
123 	Elf_Scn *scn = NULL;
124 	Elf_Data *dofdata = NULL;
125 	dof_hdr_t *dof_next = NULL;
126 	GElf_Shdr shdr;
127 	int efd;
128 	char *s;
129 	size_t shstridx;
130 #endif
131 
132 	if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
133 		return;
134 
135 	if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
136 		dof_init_debug = B_TRUE;
137 
138 	if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
139 		dprintf(1, "couldn't discover module name or address\n");
140 		return;
141 	}
142 
143 #if defined(sun)
144 	if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
145 		dprintf(1, "couldn't discover link map ID\n");
146 		return;
147 	}
148 #endif
149 
150 	if ((modname = strrchr(lmp->l_name, '/')) == NULL)
151 		modname = lmp->l_name;
152 	else
153 		modname++;
154 #if !defined(sun)
155 	elf_version(EV_CURRENT);
156 	if ((efd = open(lmp->l_name, O_RDONLY, 0)) < 0) {
157 		dprintf(1, "couldn't open file for reading\n");
158 		return;
159 	}
160 	if ((e = elf_begin(efd, ELF_C_READ, NULL)) == NULL) {
161 		dprintf(1, "elf_begin failed\n");
162 		close(efd);
163 		return;
164 	}
165 	elf_getshdrstrndx(e, &shstridx);
166 	dof = NULL;
167 	while ((scn = elf_nextscn(e, scn)) != NULL) {
168 		gelf_getshdr(scn, &shdr);
169 		if (shdr.sh_type == SHT_SUNW_dof) {
170 			s = elf_strptr(e, shstridx, shdr.sh_name);
171 			if (s != NULL && strcmp(s, ".SUNW_dof") == 0) {
172 				dofdata = elf_getdata(scn, NULL);
173 				dof = dofdata->d_buf;
174 			}
175 		}
176 	}
177 	if (dof == NULL) {
178 		dprintf(1, "SUNW_dof section not found\n");
179 		elf_end(e);
180 		close(efd);
181 		return;
182 	}
183 
184 	while ((char *) dof < (char *) dofdata->d_buf + dofdata->d_size) {
185 		dof_next = (void *) ((char *) dof + dof->dofh_filesz);
186 #endif
187 
188 	if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
189 	    dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
190 	    dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
191 	    dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
192 		dprintf(0, ".SUNW_dof section corrupt\n");
193 		return;
194 	}
195 
196 	elf = (void *)lmp->l_addr;
197 
198 	dh.dofhp_dof = (uintptr_t)dof;
199 	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
200 
201 	if (lmid == 0) {
202 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
203 		    "%s", modname);
204 	} else {
205 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
206 		    "LM%lu`%s", lmid, modname);
207 	}
208 
209 	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
210 		devnamep = p;
211 
212 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
213 		dprintf(1, "failed to open helper device %s", devnamep);
214 #if defined(sun)
215 		/*
216 		 * If the device path wasn't explicitly set, try again with
217 		 * the old device path.
218 		 */
219 		if (p != NULL)
220 			return;
221 
222 		devnamep = olddevname;
223 
224 		if ((fd = open64(devnamep, O_RDWR)) < 0) {
225 			dprintf(1, "failed to open helper device %s", devnamep);
226 			return;
227 		}
228 #else
229 		return;
230 #endif
231 	}
232 	if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
233 		dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
234 	else {
235 		dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
236 #if !defined(sun)
237 		gen = dh.gen;
238 #endif
239 	}
240 
241 	(void) close(fd);
242 
243 #if !defined(sun)
244 		/* End of while loop */
245 		dof = dof_next;
246 	}
247 
248 	elf_end(e);
249 	(void) close(efd);
250 #endif
251 }
252 
253 #if defined(sun)
254 #pragma fini(dtrace_dof_fini)
255 #else
256 static void dtrace_dof_fini(void) __attribute__ ((destructor));
257 #endif
258 
259 static void
260 dtrace_dof_fini(void)
261 {
262 	int fd;
263 
264 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
265 		dprintf(1, "failed to open helper device %s", devnamep);
266 		return;
267 	}
268 
269 	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
270 		dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
271 	else
272 		dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
273 
274 	(void) close(fd);
275 }
276