Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/drti.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2008 Sun Microsystems, Inc. All rights reserved.22* Copyright 2013 Voxer Inc. All rights reserved.23* Use is subject to license terms.24*/2526#include <sys/types.h>27#include <unistd.h>28#include <fcntl.h>29#include <dlfcn.h>30#include <link.h>31#include <sys/dtrace.h>3233#include <stdarg.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <errno.h>38#include <libelf.h>3940/*41* In Solaris 10 GA, the only mechanism for communicating helper information42* is through the DTrace helper pseudo-device node in /devices; there is43* no /dev link. Because of this, USDT providers and helper actions don't44* work inside of non-global zones. This issue was addressed by adding45* the /dev and having this initialization code use that /dev link. If the46* /dev link doesn't exist it falls back to looking for the /devices node47* 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 way50* helper initialization takes place:51*52* DTRACE_DOF_INIT_DEBUG enable debugging output53* DTRACE_DOF_INIT_DISABLE disable helper loading54* DTRACE_DOF_INIT_DEVNAME set the path to the helper node55*/5657static const char *devnamep = "/dev/dtrace/helper";58#ifdef illumos59static const char *olddevname = "/devices/pseudo/dtrace@0:helper";60#endif6162static const char *modname; /* Name of this load object */63static int gen; /* DOF helper generation */64extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */65static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */6667static void68dbg_printf(int debug, const char *fmt, ...)69{70va_list ap;7172if (debug && !dof_init_debug)73return;7475va_start(ap, fmt);7677if (modname == NULL)78(void) fprintf(stderr, "dtrace DOF: ");79else80(void) fprintf(stderr, "dtrace DOF %s: ", modname);8182(void) vfprintf(stderr, fmt, ap);8384if (fmt[strlen(fmt) - 1] != '\n')85(void) fprintf(stderr, ": %s\n", strerror(errno));8687va_end(ap);88}8990#ifdef illumos91#pragma init(dtrace_dof_init)92#else93static void dtrace_dof_init(void) __attribute__ ((constructor));94#endif9596static void97dtrace_dof_init(void)98{99dof_hdr_t *dof = &__SUNW_dof;100#ifdef _LP64101Elf64_Ehdr *elf;102#else103Elf32_Ehdr *elf;104#endif105dof_helper_t dh;106Link_map *lmp = NULL;107#ifdef illumos108Lmid_t lmid;109#else110u_long lmid = 0;111#endif112int fd;113const char *p;114115if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)116return;117118if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)119dof_init_debug = B_TRUE;120121if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {122dbg_printf(1, "couldn't discover module name or address\n");123return;124}125126#ifdef illumos127if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {128dbg_printf(1, "couldn't discover link map ID\n");129return;130}131#endif132133if ((modname = strrchr(lmp->l_name, '/')) == NULL)134modname = lmp->l_name;135else136modname++;137138if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||139dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||140dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||141dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {142dbg_printf(0, ".SUNW_dof section corrupt\n");143return;144}145146#ifdef __FreeBSD__147elf = (void *)lmp->l_base;148#else149elf = (void *)lmp->l_addr;150#endif151152dh.dofhp_dof = (uintptr_t)dof;153#ifdef __FreeBSD__154dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_base : 0;155dh.dofhp_pid = getpid();156#else157dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0;158#endif159160if (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}167168if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)169devnamep = p;170171if ((fd = open64(devnamep, O_RDWR)) < 0) {172dbg_printf(1, "failed to open helper device %s", devnamep);173#ifdef illumos174/*175* If the device path wasn't explicitly set, try again with176* the old device path.177*/178if (p != NULL)179return;180181devnamep = olddevname;182183if ((fd = open64(devnamep, O_RDWR)) < 0) {184dbg_printf(1, "failed to open helper device %s", devnamep);185return;186}187#else188return;189#endif190}191if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1)192dbg_printf(1, "DTrace ioctl failed for DOF at %p", dof);193else {194dbg_printf(1, "DTrace ioctl succeeded for DOF at %p\n", dof);195#ifdef __FreeBSD__196gen = dh.dofhp_gen;197#endif198}199200(void) close(fd);201}202203#ifdef illumos204#pragma fini(dtrace_dof_fini)205#else206static void dtrace_dof_fini(void) __attribute__ ((destructor));207#endif208209static void210dtrace_dof_fini(void)211{212int fd;213214if ((fd = open64(devnamep, O_RDWR)) < 0) {215dbg_printf(1, "failed to open helper device %s", devnamep);216return;217}218219if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1)220dbg_printf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);221else222dbg_printf(1, "DTrace ioctl removed DOF (%d)\n", gen);223224(void) close(fd);225}226227228