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