sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 3 | /*--- Startup: the real stuff vg_main.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, an extensible x86 protected-mode |
| 8 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 9 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2004 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 28 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 31 | #define _FILE_OFFSET_BITS 64 |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 32 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 33 | #include "core.h" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 34 | #include "ume.h" |
| 35 | #include "ume_arch.h" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 36 | |
| 37 | #include <dirent.h> |
| 38 | #include <dlfcn.h> |
| 39 | #include <errno.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <stdio.h> |
fitzhardinge | b727d04 | 2004-01-06 00:18:21 +0000 | [diff] [blame] | 42 | #include <stdlib.h> |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | #include <sys/mman.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/stat.h> |
fitzhardinge | b727d04 | 2004-01-06 00:18:21 +0000 | [diff] [blame] | 47 | #include <sys/ptrace.h> |
| 48 | #include <sys/signal.h> |
fitzhardinge | b727d04 | 2004-01-06 00:18:21 +0000 | [diff] [blame] | 49 | #include <sys/wait.h> |
| 50 | #include <unistd.h> |
| 51 | |
thughes | 74b8de2 | 2004-04-22 18:12:31 +0000 | [diff] [blame] | 52 | #ifndef AT_DCACHEBSIZE |
| 53 | #define AT_DCACHEBSIZE 19 |
| 54 | #endif /* AT_DCACHEBSIZE */ |
| 55 | |
| 56 | #ifndef AT_ICACHEBSIZE |
| 57 | #define AT_ICACHEBSIZE 20 |
| 58 | #endif /* AT_ICACHEBSIZE */ |
| 59 | |
| 60 | #ifndef AT_UCACHEBSIZE |
| 61 | #define AT_UCACHEBSIZE 21 |
| 62 | #endif /* AT_UCACHEBSIZE */ |
| 63 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 64 | #ifndef AT_SYSINFO |
| 65 | #define AT_SYSINFO 32 |
| 66 | #endif /* AT_SYSINFO */ |
| 67 | |
| 68 | #ifndef AT_SYSINFO_EHDR |
| 69 | #define AT_SYSINFO_EHDR 33 |
| 70 | #endif /* AT_SYSINFO_EHDR */ |
| 71 | |
| 72 | #ifndef AT_SECURE |
| 73 | #define AT_SECURE 23 /* secure mode boolean */ |
| 74 | #endif /* AT_SECURE */ |
| 75 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 76 | /* redzone gap between client address space and shadow */ |
| 77 | #define REDZONE_SIZE (1 * 1024*1024) |
| 78 | |
| 79 | /* size multiple for client address space */ |
nethercote | e94b2de | 2004-06-23 16:15:06 +0000 | [diff] [blame] | 80 | #define CLIENT_SIZE_MULTIPLE (1 * 1024*1024) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 81 | |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 82 | /* Proportion of client space for its heap (rest is for mmaps + stack) */ |
| 83 | #define CLIENT_HEAP_PROPORTION 0.333 |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 84 | |
| 85 | /*====================================================================*/ |
| 86 | /*=== Global entities not referenced from generated code ===*/ |
| 87 | /*====================================================================*/ |
| 88 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 89 | /* --------------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 90 | Startup stuff |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 91 | ------------------------------------------------------------------ */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 92 | /* linker-defined base address */ |
| 93 | extern char kickstart_base; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 94 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 95 | /* Client address space, lowest to highest (see top of ume.c) */ |
| 96 | Addr VG_(client_base); /* client address space limits */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 97 | Addr VG_(client_end); |
| 98 | Addr VG_(client_mapbase); |
fitzhardinge | 9236079 | 2003-12-24 10:11:11 +0000 | [diff] [blame] | 99 | Addr VG_(client_trampoline_code); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 100 | Addr VG_(clstk_base); |
| 101 | Addr VG_(clstk_end); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 102 | |
| 103 | Addr VG_(brk_base); /* start of brk */ |
| 104 | Addr VG_(brk_limit); /* current brk */ |
| 105 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 106 | Addr VG_(shadow_base); /* tool's shadow memory */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 107 | Addr VG_(shadow_end); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 108 | |
| 109 | Addr VG_(valgrind_base); /* valgrind's address range */ |
rjwalsh | 18b6631 | 2004-08-23 18:15:12 +0000 | [diff] [blame] | 110 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 111 | // Note that VG_(valgrind_last) names the last byte of the section, whereas |
| 112 | // the VG_(*_end) vars name the byte one past the end of the section. |
| 113 | Addr VG_(valgrind_last); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 114 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 115 | vki_rlimit VG_(client_rlimit_data); |
| 116 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 117 | /* This is set early to indicate whether this CPU has the |
| 118 | SSE/fxsave/fxrestor features. */ |
| 119 | Bool VG_(have_ssestate); |
| 120 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 121 | /* stage1 (main) executable */ |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 122 | static Int vgexecfd = -1; |
fitzhardinge | a49f9b5 | 2003-12-16 22:26:45 +0000 | [diff] [blame] | 123 | |
| 124 | /* client executable */ |
| 125 | Int VG_(clexecfd) = -1; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 126 | |
| 127 | /* Path to library directory */ |
| 128 | const Char *VG_(libdir) = VG_LIBDIR; |
| 129 | |
| 130 | /* our argc/argv */ |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 131 | static Int vg_argc; |
| 132 | static Char **vg_argv; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 133 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 134 | /* PID of the main thread */ |
| 135 | Int VG_(main_pid); |
| 136 | |
| 137 | /* PGRP of process */ |
| 138 | Int VG_(main_pgrp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 139 | |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 140 | /* Application-visible file descriptor limits */ |
| 141 | Int VG_(fd_soft_limit) = -1; |
| 142 | Int VG_(fd_hard_limit) = -1; |
fitzhardinge | f0046f2 | 2003-12-18 02:39:22 +0000 | [diff] [blame] | 143 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 144 | /* As deduced from esp_at_startup, the client's argc, argv[] and |
| 145 | envp[] as extracted from the client's stack at startup-time. */ |
| 146 | Int VG_(client_argc); |
| 147 | Char** VG_(client_argv); |
| 148 | Char** VG_(client_envp); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 149 | |
| 150 | /* --------------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 151 | Running stuff |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 152 | ------------------------------------------------------------------ */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 153 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 154 | /* Counts downwards in VG_(run_innerloop). */ |
| 155 | UInt VG_(dispatch_ctr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 156 | |
| 157 | /* 64-bit counter for the number of basic blocks done. */ |
| 158 | ULong VG_(bbs_done); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 159 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 160 | /* Tell the logging mechanism whether we are logging to a file |
| 161 | descriptor or a socket descriptor. */ |
| 162 | Bool VG_(logging_to_filedes) = True; |
| 163 | |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 164 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 165 | /*====================================================================*/ |
| 166 | /*=== Counters, for profiling purposes only ===*/ |
| 167 | /*====================================================================*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 168 | |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 169 | // These ones maintained by vg_dispatch.S |
| 170 | UInt VG_(bb_enchain_count) = 0; // Number of chain operations done |
| 171 | UInt VG_(bb_dechain_count) = 0; // Number of unchain operations done |
| 172 | UInt VG_(unchained_jumps_done) = 0; // Number of unchained jumps done |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 173 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 174 | /* Counts pertaining to internal sanity checking. */ |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 175 | static UInt sanity_fast_count = 0; |
| 176 | static UInt sanity_slow_count = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 177 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 178 | static void print_all_stats ( void ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 179 | { |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 180 | // Translation stats |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 181 | VG_(print_tt_tc_stats)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 182 | VG_(message)(Vg_DebugMsg, |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 183 | "chainings: %d chainings, %d unchainings.", |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 184 | VG_(bb_enchain_count), VG_(bb_dechain_count) ); |
| 185 | VG_(message)(Vg_DebugMsg, |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 186 | " dispatch: %llu jumps (bb entries); of them %u (%lu%%) unchained.", |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 187 | VG_(bbs_done), |
| 188 | VG_(unchained_jumps_done), |
| 189 | ((ULong)(100) * (ULong)(VG_(unchained_jumps_done))) |
| 190 | / ( VG_(bbs_done)==0 ? 1 : VG_(bbs_done) ) |
| 191 | ); |
| 192 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 193 | // Scheduler stats |
nethercote | 844e712 | 2004-08-02 15:27:22 +0000 | [diff] [blame] | 194 | VG_(print_scheduler_stats)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 195 | |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 196 | // Reg-alloc stats |
nethercote | bee3fd9 | 2004-08-02 15:17:43 +0000 | [diff] [blame] | 197 | VG_(print_reg_alloc_stats)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 198 | VG_(message)(Vg_DebugMsg, |
| 199 | " sanity: %d cheap, %d expensive checks.", |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 200 | sanity_fast_count, sanity_slow_count ); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 201 | |
| 202 | // C call stats |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 203 | VG_(print_ccall_stats)(); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 204 | |
| 205 | // UInstr histogram |
| 206 | if (VG_(clo_verbosity) > 3) |
| 207 | VG_(print_UInstr_histogram)(); |
| 208 | |
| 209 | // Memory stats |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 210 | if (VG_(clo_verbosity) > 2) { |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 211 | VG_(message)(Vg_DebugMsg, ""); |
| 212 | VG_(message)(Vg_DebugMsg, |
| 213 | "------ Valgrind's internal memory use stats follow ------" ); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 214 | VG_(sanity_check_malloc_all)(); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 215 | VG_(print_all_arena_stats)(); |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 216 | VG_(message)(Vg_DebugMsg, ""); |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 217 | VG_(message)(Vg_DebugMsg, |
| 218 | "------ Valgrind's ExeContext management stats follow ------" ); |
| 219 | VG_(print_ExeContext_stats)(); |
| 220 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | /*====================================================================*/ |
| 225 | /*=== Miscellaneous global functions ===*/ |
| 226 | /*====================================================================*/ |
| 227 | |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 228 | /* Start debugger and get it to attach to this process. Called if the |
| 229 | user requests this service after an error has been shown, so she can |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 230 | poke around and look at parameters, memory, etc. You can't |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 231 | meaningfully get the debugger to continue the program, though; to |
| 232 | continue, quit the debugger. */ |
| 233 | void VG_(start_debugger) ( Int tid ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 234 | { |
| 235 | Int pid; |
| 236 | |
| 237 | if ((pid = fork()) == 0) { |
| 238 | ptrace(PTRACE_TRACEME, 0, NULL, NULL); |
| 239 | VG_(kkill)(VG_(getpid)(), VKI_SIGSTOP); |
| 240 | |
| 241 | } else if (pid > 0) { |
| 242 | struct user_regs_struct regs; |
| 243 | Int status; |
| 244 | Int res; |
| 245 | |
nethercote | 9380b4a | 2004-09-07 22:22:39 +0000 | [diff] [blame] | 246 | if (VG_(is_running_thread)( tid )) |
| 247 | VGA_(regs_for_ptrace_from_BB)(®s); |
| 248 | else |
| 249 | VGA_(regs_for_ptrace_from_tst)(&VG_(threads)[tid].arch, ®s); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 250 | |
| 251 | if ((res = VG_(waitpid)(pid, &status, 0)) == pid && |
| 252 | WIFSTOPPED(status) && WSTOPSIG(status) == SIGSTOP && |
| 253 | ptrace(PTRACE_SETREGS, pid, NULL, ®s) == 0 && |
thughes | 1d16005 | 2004-04-21 15:39:57 +0000 | [diff] [blame] | 254 | kill(pid, SIGSTOP) == 0 && |
| 255 | ptrace(PTRACE_DETACH, pid, NULL, 0) == 0) { |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 256 | Char pidbuf[15]; |
| 257 | Char file[30]; |
| 258 | Char buf[100]; |
| 259 | Char *bufptr; |
| 260 | Char *cmdptr; |
| 261 | |
| 262 | VG_(sprintf)(pidbuf, "%d", pid); |
| 263 | VG_(sprintf)(file, "/proc/%d/fd/%d", pid, VG_(clexecfd)); |
| 264 | |
| 265 | bufptr = buf; |
| 266 | cmdptr = VG_(clo_db_command); |
| 267 | |
| 268 | while (*cmdptr) { |
| 269 | switch (*cmdptr) { |
| 270 | case '%': |
| 271 | switch (*++cmdptr) { |
| 272 | case 'f': |
| 273 | VG_(memcpy)(bufptr, file, VG_(strlen)(file)); |
| 274 | bufptr += VG_(strlen)(file); |
| 275 | cmdptr++; |
| 276 | break; |
| 277 | case 'p': |
| 278 | VG_(memcpy)(bufptr, pidbuf, VG_(strlen)(pidbuf)); |
| 279 | bufptr += VG_(strlen)(pidbuf); |
| 280 | cmdptr++; |
| 281 | break; |
| 282 | default: |
| 283 | *bufptr++ = *cmdptr++; |
| 284 | break; |
| 285 | } |
| 286 | break; |
| 287 | default: |
| 288 | *bufptr++ = *cmdptr++; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | *bufptr++ = '\0'; |
| 294 | |
| 295 | VG_(message)(Vg_UserMsg, "starting debugger with cmd: %s", buf); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 296 | res = VG_(system)(buf); |
| 297 | if (res == 0) { |
| 298 | VG_(message)(Vg_UserMsg, ""); |
| 299 | VG_(message)(Vg_UserMsg, |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 300 | "Debugger has detached. Valgrind regains control. We continue."); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 301 | } else { |
| 302 | VG_(message)(Vg_UserMsg, "Apparently failed!"); |
| 303 | VG_(message)(Vg_UserMsg, ""); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | VG_(kkill)(pid, VKI_SIGKILL); |
| 308 | VG_(waitpid)(pid, &status, 0); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /* Print some helpful-ish text about unimplemented things, and give |
| 314 | up. */ |
| 315 | void VG_(unimplemented) ( Char* msg ) |
| 316 | { |
| 317 | VG_(message)(Vg_UserMsg, ""); |
| 318 | VG_(message)(Vg_UserMsg, |
| 319 | "Valgrind detected that your program requires"); |
| 320 | VG_(message)(Vg_UserMsg, |
| 321 | "the following unimplemented functionality:"); |
| 322 | VG_(message)(Vg_UserMsg, " %s", msg); |
| 323 | VG_(message)(Vg_UserMsg, |
| 324 | "This may be because the functionality is hard to implement,"); |
| 325 | VG_(message)(Vg_UserMsg, |
| 326 | "or because no reasonable program would behave this way,"); |
| 327 | VG_(message)(Vg_UserMsg, |
| 328 | "or because nobody has yet needed it. In any case, let us know at"); |
| 329 | VG_(message)(Vg_UserMsg, |
| 330 | "%s and/or try to work around the problem, if you can.", VG_BUGS_TO); |
| 331 | VG_(message)(Vg_UserMsg, |
| 332 | ""); |
| 333 | VG_(message)(Vg_UserMsg, |
| 334 | "Valgrind has to exit now. Sorry. Bye!"); |
| 335 | VG_(message)(Vg_UserMsg, |
| 336 | ""); |
| 337 | VG_(pp_sched_status)(); |
| 338 | VG_(exit)(1); |
| 339 | } |
| 340 | |
| 341 | Addr VG_(get_stack_pointer) ( void ) |
| 342 | { |
| 343 | return VG_(baseBlock)[VGOFF_(m_esp)]; |
| 344 | } |
| 345 | |
| 346 | /* Debugging thing .. can be called from assembly with OYNK macro. */ |
| 347 | void VG_(oynk) ( Int n ) |
| 348 | { |
| 349 | OINK(n); |
| 350 | } |
| 351 | |
| 352 | /* Initialize the PID and PGRP of scheduler LWP; this is also called |
| 353 | in any new children after fork. */ |
| 354 | static void newpid(ThreadId unused) |
| 355 | { |
| 356 | /* PID of scheduler LWP */ |
| 357 | VG_(main_pid) = VG_(getpid)(); |
| 358 | VG_(main_pgrp) = VG_(getpgrp)(); |
| 359 | } |
| 360 | |
| 361 | /*====================================================================*/ |
| 362 | /*=== Check we were launched by stage 1 ===*/ |
| 363 | /*====================================================================*/ |
| 364 | |
| 365 | /* Look for our AUXV table */ |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 366 | int scan_auxv(void) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 367 | { |
| 368 | const struct ume_auxv *auxv = find_auxv((int *)ume_exec_esp); |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 369 | int padfile = -1, found = 0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 370 | |
| 371 | for (; auxv->a_type != AT_NULL; auxv++) |
| 372 | switch(auxv->a_type) { |
| 373 | case AT_UME_PADFD: |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 374 | padfile = auxv->u.a_val; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 375 | found |= 1; |
| 376 | break; |
| 377 | |
| 378 | case AT_UME_EXECFD: |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 379 | vgexecfd = auxv->u.a_val; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 380 | found |= 2; |
| 381 | break; |
| 382 | } |
| 383 | |
nethercote | 361a14e | 2004-07-26 11:11:56 +0000 | [diff] [blame] | 384 | if ( found != (1|2) ) { |
| 385 | fprintf(stderr, "valgrind: stage2 must be launched by stage1\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 386 | exit(127); |
| 387 | } |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 388 | vg_assert(padfile >= 0); |
| 389 | return padfile; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
| 393 | /*====================================================================*/ |
| 394 | /*=== Address space determination ===*/ |
| 395 | /*====================================================================*/ |
| 396 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 397 | static void layout_remaining_space(Addr argc_addr, float ratio) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 398 | { |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 399 | Int ires; |
| 400 | void* vres; |
| 401 | addr_t client_size, shadow_size; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 402 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 403 | VG_(valgrind_base) = (addr_t)&kickstart_base; |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 404 | VG_(valgrind_last) = ROUNDUP(argc_addr, 0x10000) - 1; // stack |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 405 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 406 | // This gives the client the largest possible address space while |
| 407 | // taking into account the tool's shadow needs. |
| 408 | client_size = ROUNDDN((VG_(valgrind_base)-REDZONE_SIZE) / (1.+ratio), |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 409 | CLIENT_SIZE_MULTIPLE); |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 410 | VG_(client_base) = CLIENT_BASE; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 411 | VG_(client_end) = VG_(client_base) + client_size; |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 412 | /* where !FIXED mmap goes */ |
nethercote | bc7b4f4 | 2004-07-26 12:44:35 +0000 | [diff] [blame] | 413 | VG_(client_mapbase) = VG_(client_base) + |
| 414 | PGROUNDDN((addr_t)(client_size * CLIENT_HEAP_PROPORTION)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 415 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 416 | VG_(shadow_base) = VG_(client_end) + REDZONE_SIZE; |
nethercote | 410bfe6 | 2004-09-04 15:53:35 +0000 | [diff] [blame] | 417 | VG_(shadow_end) = VG_(valgrind_base); |
| 418 | shadow_size = VG_(shadow_end) - VG_(shadow_base); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 419 | |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 420 | #define SEGSIZE(a,b) ((VG_(b) - VG_(a))/(1024*1024)) |
| 421 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 422 | if (0) |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 423 | VG_(printf)( |
| 424 | "client_base %8x (%dMB)\n" |
| 425 | "client_mapbase %8x (%dMB)\n" |
| 426 | "client_end %8x (%dMB)\n" |
| 427 | "shadow_base %8x (%dMB)\n" |
nethercote | 410bfe6 | 2004-09-04 15:53:35 +0000 | [diff] [blame] | 428 | "shadow_end %8x\n" |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 429 | "valgrind_base %8x (%dMB)\n" |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 430 | "valgrind_last %8x\n", |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 431 | VG_(client_base), SEGSIZE(client_base, client_mapbase), |
| 432 | VG_(client_mapbase), SEGSIZE(client_mapbase, client_end), |
| 433 | VG_(client_end), SEGSIZE(client_end, shadow_base), |
| 434 | VG_(shadow_base), SEGSIZE(shadow_base, shadow_end), |
nethercote | 410bfe6 | 2004-09-04 15:53:35 +0000 | [diff] [blame] | 435 | VG_(shadow_end), |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 436 | VG_(valgrind_base), SEGSIZE(valgrind_base, valgrind_last), |
| 437 | VG_(valgrind_last) |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 438 | ); |
| 439 | |
| 440 | #undef SEGSIZE |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 441 | |
| 442 | // Ban redzone |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 443 | vres = mmap((void *)VG_(client_end), REDZONE_SIZE, PROT_NONE, |
| 444 | MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0); |
| 445 | vg_assert((void*)-1 != vres); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 446 | |
| 447 | // Make client hole |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 448 | ires = munmap((void*)VG_(client_base), client_size); |
| 449 | vg_assert(0 == ires); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 450 | |
| 451 | // Map shadow memory. |
| 452 | // Initially all inaccessible, incrementally initialized as it is used |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 453 | if (shadow_size != 0) { |
| 454 | vres = mmap((char *)VG_(shadow_base), shadow_size, PROT_NONE, |
| 455 | MAP_PRIVATE|MAP_ANON|MAP_FIXED, -1, 0); |
nethercote | d472262 | 2004-08-30 19:36:42 +0000 | [diff] [blame] | 456 | if ((void*)-1 == vres) { |
| 457 | fprintf(stderr, |
| 458 | "valgrind: Couldn't allocate address space for shadow memory\n" |
| 459 | "valgrind: Are you using a kernel with a small user address space,\n" |
| 460 | "valgrind: or do you have your virtual memory size limited?\n"); |
| 461 | exit(1); |
| 462 | } |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 463 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /*====================================================================*/ |
| 467 | /*=== Command line setup ===*/ |
| 468 | /*====================================================================*/ |
| 469 | |
| 470 | /* Nb: malloc'd memory never freed -- kept throughout like argv, envp */ |
| 471 | static char* get_file_clo(char* dir) |
| 472 | { |
| 473 | # define FLEN 512 |
| 474 | Int fd, n; |
| 475 | struct stat s1; |
| 476 | char* f_clo = NULL; |
| 477 | char filename[FLEN]; |
| 478 | |
| 479 | snprintf(filename, FLEN, "%s/.valgrindrc", ( NULL == dir ? "" : dir ) ); |
| 480 | fd = VG_(open)(filename, 0, VKI_S_IRUSR); |
| 481 | if ( fd > 0 ) { |
| 482 | if ( 0 == fstat(fd, &s1) ) { |
| 483 | f_clo = malloc(s1.st_size+1); |
| 484 | vg_assert(f_clo); |
| 485 | n = read(fd, f_clo, s1.st_size); |
| 486 | if (n == -1) n = 0; |
| 487 | f_clo[n] = '\0'; |
| 488 | } |
| 489 | close(fd); |
| 490 | } |
| 491 | return f_clo; |
| 492 | # undef FLEN |
| 493 | } |
| 494 | |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 495 | #define ISSPACE(cc) ((cc) == ' ' || (cc) == '\t' || (cc) == '\n') |
| 496 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 497 | static Int count_args(char* s) |
| 498 | { |
| 499 | Int n = 0; |
| 500 | if (s) { |
| 501 | char* cp = s; |
| 502 | while (True) { |
| 503 | // We have alternating sequences: blanks, non-blanks, blanks... |
| 504 | // count the non-blanks sequences. |
| 505 | while ( ISSPACE(*cp) ) cp++; |
| 506 | if ( !*cp ) break; |
| 507 | n++; |
| 508 | while ( !ISSPACE(*cp) && *cp ) cp++; |
| 509 | } |
| 510 | } |
| 511 | return n; |
| 512 | } |
| 513 | |
| 514 | /* add args out of environment, skipping multiple spaces and -- args */ |
| 515 | static char** copy_args( char* s, char** to ) |
| 516 | { |
| 517 | if (s) { |
| 518 | char* cp = s; |
| 519 | while (True) { |
| 520 | // We have alternating sequences: blanks, non-blanks, blanks... |
| 521 | // copy the non-blanks sequences, and add terminating '\0' |
| 522 | while ( ISSPACE(*cp) ) cp++; |
| 523 | if ( !*cp ) break; |
| 524 | *to++ = cp; |
| 525 | while ( !ISSPACE(*cp) && *cp ) cp++; |
| 526 | if ( *cp ) *cp++ = '\0'; // terminate if necessary |
| 527 | if (VG_STREQ(to[-1], "--")) to--; // undo any '--' arg |
| 528 | } |
| 529 | } |
| 530 | return to; |
| 531 | } |
| 532 | |
nethercote | e209731 | 2004-06-27 12:29:56 +0000 | [diff] [blame] | 533 | #undef ISSPACE |
| 534 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 535 | // Augment command line with arguments from environment and .valgrindrc |
| 536 | // files. |
| 537 | static void augment_command_line(Int* vg_argc_inout, char*** vg_argv_inout) |
| 538 | { |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 539 | int vg_argc0 = *vg_argc_inout; |
| 540 | char** vg_argv0 = *vg_argv_inout; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 541 | |
| 542 | char* env_clo = getenv(VALGRINDOPTS); |
| 543 | char* f1_clo = get_file_clo( getenv("HOME") ); |
| 544 | char* f2_clo = get_file_clo("."); |
| 545 | |
| 546 | /* copy any extra args from file or environment, if present */ |
| 547 | if ( (env_clo && *env_clo) || (f1_clo && *f1_clo) || (f2_clo && *f2_clo) ) { |
| 548 | /* ' ' separated extra options */ |
| 549 | char **from; |
| 550 | char **to; |
| 551 | int env_arg_count, f1_arg_count, f2_arg_count; |
| 552 | |
| 553 | env_arg_count = count_args(env_clo); |
| 554 | f1_arg_count = count_args(f1_clo); |
| 555 | f2_arg_count = count_args(f2_clo); |
| 556 | |
| 557 | if (0) |
| 558 | printf("extra-argc=%d %d %d\n", |
| 559 | env_arg_count, f1_arg_count, f2_arg_count); |
| 560 | |
| 561 | /* +2: +1 for null-termination, +1 for added '--' */ |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 562 | from = vg_argv0; |
| 563 | vg_argv0 = malloc( (vg_argc0 + env_arg_count + f1_arg_count |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 564 | + f2_arg_count + 2) * sizeof(char **)); |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 565 | vg_assert(vg_argv0); |
| 566 | to = vg_argv0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 567 | |
| 568 | /* copy argv[0] */ |
| 569 | *to++ = *from++; |
| 570 | |
| 571 | /* Copy extra args from env var and file, in the order: ~/.valgrindrc, |
| 572 | * $VALGRIND_OPTS, ./.valgrindrc -- more local options are put later |
| 573 | * to override less local ones. */ |
| 574 | to = copy_args(f1_clo, to); |
| 575 | to = copy_args(env_clo, to); |
| 576 | to = copy_args(f2_clo, to); |
| 577 | |
| 578 | /* copy original arguments, stopping at command or -- */ |
| 579 | while (*from) { |
| 580 | if (**from != '-') |
| 581 | break; |
| 582 | if (VG_STREQ(*from, "--")) { |
| 583 | from++; /* skip -- */ |
| 584 | break; |
| 585 | } |
| 586 | *to++ = *from++; |
| 587 | } |
| 588 | |
| 589 | /* add -- */ |
| 590 | *to++ = "--"; |
| 591 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 592 | vg_argc0 = to - vg_argv0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 593 | |
| 594 | /* copy rest of original command line, then NULL */ |
| 595 | while (*from) *to++ = *from++; |
| 596 | *to = NULL; |
| 597 | } |
| 598 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 599 | *vg_argc_inout = vg_argc0; |
| 600 | *vg_argv_inout = vg_argv0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 601 | } |
| 602 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 603 | #define VG_CLO_SEP '\01' |
| 604 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 605 | static void get_command_line( int argc, char** argv, |
| 606 | Int* vg_argc_out, Char*** vg_argv_out, |
| 607 | char*** cl_argv_out ) |
| 608 | { |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 609 | int vg_argc0; |
| 610 | char** vg_argv0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 611 | char** cl_argv; |
| 612 | char* env_clo = getenv(VALGRINDCLO); |
| 613 | |
| 614 | if (env_clo != NULL && *env_clo != '\0') { |
| 615 | char *cp; |
| 616 | char **cpp; |
| 617 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 618 | /* OK, VALGRINDCLO is set, which means we must be a child of another |
| 619 | Valgrind process using --trace-children, so we're getting all our |
| 620 | arguments from VALGRINDCLO, and the entire command line belongs to |
| 621 | the client (including argv[0]) */ |
| 622 | vg_argc0 = 1; /* argv[0] */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 623 | for (cp = env_clo; *cp; cp++) |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 624 | if (*cp == VG_CLO_SEP) |
| 625 | vg_argc0++; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 626 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 627 | vg_argv0 = malloc(sizeof(char **) * (vg_argc0 + 1)); |
| 628 | vg_assert(vg_argv0); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 629 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 630 | cpp = vg_argv0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 631 | |
| 632 | *cpp++ = "valgrind"; /* nominal argv[0] */ |
| 633 | *cpp++ = env_clo; |
| 634 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 635 | // Replace the VG_CLO_SEP args separator with '\0' |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 636 | for (cp = env_clo; *cp; cp++) { |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 637 | if (*cp == VG_CLO_SEP) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 638 | *cp++ = '\0'; /* chop it up in place */ |
| 639 | *cpp++ = cp; |
| 640 | } |
| 641 | } |
| 642 | *cpp = NULL; |
| 643 | cl_argv = argv; |
| 644 | |
| 645 | } else { |
| 646 | /* Count the arguments on the command line. */ |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 647 | vg_argv0 = argv; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 648 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 649 | for (vg_argc0 = 1; vg_argc0 < argc; vg_argc0++) { |
| 650 | if (argv[vg_argc0][0] != '-') /* exe name */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 651 | break; |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 652 | if (VG_STREQ(argv[vg_argc0], "--")) { /* dummy arg */ |
| 653 | vg_argc0++; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | } |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 657 | cl_argv = &argv[vg_argc0]; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 658 | |
| 659 | /* Get extra args from VALGRIND_OPTS and .valgrindrc files. |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 660 | Note we don't do this if getting args from VALGRINDCLO, as |
| 661 | those extra args will already be present in VALGRINDCLO. */ |
| 662 | augment_command_line(&vg_argc0, &vg_argv0); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (0) { |
| 666 | Int i; |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 667 | for (i = 0; i < vg_argc0; i++) |
| 668 | printf("vg_argv0[%d]=\"%s\"\n", i, vg_argv0[i]); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 669 | } |
| 670 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 671 | *vg_argc_out = vg_argc0; |
| 672 | *vg_argv_out = (Char**)vg_argv0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 673 | *cl_argv_out = cl_argv; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | /*====================================================================*/ |
| 678 | /*=== Environment and stack setup ===*/ |
| 679 | /*====================================================================*/ |
| 680 | |
| 681 | /* Scan a colon-separated list, and call a function on each element. |
| 682 | The string must be mutable, because we insert a temporary '\0', but |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 683 | the string will end up unmodified. (*func) should return True if it |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 684 | doesn't need to see any more. |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 685 | |
| 686 | This routine will return True if (*func) returns True and False if |
| 687 | it reaches the end of the list without that happening. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 688 | */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 689 | static Bool scan_colsep(char *colsep, Bool (*func)(const char *)) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 690 | { |
| 691 | char *cp, *entry; |
| 692 | int end; |
| 693 | |
| 694 | if (colsep == NULL || |
| 695 | *colsep == '\0') |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 696 | return False; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 697 | |
| 698 | entry = cp = colsep; |
| 699 | |
| 700 | do { |
| 701 | end = (*cp == '\0'); |
| 702 | |
| 703 | if (*cp == ':' || *cp == '\0') { |
| 704 | char save = *cp; |
| 705 | |
| 706 | *cp = '\0'; |
thughes | 21942d9 | 2004-07-12 09:35:37 +0000 | [diff] [blame] | 707 | if ((*func)(entry)) { |
| 708 | *cp = save; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 709 | return True; |
thughes | 21942d9 | 2004-07-12 09:35:37 +0000 | [diff] [blame] | 710 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 711 | *cp = save; |
| 712 | entry = cp+1; |
| 713 | } |
| 714 | cp++; |
| 715 | } while(!end); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 716 | |
| 717 | return False; |
| 718 | } |
| 719 | |
| 720 | static Bool contains(const char *p) { |
| 721 | if (VG_STREQ(p, VG_(libdir))) { |
| 722 | return True; |
| 723 | } |
| 724 | return False; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* Prepare the client's environment. This is basically a copy of our |
| 728 | environment, except: |
| 729 | 1. LD_LIBRARY_PATH=$VALGRINDLIB:$LD_LIBRARY_PATH |
| 730 | 2. LD_PRELOAD=$VALGRINDLIB/vg_inject.so:($VALGRINDLIB/vgpreload_TOOL.so:)?$LD_PRELOAD |
| 731 | |
| 732 | If any of these is missing, then it is added. |
| 733 | |
| 734 | Yummy. String hacking in C. |
| 735 | |
| 736 | If this needs to handle any more variables it should be hacked |
| 737 | into something table driven. |
| 738 | */ |
| 739 | static char **fix_environment(char **origenv, const char *preload) |
| 740 | { |
| 741 | static const char inject_so[] = "vg_inject.so"; |
| 742 | static const char ld_library_path[] = "LD_LIBRARY_PATH="; |
| 743 | static const char ld_preload[] = "LD_PRELOAD="; |
| 744 | static const char valgrind_clo[] = VALGRINDCLO "="; |
| 745 | static const int ld_library_path_len = sizeof(ld_library_path)-1; |
| 746 | static const int ld_preload_len = sizeof(ld_preload)-1; |
| 747 | static const int valgrind_clo_len = sizeof(valgrind_clo)-1; |
| 748 | int ld_preload_done = 0; |
| 749 | int ld_library_path_done = 0; |
| 750 | char *inject_path; |
| 751 | int inject_path_len; |
| 752 | int vgliblen = strlen(VG_(libdir)); |
| 753 | char **cpp; |
| 754 | char **ret; |
| 755 | int envc; |
| 756 | const int preloadlen = (preload == NULL) ? 0 : strlen(preload); |
| 757 | |
| 758 | /* Find the vg_inject.so; also make room for the tool preload |
| 759 | library */ |
| 760 | inject_path_len = sizeof(inject_so) + vgliblen + preloadlen + 16; |
| 761 | inject_path = malloc(inject_path_len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 762 | vg_assert(inject_path); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 763 | |
| 764 | if (preload) |
| 765 | snprintf(inject_path, inject_path_len, "%s/%s:%s", |
| 766 | VG_(libdir), inject_so, preload); |
| 767 | else |
| 768 | snprintf(inject_path, inject_path_len, "%s/%s", |
| 769 | VG_(libdir), inject_so); |
| 770 | |
| 771 | /* Count the original size of the env */ |
| 772 | envc = 0; /* trailing NULL */ |
| 773 | for (cpp = origenv; cpp && *cpp; cpp++) |
| 774 | envc++; |
| 775 | |
| 776 | /* Allocate a new space */ |
| 777 | ret = malloc(sizeof(char *) * (envc+3+1)); /* 3 new entries + NULL */ |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 778 | vg_assert(ret); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 779 | |
| 780 | /* copy it over */ |
| 781 | for (cpp = ret; *origenv; ) |
| 782 | *cpp++ = *origenv++; |
| 783 | *cpp = NULL; |
| 784 | |
| 785 | vg_assert(envc == (cpp - ret)); |
| 786 | |
| 787 | /* Walk over the new environment, mashing as we go */ |
| 788 | for (cpp = ret; cpp && *cpp; cpp++) { |
| 789 | if (memcmp(*cpp, ld_library_path, ld_library_path_len) == 0) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 790 | /* If the LD_LIBRARY_PATH already contains libdir, then don't |
| 791 | bother adding it again, even if it isn't the first (it |
| 792 | seems that the Java runtime will keep reexecing itself |
| 793 | unless its paths are at the front of LD_LIBRARY_PATH) */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 794 | if (!scan_colsep(*cpp + ld_library_path_len, contains)) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 795 | int len = strlen(*cpp) + vgliblen*2 + 16; |
| 796 | char *cp = malloc(len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 797 | vg_assert(cp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 798 | |
| 799 | snprintf(cp, len, "%s%s:%s", |
| 800 | ld_library_path, VG_(libdir), |
| 801 | (*cpp)+ld_library_path_len); |
| 802 | |
| 803 | *cpp = cp; |
| 804 | } |
| 805 | |
| 806 | ld_library_path_done = 1; |
| 807 | } else if (memcmp(*cpp, ld_preload, ld_preload_len) == 0) { |
| 808 | int len = strlen(*cpp) + inject_path_len; |
| 809 | char *cp = malloc(len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 810 | vg_assert(cp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 811 | |
| 812 | snprintf(cp, len, "%s%s:%s", |
| 813 | ld_preload, inject_path, (*cpp)+ld_preload_len); |
| 814 | |
| 815 | *cpp = cp; |
| 816 | |
| 817 | ld_preload_done = 1; |
| 818 | } else if (memcmp(*cpp, valgrind_clo, valgrind_clo_len) == 0) { |
| 819 | *cpp = ""; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /* Add the missing bits */ |
| 824 | |
| 825 | if (!ld_library_path_done) { |
| 826 | int len = ld_library_path_len + vgliblen*2 + 16; |
| 827 | char *cp = malloc(len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 828 | vg_assert(cp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 829 | |
| 830 | snprintf(cp, len, "%s%s", ld_library_path, VG_(libdir)); |
| 831 | |
| 832 | ret[envc++] = cp; |
| 833 | } |
| 834 | |
| 835 | if (!ld_preload_done) { |
| 836 | int len = ld_preload_len + inject_path_len; |
| 837 | char *cp = malloc(len); |
nethercote | 7c018f4 | 2004-07-17 16:40:50 +0000 | [diff] [blame] | 838 | vg_assert(cp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 839 | |
| 840 | snprintf(cp, len, "%s%s", |
| 841 | ld_preload, inject_path); |
| 842 | |
| 843 | ret[envc++] = cp; |
| 844 | } |
| 845 | |
| 846 | ret[envc] = NULL; |
| 847 | |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | extern char **environ; /* our environment */ |
| 852 | //#include <error.h> |
| 853 | |
| 854 | /* Add a string onto the string table, and return its address */ |
| 855 | static char *copy_str(char **tab, const char *str) |
| 856 | { |
| 857 | char *cp = *tab; |
| 858 | char *orig = cp; |
| 859 | |
| 860 | while(*str) |
| 861 | *cp++ = *str++; |
| 862 | *cp++ = '\0'; |
| 863 | |
| 864 | if (0) |
nethercote | f84f695 | 2004-07-15 14:58:33 +0000 | [diff] [blame] | 865 | printf("copied %p \"%s\" len %d\n", orig, orig, cp-orig); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 866 | |
| 867 | *tab = cp; |
| 868 | |
| 869 | return orig; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | This sets up the client's initial stack, containing the args, |
| 874 | environment and aux vector. |
| 875 | |
| 876 | The format of the stack is: |
| 877 | |
| 878 | higher address +-----------------+ |
| 879 | | Trampoline code | |
| 880 | +-----------------+ |
| 881 | | | |
| 882 | : string table : |
| 883 | | | |
| 884 | +-----------------+ |
| 885 | | AT_NULL | |
| 886 | - - |
| 887 | | auxv | |
| 888 | +-----------------+ |
| 889 | | NULL | |
| 890 | - - |
| 891 | | envp | |
| 892 | +-----------------+ |
| 893 | | NULL | |
| 894 | - - |
| 895 | | argv | |
| 896 | +-----------------+ |
| 897 | | argc | |
| 898 | lower address +-----------------+ <- esp |
| 899 | | undefined | |
| 900 | : : |
| 901 | */ |
| 902 | static Addr setup_client_stack(char **orig_argv, char **orig_envp, |
| 903 | const struct exeinfo *info, |
| 904 | UInt** client_auxv) |
| 905 | { |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 906 | void* res; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 907 | char **cpp; |
| 908 | char *strtab; /* string table */ |
| 909 | char *stringbase; |
| 910 | addr_t *ptr; |
| 911 | struct ume_auxv *auxv; |
| 912 | const struct ume_auxv *orig_auxv; |
| 913 | const struct ume_auxv *cauxv; |
| 914 | unsigned stringsize; /* total size of strings in bytes */ |
| 915 | unsigned auxsize; /* total size of auxv in bytes */ |
| 916 | int argc; /* total argc */ |
| 917 | int envc; /* total number of env vars */ |
| 918 | unsigned stacksize; /* total client stack size */ |
| 919 | addr_t cl_esp; /* client stack base (initial esp) */ |
| 920 | |
| 921 | /* use our own auxv as a prototype */ |
| 922 | orig_auxv = find_auxv(ume_exec_esp); |
| 923 | |
| 924 | /* ==================== compute sizes ==================== */ |
| 925 | |
| 926 | /* first of all, work out how big the client stack will be */ |
| 927 | stringsize = 0; |
| 928 | |
| 929 | /* paste on the extra args if the loader needs them (ie, the #! |
| 930 | interpreter and its argument) */ |
| 931 | argc = 0; |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 932 | if (info->interp_name != NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 933 | argc++; |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 934 | stringsize += strlen(info->interp_name) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 935 | } |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 936 | if (info->interp_args != NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 937 | argc++; |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 938 | stringsize += strlen(info->interp_args) + 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* now scan the args we're given... */ |
| 942 | for (cpp = orig_argv; *cpp; cpp++) { |
| 943 | argc++; |
| 944 | stringsize += strlen(*cpp) + 1; |
| 945 | } |
| 946 | |
| 947 | /* ...and the environment */ |
| 948 | envc = 0; |
| 949 | for (cpp = orig_envp; cpp && *cpp; cpp++) { |
| 950 | envc++; |
| 951 | stringsize += strlen(*cpp) + 1; |
| 952 | } |
| 953 | |
| 954 | /* now, how big is the auxv? */ |
| 955 | auxsize = sizeof(*auxv); /* there's always at least one entry: AT_NULL */ |
| 956 | for (cauxv = orig_auxv; cauxv->a_type != AT_NULL; cauxv++) { |
| 957 | if (cauxv->a_type == AT_PLATFORM) |
| 958 | stringsize += strlen(cauxv->u.a_ptr) + 1; |
| 959 | auxsize += sizeof(*cauxv); |
| 960 | } |
| 961 | |
| 962 | /* OK, now we know how big the client stack is */ |
| 963 | stacksize = |
| 964 | sizeof(int) + /* argc */ |
| 965 | sizeof(char **)*argc + /* argv */ |
| 966 | sizeof(char **) + /* terminal NULL */ |
| 967 | sizeof(char **)*envc + /* envp */ |
| 968 | sizeof(char **) + /* terminal NULL */ |
| 969 | auxsize + /* auxv */ |
| 970 | ROUNDUP(stringsize, sizeof(int)) +/* strings (aligned) */ |
| 971 | VKI_BYTES_PER_PAGE; /* page for trampoline code */ |
| 972 | |
nethercote | f84f695 | 2004-07-15 14:58:33 +0000 | [diff] [blame] | 973 | // decide where stack goes! |
| 974 | VG_(clstk_end) = VG_(client_end); |
| 975 | |
| 976 | VG_(client_trampoline_code) = VG_(clstk_end) - VKI_BYTES_PER_PAGE; |
| 977 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 978 | /* cl_esp is the client's stack pointer */ |
nethercote | f84f695 | 2004-07-15 14:58:33 +0000 | [diff] [blame] | 979 | cl_esp = VG_(clstk_end) - stacksize; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 980 | cl_esp = ROUNDDN(cl_esp, 16); /* make stack 16 byte aligned */ |
| 981 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 982 | /* base of the string table (aligned) */ |
| 983 | stringbase = strtab = (char *)(VG_(client_trampoline_code) - ROUNDUP(stringsize, sizeof(int))); |
| 984 | |
| 985 | VG_(clstk_base) = PGROUNDDN(cl_esp); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 986 | |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 987 | if (0) |
| 988 | printf("stringsize=%d auxsize=%d stacksize=%d\n" |
nethercote | 6a27d83 | 2004-09-07 10:17:02 +0000 | [diff] [blame] | 989 | "clstk_base %p\n" |
| 990 | "clstk_end %p\n", |
| 991 | stringsize, auxsize, stacksize, |
| 992 | (void*)VG_(clstk_base), (void*)VG_(clstk_end)); |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 993 | |
| 994 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 995 | /* ==================== allocate space ==================== */ |
| 996 | |
| 997 | /* allocate a stack - mmap enough space for the stack */ |
nethercote | f84f695 | 2004-07-15 14:58:33 +0000 | [diff] [blame] | 998 | res = mmap((void *)PGROUNDDN(cl_esp), VG_(clstk_end) - PGROUNDDN(cl_esp), |
nethercote | e567e70 | 2004-07-10 17:49:17 +0000 | [diff] [blame] | 999 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 1000 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); |
| 1001 | vg_assert((void*)-1 != res); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1002 | |
| 1003 | /* ==================== copy client stack ==================== */ |
| 1004 | |
| 1005 | ptr = (addr_t *)cl_esp; |
| 1006 | |
| 1007 | /* --- argc --- */ |
| 1008 | *ptr++ = argc; /* client argc */ |
| 1009 | |
| 1010 | /* --- argv --- */ |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 1011 | if (info->interp_name) { |
| 1012 | *ptr++ = (addr_t)copy_str(&strtab, info->interp_name); |
| 1013 | free(info->interp_name); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1014 | } |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 1015 | if (info->interp_args) { |
| 1016 | *ptr++ = (addr_t)copy_str(&strtab, info->interp_args); |
| 1017 | free(info->interp_args); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1018 | } |
| 1019 | for (cpp = orig_argv; *cpp; ptr++, cpp++) { |
| 1020 | *ptr = (addr_t)copy_str(&strtab, *cpp); |
| 1021 | } |
| 1022 | *ptr++ = 0; |
| 1023 | |
| 1024 | /* --- envp --- */ |
| 1025 | VG_(client_envp) = (Char **)ptr; |
| 1026 | for (cpp = orig_envp; cpp && *cpp; ptr++, cpp++) |
| 1027 | *ptr = (addr_t)copy_str(&strtab, *cpp); |
| 1028 | *ptr++ = 0; |
| 1029 | |
| 1030 | /* --- auxv --- */ |
| 1031 | auxv = (struct ume_auxv *)ptr; |
| 1032 | *client_auxv = (UInt *)auxv; |
| 1033 | |
| 1034 | for (; orig_auxv->a_type != AT_NULL; auxv++, orig_auxv++) { |
| 1035 | /* copy the entry... */ |
| 1036 | *auxv = *orig_auxv; |
| 1037 | |
| 1038 | /* ...and fix up the copy */ |
| 1039 | switch(auxv->a_type) { |
| 1040 | case AT_PHDR: |
| 1041 | if (info->phdr == 0) |
| 1042 | auxv->a_type = AT_IGNORE; |
| 1043 | else |
| 1044 | auxv->u.a_val = info->phdr; |
| 1045 | break; |
| 1046 | |
| 1047 | case AT_PHNUM: |
| 1048 | if (info->phdr == 0) |
| 1049 | auxv->a_type = AT_IGNORE; |
| 1050 | else |
| 1051 | auxv->u.a_val = info->phnum; |
| 1052 | break; |
| 1053 | |
| 1054 | case AT_BASE: |
| 1055 | if (info->interp_base == 0) |
| 1056 | auxv->a_type = AT_IGNORE; |
| 1057 | else |
| 1058 | auxv->u.a_val = info->interp_base; |
| 1059 | break; |
| 1060 | |
| 1061 | case AT_PLATFORM: /* points to a platform description string */ |
| 1062 | auxv->u.a_ptr = copy_str(&strtab, orig_auxv->u.a_ptr); |
| 1063 | break; |
| 1064 | |
| 1065 | case AT_ENTRY: |
| 1066 | auxv->u.a_val = info->entry; |
| 1067 | break; |
| 1068 | |
| 1069 | case AT_IGNORE: |
| 1070 | case AT_EXECFD: |
| 1071 | case AT_PHENT: |
| 1072 | case AT_PAGESZ: |
| 1073 | case AT_FLAGS: |
| 1074 | case AT_NOTELF: |
| 1075 | case AT_UID: |
| 1076 | case AT_EUID: |
| 1077 | case AT_GID: |
| 1078 | case AT_EGID: |
| 1079 | case AT_CLKTCK: |
| 1080 | case AT_HWCAP: |
| 1081 | case AT_FPUCW: |
| 1082 | case AT_DCACHEBSIZE: |
| 1083 | case AT_ICACHEBSIZE: |
| 1084 | case AT_UCACHEBSIZE: |
| 1085 | /* All these are pointerless, so we don't need to do anything |
| 1086 | about them. */ |
| 1087 | break; |
| 1088 | |
| 1089 | case AT_SECURE: |
| 1090 | /* If this is 1, then it means that this program is running |
| 1091 | suid, and therefore the dynamic linker should be careful |
| 1092 | about LD_PRELOAD, etc. However, since stage1 (the thing |
| 1093 | the kernel actually execve's) should never be SUID, and we |
| 1094 | need LD_PRELOAD/LD_LIBRARY_PATH to work for the client, we |
| 1095 | set AT_SECURE to 0. */ |
| 1096 | auxv->u.a_val = 0; |
| 1097 | break; |
| 1098 | |
| 1099 | case AT_SYSINFO: |
| 1100 | /* Leave this unmolested for now, but we'll update it later |
| 1101 | when we set up the client trampoline code page */ |
| 1102 | break; |
| 1103 | |
| 1104 | case AT_SYSINFO_EHDR: |
| 1105 | /* Trash this, because we don't reproduce it */ |
| 1106 | auxv->a_type = AT_IGNORE; |
| 1107 | break; |
| 1108 | |
| 1109 | default: |
| 1110 | /* stomp out anything we don't know about */ |
| 1111 | if (0) |
| 1112 | printf("stomping auxv entry %d\n", auxv->a_type); |
| 1113 | auxv->a_type = AT_IGNORE; |
| 1114 | break; |
| 1115 | |
| 1116 | } |
| 1117 | } |
| 1118 | *auxv = *orig_auxv; |
| 1119 | vg_assert(auxv->a_type == AT_NULL); |
| 1120 | |
nethercote | f84f695 | 2004-07-15 14:58:33 +0000 | [diff] [blame] | 1121 | /* --- trampoline page --- */ |
| 1122 | VG_(memcpy)( (void *)VG_(client_trampoline_code), |
| 1123 | &VG_(trampoline_code_start), VG_(trampoline_code_length) ); |
| 1124 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1125 | vg_assert((strtab-stringbase) == stringsize); |
| 1126 | |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 1127 | /* We know the initial ESP is pointing at argc/argv */ |
| 1128 | VG_(client_argc) = *(Int*)cl_esp; |
| 1129 | VG_(client_argv) = (Char**)(cl_esp + sizeof(Int)); |
| 1130 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1131 | return cl_esp; |
| 1132 | } |
| 1133 | |
| 1134 | /*====================================================================*/ |
| 1135 | /*=== Find executable ===*/ |
| 1136 | /*====================================================================*/ |
| 1137 | |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 1138 | static const char* executable_name; |
| 1139 | |
| 1140 | static Bool match_executable(const char *entry) { |
| 1141 | char buf[strlen(entry) + strlen(executable_name) + 2]; |
| 1142 | |
| 1143 | /* empty PATH element means . */ |
| 1144 | if (*entry == '\0') |
| 1145 | entry = "."; |
| 1146 | |
| 1147 | snprintf(buf, sizeof(buf), "%s/%s", entry, executable_name); |
| 1148 | |
| 1149 | if (access(buf, R_OK|X_OK) == 0) { |
| 1150 | executable_name = strdup(buf); |
| 1151 | vg_assert(NULL != executable_name); |
| 1152 | return True; |
| 1153 | } |
| 1154 | return False; |
| 1155 | } |
| 1156 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1157 | static const char* find_executable(const char* exec) |
| 1158 | { |
| 1159 | vg_assert(NULL != exec); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 1160 | executable_name = exec; |
| 1161 | if (strchr(executable_name, '/') == NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1162 | /* no '/' - we need to search the path */ |
| 1163 | char *path = getenv("PATH"); |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 1164 | scan_colsep(path, match_executable); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1165 | } |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 1166 | return executable_name; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /*====================================================================*/ |
| 1171 | /*=== Loading tools ===*/ |
| 1172 | /*====================================================================*/ |
| 1173 | |
| 1174 | static void list_tools(void) |
| 1175 | { |
| 1176 | DIR *dir = opendir(VG_(libdir)); |
| 1177 | struct dirent *de; |
| 1178 | int first = 1; |
| 1179 | |
| 1180 | if (dir == NULL) { |
| 1181 | fprintf(stderr, "Can't open %s: %s (installation problem?)\n", |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1182 | VG_(libdir), strerror(errno)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1183 | return; |
| 1184 | } |
| 1185 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1186 | while ((de = readdir(dir)) != NULL) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1187 | int len = strlen(de->d_name); |
| 1188 | |
| 1189 | /* look for vgskin_TOOL.so names */ |
| 1190 | if (len > (7+1+3) && /* "vgskin_" + at least 1-char toolname + ".so" */ |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1191 | strncmp(de->d_name, "vgskin_", 7) == 0 && |
| 1192 | VG_STREQ(de->d_name + len - 3, ".so")) { |
| 1193 | if (first) { |
| 1194 | fprintf(stderr, "Available tools:\n"); |
| 1195 | first = 0; |
| 1196 | } |
| 1197 | de->d_name[len-3] = '\0'; |
| 1198 | fprintf(stderr, "\t%s\n", de->d_name+7); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | closedir(dir); |
| 1203 | |
| 1204 | if (first) |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1205 | fprintf(stderr, "No tools available in \"%s\" (installation problem?)\n", |
| 1206 | VG_(libdir)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | /* Find and load a tool, and check it looks ok. Also looks to see if there's |
| 1211 | * a matching vgpreload_*.so file, and returns its name in *preloadpath. */ |
| 1212 | static void load_tool( const char *toolname, void** handle_out, |
| 1213 | ToolInfo** toolinfo_out, char **preloadpath_out ) |
| 1214 | { |
| 1215 | Bool ok; |
| 1216 | int len = strlen(VG_(libdir)) + strlen(toolname)*2 + 16; |
| 1217 | char buf[len]; |
| 1218 | void* handle; |
| 1219 | ToolInfo* toolinfo; |
| 1220 | char* preloadpath = NULL; |
| 1221 | Int* vg_malloc_redzonep; |
| 1222 | |
| 1223 | // XXX: allowing full paths for --tool option -- does it make sense? |
| 1224 | // Doesn't allow for vgpreload_<tool>.so. |
| 1225 | |
| 1226 | if (strchr(toolname, '/') != 0) { |
| 1227 | /* toolname contains '/', and so must be a pathname */ |
| 1228 | handle = dlopen(toolname, RTLD_NOW); |
| 1229 | } else { |
| 1230 | /* just try in the libdir */ |
| 1231 | snprintf(buf, len, "%s/vgskin_%s.so", VG_(libdir), toolname); |
| 1232 | handle = dlopen(buf, RTLD_NOW); |
| 1233 | |
| 1234 | if (handle != NULL) { |
| 1235 | snprintf(buf, len, "%s/vgpreload_%s.so", VG_(libdir), toolname); |
| 1236 | if (access(buf, R_OK) == 0) { |
| 1237 | preloadpath = strdup(buf); |
| 1238 | vg_assert(NULL != preloadpath); |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | ok = (NULL != handle); |
| 1244 | if (!ok) { |
| 1245 | fprintf(stderr, "Can't open tool \"%s\": %s\n", toolname, dlerror()); |
| 1246 | goto bad_load; |
| 1247 | } |
| 1248 | |
| 1249 | toolinfo = dlsym(handle, "vgSkin_tool_info"); |
| 1250 | ok = (NULL != toolinfo); |
| 1251 | if (!ok) { |
| 1252 | fprintf(stderr, "Tool \"%s\" doesn't define SK_(tool_info) - " |
| 1253 | "add VG_DETERMINE_INTERFACE_VERSION?\n", toolname); |
| 1254 | goto bad_load; |
| 1255 | } |
| 1256 | |
| 1257 | ok = (toolinfo->sizeof_ToolInfo == sizeof(*toolinfo) && |
| 1258 | toolinfo->interface_major_version == VG_CORE_INTERFACE_MAJOR_VERSION && |
| 1259 | toolinfo->sk_pre_clo_init != NULL); |
| 1260 | if (!ok) { |
| 1261 | fprintf(stderr, "Error:\n" |
| 1262 | " Tool and core interface versions do not match.\n" |
| 1263 | " Interface version used by core is: %d.%d (size %d)\n" |
| 1264 | " Interface version used by tool is: %d.%d (size %d)\n" |
| 1265 | " The major version numbers must match.\n", |
| 1266 | VG_CORE_INTERFACE_MAJOR_VERSION, |
| 1267 | VG_CORE_INTERFACE_MINOR_VERSION, |
| 1268 | sizeof(*toolinfo), |
| 1269 | toolinfo->interface_major_version, |
| 1270 | toolinfo->interface_minor_version, |
| 1271 | toolinfo->sizeof_ToolInfo); |
| 1272 | fprintf(stderr, " You need to at least recompile, and possibly update,\n"); |
| 1273 | if (VG_CORE_INTERFACE_MAJOR_VERSION > toolinfo->interface_major_version) |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1274 | fprintf(stderr, " your tool to work with this version of Valgrind.\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1275 | else |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1276 | fprintf(stderr, " your version of Valgrind to work with this tool.\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1277 | goto bad_load; |
| 1278 | } |
| 1279 | |
| 1280 | // Set redzone size for V's allocator |
| 1281 | vg_malloc_redzonep = dlsym(handle, STR(VG_(vg_malloc_redzone_szB))); |
| 1282 | if ( NULL != vg_malloc_redzonep ) { |
| 1283 | VG_(vg_malloc_redzone_szB) = *vg_malloc_redzonep; |
| 1284 | } |
| 1285 | |
| 1286 | vg_assert(NULL != handle && NULL != toolinfo); |
| 1287 | *handle_out = handle; |
| 1288 | *toolinfo_out = toolinfo; |
| 1289 | *preloadpath_out = preloadpath; |
| 1290 | return; |
| 1291 | |
| 1292 | |
| 1293 | bad_load: |
| 1294 | if (handle != NULL) |
| 1295 | dlclose(handle); |
| 1296 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1297 | fprintf(stderr, "valgrind: couldn't load tool\n"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1298 | list_tools(); |
| 1299 | exit(127); |
| 1300 | } |
| 1301 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1302 | |
| 1303 | /*====================================================================*/ |
| 1304 | /*=== Command line errors ===*/ |
| 1305 | /*====================================================================*/ |
| 1306 | |
| 1307 | static void abort_msg ( void ) |
| 1308 | { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1309 | VG_(clo_log_to) = VgLogTo_Fd; |
| 1310 | VG_(clo_log_fd) = 2; /* stderr */ |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | void VG_(bad_option) ( Char* opt ) |
| 1314 | { |
| 1315 | abort_msg(); |
| 1316 | VG_(printf)("valgrind: Bad option `%s'; aborting.\n", opt); |
| 1317 | VG_(printf)("valgrind: Use --help for more information.\n"); |
| 1318 | VG_(exit)(1); |
| 1319 | } |
| 1320 | |
| 1321 | static void missing_tool_option ( void ) |
| 1322 | { |
| 1323 | abort_msg(); |
| 1324 | VG_(printf)("valgrind: Missing --tool option\n"); |
| 1325 | list_tools(); |
| 1326 | VG_(printf)("valgrind: Use --help for more information.\n"); |
| 1327 | VG_(exit)(1); |
| 1328 | } |
| 1329 | |
| 1330 | static void missing_prog ( void ) |
| 1331 | { |
| 1332 | abort_msg(); |
| 1333 | VG_(printf)("valgrind: no program specified\n"); |
| 1334 | VG_(printf)("valgrind: Use --help for more information.\n"); |
| 1335 | VG_(exit)(1); |
| 1336 | } |
| 1337 | |
| 1338 | static void config_error ( Char* msg ) |
| 1339 | { |
| 1340 | abort_msg(); |
| 1341 | VG_(printf)("valgrind: Startup or configuration error:\n %s\n", msg); |
| 1342 | VG_(printf)("valgrind: Unable to start up properly. Giving up.\n"); |
| 1343 | VG_(exit)(1); |
| 1344 | } |
| 1345 | |
| 1346 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1347 | /*====================================================================*/ |
| 1348 | /*=== Loading the client ===*/ |
| 1349 | /*====================================================================*/ |
| 1350 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1351 | static void load_client(char* cl_argv[], const char* exec, Int need_help, |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1352 | /*out*/struct exeinfo* info, /*out*/Addr* client_eip) |
| 1353 | { |
| 1354 | // If they didn't specify an executable with --exec, and didn't specify |
| 1355 | // --help, then use client argv[0] (searching $PATH if necessary). |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1356 | if (NULL == exec && !need_help) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1357 | if (cl_argv[0] == NULL || |
| 1358 | ( NULL == (exec = find_executable(cl_argv[0])) ) ) |
| 1359 | { |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1360 | missing_prog(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | info->map_base = VG_(client_mapbase); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1365 | info->exe_base = VG_(client_base); |
| 1366 | info->exe_end = VG_(client_end); |
| 1367 | info->argv = cl_argv; |
| 1368 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1369 | if (need_help) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1370 | VG_(clexecfd) = -1; |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 1371 | // Set the minimal number of entries in 'info' to continue. |
nethercote | d6a5687 | 2004-07-26 15:32:47 +0000 | [diff] [blame] | 1372 | info->interp_name = NULL; |
| 1373 | info->interp_args = NULL; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1374 | } else { |
| 1375 | Int ret; |
| 1376 | VG_(clexecfd) = VG_(open)(exec, O_RDONLY, VKI_S_IRUSR); |
| 1377 | ret = do_exec(exec, info); |
| 1378 | if (ret != 0) { |
nethercote | ea147e7 | 2004-07-26 15:43:57 +0000 | [diff] [blame] | 1379 | fprintf(stderr, "valgrind: do_exec(%s) failed: %s\n", |
| 1380 | exec, strerror(ret)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1381 | exit(127); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | /* Copy necessary bits of 'info' that were filled in */ |
| 1386 | *client_eip = info->init_eip; |
| 1387 | VG_(brk_base) = VG_(brk_limit) = info->brkbase; |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | /*====================================================================*/ |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1392 | /*=== Command-line: variables, processing, etc ===*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1393 | /*====================================================================*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1394 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1395 | /* Define, and set defaults. */ |
| 1396 | Bool VG_(clo_error_limit) = True; |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1397 | Bool VG_(clo_db_attach) = False; |
| 1398 | Char* VG_(clo_db_command) = VG_CLO_DEFAULT_DBCOMMAND; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1399 | Bool VG_(clo_gen_suppressions) = False; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1400 | Int VG_(clo_sanity_level) = 1; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1401 | Int VG_(clo_verbosity) = 1; |
| 1402 | Bool VG_(clo_demangle) = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1403 | Bool VG_(clo_trace_children) = False; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1404 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 1405 | /* See big comment in core.h for meaning of these three. |
nethercote | e173069 | 2003-11-20 10:38:07 +0000 | [diff] [blame] | 1406 | fd is initially stdout, for --help, but gets moved to stderr by default |
| 1407 | immediately afterwards. */ |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1408 | VgLogTo VG_(clo_log_to) = VgLogTo_Fd; |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1409 | Int VG_(clo_log_fd) = 1; |
| 1410 | Char* VG_(clo_log_name) = NULL; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1411 | |
thughes | 6233a38 | 2004-08-21 11:10:44 +0000 | [diff] [blame] | 1412 | Bool VG_(clo_time_stamp) = False; |
| 1413 | |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 1414 | Int VG_(clo_input_fd) = 0; /* stdin */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1415 | Int VG_(clo_n_suppressions) = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1416 | Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1417 | Bool VG_(clo_profile) = False; |
| 1418 | Bool VG_(clo_single_step) = False; |
| 1419 | Bool VG_(clo_optimise) = True; |
| 1420 | UChar VG_(clo_trace_codegen) = 0; // 00000000b |
| 1421 | Bool VG_(clo_trace_syscalls) = False; |
| 1422 | Bool VG_(clo_trace_signals) = False; |
| 1423 | Bool VG_(clo_trace_symtab) = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1424 | Bool VG_(clo_trace_sched) = False; |
| 1425 | Int VG_(clo_trace_pthread_level) = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1426 | Int VG_(clo_dump_error) = 0; |
| 1427 | Int VG_(clo_backtrace_size) = 4; |
| 1428 | Char* VG_(clo_weird_hacks) = NULL; |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1429 | Bool VG_(clo_run_libc_freeres) = True; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame] | 1430 | Bool VG_(clo_track_fds) = False; |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1431 | Bool VG_(clo_chain_bb) = True; |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 1432 | Bool VG_(clo_show_below_main) = False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1433 | Bool VG_(clo_pointercheck) = True; |
fitzhardinge | 462f4f9 | 2003-12-18 02:10:54 +0000 | [diff] [blame] | 1434 | Bool VG_(clo_branchpred) = False; |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 1435 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1436 | static Bool VG_(clo_wait_for_gdb) = False; |
| 1437 | |
| 1438 | /* If we're doing signal routing, poll for signals every 50mS by |
| 1439 | default. */ |
| 1440 | Int VG_(clo_signal_polltime) = 50; |
| 1441 | |
| 1442 | /* These flags reduce thread wakeup latency on syscall completion and |
| 1443 | signal delivery, respectively. The downside is possible unfairness. */ |
| 1444 | Bool VG_(clo_lowlat_syscalls) = False; /* low-latency syscalls */ |
| 1445 | Bool VG_(clo_lowlat_signals) = False; /* low-latency signals */ |
| 1446 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1447 | |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1448 | void usage ( Bool debug_help ) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1449 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1450 | Char* usage1 = |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1451 | "usage: valgrind --tool=<toolname> [options] prog-and-args\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1452 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 1453 | " common user options for all Valgrind tools, with defaults in [ ]:\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 1454 | " --tool=<name> use the Valgrind tool named <name>\n" |
nethercote | a76368b | 2004-06-16 11:56:29 +0000 | [diff] [blame] | 1455 | " -h --help show this message\n" |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1456 | " --help-debug show this message, plus debugging options\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1457 | " --version show version\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1458 | " -q --quiet run silently; only print error msgs\n" |
| 1459 | " -v --verbose be more verbose, incl counts of errors\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 1460 | " --trace-children=no|yes Valgrind-ise child processes? [no]\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 1461 | " --track-fds=no|yes track open file descriptors? [no]\n" |
thughes | 6233a38 | 2004-08-21 11:10:44 +0000 | [diff] [blame] | 1462 | " --time-stamp=no|yes add timestamps to log messages? [no]\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 1463 | "\n" |
| 1464 | " uncommon user options for all Valgrind tools:\n" |
nethercote | 0d58850 | 2004-06-21 13:27:11 +0000 | [diff] [blame] | 1465 | " --run-libc-freeres=no|yes free up glibc memory at exit? [yes]\n" |
| 1466 | " --weird-hacks=hack1,hack2,... recognised hacks: lax-ioctls [none]\n" |
| 1467 | " --signal-polltime=<time> signal poll period (mS) for older kernels [50]\n" |
| 1468 | " --lowlat-signals=no|yes improve thread signal wake-up latency [no]\n" |
| 1469 | " --lowlat-syscalls=no|yes improve thread syscall wake-up latency [no]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1470 | " --pointercheck=no|yes enforce client address space limits [yes]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1471 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 1472 | " user options for Valgrind tools that report errors:\n" |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1473 | " --log-fd=<number> log messages to file descriptor [2=stderr]\n" |
| 1474 | " --log-file=<file> log messages to <file>.pid<pid>\n" |
| 1475 | " --log-socket=ipaddr:port log messages to socket ipaddr:port\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 1476 | " --demangle=no|yes automatically demangle C++ names? [yes]\n" |
| 1477 | " --num-callers=<number> show <num> callers in stack traces [4]\n" |
| 1478 | " --error-limit=no|yes stop showing new errors if too many? [yes]\n" |
| 1479 | " --show-below-main=no|yes continue stack traces below main() [no]\n" |
| 1480 | " --suppressions=<filename> suppress errors described in <filename>\n" |
| 1481 | " --gen-suppressions=no|yes print suppressions for errors detected [no]\n" |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1482 | " --db-attach=no|yes start debugger when errors detected? [no]\n" |
| 1483 | " --db-command=<command> command to start debugger [gdb -nw %%f %%p]\n" |
| 1484 | " --input-fd=<number> file descriptor for input [0=stdin]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1485 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1486 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1487 | Char* usage2 = |
| 1488 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 1489 | " debugging options for all Valgrind tools:\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1490 | " --sanity-level=<number> level of sanity checking to do [1]\n" |
| 1491 | " --single-step=no|yes translate each instr separately? [no]\n" |
| 1492 | " --optimise=no|yes improve intermediate code? [yes]\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 1493 | " --profile=no|yes profile? (tool must be built for it) [no]\n" |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1494 | " --chain-bb=no|yes do basic-block chaining? [yes]\n" |
fitzhardinge | 462f4f9 | 2003-12-18 02:10:54 +0000 | [diff] [blame] | 1495 | " --branchpred=yes|no generate branch prediction hints [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1496 | " --trace-codegen=<XXXXX> show generated code? (X = 0|1) [00000]\n" |
| 1497 | " --trace-syscalls=no|yes show all system calls? [no]\n" |
| 1498 | " --trace-signals=no|yes show signal handling details? [no]\n" |
| 1499 | " --trace-symtab=no|yes show symbol table details? [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1500 | " --trace-sched=no|yes show thread scheduler details? [no]\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1501 | " --trace-pthread=none|some|all show pthread event details? [none]\n" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1502 | " --wait-for-gdb=yes|no pause on startup to wait for gdb attach\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1503 | "\n" |
nethercote | 2b0793f | 2003-12-02 10:41:18 +0000 | [diff] [blame] | 1504 | " debugging options for Valgrind tools that report errors\n" |
| 1505 | " --dump-error=<number> show translation for basic block associated\n" |
| 1506 | " with <number>'th error context [0=show none]\n" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1507 | "\n"; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1508 | |
| 1509 | Char* usage3 = |
| 1510 | "\n" |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1511 | " Extra options read from ~/.valgrindrc, $VALGRIND_OPTS, ./.valgrindrc\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1512 | "\n" |
nethercote | 08fa9a7 | 2004-07-16 17:44:00 +0000 | [diff] [blame] | 1513 | " Valgrind is Copyright (C) 2000-2004 Julian Seward et al.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1514 | " and licensed under the GNU General Public License, version 2.\n" |
| 1515 | " Bug reports, feedback, admiration, abuse, etc, to: %s.\n" |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1516 | "\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 1517 | " Tools are copyright and licensed by their authors. See each\n" |
| 1518 | " tool's start-up message for more information.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1519 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1520 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1521 | VG_(printf)(usage1); |
| 1522 | if (VG_(details).name) { |
| 1523 | VG_(printf)(" user options for %s:\n", VG_(details).name); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1524 | if (VG_(needs).command_line_options) |
| 1525 | SK_(print_usage)(); |
| 1526 | else |
| 1527 | VG_(printf)(" (none)\n"); |
| 1528 | } |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1529 | if (debug_help) { |
| 1530 | VG_(printf)(usage2); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1531 | |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1532 | if (VG_(details).name) { |
| 1533 | VG_(printf)(" debugging options for %s:\n", VG_(details).name); |
| 1534 | |
| 1535 | if (VG_(needs).command_line_options) |
| 1536 | SK_(print_debug_usage)(); |
| 1537 | else |
| 1538 | VG_(printf)(" (none)\n"); |
| 1539 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1540 | } |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 1541 | VG_(printf)(usage3, VG_BUGS_TO); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1542 | VG_(exit)(0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1543 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1544 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1545 | static void pre_process_cmd_line_options |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1546 | ( Int* need_help, const char** tool, const char** exec ) |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 1547 | { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1548 | UInt i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1549 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1550 | /* parse the options we have (only the options we care about now) */ |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1551 | for (i = 1; i < vg_argc; i++) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1552 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1553 | if (strcmp(vg_argv[i], "--version") == 0) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1554 | printf("valgrind-" VERSION "\n"); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1555 | exit(0); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1556 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1557 | } else if (VG_CLO_STREQ(vg_argv[i], "--help") || |
| 1558 | VG_CLO_STREQ(vg_argv[i], "-h")) { |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1559 | *need_help = 1; |
| 1560 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1561 | } else if (VG_CLO_STREQ(vg_argv[i], "--help-debug")) { |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 1562 | *need_help = 2; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1563 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1564 | } else if (VG_CLO_STREQN(7, vg_argv[i], "--tool=") || |
| 1565 | VG_CLO_STREQN(7, vg_argv[i], "--skin=")) { |
| 1566 | *tool = &vg_argv[i][7]; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1567 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1568 | } else if (VG_CLO_STREQN(7, vg_argv[i], "--exec=")) { |
| 1569 | *exec = &vg_argv[i][7]; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1573 | /* If no tool specified, can act appropriately without loading tool */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1574 | if (*tool == NULL) { |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1575 | if (0 == *need_help) { |
| 1576 | // neither --tool nor --help/--help-debug specified |
| 1577 | missing_tool_option(); |
| 1578 | } else { |
| 1579 | // Give help message, without any tool-specific help |
| 1580 | usage(/*help-debug?*/2 == *need_help); |
| 1581 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 1585 | static void process_cmd_line_options( UInt* client_auxv, const char* toolname ) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1586 | { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1587 | Int i, eventually_log_fd; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1588 | Int *auxp; |
| 1589 | Int toolname_len = VG_(strlen)(toolname); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1590 | |
nethercote | e173069 | 2003-11-20 10:38:07 +0000 | [diff] [blame] | 1591 | /* log to stderr by default, but usage message goes to stdout */ |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1592 | eventually_log_fd = 2; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1593 | |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1594 | /* Check for sane path in ./configure --prefix=... */ |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1595 | if (VG_LIBDIR[0] != '/') |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1596 | config_error("Please use absolute paths in " |
| 1597 | "./configure --prefix=... or --libdir=..."); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 1598 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1599 | for (auxp = client_auxv; auxp[0] != VKI_AT_NULL; auxp += 2) { |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1600 | switch(auxp[0]) { |
| 1601 | case VKI_AT_SYSINFO: |
fitzhardinge | 9236079 | 2003-12-24 10:11:11 +0000 | [diff] [blame] | 1602 | auxp[1] = (Int)(VG_(client_trampoline_code) + VG_(tramp_syscall_offset)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1603 | break; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1604 | } |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1605 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1606 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1607 | for (i = 1; i < vg_argc; i++) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1608 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1609 | Char* arg = vg_argv[i]; |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 1610 | Char* colon = arg; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1611 | |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 1612 | /* Look for a colon in the switch name */ |
| 1613 | while (*colon && *colon != ':' && *colon != '=') |
| 1614 | colon++; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1615 | |
| 1616 | /* Look for matching "--toolname:foo" */ |
thughes | 3bfd5a0 | 2004-07-18 08:05:44 +0000 | [diff] [blame] | 1617 | if (*colon == ':') { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1618 | if (VG_CLO_STREQN(2, arg, "--") && |
| 1619 | VG_CLO_STREQN(toolname_len, arg+2, toolname) && |
| 1620 | VG_CLO_STREQN(1, arg+2+toolname_len, ":")) |
| 1621 | { |
| 1622 | // prefix matches, convert "--toolname:foo" to "--foo" |
| 1623 | if (0) |
| 1624 | VG_(printf)("tool-specific arg: %s\n", arg); |
| 1625 | arg += toolname_len + 1; |
| 1626 | arg[0] = '-'; |
| 1627 | arg[1] = '-'; |
| 1628 | |
| 1629 | } else { |
| 1630 | // prefix doesn't match, skip to next arg |
| 1631 | continue; |
| 1632 | } |
| 1633 | } |
| 1634 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1635 | /* Ignore these options - they've already been handled */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1636 | if (VG_CLO_STREQN(7, arg, "--tool=") || |
| 1637 | VG_CLO_STREQN(7, arg, "--skin=")) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1638 | continue; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1639 | if (VG_CLO_STREQN(7, arg, "--exec=")) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1640 | continue; |
| 1641 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1642 | if ( VG_CLO_STREQ(arg, "--")) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1643 | continue; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1644 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1645 | else if (VG_CLO_STREQ(arg, "-v") || |
| 1646 | VG_CLO_STREQ(arg, "--verbose")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1647 | VG_(clo_verbosity)++; |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1648 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1649 | else if (VG_CLO_STREQ(arg, "-q") || |
| 1650 | VG_CLO_STREQ(arg, "--quiet")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1651 | VG_(clo_verbosity)--; |
| 1652 | |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1653 | else VG_BOOL_CLO("--branchpred", VG_(clo_branchpred)) |
| 1654 | else VG_BOOL_CLO("--chain-bb", VG_(clo_chain_bb)) |
| 1655 | else VG_BOOL_CLO("--db-attach", VG_(clo_db_attach)) |
| 1656 | else VG_BOOL_CLO("--demangle", VG_(clo_demangle)) |
| 1657 | else VG_BOOL_CLO("--error-limit", VG_(clo_error_limit)) |
| 1658 | else VG_BOOL_CLO("--gen-suppressions", VG_(clo_gen_suppressions)) |
| 1659 | else VG_BOOL_CLO("--lowlat-signals", VG_(clo_lowlat_signals)) |
| 1660 | else VG_BOOL_CLO("--lowlat-syscalls", VG_(clo_lowlat_syscalls)) |
| 1661 | else VG_BOOL_CLO("--optimise", VG_(clo_optimise)) |
| 1662 | else VG_BOOL_CLO("--pointercheck", VG_(clo_pointercheck)) |
| 1663 | else VG_BOOL_CLO("--profile", VG_(clo_profile)) |
| 1664 | else VG_BOOL_CLO("--run-libc-freeres", VG_(clo_run_libc_freeres)) |
| 1665 | else VG_BOOL_CLO("--show-below-main", VG_(clo_show_below_main)) |
| 1666 | else VG_BOOL_CLO("--single-step", VG_(clo_single_step)) |
thughes | 6233a38 | 2004-08-21 11:10:44 +0000 | [diff] [blame] | 1667 | else VG_BOOL_CLO("--time-stamp", VG_(clo_time_stamp)) |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1668 | else VG_BOOL_CLO("--track-fds", VG_(clo_track_fds)) |
| 1669 | else VG_BOOL_CLO("--trace-children", VG_(clo_trace_children)) |
| 1670 | else VG_BOOL_CLO("--trace-sched", VG_(clo_trace_sched)) |
| 1671 | else VG_BOOL_CLO("--trace-signals", VG_(clo_trace_signals)) |
| 1672 | else VG_BOOL_CLO("--trace-symtab", VG_(clo_trace_symtab)) |
| 1673 | else VG_BOOL_CLO("--trace-syscalls", VG_(clo_trace_syscalls)) |
| 1674 | else VG_BOOL_CLO("--wait-for-gdb", VG_(clo_wait_for_gdb)) |
sewardj | 72f98ff | 2002-06-13 17:23:38 +0000 | [diff] [blame] | 1675 | |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1676 | else VG_STR_CLO ("--db-command", VG_(clo_db_command)) |
| 1677 | else VG_STR_CLO ("--weird-hacks", VG_(clo_weird_hacks)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1678 | |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1679 | else VG_NUM_CLO ("--dump-error", VG_(clo_dump_error)) |
| 1680 | else VG_NUM_CLO ("--input-fd", VG_(clo_input_fd)) |
| 1681 | else VG_NUM_CLO ("--sanity-level", VG_(clo_sanity_level)) |
| 1682 | else VG_NUM_CLO ("--signalÂpolltime", VG_(clo_signal_polltime)) |
| 1683 | else VG_BNUM_CLO("--num-callers", VG_(clo_backtrace_size), 1, |
| 1684 | VG_DEEPEST_BACKTRACE) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1685 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1686 | // for backwards compatibility, replaced by --log-fd |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1687 | else if (VG_CLO_STREQN(13, arg, "--logfile-fd=")) { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1688 | VG_(clo_log_to) = VgLogTo_Fd; |
| 1689 | VG_(clo_log_name) = NULL; |
| 1690 | eventually_log_fd = (Int)VG_(atoll)(&arg[13]); |
| 1691 | } |
| 1692 | else if (VG_CLO_STREQN(9, arg, "--log-fd=")) { |
| 1693 | VG_(clo_log_to) = VgLogTo_Fd; |
| 1694 | VG_(clo_log_name) = NULL; |
| 1695 | eventually_log_fd = (Int)VG_(atoll)(&arg[9]); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1698 | // for backwards compatibility, replaced by --log-file |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1699 | else if (VG_CLO_STREQN(10, arg, "--logfile=")) { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1700 | VG_(clo_log_to) = VgLogTo_File; |
| 1701 | VG_(clo_log_name) = &arg[10]; |
| 1702 | } |
| 1703 | else if (VG_CLO_STREQN(11, arg, "--log-file=")) { |
| 1704 | VG_(clo_log_to) = VgLogTo_File; |
| 1705 | VG_(clo_log_name) = &arg[11]; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1706 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1707 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1708 | // for backwards compatibility, replaced by --log-socket |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1709 | else if (VG_CLO_STREQN(12, arg, "--logsocket=")) { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1710 | VG_(clo_log_to) = VgLogTo_Socket; |
| 1711 | VG_(clo_log_name) = &arg[12]; |
| 1712 | } |
| 1713 | else if (VG_CLO_STREQN(13, arg, "--log-socket=")) { |
| 1714 | VG_(clo_log_to) = VgLogTo_Socket; |
| 1715 | VG_(clo_log_name) = &arg[13]; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1718 | else if (VG_CLO_STREQN(15, arg, "--suppressions=")) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1719 | if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1720 | VG_(message)(Vg_UserMsg, "Too many suppression files specified."); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1721 | VG_(message)(Vg_UserMsg, |
| 1722 | "Increase VG_CLO_MAX_SFILES and recompile."); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1723 | VG_(bad_option)(arg); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1724 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1725 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = &arg[15]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1726 | VG_(clo_n_suppressions)++; |
| 1727 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1728 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1729 | /* "vwxyz" --> 000zyxwv (binary) */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1730 | else if (VG_CLO_STREQN(16, arg, "--trace-codegen=")) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1731 | Int j; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1732 | char* opt = & arg[16]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1733 | |
| 1734 | if (5 != VG_(strlen)(opt)) { |
| 1735 | VG_(message)(Vg_UserMsg, |
| 1736 | "--trace-codegen argument must have 5 digits"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1737 | VG_(bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1738 | } |
| 1739 | for (j = 0; j < 5; j++) { |
| 1740 | if ('0' == opt[j]) { /* do nothing */ } |
| 1741 | else if ('1' == opt[j]) VG_(clo_trace_codegen) |= (1 << j); |
| 1742 | else { |
| 1743 | VG_(message)(Vg_UserMsg, "--trace-codegen argument can only " |
| 1744 | "contain 0s and 1s"); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1745 | VG_(bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1749 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1750 | else if (VG_CLO_STREQ(arg, "--trace-pthread=none")) |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1751 | VG_(clo_trace_pthread_level) = 0; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1752 | else if (VG_CLO_STREQ(arg, "--trace-pthread=some")) |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1753 | VG_(clo_trace_pthread_level) = 1; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1754 | else if (VG_CLO_STREQ(arg, "--trace-pthread=all")) |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1755 | VG_(clo_trace_pthread_level) = 2; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1756 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1757 | else if ( ! VG_(needs).command_line_options |
| 1758 | || ! SK_(process_cmd_line_option)(arg) ) { |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1759 | VG_(bad_option)(arg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1760 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 1763 | // Check various option values |
| 1764 | |
njn | f9ebf67 | 2003-05-12 21:41:30 +0000 | [diff] [blame] | 1765 | if (VG_(clo_verbosity) < 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1766 | VG_(clo_verbosity) = 0; |
| 1767 | |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1768 | if (VG_(clo_db_attach) && VG_(clo_trace_children)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1769 | VG_(message)(Vg_UserMsg, ""); |
| 1770 | VG_(message)(Vg_UserMsg, |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1771 | "--db-attach=yes conflicts with --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1772 | VG_(message)(Vg_UserMsg, |
| 1773 | "Please choose one or the other, but not both."); |
nethercote | 04d0fbc | 2004-01-26 16:48:06 +0000 | [diff] [blame] | 1774 | VG_(bad_option)("--db-attach=yes and --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1777 | /* Set up logging now. After this is done, VG_(clo_log_fd) |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1778 | should be connected to whatever sink has been selected, and we |
| 1779 | indiscriminately chuck stuff into it without worrying what the |
| 1780 | nature of it is. Oh the wonder of Unix streams. */ |
| 1781 | |
nethercote | e173069 | 2003-11-20 10:38:07 +0000 | [diff] [blame] | 1782 | /* So far we should be still attached to stdout, so we can show on |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1783 | the terminal any problems to do with processing command line |
| 1784 | opts. */ |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1785 | vg_assert(VG_(clo_log_fd) == 1 /* stdout */); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1786 | vg_assert(VG_(logging_to_filedes) == True); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1787 | |
| 1788 | switch (VG_(clo_log_to)) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1789 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1790 | case VgLogTo_Fd: |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1791 | vg_assert(VG_(clo_log_name) == NULL); |
| 1792 | VG_(clo_log_fd) = eventually_log_fd; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1793 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1794 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1795 | case VgLogTo_File: { |
| 1796 | Char logfilename[1000]; |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1797 | Int seq = 0; |
| 1798 | Int pid = VG_(getpid)(); |
| 1799 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1800 | vg_assert(VG_(clo_log_name) != NULL); |
| 1801 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1802 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 1803 | for (;;) { |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1804 | if (seq == 0) |
| 1805 | VG_(sprintf)(logfilename, "%s.pid%d", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1806 | VG_(clo_log_name), pid ); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1807 | else |
| 1808 | VG_(sprintf)(logfilename, "%s.pid%d.%d", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1809 | VG_(clo_log_name), pid, seq ); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1810 | seq++; |
| 1811 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1812 | eventually_log_fd |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1813 | = VG_(open)(logfilename, |
| 1814 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC, |
| 1815 | VKI_S_IRUSR|VKI_S_IWUSR); |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1816 | if (eventually_log_fd >= 0) { |
| 1817 | VG_(clo_log_fd) = VG_(safe_fd)(eventually_log_fd); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1818 | break; |
| 1819 | } else { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1820 | if (eventually_log_fd != -VKI_EEXIST) { |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1821 | VG_(message)(Vg_UserMsg, |
| 1822 | "Can't create/open log file `%s.pid%d'; giving up!", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1823 | VG_(clo_log_name), pid); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1824 | VG_(bad_option)( |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1825 | "--log-file=<file> didn't work out for some reason."); |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1826 | break; |
| 1827 | } |
| 1828 | } |
| 1829 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1830 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | case VgLogTo_Socket: { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1834 | vg_assert(VG_(clo_log_name) != NULL); |
| 1835 | vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */ |
| 1836 | eventually_log_fd = VG_(connect_via_socket)( VG_(clo_log_name) ); |
| 1837 | if (eventually_log_fd == -1) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1838 | VG_(message)(Vg_UserMsg, |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1839 | "Invalid --log-socket=ipaddr or --log-socket=ipaddr:port spec"); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1840 | VG_(message)(Vg_UserMsg, |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1841 | "of `%s'; giving up!", VG_(clo_log_name) ); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1842 | VG_(bad_option)( |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1843 | "--log-socket="); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1844 | } |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1845 | if (eventually_log_fd == -2) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1846 | VG_(message)(Vg_UserMsg, |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1847 | "valgrind: failed to connect to logging server `%s'.", |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1848 | VG_(clo_log_name) ); |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1849 | VG_(message)(Vg_UserMsg, |
| 1850 | "Log messages will sent to stderr instead." ); |
| 1851 | VG_(message)(Vg_UserMsg, |
| 1852 | "" ); |
| 1853 | /* We don't change anything here. */ |
| 1854 | } else { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1855 | vg_assert(eventually_log_fd > 0); |
| 1856 | VG_(clo_log_fd) = eventually_log_fd; |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1857 | VG_(logging_to_filedes) = False; |
| 1858 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1859 | break; |
| 1860 | } |
| 1861 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1864 | /* Move log_fd into the safe range, so it doesn't conflict with any app fds */ |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 1865 | eventually_log_fd = VG_(fcntl)(VG_(clo_log_fd), VKI_F_DUPFD, VG_(fd_hard_limit)); |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1866 | if (eventually_log_fd < 0) |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1867 | VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd into safe range"); |
| 1868 | else { |
nethercote | f854867 | 2004-06-21 12:42:35 +0000 | [diff] [blame] | 1869 | VG_(clo_log_fd) = eventually_log_fd; |
| 1870 | VG_(fcntl)(VG_(clo_log_fd), VKI_F_SETFD, VKI_FD_CLOEXEC); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1873 | /* Ok, the logging sink is running now. Print a suitable preamble. |
| 1874 | If logging to file or a socket, write details of parent PID and |
| 1875 | command line args, to help people trying to interpret the |
| 1876 | results of a run which encompasses multiple processes. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1877 | |
sewardj | 83adf41 | 2002-05-01 01:25:45 +0000 | [diff] [blame] | 1878 | if (VG_(clo_verbosity > 0)) { |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1879 | /* Tool details */ |
nethercote | a131bb8 | 2004-09-06 15:34:37 +0000 | [diff] [blame] | 1880 | VG_(message)(Vg_UserMsg, "%s%s%s, %s for %s.", |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1881 | VG_(details).name, |
| 1882 | NULL == VG_(details).version ? "" : "-", |
| 1883 | NULL == VG_(details).version |
| 1884 | ? (Char*)"" : VG_(details).version, |
nethercote | a131bb8 | 2004-09-06 15:34:37 +0000 | [diff] [blame] | 1885 | VG_(details).description, |
| 1886 | VG_PLATFORM); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1887 | VG_(message)(Vg_UserMsg, "%s", VG_(details).copyright_author); |
sewardj | 3b2736a | 2002-03-24 12:18:35 +0000 | [diff] [blame] | 1888 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1889 | /* Core details */ |
| 1890 | VG_(message)(Vg_UserMsg, |
nethercote | a131bb8 | 2004-09-06 15:34:37 +0000 | [diff] [blame] | 1891 | "Using valgrind-%s, a program supervision framework for %s.", |
| 1892 | VERSION, VG_PLATFORM); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1893 | VG_(message)(Vg_UserMsg, |
nethercote | 08fa9a7 | 2004-07-16 17:44:00 +0000 | [diff] [blame] | 1894 | "Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al."); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
nethercote | c1e395d | 2003-11-10 13:26:49 +0000 | [diff] [blame] | 1897 | if (VG_(clo_verbosity) > 0 && VG_(clo_log_to) != VgLogTo_Fd) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1898 | VG_(message)(Vg_UserMsg, ""); |
| 1899 | VG_(message)(Vg_UserMsg, |
| 1900 | "My PID = %d, parent PID = %d. Prog and args are:", |
| 1901 | VG_(getpid)(), VG_(getppid)() ); |
| 1902 | for (i = 0; i < VG_(client_argc); i++) |
| 1903 | VG_(message)(Vg_UserMsg, " %s", VG_(client_argv)[i]); |
| 1904 | } |
| 1905 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1906 | if (VG_(clo_verbosity) > 1) { |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1907 | Int fd; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1908 | if (VG_(clo_log_to) != VgLogTo_Fd) |
| 1909 | VG_(message)(Vg_UserMsg, ""); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1910 | VG_(message)(Vg_UserMsg, "Valgrind library directory: %s", VG_(libdir)); |
njn | 86dc2bc | 2003-09-09 07:26:21 +0000 | [diff] [blame] | 1911 | VG_(message)(Vg_UserMsg, "Command line"); |
| 1912 | for (i = 0; i < VG_(client_argc); i++) |
| 1913 | VG_(message)(Vg_UserMsg, " %s", VG_(client_argv)[i]); |
| 1914 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1915 | VG_(message)(Vg_UserMsg, "Startup, with flags:"); |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1916 | for (i = 1; i < vg_argc; i++) { |
| 1917 | VG_(message)(Vg_UserMsg, " %s", vg_argv[i]); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1918 | } |
nethercote | a70f735 | 2004-04-18 12:08:46 +0000 | [diff] [blame] | 1919 | |
| 1920 | VG_(message)(Vg_UserMsg, "Contents of /proc/version:"); |
| 1921 | fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 ); |
| 1922 | if (fd < 0) { |
| 1923 | VG_(message)(Vg_UserMsg, " can't open /proc/version"); |
| 1924 | } else { |
| 1925 | #define BUF_LEN 256 |
| 1926 | Char version_buf[BUF_LEN]; |
| 1927 | Int n = VG_(read) ( fd, version_buf, BUF_LEN ); |
| 1928 | vg_assert(n <= 256); |
| 1929 | if (n > 0) { |
| 1930 | version_buf[n-1] = '\0'; |
| 1931 | VG_(message)(Vg_UserMsg, " %s", version_buf); |
| 1932 | } else { |
| 1933 | VG_(message)(Vg_UserMsg, " (empty?)"); |
| 1934 | } |
| 1935 | VG_(close)(fd); |
| 1936 | #undef BUF_LEN |
| 1937 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1940 | if (VG_(clo_n_suppressions) < VG_CLO_MAX_SFILES-1 && |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1941 | (VG_(needs).core_errors || VG_(needs).skin_errors)) { |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1942 | /* If there are no suppression files specified and the tool |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1943 | needs one, load the default */ |
| 1944 | static const Char default_supp[] = "default.supp"; |
| 1945 | Int len = VG_(strlen)(VG_(libdir)) + 1 + sizeof(default_supp); |
| 1946 | Char *buf = VG_(arena_malloc)(VG_AR_CORE, len); |
| 1947 | VG_(sprintf)(buf, "%s/%s", VG_(libdir), default_supp); |
| 1948 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = buf; |
| 1949 | VG_(clo_n_suppressions)++; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1950 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1951 | |
njn | 6a23053 | 2003-07-21 10:38:23 +0000 | [diff] [blame] | 1952 | if (VG_(clo_gen_suppressions) && |
| 1953 | !VG_(needs).core_errors && !VG_(needs).skin_errors) { |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 1954 | VG_(message)(Vg_UserMsg, |
| 1955 | "Can't use --gen-suppressions=yes with this tool,"); |
| 1956 | VG_(message)(Vg_UserMsg, |
| 1957 | "as it doesn't generate errors."); |
| 1958 | VG_(bad_option)("--gen-suppressions=yes"); |
njn | 6a23053 | 2003-07-21 10:38:23 +0000 | [diff] [blame] | 1959 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 1962 | // Build the string for VALGRINDCLO. |
| 1963 | Char* VG_(build_child_VALGRINDCLO)( Char* exename ) |
| 1964 | { |
| 1965 | /* If we're tracing the children, then we need to start it |
| 1966 | with our starter+arguments, which are copied into VALGRINDCLO, |
| 1967 | except the --exec= option is changed if present. |
| 1968 | */ |
| 1969 | Int i; |
| 1970 | Char *exec; |
| 1971 | Char *cp; |
| 1972 | Char *optvar; |
| 1973 | Int optlen, execlen; |
| 1974 | |
| 1975 | // All these allocated blocks are not free - because we're either |
| 1976 | // going to exec, or panic when we fail. |
| 1977 | |
| 1978 | // Create --exec= option: "--exec=<exename>" |
| 1979 | exec = VG_(arena_malloc)(VG_AR_CORE, |
| 1980 | VG_(strlen)( exename ) + 7/*--exec=*/ + 1/*\0*/); |
| 1981 | vg_assert(NULL != exec); |
| 1982 | VG_(sprintf)(exec, "--exec=%s", exename); |
| 1983 | |
| 1984 | // Allocate space for optvar (may overestimate by counting --exec twice, |
| 1985 | // no matter) |
| 1986 | optlen = 1; |
| 1987 | for (i = 0; i < vg_argc; i++) |
| 1988 | optlen += VG_(strlen)(vg_argv[i]) + 1; |
| 1989 | optlen += VG_(strlen)(exec)+1; |
| 1990 | optvar = VG_(arena_malloc)(VG_AR_CORE, optlen); |
| 1991 | |
| 1992 | // Copy all valgrind args except the old --exec (if present) |
| 1993 | // VG_CLO_SEP is the separator. |
| 1994 | cp = optvar; |
| 1995 | for (i = 1; i < vg_argc; i++) { |
| 1996 | Char *arg = vg_argv[i]; |
| 1997 | |
| 1998 | if (VG_(memcmp)(arg, "--exec=", 7) == 0) { |
| 1999 | // don't copy existing --exec= arg |
| 2000 | } else if (VG_(strcmp)(arg, "--") == 0) { |
| 2001 | // stop at "--" |
| 2002 | break; |
| 2003 | } else { |
| 2004 | // copy non "--exec" arg |
| 2005 | Int len = VG_(strlen)(arg); |
| 2006 | VG_(memcpy)(cp, arg, len); |
| 2007 | cp += len; |
| 2008 | *cp++ = VG_CLO_SEP; |
| 2009 | } |
| 2010 | } |
| 2011 | // Add the new --exec= option |
| 2012 | execlen = VG_(strlen)(exec); |
| 2013 | VG_(memcpy)(cp, exec, execlen); |
| 2014 | cp += execlen; |
| 2015 | *cp++ = VG_CLO_SEP; |
| 2016 | |
| 2017 | *cp = '\0'; |
| 2018 | |
| 2019 | return optvar; |
| 2020 | } |
| 2021 | |
| 2022 | // Build "/proc/self/fd/<execfd>". |
| 2023 | Char* VG_(build_child_exename)( void ) |
| 2024 | { |
| 2025 | Char* exename = VG_(arena_malloc)(VG_AR_CORE, 64); |
| 2026 | vg_assert(NULL != exename); |
| 2027 | VG_(sprintf)(exename, "/proc/self/fd/%d", vgexecfd); |
| 2028 | return exename; |
| 2029 | } |
| 2030 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2031 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2032 | /*====================================================================*/ |
| 2033 | /*=== File descriptor setup ===*/ |
| 2034 | /*====================================================================*/ |
| 2035 | |
| 2036 | static void setup_file_descriptors(void) |
| 2037 | { |
| 2038 | struct vki_rlimit rl; |
| 2039 | |
| 2040 | /* Get the current file descriptor limits. */ |
| 2041 | if (VG_(getrlimit)(VKI_RLIMIT_NOFILE, &rl) < 0) { |
| 2042 | rl.rlim_cur = 1024; |
| 2043 | rl.rlim_max = 1024; |
| 2044 | } |
| 2045 | |
| 2046 | /* Work out where to move the soft limit to. */ |
| 2047 | if (rl.rlim_cur + VG_N_RESERVED_FDS <= rl.rlim_max) { |
| 2048 | rl.rlim_cur = rl.rlim_cur + VG_N_RESERVED_FDS; |
| 2049 | } else { |
| 2050 | rl.rlim_cur = rl.rlim_max; |
| 2051 | } |
| 2052 | |
| 2053 | /* Reserve some file descriptors for our use. */ |
thughes | ad1c956 | 2004-06-26 11:27:52 +0000 | [diff] [blame] | 2054 | VG_(fd_soft_limit) = rl.rlim_cur - VG_N_RESERVED_FDS; |
| 2055 | VG_(fd_hard_limit) = rl.rlim_cur - VG_N_RESERVED_FDS; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2056 | |
| 2057 | /* Update the soft limit. */ |
| 2058 | VG_(setrlimit)(VKI_RLIMIT_NOFILE, &rl); |
| 2059 | |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 2060 | if (vgexecfd != -1) |
| 2061 | vgexecfd = VG_(safe_fd)( vgexecfd ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2062 | if (VG_(clexecfd) != -1) |
| 2063 | VG_(clexecfd) = VG_(safe_fd)( VG_(clexecfd) ); |
| 2064 | } |
| 2065 | |
| 2066 | |
| 2067 | /*====================================================================*/ |
nethercote | 1d44709 | 2004-02-01 17:29:59 +0000 | [diff] [blame] | 2068 | /*=== baseBlock: definition + setup ===*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2069 | /*====================================================================*/ |
| 2070 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2071 | Int VGOFF_(helper_undefined_instruction) = INVALID_OFFSET; |
| 2072 | |
| 2073 | /* MAX_NONCOMPACT_HELPERS can be increased easily. If MAX_COMPACT_HELPERS is |
| 2074 | * increased too much, they won't really be compact any more... */ |
| 2075 | #define MAX_COMPACT_HELPERS 8 |
| 2076 | #define MAX_NONCOMPACT_HELPERS 50 |
| 2077 | |
nethercote | 81f9a6f | 2004-08-03 15:45:46 +0000 | [diff] [blame] | 2078 | /* For storing tool-specific helpers, determined at runtime. The addr |
| 2079 | * and offset arrays together form a (addr, offset) map that allows a |
| 2080 | * helper's baseBlock offset to be computed from its address. It's done |
| 2081 | * like this so CCALLs can use the function address rather than having to |
| 2082 | * muck around with offsets. */ |
| 2083 | static UInt VG_(n_compact_helpers) = 0; |
| 2084 | static UInt VG_(n_noncompact_helpers) = 0; |
| 2085 | static Addr VG_(compact_helper_addrs) [MAX_COMPACT_HELPERS]; |
| 2086 | static Int VG_(compact_helper_offsets)[MAX_COMPACT_HELPERS]; |
| 2087 | static Addr VG_(noncompact_helper_addrs) [MAX_NONCOMPACT_HELPERS]; |
| 2088 | static Int VG_(noncompact_helper_offsets)[MAX_NONCOMPACT_HELPERS]; |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2089 | |
| 2090 | /* This is the actual defn of baseblock. */ |
| 2091 | UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS]; |
| 2092 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2093 | /* Words. */ |
| 2094 | static Int baB_off = 0; |
| 2095 | |
| 2096 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2097 | /* Returns the offset, in words. */ |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2098 | Int VG_(alloc_BaB) ( Int words ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2099 | { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2100 | Int off = baB_off; |
| 2101 | baB_off += words; |
| 2102 | if (baB_off >= VG_BASEBLOCK_WORDS) |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2103 | VG_(core_panic)( "VG_(alloc_BaB): baseBlock is too small"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2104 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2105 | return off; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2108 | /* Align offset, in *bytes* */ |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2109 | void VG_(align_BaB) ( UInt align ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2110 | { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2111 | vg_assert(2 == align || 4 == align || 8 == align || 16 == align); |
| 2112 | baB_off += (align-1); |
| 2113 | baB_off &= ~(align-1); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2116 | /* Allocate 1 word in baseBlock and set it to the given value. */ |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2117 | Int VG_(alloc_BaB_1_set) ( Addr a ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2118 | { |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2119 | Int off = VG_(alloc_BaB)(1); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2120 | VG_(baseBlock)[off] = (UInt)a; |
| 2121 | return off; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2124 | /* Registers a function in compact_helper_addrs; compact_helper_offsets is |
| 2125 | filled in later. */ |
| 2126 | void VG_(register_compact_helper)(Addr a) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2127 | { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2128 | if (MAX_COMPACT_HELPERS <= VG_(n_compact_helpers)) { |
| 2129 | VG_(printf)("Can only register %d compact helpers\n", |
| 2130 | MAX_COMPACT_HELPERS); |
| 2131 | VG_(core_panic)("Too many compact helpers registered"); |
| 2132 | } |
| 2133 | VG_(compact_helper_addrs)[VG_(n_compact_helpers)] = a; |
| 2134 | VG_(n_compact_helpers)++; |
| 2135 | } |
| 2136 | |
| 2137 | /* Registers a function in noncompact_helper_addrs; noncompact_helper_offsets |
| 2138 | * is filled in later. |
| 2139 | */ |
| 2140 | void VG_(register_noncompact_helper)(Addr a) |
| 2141 | { |
| 2142 | if (MAX_NONCOMPACT_HELPERS <= VG_(n_noncompact_helpers)) { |
| 2143 | VG_(printf)("Can only register %d non-compact helpers\n", |
| 2144 | MAX_NONCOMPACT_HELPERS); |
| 2145 | VG_(printf)("Try increasing MAX_NON_COMPACT_HELPERS\n"); |
| 2146 | VG_(core_panic)("Too many non-compact helpers registered"); |
| 2147 | } |
| 2148 | VG_(noncompact_helper_addrs)[VG_(n_noncompact_helpers)] = a; |
| 2149 | VG_(n_noncompact_helpers)++; |
| 2150 | } |
| 2151 | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 2152 | /* Allocate offsets in baseBlock for the tool helpers */ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2153 | static |
| 2154 | void assign_helpers_in_baseBlock(UInt n, Int offsets[], Addr addrs[]) |
| 2155 | { |
| 2156 | UInt i; |
| 2157 | for (i = 0; i < n; i++) |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2158 | offsets[i] = VG_(alloc_BaB_1_set)( addrs[i] ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | Bool VG_(need_to_handle_esp_assignment)(void) |
| 2162 | { |
| 2163 | return ( VG_(defined_new_mem_stack_4)() || |
| 2164 | VG_(defined_die_mem_stack_4)() || |
| 2165 | VG_(defined_new_mem_stack_8)() || |
| 2166 | VG_(defined_die_mem_stack_8)() || |
| 2167 | VG_(defined_new_mem_stack_12)() || |
| 2168 | VG_(defined_die_mem_stack_12)() || |
| 2169 | VG_(defined_new_mem_stack_16)() || |
| 2170 | VG_(defined_die_mem_stack_16)() || |
| 2171 | VG_(defined_new_mem_stack_32)() || |
| 2172 | VG_(defined_die_mem_stack_32)() || |
| 2173 | VG_(defined_new_mem_stack)() || |
| 2174 | VG_(defined_die_mem_stack)() |
| 2175 | ); |
| 2176 | } |
| 2177 | |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2178 | // The low/high split is for x86, so that the more common helpers can be |
| 2179 | // in the first 128 bytes of the start, which allows the use of a more |
| 2180 | // compact addressing mode. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2181 | static void init_baseBlock ( Addr client_eip, Addr esp_at_startup ) |
| 2182 | { |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2183 | VGA_(init_low_baseBlock)(client_eip, esp_at_startup); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2184 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2185 | /* Allocate slots for compact helpers */ |
| 2186 | assign_helpers_in_baseBlock(VG_(n_compact_helpers), |
| 2187 | VG_(compact_helper_offsets), |
| 2188 | VG_(compact_helper_addrs)); |
| 2189 | |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2190 | VGA_(init_high_baseBlock)(client_eip, esp_at_startup); |
fitzhardinge | f0046f2 | 2003-12-18 02:39:22 +0000 | [diff] [blame] | 2191 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2192 | #define REG(kind, size) \ |
| 2193 | if (VG_(defined_##kind##_mem_stack##size)()) \ |
| 2194 | VG_(register_noncompact_helper)( \ |
| 2195 | (Addr) VG_(tool_interface).track_##kind##_mem_stack##size ); |
| 2196 | REG(new, _8); |
| 2197 | REG(new, _12); |
| 2198 | REG(new, _16); |
| 2199 | REG(new, _32); |
| 2200 | REG(new, ); |
| 2201 | REG(die, _8); |
| 2202 | REG(die, _12); |
| 2203 | REG(die, _16); |
| 2204 | REG(die, _32); |
| 2205 | REG(die, ); |
| 2206 | #undef REG |
fitzhardinge | f0046f2 | 2003-12-18 02:39:22 +0000 | [diff] [blame] | 2207 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2208 | if (VG_(need_to_handle_esp_assignment)()) |
| 2209 | VG_(register_noncompact_helper)((Addr) VG_(unknown_esp_update)); |
fitzhardinge | b791a19 | 2003-12-18 07:22:44 +0000 | [diff] [blame] | 2210 | |
nethercote | 2e05c33 | 2004-09-06 16:43:37 +0000 | [diff] [blame] | 2211 | VGOFF_(helper_undefined_instruction) |
| 2212 | = VG_(alloc_BaB_1_set)( (Addr) & VG_(helper_undefined_instruction)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2213 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2214 | /* Allocate slots for noncompact helpers */ |
| 2215 | assign_helpers_in_baseBlock(VG_(n_noncompact_helpers), |
| 2216 | VG_(noncompact_helper_offsets), |
| 2217 | VG_(noncompact_helper_addrs)); |
| 2218 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2219 | |
nethercote | 81f9a6f | 2004-08-03 15:45:46 +0000 | [diff] [blame] | 2220 | // Finds the baseBlock offset of a tool-specified helper. |
| 2221 | // Searches through compacts first, then non-compacts. |
| 2222 | Int VG_(helper_offset)(Addr a) |
| 2223 | { |
| 2224 | UInt i; |
| 2225 | Char buf[100]; |
| 2226 | |
| 2227 | for (i = 0; i < VG_(n_compact_helpers); i++) |
| 2228 | if (VG_(compact_helper_addrs)[i] == a) |
| 2229 | return VG_(compact_helper_offsets)[i]; |
| 2230 | for (i = 0; i < VG_(n_noncompact_helpers); i++) |
| 2231 | if (VG_(noncompact_helper_addrs)[i] == a) |
| 2232 | return VG_(noncompact_helper_offsets)[i]; |
| 2233 | |
| 2234 | /* Shouldn't get here */ |
| 2235 | VG_(get_fnname) ( a, buf, 100 ); |
| 2236 | |
| 2237 | VG_(printf)( |
| 2238 | "\nCouldn't find offset of helper from its address (%p: %s).\n" |
| 2239 | "A helper function probably used hasn't been registered?\n\n", a, buf); |
| 2240 | |
| 2241 | VG_(printf)(" compact helpers: "); |
| 2242 | for (i = 0; i < VG_(n_compact_helpers); i++) |
| 2243 | VG_(printf)("%p ", VG_(compact_helper_addrs)[i]); |
| 2244 | |
| 2245 | VG_(printf)("\n non-compact helpers: "); |
| 2246 | for (i = 0; i < VG_(n_noncompact_helpers); i++) |
| 2247 | VG_(printf)("%p ", VG_(noncompact_helper_addrs)[i]); |
| 2248 | |
| 2249 | VG_(printf)("\n"); |
| 2250 | VG_(skin_panic)("Unfound helper"); |
| 2251 | } |
| 2252 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2253 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2254 | /*====================================================================*/ |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2255 | /*=== Initialise program data/text, etc. ===*/ |
| 2256 | /*====================================================================*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2257 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2258 | static void build_valgrind_map_callback |
| 2259 | ( Addr start, UInt size, Char rr, Char ww, Char xx, |
| 2260 | UInt dev, UInt ino, ULong foffset, const UChar* filename ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2261 | { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2262 | UInt prot = 0; |
| 2263 | UInt flags = SF_MMAP|SF_NOSYMS; |
| 2264 | Bool is_stack_segment; |
| 2265 | |
| 2266 | is_stack_segment = |
| 2267 | (start == VG_(clstk_base) && (start+size) == VG_(clstk_end)); |
| 2268 | |
| 2269 | /* Only record valgrind mappings for now, without loading any |
| 2270 | symbols. This is so we know where the free space is before we |
| 2271 | start allocating more memory (note: heap is OK, it's just mmap |
| 2272 | which is the problem here). */ |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 2273 | if (start >= VG_(valgrind_base) && (start+size-1) <= VG_(valgrind_last)) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2274 | flags |= SF_VALGRIND; |
| 2275 | VG_(map_file_segment)(start, size, prot, flags, dev, ino, foffset, filename); |
| 2276 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2279 | // Global var used to pass local data to callback |
| 2280 | Addr esp_at_startup___global_arg = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2281 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2282 | static void build_segment_map_callback |
| 2283 | ( Addr start, UInt size, Char rr, Char ww, Char xx, |
| 2284 | UInt dev, UInt ino, ULong foffset, const UChar* filename ) |
sewardj | 45b672d | 2003-07-25 19:58:11 +0000 | [diff] [blame] | 2285 | { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2286 | UInt prot = 0; |
| 2287 | UInt flags; |
| 2288 | Bool is_stack_segment; |
| 2289 | Addr r_esp; |
sewardj | 45b672d | 2003-07-25 19:58:11 +0000 | [diff] [blame] | 2290 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2291 | is_stack_segment |
| 2292 | = (start == VG_(clstk_base) && (start+size) == VG_(clstk_end)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2293 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2294 | if (rr == 'r') prot |= VKI_PROT_READ; |
| 2295 | if (ww == 'w') prot |= VKI_PROT_WRITE; |
| 2296 | if (xx == 'x') prot |= VKI_PROT_EXEC; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2297 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2298 | if (is_stack_segment) |
| 2299 | flags = SF_STACK | SF_GROWDOWN; |
| 2300 | else |
| 2301 | flags = SF_EXEC|SF_MMAP; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2302 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2303 | if (filename != NULL) |
| 2304 | flags |= SF_FILE; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2305 | |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 2306 | if (start >= VG_(valgrind_base) && (start+size-1) <= VG_(valgrind_last)) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2307 | flags |= SF_VALGRIND; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2308 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2309 | VG_(map_file_segment)(start, size, prot, flags, dev, ino, foffset, filename); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 2310 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2311 | if (VG_(is_client_addr)(start) && VG_(is_client_addr)(start+size-1)) |
| 2312 | VG_TRACK( new_mem_startup, start, size, rr=='r', ww=='w', xx=='x' ); |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 2313 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2314 | /* If this is the stack segment mark all below %esp as noaccess. */ |
| 2315 | r_esp = esp_at_startup___global_arg; |
| 2316 | vg_assert(0 != r_esp); |
| 2317 | if (is_stack_segment) { |
| 2318 | if (0) |
| 2319 | VG_(message)(Vg_DebugMsg, "invalidating stack area: %x .. %x", |
| 2320 | start,r_esp); |
| 2321 | VG_TRACK( die_mem_stack, start, r_esp-start ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2322 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2326 | /*====================================================================*/ |
| 2327 | /*=== Sanity check machinery (permanently engaged) ===*/ |
| 2328 | /*====================================================================*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2329 | |
| 2330 | /* A fast sanity check -- suitable for calling circa once per |
| 2331 | millisecond. */ |
| 2332 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 2333 | void VG_(sanity_check_general) ( Bool force_expensive ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2334 | { |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2335 | VGP_PUSHCC(VgpCoreCheapSanity); |
| 2336 | |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 2337 | if (VG_(clo_sanity_level) < 1) return; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2338 | |
| 2339 | /* --- First do all the tests that we can do quickly. ---*/ |
| 2340 | |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 2341 | sanity_fast_count++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2342 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2343 | /* Check stuff pertaining to the memory check system. */ |
| 2344 | |
| 2345 | /* Check that nobody has spuriously claimed that the first or |
| 2346 | last 16 pages of memory have become accessible [...] */ |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2347 | if (VG_(needs).sanity_checks) { |
| 2348 | VGP_PUSHCC(VgpSkinCheapSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2349 | vg_assert(SK_(cheap_sanity_check)()); |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2350 | VGP_POPCC(VgpSkinCheapSanity); |
| 2351 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2352 | |
| 2353 | /* --- Now some more expensive checks. ---*/ |
| 2354 | |
| 2355 | /* Once every 25 times, check some more expensive stuff. */ |
| 2356 | if ( force_expensive |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 2357 | || VG_(clo_sanity_level) > 1 |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 2358 | || (VG_(clo_sanity_level) == 1 && (sanity_fast_count % 25) == 0)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2359 | |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2360 | VGP_PUSHCC(VgpCoreExpensiveSanity); |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 2361 | sanity_slow_count++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2362 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 2363 | VG_(sanity_check_proxy)(); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2364 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2365 | # if 0 |
| 2366 | { void zzzmemscan(void); zzzmemscan(); } |
| 2367 | # endif |
| 2368 | |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 2369 | if ((sanity_fast_count % 250) == 0) |
nethercote | 92e7b7f | 2004-08-07 17:52:25 +0000 | [diff] [blame] | 2370 | VG_(sanity_check_tt_tc)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2371 | |
| 2372 | if (VG_(needs).sanity_checks) { |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2373 | VGP_PUSHCC(VgpSkinExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2374 | vg_assert(SK_(expensive_sanity_check)()); |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2375 | VGP_POPCC(VgpSkinExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2376 | } |
| 2377 | /* |
nethercote | 297effd | 2004-08-02 15:07:57 +0000 | [diff] [blame] | 2378 | if ((sanity_fast_count % 500) == 0) VG_(mallocSanityCheckAll)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2379 | */ |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2380 | VGP_POPCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 2383 | if (VG_(clo_sanity_level) > 1) { |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2384 | VGP_PUSHCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2385 | /* Check sanity of the low-level memory manager. Note that bugs |
| 2386 | in the client's code can cause this to fail, so we don't do |
| 2387 | this check unless specially asked for. And because it's |
| 2388 | potentially very expensive. */ |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 2389 | VG_(sanity_check_malloc_all)(); |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2390 | VGP_POPCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2391 | } |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2392 | VGP_POPCC(VgpCoreCheapSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2393 | } |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2394 | |
| 2395 | |
| 2396 | /*====================================================================*/ |
| 2397 | /*=== main() ===*/ |
| 2398 | /*====================================================================*/ |
| 2399 | |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2400 | /* |
| 2401 | This code decides on the layout of the client and Valgrind address |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 2402 | spaces, loads valgrind.so and the tool.so into the valgrind part, |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2403 | loads the client executable (and the dynamic linker, if necessary) |
| 2404 | into the client part, and calls into Valgrind proper. |
| 2405 | |
| 2406 | The code is careful not to allow spurious mappings to appear in the |
| 2407 | wrong parts of the address space. In particular, to make sure |
| 2408 | dlopen puts things in the right place, it will pad out the forbidden |
| 2409 | chunks of address space so that dlopen is forced to put things where |
| 2410 | we want them. |
| 2411 | |
| 2412 | The memory map it creates is: |
| 2413 | |
| 2414 | CLIENT_BASE +-------------------------+ |
| 2415 | | client address space | |
| 2416 | : : |
| 2417 | : : |
| 2418 | | client stack | |
| 2419 | client_end +-------------------------+ |
| 2420 | | redzone | |
| 2421 | shadow_base +-------------------------+ |
| 2422 | | | |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 2423 | : shadow memory for tools : |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2424 | | (may be 0 sized) | |
| 2425 | shadow_end +-------------------------+ |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2426 | valgrind_base +-------------------------+ |
| 2427 | | kickstart executable | |
| 2428 | | valgrind heap vvvvvvvvv| (barely used) |
| 2429 | - - |
| 2430 | | valgrind .so files | |
| 2431 | | and mappings | |
| 2432 | - - |
| 2433 | | valgrind stack ^^^^^^^^^| |
nethercote | 820bd8c | 2004-09-07 23:04:49 +0000 | [diff] [blame^] | 2434 | valgrind_last +-------------------------+ |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2435 | : kernel : |
| 2436 | |
| 2437 | Nb: Before we can do general allocations with VG_(arena_malloc)() and |
| 2438 | VG_(mmap)(), we need to build the segment skip-list, so we know where |
| 2439 | we can put things. However, building that structure requires |
| 2440 | allocating memory. So we need to a bootstrapping process. It's done |
| 2441 | by making VG_(arena_malloc)() have a special static superblock that's |
| 2442 | used for the first 1MB's worth of allocations. This is enough to |
| 2443 | build the segment skip-list. |
| 2444 | */ |
| 2445 | |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2446 | static int prmap(char *start, char *end, const char *perm, off_t off, |
| 2447 | int maj, int min, int ino, void* dummy) { |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 2448 | printf("mapping %10p-%10p %s %02x:%02x %d\n", |
| 2449 | start, end, perm, maj, min, ino); |
| 2450 | return True; |
| 2451 | } |
| 2452 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2453 | int main(int argc, char **argv) |
| 2454 | { |
| 2455 | char **cl_argv; |
| 2456 | const char *tool = NULL; |
| 2457 | const char *exec = NULL; |
| 2458 | char *preload; /* tool-specific LD_PRELOAD .so */ |
| 2459 | char **env; |
nethercote | 6c999f2 | 2004-01-31 22:55:15 +0000 | [diff] [blame] | 2460 | Int need_help = 0; // 0 = no, 1 = --help, 2 = --help-debug |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2461 | struct exeinfo info; |
| 2462 | ToolInfo *toolinfo = NULL; |
| 2463 | void *tool_dlhandle; |
| 2464 | Addr client_eip; |
| 2465 | Addr esp_at_startup; /* client's %esp at the point we gained control. */ |
| 2466 | UInt * client_auxv; |
| 2467 | VgSchedReturnCode src; |
nethercote | 47dd12c | 2004-06-22 14:18:42 +0000 | [diff] [blame] | 2468 | Int exitcode = 0; |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 2469 | Int fatal_sigNo = -1; |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 2470 | vki_rlimit zero = { 0, 0 }; |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2471 | Int padfile; |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 2472 | ThreadId last_run_tid = 0; // Last thread the scheduler ran. |
| 2473 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2474 | |
| 2475 | //============================================================ |
| 2476 | // Nb: startup is complex. Prerequisites are shown at every step. |
| 2477 | // |
| 2478 | // *** Be very careful when messing with the order *** |
| 2479 | //============================================================ |
| 2480 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2481 | //============================================================ |
| 2482 | // Command line argument handling order: |
| 2483 | // * If --help/--help-debug are present, show usage message |
| 2484 | // (if --tool is also present, that includes the tool-specific usage) |
| 2485 | // * Then, if --tool is missing, abort with error msg |
| 2486 | // * Then, if client is missing, abort with error msg |
| 2487 | // * Then, if any cmdline args are bad, abort with error msg |
| 2488 | //============================================================ |
| 2489 | |
fitzhardinge | b50068f | 2004-02-24 23:42:55 +0000 | [diff] [blame] | 2490 | // Get the current process datasize rlimit, and set it to zero. |
| 2491 | // This prevents any internal uses of brk() from having any effect. |
| 2492 | // We remember the old value so we can restore it on exec, so that |
| 2493 | // child processes will have a reasonable brk value. |
| 2494 | VG_(getrlimit)(VKI_RLIMIT_DATA, &VG_(client_rlimit_data)); |
| 2495 | zero.rlim_max = VG_(client_rlimit_data).rlim_max; |
| 2496 | VG_(setrlimit)(VKI_RLIMIT_DATA, &zero); |
| 2497 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2498 | //-------------------------------------------------------------- |
| 2499 | // Check we were launched by stage1 |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2500 | // p: n/a |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2501 | //-------------------------------------------------------------- |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2502 | padfile = scan_auxv(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2503 | |
| 2504 | if (0) { |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2505 | printf("========== main() ==========\n"); |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2506 | foreach_map(prmap, /*dummy*/NULL); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | //-------------------------------------------------------------- |
| 2510 | // Look for alternative libdir |
| 2511 | // p: n/a |
| 2512 | //-------------------------------------------------------------- |
| 2513 | { char *cp = getenv(VALGRINDLIB); |
| 2514 | if (cp != NULL) |
| 2515 | VG_(libdir) = cp; |
| 2516 | } |
| 2517 | |
| 2518 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2519 | // Get valgrind args + client args (inc. from VALGRIND_OPTS/.valgrindrc). |
| 2520 | // Pre-process the command line. |
| 2521 | // p: n/a |
| 2522 | //-------------------------------------------------------------- |
nethercote | f6a1d50 | 2004-08-09 12:21:57 +0000 | [diff] [blame] | 2523 | get_command_line(argc, argv, &vg_argc, &vg_argv, &cl_argv); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2524 | pre_process_cmd_line_options(&need_help, &tool, &exec); |
| 2525 | |
| 2526 | //============================================================== |
| 2527 | // Nb: once a tool is specified, the tool.so must be loaded even if |
| 2528 | // they specified --help or didn't specify a client program. |
| 2529 | //============================================================== |
| 2530 | |
| 2531 | //-------------------------------------------------------------- |
| 2532 | // With client padded out, map in tool |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2533 | // p: set-libdir [for VG_(libdir)] |
| 2534 | // p: pre_process_cmd_line_options() [for 'tool'] |
| 2535 | //-------------------------------------------------------------- |
| 2536 | load_tool(tool, &tool_dlhandle, &toolinfo, &preload); |
| 2537 | |
| 2538 | //============================================================== |
| 2539 | // Can use VG_(malloc)() and VG_(arena_malloc)() only after load_tool() |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2540 | // -- redzone size is now set. This is checked by vg_malloc2.c. |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2541 | //============================================================== |
| 2542 | |
| 2543 | //-------------------------------------------------------------- |
| 2544 | // Finalise address space layout |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2545 | // p: load_tool() [for 'toolinfo'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2546 | //-------------------------------------------------------------- |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2547 | layout_remaining_space( (Addr) & argc, toolinfo->shadow_ratio ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2548 | |
| 2549 | //-------------------------------------------------------------- |
| 2550 | // Load client executable, finding in $PATH if necessary |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2551 | // p: pre_process_cmd_line_options() [for 'exec', 'need_help'] |
| 2552 | // p: layout_remaining_space [so there's space] |
| 2553 | //-------------------------------------------------------------- |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2554 | load_client(cl_argv, exec, need_help, &info, &client_eip); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2555 | |
| 2556 | //-------------------------------------------------------------- |
| 2557 | // Everything in place, unpad us |
| 2558 | // p: layout_remaining_space() [everything must be mapped in before now] |
| 2559 | // p: load_client() [ditto] |
| 2560 | //-------------------------------------------------------------- |
nethercote | 31779c7 | 2004-07-30 21:50:15 +0000 | [diff] [blame] | 2561 | as_unpad((void *)VG_(shadow_end), (void *)~0, padfile); |
| 2562 | as_closepadfile(padfile); // no more padding |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2563 | |
| 2564 | //-------------------------------------------------------------- |
| 2565 | // Set up client's environment |
| 2566 | // p: set-libdir [for VG_(libdir)] |
| 2567 | // p: load_tool() [for 'preload'] |
| 2568 | //-------------------------------------------------------------- |
| 2569 | env = fix_environment(environ, preload); |
| 2570 | |
| 2571 | //-------------------------------------------------------------- |
nethercote | 5ee67ca | 2004-06-22 14:00:09 +0000 | [diff] [blame] | 2572 | // Setup client stack, eip, and VG_(client_arg[cv]) |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2573 | // p: load_client() [for 'info'] |
| 2574 | // p: fix_environment() [for 'env'] |
| 2575 | //-------------------------------------------------------------- |
| 2576 | esp_at_startup = setup_client_stack(cl_argv, env, &info, &client_auxv); |
| 2577 | |
| 2578 | if (0) |
nethercote | 6a27d83 | 2004-09-07 10:17:02 +0000 | [diff] [blame] | 2579 | printf("entry=%p client esp=%p vg_argc=%d brkbase=%p\n", |
| 2580 | (void*)client_eip, (void*)esp_at_startup, vg_argc, |
| 2581 | (void*)VG_(brk_base)); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2582 | |
| 2583 | //============================================================== |
| 2584 | // Finished setting up operating environment. Now initialise |
| 2585 | // Valgrind. (This is where the old VG_(main)() started.) |
| 2586 | //============================================================== |
| 2587 | |
| 2588 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2589 | // atfork |
| 2590 | // p: n/a |
| 2591 | //-------------------------------------------------------------- |
| 2592 | VG_(atfork)(NULL, NULL, newpid); |
| 2593 | newpid(VG_INVALID_THREADID); |
| 2594 | |
| 2595 | //-------------------------------------------------------------- |
| 2596 | // setup file descriptors |
| 2597 | // p: n/a |
| 2598 | //-------------------------------------------------------------- |
| 2599 | setup_file_descriptors(); |
| 2600 | |
| 2601 | //-------------------------------------------------------------- |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2602 | // Read /proc/self/maps into a buffer |
| 2603 | // p: all memory layout, environment setup [so memory maps are right] |
| 2604 | //-------------------------------------------------------------- |
| 2605 | VG_(read_procselfmaps)(); |
| 2606 | |
| 2607 | //-------------------------------------------------------------- |
| 2608 | // Build segment map (Valgrind segments only) |
| 2609 | // p: read proc/self/maps |
| 2610 | // p: sk_pre_clo_init() [to setup new_mem_startup tracker] |
| 2611 | //-------------------------------------------------------------- |
| 2612 | VG_(parse_procselfmaps) ( build_valgrind_map_callback ); |
| 2613 | |
| 2614 | //============================================================== |
| 2615 | // Can use VG_(arena_malloc)() with non-CORE arena after segments set up |
| 2616 | //============================================================== |
| 2617 | |
| 2618 | //-------------------------------------------------------------- |
| 2619 | // Init tool: pre_clo_init, process cmd line, post_clo_init |
| 2620 | // p: setup_client_stack() [for 'VG_(client_arg[cv]'] |
| 2621 | // p: load_tool() [for 'tool'] |
| 2622 | // p: setup_file_descriptors() [for 'VG_(fd_xxx_limit)'] |
| 2623 | // p: parse_procselfmaps [so VG segments are setup so tool can |
| 2624 | // call VG_(malloc)] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2625 | //-------------------------------------------------------------- |
| 2626 | (*toolinfo->sk_pre_clo_init)(); |
| 2627 | VG_(tool_init_dlsym)(tool_dlhandle); |
| 2628 | VG_(sanity_check_needs)(); |
| 2629 | |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2630 | // If --tool and --help/--help-debug was given, now give the core+tool |
| 2631 | // help message |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2632 | if (need_help) { |
| 2633 | usage(/*--help-debug?*/2 == need_help); |
| 2634 | } |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2635 | process_cmd_line_options(client_auxv, tool); |
| 2636 | |
| 2637 | SK_(post_clo_init)(); |
nethercote | f4928da | 2004-06-15 10:54:40 +0000 | [diff] [blame] | 2638 | |
| 2639 | //-------------------------------------------------------------- |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2640 | // Build segment map (all segments) |
| 2641 | // p: setup_client_stack() [for 'esp_at_startup'] |
| 2642 | // p: init tool [for 'new_mem_startup'] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2643 | //-------------------------------------------------------------- |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2644 | esp_at_startup___global_arg = esp_at_startup; |
| 2645 | VG_(parse_procselfmaps) ( build_segment_map_callback ); /* everything */ |
| 2646 | esp_at_startup___global_arg = 0; |
| 2647 | |
| 2648 | //-------------------------------------------------------------- |
nethercote | f84f695 | 2004-07-15 14:58:33 +0000 | [diff] [blame] | 2649 | // Protect client trampoline page (which is also sysinfo stuff) |
| 2650 | // p: segment stuff [otherwise get seg faults...] |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2651 | //-------------------------------------------------------------- |
nethercote | c314eba | 2004-07-15 12:59:41 +0000 | [diff] [blame] | 2652 | VG_(mprotect)( (void *)VG_(client_trampoline_code), |
| 2653 | VG_(trampoline_code_length), VKI_PROT_READ|VKI_PROT_EXEC ); |
| 2654 | |
| 2655 | //============================================================== |
| 2656 | // Can use VG_(map)() after segments set up |
| 2657 | //============================================================== |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2658 | |
| 2659 | //-------------------------------------------------------------- |
| 2660 | // Allow GDB attach |
| 2661 | // p: process_cmd_line_options() [for VG_(clo_wait_for_gdb)] |
| 2662 | //-------------------------------------------------------------- |
| 2663 | /* Hook to delay things long enough so we can get the pid and |
| 2664 | attach GDB in another shell. */ |
| 2665 | if (VG_(clo_wait_for_gdb)) { |
| 2666 | VG_(printf)("pid=%d\n", VG_(getpid)()); |
| 2667 | /* do "jump *$eip" to skip this in gdb */ |
| 2668 | VG_(do_syscall)(__NR_pause); |
| 2669 | } |
| 2670 | |
| 2671 | //-------------------------------------------------------------- |
nethercote | 1d44709 | 2004-02-01 17:29:59 +0000 | [diff] [blame] | 2672 | // Set up baseBlock |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2673 | // p: {pre,post}_clo_init() [for tool helper registration] |
| 2674 | // load_client() [for 'client_eip'] |
| 2675 | // setup_client_stack() [for 'esp_at_startup'] |
| 2676 | //-------------------------------------------------------------- |
| 2677 | init_baseBlock(client_eip, esp_at_startup); |
| 2678 | |
| 2679 | //-------------------------------------------------------------- |
| 2680 | // Search for file descriptors that are inherited from our parent |
| 2681 | // p: process_cmd_line_options [for VG_(clo_track_fds)] |
| 2682 | //-------------------------------------------------------------- |
| 2683 | if (VG_(clo_track_fds)) |
| 2684 | VG_(init_preopened_fds)(); |
| 2685 | |
| 2686 | //-------------------------------------------------------------- |
| 2687 | // Initialise the scheduler |
| 2688 | // p: init_baseBlock() [baseBlock regs copied into VG_(threads)[1]] |
| 2689 | // p: setup_file_descriptors() [else VG_(safe_fd)() breaks] |
| 2690 | //-------------------------------------------------------------- |
| 2691 | VG_(scheduler_init)(); |
| 2692 | |
| 2693 | //-------------------------------------------------------------- |
| 2694 | // Set up the ProxyLWP machinery |
| 2695 | // p: VG_(scheduler_init)()? [XXX: subtle dependency?] |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2696 | //-------------------------------------------------------------- |
| 2697 | VG_(proxy_init)(); |
| 2698 | |
| 2699 | //-------------------------------------------------------------- |
| 2700 | // Initialise the signal handling subsystem |
| 2701 | // p: VG_(atfork)(NULL, NULL, newpid) [else problems with sigmasks] |
| 2702 | // p: VG_(proxy_init)() [else breaks...] |
| 2703 | //-------------------------------------------------------------- |
| 2704 | // Nb: temporarily parks the saved blocking-mask in saved_sigmask. |
| 2705 | VG_(sigstartup_actions)(); |
| 2706 | |
| 2707 | //-------------------------------------------------------------- |
| 2708 | // Perhaps we're profiling Valgrind? |
| 2709 | // p: process_cmd_line_options() [for VG_(clo_profile)] |
| 2710 | // p: others? |
| 2711 | // |
| 2712 | // XXX: this seems to be broken? It always says the tool wasn't built |
| 2713 | // for profiling; vg_profile.c's functions don't seem to be overriding |
| 2714 | // vg_dummy_profile.c's? |
| 2715 | // |
| 2716 | // XXX: want this as early as possible. Looking for --profile |
| 2717 | // in pre_process_cmd_line_options() could get it earlier. |
| 2718 | //-------------------------------------------------------------- |
| 2719 | if (VG_(clo_profile)) |
| 2720 | VGP_(init_profiling)(); |
| 2721 | |
| 2722 | VGP_PUSHCC(VgpStartup); |
| 2723 | |
| 2724 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2725 | // Read suppression file |
| 2726 | // p: process_cmd_line_options() [for VG_(clo_suppressions)] |
| 2727 | //-------------------------------------------------------------- |
| 2728 | if (VG_(needs).core_errors || VG_(needs).skin_errors) |
| 2729 | VG_(load_suppressions)(); |
| 2730 | |
| 2731 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2732 | // Initialise translation table and translation cache |
| 2733 | // p: read_procselfmaps [so the anonymous mmaps for the TT/TC |
| 2734 | // aren't identified as part of the client, which would waste |
| 2735 | // > 20M of virtual address space.] |
| 2736 | //-------------------------------------------------------------- |
| 2737 | VG_(init_tt_tc)(); |
| 2738 | |
| 2739 | //-------------------------------------------------------------- |
| 2740 | // Read debug info to find glibc entry points to intercept |
| 2741 | // p: parse_procselfmaps? [XXX for debug info?] |
| 2742 | // p: init_tt_tc? [XXX ???] |
| 2743 | //-------------------------------------------------------------- |
| 2744 | VG_(setup_code_redirect_table)(); |
| 2745 | |
| 2746 | //-------------------------------------------------------------- |
| 2747 | // Verbosity message |
| 2748 | // p: end_rdtsc_calibration [so startup message is printed first] |
| 2749 | //-------------------------------------------------------------- |
| 2750 | if (VG_(clo_verbosity) == 1) |
| 2751 | VG_(message)(Vg_UserMsg, "For more details, rerun with: -v"); |
| 2752 | if (VG_(clo_verbosity) > 0) |
| 2753 | VG_(message)(Vg_UserMsg, ""); |
| 2754 | |
| 2755 | //-------------------------------------------------------------- |
| 2756 | // Setup pointercheck |
| 2757 | // p: process_cmd_line_options() [for VG_(clo_pointercheck)] |
| 2758 | //-------------------------------------------------------------- |
nethercote | 775508a | 2004-09-07 22:38:23 +0000 | [diff] [blame] | 2759 | if (VG_(clo_pointercheck)) |
| 2760 | VG_(clo_pointercheck) = VGA_(setup_pointercheck)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2761 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2762 | //-------------------------------------------------------------- |
| 2763 | // Run! |
| 2764 | //-------------------------------------------------------------- |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2765 | VGP_POPCC(VgpStartup); |
| 2766 | VGP_PUSHCC(VgpSched); |
| 2767 | |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 2768 | src = VG_(scheduler)( &exitcode, &last_run_tid, &fatal_sigNo ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2769 | |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 2770 | VGP_POPCC(VgpSched); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2771 | |
| 2772 | |
| 2773 | //-------------------------------------------------------------- |
| 2774 | // Finalisation: cleanup, messages, etc. Order no so important, only |
| 2775 | // affects what order the messages come. |
| 2776 | //-------------------------------------------------------------- |
| 2777 | if (VG_(clo_verbosity) > 0) |
| 2778 | VG_(message)(Vg_UserMsg, ""); |
| 2779 | |
| 2780 | if (src == VgSrc_Deadlock) { |
| 2781 | VG_(message)(Vg_UserMsg, |
| 2782 | "Warning: pthread scheduler exited due to deadlock"); |
| 2783 | } |
| 2784 | |
| 2785 | /* Print out file descriptor summary and stats. */ |
| 2786 | if (VG_(clo_track_fds)) |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 2787 | VG_(show_open_fds)(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2788 | |
| 2789 | if (VG_(needs).core_errors || VG_(needs).skin_errors) |
| 2790 | VG_(show_all_errors)(); |
| 2791 | |
nethercote | 47dd12c | 2004-06-22 14:18:42 +0000 | [diff] [blame] | 2792 | SK_(fini)( exitcode ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2793 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 2794 | VG_(sanity_check_general)( True /*include expensive checks*/ ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2795 | |
| 2796 | if (VG_(clo_verbosity) > 1) |
nethercote | 3a42fb8 | 2004-08-03 18:08:50 +0000 | [diff] [blame] | 2797 | print_all_stats(); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2798 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2799 | if (VG_(clo_profile)) |
| 2800 | VGP_(done_profiling)(); |
| 2801 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2802 | /* We're exiting, so nuke all the threads and clean up the proxy LWPs */ |
| 2803 | vg_assert(src == VgSrc_FatalSig || |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 2804 | VG_(threads)[last_run_tid].status == VgTs_Runnable || |
| 2805 | VG_(threads)[last_run_tid].status == VgTs_WaitJoiner); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2806 | VG_(nuke_all_threads_except)(VG_INVALID_THREADID); |
| 2807 | |
| 2808 | //-------------------------------------------------------------- |
| 2809 | // Exit, according to the scheduler's return code |
| 2810 | //-------------------------------------------------------------- |
| 2811 | switch (src) { |
| 2812 | case VgSrc_ExitSyscall: /* the normal way out */ |
nethercote | 759dda3 | 2004-08-07 18:16:56 +0000 | [diff] [blame] | 2813 | vg_assert(last_run_tid > 0 && last_run_tid < VG_N_THREADS); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2814 | VG_(proxy_shutdown)(); |
| 2815 | |
| 2816 | /* The thread's %EBX at the time it did __NR_exit() will hold |
| 2817 | the arg to __NR_exit(), so we just do __NR_exit() with |
| 2818 | that arg. */ |
nethercote | 47dd12c | 2004-06-22 14:18:42 +0000 | [diff] [blame] | 2819 | VG_(exit)( exitcode ); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2820 | /* NOT ALIVE HERE! */ |
| 2821 | VG_(core_panic)("entered the afterlife in main() -- ExitSyscall"); |
| 2822 | break; /* what the hell :) */ |
| 2823 | |
| 2824 | case VgSrc_Deadlock: |
| 2825 | /* Just exit now. No point in continuing. */ |
| 2826 | VG_(proxy_shutdown)(); |
| 2827 | VG_(exit)(0); |
| 2828 | VG_(core_panic)("entered the afterlife in main() -- Deadlock"); |
| 2829 | break; |
| 2830 | |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2831 | case VgSrc_FatalSig: |
| 2832 | /* We were killed by a fatal signal, so replicate the effect */ |
nethercote | 238a3c3 | 2004-08-09 13:13:31 +0000 | [diff] [blame] | 2833 | vg_assert(fatal_sigNo != -1); |
| 2834 | VG_(kill_self)(fatal_sigNo); |
nethercote | 71980f0 | 2004-01-24 18:18:54 +0000 | [diff] [blame] | 2835 | VG_(core_panic)("main(): signal was supposed to be fatal"); |
| 2836 | break; |
| 2837 | |
| 2838 | default: |
| 2839 | VG_(core_panic)("main(): unexpected scheduler return code"); |
| 2840 | } |
| 2841 | |
| 2842 | abort(); |
| 2843 | } |
| 2844 | |
| 2845 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2846 | /*--------------------------------------------------------------------*/ |
| 2847 | /*--- end vg_main.c ---*/ |
| 2848 | /*--------------------------------------------------------------------*/ |