vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / launcher-linux.c
blobbc95e3c115340691c6591cafe6f1b3ae6708f984
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Launching valgrind m_launcher.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2000-2017 Julian Seward
12 jseward@acm.org
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.
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.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 /* Note: this is a "normal" program and not part of Valgrind proper,
31 and so it doesn't have to conform to Valgrind's arcane rules on
32 no-glibc-usage etc. */
34 /* Include valgrind headers before system headers to avoid problems
35 with the system headers #defining things which are used as names
36 of structure members in vki headers. */
38 #include "pub_core_debuglog.h"
39 #include "pub_core_vki.h" // Avoids warnings from
40 // pub_core_libcfile.h
41 #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER
42 #include "pub_core_ume.h"
44 #include <assert.h>
45 #include <ctype.h>
46 #include <elf.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 #ifndef EM_X86_64
55 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
56 #endif
58 #ifndef EM_AARCH64
59 #define EM_AARCH64 183 // ditto
60 #endif
62 #ifndef EM_PPC64
63 #define EM_PPC64 21 // ditto
64 #endif
66 #ifndef EM_NANOMIPS
67 #define EM_NANOMIPS 249
68 #endif
70 #ifndef E_MIPS_ABI_O32
71 #define E_MIPS_ABI_O32 0x00001000
72 #endif
74 #ifndef E_MIPS_ABI2
75 #define E_MIPS_ABI2 0x00000020
76 #endif
78 /* Report fatal errors */
79 __attribute__((noreturn))
80 static void barf ( const char *format, ... )
82 va_list vargs;
84 va_start(vargs, format);
85 fprintf(stderr, "valgrind: Cannot continue: ");
86 vfprintf(stderr, format, vargs);
87 fprintf(stderr, "\n");
88 va_end(vargs);
90 exit(1);
91 /*NOTREACHED*/
92 assert(0);
95 /* Search the path for the client program */
96 static char *find_client(const char *clientname)
98 char *fullname;
99 const char *path = getenv("PATH");
100 const char *colon;
102 assert(clientname != NULL);
104 if (path == NULL) return strdup(clientname);
106 /* Make the size of the FULLNAME buffer large enough. */
107 unsigned need = strlen(path) + strlen("/") + strlen(clientname) + 1;
109 fullname = malloc(need);
110 if (fullname == NULL)
111 barf("malloc of fullname failed.");
113 while (path)
115 if ((colon = strchr(path, ':')) == NULL)
117 strcpy(fullname, path);
118 path = NULL;
120 else
122 strncpy(fullname, path, colon - path);
123 fullname[colon - path] = '\0';
124 path = colon + 1;
127 strcat(fullname, "/");
128 strcat(fullname, clientname);
130 if (access(fullname, R_OK|X_OK) == 0)
131 return fullname;
133 free(fullname);
135 return strdup(clientname);
138 /* Examine the client and work out which platform it is for */
139 static const char *select_platform(const char *clientname)
141 int fd;
142 union {
143 char c[4096];
144 Elf32_Ehdr ehdr32;
145 Elf64_Ehdr ehdr64;
146 } header;
147 ssize_t n_bytes;
148 const char *platform = NULL;
149 char *client;
151 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
153 if (strchr(clientname, '/') == NULL)
154 client = find_client(clientname);
155 else
156 client = strdup(clientname);
158 if (strcmp (client, clientname) != 0)
159 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", client);
161 if ((fd = open(client, O_RDONLY)) < 0) {
162 return_null:
163 free (client);
164 return NULL;
166 // barf("open(%s): %s", clientname, strerror(errno));
168 VG_(debugLog)(2, "launcher", "opened '%s'\n", client);
170 n_bytes = read(fd, header.c, sizeof(header));
171 close(fd);
172 if (n_bytes < 2) {
173 goto return_null;
176 VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
177 (long int)n_bytes, client);
179 if (header.c[0] == '#' && header.c[1] == '!') {
180 int i = 2;
182 STATIC_ASSERT(VKI_BINPRM_BUF_SIZE < sizeof header);
183 if (n_bytes > VKI_BINPRM_BUF_SIZE)
184 n_bytes = VKI_BINPRM_BUF_SIZE - 1;
185 header.c[n_bytes] = '\0';
186 char *eol = strchr(header.c, '\n');
187 if (eol != NULL)
188 *eol = '\0';
190 // Skip whitespace.
191 while (header.c[i] == ' '|| header.c[i] == '\t')
192 i++;
194 // Get the interpreter name.
195 const char *interp = header.c + i;
197 if (header.c[i] == '\0') {
198 // No interpreter was found; fall back to default shell
199 # if defined(VGPV_arm_linux_android) \
200 || defined(VGPV_x86_linux_android) \
201 || defined(VGPV_mips32_linux_android) \
202 || defined(VGPV_arm64_linux_android)
203 interp = "/system/bin/sh";
204 # else
205 interp = "/bin/sh";
206 # endif
207 } else {
208 while (header.c[i]) {
209 if (header.c[i] == ' ' || header.c[i] == '\t') break;
210 i++;
212 header.c[i] = '\0';
215 platform = select_platform(interp);
217 } else if (n_bytes >= SELFMAG && memcmp(header.c, ELFMAG, SELFMAG) == 0) {
219 if (n_bytes >= sizeof(Elf32_Ehdr) && header.c[EI_CLASS] == ELFCLASS32) {
221 if (header.c[EI_DATA] == ELFDATA2LSB) {
222 # if defined(VGO_solaris)
223 if (header.ehdr32.e_machine == EM_386 &&
224 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
225 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)) {
226 platform = "x86-solaris";
228 else
229 # endif
230 if (header.ehdr32.e_machine == EM_386 &&
231 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
232 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
233 platform = "x86-linux";
235 else
236 if (header.ehdr32.e_machine == EM_ARM &&
237 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
238 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
239 platform = "arm-linux";
241 else
242 if (header.ehdr32.e_machine == EM_MIPS &&
243 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
244 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
245 (header.ehdr32.e_flags & E_MIPS_ABI_O32)) {
246 platform = "mips32-linux";
248 else
249 if (header.ehdr32.e_machine == EM_MIPS &&
250 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
251 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
252 (header.ehdr32.e_flags & E_MIPS_ABI2)) {
253 platform = "mips64-linux";
255 else
256 if (header.ehdr32.e_machine == EM_NANOMIPS &&
257 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
258 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
259 platform = "nanomips-linux";
262 else if (header.c[EI_DATA] == ELFDATA2MSB) {
263 if (header.ehdr32.e_machine == EM_PPC &&
264 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
265 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
266 platform = "ppc32-linux";
268 else
269 if (header.ehdr32.e_machine == EM_MIPS &&
270 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
271 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
272 (header.ehdr32.e_flags & E_MIPS_ABI_O32)) {
273 platform = "mips32-linux";
275 else
276 if (header.ehdr32.e_machine == EM_MIPS &&
277 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
278 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX) &&
279 (header.ehdr32.e_flags & E_MIPS_ABI2)) {
280 platform = "mips64-linux";
282 else
283 if (header.ehdr32.e_machine == EM_NANOMIPS &&
284 (header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
285 header.ehdr32.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
286 platform = "nanomips-linux";
290 } else if (n_bytes >= sizeof(Elf64_Ehdr) && header.c[EI_CLASS] == ELFCLASS64) {
292 if (header.c[EI_DATA] == ELFDATA2LSB) {
293 # if defined(VGO_solaris)
294 if (header.ehdr64.e_machine == EM_X86_64 &&
295 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
296 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)) {
297 platform = "amd64-solaris";
299 else
300 # endif
301 if (header.ehdr64.e_machine == EM_X86_64 &&
302 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
303 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
304 platform = "amd64-linux";
305 } else if (header.ehdr64.e_machine == EM_MIPS &&
306 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
307 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
308 platform = "mips64-linux";
309 } else if (header.ehdr64.e_machine == EM_AARCH64 &&
310 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
311 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
312 platform = "arm64-linux";
313 } else if (header.ehdr64.e_machine == EM_PPC64 &&
314 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
315 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
316 platform = "ppc64le-linux";
318 } else if (header.c[EI_DATA] == ELFDATA2MSB) {
319 # if !defined(VGPV_arm_linux_android) \
320 && !defined(VGPV_x86_linux_android) \
321 && !defined(VGPV_mips32_linux_android) \
322 && !defined(VGPV_arm64_linux_android)
323 if (header.ehdr64.e_machine == EM_PPC64 &&
324 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
325 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
326 platform = "ppc64be-linux";
328 else
329 if (header.ehdr64.e_machine == EM_S390 &&
330 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
331 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
332 platform = "s390x-linux";
333 } else if (header.ehdr64.e_machine == EM_MIPS &&
334 (header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_SYSV ||
335 header.ehdr64.e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
336 platform = "mips64-linux";
338 # endif
343 VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
344 platform ? platform : "unknown");
346 free (client);
348 return platform;
351 /* Where we expect to find all our aux files */
352 static const char *valgrind_lib = VG_LIBDIR;
354 int main(int argc, char** argv, char** envp)
356 int i, j, loglevel, r;
357 const char *toolname = NULL;
358 const char *clientname = NULL;
359 const char *platform;
360 const char *default_platform;
361 const char *cp;
362 const char *linkname;
363 char *toolfile;
364 const char *launcher_name;
365 char* new_line;
366 char** new_env;
368 /* Start the debugging-log system ASAP. First find out how many
369 "-d"s were specified. This is a pre-scan of the command line.
370 At the same time, look for the tool name. */
371 loglevel = 0;
372 for (i = 1; i < argc; i++) {
373 if (argv[i][0] != '-') {
374 clientname = argv[i];
375 break;
377 if (0 == strcmp(argv[i], "--")) {
378 if (i+1 < argc)
379 clientname = argv[i+1];
380 break;
382 if (0 == strcmp(argv[i], "-d"))
383 loglevel++;
384 if (0 == strncmp(argv[i], "--tool=", 7))
385 toolname = argv[i] + 7;
388 /* ... and start the debug logger. Now we can safely emit logging
389 messages all through startup. */
390 VG_(debugLog_startup)(loglevel, "Stage 1");
392 /* Make sure we know which tool we're using */
393 if (toolname) {
394 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
395 } else {
396 VG_(debugLog)(1, "launcher",
397 "no tool requested, defaulting to 'memcheck'\n");
398 toolname = "memcheck";
401 /* Select a platform to use if we can't decide that by looking at
402 the executable (eg because it's a shell script). VG_PLATFORM is the
403 default_platform. Its value is defined in coregrind/Makefile.am and
404 typically it is the primary build target. Unless the primary build
405 target is not built is not built in which case VG_PLATFORM is the
406 secondary build target. */
407 # if defined(VGO_linux)
408 if ((0==strcmp(VG_PLATFORM,"x86-linux")) ||
409 (0==strcmp(VG_PLATFORM,"amd64-linux")) ||
410 (0==strcmp(VG_PLATFORM,"ppc32-linux")) ||
411 (0==strcmp(VG_PLATFORM,"ppc64be-linux")) ||
412 (0==strcmp(VG_PLATFORM,"ppc64le-linux")) ||
413 (0==strcmp(VG_PLATFORM,"arm-linux")) ||
414 (0==strcmp(VG_PLATFORM,"arm64-linux")) ||
415 (0==strcmp(VG_PLATFORM,"s390x-linux")) ||
416 (0==strcmp(VG_PLATFORM,"mips32-linux")) ||
417 (0==strcmp(VG_PLATFORM,"mips64-linux")) ||
418 (0==strcmp(VG_PLATFORM,"nanomips-linux")))
419 default_platform = VG_PLATFORM;
420 # elif defined(VGO_solaris)
421 if ((0==strcmp(VG_PLATFORM,"x86-solaris")) ||
422 (0==strcmp(VG_PLATFORM,"amd64-solaris")))
423 default_platform = SOLARIS_LAUNCHER_DEFAULT_PLATFORM;
424 # else
425 # error Unknown OS
426 # endif
427 else
428 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
430 /* Work out what platform to use, or use the default platform if
431 not possible. */
432 if (clientname == NULL) {
433 VG_(debugLog)(1, "launcher",
434 "no client specified, defaulting platform to '%s'\n",
435 default_platform);
436 platform = default_platform;
437 } else if ((platform = select_platform(clientname)) != NULL) {
438 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
439 } else {
440 VG_(debugLog)(1, "launcher",
441 "no platform detected, defaulting platform to '%s'\n",
442 default_platform);
443 platform = default_platform;
446 /* Figure out the name of this executable (viz, the launcher), so
447 we can tell stage2. stage2 will use the name for recursive
448 invocations of valgrind on child processes. */
449 # if defined(VGO_linux)
450 linkname = "/proc/self/exe";
451 # elif defined(VGO_solaris)
452 linkname = "/proc/self/path/a.out";
453 # else
454 # error Unknown OS
455 # endif
456 unsigned bufsiz = 0;
457 char *buf = NULL;
459 while (42) {
460 bufsiz += 500;
461 buf = realloc(buf, bufsiz);
462 if (buf == NULL)
463 barf("realloc of buf failed.");
464 r = readlink(linkname, buf, bufsiz);
465 if (r == -1) {
466 /* If /proc/self/exe (/proc/self/path/a.out) can't be followed, don't
467 give up. Instead continue with an empty string for VALGRIND_LAUNCHER.
468 In the sys_execve wrapper, this is tested, and if found to be empty,
469 fail the execve. */
470 fprintf(stderr, "valgrind: warning (non-fatal): "
471 "readlink(\"%s\") failed.\n", linkname);
472 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
473 "will not work.\n");
474 launcher_name = "";
475 break;
477 if (r == bufsiz) continue; // buffer to small; retry
479 assert(r < bufsiz); // paranoia
481 buf[r] = '\0';
482 launcher_name = buf;
483 break;
486 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
487 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
488 + strlen(launcher_name) + 1);
489 if (new_line == NULL)
490 barf("malloc of new_line failed.");
491 strcpy(new_line, VALGRIND_LAUNCHER);
492 strcat(new_line, "=");
493 strcat(new_line, launcher_name);
495 for (j = 0; envp[j]; j++)
497 new_env = malloc((j+2) * sizeof(char*));
498 if (new_env == NULL)
499 barf("malloc of new_env failed.");
500 for (i = 0; i < j; i++)
501 new_env[i] = envp[i];
502 new_env[i++] = new_line;
503 new_env[i++] = NULL;
504 assert(i == j+2);
506 /* Establish the correct VALGRIND_LIB. */
507 cp = getenv(VALGRIND_LIB);
509 if (cp != NULL)
510 valgrind_lib = cp;
512 /* Build the stage2 invocation, and execve it. Bye! */
513 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
514 if (toolfile == NULL)
515 barf("malloc of toolfile failed.");
516 sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);
518 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
520 execve(toolfile, argv, new_env);
522 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
523 toolname, platform, strerror(errno));
525 exit(1);