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)
132 else if (access(fullname
, X_OK
) == 0)
133 barf("Need read permission on %s", fullname
);
137 return strdup(clientname
);
140 /* Examine the client and work out which platform it is for */
141 static const char *select_platform(const char *clientname
)
150 const char *platform
= NULL
;
153 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", clientname
);
155 if (strchr(clientname
, '/') == NULL
)
156 client
= find_client(clientname
);
158 client
= strdup(clientname
);
160 if (strcmp (client
, clientname
) != 0)
161 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", client
);
163 if ((fd
= open(client
, O_RDONLY
)) < 0) {
168 // barf("open(%s): %s", clientname, strerror(errno));
170 VG_(debugLog
)(2, "launcher", "opened '%s'\n", client
);
172 n_bytes
= read(fd
, header
.c
, sizeof(header
));
178 VG_(debugLog
)(2, "launcher", "read %ld bytes from '%s'\n",
179 (long int)n_bytes
, client
);
181 if (header
.c
[0] == '#' && header
.c
[1] == '!') {
184 STATIC_ASSERT(VKI_BINPRM_BUF_SIZE
< sizeof header
);
185 if (n_bytes
> VKI_BINPRM_BUF_SIZE
)
186 n_bytes
= VKI_BINPRM_BUF_SIZE
- 1;
187 header
.c
[n_bytes
] = '\0';
188 char *eol
= strchr(header
.c
, '\n');
193 while (header
.c
[i
] == ' '|| header
.c
[i
] == '\t')
196 // Get the interpreter name.
197 const char *interp
= header
.c
+ i
;
199 if (header
.c
[i
] == '\0') {
200 // No interpreter was found; fall back to default shell
201 # if defined(VGPV_arm_linux_android) \
202 || defined(VGPV_x86_linux_android) \
203 || defined(VGPV_mips32_linux_android) \
204 || defined(VGPV_arm64_linux_android)
205 interp
= "/system/bin/sh";
210 while (header
.c
[i
]) {
211 if (header
.c
[i
] == ' ' || header
.c
[i
] == '\t') break;
217 platform
= select_platform(interp
);
219 } else if (n_bytes
>= SELFMAG
&& memcmp(header
.c
, ELFMAG
, SELFMAG
) == 0) {
221 if (n_bytes
>= sizeof(Elf32_Ehdr
) && header
.c
[EI_CLASS
] == ELFCLASS32
) {
223 if (header
.c
[EI_DATA
] == ELFDATA2LSB
) {
224 # if defined(VGO_solaris)
225 if (header
.ehdr32
.e_machine
== EM_386
&&
226 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
227 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SOLARIS
)) {
228 platform
= "x86-solaris";
232 if (header
.ehdr32
.e_machine
== EM_386
&&
233 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
234 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
235 platform
= "x86-linux";
238 if (header
.ehdr32
.e_machine
== EM_ARM
&&
239 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
240 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
241 platform
= "arm-linux";
244 if (header
.ehdr32
.e_machine
== EM_MIPS
&&
245 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
246 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
) &&
247 (header
.ehdr32
.e_flags
& E_MIPS_ABI_O32
)) {
248 platform
= "mips32-linux";
251 if (header
.ehdr32
.e_machine
== EM_MIPS
&&
252 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
253 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
) &&
254 (header
.ehdr32
.e_flags
& E_MIPS_ABI2
)) {
255 platform
= "mips64-linux";
258 if (header
.ehdr32
.e_machine
== EM_NANOMIPS
&&
259 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
260 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
261 platform
= "nanomips-linux";
264 else if (header
.c
[EI_DATA
] == ELFDATA2MSB
) {
265 if (header
.ehdr32
.e_machine
== EM_PPC
&&
266 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
267 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
268 platform
= "ppc32-linux";
271 if (header
.ehdr32
.e_machine
== EM_MIPS
&&
272 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
273 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
) &&
274 (header
.ehdr32
.e_flags
& E_MIPS_ABI_O32
)) {
275 platform
= "mips32-linux";
278 if (header
.ehdr32
.e_machine
== EM_MIPS
&&
279 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
280 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
) &&
281 (header
.ehdr32
.e_flags
& E_MIPS_ABI2
)) {
282 platform
= "mips64-linux";
285 if (header
.ehdr32
.e_machine
== EM_NANOMIPS
&&
286 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
287 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
288 platform
= "nanomips-linux";
292 } else if (n_bytes
>= sizeof(Elf64_Ehdr
) && header
.c
[EI_CLASS
] == ELFCLASS64
) {
294 if (header
.c
[EI_DATA
] == ELFDATA2LSB
) {
295 # if defined(VGO_solaris)
296 if (header
.ehdr64
.e_machine
== EM_X86_64
&&
297 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
298 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SOLARIS
)) {
299 platform
= "amd64-solaris";
303 if (header
.ehdr64
.e_machine
== EM_X86_64
&&
304 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
305 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
306 platform
= "amd64-linux";
307 } else if (header
.ehdr64
.e_machine
== EM_MIPS
&&
308 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
309 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
310 platform
= "mips64-linux";
311 } else if (header
.ehdr64
.e_machine
== EM_AARCH64
&&
312 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
313 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
314 platform
= "arm64-linux";
315 } else if (header
.ehdr64
.e_machine
== EM_PPC64
&&
316 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
317 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
318 platform
= "ppc64le-linux";
320 } else if (header
.c
[EI_DATA
] == ELFDATA2MSB
) {
321 # if !defined(VGPV_arm_linux_android) \
322 && !defined(VGPV_x86_linux_android) \
323 && !defined(VGPV_mips32_linux_android) \
324 && !defined(VGPV_arm64_linux_android)
325 if (header
.ehdr64
.e_machine
== EM_PPC64
&&
326 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
327 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
328 platform
= "ppc64be-linux";
331 if (header
.ehdr64
.e_machine
== EM_S390
&&
332 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
333 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
334 platform
= "s390x-linux";
335 } else if (header
.ehdr64
.e_machine
== EM_MIPS
&&
336 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
337 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
338 platform
= "mips64-linux";
345 VG_(debugLog
)(2, "launcher", "selected platform '%s'\n",
346 platform
? platform
: "unknown");
353 /* Where we expect to find all our aux files */
354 static const char *valgrind_lib
= VG_LIBDIR
;
356 int main(int argc
, char** argv
, char** envp
)
358 int i
, j
, loglevel
, r
;
359 const char *toolname
= NULL
;
360 const char *clientname
= NULL
;
361 const char *platform
;
362 const char *default_platform
;
364 const char *linkname
;
366 const char *launcher_name
;
370 /* Start the debugging-log system ASAP. First find out how many
371 "-d"s were specified. This is a pre-scan of the command line.
372 At the same time, look for the tool name. */
374 for (i
= 1; i
< argc
; i
++) {
375 if (argv
[i
][0] != '-') {
376 clientname
= argv
[i
];
379 if (0 == strcmp(argv
[i
], "--")) {
381 clientname
= argv
[i
+1];
384 if (0 == strcmp(argv
[i
], "-d"))
386 if (0 == strncmp(argv
[i
], "--tool=", 7))
387 toolname
= argv
[i
] + 7;
390 /* ... and start the debug logger. Now we can safely emit logging
391 messages all through startup. */
392 VG_(debugLog_startup
)(loglevel
, "Stage 1");
394 /* Make sure we know which tool we're using */
396 VG_(debugLog
)(1, "launcher", "tool '%s' requested\n", toolname
);
398 VG_(debugLog
)(1, "launcher",
399 "no tool requested, defaulting to 'memcheck'\n");
400 toolname
= "memcheck";
403 /* Select a platform to use if we can't decide that by looking at
404 the executable (eg because it's a shell script). VG_PLATFORM is the
405 default_platform. Its value is defined in coregrind/Makefile.am and
406 typically it is the primary build target. Unless the primary build
407 target is not built is not built in which case VG_PLATFORM is the
408 secondary build target. */
409 # if defined(VGO_linux)
410 if ((0==strcmp(VG_PLATFORM
,"x86-linux")) ||
411 (0==strcmp(VG_PLATFORM
,"amd64-linux")) ||
412 (0==strcmp(VG_PLATFORM
,"ppc32-linux")) ||
413 (0==strcmp(VG_PLATFORM
,"ppc64be-linux")) ||
414 (0==strcmp(VG_PLATFORM
,"ppc64le-linux")) ||
415 (0==strcmp(VG_PLATFORM
,"arm-linux")) ||
416 (0==strcmp(VG_PLATFORM
,"arm64-linux")) ||
417 (0==strcmp(VG_PLATFORM
,"s390x-linux")) ||
418 (0==strcmp(VG_PLATFORM
,"mips32-linux")) ||
419 (0==strcmp(VG_PLATFORM
,"mips64-linux")) ||
420 (0==strcmp(VG_PLATFORM
,"nanomips-linux")))
421 default_platform
= VG_PLATFORM
;
422 # elif defined(VGO_solaris)
423 if ((0==strcmp(VG_PLATFORM
,"x86-solaris")) ||
424 (0==strcmp(VG_PLATFORM
,"amd64-solaris")))
425 default_platform
= SOLARIS_LAUNCHER_DEFAULT_PLATFORM
;
430 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM
);
432 /* Work out what platform to use, or use the default platform if
434 if (clientname
== NULL
) {
435 VG_(debugLog
)(1, "launcher",
436 "no client specified, defaulting platform to '%s'\n",
438 platform
= default_platform
;
439 } else if ((platform
= select_platform(clientname
)) != NULL
) {
440 VG_(debugLog
)(1, "launcher", "selected platform '%s'\n", platform
);
442 VG_(debugLog
)(1, "launcher",
443 "no platform detected, defaulting platform to '%s'\n",
445 platform
= default_platform
;
448 /* Figure out the name of this executable (viz, the launcher), so
449 we can tell stage2. stage2 will use the name for recursive
450 invocations of valgrind on child processes. */
451 # if defined(VGO_linux)
452 linkname
= "/proc/self/exe";
453 # elif defined(VGO_solaris)
454 linkname
= "/proc/self/path/a.out";
463 buf
= realloc(buf
, bufsiz
);
465 barf("realloc of buf failed.");
466 r
= readlink(linkname
, buf
, bufsiz
);
468 /* If /proc/self/exe (/proc/self/path/a.out) can't be followed, don't
469 give up. Instead continue with an empty string for VALGRIND_LAUNCHER.
470 In the sys_execve wrapper, this is tested, and if found to be empty,
472 fprintf(stderr
, "valgrind: warning (non-fatal): "
473 "readlink(\"%s\") failed.\n", linkname
);
474 fprintf(stderr
, "valgrind: continuing, however --trace-children=yes "
479 if (r
== bufsiz
) continue; // buffer to small; retry
481 assert(r
< bufsiz
); // paranoia
488 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
489 new_line
= malloc(strlen(VALGRIND_LAUNCHER
) + 1
490 + strlen(launcher_name
) + 1);
491 if (new_line
== NULL
)
492 barf("malloc of new_line failed.");
493 strcpy(new_line
, VALGRIND_LAUNCHER
);
494 strcat(new_line
, "=");
495 strcat(new_line
, launcher_name
);
497 for (j
= 0; envp
[j
]; j
++)
499 new_env
= malloc((j
+2) * sizeof(char*));
501 barf("malloc of new_env failed.");
502 for (i
= 0; i
< j
; i
++)
503 new_env
[i
] = envp
[i
];
504 new_env
[i
++] = new_line
;
508 /* Establish the correct VALGRIND_LIB. */
509 cp
= getenv(VALGRIND_LIB
);
514 /* Build the stage2 invocation, and execve it. Bye! */
515 toolfile
= malloc(strlen(valgrind_lib
) + strlen(toolname
) + strlen(platform
) + 3);
516 if (toolfile
== NULL
)
517 barf("malloc of toolfile failed.");
518 sprintf(toolfile
, "%s/%s-%s", valgrind_lib
, toolname
, platform
);
520 VG_(debugLog
)(1, "launcher", "launching %s\n", toolfile
);
522 execve(toolfile
, argv
, new_env
);
524 fprintf(stderr
, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
525 toolname
, platform
, strerror(errno
));