xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/drti.c (revision 863dbc940e48d90238c9b184dce81b2dd6b23800)
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 *symtabdata = NULL, *dynsymdata = NULL, *dofdata = NULL;
125 	dof_hdr_t *dof_next = NULL;
126 	GElf_Shdr shdr;
127 	int efd;
128 	char *s;
129 	size_t shstridx, symtabidx = 0, dynsymidx = 0;
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_SYMTAB) {
170 			symtabidx = shdr.sh_link;
171 			symtabdata = elf_getdata(scn, NULL);
172 		} else if (shdr.sh_type == SHT_DYNSYM) {
173 			dynsymidx = shdr.sh_link;
174 			dynsymdata = elf_getdata(scn, NULL);
175 		} else if (shdr.sh_type == SHT_SUNW_dof) {
176 			s = elf_strptr(e, shstridx, shdr.sh_name);
177 			if  (s != NULL && strcmp(s, ".SUNW_dof") == 0) {
178 				dofdata = elf_getdata(scn, NULL);
179 				dof = dofdata->d_buf;
180 			}
181 		}
182 	}
183 	if (dof == NULL) {
184 		dprintf(1, "SUNW_dof section not found\n");
185 		elf_end(e);
186 		close(efd);
187 		return;
188 	}
189 
190 	while ((char *) dof < (char *) dofdata->d_buf + dofdata->d_size) {
191 		dof_next = (void *) ((char *) dof + dof->dofh_filesz);
192 #endif
193 
194 	if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
195 	    dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
196 	    dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
197 	    dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
198 		dprintf(0, ".SUNW_dof section corrupt\n");
199 		return;
200 	}
201 
202 	elf = (void *)lmp->l_addr;
203 
204 	dh.dofhp_dof = (uintptr_t)dof;
205 	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
206 
207 	if (lmid == 0) {
208 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
209 		    "%s", modname);
210 	} else {
211 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
212 		    "LM%lu`%s", lmid, modname);
213 	}
214 
215 	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
216 		devnamep = p;
217 
218 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
219 		dprintf(1, "failed to open helper device %s", devnamep);
220 #if defined(sun)
221 		/*
222 		 * If the device path wasn't explicitly set, try again with
223 		 * the old device path.
224 		 */
225 		if (p != NULL)
226 			return;
227 
228 		devnamep = olddevname;
229 
230 		if ((fd = open64(devnamep, O_RDWR)) < 0) {
231 			dprintf(1, "failed to open helper device %s", devnamep);
232 			return;
233 		}
234 #else
235 		return;
236 #endif
237 	}
238 	if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
239 		dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
240 	else {
241 		dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
242 #if !defined(sun)
243 		gen = dh.gen;
244 #endif
245 	}
246 
247 	(void) close(fd);
248 
249 #if !defined(sun)
250 		/* End of while loop */
251 		dof = dof_next;
252 	}
253 
254 	elf_end(e);
255 	(void) close(efd);
256 #endif
257 }
258 
259 #if defined(sun)
260 #pragma fini(dtrace_dof_fini)
261 #else
262 static void dtrace_dof_fini(void) __attribute__ ((destructor));
263 #endif
264 
265 static void
266 dtrace_dof_fini(void)
267 {
268 	int fd;
269 
270 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
271 		dprintf(1, "failed to open helper device %s", devnamep);
272 		return;
273 	}
274 
275 	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
276 		dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
277 	else
278 		dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
279 
280 	(void) close(fd);
281 }
282