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