xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/common/drti.c (revision ad49149131b7552cf1c4fc41370cc055d9b4a80e)
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 /*
28  * XXX Hack: Force the use of sys/exec_elf.h, not elfdefinitions.h.
29  * Why?
30  *
31  * - sys/dtrace.h is needed because (XXX why?).
32  *   => sys/dtrace.h pulls in sys/ctf_api.h
33  *   => which pulls in sys/elf.h
34  *   => which pulls in elfdefinitions.h (XXX should it?)
35  *
36  * - link.h is needed for Link_map.
37  *   => link.h pulls in link_elf.h
38  *   => which pulls in sys/exec_elf.h
39  *
40  * But elfdefinitions.h and sys/exec_elf.h collide over most basic ELF
41  * definitions.  Since we don't need elfdefinitions.h otherwise, prefer
42  * sys/exec_elf.h.
43  */
44 #define	_SYS_ELFDEFINITIONS_H_
45 
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <dlfcn.h>
49 #include <link.h>
50 #include <sys/dtrace.h>
51 #include <sys/ioctl.h>
52 
53 #include <stdarg.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <errno.h>
58 #include <libelf.h>
59 
60 /*
61  * In Solaris 10 GA, the only mechanism for communicating helper information
62  * is through the DTrace helper pseudo-device node in /devices; there is
63  * no /dev link. Because of this, USDT providers and helper actions don't
64  * work inside of non-global zones. This issue was addressed by adding
65  * the /dev and having this initialization code use that /dev link. If the
66  * /dev link doesn't exist it falls back to looking for the /devices node
67  * as this code may be embedded in a binary which runs on Solaris 10 GA.
68  *
69  * Users may set the following environment variable to affect the way
70  * helper initialization takes place:
71  *
72  *	DTRACE_DOF_INIT_DEBUG		enable debugging output
73  *	DTRACE_DOF_INIT_DISABLE		disable helper loading
74  *	DTRACE_DOF_INIT_DEVNAME		set the path to the helper node
75  */
76 
77 static const char *devnamep = "/dev/dtrace/helper";
78 #ifdef illumos
79 static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
80 #endif
81 
82 static const char *modname;	/* Name of this load object */
83 static int gen;			/* DOF helper generation */
84 extern dof_hdr_t __SUNW_dof;	/* DOF defined in the .SUNW_dof section */
85 static boolean_t dof_init_debug = B_FALSE;	/* From DTRACE_DOF_INIT_DEBUG */
86 
87 static void __printflike(2,3)
dbg_printf(int debug,const char * fmt,...)88 dbg_printf(int debug, const char *fmt, ...)
89 {
90 	va_list ap;
91 
92 	if (debug && !dof_init_debug)
93 		return;
94 
95 	va_start(ap, fmt);
96 
97 	if (modname == NULL)
98 		(void) fprintf(stderr, "dtrace DOF: ");
99 	else
100 		(void) fprintf(stderr, "dtrace DOF %s: ", modname);
101 
102 	(void) vfprintf(stderr, fmt, ap);
103 
104 	if (fmt[strlen(fmt) - 1] != '\n')
105 		(void) fprintf(stderr, ": %s\n", strerror(errno));
106 
107 	va_end(ap);
108 }
109 
110 #ifdef illumos
111 #pragma init(dtrace_dof_init)
112 #else
113 static void dtrace_dof_init(void) __attribute__ ((constructor));
114 #endif
115 
116 static void
dtrace_dof_init(void)117 dtrace_dof_init(void)
118 {
119 	dof_hdr_t *dof = &__SUNW_dof;
120 #ifdef _LP64
121 	Elf64_Ehdr *elf;
122 #else
123 	Elf32_Ehdr *elf;
124 #endif
125 	dof_helper_t dh;
126 	Link_map *lmp = NULL;
127 #ifdef illumos
128 	Lmid_t lmid;
129 #else
130 	u_long lmid = 0;
131 #endif
132 	int fd;
133 	const char *p;
134 
135 	if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
136 		return;
137 
138 	if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
139 		dof_init_debug = B_TRUE;
140 
141 	if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
142 		dbg_printf(1, "couldn't discover module name or address\n");
143 		return;
144 	}
145 
146 #ifdef illumos
147 	if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
148 		dbg_printf(1, "couldn't discover link map ID\n");
149 		return;
150 	}
151 #endif
152 
153 	if ((modname = strrchr(lmp->l_name, '/')) == NULL)
154 		modname = lmp->l_name;
155 	else
156 		modname++;
157 
158 	if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
159 	    dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
160 	    dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
161 	    dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
162 		dbg_printf(0, ".SUNW_dof section corrupt\n");
163 		return;
164 	}
165 
166 	elf = (void *)lmp->l_addr;
167 
168 	dh.dofhp_dof = (uintptr_t)dof;
169 	dh.dofhp_addr = elf && elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
170 #if defined(__FreeBSD__) || defined(__NetBSD__)
171 	dh.dofhp_pid = getpid();
172 #endif
173 
174 	if (lmid == 0) {
175 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
176 		    "%s", modname);
177 	} else {
178 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
179 		    "LM%lu`%s", lmid, modname);
180 	}
181 
182 	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
183 		devnamep = p;
184 
185 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
186 		dbg_printf(1, "failed to open helper device %s", devnamep);
187 #ifdef illumos
188 		/*
189 		 * If the device path wasn't explicitly set, try again with
190 		 * the old device path.
191 		 */
192 		if (p != NULL)
193 			return;
194 
195 		devnamep = olddevname;
196 
197 		if ((fd = open64(devnamep, O_RDWR)) < 0) {
198 			dbg_printf(1, "failed to open helper device %s", devnamep);
199 			return;
200 		}
201 #else
202 		return;
203 #endif
204 	}
205 	if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
206 		dbg_printf(1, "DTrace ioctl failed for DOF at %p", dof);
207 	else {
208 		dbg_printf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
209 #if defined(__FreeBSD__) || defined(__NetBSD__)
210 		gen = dh.dofhp_gen;
211 #endif
212 	}
213 
214 	(void) close(fd);
215 }
216 
217 #ifdef illumos
218 #pragma fini(dtrace_dof_fini)
219 #else
220 static void dtrace_dof_fini(void) __attribute__ ((destructor));
221 #endif
222 
223 static void
dtrace_dof_fini(void)224 dtrace_dof_fini(void)
225 {
226 	int fd;
227 
228 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
229 		dbg_printf(1, "failed to open helper device %s", devnamep);
230 		return;
231 	}
232 
233 	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
234 		dbg_printf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
235 	else
236 		dbg_printf(1, "DTrace ioctl removed DOF (%d)\n", gen);
237 
238 	(void) close(fd);
239 }
240