blob: 3dedb06079d3915c1867e0d072994a7913b5824d [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
10 Copyright (C) 2000-2005 Julian Seward
11 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
tomfb7bcde2005-11-07 15:24:38 +000035#include <assert.h>
36#include <ctype.h>
37#include <elf.h>
sewardj45f4e7c2005-09-27 19:20:21 +000038#include <errno.h>
tomfb7bcde2005-11-07 15:24:38 +000039#include <fcntl.h>
40#include <stdarg.h>
sewardj45f4e7c2005-09-27 19:20:21 +000041#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
tomfb7bcde2005-11-07 15:24:38 +000044#include <sys/mman.h>
45#include <sys/user.h>
sewardj45f4e7c2005-09-27 19:20:21 +000046#include <unistd.h>
sewardj45f4e7c2005-09-27 19:20:21 +000047
48#include "pub_core_debuglog.h"
49#include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER
tomfb7bcde2005-11-07 15:24:38 +000050#include "pub_core_ume.h"
sewardj45f4e7c2005-09-27 19:20:21 +000051
52
53
54#define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno
55 where it is defined */
56
tomfb7bcde2005-11-07 15:24:38 +000057/* Report fatal errors */
58static void barf ( const char *format, ... )
sewardj45f4e7c2005-09-27 19:20:21 +000059{
tomfb7bcde2005-11-07 15:24:38 +000060 va_list vargs;
61
62 va_start(vargs, format);
63 fprintf(stderr, "valgrind: Cannot continue: ");
64 vfprintf(stderr, format, vargs);
65 fprintf(stderr, "\n");
66 va_end(vargs);
67
sewardj45f4e7c2005-09-27 19:20:21 +000068 exit(1);
69}
70
tomfb7bcde2005-11-07 15:24:38 +000071/* Search the path for the client program */
72static const char *find_client(const char *clientname)
73{
74 static char fullname[PATH_MAX];
75 const char *path = getenv("PATH");
76 const char *colon;
77
78 while (path)
79 {
80 if ((colon = strchr(path, ':')) == NULL)
81 {
82 strcpy(fullname, path);
83 path = NULL;
84 }
85 else
86 {
87 memcpy(fullname, path, colon - path);
88 fullname[colon - path] = '\0';
89 path = colon + 1;
90 }
91
92 strcat(fullname, "/");
93 strcat(fullname, clientname);
94
95 if (access(fullname, R_OK|X_OK) == 0)
96 return fullname;
97 }
98
99 return clientname;
100}
101
102/* Examine the client and work out which platform it is for */
103static const char *select_platform(const char *clientname)
104{
105 int fd;
106 unsigned char *header;
107 const char *platform = NULL;
tomd822c6c2005-11-07 16:50:55 +0000108 long pagesize = sysconf(_SC_PAGESIZE);
tomfb7bcde2005-11-07 15:24:38 +0000109
110 if (strchr(clientname, '/') == NULL)
111 clientname = find_client(clientname);
112
113 if ((fd = open(clientname, O_RDONLY)) < 0)
114 return NULL;
115 // barf("open(%s): %s", clientname, strerror(errno));
116
tom0f7573d2005-11-07 16:46:55 +0000117 if ((header = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
tomfb7bcde2005-11-07 15:24:38 +0000118 return NULL;
119 // barf("mmap(%s): %s", clientname, strerror(errno));
120
121 close(fd);
122
123 if (header[0] == '#' && header[1] == '!') {
124 char *interp = (char *)header + 2;
125 char *interpend;
126
127 while (*interp == ' ' || *interp == '\t')
128 interp++;
129
130 for (interpend = interp; !isspace(*interpend); interpend++)
131 ;
132
133 *interpend = '\0';
134
135 platform = select_platform(interp);
136 } else if (memcmp(header, ELFMAG, SELFMAG) == 0 &&
137 header[EI_CLASS] == ELFCLASS32 &&
138 header[EI_DATA] == ELFDATA2LSB) {
139 const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
140
141 if (ehdr->e_machine == EM_386 &&
142 ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) {
143 platform = "x86-linux";
144 } else if (ehdr->e_machine == EM_PPC &&
145 ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) {
146 platform = "ppc32-linux";
147 }
148 } else if (memcmp(header, ELFMAG, SELFMAG) == 0 &&
149 header[EI_CLASS] == ELFCLASS64 &&
150 header[EI_DATA] == ELFDATA2LSB) {
151 const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;
152
153 if (ehdr->e_machine == EM_X86_64 &&
154 ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV) {
155 platform = "amd64-linux";
156 }
157 }
158
tom0f7573d2005-11-07 16:46:55 +0000159 munmap(header, pagesize);
tomfb7bcde2005-11-07 15:24:38 +0000160
161 return platform;
162}
163
sewardj45f4e7c2005-09-27 19:20:21 +0000164/* Where we expect to find all our aux files */
165static const char *valgrind_lib = VG_LIBDIR;
166
167int main(int argc, char** argv, char** envp)
168{
169 int i, j, loglevel, r;
170 const char *toolname = NULL;
tomfb7bcde2005-11-07 15:24:38 +0000171 const char *clientname = NULL;
172 const char *platform;
sewardj45f4e7c2005-09-27 19:20:21 +0000173 const char *cp;
174 char *toolfile;
175 char launcher_name[PATH_MAX+1];
176 char* new_line;
177 char** new_env;
178
179 /* Start the debugging-log system ASAP. First find out how many
180 "-d"s were specified. This is a pre-scan of the command line.
181 At the same time, look for the tool name. */
182 loglevel = 0;
183 for (i = 1; i < argc; i++) {
tomfb7bcde2005-11-07 15:24:38 +0000184 if (argv[i][0] != '-') {
185 clientname = argv[i];
sewardj45f4e7c2005-09-27 19:20:21 +0000186 break;
tomfb7bcde2005-11-07 15:24:38 +0000187 }
188 if (0 == strcmp(argv[i], "--")) {
189 if (i+1 < argc)
190 clientname = argv[i+1];
sewardj45f4e7c2005-09-27 19:20:21 +0000191 break;
tomfb7bcde2005-11-07 15:24:38 +0000192 }
sewardj45f4e7c2005-09-27 19:20:21 +0000193 if (0 == strcmp(argv[i], "-d"))
194 loglevel++;
195 if (0 == strncmp(argv[i], "--tool=", 7))
196 toolname = argv[i] + 7;
197 }
198
199 /* ... and start the debug logger. Now we can safely emit logging
200 messages all through startup. */
201 VG_(debugLog_startup)(loglevel, "Stage 1");
202
203 /* Make sure we know which tool we're using */
204 if (toolname) {
205 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
206 } else {
207 VG_(debugLog)(1, "launcher",
208 "no tool requested, defaulting to 'memcheck'\n");
209 toolname = "memcheck";
210 }
211
tomfb7bcde2005-11-07 15:24:38 +0000212 /* Work out what platform to use */
213 if (clientname == NULL) {
214 VG_(debugLog)(1, "launcher", "no client specified, defaulting platform to '%s'\n", VG_PLATFORM);
215 platform = VG_PLATFORM;
216 } else if ((platform = select_platform(clientname)) != NULL) {
217 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
218 } else {
219 VG_(debugLog)(1, "launcher", "no platform detected, defaulting platform to '%s'\n", VG_PLATFORM);
220 platform = VG_PLATFORM;
221 }
222
sewardj45f4e7c2005-09-27 19:20:21 +0000223 /* Figure out the name of this executable (viz, the launcher), so
224 we can tell stage2. stage2 will use the name for recursive
225 invokations of valgrind on child processes. */
226 memset(launcher_name, 0, PATH_MAX+1);
227 r = readlink("/proc/self/exe", launcher_name, PATH_MAX);
sewardj98e68a42005-10-04 23:07:33 +0000228 if (r == -1) {
229 /* If /proc/self/exe can't be followed, don't give up. Instead
230 continue with an empty string for VALGRIND_LAUNCHER. In the
231 sys_execve wrapper, this is tested, and if found to be empty,
232 fail the execve. */
233 fprintf(stderr, "valgrind: warning (non-fatal): "
234 "readlink(\"/proc/self/exe\") failed.\n");
235 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
236 "will not work.\n");
237 }
sewardj45f4e7c2005-09-27 19:20:21 +0000238
239 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
240 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
241 + strlen(launcher_name) + 1);
242 if (new_line == NULL)
243 barf("malloc of new_line failed.");
244 strcpy(new_line, VALGRIND_LAUNCHER);
245 strcat(new_line, "=");
246 strcat(new_line, launcher_name);
247
248 for (j = 0; envp[j]; j++)
249 ;
250 new_env = malloc((j+2) * sizeof(char*));
251 if (new_env == NULL)
252 barf("malloc of new_env failed.");
253 for (i = 0; i < j; i++)
254 new_env[i] = envp[i];
255 new_env[i++] = new_line;
256 new_env[i++] = NULL;
257 assert(i == j+2);
258
259 /* Establish the correct VALGRIND_LIB. */
260 cp = getenv(VALGRIND_LIB);
261
262 if (cp != NULL)
263 valgrind_lib = cp;
264
265 /* Build the stage2 invokation, and execve it. Bye! */
tomfb7bcde2005-11-07 15:24:38 +0000266 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
sewardj45f4e7c2005-09-27 19:20:21 +0000267 if (toolfile == NULL)
268 barf("malloc of toolfile failed.");
tomfb7bcde2005-11-07 15:24:38 +0000269 sprintf(toolfile, "%s/%s/%s", valgrind_lib, platform, toolname);
sewardj45f4e7c2005-09-27 19:20:21 +0000270
271 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
272
273 execve(toolfile, argv, new_env);
274
tomfb7bcde2005-11-07 15:24:38 +0000275 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
276 toolname, platform, strerror(errno));
sewardj45f4e7c2005-09-27 19:20:21 +0000277
278 exit(1);
279}