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 #define dprintf __hide_dprintf 36 #include <stdio.h> 37 #undef dprintf 38 #include <stdlib.h> 39 #include <string.h> 40 #include <errno.h> 41 #include <libelf.h> 42 43 /* 44 * In Solaris 10 GA, the only mechanism for communicating helper information 45 * is through the DTrace helper pseudo-device node in /devices; there is 46 * no /dev link. Because of this, USDT providers and helper actions don't 47 * work inside of non-global zones. This issue was addressed by adding 48 * the /dev and having this initialization code use that /dev link. If the 49 * /dev link doesn't exist it falls back to looking for the /devices node 50 * as this code may be embedded in a binary which runs on Solaris 10 GA. 51 * 52 * Users may set the following environment variable to affect the way 53 * helper initialization takes place: 54 * 55 * DTRACE_DOF_INIT_DEBUG enable debugging output 56 * DTRACE_DOF_INIT_DISABLE disable helper loading 57 * DTRACE_DOF_INIT_DEVNAME set the path to the helper node 58 */ 59 60 static const char *devnamep = "/dev/dtrace/helper"; 61 #ifdef illumos 62 static const char *olddevname = "/devices/pseudo/dtrace@0:helper"; 63 #endif 64 65 static const char *modname; /* Name of this load object */ 66 static int gen; /* DOF helper generation */ 67 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */ 68 static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */ 69 70 static void __printflike(2,3) 71 dprintf(int debug, const char *fmt, ...) 72 { 73 va_list ap; 74 75 if (debug && !dof_init_debug) 76 return; 77 78 va_start(ap, fmt); 79 80 if (modname == NULL) 81 (void) fprintf(stderr, "dtrace DOF: "); 82 else 83 (void) fprintf(stderr, "dtrace DOF %s: ", modname); 84 85 (void) vfprintf(stderr, fmt, ap); 86 87 if (fmt[strlen(fmt) - 1] != '\n') 88 (void) fprintf(stderr, ": %s\n", strerror(errno)); 89 90 va_end(ap); 91 } 92 93 #ifdef illumos 94 #pragma init(dtrace_dof_init) 95 #else 96 static void dtrace_dof_init(void) __attribute__ ((constructor)); 97 #endif 98 99 static void 100 dtrace_dof_init(void) 101 { 102 dof_hdr_t *dof = &__SUNW_dof; 103 #ifdef _LP64 104 Elf64_Ehdr *elf; 105 #else 106 Elf32_Ehdr *elf; 107 #endif 108 dof_helper_t dh; 109 Link_map *lmp = NULL; 110 #ifdef illumos 111 Lmid_t lmid; 112 #else 113 u_long lmid = 0; 114 #endif 115 int fd; 116 const char *p; 117 118 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL) 119 return; 120 121 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL) 122 dof_init_debug = B_TRUE; 123 124 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) { 125 dprintf(1, "couldn't discover module name or address\n"); 126 return; 127 } 128 129 #ifdef illumos 130 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) { 131 dprintf(1, "couldn't discover link map ID\n"); 132 return; 133 } 134 #endif 135 136 if ((modname = strrchr(lmp->l_name, '/')) == NULL) 137 modname = lmp->l_name; 138 else 139 modname++; 140 141 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 || 142 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 || 143 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 || 144 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) { 145 dprintf(0, ".SUNW_dof section corrupt\n"); 146 return; 147 } 148 149 elf = (void *)lmp->l_addr; 150 151 dh.dofhp_dof = (uintptr_t)dof; 152 dh.dofhp_addr = elf && elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0; 153 #if defined(__FreeBSD__) || defined(__NetBSD__) 154 dh.dofhp_pid = getpid(); 155 #endif 156 157 if (lmid == 0) { 158 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod), 159 "%s", modname); 160 } else { 161 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod), 162 "LM%lu`%s", lmid, modname); 163 } 164 165 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL) 166 devnamep = p; 167 168 if ((fd = open64(devnamep, O_RDWR)) < 0) { 169 dprintf(1, "failed to open helper device %s", devnamep); 170 #ifdef illumos 171 /* 172 * If the device path wasn't explicitly set, try again with 173 * the old device path. 174 */ 175 if (p != NULL) 176 return; 177 178 devnamep = olddevname; 179 180 if ((fd = open64(devnamep, O_RDWR)) < 0) { 181 dprintf(1, "failed to open helper device %s", devnamep); 182 return; 183 } 184 #else 185 return; 186 #endif 187 } 188 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1) 189 dprintf(1, "DTrace ioctl failed for DOF at %p", dof); 190 else { 191 dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof); 192 #if defined(__FreeBSD__) || defined(__NetBSD__) 193 gen = dh.dofhp_gen; 194 #endif 195 } 196 197 (void) close(fd); 198 } 199 200 #ifdef illumos 201 #pragma fini(dtrace_dof_fini) 202 #else 203 static void dtrace_dof_fini(void) __attribute__ ((destructor)); 204 #endif 205 206 static void 207 dtrace_dof_fini(void) 208 { 209 int fd; 210 211 if ((fd = open64(devnamep, O_RDWR)) < 0) { 212 dprintf(1, "failed to open helper device %s", devnamep); 213 return; 214 } 215 216 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1) 217 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen); 218 else 219 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen); 220 221 (void) close(fd); 222 } 223