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 #include <gelf.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 #ifdef illumos 66 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */ 67 #endif 68 static boolean_t dof_init_debug = B_FALSE; /* From DTRACE_DOF_INIT_DEBUG */ 69 70 static void 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 #ifdef illumos 103 dof_hdr_t *dof = &__SUNW_dof; 104 #else 105 dof_hdr_t *dof = NULL; 106 #endif 107 #ifdef _LP64 108 Elf64_Ehdr *elf; 109 #else 110 Elf32_Ehdr *elf; 111 #endif 112 dof_helper_t dh; 113 Link_map *lmp = NULL; 114 #ifdef illumos 115 Lmid_t lmid; 116 #else 117 u_long lmid = 0; 118 #endif 119 int fd; 120 const char *p; 121 #ifndef illumos 122 Elf *e; 123 Elf_Scn *scn = NULL; 124 Elf_Data *dofdata = NULL; 125 dof_hdr_t *dof_next = NULL; 126 GElf_Shdr shdr; 127 int efd; 128 char *s; 129 size_t shstridx; 130 uint64_t aligned_filesz; 131 #endif 132 133 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL) 134 return; 135 136 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL) 137 dof_init_debug = B_TRUE; 138 139 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) { 140 dprintf(1, "couldn't discover module name or address\n"); 141 return; 142 } 143 144 #ifdef illumos 145 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) { 146 dprintf(1, "couldn't discover link map ID\n"); 147 return; 148 } 149 #endif 150 151 if ((modname = strrchr(lmp->l_name, '/')) == NULL) 152 modname = lmp->l_name; 153 else 154 modname++; 155 #ifndef illumos 156 elf_version(EV_CURRENT); 157 if ((efd = open(lmp->l_name, O_RDONLY, 0)) < 0) { 158 dprintf(1, "couldn't open file for reading\n"); 159 return; 160 } 161 if ((e = elf_begin(efd, ELF_C_READ, NULL)) == NULL) { 162 dprintf(1, "elf_begin failed\n"); 163 close(efd); 164 return; 165 } 166 elf_getshdrstrndx(e, &shstridx); 167 dof = NULL; 168 while ((scn = elf_nextscn(e, scn)) != NULL) { 169 gelf_getshdr(scn, &shdr); 170 if (shdr.sh_type == SHT_SUNW_dof) { 171 s = elf_strptr(e, shstridx, shdr.sh_name); 172 if (s != NULL && strcmp(s, ".SUNW_dof") == 0) { 173 dofdata = elf_getdata(scn, NULL); 174 dof = dofdata->d_buf; 175 break; 176 } 177 } 178 } 179 if (dof == NULL) { 180 dprintf(1, "SUNW_dof section not found\n"); 181 elf_end(e); 182 close(efd); 183 return; 184 } 185 186 while ((char *) dof < (char *) dofdata->d_buf + dofdata->d_size) { 187 aligned_filesz = (shdr.sh_addralign == 0 ? dof->dofh_filesz : 188 roundup2(dof->dofh_filesz, shdr.sh_addralign)); 189 dof_next = (void *) ((char *) dof + aligned_filesz); 190 #endif 191 192 if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 || 193 dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 || 194 dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 || 195 dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) { 196 dprintf(0, ".SUNW_dof section corrupt\n"); 197 return; 198 } 199 200 elf = (void *)lmp->l_addr; 201 202 dh.dofhp_dof = (uintptr_t)dof; 203 dh.dofhp_addr = elf->e_type == ET_DYN ? (uintptr_t) lmp->l_addr : 0; 204 205 if (lmid == 0) { 206 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod), 207 "%s", modname); 208 } else { 209 (void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod), 210 "LM%lu`%s", lmid, modname); 211 } 212 213 if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL) 214 devnamep = p; 215 216 if ((fd = open64(devnamep, O_RDWR)) < 0) { 217 dprintf(1, "failed to open helper device %s", devnamep); 218 #ifdef illumos 219 /* 220 * If the device path wasn't explicitly set, try again with 221 * the old device path. 222 */ 223 if (p != NULL) 224 return; 225 226 devnamep = olddevname; 227 228 if ((fd = open64(devnamep, O_RDWR)) < 0) { 229 dprintf(1, "failed to open helper device %s", devnamep); 230 return; 231 } 232 #else 233 return; 234 #endif 235 } 236 if ((gen = ioctl(fd, DTRACEHIOC_ADDDOF, &dh)) == -1) 237 dprintf(1, "DTrace ioctl failed for DOF at %p", dof); 238 else { 239 dprintf(1, "DTrace ioctl succeeded for DOF at %p\n", dof); 240 #ifndef illumos 241 gen = dh.gen; 242 #endif 243 } 244 245 (void) close(fd); 246 247 #ifndef illumos 248 /* End of while loop */ 249 dof = dof_next; 250 } 251 252 elf_end(e); 253 (void) close(efd); 254 #endif 255 } 256 257 #ifdef illumos 258 #pragma fini(dtrace_dof_fini) 259 #else 260 static void dtrace_dof_fini(void) __attribute__ ((destructor)); 261 #endif 262 263 static void 264 dtrace_dof_fini(void) 265 { 266 int fd; 267 268 if ((fd = open64(devnamep, O_RDWR)) < 0) { 269 dprintf(1, "failed to open helper device %s", devnamep); 270 return; 271 } 272 273 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, &gen)) == -1) 274 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen); 275 else 276 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen); 277 278 (void) close(fd); 279 } 280