Bug 439685 compiler warning in callgrind/main.c
[valgrind.git] / coregrind / launcher-freebsd.c
blob030b183b572fc1027cf92fca1e91df61c44ac7dc
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>
48 #include <limits.h>
50 #include "pub_core_debuglog.h"
51 #include "pub_core_vki.h" // Avoids warnings from
52 // pub_core_libcfile.h
53 #include "pub_core_libcproc.h" // For VALGRIND_LIB, VALGRIND_LAUNCHER
54 #include "pub_core_ume.h"
56 #ifndef EM_X86_64
57 #define EM_X86_64 62 // elf.h doesn't define this on some older systems
58 #endif
60 /* Report fatal errors */
61 __attribute__((noreturn))
62 static void barf ( const char *format, ... )
64 va_list vargs;
66 va_start(vargs, format);
67 fprintf(stderr, "valgrind: Cannot continue: ");
68 vfprintf(stderr, format, vargs);
69 fprintf(stderr, "\n");
70 va_end(vargs);
72 exit(1);
73 /*NOTREACHED*/
74 assert(0);
77 /* Search the path for the client program */
78 static const char *find_client(const char *clientname)
80 static char fullname[PATH_MAX];
81 const char *path = getenv("PATH");
82 const char *colon;
84 while (path) {
85 if ((colon = strchr(path, ':')) == NULL) {
86 strlcpy(fullname, path, PATH_MAX);
87 path = NULL;
88 } else {
89 memcpy(fullname, path, colon - path);
90 fullname[colon - path] = '\0';
91 path = colon + 1;
94 strlcat(fullname, "/", PATH_MAX);
95 strlcat(fullname, clientname, PATH_MAX);
97 if (access(fullname, R_OK|X_OK) == 0) {
98 return fullname;
102 return clientname;
105 /* Examine the client and work out which platform it is for */
106 static const char *select_platform(const char *clientname)
108 int fd;
109 char header[4096];
110 ssize_t n_bytes;
111 const char *platform = NULL;
113 VG_(debugLog)(2, "launcher", "selecting platform for '%s'\n", clientname);
115 if (strchr(clientname, '/') == NULL) {
116 clientname = find_client(clientname);
119 if ((fd = open(clientname, O_RDONLY)) < 0) {
120 return NULL;
122 // barf("open(%s): %s", clientname, strerror(errno));
124 n_bytes = read(fd, header, sizeof(header));
125 close(fd);
126 if (n_bytes < 2) {
127 return NULL;
130 if (header[0] == '#' && header[1] == '!') {
131 int i = 2;
132 char *interp = (char *)header + 2;
134 // Skip whitespace.
135 while (1) {
136 if (i == n_bytes) {
137 return NULL;
139 if (' ' != header[i] && '\t' != header[i]) {
140 break;
142 i++;
145 // Get the interpreter name.
146 interp = &header[i];
147 while (1) {
148 if (i == n_bytes) {
149 break;
151 if (isspace(header[i])) {
152 break;
154 i++;
156 if (i == n_bytes) {
157 return NULL;
159 header[i] = '\0';
161 platform = select_platform(interp);
163 } else if (n_bytes >= SELFMAG && memcmp(header, ELFMAG, SELFMAG) == 0) {
165 if ((size_t)n_bytes >= sizeof(Elf32_Ehdr) && header[EI_CLASS] == ELFCLASS32) {
166 const Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header;
168 if (header[EI_DATA] == ELFDATA2LSB) {
169 if (ehdr->e_machine == EM_386 &&
170 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
171 platform = "x86-freebsd";
174 } else if ((size_t)n_bytes >= sizeof(Elf64_Ehdr) && header[EI_CLASS] == ELFCLASS64) {
175 const Elf64_Ehdr *ehdr = (Elf64_Ehdr *)header;
177 if (header[EI_DATA] == ELFDATA2LSB) {
178 if (ehdr->e_machine == EM_X86_64 &&
179 ehdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
180 platform = "amd64-freebsd";
186 VG_(debugLog)(2, "launcher", "selected platform '%s'\n",
187 platform ? platform : "unknown");
189 return platform;
192 /* Where we expect to find all our aux files */
193 static const char *valgrind_lib = VG_LIBDIR;
195 int main(int argc, char** argv, char** envp)
197 int i, j, loglevel, r;
198 const char *toolname = NULL;
199 const char *clientname = NULL;
200 const char *platform;
201 const char *default_platform;
202 const char *cp;
203 char *toolfile;
204 char launcher_name[PATH_MAX+1];
205 char* new_line;
206 char** new_env;
207 int oid[4];
208 vki_size_t len;
210 /* Start the debugging-log system ASAP. First find out how many
211 "-d"s were specified. This is a pre-scan of the command line.
212 At the same time, look for the tool name. */
213 loglevel = 0;
214 for (i = 1; i < argc; i++) {
215 if (argv[i][0] != '-') {
216 clientname = argv[i];
217 break;
219 if (0 == strcmp(argv[i], "--")) {
220 if (i+1 < argc) {
221 clientname = argv[i+1];
223 break;
225 if (0 == strcmp(argv[i], "-d")) {
226 loglevel++;
228 if (0 == strncmp(argv[i], "--tool=", 7)) {
229 toolname = argv[i] + 7;
233 /* ... and start the debug logger. Now we can safely emit logging
234 messages all through startup. */
235 VG_(debugLog_startup)(loglevel, "Stage 1");
237 /* Make sure we know which tool we're using */
238 if (toolname) {
239 VG_(debugLog)(1, "launcher", "tool '%s' requested\n", toolname);
240 } else {
241 VG_(debugLog)(1, "launcher",
242 "no tool requested, defaulting to 'memcheck'\n");
243 toolname = "memcheck";
246 /* Select a platform to use if we can't decide that by looking at
247 the executable (eg because it's a shell script). Note that the
248 default_platform is not necessarily either the primary or
249 secondary build target. Instead it's chosen to maximise the
250 chances that /bin/sh will work on it. Hence for a primary
251 target of ppc64-linux we still choose ppc32-linux as the default
252 target, because on most ppc64-linux setups, the basic /bin,
253 /usr/bin, etc, stuff is built in 32-bit mode, not 64-bit
254 mode. */
255 if (0==strcmp(VG_PLATFORM,"x86-freebsd")) {
256 default_platform = "x86-freebsd";
257 } else if (0==strcmp(VG_PLATFORM,"amd64-freebsd")) {
258 default_platform = "amd64-freebsd";
259 } else {
260 barf("Unknown VG_PLATFORM '%s'", VG_PLATFORM);
263 /* Work out what platform to use, or use the default platform if
264 not possible. */
265 if (clientname == NULL) {
266 VG_(debugLog)(1, "launcher",
267 "no client specified, defaulting platform to '%s'\n",
268 default_platform);
269 platform = default_platform;
270 } else if ((platform = select_platform(clientname)) != NULL) {
271 VG_(debugLog)(1, "launcher", "selected platform '%s'\n", platform);
272 } else {
273 VG_(debugLog)(1, "launcher",
274 "no platform detected, defaulting platform to '%s'\n",
275 default_platform);
276 platform = default_platform;
279 /* Figure out the name of this executable (viz, the launcher), so
280 we can tell stage2. stage2 will use the name for recursive
281 invocations of valgrind on child processes. */
282 memset(launcher_name, 0, PATH_MAX+1);
284 oid[0] = CTL_KERN;
285 oid[1] = KERN_PROC;
286 oid[2] = KERN_PROC_PATHNAME;
287 oid[3] = getpid();
288 len = PATH_MAX;
289 r = sysctl(oid, 4, launcher_name, &len, 0, 0);
290 if (r != 0) {
291 fprintf(stderr, "valgrind: warning (non-fatal): "
292 "sysctl(\"kern.proc.pathname\") failed.\n");
293 fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
294 "will not work.\n");
297 /* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */
298 new_line = malloc(strlen(VALGRIND_LAUNCHER) + 1
299 + strlen(launcher_name) + 1);
300 if (new_line == NULL) {
301 barf("malloc of new_line failed.");
303 strcpy(new_line, VALGRIND_LAUNCHER);
304 strcat(new_line, "=");
305 strcat(new_line, launcher_name);
307 for (j = 0; envp[j]; j++) {
308 // do nothing
310 new_env = malloc((j+2) * sizeof(char*));
311 if (new_env == NULL) {
312 barf("malloc of new_env failed.");
314 for (i = 0; i < j; i++) {
315 new_env[i] = envp[i];
317 new_env[i++] = new_line;
318 new_env[i++] = NULL;
319 assert(i == j+2);
321 /* Establish the correct VALGRIND_LIB. */
322 cp = getenv(VALGRIND_LIB);
324 if (cp != NULL) {
325 valgrind_lib = cp;
328 /* Build the stage2 invocation, and execve it. Bye! */
329 toolfile = malloc(strlen(valgrind_lib) + strlen(toolname) + strlen(platform) + 3);
330 if (toolfile == NULL) {
331 barf("malloc of toolfile failed.");
333 sprintf(toolfile, "%s/%s-%s", valgrind_lib, toolname, platform);
335 VG_(debugLog)(1, "launcher", "launching %s\n", toolfile);
337 execve(toolfile, argv, new_env);
339 fprintf(stderr, "valgrind: failed to start tool '%s' for platform '%s': %s\n",
340 toolname, platform, strerror(errno));
342 exit(1);