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