| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1 |  | 
|  | 2 | /*--------------------------------------------------------------------*/ | 
|  | 3 | /*--- Launching valgrind                              m_launcher.c ---*/ | 
|  | 4 | /*--------------------------------------------------------------------*/ | 
|  | 5 |  | 
|  | 6 | /* | 
|  | 7 | This file is part of Valgrind, a dynamic binary instrumentation | 
|  | 8 | framework. | 
|  | 9 |  | 
| sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2010 Julian Seward | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 11 | jseward@acm.org | 
|  | 12 |  | 
|  | 13 | This program is free software; you can redistribute it and/or | 
|  | 14 | modify it under the terms of the GNU General Public License as | 
|  | 15 | published by the Free Software Foundation; either version 2 of the | 
|  | 16 | License, or (at your option) any later version. | 
|  | 17 |  | 
|  | 18 | This program is distributed in the hope that it will be useful, but | 
|  | 19 | WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 21 | General Public License for more details. | 
|  | 22 |  | 
|  | 23 | You should have received a copy of the GNU General Public License | 
|  | 24 | along with this program; if not, write to the Free Software | 
|  | 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | 
|  | 26 | 02111-1307, USA. | 
|  | 27 |  | 
|  | 28 | The GNU General Public License is contained in the file COPYING. | 
|  | 29 | */ | 
|  | 30 |  | 
|  | 31 | /* Note: this is a "normal" program and not part of Valgrind proper, | 
|  | 32 | and so it doesn't have to conform to Valgrind's arcane rules on | 
|  | 33 | no-glibc-usage etc. */ | 
|  | 34 |  | 
| tom | 5c46da8 | 2010-04-29 09:01:21 +0000 | [diff] [blame] | 35 | /* Include valgrind headers before system headers to avoid problems | 
|  | 36 | with the system headers #defining things which are used as names | 
|  | 37 | of structure members in vki headers. */ | 
|  | 38 |  | 
|  | 39 | #include "pub_core_debuglog.h" | 
|  | 40 | #include "pub_core_vki.h"       // Avoids warnings from | 
|  | 41 | // pub_core_libcfile.h | 
|  | 42 | #include "pub_core_libcproc.h"  // For VALGRIND_LIB, VALGRIND_LAUNCHER | 
|  | 43 | #include "pub_core_ume.h" | 
|  | 44 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 45 | #include <assert.h> | 
|  | 46 | #include <ctype.h> | 
|  | 47 | #include <elf.h> | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 48 | #include <errno.h> | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 49 | #include <fcntl.h> | 
|  | 50 | #include <stdarg.h> | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 51 | #include <stdio.h> | 
|  | 52 | #include <stdlib.h> | 
|  | 53 | #include <string.h> | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 54 | #include <sys/mman.h> | 
|  | 55 | #include <sys/user.h> | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 56 | #include <unistd.h> | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 57 |  | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 58 |  | 
|  | 59 |  | 
|  | 60 | #define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno | 
|  | 61 | where it is defined */ | 
|  | 62 |  | 
| tom | a879a47 | 2005-12-19 12:27:42 +0000 | [diff] [blame] | 63 | #ifndef EM_X86_64 | 
| njn | 81e60b2 | 2005-12-19 17:01:14 +0000 | [diff] [blame] | 64 | #define EM_X86_64 62    // elf.h doesn't define this on some older systems | 
|  | 65 | #endif | 
| tom | a879a47 | 2005-12-19 12:27:42 +0000 | [diff] [blame] | 66 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 67 | /* Report fatal errors */ | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 68 | __attribute__((noreturn)) | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 69 | static void barf ( const char *format, ... ) | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 70 | { | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 71 | va_list vargs; | 
|  | 72 |  | 
|  | 73 | va_start(vargs, format); | 
|  | 74 | fprintf(stderr, "valgrind: Cannot continue: "); | 
|  | 75 | vfprintf(stderr, format, vargs); | 
|  | 76 | fprintf(stderr, "\n"); | 
|  | 77 | va_end(vargs); | 
|  | 78 |  | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 79 | exit(1); | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 80 | /*NOTREACHED*/ | 
|  | 81 | assert(0); | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 84 | /* Search the path for the client program */ | 
|  | 85 | static const char *find_client(const char *clientname) | 
|  | 86 | { | 
|  | 87 | static char fullname[PATH_MAX]; | 
|  | 88 | const char *path = getenv("PATH"); | 
|  | 89 | const char *colon; | 
|  | 90 |  | 
|  | 91 | while (path) | 
|  | 92 | { | 
|  | 93 | if ((colon = strchr(path, ':')) == NULL) | 
|  | 94 | { | 
|  | 95 | strcpy(fullname, path); | 
|  | 96 | path = NULL; | 
|  | 97 | } | 
|  | 98 | else | 
|  | 99 | { | 
|  | 100 | memcpy(fullname, path, colon - path); | 
|  | 101 | fullname[colon - path] = '\0'; | 
|  | 102 | path = colon + 1; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | strcat(fullname, "/"); | 
|  | 106 | strcat(fullname, clientname); | 
|  | 107 |  | 
|  | 108 | if (access(fullname, R_OK|X_OK) == 0) | 
|  | 109 | return fullname; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | return clientname; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | /* Examine the client and work out which platform it is for */ | 
|  | 116 | static const char *select_platform(const char *clientname) | 
|  | 117 | { | 
|  | 118 | int fd; | 
| njn | 33e57a7 | 2009-06-29 06:57:30 +0000 | [diff] [blame] | 119 | uint8_t header[4096]; | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 120 | ssize_t n_bytes; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 121 | const char *platform = NULL; | 
|  | 122 |  | 
| tom | 1edc55a | 2009-08-20 07:56:45 +0000 | [diff] [blame] | 123 | VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname); | 
|  | 124 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 125 | if (strchr(clientname, '/') == NULL) | 
|  | 126 | clientname = find_client(clientname); | 
|  | 127 |  | 
| tom | 1edc55a | 2009-08-20 07:56:45 +0000 | [diff] [blame] | 128 | VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname); | 
|  | 129 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 130 | if ((fd = open(clientname, O_RDONLY)) < 0) | 
|  | 131 | return NULL; | 
|  | 132 | //   barf("open(%s): %s", clientname, strerror(errno)); | 
|  | 133 |  | 
| tom | 1edc55a | 2009-08-20 07:56:45 +0000 | [diff] [blame] | 134 | VG_(debugLog)(2, "launcher", "opened '%s'\n", clientname); | 
|  | 135 |  | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 136 | n_bytes = read(fd, header, sizeof(header)); | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 137 | close(fd); | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 138 | if (n_bytes < 2) { | 
| njn | 33e57a7 | 2009-06-29 06:57:30 +0000 | [diff] [blame] | 139 | return NULL; | 
|  | 140 | } | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 141 |  | 
| sewardj | 6431e30 | 2009-08-27 23:22:39 +0000 | [diff] [blame] | 142 | VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n", | 
|  | 143 | (long int)n_bytes, clientname); | 
| tom | 1edc55a | 2009-08-20 07:56:45 +0000 | [diff] [blame] | 144 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 145 | if (header[0] == '#' && header[1] == '!') { | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 146 | int i = 2; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 147 | char *interp = (char *)header + 2; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 148 |  | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 149 | // Skip whitespace. | 
|  | 150 | while (1) { | 
|  | 151 | if (i == n_bytes) return NULL; | 
| sewardj | 220591d | 2009-08-19 10:32:49 +0000 | [diff] [blame] | 152 | if (' ' != header[i] && '\t' != header[i]) break; | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 153 | i++; | 
|  | 154 | } | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 155 |  | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 156 | // Get the interpreter name. | 
|  | 157 | interp = &header[i]; | 
|  | 158 | while (1) { | 
|  | 159 | if (i == n_bytes) break; | 
|  | 160 | if (isspace(header[i])) break; | 
|  | 161 | i++; | 
|  | 162 | } | 
|  | 163 | if (i == n_bytes) return NULL; | 
|  | 164 | header[i] = '\0'; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 165 |  | 
|  | 166 | platform = select_platform(interp); | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 167 |  | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 168 | } else if (n_bytes >= SELFMAG && memcmp(header, ELFMAG, SELFMAG) == 0) { | 
|  | 169 |  | 
|  | 170 | if (n_bytes >= sizeof(Elf32_Ehdr) && header[EI_CLASS] == ELFCLASS32) { | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 171 | const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 172 |  | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 173 | if (header[EI_DATA] == ELFDATA2LSB) { | 
|  | 174 | if (ehdr->e_machine == EM_386 && | 
| tom | 4ff8f6c | 2009-08-18 14:12:48 +0000 | [diff] [blame] | 175 | (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV || | 
|  | 176 | ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) { | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 177 | platform = "x86-linux"; | 
|  | 178 | } | 
| sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 179 | else | 
|  | 180 | if (ehdr->e_machine == EM_ARM && | 
|  | 181 | (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV || | 
|  | 182 | ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) { | 
|  | 183 | platform = "arm-linux"; | 
|  | 184 | } | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 185 | } | 
|  | 186 | else if (header[EI_DATA] == ELFDATA2MSB) { | 
|  | 187 | if (ehdr->e_machine == EM_PPC && | 
| tom | 4ff8f6c | 2009-08-18 14:12:48 +0000 | [diff] [blame] | 188 | (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV || | 
|  | 189 | ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) { | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 190 | platform = "ppc32-linux"; | 
|  | 191 | } | 
|  | 192 | } | 
| sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 193 |  | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 194 | } else if (n_bytes >= sizeof(Elf64_Ehdr) && header[EI_CLASS] == ELFCLASS64) { | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 195 | const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header; | 
|  | 196 |  | 
|  | 197 | if (header[EI_DATA] == ELFDATA2LSB) { | 
|  | 198 | if (ehdr->e_machine == EM_X86_64 && | 
| tom | 4ff8f6c | 2009-08-18 14:12:48 +0000 | [diff] [blame] | 199 | (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV || | 
|  | 200 | ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) { | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 201 | platform = "amd64-linux"; | 
|  | 202 | } | 
|  | 203 | } else if (header[EI_DATA] == ELFDATA2MSB) { | 
|  | 204 | if (ehdr->e_machine == EM_PPC64 && | 
| tom | 4ff8f6c | 2009-08-18 14:12:48 +0000 | [diff] [blame] | 205 | (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV || | 
|  | 206 | ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) { | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 207 | platform = "ppc64-linux"; | 
| sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 208 | } else if (ehdr->e_machine == EM_S390 && | 
|  | 209 | (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV || | 
|  | 210 | ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) { | 
|  | 211 | platform = "s390x-linux"; | 
| cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 212 | } | 
|  | 213 | } | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 214 | } | 
|  | 215 | } | 
|  | 216 |  | 
| tom | 1edc55a | 2009-08-20 07:56:45 +0000 | [diff] [blame] | 217 | VG_(debugLog)(2, "launcher", "selected platform '%s'\n", | 
|  | 218 | platform ? platform : "unknown"); | 
|  | 219 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 220 | return platform; | 
|  | 221 | } | 
|  | 222 |  | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 223 | /* Where we expect to find all our aux files */ | 
|  | 224 | static const char *valgrind_lib = VG_LIBDIR; | 
|  | 225 |  | 
|  | 226 | int main(int argc, char** argv, char** envp) | 
|  | 227 | { | 
|  | 228 | int i, j, loglevel, r; | 
|  | 229 | const char *toolname = NULL; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 230 | const char *clientname = NULL; | 
|  | 231 | const char *platform; | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 232 | const char *default_platform; | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 233 | const char *cp; | 
|  | 234 | char *toolfile; | 
|  | 235 | char launcher_name[PATH_MAX+1]; | 
|  | 236 | char* new_line; | 
|  | 237 | char** new_env; | 
|  | 238 |  | 
|  | 239 | /* Start the debugging-log system ASAP.  First find out how many | 
|  | 240 | "-d"s were specified.  This is a pre-scan of the command line. | 
|  | 241 | At the same time, look for the tool name. */ | 
|  | 242 | loglevel = 0; | 
|  | 243 | for (i = 1; i < argc; i++) { | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 244 | if (argv[i][0] != '-') { | 
|  | 245 | clientname = argv[i]; | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 246 | break; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 247 | } | 
|  | 248 | if (0 == strcmp(argv[i], "--")) { | 
|  | 249 | if (i+1 < argc) | 
|  | 250 | clientname = argv[i+1]; | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 251 | break; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 252 | } | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 253 | if (0 == strcmp(argv[i], "-d")) | 
|  | 254 | loglevel++; | 
|  | 255 | if (0 == strncmp(argv[i], "--tool=", 7)) | 
|  | 256 | toolname = argv[i] + 7; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | /* ... and start the debug logger.  Now we can safely emit logging | 
|  | 260 | messages all through startup. */ | 
|  | 261 | VG_(debugLog_startup)(loglevel, "Stage 1"); | 
|  | 262 |  | 
|  | 263 | /* Make sure we know which tool we're using */ | 
|  | 264 | if (toolname) { | 
|  | 265 | VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname); | 
|  | 266 | } else { | 
|  | 267 | VG_(debugLog)(1, "launcher", | 
|  | 268 | "no tool requested, defaulting to 'memcheck'\n"); | 
|  | 269 | toolname = "memcheck"; | 
|  | 270 | } | 
|  | 271 |  | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 272 | /* Select a platform to use if we can't decide that by looking at | 
|  | 273 | the executable (eg because it's a shell script).  Note that the | 
|  | 274 | default_platform is not necessarily either the primary or | 
|  | 275 | secondary build target.  Instead it's chosen to maximise the | 
|  | 276 | chances that /bin/sh will work on it.  Hence for a primary | 
|  | 277 | target of ppc64-linux we still choose ppc32-linux as the default | 
|  | 278 | target, because on most ppc64-linux setups, the basic /bin, | 
|  | 279 | /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit | 
|  | 280 | mode. */ | 
| njn | f6ded8c | 2009-02-06 04:49:14 +0000 | [diff] [blame] | 281 | if ((0==strcmp(VG_PLATFORM,"x86-linux"))   || | 
|  | 282 | (0==strcmp(VG_PLATFORM,"amd64-linux")) || | 
|  | 283 | (0==strcmp(VG_PLATFORM,"ppc32-linux")) || | 
| sewardj | 59570ff | 2010-01-01 11:59:33 +0000 | [diff] [blame] | 284 | (0==strcmp(VG_PLATFORM,"ppc64-linux")) || | 
| sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 285 | (0==strcmp(VG_PLATFORM,"arm-linux"))   || | 
|  | 286 | (0==strcmp(VG_PLATFORM,"s390x-linux"))) | 
| njn | 4f350d1 | 2009-02-06 05:34:19 +0000 | [diff] [blame] | 287 | default_platform = VG_PLATFORM; | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 288 | else | 
|  | 289 | barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM); | 
|  | 290 |  | 
|  | 291 | /* Work out what platform to use, or use the default platform if | 
|  | 292 | not possible. */ | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 293 | if (clientname == NULL) { | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 294 | VG_(debugLog)(1, "launcher", | 
|  | 295 | "no client specified, defaulting platform to '%s'\n", | 
|  | 296 | default_platform); | 
|  | 297 | platform = default_platform; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 298 | } else if ((platform = select_platform(clientname)) != NULL) { | 
|  | 299 | VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform); | 
|  | 300 | } else { | 
| sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 301 | VG_(debugLog)(1, "launcher", | 
|  | 302 | "no platform detected, defaulting platform to '%s'\n", | 
|  | 303 | default_platform); | 
|  | 304 | platform = default_platform; | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 305 | } | 
|  | 306 |  | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 307 | /* Figure out the name of this executable (viz, the launcher), so | 
|  | 308 | we can tell stage2.  stage2 will use the name for recursive | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 309 | invocations of valgrind on child processes. */ | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 310 | memset(launcher_name, 0, PATH_MAX+1); | 
|  | 311 | r = readlink("/proc/self/exe", launcher_name, PATH_MAX); | 
| sewardj | 98e68a4 | 2005-10-04 23:07:33 +0000 | [diff] [blame] | 312 | if (r == -1) { | 
|  | 313 | /* If /proc/self/exe can't be followed, don't give up.  Instead | 
|  | 314 | continue with an empty string for VALGRIND_LAUNCHER.  In the | 
|  | 315 | sys_execve wrapper, this is tested, and if found to be empty, | 
|  | 316 | fail the execve. */ | 
|  | 317 | fprintf(stderr, "valgrind: warning (non-fatal): " | 
|  | 318 | "readlink(\"/proc/self/exe\") failed.\n"); | 
|  | 319 | fprintf(stderr, "valgrind: continuing, however --trace-children=yes " | 
|  | 320 | "will not work.\n"); | 
|  | 321 | } | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 322 |  | 
|  | 323 | /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */ | 
|  | 324 | new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1 | 
|  | 325 | + strlen(launcher_name) + 1); | 
|  | 326 | if (new_line == NULL) | 
|  | 327 | barf("malloc of new_line failed."); | 
|  | 328 | strcpy(new_line, VALGRIND_LAUNCHER); | 
|  | 329 | strcat(new_line, "="); | 
|  | 330 | strcat(new_line, launcher_name); | 
|  | 331 |  | 
|  | 332 | for (j = 0; envp[j]; j++) | 
|  | 333 | ; | 
|  | 334 | new_env = malloc((j+2) * sizeof(char*)); | 
|  | 335 | if (new_env == NULL) | 
|  | 336 | barf("malloc of new_env failed."); | 
|  | 337 | for (i = 0; i < j; i++) | 
|  | 338 | new_env[i] = envp[i]; | 
|  | 339 | new_env[i++] = new_line; | 
|  | 340 | new_env[i++] = NULL; | 
|  | 341 | assert(i == j+2); | 
|  | 342 |  | 
|  | 343 | /* Establish the correct VALGRIND_LIB. */ | 
|  | 344 | cp = getenv(VALGRIND_LIB); | 
|  | 345 |  | 
|  | 346 | if (cp != NULL) | 
|  | 347 | valgrind_lib = cp; | 
|  | 348 |  | 
| njn | 58899e8 | 2009-06-30 06:06:14 +0000 | [diff] [blame] | 349 | /* Build the stage2 invocation, and execve it.  Bye! */ | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 350 | toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3); | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 351 | if (toolfile == NULL) | 
|  | 352 | barf("malloc of toolfile failed."); | 
| njn | 6bf365c | 2009-02-11 00:35:45 +0000 | [diff] [blame] | 353 | sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform); | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 354 |  | 
|  | 355 | VG_(debugLog)(1, "launcher", "launching %s\n", toolfile); | 
|  | 356 |  | 
|  | 357 | execve(toolfile, argv, new_env); | 
|  | 358 |  | 
| tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 359 | fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n", | 
|  | 360 | toolname, platform, strerror(errno)); | 
| sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 361 |  | 
|  | 362 | exit(1); | 
|  | 363 | } |