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 | /* |
| 8 | This file is part of Valgrind, an x86 protected-mode emulator |
| 9 | designed for debugging and profiling binaries on x86-Unixes. |
| 10 | |
| 11 | Copyright (C) 2000-2002 Julian Seward |
| 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; |
| 51 | Int VGOFF_(m_fpustate) = INVALID_OFFSET; |
| 52 | Int VGOFF_(m_eip) = INVALID_OFFSET; |
| 53 | Int VGOFF_(spillslots) = INVALID_OFFSET; |
| 54 | Int VGOFF_(sh_eax) = INVALID_OFFSET; |
| 55 | Int VGOFF_(sh_ecx) = INVALID_OFFSET; |
| 56 | Int VGOFF_(sh_edx) = INVALID_OFFSET; |
| 57 | Int VGOFF_(sh_ebx) = INVALID_OFFSET; |
| 58 | Int VGOFF_(sh_esp) = INVALID_OFFSET; |
| 59 | Int VGOFF_(sh_ebp) = INVALID_OFFSET; |
| 60 | Int VGOFF_(sh_esi) = INVALID_OFFSET; |
| 61 | Int VGOFF_(sh_edi) = INVALID_OFFSET; |
| 62 | Int VGOFF_(sh_eflags) = INVALID_OFFSET; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 63 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 64 | Int VGOFF_(helper_idiv_64_32) = INVALID_OFFSET; |
| 65 | Int VGOFF_(helper_div_64_32) = INVALID_OFFSET; |
| 66 | Int VGOFF_(helper_idiv_32_16) = INVALID_OFFSET; |
| 67 | Int VGOFF_(helper_div_32_16) = INVALID_OFFSET; |
| 68 | Int VGOFF_(helper_idiv_16_8) = INVALID_OFFSET; |
| 69 | Int VGOFF_(helper_div_16_8) = INVALID_OFFSET; |
| 70 | Int VGOFF_(helper_imul_32_64) = INVALID_OFFSET; |
| 71 | Int VGOFF_(helper_mul_32_64) = INVALID_OFFSET; |
| 72 | Int VGOFF_(helper_imul_16_32) = INVALID_OFFSET; |
| 73 | Int VGOFF_(helper_mul_16_32) = INVALID_OFFSET; |
| 74 | Int VGOFF_(helper_imul_8_16) = INVALID_OFFSET; |
| 75 | Int VGOFF_(helper_mul_8_16) = INVALID_OFFSET; |
| 76 | Int VGOFF_(helper_CLD) = INVALID_OFFSET; |
| 77 | Int VGOFF_(helper_STD) = INVALID_OFFSET; |
| 78 | Int VGOFF_(helper_get_dirflag) = INVALID_OFFSET; |
sewardj | 7d78e78 | 2002-06-02 00:04:00 +0000 | [diff] [blame] | 79 | Int VGOFF_(helper_CLC) = INVALID_OFFSET; |
| 80 | Int VGOFF_(helper_STC) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 81 | Int VGOFF_(helper_shldl) = INVALID_OFFSET; |
| 82 | Int VGOFF_(helper_shldw) = INVALID_OFFSET; |
| 83 | Int VGOFF_(helper_shrdl) = INVALID_OFFSET; |
| 84 | Int VGOFF_(helper_shrdw) = INVALID_OFFSET; |
| 85 | Int VGOFF_(helper_RDTSC) = INVALID_OFFSET; |
| 86 | Int VGOFF_(helper_CPUID) = INVALID_OFFSET; |
| 87 | Int VGOFF_(helper_BSWAP) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 88 | Int VGOFF_(helper_bsf) = INVALID_OFFSET; |
| 89 | Int VGOFF_(helper_bsr) = INVALID_OFFSET; |
| 90 | Int VGOFF_(helper_fstsw_AX) = INVALID_OFFSET; |
| 91 | Int VGOFF_(helper_SAHF) = INVALID_OFFSET; |
sewardj | 4d0ab1f | 2002-03-24 10:00:09 +0000 | [diff] [blame] | 92 | Int VGOFF_(helper_DAS) = INVALID_OFFSET; |
sewardj | fe8a166 | 2002-03-24 11:54:07 +0000 | [diff] [blame] | 93 | Int VGOFF_(helper_DAA) = INVALID_OFFSET; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 94 | Int VGOFF_(handle_esp_assignment) = INVALID_OFFSET; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 95 | |
| 96 | /* MAX_NONCOMPACT_HELPERS can be increased easily. If MAX_COMPACT_HELPERS is |
| 97 | * increased too much, they won't really be compact any more... */ |
| 98 | #define MAX_COMPACT_HELPERS 8 |
| 99 | #define MAX_NONCOMPACT_HELPERS 8 |
| 100 | |
| 101 | UInt VG_(n_compact_helpers) = 0; |
| 102 | UInt VG_(n_noncompact_helpers) = 0; |
| 103 | |
| 104 | Addr VG_(compact_helper_addrs) [MAX_COMPACT_HELPERS]; |
| 105 | Int VG_(compact_helper_offsets)[MAX_COMPACT_HELPERS]; |
| 106 | Addr VG_(noncompact_helper_addrs) [MAX_NONCOMPACT_HELPERS]; |
| 107 | Int VG_(noncompact_helper_offsets)[MAX_NONCOMPACT_HELPERS]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 108 | |
| 109 | /* This is the actual defn of baseblock. */ |
| 110 | UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS]; |
| 111 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 112 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 113 | /* Words. */ |
| 114 | static Int baB_off = 0; |
| 115 | |
| 116 | /* Returns the offset, in words. */ |
| 117 | static Int alloc_BaB ( Int words ) |
| 118 | { |
| 119 | Int off = baB_off; |
| 120 | baB_off += words; |
| 121 | if (baB_off >= VG_BASEBLOCK_WORDS) |
| 122 | VG_(panic)( "alloc_BaB: baseBlock is too small"); |
| 123 | |
| 124 | return off; |
| 125 | } |
| 126 | |
| 127 | /* Allocate 1 word in baseBlock and set it to the given value. */ |
| 128 | static Int alloc_BaB_1_set ( Addr a ) |
| 129 | { |
| 130 | Int off = alloc_BaB(1); |
| 131 | VG_(baseBlock)[off] = (UInt)a; |
| 132 | return off; |
| 133 | } |
| 134 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 135 | /* Registers a function in compact_helper_addrs; compact_helper_offsets is |
| 136 | * filled in later. |
| 137 | */ |
| 138 | void VG_(register_compact_helper)(Addr a) |
| 139 | { |
| 140 | if (MAX_COMPACT_HELPERS <= VG_(n_compact_helpers)) { |
| 141 | VG_(printf)("Can only register %d compact helpers\n", |
| 142 | MAX_COMPACT_HELPERS); |
| 143 | VG_(panic)("Too many compact helpers registered"); |
| 144 | } |
| 145 | VG_(compact_helper_addrs)[VG_(n_compact_helpers)] = a; |
| 146 | VG_(n_compact_helpers)++; |
| 147 | } |
| 148 | |
| 149 | /* Registers a function in noncompact_helper_addrs; noncompact_helper_offsets |
| 150 | * is filled in later. |
| 151 | */ |
| 152 | void VG_(register_noncompact_helper)(Addr a) |
| 153 | { |
| 154 | if (MAX_NONCOMPACT_HELPERS <= VG_(n_noncompact_helpers)) { |
| 155 | VG_(printf)("Can only register %d non-compact helpers\n", |
| 156 | MAX_NONCOMPACT_HELPERS); |
| 157 | VG_(printf)("Try increasing MAX_NON_COMPACT_HELPERS\n"); |
| 158 | VG_(panic)("Too many non-compact helpers registered"); |
| 159 | } |
| 160 | VG_(noncompact_helper_addrs)[VG_(n_noncompact_helpers)] = a; |
| 161 | VG_(n_noncompact_helpers)++; |
| 162 | } |
| 163 | |
| 164 | /* Allocate offsets in baseBlock for the skin helpers */ |
| 165 | static void assign_helpers_in_baseBlock(UInt n, Int offsets[], Addr addrs[]) |
| 166 | { |
| 167 | Int i; |
| 168 | for (i = 0; i < n; i++) offsets[i] = alloc_BaB_1_set( addrs[i] ); |
| 169 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 170 | |
| 171 | /* Here we assign actual offsets. It's important to get the most |
| 172 | popular referents within 128 bytes of the start, so we can take |
| 173 | advantage of short addressing modes relative to %ebp. Popularity |
| 174 | of offsets was measured on 22 Feb 02 running a KDE application, and |
| 175 | the slots rearranged accordingly, with a 1.5% reduction in total |
| 176 | size of translations. */ |
| 177 | |
| 178 | static void vg_init_baseBlock ( void ) |
| 179 | { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 180 | /* Those with offsets under 128 are carefully chosen. */ |
| 181 | |
| 182 | /* WORD offsets in this column */ |
| 183 | /* 0 */ VGOFF_(m_eax) = alloc_BaB(1); |
| 184 | /* 1 */ VGOFF_(m_ecx) = alloc_BaB(1); |
| 185 | /* 2 */ VGOFF_(m_edx) = alloc_BaB(1); |
| 186 | /* 3 */ VGOFF_(m_ebx) = alloc_BaB(1); |
| 187 | /* 4 */ VGOFF_(m_esp) = alloc_BaB(1); |
| 188 | /* 5 */ VGOFF_(m_ebp) = alloc_BaB(1); |
| 189 | /* 6 */ VGOFF_(m_esi) = alloc_BaB(1); |
| 190 | /* 7 */ VGOFF_(m_edi) = alloc_BaB(1); |
| 191 | /* 8 */ VGOFF_(m_eflags) = alloc_BaB(1); |
| 192 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 193 | if (VG_(needs).shadow_regs) { |
| 194 | /* 9 */ VGOFF_(sh_eax) = alloc_BaB(1); |
| 195 | /* 10 */ VGOFF_(sh_ecx) = alloc_BaB(1); |
| 196 | /* 11 */ VGOFF_(sh_edx) = alloc_BaB(1); |
| 197 | /* 12 */ VGOFF_(sh_ebx) = alloc_BaB(1); |
| 198 | /* 13 */ VGOFF_(sh_esp) = alloc_BaB(1); |
| 199 | /* 14 */ VGOFF_(sh_ebp) = alloc_BaB(1); |
| 200 | /* 15 */ VGOFF_(sh_esi) = alloc_BaB(1); |
| 201 | /* 16 */ VGOFF_(sh_edi) = alloc_BaB(1); |
| 202 | /* 17 */ VGOFF_(sh_eflags) = alloc_BaB(1); |
| 203 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 204 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 205 | /* 9,10,11 or 18,19,20... depends on number whether shadow regs are used |
| 206 | * and on compact helpers registered */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 207 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 208 | /* (9 or 18) + n_compact_helpers */ |
| 209 | /* Register VG_(handle_esp_assignment) if needed. */ |
| 210 | if (VG_(track_events).new_mem_stack_aligned || |
| 211 | VG_(track_events).die_mem_stack_aligned) |
| 212 | VG_(register_compact_helper)( (Addr) & VG_(handle_esp_assignment) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 213 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 214 | /* Allocate slots for compact helpers */ |
| 215 | assign_helpers_in_baseBlock(VG_(n_compact_helpers), |
| 216 | VG_(compact_helper_offsets), |
| 217 | VG_(compact_helper_addrs)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 218 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 219 | /* (9/10 or 18/19) + n_compact_helpers */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 220 | VGOFF_(m_eip) = alloc_BaB(1); |
| 221 | |
| 222 | /* There are currently 24 spill slots */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 223 | /* (11+/20+ .. 32+/43+) + n_compact_helpers. This can overlap the magic |
| 224 | * boundary at >= 32 words, but most spills are to low numbered spill |
| 225 | * slots, so the ones above the boundary don't see much action. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 226 | VGOFF_(spillslots) = alloc_BaB(VG_MAX_SPILLSLOTS); |
| 227 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 228 | /* I gave up counting at this point. Since they're above the |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 229 | short-amode-boundary, there's no point. */ |
| 230 | |
| 231 | VGOFF_(m_fpustate) = alloc_BaB(VG_SIZE_OF_FPUSTATE_W); |
| 232 | |
| 233 | VGOFF_(helper_idiv_64_32) |
| 234 | = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_64_32) ); |
| 235 | VGOFF_(helper_div_64_32) |
| 236 | = alloc_BaB_1_set( (Addr) & VG_(helper_div_64_32) ); |
| 237 | VGOFF_(helper_idiv_32_16) |
| 238 | = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_32_16) ); |
| 239 | VGOFF_(helper_div_32_16) |
| 240 | = alloc_BaB_1_set( (Addr) & VG_(helper_div_32_16) ); |
| 241 | VGOFF_(helper_idiv_16_8) |
| 242 | = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_16_8) ); |
| 243 | VGOFF_(helper_div_16_8) |
| 244 | = alloc_BaB_1_set( (Addr) & VG_(helper_div_16_8) ); |
| 245 | |
| 246 | VGOFF_(helper_imul_32_64) |
| 247 | = alloc_BaB_1_set( (Addr) & VG_(helper_imul_32_64) ); |
| 248 | VGOFF_(helper_mul_32_64) |
| 249 | = alloc_BaB_1_set( (Addr) & VG_(helper_mul_32_64) ); |
| 250 | VGOFF_(helper_imul_16_32) |
| 251 | = alloc_BaB_1_set( (Addr) & VG_(helper_imul_16_32) ); |
| 252 | VGOFF_(helper_mul_16_32) |
| 253 | = alloc_BaB_1_set( (Addr) & VG_(helper_mul_16_32) ); |
| 254 | VGOFF_(helper_imul_8_16) |
| 255 | = alloc_BaB_1_set( (Addr) & VG_(helper_imul_8_16) ); |
| 256 | VGOFF_(helper_mul_8_16) |
| 257 | = alloc_BaB_1_set( (Addr) & VG_(helper_mul_8_16) ); |
| 258 | |
| 259 | VGOFF_(helper_CLD) |
| 260 | = alloc_BaB_1_set( (Addr) & VG_(helper_CLD) ); |
| 261 | VGOFF_(helper_STD) |
| 262 | = alloc_BaB_1_set( (Addr) & VG_(helper_STD) ); |
| 263 | VGOFF_(helper_get_dirflag) |
| 264 | = alloc_BaB_1_set( (Addr) & VG_(helper_get_dirflag) ); |
| 265 | |
sewardj | 7d78e78 | 2002-06-02 00:04:00 +0000 | [diff] [blame] | 266 | VGOFF_(helper_CLC) |
| 267 | = alloc_BaB_1_set( (Addr) & VG_(helper_CLC) ); |
| 268 | VGOFF_(helper_STC) |
| 269 | = alloc_BaB_1_set( (Addr) & VG_(helper_STC) ); |
| 270 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 271 | VGOFF_(helper_shldl) |
| 272 | = alloc_BaB_1_set( (Addr) & VG_(helper_shldl) ); |
| 273 | VGOFF_(helper_shldw) |
| 274 | = alloc_BaB_1_set( (Addr) & VG_(helper_shldw) ); |
| 275 | VGOFF_(helper_shrdl) |
| 276 | = alloc_BaB_1_set( (Addr) & VG_(helper_shrdl) ); |
| 277 | VGOFF_(helper_shrdw) |
| 278 | = alloc_BaB_1_set( (Addr) & VG_(helper_shrdw) ); |
| 279 | |
| 280 | VGOFF_(helper_RDTSC) |
| 281 | = alloc_BaB_1_set( (Addr) & VG_(helper_RDTSC) ); |
| 282 | VGOFF_(helper_CPUID) |
| 283 | = alloc_BaB_1_set( (Addr) & VG_(helper_CPUID) ); |
| 284 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 285 | VGOFF_(helper_bsf) |
| 286 | = alloc_BaB_1_set( (Addr) & VG_(helper_bsf) ); |
| 287 | VGOFF_(helper_bsr) |
| 288 | = alloc_BaB_1_set( (Addr) & VG_(helper_bsr) ); |
| 289 | |
| 290 | VGOFF_(helper_fstsw_AX) |
| 291 | = alloc_BaB_1_set( (Addr) & VG_(helper_fstsw_AX) ); |
| 292 | VGOFF_(helper_SAHF) |
| 293 | = alloc_BaB_1_set( (Addr) & VG_(helper_SAHF) ); |
sewardj | 4d0ab1f | 2002-03-24 10:00:09 +0000 | [diff] [blame] | 294 | VGOFF_(helper_DAS) |
| 295 | = alloc_BaB_1_set( (Addr) & VG_(helper_DAS) ); |
sewardj | fe8a166 | 2002-03-24 11:54:07 +0000 | [diff] [blame] | 296 | VGOFF_(helper_DAA) |
| 297 | = alloc_BaB_1_set( (Addr) & VG_(helper_DAA) ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 298 | |
| 299 | /* Allocate slots for compact helpers */ |
| 300 | assign_helpers_in_baseBlock(VG_(n_noncompact_helpers), |
| 301 | VG_(noncompact_helper_offsets), |
| 302 | VG_(noncompact_helper_addrs)); |
| 303 | } |
| 304 | |
| 305 | static void vg_init_shadow_regs ( void ) |
| 306 | { |
| 307 | if (VG_(needs).shadow_regs) { |
| 308 | UInt eflags; |
| 309 | |
| 310 | SK_(written_shadow_regs_values) ( & VG_(written_shadow_reg), & eflags ); |
| 311 | VG_(baseBlock)[VGOFF_(sh_esp)] = |
| 312 | VG_(baseBlock)[VGOFF_(sh_ebp)] = |
| 313 | VG_(baseBlock)[VGOFF_(sh_eax)] = |
| 314 | VG_(baseBlock)[VGOFF_(sh_ecx)] = |
| 315 | VG_(baseBlock)[VGOFF_(sh_edx)] = |
| 316 | VG_(baseBlock)[VGOFF_(sh_ebx)] = |
| 317 | VG_(baseBlock)[VGOFF_(sh_esi)] = |
| 318 | VG_(baseBlock)[VGOFF_(sh_edi)] = VG_(written_shadow_reg); |
| 319 | VG_(baseBlock)[VGOFF_(sh_eflags)] = eflags; |
| 320 | |
| 321 | } else |
| 322 | VG_(written_shadow_reg) = VG_UNUSED_SHADOW_REG_VALUE; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
| 326 | /* --------------------------------------------------------------------- |
| 327 | Global entities which are not referenced from generated code. |
| 328 | ------------------------------------------------------------------ */ |
| 329 | |
| 330 | /* The stack on which Valgrind runs. We can't use the same stack as |
| 331 | the simulatee -- that's an important design decision. */ |
| 332 | UInt VG_(stack)[10000]; |
| 333 | |
| 334 | /* Ditto our signal delivery stack. */ |
| 335 | UInt VG_(sigstack)[10000]; |
| 336 | |
| 337 | /* Saving stuff across system calls. */ |
sewardj | 43c356f | 2002-06-02 00:21:08 +0000 | [diff] [blame] | 338 | UInt VG_(real_fpu_state_saved_over_syscall)[VG_SIZE_OF_FPUSTATE_W]; |
| 339 | Addr VG_(esp_saved_over_syscall); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 340 | |
| 341 | /* Counts downwards in vg_run_innerloop. */ |
| 342 | UInt VG_(dispatch_ctr); |
| 343 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 344 | |
| 345 | /* 64-bit counter for the number of basic blocks done. */ |
| 346 | ULong VG_(bbs_done); |
| 347 | /* 64-bit counter for the number of bbs to go before a debug exit. */ |
| 348 | ULong VG_(bbs_to_go); |
| 349 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 350 | /* The current LRU epoch. */ |
| 351 | UInt VG_(current_epoch) = 0; |
| 352 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 353 | /* This is the ThreadId of the last thread the scheduler ran. */ |
| 354 | ThreadId VG_(last_run_tid) = 0; |
| 355 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 356 | /* This is the argument to __NR_exit() supplied by the first thread to |
| 357 | call that syscall. We eventually pass that to __NR_exit() for |
| 358 | real. */ |
| 359 | UInt VG_(exitcode) = 0; |
| 360 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 361 | |
| 362 | /* --------------------------------------------------------------------- |
| 363 | Counters, for informational purposes only. |
| 364 | ------------------------------------------------------------------ */ |
| 365 | |
| 366 | /* Number of lookups which miss the fast tt helper. */ |
| 367 | UInt VG_(tt_fast_misses) = 0; |
| 368 | |
| 369 | |
| 370 | /* Counts for LRU informational messages. */ |
| 371 | |
| 372 | /* Number and total o/t size of new translations this epoch. */ |
| 373 | UInt VG_(this_epoch_in_count) = 0; |
| 374 | UInt VG_(this_epoch_in_osize) = 0; |
| 375 | UInt VG_(this_epoch_in_tsize) = 0; |
| 376 | /* Number and total o/t size of discarded translations this epoch. */ |
| 377 | UInt VG_(this_epoch_out_count) = 0; |
| 378 | UInt VG_(this_epoch_out_osize) = 0; |
| 379 | UInt VG_(this_epoch_out_tsize) = 0; |
| 380 | /* Number and total o/t size of translations overall. */ |
| 381 | UInt VG_(overall_in_count) = 0; |
| 382 | UInt VG_(overall_in_osize) = 0; |
| 383 | UInt VG_(overall_in_tsize) = 0; |
| 384 | /* Number and total o/t size of discards overall. */ |
| 385 | UInt VG_(overall_out_count) = 0; |
| 386 | UInt VG_(overall_out_osize) = 0; |
| 387 | UInt VG_(overall_out_tsize) = 0; |
| 388 | |
| 389 | /* The number of LRU-clearings of TT/TC. */ |
| 390 | UInt VG_(number_of_lrus) = 0; |
| 391 | |
| 392 | |
| 393 | /* Counts pertaining to the register allocator. */ |
| 394 | |
| 395 | /* total number of uinstrs input to reg-alloc */ |
| 396 | UInt VG_(uinstrs_prealloc) = 0; |
| 397 | |
| 398 | /* total number of uinstrs added due to spill code */ |
| 399 | UInt VG_(uinstrs_spill) = 0; |
| 400 | |
| 401 | /* number of bbs requiring spill code */ |
| 402 | UInt VG_(translations_needing_spill) = 0; |
| 403 | |
| 404 | /* total of register ranks over all translations */ |
| 405 | UInt VG_(total_reg_rank) = 0; |
| 406 | |
| 407 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 408 | /* Counts pertaining to internal sanity checking. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 409 | UInt VG_(sanity_fast_count) = 0; |
| 410 | UInt VG_(sanity_slow_count) = 0; |
| 411 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 412 | /* Counts pertaining to the scheduler. */ |
| 413 | UInt VG_(num_scheduling_events_MINOR) = 0; |
| 414 | UInt VG_(num_scheduling_events_MAJOR) = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 415 | |
| 416 | |
| 417 | /* --------------------------------------------------------------------- |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 418 | Skin data structure initialisation |
| 419 | ------------------------------------------------------------------ */ |
| 420 | |
| 421 | /* Init with default values. */ |
| 422 | VgNeeds VG_(needs) = { |
| 423 | .name = NULL, |
| 424 | .description = NULL, |
| 425 | |
| 426 | .core_errors = False, |
| 427 | .skin_errors = False, |
| 428 | .run_libc_freeres = False, |
| 429 | |
| 430 | .sizeof_shadow_block = 0, |
| 431 | |
| 432 | .basic_block_discards = False, |
| 433 | .shadow_regs = False, |
| 434 | .command_line_options = False, |
| 435 | .client_requests = False, |
| 436 | .extended_UCode = False, |
| 437 | .syscall_wrapper = False, |
| 438 | .alternative_free = False, |
| 439 | .sanity_checks = False, |
| 440 | }; |
| 441 | |
| 442 | VgTrackEvents VG_(track_events) = { |
| 443 | /* Memory events */ |
| 444 | .new_mem_startup = NULL, |
| 445 | .new_mem_heap = NULL, |
| 446 | .new_mem_stack = NULL, |
| 447 | .new_mem_stack_aligned = NULL, |
| 448 | .new_mem_stack_signal = NULL, |
| 449 | .new_mem_brk = NULL, |
| 450 | .new_mem_mmap = NULL, |
| 451 | |
| 452 | .copy_mem_heap = NULL, |
| 453 | .change_mem_mprotect = NULL, |
| 454 | |
| 455 | .ban_mem_heap = NULL, |
| 456 | .ban_mem_stack = NULL, |
| 457 | |
| 458 | .die_mem_heap = NULL, |
| 459 | .die_mem_stack = NULL, |
| 460 | .die_mem_stack_aligned = NULL, |
| 461 | .die_mem_stack_signal = NULL, |
| 462 | .die_mem_brk = NULL, |
| 463 | .die_mem_munmap = NULL, |
| 464 | |
| 465 | .bad_free = NULL, |
| 466 | .mismatched_free = NULL, |
| 467 | |
| 468 | .pre_mem_read = NULL, |
| 469 | .pre_mem_read_asciiz = NULL, |
| 470 | .pre_mem_write = NULL, |
| 471 | .post_mem_write = NULL, |
| 472 | |
| 473 | /* Mutex events */ |
| 474 | .post_mutex_lock = NULL, |
| 475 | .post_mutex_unlock = NULL, |
| 476 | }; |
| 477 | |
| 478 | static void sanity_check_needs ( void ) |
| 479 | { |
| 480 | #define CHECK_NOT(var, value) \ |
| 481 | if ((var)==(value)) { \ |
| 482 | VG_(printf)("\n`%s' not initialised\n", VG__STRING(var)); \ |
| 483 | VG_(skin_error)("Uninitialised needs field\n"); \ |
| 484 | } |
| 485 | |
| 486 | CHECK_NOT(VG_(needs).name, NULL); |
| 487 | CHECK_NOT(VG_(needs).description, NULL); |
| 488 | |
| 489 | #undef CHECK_NOT |
| 490 | #undef INVALID_Bool |
| 491 | } |
| 492 | |
| 493 | /* --------------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 494 | Values derived from command-line options. |
| 495 | ------------------------------------------------------------------ */ |
| 496 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 497 | /* Define, and set defaults. */ |
| 498 | Bool VG_(clo_error_limit) = True; |
| 499 | Bool VG_(clo_GDB_attach) = False; |
| 500 | Int VG_(sanity_level) = 1; |
| 501 | Int VG_(clo_verbosity) = 1; |
| 502 | Bool VG_(clo_demangle) = True; |
| 503 | Bool VG_(clo_sloppy_malloc) = False; |
| 504 | Int VG_(clo_alignment) = 4; |
| 505 | Bool VG_(clo_trace_children) = False; |
| 506 | Int VG_(clo_logfile_fd) = 2; |
| 507 | Int VG_(clo_n_suppressions) = 0; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 508 | Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 509 | Bool VG_(clo_profile) = False; |
| 510 | Bool VG_(clo_single_step) = False; |
| 511 | Bool VG_(clo_optimise) = True; |
| 512 | UChar VG_(clo_trace_codegen) = 0; // 00000000b |
| 513 | Bool VG_(clo_trace_syscalls) = False; |
| 514 | Bool VG_(clo_trace_signals) = False; |
| 515 | Bool VG_(clo_trace_symtab) = False; |
| 516 | Bool VG_(clo_trace_malloc) = False; |
| 517 | Bool VG_(clo_trace_sched) = False; |
| 518 | Int VG_(clo_trace_pthread_level) = 0; |
| 519 | ULong VG_(clo_stop_after) = 1000000000000LL; |
| 520 | Int VG_(clo_dump_error) = 0; |
| 521 | Int VG_(clo_backtrace_size) = 4; |
| 522 | Char* VG_(clo_weird_hacks) = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 523 | |
| 524 | /* This Bool is needed by wrappers in vg_clientmalloc.c to decide how |
| 525 | to behave. Initially we say False. */ |
| 526 | Bool VG_(running_on_simd_CPU) = False; |
| 527 | |
| 528 | /* Holds client's %esp at the point we gained control. */ |
| 529 | Addr VG_(esp_at_startup); |
| 530 | |
| 531 | /* As deduced from VG_(esp_at_startup), the client's argc, argv[] and |
| 532 | envp[] as extracted from the client's stack at startup-time. */ |
| 533 | Int VG_(client_argc); |
| 534 | Char** VG_(client_argv); |
| 535 | Char** VG_(client_envp); |
| 536 | |
| 537 | /* A place into which to copy the value of env var VG_ARGS, so we |
| 538 | don't have to modify the original. */ |
| 539 | static Char vg_cmdline_copy[M_VG_CMDLINE_STRLEN]; |
| 540 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 541 | /* --------------------------------------------------------------------- |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 542 | Processing of command-line options. |
| 543 | ------------------------------------------------------------------ */ |
| 544 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 545 | void VG_(bad_option) ( Char* opt ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 546 | { |
| 547 | VG_(shutdown_logging)(); |
| 548 | VG_(clo_logfile_fd) = 2; /* stderr */ |
| 549 | VG_(printf)("valgrind.so: Bad option `%s'; aborting.\n", opt); |
| 550 | VG_(exit)(1); |
| 551 | } |
| 552 | |
| 553 | static void config_error ( Char* msg ) |
| 554 | { |
| 555 | VG_(shutdown_logging)(); |
| 556 | VG_(clo_logfile_fd) = 2; /* stderr */ |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 557 | VG_(printf)( |
| 558 | "valgrind.so: Startup or configuration error:\n %s\n", msg); |
| 559 | VG_(printf)( |
| 560 | "valgrind.so: Unable to start up properly. Giving up.\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 561 | VG_(exit)(1); |
| 562 | } |
| 563 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 564 | static void args_grok_error ( Char* msg ) |
| 565 | { |
| 566 | VG_(shutdown_logging)(); |
| 567 | VG_(clo_logfile_fd) = 2; /* stderr */ |
| 568 | VG_(printf)("valgrind.so: When searching for " |
| 569 | "client's argc/argc/envp:\n\t%s\n", msg); |
| 570 | config_error("couldn't find client's argc/argc/envp"); |
| 571 | } |
| 572 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 573 | static void usage ( void ) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 574 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 575 | Char* usage1 = |
| 576 | "usage: valgrind [options] prog-and-args\n" |
| 577 | "\n" |
| 578 | " core user options, with defaults in [ ], are:\n" |
| 579 | " --help show this message\n" |
| 580 | " --version show version\n" |
| 581 | " --skin=<name> main task (skin to use) [Valgrind]\n" |
| 582 | " -q --quiet run silently; only print error msgs\n" |
| 583 | " -v --verbose be more verbose, incl counts of errors\n" |
| 584 | " --gdb-attach=no|yes start GDB when errors detected? [no]\n" |
| 585 | " --demangle=no|yes automatically demangle C++ names? [yes]\n" |
| 586 | " --num-callers=<number> show <num> callers in stack traces [4]\n" |
| 587 | " --error-limit=no|yes stop showing new errors if too many? [yes]\n" |
| 588 | " --sloppy-malloc=no|yes round malloc sizes to next word? [no]\n" |
| 589 | " --alignment=<number> set minimum alignment of allocations [4]\n" |
| 590 | " --trace-children=no|yes Valgrind-ise child processes? [no]\n" |
| 591 | " --logfile-fd=<number> file descriptor for messages [2=stderr]\n" |
| 592 | " --suppressions=<filename> suppress errors described in\n" |
| 593 | " suppressions file <filename>\n" |
| 594 | " --weird-hacks=hack1,hack2,... [no hacks selected]\n" |
| 595 | " recognised hacks are: ioctl-VTIME truncate-writes\n" |
| 596 | "\n" |
| 597 | " %s skin user options:\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 598 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 599 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 600 | Char* usage2 = |
| 601 | "\n" |
| 602 | " core options for debugging Valgrind itself are:\n" |
| 603 | " --sanity-level=<number> level of sanity checking to do [1]\n" |
| 604 | " --single-step=no|yes translate each instr separately? [no]\n" |
| 605 | " --optimise=no|yes improve intermediate code? [yes]\n" |
| 606 | " --profile=no|yes profile? (skin must be built for it) [no]\n" |
| 607 | " --trace-codegen=<XXXXX> show generated code? (X = 0|1) [00000]\n" |
| 608 | " --trace-syscalls=no|yes show all system calls? [no]\n" |
| 609 | " --trace-signals=no|yes show signal handling details? [no]\n" |
| 610 | " --trace-symtab=no|yes show symbol table details? [no]\n" |
| 611 | " --trace-malloc=no|yes show client malloc details? [no]\n" |
| 612 | " --trace-sched=no|yes show thread scheduler details? [no]\n" |
| 613 | " --trace-pthread=none|some|all show pthread event details? [no]\n" |
| 614 | " --stop-after=<number> switch to real CPU after executing\n" |
| 615 | " <number> basic blocks [infinity]\n" |
| 616 | " --dump-error=<number> show translation for basic block\n" |
| 617 | " associated with <number>'th\n" |
| 618 | " error context [0=don't show any]\n" |
| 619 | "\n" |
| 620 | " Extra options are read from env variable $VALGRIND_OPTS\n" |
| 621 | "\n" |
| 622 | " Valgrind is Copyright (C) 2000-2002 Julian Seward\n" |
| 623 | " and licensed under the GNU General Public License, version 2.\n" |
| 624 | " Bug reports, feedback, admiration, abuse, etc, to: %s.\n" |
| 625 | "\n"; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 626 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 627 | VG_(printf)(usage1, VG_(needs).name); |
| 628 | /* Don't print skin string directly for security, ha! */ |
| 629 | if (VG_(needs).command_line_options) |
| 630 | VG_(printf)("%s", SK_(usage)()); |
| 631 | else |
| 632 | VG_(printf)(" (none)\n"); |
| 633 | VG_(printf)(usage2, VG_EMAIL_ADDR); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 634 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 635 | VG_(shutdown_logging)(); |
| 636 | VG_(clo_logfile_fd) = 2; /* stderr */ |
| 637 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 638 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 639 | |
| 640 | static void process_cmd_line_options ( void ) |
| 641 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 642 | Char* argv[M_VG_CMDLINE_OPTS]; |
| 643 | UInt argc; |
| 644 | Char* p; |
| 645 | Char* str; |
| 646 | Int i, eventually_logfile_fd, ctr; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 647 | |
| 648 | # define ISSPACE(cc) ((cc) == ' ' || (cc) == '\t' || (cc) == '\n') |
| 649 | # define STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2))) |
| 650 | # define STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn))) |
| 651 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 652 | eventually_logfile_fd = VG_(clo_logfile_fd); |
| 653 | |
| 654 | /* Once logging is started, we can safely send messages pertaining |
| 655 | to failures in initialisation. */ |
| 656 | VG_(startup_logging)(); |
| 657 | |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 658 | /* Check for sane path in ./configure --prefix=... */ |
| 659 | if (VG_(strlen)(VG_LIBDIR) < 1 |
| 660 | || VG_LIBDIR[0] != '/') |
| 661 | config_error("Please use absolute paths in " |
| 662 | "./configure --prefix=... or --libdir=..."); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 663 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 664 | /* (Suggested by Fabrice Bellard ... ) |
| 665 | 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] | 666 | envc & envp. It is not fool-proof, but these structures should |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 667 | change less often than the libc ones. */ |
| 668 | { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 669 | UInt* sp = 0; /* bogus init to keep gcc -O happy */ |
| 670 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 671 | /* locate the top of the stack */ |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 672 | if (VG_STACK_MATCHES_BASE( VG_(esp_at_startup), |
| 673 | VG_STARTUP_STACK_BASE_1 )) { |
| 674 | sp = (UInt*)VG_STARTUP_STACK_BASE_1; |
| 675 | } else |
| 676 | if (VG_STACK_MATCHES_BASE( VG_(esp_at_startup), |
| 677 | VG_STARTUP_STACK_BASE_2 )) { |
| 678 | sp = (UInt*)VG_STARTUP_STACK_BASE_2; |
daywalker | a256220 | 2002-07-15 19:39:51 +0000 | [diff] [blame] | 679 | } else |
| 680 | if (VG_STACK_MATCHES_BASE( VG_(esp_at_startup), |
| 681 | VG_STARTUP_STACK_BASE_3 )) { |
| 682 | sp = (UInt*)VG_STARTUP_STACK_BASE_3; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 683 | } else |
| 684 | if (VG_STACK_MATCHES_BASE( VG_(esp_at_startup), |
| 685 | VG_STARTUP_STACK_BASE_4 )) { |
| 686 | sp = (UInt*)VG_STARTUP_STACK_BASE_4; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 687 | } else { |
| 688 | args_grok_error( |
| 689 | "startup %esp is not near any VG_STARTUP_STACK_BASE_*\n " |
| 690 | "constants defined in vg_include.h. You should investigate." |
| 691 | ); |
| 692 | } |
| 693 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 694 | /* we locate: NEW_AUX_ENT(1, AT_PAGESZ, ELF_EXEC_PAGESIZE) in |
| 695 | the elf interpreter table */ |
| 696 | sp -= 2; |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 697 | while (sp[0] != VKI_AT_PAGESZ || sp[1] != 4096) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 698 | /* VG_(printf)("trying %p\n", sp); */ |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 699 | sp--; |
| 700 | } |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 701 | |
| 702 | if (sp[2] == VKI_AT_BASE |
| 703 | && sp[0] == VKI_AT_PAGESZ |
| 704 | && sp[-2] == VKI_AT_PHNUM |
| 705 | && sp[-4] == VKI_AT_PHENT |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 706 | && sp[-6] == VKI_AT_PHDR |
| 707 | && sp[-6-1] == 0) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 708 | if (0) |
| 709 | 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] | 710 | sp -= 6; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 711 | } else |
| 712 | if (sp[2] == VKI_AT_CLKTCK |
| 713 | && sp[0] == VKI_AT_PAGESZ |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 714 | && sp[-2] == VKI_AT_HWCAP |
| 715 | && sp[-2-1] == 0) { |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 716 | if (0) |
| 717 | VG_(printf)("Looks like you've got a 2.4.X kernel here.\n"); |
| 718 | sp -= 2; |
| 719 | } else |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 720 | if (sp[2] == VKI_AT_CLKTCK |
| 721 | && sp[0] == VKI_AT_PAGESZ |
| 722 | && sp[-2] == VKI_AT_HWCAP |
sewardj | ce61d05 | 2002-07-25 00:49:51 +0000 | [diff] [blame] | 723 | && sp[-4] == VKI_AT_USER_AUX_SEGMENT |
| 724 | && sp[-4-1] == 0) { |
| 725 | if (0) |
| 726 | VG_(printf)("Looks like you've got a R-H Limbo 2.4.X " |
| 727 | "kernel here.\n"); |
| 728 | sp -= 4; |
| 729 | } else |
| 730 | if (sp[2] == VKI_AT_CLKTCK |
| 731 | && sp[0] == VKI_AT_PAGESZ |
| 732 | && sp[-2] == VKI_AT_HWCAP |
sewardj | d9c2d6d | 2002-05-18 11:55:05 +0000 | [diff] [blame] | 733 | && sp[-2-20-1] == 0) { |
| 734 | if (0) |
| 735 | VG_(printf)("Looks like you've got a early 2.4.X kernel here.\n"); |
| 736 | sp -= 22; |
| 737 | } else |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 738 | args_grok_error( |
| 739 | "ELF frame does not look like 2.2.X or 2.4.X.\n " |
| 740 | "See kernel sources linux/fs/binfmt_elf.c to make sense of this." |
| 741 | ); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 742 | |
| 743 | sp--; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 744 | if (*sp != 0) |
| 745 | args_grok_error("can't find NULL at end of env[]"); |
| 746 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 747 | /* sp now points to NULL at the end of env[] */ |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 748 | ctr = 0; |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 749 | while (True) { |
| 750 | sp --; |
| 751 | if (*sp == 0) break; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 752 | if (++ctr >= 1000) |
| 753 | args_grok_error( |
| 754 | "suspiciously many (1000) env[] entries; giving up"); |
| 755 | |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 756 | } |
| 757 | /* sp now points to NULL at the end of argv[] */ |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 758 | VG_(client_envp) = (Char**)(sp+1); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 759 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 760 | ctr = 0; |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 761 | VG_(client_argc) = 0; |
| 762 | while (True) { |
| 763 | sp--; |
| 764 | if (*sp == VG_(client_argc)) |
| 765 | break; |
| 766 | VG_(client_argc)++; |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 767 | if (++ctr >= 1000) |
| 768 | args_grok_error( |
| 769 | "suspiciously many (1000) argv[] entries; giving up"); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 770 | } |
| 771 | |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 772 | VG_(client_argv) = (Char**)(sp+1); |
sewardj | 3817091 | 2002-05-10 21:07:22 +0000 | [diff] [blame] | 773 | } |
| 774 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 775 | /* Now that VG_(client_envp) has been set, we can extract the args |
| 776 | for Valgrind itself. Copy into global var so that we don't have to |
| 777 | write zeroes to the getenv'd value itself. */ |
| 778 | str = VG_(getenv)("VG_ARGS"); |
| 779 | argc = 0; |
| 780 | |
| 781 | if (!str) { |
| 782 | config_error("Can't read options from env var VG_ARGS."); |
| 783 | } |
| 784 | |
| 785 | if (VG_(strlen)(str) >= M_VG_CMDLINE_STRLEN-1) { |
| 786 | config_error("Command line length exceeds M_CMDLINE_STRLEN."); |
| 787 | } |
| 788 | VG_(strcpy)(vg_cmdline_copy, str); |
| 789 | str = NULL; |
| 790 | |
| 791 | p = &vg_cmdline_copy[0]; |
| 792 | while (True) { |
| 793 | while (ISSPACE(*p)) { *p = 0; p++; } |
| 794 | if (*p == 0) break; |
| 795 | if (argc < M_VG_CMDLINE_OPTS-1) { |
| 796 | argv[argc] = p; argc++; |
| 797 | } else { |
| 798 | config_error( |
| 799 | "Found more than M_CMDLINE_OPTS command-line opts."); |
| 800 | } |
| 801 | while (*p != 0 && !ISSPACE(*p)) p++; |
| 802 | } |
| 803 | |
| 804 | for (i = 0; i < argc; i++) { |
| 805 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 806 | if (STREQ(argv[i], "-v") || STREQ(argv[i], "--verbose")) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 807 | VG_(clo_verbosity)++; |
| 808 | else if (STREQ(argv[i], "-q") || STREQ(argv[i], "--quiet")) |
| 809 | VG_(clo_verbosity)--; |
| 810 | |
sewardj | 2e43290 | 2002-06-13 20:44:00 +0000 | [diff] [blame] | 811 | else if (STREQ(argv[i], "--error-limit=yes")) |
| 812 | VG_(clo_error_limit) = True; |
| 813 | else if (STREQ(argv[i], "--error-limit=no")) |
| 814 | VG_(clo_error_limit) = False; |
sewardj | 72f98ff | 2002-06-13 17:23:38 +0000 | [diff] [blame] | 815 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 816 | else if (STREQ(argv[i], "--gdb-attach=yes")) |
| 817 | VG_(clo_GDB_attach) = True; |
| 818 | else if (STREQ(argv[i], "--gdb-attach=no")) |
| 819 | VG_(clo_GDB_attach) = False; |
| 820 | |
| 821 | else if (STREQ(argv[i], "--demangle=yes")) |
| 822 | VG_(clo_demangle) = True; |
| 823 | else if (STREQ(argv[i], "--demangle=no")) |
| 824 | VG_(clo_demangle) = False; |
| 825 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 826 | else if (STREQ(argv[i], "--sloppy-malloc=yes")) |
| 827 | VG_(clo_sloppy_malloc) = True; |
| 828 | else if (STREQ(argv[i], "--sloppy-malloc=no")) |
| 829 | VG_(clo_sloppy_malloc) = False; |
| 830 | |
sewardj | 246d466 | 2002-06-14 10:17:05 +0000 | [diff] [blame] | 831 | else if (STREQN(12, argv[i], "--alignment=")) |
| 832 | VG_(clo_alignment) = (Int)VG_(atoll)(&argv[i][12]); |
| 833 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 834 | else if (STREQ(argv[i], "--trace-children=yes")) |
| 835 | VG_(clo_trace_children) = True; |
| 836 | else if (STREQ(argv[i], "--trace-children=no")) |
| 837 | VG_(clo_trace_children) = False; |
| 838 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 839 | else if (STREQN(15, argv[i], "--sanity-level=")) |
| 840 | VG_(sanity_level) = (Int)VG_(atoll)(&argv[i][15]); |
| 841 | |
| 842 | else if (STREQN(13, argv[i], "--logfile-fd=")) |
| 843 | eventually_logfile_fd = (Int)VG_(atoll)(&argv[i][13]); |
| 844 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 845 | else if (STREQN(15, argv[i], "--suppressions=")) { |
| 846 | if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 847 | VG_(message)(Vg_UserMsg, "Too many suppression files specified."); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 848 | VG_(message)(Vg_UserMsg, |
| 849 | "Increase VG_CLO_MAX_SFILES and recompile."); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 850 | VG_(bad_option)(argv[i]); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 851 | } |
| 852 | VG_(clo_suppressions)[VG_(clo_n_suppressions)] = &argv[i][15]; |
| 853 | VG_(clo_n_suppressions)++; |
| 854 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 855 | else if (STREQ(argv[i], "--profile=yes")) |
| 856 | VG_(clo_profile) = True; |
| 857 | else if (STREQ(argv[i], "--profile=no")) |
| 858 | VG_(clo_profile) = False; |
| 859 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 860 | else if (STREQ(argv[i], "--single-step=yes")) |
| 861 | VG_(clo_single_step) = True; |
| 862 | else if (STREQ(argv[i], "--single-step=no")) |
| 863 | VG_(clo_single_step) = False; |
| 864 | |
| 865 | else if (STREQ(argv[i], "--optimise=yes")) |
| 866 | VG_(clo_optimise) = True; |
| 867 | else if (STREQ(argv[i], "--optimise=no")) |
| 868 | VG_(clo_optimise) = False; |
| 869 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 870 | /* "vwxyz" --> 000zyxwv (binary) */ |
| 871 | else if (STREQN(16, argv[i], "--trace-codegen=")) { |
| 872 | Int j; |
| 873 | char* opt = & argv[i][16]; |
| 874 | |
| 875 | if (5 != VG_(strlen)(opt)) { |
| 876 | VG_(message)(Vg_UserMsg, |
| 877 | "--trace-codegen argument must have 5 digits"); |
| 878 | VG_(bad_option)(argv[i]); |
| 879 | } |
| 880 | for (j = 0; j < 5; j++) { |
| 881 | if ('0' == opt[j]) { /* do nothing */ } |
| 882 | else if ('1' == opt[j]) VG_(clo_trace_codegen) |= (1 << j); |
| 883 | else { |
| 884 | VG_(message)(Vg_UserMsg, "--trace-codegen argument can only " |
| 885 | "contain 0s and 1s"); |
| 886 | VG_(bad_option)(argv[i]); |
| 887 | } |
| 888 | } |
| 889 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 890 | |
| 891 | else if (STREQ(argv[i], "--trace-syscalls=yes")) |
| 892 | VG_(clo_trace_syscalls) = True; |
| 893 | else if (STREQ(argv[i], "--trace-syscalls=no")) |
| 894 | VG_(clo_trace_syscalls) = False; |
| 895 | |
| 896 | else if (STREQ(argv[i], "--trace-signals=yes")) |
| 897 | VG_(clo_trace_signals) = True; |
| 898 | else if (STREQ(argv[i], "--trace-signals=no")) |
| 899 | VG_(clo_trace_signals) = False; |
| 900 | |
| 901 | else if (STREQ(argv[i], "--trace-symtab=yes")) |
| 902 | VG_(clo_trace_symtab) = True; |
| 903 | else if (STREQ(argv[i], "--trace-symtab=no")) |
| 904 | VG_(clo_trace_symtab) = False; |
| 905 | |
| 906 | else if (STREQ(argv[i], "--trace-malloc=yes")) |
| 907 | VG_(clo_trace_malloc) = True; |
| 908 | else if (STREQ(argv[i], "--trace-malloc=no")) |
| 909 | VG_(clo_trace_malloc) = False; |
| 910 | |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 911 | else if (STREQ(argv[i], "--trace-sched=yes")) |
| 912 | VG_(clo_trace_sched) = True; |
| 913 | else if (STREQ(argv[i], "--trace-sched=no")) |
| 914 | VG_(clo_trace_sched) = False; |
| 915 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 916 | else if (STREQ(argv[i], "--trace-pthread=none")) |
| 917 | VG_(clo_trace_pthread_level) = 0; |
| 918 | else if (STREQ(argv[i], "--trace-pthread=some")) |
| 919 | VG_(clo_trace_pthread_level) = 1; |
| 920 | else if (STREQ(argv[i], "--trace-pthread=all")) |
| 921 | VG_(clo_trace_pthread_level) = 2; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 922 | |
sewardj | 8d365b5 | 2002-05-12 10:52:16 +0000 | [diff] [blame] | 923 | else if (STREQN(14, argv[i], "--weird-hacks=")) |
| 924 | VG_(clo_weird_hacks) = &argv[i][14]; |
sewardj | 3984b85 | 2002-05-12 03:00:17 +0000 | [diff] [blame] | 925 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 926 | else if (STREQN(13, argv[i], "--stop-after=")) |
| 927 | VG_(clo_stop_after) = VG_(atoll)(&argv[i][13]); |
| 928 | |
| 929 | else if (STREQN(13, argv[i], "--dump-error=")) |
| 930 | VG_(clo_dump_error) = (Int)VG_(atoll)(&argv[i][13]); |
| 931 | |
| 932 | else if (STREQN(14, argv[i], "--num-callers=")) { |
| 933 | /* Make sure it's sane. */ |
| 934 | VG_(clo_backtrace_size) = (Int)VG_(atoll)(&argv[i][14]); |
| 935 | if (VG_(clo_backtrace_size) < 2) |
| 936 | VG_(clo_backtrace_size) = 2; |
| 937 | if (VG_(clo_backtrace_size) >= VG_DEEPEST_BACKTRACE) |
| 938 | VG_(clo_backtrace_size) = VG_DEEPEST_BACKTRACE; |
| 939 | } |
| 940 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 941 | else if (VG_(needs).command_line_options) { |
| 942 | Bool ok = SK_(process_cmd_line_option)(argv[i]); |
| 943 | if (!ok) |
| 944 | usage(); |
| 945 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 946 | else |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 947 | usage(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | # undef ISSPACE |
| 951 | # undef STREQ |
| 952 | # undef STREQN |
| 953 | |
| 954 | if (VG_(clo_verbosity < 0)) |
| 955 | VG_(clo_verbosity) = 0; |
| 956 | |
sewardj | 246d466 | 2002-06-14 10:17:05 +0000 | [diff] [blame] | 957 | if (VG_(clo_alignment) < 4 |
| 958 | || VG_(clo_alignment) > 4096 |
| 959 | || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) { |
| 960 | VG_(message)(Vg_UserMsg, ""); |
| 961 | VG_(message)(Vg_UserMsg, |
| 962 | "Invalid --alignment= setting. " |
| 963 | "Should be a power of 2, >= 4, <= 4096."); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 964 | VG_(bad_option)("--alignment"); |
sewardj | 246d466 | 2002-06-14 10:17:05 +0000 | [diff] [blame] | 965 | } |
| 966 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 967 | if (VG_(clo_GDB_attach) && VG_(clo_trace_children)) { |
| 968 | VG_(message)(Vg_UserMsg, ""); |
| 969 | VG_(message)(Vg_UserMsg, |
| 970 | "--gdb-attach=yes conflicts with --trace-children=yes"); |
| 971 | VG_(message)(Vg_UserMsg, |
| 972 | "Please choose one or the other, but not both."); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 973 | VG_(bad_option)("--gdb-attach=yes and --trace-children=yes"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 974 | } |
| 975 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 976 | VG_(clo_logfile_fd) = eventually_logfile_fd; |
| 977 | |
sewardj | 83adf41 | 2002-05-01 01:25:45 +0000 | [diff] [blame] | 978 | if (VG_(clo_verbosity > 0)) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 979 | VG_(message)(Vg_UserMsg, "%s-%s, %s for x86 GNU/Linux.", |
| 980 | VG_(needs).name, VERSION, VG_(needs).description); |
sewardj | 83adf41 | 2002-05-01 01:25:45 +0000 | [diff] [blame] | 981 | } |
sewardj | 3b2736a | 2002-03-24 12:18:35 +0000 | [diff] [blame] | 982 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 983 | if (VG_(clo_verbosity > 0)) |
| 984 | VG_(message)(Vg_UserMsg, |
| 985 | "Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward."); |
| 986 | if (VG_(clo_verbosity) > 1) { |
| 987 | VG_(message)(Vg_UserMsg, "Startup, with flags:"); |
| 988 | for (i = 0; i < argc; i++) { |
| 989 | VG_(message)(Vg_UserMsg, " %s", argv[i]); |
| 990 | } |
| 991 | } |
| 992 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 993 | if (VG_(clo_n_suppressions) == 0 && |
| 994 | (VG_(needs).core_errors || VG_(needs).skin_errors)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 995 | config_error("No error-suppression files were specified."); |
| 996 | } |
| 997 | } |
| 998 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 999 | /* --------------------------------------------------------------------- |
| 1000 | Copying to/from m_state_static. |
| 1001 | ------------------------------------------------------------------ */ |
| 1002 | |
| 1003 | UInt VG_(m_state_static) [8 /* int regs, in Intel order */ |
| 1004 | + 1 /* %eflags */ |
| 1005 | + 1 /* %eip */ |
| 1006 | + VG_SIZE_OF_FPUSTATE_W /* FPU state */ |
| 1007 | ]; |
| 1008 | |
| 1009 | void VG_(copy_baseBlock_to_m_state_static) ( void ) |
| 1010 | { |
| 1011 | Int i; |
| 1012 | VG_(m_state_static)[ 0/4] = VG_(baseBlock)[VGOFF_(m_eax)]; |
| 1013 | VG_(m_state_static)[ 4/4] = VG_(baseBlock)[VGOFF_(m_ecx)]; |
| 1014 | VG_(m_state_static)[ 8/4] = VG_(baseBlock)[VGOFF_(m_edx)]; |
| 1015 | VG_(m_state_static)[12/4] = VG_(baseBlock)[VGOFF_(m_ebx)]; |
| 1016 | VG_(m_state_static)[16/4] = VG_(baseBlock)[VGOFF_(m_esp)]; |
| 1017 | VG_(m_state_static)[20/4] = VG_(baseBlock)[VGOFF_(m_ebp)]; |
| 1018 | VG_(m_state_static)[24/4] = VG_(baseBlock)[VGOFF_(m_esi)]; |
| 1019 | VG_(m_state_static)[28/4] = VG_(baseBlock)[VGOFF_(m_edi)]; |
| 1020 | |
| 1021 | VG_(m_state_static)[32/4] = VG_(baseBlock)[VGOFF_(m_eflags)]; |
| 1022 | VG_(m_state_static)[36/4] = VG_(baseBlock)[VGOFF_(m_eip)]; |
| 1023 | |
| 1024 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
| 1025 | VG_(m_state_static)[40/4 + i] |
| 1026 | = VG_(baseBlock)[VGOFF_(m_fpustate) + i]; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | void VG_(copy_m_state_static_to_baseBlock) ( void ) |
| 1031 | { |
| 1032 | Int i; |
| 1033 | VG_(baseBlock)[VGOFF_(m_eax)] = VG_(m_state_static)[ 0/4]; |
| 1034 | VG_(baseBlock)[VGOFF_(m_ecx)] = VG_(m_state_static)[ 4/4]; |
| 1035 | VG_(baseBlock)[VGOFF_(m_edx)] = VG_(m_state_static)[ 8/4]; |
| 1036 | VG_(baseBlock)[VGOFF_(m_ebx)] = VG_(m_state_static)[12/4]; |
| 1037 | VG_(baseBlock)[VGOFF_(m_esp)] = VG_(m_state_static)[16/4]; |
| 1038 | VG_(baseBlock)[VGOFF_(m_ebp)] = VG_(m_state_static)[20/4]; |
| 1039 | VG_(baseBlock)[VGOFF_(m_esi)] = VG_(m_state_static)[24/4]; |
| 1040 | VG_(baseBlock)[VGOFF_(m_edi)] = VG_(m_state_static)[28/4]; |
| 1041 | |
| 1042 | VG_(baseBlock)[VGOFF_(m_eflags)] = VG_(m_state_static)[32/4]; |
| 1043 | VG_(baseBlock)[VGOFF_(m_eip)] = VG_(m_state_static)[36/4]; |
| 1044 | |
| 1045 | for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++) |
| 1046 | VG_(baseBlock)[VGOFF_(m_fpustate) + i] |
| 1047 | = VG_(m_state_static)[40/4 + i]; |
| 1048 | } |
| 1049 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1050 | Addr VG_(get_stack_pointer) ( void ) |
| 1051 | { |
| 1052 | return VG_(baseBlock)[VGOFF_(m_esp)]; |
| 1053 | } |
| 1054 | |
| 1055 | /* Some random tests needed for leak checking */ |
| 1056 | |
| 1057 | Bool VG_(within_stack)(Addr a) |
| 1058 | { |
| 1059 | if (a >= ((Addr)(&VG_(stack))) |
| 1060 | && a <= ((Addr)(&VG_(stack))) + sizeof(VG_(stack))) |
| 1061 | return True; |
| 1062 | else |
| 1063 | return False; |
| 1064 | } |
| 1065 | |
| 1066 | Bool VG_(within_m_state_static)(Addr a) |
| 1067 | { |
| 1068 | if (a >= ((Addr)(&VG_(m_state_static))) |
| 1069 | && a <= ((Addr)(&VG_(m_state_static))) + sizeof(VG_(m_state_static))) |
| 1070 | return True; |
| 1071 | else |
| 1072 | return False; |
| 1073 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1074 | |
| 1075 | /* --------------------------------------------------------------------- |
| 1076 | Show accumulated counts. |
| 1077 | ------------------------------------------------------------------ */ |
| 1078 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1079 | static __inline__ Int safe_idiv(Int a, Int b) |
| 1080 | { |
| 1081 | return (b == 0 ? 0 : a / b); |
| 1082 | } |
| 1083 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1084 | static void vg_show_counts ( void ) |
| 1085 | { |
| 1086 | VG_(message)(Vg_DebugMsg, |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1087 | " lru: %d epochs, %d clearings.", |
| 1088 | VG_(current_epoch), |
| 1089 | VG_(number_of_lrus) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1090 | VG_(message)(Vg_DebugMsg, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1091 | "translate: new %d (%d -> %d; ratio %d:10)", |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1092 | VG_(overall_in_count), |
| 1093 | VG_(overall_in_osize), |
| 1094 | VG_(overall_in_tsize), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1095 | safe_idiv(10*VG_(overall_in_tsize), VG_(overall_in_osize))); |
| 1096 | VG_(message)(Vg_DebugMsg, |
| 1097 | " discard %d (%d -> %d; ratio %d:10).", |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1098 | VG_(overall_out_count), |
| 1099 | VG_(overall_out_osize), |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1100 | VG_(overall_out_tsize), |
| 1101 | safe_idiv(10*VG_(overall_out_tsize), VG_(overall_out_osize))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1102 | VG_(message)(Vg_DebugMsg, |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1103 | " dispatch: %lu basic blocks, %d/%d sched events, %d tt_fast misses.", |
| 1104 | VG_(bbs_done), VG_(num_scheduling_events_MAJOR), |
| 1105 | VG_(num_scheduling_events_MINOR), |
| 1106 | VG_(tt_fast_misses)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1107 | VG_(message)(Vg_DebugMsg, |
| 1108 | "reg-alloc: %d t-req-spill, " |
| 1109 | "%d+%d orig+spill uis, %d total-reg-r.", |
| 1110 | VG_(translations_needing_spill), |
| 1111 | VG_(uinstrs_prealloc), |
| 1112 | VG_(uinstrs_spill), |
| 1113 | VG_(total_reg_rank) ); |
| 1114 | VG_(message)(Vg_DebugMsg, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1115 | " sanity: %d cheap, %d expensive checks.", |
| 1116 | VG_(sanity_fast_count), |
| 1117 | VG_(sanity_slow_count) ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1118 | VG_(print_ccall_stats)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | /* --------------------------------------------------------------------- |
| 1123 | Main! |
| 1124 | ------------------------------------------------------------------ */ |
| 1125 | |
| 1126 | /* Where we jump to once Valgrind has got control, and the real |
| 1127 | machine's state has been copied to the m_state_static. */ |
| 1128 | |
| 1129 | void VG_(main) ( void ) |
| 1130 | { |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1131 | Int i; |
| 1132 | VgSchedReturnCode src; |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1133 | ThreadState* tst; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* Set up our stack sanity-check words. */ |
| 1136 | for (i = 0; i < 10; i++) { |
| 1137 | VG_(stack)[i] = (UInt)(&VG_(stack)[i]) ^ 0xA4B3C2D1; |
| 1138 | VG_(stack)[10000-1-i] = (UInt)(&VG_(stack)[10000-i-1]) ^ 0xABCD4321; |
| 1139 | } |
| 1140 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1141 | /* Setup stuff that depends on the skin. Must be before: |
| 1142 | - vg_init_baseBlock(): to register helpers |
| 1143 | - process_cmd_line_options(): to register skin name and description, |
| 1144 | and turn on/off 'command_line_options' need |
| 1145 | - init_memory() (to setup memory event trackers). |
| 1146 | */ |
| 1147 | SK_(pre_clo_init) ( & VG_(needs), & VG_(track_events) ); |
| 1148 | sanity_check_needs(); |
| 1149 | |
| 1150 | /* Set up baseBlock offsets and copy the saved machine's state into it. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1151 | vg_init_baseBlock(); |
| 1152 | VG_(copy_m_state_static_to_baseBlock)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1153 | vg_init_shadow_regs(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* Process Valgrind's command-line opts (from env var VG_OPTS). */ |
| 1156 | process_cmd_line_options(); |
| 1157 | |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 1158 | /* Hook to delay things long enough so we can get the pid and |
| 1159 | attach GDB in another shell. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1160 | #if 0 |
| 1161 | { |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 1162 | Int p, q; |
| 1163 | for (p = 0; p < 50000; p++) |
| 1164 | for (q = 0; q < 50000; q++) ; |
| 1165 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1166 | #endif |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 1167 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1168 | /* Initialise the scheduler, and copy the client's state from |
| 1169 | baseBlock into VG_(threads)[1]. This has to come before signal |
| 1170 | initialisations. */ |
| 1171 | VG_(scheduler_init)(); |
| 1172 | |
| 1173 | /* Initialise the signal handling subsystem, temporarily parking |
| 1174 | the saved blocking-mask in saved_sigmask. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1175 | VG_(sigstartup_actions)(); |
| 1176 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1177 | /* Perhaps we're profiling Valgrind? */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1178 | if (VG_(clo_profile)) |
| 1179 | VGP_(init_profiling)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1180 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1181 | /* Start calibration of our RDTSC-based clock. */ |
| 1182 | VG_(start_rdtsc_calibration)(); |
| 1183 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1184 | /* Do this here just to give rdtsc calibration more time */ |
| 1185 | SK_(post_clo_init)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1186 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1187 | /* Must come after SK_(init) so memory handler accompaniments (eg. |
| 1188 | * shadow memory) can be setup ok */ |
| 1189 | VGP_PUSHCC(VgpInitMem); |
| 1190 | VG_(init_memory)(); |
| 1191 | VGP_POPCC(VgpInitMem); |
| 1192 | |
| 1193 | /* Read the list of errors to suppress. This should be found in |
| 1194 | the file specified by vg_clo_suppressions. */ |
| 1195 | if (VG_(needs).core_errors || VG_(needs).skin_errors) |
| 1196 | VG_(load_suppressions)(); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1197 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1198 | /* End calibration of our RDTSC-based clock, leaving it as long as |
| 1199 | we can. */ |
| 1200 | VG_(end_rdtsc_calibration)(); |
| 1201 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1202 | /* This should come after init_memory_and_symbols(); otherwise the |
| 1203 | latter carefully sets up the permissions maps to cover the |
| 1204 | anonymous mmaps for the translation table and translation cache, |
| 1205 | which wastes > 20M of virtual address space. */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1206 | VG_(init_tt_tc)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (VG_(clo_verbosity) == 1) { |
| 1209 | VG_(message)(Vg_UserMsg, |
| 1210 | "For more details, rerun with: -v"); |
| 1211 | } |
| 1212 | |
| 1213 | /* Now it is safe for malloc et al in vg_clientmalloc.c to act |
| 1214 | instrumented-ly. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1215 | if (VG_(clo_verbosity) > 0) |
| 1216 | VG_(message)(Vg_UserMsg, ""); |
| 1217 | |
| 1218 | VG_(bbs_to_go) = VG_(clo_stop_after); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1219 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1220 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1221 | /* Run! */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1222 | VG_(running_on_simd_CPU) = True; |
sewardj | 671ff54 | 2002-05-07 09:25:30 +0000 | [diff] [blame] | 1223 | VGP_PUSHCC(VgpSched); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1224 | src = VG_(scheduler)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1225 | VGP_POPCC(VgpSched); |
| 1226 | VG_(running_on_simd_CPU) = False; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (VG_(clo_verbosity) > 0) |
| 1229 | VG_(message)(Vg_UserMsg, ""); |
| 1230 | |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1231 | if (src == VgSrc_Deadlock) { |
| 1232 | VG_(message)(Vg_UserMsg, |
| 1233 | "Warning: pthread scheduler exited due to deadlock"); |
| 1234 | } |
| 1235 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1236 | if (VG_(needs).core_errors || VG_(needs).skin_errors) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1237 | VG_(show_all_errors)(); |
sewardj | 2e93c50 | 2002-04-12 11:12:52 +0000 | [diff] [blame] | 1238 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1239 | SK_(fini)(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1240 | |
sewardj | 0c3b53f | 2002-05-01 01:58:35 +0000 | [diff] [blame] | 1241 | VG_(do_sanity_checks)( True /*include expensive checks*/ ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (VG_(clo_verbosity) > 1) |
| 1244 | vg_show_counts(); |
| 1245 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1246 | if (VG_(clo_verbosity) > 2) |
| 1247 | VG_(print_UInstr_histogram)(); |
| 1248 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1249 | if (0) { |
| 1250 | VG_(message)(Vg_DebugMsg, ""); |
| 1251 | VG_(message)(Vg_DebugMsg, |
| 1252 | "------ Valgrind's internal memory use stats follow ------" ); |
| 1253 | VG_(mallocSanityCheckAll)(); |
| 1254 | VG_(show_all_arena_stats)(); |
| 1255 | VG_(message)(Vg_DebugMsg, |
| 1256 | "------ Valgrind's ExeContext management stats follow ------" ); |
| 1257 | VG_(show_ExeContext_stats)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1260 | if (VG_(clo_profile)) |
| 1261 | VGP_(done_profiling)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1262 | |
| 1263 | VG_(shutdown_logging)(); |
| 1264 | |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1265 | /* Remove valgrind.so from a LD_PRELOAD=... string so child |
| 1266 | processes don't get traced into. Also mess up $libdir/valgrind |
| 1267 | so that our libpthread.so disappears from view. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1268 | if (!VG_(clo_trace_children)) { |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1269 | VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH)( |
| 1270 | VG_(getenv)("LD_PRELOAD"), |
| 1271 | VG_(getenv)("LD_LIBRARY_PATH") |
| 1272 | ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1275 | /* Decide how to exit. This depends on what the scheduler |
| 1276 | returned. */ |
| 1277 | switch (src) { |
| 1278 | case VgSrc_ExitSyscall: /* the normal way out */ |
| 1279 | vg_assert(VG_(last_run_tid) > 0 |
| 1280 | && VG_(last_run_tid) < VG_N_THREADS); |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1281 | tst = & VG_(threads)[VG_(last_run_tid)]; |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1282 | vg_assert(tst->status == VgTs_Runnable); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1283 | /* The thread's %EBX at the time it did __NR_exit() will hold |
| 1284 | the arg to __NR_exit(), so we just do __NR_exit() with |
| 1285 | that arg. */ |
| 1286 | VG_(exit)( VG_(exitcode) ); |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1287 | /* NOT ALIVE HERE! */ |
| 1288 | VG_(panic)("entered the afterlife in vg_main() -- ExitSyscall"); |
| 1289 | break; /* what the hell :) */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1290 | |
sewardj | 7e87e38 | 2002-05-03 19:09:05 +0000 | [diff] [blame] | 1291 | case VgSrc_Deadlock: |
| 1292 | /* Just exit now. No point in continuing. */ |
| 1293 | VG_(exit)(0); |
| 1294 | VG_(panic)("entered the afterlife in vg_main() -- Deadlock"); |
| 1295 | break; |
| 1296 | |
| 1297 | case VgSrc_BbsDone: |
| 1298 | /* Tricky; we have to try and switch back to the real CPU. |
| 1299 | This is all very dodgy and won't work at all in the |
| 1300 | presence of threads, or if the client happened to be |
| 1301 | running a signal handler. */ |
| 1302 | /* Prepare to restore state to the real CPU. */ |
| 1303 | VG_(load_thread_state)(1 /* root thread */ ); |
| 1304 | VG_(copy_baseBlock_to_m_state_static)(); |
| 1305 | |
| 1306 | /* This pushes a return address on the simulator's stack, |
| 1307 | which is abandoned. We call vg_sigshutdown_actions() at |
| 1308 | the end of vg_switch_to_real_CPU(), so as to ensure that |
| 1309 | the original stack and machine state is restored before |
| 1310 | the real signal mechanism is restored. */ |
| 1311 | VG_(switch_to_real_CPU)(); |
| 1312 | |
| 1313 | default: |
| 1314 | VG_(panic)("vg_main(): unexpected scheduler return code"); |
| 1315 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | /* Debugging thing .. can be called from assembly with OYNK macro. */ |
| 1320 | void VG_(oynk) ( Int n ) |
| 1321 | { |
| 1322 | OINK(n); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | /* Find "valgrind.so" in a LD_PRELOAD=... string, and convert it to |
| 1327 | "valgrinq.so", which doesn't do anything. This is used to avoid |
| 1328 | tracing into child processes. To make this work the build system |
| 1329 | also supplies a dummy file, "valgrinq.so". |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1330 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1331 | Also replace "vgskin_<foo>.so" with whitespace, for the same reason; |
| 1332 | without it, child processes try to find valgrind.so symbols in the |
| 1333 | skin .so. |
| 1334 | |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1335 | Also look for $(libdir)/lib/valgrind in LD_LIBRARY_PATH and change |
| 1336 | it to $(libdir)/lib/valgrinq, so as to make our libpthread.so |
| 1337 | disappear. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1338 | */ |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1339 | void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str, |
| 1340 | Char* ld_library_path_str ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1341 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1342 | Char* p_prel = NULL; |
| 1343 | Char* sk_prel = NULL; |
| 1344 | Char* p_path = NULL; |
| 1345 | Int what = 0; |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1346 | if (ld_preload_str == NULL || ld_library_path_str == NULL) |
| 1347 | goto mutancy; |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1348 | |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1349 | /* VG_(printf)("%s %s\n", ld_preload_str, ld_library_path_str); */ |
| 1350 | |
| 1351 | p_prel = VG_(strstr)(ld_preload_str, "valgrind.so"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1352 | sk_prel = VG_(strstr)(ld_preload_str, "vgskin_"); |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1353 | p_path = VG_(strstr)(ld_library_path_str, VG_LIBDIR); |
| 1354 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1355 | what = 1; |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1356 | if (p_prel == NULL) { |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1357 | /* perhaps already happened? */ |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1358 | if (VG_(strstr)(ld_preload_str, "valgrinq.so") == NULL) |
| 1359 | goto mutancy; |
| 1360 | if (VG_(strstr)(ld_library_path_str, "lib/valgrinq") == NULL) |
| 1361 | goto mutancy; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1362 | return; |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1365 | what = 2; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1366 | if (sk_prel == NULL) goto mutancy; |
| 1367 | |
| 1368 | what = 3; |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1369 | if (p_path == NULL) goto mutancy; |
| 1370 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1371 | what = 4; |
| 1372 | { |
| 1373 | /* Blank from "vgskin_" back to prev. LD_PRELOAD entry, or start */ |
| 1374 | Char* p = sk_prel; |
| 1375 | while (*p != ':' && p > ld_preload_str) { |
| 1376 | *p = ' '; |
| 1377 | p--; |
| 1378 | } |
| 1379 | /* Blank from "vgskin_" to next LD_PRELOAD entry */ |
| 1380 | while (*p != ':' && *p != '\0') { |
| 1381 | *p = ' '; |
| 1382 | p++; |
| 1383 | } |
| 1384 | if (*p == '\0') goto mutancy; /* valgrind.so has disappeared?! */ |
| 1385 | *p = ' '; /* blank ending ':' */ |
| 1386 | } |
| 1387 | |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1388 | /* in LD_PRELOAD, turn valgrind.so into valgrinq.so. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1389 | what = 5; |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1390 | if (p_prel[7] != 'd') goto mutancy; |
| 1391 | p_prel[7] = 'q'; |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1392 | |
| 1393 | /* in LD_LIBRARY_PATH, turn $libdir/valgrind (as configure'd) from |
| 1394 | .../lib/valgrind .../lib/valgrinq, which doesn't exist, |
| 1395 | so that our own libpthread.so goes out of scope. */ |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1396 | p_path += VG_(strlen)(VG_LIBDIR); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1397 | what = 6; |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1398 | if (p_path[0] != '/') goto mutancy; |
| 1399 | p_path++; /* step over / */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1400 | what = 7; |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1401 | if (p_path[7] != 'd') goto mutancy; |
| 1402 | p_path[7] = 'q'; |
| 1403 | return; |
| 1404 | |
| 1405 | mutancy: |
| 1406 | VG_(printf)( |
| 1407 | "\nVG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH): internal error:\n" |
| 1408 | " what = %d\n" |
| 1409 | " ld_preload_str = `%s'\n" |
| 1410 | " ld_library_path_str = `%s'\n" |
| 1411 | " p_prel = `%s'\n" |
| 1412 | " p_path = `%s'\n" |
| 1413 | " VG_LIBDIR = `%s'\n", |
sewardj | 19d8141 | 2002-06-03 01:10:40 +0000 | [diff] [blame] | 1414 | what, ld_preload_str, ld_library_path_str, |
| 1415 | p_prel, p_path, VG_LIBDIR |
| 1416 | ); |
sewardj | c26b448 | 2002-07-13 12:20:35 +0000 | [diff] [blame] | 1417 | VG_(printf)( |
| 1418 | "\n" |
| 1419 | "Note that this is often caused by mis-installation of valgrind.\n" |
| 1420 | "Correct installation procedure is:\n" |
| 1421 | " ./configure --prefix=/install/dir\n" |
| 1422 | " make install\n" |
| 1423 | "And then use /install/dir/bin/valgrind\n" |
| 1424 | "Moving the installation directory elsewhere after 'make install'\n" |
| 1425 | "will cause the above error. Hand-editing the paths in the shell\n" |
| 1426 | "scripts is also likely to cause problems.\n" |
| 1427 | "\n" |
| 1428 | ); |
sewardj | 78e25c9 | 2002-05-20 23:38:33 +0000 | [diff] [blame] | 1429 | VG_(panic)("VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) failed\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
sewardj | 3e1eb1f | 2002-05-18 13:14:17 +0000 | [diff] [blame] | 1432 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1433 | /* RUNS ON THE CLIENT'S STACK, but on the real CPU. Start GDB and get |
| 1434 | it to attach to this process. Called if the user requests this |
| 1435 | service after an error has been shown, so she can poke around and |
| 1436 | look at parameters, memory, etc. You can't meaningfully get GDB to |
| 1437 | continue the program, though; to continue, quit GDB. */ |
| 1438 | extern void VG_(start_GDB_whilst_on_client_stack) ( void ) |
| 1439 | { |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1440 | Int res; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1441 | UChar buf[100]; |
| 1442 | VG_(sprintf)(buf, |
| 1443 | "/usr/bin/gdb -nw /proc/%d/exe %d", |
| 1444 | VG_(getpid)(), VG_(getpid)()); |
sewardj | e6a2524 | 2002-04-21 22:03:07 +0000 | [diff] [blame] | 1445 | VG_(message)(Vg_UserMsg, "starting GDB with cmd: %s", buf); |
| 1446 | res = VG_(system)(buf); |
| 1447 | if (res == 0) { |
| 1448 | VG_(message)(Vg_UserMsg, ""); |
| 1449 | VG_(message)(Vg_UserMsg, |
| 1450 | "GDB has detached. Valgrind regains control. We continue."); |
| 1451 | } else { |
| 1452 | VG_(message)(Vg_UserMsg, "Apparently failed!"); |
| 1453 | VG_(message)(Vg_UserMsg, ""); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1454 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | |
| 1458 | /* Print some helpful-ish text about unimplemented things, and give |
| 1459 | up. */ |
sewardj | cfc39b2 | 2002-05-08 01:58:18 +0000 | [diff] [blame] | 1460 | void VG_(unimplemented) ( Char* msg ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1461 | { |
| 1462 | VG_(message)(Vg_UserMsg, ""); |
| 1463 | VG_(message)(Vg_UserMsg, |
| 1464 | "Valgrind detected that your program requires"); |
| 1465 | VG_(message)(Vg_UserMsg, |
| 1466 | "the following unimplemented functionality:"); |
| 1467 | VG_(message)(Vg_UserMsg, " %s", msg); |
| 1468 | VG_(message)(Vg_UserMsg, |
| 1469 | "This may be because the functionality is hard to implement,"); |
| 1470 | VG_(message)(Vg_UserMsg, |
| 1471 | "or because no reasonable program would behave this way,"); |
| 1472 | VG_(message)(Vg_UserMsg, |
| 1473 | "or because nobody has yet needed it. In any case, let me know"); |
| 1474 | VG_(message)(Vg_UserMsg, |
| 1475 | "(jseward@acm.org) and/or try to work around the problem, if you can."); |
| 1476 | VG_(message)(Vg_UserMsg, |
| 1477 | ""); |
| 1478 | VG_(message)(Vg_UserMsg, |
| 1479 | "Valgrind has to exit now. Sorry. Bye!"); |
| 1480 | VG_(message)(Vg_UserMsg, |
| 1481 | ""); |
sewardj | 15a43e1 | 2002-04-17 19:35:12 +0000 | [diff] [blame] | 1482 | VG_(pp_sched_status)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1483 | VG_(exit)(1); |
| 1484 | } |
| 1485 | |
| 1486 | |
sewardj | cfc39b2 | 2002-05-08 01:58:18 +0000 | [diff] [blame] | 1487 | void VG_(nvidia_moan) ( void) |
| 1488 | { |
| 1489 | VG_(message)(Vg_UserMsg, |
| 1490 | "The following failure _might_ be caused by linking to NVidia's\n " |
| 1491 | "libGL.so, so avoiding it, if you can, _might_ help you. For example,\n " |
| 1492 | "re-build any Qt libraries you are using without OpenGL support."); |
| 1493 | } |
| 1494 | |
| 1495 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame^] | 1496 | /* --------------------------------------------------------------------- |
| 1497 | Sanity check machinery (permanently engaged). |
| 1498 | ------------------------------------------------------------------ */ |
| 1499 | |
| 1500 | /* A fast sanity check -- suitable for calling circa once per |
| 1501 | millisecond. */ |
| 1502 | |
| 1503 | void VG_(do_sanity_checks) ( Bool force_expensive ) |
| 1504 | { |
| 1505 | Int i; |
| 1506 | |
| 1507 | if (VG_(sanity_level) < 1) return; |
| 1508 | |
| 1509 | /* --- First do all the tests that we can do quickly. ---*/ |
| 1510 | |
| 1511 | VG_(sanity_fast_count)++; |
| 1512 | |
| 1513 | /* Check that we haven't overrun our private stack. */ |
| 1514 | for (i = 0; i < 10; i++) { |
| 1515 | vg_assert(VG_(stack)[i] |
| 1516 | == ((UInt)(&VG_(stack)[i]) ^ 0xA4B3C2D1)); |
| 1517 | vg_assert(VG_(stack)[10000-1-i] |
| 1518 | == ((UInt)(&VG_(stack)[10000-i-1]) ^ 0xABCD4321)); |
| 1519 | } |
| 1520 | |
| 1521 | /* Check stuff pertaining to the memory check system. */ |
| 1522 | |
| 1523 | /* Check that nobody has spuriously claimed that the first or |
| 1524 | last 16 pages of memory have become accessible [...] */ |
| 1525 | if (VG_(needs).sanity_checks) |
| 1526 | vg_assert(SK_(cheap_sanity_check)()); |
| 1527 | |
| 1528 | /* --- Now some more expensive checks. ---*/ |
| 1529 | |
| 1530 | /* Once every 25 times, check some more expensive stuff. */ |
| 1531 | if ( force_expensive |
| 1532 | || VG_(sanity_level) > 1 |
| 1533 | || (VG_(sanity_level) == 1 && (VG_(sanity_fast_count) % 25) == 0)) { |
| 1534 | |
| 1535 | VG_(sanity_slow_count)++; |
| 1536 | |
| 1537 | # if 0 |
| 1538 | { void zzzmemscan(void); zzzmemscan(); } |
| 1539 | # endif |
| 1540 | |
| 1541 | if ((VG_(sanity_fast_count) % 250) == 0) |
| 1542 | VG_(sanity_check_tc_tt)(); |
| 1543 | |
| 1544 | if (VG_(needs).sanity_checks) { |
| 1545 | vg_assert(SK_(expensive_sanity_check)()); |
| 1546 | } |
| 1547 | /* |
| 1548 | if ((VG_(sanity_fast_count) % 500) == 0) VG_(mallocSanityCheckAll)(); |
| 1549 | */ |
| 1550 | } |
| 1551 | |
| 1552 | if (VG_(sanity_level) > 1) { |
| 1553 | /* Check sanity of the low-level memory manager. Note that bugs |
| 1554 | in the client's code can cause this to fail, so we don't do |
| 1555 | this check unless specially asked for. And because it's |
| 1556 | potentially very expensive. */ |
| 1557 | VG_(mallocSanityCheckAll)(); |
| 1558 | } |
| 1559 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1560 | /*--------------------------------------------------------------------*/ |
| 1561 | /*--- end vg_main.c ---*/ |
| 1562 | /*--------------------------------------------------------------------*/ |