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