10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*8337SJonathan.Haslam@Sun.COM * Common Development and Distribution License (the "License").
6*8337SJonathan.Haslam@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*8337SJonathan.Haslam@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <unistd.h>
270Sstevel@tonic-gate #include <fcntl.h>
280Sstevel@tonic-gate #include <dlfcn.h>
290Sstevel@tonic-gate #include <link.h>
300Sstevel@tonic-gate #include <sys/dtrace.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * In Solaris 10 GA, the only mechanism for communicating helper information
400Sstevel@tonic-gate * is through the DTrace helper pseudo-device node in /devices; there is
410Sstevel@tonic-gate * no /dev link. Because of this, USDT providers and helper actions don't
420Sstevel@tonic-gate * work inside of non-global zones. This issue was addressed by adding
430Sstevel@tonic-gate * the /dev and having this initialization code use that /dev link. If the
440Sstevel@tonic-gate * /dev link doesn't exist it falls back to looking for the /devices node
450Sstevel@tonic-gate * as this code may be embedded in a binary which runs on Solaris 10 GA.
460Sstevel@tonic-gate *
470Sstevel@tonic-gate * Users may set the following environment variable to affect the way
480Sstevel@tonic-gate * helper initialization takes place:
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * DTRACE_DOF_INIT_DEBUG enable debugging output
510Sstevel@tonic-gate * DTRACE_DOF_INIT_DISABLE disable helper loading
520Sstevel@tonic-gate * DTRACE_DOF_INIT_DEVNAME set the path to the helper node
530Sstevel@tonic-gate */
540Sstevel@tonic-gate
550Sstevel@tonic-gate static const char *devname = "/dev/dtrace/helper";
560Sstevel@tonic-gate static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
570Sstevel@tonic-gate
580Sstevel@tonic-gate static const char *modname; /* Name of this load object */
590Sstevel@tonic-gate static int gen; /* DOF helper generation */
600Sstevel@tonic-gate extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
61*8337SJonathan.Haslam@Sun.COM static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */
620Sstevel@tonic-gate
630Sstevel@tonic-gate static void
dprintf(int debug,const char * fmt,...)640Sstevel@tonic-gate dprintf(int debug, const char *fmt, ...)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate va_list ap;
670Sstevel@tonic-gate
68*8337SJonathan.Haslam@Sun.COM if (debug && !dof_init_debug)
690Sstevel@tonic-gate return;
700Sstevel@tonic-gate
710Sstevel@tonic-gate va_start(ap, fmt);
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (modname == NULL)
740Sstevel@tonic-gate (void) fprintf(stderr, "dtrace DOF: ");
750Sstevel@tonic-gate else
760Sstevel@tonic-gate (void) fprintf(stderr, "dtrace DOF %s: ", modname);
770Sstevel@tonic-gate
780Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (fmt[strlen(fmt) - 1] != '\n')
810Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(errno));
820Sstevel@tonic-gate
830Sstevel@tonic-gate va_end(ap);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate #pragma init(dtrace_dof_init)
870Sstevel@tonic-gate static void
dtrace_dof_init(void)880Sstevel@tonic-gate dtrace_dof_init(void)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate dof_hdr_t *dof = &__SUNW_dof;
910Sstevel@tonic-gate #ifdef _LP64
920Sstevel@tonic-gate Elf64_Ehdr *elf;
930Sstevel@tonic-gate #else
940Sstevel@tonic-gate Elf32_Ehdr *elf;
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate dof_helper_t dh;
970Sstevel@tonic-gate Link_map *lmp;
980Sstevel@tonic-gate Lmid_t lmid;
990Sstevel@tonic-gate int fd;
1000Sstevel@tonic-gate const char *p;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
1030Sstevel@tonic-gate return;
1040Sstevel@tonic-gate
105*8337SJonathan.Haslam@Sun.COM if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
106*8337SJonathan.Haslam@Sun.COM dof_init_debug = B_TRUE;
107*8337SJonathan.Haslam@Sun.COM
1080Sstevel@tonic-gate if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
1090Sstevel@tonic-gate dprintf(1, "couldn't discover module name or address\n");
1100Sstevel@tonic-gate return;
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
1140Sstevel@tonic-gate dprintf(1, "couldn't discover link map ID\n");
1150Sstevel@tonic-gate return;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate if ((modname = strrchr(lmp->l_name, '/')) == NULL)
1190Sstevel@tonic-gate modname = lmp->l_name;
1200Sstevel@tonic-gate else
1210Sstevel@tonic-gate modname++;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
1240Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
1250Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
1260Sstevel@tonic-gate dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
1270Sstevel@tonic-gate dprintf(0, ".SUNW_dof section corrupt\n");
1280Sstevel@tonic-gate return;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate elf = (void *)lmp->l_addr;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate dh.dofhp_dof = (uintptr_t)dof;
1340Sstevel@tonic-gate dh.dofhp_addr = elf->e_type == ET_DYN ? lmp->l_addr : 0;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if (lmid == 0) {
1370Sstevel@tonic-gate (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
1380Sstevel@tonic-gate "%s", modname);
1390Sstevel@tonic-gate } else {
1400Sstevel@tonic-gate (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
1410Sstevel@tonic-gate "LM%lu`%s", lmid, modname);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
1450Sstevel@tonic-gate devname = p;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if ((fd = open64(devname, O_RDWR)) < 0) {
1480Sstevel@tonic-gate dprintf(1, "failed to open helper device %s", devname);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate * If the device path wasn't explicitly set, try again with
1520Sstevel@tonic-gate * the old device path.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate if (p != NULL)
1550Sstevel@tonic-gate return;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate devname = olddevname;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if ((fd = open64(devname, O_RDWR)) < 0) {
1600Sstevel@tonic-gate dprintf(1, "failed to open helper device %s", devname);
1610Sstevel@tonic-gate return;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
1660Sstevel@tonic-gate dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
1670Sstevel@tonic-gate else
1680Sstevel@tonic-gate dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate (void) close(fd);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate #pragma fini(dtrace_dof_fini)
1740Sstevel@tonic-gate static void
dtrace_dof_fini(void)1750Sstevel@tonic-gate dtrace_dof_fini(void)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate int fd;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if ((fd = open64(devname, O_RDWR)) < 0) {
1800Sstevel@tonic-gate dprintf(1, "failed to open helper device %s", devname);
1810Sstevel@tonic-gate return;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
1850Sstevel@tonic-gate dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
1860Sstevel@tonic-gate else
1870Sstevel@tonic-gate dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate (void) close(fd);
1900Sstevel@tonic-gate }
191