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 * Copyright 2013 Voxer Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <unistd.h> 28 #include <fcntl.h> 29 #include <dlfcn.h> 30 #include <link.h> 31 #include <sys/dtrace.h> 32 #include <sys/ioctl.h> 33 34 #include <stdarg.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <errno.h> 39 #include <libelf.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 58 static const char *devnamep = "/dev/dtrace/helper"; 59 #ifdef illumos 60 static const char *olddevname = "/devices/pseudo/dtrace@0:helper"; 61 #endif 62 63 static const char *modname; /* Name of this load object */ 64 static int gen; /* DOF helper generation */ 65 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */ 66 static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */ 67 68 static void __printflike(2,3) 69 dbg_printf(int debug, const char *fmt, ...) 70 { 71 va_list ap; 72 73 if (debug && !dof_init_debug) 74 return; 75 76 va_start(ap, fmt); 77 78 if (modname == NULL) 79 (void) fprintf(stderr, "dtrace DOF: "); 80 else 81 (void) fprintf(stderr, "dtrace DOF %s: ", modname); 82 83 (void) vfprintf(stderr, fmt, ap); 84 85 if (fmt[strlen(fmt) - 1] != '\n') 86 (void) fprintf(stderr, ": %s\n", strerror(errno)); 87 88 va_end(ap); 89 } 90 91 #ifdef illumos 92 #pragma init(dtrace_dof_init) 93 #else 94 static void dtrace_dof_init(void) __attribute__ ((constructor)); 95 #endif 96 97 static void 98 dtrace_dof_init(void) 99 { 100 dof_hdr_t *dof = &__SUNW_dof; 101 #ifdef _LP64 102 Elf64_Ehdr *elf; 103 #else 104 Elf32_Ehdr *elf; 105 #endif 106 dof_helper_t dh; 107 Link_map *lmp = NULL; 108 #ifdef illumos 109 Lmid_t lmid; 110 #else 111 u_long lmid = 0; 112 #endif 113 int fd; 114 const char *p; 115 116 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL) 117 return; 118 119 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL) 120 dof_init_debug = B_TRUE; 121 122 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) { 123 dbg_printf(1, "couldn't discover module name or address\n"); 124 return; 125 } 126 127 #ifdef illumos 128 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) { 129 dbg_printf(1, "couldn't discover link map ID\n"); 130 return; 131 } 132 #endif 133 134 if ((modname = strrchr(lmp->l_name, '/')) == NULL) 135 modname = lmp->l_name; 136 else 137 modname++; 138 139 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 || 140 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 || 141 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 || 142 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) { 143 dbg_printf(0, ".SUNW_dof section corrupt\n"); 144 return; 145 } 146 147 elf = (void *)lmp->l_addr; 148 149 dh.dofhp_dof = (uintptr_t)dof; 150 dh.dofhp_addr = elf && elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0; 151 #if defined(__FreeBSD__) || defined(__NetBSD__) 152 dh.dofhp_pid = getpid(); 153 #endif 154 155 if (lmid == 0) { 156 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod), 157 "%s", modname); 158 } else { 159 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod), 160 "LM%lu`%s", lmid, modname); 161 } 162 163 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL) 164 devnamep = p; 165 166 if ((fd = open64(devnamep, O_RDWR)) < 0) { 167 dbg_printf(1, "failed to open helper device %s", devnamep); 168 #ifdef illumos 169 /* 170 * If the device path wasn't explicitly set, try again with 171 * the old device path. 172 */ 173 if (p != NULL) 174 return; 175 176 devnamep = olddevname; 177 178 if ((fd = open64(devnamep, O_RDWR)) < 0) { 179 dbg_printf(1, "failed to open helper device %s", devnamep); 180 return; 181 } 182 #else 183 return; 184 #endif 185 } 186 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1) 187 dbg_printf(1, "DTrace ioctl failed for DOF at %p", dof); 188 else { 189 dbg_printf(1, "DTrace ioctl succeeded for DOF at %p\n", dof); 190 #if defined(__FreeBSD__) || defined(__NetBSD__) 191 gen = dh.dofhp_gen; 192 #endif 193 } 194 195 (void) close(fd); 196 } 197 198 #ifdef illumos 199 #pragma fini(dtrace_dof_fini) 200 #else 201 static void dtrace_dof_fini(void) __attribute__ ((destructor)); 202 #endif 203 204 static void 205 dtrace_dof_fini(void) 206 { 207 int fd; 208 209 if ((fd = open64(devnamep, O_RDWR)) < 0) { 210 dbg_printf(1, "failed to open helper device %s", devnamep); 211 return; 212 } 213 214 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1) 215 dbg_printf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen); 216 else 217 dbg_printf(1, "DTrace ioctl removed DOF (%d)\n", gen); 218 219 (void) close(fd); 220 } 221