drd: Add a consistency check
[valgrind.git] / coregrind / launcher-linux.c
blobc7f486917faf0fe289a9dd8de57e74c9d627679f
2 /*--------------------------------------------------------------------*/
3 /*--- Launching valgrind m_launcher.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2013 Julian Seward
11 jseward@acm.org
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
26 02111-1307, USA.
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"
45 #include <assert.h>
46 #include <ctype.h>
47 #include <elf.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
55 #ifndef EM_X86_64
56 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
57 #endif
59 #ifndef EM_AARCH64
60 #define EM_AARCH64 183 // ditto
61 #endif
63 #ifndef EM_PPC64
64 #define EM_PPC64 21 // ditto
65 #endif
67 /* Report fatal errors */
68 __attribute__((noreturn))
69 static void barf ( const char *format, ... )
71 va_list vargs;
73 va_start(vargs, format);
74 fprintf(stderr, "valgrind: Cannot continue: ");
75 vfprintf(stderr, format, vargs);
76 fprintf(stderr, "\n");
77 va_end(vargs);
79 exit(1);
80 /*NOTREACHED*/
81 assert(0);
84 /* Search the path for the client program */
85 static const char *find_client(const char *clientname)
87 char *fullname;
88 const char *path = getenv("PATH");
89 const char *colon;
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);
99 if (fullname == NULL)
100 barf("malloc of fullname failed.");
102 while (path)
104 if ((colon = strchr(path, ':')) == NULL)
106 strcpy(fullname, path);
107 path = NULL;
109 else
111 strncpy(fullname, path, colon - path);
112 fullname[colon - path] = '\0';
113 path = colon + 1;
116 strcat(fullname, "/");
117 strcat(fullname, clientname);
119 if (access(fullname, R_OK|X_OK) == 0)
120 return fullname;
122 free(fullname);
124 return clientname;
127 /* Examine the client and work out which platform it is for */
128 static const char *select_platform(const char *clientname)
130 int fd;
131 char header[4096];
132 ssize_t n_bytes;
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)
143 return NULL;
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));
149 close(fd);
150 if (n_bytes < 2) {
151 return NULL;
154 VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
155 (long int)n_bytes, clientname);
157 if (header[0] == '#' && header[1] == '!') {
158 int i = 2;
159 char *interp = (char *)header + 2;
161 // Skip whitespace.
162 while (1) {
163 if (i == n_bytes) return NULL;
164 if (' ' != header[i] && '\t' != header[i]) break;
165 i++;
168 // Get the interpreter name.
169 interp = &header[i];
170 while (1) {
171 if (i == n_bytes) break;
172 if (isspace(header[i])) break;
173 i++;
175 if (i == n_bytes) return NULL;
176 header[i] = '\0';
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";
191 else
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";
197 else
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";
210 else
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";
249 else
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";
259 # endif
264 VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
265 platform ? platform : "unknown");
267 return platform;
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;
280 const char *cp;
281 char *toolfile;
282 const char *launcher_name;
283 char* new_line;
284 char** new_env;
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. */
289 loglevel = 0;
290 for (i = 1; i < argc; i++) {
291 if (argv[i][0] != '-') {
292 clientname = argv[i];
293 break;
295 if (0 == strcmp(argv[i], "--")) {
296 if (i+1 < argc)
297 clientname = argv[i+1];
298 break;
300 if (0 == strcmp(argv[i], "-d"))
301 loglevel++;
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 */
311 if (toolname) {
312 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
313 } else {
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
327 mode. */
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;
339 else
340 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
342 /* Work out what platform to use, or use the default platform if
343 not possible. */
344 if (clientname == NULL) {
345 VG_(debugLog)(1, "launcher",
346 "no client specified, defaulting platform to '%s'\n",
347 default_platform);
348 platform = default_platform;
349 } else if ((platform = select_platform(clientname)) != NULL) {
350 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
351 } else {
352 VG_(debugLog)(1, "launcher",
353 "no platform detected, defaulting platform to '%s'\n",
354 default_platform);
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. */
361 unsigned bufsiz = 0;
362 char *buf = NULL;
364 while (42) {
365 bufsiz += 500;
366 buf = realloc(buf, bufsiz);
367 if (buf == NULL)
368 barf("realloc of buf failed.");
369 r = readlink("/proc/self/exe", buf, bufsiz);
370 if (r == -1) {
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,
374 fail the execve. */
375 fprintf(stderr, "valgrind: warning (non-fatal): "
376 "readlink(\"/proc/self/exe\") failed.\n");
377 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
378 "will not work.\n");
379 launcher_name = "";
380 break;
382 if (r == bufsiz) continue; // buffer to small; retry
384 assert(r < bufsiz); // paranoia
386 buf[r] = '\0';
387 launcher_name = buf;
388 break;
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*));
403 if (new_env == NULL)
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;
408 new_env[i++] = NULL;
409 assert(i == j+2);
411 /* Establish the correct VALGRIND_LIB. */
412 cp = getenv(VALGRIND_LIB);
414 if (cp != NULL)
415 valgrind_lib = cp;
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));
430 exit(1);