xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/drti.c (revision 643ac419fafba89f5adda0e0ea75b538727453fb)
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 <sys/types.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <dlfcn.h>
31 #include <link.h>
32 #include <sys/dtrace.h>
33 
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <libelf.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 #ifdef illumos
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 extern dof_hdr_t __SUNW_dof;	/* DOF defined in the .SUNW_dof section */
66 static boolean_t dof_init_debug = B_FALSE;	/* From DTRACE_DOF_INIT_DEBUG */
67 
68 static void
69 dbg_printf(int debug, const char *fmt, ...)
70 {
71 	va_list ap;
72 
73 	if (debug && !dof_init_debug)
74 		return;
75 
76 	va_start(ap, fmt);
77 
78 	if (modname == NULL)
79 		(void) fprintf(stderr, "dtrace DOF: ");
80 	else
81 		(void) fprintf(stderr, "dtrace DOF %s: ", modname);
82 
83 	(void) vfprintf(stderr, fmt, ap);
84 
85 	if (fmt[strlen(fmt) - 1] != '\n')
86 		(void) fprintf(stderr, ": %s\n", strerror(errno));
87 
88 	va_end(ap);
89 }
90 
91 #ifdef illumos
92 #pragma init(dtrace_dof_init)
93 #else
94 static void dtrace_dof_init(void) __attribute__ ((constructor));
95 #endif
96 
97 static void
98 dtrace_dof_init(void)
99 {
100 	dof_hdr_t *dof = &__SUNW_dof;
101 #ifdef _LP64
102 	Elf64_Ehdr *elf;
103 #else
104 	Elf32_Ehdr *elf;
105 #endif
106 	dof_helper_t dh;
107 	Link_map *lmp = NULL;
108 #ifdef illumos
109 	Lmid_t lmid;
110 #else
111 	u_long lmid = 0;
112 #endif
113 	int fd;
114 	const char *p;
115 
116 	if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
117 		return;
118 
119 	if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
120 		dof_init_debug = B_TRUE;
121 
122 	if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
123 		dbg_printf(1, "couldn't discover module name or address\n");
124 		return;
125 	}
126 
127 #ifdef illumos
128 	if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
129 		dbg_printf(1, "couldn't discover link map ID\n");
130 		return;
131 	}
132 #endif
133 
134 	if ((modname = strrchr(lmp->l_name, '/')) == NULL)
135 		modname = lmp->l_name;
136 	else
137 		modname++;
138 
139 	if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
140 	    dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
141 	    dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
142 	    dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
143 		dbg_printf(0, ".SUNW_dof section corrupt\n");
144 		return;
145 	}
146 
147 #ifdef __FreeBSD__
148 	elf = (void *)lmp->l_base;
149 #else
150 	elf = (void *)lmp->l_addr;
151 #endif
152 
153 	dh.dofhp_dof = (uintptr_t)dof;
154 #ifdef __FreeBSD__
155 	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_base : 0;
156 	dh.dofhp_pid = getpid();
157 #else
158 	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
159 #endif
160 
161 	if (lmid == 0) {
162 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
163 		    "%s", modname);
164 	} else {
165 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
166 		    "LM%lu`%s", lmid, modname);
167 	}
168 
169 	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
170 		devnamep = p;
171 
172 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
173 		dbg_printf(1, "failed to open helper device %s", devnamep);
174 #ifdef illumos
175 		/*
176 		 * If the device path wasn't explicitly set, try again with
177 		 * the old device path.
178 		 */
179 		if (p != NULL)
180 			return;
181 
182 		devnamep = olddevname;
183 
184 		if ((fd = open64(devnamep, O_RDWR)) < 0) {
185 			dbg_printf(1, "failed to open helper device %s", devnamep);
186 			return;
187 		}
188 #else
189 		return;
190 #endif
191 	}
192 	if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
193 		dbg_printf(1, "DTrace ioctl failed for DOF at %p", dof);
194 	else {
195 		dbg_printf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
196 #ifdef __FreeBSD__
197 		gen = dh.dofhp_gen;
198 #endif
199 	}
200 
201 	(void) close(fd);
202 }
203 
204 #ifdef illumos
205 #pragma fini(dtrace_dof_fini)
206 #else
207 static void dtrace_dof_fini(void) __attribute__ ((destructor));
208 #endif
209 
210 static void
211 dtrace_dof_fini(void)
212 {
213 	int fd;
214 
215 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
216 		dbg_printf(1, "failed to open helper device %s", devnamep);
217 		return;
218 	}
219 
220 	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
221 		dbg_printf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
222 	else
223 		dbg_printf(1, "DTrace ioctl removed DOF (%d)\n", gen);
224 
225 	(void) close(fd);
226 }
227