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