drd: Add a consistency check
[valgrind.git] / drd / drd_main.c
blob0960fe5ded79344fa4bdbb44cb1e01be1183c1fe
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2013 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
21 The GNU General Public License is contained in the file COPYING.
25 #include "drd_barrier.h"
26 #include "drd_clientobj.h"
27 #include "drd_clientreq.h"
28 #include "drd_cond.h"
29 #include "drd_error.h"
30 #include "drd_hb.h"
31 #include "drd_load_store.h"
32 #include "drd_malloc_wrappers.h"
33 #include "drd_mutex.h"
34 #include "drd_rwlock.h"
35 #include "drd_segment.h"
36 #include "drd_semaphore.h"
37 #include "drd_suppression.h"
38 #include "drd_thread.h"
39 #include "libvex_guest_offsets.h"
40 #include "pub_drd_bitmap.h"
41 #include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
42 #include "pub_tool_basics.h"
43 #include "pub_tool_libcassert.h" // tl_assert()
44 #include "pub_tool_libcbase.h" // VG_(strcmp)
45 #include "pub_tool_libcprint.h" // VG_(printf)
46 #include "pub_tool_libcproc.h" // VG_(getenv)()
47 #include "pub_tool_machine.h"
48 #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
49 #include "pub_tool_options.h" // command line options
50 #include "pub_tool_replacemalloc.h"
51 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
52 #include "pub_tool_tooliface.h"
53 #include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client)
56 /* Local variables. */
58 static Bool s_print_stats;
59 static Bool s_var_info;
60 static Bool s_show_stack_usage;
61 static Bool s_trace_alloc;
62 static Bool trace_sectsuppr;
65 /**
66 * Implement the needs_command_line_options for drd.
68 static Bool DRD_(process_cmd_line_option)(const HChar* arg)
70 int check_stack_accesses = -1;
71 int join_list_vol = -1;
72 int exclusive_threshold_ms = -1;
73 int first_race_only = -1;
74 int report_signal_unlocked = -1;
75 int segment_merging = -1;
76 int segment_merge_interval = -1;
77 int shared_threshold_ms = -1;
78 int show_confl_seg = -1;
79 int trace_barrier = -1;
80 int trace_clientobj = -1;
81 int trace_cond = -1;
82 int trace_csw = -1;
83 int trace_fork_join = -1;
84 int trace_hb = -1;
85 int trace_conflict_set = -1;
86 int trace_conflict_set_bm = -1;
87 int trace_mutex = -1;
88 int trace_rwlock = -1;
89 int trace_segment = -1;
90 int trace_semaphore = -1;
91 int trace_suppression = -1;
92 const HChar* trace_address = 0;
93 const HChar* ptrace_address= 0;
95 if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {}
96 else if VG_INT_CLO (arg, "--join-list-vol", join_list_vol) {}
97 else if VG_BOOL_CLO(arg, "--drd-stats", s_print_stats) {}
98 else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {}
99 else if VG_BOOL_CLO(arg, "--free-is-write", DRD_(g_free_is_write)) {}
100 else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
102 else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {}
103 else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval)
105 else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {}
106 else if VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) {}
107 else if VG_BOOL_CLO(arg, "--trace-alloc", s_trace_alloc) {}
108 else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {}
109 else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {}
110 else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {}
111 else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {}
112 else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){}
113 else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {}
114 else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {}
115 else if VG_BOOL_CLO(arg, "--trace-hb", trace_hb) {}
116 else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {}
117 else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {}
118 else if VG_BOOL_CLO(arg, "--trace-sectsuppr", trace_sectsuppr) {}
119 else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {}
120 else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {}
121 else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {}
122 else if VG_BOOL_CLO(arg, "--var-info", s_var_info) {}
123 else if VG_BOOL_CLO(arg, "--verify-conflict-set", DRD_(verify_conflict_set))
125 else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
126 else if VG_STR_CLO (arg, "--ptrace-addr", ptrace_address) {}
127 else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {}
128 else if VG_STR_CLO (arg, "--trace-addr", trace_address) {}
129 else
130 return VG_(replacement_malloc_process_cmd_line_option)(arg);
132 if (check_stack_accesses != -1)
133 DRD_(set_check_stack_accesses)(check_stack_accesses);
134 if (exclusive_threshold_ms != -1)
136 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
137 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
139 if (first_race_only != -1)
141 DRD_(set_first_race_only)(first_race_only);
143 if (join_list_vol != -1)
144 DRD_(thread_set_join_list_vol)(join_list_vol);
145 if (report_signal_unlocked != -1)
147 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
149 if (shared_threshold_ms != -1)
151 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
153 if (segment_merging != -1)
154 DRD_(thread_set_segment_merging)(segment_merging);
155 if (segment_merge_interval != -1)
156 DRD_(thread_set_segment_merge_interval)(segment_merge_interval);
157 if (show_confl_seg != -1)
158 DRD_(set_show_conflicting_segments)(show_confl_seg);
159 if (trace_address) {
160 const Addr addr = VG_(strtoll16)(trace_address, 0);
161 DRD_(start_tracing_address_range)(addr, addr + 1, False);
163 if (ptrace_address) {
164 char *plus = VG_(strchr)(ptrace_address, '+');
165 Addr addr, length;
166 if (plus)
167 *plus = '\0';
168 addr = VG_(strtoll16)(ptrace_address, 0);
169 length = plus ? VG_(strtoll16)(plus + 1, 0) : 1;
170 DRD_(start_tracing_address_range)(addr, addr + length, True);
172 if (trace_barrier != -1)
173 DRD_(barrier_set_trace)(trace_barrier);
174 if (trace_clientobj != -1)
175 DRD_(clientobj_set_trace)(trace_clientobj);
176 if (trace_cond != -1)
177 DRD_(cond_set_trace)(trace_cond);
178 if (trace_csw != -1)
179 DRD_(thread_trace_context_switches)(trace_csw);
180 if (trace_fork_join != -1)
181 DRD_(thread_set_trace_fork_join)(trace_fork_join);
182 if (trace_hb != -1)
183 DRD_(hb_set_trace)(trace_hb);
184 if (trace_conflict_set != -1)
185 DRD_(thread_trace_conflict_set)(trace_conflict_set);
186 if (trace_conflict_set_bm != -1)
187 DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm);
188 if (trace_mutex != -1)
189 DRD_(mutex_set_trace)(trace_mutex);
190 if (trace_rwlock != -1)
191 DRD_(rwlock_set_trace)(trace_rwlock);
192 if (trace_segment != -1)
193 DRD_(sg_set_trace)(trace_segment);
194 if (trace_semaphore != -1)
195 DRD_(semaphore_set_trace)(trace_semaphore);
196 if (trace_suppression != -1)
197 DRD_(suppression_set_trace)(trace_suppression);
199 return True;
202 static void DRD_(print_usage)(void)
204 VG_(printf)(
205 " --check-stack-var=yes|no Whether or not to report data races on\n"
206 " stack variables [no].\n"
207 " --exclusive-threshold=<n> Print an error message if any mutex or\n"
208 " writer lock is held longer than the specified\n"
209 " time (in milliseconds) [off].\n"
210 " --first-race-only=yes|no Only report the first data race that occurs on\n"
211 " a memory location instead of all races [no].\n"
212 " --free-is-write=yes|no Whether to report races between freeing memory\n"
213 " and subsequent accesses of that memory[no].\n"
214 " --join-list-vol=<n> Number of threads to delay cleanup for [10].\n"
215 " --report-signal-unlocked=yes|no Whether to report calls to\n"
216 " pthread_cond_signal() where the mutex associated\n"
217 " with the signal via pthread_cond_wait() is not\n"
218 " locked at the time the signal is sent [yes].\n"
219 " --segment-merging=yes|no Controls segment merging [yes].\n"
220 " Segment merging is an algorithm to limit memory usage of the\n"
221 " data race detection algorithm. Disabling segment merging may\n"
222 " improve the accuracy of the so-called 'other segments' displayed\n"
223 " in race reports but can also trigger an out of memory error.\n"
224 " --segment-merging-interval=<n> Perform segment merging every time n new\n"
225 " segments have been created. Default: %d.\n"
226 " --shared-threshold=<n> Print an error message if a reader lock\n"
227 " is held longer than the specified time (in\n"
228 " milliseconds) [off]\n"
229 " --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
230 " --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
231 "\n"
232 " drd options for monitoring process behavior:\n"
233 " --ptrace-addr=<address>[+<length>] Trace all load and store activity for\n"
234 " the specified address range and keep doing that\n"
235 " even after the memory at that address has been\n"
236 " freed and reallocated [off].\n"
237 " --trace-addr=<address> Trace all load and store activity for the\n"
238 " specified address [off].\n"
239 " --trace-alloc=yes|no Trace all memory allocations and deallocations\n"
240 " [no].\n"
241 " --trace-barrier=yes|no Trace all barrier activity [no].\n"
242 " --trace-cond=yes|no Trace all condition variable activity [no].\n"
243 " --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
244 " --trace-hb=yes|no Trace ANNOTATE_HAPPENS_BEFORE() etc. [no].\n"
245 " --trace-mutex=yes|no Trace all mutex activity [no].\n"
246 " --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
247 " --trace-semaphore=yes|no Trace all semaphore activity [no].\n",
248 DRD_(thread_get_segment_merge_interval)()
252 static void DRD_(print_debug_usage)(void)
254 VG_(printf)(
255 " --drd-stats=yes|no Print statistics about DRD activity [no].\n"
256 " --trace-clientobj=yes|no Trace all client object activity [no].\n"
257 " --trace-csw=yes|no Trace all scheduler context switches [no].\n"
258 " --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
259 " --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n"
260 " updates [no]. Note: enabling this option\n"
261 " will generate a lot of output !\n"
262 " --trace-sectsuppr=yes|no Trace which the dynamic library sections on\n"
263 " which data race detection is suppressed.\n"
264 " --trace-segment=yes|no Trace segment actions [no].\n"
265 " --trace-suppr=yes|no Trace all address suppression actions [no].\n"
266 " --verify-conflict-set=yes|no Verify conflict set consistency [no].\n"
272 // Implements the thread-related core callbacks.
275 static void drd_pre_mem_read(const CorePart part,
276 const ThreadId tid,
277 const HChar* const s,
278 const Addr a,
279 const SizeT size)
281 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
282 if (size > 0)
284 DRD_(trace_load)(a, size);
288 static void drd_pre_mem_read_asciiz(const CorePart part,
289 const ThreadId tid,
290 const HChar* const s,
291 const Addr a)
293 const HChar* p = (void*)a;
294 SizeT size = 0;
296 // Don't segfault if the string starts in an obviously stupid
297 // place. Actually we should check the whole string, not just
298 // the start address, but that's too much trouble. At least
299 // checking the first byte is better than nothing. See #255009.
300 if (!VG_(am_is_valid_for_client) (a, 1, VKI_PROT_READ))
301 return;
303 /* Note: the expression '*p' reads client memory and may crash if the */
304 /* client provided an invalid pointer ! */
305 while (*p)
307 p++;
308 size++;
310 if (size > 0)
312 DRD_(trace_load)(a, size);
316 static void drd_post_mem_write(const CorePart part,
317 const ThreadId tid,
318 const Addr a,
319 const SizeT size)
321 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
322 if (size > 0)
324 DRD_(trace_store)(a, size);
328 static __inline__
329 void drd_start_using_mem(const Addr a1, const SizeT len,
330 const Bool is_stack_mem)
332 const Addr a2 = a1 + len;
334 tl_assert(a1 <= a2);
336 if (!is_stack_mem && s_trace_alloc)
337 DRD_(trace_msg)("Started using memory range 0x%lx + %ld%s",
338 a1, len, DRD_(running_thread_inside_pthread_create)()
339 ? " (inside pthread_create())" : "");
341 if (!is_stack_mem && DRD_(g_free_is_write))
342 DRD_(thread_stop_using_mem)(a1, a2);
344 if (UNLIKELY(DRD_(any_address_is_traced)()))
346 DRD_(trace_mem_access)(a1, len, eStart, 0, 0);
349 if (UNLIKELY(DRD_(running_thread_inside_pthread_create)()))
351 DRD_(start_suppression)(a1, a2, "pthread_create()");
355 static void drd_start_using_mem_w_ecu(const Addr a1,
356 const SizeT len,
357 UInt ec_uniq)
359 drd_start_using_mem(a1, len, False);
362 static void drd_start_using_mem_w_tid(const Addr a1,
363 const SizeT len,
364 ThreadId tid)
366 drd_start_using_mem(a1, len, False);
369 static __inline__
370 void drd_stop_using_mem(const Addr a1, const SizeT len,
371 const Bool is_stack_mem)
373 const Addr a2 = a1 + len;
375 tl_assert(a1 <= a2);
377 if (UNLIKELY(DRD_(any_address_is_traced)()))
378 DRD_(trace_mem_access)(a1, len, eEnd, 0, 0);
380 if (!is_stack_mem && s_trace_alloc)
381 DRD_(trace_msg)("Stopped using memory range 0x%lx + %ld",
382 a1, len);
384 if (!is_stack_mem || DRD_(get_check_stack_accesses)())
386 if (is_stack_mem || !DRD_(g_free_is_write))
387 DRD_(thread_stop_using_mem)(a1, a2);
388 else if (DRD_(g_free_is_write))
389 DRD_(trace_store)(a1, len);
390 DRD_(clientobj_stop_using_mem)(a1, a2);
391 DRD_(suppression_stop_using_mem)(a1, a2);
395 static __inline__
396 void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
398 drd_stop_using_mem(a1, len, False);
402 * Discard all information DRD has about memory accesses and client objects
403 * in the specified address range.
405 void DRD_(clean_memory)(const Addr a1, const SizeT len)
407 const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
408 drd_stop_using_mem(a1, len, is_stack_memory);
409 drd_start_using_mem(a1, len, is_stack_memory);
413 * Suppress data race reports on all addresses contained in .plt, .got and
414 * .got.plt sections inside the address range [ a, a + len [. The data in
415 * these sections is modified by _dl_relocate_object() every time a function
416 * in a shared library is called for the first time. Since the first call
417 * to a function in a shared library can happen from a multithreaded context,
418 * such calls can cause conflicting accesses. See also Ulrich Drepper's
419 * paper "How to Write Shared Libraries" for more information about relocation
420 * (http://people.redhat.com/drepper/dsohowto.pdf).
421 * Note: the contents of the .got section is only modified by the MIPS resolver.
423 static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
425 const DebugInfo* di;
427 if (trace_sectsuppr)
428 VG_(dmsg)("Evaluating range @ 0x%lx size %ld\n", a, len);
430 for (di = VG_(next_DebugInfo)(0); di; di = VG_(next_DebugInfo)(di)) {
431 Addr avma;
432 SizeT size;
434 if (trace_sectsuppr)
435 VG_(dmsg)("Examining %s / %s\n", VG_(DebugInfo_get_filename)(di),
436 VG_(DebugInfo_get_soname)(di));
439 * Suppress the race report on the libpthread global variable
440 * __pthread_multiple_threads. See also
441 * http://bugs.kde.org/show_bug.cgi?id=323905.
443 avma = VG_(DebugInfo_get_bss_avma)(di);
444 size = VG_(DebugInfo_get_bss_size)(di);
445 tl_assert((avma && size) || (avma == 0 && size == 0));
446 if (size > 0 &&
447 VG_(strcmp)(VG_(DebugInfo_get_soname)(di), "libpthread.so.0") == 0) {
448 if (trace_sectsuppr)
449 VG_(dmsg)("Suppressing .bss @ 0x%lx size %ld\n", avma, size);
450 tl_assert(VG_(DebugInfo_sect_kind)(NULL, avma) == Vg_SectBSS);
451 DRD_(start_suppression)(avma, avma + size, ".bss");
454 avma = VG_(DebugInfo_get_plt_avma)(di);
455 size = VG_(DebugInfo_get_plt_size)(di);
456 tl_assert((avma && size) || (avma == 0 && size == 0));
457 if (size > 0) {
458 if (trace_sectsuppr)
459 VG_(dmsg)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
460 tl_assert(VG_(DebugInfo_sect_kind)(NULL, avma) == Vg_SectPLT);
461 DRD_(start_suppression)(avma, avma + size, ".plt");
464 avma = VG_(DebugInfo_get_gotplt_avma)(di);
465 size = VG_(DebugInfo_get_gotplt_size)(di);
466 tl_assert((avma && size) || (avma == 0 && size == 0));
467 if (size > 0) {
468 if (trace_sectsuppr)
469 VG_(dmsg)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
470 tl_assert(VG_(DebugInfo_sect_kind)(NULL, avma) == Vg_SectGOTPLT);
471 DRD_(start_suppression)(avma, avma + size, ".gotplt");
474 avma = VG_(DebugInfo_get_got_avma)(di);
475 size = VG_(DebugInfo_get_got_size)(di);
476 tl_assert((avma && size) || (avma == 0 && size == 0));
477 if (size > 0) {
478 if (trace_sectsuppr)
479 VG_(dmsg)("Suppressing .got @ 0x%lx size %ld\n", avma, size);
480 tl_assert(VG_(DebugInfo_sect_kind)(NULL, avma) == Vg_SectGOT);
481 DRD_(start_suppression)(avma, avma + size, ".got");
486 static
487 void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
488 const Bool rr, const Bool ww, const Bool xx,
489 ULong di_handle)
491 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
493 drd_start_using_mem(a, len, False);
495 DRD_(suppress_relocation_conflicts)(a, len);
499 * Called by the core when the stack of a thread grows, to indicate that
500 * the addresses in range [ a, a + len [ may now be used by the client.
501 * Assumption: stacks grow downward.
503 static __inline__
504 void drd_start_using_mem_stack2(const DrdThreadId tid, const Addr a,
505 const SizeT len)
507 DRD_(thread_set_stack_min)(tid, a - VG_STACK_REDZONE_SZB);
508 drd_start_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
509 True);
512 static __inline__
513 void drd_start_using_mem_stack(const Addr a, const SizeT len)
515 drd_start_using_mem_stack2(DRD_(thread_get_running_tid)(), a, len);
519 * Called by the core when the stack of a thread shrinks, to indicate that
520 * the addresses [ a, a + len [ are no longer accessible for the client.
521 * Assumption: stacks grow downward.
523 static __inline__
524 void drd_stop_using_mem_stack2(const DrdThreadId tid, const Addr a,
525 const SizeT len)
527 DRD_(thread_set_stack_min)(tid, a + len - VG_STACK_REDZONE_SZB);
528 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
529 True);
532 static __inline__
533 void drd_stop_using_mem_stack(const Addr a, const SizeT len)
535 drd_stop_using_mem_stack2(DRD_(thread_get_running_tid)(), a, len);
538 static
539 Bool on_alt_stack(const Addr a)
541 ThreadId vg_tid;
542 Addr alt_min;
543 SizeT alt_size;
545 vg_tid = VG_(get_running_tid)();
546 alt_min = VG_(thread_get_altstack_min)(vg_tid);
547 alt_size = VG_(thread_get_altstack_size)(vg_tid);
548 return (SizeT)(a - alt_min) < alt_size;
551 static
552 void drd_start_using_mem_alt_stack(const Addr a, const SizeT len)
554 if (!on_alt_stack(a))
555 drd_start_using_mem_stack(a, len);
558 static
559 void drd_stop_using_mem_alt_stack(const Addr a, const SizeT len)
561 if (!on_alt_stack(a))
562 drd_stop_using_mem_stack(a, len);
566 * Callback function invoked by the Valgrind core before a signal is delivered.
568 static
569 void drd_pre_deliver_signal(const ThreadId vg_tid, const Int sigNo,
570 const Bool alt_stack)
572 DrdThreadId drd_tid;
574 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
575 DRD_(thread_set_on_alt_stack)(drd_tid, alt_stack);
576 if (alt_stack)
579 * As soon a signal handler has been invoked on the alternate stack,
580 * switch to stack memory handling functions that can handle the
581 * alternate stack.
583 VG_(track_new_mem_stack)(drd_start_using_mem_alt_stack);
584 VG_(track_die_mem_stack)(drd_stop_using_mem_alt_stack);
589 * Callback function invoked by the Valgrind core after a signal is delivered,
590 * at least if the signal handler did not longjmp().
592 static
593 void drd_post_deliver_signal(const ThreadId vg_tid, const Int sigNo)
595 DrdThreadId drd_tid;
597 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
598 DRD_(thread_set_on_alt_stack)(drd_tid, False);
599 if (DRD_(thread_get_threads_on_alt_stack)() == 0)
601 VG_(track_new_mem_stack)(drd_start_using_mem_stack);
602 VG_(track_die_mem_stack)(drd_stop_using_mem_stack);
607 * Callback function called by the Valgrind core before a stack area is
608 * being used by a signal handler.
610 * @param[in] a Start of address range - VG_STACK_REDZONE_SZB.
611 * @param[in] len Address range length + VG_STACK_REDZONE_SZB.
612 * @param[in] tid Valgrind thread ID for whom the signal frame is being
613 * constructed.
615 static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
616 ThreadId tid)
618 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
619 drd_start_using_mem(a + VG_STACK_REDZONE_SZB, len - VG_STACK_REDZONE_SZB,
620 True);
623 static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
625 drd_stop_using_mem(a + VG_STACK_REDZONE_SZB, len - VG_STACK_REDZONE_SZB,
626 True);
629 static
630 void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
632 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
633 tl_assert(created != VG_INVALID_THREADID);
634 DRD_(thread_pre_create)(drd_creator, created);
635 if (DRD_(IsValidDrdThreadId)(drd_creator))
637 DRD_(thread_new_segment)(drd_creator);
639 if (DRD_(thread_get_trace_fork_join)())
641 DRD_(trace_msg)("drd_pre_thread_create creator = %d, created = %d",
642 drd_creator, created);
647 * Called by Valgrind's core before any loads or stores are performed on
648 * the context of thread "created".
650 static
651 void drd_post_thread_create(const ThreadId vg_created)
653 DrdThreadId drd_created;
654 Addr stack_max;
656 tl_assert(vg_created != VG_INVALID_THREADID);
658 drd_created = DRD_(thread_post_create)(vg_created);
660 /* Set up red zone before the code in glibc's clone.S is run. */
661 stack_max = DRD_(thread_get_stack_max)(drd_created);
662 drd_start_using_mem_stack2(drd_created, stack_max, 0);
664 if (DRD_(thread_get_trace_fork_join)())
666 DRD_(trace_msg)("drd_post_thread_create created = %d", drd_created);
668 if (! DRD_(get_check_stack_accesses)())
670 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
671 - DRD_(thread_get_stack_size)(drd_created),
672 DRD_(thread_get_stack_max)(drd_created),
673 "stack");
677 /* Called after a thread has performed its last memory access. */
678 static void drd_thread_finished(ThreadId vg_tid)
680 DrdThreadId drd_tid;
683 * Ignore if invoked because thread creation failed. See e.g.
684 * coregrind/m_syswrap/syswrap-amd64-linux.c
686 if (VG_(get_running_tid)() != vg_tid)
687 return;
689 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
690 tl_assert(drd_tid != DRD_INVALID_THREADID);
691 if (DRD_(thread_get_trace_fork_join)())
693 DRD_(trace_msg)("drd_thread_finished tid = %d%s", drd_tid,
694 DRD_(thread_get_joinable)(drd_tid)
695 ? "" : " (which is a detached thread)");
697 if (s_show_stack_usage && !VG_(clo_xml)) {
698 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
699 const SizeT used_stack
700 = (DRD_(thread_get_stack_max)(drd_tid)
701 - DRD_(thread_get_stack_min_min)(drd_tid));
702 VG_(message)(Vg_UserMsg,
703 "thread %d%s finished and used %ld bytes out of %ld"
704 " on its stack. Margin: %ld bytes.\n",
705 drd_tid,
706 DRD_(thread_get_joinable)(drd_tid)
707 ? "" : " (which is a detached thread)",
708 used_stack, stack_size, stack_size - used_stack);
711 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
712 DRD_(thread_get_stack_max)(drd_tid)
713 - DRD_(thread_get_stack_min)(drd_tid),
714 True);
715 DRD_(thread_set_record_loads)(drd_tid, False);
716 DRD_(thread_set_record_stores)(drd_tid, False);
717 DRD_(thread_finished)(drd_tid);
721 * Called immediately after fork for the child process only. 'tid' is the
722 * only surviving thread in the child process. Cleans up thread state.
723 * See also http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html for a detailed discussion of using fork() in combination with mutexes.
725 static
726 void drd__atfork_child(ThreadId tid)
728 DRD_(drd_thread_atfork_child)(tid);
733 // Implementation of the tool interface.
736 static void DRD_(post_clo_init)(void)
738 #if defined(VGO_linux) || defined(VGO_darwin)
739 /* fine */
740 #else
741 VG_(printf)("\nWARNING: DRD has not yet been tested on this operating system.\n\n");
742 # endif
744 if (s_var_info)
746 VG_(needs_var_info)();
750 static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
752 tl_assert(tid == VG_(get_running_tid)());
753 DRD_(thread_set_vg_running_tid)(tid);
756 static void DRD_(fini)(Int exitcode)
758 // thread_print_all();
759 if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) {
760 VG_(message)(Vg_UserMsg, "For counts of detected and suppressed errors, "
761 "rerun with: -v\n");
764 if ((VG_(clo_stats) || s_print_stats) && !VG_(clo_xml))
766 ULong pu = DRD_(thread_get_update_conflict_set_count)();
767 ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)();
768 ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)();
769 ULong pu_join = DRD_(thread_get_update_conflict_set_join_count)();
771 VG_(message)(Vg_UserMsg,
772 " thread: %lld context switches.\n",
773 DRD_(thread_get_context_switch_count)());
774 VG_(message)(Vg_UserMsg,
775 "confl set: %lld full updates and %lld partial updates;\n",
776 DRD_(thread_get_compute_conflict_set_count)(),
777 pu);
778 VG_(message)(Vg_UserMsg,
779 " %lld partial updates during segment creation,\n",
780 pu_seg_cr);
781 VG_(message)(Vg_UserMsg,
782 " %lld because of mutex/sema/cond.var. operations,\n",
783 pu_mtx_cv);
784 VG_(message)(Vg_UserMsg,
785 " %lld because of barrier/rwlock operations and\n",
786 pu - pu_seg_cr - pu_mtx_cv - pu_join);
787 VG_(message)(Vg_UserMsg,
788 " %lld partial updates because of thread join"
789 " operations.\n",
790 pu_join);
791 VG_(message)(Vg_UserMsg,
792 " segments: created %lld segments, max %lld alive,\n",
793 DRD_(sg_get_segments_created_count)(),
794 DRD_(sg_get_max_segments_alive_count)());
795 VG_(message)(Vg_UserMsg,
796 " %lld discard points and %lld merges.\n",
797 DRD_(thread_get_discard_ordered_segments_count)(),
798 DRD_(sg_get_segment_merge_count)());
799 VG_(message)(Vg_UserMsg,
800 "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and"
801 " %lld barrier.\n",
802 DRD_(get_mutex_segment_creation_count)(),
803 DRD_(get_rwlock_segment_creation_count)(),
804 DRD_(get_semaphore_segment_creation_count)(),
805 DRD_(get_barrier_segment_creation_count)());
806 VG_(message)(Vg_UserMsg,
807 " bitmaps: %lld level one"
808 " and %lld level two bitmaps were allocated.\n",
809 DRD_(bm_get_bitmap_creation_count)(),
810 DRD_(bm_get_bitmap2_creation_count)());
811 VG_(message)(Vg_UserMsg,
812 " mutex: %lld non-recursive lock/unlock events.\n",
813 DRD_(get_mutex_lock_count)());
814 DRD_(print_malloc_stats)();
817 DRD_(bm_module_cleanup)();
820 static
821 void drd_pre_clo_init(void)
823 // Basic tool stuff.
824 VG_(details_name) ("drd");
825 VG_(details_version) (NULL);
826 VG_(details_description) ("a thread error detector");
827 VG_(details_copyright_author)("Copyright (C) 2006-2013, and GNU GPL'd,"
828 " by Bart Van Assche.");
829 VG_(details_bug_reports_to) (VG_BUGS_TO);
831 VG_(basic_tool_funcs) (DRD_(post_clo_init),
832 DRD_(instrument),
833 DRD_(fini));
835 // Command line stuff.
836 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
837 DRD_(print_usage),
838 DRD_(print_debug_usage));
839 VG_(needs_xml_output) ();
841 // Error handling.
842 DRD_(register_error_handlers)();
844 // Core event tracking.
845 VG_(track_pre_mem_read) (drd_pre_mem_read);
846 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
847 VG_(track_post_mem_write) (drd_post_mem_write);
848 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
849 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
850 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
851 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
852 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
853 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
854 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
855 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
856 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
857 VG_(track_pre_deliver_signal) (drd_pre_deliver_signal);
858 VG_(track_post_deliver_signal) (drd_post_deliver_signal);
859 VG_(track_start_client_code) (drd_start_client_code);
860 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
861 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
862 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
863 VG_(atfork) (NULL/*pre*/, NULL/*parent*/,
864 drd__atfork_child/*child*/);
866 // Other stuff.
867 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
868 drd_stop_using_nonstack_mem);
870 DRD_(bm_module_init)();
872 DRD_(clientreq_init)();
874 DRD_(suppression_init)();
876 DRD_(clientobj_init)();
878 DRD_(thread_init)();
881 HChar* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL");
882 if (smi)
883 DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL));
886 if (VG_(getenv)("DRD_VERIFY_CONFLICT_SET"))
887 DRD_(verify_conflict_set) = True;
892 VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)