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