2 /*--------------------------------------------------------------------*/
3 /*--- Launching valgrind m_launcher.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2013 Julian Seward
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 /* Note: this is a "normal" program and not part of Valgrind proper,
32 and so it doesn't have to conform to Valgrind's arcane rules on
33 no-glibc-usage etc. */
35 /* Include valgrind headers before system headers to avoid problems
36 with the system headers #defining things which are used as names
37 of structure members in vki headers. */
39 #include "pub_core_debuglog.h"
40 #include "pub_core_vki.h" // Avoids warnings from
41 // pub_core_libcfile.h
42 #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER
43 #include "pub_core_ume.h"
56 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
60 #define EM_AARCH64 183 // ditto
64 #define EM_PPC64 21 // ditto
67 /* Report fatal errors */
68 __attribute__((noreturn
))
69 static void barf ( const char *format
, ... )
73 va_start(vargs
, format
);
74 fprintf(stderr
, "valgrind: Cannot continue: ");
75 vfprintf(stderr
, format
, vargs
);
76 fprintf(stderr
, "\n");
84 /* Search the path for the client program */
85 static const char *find_client(const char *clientname
)
88 const char *path
= getenv("PATH");
91 assert(clientname
!= NULL
);
93 if (path
== NULL
) return clientname
;
95 /* Make the size of the FULLNAME buffer large enough. */
96 unsigned need
= strlen(path
) + strlen("/") + strlen(clientname
) + 1;
98 fullname
= malloc(need
);
100 barf("malloc of fullname failed.");
104 if ((colon
= strchr(path
, ':')) == NULL
)
106 strcpy(fullname
, path
);
111 strncpy(fullname
, path
, colon
- path
);
112 fullname
[colon
- path
] = '\0';
116 strcat(fullname
, "/");
117 strcat(fullname
, clientname
);
119 if (access(fullname
, R_OK
|X_OK
) == 0)
127 /* Examine the client and work out which platform it is for */
128 static const char *select_platform(const char *clientname
)
133 const char *platform
= NULL
;
135 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", clientname
);
137 if (strchr(clientname
, '/') == NULL
)
138 clientname
= find_client(clientname
);
140 VG_(debugLog
)(2, "launcher", "selecting platform for '%s'\n", clientname
);
142 if ((fd
= open(clientname
, O_RDONLY
)) < 0)
144 // barf("open(%s): %s", clientname, strerror(errno));
146 VG_(debugLog
)(2, "launcher", "opened '%s'\n", clientname
);
148 n_bytes
= read(fd
, header
, sizeof(header
));
154 VG_(debugLog
)(2, "launcher", "read %ld bytes from '%s'\n",
155 (long int)n_bytes
, clientname
);
157 if (header
[0] == '#' && header
[1] == '!') {
159 char *interp
= (char *)header
+ 2;
163 if (i
== n_bytes
) return NULL
;
164 if (' ' != header
[i
] && '\t' != header
[i
]) break;
168 // Get the interpreter name.
171 if (i
== n_bytes
) break;
172 if (isspace(header
[i
])) break;
175 if (i
== n_bytes
) return NULL
;
178 platform
= select_platform(interp
);
180 } else if (n_bytes
>= SELFMAG
&& memcmp(header
, ELFMAG
, SELFMAG
) == 0) {
182 if (n_bytes
>= sizeof(Elf32_Ehdr
) && header
[EI_CLASS
] == ELFCLASS32
) {
183 const Elf32_Ehdr
*ehdr
= (Elf32_Ehdr
*)header
;
185 if (header
[EI_DATA
] == ELFDATA2LSB
) {
186 if (ehdr
->e_machine
== EM_386
&&
187 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
188 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
189 platform
= "x86-linux";
192 if (ehdr
->e_machine
== EM_ARM
&&
193 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
194 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
195 platform
= "arm-linux";
198 if (ehdr
->e_machine
== EM_MIPS
&&
199 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
200 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
201 platform
= "mips32-linux";
204 else if (header
[EI_DATA
] == ELFDATA2MSB
) {
205 if (ehdr
->e_machine
== EM_PPC
&&
206 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
207 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
208 platform
= "ppc32-linux";
211 if (ehdr
->e_machine
== EM_MIPS
&&
212 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
213 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
214 platform
= "mips32-linux";
218 } else if (n_bytes
>= sizeof(Elf64_Ehdr
) && header
[EI_CLASS
] == ELFCLASS64
) {
219 const Elf64_Ehdr
*ehdr
= (Elf64_Ehdr
*)header
;
221 if (header
[EI_DATA
] == ELFDATA2LSB
) {
222 if (ehdr
->e_machine
== EM_X86_64
&&
223 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
224 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
225 platform
= "amd64-linux";
226 } else if (ehdr
->e_machine
== EM_MIPS
&&
227 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
228 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
229 platform
= "mips64-linux";
230 } else if (ehdr
->e_machine
== EM_AARCH64
&&
231 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
232 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
233 platform
= "arm64-linux";
234 } else if (ehdr
->e_machine
== EM_PPC64
&&
235 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
236 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
237 platform
= "ppc64le-linux";
239 } else if (header
[EI_DATA
] == ELFDATA2MSB
) {
240 # if !defined(VGPV_arm_linux_android) \
241 && !defined(VGPV_x86_linux_android) \
242 && !defined(VGPV_mips32_linux_android) \
243 && !defined(VGPV_arm64_linux_android)
244 if (ehdr
->e_machine
== EM_PPC64
&&
245 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
246 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
247 platform
= "ppc64be-linux";
250 if (ehdr
->e_machine
== EM_S390
&&
251 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
252 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
253 platform
= "s390x-linux";
254 } else if (ehdr
->e_machine
== EM_MIPS
&&
255 (ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_SYSV
||
256 ehdr
->e_ident
[EI_OSABI
] == ELFOSABI_LINUX
)) {
257 platform
= "mips64-linux";
264 VG_(debugLog
)(2, "launcher", "selected platform '%s'\n",
265 platform
? platform
: "unknown");
270 /* Where we expect to find all our aux files */
271 static const char *valgrind_lib
= VG_LIBDIR
;
273 int main(int argc
, char** argv
, char** envp
)
275 int i
, j
, loglevel
, r
;
276 const char *toolname
= NULL
;
277 const char *clientname
= NULL
;
278 const char *platform
;
279 const char *default_platform
;
282 const char *launcher_name
;
286 /* Start the debugging-log system ASAP. First find out how many
287 "-d"s were specified. This is a pre-scan of the command line.
288 At the same time, look for the tool name. */
290 for (i
= 1; i
< argc
; i
++) {
291 if (argv
[i
][0] != '-') {
292 clientname
= argv
[i
];
295 if (0 == strcmp(argv
[i
], "--")) {
297 clientname
= argv
[i
+1];
300 if (0 == strcmp(argv
[i
], "-d"))
302 if (0 == strncmp(argv
[i
], "--tool=", 7))
303 toolname
= argv
[i
] + 7;
306 /* ... and start the debug logger. Now we can safely emit logging
307 messages all through startup. */
308 VG_(debugLog_startup
)(loglevel
, "Stage 1");
310 /* Make sure we know which tool we're using */
312 VG_(debugLog
)(1, "launcher", "tool '%s' requested\n", toolname
);
314 VG_(debugLog
)(1, "launcher",
315 "no tool requested, defaulting to 'memcheck'\n");
316 toolname
= "memcheck";
319 /* Select a platform to use if we can't decide that by looking at
320 the executable (eg because it's a shell script). Note that the
321 default_platform is not necessarily either the primary or
322 secondary build target. Instead it's chosen to maximise the
323 chances that /bin/sh will work on it. Hence for a primary
324 target of ppc64-linux we still choose ppc32-linux as the default
325 target, because on most ppc64-linux setups, the basic /bin,
326 /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit
328 if ((0==strcmp(VG_PLATFORM
,"x86-linux")) ||
329 (0==strcmp(VG_PLATFORM
,"amd64-linux")) ||
330 (0==strcmp(VG_PLATFORM
,"ppc32-linux")) ||
331 (0==strcmp(VG_PLATFORM
,"ppc64be-linux")) ||
332 (0==strcmp(VG_PLATFORM
,"ppc64le-linux")) ||
333 (0==strcmp(VG_PLATFORM
,"arm-linux")) ||
334 (0==strcmp(VG_PLATFORM
,"arm64-linux")) ||
335 (0==strcmp(VG_PLATFORM
,"s390x-linux")) ||
336 (0==strcmp(VG_PLATFORM
,"mips32-linux")) ||
337 (0==strcmp(VG_PLATFORM
,"mips64-linux")))
338 default_platform
= VG_PLATFORM
;
340 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM
);
342 /* Work out what platform to use, or use the default platform if
344 if (clientname
== NULL
) {
345 VG_(debugLog
)(1, "launcher",
346 "no client specified, defaulting platform to '%s'\n",
348 platform
= default_platform
;
349 } else if ((platform
= select_platform(clientname
)) != NULL
) {
350 VG_(debugLog
)(1, "launcher", "selected platform '%s'\n", platform
);
352 VG_(debugLog
)(1, "launcher",
353 "no platform detected, defaulting platform to '%s'\n",
355 platform
= default_platform
;
358 /* Figure out the name of this executable (viz, the launcher), so
359 we can tell stage2. stage2 will use the name for recursive
360 invocations of valgrind on child processes. */
366 buf
= realloc(buf
, bufsiz
);
368 barf("realloc of buf failed.");
369 r
= readlink("/proc/self/exe", buf
, bufsiz
);
371 /* If /proc/self/exe can't be followed, don't give up. Instead
372 continue with an empty string for VALGRIND_LAUNCHER. In the
373 sys_execve wrapper, this is tested, and if found to be empty,
375 fprintf(stderr
, "valgrind: warning (non-fatal): "
376 "readlink(\"/proc/self/exe\") failed.\n");
377 fprintf(stderr
, "valgrind: continuing, however --trace-children=yes "
382 if (r
== bufsiz
) continue; // buffer to small; retry
384 assert(r
< bufsiz
); // paranoia
391 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
392 new_line
= malloc(strlen(VALGRIND_LAUNCHER
) + 1
393 + strlen(launcher_name
) + 1);
394 if (new_line
== NULL
)
395 barf("malloc of new_line failed.");
396 strcpy(new_line
, VALGRIND_LAUNCHER
);
397 strcat(new_line
, "=");
398 strcat(new_line
, launcher_name
);
400 for (j
= 0; envp
[j
]; j
++)
402 new_env
= malloc((j
+2) * sizeof(char*));
404 barf("malloc of new_env failed.");
405 for (i
= 0; i
< j
; i
++)
406 new_env
[i
] = envp
[i
];
407 new_env
[i
++] = new_line
;
411 /* Establish the correct VALGRIND_LIB. */
412 cp
= getenv(VALGRIND_LIB
);
417 /* Build the stage2 invocation, and execve it. Bye! */
418 toolfile
= malloc(strlen(valgrind_lib
) + strlen(toolname
) + strlen(platform
) + 3);
419 if (toolfile
== NULL
)
420 barf("malloc of toolfile failed.");
421 sprintf(toolfile
, "%s/%s-%s", valgrind_lib
, toolname
, platform
);
423 VG_(debugLog
)(1, "launcher", "launching %s\n", toolfile
);
425 execve(toolfile
, argv
, new_env
);
427 fprintf(stderr
, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
428 toolname
, platform
, strerror(errno
));