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 #ifdef __FreeBSD__
147 	elf = (void *)lmp->l_base;
148 #else
149 	elf = (void *)lmp->l_addr;
150 #endif
151 
152 	dh.dofhp_dof = (uintptr_t)dof;
153 #ifdef __FreeBSD__
154 	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_base : 0;
155 	dh.dofhp_pid = getpid();
156 #else
157 	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
158 #endif
159 
160 	if (lmid == 0) {
161 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
162 		    "%s", modname);
163 	} else {
164 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
165 		    "LM%lu`%s", lmid, modname);
166 	}
167 
168 	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
169 		devnamep = p;
170 
171 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
172 		dbg_printf(1, "failed to open helper device %s", devnamep);
173 #ifdef illumos
174 		/*
175 		 * If the device path wasn't explicitly set, try again with
176 		 * the old device path.
177 		 */
178 		if (p != NULL)
179 			return;
180 
181 		devnamep = olddevname;
182 
183 		if ((fd = open64(devnamep, O_RDWR)) < 0) {
184 			dbg_printf(1, "failed to open helper device %s", devnamep);
185 			return;
186 		}
187 #else
188 		return;
189 #endif
190 	}
191 	if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
192 		dbg_printf(1, "DTrace ioctl failed for DOF at %p", dof);
193 	else {
194 		dbg_printf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
195 #ifdef __FreeBSD__
196 		gen = dh.dofhp_gen;
197 #endif
198 	}
199 
200 	(void) close(fd);
201 }
202 
203 #ifdef illumos
204 #pragma fini(dtrace_dof_fini)
205 #else
206 static void dtrace_dof_fini(void) __attribute__ ((destructor));
207 #endif
208 
209 static void
210 dtrace_dof_fini(void)
211 {
212 	int fd;
213 
214 	if ((fd = open64(devnamep, O_RDWR)) < 0) {
215 		dbg_printf(1, "failed to open helper device %s", devnamep);
216 		return;
217 	}
218 
219 	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)
220 		dbg_printf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
221 	else
222 		dbg_printf(1, "DTrace ioctl removed DOF (%d)\n", gen);
223 
224 	(void) close(fd);
225 }
226