blob: e7c9c7f7e0cdc54d0dfe5524fb445d0fe2562dfc [file] [log] [blame]
sewardj45f4e7c2005-09-27 19:20:21 +00001
2/*--------------------------------------------------------------------*/
3/*--- Launching valgrind m_launcher.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj9eecbbb2010-05-03 21:37:12 +000010 Copyright (C) 2000-2010 Julian Seward
sewardj45f4e7c2005-09-27 19:20:21 +000011 jseward@acm.org
12
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.
17
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.
22
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.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
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. */
34
tom5c46da82010-04-29 09:01:21 +000035/* 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. */
38
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"
44
tomfb7bcde2005-11-07 15:24:38 +000045#include <assert.h>
46#include <ctype.h>
47#include <elf.h>
sewardj45f4e7c2005-09-27 19:20:21 +000048#include <errno.h>
tomfb7bcde2005-11-07 15:24:38 +000049#include <fcntl.h>
50#include <stdarg.h>
sewardj45f4e7c2005-09-27 19:20:21 +000051#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
tomfb7bcde2005-11-07 15:24:38 +000054#include <sys/mman.h>
55#include <sys/user.h>
sewardj45f4e7c2005-09-27 19:20:21 +000056#include <unistd.h>
sewardj45f4e7c2005-09-27 19:20:21 +000057
sewardj45f4e7c2005-09-27 19:20:21 +000058
59
60#define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno
61 where it is defined */
62
toma879a472005-12-19 12:27:42 +000063#ifndef EM_X86_64
njn81e60b22005-12-19 17:01:14 +000064#define EM_X86_64 62 // elf.h doesn't define this on some older systems
65#endif
toma879a472005-12-19 12:27:42 +000066
tomfb7bcde2005-11-07 15:24:38 +000067/* Report fatal errors */
sewardj8813eef2006-01-17 16:41:34 +000068__attribute__((noreturn))
tomfb7bcde2005-11-07 15:24:38 +000069static void barf ( const char *format, ... )
sewardj45f4e7c2005-09-27 19:20:21 +000070{
tomfb7bcde2005-11-07 15:24:38 +000071 va_list vargs;
72
73 va_start(vargs, format);
74 fprintf(stderr, "valgrind: Cannot continue: ");
75 vfprintf(stderr, format, vargs);
76 fprintf(stderr, "\n");
77 va_end(vargs);
78
sewardj45f4e7c2005-09-27 19:20:21 +000079 exit(1);
sewardj8813eef2006-01-17 16:41:34 +000080 /*NOTREACHED*/
81 assert(0);
sewardj45f4e7c2005-09-27 19:20:21 +000082}
83
tomfb7bcde2005-11-07 15:24:38 +000084/* Search the path for the client program */
85static const char *find_client(const char *clientname)
86{
87 static char fullname[PATH_MAX];
88 const char *path = getenv("PATH");
89 const char *colon;
90
91 while (path)
92 {
93 if ((colon = strchr(path, ':')) == NULL)
94 {
95 strcpy(fullname, path);
96 path = NULL;
97 }
98 else
99 {
100 memcpy(fullname, path, colon - path);
101 fullname[colon - path] = '\0';
102 path = colon + 1;
103 }
104
105 strcat(fullname, "/");
106 strcat(fullname, clientname);
107
108 if (access(fullname, R_OK|X_OK) == 0)
109 return fullname;
110 }
111
112 return clientname;
113}
114
115/* Examine the client and work out which platform it is for */
116static const char *select_platform(const char *clientname)
117{
118 int fd;
njn33e57a72009-06-29 06:57:30 +0000119 uint8_t header[4096];
njn58899e82009-06-30 06:06:14 +0000120 ssize_t n_bytes;
tomfb7bcde2005-11-07 15:24:38 +0000121 const char *platform = NULL;
122
tom1edc55a2009-08-20 07:56:45 +0000123 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
124
tomfb7bcde2005-11-07 15:24:38 +0000125 if (strchr(clientname, '/') == NULL)
126 clientname = find_client(clientname);
127
tom1edc55a2009-08-20 07:56:45 +0000128 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
129
tomfb7bcde2005-11-07 15:24:38 +0000130 if ((fd = open(clientname, O_RDONLY)) < 0)
131 return NULL;
132 // barf("open(%s): %s", clientname, strerror(errno));
133
tom1edc55a2009-08-20 07:56:45 +0000134 VG_(debugLog)(2, "launcher", "opened '%s'\n", clientname);
135
njn58899e82009-06-30 06:06:14 +0000136 n_bytes = read(fd, header, sizeof(header));
tomfb7bcde2005-11-07 15:24:38 +0000137 close(fd);
njn58899e82009-06-30 06:06:14 +0000138 if (n_bytes < 2) {
njn33e57a72009-06-29 06:57:30 +0000139 return NULL;
140 }
tomfb7bcde2005-11-07 15:24:38 +0000141
sewardj6431e302009-08-27 23:22:39 +0000142 VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
143 (long int)n_bytes, clientname);
tom1edc55a2009-08-20 07:56:45 +0000144
tomfb7bcde2005-11-07 15:24:38 +0000145 if (header[0] == '#' && header[1] == '!') {
njn58899e82009-06-30 06:06:14 +0000146 int i = 2;
tomfb7bcde2005-11-07 15:24:38 +0000147 char *interp = (char *)header + 2;
tomfb7bcde2005-11-07 15:24:38 +0000148
njn58899e82009-06-30 06:06:14 +0000149 // Skip whitespace.
150 while (1) {
151 if (i == n_bytes) return NULL;
sewardj220591d2009-08-19 10:32:49 +0000152 if (' ' != header[i] && '\t' != header[i]) break;
njn58899e82009-06-30 06:06:14 +0000153 i++;
154 }
tomfb7bcde2005-11-07 15:24:38 +0000155
njn58899e82009-06-30 06:06:14 +0000156 // Get the interpreter name.
157 interp = &header[i];
158 while (1) {
159 if (i == n_bytes) break;
160 if (isspace(header[i])) break;
161 i++;
162 }
163 if (i == n_bytes) return NULL;
164 header[i] = '\0';
tomfb7bcde2005-11-07 15:24:38 +0000165
166 platform = select_platform(interp);
tomfb7bcde2005-11-07 15:24:38 +0000167
njn58899e82009-06-30 06:06:14 +0000168 } else if (n_bytes >= SELFMAG && memcmp(header, ELFMAG, SELFMAG) == 0) {
169
170 if (n_bytes >= sizeof(Elf32_Ehdr) && header[EI_CLASS] == ELFCLASS32) {
cerion21082042005-12-06 19:07:08 +0000171 const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
tomfb7bcde2005-11-07 15:24:38 +0000172
cerion21082042005-12-06 19:07:08 +0000173 if (header[EI_DATA] == ELFDATA2LSB) {
174 if (ehdr->e_machine == EM_386 &&
tom4ff8f6c2009-08-18 14:12:48 +0000175 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
176 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
cerion21082042005-12-06 19:07:08 +0000177 platform = "x86-linux";
178 }
sewardj59570ff2010-01-01 11:59:33 +0000179 else
180 if (ehdr->e_machine == EM_ARM &&
181 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
182 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
183 platform = "arm-linux";
184 }
cerion21082042005-12-06 19:07:08 +0000185 }
186 else if (header[EI_DATA] == ELFDATA2MSB) {
187 if (ehdr->e_machine == EM_PPC &&
tom4ff8f6c2009-08-18 14:12:48 +0000188 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
189 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
cerion21082042005-12-06 19:07:08 +0000190 platform = "ppc32-linux";
191 }
192 }
sewardj59570ff2010-01-01 11:59:33 +0000193
njn58899e82009-06-30 06:06:14 +0000194 } else if (n_bytes >= sizeof(Elf64_Ehdr) && header[EI_CLASS] == ELFCLASS64) {
cerion21082042005-12-06 19:07:08 +0000195 const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;
196
197 if (header[EI_DATA] == ELFDATA2LSB) {
198 if (ehdr->e_machine == EM_X86_64 &&
tom4ff8f6c2009-08-18 14:12:48 +0000199 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
200 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
cerion21082042005-12-06 19:07:08 +0000201 platform = "amd64-linux";
202 }
203 } else if (header[EI_DATA] == ELFDATA2MSB) {
204 if (ehdr->e_machine == EM_PPC64 &&
tom4ff8f6c2009-08-18 14:12:48 +0000205 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
206 ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
cerion21082042005-12-06 19:07:08 +0000207 platform = "ppc64-linux";
208 }
209 }
tomfb7bcde2005-11-07 15:24:38 +0000210 }
211 }
212
tom1edc55a2009-08-20 07:56:45 +0000213 VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
214 platform ? platform : "unknown");
215
tomfb7bcde2005-11-07 15:24:38 +0000216 return platform;
217}
218
sewardj45f4e7c2005-09-27 19:20:21 +0000219/* Where we expect to find all our aux files */
220static const char *valgrind_lib = VG_LIBDIR;
221
222int main(int argc, char** argv, char** envp)
223{
224 int i, j, loglevel, r;
225 const char *toolname = NULL;
tomfb7bcde2005-11-07 15:24:38 +0000226 const char *clientname = NULL;
227 const char *platform;
sewardj8813eef2006-01-17 16:41:34 +0000228 const char *default_platform;
sewardj45f4e7c2005-09-27 19:20:21 +0000229 const char *cp;
230 char *toolfile;
231 char launcher_name[PATH_MAX+1];
232 char* new_line;
233 char** new_env;
234
235 /* Start the debugging-log system ASAP. First find out how many
236 "-d"s were specified. This is a pre-scan of the command line.
237 At the same time, look for the tool name. */
238 loglevel = 0;
239 for (i = 1; i < argc; i++) {
tomfb7bcde2005-11-07 15:24:38 +0000240 if (argv[i][0] != '-') {
241 clientname = argv[i];
sewardj45f4e7c2005-09-27 19:20:21 +0000242 break;
tomfb7bcde2005-11-07 15:24:38 +0000243 }
244 if (0 == strcmp(argv[i], "--")) {
245 if (i+1 < argc)
246 clientname = argv[i+1];
sewardj45f4e7c2005-09-27 19:20:21 +0000247 break;
tomfb7bcde2005-11-07 15:24:38 +0000248 }
sewardj45f4e7c2005-09-27 19:20:21 +0000249 if (0 == strcmp(argv[i], "-d"))
250 loglevel++;
251 if (0 == strncmp(argv[i], "--tool=", 7))
252 toolname = argv[i] + 7;
253 }
254
255 /* ... and start the debug logger. Now we can safely emit logging
256 messages all through startup. */
257 VG_(debugLog_startup)(loglevel, "Stage 1");
258
259 /* Make sure we know which tool we're using */
260 if (toolname) {
261 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
262 } else {
263 VG_(debugLog)(1, "launcher",
264 "no tool requested, defaulting to 'memcheck'\n");
265 toolname = "memcheck";
266 }
267
sewardj8813eef2006-01-17 16:41:34 +0000268 /* Select a platform to use if we can't decide that by looking at
269 the executable (eg because it's a shell script). Note that the
270 default_platform is not necessarily either the primary or
271 secondary build target. Instead it's chosen to maximise the
272 chances that /bin/sh will work on it. Hence for a primary
273 target of ppc64-linux we still choose ppc32-linux as the default
274 target, because on most ppc64-linux setups, the basic /bin,
275 /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit
276 mode. */
njnf6ded8c2009-02-06 04:49:14 +0000277 if ((0==strcmp(VG_PLATFORM,"x86-linux")) ||
278 (0==strcmp(VG_PLATFORM,"amd64-linux")) ||
279 (0==strcmp(VG_PLATFORM,"ppc32-linux")) ||
sewardj59570ff2010-01-01 11:59:33 +0000280 (0==strcmp(VG_PLATFORM,"ppc64-linux")) ||
281 (0==strcmp(VG_PLATFORM,"arm-linux")))
njn4f350d12009-02-06 05:34:19 +0000282 default_platform = VG_PLATFORM;
sewardj8813eef2006-01-17 16:41:34 +0000283 else
284 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
285
286 /* Work out what platform to use, or use the default platform if
287 not possible. */
tomfb7bcde2005-11-07 15:24:38 +0000288 if (clientname == NULL) {
sewardj8813eef2006-01-17 16:41:34 +0000289 VG_(debugLog)(1, "launcher",
290 "no client specified, defaulting platform to '%s'\n",
291 default_platform);
292 platform = default_platform;
tomfb7bcde2005-11-07 15:24:38 +0000293 } else if ((platform = select_platform(clientname)) != NULL) {
294 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
295 } else {
sewardj8813eef2006-01-17 16:41:34 +0000296 VG_(debugLog)(1, "launcher",
297 "no platform detected, defaulting platform to '%s'\n",
298 default_platform);
299 platform = default_platform;
tomfb7bcde2005-11-07 15:24:38 +0000300 }
301
sewardj45f4e7c2005-09-27 19:20:21 +0000302 /* Figure out the name of this executable (viz, the launcher), so
303 we can tell stage2. stage2 will use the name for recursive
njn58899e82009-06-30 06:06:14 +0000304 invocations of valgrind on child processes. */
sewardj45f4e7c2005-09-27 19:20:21 +0000305 memset(launcher_name, 0, PATH_MAX+1);
306 r = readlink("/proc/self/exe", launcher_name, PATH_MAX);
sewardj98e68a42005-10-04 23:07:33 +0000307 if (r == -1) {
308 /* If /proc/self/exe can't be followed, don't give up. Instead
309 continue with an empty string for VALGRIND_LAUNCHER. In the
310 sys_execve wrapper, this is tested, and if found to be empty,
311 fail the execve. */
312 fprintf(stderr, "valgrind: warning (non-fatal): "
313 "readlink(\"/proc/self/exe\") failed.\n");
314 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
315 "will not work.\n");
316 }
sewardj45f4e7c2005-09-27 19:20:21 +0000317
318 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
319 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
320 + strlen(launcher_name) + 1);
321 if (new_line == NULL)
322 barf("malloc of new_line failed.");
323 strcpy(new_line, VALGRIND_LAUNCHER);
324 strcat(new_line, "=");
325 strcat(new_line, launcher_name);
326
327 for (j = 0; envp[j]; j++)
328 ;
329 new_env = malloc((j+2) * sizeof(char*));
330 if (new_env == NULL)
331 barf("malloc of new_env failed.");
332 for (i = 0; i < j; i++)
333 new_env[i] = envp[i];
334 new_env[i++] = new_line;
335 new_env[i++] = NULL;
336 assert(i == j+2);
337
338 /* Establish the correct VALGRIND_LIB. */
339 cp = getenv(VALGRIND_LIB);
340
341 if (cp != NULL)
342 valgrind_lib = cp;
343
njn58899e82009-06-30 06:06:14 +0000344 /* Build the stage2 invocation, and execve it. Bye! */
tomfb7bcde2005-11-07 15:24:38 +0000345 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
sewardj45f4e7c2005-09-27 19:20:21 +0000346 if (toolfile == NULL)
347 barf("malloc of toolfile failed.");
njn6bf365c2009-02-11 00:35:45 +0000348 sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);
sewardj45f4e7c2005-09-27 19:20:21 +0000349
350 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
351
352 execve(toolfile, argv, new_env);
353
tomfb7bcde2005-11-07 15:24:38 +0000354 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
355 toolname, platform, strerror(errno));
sewardj45f4e7c2005-09-27 19:20:21 +0000356
357 exit(1);
358}