blob: 9f50ab21bd4e89bad42f0b9051dd524ce9e7652b [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- C startup stuff, reached from vg_startup.S. ---*/
4/*--- vg_main.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of Valgrind, an x86 protected-mode emulator
9 designed for debugging and profiling binaries on x86-Unixes.
10
11 Copyright (C) 2000-2002 Julian Seward
12 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000013
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
29 The GNU General Public License is contained in the file LICENSE.
30*/
31
32#include "vg_include.h"
33#include "vg_constants.h"
sewardjde4a1d02002-03-22 01:27:54 +000034
35
36/* ---------------------------------------------------------------------
37 Compute offsets into baseBlock. See comments in vg_include.h.
38 ------------------------------------------------------------------ */
39
40/* The variables storing offsets. */
41
42#define INVALID_OFFSET (-1)
43
44Int VGOFF_(m_eax) = INVALID_OFFSET;
45Int VGOFF_(m_ecx) = INVALID_OFFSET;
46Int VGOFF_(m_edx) = INVALID_OFFSET;
47Int VGOFF_(m_ebx) = INVALID_OFFSET;
48Int VGOFF_(m_esp) = INVALID_OFFSET;
49Int VGOFF_(m_ebp) = INVALID_OFFSET;
50Int VGOFF_(m_esi) = INVALID_OFFSET;
51Int VGOFF_(m_edi) = INVALID_OFFSET;
52Int VGOFF_(m_eflags) = INVALID_OFFSET;
53Int VGOFF_(m_fpustate) = INVALID_OFFSET;
54Int VGOFF_(m_eip) = INVALID_OFFSET;
55Int VGOFF_(spillslots) = INVALID_OFFSET;
56Int VGOFF_(sh_eax) = INVALID_OFFSET;
57Int VGOFF_(sh_ecx) = INVALID_OFFSET;
58Int VGOFF_(sh_edx) = INVALID_OFFSET;
59Int VGOFF_(sh_ebx) = INVALID_OFFSET;
60Int VGOFF_(sh_esp) = INVALID_OFFSET;
61Int VGOFF_(sh_ebp) = INVALID_OFFSET;
62Int VGOFF_(sh_esi) = INVALID_OFFSET;
63Int VGOFF_(sh_edi) = INVALID_OFFSET;
64Int VGOFF_(sh_eflags) = INVALID_OFFSET;
65Int VGOFF_(helper_idiv_64_32) = INVALID_OFFSET;
66Int VGOFF_(helper_div_64_32) = INVALID_OFFSET;
67Int VGOFF_(helper_idiv_32_16) = INVALID_OFFSET;
68Int VGOFF_(helper_div_32_16) = INVALID_OFFSET;
69Int VGOFF_(helper_idiv_16_8) = INVALID_OFFSET;
70Int VGOFF_(helper_div_16_8) = INVALID_OFFSET;
71Int VGOFF_(helper_imul_32_64) = INVALID_OFFSET;
72Int VGOFF_(helper_mul_32_64) = INVALID_OFFSET;
73Int VGOFF_(helper_imul_16_32) = INVALID_OFFSET;
74Int VGOFF_(helper_mul_16_32) = INVALID_OFFSET;
75Int VGOFF_(helper_imul_8_16) = INVALID_OFFSET;
76Int VGOFF_(helper_mul_8_16) = INVALID_OFFSET;
77Int VGOFF_(helper_CLD) = INVALID_OFFSET;
78Int VGOFF_(helper_STD) = INVALID_OFFSET;
79Int VGOFF_(helper_get_dirflag) = INVALID_OFFSET;
sewardj7d78e782002-06-02 00:04:00 +000080Int VGOFF_(helper_CLC) = INVALID_OFFSET;
81Int VGOFF_(helper_STC) = INVALID_OFFSET;
sewardjde4a1d02002-03-22 01:27:54 +000082Int VGOFF_(helper_shldl) = INVALID_OFFSET;
83Int VGOFF_(helper_shldw) = INVALID_OFFSET;
84Int VGOFF_(helper_shrdl) = INVALID_OFFSET;
85Int VGOFF_(helper_shrdw) = INVALID_OFFSET;
86Int VGOFF_(helper_RDTSC) = INVALID_OFFSET;
87Int VGOFF_(helper_CPUID) = INVALID_OFFSET;
88Int VGOFF_(helper_BSWAP) = INVALID_OFFSET;
sewardjde4a1d02002-03-22 01:27:54 +000089Int VGOFF_(helper_bsf) = INVALID_OFFSET;
90Int VGOFF_(helper_bsr) = INVALID_OFFSET;
91Int VGOFF_(helper_fstsw_AX) = INVALID_OFFSET;
92Int VGOFF_(helper_SAHF) = INVALID_OFFSET;
sewardj4d0ab1f2002-03-24 10:00:09 +000093Int VGOFF_(helper_DAS) = INVALID_OFFSET;
sewardjfe8a1662002-03-24 11:54:07 +000094Int VGOFF_(helper_DAA) = INVALID_OFFSET;
sewardjde4a1d02002-03-22 01:27:54 +000095Int VGOFF_(helper_value_check4_fail) = INVALID_OFFSET;
96Int VGOFF_(helper_value_check2_fail) = INVALID_OFFSET;
97Int VGOFF_(helper_value_check1_fail) = INVALID_OFFSET;
98Int VGOFF_(helper_value_check0_fail) = INVALID_OFFSET;
sewardjde4a1d02002-03-22 01:27:54 +000099Int VGOFF_(helperc_LOADV4) = INVALID_OFFSET;
100Int VGOFF_(helperc_LOADV2) = INVALID_OFFSET;
101Int VGOFF_(helperc_LOADV1) = INVALID_OFFSET;
102Int VGOFF_(helperc_STOREV4) = INVALID_OFFSET;
103Int VGOFF_(helperc_STOREV2) = INVALID_OFFSET;
104Int VGOFF_(helperc_STOREV1) = INVALID_OFFSET;
105Int VGOFF_(handle_esp_assignment) = INVALID_OFFSET;
106Int VGOFF_(fpu_write_check) = INVALID_OFFSET;
107Int VGOFF_(fpu_read_check) = INVALID_OFFSET;
njn4f9c9342002-04-29 16:03:24 +0000108Int VGOFF_(cachesim_log_non_mem_instr) = INVALID_OFFSET;
109Int VGOFF_(cachesim_log_mem_instr) = INVALID_OFFSET;
sewardjde4a1d02002-03-22 01:27:54 +0000110
111/* This is the actual defn of baseblock. */
112UInt VG_(baseBlock)[VG_BASEBLOCK_WORDS];
113
114/* Words. */
115static Int baB_off = 0;
116
117/* Returns the offset, in words. */
118static Int alloc_BaB ( Int words )
119{
120 Int off = baB_off;
121 baB_off += words;
122 if (baB_off >= VG_BASEBLOCK_WORDS)
123 VG_(panic)( "alloc_BaB: baseBlock is too small");
124
125 return off;
126}
127
128/* Allocate 1 word in baseBlock and set it to the given value. */
129static Int alloc_BaB_1_set ( Addr a )
130{
131 Int off = alloc_BaB(1);
132 VG_(baseBlock)[off] = (UInt)a;
133 return off;
134}
135
136
137/* Here we assign actual offsets. It's important to get the most
138 popular referents within 128 bytes of the start, so we can take
139 advantage of short addressing modes relative to %ebp. Popularity
140 of offsets was measured on 22 Feb 02 running a KDE application, and
141 the slots rearranged accordingly, with a 1.5% reduction in total
142 size of translations. */
143
144static void vg_init_baseBlock ( void )
145{
146 baB_off = 0;
147
148 /* Those with offsets under 128 are carefully chosen. */
149
150 /* WORD offsets in this column */
151 /* 0 */ VGOFF_(m_eax) = alloc_BaB(1);
152 /* 1 */ VGOFF_(m_ecx) = alloc_BaB(1);
153 /* 2 */ VGOFF_(m_edx) = alloc_BaB(1);
154 /* 3 */ VGOFF_(m_ebx) = alloc_BaB(1);
155 /* 4 */ VGOFF_(m_esp) = alloc_BaB(1);
156 /* 5 */ VGOFF_(m_ebp) = alloc_BaB(1);
157 /* 6 */ VGOFF_(m_esi) = alloc_BaB(1);
158 /* 7 */ VGOFF_(m_edi) = alloc_BaB(1);
159 /* 8 */ VGOFF_(m_eflags) = alloc_BaB(1);
160
161 /* 9 */ VGOFF_(sh_eax) = alloc_BaB(1);
162 /* 10 */ VGOFF_(sh_ecx) = alloc_BaB(1);
163 /* 11 */ VGOFF_(sh_edx) = alloc_BaB(1);
164 /* 12 */ VGOFF_(sh_ebx) = alloc_BaB(1);
165 /* 13 */ VGOFF_(sh_esp) = alloc_BaB(1);
166 /* 14 */ VGOFF_(sh_ebp) = alloc_BaB(1);
167 /* 15 */ VGOFF_(sh_esi) = alloc_BaB(1);
168 /* 16 */ VGOFF_(sh_edi) = alloc_BaB(1);
169 /* 17 */ VGOFF_(sh_eflags) = alloc_BaB(1);
170
njn4f9c9342002-04-29 16:03:24 +0000171 /* 17a */
172 VGOFF_(cachesim_log_non_mem_instr)
173 = alloc_BaB_1_set( (Addr) & VG_(cachesim_log_non_mem_instr) );
174 /* 17b */
175 VGOFF_(cachesim_log_mem_instr)
176 = alloc_BaB_1_set( (Addr) & VG_(cachesim_log_mem_instr) );
177
sewardjde4a1d02002-03-22 01:27:54 +0000178 /* 18 */
179 VGOFF_(helper_value_check4_fail)
180 = alloc_BaB_1_set( (Addr) & VG_(helper_value_check4_fail) );
181 /* 19 */
182 VGOFF_(helper_value_check0_fail)
183 = alloc_BaB_1_set( (Addr) & VG_(helper_value_check0_fail) );
184
185 /* 20 */
186 VGOFF_(helperc_STOREV4)
187 = alloc_BaB_1_set( (Addr) & VG_(helperc_STOREV4) );
188 /* 21 */
189 VGOFF_(helperc_STOREV1)
190 = alloc_BaB_1_set( (Addr) & VG_(helperc_STOREV1) );
191
192 /* 22 */
193 VGOFF_(helperc_LOADV4)
194 = alloc_BaB_1_set( (Addr) & VG_(helperc_LOADV4) );
195 /* 23 */
196 VGOFF_(helperc_LOADV1)
197 = alloc_BaB_1_set( (Addr) & VG_(helperc_LOADV1) );
198
199 /* 24 */
200 VGOFF_(handle_esp_assignment)
201 = alloc_BaB_1_set( (Addr) & VGM_(handle_esp_assignment) );
202
203 /* 25 */
204 VGOFF_(m_eip) = alloc_BaB(1);
205
206 /* There are currently 24 spill slots */
207 /* 26 .. 49 This overlaps the magic boundary at >= 32 words, but
208 most spills are to low numbered spill slots, so the ones above
209 the boundary don't see much action. */
210 VGOFF_(spillslots) = alloc_BaB(VG_MAX_SPILLSLOTS);
211
212 /* These two pushed beyond the boundary because 2-byte transactions
213 are rare. */
214 /* 50 */
215 VGOFF_(helperc_STOREV2)
216 = alloc_BaB_1_set( (Addr) & VG_(helperc_STOREV2) );
217 /* 51 */
218 VGOFF_(helperc_LOADV2)
219 = alloc_BaB_1_set( (Addr) & VG_(helperc_LOADV2) );
220
221 /* 52 */
222 VGOFF_(fpu_write_check)
223 = alloc_BaB_1_set( (Addr) & VGM_(fpu_write_check) );
224 /* 53 */
225 VGOFF_(fpu_read_check)
226 = alloc_BaB_1_set( (Addr) & VGM_(fpu_read_check) );
227
228 /* Actually I don't think these two are ever used. */
229 /* 54 */
230 VGOFF_(helper_value_check2_fail)
231 = alloc_BaB_1_set( (Addr) & VG_(helper_value_check2_fail) );
232 /* 55 */
233 VGOFF_(helper_value_check1_fail)
234 = alloc_BaB_1_set( (Addr) & VG_(helper_value_check1_fail) );
235
236 /* I gave up counting at this point. Since they're way above the
237 short-amode-boundary, there's no point. */
238
239 VGOFF_(m_fpustate) = alloc_BaB(VG_SIZE_OF_FPUSTATE_W);
240
241 VGOFF_(helper_idiv_64_32)
242 = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_64_32) );
243 VGOFF_(helper_div_64_32)
244 = alloc_BaB_1_set( (Addr) & VG_(helper_div_64_32) );
245 VGOFF_(helper_idiv_32_16)
246 = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_32_16) );
247 VGOFF_(helper_div_32_16)
248 = alloc_BaB_1_set( (Addr) & VG_(helper_div_32_16) );
249 VGOFF_(helper_idiv_16_8)
250 = alloc_BaB_1_set( (Addr) & VG_(helper_idiv_16_8) );
251 VGOFF_(helper_div_16_8)
252 = alloc_BaB_1_set( (Addr) & VG_(helper_div_16_8) );
253
254 VGOFF_(helper_imul_32_64)
255 = alloc_BaB_1_set( (Addr) & VG_(helper_imul_32_64) );
256 VGOFF_(helper_mul_32_64)
257 = alloc_BaB_1_set( (Addr) & VG_(helper_mul_32_64) );
258 VGOFF_(helper_imul_16_32)
259 = alloc_BaB_1_set( (Addr) & VG_(helper_imul_16_32) );
260 VGOFF_(helper_mul_16_32)
261 = alloc_BaB_1_set( (Addr) & VG_(helper_mul_16_32) );
262 VGOFF_(helper_imul_8_16)
263 = alloc_BaB_1_set( (Addr) & VG_(helper_imul_8_16) );
264 VGOFF_(helper_mul_8_16)
265 = alloc_BaB_1_set( (Addr) & VG_(helper_mul_8_16) );
266
267 VGOFF_(helper_CLD)
268 = alloc_BaB_1_set( (Addr) & VG_(helper_CLD) );
269 VGOFF_(helper_STD)
270 = alloc_BaB_1_set( (Addr) & VG_(helper_STD) );
271 VGOFF_(helper_get_dirflag)
272 = alloc_BaB_1_set( (Addr) & VG_(helper_get_dirflag) );
273
sewardj7d78e782002-06-02 00:04:00 +0000274 VGOFF_(helper_CLC)
275 = alloc_BaB_1_set( (Addr) & VG_(helper_CLC) );
276 VGOFF_(helper_STC)
277 = alloc_BaB_1_set( (Addr) & VG_(helper_STC) );
278
sewardjde4a1d02002-03-22 01:27:54 +0000279 VGOFF_(helper_shldl)
280 = alloc_BaB_1_set( (Addr) & VG_(helper_shldl) );
281 VGOFF_(helper_shldw)
282 = alloc_BaB_1_set( (Addr) & VG_(helper_shldw) );
283 VGOFF_(helper_shrdl)
284 = alloc_BaB_1_set( (Addr) & VG_(helper_shrdl) );
285 VGOFF_(helper_shrdw)
286 = alloc_BaB_1_set( (Addr) & VG_(helper_shrdw) );
287
288 VGOFF_(helper_RDTSC)
289 = alloc_BaB_1_set( (Addr) & VG_(helper_RDTSC) );
290 VGOFF_(helper_CPUID)
291 = alloc_BaB_1_set( (Addr) & VG_(helper_CPUID) );
292
sewardjde4a1d02002-03-22 01:27:54 +0000293 VGOFF_(helper_bsf)
294 = alloc_BaB_1_set( (Addr) & VG_(helper_bsf) );
295 VGOFF_(helper_bsr)
296 = alloc_BaB_1_set( (Addr) & VG_(helper_bsr) );
297
298 VGOFF_(helper_fstsw_AX)
299 = alloc_BaB_1_set( (Addr) & VG_(helper_fstsw_AX) );
300 VGOFF_(helper_SAHF)
301 = alloc_BaB_1_set( (Addr) & VG_(helper_SAHF) );
sewardj4d0ab1f2002-03-24 10:00:09 +0000302 VGOFF_(helper_DAS)
303 = alloc_BaB_1_set( (Addr) & VG_(helper_DAS) );
sewardjfe8a1662002-03-24 11:54:07 +0000304 VGOFF_(helper_DAA)
305 = alloc_BaB_1_set( (Addr) & VG_(helper_DAA) );
sewardjde4a1d02002-03-22 01:27:54 +0000306}
307
308
309/* ---------------------------------------------------------------------
310 Global entities which are not referenced from generated code.
311 ------------------------------------------------------------------ */
312
313/* The stack on which Valgrind runs. We can't use the same stack as
314 the simulatee -- that's an important design decision. */
315UInt VG_(stack)[10000];
316
317/* Ditto our signal delivery stack. */
318UInt VG_(sigstack)[10000];
319
320/* Saving stuff across system calls. */
sewardj43c356f2002-06-02 00:21:08 +0000321UInt VG_(real_fpu_state_saved_over_syscall)[VG_SIZE_OF_FPUSTATE_W];
322Addr VG_(esp_saved_over_syscall);
sewardjde4a1d02002-03-22 01:27:54 +0000323
324/* Counts downwards in vg_run_innerloop. */
325UInt VG_(dispatch_ctr);
326
sewardjde4a1d02002-03-22 01:27:54 +0000327
328/* 64-bit counter for the number of basic blocks done. */
329ULong VG_(bbs_done);
330/* 64-bit counter for the number of bbs to go before a debug exit. */
331ULong VG_(bbs_to_go);
332
333/* Produce debugging output? */
334Bool VG_(disassemble) = False;
335
336/* The current LRU epoch. */
337UInt VG_(current_epoch) = 0;
338
sewardj7e87e382002-05-03 19:09:05 +0000339/* This is the ThreadId of the last thread the scheduler ran. */
340ThreadId VG_(last_run_tid) = 0;
341
sewardjde4a1d02002-03-22 01:27:54 +0000342
343/* ---------------------------------------------------------------------
344 Counters, for informational purposes only.
345 ------------------------------------------------------------------ */
346
347/* Number of lookups which miss the fast tt helper. */
348UInt VG_(tt_fast_misses) = 0;
349
350
351/* Counts for LRU informational messages. */
352
353/* Number and total o/t size of new translations this epoch. */
354UInt VG_(this_epoch_in_count) = 0;
355UInt VG_(this_epoch_in_osize) = 0;
356UInt VG_(this_epoch_in_tsize) = 0;
357/* Number and total o/t size of discarded translations this epoch. */
358UInt VG_(this_epoch_out_count) = 0;
359UInt VG_(this_epoch_out_osize) = 0;
360UInt VG_(this_epoch_out_tsize) = 0;
361/* Number and total o/t size of translations overall. */
362UInt VG_(overall_in_count) = 0;
363UInt VG_(overall_in_osize) = 0;
364UInt VG_(overall_in_tsize) = 0;
365/* Number and total o/t size of discards overall. */
366UInt VG_(overall_out_count) = 0;
367UInt VG_(overall_out_osize) = 0;
368UInt VG_(overall_out_tsize) = 0;
369
370/* The number of LRU-clearings of TT/TC. */
371UInt VG_(number_of_lrus) = 0;
372
373
374/* Counts pertaining to the register allocator. */
375
376/* total number of uinstrs input to reg-alloc */
377UInt VG_(uinstrs_prealloc) = 0;
378
379/* total number of uinstrs added due to spill code */
380UInt VG_(uinstrs_spill) = 0;
381
382/* number of bbs requiring spill code */
383UInt VG_(translations_needing_spill) = 0;
384
385/* total of register ranks over all translations */
386UInt VG_(total_reg_rank) = 0;
387
388
sewardjde4a1d02002-03-22 01:27:54 +0000389/* Counts pertaining to internal sanity checking. */
sewardjde4a1d02002-03-22 01:27:54 +0000390UInt VG_(sanity_fast_count) = 0;
391UInt VG_(sanity_slow_count) = 0;
392
sewardj2e93c502002-04-12 11:12:52 +0000393/* Counts pertaining to the scheduler. */
394UInt VG_(num_scheduling_events_MINOR) = 0;
395UInt VG_(num_scheduling_events_MAJOR) = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000396
397
398/* ---------------------------------------------------------------------
399 Values derived from command-line options.
400 ------------------------------------------------------------------ */
401
sewardj97ced732002-03-25 00:07:36 +0000402Bool VG_(clo_check_addrVs);
sewardjde4a1d02002-03-22 01:27:54 +0000403Bool VG_(clo_GDB_attach);
404Int VG_(sanity_level);
405Int VG_(clo_verbosity);
406Bool VG_(clo_demangle);
407Bool VG_(clo_leak_check);
408Bool VG_(clo_show_reachable);
409Int VG_(clo_leak_resolution);
410Bool VG_(clo_sloppy_malloc);
411Bool VG_(clo_partial_loads_ok);
412Bool VG_(clo_trace_children);
413Int VG_(clo_logfile_fd);
414Int VG_(clo_freelist_vol);
415Bool VG_(clo_workaround_gcc296_bugs);
416Int VG_(clo_n_suppressions);
417Char* VG_(clo_suppressions)[VG_CLO_MAX_SFILES];
418Bool VG_(clo_single_step);
419Bool VG_(clo_optimise);
420Bool VG_(clo_instrument);
421Bool VG_(clo_cleanup);
njn4f9c9342002-04-29 16:03:24 +0000422Bool VG_(clo_cachesim);
sewardjde4a1d02002-03-22 01:27:54 +0000423Int VG_(clo_smc_check);
424Bool VG_(clo_trace_syscalls);
425Bool VG_(clo_trace_signals);
426Bool VG_(clo_trace_symtab);
427Bool VG_(clo_trace_malloc);
sewardj8937c812002-04-12 20:12:20 +0000428Bool VG_(clo_trace_sched);
sewardj45b4b372002-04-16 22:50:32 +0000429Int VG_(clo_trace_pthread_level);
sewardjde4a1d02002-03-22 01:27:54 +0000430ULong VG_(clo_stop_after);
431Int VG_(clo_dump_error);
432Int VG_(clo_backtrace_size);
sewardj8d365b52002-05-12 10:52:16 +0000433Char* VG_(clo_weird_hacks);
sewardjde4a1d02002-03-22 01:27:54 +0000434
435/* This Bool is needed by wrappers in vg_clientmalloc.c to decide how
436 to behave. Initially we say False. */
437Bool VG_(running_on_simd_CPU) = False;
438
439/* Holds client's %esp at the point we gained control. */
440Addr VG_(esp_at_startup);
441
442/* As deduced from VG_(esp_at_startup), the client's argc, argv[] and
443 envp[] as extracted from the client's stack at startup-time. */
444Int VG_(client_argc);
445Char** VG_(client_argv);
446Char** VG_(client_envp);
447
448/* A place into which to copy the value of env var VG_ARGS, so we
449 don't have to modify the original. */
450static Char vg_cmdline_copy[M_VG_CMDLINE_STRLEN];
451
452
453/* ---------------------------------------------------------------------
sewardjde4a1d02002-03-22 01:27:54 +0000454 Processing of command-line options.
455 ------------------------------------------------------------------ */
456
457static void bad_option ( Char* opt )
458{
459 VG_(shutdown_logging)();
460 VG_(clo_logfile_fd) = 2; /* stderr */
461 VG_(printf)("valgrind.so: Bad option `%s'; aborting.\n", opt);
462 VG_(exit)(1);
463}
464
465static void config_error ( Char* msg )
466{
467 VG_(shutdown_logging)();
468 VG_(clo_logfile_fd) = 2; /* stderr */
469 VG_(printf)("valgrind.so: Startup or configuration error:\n\t%s\n", msg);
470 VG_(printf)("valgrind.so: Unable to start up properly. Giving up.\n");
471 VG_(exit)(1);
472}
473
sewardja1679dd2002-05-10 22:31:40 +0000474static void args_grok_error ( Char* msg )
475{
476 VG_(shutdown_logging)();
477 VG_(clo_logfile_fd) = 2; /* stderr */
478 VG_(printf)("valgrind.so: When searching for "
479 "client's argc/argc/envp:\n\t%s\n", msg);
480 config_error("couldn't find client's argc/argc/envp");
481}
482
sewardjde4a1d02002-03-22 01:27:54 +0000483
484static void process_cmd_line_options ( void )
485{
486 UChar* argv[M_VG_CMDLINE_OPTS];
487 UInt argc;
488 UChar* p;
489 UChar* str;
sewardja1679dd2002-05-10 22:31:40 +0000490 Int i, eventually_logfile_fd, ctr;
sewardjde4a1d02002-03-22 01:27:54 +0000491
492# define ISSPACE(cc) ((cc) == ' ' || (cc) == '\t' || (cc) == '\n')
493# define STREQ(s1,s2) (0==VG_(strcmp_ws)((s1),(s2)))
494# define STREQN(nn,s1,s2) (0==VG_(strncmp_ws)((s1),(s2),(nn)))
495
496 /* Set defaults. */
sewardj97ced732002-03-25 00:07:36 +0000497 VG_(clo_check_addrVs) = True;
sewardjde4a1d02002-03-22 01:27:54 +0000498 VG_(clo_GDB_attach) = False;
499 VG_(sanity_level) = 1;
500 VG_(clo_verbosity) = 1;
501 VG_(clo_demangle) = True;
502 VG_(clo_leak_check) = False;
503 VG_(clo_show_reachable) = False;
504 VG_(clo_leak_resolution) = 2;
505 VG_(clo_sloppy_malloc) = False;
506 VG_(clo_partial_loads_ok) = True;
507 VG_(clo_trace_children) = False;
508 VG_(clo_logfile_fd) = 2; /* stderr */
509 VG_(clo_freelist_vol) = 1000000;
510 VG_(clo_workaround_gcc296_bugs) = False;
511 VG_(clo_n_suppressions) = 0;
512 VG_(clo_single_step) = False;
513 VG_(clo_optimise) = True;
514 VG_(clo_instrument) = True;
515 VG_(clo_cleanup) = True;
sewardjde4a1d02002-03-22 01:27:54 +0000516 VG_(clo_smc_check) = /* VG_CLO_SMC_SOME */ VG_CLO_SMC_NONE;
517 VG_(clo_trace_syscalls) = False;
518 VG_(clo_trace_signals) = False;
519 VG_(clo_trace_symtab) = False;
520 VG_(clo_trace_malloc) = False;
sewardj8937c812002-04-12 20:12:20 +0000521 VG_(clo_trace_sched) = False;
sewardj45b4b372002-04-16 22:50:32 +0000522 VG_(clo_trace_pthread_level) = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000523 VG_(clo_stop_after) = 1000000000000LL;
524 VG_(clo_dump_error) = 0;
525 VG_(clo_backtrace_size) = 4;
sewardj8d365b52002-05-12 10:52:16 +0000526 VG_(clo_weird_hacks) = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000527
528 eventually_logfile_fd = VG_(clo_logfile_fd);
529
530 /* Once logging is started, we can safely send messages pertaining
531 to failures in initialisation. */
532 VG_(startup_logging)();
533
sewardj38170912002-05-10 21:07:22 +0000534
sewardja1679dd2002-05-10 22:31:40 +0000535 /* (Suggested by Fabrice Bellard ... )
536 We look for the Linux ELF table and go down until we find the
sewardj38170912002-05-10 21:07:22 +0000537 envc & envp. It is not full proof, but these structures should
538 change less often than the libc ones. */
539 {
sewardja1679dd2002-05-10 22:31:40 +0000540 UInt* sp = 0; /* bogus init to keep gcc -O happy */
541
sewardj38170912002-05-10 21:07:22 +0000542 /* locate the top of the stack */
sewardja1679dd2002-05-10 22:31:40 +0000543 if (VG_STACK_MATCHES_BASE( VG_(esp_at_startup),
544 VG_STARTUP_STACK_BASE_1 )) {
545 sp = (UInt*)VG_STARTUP_STACK_BASE_1;
546 } else
547 if (VG_STACK_MATCHES_BASE( VG_(esp_at_startup),
548 VG_STARTUP_STACK_BASE_2 )) {
549 sp = (UInt*)VG_STARTUP_STACK_BASE_2;
550 } else {
551 args_grok_error(
552 "startup %esp is not near any VG_STARTUP_STACK_BASE_*\n "
553 "constants defined in vg_include.h. You should investigate."
554 );
555 }
556
sewardj38170912002-05-10 21:07:22 +0000557 /* we locate: NEW_AUX_ENT(1, AT_PAGESZ, ELF_EXEC_PAGESIZE) in
558 the elf interpreter table */
559 sp -= 2;
sewardj38170912002-05-10 21:07:22 +0000560 while (sp[0] != VKI_AT_PAGESZ || sp[1] != 4096) {
sewardja1679dd2002-05-10 22:31:40 +0000561 /* VG_(printf)("trying %p\n", sp); */
sewardj38170912002-05-10 21:07:22 +0000562 sp--;
563 }
sewardj38170912002-05-10 21:07:22 +0000564
565 if (sp[2] == VKI_AT_BASE
566 && sp[0] == VKI_AT_PAGESZ
567 && sp[-2] == VKI_AT_PHNUM
568 && sp[-4] == VKI_AT_PHENT
sewardjd9c2d6d2002-05-18 11:55:05 +0000569 && sp[-6] == VKI_AT_PHDR
570 && sp[-6-1] == 0) {
sewardja1679dd2002-05-10 22:31:40 +0000571 if (0)
572 VG_(printf)("Looks like you've got a 2.2.X kernel here.\n");
sewardj38170912002-05-10 21:07:22 +0000573 sp -= 6;
sewardja1679dd2002-05-10 22:31:40 +0000574 } else
575 if (sp[2] == VKI_AT_CLKTCK
576 && sp[0] == VKI_AT_PAGESZ
sewardjd9c2d6d2002-05-18 11:55:05 +0000577 && sp[-2] == VKI_AT_HWCAP
578 && sp[-2-1] == 0) {
sewardja1679dd2002-05-10 22:31:40 +0000579 if (0)
580 VG_(printf)("Looks like you've got a 2.4.X kernel here.\n");
581 sp -= 2;
582 } else
sewardjd9c2d6d2002-05-18 11:55:05 +0000583 if (sp[2] == VKI_AT_CLKTCK
584 && sp[0] == VKI_AT_PAGESZ
585 && sp[-2] == VKI_AT_HWCAP
586 && sp[-2-20-1] == 0) {
587 if (0)
588 VG_(printf)("Looks like you've got a early 2.4.X kernel here.\n");
589 sp -= 22;
590 } else
sewardja1679dd2002-05-10 22:31:40 +0000591 args_grok_error(
592 "ELF frame does not look like 2.2.X or 2.4.X.\n "
593 "See kernel sources linux/fs/binfmt_elf.c to make sense of this."
594 );
sewardj38170912002-05-10 21:07:22 +0000595
596 sp--;
sewardja1679dd2002-05-10 22:31:40 +0000597 if (*sp != 0)
598 args_grok_error("can't find NULL at end of env[]");
599
sewardj38170912002-05-10 21:07:22 +0000600 /* sp now points to NULL at the end of env[] */
sewardja1679dd2002-05-10 22:31:40 +0000601 ctr = 0;
sewardj38170912002-05-10 21:07:22 +0000602 while (True) {
603 sp --;
604 if (*sp == 0) break;
sewardja1679dd2002-05-10 22:31:40 +0000605 if (++ctr >= 1000)
606 args_grok_error(
607 "suspiciously many (1000) env[] entries; giving up");
608
sewardj38170912002-05-10 21:07:22 +0000609 }
610 /* sp now points to NULL at the end of argv[] */
sewardja1679dd2002-05-10 22:31:40 +0000611 VG_(client_envp) = (Char**)(sp+1);
sewardj38170912002-05-10 21:07:22 +0000612
sewardja1679dd2002-05-10 22:31:40 +0000613 ctr = 0;
sewardj38170912002-05-10 21:07:22 +0000614 VG_(client_argc) = 0;
615 while (True) {
616 sp--;
617 if (*sp == VG_(client_argc))
618 break;
619 VG_(client_argc)++;
sewardja1679dd2002-05-10 22:31:40 +0000620 if (++ctr >= 1000)
621 args_grok_error(
622 "suspiciously many (1000) argv[] entries; giving up");
sewardj38170912002-05-10 21:07:22 +0000623 }
624
sewardja1679dd2002-05-10 22:31:40 +0000625 VG_(client_argv) = (Char**)(sp+1);
sewardj38170912002-05-10 21:07:22 +0000626 }
627
sewardjde4a1d02002-03-22 01:27:54 +0000628 /* Now that VG_(client_envp) has been set, we can extract the args
629 for Valgrind itself. Copy into global var so that we don't have to
630 write zeroes to the getenv'd value itself. */
631 str = VG_(getenv)("VG_ARGS");
632 argc = 0;
633
634 if (!str) {
635 config_error("Can't read options from env var VG_ARGS.");
636 }
637
638 if (VG_(strlen)(str) >= M_VG_CMDLINE_STRLEN-1) {
639 config_error("Command line length exceeds M_CMDLINE_STRLEN.");
640 }
641 VG_(strcpy)(vg_cmdline_copy, str);
642 str = NULL;
643
644 p = &vg_cmdline_copy[0];
645 while (True) {
646 while (ISSPACE(*p)) { *p = 0; p++; }
647 if (*p == 0) break;
648 if (argc < M_VG_CMDLINE_OPTS-1) {
649 argv[argc] = p; argc++;
650 } else {
651 config_error(
652 "Found more than M_CMDLINE_OPTS command-line opts.");
653 }
654 while (*p != 0 && !ISSPACE(*p)) p++;
655 }
656
657 for (i = 0; i < argc; i++) {
658
659 if (STREQ(argv[i], "-v") || STREQ(argv[i], "--verbose"))
660 VG_(clo_verbosity)++;
661 else if (STREQ(argv[i], "-q") || STREQ(argv[i], "--quiet"))
662 VG_(clo_verbosity)--;
663
sewardj97ced732002-03-25 00:07:36 +0000664 else if (STREQ(argv[i], "--check-addrVs=yes"))
665 VG_(clo_check_addrVs) = True;
666 else if (STREQ(argv[i], "--check-addrVs=no"))
667 VG_(clo_check_addrVs) = False;
668
sewardjde4a1d02002-03-22 01:27:54 +0000669 else if (STREQ(argv[i], "--gdb-attach=yes"))
670 VG_(clo_GDB_attach) = True;
671 else if (STREQ(argv[i], "--gdb-attach=no"))
672 VG_(clo_GDB_attach) = False;
673
674 else if (STREQ(argv[i], "--demangle=yes"))
675 VG_(clo_demangle) = True;
676 else if (STREQ(argv[i], "--demangle=no"))
677 VG_(clo_demangle) = False;
678
679 else if (STREQ(argv[i], "--partial-loads-ok=yes"))
680 VG_(clo_partial_loads_ok) = True;
681 else if (STREQ(argv[i], "--partial-loads-ok=no"))
682 VG_(clo_partial_loads_ok) = False;
683
684 else if (STREQ(argv[i], "--leak-check=yes"))
685 VG_(clo_leak_check) = True;
686 else if (STREQ(argv[i], "--leak-check=no"))
687 VG_(clo_leak_check) = False;
688
689 else if (STREQ(argv[i], "--show-reachable=yes"))
690 VG_(clo_show_reachable) = True;
691 else if (STREQ(argv[i], "--show-reachable=no"))
692 VG_(clo_show_reachable) = False;
693
694 else if (STREQ(argv[i], "--leak-resolution=low"))
695 VG_(clo_leak_resolution) = 2;
696 else if (STREQ(argv[i], "--leak-resolution=med"))
697 VG_(clo_leak_resolution) = 4;
698 else if (STREQ(argv[i], "--leak-resolution=high"))
699 VG_(clo_leak_resolution) = VG_DEEPEST_BACKTRACE;
700
701 else if (STREQ(argv[i], "--sloppy-malloc=yes"))
702 VG_(clo_sloppy_malloc) = True;
703 else if (STREQ(argv[i], "--sloppy-malloc=no"))
704 VG_(clo_sloppy_malloc) = False;
705
706 else if (STREQ(argv[i], "--trace-children=yes"))
707 VG_(clo_trace_children) = True;
708 else if (STREQ(argv[i], "--trace-children=no"))
709 VG_(clo_trace_children) = False;
710
711 else if (STREQ(argv[i], "--workaround-gcc296-bugs=yes"))
712 VG_(clo_workaround_gcc296_bugs) = True;
713 else if (STREQ(argv[i], "--workaround-gcc296-bugs=no"))
714 VG_(clo_workaround_gcc296_bugs) = False;
715
716 else if (STREQN(15, argv[i], "--sanity-level="))
717 VG_(sanity_level) = (Int)VG_(atoll)(&argv[i][15]);
718
719 else if (STREQN(13, argv[i], "--logfile-fd="))
720 eventually_logfile_fd = (Int)VG_(atoll)(&argv[i][13]);
721
722 else if (STREQN(15, argv[i], "--freelist-vol=")) {
723 VG_(clo_freelist_vol) = (Int)VG_(atoll)(&argv[i][15]);
724 if (VG_(clo_freelist_vol) < 0) VG_(clo_freelist_vol) = 2;
725 }
726
727 else if (STREQN(15, argv[i], "--suppressions=")) {
728 if (VG_(clo_n_suppressions) >= VG_CLO_MAX_SFILES) {
729 VG_(message)(Vg_UserMsg, "Too many logfiles specified.");
730 VG_(message)(Vg_UserMsg,
731 "Increase VG_CLO_MAX_SFILES and recompile.");
732 bad_option(argv[i]);
733 }
734 VG_(clo_suppressions)[VG_(clo_n_suppressions)] = &argv[i][15];
735 VG_(clo_n_suppressions)++;
736 }
737 else if (STREQ(argv[i], "--single-step=yes"))
738 VG_(clo_single_step) = True;
739 else if (STREQ(argv[i], "--single-step=no"))
740 VG_(clo_single_step) = False;
741
742 else if (STREQ(argv[i], "--optimise=yes"))
743 VG_(clo_optimise) = True;
744 else if (STREQ(argv[i], "--optimise=no"))
745 VG_(clo_optimise) = False;
746
747 else if (STREQ(argv[i], "--instrument=yes"))
748 VG_(clo_instrument) = True;
749 else if (STREQ(argv[i], "--instrument=no"))
750 VG_(clo_instrument) = False;
751
752 else if (STREQ(argv[i], "--cleanup=yes"))
753 VG_(clo_cleanup) = True;
754 else if (STREQ(argv[i], "--cleanup=no"))
755 VG_(clo_cleanup) = False;
756
njn4f9c9342002-04-29 16:03:24 +0000757 else if (STREQ(argv[i], "--cachesim=yes"))
758 VG_(clo_cachesim) = True;
759 else if (STREQ(argv[i], "--cachesim=no"))
760 VG_(clo_cachesim) = False;
761
sewardjde4a1d02002-03-22 01:27:54 +0000762 else if (STREQ(argv[i], "--smc-check=none"))
763 VG_(clo_smc_check) = VG_CLO_SMC_NONE;
764 else if (STREQ(argv[i], "--smc-check=some"))
765 VG_(clo_smc_check) = VG_CLO_SMC_SOME;
766 else if (STREQ(argv[i], "--smc-check=all"))
767 VG_(clo_smc_check) = VG_CLO_SMC_ALL;
768
769 else if (STREQ(argv[i], "--trace-syscalls=yes"))
770 VG_(clo_trace_syscalls) = True;
771 else if (STREQ(argv[i], "--trace-syscalls=no"))
772 VG_(clo_trace_syscalls) = False;
773
774 else if (STREQ(argv[i], "--trace-signals=yes"))
775 VG_(clo_trace_signals) = True;
776 else if (STREQ(argv[i], "--trace-signals=no"))
777 VG_(clo_trace_signals) = False;
778
779 else if (STREQ(argv[i], "--trace-symtab=yes"))
780 VG_(clo_trace_symtab) = True;
781 else if (STREQ(argv[i], "--trace-symtab=no"))
782 VG_(clo_trace_symtab) = False;
783
784 else if (STREQ(argv[i], "--trace-malloc=yes"))
785 VG_(clo_trace_malloc) = True;
786 else if (STREQ(argv[i], "--trace-malloc=no"))
787 VG_(clo_trace_malloc) = False;
788
sewardj8937c812002-04-12 20:12:20 +0000789 else if (STREQ(argv[i], "--trace-sched=yes"))
790 VG_(clo_trace_sched) = True;
791 else if (STREQ(argv[i], "--trace-sched=no"))
792 VG_(clo_trace_sched) = False;
793
sewardj45b4b372002-04-16 22:50:32 +0000794 else if (STREQ(argv[i], "--trace-pthread=none"))
795 VG_(clo_trace_pthread_level) = 0;
796 else if (STREQ(argv[i], "--trace-pthread=some"))
797 VG_(clo_trace_pthread_level) = 1;
798 else if (STREQ(argv[i], "--trace-pthread=all"))
799 VG_(clo_trace_pthread_level) = 2;
sewardj8937c812002-04-12 20:12:20 +0000800
sewardj8d365b52002-05-12 10:52:16 +0000801 else if (STREQN(14, argv[i], "--weird-hacks="))
802 VG_(clo_weird_hacks) = &argv[i][14];
sewardj3984b852002-05-12 03:00:17 +0000803
sewardjde4a1d02002-03-22 01:27:54 +0000804 else if (STREQN(13, argv[i], "--stop-after="))
805 VG_(clo_stop_after) = VG_(atoll)(&argv[i][13]);
806
807 else if (STREQN(13, argv[i], "--dump-error="))
808 VG_(clo_dump_error) = (Int)VG_(atoll)(&argv[i][13]);
809
810 else if (STREQN(14, argv[i], "--num-callers=")) {
811 /* Make sure it's sane. */
812 VG_(clo_backtrace_size) = (Int)VG_(atoll)(&argv[i][14]);
813 if (VG_(clo_backtrace_size) < 2)
814 VG_(clo_backtrace_size) = 2;
815 if (VG_(clo_backtrace_size) >= VG_DEEPEST_BACKTRACE)
816 VG_(clo_backtrace_size) = VG_DEEPEST_BACKTRACE;
817 }
818
819 else
820 bad_option(argv[i]);
821 }
822
823# undef ISSPACE
824# undef STREQ
825# undef STREQN
826
827 if (VG_(clo_verbosity < 0))
828 VG_(clo_verbosity) = 0;
829
830 if (VG_(clo_GDB_attach) && VG_(clo_trace_children)) {
831 VG_(message)(Vg_UserMsg, "");
832 VG_(message)(Vg_UserMsg,
833 "--gdb-attach=yes conflicts with --trace-children=yes");
834 VG_(message)(Vg_UserMsg,
835 "Please choose one or the other, but not both.");
836 bad_option("--gdb-attach=yes and --trace-children=yes");
837 }
838
sewardjde4a1d02002-03-22 01:27:54 +0000839 VG_(clo_logfile_fd) = eventually_logfile_fd;
840
njn4f9c9342002-04-29 16:03:24 +0000841 /* Don't do memory checking if simulating the cache. */
842 if (VG_(clo_cachesim)) {
843 VG_(clo_instrument) = False;
844 }
845
sewardj83adf412002-05-01 01:25:45 +0000846 if (VG_(clo_verbosity > 0)) {
847 if (VG_(clo_cachesim)) {
848 VG_(message)(Vg_UserMsg,
849 "cachegrind-%s, an I1/D1/L2 cache profiler for x86 GNU/Linux.",
850 VERSION);
851 } else {
852 VG_(message)(Vg_UserMsg,
853 "valgrind-%s, a memory error detector for x86 GNU/Linux.",
854 VERSION);
855 }
856 }
sewardj3b2736a2002-03-24 12:18:35 +0000857
sewardjde4a1d02002-03-22 01:27:54 +0000858 if (VG_(clo_verbosity > 0))
859 VG_(message)(Vg_UserMsg,
860 "Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward.");
861 if (VG_(clo_verbosity) > 1) {
862 VG_(message)(Vg_UserMsg, "Startup, with flags:");
863 for (i = 0; i < argc; i++) {
864 VG_(message)(Vg_UserMsg, " %s", argv[i]);
865 }
866 }
867
sewardj83adf412002-05-01 01:25:45 +0000868 if (VG_(clo_n_suppressions) == 0 && !VG_(clo_cachesim)) {
sewardjde4a1d02002-03-22 01:27:54 +0000869 config_error("No error-suppression files were specified.");
870 }
871}
872
873
874/* ---------------------------------------------------------------------
875 Copying to/from m_state_static.
876 ------------------------------------------------------------------ */
877
878UInt VG_(m_state_static) [8 /* int regs, in Intel order */
879 + 1 /* %eflags */
880 + 1 /* %eip */
881 + VG_SIZE_OF_FPUSTATE_W /* FPU state */
882 ];
883
884void VG_(copy_baseBlock_to_m_state_static) ( void )
885{
886 Int i;
887 VG_(m_state_static)[ 0/4] = VG_(baseBlock)[VGOFF_(m_eax)];
888 VG_(m_state_static)[ 4/4] = VG_(baseBlock)[VGOFF_(m_ecx)];
889 VG_(m_state_static)[ 8/4] = VG_(baseBlock)[VGOFF_(m_edx)];
890 VG_(m_state_static)[12/4] = VG_(baseBlock)[VGOFF_(m_ebx)];
891 VG_(m_state_static)[16/4] = VG_(baseBlock)[VGOFF_(m_esp)];
892 VG_(m_state_static)[20/4] = VG_(baseBlock)[VGOFF_(m_ebp)];
893 VG_(m_state_static)[24/4] = VG_(baseBlock)[VGOFF_(m_esi)];
894 VG_(m_state_static)[28/4] = VG_(baseBlock)[VGOFF_(m_edi)];
895
896 VG_(m_state_static)[32/4] = VG_(baseBlock)[VGOFF_(m_eflags)];
897 VG_(m_state_static)[36/4] = VG_(baseBlock)[VGOFF_(m_eip)];
898
899 for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++)
900 VG_(m_state_static)[40/4 + i]
901 = VG_(baseBlock)[VGOFF_(m_fpustate) + i];
902}
903
904
905void VG_(copy_m_state_static_to_baseBlock) ( void )
906{
907 Int i;
908 VG_(baseBlock)[VGOFF_(m_eax)] = VG_(m_state_static)[ 0/4];
909 VG_(baseBlock)[VGOFF_(m_ecx)] = VG_(m_state_static)[ 4/4];
910 VG_(baseBlock)[VGOFF_(m_edx)] = VG_(m_state_static)[ 8/4];
911 VG_(baseBlock)[VGOFF_(m_ebx)] = VG_(m_state_static)[12/4];
912 VG_(baseBlock)[VGOFF_(m_esp)] = VG_(m_state_static)[16/4];
913 VG_(baseBlock)[VGOFF_(m_ebp)] = VG_(m_state_static)[20/4];
914 VG_(baseBlock)[VGOFF_(m_esi)] = VG_(m_state_static)[24/4];
915 VG_(baseBlock)[VGOFF_(m_edi)] = VG_(m_state_static)[28/4];
916
917 VG_(baseBlock)[VGOFF_(m_eflags)] = VG_(m_state_static)[32/4];
918 VG_(baseBlock)[VGOFF_(m_eip)] = VG_(m_state_static)[36/4];
919
920 for (i = 0; i < VG_SIZE_OF_FPUSTATE_W; i++)
921 VG_(baseBlock)[VGOFF_(m_fpustate) + i]
922 = VG_(m_state_static)[40/4 + i];
923}
924
925
926/* ---------------------------------------------------------------------
927 Show accumulated counts.
928 ------------------------------------------------------------------ */
929
930static void vg_show_counts ( void )
931{
932 VG_(message)(Vg_DebugMsg,
sewardj2e93c502002-04-12 11:12:52 +0000933 " lru: %d epochs, %d clearings.",
934 VG_(current_epoch),
935 VG_(number_of_lrus) );
sewardjde4a1d02002-03-22 01:27:54 +0000936 VG_(message)(Vg_DebugMsg,
937 "translate: new %d (%d -> %d), discard %d (%d -> %d).",
938 VG_(overall_in_count),
939 VG_(overall_in_osize),
940 VG_(overall_in_tsize),
941 VG_(overall_out_count),
942 VG_(overall_out_osize),
943 VG_(overall_out_tsize) );
944 VG_(message)(Vg_DebugMsg,
sewardj2e93c502002-04-12 11:12:52 +0000945 " dispatch: %lu basic blocks, %d/%d sched events, %d tt_fast misses.",
946 VG_(bbs_done), VG_(num_scheduling_events_MAJOR),
947 VG_(num_scheduling_events_MINOR),
948 VG_(tt_fast_misses));
sewardjde4a1d02002-03-22 01:27:54 +0000949 VG_(message)(Vg_DebugMsg,
950 "reg-alloc: %d t-req-spill, "
951 "%d+%d orig+spill uis, %d total-reg-r.",
952 VG_(translations_needing_spill),
953 VG_(uinstrs_prealloc),
954 VG_(uinstrs_spill),
955 VG_(total_reg_rank) );
956 VG_(message)(Vg_DebugMsg,
sewardjde4a1d02002-03-22 01:27:54 +0000957 " sanity: %d cheap, %d expensive checks.",
958 VG_(sanity_fast_count),
959 VG_(sanity_slow_count) );
960}
961
962
963/* ---------------------------------------------------------------------
964 Main!
965 ------------------------------------------------------------------ */
966
967/* Where we jump to once Valgrind has got control, and the real
968 machine's state has been copied to the m_state_static. */
969
970void VG_(main) ( void )
971{
sewardj2e93c502002-04-12 11:12:52 +0000972 Int i;
973 VgSchedReturnCode src;
sewardj7e87e382002-05-03 19:09:05 +0000974 ThreadState* tst;
sewardjde4a1d02002-03-22 01:27:54 +0000975
976 /* Set up our stack sanity-check words. */
977 for (i = 0; i < 10; i++) {
978 VG_(stack)[i] = (UInt)(&VG_(stack)[i]) ^ 0xA4B3C2D1;
979 VG_(stack)[10000-1-i] = (UInt)(&VG_(stack)[10000-i-1]) ^ 0xABCD4321;
980 }
981
982 /* Set up baseBlock offsets and copy the saved machine's state into
983 it. */
984 vg_init_baseBlock();
985 VG_(copy_m_state_static_to_baseBlock)();
986
987 /* Process Valgrind's command-line opts (from env var VG_OPTS). */
988 process_cmd_line_options();
989
sewardj64039bb2002-06-03 00:58:18 +0000990 /* Hook to delay things long enough so we can get the pid and
991 attach GDB in another shell. */
992 if (0) {
993 Int p, q;
994 for (p = 0; p < 50000; p++)
995 for (q = 0; q < 50000; q++) ;
996 }
997
sewardj018f7622002-05-15 21:13:39 +0000998 /* Initialise the scheduler, and copy the client's state from
999 baseBlock into VG_(threads)[1]. This has to come before signal
1000 initialisations. */
1001 VG_(scheduler_init)();
1002
1003 /* Initialise the signal handling subsystem, temporarily parking
1004 the saved blocking-mask in saved_sigmask. */
sewardjde4a1d02002-03-22 01:27:54 +00001005 VG_(sigstartup_actions)();
1006
sewardj018f7622002-05-15 21:13:39 +00001007 /* Perhaps we're profiling Valgrind? */
sewardjde4a1d02002-03-22 01:27:54 +00001008# ifdef VG_PROFILE
1009 VGP_(init_profiling)();
1010# endif
1011
sewardj5f07b662002-04-23 16:52:51 +00001012 /* Start calibration of our RDTSC-based clock. */
1013 VG_(start_rdtsc_calibration)();
1014
njn4f9c9342002-04-29 16:03:24 +00001015 if (VG_(clo_instrument) || VG_(clo_cachesim)) {
sewardjde4a1d02002-03-22 01:27:54 +00001016 VGP_PUSHCC(VgpInitAudit);
1017 VGM_(init_memory_audit)();
1018 VGP_POPCC;
sewardjde4a1d02002-03-22 01:27:54 +00001019 }
1020
sewardj18d75132002-05-16 11:06:21 +00001021 VGP_PUSHCC(VgpReadSyms);
1022 VG_(read_symbols)();
1023 VGP_POPCC;
1024
sewardj5f07b662002-04-23 16:52:51 +00001025 /* End calibration of our RDTSC-based clock, leaving it as long as
1026 we can. */
1027 VG_(end_rdtsc_calibration)();
1028
sewardjde4a1d02002-03-22 01:27:54 +00001029 /* This should come after init_memory_audit; otherwise the latter
1030 carefully sets up the permissions maps to cover the anonymous
1031 mmaps for the translation table and translation cache, which
1032 wastes > 20M of virtual address space. */
sewardj18d75132002-05-16 11:06:21 +00001033 VG_(init_tt_tc)();
sewardjde4a1d02002-03-22 01:27:54 +00001034
1035 if (VG_(clo_verbosity) == 1) {
1036 VG_(message)(Vg_UserMsg,
1037 "For more details, rerun with: -v");
1038 }
1039
1040 /* Now it is safe for malloc et al in vg_clientmalloc.c to act
1041 instrumented-ly. */
1042 VG_(running_on_simd_CPU) = True;
1043 if (VG_(clo_instrument)) {
1044 VGM_(make_readable) ( (Addr)&VG_(running_on_simd_CPU), 1 );
1045 VGM_(make_readable) ( (Addr)&VG_(clo_instrument), 1 );
1046 VGM_(make_readable) ( (Addr)&VG_(clo_trace_malloc), 1 );
1047 VGM_(make_readable) ( (Addr)&VG_(clo_sloppy_malloc), 1 );
1048 }
1049
njn4f9c9342002-04-29 16:03:24 +00001050 if (VG_(clo_cachesim))
1051 VG_(init_cachesim)();
1052
sewardjde4a1d02002-03-22 01:27:54 +00001053 if (VG_(clo_verbosity) > 0)
1054 VG_(message)(Vg_UserMsg, "");
1055
1056 VG_(bbs_to_go) = VG_(clo_stop_after);
sewardj2e93c502002-04-12 11:12:52 +00001057
sewardj018f7622002-05-15 21:13:39 +00001058 /* Run! */
sewardj671ff542002-05-07 09:25:30 +00001059 VGP_PUSHCC(VgpSched);
sewardj2e93c502002-04-12 11:12:52 +00001060 src = VG_(scheduler)();
sewardj671ff542002-05-07 09:25:30 +00001061 VGP_POPCC;
sewardjde4a1d02002-03-22 01:27:54 +00001062
1063 if (VG_(clo_verbosity) > 0)
1064 VG_(message)(Vg_UserMsg, "");
1065
sewardj2e93c502002-04-12 11:12:52 +00001066 if (src == VgSrc_Deadlock) {
1067 VG_(message)(Vg_UserMsg,
1068 "Warning: pthread scheduler exited due to deadlock");
1069 }
1070
sewardjde4a1d02002-03-22 01:27:54 +00001071 if (VG_(clo_instrument)) {
1072 VG_(show_all_errors)();
1073 VG_(clientmalloc_done)();
1074 if (VG_(clo_verbosity) == 1) {
1075 VG_(message)(Vg_UserMsg,
1076 "For counts of detected errors, rerun with: -v");
1077 }
1078 if (VG_(clo_leak_check)) VG_(detect_memory_leaks)();
1079 }
1080 VG_(running_on_simd_CPU) = False;
sewardj2e93c502002-04-12 11:12:52 +00001081
njn4f9c9342002-04-29 16:03:24 +00001082 if (VG_(clo_cachesim))
1083 VG_(show_cachesim_results)(VG_(client_argc), VG_(client_argv));
1084
sewardj0c3b53f2002-05-01 01:58:35 +00001085 VG_(do_sanity_checks)( True /*include expensive checks*/ );
sewardjde4a1d02002-03-22 01:27:54 +00001086
1087 if (VG_(clo_verbosity) > 1)
1088 vg_show_counts();
1089
1090 if (0) {
1091 VG_(message)(Vg_DebugMsg, "");
1092 VG_(message)(Vg_DebugMsg,
1093 "------ Valgrind's internal memory use stats follow ------" );
1094 VG_(mallocSanityCheckAll)();
1095 VG_(show_all_arena_stats)();
1096 VG_(message)(Vg_DebugMsg,
1097 "------ Valgrind's ExeContext management stats follow ------" );
1098 VG_(show_ExeContext_stats)();
1099 VG_(message)(Vg_DebugMsg,
1100 "------ Valgrind's client block stats follow ---------------" );
1101 VG_(show_client_block_stats)();
1102 }
1103
1104# ifdef VG_PROFILE
1105 VGP_(done_profiling)();
1106# endif
1107
1108 VG_(done_prof_mem)();
1109
1110 VG_(shutdown_logging)();
1111
sewardj3e1eb1f2002-05-18 13:14:17 +00001112 /* Remove valgrind.so from a LD_PRELOAD=... string so child
1113 processes don't get traced into. Also mess up $libdir/valgrind
1114 so that our libpthread.so disappears from view. */
sewardjde4a1d02002-03-22 01:27:54 +00001115 if (!VG_(clo_trace_children)) {
sewardj3e1eb1f2002-05-18 13:14:17 +00001116 VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH)(
1117 VG_(getenv)("LD_PRELOAD"),
1118 VG_(getenv)("LD_LIBRARY_PATH")
1119 );
sewardjde4a1d02002-03-22 01:27:54 +00001120 }
1121
sewardj7e87e382002-05-03 19:09:05 +00001122 /* Decide how to exit. This depends on what the scheduler
1123 returned. */
1124 switch (src) {
1125 case VgSrc_ExitSyscall: /* the normal way out */
1126 vg_assert(VG_(last_run_tid) > 0
1127 && VG_(last_run_tid) < VG_N_THREADS);
sewardj018f7622002-05-15 21:13:39 +00001128 tst = & VG_(threads)[VG_(last_run_tid)];
sewardj7e87e382002-05-03 19:09:05 +00001129 vg_assert(tst->status == VgTs_Runnable);
1130 /* The thread's %EBX will hold the arg to exit(), so we just
1131 do exit with that arg. */
1132 VG_(exit)( tst->m_ebx );
1133 /* NOT ALIVE HERE! */
1134 VG_(panic)("entered the afterlife in vg_main() -- ExitSyscall");
1135 break; /* what the hell :) */
sewardjde4a1d02002-03-22 01:27:54 +00001136
sewardj7e87e382002-05-03 19:09:05 +00001137 case VgSrc_Deadlock:
1138 /* Just exit now. No point in continuing. */
1139 VG_(exit)(0);
1140 VG_(panic)("entered the afterlife in vg_main() -- Deadlock");
1141 break;
1142
1143 case VgSrc_BbsDone:
1144 /* Tricky; we have to try and switch back to the real CPU.
1145 This is all very dodgy and won't work at all in the
1146 presence of threads, or if the client happened to be
1147 running a signal handler. */
1148 /* Prepare to restore state to the real CPU. */
1149 VG_(load_thread_state)(1 /* root thread */ );
1150 VG_(copy_baseBlock_to_m_state_static)();
1151
1152 /* This pushes a return address on the simulator's stack,
1153 which is abandoned. We call vg_sigshutdown_actions() at
1154 the end of vg_switch_to_real_CPU(), so as to ensure that
1155 the original stack and machine state is restored before
1156 the real signal mechanism is restored. */
1157 VG_(switch_to_real_CPU)();
1158
1159 default:
1160 VG_(panic)("vg_main(): unexpected scheduler return code");
1161 }
sewardjde4a1d02002-03-22 01:27:54 +00001162}
1163
1164
1165/* Debugging thing .. can be called from assembly with OYNK macro. */
1166void VG_(oynk) ( Int n )
1167{
1168 OINK(n);
1169}
1170
1171
1172/* Find "valgrind.so" in a LD_PRELOAD=... string, and convert it to
1173 "valgrinq.so", which doesn't do anything. This is used to avoid
1174 tracing into child processes. To make this work the build system
1175 also supplies a dummy file, "valgrinq.so".
sewardj78e25c92002-05-20 23:38:33 +00001176
1177 Also look for $(libdir)/lib/valgrind in LD_LIBRARY_PATH and change
1178 it to $(libdir)/lib/valgrinq, so as to make our libpthread.so
1179 disappear.
sewardjde4a1d02002-03-22 01:27:54 +00001180*/
sewardj3e1eb1f2002-05-18 13:14:17 +00001181void VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) ( Char* ld_preload_str,
1182 Char* ld_library_path_str )
sewardjde4a1d02002-03-22 01:27:54 +00001183{
sewardj78e25c92002-05-20 23:38:33 +00001184 Char* p_prel = NULL;
1185 Char* p_path = NULL;
1186 Int what = 0;
1187 if (ld_preload_str == NULL || ld_library_path_str == NULL)
1188 goto mutancy;
sewardj3e1eb1f2002-05-18 13:14:17 +00001189
sewardj78e25c92002-05-20 23:38:33 +00001190 /* VG_(printf)("%s %s\n", ld_preload_str, ld_library_path_str); */
1191
1192 p_prel = VG_(strstr)(ld_preload_str, "valgrind.so");
1193 p_path = VG_(strstr)(ld_library_path_str, VG_LIBDIR);
1194
1195 if (p_prel == NULL) {
sewardj3e1eb1f2002-05-18 13:14:17 +00001196 /* perhaps already happened? */
sewardj78e25c92002-05-20 23:38:33 +00001197 what = 1;
1198 if (VG_(strstr)(ld_preload_str, "valgrinq.so") == NULL)
1199 goto mutancy;
1200 if (VG_(strstr)(ld_library_path_str, "lib/valgrinq") == NULL)
1201 goto mutancy;
sewardjde4a1d02002-03-22 01:27:54 +00001202 return;
sewardj3e1eb1f2002-05-18 13:14:17 +00001203 }
1204
sewardj78e25c92002-05-20 23:38:33 +00001205 what = 2;
1206 if (p_path == NULL) goto mutancy;
1207
1208 /* in LD_PRELOAD, turn valgrind.so into valgrinq.so. */
1209 what = 3;
1210 if (p_prel[7] != 'd') goto mutancy;
1211 p_prel[7] = 'q';
sewardj3e1eb1f2002-05-18 13:14:17 +00001212
1213 /* in LD_LIBRARY_PATH, turn $libdir/valgrind (as configure'd) from
1214 .../lib/valgrind .../lib/valgrinq, which doesn't exist,
1215 so that our own libpthread.so goes out of scope. */
sewardj78e25c92002-05-20 23:38:33 +00001216 p_path += VG_(strlen)(VG_LIBDIR);
1217 what = 4;
1218 if (p_path[0] != '/') goto mutancy;
1219 p_path++; /* step over / */
1220 what = 5;
1221 if (p_path[7] != 'd') goto mutancy;
1222 p_path[7] = 'q';
1223 return;
1224
1225 mutancy:
1226 VG_(printf)(
1227 "\nVG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH): internal error:\n"
1228 " what = %d\n"
1229 " ld_preload_str = `%s'\n"
1230 " ld_library_path_str = `%s'\n"
1231 " p_prel = `%s'\n"
1232 " p_path = `%s'\n"
1233 " VG_LIBDIR = `%s'\n",
1234 what, ld_preload_str, ld_library_path_str, p_prel, p_path, VG_LIBDIR );
1235 VG_(panic)("VG_(mash_LD_PRELOAD_and_LD_LIBRARY_PATH) failed\n");
sewardjde4a1d02002-03-22 01:27:54 +00001236}
1237
sewardj3e1eb1f2002-05-18 13:14:17 +00001238
sewardjde4a1d02002-03-22 01:27:54 +00001239/* RUNS ON THE CLIENT'S STACK, but on the real CPU. Start GDB and get
1240 it to attach to this process. Called if the user requests this
1241 service after an error has been shown, so she can poke around and
1242 look at parameters, memory, etc. You can't meaningfully get GDB to
1243 continue the program, though; to continue, quit GDB. */
1244extern void VG_(start_GDB_whilst_on_client_stack) ( void )
1245{
sewardje6a25242002-04-21 22:03:07 +00001246 Int res;
sewardjde4a1d02002-03-22 01:27:54 +00001247 UChar buf[100];
1248 VG_(sprintf)(buf,
1249 "/usr/bin/gdb -nw /proc/%d/exe %d",
1250 VG_(getpid)(), VG_(getpid)());
sewardje6a25242002-04-21 22:03:07 +00001251 VG_(message)(Vg_UserMsg, "starting GDB with cmd: %s", buf);
1252 res = VG_(system)(buf);
1253 if (res == 0) {
1254 VG_(message)(Vg_UserMsg, "");
1255 VG_(message)(Vg_UserMsg,
1256 "GDB has detached. Valgrind regains control. We continue.");
1257 } else {
1258 VG_(message)(Vg_UserMsg, "Apparently failed!");
1259 VG_(message)(Vg_UserMsg, "");
sewardjde4a1d02002-03-22 01:27:54 +00001260 }
sewardjde4a1d02002-03-22 01:27:54 +00001261}
1262
1263
1264/* Print some helpful-ish text about unimplemented things, and give
1265 up. */
sewardjcfc39b22002-05-08 01:58:18 +00001266void VG_(unimplemented) ( Char* msg )
sewardjde4a1d02002-03-22 01:27:54 +00001267{
1268 VG_(message)(Vg_UserMsg, "");
1269 VG_(message)(Vg_UserMsg,
1270 "Valgrind detected that your program requires");
1271 VG_(message)(Vg_UserMsg,
1272 "the following unimplemented functionality:");
1273 VG_(message)(Vg_UserMsg, " %s", msg);
1274 VG_(message)(Vg_UserMsg,
1275 "This may be because the functionality is hard to implement,");
1276 VG_(message)(Vg_UserMsg,
1277 "or because no reasonable program would behave this way,");
1278 VG_(message)(Vg_UserMsg,
1279 "or because nobody has yet needed it. In any case, let me know");
1280 VG_(message)(Vg_UserMsg,
1281 "(jseward@acm.org) and/or try to work around the problem, if you can.");
1282 VG_(message)(Vg_UserMsg,
1283 "");
1284 VG_(message)(Vg_UserMsg,
1285 "Valgrind has to exit now. Sorry. Bye!");
1286 VG_(message)(Vg_UserMsg,
1287 "");
sewardj15a43e12002-04-17 19:35:12 +00001288 VG_(pp_sched_status)();
sewardjde4a1d02002-03-22 01:27:54 +00001289 VG_(exit)(1);
1290}
1291
1292
sewardjcfc39b22002-05-08 01:58:18 +00001293void VG_(nvidia_moan) ( void)
1294{
1295 VG_(message)(Vg_UserMsg,
1296 "The following failure _might_ be caused by linking to NVidia's\n "
1297 "libGL.so, so avoiding it, if you can, _might_ help you. For example,\n "
1298 "re-build any Qt libraries you are using without OpenGL support.");
1299}
1300
1301
sewardjde4a1d02002-03-22 01:27:54 +00001302/*--------------------------------------------------------------------*/
1303/*--- end vg_main.c ---*/
1304/*--------------------------------------------------------------------*/