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