• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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-2011 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 
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. */
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 
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>
54 
55 
56 
57 #define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno
58                          where it is defined */
59 
60 #ifndef EM_X86_64
61 #define EM_X86_64 62    // elf.h doesn't define this on some older systems
62 #endif
63 #ifndef EM_PPC64
64 #define EM_PPC64 21     // elf.h doesn't define this on Android
65 #endif
66 
67 
68 /* Report fatal errors */
69 __attribute__((noreturn))
barf(const char * format,...)70 static void barf ( const char *format, ... )
71 {
72    va_list vargs;
73 
74    va_start(vargs, format);
75    fprintf(stderr, "valgrind: Cannot continue: ");
76    vfprintf(stderr, format, vargs);
77    fprintf(stderr, "\n");
78    va_end(vargs);
79 
80    exit(1);
81    /*NOTREACHED*/
82    assert(0);
83 }
84 
85 /* Search the path for the client program */
find_client(const char * clientname)86 static const char *find_client(const char *clientname)
87 {
88    static char fullname[PATH_MAX];
89    const char *path = getenv("PATH");
90    const char *colon;
91 
92    while (path)
93    {
94       if ((colon = strchr(path, ':')) == NULL)
95       {
96          strcpy(fullname, path);
97          path = NULL;
98       }
99       else
100       {
101          memcpy(fullname, path, colon - path);
102          fullname[colon - path] = '\0';
103          path = colon + 1;
104       }
105 
106       strcat(fullname, "/");
107       strcat(fullname, clientname);
108 
109       if (access(fullname, R_OK|X_OK) == 0)
110          return fullname;
111    }
112 
113    return clientname;
114 }
115 
116 /* Examine the client and work out which platform it is for */
select_platform(const char * clientname)117 static const char *select_platform(const char *clientname)
118 {
119    int fd;
120    uint8_t header[4096];
121    ssize_t n_bytes;
122    const char *platform = NULL;
123 
124    VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
125 
126    if (strchr(clientname, '/') == NULL)
127       clientname = find_client(clientname);
128 
129    VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
130 
131    if ((fd = open(clientname, O_RDONLY)) < 0)
132       return NULL;
133    //   barf("open(%s): %s", clientname, strerror(errno));
134 
135    VG_(debugLog)(2, "launcher", "opened '%s'\n", clientname);
136 
137    n_bytes = read(fd, header, sizeof(header));
138    close(fd);
139    if (n_bytes < 2) {
140       return NULL;
141    }
142 
143    VG_(debugLog)(2, "launcher", "read %ld bytes from '%s'\n",
144                     (long int)n_bytes, clientname);
145 
146    if (header[0] == '#' && header[1] == '!') {
147       int i = 2;
148       char *interp = (char *)header + 2;
149 
150       // Skip whitespace.
151       while (1) {
152          if (i == n_bytes) return NULL;
153          if (' ' != header[i] && '\t' != header[i]) break;
154          i++;
155       }
156 
157       // Get the interpreter name.
158       interp = &header[i];
159       while (1) {
160          if (i == n_bytes) break;
161          if (isspace(header[i])) break;
162          i++;
163       }
164       if (i == n_bytes) return NULL;
165       header[i] = '\0';
166 
167       platform = select_platform(interp);
168 
169    } else if (n_bytes >= SELFMAG && memcmp(header, ELFMAG, SELFMAG) == 0) {
170 
171       if (n_bytes >= sizeof(Elf32_Ehdr) && header[EI_CLASS] == ELFCLASS32) {
172          const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
173 
174          if (header[EI_DATA] == ELFDATA2LSB) {
175             if (ehdr->e_machine == EM_386 &&
176                 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
177                  ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
178                platform = "x86-linux";
179             }
180             else
181             if (ehdr->e_machine == EM_ARM &&
182                 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
183                  ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
184                platform = "arm-linux";
185             }
186          }
187          else if (header[EI_DATA] == ELFDATA2MSB) {
188             if (ehdr->e_machine == EM_PPC &&
189                 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
190                  ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
191                platform = "ppc32-linux";
192             }
193          }
194 
195       } else if (n_bytes >= sizeof(Elf64_Ehdr) && header[EI_CLASS] == ELFCLASS64) {
196          const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;
197 
198          if (header[EI_DATA] == ELFDATA2LSB) {
199             if (ehdr->e_machine == EM_X86_64 &&
200                 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
201                  ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
202                platform = "amd64-linux";
203             }
204          } else if (header[EI_DATA] == ELFDATA2MSB) {
205 #           if !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android)
206             if (ehdr->e_machine == EM_PPC64 &&
207                 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
208                  ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
209                platform = "ppc64-linux";
210             }
211             else
212             if (ehdr->e_machine == EM_S390 &&
213                 (ehdr->e_ident[EI_OSABI] == ELFOSABI_SYSV ||
214                  ehdr->e_ident[EI_OSABI] == ELFOSABI_LINUX)) {
215                platform = "s390x-linux";
216             }
217 #           endif
218          }
219       }
220    }
221 
222    VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
223                  platform ? platform : "unknown");
224 
225    return platform;
226 }
227 
228 /* Where we expect to find all our aux files */
229 static const char *valgrind_lib = VG_LIBDIR;
230 
main(int argc,char ** argv,char ** envp)231 int main(int argc, char** argv, char** envp)
232 {
233    int i, j, loglevel, r;
234    const char *toolname = NULL;
235    const char *clientname = NULL;
236    const char *platform;
237    const char *default_platform;
238    const char *cp;
239    char *toolfile;
240    char launcher_name[PATH_MAX+1];
241    char* new_line;
242    char** new_env;
243 
244    /* Start the debugging-log system ASAP.  First find out how many
245       "-d"s were specified.  This is a pre-scan of the command line.
246       At the same time, look for the tool name. */
247    loglevel = 0;
248    for (i = 1; i < argc; i++) {
249       if (argv[i][0] != '-') {
250          clientname = argv[i];
251          break;
252       }
253       if (0 == strcmp(argv[i], "--")) {
254          if (i+1 < argc)
255             clientname = argv[i+1];
256          break;
257       }
258       if (0 == strcmp(argv[i], "-d"))
259          loglevel++;
260       if (0 == strncmp(argv[i], "--tool=", 7))
261          toolname = argv[i] + 7;
262    }
263 
264    /* ... and start the debug logger.  Now we can safely emit logging
265       messages all through startup. */
266    VG_(debugLog_startup)(loglevel, "Stage 1");
267 
268    /* Make sure we know which tool we're using */
269    if (toolname) {
270       VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
271    } else {
272       VG_(debugLog)(1, "launcher",
273                        "no tool requested, defaulting to 'memcheck'\n");
274       toolname = "memcheck";
275    }
276 
277    /* Select a platform to use if we can't decide that by looking at
278       the executable (eg because it's a shell script).  Note that the
279       default_platform is not necessarily either the primary or
280       secondary build target.  Instead it's chosen to maximise the
281       chances that /bin/sh will work on it.  Hence for a primary
282       target of ppc64-linux we still choose ppc32-linux as the default
283       target, because on most ppc64-linux setups, the basic /bin,
284       /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit
285       mode. */
286    if ((0==strcmp(VG_PLATFORM,"x86-linux"))   ||
287        (0==strcmp(VG_PLATFORM,"amd64-linux")) ||
288        (0==strcmp(VG_PLATFORM,"ppc32-linux")) ||
289        (0==strcmp(VG_PLATFORM,"ppc64-linux")) ||
290        (0==strcmp(VG_PLATFORM,"arm-linux"))   ||
291        (0==strcmp(VG_PLATFORM,"s390x-linux")))
292       default_platform = VG_PLATFORM;
293    else
294       barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
295 
296    /* Work out what platform to use, or use the default platform if
297       not possible. */
298    if (clientname == NULL) {
299       VG_(debugLog)(1, "launcher",
300                        "no client specified, defaulting platform to '%s'\n",
301                         default_platform);
302       platform = default_platform;
303    } else if ((platform = select_platform(clientname)) != NULL) {
304       VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
305    } else {
306       VG_(debugLog)(1, "launcher",
307                        "no platform detected, defaulting platform to '%s'\n",
308                        default_platform);
309       platform = default_platform;
310    }
311 
312    /* Figure out the name of this executable (viz, the launcher), so
313       we can tell stage2.  stage2 will use the name for recursive
314       invocations of valgrind on child processes. */
315    memset(launcher_name, 0, PATH_MAX+1);
316    r = readlink("/proc/self/exe", launcher_name, PATH_MAX);
317    if (r == -1) {
318       /* If /proc/self/exe can't be followed, don't give up.  Instead
319          continue with an empty string for VALGRIND_LAUNCHER.  In the
320          sys_execve wrapper, this is tested, and if found to be empty,
321          fail the execve. */
322       fprintf(stderr, "valgrind: warning (non-fatal): "
323                       "readlink(\"/proc/self/exe\") failed.\n");
324       fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
325                       "will not work.\n");
326    }
327 
328    /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
329    new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
330                      + strlen(launcher_name) + 1);
331    if (new_line == NULL)
332       barf("malloc of new_line failed.");
333    strcpy(new_line, VALGRIND_LAUNCHER);
334    strcat(new_line, "=");
335    strcat(new_line, launcher_name);
336 
337    for (j = 0; envp[j]; j++)
338       ;
339    new_env = malloc((j+2) * sizeof(char*));
340    if (new_env == NULL)
341       barf("malloc of new_env failed.");
342    for (i = 0; i < j; i++)
343       new_env[i] = envp[i];
344    new_env[i++] = new_line;
345    new_env[i++] = NULL;
346    assert(i == j+2);
347 
348    /* Establish the correct VALGRIND_LIB. */
349    cp = getenv(VALGRIND_LIB);
350 
351    if (cp != NULL)
352       valgrind_lib = cp;
353 
354    /* Build the stage2 invocation, and execve it.  Bye! */
355    toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
356    if (toolfile == NULL)
357       barf("malloc of toolfile failed.");
358    sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);
359 
360    VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
361 
362    execve(toolfile, argv, new_env);
363 
364    fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
365                    toolname, platform, strerror(errno));
366 
367    exit(1);
368 }
369