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