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