drti.c revision 178572
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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#include <unistd.h>
30#include <fcntl.h>
31#include <dlfcn.h>
32#include <link.h>
33#include <sys/dtrace.h>
34
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <errno.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
58static const char *devnamep = "/dev/dtrace/helper";
59static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
60
61static const char *modname;	/* Name of this load object */
62static int gen;			/* DOF helper generation */
63extern dof_hdr_t __SUNW_dof;	/* DOF defined in the .SUNW_dof section */
64
65static void
66dprintf(int debug, const char *fmt, ...)
67{
68	va_list ap;
69
70	if (debug && getenv("DTRACE_DOF_INIT_DEBUG") == NULL)
71		return;
72
73	va_start(ap, fmt);
74
75	if (modname == NULL)
76		(void) fprintf(stderr, "dtrace DOF: ");
77	else
78		(void) fprintf(stderr, "dtrace DOF %s: ", modname);
79
80	(void) vfprintf(stderr, fmt, ap);
81
82	if (fmt[strlen(fmt) - 1] != '\n')
83		(void) fprintf(stderr, ": %s\n", strerror(errno));
84
85	va_end(ap);
86}
87
88#if defined(sun)
89#pragma init(dtrace_dof_init)
90#else
91static void dtrace_dof_init(void) __attribute__ ((constructor));
92#endif
93
94static void
95dtrace_dof_init(void)
96{
97	dof_hdr_t *dof = &__SUNW_dof;
98#ifdef _LP64
99	Elf64_Ehdr *elf;
100#else
101	Elf32_Ehdr *elf;
102#endif
103	dof_helper_t dh;
104#if defined(sun)
105	Link_map *lmp;
106	Lmid_t lmid;
107#else
108	struct link_map *lmp;
109	u_long lmid = 0;
110#endif
111	int fd;
112	const char *p;
113
114	if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
115		return;
116
117	if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
118		dprintf(1, "couldn't discover module name or address\n");
119		return;
120	}
121
122#if defined(sun)
123	if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
124		dprintf(1, "couldn't discover link map ID\n");
125		return;
126	}
127#endif
128
129	if ((modname = strrchr(lmp->l_name, '/')) == NULL)
130		modname = lmp->l_name;
131	else
132		modname++;
133
134	if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
135	    dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
136	    dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
137	    dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
138		dprintf(0, ".SUNW_dof section corrupt\n");
139		return;
140	}
141
142	elf = (void *)lmp->l_addr;
143
144	dh.dofhp_dof = (uintptr_t)dof;
145	dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;
146
147	if (lmid == 0) {
148		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
149		    "%s", modname);
150	} else {
151		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
152		    "LM%lu`%s", lmid, modname);
153	}
154
155	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
156		devnamep = p;
157
158	if ((fd = open64(devnamep, O_RDWR)) < 0) {
159		dprintf(1, "failed to open helper device %s", devnamep);
160
161		/*
162		 * If the device path wasn't explicitly set, try again with
163		 * the old device path.
164		 */
165		if (p != NULL)
166			return;
167
168		devnamep = olddevname;
169
170		if ((fd = open64(devnamep, O_RDWR)) < 0) {
171			dprintf(1, "failed to open helper device %s", devnamep);
172			return;
173		}
174	}
175
176	if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
177		dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
178	else
179		dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
180
181	(void) close(fd);
182}
183
184#if defined(sun)
185#pragma fini(dtrace_dof_fini)
186#else
187static void dtrace_dof_fini(void) __attribute__ ((destructor));
188#endif
189
190static void
191dtrace_dof_fini(void)
192{
193	int fd;
194
195	if ((fd = open64(devnamep, O_RDWR)) < 0) {
196		dprintf(1, "failed to open helper device %s", devnamep);
197		return;
198	}
199
200	if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
201		dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
202	else
203		dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
204
205	(void) close(fd);
206}
207