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