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