2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <dlfcn.h>
2N/A#include <link.h>
2N/A#include <sys/dtrace.h>
2N/A
2N/A#include <stdarg.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <errno.h>
2N/A
2N/A/*
2N/A * In Solaris 10 GA, the only mechanism for communicating helper information
2N/A * is through the DTrace helper pseudo-device node in /devices; there is
2N/A * no /dev link. Because of this, USDT providers and helper actions don't
2N/A * work inside of non-global zones. This issue was addressed by adding
2N/A * the /dev and having this initialization code use that /dev link. If the
2N/A * /dev link doesn't exist it falls back to looking for the /devices node
2N/A * as this code may be embedded in a binary which runs on Solaris 10 GA.
2N/A *
2N/A * Users may set the following environment variable to affect the way
2N/A * helper initialization takes place:
2N/A *
2N/A * DTRACE_DOF_INIT_DEBUG enable debugging output
2N/A * DTRACE_DOF_INIT_DISABLE disable helper loading
2N/A * DTRACE_DOF_INIT_DEVNAME set the path to the helper node
2N/A */
2N/A
2N/Astatic const char *devname = "/dev/dtrace/helper";
2N/Astatic const char *olddevname = "/devices/pseudo/dtrace@0:helper";
2N/A
2N/Astatic const char *modname; /* Name of this load object */
2N/Astatic int gen; /* DOF helper generation */
2N/Aextern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
2N/Astatic boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */
2N/A
2N/Astatic void
2N/Adprintf(int debug, const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A if (debug && !dof_init_debug)
2N/A return;
2N/A
2N/A va_start(ap, fmt);
2N/A
2N/A if (modname == NULL)
2N/A (void) fprintf(stderr, "dtrace DOF: ");
2N/A else
2N/A (void) fprintf(stderr, "dtrace DOF %s: ", modname);
2N/A
2N/A (void) vfprintf(stderr, fmt, ap);
2N/A
2N/A if (fmt[strlen(fmt) - 1] != '\n')
2N/A (void) fprintf(stderr, ": %s\n", strerror(errno));
2N/A
2N/A va_end(ap);
2N/A}
2N/A
2N/A#pragma init(dtrace_dof_init)
2N/Astatic void
2N/Adtrace_dof_init(void)
2N/A{
2N/A dof_hdr_t *dof = &__SUNW_dof;
2N/A#ifdef _LP64
2N/A Elf64_Ehdr *elf;
2N/A#else
2N/A Elf32_Ehdr *elf;
2N/A#endif
2N/A dof_helper_t dh;
2N/A Link_map *lmp;
2N/A Lmid_t lmid;
2N/A int fd;
2N/A const char *p;
2N/A
2N/A if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
2N/A return;
2N/A
2N/A if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
2N/A dof_init_debug = B_TRUE;
2N/A
2N/A if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
2N/A dprintf(1, "couldn't discover module name or address\n");
2N/A return;
2N/A }
2N/A
2N/A if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
2N/A dprintf(1, "couldn't discover link map ID\n");
2N/A return;
2N/A }
2N/A
2N/A if ((modname = strrchr(lmp->l_name, '/')) == NULL)
2N/A modname = lmp->l_name;
2N/A else
2N/A modname++;
2N/A
2N/A if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
2N/A dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
2N/A dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
2N/A dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
2N/A dprintf(0, ".SUNW_dof section corrupt\n");
2N/A return;
2N/A }
2N/A
2N/A elf = (void *)lmp->l_addr;
2N/A
2N/A dh.dofhp_dof = (uintptr_t)dof;
2N/A dh.dofhp_addr = elf->e_type == ET_DYN ? lmp->l_addr : 0;
2N/A
2N/A if (lmid == 0) {
2N/A (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
2N/A "%s", modname);
2N/A } else {
2N/A (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
2N/A "LM%lu`%s", lmid, modname);
2N/A }
2N/A
2N/A if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
2N/A devname = p;
2N/A
2N/A if ((fd = open64(devname, O_RDWR)) < 0) {
2N/A dprintf(1, "failed to open helper device %s", devname);
2N/A
2N/A /*
2N/A * If the device path wasn't explicitly set, try again with
2N/A * the old device path.
2N/A */
2N/A if (p != NULL)
2N/A return;
2N/A
2N/A devname = olddevname;
2N/A
2N/A if ((fd = open64(devname, O_RDWR)) < 0) {
2N/A dprintf(1, "failed to open helper device %s", devname);
2N/A return;
2N/A }
2N/A }
2N/A
2N/A if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)
2N/A dprintf(1, "DTrace ioctl failed for DOF at %p", dof);
2N/A else
2N/A dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);
2N/A
2N/A (void) close(fd);
2N/A}
2N/A
2N/A#pragma fini(dtrace_dof_fini)
2N/Astatic void
2N/Adtrace_dof_fini(void)
2N/A{
2N/A int fd;
2N/A
2N/A if ((fd = open64(devname, O_RDWR)) < 0) {
2N/A dprintf(1, "failed to open helper device %s", devname);
2N/A return;
2N/A }
2N/A
2N/A if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
2N/A dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
2N/A else
2N/A dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
2N/A
2N/A (void) close(fd);
2N/A}