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 | |
| 10 | Copyright (C) 2000-2005 Julian Seward |
| 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" |
| 49 | #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 50 | #include "pub_core_ume.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | |
| 54 | #define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno |
| 55 | where it is defined */ |
| 56 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 57 | /* Report fatal errors */ |
| 58 | static void barf ( const char *format, ... ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 59 | { |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 60 | va_list vargs; |
| 61 | |
| 62 | va_start(vargs, format); |
| 63 | fprintf(stderr, "valgrind: Cannot continue: "); |
| 64 | vfprintf(stderr, format, vargs); |
| 65 | fprintf(stderr, "\n"); |
| 66 | va_end(vargs); |
| 67 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 68 | exit(1); |
| 69 | } |
| 70 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 71 | /* Search the path for the client program */ |
| 72 | static const char *find_client(const char *clientname) |
| 73 | { |
| 74 | static char fullname[PATH_MAX]; |
| 75 | const char *path = getenv("PATH"); |
| 76 | const char *colon; |
| 77 | |
| 78 | while (path) |
| 79 | { |
| 80 | if ((colon = strchr(path, ':')) == NULL) |
| 81 | { |
| 82 | strcpy(fullname, path); |
| 83 | path = NULL; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | memcpy(fullname, path, colon - path); |
| 88 | fullname[colon - path] = '\0'; |
| 89 | path = colon + 1; |
| 90 | } |
| 91 | |
| 92 | strcat(fullname, "/"); |
| 93 | strcat(fullname, clientname); |
| 94 | |
| 95 | if (access(fullname, R_OK|X_OK) == 0) |
| 96 | return fullname; |
| 97 | } |
| 98 | |
| 99 | return clientname; |
| 100 | } |
| 101 | |
| 102 | /* Examine the client and work out which platform it is for */ |
| 103 | static const char *select_platform(const char *clientname) |
| 104 | { |
| 105 | int fd; |
| 106 | unsigned char *header; |
| 107 | const char *platform = NULL; |
tom | d822c6c | 2005-11-07 16:50:55 +0000 | [diff] [blame^] | 108 | long pagesize = sysconf(_SC_PAGESIZE); |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 109 | |
| 110 | if (strchr(clientname, '/') == NULL) |
| 111 | clientname = find_client(clientname); |
| 112 | |
| 113 | if ((fd = open(clientname, O_RDONLY)) < 0) |
| 114 | return NULL; |
| 115 | // barf("open(%s): %s", clientname, strerror(errno)); |
| 116 | |
tom | 0f7573d | 2005-11-07 16:46:55 +0000 | [diff] [blame] | 117 | 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] | 118 | return NULL; |
| 119 | // barf("mmap(%s): %s", clientname, strerror(errno)); |
| 120 | |
| 121 | close(fd); |
| 122 | |
| 123 | if (header[0] == '#' && header[1] == '!') { |
| 124 | char *interp = (char *)header + 2; |
| 125 | char *interpend; |
| 126 | |
| 127 | while (*interp == ' ' || *interp == '\t') |
| 128 | interp++; |
| 129 | |
| 130 | for (interpend = interp; !isspace(*interpend); interpend++) |
| 131 | ; |
| 132 | |
| 133 | *interpend = '\0'; |
| 134 | |
| 135 | platform = select_platform(interp); |
| 136 | } else if (memcmp(header, ELFMAG, SELFMAG) == 0 && |
| 137 | header[EI_CLASS] == ELFCLASS32 && |
| 138 | header[EI_DATA] == ELFDATA2LSB) { |
| 139 | const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header; |
| 140 | |
| 141 | if (ehdr->e_machine == EM_386 && |
| 142 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 143 | platform = "x86-linux"; |
| 144 | } else if (ehdr->e_machine == EM_PPC && |
| 145 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 146 | platform = "ppc32-linux"; |
| 147 | } |
| 148 | } else if (memcmp(header, ELFMAG, SELFMAG) == 0 && |
| 149 | header[EI_CLASS] == ELFCLASS64 && |
| 150 | header[EI_DATA] == ELFDATA2LSB) { |
| 151 | const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header; |
| 152 | |
| 153 | if (ehdr->e_machine == EM_X86_64 && |
| 154 | ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) { |
| 155 | platform = "amd64-linux"; |
| 156 | } |
| 157 | } |
| 158 | |
tom | 0f7573d | 2005-11-07 16:46:55 +0000 | [diff] [blame] | 159 | munmap(header, pagesize); |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 160 | |
| 161 | return platform; |
| 162 | } |
| 163 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 164 | /* Where we expect to find all our aux files */ |
| 165 | static const char *valgrind_lib = VG_LIBDIR; |
| 166 | |
| 167 | int main(int argc, char** argv, char** envp) |
| 168 | { |
| 169 | int i, j, loglevel, r; |
| 170 | const char *toolname = NULL; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 171 | const char *clientname = NULL; |
| 172 | const char *platform; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 173 | const char *cp; |
| 174 | char *toolfile; |
| 175 | char launcher_name[PATH_MAX+1]; |
| 176 | char* new_line; |
| 177 | char** new_env; |
| 178 | |
| 179 | /* Start the debugging-log system ASAP. First find out how many |
| 180 | "-d"s were specified. This is a pre-scan of the command line. |
| 181 | At the same time, look for the tool name. */ |
| 182 | loglevel = 0; |
| 183 | for (i = 1; i < argc; i++) { |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 184 | if (argv[i][0] != '-') { |
| 185 | clientname = argv[i]; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 186 | break; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 187 | } |
| 188 | if (0 == strcmp(argv[i], "--")) { |
| 189 | if (i+1 < argc) |
| 190 | clientname = argv[i+1]; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 191 | break; |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 192 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 193 | if (0 == strcmp(argv[i], "-d")) |
| 194 | loglevel++; |
| 195 | if (0 == strncmp(argv[i], "--tool=", 7)) |
| 196 | toolname = argv[i] + 7; |
| 197 | } |
| 198 | |
| 199 | /* ... and start the debug logger. Now we can safely emit logging |
| 200 | messages all through startup. */ |
| 201 | VG_(debugLog_startup)(loglevel, "Stage 1"); |
| 202 | |
| 203 | /* Make sure we know which tool we're using */ |
| 204 | if (toolname) { |
| 205 | VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname); |
| 206 | } else { |
| 207 | VG_(debugLog)(1, "launcher", |
| 208 | "no tool requested, defaulting to 'memcheck'\n"); |
| 209 | toolname = "memcheck"; |
| 210 | } |
| 211 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 212 | /* Work out what platform to use */ |
| 213 | if (clientname == NULL) { |
| 214 | VG_(debugLog)(1, "launcher", "no client specified, defaulting platform to '%s'\n", VG_PLATFORM); |
| 215 | platform = VG_PLATFORM; |
| 216 | } else if ((platform = select_platform(clientname)) != NULL) { |
| 217 | VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform); |
| 218 | } else { |
| 219 | VG_(debugLog)(1, "launcher", "no platform detected, defaulting platform to '%s'\n", VG_PLATFORM); |
| 220 | platform = VG_PLATFORM; |
| 221 | } |
| 222 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 223 | /* Figure out the name of this executable (viz, the launcher), so |
| 224 | we can tell stage2. stage2 will use the name for recursive |
| 225 | invokations of valgrind on child processes. */ |
| 226 | memset(launcher_name, 0, PATH_MAX+1); |
| 227 | r = readlink("/proc/self/exe", launcher_name, PATH_MAX); |
sewardj | 98e68a4 | 2005-10-04 23:07:33 +0000 | [diff] [blame] | 228 | if (r == -1) { |
| 229 | /* If /proc/self/exe can't be followed, don't give up. Instead |
| 230 | continue with an empty string for VALGRIND_LAUNCHER. In the |
| 231 | sys_execve wrapper, this is tested, and if found to be empty, |
| 232 | fail the execve. */ |
| 233 | fprintf(stderr, "valgrind: warning (non-fatal): " |
| 234 | "readlink(\"/proc/self/exe\") failed.\n"); |
| 235 | fprintf(stderr, "valgrind: continuing, however --trace-children=yes " |
| 236 | "will not work.\n"); |
| 237 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 238 | |
| 239 | /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */ |
| 240 | new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1 |
| 241 | + strlen(launcher_name) + 1); |
| 242 | if (new_line == NULL) |
| 243 | barf("malloc of new_line failed."); |
| 244 | strcpy(new_line, VALGRIND_LAUNCHER); |
| 245 | strcat(new_line, "="); |
| 246 | strcat(new_line, launcher_name); |
| 247 | |
| 248 | for (j = 0; envp[j]; j++) |
| 249 | ; |
| 250 | new_env = malloc((j+2) * sizeof(char*)); |
| 251 | if (new_env == NULL) |
| 252 | barf("malloc of new_env failed."); |
| 253 | for (i = 0; i < j; i++) |
| 254 | new_env[i] = envp[i]; |
| 255 | new_env[i++] = new_line; |
| 256 | new_env[i++] = NULL; |
| 257 | assert(i == j+2); |
| 258 | |
| 259 | /* Establish the correct VALGRIND_LIB. */ |
| 260 | cp = getenv(VALGRIND_LIB); |
| 261 | |
| 262 | if (cp != NULL) |
| 263 | valgrind_lib = cp; |
| 264 | |
| 265 | /* Build the stage2 invokation, and execve it. Bye! */ |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 266 | toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 267 | if (toolfile == NULL) |
| 268 | barf("malloc of toolfile failed."); |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 269 | sprintf(toolfile, "%s/%s/%s", valgrind_lib, platform, toolname); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 270 | |
| 271 | VG_(debugLog)(1, "launcher", "launching %s\n", toolfile); |
| 272 | |
| 273 | execve(toolfile, argv, new_env); |
| 274 | |
tom | fb7bcde | 2005-11-07 15:24:38 +0000 | [diff] [blame] | 275 | fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n", |
| 276 | toolname, platform, strerror(errno)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 277 | |
| 278 | exit(1); |
| 279 | } |