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