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 | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2007 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 | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 35 | #include <assert.h> |
| 36 | #include <ctype.h> |
| 37 | #include <elf.h> |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 38 | #include <errno.h> |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 39 | #include <fcntl.h> |
| 40 | #include <stdarg.h> |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 44 | #include <sys/mman.h> |
| 45 | #include <sys/user.h> |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 46 | #include <unistd.h> |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 47 | |
| 48 | #include "pub_core_debuglog.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 49 | #include "pub_core_vki.h" // Avoids warnings from |
| 50 | // pub_core_libcfile.h |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 51 | #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 52 | #include "pub_core_ume.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | |
| 56 | #define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno |
| 57 | where it is defined */ |
| 58 | |
tom | a879a47 | 2005-12-19 12:27:42 +0000 | [diff] [blame] | 59 | #ifndef EM_X86_64 |
njn | 81e60b2 | 2005-12-19 17:01:14 +0000 | [diff] [blame] | 60 | #define EM_X86_64 62 // elf.h doesn't define this on some older systems |
| 61 | #endif |
tom | a879a47 | 2005-12-19 12:27:42 +0000 | [diff] [blame] | 62 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 63 | /* Report fatal errors */ |
sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 64 | __attribute__((noreturn)) |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 65 | static void barf ( const char *format, ... ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 66 | { |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 67 | va_list vargs; |
| 68 | |
| 69 | va_start(vargs, format); |
| 70 | fprintf(stderr, "valgrind: Cannot continue: "); |
| 71 | vfprintf(stderr, format, vargs); |
| 72 | fprintf(stderr, "\n"); |
| 73 | va_end(vargs); |
| 74 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 75 | exit(1); |
sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 76 | /*NOTREACHED*/ |
| 77 | assert(0); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 78 | } |
| 79 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 80 | /* Search the path for the client program */ |
| 81 | static const char *find_client(const char *clientname) |
| 82 | { |
| 83 | static char fullname[PATH_MAX]; |
| 84 | const char *path = getenv("PATH"); |
| 85 | const char *colon; |
| 86 | |
| 87 | while (path) |
| 88 | { |
| 89 | if ((colon = strchr(path, ':')) == NULL) |
| 90 | { |
| 91 | strcpy(fullname, path); |
| 92 | path = NULL; |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | memcpy(fullname, path, colon - path); |
| 97 | fullname[colon - path] = '\0'; |
| 98 | path = colon + 1; |
| 99 | } |
| 100 | |
| 101 | strcat(fullname, "/"); |
| 102 | strcat(fullname, clientname); |
| 103 | |
| 104 | if (access(fullname, R_OK|X_OK) == 0) |
| 105 | return fullname; |
| 106 | } |
| 107 | |
| 108 | return clientname; |
| 109 | } |
| 110 | |
| 111 | /* Examine the client and work out which platform it is for */ |
| 112 | static const char *select_platform(const char *clientname) |
| 113 | { |
| 114 | int fd; |
| 115 | unsigned char *header; |
| 116 | const char *platform = NULL; |
tom | d822c6c | 2005-11-07 16:50:55 +0000 | [diff] [blame] | 117 | long pagesize = sysconf(_SC_PAGESIZE); |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 118 | |
| 119 | if (strchr(clientname, '/') == NULL) |
| 120 | clientname = find_client(clientname); |
| 121 | |
| 122 | if ((fd = open(clientname, O_RDONLY)) < 0) |
| 123 | return NULL; |
| 124 | // barf("open(%s): %s", clientname, strerror(errno)); |
| 125 | |
tom | 0f7573d | 2005-11-07 16:46:55 +0000 | [diff] [blame] | 126 | if ((header = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED) |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 127 | return NULL; |
| 128 | // barf("mmap(%s): %s", clientname, strerror(errno)); |
| 129 | |
| 130 | close(fd); |
| 131 | |
| 132 | if (header[0] == '#' && header[1] == '!') { |
| 133 | char *interp = (char *)header + 2; |
| 134 | char *interpend; |
| 135 | |
| 136 | while (*interp == ' ' || *interp == '\t') |
| 137 | interp++; |
| 138 | |
| 139 | for (interpend = interp; !isspace(*interpend); interpend++) |
| 140 | ; |
| 141 | |
| 142 | *interpend = '\0'; |
| 143 | |
| 144 | platform = select_platform(interp); |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 145 | } else if (memcmp(header, ELFMAG, SELFMAG) == 0) { |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 146 | |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 147 | if (header[EI_CLASS] == ELFCLASS32) { |
| 148 | const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 149 | |
cerion | 2108204 | 2005-12-06 19:07:08 +0000 | [diff] [blame] | 150 | if (header[EI_DATA] == ELFDATA2LSB) { |
| 151 | if (ehdr->e_machine == EM_386 && |
| 152 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 153 | platform = "x86-linux"; |
| 154 | } |
| 155 | } |
| 156 | else if (header[EI_DATA] == ELFDATA2MSB) { |
| 157 | if (ehdr->e_machine == EM_PPC && |
| 158 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 159 | platform = "ppc32-linux"; |
| 160 | } |
| 161 | } |
| 162 | } else if (header[EI_CLASS] == ELFCLASS64) { |
| 163 | const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header; |
| 164 | |
| 165 | if (header[EI_DATA] == ELFDATA2LSB) { |
| 166 | if (ehdr->e_machine == EM_X86_64 && |
| 167 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 168 | platform = "amd64-linux"; |
| 169 | } |
| 170 | } else if (header[EI_DATA] == ELFDATA2MSB) { |
| 171 | if (ehdr->e_machine == EM_PPC64 && |
| 172 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 173 | platform = "ppc64-linux"; |
| 174 | } |
| 175 | } |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
tom | 0f7573d | 2005-11-07 16:46:55 +0000 | [diff] [blame] | 179 | munmap(header, pagesize); |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 180 | |
| 181 | return platform; |
| 182 | } |
| 183 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 184 | /* Where we expect to find all our aux files */ |
| 185 | static const char *valgrind_lib = VG_LIBDIR; |
| 186 | |
| 187 | int main(int argc, char** argv, char** envp) |
| 188 | { |
| 189 | int i, j, loglevel, r; |
| 190 | const char *toolname = NULL; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 191 | const char *clientname = NULL; |
| 192 | const char *platform; |
sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 193 | const char *default_platform; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 194 | const char *cp; |
| 195 | char *toolfile; |
| 196 | char launcher_name[PATH_MAX+1]; |
| 197 | char* new_line; |
| 198 | char** new_env; |
| 199 | |
| 200 | /* Start the debugging-log system ASAP. First find out how many |
| 201 | "-d"s were specified. This is a pre-scan of the command line. |
| 202 | At the same time, look for the tool name. */ |
| 203 | loglevel = 0; |
| 204 | for (i = 1; i < argc; i++) { |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 205 | if (argv[i][0] != '-') { |
| 206 | clientname = argv[i]; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 207 | break; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 208 | } |
| 209 | if (0 == strcmp(argv[i], "--")) { |
| 210 | if (i+1 < argc) |
| 211 | clientname = argv[i+1]; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 212 | break; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 213 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 214 | if (0 == strcmp(argv[i], "-d")) |
| 215 | loglevel++; |
| 216 | if (0 == strncmp(argv[i], "--tool=", 7)) |
| 217 | toolname = argv[i] + 7; |
| 218 | } |
| 219 | |
| 220 | /* ... and start the debug logger. Now we can safely emit logging |
| 221 | messages all through startup. */ |
| 222 | VG_(debugLog_startup)(loglevel, "Stage 1"); |
| 223 | |
| 224 | /* Make sure we know which tool we're using */ |
| 225 | if (toolname) { |
| 226 | VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname); |
| 227 | } else { |
| 228 | VG_(debugLog)(1, "launcher", |
| 229 | "no tool requested, defaulting to 'memcheck'\n"); |
| 230 | toolname = "memcheck"; |
| 231 | } |
| 232 | |
sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 233 | /* Select a platform to use if we can't decide that by looking at |
| 234 | the executable (eg because it's a shell script). Note that the |
| 235 | default_platform is not necessarily either the primary or |
| 236 | secondary build target. Instead it's chosen to maximise the |
| 237 | chances that /bin/sh will work on it. Hence for a primary |
| 238 | target of ppc64-linux we still choose ppc32-linux as the default |
| 239 | target, because on most ppc64-linux setups, the basic /bin, |
| 240 | /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit |
| 241 | mode. */ |
| 242 | if (0==strcmp(VG_PLATFORM,"x86-linux")) |
| 243 | default_platform = "x86-linux"; |
| 244 | else if (0==strcmp(VG_PLATFORM,"amd64-linux")) |
| 245 | default_platform = "amd64-linux"; |
| 246 | else if (0==strcmp(VG_PLATFORM,"ppc32-linux")) |
| 247 | default_platform = "ppc32-linux"; |
| 248 | else if (0==strcmp(VG_PLATFORM,"ppc64-linux")) |
| 249 | default_platform = "ppc32-linux"; |
| 250 | else |
| 251 | barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM); |
| 252 | |
| 253 | /* Work out what platform to use, or use the default platform if |
| 254 | not possible. */ |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 255 | if (clientname == NULL) { |
sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 256 | VG_(debugLog)(1, "launcher", |
| 257 | "no client specified, defaulting platform to '%s'\n", |
| 258 | default_platform); |
| 259 | platform = default_platform; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 260 | } else if ((platform = select_platform(clientname)) != NULL) { |
| 261 | VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform); |
| 262 | } else { |
sewardj | 8813eef | 2006-01-17 16:41:34 +0000 | [diff] [blame] | 263 | VG_(debugLog)(1, "launcher", |
| 264 | "no platform detected, defaulting platform to '%s'\n", |
| 265 | default_platform); |
| 266 | platform = default_platform; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 267 | } |
| 268 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 269 | /* Figure out the name of this executable (viz, the launcher), so |
| 270 | we can tell stage2. stage2 will use the name for recursive |
| 271 | invokations of valgrind on child processes. */ |
| 272 | memset(launcher_name, 0, PATH_MAX+1); |
| 273 | r = readlink("/proc/self/exe", launcher_name, PATH_MAX); |
sewardj | 98e68a4 | 2005-10-04 23:07:33 +0000 | [diff] [blame] | 274 | if (r == -1) { |
| 275 | /* If /proc/self/exe can't be followed, don't give up. Instead |
| 276 | continue with an empty string for VALGRIND_LAUNCHER. In the |
| 277 | sys_execve wrapper, this is tested, and if found to be empty, |
| 278 | fail the execve. */ |
| 279 | fprintf(stderr, "valgrind: warning (non-fatal): " |
| 280 | "readlink(\"/proc/self/exe\") failed.\n"); |
| 281 | fprintf(stderr, "valgrind: continuing, however --trace-children=yes " |
| 282 | "will not work.\n"); |
| 283 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 284 | |
| 285 | /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */ |
| 286 | new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1 |
| 287 | + strlen(launcher_name) + 1); |
| 288 | if (new_line == NULL) |
| 289 | barf("malloc of new_line failed."); |
| 290 | strcpy(new_line, VALGRIND_LAUNCHER); |
| 291 | strcat(new_line, "="); |
| 292 | strcat(new_line, launcher_name); |
| 293 | |
| 294 | for (j = 0; envp[j]; j++) |
| 295 | ; |
| 296 | new_env = malloc((j+2) * sizeof(char*)); |
| 297 | if (new_env == NULL) |
| 298 | barf("malloc of new_env failed."); |
| 299 | for (i = 0; i < j; i++) |
| 300 | new_env[i] = envp[i]; |
| 301 | new_env[i++] = new_line; |
| 302 | new_env[i++] = NULL; |
| 303 | assert(i == j+2); |
| 304 | |
| 305 | /* Establish the correct VALGRIND_LIB. */ |
| 306 | cp = getenv(VALGRIND_LIB); |
| 307 | |
| 308 | if (cp != NULL) |
| 309 | valgrind_lib = cp; |
| 310 | |
| 311 | /* Build the stage2 invokation, and execve it. Bye! */ |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 312 | toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 313 | if (toolfile == NULL) |
| 314 | barf("malloc of toolfile failed."); |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 315 | sprintf(toolfile, "%s/%s/%s", valgrind_lib, platform, toolname); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 316 | |
| 317 | VG_(debugLog)(1, "launcher", "launching %s\n", toolfile); |
| 318 | |
| 319 | execve(toolfile, argv, new_env); |
| 320 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 321 | fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n", |
| 322 | toolname, platform, strerror(errno)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 323 | |
| 324 | exit(1); |
| 325 | } |