-> 3.19.0 final.
[valgrind.git] / coregrind / launcher-freebsd.c
blob7d40ad6cce5eb0b32a951b3ddf9e905a62cfcc62
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-2009 Julian Seward
11 jseward@acm.org
12 Copyright (C) 2018-2021 Paul Floyd
13 pjfloyd@wanadoo.fr
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of the
18 License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, see <http://www.gnu.org/licenses/>.
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 <assert.h>
36 #include <ctype.h>
37 #include <elf.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/mman.h>
45 #include <sys/sysctl.h>
46 /* #include <sys/user.h> */
47 #include <unistd.h>
49 #include "pub_core_debuglog.h"
50 #include "pub_core_vki.h" // Avoids warnings from
51 // pub_core_libcfile.h
52 #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER
53 #include "pub_core_ume.h"
56 #if !defined(PATH_MAX)
57 #define PATH_MAX 4096 /* POSIX refers to this a lot but I dunno
58 where it is defined */
59 #endif
61 #ifndef EM_X86_64
62 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
63 #endif
65 /* Report fatal errors */
66 __attribute__((noreturn))
67 static void barf ( const char *format, ... )
69 va_list vargs;
71 va_start(vargs, format);
72 fprintf(stderr, "valgrind: Cannot continue: ");
73 vfprintf(stderr, format, vargs);
74 fprintf(stderr, "\n");
75 va_end(vargs);
77 exit(1);
78 /*NOTREACHED*/
79 assert(0);
82 /* Search the path for the client program */
83 static const char *find_client(const char *clientname)
85 static char fullname[PATH_MAX];
86 const char *path = getenv("PATH");
87 const char *colon;
89 while (path) {
90 if ((colon = strchr(path, ':')) == NULL) {
91 strcpy(fullname, path);
92 path = NULL;
93 } else {
94 memcpy(fullname, path, colon - path);
95 fullname[colon - path] = '\0';
96 path = colon + 1;
99 strcat(fullname, "/");
100 strcat(fullname, clientname);
102 if (access(fullname, R_OK|X_OK) == 0)
103 return fullname;
106 return clientname;
109 /* Examine the client and work out which platform it is for */
110 static const char *select_platform(const char *clientname)
112 int fd;
113 char header[4096];
114 ssize_t n_bytes;
115 const char *platform = NULL;
117 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
119 if (strchr(clientname, '/') == NULL)
120 clientname = find_client(clientname);
122 if ((fd = open(clientname, O_RDONLY)) < 0)
123 return NULL;
124 // barf("open(%s): %s", clientname, strerror(errno));
126 n_bytes = read(fd, header, sizeof(header));
127 close(fd);
128 if (n_bytes < 2) {
129 return NULL;
132 if (header[0] == '#' && header[1] == '!') {
133 int i = 2;
134 char *interp = (char *)header + 2;
136 // Skip whitespace.
137 while (1) {
138 if (i == n_bytes) return NULL;
139 if (' ' != header[i] && '\t' == header[i]) break;
140 i++;
143 // Get the interpreter name.
144 interp = &header[i];
145 while (1) {
146 if (i == n_bytes) break;
147 if (isspace(header[i])) break;
148 i++;
150 if (i == n_bytes) return NULL;
151 header[i] = '\0';
153 platform = select_platform(interp);
155 } else if (n_bytes >= SELFMAG && memcmp(header, ELFMAG, SELFMAG) == 0) {
157 if ((size_t)n_bytes >= sizeof(Elf32_Ehdr) && header[EI_CLASS] == ELFCLASS32) {
158 const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
160 if (header[EI_DATA] == ELFDATA2LSB) {
161 if (ehdr->e_machine == EM_386 &&
162 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
163 platform = "x86-freebsd";
166 } else if ((size_t)n_bytes >= sizeof(Elf64_Ehdr) && header[EI_CLASS] == ELFCLASS64) {
167 const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;
169 if (header[EI_DATA] == ELFDATA2LSB) {
170 if (ehdr->e_machine == EM_X86_64 &&
171 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
172 platform = "amd64-freebsd";
178 VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
179 platform ? platform : "unknown");
181 return platform;
184 /* Where we expect to find all our aux files */
185 static const char *valgrind_lib = VG_LIBDIR;
187 int main(int argc, char** argv, char** envp)
189 int i, j, loglevel, r;
190 const char *toolname = NULL;
191 const char *clientname = NULL;
192 const char *platform;
193 const char *default_platform;
194 const char *cp;
195 char *toolfile;
196 char launcher_name[PATH_MAX+1];
197 char* new_line;
198 char** new_env;
199 int oid[4];
200 vki_size_t len;
202 /* Start the debugging-log system ASAP. First find out how many
203 "-d"s were specified. This is a pre-scan of the command line.
204 At the same time, look for the tool name. */
205 loglevel = 0;
206 for (i = 1; i < argc; i++) {
207 if (argv[i][0] != '-') {
208 clientname = argv[i];
209 break;
211 if (0 == strcmp(argv[i], "--")) {
212 if (i+1 < argc)
213 clientname = argv[i+1];
214 break;
216 if (0 == strcmp(argv[i], "-d"))
217 loglevel++;
218 if (0 == strncmp(argv[i], "--tool=", 7))
219 toolname = argv[i] + 7;
222 /* ... and start the debug logger. Now we can safely emit logging
223 messages all through startup. */
224 VG_(debugLog_startup)(loglevel, "Stage 1");
226 /* Make sure we know which tool we're using */
227 if (toolname) {
228 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
229 } else {
230 VG_(debugLog)(1, "launcher",
231 "no tool requested, defaulting to 'memcheck'\n");
232 toolname = "memcheck";
235 /* Select a platform to use if we can't decide that by looking at
236 the executable (eg because it's a shell script). Note that the
237 default_platform is not necessarily either the primary or
238 secondary build target. Instead it's chosen to maximise the
239 chances that /bin/sh will work on it. Hence for a primary
240 target of ppc64-linux we still choose ppc32-linux as the default
241 target, because on most ppc64-linux setups, the basic /bin,
242 /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit
243 mode. */
244 if (0==strcmp(VG_PLATFORM,"x86-freebsd"))
245 default_platform = "x86-freebsd";
246 else if (0==strcmp(VG_PLATFORM,"amd64-freebsd"))
247 default_platform = "amd64-freebsd";
248 else
249 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
251 /* Work out what platform to use, or use the default platform if
252 not possible. */
253 if (clientname == NULL) {
254 VG_(debugLog)(1, "launcher",
255 "no client specified, defaulting platform to '%s'\n",
256 default_platform);
257 platform = default_platform;
258 } else if ((platform = select_platform(clientname)) != NULL) {
259 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
260 } else {
261 VG_(debugLog)(1, "launcher",
262 "no platform detected, defaulting platform to '%s'\n",
263 default_platform);
264 platform = default_platform;
267 /* Figure out the name of this executable (viz, the launcher), so
268 we can tell stage2. stage2 will use the name for recursive
269 invocations of valgrind on child processes. */
270 memset(launcher_name, 0, PATH_MAX+1);
272 oid[0] = CTL_KERN;
273 oid[1] = KERN_PROC;
274 oid[2] = KERN_PROC_PATHNAME;
275 oid[3] = getpid();
276 len = PATH_MAX;
277 r = sysctl(oid, 4, launcher_name, &len, 0, 0);
278 if (r != 0) {
279 fprintf(stderr, "valgrind: warning (non-fatal): "
280 "sysctl(\"kern.proc.pathname\") failed.\n");
281 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
282 "will not work.\n");
285 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
286 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
287 + strlen(launcher_name) + 1);
288 if (new_line == NULL)
289 barf("malloc of new_line failed.");
290 strcpy(new_line, VALGRIND_LAUNCHER);
291 strcat(new_line, "=");
292 strcat(new_line, launcher_name);
294 for (j = 0; envp[j]; j++)
296 new_env = malloc((j+2) * sizeof(char*));
297 if (new_env == NULL)
298 barf("malloc of new_env failed.");
299 for (i = 0; i < j; i++)
300 new_env[i] = envp[i];
301 new_env[i++] = new_line;
302 new_env[i++] = NULL;
303 assert(i == j+2);
305 /* Establish the correct VALGRIND_LIB. */
306 cp = getenv(VALGRIND_LIB);
308 if (cp != NULL)
309 valgrind_lib = cp;
311 /* Build the stage2 invocation, and execve it. Bye! */
312 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
313 if (toolfile == NULL)
314 barf("malloc of toolfile failed.");
315 sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);
317 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
319 execve(toolfile, argv, new_env);
321 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
322 toolname, platform, strerror(errno));
324 exit(1);