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