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, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 The GNU General Public License is contained in the file COPYING.
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. */
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. */
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"
57 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
61 #define EM_AARCH64 183 // ditto
65 #define EM_PPC64 21 // ditto
68 /* Report fatal errors */
69 __attribute__((noreturn
))
70 static void barf ( const char *format
, ... )
74 va_start(vargs
, format
);
75 fprintf(stderr
, "valgrind: Cannot continue: ");
76 vfprintf(stderr
, format
, vargs
);
77 fprintf(stderr
, "\n");
85 /* Search the path for the client program */
86 static const char *find_client(const char *clientname
)
89 const char *path
= getenv("PATH");
92 assert(clientname
!= NULL
);
94 if (path
== NULL
) return clientname
;
96 /* Make the size of the FULLNAME buffer large enough. */
97 unsigned need
= strlen(path
) + strlen("/") + strlen(clientname
) + 1;
99 fullname
= malloc(need
);
100 if (fullname
== NULL
)
101 barf("malloc of fullname failed.");
105 if ((colon
= strchr(path
, ':')) == NULL
)
107 strcpy(fullname
, path
);
112 strncpy(fullname
, path
, colon
- path
);
113 fullname
[colon
- path
] = '\0';
117 strcat(fullname
, "/");
118 strcat(fullname
, clientname
);
120 if (access(fullname
, R_OK
|X_OK
) == 0)
128 /* Examine the client and work out which platform it is for */
129 static const char *select_platform(const char *clientname
)
138 const char *platform
= NULL
;
140 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", clientname
);
142 if (strchr(clientname
, '/') == NULL
)
143 clientname
= find_client(clientname
);
145 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", clientname
);
147 if ((fd
= open(clientname
, O_RDONLY
)) < 0)
149 // barf("open(%s): %s", clientname, strerror(errno));
151 VG_(debugLog
)(2, "launcher", "opened '%s'\n", clientname
);
153 n_bytes
= read(fd
, header
.c
, sizeof(header
));
159 VG_(debugLog
)(2, "launcher", "read %ld bytes from '%s'\n",
160 (long int)n_bytes
, clientname
);
162 if (header
.c
[0] == '#' && header
.c
[1] == '!') {
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
.c
[n_bytes
] = '\0';
169 char *eol
= strchr(header
.c
, '\n');
174 while (header
.c
[i
] == ' '|| header
.c
[i
] == '\t')
177 // Get the interpreter name.
178 const char *interp
= header
.c
+ i
;
180 if (header
.c
[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";
191 while (header
.c
[i
]) {
192 if (header
.c
[i
] == ' ' || header
.c
[i
] == '\t') break;
198 platform
= select_platform(interp
);
200 } else if (n_bytes
>= SELFMAG
&& memcmp(header
.c
, ELFMAG
, SELFMAG
) == 0) {
202 if (n_bytes
>= sizeof(Elf32_Ehdr
) && header
.c
[EI_CLASS
] == ELFCLASS32
) {
204 if (header
.c
[EI_DATA
] == ELFDATA2LSB
) {
205 # if defined(VGO_solaris)
206 if (header
.ehdr32
.e_machine
== EM_386
&&
207 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
208 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SOLARIS
)) {
209 platform
= "x86-solaris";
213 if (header
.ehdr32
.e_machine
== EM_386
&&
214 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
215 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
216 platform
= "x86-linux";
219 if (header
.ehdr32
.e_machine
== EM_ARM
&&
220 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
221 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
222 platform
= "arm-linux";
225 if (header
.ehdr32
.e_machine
== EM_MIPS
&&
226 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
227 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
228 platform
= "mips32-linux";
231 else if (header
.c
[EI_DATA
] == ELFDATA2MSB
) {
232 if (header
.ehdr32
.e_machine
== EM_PPC
&&
233 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
234 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
235 platform
= "ppc32-linux";
238 if (header
.ehdr32
.e_machine
== EM_MIPS
&&
239 (header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
240 header
.ehdr32
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
241 platform
= "mips32-linux";
245 } else if (n_bytes
>= sizeof(Elf64_Ehdr
) && header
.c
[EI_CLASS
] == ELFCLASS64
) {
247 if (header
.c
[EI_DATA
] == ELFDATA2LSB
) {
248 # if defined(VGO_solaris)
249 if (header
.ehdr64
.e_machine
== EM_X86_64
&&
250 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
251 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SOLARIS
)) {
252 platform
= "amd64-solaris";
256 if (header
.ehdr64
.e_machine
== EM_X86_64
&&
257 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
258 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
259 platform
= "amd64-linux";
260 } else if (header
.ehdr64
.e_machine
== EM_MIPS
&&
261 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
262 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
263 platform
= "mips64-linux";
264 } else if (header
.ehdr64
.e_machine
== EM_AARCH64
&&
265 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
266 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
267 platform
= "arm64-linux";
268 } else if (header
.ehdr64
.e_machine
== EM_PPC64
&&
269 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
270 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
271 platform
= "ppc64le-linux";
273 } else if (header
.c
[EI_DATA
] == ELFDATA2MSB
) {
274 # if !defined(VGPV_arm_linux_android) \
275 && !defined(VGPV_x86_linux_android) \
276 && !defined(VGPV_mips32_linux_android) \
277 && !defined(VGPV_arm64_linux_android)
278 if (header
.ehdr64
.e_machine
== EM_PPC64
&&
279 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
280 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
281 platform
= "ppc64be-linux";
284 if (header
.ehdr64
.e_machine
== EM_S390
&&
285 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
286 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
287 platform
= "s390x-linux";
288 } else if (header
.ehdr64
.e_machine
== EM_MIPS
&&
289 (header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
290 header
.ehdr64
.e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
291 platform
= "mips64-linux";
298 VG_(debugLog
)(2, "launcher", "selected platform '%s'\n",
299 platform
? platform
: "unknown");
304 /* Where we expect to find all our aux files */
305 static const char *valgrind_lib
= VG_LIBDIR
;
307 int main(int argc
, char** argv
, char** envp
)
309 int i
, j
, loglevel
, r
;
310 const char *toolname
= NULL
;
311 const char *clientname
= NULL
;
312 const char *platform
;
313 const char *default_platform
;
315 const char *linkname
;
317 const char *launcher_name
;
321 /* Start the debugging-log system ASAP. First find out how many
322 "-d"s were specified. This is a pre-scan of the command line.
323 At the same time, look for the tool name. */
325 for (i
= 1; i
< argc
; i
++) {
326 if (argv
[i
][0] != '-') {
327 clientname
= argv
[i
];
330 if (0 == strcmp(argv
[i
], "--")) {
332 clientname
= argv
[i
+1];
335 if (0 == strcmp(argv
[i
], "-d"))
337 if (0 == strncmp(argv
[i
], "--tool=", 7))
338 toolname
= argv
[i
] + 7;
341 /* ... and start the debug logger. Now we can safely emit logging
342 messages all through startup. */
343 VG_(debugLog_startup
)(loglevel
, "Stage 1");
345 /* Make sure we know which tool we're using */
347 VG_(debugLog
)(1, "launcher", "tool '%s' requested\n", toolname
);
349 VG_(debugLog
)(1, "launcher",
350 "no tool requested, defaulting to 'memcheck'\n");
351 toolname
= "memcheck";
354 /* Select a platform to use if we can't decide that by looking at
355 the executable (eg because it's a shell script). VG_PLATFORM is the
356 default_platform. Its value is defined in coregrind/Makefile.am and
357 typically it is the primary build target. Unless the primary build
358 target is not built is not built in which case VG_PLATFORM is the
359 secondary build target. */
360 # if defined(VGO_linux)
361 if ((0==strcmp(VG_PLATFORM
,"x86-linux")) ||
362 (0==strcmp(VG_PLATFORM
,"amd64-linux")) ||
363 (0==strcmp(VG_PLATFORM
,"ppc32-linux")) ||
364 (0==strcmp(VG_PLATFORM
,"ppc64be-linux")) ||
365 (0==strcmp(VG_PLATFORM
,"ppc64le-linux")) ||
366 (0==strcmp(VG_PLATFORM
,"arm-linux")) ||
367 (0==strcmp(VG_PLATFORM
,"arm64-linux")) ||
368 (0==strcmp(VG_PLATFORM
,"s390x-linux")) ||
369 (0==strcmp(VG_PLATFORM
,"mips32-linux")) ||
370 (0==strcmp(VG_PLATFORM
,"mips64-linux")))
371 default_platform
= VG_PLATFORM
;
372 # elif defined(VGO_solaris)
373 if ((0==strcmp(VG_PLATFORM
,"x86-solaris")) ||
374 (0==strcmp(VG_PLATFORM
,"amd64-solaris")))
375 default_platform
= SOLARIS_LAUNCHER_DEFAULT_PLATFORM
;
380 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM
);
382 /* Work out what platform to use, or use the default platform if
384 if (clientname
== NULL
) {
385 VG_(debugLog
)(1, "launcher",
386 "no client specified, defaulting platform to '%s'\n",
388 platform
= default_platform
;
389 } else if ((platform
= select_platform(clientname
)) != NULL
) {
390 VG_(debugLog
)(1, "launcher", "selected platform '%s'\n", platform
);
392 VG_(debugLog
)(1, "launcher",
393 "no platform detected, defaulting platform to '%s'\n",
395 platform
= default_platform
;
398 /* Figure out the name of this executable (viz, the launcher), so
399 we can tell stage2. stage2 will use the name for recursive
400 invocations of valgrind on child processes. */
401 # if defined(VGO_linux)
402 linkname
= "/proc/self/exe";
403 # elif defined(VGO_solaris)
404 linkname
= "/proc/self/path/a.out";
413 buf
= realloc(buf
, bufsiz
);
415 barf("realloc of buf failed.");
416 r
= readlink(linkname
, buf
, bufsiz
);
418 /* If /proc/self/exe (/proc/self/path/a.out) can't be followed, don't
419 give up. Instead continue with an empty string for VALGRIND_LAUNCHER.
420 In the sys_execve wrapper, this is tested, and if found to be empty,
422 fprintf(stderr
, "valgrind: warning (non-fatal): "
423 "readlink(\"%s\") failed.\n", linkname
);
424 fprintf(stderr
, "valgrind: continuing, however --trace-children=yes "
429 if (r
== bufsiz
) continue; // buffer to small; retry
431 assert(r
< bufsiz
); // paranoia
438 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
439 new_line
= malloc(strlen(VALGRIND_LAUNCHER
) + 1
440 + strlen(launcher_name
) + 1);
441 if (new_line
== NULL
)
442 barf("malloc of new_line failed.");
443 strcpy(new_line
, VALGRIND_LAUNCHER
);
444 strcat(new_line
, "=");
445 strcat(new_line
, launcher_name
);
447 for (j
= 0; envp
[j
]; j
++)
449 new_env
= malloc((j
+2) * sizeof(char*));
451 barf("malloc of new_env failed.");
452 for (i
= 0; i
< j
; i
++)
453 new_env
[i
] = envp
[i
];
454 new_env
[i
++] = new_line
;
458 /* Establish the correct VALGRIND_LIB. */
459 cp
= getenv(VALGRIND_LIB
);
464 /* Build the stage2 invocation, and execve it. Bye! */
465 toolfile
= malloc(strlen(valgrind_lib
) + strlen(toolname
) + strlen(platform
) + 3);
466 if (toolfile
== NULL
)
467 barf("malloc of toolfile failed.");
468 sprintf(toolfile
, "%s/%s-%s", valgrind_lib
, toolname
, platform
);
470 VG_(debugLog
)(1, "launcher", "launching %s\n", toolfile
);
472 execve(toolfile
, argv
, new_env
);
474 fprintf(stderr
, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
475 toolname
, platform
, strerror(errno
));