1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--------------------------------------------------------------------*/
3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--- Launching valgrind                         launcher-darwin.c ---*/
4ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*--------------------------------------------------------------------*/
5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/*
7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   This file is part of Valgrind, a dynamic binary instrumentation
8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   framework.
9ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
10663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   Copyright (C) 2000-2012 Julian Seward
11ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      jseward@acm.org
12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   This program is free software; you can redistribute it and/or
14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   modify it under the terms of the GNU General Public License as
15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   published by the Free Software Foundation; either version 2 of the
16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   License, or (at your option) any later version.
17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   This program is distributed in the hope that it will be useful, but
19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   WITHOUT ANY WARRANTY; without even the implied warranty of
20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   General Public License for more details.
22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   You should have received a copy of the GNU General Public License
24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   along with this program; if not, write to the Free Software
25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   02111-1307, USA.
27ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
28ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   The GNU General Public License is contained in the file COPYING.
29ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown*/
30ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
31ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Note: this is a "normal" program and not part of Valgrind proper,
32ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   and so it doesn't have to conform to Valgrind's arcane rules on
33ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   no-glibc-usage etc. */
34ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
35ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <assert.h>
36ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <ctype.h>
37ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <errno.h>
38ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <fcntl.h>
39ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <libgen.h>
40ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <stdarg.h>
41ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <stdio.h>
42ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <stdlib.h>
43ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <string.h>
44ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <sys/mman.h>
45ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <sys/param.h>
46ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <sys/stat.h>
47ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <sys/user.h>
48ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <unistd.h>
49ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <mach-o/fat.h>
50ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include <mach-o/loader.h>
51ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
52ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_debuglog.h"
53ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_vki.h"       // Avoids warnings from pub_core_libcfile.h
54ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_libcproc.h"  // For VALGRIND_LIB, VALGRIND_LAUNCHER
55ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "pub_core_ume.h"
56ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
57ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic struct {
58ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   cpu_type_t cputype;
59ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *apple_name;     // e.g. x86_64
60ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *valgrind_name;  // e.g. amd64
61ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown} valid_archs[] = {
62ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   { CPU_TYPE_X86,       "i386",   "x86" },
63ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   { CPU_TYPE_X86_64,    "x86_64", "amd64" },
64ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   { CPU_TYPE_ARM,       "arm",    "arm" },
65ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   { CPU_TYPE_POWERPC,   "ppc",    "ppc32" },
66ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   { CPU_TYPE_POWERPC64, "ppc64",  "ppc64" },
67ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown};
68ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic int valid_archs_count = sizeof(valid_archs)/sizeof(valid_archs[0]);
69ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
70ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic const char *name_for_cputype(cpu_type_t cputype)
71ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
72ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   int i;
73ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; i < valid_archs_count; i++) {
74ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (valid_archs[i].cputype == cputype) {
75ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         return valid_archs[i].valgrind_name;
76ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
77ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
78ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return NULL;
79ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
80ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
81ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Report fatal errors */
82ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown__attribute__((noreturn))
83ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic void barf ( const char *format, ... )
84ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
85ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   va_list vargs;
86ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
87ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   va_start(vargs, format);
88ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   fprintf(stderr, "valgrind: ");
89ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   vfprintf(stderr, format, vargs);
90ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   fprintf(stderr, "\n");
91ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   va_end(vargs);
92ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
93ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   exit(1);
94ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /*NOTREACHED*/
95ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   assert(0);
96ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
97ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
98ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Search the path for the client program */
99ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic const char *find_client(const char *clientname)
100ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
101ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   static char fullname[PATH_MAX];
102ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *path = getenv("PATH");
103ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *colon;
104ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
105ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   while (path)
106ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
107ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if ((colon = strchr(path, ':')) == NULL)
108ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      {
109ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         strcpy(fullname, path);
110ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         path = NULL;
111ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
112ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      else
113ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      {
114ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         memcpy(fullname, path, colon - path);
115ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         fullname[colon - path] = '\0';
116ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         path = colon + 1;
117ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
118ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
119ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      strcat(fullname, "/");
120ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      strcat(fullname, clientname);
121ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
122ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (access(fullname, R_OK|X_OK) == 0)
123ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         return fullname;
124ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
125ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
126ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return clientname;
127ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
128ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
129ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic int fat_has_cputype(struct fat_header *fh, cpu_type_t cputype)
130ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
131ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   struct fat_arch *fa = (struct fat_arch *)(fh+1);
132ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   uint32_t nfat_arch = ntohl(fh->nfat_arch);
133ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   uint32_t i;
134ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; i < nfat_arch; i++) {
135ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (ntohl(fa[i].cputype) == cputype) return 1;
136ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
137ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return 0;
138ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
139ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
140ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Examine the client and work out which arch it is for */
141ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic const char *select_arch(
142ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      const char *clientname, cpu_type_t default_cputype,
143ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      const char *default_arch)
144ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
145ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   uint8_t buf[4096];
146ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   ssize_t bytes;
147ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   int fd = open(find_client(clientname), O_RDONLY);
148ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (fd < 0) {
149ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      barf("%s: %s", clientname, strerror(errno));
150ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
151ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
152ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   bytes = read(fd, buf, sizeof(buf));
153ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   close(fd);
154ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (bytes != sizeof(buf)) {
155ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      return NULL;
156ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
157ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
158ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // If it's thin, return that arch.
159ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
160ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      struct mach_header *mh = (struct mach_header *)buf;
161ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (mh->magic == MH_MAGIC  ||  mh->magic == MH_MAGIC_64) {
162ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         return name_for_cputype(mh->cputype);
163ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      } else if (mh->magic == MH_CIGAM  ||  mh->magic == MH_CIGAM_64) {
164ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         return name_for_cputype(OSSwapInt32(mh->cputype));
165ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
166ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
167ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
168ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // If it's fat, look for a good arch.
169ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {
170ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      struct fat_header *fh = (struct fat_header *)buf;
171ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (ntohl(fh->magic) == FAT_MAGIC) {
172ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         uint32_t nfat_arch = ntohl(fh->nfat_arch);
173ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         int i;
174ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         // If only one fat arch, use it.
175ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         if (nfat_arch == 1) {
176ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            struct fat_arch *fa = (struct fat_arch *)(fh+1);
177ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            return name_for_cputype(ntohl(fa->cputype));
178ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         }
179ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         // Scan fat headers for default arch.
180ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         if (fat_has_cputype(fh, default_cputype)) {
181ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            return default_arch;
182ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         }
183ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
184ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         // Scan fat headers for any supported arch.
185ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         for (i = 0; i < valid_archs_count; i++) {
186ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            if (fat_has_cputype(fh, valid_archs[i].cputype)) {
187ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown               return valid_archs[i].valgrind_name;
188ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            }
189ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         }
190ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
191ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
192ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
193ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   return NULL;
194ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
195ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
196ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
197ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown/* Where we expect to find all our aux files */
198ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownstatic const char *valgrind_lib;
199ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
200ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownint main(int argc, char** argv, char** envp)
201ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown{
202ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   int i, j, loglevel;
203ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *toolname = NULL;
204ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *clientname = NULL;
205ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   int clientname_arg = 0;
206ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *archname = NULL;
207ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *arch;
208ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   const char *default_arch;
209ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   cpu_type_t default_cputype;
210ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char *toolfile;
211ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char launcher_name[PATH_MAX+1];
212ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char* new_line;
213ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char* set_cwd;
214ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char* cwd;
215ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char** new_env;
216ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   char **new_argv;
217ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   int new_argc;
218ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
219ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Start the debugging-log system ASAP.  First find out how many
220ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      "-d"s were specified.  This is a pre-scan of the command line.
221ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      At the same time, look for the tool name. */
222ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   loglevel = 0;
223ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 1; i < argc; i++) {
224ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (argv[i][0] != '-') {
225ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         clientname = argv[i];
226ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         clientname_arg = i;
227ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         break;
228ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
229ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strcmp(argv[i], "--")) {
230ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         if (i+1 < argc) {
231ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            clientname = argv[i+1];
232ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            clientname_arg = i;
233ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         }
234ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         break;
235ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
236ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strcmp(argv[i], "-d"))
237ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         loglevel++;
238ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strncmp(argv[i], "--tool=", 7))
239ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         toolname = argv[i] + 7;
240ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strncmp(argv[i], "--arch=", 7))
241ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         archname = argv[i] + 7;
242ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
243ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
244ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* ... and start the debug logger.  Now we can safely emit logging
245ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      messages all through startup. */
246ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(debugLog_startup)(loglevel, "Stage 1");
247ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
248ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Make sure we know which tool we're using */
249ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (toolname) {
250ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
251ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   } else {
252ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher",
253ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                       "no tool requested, defaulting to 'memcheck'\n");
254ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      toolname = "memcheck";
255ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
256ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
257ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Find the real executable if clientname is an app bundle. */
258ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (clientname) {
259ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      struct stat st;
260ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == stat(clientname, &st)  &&  (st.st_mode & S_IFDIR)) {
261ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         char *copy = strdup(clientname);
262ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         char *appname = basename(copy);
263ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         char *dot = strrchr(appname, '.');
264ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         if (dot) {
265ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            char *newclient;
266ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            *dot = '\0';
267ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            asprintf(&newclient, "%s/Contents/MacOS/%s", clientname, appname);
268ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            VG_(debugLog)(1, "launcher", "Using executable in app bundle: %s\n", newclient);
269ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            clientname = newclient;
270ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            argv[clientname_arg] = newclient;
271ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         }
272ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         free(copy);
273ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
274ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
275ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
276ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Establish the correct VALGRIND_LIB. */
277ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   {  const char *cp;
278ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      cp = getenv(VALGRIND_LIB);
279ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      valgrind_lib = ( cp == NULL ? VG_LIBDIR : cp );
280ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher", "valgrind_lib = %s\n", valgrind_lib);
281ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
282ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
283ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Find installed architectures. Use vgpreload_core-<platform>.so as the
284ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown    * indicator of whether the platform is installed. */
285ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; i < valid_archs_count; i++) {
286ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      char *vgpreload_core;
287ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      asprintf(&vgpreload_core, "%s/vgpreload_core-%s-darwin.so", valgrind_lib, valid_archs[i].valgrind_name);
288ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (access(vgpreload_core, R_OK|X_OK) != 0) {
289ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         VG_(debugLog)(1, "launcher", "arch '%s' IS NOT installed\n", valid_archs[i].valgrind_name);
290ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         bzero(&valid_archs[i], sizeof(valid_archs[i]));
291ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      } else {
292ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         VG_(debugLog)(1, "launcher", "arch '%s' IS installed\n", valid_archs[i].valgrind_name);
293ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
294ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      free(vgpreload_core);
295ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
296ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
297ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Find the "default" arch (VGCONF_ARCH_PRI from configure).
298ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      This is the preferred arch from fat files and the fallback. */
299ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   default_arch = NULL;
300ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   default_cputype = 0;
301ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; i < valid_archs_count; i++) {
302ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (!valid_archs[i].cputype) continue;
303ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strncmp(VG_PLATFORM, valid_archs[i].valgrind_name,
304ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                       strlen(valid_archs[i].valgrind_name)))
305ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      {
306ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         default_arch = valid_archs[i].valgrind_name;
307ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         default_cputype = valid_archs[i].cputype;
308ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         break;
309ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
310ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
311ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (i == valid_archs_count) barf("Unknown/uninstalled VG_PLATFORM '%s'", VG_PLATFORM);
312ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   assert(NULL != default_arch);
313ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   assert(0 != default_cputype);
314ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
315ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Work out what arch to use, or use the default arch if not possible. */
316ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (archname != NULL) {
317ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      // --arch from command line
318ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      arch = NULL;
319ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      for (i = 0; i < valid_archs_count; i++) {
320ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         if (0 == strcmp(archname, valid_archs[i].apple_name)  ||
321ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown             0 == strcmp(archname, valid_archs[i].valgrind_name))
322ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         {
323ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            arch = valid_archs[i].valgrind_name;
324ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown            break;
325ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         }
326ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
327ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (i == valid_archs_count) barf("Unknown --arch '%s'", archname);
328ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      assert(NULL != arch);
329ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher", "using arch '%s' from --arch=%s\n",
330ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                    arch, archname);
331ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
332ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   else if (clientname == NULL) {
333ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      // no client executable; use default as fallback
334ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher",
335ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                       "no client specified, defaulting arch to '%s'\n",
336ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                        default_arch);
337ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      arch = default_arch;
338ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
339ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   else if ((arch = select_arch(clientname, default_cputype,default_arch))) {
340ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      // arch from client executable
341ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher", "selected arch '%s'\n", arch);
342ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
343ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   else {
344ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      // nothing found in client executable; use default as fallback
345ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      VG_(debugLog)(1, "launcher",
346ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                       "no arch detected, defaulting arch to '%s'\n",
347ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                       default_arch);
348ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      arch = default_arch;
349ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
350ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
351ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   cwd = getcwd(NULL, 0);
352ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (!cwd) barf("Current directory no longer exists.");
353ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
354ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Figure out the name of this executable (viz, the launcher), so
355ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      we can tell stage2.  stage2 will use the name for recursive
356ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      invokations of valgrind on child processes. */
357ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   memset(launcher_name, 0, PATH_MAX+1);
358ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; envp[i]; i++)
359ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown       ; /* executable path is after last envp item */
360ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* envp[i] == NULL ; envp[i+1] == executable_path */
361ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (envp[i+1][0] != '/') {
362ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      strcpy(launcher_name, cwd);
363ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      strcat(launcher_name, "/");
364ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
365ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (strlen(launcher_name) + strlen(envp[i+1]) > PATH_MAX)
366ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      barf("launcher path is too long");
367ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   strcat(launcher_name, envp[i+1]);
368ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(debugLog)(1, "launcher", "launcher_name = %s\n", launcher_name);
369ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
370ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
371ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   asprintf(&new_line, VALGRIND_LAUNCHER "=%s", launcher_name);
372ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
373ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* tediously augment the env: VALGRIND_STARTUP_PWD_%PID_XYZZY=current_working_dir */
374ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   asprintf(&set_cwd, "VALGRIND_STARTUP_PWD_%u_XYZZY=%s", getppid(), cwd);
375ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
376ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // Note that Apple binaries get a secret fourth arg, "char* apple", which
377ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   // contains the executable path.  Don't forget about it.
378ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (j = 0; envp[j]; j++)
379ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      ;
380ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_env = malloc((j+4) * sizeof(char*));
381ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (new_env == NULL)
382ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      barf("malloc of new_env failed.");
383ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; i < j; i++)
384ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      new_env[i] = envp[i];
385ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_env[i++] = new_line;
386ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_env[i++] = set_cwd;
387ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_env[i++] = NULL;
388ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_env[i  ] = envp[i-2]; // the 'apple' arg == the executable_path
389ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   assert(i == j+3);
390ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
391ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* tediously edit env: hide dyld options from valgrind's captive dyld */
392ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0; envp[i]; i++) {
393ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strncmp(envp[i], "DYLD_", 5)) {
394ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         envp[i][0] = 'V';  /* VYLD_; changed back by initimg-darwin */
395ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
396ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
397ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
398ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* tediously edit argv: remove --arch= */
399ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_argv = malloc((1+argc) * sizeof(char *));
400ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   for (i = 0, new_argc = 0; i < argc; i++) {
401ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      if (0 == strncmp(argv[i], "--arch=", 7)) {
402ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         // skip
403ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      } else {
404ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown         new_argv[new_argc++] = argv[i];
405ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      }
406ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
407ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   new_argv[new_argc++] = NULL;
408ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
409ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   /* Build the stage2 invokation, and execve it.  Bye! */
410ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   asprintf(&toolfile, "%s/%s-%s-darwin", valgrind_lib, toolname, arch);
411ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   if (access(toolfile, R_OK|X_OK) != 0) {
412ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown      barf("tool '%s' not installed (%s) (%s)", toolname, toolfile, strerror(errno));
413ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   }
414ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
415ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
416ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
417ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   execve(toolfile, new_argv, new_env);
418ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
419ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s-darwin': %s\n",
420ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown                   toolname, arch, strerror(errno));
421ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown
422ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown   exit(1);
423ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown}
424