1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Launching valgrind m_launcher.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2017 Julian Seward
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"
55 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
59 #define EM_AARCH64 183 // ditto
63 #define EM_PPC64 21 // ditto
67 #define EM_NANOMIPS 249
70 #ifndef E_MIPS_ABI_O32
71 #define E_MIPS_ABI_O32 0x00001000
75 #define E_MIPS_ABI2 0x00000020
78 /* Report fatal errors */
79 __attribute__((noreturn
))
80 static void barf ( const char *format
, ... )
84 va_start(vargs
, format
);
85 fprintf(stderr
, "valgrind: Cannot continue: ");
86 vfprintf(stderr
, format
, vargs
);
87 fprintf(stderr
, "\n");
95 /* Search the path for the client program */
96 static char *find_client(const char *clientname
)
99 const char *path
= getenv("PATH");
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.");
115 if ((colon
= strchr(path
, ':')) == NULL
)
117 strcpy(fullname
, path
);
122 strncpy(fullname
, path
, colon
- path
);
123 fullname
[colon
- path
] = '\0';
127 strcat(fullname
, "/");
128 strcat(fullname
, clientname
);
130 if (access(fullname
, R_OK
|X_OK
) == 0)
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
)
148 const char *platform
= NULL
;
151 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", clientname
);
153 if (strchr(clientname
, '/') == NULL
)
154 client
= find_client(clientname
);
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) {
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
));
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] == '!') {
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');
191 while (header
.c
[i
] == ' '|| header
.c
[i
] == '\t')
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";
208 while (header
.c
[i
]) {
209 if (header
.c
[i
] == ' ' || header
.c
[i
] == '\t') break;
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";
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";
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";
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";
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";
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";
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";
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";
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";
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";
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";
343 VG_(debugLog
)(2, "launcher", "selected platform '%s'\n",
344 platform
? platform
: "unknown");
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
;
362 const char *linkname
;
364 const char *launcher_name
;
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. */
372 for (i
= 1; i
< argc
; i
++) {
373 if (argv
[i
][0] != '-') {
374 clientname
= argv
[i
];
377 if (0 == strcmp(argv
[i
], "--")) {
379 clientname
= argv
[i
+1];
382 if (0 == strcmp(argv
[i
], "-d"))
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 */
394 VG_(debugLog
)(1, "launcher", "tool '%s' requested\n", toolname
);
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
;
428 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM
);
430 /* Work out what platform to use, or use the default platform if
432 if (clientname
== NULL
) {
433 VG_(debugLog
)(1, "launcher",
434 "no client specified, defaulting platform to '%s'\n",
436 platform
= default_platform
;
437 } else if ((platform
= select_platform(clientname
)) != NULL
) {
438 VG_(debugLog
)(1, "launcher", "selected platform '%s'\n", platform
);
440 VG_(debugLog
)(1, "launcher",
441 "no platform detected, defaulting platform to '%s'\n",
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";
461 buf
= realloc(buf
, bufsiz
);
463 barf("realloc of buf failed.");
464 r
= readlink(linkname
, buf
, bufsiz
);
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,
470 fprintf(stderr
, "valgrind: warning (non-fatal): "
471 "readlink(\"%s\") failed.\n", linkname
);
472 fprintf(stderr
, "valgrind: continuing, however --trace-children=yes "
477 if (r
== bufsiz
) continue; // buffer to small; retry
479 assert(r
< bufsiz
); // paranoia
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*));
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
;
506 /* Establish the correct VALGRIND_LIB. */
507 cp
= getenv(VALGRIND_LIB
);
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
));