blob: 70e15570b694e855d96ce324c5b086fcd4e0e907 [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"
bartd59bb0f2008-06-08 08:08:31 +000038#include "drd_thread_bitmap.h"
sewardjaf44c822007-11-25 14:01:38 +000039#include "drd_track.h"
40#include "drd_vc.h"
barteacd9162008-06-16 20:22:18 +000041#include "libvex_guest_offsets.h"
sewardj721ad7b2007-11-30 08:30:29 +000042#include "priv_drd_clientreq.h"
sewardj85642922008-01-14 11:54:56 +000043#include "pub_drd_bitmap.h"
bart024a95a2008-04-01 18:27:41 +000044#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
sewardjaf44c822007-11-25 14:01:38 +000045#include "pub_tool_basics.h"
46#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
47#include "pub_tool_libcassert.h" // tl_assert()
48#include "pub_tool_libcbase.h" // VG_(strcmp)
49#include "pub_tool_libcprint.h" // VG_(printf)
50#include "pub_tool_libcproc.h"
51#include "pub_tool_machine.h"
bart024a95a2008-04-01 18:27:41 +000052#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardjaf44c822007-11-25 14:01:38 +000053#include "pub_tool_options.h" // command line options
bartceded212008-03-26 17:39:52 +000054#include "pub_tool_replacemalloc.h"
bart72b751c2008-03-01 13:44:24 +000055#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000056#include "pub_tool_tooliface.h"
57
58
bartbf80e122008-06-06 10:18:24 +000059/* Include several source files here in order to allow the compiler to */
60/* do more inlining. */
61#include "drd_bitmap.c"
62#include "drd_segment.c"
63#include "drd_thread.c"
64#include "drd_vc.c"
65
66
67
sewardjaf44c822007-11-25 14:01:38 +000068// Function declarations.
69
70static void drd_start_client_code(const ThreadId tid, const ULong bbs_done);
sewardjaf44c822007-11-25 14:01:38 +000071
72
73// Local variables.
74
bart08865622008-06-06 14:31:36 +000075static Bool s_drd_check_stack_accesses = False;
76static Bool s_drd_print_stats = False;
77static Bool s_drd_trace_fork_join = False;
78static Bool s_drd_var_info = False;
79static Bool s_show_stack_usage = False;
sewardjaf44c822007-11-25 14:01:38 +000080
81
82//
83// Implement the needs_command_line_options for drd.
84//
85
86static Bool drd_process_cmd_line_option(Char* arg)
87{
bart9d5b7962008-05-14 12:25:00 +000088 int exclusive_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000089 int segment_merging = -1;
bart9d5b7962008-05-14 12:25:00 +000090 int shared_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000091 int show_confl_seg = -1;
92 int trace_barrier = -1;
93 int trace_clientobj = -1;
94 int trace_cond = -1;
95 int trace_csw = -1;
barte73b0aa2008-06-28 07:19:56 +000096 int trace_conflict_set = -1;
bart08865622008-06-06 14:31:36 +000097 int trace_mutex = -1;
98 int trace_rwlock = -1;
99 int trace_segment = -1;
100 int trace_semaphore = -1;
101 int trace_suppression = -1;
102 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +0000103
bart08865622008-06-06 14:31:36 +0000104 VG_BOOL_CLO (arg, "--check-stack-var", s_drd_check_stack_accesses)
bart9d5b7962008-05-14 12:25:00 +0000105 else VG_BOOL_CLO(arg, "--drd-stats", s_drd_print_stats)
bart46b5fce2008-06-28 13:01:30 +0000106 else VG_BOOL_CLO(arg,"--report-signal-unlocked",s_drd_report_signal_unlocked)
bart9d5b7962008-05-14 12:25:00 +0000107 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
108 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
109 else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage)
110 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
111 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
112 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
barte73b0aa2008-06-28 07:19:56 +0000113 else VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set)
bart46b5fce2008-06-28 13:01:30 +0000114 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
bart9d5b7962008-05-14 12:25:00 +0000115 else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join)
116 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
117 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
118 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
119 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
120 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
121 else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info)
122 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
123 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
124 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000125 else
bartceded212008-03-26 17:39:52 +0000126 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000127
bart9d5b7962008-05-14 12:25:00 +0000128 if (exclusive_threshold_ms != -1)
129 {
130 mutex_set_lock_threshold(exclusive_threshold_ms);
131 rwlock_set_exclusive_threshold(exclusive_threshold_ms);
132 }
133 if (shared_threshold_ms != -1)
134 {
135 rwlock_set_shared_threshold(shared_threshold_ms);
136 }
barta9c37392008-03-22 09:38:48 +0000137 if (segment_merging != -1)
138 thread_set_segment_merging(segment_merging);
139 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000140 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000141 if (trace_address)
142 {
bart005dc972008-03-29 14:42:59 +0000143 const Addr addr = VG_(strtoll16)(trace_address, 0);
144 drd_start_tracing_address_range(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000145 }
barta9c37392008-03-22 09:38:48 +0000146 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000147 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000148 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000149 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000150 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000151 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000152 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000153 thread_trace_context_switches(trace_csw);
barte73b0aa2008-06-28 07:19:56 +0000154 if (trace_conflict_set != -1)
155 thread_trace_conflict_set(trace_conflict_set);
barta9c37392008-03-22 09:38:48 +0000156 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000157 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000158 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000159 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000160 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000161 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000162 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000163 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000164 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000165 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000166
bart3772a982008-03-15 08:11:03 +0000167 return True;
sewardjaf44c822007-11-25 14:01:38 +0000168}
169
170static void drd_print_usage(void)
bartbd7e56e2008-03-31 18:14:12 +0000171{
172 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000173" --check-stack-var=yes|no Whether or not to report data races on\n"
174" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000175" --exclusive-threshold=<n> Print an error message if any mutex or\n"
176" writer lock is held longer than the specified time (in milliseconds).\n"
bart46b5fce2008-06-28 13:01:30 +0000177" --report-signal-unlocked=yes|no Whether to report calls to\n"
178" pthread_cond_signal() where the mutex associated\n"
179" with the signal via pthread_cond_wait() is not\n"
180" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000181" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000182" Segment merging is an algorithm to limit memory usage of the\n"
183" data race detection algorithm. Disabling segment merging may\n"
184" improve the accuracy of the so-called 'other segments' displayed\n"
185" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000186" --shared-threshold=<n> Print an error message if a reader lock\n"
187" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000188" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
189" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
190" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000191" stack variables when a race is reported on such a variable. This\n"
192" information is by default not displayed since for big programs\n"
bart85d22532008-06-26 07:30:32 +0000193" reading in all debug information at once may cause an out of\n"
194" memory error [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000195"\n"
bart2cb48d62008-04-01 16:57:42 +0000196" exp-drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000197" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000198" specified address [off].\n"
199" --trace-barrier=yes|no Trace all barrier activity [no].\n"
200" --trace-cond=yes|no Trace all condition variable activity [no].\n"
201" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
202" --trace-mutex=yes|no Trace all mutex activity [no].\n"
203" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart130463a2008-04-01 17:03:33 +0000204" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000205 );
bart130463a2008-04-01 17:03:33 +0000206 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000207}
208
209static void drd_print_debug_usage(void)
210{
bart130463a2008-04-01 17:03:33 +0000211 VG_(printf)(
212" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
213" --trace-clientobj=yes|no Trace all client object activity [no].\n"
214" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000215" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart987781d2008-06-27 15:00:07 +0000216" --trace-segment=yes|no Trace segment actions [no].\n"
217" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bart130463a2008-04-01 17:03:33 +0000218 );
219 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000220}
221
222
223//
224// Implements the thread-related core callbacks.
225//
226
barta79df6e2008-03-14 17:07:51 +0000227static void drd_trace_mem_access(const Addr addr, const SizeT size,
228 const BmAccessTypeT access_type)
229{
bartb9c7d742008-06-10 12:51:51 +0000230 if (drd_is_any_traced(addr, addr + size))
231 {
232 char vc[80];
233 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
234 VG_(message)(Vg_UserMsg,
235 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
236 access_type == eLoad
237 ? "load "
238 : access_type == eStore
239 ? "store"
240 : access_type == eStart
241 ? "start"
242 : access_type == eEnd
243 ? "end "
244 : "????",
245 addr,
246 size,
247 VG_(get_running_tid)(),
248 thread_get_running_tid(),
249 vc);
250 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
251 VG_(clo_backtrace_size));
252 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
253 == VG_(get_running_tid)());
254 }
barta79df6e2008-03-14 17:07:51 +0000255}
256
bart29a0e2a2008-06-10 13:55:13 +0000257static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size)
258{
259 return drd_trace_mem_access(addr, size, eLoad);
260}
261
262static VG_REGPARM(2) void drd_trace_mem_store(const Addr addr,const SizeT size)
263{
264 return drd_trace_mem_access(addr, size, eStore);
265}
266
barta79df6e2008-03-14 17:07:51 +0000267static void drd_report_race(const Addr addr, const SizeT size,
268 const BmAccessTypeT access_type)
269{
bart49c3a112008-03-15 10:28:36 +0000270 DataRaceErrInfo drei;
271
bart354009c2008-03-16 10:42:33 +0000272 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000273 drei.addr = addr;
274 drei.size = size;
275 drei.access_type = access_type;
276 VG_(maybe_record_error)(VG_(get_running_tid)(),
277 DataRaceErr,
278 VG_(get_IP)(VG_(get_running_tid)()),
279 "Conflicting accesses",
280 &drei);
barta79df6e2008-03-14 17:07:51 +0000281}
282
283static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000284{
bart8b4b2ee2008-06-11 13:17:56 +0000285#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000286 /* The assert below has been commented out because of performance reasons.*/
287 tl_assert(thread_get_running_tid()
288 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000289#endif
sewardjaf44c822007-11-25 14:01:38 +0000290
bart0e5c04f2008-06-09 15:18:59 +0000291 if (running_thread_is_recording()
292 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000293 && bm_access_load_triggers_conflict(addr, addr + size)
294 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000295 {
296 drd_report_race(addr, size, eLoad);
297 }
barta79df6e2008-03-14 17:07:51 +0000298}
299
300static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
301{
bart0e5c04f2008-06-09 15:18:59 +0000302 if (running_thread_is_recording()
303 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000304 && bm_access_load_1_triggers_conflict(addr)
305 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000306 {
307 drd_report_race(addr, 1, eLoad);
308 }
barta79df6e2008-03-14 17:07:51 +0000309}
310
311static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
312{
bart0e5c04f2008-06-09 15:18:59 +0000313 if (running_thread_is_recording()
314 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000315 && bm_access_load_2_triggers_conflict(addr)
316 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000317 {
318 drd_report_race(addr, 2, eLoad);
319 }
barta79df6e2008-03-14 17:07:51 +0000320}
321
322static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
323{
bart0e5c04f2008-06-09 15:18:59 +0000324 if (running_thread_is_recording()
325 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000326 && bm_access_load_4_triggers_conflict(addr)
327 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000328 {
329 drd_report_race(addr, 4, eLoad);
330 }
barta79df6e2008-03-14 17:07:51 +0000331}
332
333static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
334{
bart0e5c04f2008-06-09 15:18:59 +0000335 if (running_thread_is_recording()
336 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000337 && bm_access_load_8_triggers_conflict(addr)
338 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000339 {
340 drd_report_race(addr, 8, eLoad);
341 }
sewardjaf44c822007-11-25 14:01:38 +0000342}
343
344static
345VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
346{
bart8b4b2ee2008-06-11 13:17:56 +0000347#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000348 /* The assert below has been commented out because of performance reasons.*/
349 tl_assert(thread_get_running_tid()
350 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000351#endif
sewardjaf44c822007-11-25 14:01:38 +0000352
bart0e5c04f2008-06-09 15:18:59 +0000353 if (running_thread_is_recording()
354 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000355 && bm_access_store_triggers_conflict(addr, addr + size)
356 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000357 {
358 drd_report_race(addr, size, eStore);
359 }
barta79df6e2008-03-14 17:07:51 +0000360}
361
362static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
363{
bart0e5c04f2008-06-09 15:18:59 +0000364 if (running_thread_is_recording()
365 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000366 && bm_access_store_1_triggers_conflict(addr)
367 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000368 {
369 drd_report_race(addr, 1, eStore);
370 }
barta79df6e2008-03-14 17:07:51 +0000371}
372
373static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
374{
bart0e5c04f2008-06-09 15:18:59 +0000375 if (running_thread_is_recording()
376 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000377 && bm_access_store_2_triggers_conflict(addr)
378 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000379 {
380 drd_report_race(addr, 2, eStore);
381 }
barta79df6e2008-03-14 17:07:51 +0000382}
383
384static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
385{
bart0e5c04f2008-06-09 15:18:59 +0000386 if (running_thread_is_recording()
387 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000388 && bm_access_store_4_triggers_conflict(addr)
389 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000390 {
391 drd_report_race(addr, 4, eStore);
392 }
barta79df6e2008-03-14 17:07:51 +0000393}
394
395static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
396{
bart0e5c04f2008-06-09 15:18:59 +0000397 if (running_thread_is_recording()
398 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000399 && bm_access_store_8_triggers_conflict(addr)
400 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000401 {
402 drd_report_race(addr, 8, eStore);
403 }
sewardjaf44c822007-11-25 14:01:38 +0000404}
405
406static void drd_pre_mem_read(const CorePart part,
407 const ThreadId tid,
408 Char* const s,
409 const Addr a,
410 const SizeT size)
411{
bart3772a982008-03-15 08:11:03 +0000412 if (size > 0)
413 {
414 drd_trace_load(a, size);
415 }
sewardjaf44c822007-11-25 14:01:38 +0000416}
417
bart5e85d262008-03-01 10:49:37 +0000418static void drd_pre_mem_read_asciiz(const CorePart part,
419 const ThreadId tid,
420 Char* const s,
421 const Addr a)
422{
bart3772a982008-03-15 08:11:03 +0000423 const char* p = (void*)a;
424 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000425
bart3772a982008-03-15 08:11:03 +0000426 /* Note: the expression '*p' reads client memory and may crash if the */
427 /* client provided an invalid pointer ! */
428 while (*p)
429 {
430 p++;
431 size++;
432 }
433 // To do: find out what a reasonable upper limit on 'size' is.
434 tl_assert(size < 4096);
435 if (size > 0)
436 {
437 drd_trace_load(a, size);
438 }
bart5e85d262008-03-01 10:49:37 +0000439}
440
sewardjaf44c822007-11-25 14:01:38 +0000441static void drd_post_mem_write(const CorePart part,
442 const ThreadId tid,
443 const Addr a,
444 const SizeT size)
445{
bart3772a982008-03-15 08:11:03 +0000446 thread_set_vg_running_tid(VG_(get_running_tid)());
447 if (size > 0)
448 {
449 drd_trace_store(a, size);
450 }
sewardjaf44c822007-11-25 14:01:38 +0000451}
452
bart08865622008-06-06 14:31:36 +0000453static __inline__
454void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000455{
bart005dc972008-03-29 14:42:59 +0000456 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000457
bartb9c7d742008-06-10 12:51:51 +0000458 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000459 {
bartd5765912008-03-16 08:40:55 +0000460 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000461 }
sewardjaf44c822007-11-25 14:01:38 +0000462}
463
sewardj7cf4e6b2008-05-01 20:24:26 +0000464static void drd_start_using_mem_w_ecu(const Addr a1,
465 const SizeT len,
466 UInt ec_uniq)
467{
468 drd_start_using_mem(a1, len);
469}
470
471static void drd_start_using_mem_w_tid(const Addr a1,
472 const SizeT len,
473 ThreadId tid)
474{
475 drd_start_using_mem(a1, len);
476}
477
bart0ffa4832008-04-05 12:57:01 +0000478static __inline__
479void drd_stop_using_mem(const Addr a1, const SizeT len,
480 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000481{
bart3772a982008-03-15 08:11:03 +0000482 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000483
bart3772a982008-03-15 08:11:03 +0000484 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000485
bartb9c7d742008-06-10 12:51:51 +0000486 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000487 {
bartd43f8d32008-03-16 17:29:20 +0000488 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000489 }
bart08865622008-06-06 14:31:36 +0000490 if (! is_stack_mem || s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000491 {
492 thread_stop_using_mem(a1, a2);
493 clientobj_stop_using_mem(a1, a2);
494 drd_suppression_stop_using_mem(a1, a2);
495 }
496}
497
498static __inline__
499void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
500{
501 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000502}
503
bartcb2d0072008-05-31 07:55:51 +0000504/** Suppress data race reports on all addresses contained in .plt and
505 * .got.plt sections inside the address range [ a, a + len [. The data in
506 * these sections is modified by _dl_relocate_object() every time a function
507 * in a shared library is called for the first time. Since the first call
508 * to a function in a shared library can happen from a multithreaded context,
509 * such calls can cause conflicting accesses. See also Ulrich Drepper's
510 * paper "How to Write Shared Libraries" for more information about relocation
511 * (http://people.redhat.com/drepper/dsohowto.pdf).
512 */
513static void suppress_relocation_conflicts(const Addr a, const SizeT len)
514{
515 const DebugInfo* di;
516
517#if 0
518 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
519#endif
520
521 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
522 {
523 Addr avma;
524 SizeT size;
525
526 avma = VG_(seginfo_get_plt_avma)(di);
527 size = VG_(seginfo_get_plt_size)(di);
528 if (a <= avma && avma + size <= a + len)
529 {
530#if 0
531 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
532#endif
533 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
534 drd_start_suppression(avma, avma + size, ".plt");
535 }
536
537 avma = VG_(seginfo_get_gotplt_avma)(di);
538 size = VG_(seginfo_get_gotplt_size)(di);
539 if (a <= avma && avma + size <= a + len)
540 {
541#if 0
542 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
543#endif
544 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
545 drd_start_suppression(avma, avma + size, ".gotplt");
546 }
547 }
548}
549
bart5e85d262008-03-01 10:49:37 +0000550static
551void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
552 const Bool rr, const Bool ww, const Bool xx)
553{
bartd5765912008-03-16 08:40:55 +0000554 thread_set_vg_running_tid(VG_(get_running_tid)());
555
bart3772a982008-03-15 08:11:03 +0000556 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000557
558 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000559}
560
sewardjaf44c822007-11-25 14:01:38 +0000561/* Called by the core when the stack of a thread grows, to indicate that */
562/* the addresses in range [ a, a + len [ may now be used by the client. */
563/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000564static __inline__
565void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000566{
bartd43f8d32008-03-16 17:29:20 +0000567 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000568 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
569 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000570}
571
572/* Called by the core when the stack of a thread shrinks, to indicate that */
573/* the addresses [ a, a + len [ are no longer accessible for the client. */
574/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000575static __inline__
576void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000577{
bartd43f8d32008-03-16 17:29:20 +0000578 thread_set_stack_min(thread_get_running_tid(),
579 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000580 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
581 True);
sewardjaf44c822007-11-25 14:01:38 +0000582}
583
sewardj7cf4e6b2008-05-01 20:24:26 +0000584static void drd_start_using_mem_stack_signal(
585 const Addr a, const SizeT len,
586 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000587{
bartd5765912008-03-16 08:40:55 +0000588 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000589 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000590}
591
bart5e85d262008-03-01 10:49:37 +0000592static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000593{
bart0ffa4832008-04-05 12:57:01 +0000594 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000595}
596
597static
598void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
599{
bart3772a982008-03-15 08:11:03 +0000600 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
601 tl_assert(created != VG_INVALID_THREADID);
602 thread_pre_create(drd_creator, created);
603 if (IsValidDrdThreadId(drd_creator))
604 {
605 thread_new_segment(drd_creator);
606 }
bartbd7e56e2008-03-31 18:14:12 +0000607 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000608 {
609 VG_(message)(Vg_DebugMsg,
610 "drd_pre_thread_create creator = %d/%d, created = %d",
611 creator, drd_creator, created);
612 }
sewardjaf44c822007-11-25 14:01:38 +0000613}
614
615/* Called by Valgrind's core before any loads or stores are performed on */
616/* the context of thread "created". At startup, this function is called */
617/* with arguments (0,1). */
618static
bart0ffa4832008-04-05 12:57:01 +0000619void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000620{
bart0ffa4832008-04-05 12:57:01 +0000621 DrdThreadId drd_created;
622
623 tl_assert(vg_created != VG_INVALID_THREADID);
624
625 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000626 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000627 {
628 VG_(message)(Vg_DebugMsg,
629 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000630 vg_created, drd_created);
631 }
bart08865622008-06-06 14:31:36 +0000632 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000633 {
634 drd_start_suppression(thread_get_stack_max(drd_created)
635 - thread_get_stack_size(drd_created),
636 thread_get_stack_max(drd_created),
637 "stack");
bart3772a982008-03-15 08:11:03 +0000638 }
sewardjaf44c822007-11-25 14:01:38 +0000639}
640
641/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
642/* after thread drd_joiner joined thread drd_joinee. */
643void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
644{
bart3772a982008-03-15 08:11:03 +0000645 tl_assert(IsValidDrdThreadId(drd_joiner));
646 tl_assert(IsValidDrdThreadId(drd_joinee));
647 thread_new_segment(drd_joinee);
648 thread_combine_vc(drd_joiner, drd_joinee);
649 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000650
bartbd7e56e2008-03-31 18:14:12 +0000651 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000652 {
bartfdd8d4e2008-04-01 18:38:29 +0000653 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
654 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000655 const unsigned msg_size = 256;
656 char* msg;
657
658 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000659 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000660 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000661 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
662 joiner, drd_joiner, joinee, drd_joinee);
663 if (joiner)
664 {
bart024a95a2008-04-01 18:27:41 +0000665 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000666 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000667 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000668 thread_get_vc(drd_joiner));
669 }
670 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000671 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000672 }
sewardjaf44c822007-11-25 14:01:38 +0000673
bart08865622008-06-06 14:31:36 +0000674 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000675 {
676 drd_finish_suppression(thread_get_stack_max(drd_joinee)
677 - thread_get_stack_size(drd_joinee),
678 thread_get_stack_max(drd_joinee));
679 }
bart3772a982008-03-15 08:11:03 +0000680 thread_delete(drd_joinee);
681 mutex_thread_delete(drd_joinee);
682 cond_thread_delete(drd_joinee);
683 semaphore_thread_delete(drd_joinee);
684 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000685}
686
bart5bd9f2d2008-03-03 20:31:58 +0000687
sewardjaf44c822007-11-25 14:01:38 +0000688/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000689static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000690{
bart3772a982008-03-15 08:11:03 +0000691 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000692
bartd43f8d32008-03-16 17:29:20 +0000693 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000694
bartd43f8d32008-03-16 17:29:20 +0000695 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000696 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000697 {
698 VG_(message)(Vg_DebugMsg,
699 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000700 vg_tid,
bart3772a982008-03-15 08:11:03 +0000701 drd_tid,
702 thread_get_joinable(drd_tid)
703 ? ""
704 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000705 }
706 if (s_show_stack_usage)
707 {
708 const SizeT stack_size = thread_get_stack_size(drd_tid);
709 const SizeT used_stack
710 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
711 VG_(message)(Vg_UserMsg,
712 "thread %d/%d%s finished and used %ld bytes out of %ld"
713 " on its stack. Margin: %ld bytes.",
714 vg_tid,
715 drd_tid,
716 thread_get_joinable(drd_tid)
717 ? ""
718 : " (which is a detached thread)",
719 used_stack,
720 stack_size,
721 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000722
bart3772a982008-03-15 08:11:03 +0000723 }
bartd43f8d32008-03-16 17:29:20 +0000724 drd_stop_using_mem(thread_get_stack_min(drd_tid),
725 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000726 - thread_get_stack_min(drd_tid),
727 True);
bartd43f8d32008-03-16 17:29:20 +0000728 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000729 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000730}
731
bart0268dfa2008-03-11 20:10:21 +0000732void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000733{
bart3772a982008-03-15 08:11:03 +0000734 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000735}
736
sewardj347eeba2008-01-21 14:19:07 +0000737void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000738{
bart3772a982008-03-15 08:11:03 +0000739 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000740}
741
bart2e3a3c12008-03-24 08:33:47 +0000742void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
743 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000744{
bart2e3a3c12008-03-24 08:33:47 +0000745 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000746}
747
bart00344642008-03-01 15:27:41 +0000748void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000749{
bart4a975e12008-03-30 13:28:33 +0000750 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000751}
752
bart00344642008-03-01 15:27:41 +0000753void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000754{
bart3772a982008-03-15 08:11:03 +0000755 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000756}
757
bart0268dfa2008-03-11 20:10:21 +0000758void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000759{
bart3772a982008-03-15 08:11:03 +0000760 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000761}
762
bart72b751c2008-03-01 13:44:24 +0000763void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000764{
bart3772a982008-03-15 08:11:03 +0000765 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000766}
767
bart0268dfa2008-03-11 20:10:21 +0000768void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000769 const Word pshared, const Word value)
770{
bart3772a982008-03-15 08:11:03 +0000771 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000772}
773
774void drd_semaphore_destroy(const Addr semaphore)
775{
bart3772a982008-03-15 08:11:03 +0000776 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000777}
778
bart0268dfa2008-03-11 20:10:21 +0000779void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000780{
bart3772a982008-03-15 08:11:03 +0000781 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000782}
783
784void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
785 const Bool waited)
786{
bart3772a982008-03-15 08:11:03 +0000787 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000788}
789
bart0268dfa2008-03-11 20:10:21 +0000790void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000791{
bart3772a982008-03-15 08:11:03 +0000792 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000793}
794
795void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000796 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000797{
bart3772a982008-03-15 08:11:03 +0000798 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000799}
800
801
bart0268dfa2008-03-11 20:10:21 +0000802void drd_barrier_init(const Addr barrier,
803 const BarrierT barrier_type, const Word count,
804 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000805{
bart3772a982008-03-15 08:11:03 +0000806 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000807}
808
bart0268dfa2008-03-11 20:10:21 +0000809void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000810{
bart3772a982008-03-15 08:11:03 +0000811 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000812}
813
bart0268dfa2008-03-11 20:10:21 +0000814void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
815 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000816{
bart3772a982008-03-15 08:11:03 +0000817 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000818}
819
820void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000821 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000822{
bart3772a982008-03-15 08:11:03 +0000823 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000824}
825
sewardjaf44c822007-11-25 14:01:38 +0000826
827//
828// Implementation of the tool interface.
829//
830
831static
832void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000833{
834# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000835 /* fine */
bartd5c63522008-06-01 07:33:14 +0000836# elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bartcb2d0072008-05-31 07:55:51 +0000837 VG_(printf)(
838"\nWARNING: support for PowerPC-specific atomic instructions like lwarx and\n"
839"stwcx is not yet complete. As a result, false positives will be reported on\n"
840"code that uses these instructions. This will happen e.g. when printf() is\n"
841"called from more than one thread.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000842# else
bart3772a982008-03-15 08:11:03 +0000843 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000844# endif
bart95761b52008-03-29 08:34:03 +0000845
846 if (s_drd_var_info)
847 {
848 VG_(needs_var_info)();
849 }
sewardjdcbb8d32007-11-26 21:34:30 +0000850}
sewardjaf44c822007-11-25 14:01:38 +0000851
barteacd9162008-06-16 20:22:18 +0000852#if defined(VGA_x86)
853#define STACK_POINTER_OFFSET OFFSET_x86_ESP
854#elif defined(VGA_amd64)
855#define STACK_POINTER_OFFSET OFFSET_amd64_RSP
856#elif defined(VGA_ppc32)
857#define STACK_POINTER_OFFSET ((OFFSET_ppc32_GPR0 + OFFSET_ppc32_GPR2) / 2)
858#elif defined(VGA_ppc64)
859#define STACK_POINTER_OFFSET ((OFFSET_ppc64_GPR0 + OFFSET_ppc64_GPR2) / 2)
860#else
861#error Unknown architecture.
862#endif
863
864
865/** Return true if and only if addr_expr matches the pattern (SP) or
866 * <offset>(SP).
867 */
868static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr)
869{
870 Bool result = False;
871
872 if (addr_expr->tag == Iex_RdTmp)
873 {
874 int i;
875 for (i = 0; i < bb->stmts_size; i++)
876 {
877 if (bb->stmts[i]
878 && bb->stmts[i]->tag == Ist_WrTmp
879 && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp)
880 {
881 IRExpr* e = bb->stmts[i]->Ist.WrTmp.data;
882 if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET)
883 {
884 result = True;
885 }
886
887 //ppIRExpr(e);
888 //VG_(printf)(" (%s)\n", result ? "True" : "False");
889 break;
890 }
891 }
892 }
893 return result;
894}
895
barta79df6e2008-03-14 17:07:51 +0000896static void instrument_load(IRSB* const bb,
897 IRExpr* const addr_expr,
898 const HWord size)
899{
bart3772a982008-03-15 08:11:03 +0000900 IRExpr* size_expr;
901 IRExpr** argv;
902 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000903
bart29a0e2a2008-06-10 13:55:13 +0000904 if (UNLIKELY(drd_any_address_is_traced()))
905 {
906 addStmtToIRSB(bb,
907 IRStmt_Dirty(
908 unsafeIRDirty_0_N(/*regparms*/2,
909 "drd_trace_load",
910 VG_(fnptr_to_fnentry)
911 (drd_trace_mem_load),
912 mkIRExprVec_2(addr_expr,
913 mkIRExpr_HWord(size)))));
914 }
915
barteacd9162008-06-16 20:22:18 +0000916 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
917 return;
918
bart3772a982008-03-15 08:11:03 +0000919 switch (size)
920 {
921 case 1:
922 argv = mkIRExprVec_1(addr_expr);
923 di = unsafeIRDirty_0_N(/*regparms*/1,
924 "drd_trace_load_1",
925 VG_(fnptr_to_fnentry)(drd_trace_load_1),
926 argv);
927 break;
928 case 2:
929 argv = mkIRExprVec_1(addr_expr);
930 di = unsafeIRDirty_0_N(/*regparms*/1,
931 "drd_trace_load_2",
932 VG_(fnptr_to_fnentry)(drd_trace_load_2),
933 argv);
934 break;
935 case 4:
936 argv = mkIRExprVec_1(addr_expr);
937 di = unsafeIRDirty_0_N(/*regparms*/1,
938 "drd_trace_load_4",
939 VG_(fnptr_to_fnentry)(drd_trace_load_4),
940 argv);
941 break;
942 case 8:
943 argv = mkIRExprVec_1(addr_expr);
944 di = unsafeIRDirty_0_N(/*regparms*/1,
945 "drd_trace_load_8",
946 VG_(fnptr_to_fnentry)(drd_trace_load_8),
947 argv);
948 break;
949 default:
950 size_expr = mkIRExpr_HWord(size);
951 argv = mkIRExprVec_2(addr_expr, size_expr);
952 di = unsafeIRDirty_0_N(/*regparms*/2,
953 "drd_trace_load",
954 VG_(fnptr_to_fnentry)(drd_trace_load),
955 argv);
956 break;
957 }
958 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000959}
960
961static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000962 IRExpr* const addr_expr,
963 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000964{
bart3772a982008-03-15 08:11:03 +0000965 IRExpr* size_expr;
966 IRExpr** argv;
967 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000968
bart29a0e2a2008-06-10 13:55:13 +0000969 if (UNLIKELY(drd_any_address_is_traced()))
970 {
971 addStmtToIRSB(bb,
972 IRStmt_Dirty(
973 unsafeIRDirty_0_N(/*regparms*/2,
974 "drd_trace_store",
975 VG_(fnptr_to_fnentry)
976 (drd_trace_mem_store),
977 mkIRExprVec_2(addr_expr,
978 mkIRExpr_HWord(size)))));
979 }
980
barteacd9162008-06-16 20:22:18 +0000981 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
982 return;
983
bart3772a982008-03-15 08:11:03 +0000984 switch (size)
985 {
986 case 1:
987 argv = mkIRExprVec_1(addr_expr);
988 di = unsafeIRDirty_0_N(/*regparms*/1,
989 "drd_trace_store_1",
990 VG_(fnptr_to_fnentry)(drd_trace_store_1),
991 argv);
992 break;
993 case 2:
994 argv = mkIRExprVec_1(addr_expr);
995 di = unsafeIRDirty_0_N(/*regparms*/1,
996 "drd_trace_store_2",
997 VG_(fnptr_to_fnentry)(drd_trace_store_2),
998 argv);
999 break;
1000 case 4:
1001 argv = mkIRExprVec_1(addr_expr);
1002 di = unsafeIRDirty_0_N(/*regparms*/1,
1003 "drd_trace_store_4",
1004 VG_(fnptr_to_fnentry)(drd_trace_store_4),
1005 argv);
1006 break;
1007 case 8:
1008 argv = mkIRExprVec_1(addr_expr);
1009 di = unsafeIRDirty_0_N(/*regparms*/1,
1010 "drd_trace_store_8",
1011 VG_(fnptr_to_fnentry)(drd_trace_store_8),
1012 argv);
1013 break;
1014 default:
1015 size_expr = mkIRExpr_HWord(size);
1016 argv = mkIRExprVec_2(addr_expr, size_expr);
1017 di = unsafeIRDirty_0_N(/*regparms*/2,
1018 "drd_trace_store",
1019 VG_(fnptr_to_fnentry)(drd_trace_store),
1020 argv);
1021 break;
1022 }
1023 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +00001024}
1025
sewardjaf44c822007-11-25 14:01:38 +00001026static
1027IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +00001028 IRSB* const bb_in,
1029 VexGuestLayout* const layout,
1030 VexGuestExtents* const vge,
1031 IRType const gWordTy,
1032 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +00001033{
bart3772a982008-03-15 08:11:03 +00001034 IRDirty* di;
1035 Int i;
1036 IRSB* bb;
1037 IRExpr** argv;
1038 Bool instrument = True;
1039 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +00001040
bart3772a982008-03-15 08:11:03 +00001041 /* Set up BB */
1042 bb = emptyIRSB();
1043 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
1044 bb->next = deepCopyIRExpr(bb_in->next);
1045 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +00001046
bart3772a982008-03-15 08:11:03 +00001047 for (i = 0; i < bb_in->stmts_used; i++)
1048 {
1049 IRStmt* const st = bb_in->stmts[i];
1050 tl_assert(st);
1051 if (st->tag == Ist_NoOp)
1052 continue;
sewardjaf44c822007-11-25 14:01:38 +00001053
bart3772a982008-03-15 08:11:03 +00001054 switch (st->tag)
1055 {
bart3772a982008-03-15 08:11:03 +00001056 case Ist_MBE:
1057 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001058 {
bart3772a982008-03-15 08:11:03 +00001059 case Imbe_Fence:
1060 break; /* not interesting */
1061 case Imbe_BusLock:
1062 tl_assert(! bus_locked);
1063 bus_locked = True;
1064 break;
1065 case Imbe_BusUnlock:
1066 tl_assert(bus_locked);
1067 bus_locked = False;
1068 break;
sewardjaf44c822007-11-25 14:01:38 +00001069 default:
bart3772a982008-03-15 08:11:03 +00001070 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001071 }
bart3772a982008-03-15 08:11:03 +00001072 addStmtToIRSB(bb, st);
1073 break;
sewardjaf44c822007-11-25 14:01:38 +00001074
bart3772a982008-03-15 08:11:03 +00001075 case Ist_Store:
1076 if (instrument && ! bus_locked)
1077 {
1078 instrument_store(bb,
1079 st->Ist.Store.addr,
1080 sizeofIRType(typeOfIRExpr(bb->tyenv,
1081 st->Ist.Store.data)));
1082 }
1083 addStmtToIRSB(bb, st);
1084 break;
barta47b3512008-03-07 17:22:26 +00001085
bart3772a982008-03-15 08:11:03 +00001086 case Ist_WrTmp:
1087 if (instrument)
1088 {
1089 const IRExpr* const data = st->Ist.WrTmp.data;
1090 if (data->tag == Iex_Load)
1091 {
1092 instrument_load(bb,
1093 data->Iex.Load.addr,
1094 sizeofIRType(data->Iex.Load.ty));
1095 }
1096 }
1097 addStmtToIRSB(bb, st);
1098 break;
1099
1100 case Ist_Dirty:
1101 if (instrument)
1102 {
1103 IRDirty* d = st->Ist.Dirty.details;
1104 IREffect const mFx = d->mFx;
1105 switch (mFx) {
1106 case Ifx_None:
1107 break;
1108 case Ifx_Read:
1109 case Ifx_Write:
1110 case Ifx_Modify:
1111 tl_assert(d->mAddr);
1112 tl_assert(d->mSize > 0);
1113 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1114 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1115 di = unsafeIRDirty_0_N(
1116 /*regparms*/2,
1117 "drd_trace_load",
1118 VG_(fnptr_to_fnentry)(drd_trace_load),
1119 argv);
1120 addStmtToIRSB(bb, IRStmt_Dirty(di));
1121 }
1122 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1123 && ! bus_locked)
1124 {
1125 di = unsafeIRDirty_0_N(
1126 /*regparms*/2,
1127 "drd_trace_store",
1128 VG_(fnptr_to_fnentry)(drd_trace_store),
1129 argv);
1130 addStmtToIRSB(bb, IRStmt_Dirty(di));
1131 }
1132 break;
1133 default:
1134 tl_assert(0);
1135 }
1136 }
1137 addStmtToIRSB(bb, st);
1138 break;
1139
1140 default:
1141 addStmtToIRSB(bb, st);
1142 break;
1143 }
1144 }
1145
1146 tl_assert(! bus_locked);
1147
1148 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001149}
1150
sewardjaf44c822007-11-25 14:01:38 +00001151static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1152{
bart3772a982008-03-15 08:11:03 +00001153 tl_assert(tid == VG_(get_running_tid)());
1154 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001155}
1156
1157static
1158void drd_fini(Int exitcode)
1159{
bart3772a982008-03-15 08:11:03 +00001160 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001161 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001162 {
barte73b0aa2008-06-28 07:19:56 +00001163 ULong update_conflict_set_count;
barte4504dd2008-04-06 15:02:58 +00001164 ULong dsnsc;
1165 ULong dscvc;
1166
barte73b0aa2008-06-28 07:19:56 +00001167 update_conflict_set_count
1168 = thread_get_update_conflict_set_count(&dsnsc, &dscvc);
barte4504dd2008-04-06 15:02:58 +00001169
bartbd7e56e2008-03-31 18:14:12 +00001170 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001171 " thread: %lld context switches"
barte73b0aa2008-06-28 07:19:56 +00001172 " / %lld updates of the conflict set",
bart3772a982008-03-15 08:11:03 +00001173 thread_get_context_switch_count(),
barte73b0aa2008-06-28 07:19:56 +00001174 update_conflict_set_count);
barte4504dd2008-04-06 15:02:58 +00001175 VG_(message)(Vg_UserMsg,
1176 " (%lld new sg + %lld combine vc + %lld csw).",
1177 dsnsc,
1178 dscvc,
barte73b0aa2008-06-28 07:19:56 +00001179 update_conflict_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001180 VG_(message)(Vg_UserMsg,
1181 " segments: created %lld segments, max %lld alive,"
1182 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001183 sg_get_created_segments_count(),
1184 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001185 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001186 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001187 " (%lld m, %lld rw, %lld s, %lld b)",
1188 get_mutex_segment_creation_count(),
1189 get_rwlock_segment_creation_count(),
1190 get_semaphore_segment_creation_count(),
1191 get_barrier_segment_creation_count());
1192 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001193 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001194 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001195 bm_get_bitmap2_node_creation_count());
1196 VG_(message)(Vg_UserMsg,
1197 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001198 bm_get_bitmap2_creation_count());
1199 VG_(message)(Vg_UserMsg,
1200 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001201 get_mutex_lock_count());
1202 drd_print_malloc_stats();
1203 }
sewardjaf44c822007-11-25 14:01:38 +00001204}
1205
sewardjaf44c822007-11-25 14:01:38 +00001206static
1207void drd_pre_clo_init(void)
1208{
bart3772a982008-03-15 08:11:03 +00001209 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001210
bart3772a982008-03-15 08:11:03 +00001211 VG_(details_name) ("exp-drd");
1212 VG_(details_version) (NULL);
1213 VG_(details_description) ("a data race detector");
1214 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1215 " by Bart Van Assche.");
1216 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001217
bart3772a982008-03-15 08:11:03 +00001218 VG_(basic_tool_funcs) (drd_post_clo_init,
1219 drd_instrument,
1220 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001221
bart3772a982008-03-15 08:11:03 +00001222 // Command line stuff.
1223 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1224 drd_print_usage,
1225 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001226
bart3772a982008-03-15 08:11:03 +00001227 // Error handling.
1228 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001229
bart3772a982008-03-15 08:11:03 +00001230 // Core event tracking.
1231 VG_(track_pre_mem_read) (drd_pre_mem_read);
1232 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1233 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001234 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001235 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1236 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1237 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1238 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001239 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1240 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001241 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1242 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1243 VG_(track_start_client_code) (drd_start_client_code);
1244 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1245 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1246 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001247
bart3772a982008-03-15 08:11:03 +00001248 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001249 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001250 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001251
bart3772a982008-03-15 08:11:03 +00001252 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001253
bart3772a982008-03-15 08:11:03 +00001254 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001255
bart3772a982008-03-15 08:11:03 +00001256 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001257}
1258
1259
1260VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)