blob: d6f21dc7c99830f9e5ded10dac1509361b26e525 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
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
sewardj85642922008-01-14 11:54:56 +000026#include "drd_barrier.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_clientreq.h"
bart4bb53d82008-02-28 19:06:34 +000028#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "drd_cond.h"
30#include "drd_error.h"
31#include "drd_malloc_wrappers.h"
32#include "drd_mutex.h"
bart5bd9f2d2008-03-03 20:31:58 +000033#include "drd_rwlock.h"
sewardjaf44c822007-11-25 14:01:38 +000034#include "drd_segment.h"
sewardj85642922008-01-14 11:54:56 +000035#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "drd_suppression.h"
37#include "drd_thread.h"
38#include "drd_track.h"
39#include "drd_vc.h"
sewardj721ad7b2007-11-30 08:30:29 +000040#include "priv_drd_clientreq.h"
sewardj85642922008-01-14 11:54:56 +000041#include "pub_drd_bitmap.h"
bart024a95a2008-04-01 18:27:41 +000042#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
sewardjaf44c822007-11-25 14:01:38 +000043#include "pub_tool_basics.h"
44#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
45#include "pub_tool_libcassert.h" // tl_assert()
46#include "pub_tool_libcbase.h" // VG_(strcmp)
47#include "pub_tool_libcprint.h" // VG_(printf)
48#include "pub_tool_libcproc.h"
49#include "pub_tool_machine.h"
bart024a95a2008-04-01 18:27:41 +000050#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardjaf44c822007-11-25 14:01:38 +000051#include "pub_tool_options.h" // command line options
bartceded212008-03-26 17:39:52 +000052#include "pub_tool_replacemalloc.h"
bart72b751c2008-03-01 13:44:24 +000053#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000054#include "pub_tool_tooliface.h"
55
56
bartbf80e122008-06-06 10:18:24 +000057/* Include several source files here in order to allow the compiler to */
58/* do more inlining. */
59#include "drd_bitmap.c"
60#include "drd_segment.c"
61#include "drd_thread.c"
62#include "drd_vc.c"
63
64
65
sewardjaf44c822007-11-25 14:01:38 +000066// Function declarations.
67
68static void drd_start_client_code(const ThreadId tid, const ULong bbs_done);
sewardjaf44c822007-11-25 14:01:38 +000069
70
71// Local variables.
72
bart08865622008-06-06 14:31:36 +000073static Bool s_drd_check_stack_accesses = False;
74static Bool s_drd_print_stats = False;
75static Bool s_drd_trace_fork_join = False;
76static Bool s_drd_var_info = False;
77static Bool s_show_stack_usage = False;
sewardjaf44c822007-11-25 14:01:38 +000078
79
80//
81// Implement the needs_command_line_options for drd.
82//
83
84static Bool drd_process_cmd_line_option(Char* arg)
85{
bart9d5b7962008-05-14 12:25:00 +000086 int exclusive_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000087 int segment_merging = -1;
bart9d5b7962008-05-14 12:25:00 +000088 int shared_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000089 int show_confl_seg = -1;
90 int trace_barrier = -1;
91 int trace_clientobj = -1;
92 int trace_cond = -1;
93 int trace_csw = -1;
94 int trace_danger_set = -1;
95 int trace_mutex = -1;
96 int trace_rwlock = -1;
97 int trace_segment = -1;
98 int trace_semaphore = -1;
99 int trace_suppression = -1;
100 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +0000101
bart08865622008-06-06 14:31:36 +0000102 VG_BOOL_CLO (arg, "--check-stack-var", s_drd_check_stack_accesses)
bart9d5b7962008-05-14 12:25:00 +0000103 else VG_BOOL_CLO(arg, "--drd-stats", s_drd_print_stats)
104 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
105 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
106 else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage)
107 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
108 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
109 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
110 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
111 else VG_BOOL_CLO(arg, "--trace-danger-set", trace_danger_set)
112 else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join)
113 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
114 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
115 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
116 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
117 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
118 else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info)
119 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
120 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
121 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000122 else
bartceded212008-03-26 17:39:52 +0000123 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000124
bart9d5b7962008-05-14 12:25:00 +0000125 if (exclusive_threshold_ms != -1)
126 {
127 mutex_set_lock_threshold(exclusive_threshold_ms);
128 rwlock_set_exclusive_threshold(exclusive_threshold_ms);
129 }
130 if (shared_threshold_ms != -1)
131 {
132 rwlock_set_shared_threshold(shared_threshold_ms);
133 }
barta9c37392008-03-22 09:38:48 +0000134 if (segment_merging != -1)
135 thread_set_segment_merging(segment_merging);
136 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000137 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000138 if (trace_address)
139 {
bart005dc972008-03-29 14:42:59 +0000140 const Addr addr = VG_(strtoll16)(trace_address, 0);
141 drd_start_tracing_address_range(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000142 }
barta9c37392008-03-22 09:38:48 +0000143 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000144 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000145 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000146 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000147 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000148 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000149 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000150 thread_trace_context_switches(trace_csw);
barta9c37392008-03-22 09:38:48 +0000151 if (trace_danger_set != -1)
bart3772a982008-03-15 08:11:03 +0000152 thread_trace_danger_set(trace_danger_set);
barta9c37392008-03-22 09:38:48 +0000153 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000154 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000155 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000156 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000157 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000158 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000159 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000160 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000161 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000162 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000163
bart3772a982008-03-15 08:11:03 +0000164 return True;
sewardjaf44c822007-11-25 14:01:38 +0000165}
166
167static void drd_print_usage(void)
bartbd7e56e2008-03-31 18:14:12 +0000168{
169 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000170" --check-stack-var=yes|no Whether or not to report data races on\n"
171" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000172" --exclusive-threshold=<n> Print an error message if any mutex or\n"
173" writer lock is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000174" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000175" Segment merging is an algorithm to limit memory usage of the\n"
176" data race detection algorithm. Disabling segment merging may\n"
177" improve the accuracy of the so-called 'other segments' displayed\n"
178" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000179" --shared-threshold=<n> Print an error message if a reader lock\n"
180" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000181" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
182" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
183" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000184" stack variables when a race is reported on such a variable. This\n"
185" information is by default not displayed since for big programs\n"
186" reading in all debug information for all variables at once may\n"
187" cause an out of memory error [no].\n"
188"\n"
bart2cb48d62008-04-01 16:57:42 +0000189" exp-drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000190" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000191" specified address [off].\n"
192" --trace-barrier=yes|no Trace all barrier activity [no].\n"
193" --trace-cond=yes|no Trace all condition variable activity [no].\n"
194" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
195" --trace-mutex=yes|no Trace all mutex activity [no].\n"
196" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
197" --trace-segment=yes|no Trace segment actions [no].\n"
198" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000199 );
bart130463a2008-04-01 17:03:33 +0000200 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000201}
202
203static void drd_print_debug_usage(void)
204{
bart130463a2008-04-01 17:03:33 +0000205 VG_(printf)(
206" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
207" --trace-clientobj=yes|no Trace all client object activity [no].\n"
208" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
209" --trace-danger-set=yes|no Trace all danger set updates [no].\n"
210" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
211 );
212 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000213}
214
215
216//
217// Implements the thread-related core callbacks.
218//
219
barta79df6e2008-03-14 17:07:51 +0000220static void drd_trace_mem_access(const Addr addr, const SizeT size,
221 const BmAccessTypeT access_type)
222{
bart3772a982008-03-15 08:11:03 +0000223 char vc[80];
224 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
225 VG_(message)(Vg_UserMsg,
bart354009c2008-03-16 10:42:33 +0000226 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
bartd5765912008-03-16 08:40:55 +0000227 access_type == eLoad
228 ? "load "
229 : access_type == eStore
230 ? "store"
231 : access_type == eStart
232 ? "start"
233 : access_type == eEnd
bartd43f8d32008-03-16 17:29:20 +0000234 ? "end "
bartd5765912008-03-16 08:40:55 +0000235 : "????",
bart3772a982008-03-15 08:11:03 +0000236 addr,
237 size,
bart3772a982008-03-15 08:11:03 +0000238 VG_(get_running_tid)(),
239 thread_get_running_tid(),
240 vc);
241 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
242 VG_(clo_backtrace_size));
243 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
244 == VG_(get_running_tid)());
barta79df6e2008-03-14 17:07:51 +0000245}
246
247static void drd_report_race(const Addr addr, const SizeT size,
248 const BmAccessTypeT access_type)
249{
bart49c3a112008-03-15 10:28:36 +0000250 DataRaceErrInfo drei;
251
bart7a282142008-03-15 08:34:23 +0000252 if (drd_is_suppressed(addr, addr + size))
253 return;
254
bart354009c2008-03-16 10:42:33 +0000255 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000256 drei.addr = addr;
257 drei.size = size;
258 drei.access_type = access_type;
259 VG_(maybe_record_error)(VG_(get_running_tid)(),
260 DataRaceErr,
261 VG_(get_IP)(VG_(get_running_tid)()),
262 "Conflicting accesses",
263 &drei);
barta79df6e2008-03-14 17:07:51 +0000264}
265
266static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000267{
bartf00a85b2008-03-13 18:49:23 +0000268#if 0
bart3772a982008-03-15 08:11:03 +0000269 /* The assert below has been commented out because of performance reasons.*/
270 tl_assert(thread_get_running_tid()
271 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000272#endif
sewardjaf44c822007-11-25 14:01:38 +0000273
bart3772a982008-03-15 08:11:03 +0000274 if (! running_thread_is_recording())
275 return;
bart0268dfa2008-03-11 20:10:21 +0000276
bart005dc972008-03-29 14:42:59 +0000277 if (range_any_is_traced(addr, size))
bart3772a982008-03-15 08:11:03 +0000278 {
279 drd_trace_mem_access(addr, size, eLoad);
280 }
bart08865622008-06-06 14:31:36 +0000281 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
282 && bm_access_load_triggers_conflict(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000283 {
284 drd_report_race(addr, size, eLoad);
285 }
barta79df6e2008-03-14 17:07:51 +0000286}
287
288static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
289{
bart3772a982008-03-15 08:11:03 +0000290 if (! running_thread_is_recording())
291 return;
barta79df6e2008-03-14 17:07:51 +0000292
bart005dc972008-03-29 14:42:59 +0000293 if (range_any_is_traced(addr, 1))
bart3772a982008-03-15 08:11:03 +0000294 {
295 drd_trace_mem_access(addr, 1, eLoad);
296 }
bart08865622008-06-06 14:31:36 +0000297 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
298 && bm_access_load_1_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000299 {
300 drd_report_race(addr, 1, eLoad);
301 }
barta79df6e2008-03-14 17:07:51 +0000302}
303
304static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
305{
bart3772a982008-03-15 08:11:03 +0000306 if (! running_thread_is_recording())
307 return;
barta79df6e2008-03-14 17:07:51 +0000308
bart005dc972008-03-29 14:42:59 +0000309 if (range_any_is_traced(addr, 2))
bart3772a982008-03-15 08:11:03 +0000310 {
311 drd_trace_mem_access(addr, 2, eLoad);
312 }
bart08865622008-06-06 14:31:36 +0000313 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
314 && bm_access_load_2_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000315 {
316 drd_report_race(addr, 2, eLoad);
317 }
barta79df6e2008-03-14 17:07:51 +0000318}
319
320static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
321{
bart3772a982008-03-15 08:11:03 +0000322 if (! running_thread_is_recording())
323 return;
barta79df6e2008-03-14 17:07:51 +0000324
bart005dc972008-03-29 14:42:59 +0000325 if (range_any_is_traced(addr, 4))
bart3772a982008-03-15 08:11:03 +0000326 {
327 drd_trace_mem_access(addr, 4, eLoad);
328 }
bart08865622008-06-06 14:31:36 +0000329 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
330 && bm_access_load_4_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000331 {
332 drd_report_race(addr, 4, eLoad);
333 }
barta79df6e2008-03-14 17:07:51 +0000334}
335
336static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
337{
bart3772a982008-03-15 08:11:03 +0000338 if (! running_thread_is_recording())
339 return;
barta79df6e2008-03-14 17:07:51 +0000340
bart005dc972008-03-29 14:42:59 +0000341 if (range_any_is_traced(addr, 8))
bart3772a982008-03-15 08:11:03 +0000342 {
343 drd_trace_mem_access(addr, 8, eLoad);
344 }
bart08865622008-06-06 14:31:36 +0000345 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
346 && bm_access_load_8_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000347 {
348 drd_report_race(addr, 8, eLoad);
349 }
sewardjaf44c822007-11-25 14:01:38 +0000350}
351
352static
353VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
354{
bartf00a85b2008-03-13 18:49:23 +0000355#if 0
bart3772a982008-03-15 08:11:03 +0000356 /* The assert below has been commented out because of performance reasons.*/
357 tl_assert(thread_get_running_tid()
358 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000359#endif
sewardjaf44c822007-11-25 14:01:38 +0000360
bart3772a982008-03-15 08:11:03 +0000361 if (! running_thread_is_recording())
362 return;
bart0268dfa2008-03-11 20:10:21 +0000363
bart005dc972008-03-29 14:42:59 +0000364 if (range_any_is_traced(addr, size))
bart3772a982008-03-15 08:11:03 +0000365 {
366 drd_trace_mem_access(addr, size, eStore);
367 }
bart08865622008-06-06 14:31:36 +0000368 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
369 && bm_access_store_triggers_conflict(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000370 {
371 drd_report_race(addr, size, eStore);
372 }
barta79df6e2008-03-14 17:07:51 +0000373}
374
375static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
376{
bart3772a982008-03-15 08:11:03 +0000377 if (! running_thread_is_recording())
378 return;
barta79df6e2008-03-14 17:07:51 +0000379
bart005dc972008-03-29 14:42:59 +0000380 if (range_any_is_traced(addr, 1))
bart3772a982008-03-15 08:11:03 +0000381 {
382 drd_trace_mem_access(addr, 1, eStore);
383 }
bart08865622008-06-06 14:31:36 +0000384 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
385 && bm_access_store_1_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000386 {
387 drd_report_race(addr, 1, eStore);
388 }
barta79df6e2008-03-14 17:07:51 +0000389}
390
391static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
392{
bart3772a982008-03-15 08:11:03 +0000393 if (! running_thread_is_recording())
394 return;
barta79df6e2008-03-14 17:07:51 +0000395
bart005dc972008-03-29 14:42:59 +0000396 if (range_any_is_traced(addr, 2))
bart3772a982008-03-15 08:11:03 +0000397 {
398 drd_trace_mem_access(addr, 2, eStore);
399 }
bart08865622008-06-06 14:31:36 +0000400 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
401 && bm_access_store_2_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000402 {
403 drd_report_race(addr, 2, eStore);
404 }
barta79df6e2008-03-14 17:07:51 +0000405}
406
407static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
408{
bart3772a982008-03-15 08:11:03 +0000409 if (! running_thread_is_recording())
410 return;
barta79df6e2008-03-14 17:07:51 +0000411
bart005dc972008-03-29 14:42:59 +0000412 if (range_any_is_traced(addr, 4))
bart3772a982008-03-15 08:11:03 +0000413 {
414 drd_trace_mem_access(addr, 4, eStore);
415 }
bart08865622008-06-06 14:31:36 +0000416 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
417 && bm_access_store_4_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000418 {
419 drd_report_race(addr, 4, eStore);
420 }
barta79df6e2008-03-14 17:07:51 +0000421}
422
423static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
424{
bart3772a982008-03-15 08:11:03 +0000425 if (! running_thread_is_recording())
426 return;
barta79df6e2008-03-14 17:07:51 +0000427
bart005dc972008-03-29 14:42:59 +0000428 if (range_any_is_traced(addr, 8))
bart3772a982008-03-15 08:11:03 +0000429 {
430 drd_trace_mem_access(addr, 8, eStore);
431 }
bart08865622008-06-06 14:31:36 +0000432 if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
433 && bm_access_store_8_triggers_conflict(addr))
bart3772a982008-03-15 08:11:03 +0000434 {
435 drd_report_race(addr, 8, eStore);
436 }
sewardjaf44c822007-11-25 14:01:38 +0000437}
438
439static void drd_pre_mem_read(const CorePart part,
440 const ThreadId tid,
441 Char* const s,
442 const Addr a,
443 const SizeT size)
444{
bart3772a982008-03-15 08:11:03 +0000445 if (size > 0)
446 {
447 drd_trace_load(a, size);
448 }
sewardjaf44c822007-11-25 14:01:38 +0000449}
450
bart5e85d262008-03-01 10:49:37 +0000451static void drd_pre_mem_read_asciiz(const CorePart part,
452 const ThreadId tid,
453 Char* const s,
454 const Addr a)
455{
bart3772a982008-03-15 08:11:03 +0000456 const char* p = (void*)a;
457 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000458
bart3772a982008-03-15 08:11:03 +0000459 /* Note: the expression '*p' reads client memory and may crash if the */
460 /* client provided an invalid pointer ! */
461 while (*p)
462 {
463 p++;
464 size++;
465 }
466 // To do: find out what a reasonable upper limit on 'size' is.
467 tl_assert(size < 4096);
468 if (size > 0)
469 {
470 drd_trace_load(a, size);
471 }
bart5e85d262008-03-01 10:49:37 +0000472}
473
sewardjaf44c822007-11-25 14:01:38 +0000474static void drd_post_mem_write(const CorePart part,
475 const ThreadId tid,
476 const Addr a,
477 const SizeT size)
478{
bart3772a982008-03-15 08:11:03 +0000479 thread_set_vg_running_tid(VG_(get_running_tid)());
480 if (size > 0)
481 {
482 drd_trace_store(a, size);
483 }
sewardjaf44c822007-11-25 14:01:38 +0000484}
485
bart08865622008-06-06 14:31:36 +0000486static __inline__
487void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000488{
bart005dc972008-03-29 14:42:59 +0000489 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000490
bart005dc972008-03-29 14:42:59 +0000491 if (range_any_is_traced(a1, len))
bart3772a982008-03-15 08:11:03 +0000492 {
bartd5765912008-03-16 08:40:55 +0000493 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000494 }
sewardjaf44c822007-11-25 14:01:38 +0000495}
496
sewardj7cf4e6b2008-05-01 20:24:26 +0000497static void drd_start_using_mem_w_ecu(const Addr a1,
498 const SizeT len,
499 UInt ec_uniq)
500{
501 drd_start_using_mem(a1, len);
502}
503
504static void drd_start_using_mem_w_tid(const Addr a1,
505 const SizeT len,
506 ThreadId tid)
507{
508 drd_start_using_mem(a1, len);
509}
510
bart0ffa4832008-04-05 12:57:01 +0000511static __inline__
512void drd_stop_using_mem(const Addr a1, const SizeT len,
513 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000514{
bart3772a982008-03-15 08:11:03 +0000515 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000516
bart3772a982008-03-15 08:11:03 +0000517 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000518
bart005dc972008-03-29 14:42:59 +0000519 if (range_any_is_traced(a1, len))
bart3772a982008-03-15 08:11:03 +0000520 {
bartd43f8d32008-03-16 17:29:20 +0000521 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000522 }
bart08865622008-06-06 14:31:36 +0000523 if (! is_stack_mem || s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000524 {
525 thread_stop_using_mem(a1, a2);
526 clientobj_stop_using_mem(a1, a2);
527 drd_suppression_stop_using_mem(a1, a2);
528 }
529}
530
531static __inline__
532void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
533{
534 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000535}
536
bartcb2d0072008-05-31 07:55:51 +0000537/** Suppress data race reports on all addresses contained in .plt and
538 * .got.plt sections inside the address range [ a, a + len [. The data in
539 * these sections is modified by _dl_relocate_object() every time a function
540 * in a shared library is called for the first time. Since the first call
541 * to a function in a shared library can happen from a multithreaded context,
542 * such calls can cause conflicting accesses. See also Ulrich Drepper's
543 * paper "How to Write Shared Libraries" for more information about relocation
544 * (http://people.redhat.com/drepper/dsohowto.pdf).
545 */
546static void suppress_relocation_conflicts(const Addr a, const SizeT len)
547{
548 const DebugInfo* di;
549
550#if 0
551 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
552#endif
553
554 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
555 {
556 Addr avma;
557 SizeT size;
558
559 avma = VG_(seginfo_get_plt_avma)(di);
560 size = VG_(seginfo_get_plt_size)(di);
561 if (a <= avma && avma + size <= a + len)
562 {
563#if 0
564 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
565#endif
566 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
567 drd_start_suppression(avma, avma + size, ".plt");
568 }
569
570 avma = VG_(seginfo_get_gotplt_avma)(di);
571 size = VG_(seginfo_get_gotplt_size)(di);
572 if (a <= avma && avma + size <= a + len)
573 {
574#if 0
575 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
576#endif
577 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
578 drd_start_suppression(avma, avma + size, ".gotplt");
579 }
580 }
581}
582
bart5e85d262008-03-01 10:49:37 +0000583static
584void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
585 const Bool rr, const Bool ww, const Bool xx)
586{
bartd5765912008-03-16 08:40:55 +0000587 thread_set_vg_running_tid(VG_(get_running_tid)());
588
bart3772a982008-03-15 08:11:03 +0000589 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000590
591 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000592}
593
sewardjaf44c822007-11-25 14:01:38 +0000594/* Called by the core when the stack of a thread grows, to indicate that */
595/* the addresses in range [ a, a + len [ may now be used by the client. */
596/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000597static __inline__
598void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000599{
bartd43f8d32008-03-16 17:29:20 +0000600 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000601 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
602 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000603}
604
605/* Called by the core when the stack of a thread shrinks, to indicate that */
606/* the addresses [ a, a + len [ are no longer accessible for the client. */
607/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000608static __inline__
609void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000610{
bartd43f8d32008-03-16 17:29:20 +0000611 thread_set_stack_min(thread_get_running_tid(),
612 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000613 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
614 True);
sewardjaf44c822007-11-25 14:01:38 +0000615}
616
sewardj7cf4e6b2008-05-01 20:24:26 +0000617static void drd_start_using_mem_stack_signal(
618 const Addr a, const SizeT len,
619 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000620{
bartd5765912008-03-16 08:40:55 +0000621 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000622 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000623}
624
bart5e85d262008-03-01 10:49:37 +0000625static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000626{
bart0ffa4832008-04-05 12:57:01 +0000627 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000628}
629
630static
631void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
632{
bart3772a982008-03-15 08:11:03 +0000633 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
634 tl_assert(created != VG_INVALID_THREADID);
635 thread_pre_create(drd_creator, created);
636 if (IsValidDrdThreadId(drd_creator))
637 {
638 thread_new_segment(drd_creator);
639 }
bartbd7e56e2008-03-31 18:14:12 +0000640 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000641 {
642 VG_(message)(Vg_DebugMsg,
643 "drd_pre_thread_create creator = %d/%d, created = %d",
644 creator, drd_creator, created);
645 }
sewardjaf44c822007-11-25 14:01:38 +0000646}
647
648/* Called by Valgrind's core before any loads or stores are performed on */
649/* the context of thread "created". At startup, this function is called */
650/* with arguments (0,1). */
651static
bart0ffa4832008-04-05 12:57:01 +0000652void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000653{
bart0ffa4832008-04-05 12:57:01 +0000654 DrdThreadId drd_created;
655
656 tl_assert(vg_created != VG_INVALID_THREADID);
657
658 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000659 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000660 {
661 VG_(message)(Vg_DebugMsg,
662 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000663 vg_created, drd_created);
664 }
bart08865622008-06-06 14:31:36 +0000665 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000666 {
667 drd_start_suppression(thread_get_stack_max(drd_created)
668 - thread_get_stack_size(drd_created),
669 thread_get_stack_max(drd_created),
670 "stack");
bart3772a982008-03-15 08:11:03 +0000671 }
sewardjaf44c822007-11-25 14:01:38 +0000672}
673
674/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
675/* after thread drd_joiner joined thread drd_joinee. */
676void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
677{
bart3772a982008-03-15 08:11:03 +0000678 tl_assert(IsValidDrdThreadId(drd_joiner));
679 tl_assert(IsValidDrdThreadId(drd_joinee));
680 thread_new_segment(drd_joinee);
681 thread_combine_vc(drd_joiner, drd_joinee);
682 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000683
bartbd7e56e2008-03-31 18:14:12 +0000684 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000685 {
bartfdd8d4e2008-04-01 18:38:29 +0000686 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
687 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000688 const unsigned msg_size = 256;
689 char* msg;
690
691 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000692 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000693 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000694 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
695 joiner, drd_joiner, joinee, drd_joinee);
696 if (joiner)
697 {
bart024a95a2008-04-01 18:27:41 +0000698 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000699 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000700 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000701 thread_get_vc(drd_joiner));
702 }
703 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000704 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000705 }
sewardjaf44c822007-11-25 14:01:38 +0000706
bart08865622008-06-06 14:31:36 +0000707 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000708 {
709 drd_finish_suppression(thread_get_stack_max(drd_joinee)
710 - thread_get_stack_size(drd_joinee),
711 thread_get_stack_max(drd_joinee));
712 }
bart3772a982008-03-15 08:11:03 +0000713 thread_delete(drd_joinee);
714 mutex_thread_delete(drd_joinee);
715 cond_thread_delete(drd_joinee);
716 semaphore_thread_delete(drd_joinee);
717 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000718}
719
bart5bd9f2d2008-03-03 20:31:58 +0000720
sewardjaf44c822007-11-25 14:01:38 +0000721/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000722static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000723{
bart3772a982008-03-15 08:11:03 +0000724 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000725
bartd43f8d32008-03-16 17:29:20 +0000726 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000727
bartd43f8d32008-03-16 17:29:20 +0000728 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000729 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000730 {
731 VG_(message)(Vg_DebugMsg,
732 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000733 vg_tid,
bart3772a982008-03-15 08:11:03 +0000734 drd_tid,
735 thread_get_joinable(drd_tid)
736 ? ""
737 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000738 }
739 if (s_show_stack_usage)
740 {
741 const SizeT stack_size = thread_get_stack_size(drd_tid);
742 const SizeT used_stack
743 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
744 VG_(message)(Vg_UserMsg,
745 "thread %d/%d%s finished and used %ld bytes out of %ld"
746 " on its stack. Margin: %ld bytes.",
747 vg_tid,
748 drd_tid,
749 thread_get_joinable(drd_tid)
750 ? ""
751 : " (which is a detached thread)",
752 used_stack,
753 stack_size,
754 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000755
bart3772a982008-03-15 08:11:03 +0000756 }
bartd43f8d32008-03-16 17:29:20 +0000757 drd_stop_using_mem(thread_get_stack_min(drd_tid),
758 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000759 - thread_get_stack_min(drd_tid),
760 True);
bartd43f8d32008-03-16 17:29:20 +0000761 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000762 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000763}
764
bart0268dfa2008-03-11 20:10:21 +0000765void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000766{
bart3772a982008-03-15 08:11:03 +0000767 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000768}
769
sewardj347eeba2008-01-21 14:19:07 +0000770void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000771{
bart3772a982008-03-15 08:11:03 +0000772 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000773}
774
bart2e3a3c12008-03-24 08:33:47 +0000775void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
776 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000777{
bart2e3a3c12008-03-24 08:33:47 +0000778 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000779}
780
bart00344642008-03-01 15:27:41 +0000781void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000782{
bart4a975e12008-03-30 13:28:33 +0000783 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000784}
785
bart00344642008-03-01 15:27:41 +0000786void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000787{
bart3772a982008-03-15 08:11:03 +0000788 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000789}
790
bart0268dfa2008-03-11 20:10:21 +0000791void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000792{
bart3772a982008-03-15 08:11:03 +0000793 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000794}
795
bart72b751c2008-03-01 13:44:24 +0000796void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000797{
bart3772a982008-03-15 08:11:03 +0000798 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000799}
800
bart0268dfa2008-03-11 20:10:21 +0000801void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000802 const Word pshared, const Word value)
803{
bart3772a982008-03-15 08:11:03 +0000804 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000805}
806
807void drd_semaphore_destroy(const Addr semaphore)
808{
bart3772a982008-03-15 08:11:03 +0000809 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000810}
811
bart0268dfa2008-03-11 20:10:21 +0000812void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000813{
bart3772a982008-03-15 08:11:03 +0000814 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000815}
816
817void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
818 const Bool waited)
819{
bart3772a982008-03-15 08:11:03 +0000820 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000821}
822
bart0268dfa2008-03-11 20:10:21 +0000823void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000824{
bart3772a982008-03-15 08:11:03 +0000825 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000826}
827
828void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000829 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000830{
bart3772a982008-03-15 08:11:03 +0000831 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000832}
833
834
bart0268dfa2008-03-11 20:10:21 +0000835void drd_barrier_init(const Addr barrier,
836 const BarrierT barrier_type, const Word count,
837 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000838{
bart3772a982008-03-15 08:11:03 +0000839 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000840}
841
bart0268dfa2008-03-11 20:10:21 +0000842void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000843{
bart3772a982008-03-15 08:11:03 +0000844 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000845}
846
bart0268dfa2008-03-11 20:10:21 +0000847void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
848 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000849{
bart3772a982008-03-15 08:11:03 +0000850 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000851}
852
853void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000854 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000855{
bart3772a982008-03-15 08:11:03 +0000856 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000857}
858
sewardjaf44c822007-11-25 14:01:38 +0000859
860//
861// Implementation of the tool interface.
862//
863
864static
865void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000866{
867# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000868 /* fine */
bartd5c63522008-06-01 07:33:14 +0000869# elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bartcb2d0072008-05-31 07:55:51 +0000870 VG_(printf)(
871"\nWARNING: support for PowerPC-specific atomic instructions like lwarx and\n"
872"stwcx is not yet complete. As a result, false positives will be reported on\n"
873"code that uses these instructions. This will happen e.g. when printf() is\n"
874"called from more than one thread.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000875# else
bart3772a982008-03-15 08:11:03 +0000876 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000877# endif
bart95761b52008-03-29 08:34:03 +0000878
879 if (s_drd_var_info)
880 {
881 VG_(needs_var_info)();
882 }
sewardjdcbb8d32007-11-26 21:34:30 +0000883}
sewardjaf44c822007-11-25 14:01:38 +0000884
barta79df6e2008-03-14 17:07:51 +0000885static void instrument_load(IRSB* const bb,
886 IRExpr* const addr_expr,
887 const HWord size)
888{
bart3772a982008-03-15 08:11:03 +0000889 IRExpr* size_expr;
890 IRExpr** argv;
891 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000892
bart3772a982008-03-15 08:11:03 +0000893 switch (size)
894 {
895 case 1:
896 argv = mkIRExprVec_1(addr_expr);
897 di = unsafeIRDirty_0_N(/*regparms*/1,
898 "drd_trace_load_1",
899 VG_(fnptr_to_fnentry)(drd_trace_load_1),
900 argv);
901 break;
902 case 2:
903 argv = mkIRExprVec_1(addr_expr);
904 di = unsafeIRDirty_0_N(/*regparms*/1,
905 "drd_trace_load_2",
906 VG_(fnptr_to_fnentry)(drd_trace_load_2),
907 argv);
908 break;
909 case 4:
910 argv = mkIRExprVec_1(addr_expr);
911 di = unsafeIRDirty_0_N(/*regparms*/1,
912 "drd_trace_load_4",
913 VG_(fnptr_to_fnentry)(drd_trace_load_4),
914 argv);
915 break;
916 case 8:
917 argv = mkIRExprVec_1(addr_expr);
918 di = unsafeIRDirty_0_N(/*regparms*/1,
919 "drd_trace_load_8",
920 VG_(fnptr_to_fnentry)(drd_trace_load_8),
921 argv);
922 break;
923 default:
924 size_expr = mkIRExpr_HWord(size);
925 argv = mkIRExprVec_2(addr_expr, size_expr);
926 di = unsafeIRDirty_0_N(/*regparms*/2,
927 "drd_trace_load",
928 VG_(fnptr_to_fnentry)(drd_trace_load),
929 argv);
930 break;
931 }
932 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000933}
934
935static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000936 IRExpr* const addr_expr,
937 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000938{
bart3772a982008-03-15 08:11:03 +0000939 IRExpr* size_expr;
940 IRExpr** argv;
941 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000942
bart3772a982008-03-15 08:11:03 +0000943 switch (size)
944 {
945 case 1:
946 argv = mkIRExprVec_1(addr_expr);
947 di = unsafeIRDirty_0_N(/*regparms*/1,
948 "drd_trace_store_1",
949 VG_(fnptr_to_fnentry)(drd_trace_store_1),
950 argv);
951 break;
952 case 2:
953 argv = mkIRExprVec_1(addr_expr);
954 di = unsafeIRDirty_0_N(/*regparms*/1,
955 "drd_trace_store_2",
956 VG_(fnptr_to_fnentry)(drd_trace_store_2),
957 argv);
958 break;
959 case 4:
960 argv = mkIRExprVec_1(addr_expr);
961 di = unsafeIRDirty_0_N(/*regparms*/1,
962 "drd_trace_store_4",
963 VG_(fnptr_to_fnentry)(drd_trace_store_4),
964 argv);
965 break;
966 case 8:
967 argv = mkIRExprVec_1(addr_expr);
968 di = unsafeIRDirty_0_N(/*regparms*/1,
969 "drd_trace_store_8",
970 VG_(fnptr_to_fnentry)(drd_trace_store_8),
971 argv);
972 break;
973 default:
974 size_expr = mkIRExpr_HWord(size);
975 argv = mkIRExprVec_2(addr_expr, size_expr);
976 di = unsafeIRDirty_0_N(/*regparms*/2,
977 "drd_trace_store",
978 VG_(fnptr_to_fnentry)(drd_trace_store),
979 argv);
980 break;
981 }
982 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000983}
984
sewardjaf44c822007-11-25 14:01:38 +0000985static
986IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +0000987 IRSB* const bb_in,
988 VexGuestLayout* const layout,
989 VexGuestExtents* const vge,
990 IRType const gWordTy,
991 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +0000992{
bart3772a982008-03-15 08:11:03 +0000993 IRDirty* di;
994 Int i;
995 IRSB* bb;
996 IRExpr** argv;
997 Bool instrument = True;
998 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +0000999
bart3772a982008-03-15 08:11:03 +00001000 /* Set up BB */
1001 bb = emptyIRSB();
1002 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
1003 bb->next = deepCopyIRExpr(bb_in->next);
1004 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +00001005
bart3772a982008-03-15 08:11:03 +00001006 for (i = 0; i < bb_in->stmts_used; i++)
1007 {
1008 IRStmt* const st = bb_in->stmts[i];
1009 tl_assert(st);
1010 if (st->tag == Ist_NoOp)
1011 continue;
sewardjaf44c822007-11-25 14:01:38 +00001012
bart3772a982008-03-15 08:11:03 +00001013 switch (st->tag)
1014 {
bart3772a982008-03-15 08:11:03 +00001015 case Ist_MBE:
1016 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001017 {
bart3772a982008-03-15 08:11:03 +00001018 case Imbe_Fence:
1019 break; /* not interesting */
1020 case Imbe_BusLock:
1021 tl_assert(! bus_locked);
1022 bus_locked = True;
1023 break;
1024 case Imbe_BusUnlock:
1025 tl_assert(bus_locked);
1026 bus_locked = False;
1027 break;
sewardjaf44c822007-11-25 14:01:38 +00001028 default:
bart3772a982008-03-15 08:11:03 +00001029 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001030 }
bart3772a982008-03-15 08:11:03 +00001031 addStmtToIRSB(bb, st);
1032 break;
sewardjaf44c822007-11-25 14:01:38 +00001033
bart3772a982008-03-15 08:11:03 +00001034 case Ist_Store:
1035 if (instrument && ! bus_locked)
1036 {
1037 instrument_store(bb,
1038 st->Ist.Store.addr,
1039 sizeofIRType(typeOfIRExpr(bb->tyenv,
1040 st->Ist.Store.data)));
1041 }
1042 addStmtToIRSB(bb, st);
1043 break;
barta47b3512008-03-07 17:22:26 +00001044
bart3772a982008-03-15 08:11:03 +00001045 case Ist_WrTmp:
1046 if (instrument)
1047 {
1048 const IRExpr* const data = st->Ist.WrTmp.data;
1049 if (data->tag == Iex_Load)
1050 {
1051 instrument_load(bb,
1052 data->Iex.Load.addr,
1053 sizeofIRType(data->Iex.Load.ty));
1054 }
1055 }
1056 addStmtToIRSB(bb, st);
1057 break;
1058
1059 case Ist_Dirty:
1060 if (instrument)
1061 {
1062 IRDirty* d = st->Ist.Dirty.details;
1063 IREffect const mFx = d->mFx;
1064 switch (mFx) {
1065 case Ifx_None:
1066 break;
1067 case Ifx_Read:
1068 case Ifx_Write:
1069 case Ifx_Modify:
1070 tl_assert(d->mAddr);
1071 tl_assert(d->mSize > 0);
1072 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1073 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1074 di = unsafeIRDirty_0_N(
1075 /*regparms*/2,
1076 "drd_trace_load",
1077 VG_(fnptr_to_fnentry)(drd_trace_load),
1078 argv);
1079 addStmtToIRSB(bb, IRStmt_Dirty(di));
1080 }
1081 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1082 && ! bus_locked)
1083 {
1084 di = unsafeIRDirty_0_N(
1085 /*regparms*/2,
1086 "drd_trace_store",
1087 VG_(fnptr_to_fnentry)(drd_trace_store),
1088 argv);
1089 addStmtToIRSB(bb, IRStmt_Dirty(di));
1090 }
1091 break;
1092 default:
1093 tl_assert(0);
1094 }
1095 }
1096 addStmtToIRSB(bb, st);
1097 break;
1098
1099 default:
1100 addStmtToIRSB(bb, st);
1101 break;
1102 }
1103 }
1104
1105 tl_assert(! bus_locked);
1106
1107 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001108}
1109
sewardjaf44c822007-11-25 14:01:38 +00001110static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1111{
bart3772a982008-03-15 08:11:03 +00001112 tl_assert(tid == VG_(get_running_tid)());
1113 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001114}
1115
1116static
1117void drd_fini(Int exitcode)
1118{
bart3772a982008-03-15 08:11:03 +00001119 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001120 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001121 {
barte4504dd2008-04-06 15:02:58 +00001122 ULong update_danger_set_count;
1123 ULong dsnsc;
1124 ULong dscvc;
1125
1126 update_danger_set_count
1127 = thread_get_update_danger_set_count(&dsnsc, &dscvc);
1128
bartbd7e56e2008-03-31 18:14:12 +00001129 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001130 " thread: %lld context switches"
barte4504dd2008-04-06 15:02:58 +00001131 " / %lld updates of the danger set",
bart3772a982008-03-15 08:11:03 +00001132 thread_get_context_switch_count(),
barte4504dd2008-04-06 15:02:58 +00001133 update_danger_set_count);
1134 VG_(message)(Vg_UserMsg,
1135 " (%lld new sg + %lld combine vc + %lld csw).",
1136 dsnsc,
1137 dscvc,
1138 update_danger_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001139 VG_(message)(Vg_UserMsg,
1140 " segments: created %lld segments, max %lld alive,"
1141 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001142 sg_get_created_segments_count(),
1143 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001144 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001145 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001146 " (%lld m, %lld rw, %lld s, %lld b)",
1147 get_mutex_segment_creation_count(),
1148 get_rwlock_segment_creation_count(),
1149 get_semaphore_segment_creation_count(),
1150 get_barrier_segment_creation_count());
1151 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001152 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001153 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001154 bm_get_bitmap2_node_creation_count());
1155 VG_(message)(Vg_UserMsg,
1156 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001157 bm_get_bitmap2_creation_count());
1158 VG_(message)(Vg_UserMsg,
1159 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001160 get_mutex_lock_count());
1161 drd_print_malloc_stats();
1162 }
sewardjaf44c822007-11-25 14:01:38 +00001163}
1164
sewardjaf44c822007-11-25 14:01:38 +00001165static
1166void drd_pre_clo_init(void)
1167{
bart3772a982008-03-15 08:11:03 +00001168 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001169
bart3772a982008-03-15 08:11:03 +00001170 VG_(details_name) ("exp-drd");
1171 VG_(details_version) (NULL);
1172 VG_(details_description) ("a data race detector");
1173 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1174 " by Bart Van Assche.");
1175 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001176
bart3772a982008-03-15 08:11:03 +00001177 VG_(basic_tool_funcs) (drd_post_clo_init,
1178 drd_instrument,
1179 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001180
bart3772a982008-03-15 08:11:03 +00001181 // Command line stuff.
1182 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1183 drd_print_usage,
1184 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001185
bart3772a982008-03-15 08:11:03 +00001186 // Error handling.
1187 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001188
bart3772a982008-03-15 08:11:03 +00001189 // Core event tracking.
1190 VG_(track_pre_mem_read) (drd_pre_mem_read);
1191 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1192 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001193 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001194 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1195 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1196 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1197 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001198 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1199 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001200 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1201 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1202 VG_(track_start_client_code) (drd_start_client_code);
1203 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1204 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1205 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001206
bart3772a982008-03-15 08:11:03 +00001207 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001208 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001209 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001210
bart3772a982008-03-15 08:11:03 +00001211 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001212
bart3772a982008-03-15 08:11:03 +00001213 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001214
bart3772a982008-03-15 08:11:03 +00001215 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001216}
1217
1218
1219VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)