1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 /*
3 This file is part of drd, a thread error detector.
4
5 Copyright (C) 2006-2010 Bart Van Assche <bvanassche@acm.org>.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23 */
24
25
26 #include "drd_barrier.h"
27 #include "drd_clientobj.h"
28 #include "drd_clientreq.h"
29 #include "drd_cond.h"
30 #include "drd_error.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_debuginfo.h" // VG_(describe_IP)()
44 #include "pub_tool_libcassert.h" // tl_assert()
45 #include "pub_tool_libcbase.h" // VG_(strcmp)
46 #include "pub_tool_libcprint.h" // VG_(printf)
47 #include "pub_tool_libcproc.h"
48 #include "pub_tool_machine.h"
49 #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
50 #include "pub_tool_options.h" // command line options
51 #include "pub_tool_replacemalloc.h"
52 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
53 #include "pub_tool_tooliface.h"
54 #include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client)
55
56
57 /* Local variables. */
58
59 static Bool s_free_is_write = False;
60 static Bool s_print_stats = False;
61 static Bool s_var_info = False;
62 static Bool s_show_stack_usage = False;
63 static Bool s_trace_alloc = False;
64
65
66 /**
67 * Implement the needs_command_line_options for drd.
68 */
DRD_(process_cmd_line_option)69 static Bool DRD_(process_cmd_line_option)(Char* arg)
70 {
71 int check_stack_accesses = -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_conflict_set = -1;
85 int trace_conflict_set_bm = -1;
86 int trace_mutex = -1;
87 int trace_rwlock = -1;
88 int trace_segment = -1;
89 int trace_semaphore = -1;
90 int trace_suppression = -1;
91 Char* trace_address = 0;
92
93 if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {}
94 else if VG_BOOL_CLO(arg, "--drd-stats", s_print_stats) {}
95 else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {}
96 else if VG_BOOL_CLO(arg, "--free-is-write", s_free_is_write) {}
97 else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
98 {}
99 else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {}
100 else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval)
101 {}
102 else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {}
103 else if VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) {}
104 else if VG_BOOL_CLO(arg, "--trace-alloc", s_trace_alloc) {}
105 else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {}
106 else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {}
107 else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {}
108 else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {}
109 else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){}
110 else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {}
111 else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {}
112 else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {}
113 else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {}
114 else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {}
115 else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {}
116 else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {}
117 else if VG_BOOL_CLO(arg, "--var-info", s_var_info) {}
118 else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
119 else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {}
120 else if VG_STR_CLO (arg, "--trace-addr", trace_address) {}
121 else
122 return VG_(replacement_malloc_process_cmd_line_option)(arg);
123
124 if (check_stack_accesses != -1)
125 DRD_(set_check_stack_accesses)(check_stack_accesses);
126 if (exclusive_threshold_ms != -1)
127 {
128 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
129 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
130 }
131 if (first_race_only != -1)
132 {
133 DRD_(set_first_race_only)(first_race_only);
134 }
135 if (report_signal_unlocked != -1)
136 {
137 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
138 }
139 if (shared_threshold_ms != -1)
140 {
141 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
142 }
143 if (segment_merging != -1)
144 DRD_(thread_set_segment_merging)(segment_merging);
145 if (segment_merge_interval != -1)
146 DRD_(thread_set_segment_merge_interval)(segment_merge_interval);
147 if (show_confl_seg != -1)
148 DRD_(set_show_conflicting_segments)(show_confl_seg);
149 if (trace_address)
150 {
151 const Addr addr = VG_(strtoll16)(trace_address, 0);
152 DRD_(start_tracing_address_range)(addr, addr + 1);
153 }
154 if (trace_barrier != -1)
155 DRD_(barrier_set_trace)(trace_barrier);
156 if (trace_clientobj != -1)
157 DRD_(clientobj_set_trace)(trace_clientobj);
158 if (trace_cond != -1)
159 DRD_(cond_set_trace)(trace_cond);
160 if (trace_csw != -1)
161 DRD_(thread_trace_context_switches)(trace_csw);
162 if (trace_fork_join != -1)
163 DRD_(thread_set_trace_fork_join)(trace_fork_join);
164 if (trace_conflict_set != -1)
165 DRD_(thread_trace_conflict_set)(trace_conflict_set);
166 if (trace_conflict_set_bm != -1)
167 DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm);
168 if (trace_mutex != -1)
169 DRD_(mutex_set_trace)(trace_mutex);
170 if (trace_rwlock != -1)
171 DRD_(rwlock_set_trace)(trace_rwlock);
172 if (trace_segment != -1)
173 DRD_(sg_set_trace)(trace_segment);
174 if (trace_semaphore != -1)
175 DRD_(semaphore_set_trace)(trace_semaphore);
176 if (trace_suppression != -1)
177 DRD_(suppression_set_trace)(trace_suppression);
178
179 return True;
180 }
181
DRD_(print_usage)182 static void DRD_(print_usage)(void)
183 {
184 VG_(printf)(
185 " --check-stack-var=yes|no Whether or not to report data races on\n"
186 " stack variables [no].\n"
187 " --exclusive-threshold=<n> Print an error message if any mutex or\n"
188 " writer lock is held longer than the specified time (in milliseconds).\n"
189 " --first-race-only=yes|no Only report the first data race that occurs on\n"
190 " a memory location instead of all races [no].\n"
191 " --free-is-write=yes|no Whether to report races between freeing memory\n"
192 " and subsequent accesses of that memory[no].\n"
193 " --report-signal-unlocked=yes|no Whether to report calls to\n"
194 " pthread_cond_signal() where the mutex associated\n"
195 " with the signal via pthread_cond_wait() is not\n"
196 " locked at the time the signal is sent [yes].\n"
197 " --segment-merging=yes|no Controls segment merging [yes].\n"
198 " Segment merging is an algorithm to limit memory usage of the\n"
199 " data race detection algorithm. Disabling segment merging may\n"
200 " improve the accuracy of the so-called 'other segments' displayed\n"
201 " in race reports but can also trigger an out of memory error.\n"
202 " --segment-merging-interval=<n> Perform segment merging every time n new\n"
203 " segments have been created. Default: %d.\n"
204 " --shared-threshold=<n> Print an error message if a reader lock\n"
205 " is held longer than the specified time (in milliseconds).\n"
206 " --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
207 " --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
208 "\n"
209 " drd options for monitoring process behavior:\n"
210 " --trace-addr=<address> Trace all load and store activity for the.\n"
211 " specified address [off].\n"
212 " --trace-alloc=yes|no Trace all memory allocations and deallocations\n"" [no].\n"
213 " --trace-barrier=yes|no Trace all barrier activity [no].\n"
214 " --trace-cond=yes|no Trace all condition variable activity [no].\n"
215 " --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
216 " --trace-mutex=yes|no Trace all mutex activity [no].\n"
217 " --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
218 " --trace-semaphore=yes|no Trace all semaphore activity [no].\n",
219 DRD_(thread_get_segment_merge_interval)()
220 );
221 }
222
DRD_(print_debug_usage)223 static void DRD_(print_debug_usage)(void)
224 {
225 VG_(printf)(
226 " --drd-stats=yes|no Print statistics about DRD activity [no].\n"
227 " --trace-clientobj=yes|no Trace all client object activity [no].\n"
228 " --trace-csw=yes|no Trace all scheduler context switches [no].\n"
229 " --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
230 " --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n"
231 " updates [no]. Note: enabling this option\n"
232 " will generate a lot of output !\n"
233 " --trace-segment=yes|no Trace segment actions [no].\n"
234 " --trace-suppr=yes|no Trace all address suppression actions [no].\n"
235 );
236 }
237
238
239 //
240 // Implements the thread-related core callbacks.
241 //
242
drd_pre_mem_read(const CorePart part,const ThreadId tid,Char * const s,const Addr a,const SizeT size)243 static void drd_pre_mem_read(const CorePart part,
244 const ThreadId tid,
245 Char* const s,
246 const Addr a,
247 const SizeT size)
248 {
249 if (size > 0)
250 {
251 DRD_(trace_load)(a, size);
252 }
253 }
254
drd_pre_mem_read_asciiz(const CorePart part,const ThreadId tid,Char * const s,const Addr a)255 static void drd_pre_mem_read_asciiz(const CorePart part,
256 const ThreadId tid,
257 Char* const s,
258 const Addr a)
259 {
260 const char* p = (void*)a;
261 SizeT size = 0;
262
263 // Don't segfault if the string starts in an obviously stupid
264 // place. Actually we should check the whole string, not just
265 // the start address, but that's too much trouble. At least
266 // checking the first byte is better than nothing. See #255009.
267 if (!VG_(am_is_valid_for_client) (a, 1, VKI_PROT_READ))
268 return;
269
270 /* Note: the expression '*p' reads client memory and may crash if the */
271 /* client provided an invalid pointer ! */
272 while (*p)
273 {
274 p++;
275 size++;
276 }
277 if (size > 0)
278 {
279 DRD_(trace_load)(a, size);
280 }
281 }
282
drd_post_mem_write(const CorePart part,const ThreadId tid,const Addr a,const SizeT size)283 static void drd_post_mem_write(const CorePart part,
284 const ThreadId tid,
285 const Addr a,
286 const SizeT size)
287 {
288 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
289 if (size > 0)
290 {
291 DRD_(trace_store)(a, size);
292 }
293 }
294
295 static __inline__
drd_start_using_mem(const Addr a1,const SizeT len,const Bool is_stack_mem)296 void drd_start_using_mem(const Addr a1, const SizeT len,
297 const Bool is_stack_mem)
298 {
299 tl_assert(a1 <= a1 + len);
300
301 if (!is_stack_mem && s_trace_alloc)
302 VG_(message)(Vg_UserMsg, "Started using memory range 0x%lx + %ld%s\n",
303 a1, len, DRD_(running_thread_inside_pthread_create)()
304 ? " (inside pthread_create())" : "");
305
306 if (UNLIKELY(DRD_(any_address_is_traced)()))
307 {
308 DRD_(trace_mem_access)(a1, len, eStart);
309 }
310
311 if (UNLIKELY(DRD_(running_thread_inside_pthread_create)()))
312 {
313 DRD_(start_suppression)(a1, a1 + len, "pthread_create()");
314 }
315 }
316
drd_start_using_mem_w_ecu(const Addr a1,const SizeT len,UInt ec_uniq)317 static void drd_start_using_mem_w_ecu(const Addr a1,
318 const SizeT len,
319 UInt ec_uniq)
320 {
321 drd_start_using_mem(a1, len, False);
322 }
323
drd_start_using_mem_w_tid(const Addr a1,const SizeT len,ThreadId tid)324 static void drd_start_using_mem_w_tid(const Addr a1,
325 const SizeT len,
326 ThreadId tid)
327 {
328 drd_start_using_mem(a1, len, False);
329 }
330
331 static __inline__
drd_stop_using_mem(const Addr a1,const SizeT len,const Bool is_stack_mem)332 void drd_stop_using_mem(const Addr a1, const SizeT len,
333 const Bool is_stack_mem)
334 {
335 const Addr a2 = a1 + len;
336
337 tl_assert(a1 <= a2);
338
339 if (UNLIKELY(DRD_(any_address_is_traced)()))
340 DRD_(trace_mem_access)(a1, len, eEnd);
341
342 if (!is_stack_mem && s_trace_alloc)
343 VG_(message)(Vg_UserMsg, "Stopped using memory range 0x%lx + %ld\n",
344 a1, len);
345
346 if (!is_stack_mem || DRD_(get_check_stack_accesses)())
347 {
348 DRD_(thread_stop_using_mem)(a1, a2, !is_stack_mem && s_free_is_write);
349 DRD_(clientobj_stop_using_mem)(a1, a2);
350 DRD_(suppression_stop_using_mem)(a1, a2);
351 }
352 if (!is_stack_mem && s_free_is_write)
353 DRD_(trace_store)(a1, len);
354 }
355
356 static __inline__
drd_stop_using_nonstack_mem(const Addr a1,const SizeT len)357 void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
358 {
359 drd_stop_using_mem(a1, len, False);
360 }
361
362 /**
363 * Discard all information DRD has about memory accesses and client objects
364 * in the specified address range.
365 */
DRD_(clean_memory)366 void DRD_(clean_memory)(const Addr a1, const SizeT len)
367 {
368 const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
369 drd_stop_using_mem(a1, len, is_stack_memory);
370 drd_start_using_mem(a1, len, is_stack_memory);
371 }
372
373 /**
374 * Suppress data race reports on all addresses contained in .plt and
375 * .got.plt sections inside the address range [ a, a + len [. The data in
376 * these sections is modified by _dl_relocate_object() every time a function
377 * in a shared library is called for the first time. Since the first call
378 * to a function in a shared library can happen from a multithreaded context,
379 * such calls can cause conflicting accesses. See also Ulrich Drepper's
380 * paper "How to Write Shared Libraries" for more information about relocation
381 * (http://people.redhat.com/drepper/dsohowto.pdf).
382 */
DRD_(suppress_relocation_conflicts)383 static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
384 {
385 const DebugInfo* di;
386
387 #if 0
388 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
389 #endif
390
391 for (di = VG_(next_DebugInfo)(0); di; di = VG_(next_DebugInfo)(di))
392 {
393 Addr avma;
394 SizeT size;
395
396 avma = VG_(DebugInfo_get_plt_avma)(di);
397 size = VG_(DebugInfo_get_plt_size)(di);
398 tl_assert((avma && size) || (avma == 0 && size == 0));
399 if (size > 0)
400 {
401 #if 0
402 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
403 #endif
404 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
405 DRD_(start_suppression)(avma, avma + size, ".plt");
406 }
407
408 avma = VG_(DebugInfo_get_gotplt_avma)(di);
409 size = VG_(DebugInfo_get_gotplt_size)(di);
410 tl_assert((avma && size) || (avma == 0 && size == 0));
411 if (size > 0)
412 {
413 #if 0
414 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
415 #endif
416 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
417 DRD_(start_suppression)(avma, avma + size, ".gotplt");
418 }
419 }
420 }
421
422 static
drd_start_using_mem_w_perms(const Addr a,const SizeT len,const Bool rr,const Bool ww,const Bool xx,ULong di_handle)423 void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
424 const Bool rr, const Bool ww, const Bool xx,
425 ULong di_handle)
426 {
427 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
428
429 drd_start_using_mem(a, len, False);
430
431 DRD_(suppress_relocation_conflicts)(a, len);
432 }
433
434 /* Called by the core when the stack of a thread grows, to indicate that */
435 /* the addresses in range [ a, a + len [ may now be used by the client. */
436 /* Assumption: stacks grow downward. */
437 static __inline__
drd_start_using_mem_stack(const Addr a,const SizeT len)438 void drd_start_using_mem_stack(const Addr a, const SizeT len)
439 {
440 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
441 a - VG_STACK_REDZONE_SZB);
442 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
443 len + VG_STACK_REDZONE_SZB,
444 True);
445 }
446
447 /* Called by the core when the stack of a thread shrinks, to indicate that */
448 /* the addresses [ a, a + len [ are no longer accessible for the client. */
449 /* Assumption: stacks grow downward. */
450 static __inline__
drd_stop_using_mem_stack(const Addr a,const SizeT len)451 void drd_stop_using_mem_stack(const Addr a, const SizeT len)
452 {
453 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
454 a + len - VG_STACK_REDZONE_SZB);
455 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
456 True);
457 }
458
459 static
on_alt_stack(const Addr a)460 Bool on_alt_stack(const Addr a)
461 {
462 ThreadId vg_tid;
463 Addr alt_min;
464 SizeT alt_size;
465
466 vg_tid = VG_(get_running_tid)();
467 alt_min = VG_(thread_get_altstack_min)(vg_tid);
468 alt_size = VG_(thread_get_altstack_size)(vg_tid);
469 return (SizeT)(a - alt_min) < alt_size;
470 }
471
472 static
drd_start_using_mem_alt_stack(const Addr a,const SizeT len)473 void drd_start_using_mem_alt_stack(const Addr a, const SizeT len)
474 {
475 if (!on_alt_stack(a))
476 drd_start_using_mem_stack(a, len);
477 }
478
479 static
drd_stop_using_mem_alt_stack(const Addr a,const SizeT len)480 void drd_stop_using_mem_alt_stack(const Addr a, const SizeT len)
481 {
482 if (!on_alt_stack(a))
483 drd_stop_using_mem_stack(a, len);
484 }
485
486 /**
487 * Callback function invoked by the Valgrind core before a signal is delivered.
488 */
489 static
drd_pre_deliver_signal(const ThreadId vg_tid,const Int sigNo,const Bool alt_stack)490 void drd_pre_deliver_signal(const ThreadId vg_tid, const Int sigNo,
491 const Bool alt_stack)
492 {
493 DrdThreadId drd_tid;
494
495 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
496 DRD_(thread_set_on_alt_stack)(drd_tid, alt_stack);
497 if (alt_stack)
498 {
499 /*
500 * As soon a signal handler has been invoked on the alternate stack,
501 * switch to stack memory handling functions that can handle the
502 * alternate stack.
503 */
504 VG_(track_new_mem_stack)(drd_start_using_mem_alt_stack);
505 VG_(track_die_mem_stack)(drd_stop_using_mem_alt_stack);
506 }
507 }
508
509 /**
510 * Callback function invoked by the Valgrind core after a signal is delivered,
511 * at least if the signal handler did not longjmp().
512 */
513 static
drd_post_deliver_signal(const ThreadId vg_tid,const Int sigNo)514 void drd_post_deliver_signal(const ThreadId vg_tid, const Int sigNo)
515 {
516 DrdThreadId drd_tid;
517
518 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
519 DRD_(thread_set_on_alt_stack)(drd_tid, False);
520 if (DRD_(thread_get_threads_on_alt_stack)() == 0)
521 {
522 VG_(track_new_mem_stack)(drd_start_using_mem_stack);
523 VG_(track_die_mem_stack)(drd_stop_using_mem_stack);
524 }
525 }
526
527 /**
528 * Callback function called by the Valgrind core before a stack area is
529 * being used by a signal handler.
530 *
531 * @param[in] a Start of address range.
532 * @param[in] len Address range length.
533 * @param[in] tid Valgrind thread ID for whom the signal frame is being
534 * constructed.
535 */
drd_start_using_mem_stack_signal(const Addr a,const SizeT len,ThreadId tid)536 static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
537 ThreadId tid)
538 {
539 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
540 drd_start_using_mem(a, len, True);
541 }
542
drd_stop_using_mem_stack_signal(Addr a,SizeT len)543 static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
544 {
545 drd_stop_using_mem(a, len, True);
546 }
547
548 static
drd_pre_thread_create(const ThreadId creator,const ThreadId created)549 void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
550 {
551 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
552 tl_assert(created != VG_INVALID_THREADID);
553 DRD_(thread_pre_create)(drd_creator, created);
554 if (DRD_(IsValidDrdThreadId)(drd_creator))
555 {
556 DRD_(thread_new_segment)(drd_creator);
557 }
558 if (DRD_(thread_get_trace_fork_join)())
559 {
560 VG_(message)(Vg_DebugMsg,
561 "drd_pre_thread_create creator = %d, created = %d\n",
562 drd_creator, created);
563 }
564 }
565
566 /* Called by Valgrind's core before any loads or stores are performed on */
567 /* the context of thread "created". At startup, this function is called */
568 /* with arguments (0,1). */
569 static
drd_post_thread_create(const ThreadId vg_created)570 void drd_post_thread_create(const ThreadId vg_created)
571 {
572 DrdThreadId drd_created;
573
574 tl_assert(vg_created != VG_INVALID_THREADID);
575
576 drd_created = DRD_(thread_post_create)(vg_created);
577 if (DRD_(thread_get_trace_fork_join)())
578 {
579 VG_(message)(Vg_DebugMsg,
580 "drd_post_thread_create created = %d\n",
581 drd_created);
582 }
583 if (! DRD_(get_check_stack_accesses)())
584 {
585 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
586 - DRD_(thread_get_stack_size)(drd_created),
587 DRD_(thread_get_stack_max)(drd_created),
588 "stack");
589 }
590 }
591
592 /* Called after a thread has performed its last memory access. */
drd_thread_finished(ThreadId vg_tid)593 static void drd_thread_finished(ThreadId vg_tid)
594 {
595 DrdThreadId drd_tid;
596
597 tl_assert(VG_(get_running_tid)() == vg_tid);
598
599 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
600 if (DRD_(thread_get_trace_fork_join)())
601 {
602 VG_(message)(Vg_DebugMsg,
603 "drd_thread_finished tid = %d%s\n",
604 drd_tid,
605 DRD_(thread_get_joinable)(drd_tid)
606 ? ""
607 : " (which is a detached thread)");
608 }
609 if (s_show_stack_usage)
610 {
611 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
612 const SizeT used_stack
613 = (DRD_(thread_get_stack_max)(drd_tid)
614 - DRD_(thread_get_stack_min_min)(drd_tid));
615 VG_(message)(Vg_UserMsg,
616 "thread %d%s finished and used %ld bytes out of %ld"
617 " on its stack. Margin: %ld bytes.\n",
618 drd_tid,
619 DRD_(thread_get_joinable)(drd_tid)
620 ? ""
621 : " (which is a detached thread)",
622 used_stack,
623 stack_size,
624 stack_size - used_stack);
625
626 }
627 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
628 DRD_(thread_get_stack_max)(drd_tid)
629 - DRD_(thread_get_stack_min)(drd_tid),
630 True);
631 DRD_(thread_set_record_loads)(drd_tid, False);
632 DRD_(thread_set_record_stores)(drd_tid, False);
633 DRD_(thread_finished)(drd_tid);
634 }
635
636 /*
637 * Called immediately after fork for the child process only. 'tid' is the
638 * only surviving thread in the child process. Cleans up thread state.
639 * See also http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html for a detailed discussion of using fork() in combination with mutexes.
640 */
641 static
drd__atfork_child(ThreadId tid)642 void drd__atfork_child(ThreadId tid)
643 {
644 DRD_(drd_thread_atfork_child)(tid);
645 }
646
647
648 //
649 // Implementation of the tool interface.
650 //
651
DRD_(post_clo_init)652 static void DRD_(post_clo_init)(void)
653 {
654 #if defined(VGO_linux) || defined(VGO_darwin)
655 /* fine */
656 #else
657 VG_(printf)("\nWARNING: DRD has not yet been tested on this operating system.\n\n");
658 # endif
659
660 if (s_var_info)
661 {
662 VG_(needs_var_info)();
663 }
664 }
665
drd_start_client_code(const ThreadId tid,const ULong bbs_done)666 static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
667 {
668 tl_assert(tid == VG_(get_running_tid)());
669 DRD_(thread_set_vg_running_tid)(tid);
670 }
671
DRD_(fini)672 static void DRD_(fini)(Int exitcode)
673 {
674 // thread_print_all();
675 if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) {
676 VG_(message)(Vg_UserMsg,
677 "For counts of detected and suppressed errors, "
678 "rerun with: -v\n");
679 }
680
681 if (VG_(clo_stats) || s_print_stats)
682 {
683 ULong pu = DRD_(thread_get_update_conflict_set_count)();
684 ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)();
685 ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)();
686 ULong pu_join = DRD_(thread_get_update_conflict_set_join_count)();
687
688 VG_(message)(Vg_UserMsg,
689 " thread: %lld context switches.\n",
690 DRD_(thread_get_context_switch_count)());
691 VG_(message)(Vg_UserMsg,
692 "confl set: %lld full updates and %lld partial updates;\n",
693 DRD_(thread_get_compute_conflict_set_count)(),
694 pu);
695 VG_(message)(Vg_UserMsg,
696 " %lld partial updates during segment creation,\n",
697 pu_seg_cr);
698 VG_(message)(Vg_UserMsg,
699 " %lld because of mutex/sema/cond.var. operations,\n",
700 pu_mtx_cv);
701 VG_(message)(Vg_UserMsg,
702 " %lld because of barrier/rwlock operations and\n",
703 pu - pu_seg_cr - pu_mtx_cv - pu_join);
704 VG_(message)(Vg_UserMsg,
705 " %lld partial updates because of thread join"
706 " operations.\n",
707 pu_join);
708 VG_(message)(Vg_UserMsg,
709 " segments: created %lld segments, max %lld alive,\n",
710 DRD_(sg_get_segments_created_count)(),
711 DRD_(sg_get_max_segments_alive_count)());
712 VG_(message)(Vg_UserMsg,
713 " %lld discard points and %lld merges.\n",
714 DRD_(thread_get_discard_ordered_segments_count)(),
715 DRD_(sg_get_segment_merge_count)());
716 VG_(message)(Vg_UserMsg,
717 "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and"
718 " %lld barrier.\n",
719 DRD_(get_mutex_segment_creation_count)(),
720 DRD_(get_rwlock_segment_creation_count)(),
721 DRD_(get_semaphore_segment_creation_count)(),
722 DRD_(get_barrier_segment_creation_count)());
723 VG_(message)(Vg_UserMsg,
724 " bitmaps: %lld level one"
725 " and %lld level two bitmaps were allocated.\n",
726 DRD_(bm_get_bitmap_creation_count)(),
727 DRD_(bm_get_bitmap2_creation_count)());
728 VG_(message)(Vg_UserMsg,
729 " mutex: %lld non-recursive lock/unlock events.\n",
730 DRD_(get_mutex_lock_count)());
731 DRD_(print_malloc_stats)();
732 }
733 }
734
735 static
drd_pre_clo_init(void)736 void drd_pre_clo_init(void)
737 {
738 // Basic tool stuff.
739 VG_(details_name) ("drd");
740 VG_(details_version) (NULL);
741 VG_(details_description) ("a thread error detector");
742 VG_(details_copyright_author)("Copyright (C) 2006-2010, and GNU GPL'd,"
743 " by Bart Van Assche.");
744 VG_(details_bug_reports_to) (VG_BUGS_TO);
745
746 VG_(basic_tool_funcs) (DRD_(post_clo_init),
747 DRD_(instrument),
748 DRD_(fini));
749
750 // Command line stuff.
751 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
752 DRD_(print_usage),
753 DRD_(print_debug_usage));
754
755 // Error handling.
756 DRD_(register_error_handlers)();
757
758 // Core event tracking.
759 VG_(track_pre_mem_read) (drd_pre_mem_read);
760 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
761 VG_(track_post_mem_write) (drd_post_mem_write);
762 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
763 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
764 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
765 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
766 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
767 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
768 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
769 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
770 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
771 VG_(track_pre_deliver_signal) (drd_pre_deliver_signal);
772 VG_(track_post_deliver_signal) (drd_post_deliver_signal);
773 VG_(track_start_client_code) (drd_start_client_code);
774 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
775 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
776 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
777 VG_(atfork) (NULL/*pre*/, NULL/*parent*/,
778 drd__atfork_child/*child*/);
779
780 // Other stuff.
781 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
782 drd_stop_using_nonstack_mem);
783
784 DRD_(clientreq_init)();
785
786 DRD_(suppression_init)();
787
788 DRD_(clientobj_init)();
789
790 {
791 Char* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL");
792 if (smi)
793 DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL));
794 }
795 }
796
797
798 VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)
799