sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- C startup stuff, reached from vg_startup.S. ---*/ |
| 4 | /*--- vg_main.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, an extensible x86 protected-mode |
| 9 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2003 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include "vg_include.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 33 | |
| 34 | /* --------------------------------------------------------------------- |
| 35 | Compute offsets into baseBlock. See comments in vg_include.h. |
| 36 | ------------------------------------------------------------------ */ |
| 37 | |
| 38 | /* The variables storing offsets. */ |
| 39 | |
| 40 | #define INVALID_OFFSET (-1) |
| 41 | |
| 42 | Int VGOFF_(m_eax) = INVALID_OFFSET; |
| 43 | Int VGOFF_(m_ecx) = INVALID_OFFSET; |
| 44 | Int VGOFF_(m_edx) = INVALID_OFFSET; |
| 45 | Int VGOFF_(m_ebx) = INVALID_OFFSET; |
| 46 | Int VGOFF_(m_esp) = INVALID_OFFSET; |
| 47 | Int VGOFF_(m_ebp) = INVALID_OFFSET; |
| 48 | Int VGOFF_(m_esi) = INVALID_OFFSET; |
| 49 | Int VGOFF_(m_edi) = INVALID_OFFSET; |
| 50 | Int VGOFF_(m_eflags) = INVALID_OFFSET; |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 51 | Int VGOFF_(m_dflag) = INVALID_OFFSET; |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 52 | Int VGOFF_(m_ssestate) = INVALID_OFFSET; |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 53 | Int VGOFF_(ldt) = INVALID_OFFSET; |
| 54 | Int VGOFF_(m_cs) = INVALID_OFFSET; |
| 55 | Int VGOFF_(m_ss) = INVALID_OFFSET; |
| 56 | Int VGOFF_(m_ds) = INVALID_OFFSET; |
| 57 | Int VGOFF_(m_es) = INVALID_OFFSET; |
| 58 | Int VGOFF_(m_fs) = INVALID_OFFSET; |
| 59 | Int VGOFF_(m_gs) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 60 | Int VGOFF_(m_eip) = INVALID_OFFSET; |
| 61 | Int VGOFF_(spillslots) = INVALID_OFFSET; |
| 62 | Int VGOFF_(sh_eax) = INVALID_OFFSET; |
| 63 | Int VGOFF_(sh_ecx) = INVALID_OFFSET; |
| 64 | Int VGOFF_(sh_edx) = INVALID_OFFSET; |
| 65 | Int VGOFF_(sh_ebx) = INVALID_OFFSET; |
| 66 | Int VGOFF_(sh_esp) = INVALID_OFFSET; |
| 67 | Int VGOFF_(sh_ebp) = INVALID_OFFSET; |
| 68 | Int VGOFF_(sh_esi) = INVALID_OFFSET; |
| 69 | Int VGOFF_(sh_edi) = INVALID_OFFSET; |
| 70 | Int VGOFF_(sh_eflags) = INVALID_OFFSET; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 71 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 72 | Int VGOFF_(helper_idiv_64_32) = INVALID_OFFSET; |
| 73 | Int VGOFF_(helper_div_64_32) = INVALID_OFFSET; |
| 74 | Int VGOFF_(helper_idiv_32_16) = INVALID_OFFSET; |
| 75 | Int VGOFF_(helper_div_32_16) = INVALID_OFFSET; |
| 76 | Int VGOFF_(helper_idiv_16_8) = INVALID_OFFSET; |
| 77 | Int VGOFF_(helper_div_16_8) = INVALID_OFFSET; |
| 78 | Int VGOFF_(helper_imul_32_64) = INVALID_OFFSET; |
| 79 | Int VGOFF_(helper_mul_32_64) = INVALID_OFFSET; |
| 80 | Int VGOFF_(helper_imul_16_32) = INVALID_OFFSET; |
| 81 | Int VGOFF_(helper_mul_16_32) = INVALID_OFFSET; |
| 82 | Int VGOFF_(helper_imul_8_16) = INVALID_OFFSET; |
| 83 | Int VGOFF_(helper_mul_8_16) = INVALID_OFFSET; |
| 84 | Int VGOFF_(helper_CLD) = INVALID_OFFSET; |
| 85 | Int VGOFF_(helper_STD) = INVALID_OFFSET; |
| 86 | Int VGOFF_(helper_get_dirflag) = INVALID_OFFSET; |
sewardj | 7d78e78 | 2002-06-02 00:04:00 +0000 | [diff] [blame] | 87 | Int VGOFF_(helper_CLC) = INVALID_OFFSET; |
| 88 | Int VGOFF_(helper_STC) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 89 | Int VGOFF_(helper_shldl) = INVALID_OFFSET; |
| 90 | Int VGOFF_(helper_shldw) = INVALID_OFFSET; |
| 91 | Int VGOFF_(helper_shrdl) = INVALID_OFFSET; |
| 92 | Int VGOFF_(helper_shrdw) = INVALID_OFFSET; |
daywalker | b18d253 | 2003-09-27 20:15:01 +0000 | [diff] [blame] | 93 | Int VGOFF_(helper_IN) = INVALID_OFFSET; |
| 94 | Int VGOFF_(helper_OUT) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 95 | Int VGOFF_(helper_RDTSC) = INVALID_OFFSET; |
| 96 | Int VGOFF_(helper_CPUID) = INVALID_OFFSET; |
| 97 | Int VGOFF_(helper_BSWAP) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 98 | Int VGOFF_(helper_bsf) = INVALID_OFFSET; |
| 99 | Int VGOFF_(helper_bsr) = INVALID_OFFSET; |
| 100 | Int VGOFF_(helper_fstsw_AX) = INVALID_OFFSET; |
| 101 | Int VGOFF_(helper_SAHF) = INVALID_OFFSET; |
njn | d6251f1 | 2003-06-03 13:38:51 +0000 | [diff] [blame] | 102 | Int VGOFF_(helper_LAHF) = INVALID_OFFSET; |
sewardj | 4d0ab1f | 2002-03-24 10:00:09 +0000 | [diff] [blame] | 103 | Int VGOFF_(helper_DAS) = INVALID_OFFSET; |
sewardj | fe8a166 | 2002-03-24 11:54:07 +0000 | [diff] [blame] | 104 | Int VGOFF_(helper_DAA) = INVALID_OFFSET; |
sewardj | 5109643 | 2002-12-14 23:59:09 +0000 | [diff] [blame] | 105 | Int VGOFF_(helper_undefined_instruction) = INVALID_OFFSET; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 106 | |
| 107 | /* MAX_NONCOMPACT_HELPERS can be increased easily. If MAX_COMPACT_HELPERS is |
| 108 | * increased too much, they won't really be compact any more... */ |
| 109 | #define MAX_COMPACT_HELPERS 8 |
njn | fedb736 | 2003-02-24 10:21:45 +0000 | [diff] [blame] | 110 | #define MAX_NONCOMPACT_HELPERS 50 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 111 | |
| 112 | UInt VG_(n_compact_helpers) = 0; |
| 113 | UInt VG_(n_noncompact_helpers) = 0; |
| 114 | |
| 115 | Addr VG_(compact_helper_addrs) [MAX_COMPACT_HELPERS]; |
| 116 | Int VG_(compact_helper_offsets)[MAX_COMPACT_HELPERS]; |
| 117 | Addr VG_(noncompact_helper_addrs) [MAX_NONCOMPACT_HELPERS]; |
| 118 | Int VG_(noncompact_helper_offsets)[MAX_NONCOMPACT_HELPERS]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 119 | |
| 120 | /* This is the actual defn of baseblock. */ |
| 121 | UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS]; |
| 122 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 123 | /* PID of the main thread */ |
| 124 | Int VG_(main_pid); |
| 125 | |
| 126 | /* PGRP of process */ |
| 127 | Int VG_(main_pgrp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 128 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 129 | /* Words. */ |
| 130 | static Int baB_off = 0; |
| 131 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 132 | /* jmp_buf for fatal signals */ |
| 133 | Int VG_(fatal_sigNo) = -1; |
| 134 | Bool VG_(fatal_signal_set) = False; |
| 135 | jmp_buf VG_(fatal_signal_jmpbuf); |
| 136 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 137 | /* Returns the offset, in words. */ |
| 138 | static Int alloc_BaB ( Int words ) |
| 139 | { |
| 140 | Int off = baB_off; |
| 141 | baB_off += words; |
| 142 | if (baB_off >= VG_BASEBLOCK_WORDS) |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 143 | VG_(core_panic)( "alloc_BaB: baseBlock is too small"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 144 | |
| 145 | return off; |
| 146 | } |
| 147 | |
njn | 0c7a5b5 | 2003-04-30 09:00:33 +0000 | [diff] [blame] | 148 | /* Align offset, in *bytes* */ |
| 149 | static void align_BaB ( UInt align ) |
| 150 | { |
| 151 | vg_assert(2 == align || 4 == align || 8 == align || 16 == align); |
| 152 | baB_off += (align-1); |
| 153 | baB_off &= ~(align-1); |
| 154 | } |
| 155 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 156 | /* Allocate 1 word in baseBlock and set it to the given value. */ |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 157 | static Int alloc_BaB_1_set ( Addr a ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 158 | { |
| 159 | Int off = alloc_BaB(1); |
| 160 | VG_(baseBlock)[off] = (UInt)a; |
| 161 | return off; |
| 162 | } |
| 163 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 164 | /* Registers a function in compact_helper_addrs; compact_helper_offsets is |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 165 | filled in later. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 166 | void VG_(register_compact_helper)(Addr a) |
| 167 | { |
| 168 | if (MAX_COMPACT_HELPERS <= VG_(n_compact_helpers)) { |
| 169 | VG_(printf)("Can only register %d compact helpers\n", |
| 170 | MAX_COMPACT_HELPERS); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 171 | VG_(core_panic)("Too many compact helpers registered"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 172 | } |
| 173 | VG_(compact_helper_addrs)[VG_(n_compact_helpers)] = a; |
| 174 | VG_(n_compact_helpers)++; |
| 175 | } |
| 176 | |
| 177 | /* Registers a function in noncompact_helper_addrs; noncompact_helper_offsets |
| 178 | * is filled in later. |
| 179 | */ |
| 180 | void VG_(register_noncompact_helper)(Addr a) |
| 181 | { |
| 182 | if (MAX_NONCOMPACT_HELPERS <= VG_(n_noncompact_helpers)) { |
| 183 | VG_(printf)("Can only register %d non-compact helpers\n", |
| 184 | MAX_NONCOMPACT_HELPERS); |
| 185 | VG_(printf)("Try increasing MAX_NON_COMPACT_HELPERS\n"); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 186 | VG_(core_panic)("Too many non-compact helpers registered"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 187 | } |
| 188 | VG_(noncompact_helper_addrs)[VG_(n_noncompact_helpers)] = a; |
| 189 | VG_(n_noncompact_helpers)++; |
| 190 | } |
| 191 | |
| 192 | /* Allocate offsets in baseBlock for the skin helpers */ |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 193 | static |
| 194 | void assign_helpers_in_baseBlock(UInt n, Int offsets[], Addr addrs[]) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 195 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 196 | UInt i; |
| 197 | for (i = 0; i < n; i++) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 198 | offsets[i] = alloc_BaB_1_set( addrs[i] ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 199 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 200 | |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 201 | Bool VG_(need_to_handle_esp_assignment)(void) |
| 202 | { |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 203 | return ( VG_(track_events).new_mem_stack_4 || |
| 204 | VG_(track_events).die_mem_stack_4 || |
| 205 | VG_(track_events).new_mem_stack_8 || |
| 206 | VG_(track_events).die_mem_stack_8 || |
| 207 | VG_(track_events).new_mem_stack_12 || |
| 208 | VG_(track_events).die_mem_stack_12 || |
| 209 | VG_(track_events).new_mem_stack_16 || |
| 210 | VG_(track_events).die_mem_stack_16 || |
| 211 | VG_(track_events).new_mem_stack_32 || |
| 212 | VG_(track_events).die_mem_stack_32 || |
| 213 | VG_(track_events).new_mem_stack || |
| 214 | VG_(track_events).die_mem_stack |
| 215 | ); |
njn | f4ce3d3 | 2003-02-10 10:17:26 +0000 | [diff] [blame] | 216 | } |
| 217 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 218 | /* Here we assign actual offsets. It's important to get the most |
| 219 | popular referents within 128 bytes of the start, so we can take |
| 220 | advantage of short addressing modes relative to %ebp. Popularity |
| 221 | of offsets was measured on 22 Feb 02 running a KDE application, and |
| 222 | the slots rearranged accordingly, with a 1.5% reduction in total |
| 223 | size of translations. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 224 | static void vg_init_baseBlock ( void ) |
| 225 | { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 226 | /* Those with offsets under 128 are carefully chosen. */ |
| 227 | |
| 228 | /* WORD offsets in this column */ |
| 229 | /* 0 */ VGOFF_(m_eax) = alloc_BaB(1); |
| 230 | /* 1 */ VGOFF_(m_ecx) = alloc_BaB(1); |
| 231 | /* 2 */ VGOFF_(m_edx) = alloc_BaB(1); |
| 232 | /* 3 */ VGOFF_(m_ebx) = alloc_BaB(1); |
| 233 | /* 4 */ VGOFF_(m_esp) = alloc_BaB(1); |
| 234 | /* 5 */ VGOFF_(m_ebp) = alloc_BaB(1); |
| 235 | /* 6 */ VGOFF_(m_esi) = alloc_BaB(1); |
| 236 | /* 7 */ VGOFF_(m_edi) = alloc_BaB(1); |
| 237 | /* 8 */ VGOFF_(m_eflags) = alloc_BaB(1); |
| 238 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 239 | if (VG_(needs).shadow_regs) { |
| 240 | /* 9 */ VGOFF_(sh_eax) = alloc_BaB(1); |
| 241 | /* 10 */ VGOFF_(sh_ecx) = alloc_BaB(1); |
| 242 | /* 11 */ VGOFF_(sh_edx) = alloc_BaB(1); |
| 243 | /* 12 */ VGOFF_(sh_ebx) = alloc_BaB(1); |
| 244 | /* 13 */ VGOFF_(sh_esp) = alloc_BaB(1); |
| 245 | /* 14 */ VGOFF_(sh_ebp) = alloc_BaB(1); |
| 246 | /* 15 */ VGOFF_(sh_esi) = alloc_BaB(1); |
| 247 | /* 16 */ VGOFF_(sh_edi) = alloc_BaB(1); |
| 248 | /* 17 */ VGOFF_(sh_eflags) = alloc_BaB(1); |
| 249 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 250 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 251 | /* 9,10,11 or 18,19,20... depends on number whether shadow regs are used |
| 252 | * and on compact helpers registered */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 253 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 254 | /* Make these most-frequently-called specialised ones compact, if they |
| 255 | are used. */ |
| 256 | if (VG_(track_events).new_mem_stack_4) |
| 257 | VG_(register_compact_helper)( (Addr) VG_(track_events).new_mem_stack_4); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 258 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 259 | if (VG_(track_events).die_mem_stack_4) |
| 260 | VG_(register_compact_helper)( (Addr) VG_(track_events).die_mem_stack_4); |
| 261 | |
| 262 | /* (9 or 18) + n_compact_helpers */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 263 | /* Allocate slots for compact helpers */ |
| 264 | assign_helpers_in_baseBlock(VG_(n_compact_helpers), |
| 265 | VG_(compact_helper_offsets), |
| 266 | VG_(compact_helper_addrs)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 267 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 268 | /* (9/10 or 18/19) + n_compact_helpers */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 269 | VGOFF_(m_eip) = alloc_BaB(1); |
| 270 | |
| 271 | /* There are currently 24 spill slots */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 272 | /* (11+/20+ .. 32+/43+) + n_compact_helpers. This can overlap the magic |
| 273 | * boundary at >= 32 words, but most spills are to low numbered spill |
| 274 | * slots, so the ones above the boundary don't see much action. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 275 | VGOFF_(spillslots) = alloc_BaB(VG_MAX_SPILLSLOTS); |
| 276 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 277 | /* I gave up counting at this point. Since they're above the |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 278 | short-amode-boundary, there's no point. */ |
| 279 | |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 280 | VGOFF_(m_dflag) = alloc_BaB(1); |
| 281 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 282 | /* The FPU/SSE state. This _must_ be 16-byte aligned. */ |
njn | 0c7a5b5 | 2003-04-30 09:00:33 +0000 | [diff] [blame] | 283 | align_BaB(16); |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 284 | VGOFF_(m_ssestate) = alloc_BaB(VG_SIZE_OF_SSESTATE_W); |
| 285 | vg_assert( |
| 286 | ( ((UInt)(& VG_(baseBlock)[VGOFF_(m_ssestate)])) |
| 287 | % 16 ) |
| 288 | == 0 |
| 289 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 290 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 291 | /* This thread's LDT pointer, and segment registers. */ |
| 292 | VGOFF_(ldt) = alloc_BaB(1); |
| 293 | VGOFF_(m_cs) = alloc_BaB(1); |
| 294 | VGOFF_(m_ss) = alloc_BaB(1); |
| 295 | VGOFF_(m_ds) = alloc_BaB(1); |
| 296 | VGOFF_(m_es) = alloc_BaB(1); |
| 297 | VGOFF_(m_fs) = alloc_BaB(1); |
| 298 | VGOFF_(m_gs) = alloc_BaB(1); |
| 299 | |
sewardj | e104247 | 2002-09-30 12:33:11 +0000 | [diff] [blame] | 300 | VG_(register_noncompact_helper)( (Addr) & VG_(do_useseg) ); |
| 301 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame] | 302 | #define REG(kind, size) \ |
| 303 | if (VG_(track_events).kind##_mem_stack##size) \ |
| 304 | VG_(register_noncompact_helper)( \ |
| 305 | (Addr) VG_(track_events).kind##_mem_stack##size ); |
| 306 | |
| 307 | REG(new, _8); |
| 308 | REG(new, _12); |
| 309 | REG(new, _16); |
| 310 | REG(new, _32); |
| 311 | REG(new, ); |
| 312 | REG(die, _8); |
| 313 | REG(die, _12); |
| 314 | REG(die, _16); |
| 315 | REG(die, _32); |
| 316 | REG(die, ); |
| 317 | #undef REG |
| 318 | |
| 319 | if (VG_(need_to_handle_esp_assignment)()) |
| 320 | VG_(register_noncompact_helper)((Addr) VG_(unknown_esp_update)); |
| 321 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 322 | /* Helper functions. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 323 | VGOFF_(helper_idiv_64_32) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 324 | = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_64_32)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 325 | VGOFF_(helper_div_64_32) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 326 | = alloc_BaB_1_set( (Addr) & VG_(helper_div_64_32)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 327 | VGOFF_(helper_idiv_32_16) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 328 | = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_32_16)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 329 | VGOFF_(helper_div_32_16) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 330 | = alloc_BaB_1_set( (Addr) & VG_(helper_div_32_16)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 331 | VGOFF_(helper_idiv_16_8) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 332 | = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_16_8)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 333 | VGOFF_(helper_div_16_8) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 334 | = alloc_BaB_1_set( (Addr) & VG_(helper_div_16_8)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 335 | |
| 336 | VGOFF_(helper_imul_32_64) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 337 | = alloc_BaB_1_set( (Addr) & VG_(helper_imul_32_64)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 338 | VGOFF_(helper_mul_32_64) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 339 | = alloc_BaB_1_set( (Addr) & VG_(helper_mul_32_64)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 340 | VGOFF_(helper_imul_16_32) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 341 | = alloc_BaB_1_set( (Addr) & VG_(helper_imul_16_32)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 342 | VGOFF_(helper_mul_16_32) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 343 | = alloc_BaB_1_set( (Addr) & VG_(helper_mul_16_32)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 344 | VGOFF_(helper_imul_8_16) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 345 | = alloc_BaB_1_set( (Addr) & VG_(helper_imul_8_16)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 346 | VGOFF_(helper_mul_8_16) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 347 | = alloc_BaB_1_set( (Addr) & VG_(helper_mul_8_16)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 348 | |
| 349 | VGOFF_(helper_CLD) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 350 | = alloc_BaB_1_set( (Addr) & VG_(helper_CLD)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 351 | VGOFF_(helper_STD) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 352 | = alloc_BaB_1_set( (Addr) & VG_(helper_STD)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 353 | VGOFF_(helper_get_dirflag) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 354 | = alloc_BaB_1_set( (Addr) & VG_(helper_get_dirflag)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 355 | |
sewardj | 7d78e78 | 2002-06-02 00:04:00 +0000 | [diff] [blame] | 356 | VGOFF_(helper_CLC) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 357 | = alloc_BaB_1_set( (Addr) & VG_(helper_CLC)); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 358 | VGOFF_(helper_STC) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 359 | = alloc_BaB_1_set( (Addr) & VG_(helper_STC)); |
sewardj | 7d78e78 | 2002-06-02 00:04:00 +0000 | [diff] [blame] | 360 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 361 | VGOFF_(helper_shldl) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 362 | = alloc_BaB_1_set( (Addr) & VG_(helper_shldl)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 363 | VGOFF_(helper_shldw) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 364 | = alloc_BaB_1_set( (Addr) & VG_(helper_shldw)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 365 | VGOFF_(helper_shrdl) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 366 | = alloc_BaB_1_set( (Addr) & VG_(helper_shrdl)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 367 | VGOFF_(helper_shrdw) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 368 | = alloc_BaB_1_set( (Addr) & VG_(helper_shrdw)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 369 | |
| 370 | VGOFF_(helper_RDTSC) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 371 | = alloc_BaB_1_set( (Addr) & VG_(helper_RDTSC)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 372 | VGOFF_(helper_CPUID) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 373 | = alloc_BaB_1_set( (Addr) & VG_(helper_CPUID)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 374 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 375 | VGOFF_(helper_bsf) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 376 | = alloc_BaB_1_set( (Addr) & VG_(helper_bsf)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 377 | VGOFF_(helper_bsr) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 378 | = alloc_BaB_1_set( (Addr) & VG_(helper_bsr)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 379 | |
| 380 | VGOFF_(helper_fstsw_AX) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 381 | = alloc_BaB_1_set( (Addr) & VG_(helper_fstsw_AX)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 382 | VGOFF_(helper_SAHF) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 383 | = alloc_BaB_1_set( (Addr) & VG_(helper_SAHF)); |
njn | d6251f1 | 2003-06-03 13:38:51 +0000 | [diff] [blame] | 384 | VGOFF_(helper_LAHF) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 385 | = alloc_BaB_1_set( (Addr) & VG_(helper_LAHF)); |
sewardj | 4d0ab1f | 2002-03-24 10:00:09 +0000 | [diff] [blame] | 386 | VGOFF_(helper_DAS) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 387 | = alloc_BaB_1_set( (Addr) & VG_(helper_DAS)); |
sewardj | fe8a166 | 2002-03-24 11:54:07 +0000 | [diff] [blame] | 388 | VGOFF_(helper_DAA) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 389 | = alloc_BaB_1_set( (Addr) & VG_(helper_DAA)); |
daywalker | b18d253 | 2003-09-27 20:15:01 +0000 | [diff] [blame] | 390 | VGOFF_(helper_IN) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 391 | = alloc_BaB_1_set( (Addr) & VG_(helper_IN)); |
daywalker | b18d253 | 2003-09-27 20:15:01 +0000 | [diff] [blame] | 392 | VGOFF_(helper_OUT) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 393 | = alloc_BaB_1_set( (Addr) & VG_(helper_OUT)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 394 | |
sewardj | 5109643 | 2002-12-14 23:59:09 +0000 | [diff] [blame] | 395 | VGOFF_(helper_undefined_instruction) |
daywalker | b106c42 | 2003-09-29 10:56:24 +0000 | [diff] [blame] | 396 | = alloc_BaB_1_set( (Addr) & VG_(helper_undefined_instruction)); |
sewardj | 5109643 | 2002-12-14 23:59:09 +0000 | [diff] [blame] | 397 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 398 | /* Allocate slots for noncompact helpers */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 399 | assign_helpers_in_baseBlock(VG_(n_noncompact_helpers), |
| 400 | VG_(noncompact_helper_offsets), |
| 401 | VG_(noncompact_helper_addrs)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 402 | |
njn | cc7bb47 | 2002-10-14 09:25:19 +0000 | [diff] [blame] | 403 | |
| 404 | /* Initialise slots that require it */ |
| 405 | VG_(copy_m_state_static_to_baseBlock)(); |
| 406 | |
| 407 | /* Pretend the root thread has a completely empty LDT to start with. */ |
| 408 | VG_(baseBlock)[VGOFF_(ldt)] = (UInt)NULL; |
| 409 | |
| 410 | /* Initialise shadow regs */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 411 | if (VG_(needs).shadow_regs) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 412 | VG_(baseBlock)[VGOFF_(sh_esp)] = |
| 413 | VG_(baseBlock)[VGOFF_(sh_ebp)] = |
| 414 | VG_(baseBlock)[VGOFF_(sh_eax)] = |
| 415 | VG_(baseBlock)[VGOFF_(sh_ecx)] = |
| 416 | VG_(baseBlock)[VGOFF_(sh_edx)] = |
| 417 | VG_(baseBlock)[VGOFF_(sh_ebx)] = |
| 418 | VG_(baseBlock)[VGOFF_(sh_esi)] = |
njn | d304045 | 2003-05-19 15:04:06 +0000 | [diff] [blame] | 419 | VG_(baseBlock)[VGOFF_(sh_edi)] = 0; |
| 420 | VG_(baseBlock)[VGOFF_(sh_eflags)] = 0; |
| 421 | VG_TRACK( post_regs_write_init ); |
njn | cc7bb47 | 2002-10-14 09:25:19 +0000 | [diff] [blame] | 422 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | |
| 426 | /* --------------------------------------------------------------------- |
| 427 | Global entities which are not referenced from generated code. |
| 428 | ------------------------------------------------------------------ */ |
| 429 | |
| 430 | /* The stack on which Valgrind runs. We can't use the same stack as |
| 431 | the simulatee -- that's an important design decision. */ |
njn | 6eba4ef | 2003-05-01 08:06:41 +0000 | [diff] [blame] | 432 | UInt VG_(stack)[VG_STACK_SIZE_W]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 433 | |
| 434 | /* Ditto our signal delivery stack. */ |
njn | 6eba4ef | 2003-05-01 08:06:41 +0000 | [diff] [blame] | 435 | UInt VG_(sigstack)[VG_SIGSTACK_SIZE_W]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 436 | |
| 437 | /* Saving stuff across system calls. */ |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 438 | __attribute__ ((aligned (16))) |
| 439 | UInt VG_(real_sse_state_saved_over_syscall)[VG_SIZE_OF_SSESTATE_W]; |
sewardj | 43c356f | 2002-06-02 00:21:08 +0000 | [diff] [blame] | 440 | Addr VG_(esp_saved_over_syscall); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 441 | |
| 442 | /* Counts downwards in vg_run_innerloop. */ |
| 443 | UInt VG_(dispatch_ctr); |
| 444 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 445 | |
| 446 | /* 64-bit counter for the number of basic blocks done. */ |
| 447 | ULong VG_(bbs_done); |
| 448 | /* 64-bit counter for the number of bbs to go before a debug exit. */ |
| 449 | ULong VG_(bbs_to_go); |
| 450 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 451 | /* This is the ThreadId of the last thread the scheduler ran. */ |
| 452 | ThreadId VG_(last_run_tid) = 0; |
| 453 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 454 | /* This is the argument to __NR_exit() supplied by the first thread to |
| 455 | call that syscall. We eventually pass that to __NR_exit() for |
| 456 | real. */ |
njn | 633de32 | 2003-05-12 20:40:13 +0000 | [diff] [blame] | 457 | Int VG_(exitcode) = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 458 | |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 459 | /* Tell the logging mechanism whether we are logging to a file |
| 460 | descriptor or a socket descriptor. */ |
| 461 | Bool VG_(logging_to_filedes) = True; |
| 462 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 463 | /* Is this a SSE/SSE2-capable CPU? If so, we had better save/restore |
| 464 | the SSE state all over the place. This is set up very early, in |
| 465 | vg_startup.S. We have to determine it early since we can't even |
| 466 | correctly snapshot the startup machine state without it. */ |
| 467 | /* Initially True. Safer to err on the side of SSEness and get SIGILL |
daywalker | 7e73e5f | 2003-07-04 16:18:15 +0000 | [diff] [blame] | 468 | than to not notice for some reason that we have SSE and get weird |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 469 | errors later on. */ |
| 470 | Bool VG_(have_ssestate) = True; |
| 471 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 472 | |
| 473 | /* --------------------------------------------------------------------- |
| 474 | Counters, for informational purposes only. |
| 475 | ------------------------------------------------------------------ */ |
| 476 | |
| 477 | /* Number of lookups which miss the fast tt helper. */ |
| 478 | UInt VG_(tt_fast_misses) = 0; |
| 479 | |
| 480 | |
sewardj | c0d8f68 | 2002-11-30 00:49:43 +0000 | [diff] [blame] | 481 | /* Counts for TT/TC informational messages. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 482 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 483 | /* Number and total o/t size of translations overall. */ |
| 484 | UInt VG_(overall_in_count) = 0; |
| 485 | UInt VG_(overall_in_osize) = 0; |
| 486 | UInt VG_(overall_in_tsize) = 0; |
| 487 | /* Number and total o/t size of discards overall. */ |
| 488 | UInt VG_(overall_out_count) = 0; |
| 489 | UInt VG_(overall_out_osize) = 0; |
| 490 | UInt VG_(overall_out_tsize) = 0; |
sewardj | c0d8f68 | 2002-11-30 00:49:43 +0000 | [diff] [blame] | 491 | /* The number of discards of TT/TC. */ |
| 492 | UInt VG_(number_of_tc_discards) = 0; |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 493 | /* Counts of chain and unchain operations done. */ |
| 494 | UInt VG_(bb_enchain_count) = 0; |
| 495 | UInt VG_(bb_dechain_count) = 0; |
| 496 | /* Number of unchained jumps performed. */ |
| 497 | UInt VG_(unchained_jumps_done) = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 498 | |
| 499 | |
| 500 | /* Counts pertaining to the register allocator. */ |
| 501 | |
| 502 | /* total number of uinstrs input to reg-alloc */ |
| 503 | UInt VG_(uinstrs_prealloc) = 0; |
| 504 | |
| 505 | /* total number of uinstrs added due to spill code */ |
| 506 | UInt VG_(uinstrs_spill) = 0; |
| 507 | |
| 508 | /* number of bbs requiring spill code */ |
| 509 | UInt VG_(translations_needing_spill) = 0; |
| 510 | |
| 511 | /* total of register ranks over all translations */ |
| 512 | UInt VG_(total_reg_rank) = 0; |
| 513 | |
| 514 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 515 | /* Counts pertaining to internal sanity checking. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 516 | UInt VG_(sanity_fast_count) = 0; |
| 517 | UInt VG_(sanity_slow_count) = 0; |
| 518 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 519 | /* Counts pertaining to the scheduler. */ |
| 520 | UInt VG_(num_scheduling_events_MINOR) = 0; |
| 521 | UInt VG_(num_scheduling_events_MAJOR) = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 522 | |
| 523 | |
| 524 | /* --------------------------------------------------------------------- |
| 525 | Values derived from command-line options. |
| 526 | ------------------------------------------------------------------ */ |
| 527 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 528 | /* Define, and set defaults. */ |
| 529 | Bool VG_(clo_error_limit) = True; |
| 530 | Bool VG_(clo_GDB_attach) = False; |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 531 | Char* VG_(clo_GDB_path) = GDB_PATH; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 532 | Bool VG_(clo_gen_suppressions) = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 533 | Int VG_(sanity_level) = 1; |
| 534 | Int VG_(clo_verbosity) = 1; |
| 535 | Bool VG_(clo_demangle) = True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 536 | Bool VG_(clo_trace_children) = False; |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 537 | |
| 538 | /* See big comment in vg_include.h for meaning of these three. */ |
| 539 | VgLogTo VG_(clo_log_to) = VgLogTo_Fd; |
| 540 | Int VG_(clo_logfile_fd) = 2; |
| 541 | Char* VG_(clo_logfile_name) = NULL; |
| 542 | |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 543 | Int VG_(clo_input_fd) = 0; /* stdin */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 544 | Int VG_(clo_n_suppressions) = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 545 | Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 546 | Bool VG_(clo_profile) = False; |
| 547 | Bool VG_(clo_single_step) = False; |
| 548 | Bool VG_(clo_optimise) = True; |
| 549 | UChar VG_(clo_trace_codegen) = 0; // 00000000b |
| 550 | Bool VG_(clo_trace_syscalls) = False; |
| 551 | Bool VG_(clo_trace_signals) = False; |
| 552 | Bool VG_(clo_trace_symtab) = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 553 | Bool VG_(clo_trace_sched) = False; |
| 554 | Int VG_(clo_trace_pthread_level) = 0; |
sewardj | 59438c0 | 2003-01-05 12:16:30 +0000 | [diff] [blame] | 555 | ULong VG_(clo_stop_after) = 1000000000000000LL; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 556 | Int VG_(clo_dump_error) = 0; |
| 557 | Int VG_(clo_backtrace_size) = 4; |
| 558 | Char* VG_(clo_weird_hacks) = NULL; |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 559 | Bool VG_(clo_run_libc_freeres) = True; |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame^] | 560 | Bool VG_(clo_track_fds) = False; |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 561 | Bool VG_(clo_chain_bb) = True; |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 562 | Bool VG_(clo_show_below_main) = False; |
sewardj | 2370f3b | 2002-11-30 15:01:01 +0000 | [diff] [blame] | 563 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 564 | static Bool VG_(clo_wait_for_gdb) = False; |
| 565 | |
| 566 | /* If we're doing signal routing, poll for signals every 50mS by |
| 567 | default. */ |
| 568 | Int VG_(clo_signal_polltime) = 50; |
| 569 | |
fitzhardinge | 89f9a32 | 2003-10-30 07:25:59 +0000 | [diff] [blame] | 570 | /* If true, assume we're running on a plain 2.4 kernel */ |
| 571 | Bool VG_(clo_assume_24) = False; |
| 572 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 573 | /* These flags reduce thread wakeup latency on syscall completion and |
| 574 | signal delivery, respectively. The downside is possible unfairness. */ |
| 575 | Bool VG_(clo_lowlat_syscalls) = False; /* low-latency syscalls */ |
| 576 | Bool VG_(clo_lowlat_signals) = False; /* low-latency signals */ |
| 577 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 578 | /* This Bool is needed by wrappers in vg_clientmalloc.c to decide how |
| 579 | to behave. Initially we say False. */ |
| 580 | Bool VG_(running_on_simd_CPU) = False; |
| 581 | |
| 582 | /* Holds client's %esp at the point we gained control. */ |
| 583 | Addr VG_(esp_at_startup); |
| 584 | |
sewardj | d5815ec | 2003-04-06 12:23:27 +0000 | [diff] [blame] | 585 | /* Indicates presence, and holds address of client's sysinfo page, a |
| 586 | feature of some modern kernels used to provide vsyscalls, etc. */ |
| 587 | Bool VG_(sysinfo_page_exists) = False; |
| 588 | Addr VG_(sysinfo_page_addr) = 0; |
| 589 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 590 | /* As deduced from VG_(esp_at_startup), the client's argc, argv[] and |
| 591 | envp[] as extracted from the client's stack at startup-time. */ |
| 592 | Int VG_(client_argc); |
| 593 | Char** VG_(client_argv); |
| 594 | Char** VG_(client_envp); |
| 595 | |
| 596 | /* A place into which to copy the value of env var VG_ARGS, so we |
| 597 | don't have to modify the original. */ |
| 598 | static Char vg_cmdline_copy[M_VG_CMDLINE_STRLEN]; |
| 599 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 600 | /* --------------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 601 | Processing of command-line options. |
| 602 | ------------------------------------------------------------------ */ |
| 603 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 604 | void VG_(bad_option) ( Char* opt ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 605 | { |
| 606 | VG_(shutdown_logging)(); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 607 | VG_(clo_log_to) = VgLogTo_Fd; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 608 | VG_(clo_logfile_fd) = 2; /* stderr */ |
| 609 | VG_(printf)("valgrind.so: Bad option `%s'; aborting.\n", opt); |
| 610 | VG_(exit)(1); |
| 611 | } |
| 612 | |
| 613 | static void config_error ( Char* msg ) |
| 614 | { |
| 615 | VG_(shutdown_logging)(); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 616 | VG_(clo_log_to) = VgLogTo_Fd; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 617 | VG_(clo_logfile_fd) = 2; /* stderr */ |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 618 | VG_(printf)( |
| 619 | "valgrind.so: Startup or configuration error:\n %s\n", msg); |
| 620 | VG_(printf)( |
| 621 | "valgrind.so: Unable to start up properly. Giving up.\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 622 | VG_(exit)(1); |
| 623 | } |
| 624 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 625 | static void args_grok_error ( Char* msg ) |
| 626 | { |
| 627 | VG_(shutdown_logging)(); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 628 | VG_(clo_log_to) = VgLogTo_Fd; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 629 | VG_(clo_logfile_fd) = 2; /* stderr */ |
| 630 | VG_(printf)("valgrind.so: When searching for " |
| 631 | "client's argc/argc/envp:\n\t%s\n", msg); |
| 632 | config_error("couldn't find client's argc/argc/envp"); |
| 633 | } |
| 634 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 635 | static void usage ( void ) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 636 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 637 | Char* usage1 = |
| 638 | "usage: valgrind [options] prog-and-args\n" |
| 639 | "\n" |
| 640 | " core user options, with defaults in [ ], are:\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 641 | " --tool=<name> Use the Valgrind tool named <name> [memcheck]\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 642 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 643 | " --help show this message\n" |
| 644 | " --version show version\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 645 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 646 | " -q --quiet run silently; only print error msgs\n" |
| 647 | " -v --verbose be more verbose, incl counts of errors\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 648 | |
| 649 | " --trace-children=no|yes Valgrind-ise child processes? [no]\n" |
| 650 | |
| 651 | " --logfile-fd=<number> file descriptor for messages [2=stderr]\n" |
| 652 | " --logfile=<file> log messages to <file>.pid<pid>\n" |
| 653 | " --logsocket=ipaddr:port log messages to socket ipaddr:port\n" |
| 654 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 655 | " --demangle=no|yes automatically demangle C++ names? [yes]\n" |
| 656 | " --num-callers=<number> show <num> callers in stack traces [4]\n" |
| 657 | " --error-limit=no|yes stop showing new errors if too many? [yes]\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 658 | " --show-below-main=no|yes continue stack traces below main() [no]\n" |
| 659 | " --suppressions=<filename> suppress errors described in <filename>\n" |
| 660 | " --gen-suppressions=no|yes print suppressions for errors detected [no]\n" |
| 661 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame^] | 662 | " --track-fds=no|yes Track open file descriptors? [no]\n" |
| 663 | |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 664 | " --gdb-attach=no|yes start GDB when errors detected? [no]\n" |
| 665 | " --gdb-path=/path/to/gdb path to the GDB to use [/usr/bin/gdb]\n" |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 666 | " --input-fd=<number> file descriptor for (gdb) input [0=stdin]\n" |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 667 | |
| 668 | " --run-libc-freeres=no|yes Free up glibc memory at exit? [yes]\n" |
| 669 | " --weird-hacks=hack1,hack2,... [none]\n" |
sewardj | 3d652a3 | 2002-10-20 18:11:49 +0000 | [diff] [blame] | 670 | " recognised hacks are: ioctl-VTIME truncate-writes lax-ioctls\n" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 671 | " --signal-polltime=<time> time, in mS, we should poll for signals.\n" |
| 672 | " Only applies for older kernels which need\n" |
| 673 | " signal routing [50]\n" |
| 674 | " --lowlat-signals=no|yes improve wake-up latency when a thread receives\n" |
| 675 | " a signal [no]\n" |
| 676 | " --lowlat-syscalls=no|yes improve wake-up latency when a thread's\n" |
| 677 | " syscall completes [no]\n" |
fitzhardinge | 89f9a32 | 2003-10-30 07:25:59 +0000 | [diff] [blame] | 678 | " --assume-2.4=no|yes assume we're running on a 2.4 kernel [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 679 | "\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 680 | " %s tool user options:\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 681 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 682 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 683 | Char* usage2 = |
| 684 | "\n" |
| 685 | " core options for debugging Valgrind itself are:\n" |
| 686 | " --sanity-level=<number> level of sanity checking to do [1]\n" |
| 687 | " --single-step=no|yes translate each instr separately? [no]\n" |
| 688 | " --optimise=no|yes improve intermediate code? [yes]\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 689 | " --profile=no|yes profile? (tool must be built for it) [no]\n" |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 690 | " --chain-bb=no|yes do basic-block chaining? [yes]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 691 | " --trace-codegen=<XXXXX> show generated code? (X = 0|1) [00000]\n" |
| 692 | " --trace-syscalls=no|yes show all system calls? [no]\n" |
| 693 | " --trace-signals=no|yes show signal handling details? [no]\n" |
| 694 | " --trace-symtab=no|yes show symbol table details? [no]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 695 | " --trace-sched=no|yes show thread scheduler details? [no]\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 696 | " --trace-pthread=none|some|all show pthread event details? [none]\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 697 | " --stop-after=<number> switch to real CPU after executing\n" |
| 698 | " <number> basic blocks [infinity]\n" |
| 699 | " --dump-error=<number> show translation for basic block\n" |
| 700 | " associated with <number>'th\n" |
| 701 | " error context [0=don't show any]\n" |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 702 | " --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] | 703 | "\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 704 | " %s tool debugging options:\n"; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 705 | |
| 706 | Char* usage3 = |
| 707 | "\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 708 | " Extra options are read from env variable $VALGRIND_OPTS\n" |
| 709 | "\n" |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 710 | " Valgrind is Copyright (C) 2000-2003 Julian Seward\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 711 | " and licensed under the GNU General Public License, version 2.\n" |
| 712 | " Bug reports, feedback, admiration, abuse, etc, to: %s.\n" |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 713 | "\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 714 | " Tools are copyright and licensed by their authors. See each\n" |
| 715 | " tool's start-up message for more information.\n" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 716 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 717 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 718 | VG_(printf)(usage1, VG_(details).name); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 719 | /* Don't print skin string directly for security, ha! */ |
| 720 | if (VG_(needs).command_line_options) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 721 | SK_(print_usage)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 722 | else |
| 723 | VG_(printf)(" (none)\n"); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 724 | VG_(printf)(usage2, VG_(details).name); |
| 725 | if (VG_(needs).command_line_options) |
| 726 | SK_(print_debug_usage)(); |
| 727 | else |
| 728 | VG_(printf)(" (none)\n"); |
| 729 | VG_(printf)(usage3, VG_EMAIL_ADDR); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 730 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 731 | VG_(shutdown_logging)(); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 732 | VG_(clo_log_to) = VgLogTo_Fd; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 733 | VG_(clo_logfile_fd) = 2; /* stderr */ |
| 734 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 735 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 736 | |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 737 | |
sewardj | 49e630d | 2003-04-23 21:18:52 +0000 | [diff] [blame] | 738 | /* Callback for looking for the stack segment. */ |
| 739 | Addr VG_(foundstack_start) = (Addr)NULL; |
| 740 | UInt VG_(foundstack_size) = 0; |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 741 | |
| 742 | static void vg_findstack_callback ( Addr start, UInt size, |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 743 | Char r, Char w, Char x, |
| 744 | UInt foffset, UChar* filename ) |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 745 | { |
| 746 | Addr lastword; |
| 747 | if (size == 0) return; |
sewardj | 04421d0 | 2003-02-23 03:54:59 +0000 | [diff] [blame] | 748 | if (r != 'r' || w != 'w' |
| 749 | /* || x != 'x' --not necessarily so on x86-64*/ |
| 750 | ) return; |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 751 | lastword = start + size - 4; |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 752 | if (start <= VG_(esp_at_startup) |
| 753 | && VG_(esp_at_startup) <= lastword) { |
sewardj | 49e630d | 2003-04-23 21:18:52 +0000 | [diff] [blame] | 754 | VG_(foundstack_start) = start; |
| 755 | VG_(foundstack_size) = size; |
| 756 | vg_assert(VG_(foundstack_size) > 0); |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
| 760 | |
| 761 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 762 | static void process_cmd_line_options ( void ) |
| 763 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 764 | Char* argv[M_VG_CMDLINE_OPTS]; |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 765 | Int argc; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 766 | Char* p; |
| 767 | Char* str; |
| 768 | Int i, eventually_logfile_fd, ctr; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 769 | |
| 770 | # define ISSPACE(cc) ((cc) == ' ' || (cc) == '\t' || (cc) == '\n') |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 771 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 772 | eventually_logfile_fd = VG_(clo_logfile_fd); |
| 773 | |
| 774 | /* Once logging is started, we can safely send messages pertaining |
| 775 | to failures in initialisation. */ |
| 776 | VG_(startup_logging)(); |
| 777 | |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 778 | /* Check for sane path in ./configure --prefix=... */ |
| 779 | if (VG_(strlen)(VG_LIBDIR) < 1 |
| 780 | || VG_LIBDIR[0] != '/') |
| 781 | config_error("Please use absolute paths in " |
| 782 | "./configure --prefix=... or --libdir=..."); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 783 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 784 | /* (Suggested by Fabrice Bellard ... ) |
| 785 | We look for the Linux ELF table and go down until we find the |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 786 | envc & envp. It is not fool-proof, but these structures should |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 787 | change less often than the libc ones. */ |
| 788 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 789 | Int* sp; |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 790 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 791 | /* Look for the stack segment by parsing /proc/self/maps and |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 792 | looking for a section bracketing VG_(esp_at_startup) which |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 793 | has rwx permissions and no associated file. Note that this uses |
| 794 | the /proc/self/maps contents read at the start of VG_(main)(), |
| 795 | and doesn't re-read /proc/self/maps. */ |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 796 | |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 797 | VG_(parse_procselfmaps)( vg_findstack_callback ); |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 798 | |
sewardj | 49e630d | 2003-04-23 21:18:52 +0000 | [diff] [blame] | 799 | /* Now foundstack_start and foundstack_size should delimit the stack. */ |
| 800 | if (VG_(foundstack_size) == 0) { |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 801 | args_grok_error("Cannot determine stack segment " |
| 802 | "from /proc/self/maps"); |
| 803 | } |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 804 | |
| 805 | if (0) |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 806 | VG_(printf)("stack segment is %p .. %p\n", |
sewardj | 49e630d | 2003-04-23 21:18:52 +0000 | [diff] [blame] | 807 | VG_(foundstack_start), |
| 808 | VG_(foundstack_start) + VG_(foundstack_size) - 4 ); |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 809 | |
sewardj | 49e630d | 2003-04-23 21:18:52 +0000 | [diff] [blame] | 810 | sp = (UInt*)(VG_(foundstack_start) + VG_(foundstack_size) ); |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 811 | if ((((UInt)(sp)) % VKI_BYTES_PER_PAGE) != 0) { |
| 812 | args_grok_error("Stack segment is not page aligned?!"); |
| 813 | } |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 814 | |
sewardj | 53fe458 | 2003-02-23 01:41:17 +0000 | [diff] [blame] | 815 | /* we locate: NEW_AUX_ENT(1, AT_PAGESZ, ELF_EXEC_PAGESIZE) in |
| 816 | the elf interpreter table */ |
sewardj | 1c1b116 | 2003-02-23 01:25:51 +0000 | [diff] [blame] | 817 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 818 | sp -= 2; |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 819 | while (sp[0] != VKI_AT_PAGESZ || sp[1] != 4096) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 820 | /* VG_(printf)("trying %p\n", sp); */ |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 821 | sp--; |
| 822 | } |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 823 | |
| 824 | if (sp[2] == VKI_AT_BASE |
| 825 | && sp[0] == VKI_AT_PAGESZ |
| 826 | && sp[-2] == VKI_AT_PHNUM |
| 827 | && sp[-4] == VKI_AT_PHENT |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 828 | && sp[-6] == VKI_AT_PHDR |
| 829 | && sp[-6-1] == 0) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 830 | if (0) |
| 831 | VG_(printf)("Looks like you've got a 2.2.X kernel here.\n"); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 832 | sp -= 6; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 833 | } else |
| 834 | if (sp[2] == VKI_AT_CLKTCK |
| 835 | && sp[0] == VKI_AT_PAGESZ |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 836 | && sp[-2] == VKI_AT_HWCAP |
| 837 | && sp[-2-1] == 0) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 838 | if (0) |
| 839 | VG_(printf)("Looks like you've got a 2.4.X kernel here.\n"); |
| 840 | sp -= 2; |
| 841 | } else |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 842 | if (sp[2] == VKI_AT_CLKTCK |
| 843 | && sp[0] == VKI_AT_PAGESZ |
| 844 | && sp[-2] == VKI_AT_HWCAP |
sewardj | d5815ec | 2003-04-06 12:23:27 +0000 | [diff] [blame] | 845 | && sp[-4] == VKI_AT_SYSINFO |
| 846 | && sp[-4-1] == 0) { |
| 847 | if (0) |
| 848 | VG_(printf)("Looks like you've got a 2.4.X kernel with " |
| 849 | "a sysinfo page at %x here.\n", sp[-3]); |
| 850 | VG_(sysinfo_page_exists) = True; |
| 851 | VG_(sysinfo_page_addr) = sp[-3]; |
| 852 | sp -= 4; |
| 853 | } else |
| 854 | if (sp[2] == VKI_AT_CLKTCK |
| 855 | && sp[0] == VKI_AT_PAGESZ |
| 856 | && sp[-2] == VKI_AT_HWCAP |
sewardj | ce61d05 | 2002-07-25 00:49:51 +0000 | [diff] [blame] | 857 | && sp[-4] == VKI_AT_USER_AUX_SEGMENT |
| 858 | && sp[-4-1] == 0) { |
| 859 | if (0) |
| 860 | VG_(printf)("Looks like you've got a R-H Limbo 2.4.X " |
| 861 | "kernel here.\n"); |
| 862 | sp -= 4; |
| 863 | } else |
| 864 | if (sp[2] == VKI_AT_CLKTCK |
| 865 | && sp[0] == VKI_AT_PAGESZ |
| 866 | && sp[-2] == VKI_AT_HWCAP |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 867 | && sp[-2-20-1] == 0) { |
| 868 | if (0) |
| 869 | VG_(printf)("Looks like you've got a early 2.4.X kernel here.\n"); |
| 870 | sp -= 22; |
| 871 | } else |
sewardj | 8fdbeed | 2003-02-23 03:09:33 +0000 | [diff] [blame] | 872 | if (sp[2] == VKI_AT_CLKTCK |
| 873 | && sp[0] == VKI_AT_PAGESZ |
| 874 | && sp[-2] == VKI_AT_HWCAP |
| 875 | && sp[-4-1] == 0) { |
| 876 | if (0) |
sewardj | 8c4e6b1 | 2003-04-26 21:49:40 +0000 | [diff] [blame] | 877 | VG_(printf)("Looks like a 2.5.43-2.5.67 kernel here.\n"); |
sewardj | 8fdbeed | 2003-02-23 03:09:33 +0000 | [diff] [blame] | 878 | sp -= 4; |
| 879 | } else |
sewardj | 8c4e6b1 | 2003-04-26 21:49:40 +0000 | [diff] [blame] | 880 | if (sp[2] == VKI_AT_CLKTCK |
| 881 | && sp[0] == VKI_AT_PAGESZ |
| 882 | && sp[-2] == VKI_AT_HWCAP |
| 883 | && sp[-6] == VKI_AT_SYSINFO |
| 884 | && sp[-6-1] == 0) { |
| 885 | if (0) |
| 886 | VG_(printf)("Looks like a >= 2.5.68 kernel with " |
| 887 | "a sysinfo page at %x here.\n", sp[-5]); |
| 888 | VG_(sysinfo_page_exists) = True; |
| 889 | VG_(sysinfo_page_addr) = sp[-5]; |
| 890 | sp -= 6; |
| 891 | } else |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 892 | args_grok_error( |
| 893 | "ELF frame does not look like 2.2.X or 2.4.X.\n " |
| 894 | "See kernel sources linux/fs/binfmt_elf.c to make sense of this." |
| 895 | ); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 896 | |
| 897 | sp--; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 898 | if (*sp != 0) |
| 899 | args_grok_error("can't find NULL at end of env[]"); |
| 900 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 901 | /* sp now points to NULL at the end of env[] */ |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 902 | ctr = 0; |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 903 | while (True) { |
| 904 | sp --; |
| 905 | if (*sp == 0) break; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 906 | if (++ctr >= 1000) |
| 907 | args_grok_error( |
| 908 | "suspiciously many (1000) env[] entries; giving up"); |
| 909 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 910 | } |
| 911 | /* sp now points to NULL at the end of argv[] */ |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 912 | VG_(client_envp) = (Char**)(sp+1); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 913 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 914 | ctr = 0; |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 915 | VG_(client_argc) = 0; |
| 916 | while (True) { |
| 917 | sp--; |
| 918 | if (*sp == VG_(client_argc)) |
| 919 | break; |
| 920 | VG_(client_argc)++; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 921 | if (++ctr >= 1000) |
| 922 | args_grok_error( |
| 923 | "suspiciously many (1000) argv[] entries; giving up"); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 924 | } |
| 925 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 926 | VG_(client_argv) = (Char**)(sp+1); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 927 | } |
| 928 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 929 | /* Now that VG_(client_envp) has been set, we can extract the args |
| 930 | for Valgrind itself. Copy into global var so that we don't have to |
| 931 | write zeroes to the getenv'd value itself. */ |
| 932 | str = VG_(getenv)("VG_ARGS"); |
| 933 | argc = 0; |
| 934 | |
| 935 | if (!str) { |
| 936 | config_error("Can't read options from env var VG_ARGS."); |
| 937 | } |
| 938 | |
| 939 | if (VG_(strlen)(str) >= M_VG_CMDLINE_STRLEN-1) { |
| 940 | config_error("Command line length exceeds M_CMDLINE_STRLEN."); |
| 941 | } |
| 942 | VG_(strcpy)(vg_cmdline_copy, str); |
| 943 | str = NULL; |
| 944 | |
| 945 | p = &vg_cmdline_copy[0]; |
| 946 | while (True) { |
| 947 | while (ISSPACE(*p)) { *p = 0; p++; } |
| 948 | if (*p == 0) break; |
| 949 | if (argc < M_VG_CMDLINE_OPTS-1) { |
| 950 | argv[argc] = p; argc++; |
| 951 | } else { |
| 952 | config_error( |
| 953 | "Found more than M_CMDLINE_OPTS command-line opts."); |
| 954 | } |
| 955 | while (*p != 0 && !ISSPACE(*p)) p++; |
| 956 | } |
| 957 | |
| 958 | for (i = 0; i < argc; i++) { |
| 959 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 960 | if (VG_CLO_STREQ(argv[i], "-v") || |
| 961 | VG_CLO_STREQ(argv[i], "--verbose")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 962 | VG_(clo_verbosity)++; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 963 | else if (VG_CLO_STREQ(argv[i], "-q") || |
| 964 | VG_CLO_STREQ(argv[i], "--quiet")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 965 | VG_(clo_verbosity)--; |
| 966 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 967 | else if (VG_CLO_STREQ(argv[i], "--error-limit=yes")) |
sewardj | 2e43290 | 2002-06-13 20:44:00 +0000 | [diff] [blame] | 968 | VG_(clo_error_limit) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 969 | else if (VG_CLO_STREQ(argv[i], "--error-limit=no")) |
sewardj | 2e43290 | 2002-06-13 20:44:00 +0000 | [diff] [blame] | 970 | VG_(clo_error_limit) = False; |
sewardj | 72f98ff | 2002-06-13 17:23:38 +0000 | [diff] [blame] | 971 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 972 | else if (VG_CLO_STREQ(argv[i], "--gdb-attach=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 973 | VG_(clo_GDB_attach) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 974 | else if (VG_CLO_STREQ(argv[i], "--gdb-attach=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 975 | VG_(clo_GDB_attach) = False; |
| 976 | |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 977 | else if (VG_CLO_STREQN(11,argv[i], "--gdb-path=")) |
| 978 | VG_(clo_GDB_path) = &argv[i][11]; |
| 979 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 980 | else if (VG_CLO_STREQ(argv[i], "--gen-suppressions=yes")) |
| 981 | VG_(clo_gen_suppressions) = True; |
| 982 | else if (VG_CLO_STREQ(argv[i], "--gen-suppressions=no")) |
| 983 | VG_(clo_gen_suppressions) = False; |
| 984 | |
nethercote | 77eba60 | 2003-11-13 17:35:04 +0000 | [diff] [blame] | 985 | else if (VG_CLO_STREQ(argv[i], "--show-below-main=yes")) |
| 986 | VG_(clo_show_below_main) = True; |
| 987 | else if (VG_CLO_STREQ(argv[i], "--show-below-main=no")) |
| 988 | VG_(clo_show_below_main) = False; |
| 989 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 990 | else if (VG_CLO_STREQ(argv[i], "--demangle=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 991 | VG_(clo_demangle) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 992 | else if (VG_CLO_STREQ(argv[i], "--demangle=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 993 | VG_(clo_demangle) = False; |
| 994 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 995 | else if (VG_CLO_STREQ(argv[i], "--trace-children=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 996 | VG_(clo_trace_children) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 997 | else if (VG_CLO_STREQ(argv[i], "--trace-children=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 998 | VG_(clo_trace_children) = False; |
| 999 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1000 | else if (VG_CLO_STREQ(argv[i], "--run-libc-freeres=yes")) |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1001 | VG_(clo_run_libc_freeres) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1002 | else if (VG_CLO_STREQ(argv[i], "--run-libc-freeres=no")) |
sewardj | 858964b | 2002-10-05 14:15:43 +0000 | [diff] [blame] | 1003 | VG_(clo_run_libc_freeres) = False; |
| 1004 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame^] | 1005 | else if (VG_CLO_STREQ(argv[i], "--track-fds=yes")) |
| 1006 | VG_(clo_track_fds) = True; |
| 1007 | else if (VG_CLO_STREQ(argv[i], "--track-fds=no")) |
| 1008 | VG_(clo_track_fds) = False; |
| 1009 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1010 | else if (VG_CLO_STREQN(15, argv[i], "--sanity-level=")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1011 | VG_(sanity_level) = (Int)VG_(atoll)(&argv[i][15]); |
| 1012 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1013 | else if (VG_CLO_STREQN(13, argv[i], "--logfile-fd=")) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1014 | VG_(clo_log_to) = VgLogTo_Fd; |
| 1015 | VG_(clo_logfile_name) = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1016 | eventually_logfile_fd = (Int)VG_(atoll)(&argv[i][13]); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1019 | else if (VG_CLO_STREQN(10, argv[i], "--logfile=")) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1020 | VG_(clo_log_to) = VgLogTo_File; |
| 1021 | VG_(clo_logfile_name) = &argv[i][10]; |
| 1022 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1023 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1024 | else if (VG_CLO_STREQN(12, argv[i], "--logsocket=")) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1025 | VG_(clo_log_to) = VgLogTo_Socket; |
| 1026 | VG_(clo_logfile_name) = &argv[i][12]; |
| 1027 | } |
| 1028 | |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 1029 | else if (VG_CLO_STREQN(11, argv[i], "--input-fd=")) |
| 1030 | VG_(clo_input_fd) = (Int)VG_(atoll)(&argv[i][11]); |
| 1031 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1032 | else if (VG_CLO_STREQN(15, argv[i], "--suppressions=")) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1033 | if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1034 | VG_(message)(Vg_UserMsg, "Too many suppression files specified."); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1035 | VG_(message)(Vg_UserMsg, |
| 1036 | "Increase VG_CLO_MAX_SFILES and recompile."); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1037 | VG_(bad_option)(argv[i]); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1038 | } |
| 1039 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = &argv[i][15]; |
| 1040 | VG_(clo_n_suppressions)++; |
| 1041 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1042 | else if (VG_CLO_STREQ(argv[i], "--profile=yes")) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1043 | VG_(clo_profile) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1044 | else if (VG_CLO_STREQ(argv[i], "--profile=no")) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1045 | VG_(clo_profile) = False; |
| 1046 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1047 | else if (VG_CLO_STREQ(argv[i], "--chain-bb=yes")) |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1048 | VG_(clo_chain_bb) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1049 | else if (VG_CLO_STREQ(argv[i], "--chain-bb=no")) |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1050 | VG_(clo_chain_bb) = False; |
| 1051 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1052 | else if (VG_CLO_STREQ(argv[i], "--single-step=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1053 | VG_(clo_single_step) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1054 | else if (VG_CLO_STREQ(argv[i], "--single-step=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1055 | VG_(clo_single_step) = False; |
| 1056 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1057 | else if (VG_CLO_STREQ(argv[i], "--optimise=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1058 | VG_(clo_optimise) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1059 | else if (VG_CLO_STREQ(argv[i], "--optimise=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1060 | VG_(clo_optimise) = False; |
| 1061 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1062 | /* "vwxyz" --> 000zyxwv (binary) */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1063 | else if (VG_CLO_STREQN(16, argv[i], "--trace-codegen=")) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1064 | Int j; |
| 1065 | char* opt = & argv[i][16]; |
| 1066 | |
| 1067 | if (5 != VG_(strlen)(opt)) { |
| 1068 | VG_(message)(Vg_UserMsg, |
| 1069 | "--trace-codegen argument must have 5 digits"); |
| 1070 | VG_(bad_option)(argv[i]); |
| 1071 | } |
| 1072 | for (j = 0; j < 5; j++) { |
| 1073 | if ('0' == opt[j]) { /* do nothing */ } |
| 1074 | else if ('1' == opt[j]) VG_(clo_trace_codegen) |= (1 << j); |
| 1075 | else { |
| 1076 | VG_(message)(Vg_UserMsg, "--trace-codegen argument can only " |
| 1077 | "contain 0s and 1s"); |
| 1078 | VG_(bad_option)(argv[i]); |
| 1079 | } |
| 1080 | } |
| 1081 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1082 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1083 | else if (VG_CLO_STREQ(argv[i], "--trace-syscalls=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1084 | VG_(clo_trace_syscalls) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1085 | else if (VG_CLO_STREQ(argv[i], "--trace-syscalls=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1086 | VG_(clo_trace_syscalls) = False; |
| 1087 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1088 | else if (VG_CLO_STREQ(argv[i], "--trace-signals=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1089 | VG_(clo_trace_signals) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1090 | else if (VG_CLO_STREQ(argv[i], "--trace-signals=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1091 | VG_(clo_trace_signals) = False; |
| 1092 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1093 | else if (VG_CLO_STREQ(argv[i], "--trace-symtab=yes")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1094 | VG_(clo_trace_symtab) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1095 | else if (VG_CLO_STREQ(argv[i], "--trace-symtab=no")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1096 | VG_(clo_trace_symtab) = False; |
| 1097 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1098 | else if (VG_CLO_STREQ(argv[i], "--trace-sched=yes")) |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1099 | VG_(clo_trace_sched) = True; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1100 | else if (VG_CLO_STREQ(argv[i], "--trace-sched=no")) |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1101 | VG_(clo_trace_sched) = False; |
| 1102 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1103 | else if (VG_CLO_STREQ(argv[i], "--trace-pthread=none")) |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1104 | VG_(clo_trace_pthread_level) = 0; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1105 | else if (VG_CLO_STREQ(argv[i], "--trace-pthread=some")) |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1106 | VG_(clo_trace_pthread_level) = 1; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1107 | else if (VG_CLO_STREQ(argv[i], "--trace-pthread=all")) |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1108 | VG_(clo_trace_pthread_level) = 2; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 1109 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1110 | else if (VG_CLO_STREQN(14, argv[i], "--weird-hacks=")) |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 1111 | VG_(clo_weird_hacks) = &argv[i][14]; |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 1112 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1113 | else if (VG_CLO_STREQN(17, argv[i], "--signal-polltime=")) |
| 1114 | VG_(clo_signal_polltime) = VG_(atoll)(&argv[i][17]); |
| 1115 | |
| 1116 | else if (VG_CLO_STREQ(argv[i], "--lowlat-signals=yes")) |
| 1117 | VG_(clo_lowlat_signals) = True; |
| 1118 | else if (VG_CLO_STREQ(argv[i], "--lowlat-signals=no")) |
| 1119 | VG_(clo_lowlat_signals) = False; |
| 1120 | |
| 1121 | else if (VG_CLO_STREQ(argv[i], "--lowlat-syscalls=yes")) |
| 1122 | VG_(clo_lowlat_syscalls) = True; |
| 1123 | else if (VG_CLO_STREQ(argv[i], "--lowlat-syscalls=no")) |
| 1124 | VG_(clo_lowlat_syscalls) = False; |
| 1125 | |
fitzhardinge | 89f9a32 | 2003-10-30 07:25:59 +0000 | [diff] [blame] | 1126 | else if (VG_CLO_STREQ(argv[i], "--assume-2.4=yes")) |
| 1127 | VG_(clo_assume_24) = True; |
| 1128 | else if (VG_CLO_STREQ(argv[i], "--assume-2.4=no")) |
| 1129 | VG_(clo_assume_24) = False; |
| 1130 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1131 | else if (VG_CLO_STREQN(13, argv[i], "--stop-after=")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1132 | VG_(clo_stop_after) = VG_(atoll)(&argv[i][13]); |
| 1133 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1134 | else if (VG_CLO_STREQN(13, argv[i], "--dump-error=")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1135 | VG_(clo_dump_error) = (Int)VG_(atoll)(&argv[i][13]); |
| 1136 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1137 | else if (VG_CLO_STREQ(argv[i], "--wait-for-gdb=yes")) |
| 1138 | VG_(clo_wait_for_gdb) = True; |
| 1139 | else if (VG_CLO_STREQ(argv[i], "--wait-for-gdb=no")) |
| 1140 | VG_(clo_wait_for_gdb) = False; |
| 1141 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1142 | else if (VG_CLO_STREQN(14, argv[i], "--num-callers=")) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1143 | /* Make sure it's sane. */ |
| 1144 | VG_(clo_backtrace_size) = (Int)VG_(atoll)(&argv[i][14]); |
njn | 6c84655 | 2003-09-16 07:41:43 +0000 | [diff] [blame] | 1145 | if (VG_(clo_backtrace_size) < 1) |
| 1146 | VG_(clo_backtrace_size) = 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1147 | if (VG_(clo_backtrace_size) >= VG_DEEPEST_BACKTRACE) |
| 1148 | VG_(clo_backtrace_size) = VG_DEEPEST_BACKTRACE; |
| 1149 | } |
| 1150 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1151 | else if (VG_(needs).command_line_options) { |
| 1152 | Bool ok = SK_(process_cmd_line_option)(argv[i]); |
| 1153 | if (!ok) |
| 1154 | usage(); |
| 1155 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1156 | else |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1157 | usage(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | # undef ISSPACE |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1161 | |
njn | f9ebf67 | 2003-05-12 21:41:30 +0000 | [diff] [blame] | 1162 | if (VG_(clo_verbosity) < 0) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1163 | VG_(clo_verbosity) = 0; |
| 1164 | |
| 1165 | if (VG_(clo_GDB_attach) && VG_(clo_trace_children)) { |
| 1166 | VG_(message)(Vg_UserMsg, ""); |
| 1167 | VG_(message)(Vg_UserMsg, |
| 1168 | "--gdb-attach=yes conflicts with --trace-children=yes"); |
| 1169 | VG_(message)(Vg_UserMsg, |
| 1170 | "Please choose one or the other, but not both."); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1171 | VG_(bad_option)("--gdb-attach=yes and --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1174 | /* Set up logging now. After this is done, VG_(clo_logfile_fd) |
| 1175 | should be connected to whatever sink has been selected, and we |
| 1176 | indiscriminately chuck stuff into it without worrying what the |
| 1177 | nature of it is. Oh the wonder of Unix streams. */ |
| 1178 | |
| 1179 | /* So far we should be still attached to stderr, so we can show on |
| 1180 | the terminal any problems to do with processing command line |
| 1181 | opts. */ |
| 1182 | vg_assert(VG_(clo_logfile_fd) == 2 /* stderr */); |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1183 | vg_assert(VG_(logging_to_filedes) == True); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1184 | |
| 1185 | switch (VG_(clo_log_to)) { |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1186 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1187 | case VgLogTo_Fd: |
| 1188 | vg_assert(VG_(clo_logfile_name) == NULL); |
| 1189 | VG_(clo_logfile_fd) = eventually_logfile_fd; |
| 1190 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1191 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1192 | case VgLogTo_File: { |
| 1193 | Char logfilename[1000]; |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1194 | Int seq = 0; |
| 1195 | Int pid = VG_(getpid)(); |
| 1196 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1197 | vg_assert(VG_(clo_logfile_name) != NULL); |
| 1198 | vg_assert(VG_(strlen)(VG_(clo_logfile_name)) <= 900); /* paranoia */ |
jsgf | f3c3f1a | 2003-10-14 22:13:28 +0000 | [diff] [blame] | 1199 | |
| 1200 | for(;;) { |
| 1201 | if (seq == 0) |
| 1202 | VG_(sprintf)(logfilename, "%s.pid%d", |
| 1203 | VG_(clo_logfile_name), pid ); |
| 1204 | else |
| 1205 | VG_(sprintf)(logfilename, "%s.pid%d.%d", |
| 1206 | VG_(clo_logfile_name), pid, seq ); |
| 1207 | seq++; |
| 1208 | |
| 1209 | eventually_logfile_fd |
| 1210 | = VG_(open)(logfilename, |
| 1211 | VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC, |
| 1212 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 1213 | if (eventually_logfile_fd >= 0) { |
| 1214 | VG_(clo_logfile_fd) = eventually_logfile_fd; |
| 1215 | break; |
| 1216 | } else { |
| 1217 | if (eventually_logfile_fd != -VKI_EEXIST) { |
| 1218 | VG_(message)(Vg_UserMsg, |
| 1219 | "Can't create/open log file `%s.pid%d'; giving up!", |
| 1220 | VG_(clo_logfile_name), pid); |
| 1221 | VG_(bad_option)( |
| 1222 | "--logfile=<file> didn't work out for some reason."); |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1227 | break; |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | case VgLogTo_Socket: { |
| 1231 | vg_assert(VG_(clo_logfile_name) != NULL); |
| 1232 | vg_assert(VG_(strlen)(VG_(clo_logfile_name)) <= 900); /* paranoia */ |
| 1233 | eventually_logfile_fd |
| 1234 | = VG_(connect_via_socket)( VG_(clo_logfile_name) ); |
| 1235 | if (eventually_logfile_fd == -1) { |
| 1236 | VG_(message)(Vg_UserMsg, |
| 1237 | "Invalid --logsocket=ipaddr or --logsocket=ipaddr:port spec"); |
| 1238 | VG_(message)(Vg_UserMsg, |
| 1239 | "of `%s'; giving up!", VG_(clo_logfile_name) ); |
| 1240 | VG_(bad_option)( |
| 1241 | "--logsocket="); |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1242 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1243 | if (eventually_logfile_fd == -2) { |
| 1244 | VG_(message)(Vg_UserMsg, |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1245 | "valgrind: failed to connect to logging server `%s'.", |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1246 | VG_(clo_logfile_name) ); |
sewardj | 570f890 | 2002-11-03 11:44:36 +0000 | [diff] [blame] | 1247 | VG_(message)(Vg_UserMsg, |
| 1248 | "Log messages will sent to stderr instead." ); |
| 1249 | VG_(message)(Vg_UserMsg, |
| 1250 | "" ); |
| 1251 | /* We don't change anything here. */ |
| 1252 | } else { |
| 1253 | vg_assert(eventually_logfile_fd > 0); |
| 1254 | VG_(clo_logfile_fd) = eventually_logfile_fd; |
| 1255 | VG_(logging_to_filedes) = False; |
| 1256 | } |
sewardj | 73cf3bc | 2002-11-03 03:20:15 +0000 | [diff] [blame] | 1257 | break; |
| 1258 | } |
| 1259 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1262 | /* Move logfile_fd into the safe range, so it doesn't conflict with any app fds */ |
| 1263 | eventually_logfile_fd = VG_(fcntl)(VG_(clo_logfile_fd), VKI_F_DUPFD, VG_MAX_FD+1); |
| 1264 | if (eventually_logfile_fd < 0) |
| 1265 | VG_(message)(Vg_UserMsg, "valgrind: failed to move logfile fd into safe range"); |
| 1266 | else { |
| 1267 | VG_(clo_logfile_fd) = eventually_logfile_fd; |
| 1268 | VG_(fcntl)(VG_(clo_logfile_fd), VKI_F_SETFD, VKI_FD_CLOEXEC); |
| 1269 | } |
| 1270 | |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1271 | /* Ok, the logging sink is running now. Print a suitable preamble. |
| 1272 | If logging to file or a socket, write details of parent PID and |
| 1273 | command line args, to help people trying to interpret the |
| 1274 | results of a run which encompasses multiple processes. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1275 | |
sewardj | 83adf41 | 2002-05-01 01:25:45 +0000 | [diff] [blame] | 1276 | if (VG_(clo_verbosity > 0)) { |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1277 | /* Skin details */ |
| 1278 | VG_(message)(Vg_UserMsg, "%s%s%s, %s for x86-linux.", |
| 1279 | VG_(details).name, |
| 1280 | NULL == VG_(details).version ? "" : "-", |
| 1281 | NULL == VG_(details).version |
| 1282 | ? (Char*)"" : VG_(details).version, |
| 1283 | VG_(details).description); |
| 1284 | VG_(message)(Vg_UserMsg, "%s", VG_(details).copyright_author); |
sewardj | 3b2736a | 2002-03-24 12:18:35 +0000 | [diff] [blame] | 1285 | |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1286 | /* Core details */ |
| 1287 | VG_(message)(Vg_UserMsg, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1288 | "Using valgrind-%s, a program supervision framework for x86-linux.", |
sewardj | 4aa62ba | 2002-10-05 15:49:27 +0000 | [diff] [blame] | 1289 | VERSION); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1290 | VG_(message)(Vg_UserMsg, |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 1291 | "Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward."); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
nethercote | c1e395d | 2003-11-10 13:26:49 +0000 | [diff] [blame] | 1294 | if (VG_(clo_verbosity) > 0 && VG_(clo_log_to) != VgLogTo_Fd) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1295 | VG_(message)(Vg_UserMsg, ""); |
| 1296 | VG_(message)(Vg_UserMsg, |
| 1297 | "My PID = %d, parent PID = %d. Prog and args are:", |
| 1298 | VG_(getpid)(), VG_(getppid)() ); |
| 1299 | for (i = 0; i < VG_(client_argc); i++) |
| 1300 | VG_(message)(Vg_UserMsg, " %s", VG_(client_argv)[i]); |
| 1301 | } |
| 1302 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1303 | if (VG_(clo_verbosity) > 1) { |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1304 | if (VG_(clo_log_to) != VgLogTo_Fd) |
| 1305 | VG_(message)(Vg_UserMsg, ""); |
njn | 86dc2bc | 2003-09-09 07:26:21 +0000 | [diff] [blame] | 1306 | VG_(message)(Vg_UserMsg, "Command line"); |
| 1307 | for (i = 0; i < VG_(client_argc); i++) |
| 1308 | VG_(message)(Vg_UserMsg, " %s", VG_(client_argv)[i]); |
| 1309 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1310 | VG_(message)(Vg_UserMsg, "Startup, with flags:"); |
| 1311 | for (i = 0; i < argc; i++) { |
| 1312 | VG_(message)(Vg_UserMsg, " %s", argv[i]); |
| 1313 | } |
| 1314 | } |
| 1315 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1316 | if (VG_(clo_n_suppressions) == 0 && |
| 1317 | (VG_(needs).core_errors || VG_(needs).skin_errors)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1318 | config_error("No error-suppression files were specified."); |
| 1319 | } |
sewardj | 4cf0569 | 2002-10-27 20:28:29 +0000 | [diff] [blame] | 1320 | |
njn | 6a23053 | 2003-07-21 10:38:23 +0000 | [diff] [blame] | 1321 | if (VG_(clo_gen_suppressions) && |
| 1322 | !VG_(needs).core_errors && !VG_(needs).skin_errors) { |
| 1323 | config_error("Can't use --gen-suppressions=yes with this skin,\n" |
| 1324 | " as it doesn't generate errors."); |
| 1325 | } |
| 1326 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1329 | /* --------------------------------------------------------------------- |
| 1330 | Copying to/from m_state_static. |
| 1331 | ------------------------------------------------------------------ */ |
| 1332 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1333 | /* See comment about this in vg_include.h. Change only with |
| 1334 | great care. |
| 1335 | */ |
| 1336 | __attribute__ ((aligned (16))) |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1337 | UInt VG_(m_state_static) [6 /* segment regs, Intel order */ |
| 1338 | + 8 /* int regs, in Intel order */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1339 | + 1 /* %eflags */ |
| 1340 | + 1 /* %eip */ |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1341 | + VG_SIZE_OF_SSESTATE_W /* FPU state */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1342 | ]; |
| 1343 | |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1344 | UInt VG_(insertDflag)(UInt eflags, Int d) |
| 1345 | { |
| 1346 | vg_assert(d == 1 || d == -1); |
| 1347 | eflags &= ~EFlagD; |
| 1348 | |
| 1349 | if (d < 0) |
| 1350 | eflags |= EFlagD; |
| 1351 | |
| 1352 | return eflags; |
| 1353 | } |
| 1354 | |
| 1355 | Int VG_(extractDflag)(UInt eflags) |
| 1356 | { |
| 1357 | Int ret; |
| 1358 | |
| 1359 | if (eflags & EFlagD) |
| 1360 | ret = -1; |
| 1361 | else |
| 1362 | ret = 1; |
| 1363 | |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1367 | void VG_(copy_baseBlock_to_m_state_static) ( void ) |
| 1368 | { |
| 1369 | Int i; |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1370 | VG_(m_state_static)[ 0/4] = VG_(baseBlock)[VGOFF_(m_cs)]; |
| 1371 | VG_(m_state_static)[ 4/4] = VG_(baseBlock)[VGOFF_(m_ss)]; |
| 1372 | VG_(m_state_static)[ 8/4] = VG_(baseBlock)[VGOFF_(m_ds)]; |
| 1373 | VG_(m_state_static)[12/4] = VG_(baseBlock)[VGOFF_(m_es)]; |
| 1374 | VG_(m_state_static)[16/4] = VG_(baseBlock)[VGOFF_(m_fs)]; |
| 1375 | VG_(m_state_static)[20/4] = VG_(baseBlock)[VGOFF_(m_gs)]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1376 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1377 | VG_(m_state_static)[24/4] = VG_(baseBlock)[VGOFF_(m_eax)]; |
| 1378 | VG_(m_state_static)[28/4] = VG_(baseBlock)[VGOFF_(m_ecx)]; |
| 1379 | VG_(m_state_static)[32/4] = VG_(baseBlock)[VGOFF_(m_edx)]; |
| 1380 | VG_(m_state_static)[36/4] = VG_(baseBlock)[VGOFF_(m_ebx)]; |
| 1381 | VG_(m_state_static)[40/4] = VG_(baseBlock)[VGOFF_(m_esp)]; |
| 1382 | VG_(m_state_static)[44/4] = VG_(baseBlock)[VGOFF_(m_ebp)]; |
| 1383 | VG_(m_state_static)[48/4] = VG_(baseBlock)[VGOFF_(m_esi)]; |
| 1384 | VG_(m_state_static)[52/4] = VG_(baseBlock)[VGOFF_(m_edi)]; |
| 1385 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1386 | VG_(m_state_static)[56/4] |
| 1387 | = VG_(insertDflag)(VG_(baseBlock)[VGOFF_(m_eflags)], |
| 1388 | VG_(baseBlock)[VGOFF_(m_dflag)]); |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1389 | VG_(m_state_static)[60/4] = VG_(baseBlock)[VGOFF_(m_eip)]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1390 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1391 | for (i = 0; i < VG_SIZE_OF_SSESTATE_W; i++) |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1392 | VG_(m_state_static)[64/4 + i] |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1393 | = VG_(baseBlock)[VGOFF_(m_ssestate) + i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | |
| 1397 | void VG_(copy_m_state_static_to_baseBlock) ( void ) |
| 1398 | { |
| 1399 | Int i; |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1400 | VG_(baseBlock)[VGOFF_(m_cs)] = VG_(m_state_static)[ 0/4]; |
| 1401 | VG_(baseBlock)[VGOFF_(m_ss)] = VG_(m_state_static)[ 4/4]; |
| 1402 | VG_(baseBlock)[VGOFF_(m_ds)] = VG_(m_state_static)[ 8/4]; |
| 1403 | VG_(baseBlock)[VGOFF_(m_es)] = VG_(m_state_static)[12/4]; |
| 1404 | VG_(baseBlock)[VGOFF_(m_fs)] = VG_(m_state_static)[16/4]; |
| 1405 | VG_(baseBlock)[VGOFF_(m_gs)] = VG_(m_state_static)[20/4]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1406 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1407 | VG_(baseBlock)[VGOFF_(m_eax)] = VG_(m_state_static)[24/4]; |
| 1408 | VG_(baseBlock)[VGOFF_(m_ecx)] = VG_(m_state_static)[28/4]; |
| 1409 | VG_(baseBlock)[VGOFF_(m_edx)] = VG_(m_state_static)[32/4]; |
| 1410 | VG_(baseBlock)[VGOFF_(m_ebx)] = VG_(m_state_static)[36/4]; |
| 1411 | VG_(baseBlock)[VGOFF_(m_esp)] = VG_(m_state_static)[40/4]; |
| 1412 | VG_(baseBlock)[VGOFF_(m_ebp)] = VG_(m_state_static)[44/4]; |
| 1413 | VG_(baseBlock)[VGOFF_(m_esi)] = VG_(m_state_static)[48/4]; |
| 1414 | VG_(baseBlock)[VGOFF_(m_edi)] = VG_(m_state_static)[52/4]; |
| 1415 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1416 | VG_(baseBlock)[VGOFF_(m_eflags)] |
| 1417 | = VG_(m_state_static)[56/4] & ~EFlagD; |
| 1418 | VG_(baseBlock)[VGOFF_(m_dflag)] |
| 1419 | = VG_(extractDflag)(VG_(m_state_static)[56/4]); |
sewardj | fa492d4 | 2002-12-08 18:20:01 +0000 | [diff] [blame] | 1420 | |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1421 | VG_(baseBlock)[VGOFF_(m_eip)] = VG_(m_state_static)[60/4]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1422 | |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1423 | for (i = 0; i < VG_SIZE_OF_SSESTATE_W; i++) |
| 1424 | VG_(baseBlock)[VGOFF_(m_ssestate) + i] |
sewardj | 92a5956 | 2002-09-30 00:53:10 +0000 | [diff] [blame] | 1425 | = VG_(m_state_static)[64/4 + i]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1428 | Addr VG_(get_stack_pointer) ( void ) |
| 1429 | { |
| 1430 | return VG_(baseBlock)[VGOFF_(m_esp)]; |
| 1431 | } |
| 1432 | |
| 1433 | /* Some random tests needed for leak checking */ |
| 1434 | |
| 1435 | Bool VG_(within_stack)(Addr a) |
| 1436 | { |
| 1437 | if (a >= ((Addr)(&VG_(stack))) |
| 1438 | && a <= ((Addr)(&VG_(stack))) + sizeof(VG_(stack))) |
| 1439 | return True; |
| 1440 | else |
| 1441 | return False; |
| 1442 | } |
| 1443 | |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1444 | Bool VG_(within_m_state_static_OR_threads)(Addr a) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1445 | { |
| 1446 | if (a >= ((Addr)(&VG_(m_state_static))) |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1447 | && a < ((Addr)(&VG_(m_state_static))) + sizeof(VG_(m_state_static))) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1448 | return True; |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (a >= ((Addr)(&VG_(threads)[0])) |
| 1451 | && a < ((Addr)(&VG_(threads)[VG_N_THREADS]))) |
| 1452 | return True; |
| 1453 | |
| 1454 | return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1455 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1456 | |
| 1457 | /* --------------------------------------------------------------------- |
| 1458 | Show accumulated counts. |
| 1459 | ------------------------------------------------------------------ */ |
| 1460 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1461 | static __inline__ Int safe_idiv(Int a, Int b) |
| 1462 | { |
| 1463 | return (b == 0 ? 0 : a / b); |
| 1464 | } |
| 1465 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1466 | static void vg_show_counts ( void ) |
| 1467 | { |
| 1468 | VG_(message)(Vg_DebugMsg, |
sewardj | c0d8f68 | 2002-11-30 00:49:43 +0000 | [diff] [blame] | 1469 | " TT/TC: %d tc sectors discarded.", |
| 1470 | VG_(number_of_tc_discards) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1471 | VG_(message)(Vg_DebugMsg, |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1472 | " %d chainings, %d unchainings.", |
| 1473 | VG_(bb_enchain_count), VG_(bb_dechain_count) ); |
| 1474 | VG_(message)(Vg_DebugMsg, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1475 | "translate: new %d (%d -> %d; ratio %d:10)", |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1476 | VG_(overall_in_count), |
| 1477 | VG_(overall_in_osize), |
| 1478 | VG_(overall_in_tsize), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1479 | safe_idiv(10*VG_(overall_in_tsize), VG_(overall_in_osize))); |
| 1480 | VG_(message)(Vg_DebugMsg, |
| 1481 | " discard %d (%d -> %d; ratio %d:10).", |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1482 | VG_(overall_out_count), |
| 1483 | VG_(overall_out_osize), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1484 | VG_(overall_out_tsize), |
| 1485 | safe_idiv(10*VG_(overall_out_tsize), VG_(overall_out_osize))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1486 | VG_(message)(Vg_DebugMsg, |
njn | e0205ff | 2003-04-08 00:56:14 +0000 | [diff] [blame] | 1487 | " dispatch: %llu jumps (bb entries), of which %u (%lu%%) were unchained.", |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1488 | VG_(bbs_done), |
| 1489 | VG_(unchained_jumps_done), |
| 1490 | ((ULong)(100) * (ULong)(VG_(unchained_jumps_done))) |
| 1491 | / ( VG_(bbs_done)==0 ? 1 : VG_(bbs_done) ) |
| 1492 | ); |
| 1493 | |
| 1494 | VG_(message)(Vg_DebugMsg, |
| 1495 | " %d/%d major/minor sched events. %d tt_fast misses.", |
| 1496 | VG_(num_scheduling_events_MAJOR), |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1497 | VG_(num_scheduling_events_MINOR), |
| 1498 | VG_(tt_fast_misses)); |
sewardj | 22854b9 | 2002-11-30 14:00:47 +0000 | [diff] [blame] | 1499 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1500 | VG_(message)(Vg_DebugMsg, |
| 1501 | "reg-alloc: %d t-req-spill, " |
| 1502 | "%d+%d orig+spill uis, %d total-reg-r.", |
| 1503 | VG_(translations_needing_spill), |
| 1504 | VG_(uinstrs_prealloc), |
| 1505 | VG_(uinstrs_spill), |
| 1506 | VG_(total_reg_rank) ); |
| 1507 | VG_(message)(Vg_DebugMsg, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1508 | " sanity: %d cheap, %d expensive checks.", |
| 1509 | VG_(sanity_fast_count), |
| 1510 | VG_(sanity_slow_count) ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1511 | VG_(print_ccall_stats)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | |
| 1515 | /* --------------------------------------------------------------------- |
| 1516 | Main! |
| 1517 | ------------------------------------------------------------------ */ |
| 1518 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1519 | /* Initialize the PID and PGRP of scheduler LWP; this is also called |
| 1520 | in any new children after fork. */ |
| 1521 | static void newpid(ThreadId unused) |
| 1522 | { |
| 1523 | /* PID of scheduler LWP */ |
| 1524 | VG_(main_pid) = VG_(getpid)(); |
| 1525 | VG_(main_pgrp) = VG_(getpgrp)(); |
| 1526 | } |
| 1527 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1528 | /* Where we jump to once Valgrind has got control, and the real |
| 1529 | machine's state has been copied to the m_state_static. */ |
| 1530 | |
| 1531 | void VG_(main) ( void ) |
| 1532 | { |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1533 | Int i; |
| 1534 | VgSchedReturnCode src; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1535 | |
njn | 0c7a5b5 | 2003-04-30 09:00:33 +0000 | [diff] [blame] | 1536 | if (0) { |
| 1537 | if (VG_(have_ssestate)) |
| 1538 | VG_(printf)("Looks like a SSE-capable CPU\n"); |
| 1539 | else |
| 1540 | VG_(printf)("Looks like a MMX-only CPU\n"); |
| 1541 | } |
sewardj | b91ae7f | 2003-04-29 23:50:00 +0000 | [diff] [blame] | 1542 | |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 1543 | /* Check skin and core versions are compatible */ |
| 1544 | if (VG_CORE_INTERFACE_MAJOR_VERSION != VG_(skin_interface_major_version)) { |
| 1545 | VG_(printf)("Error:\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 1546 | " Tool and core interface versions do not match.\n" |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 1547 | " Interface version used by core is: %d.%d\n" |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 1548 | " Interface version used by tool is: %d.%d\n" |
njn | 27f1a38 | 2002-11-08 15:48:16 +0000 | [diff] [blame] | 1549 | " The major version numbers must match.\n", |
| 1550 | VG_CORE_INTERFACE_MAJOR_VERSION, |
| 1551 | VG_CORE_INTERFACE_MINOR_VERSION, |
| 1552 | VG_(skin_interface_major_version), |
| 1553 | VG_(skin_interface_minor_version)); |
| 1554 | VG_(printf)(" You need to at least recompile, and possibly update,\n"); |
| 1555 | if (VG_CORE_INTERFACE_MAJOR_VERSION > VG_(skin_interface_major_version)) |
| 1556 | VG_(printf)(" your skin to work with this version of Valgrind.\n"); |
| 1557 | else |
| 1558 | VG_(printf)(" your version of Valgrind to work with this skin.\n"); |
| 1559 | VG_(printf)(" Aborting, sorry.\n"); |
| 1560 | VG_(exit)(1); |
| 1561 | } |
| 1562 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1563 | VG_(atfork)(NULL, NULL, newpid); |
| 1564 | newpid(VG_INVALID_THREADID); |
| 1565 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1566 | /* Set up our stack sanity-check words. */ |
| 1567 | for (i = 0; i < 10; i++) { |
njn | 6eba4ef | 2003-05-01 08:06:41 +0000 | [diff] [blame] | 1568 | VG_(stack)[i] = (UInt)(&VG_(stack)[i]) ^ 0xA4B3C2D1; |
| 1569 | VG_(stack)[VG_STACK_SIZE_W-1-i] |
| 1570 | = (UInt)(&VG_(stack)[VG_STACK_SIZE_W-i-1]) ^ 0xABCD4321; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1573 | /* Read /proc/self/maps into a buffer. Must be before: |
| 1574 | - SK_(pre_clo_init)(): so that if it calls VG_(malloc)(), any mmap'd |
| 1575 | superblocks are not erroneously identified as being owned by the |
| 1576 | client, which would be bad. |
| 1577 | - init_memory(): that's where the buffer is parsed |
| 1578 | - init_tt_tc(): so the anonymous mmaps for the translation table and |
| 1579 | translation cache aren't identified as part of the client, which would |
| 1580 | waste > 20M of virtual address space, and be bad. |
| 1581 | */ |
njn | fa1016e | 2003-09-25 17:54:11 +0000 | [diff] [blame] | 1582 | VG_(read_procselfmaps)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1583 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1584 | /* Setup stuff that depends on the skin. Must be before: |
| 1585 | - vg_init_baseBlock(): to register helpers |
| 1586 | - process_cmd_line_options(): to register skin name and description, |
| 1587 | and turn on/off 'command_line_options' need |
| 1588 | - init_memory() (to setup memory event trackers). |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1589 | */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 1590 | SK_(pre_clo_init)(); |
| 1591 | VG_(sanity_check_needs)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1592 | |
njn | cc7bb47 | 2002-10-14 09:25:19 +0000 | [diff] [blame] | 1593 | /* Process Valgrind's command-line opts (from env var VG_ARGS). */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1594 | process_cmd_line_options(); |
| 1595 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1596 | /* Hook to delay things long enough so we can get the pid and |
| 1597 | attach GDB in another shell. */ |
| 1598 | if (VG_(clo_wait_for_gdb)) { |
| 1599 | VG_(printf)("pid=%d\n", VG_(getpid)()); |
| 1600 | /* do "jump *$eip" to skip this in gdb */ |
| 1601 | VG_(do_syscall)(__NR_pause); |
| 1602 | } |
| 1603 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1604 | /* Do post command-line processing initialisation. Must be before: |
| 1605 | - vg_init_baseBlock(): to register any more helpers |
| 1606 | */ |
njn | cc7bb47 | 2002-10-14 09:25:19 +0000 | [diff] [blame] | 1607 | SK_(post_clo_init)(); |
| 1608 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1609 | /* Set up baseBlock offsets and copy the saved machine's state into it. */ |
njn | cc7bb47 | 2002-10-14 09:25:19 +0000 | [diff] [blame] | 1610 | vg_init_baseBlock(); |
| 1611 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame^] | 1612 | /* Search for file descriptors that are inherited from our parent. */ |
| 1613 | if (VG_(clo_track_fds)) |
| 1614 | VG_(init_preopened_fds)(); |
| 1615 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1616 | /* Initialise the scheduler, and copy the client's state from |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1617 | baseBlock into VG_(threads)[1]. Must be before: |
| 1618 | - VG_(sigstartup_actions)() |
| 1619 | */ |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1620 | VG_(scheduler_init)(); |
| 1621 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1622 | /* Set up the ProxyLWP machinery */ |
| 1623 | VG_(proxy_init)(); |
| 1624 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1625 | /* Initialise the signal handling subsystem, temporarily parking |
| 1626 | the saved blocking-mask in saved_sigmask. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1627 | VG_(sigstartup_actions)(); |
| 1628 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1629 | /* Perhaps we're profiling Valgrind? */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1630 | if (VG_(clo_profile)) |
| 1631 | VGP_(init_profiling)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1632 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1633 | /* Start calibration of our RDTSC-based clock. */ |
| 1634 | VG_(start_rdtsc_calibration)(); |
| 1635 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1636 | /* Parse /proc/self/maps to learn about startup segments. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1637 | VGP_PUSHCC(VgpInitMem); |
| 1638 | VG_(init_memory)(); |
| 1639 | VGP_POPCC(VgpInitMem); |
| 1640 | |
| 1641 | /* Read the list of errors to suppress. This should be found in |
| 1642 | the file specified by vg_clo_suppressions. */ |
| 1643 | if (VG_(needs).core_errors || VG_(needs).skin_errors) |
| 1644 | VG_(load_suppressions)(); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1645 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1646 | /* End calibration of our RDTSC-based clock, leaving it as long as |
| 1647 | we can. */ |
| 1648 | VG_(end_rdtsc_calibration)(); |
| 1649 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1650 | /* Initialise translation table and translation cache. */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1651 | VG_(init_tt_tc)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1652 | |
| 1653 | if (VG_(clo_verbosity) == 1) { |
| 1654 | VG_(message)(Vg_UserMsg, |
| 1655 | "For more details, rerun with: -v"); |
| 1656 | } |
| 1657 | |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 1658 | /* Force a read of the debug info so that we can look for |
| 1659 | glibc entry points to intercept. */ |
sewardj | 25c7c3a | 2003-07-10 00:17:58 +0000 | [diff] [blame] | 1660 | VG_(setup_code_redirect_table)(); |
| 1661 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1662 | /* Now it is safe for malloc et al in vg_clientmalloc.c to act |
| 1663 | instrumented-ly. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1664 | if (VG_(clo_verbosity) > 0) |
| 1665 | VG_(message)(Vg_UserMsg, ""); |
| 1666 | |
| 1667 | VG_(bbs_to_go) = VG_(clo_stop_after); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1668 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1669 | /* Run! */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1670 | VG_(running_on_simd_CPU) = True; |
sewardj | 671ff54 | 2002-05-07 09:25:30 +0000 | [diff] [blame] | 1671 | VGP_PUSHCC(VgpSched); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1672 | |
| 1673 | if (__builtin_setjmp(&VG_(fatal_signal_jmpbuf)) == 0) { |
| 1674 | VG_(fatal_signal_set) = True; |
| 1675 | src = VG_(scheduler)(); |
| 1676 | } else |
| 1677 | src = VgSrc_FatalSig; |
| 1678 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1679 | VGP_POPCC(VgpSched); |
| 1680 | VG_(running_on_simd_CPU) = False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1681 | |
| 1682 | if (VG_(clo_verbosity) > 0) |
| 1683 | VG_(message)(Vg_UserMsg, ""); |
| 1684 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1685 | if (src == VgSrc_Deadlock) { |
| 1686 | VG_(message)(Vg_UserMsg, |
| 1687 | "Warning: pthread scheduler exited due to deadlock"); |
| 1688 | } |
| 1689 | |
rjwalsh | f5f536f | 2003-11-17 17:45:00 +0000 | [diff] [blame^] | 1690 | /* Print out file descriptor summary and stats. */ |
| 1691 | if(VG_(clo_track_fds)) |
| 1692 | VG_(fd_stats)(); |
| 1693 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1694 | if (VG_(needs).core_errors || VG_(needs).skin_errors) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1695 | VG_(show_all_errors)(); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1696 | |
njn | 7d9f94d | 2003-04-22 21:41:40 +0000 | [diff] [blame] | 1697 | SK_(fini)( VG_(exitcode) ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1698 | |
sewardj | 0c3b53f | 2002-05-01 01:58:35 +0000 | [diff] [blame] | 1699 | VG_(do_sanity_checks)( True /*include expensive checks*/ ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1700 | |
| 1701 | if (VG_(clo_verbosity) > 1) |
| 1702 | vg_show_counts(); |
| 1703 | |
sewardj | c0d8f68 | 2002-11-30 00:49:43 +0000 | [diff] [blame] | 1704 | if (VG_(clo_verbosity) > 3) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1705 | VG_(print_UInstr_histogram)(); |
| 1706 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1707 | if (0) { |
| 1708 | VG_(message)(Vg_DebugMsg, ""); |
| 1709 | VG_(message)(Vg_DebugMsg, |
| 1710 | "------ Valgrind's internal memory use stats follow ------" ); |
| 1711 | VG_(mallocSanityCheckAll)(); |
| 1712 | VG_(show_all_arena_stats)(); |
| 1713 | VG_(message)(Vg_DebugMsg, |
| 1714 | "------ Valgrind's ExeContext management stats follow ------" ); |
| 1715 | VG_(show_ExeContext_stats)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1718 | if (VG_(clo_profile)) |
| 1719 | VGP_(done_profiling)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1720 | |
| 1721 | VG_(shutdown_logging)(); |
| 1722 | |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1723 | /* Remove valgrind.so from a LD_PRELOAD=... string so child |
| 1724 | processes don't get traced into. Also mess up $libdir/valgrind |
| 1725 | so that our libpthread.so disappears from view. */ |
sewardj | 44b6043 | 2003-04-26 22:29:25 +0000 | [diff] [blame] | 1726 | /* 26 Apr 03: doing this often causes trouble for no reason, and is |
| 1727 | pointless when we are just about to VgSrc_ExitSyscall. So don't |
| 1728 | bother in that case. */ |
| 1729 | if ((!VG_(clo_trace_children)) |
| 1730 | && src != VgSrc_ExitSyscall) { |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1731 | VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH)( |
| 1732 | VG_(getenv)("LD_PRELOAD"), |
| 1733 | VG_(getenv)("LD_LIBRARY_PATH") |
| 1734 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1737 | /* We're exiting, so nuke all the threads and clean up the proxy LWPs */ |
| 1738 | vg_assert(src == VgSrc_FatalSig || |
| 1739 | VG_(threads)[VG_(last_run_tid)].status == VgTs_Runnable || |
| 1740 | VG_(threads)[VG_(last_run_tid)].status == VgTs_WaitJoiner); |
| 1741 | VG_(nuke_all_threads_except)(VG_INVALID_THREADID); |
| 1742 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1743 | /* Decide how to exit. This depends on what the scheduler |
| 1744 | returned. */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1745 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1746 | switch (src) { |
| 1747 | case VgSrc_ExitSyscall: /* the normal way out */ |
| 1748 | vg_assert(VG_(last_run_tid) > 0 |
| 1749 | && VG_(last_run_tid) < VG_N_THREADS); |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1750 | VG_(proxy_shutdown)(); |
| 1751 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1752 | /* The thread's %EBX at the time it did __NR_exit() will hold |
| 1753 | the arg to __NR_exit(), so we just do __NR_exit() with |
| 1754 | that arg. */ |
| 1755 | VG_(exit)( VG_(exitcode) ); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1756 | /* NOT ALIVE HERE! */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1757 | VG_(core_panic)("entered the afterlife in vg_main() -- ExitSyscall"); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1758 | break; /* what the hell :) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1759 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1760 | case VgSrc_Deadlock: |
| 1761 | /* Just exit now. No point in continuing. */ |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1762 | VG_(proxy_shutdown)(); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1763 | VG_(exit)(0); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1764 | VG_(core_panic)("entered the afterlife in vg_main() -- Deadlock"); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1765 | break; |
| 1766 | |
| 1767 | case VgSrc_BbsDone: |
| 1768 | /* Tricky; we have to try and switch back to the real CPU. |
| 1769 | This is all very dodgy and won't work at all in the |
| 1770 | presence of threads, or if the client happened to be |
| 1771 | running a signal handler. */ |
| 1772 | /* Prepare to restore state to the real CPU. */ |
sewardj | 839299f | 2003-06-14 11:57:59 +0000 | [diff] [blame] | 1773 | VG_(sigshutdown_actions)(); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1774 | VG_(load_thread_state)(1 /* root thread */ ); |
| 1775 | VG_(copy_baseBlock_to_m_state_static)(); |
| 1776 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1777 | VG_(proxy_shutdown)(); |
| 1778 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1779 | /* This pushes a return address on the simulator's stack, |
| 1780 | which is abandoned. We call vg_sigshutdown_actions() at |
| 1781 | the end of vg_switch_to_real_CPU(), so as to ensure that |
| 1782 | the original stack and machine state is restored before |
| 1783 | the real signal mechanism is restored. */ |
| 1784 | VG_(switch_to_real_CPU)(); |
| 1785 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 1786 | case VgSrc_FatalSig: |
| 1787 | /* We were killed by a fatal signal, so replicate the effect */ |
| 1788 | vg_assert(VG_(fatal_sigNo) != -1); |
| 1789 | VG_(kill_self)(VG_(fatal_sigNo)); |
| 1790 | VG_(core_panic)("vg_main(): signal was supposed to be fatal"); |
| 1791 | break; |
| 1792 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1793 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1794 | VG_(core_panic)("vg_main(): unexpected scheduler return code"); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1795 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | /* Debugging thing .. can be called from assembly with OYNK macro. */ |
| 1800 | void VG_(oynk) ( Int n ) |
| 1801 | { |
| 1802 | OINK(n); |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | /* Find "valgrind.so" in a LD_PRELOAD=... string, and convert it to |
| 1807 | "valgrinq.so", which doesn't do anything. This is used to avoid |
| 1808 | tracing into child processes. To make this work the build system |
| 1809 | also supplies a dummy file, "valgrinq.so". |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1810 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1811 | Also replace "vgskin_<foo>.so" with whitespace, for the same reason; |
| 1812 | without it, child processes try to find valgrind.so symbols in the |
| 1813 | skin .so. |
| 1814 | |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1815 | Also look for $(libdir)/lib/valgrind in LD_LIBRARY_PATH and change |
| 1816 | it to $(libdir)/lib/valgrinq, so as to make our libpthread.so |
| 1817 | disappear. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1818 | */ |
sewardj | 45b672d | 2003-07-25 19:58:11 +0000 | [diff] [blame] | 1819 | static void slideleft ( Char* s ) |
| 1820 | { |
| 1821 | vg_assert(s && (*s == ' ' || *s == ':')); |
| 1822 | while (True) { |
| 1823 | s[0] = s[1]; |
| 1824 | if (s[0] == '\0') break; |
| 1825 | s++; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1830 | void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str, |
| 1831 | Char* ld_library_path_str ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1832 | { |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1833 | Char* vg_prel = NULL; |
| 1834 | Char* sk_prel = NULL; |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1835 | Char* coredir2 = NULL; |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1836 | Char* p; |
| 1837 | Char* coredir_first; |
| 1838 | Char* coredir_last; |
| 1839 | Int coredir_len; |
| 1840 | Int i; |
| 1841 | Int what; |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1842 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1843 | #define MUTANCY(n) { what = n; goto mutancy; } |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1844 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1845 | if (ld_preload_str == NULL || ld_library_path_str == NULL) MUTANCY(0); |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1846 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1847 | /* VG_(printf)("pre:\n%s\n%s\n", ld_preload_str, ld_library_path_str); */ |
| 1848 | |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1849 | /* LD_PRELOAD = "<skindir>/vgskin_foo.so:<coredir>/valgrind.so:X" |
| 1850 | LD_LIBRARY_PATH = "<coredir>:Y" */ |
| 1851 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1852 | /* Setting up, finding things */ |
| 1853 | |
| 1854 | /* LD_PRELOAD: Search for "valgrind.so" */ |
| 1855 | vg_prel = VG_(strstr)(ld_preload_str, "valgrind.so"); |
| 1856 | |
| 1857 | /* LD_PRELOAD: if "valgrind.so" not found, has been done before; |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1858 | "valgrinq.so" should be there instead. Then stop. */ |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1859 | if (NULL == vg_prel) { |
| 1860 | if (VG_(strstr)(ld_preload_str, "valgrinq.so") == NULL) MUTANCY(1); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1861 | return; |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1864 | /* LD_PRELOAD: find start of <coredir> */ |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1865 | p = vg_prel; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1866 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1867 | for (p = vg_prel; *p != ':' && p > ld_preload_str; p--) { } |
| 1868 | if (*p != ':') MUTANCY(2); /* skin.so entry must precede it */ |
| 1869 | coredir_first = p+1; |
| 1870 | coredir_last = vg_prel - 1; |
njn | ac2eb79 | 2003-07-23 09:22:02 +0000 | [diff] [blame] | 1871 | coredir_len = coredir_last - coredir_first; |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1872 | |
| 1873 | /* LD_PRELOAD: find "vgskin_foo.so" */ |
| 1874 | sk_prel = VG_(strstr)(ld_preload_str, "vgskin_"); |
| 1875 | if (sk_prel == NULL) MUTANCY(4); |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1876 | |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1877 | /* LD_LIBRARY_PATH: find <coredir> */ |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1878 | *coredir_last = '\0'; /* Temporarily zero-terminate coredir */ |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1879 | coredir2 = VG_(strstr)(ld_library_path_str, coredir_first); |
| 1880 | if (coredir2 == NULL) MUTANCY(5); |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1881 | *coredir_last = '/'; /* Undo zero-termination */ |
| 1882 | |
| 1883 | /* Changing things */ |
| 1884 | |
| 1885 | /* LD_PRELOAD: "valgrind.so" --> "valgrinq.so" */ |
| 1886 | if (vg_prel[7] != 'd') MUTANCY(6); |
| 1887 | vg_prel[7] = 'q'; |
| 1888 | |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1889 | /* LD_PRELOAD: "<skindir>/vgskin_foo.so:<coredir>/valgrinq.so:X" --> |
| 1890 | " vgskin_foo.so:<coredir>/valgrinq.so:X" */ |
| 1891 | p = sk_prel-1; |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1892 | while (*p != ':' && p >= ld_preload_str) { |
| 1893 | *p = ' '; |
| 1894 | p--; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1895 | } |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1896 | /* LD_PRELOAD: " vgskin_foo.so:<coredir>/valgrinq.so:X" --> |
| 1897 | " :<coredir>/valgrinq.so:X" */ |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1898 | p = sk_prel; |
| 1899 | while (*p != ':' && *p != '\0') { |
| 1900 | *p = ' '; |
| 1901 | p++; |
| 1902 | } |
| 1903 | if (*p == '\0') MUTANCY(7); /* valgrind.so has disappeared?! */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1904 | |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1905 | /* LD_LIBRARY_PATH: "<coredir>:Y" --> " :Y" */ |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1906 | for (i = 0; i < coredir_len; i++) |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1907 | coredir2[i] = ' '; |
sewardj | 45b672d | 2003-07-25 19:58:11 +0000 | [diff] [blame] | 1908 | |
| 1909 | /* Zap the leading spaces and : in both strings. */ |
| 1910 | while (ld_preload_str[0] == ' ') slideleft(ld_preload_str); |
| 1911 | if (ld_preload_str[0] == ':') slideleft(ld_preload_str); |
| 1912 | |
| 1913 | while (ld_library_path_str[0] == ' ') slideleft(ld_library_path_str); |
| 1914 | if (ld_library_path_str[0] == ':') slideleft(ld_library_path_str); |
| 1915 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1916 | /* VG_(printf)("post:\n%s\n%s\n", ld_preload_str, ld_library_path_str); */ |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1917 | |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1918 | return; |
| 1919 | |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1920 | |
| 1921 | mutancy: |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1922 | VG_(printf)( |
| 1923 | "\nVG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH): internal error:\n" |
| 1924 | " what = %d\n" |
| 1925 | " ld_preload_str = `%s'\n" |
| 1926 | " ld_library_path_str = `%s'\n" |
njn | 42e23f2 | 2003-05-05 12:47:25 +0000 | [diff] [blame] | 1927 | " vg_prel = `%s'\n" |
| 1928 | " sk_prel = `%s'\n" |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1929 | " coredir2 = `%s'\n" |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1930 | " VG_LIBDIR = `%s'\n", |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1931 | what, ld_preload_str, ld_library_path_str, |
njn | c2f5bb0 | 2003-07-23 09:40:50 +0000 | [diff] [blame] | 1932 | vg_prel, sk_prel, coredir2, VG_LIBDIR |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1933 | ); |
sewardj | c26b448 | 2002-07-13 12:20:35 +0000 | [diff] [blame] | 1934 | VG_(printf)( |
| 1935 | "\n" |
| 1936 | "Note that this is often caused by mis-installation of valgrind.\n" |
| 1937 | "Correct installation procedure is:\n" |
| 1938 | " ./configure --prefix=/install/dir\n" |
| 1939 | " make install\n" |
| 1940 | "And then use /install/dir/bin/valgrind\n" |
| 1941 | "Moving the installation directory elsewhere after 'make install'\n" |
| 1942 | "will cause the above error. Hand-editing the paths in the shell\n" |
| 1943 | "scripts is also likely to cause problems.\n" |
| 1944 | "\n" |
| 1945 | ); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1946 | VG_(core_panic)("VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) failed\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1949 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1950 | /* RUNS ON THE CLIENT'S STACK, but on the real CPU. Start GDB and get |
| 1951 | it to attach to this process. Called if the user requests this |
| 1952 | service after an error has been shown, so she can poke around and |
| 1953 | look at parameters, memory, etc. You can't meaningfully get GDB to |
| 1954 | continue the program, though; to continue, quit GDB. */ |
| 1955 | extern void VG_(start_GDB_whilst_on_client_stack) ( void ) |
| 1956 | { |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1957 | Int res; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1958 | UChar buf[100]; |
njn | 9315df3 | 2003-04-16 20:50:50 +0000 | [diff] [blame] | 1959 | |
njn | 9315df3 | 2003-04-16 20:50:50 +0000 | [diff] [blame] | 1960 | VG_(sprintf)(buf, "%s -nw /proc/%d/exe %d", |
sewardj | 6024b21 | 2003-07-13 10:54:33 +0000 | [diff] [blame] | 1961 | VG_(clo_GDB_path), VG_(getpid)(), VG_(getpid)()); |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1962 | VG_(message)(Vg_UserMsg, "starting GDB with cmd: %s", buf); |
| 1963 | res = VG_(system)(buf); |
| 1964 | if (res == 0) { |
| 1965 | VG_(message)(Vg_UserMsg, ""); |
| 1966 | VG_(message)(Vg_UserMsg, |
| 1967 | "GDB has detached. Valgrind regains control. We continue."); |
| 1968 | } else { |
| 1969 | VG_(message)(Vg_UserMsg, "Apparently failed!"); |
| 1970 | VG_(message)(Vg_UserMsg, ""); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1971 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | /* Print some helpful-ish text about unimplemented things, and give |
| 1976 | up. */ |
sewardj | cfc39b2 | 2002-05-08 01:58:18 +0000 | [diff] [blame] | 1977 | void VG_(unimplemented) ( Char* msg ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1978 | { |
| 1979 | VG_(message)(Vg_UserMsg, ""); |
| 1980 | VG_(message)(Vg_UserMsg, |
| 1981 | "Valgrind detected that your program requires"); |
| 1982 | VG_(message)(Vg_UserMsg, |
| 1983 | "the following unimplemented functionality:"); |
| 1984 | VG_(message)(Vg_UserMsg, " %s", msg); |
| 1985 | VG_(message)(Vg_UserMsg, |
| 1986 | "This may be because the functionality is hard to implement,"); |
| 1987 | VG_(message)(Vg_UserMsg, |
| 1988 | "or because no reasonable program would behave this way,"); |
| 1989 | VG_(message)(Vg_UserMsg, |
| 1990 | "or because nobody has yet needed it. In any case, let me know"); |
| 1991 | VG_(message)(Vg_UserMsg, |
| 1992 | "(jseward@acm.org) and/or try to work around the problem, if you can."); |
| 1993 | VG_(message)(Vg_UserMsg, |
| 1994 | ""); |
| 1995 | VG_(message)(Vg_UserMsg, |
| 1996 | "Valgrind has to exit now. Sorry. Bye!"); |
| 1997 | VG_(message)(Vg_UserMsg, |
| 1998 | ""); |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 1999 | VG_(pp_sched_status)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2000 | VG_(exit)(1); |
| 2001 | } |
| 2002 | |
| 2003 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2004 | /* --------------------------------------------------------------------- |
| 2005 | Sanity check machinery (permanently engaged). |
| 2006 | ------------------------------------------------------------------ */ |
| 2007 | |
| 2008 | /* A fast sanity check -- suitable for calling circa once per |
| 2009 | millisecond. */ |
| 2010 | |
| 2011 | void VG_(do_sanity_checks) ( Bool force_expensive ) |
| 2012 | { |
| 2013 | Int i; |
| 2014 | |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2015 | VGP_PUSHCC(VgpCoreCheapSanity); |
| 2016 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2017 | if (VG_(sanity_level) < 1) return; |
| 2018 | |
| 2019 | /* --- First do all the tests that we can do quickly. ---*/ |
| 2020 | |
| 2021 | VG_(sanity_fast_count)++; |
| 2022 | |
| 2023 | /* Check that we haven't overrun our private stack. */ |
| 2024 | for (i = 0; i < 10; i++) { |
| 2025 | vg_assert(VG_(stack)[i] |
| 2026 | == ((UInt)(&VG_(stack)[i]) ^ 0xA4B3C2D1)); |
njn | 6eba4ef | 2003-05-01 08:06:41 +0000 | [diff] [blame] | 2027 | vg_assert(VG_(stack)[VG_STACK_SIZE_W-1-i] |
| 2028 | == ((UInt)(&VG_(stack)[VG_STACK_SIZE_W-i-1]) ^ 0xABCD4321)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* Check stuff pertaining to the memory check system. */ |
| 2032 | |
| 2033 | /* Check that nobody has spuriously claimed that the first or |
| 2034 | last 16 pages of memory have become accessible [...] */ |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2035 | if (VG_(needs).sanity_checks) { |
| 2036 | VGP_PUSHCC(VgpSkinCheapSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2037 | vg_assert(SK_(cheap_sanity_check)()); |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2038 | VGP_POPCC(VgpSkinCheapSanity); |
| 2039 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2040 | |
| 2041 | /* --- Now some more expensive checks. ---*/ |
| 2042 | |
| 2043 | /* Once every 25 times, check some more expensive stuff. */ |
| 2044 | if ( force_expensive |
| 2045 | || VG_(sanity_level) > 1 |
| 2046 | || (VG_(sanity_level) == 1 && (VG_(sanity_fast_count) % 25) == 0)) { |
| 2047 | |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2048 | VGP_PUSHCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2049 | VG_(sanity_slow_count)++; |
| 2050 | |
jsgf | 855d93d | 2003-10-13 22:26:55 +0000 | [diff] [blame] | 2051 | VG_(proxy_sanity)(); |
| 2052 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2053 | # if 0 |
| 2054 | { void zzzmemscan(void); zzzmemscan(); } |
| 2055 | # endif |
| 2056 | |
| 2057 | if ((VG_(sanity_fast_count) % 250) == 0) |
| 2058 | VG_(sanity_check_tc_tt)(); |
| 2059 | |
| 2060 | if (VG_(needs).sanity_checks) { |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2061 | VGP_PUSHCC(VgpSkinExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2062 | vg_assert(SK_(expensive_sanity_check)()); |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2063 | VGP_POPCC(VgpSkinExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2064 | } |
| 2065 | /* |
| 2066 | if ((VG_(sanity_fast_count) % 500) == 0) VG_(mallocSanityCheckAll)(); |
| 2067 | */ |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2068 | VGP_POPCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | if (VG_(sanity_level) > 1) { |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2072 | VGP_PUSHCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2073 | /* Check sanity of the low-level memory manager. Note that bugs |
| 2074 | in the client's code can cause this to fail, so we don't do |
| 2075 | this check unless specially asked for. And because it's |
| 2076 | potentially very expensive. */ |
| 2077 | VG_(mallocSanityCheckAll)(); |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2078 | VGP_POPCC(VgpCoreExpensiveSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2079 | } |
njn | 37cea30 | 2002-09-30 11:24:00 +0000 | [diff] [blame] | 2080 | VGP_POPCC(VgpCoreCheapSanity); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2081 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 2082 | /*--------------------------------------------------------------------*/ |
| 2083 | /*--- end vg_main.c ---*/ |
| 2084 | /*--------------------------------------------------------------------*/ |