blob: da89d8554f99462c3af1b5492e7dbf41e363884d [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)
106 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
107 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
108 else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage)
109 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
110 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
111 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
112 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
barte73b0aa2008-06-28 07:19:56 +0000113 else VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set)
bart9d5b7962008-05-14 12:25:00 +0000114 else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join)
115 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
116 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
117 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
118 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
119 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
120 else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info)
121 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
122 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
123 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000124 else
bartceded212008-03-26 17:39:52 +0000125 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000126
bart9d5b7962008-05-14 12:25:00 +0000127 if (exclusive_threshold_ms != -1)
128 {
129 mutex_set_lock_threshold(exclusive_threshold_ms);
130 rwlock_set_exclusive_threshold(exclusive_threshold_ms);
131 }
132 if (shared_threshold_ms != -1)
133 {
134 rwlock_set_shared_threshold(shared_threshold_ms);
135 }
barta9c37392008-03-22 09:38:48 +0000136 if (segment_merging != -1)
137 thread_set_segment_merging(segment_merging);
138 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000139 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000140 if (trace_address)
141 {
bart005dc972008-03-29 14:42:59 +0000142 const Addr addr = VG_(strtoll16)(trace_address, 0);
143 drd_start_tracing_address_range(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000144 }
barta9c37392008-03-22 09:38:48 +0000145 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000146 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000147 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000148 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000149 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000150 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000151 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000152 thread_trace_context_switches(trace_csw);
barte73b0aa2008-06-28 07:19:56 +0000153 if (trace_conflict_set != -1)
154 thread_trace_conflict_set(trace_conflict_set);
barta9c37392008-03-22 09:38:48 +0000155 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000156 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000157 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000158 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000159 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000160 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000161 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000162 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000163 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000164 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000165
bart3772a982008-03-15 08:11:03 +0000166 return True;
sewardjaf44c822007-11-25 14:01:38 +0000167}
168
169static void drd_print_usage(void)
bartbd7e56e2008-03-31 18:14:12 +0000170{
171 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000172" --check-stack-var=yes|no Whether or not to report data races on\n"
173" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000174" --exclusive-threshold=<n> Print an error message if any mutex or\n"
175" writer lock is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000176" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000177" Segment merging is an algorithm to limit memory usage of the\n"
178" data race detection algorithm. Disabling segment merging may\n"
179" improve the accuracy of the so-called 'other segments' displayed\n"
180" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000181" --shared-threshold=<n> Print an error message if a reader lock\n"
182" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000183" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
184" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
185" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000186" stack variables when a race is reported on such a variable. This\n"
187" information is by default not displayed since for big programs\n"
bart85d22532008-06-26 07:30:32 +0000188" reading in all debug information at once may cause an out of\n"
189" memory error [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000190"\n"
bart2cb48d62008-04-01 16:57:42 +0000191" exp-drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000192" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000193" specified address [off].\n"
194" --trace-barrier=yes|no Trace all barrier activity [no].\n"
195" --trace-cond=yes|no Trace all condition variable activity [no].\n"
196" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
197" --trace-mutex=yes|no Trace all mutex activity [no].\n"
198" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart130463a2008-04-01 17:03:33 +0000199" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000200 );
bart130463a2008-04-01 17:03:33 +0000201 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000202}
203
204static void drd_print_debug_usage(void)
205{
bart130463a2008-04-01 17:03:33 +0000206 VG_(printf)(
207" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
208" --trace-clientobj=yes|no Trace all client object activity [no].\n"
209" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000210" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart987781d2008-06-27 15:00:07 +0000211" --trace-segment=yes|no Trace segment actions [no].\n"
212" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bart130463a2008-04-01 17:03:33 +0000213 );
214 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000215}
216
217
218//
219// Implements the thread-related core callbacks.
220//
221
barta79df6e2008-03-14 17:07:51 +0000222static void drd_trace_mem_access(const Addr addr, const SizeT size,
223 const BmAccessTypeT access_type)
224{
bartb9c7d742008-06-10 12:51:51 +0000225 if (drd_is_any_traced(addr, addr + size))
226 {
227 char vc[80];
228 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
229 VG_(message)(Vg_UserMsg,
230 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
231 access_type == eLoad
232 ? "load "
233 : access_type == eStore
234 ? "store"
235 : access_type == eStart
236 ? "start"
237 : access_type == eEnd
238 ? "end "
239 : "????",
240 addr,
241 size,
242 VG_(get_running_tid)(),
243 thread_get_running_tid(),
244 vc);
245 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
246 VG_(clo_backtrace_size));
247 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
248 == VG_(get_running_tid)());
249 }
barta79df6e2008-03-14 17:07:51 +0000250}
251
bart29a0e2a2008-06-10 13:55:13 +0000252static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size)
253{
254 return drd_trace_mem_access(addr, size, eLoad);
255}
256
257static VG_REGPARM(2) void drd_trace_mem_store(const Addr addr,const SizeT size)
258{
259 return drd_trace_mem_access(addr, size, eStore);
260}
261
barta79df6e2008-03-14 17:07:51 +0000262static void drd_report_race(const Addr addr, const SizeT size,
263 const BmAccessTypeT access_type)
264{
bart49c3a112008-03-15 10:28:36 +0000265 DataRaceErrInfo drei;
266
bart354009c2008-03-16 10:42:33 +0000267 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000268 drei.addr = addr;
269 drei.size = size;
270 drei.access_type = access_type;
271 VG_(maybe_record_error)(VG_(get_running_tid)(),
272 DataRaceErr,
273 VG_(get_IP)(VG_(get_running_tid)()),
274 "Conflicting accesses",
275 &drei);
barta79df6e2008-03-14 17:07:51 +0000276}
277
278static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000279{
bart8b4b2ee2008-06-11 13:17:56 +0000280#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000281 /* The assert below has been commented out because of performance reasons.*/
282 tl_assert(thread_get_running_tid()
283 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000284#endif
sewardjaf44c822007-11-25 14:01:38 +0000285
bart0e5c04f2008-06-09 15:18:59 +0000286 if (running_thread_is_recording()
287 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000288 && bm_access_load_triggers_conflict(addr, addr + size)
289 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000290 {
291 drd_report_race(addr, size, eLoad);
292 }
barta79df6e2008-03-14 17:07:51 +0000293}
294
295static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
296{
bart0e5c04f2008-06-09 15:18:59 +0000297 if (running_thread_is_recording()
298 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000299 && bm_access_load_1_triggers_conflict(addr)
300 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000301 {
302 drd_report_race(addr, 1, eLoad);
303 }
barta79df6e2008-03-14 17:07:51 +0000304}
305
306static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
307{
bart0e5c04f2008-06-09 15:18:59 +0000308 if (running_thread_is_recording()
309 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000310 && bm_access_load_2_triggers_conflict(addr)
311 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000312 {
313 drd_report_race(addr, 2, eLoad);
314 }
barta79df6e2008-03-14 17:07:51 +0000315}
316
317static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
318{
bart0e5c04f2008-06-09 15:18:59 +0000319 if (running_thread_is_recording()
320 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000321 && bm_access_load_4_triggers_conflict(addr)
322 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000323 {
324 drd_report_race(addr, 4, eLoad);
325 }
barta79df6e2008-03-14 17:07:51 +0000326}
327
328static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
329{
bart0e5c04f2008-06-09 15:18:59 +0000330 if (running_thread_is_recording()
331 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000332 && bm_access_load_8_triggers_conflict(addr)
333 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000334 {
335 drd_report_race(addr, 8, eLoad);
336 }
sewardjaf44c822007-11-25 14:01:38 +0000337}
338
339static
340VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
341{
bart8b4b2ee2008-06-11 13:17:56 +0000342#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000343 /* The assert below has been commented out because of performance reasons.*/
344 tl_assert(thread_get_running_tid()
345 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000346#endif
sewardjaf44c822007-11-25 14:01:38 +0000347
bart0e5c04f2008-06-09 15:18:59 +0000348 if (running_thread_is_recording()
349 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000350 && bm_access_store_triggers_conflict(addr, addr + size)
351 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000352 {
353 drd_report_race(addr, size, eStore);
354 }
barta79df6e2008-03-14 17:07:51 +0000355}
356
357static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
358{
bart0e5c04f2008-06-09 15:18:59 +0000359 if (running_thread_is_recording()
360 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000361 && bm_access_store_1_triggers_conflict(addr)
362 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000363 {
364 drd_report_race(addr, 1, eStore);
365 }
barta79df6e2008-03-14 17:07:51 +0000366}
367
368static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
369{
bart0e5c04f2008-06-09 15:18:59 +0000370 if (running_thread_is_recording()
371 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000372 && bm_access_store_2_triggers_conflict(addr)
373 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000374 {
375 drd_report_race(addr, 2, eStore);
376 }
barta79df6e2008-03-14 17:07:51 +0000377}
378
379static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
380{
bart0e5c04f2008-06-09 15:18:59 +0000381 if (running_thread_is_recording()
382 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000383 && bm_access_store_4_triggers_conflict(addr)
384 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000385 {
386 drd_report_race(addr, 4, eStore);
387 }
barta79df6e2008-03-14 17:07:51 +0000388}
389
390static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
391{
bart0e5c04f2008-06-09 15:18:59 +0000392 if (running_thread_is_recording()
393 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000394 && bm_access_store_8_triggers_conflict(addr)
395 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000396 {
397 drd_report_race(addr, 8, eStore);
398 }
sewardjaf44c822007-11-25 14:01:38 +0000399}
400
401static void drd_pre_mem_read(const CorePart part,
402 const ThreadId tid,
403 Char* const s,
404 const Addr a,
405 const SizeT size)
406{
bart3772a982008-03-15 08:11:03 +0000407 if (size > 0)
408 {
409 drd_trace_load(a, size);
410 }
sewardjaf44c822007-11-25 14:01:38 +0000411}
412
bart5e85d262008-03-01 10:49:37 +0000413static void drd_pre_mem_read_asciiz(const CorePart part,
414 const ThreadId tid,
415 Char* const s,
416 const Addr a)
417{
bart3772a982008-03-15 08:11:03 +0000418 const char* p = (void*)a;
419 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000420
bart3772a982008-03-15 08:11:03 +0000421 /* Note: the expression '*p' reads client memory and may crash if the */
422 /* client provided an invalid pointer ! */
423 while (*p)
424 {
425 p++;
426 size++;
427 }
428 // To do: find out what a reasonable upper limit on 'size' is.
429 tl_assert(size < 4096);
430 if (size > 0)
431 {
432 drd_trace_load(a, size);
433 }
bart5e85d262008-03-01 10:49:37 +0000434}
435
sewardjaf44c822007-11-25 14:01:38 +0000436static void drd_post_mem_write(const CorePart part,
437 const ThreadId tid,
438 const Addr a,
439 const SizeT size)
440{
bart3772a982008-03-15 08:11:03 +0000441 thread_set_vg_running_tid(VG_(get_running_tid)());
442 if (size > 0)
443 {
444 drd_trace_store(a, size);
445 }
sewardjaf44c822007-11-25 14:01:38 +0000446}
447
bart08865622008-06-06 14:31:36 +0000448static __inline__
449void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000450{
bart005dc972008-03-29 14:42:59 +0000451 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000452
bartb9c7d742008-06-10 12:51:51 +0000453 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000454 {
bartd5765912008-03-16 08:40:55 +0000455 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000456 }
sewardjaf44c822007-11-25 14:01:38 +0000457}
458
sewardj7cf4e6b2008-05-01 20:24:26 +0000459static void drd_start_using_mem_w_ecu(const Addr a1,
460 const SizeT len,
461 UInt ec_uniq)
462{
463 drd_start_using_mem(a1, len);
464}
465
466static void drd_start_using_mem_w_tid(const Addr a1,
467 const SizeT len,
468 ThreadId tid)
469{
470 drd_start_using_mem(a1, len);
471}
472
bart0ffa4832008-04-05 12:57:01 +0000473static __inline__
474void drd_stop_using_mem(const Addr a1, const SizeT len,
475 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000476{
bart3772a982008-03-15 08:11:03 +0000477 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000478
bart3772a982008-03-15 08:11:03 +0000479 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000480
bartb9c7d742008-06-10 12:51:51 +0000481 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000482 {
bartd43f8d32008-03-16 17:29:20 +0000483 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000484 }
bart08865622008-06-06 14:31:36 +0000485 if (! is_stack_mem || s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000486 {
487 thread_stop_using_mem(a1, a2);
488 clientobj_stop_using_mem(a1, a2);
489 drd_suppression_stop_using_mem(a1, a2);
490 }
491}
492
493static __inline__
494void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
495{
496 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000497}
498
bartcb2d0072008-05-31 07:55:51 +0000499/** Suppress data race reports on all addresses contained in .plt and
500 * .got.plt sections inside the address range [ a, a + len [. The data in
501 * these sections is modified by _dl_relocate_object() every time a function
502 * in a shared library is called for the first time. Since the first call
503 * to a function in a shared library can happen from a multithreaded context,
504 * such calls can cause conflicting accesses. See also Ulrich Drepper's
505 * paper "How to Write Shared Libraries" for more information about relocation
506 * (http://people.redhat.com/drepper/dsohowto.pdf).
507 */
508static void suppress_relocation_conflicts(const Addr a, const SizeT len)
509{
510 const DebugInfo* di;
511
512#if 0
513 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
514#endif
515
516 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
517 {
518 Addr avma;
519 SizeT size;
520
521 avma = VG_(seginfo_get_plt_avma)(di);
522 size = VG_(seginfo_get_plt_size)(di);
523 if (a <= avma && avma + size <= a + len)
524 {
525#if 0
526 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
527#endif
528 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
529 drd_start_suppression(avma, avma + size, ".plt");
530 }
531
532 avma = VG_(seginfo_get_gotplt_avma)(di);
533 size = VG_(seginfo_get_gotplt_size)(di);
534 if (a <= avma && avma + size <= a + len)
535 {
536#if 0
537 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
538#endif
539 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
540 drd_start_suppression(avma, avma + size, ".gotplt");
541 }
542 }
543}
544
bart5e85d262008-03-01 10:49:37 +0000545static
546void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
547 const Bool rr, const Bool ww, const Bool xx)
548{
bartd5765912008-03-16 08:40:55 +0000549 thread_set_vg_running_tid(VG_(get_running_tid)());
550
bart3772a982008-03-15 08:11:03 +0000551 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000552
553 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000554}
555
sewardjaf44c822007-11-25 14:01:38 +0000556/* Called by the core when the stack of a thread grows, to indicate that */
557/* the addresses in range [ a, a + len [ may now be used by the client. */
558/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000559static __inline__
560void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000561{
bartd43f8d32008-03-16 17:29:20 +0000562 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000563 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
564 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000565}
566
567/* Called by the core when the stack of a thread shrinks, to indicate that */
568/* the addresses [ a, a + len [ are no longer accessible for the client. */
569/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000570static __inline__
571void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000572{
bartd43f8d32008-03-16 17:29:20 +0000573 thread_set_stack_min(thread_get_running_tid(),
574 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000575 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
576 True);
sewardjaf44c822007-11-25 14:01:38 +0000577}
578
sewardj7cf4e6b2008-05-01 20:24:26 +0000579static void drd_start_using_mem_stack_signal(
580 const Addr a, const SizeT len,
581 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000582{
bartd5765912008-03-16 08:40:55 +0000583 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000584 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000585}
586
bart5e85d262008-03-01 10:49:37 +0000587static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000588{
bart0ffa4832008-04-05 12:57:01 +0000589 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000590}
591
592static
593void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
594{
bart3772a982008-03-15 08:11:03 +0000595 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
596 tl_assert(created != VG_INVALID_THREADID);
597 thread_pre_create(drd_creator, created);
598 if (IsValidDrdThreadId(drd_creator))
599 {
600 thread_new_segment(drd_creator);
601 }
bartbd7e56e2008-03-31 18:14:12 +0000602 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000603 {
604 VG_(message)(Vg_DebugMsg,
605 "drd_pre_thread_create creator = %d/%d, created = %d",
606 creator, drd_creator, created);
607 }
sewardjaf44c822007-11-25 14:01:38 +0000608}
609
610/* Called by Valgrind's core before any loads or stores are performed on */
611/* the context of thread "created". At startup, this function is called */
612/* with arguments (0,1). */
613static
bart0ffa4832008-04-05 12:57:01 +0000614void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000615{
bart0ffa4832008-04-05 12:57:01 +0000616 DrdThreadId drd_created;
617
618 tl_assert(vg_created != VG_INVALID_THREADID);
619
620 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000621 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000622 {
623 VG_(message)(Vg_DebugMsg,
624 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000625 vg_created, drd_created);
626 }
bart08865622008-06-06 14:31:36 +0000627 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000628 {
629 drd_start_suppression(thread_get_stack_max(drd_created)
630 - thread_get_stack_size(drd_created),
631 thread_get_stack_max(drd_created),
632 "stack");
bart3772a982008-03-15 08:11:03 +0000633 }
sewardjaf44c822007-11-25 14:01:38 +0000634}
635
636/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
637/* after thread drd_joiner joined thread drd_joinee. */
638void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
639{
bart3772a982008-03-15 08:11:03 +0000640 tl_assert(IsValidDrdThreadId(drd_joiner));
641 tl_assert(IsValidDrdThreadId(drd_joinee));
642 thread_new_segment(drd_joinee);
643 thread_combine_vc(drd_joiner, drd_joinee);
644 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000645
bartbd7e56e2008-03-31 18:14:12 +0000646 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000647 {
bartfdd8d4e2008-04-01 18:38:29 +0000648 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
649 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000650 const unsigned msg_size = 256;
651 char* msg;
652
653 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000654 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000655 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000656 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
657 joiner, drd_joiner, joinee, drd_joinee);
658 if (joiner)
659 {
bart024a95a2008-04-01 18:27:41 +0000660 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000661 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000662 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000663 thread_get_vc(drd_joiner));
664 }
665 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000666 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000667 }
sewardjaf44c822007-11-25 14:01:38 +0000668
bart08865622008-06-06 14:31:36 +0000669 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000670 {
671 drd_finish_suppression(thread_get_stack_max(drd_joinee)
672 - thread_get_stack_size(drd_joinee),
673 thread_get_stack_max(drd_joinee));
674 }
bart3772a982008-03-15 08:11:03 +0000675 thread_delete(drd_joinee);
676 mutex_thread_delete(drd_joinee);
677 cond_thread_delete(drd_joinee);
678 semaphore_thread_delete(drd_joinee);
679 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000680}
681
bart5bd9f2d2008-03-03 20:31:58 +0000682
sewardjaf44c822007-11-25 14:01:38 +0000683/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000684static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000685{
bart3772a982008-03-15 08:11:03 +0000686 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000687
bartd43f8d32008-03-16 17:29:20 +0000688 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000689
bartd43f8d32008-03-16 17:29:20 +0000690 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000691 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000692 {
693 VG_(message)(Vg_DebugMsg,
694 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000695 vg_tid,
bart3772a982008-03-15 08:11:03 +0000696 drd_tid,
697 thread_get_joinable(drd_tid)
698 ? ""
699 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000700 }
701 if (s_show_stack_usage)
702 {
703 const SizeT stack_size = thread_get_stack_size(drd_tid);
704 const SizeT used_stack
705 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
706 VG_(message)(Vg_UserMsg,
707 "thread %d/%d%s finished and used %ld bytes out of %ld"
708 " on its stack. Margin: %ld bytes.",
709 vg_tid,
710 drd_tid,
711 thread_get_joinable(drd_tid)
712 ? ""
713 : " (which is a detached thread)",
714 used_stack,
715 stack_size,
716 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000717
bart3772a982008-03-15 08:11:03 +0000718 }
bartd43f8d32008-03-16 17:29:20 +0000719 drd_stop_using_mem(thread_get_stack_min(drd_tid),
720 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000721 - thread_get_stack_min(drd_tid),
722 True);
bartd43f8d32008-03-16 17:29:20 +0000723 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000724 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000725}
726
bart0268dfa2008-03-11 20:10:21 +0000727void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000728{
bart3772a982008-03-15 08:11:03 +0000729 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000730}
731
sewardj347eeba2008-01-21 14:19:07 +0000732void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000733{
bart3772a982008-03-15 08:11:03 +0000734 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000735}
736
bart2e3a3c12008-03-24 08:33:47 +0000737void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
738 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000739{
bart2e3a3c12008-03-24 08:33:47 +0000740 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000741}
742
bart00344642008-03-01 15:27:41 +0000743void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000744{
bart4a975e12008-03-30 13:28:33 +0000745 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000746}
747
bart00344642008-03-01 15:27:41 +0000748void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000749{
bart3772a982008-03-15 08:11:03 +0000750 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000751}
752
bart0268dfa2008-03-11 20:10:21 +0000753void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000754{
bart3772a982008-03-15 08:11:03 +0000755 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000756}
757
bart72b751c2008-03-01 13:44:24 +0000758void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000759{
bart3772a982008-03-15 08:11:03 +0000760 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000761}
762
bart0268dfa2008-03-11 20:10:21 +0000763void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000764 const Word pshared, const Word value)
765{
bart3772a982008-03-15 08:11:03 +0000766 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000767}
768
769void drd_semaphore_destroy(const Addr semaphore)
770{
bart3772a982008-03-15 08:11:03 +0000771 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000772}
773
bart0268dfa2008-03-11 20:10:21 +0000774void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000775{
bart3772a982008-03-15 08:11:03 +0000776 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000777}
778
779void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
780 const Bool waited)
781{
bart3772a982008-03-15 08:11:03 +0000782 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000783}
784
bart0268dfa2008-03-11 20:10:21 +0000785void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000786{
bart3772a982008-03-15 08:11:03 +0000787 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000788}
789
790void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000791 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000792{
bart3772a982008-03-15 08:11:03 +0000793 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000794}
795
796
bart0268dfa2008-03-11 20:10:21 +0000797void drd_barrier_init(const Addr barrier,
798 const BarrierT barrier_type, const Word count,
799 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000800{
bart3772a982008-03-15 08:11:03 +0000801 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000802}
803
bart0268dfa2008-03-11 20:10:21 +0000804void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000805{
bart3772a982008-03-15 08:11:03 +0000806 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000807}
808
bart0268dfa2008-03-11 20:10:21 +0000809void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
810 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000811{
bart3772a982008-03-15 08:11:03 +0000812 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000813}
814
815void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000816 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000817{
bart3772a982008-03-15 08:11:03 +0000818 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000819}
820
sewardjaf44c822007-11-25 14:01:38 +0000821
822//
823// Implementation of the tool interface.
824//
825
826static
827void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000828{
829# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000830 /* fine */
bartd5c63522008-06-01 07:33:14 +0000831# elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bartcb2d0072008-05-31 07:55:51 +0000832 VG_(printf)(
833"\nWARNING: support for PowerPC-specific atomic instructions like lwarx and\n"
834"stwcx is not yet complete. As a result, false positives will be reported on\n"
835"code that uses these instructions. This will happen e.g. when printf() is\n"
836"called from more than one thread.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000837# else
bart3772a982008-03-15 08:11:03 +0000838 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000839# endif
bart95761b52008-03-29 08:34:03 +0000840
841 if (s_drd_var_info)
842 {
843 VG_(needs_var_info)();
844 }
sewardjdcbb8d32007-11-26 21:34:30 +0000845}
sewardjaf44c822007-11-25 14:01:38 +0000846
barteacd9162008-06-16 20:22:18 +0000847#if defined(VGA_x86)
848#define STACK_POINTER_OFFSET OFFSET_x86_ESP
849#elif defined(VGA_amd64)
850#define STACK_POINTER_OFFSET OFFSET_amd64_RSP
851#elif defined(VGA_ppc32)
852#define STACK_POINTER_OFFSET ((OFFSET_ppc32_GPR0 + OFFSET_ppc32_GPR2) / 2)
853#elif defined(VGA_ppc64)
854#define STACK_POINTER_OFFSET ((OFFSET_ppc64_GPR0 + OFFSET_ppc64_GPR2) / 2)
855#else
856#error Unknown architecture.
857#endif
858
859
860/** Return true if and only if addr_expr matches the pattern (SP) or
861 * <offset>(SP).
862 */
863static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr)
864{
865 Bool result = False;
866
867 if (addr_expr->tag == Iex_RdTmp)
868 {
869 int i;
870 for (i = 0; i < bb->stmts_size; i++)
871 {
872 if (bb->stmts[i]
873 && bb->stmts[i]->tag == Ist_WrTmp
874 && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp)
875 {
876 IRExpr* e = bb->stmts[i]->Ist.WrTmp.data;
877 if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET)
878 {
879 result = True;
880 }
881
882 //ppIRExpr(e);
883 //VG_(printf)(" (%s)\n", result ? "True" : "False");
884 break;
885 }
886 }
887 }
888 return result;
889}
890
barta79df6e2008-03-14 17:07:51 +0000891static void instrument_load(IRSB* const bb,
892 IRExpr* const addr_expr,
893 const HWord size)
894{
bart3772a982008-03-15 08:11:03 +0000895 IRExpr* size_expr;
896 IRExpr** argv;
897 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000898
bart29a0e2a2008-06-10 13:55:13 +0000899 if (UNLIKELY(drd_any_address_is_traced()))
900 {
901 addStmtToIRSB(bb,
902 IRStmt_Dirty(
903 unsafeIRDirty_0_N(/*regparms*/2,
904 "drd_trace_load",
905 VG_(fnptr_to_fnentry)
906 (drd_trace_mem_load),
907 mkIRExprVec_2(addr_expr,
908 mkIRExpr_HWord(size)))));
909 }
910
barteacd9162008-06-16 20:22:18 +0000911 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
912 return;
913
bart3772a982008-03-15 08:11:03 +0000914 switch (size)
915 {
916 case 1:
917 argv = mkIRExprVec_1(addr_expr);
918 di = unsafeIRDirty_0_N(/*regparms*/1,
919 "drd_trace_load_1",
920 VG_(fnptr_to_fnentry)(drd_trace_load_1),
921 argv);
922 break;
923 case 2:
924 argv = mkIRExprVec_1(addr_expr);
925 di = unsafeIRDirty_0_N(/*regparms*/1,
926 "drd_trace_load_2",
927 VG_(fnptr_to_fnentry)(drd_trace_load_2),
928 argv);
929 break;
930 case 4:
931 argv = mkIRExprVec_1(addr_expr);
932 di = unsafeIRDirty_0_N(/*regparms*/1,
933 "drd_trace_load_4",
934 VG_(fnptr_to_fnentry)(drd_trace_load_4),
935 argv);
936 break;
937 case 8:
938 argv = mkIRExprVec_1(addr_expr);
939 di = unsafeIRDirty_0_N(/*regparms*/1,
940 "drd_trace_load_8",
941 VG_(fnptr_to_fnentry)(drd_trace_load_8),
942 argv);
943 break;
944 default:
945 size_expr = mkIRExpr_HWord(size);
946 argv = mkIRExprVec_2(addr_expr, size_expr);
947 di = unsafeIRDirty_0_N(/*regparms*/2,
948 "drd_trace_load",
949 VG_(fnptr_to_fnentry)(drd_trace_load),
950 argv);
951 break;
952 }
953 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000954}
955
956static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000957 IRExpr* const addr_expr,
958 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000959{
bart3772a982008-03-15 08:11:03 +0000960 IRExpr* size_expr;
961 IRExpr** argv;
962 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000963
bart29a0e2a2008-06-10 13:55:13 +0000964 if (UNLIKELY(drd_any_address_is_traced()))
965 {
966 addStmtToIRSB(bb,
967 IRStmt_Dirty(
968 unsafeIRDirty_0_N(/*regparms*/2,
969 "drd_trace_store",
970 VG_(fnptr_to_fnentry)
971 (drd_trace_mem_store),
972 mkIRExprVec_2(addr_expr,
973 mkIRExpr_HWord(size)))));
974 }
975
barteacd9162008-06-16 20:22:18 +0000976 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
977 return;
978
bart3772a982008-03-15 08:11:03 +0000979 switch (size)
980 {
981 case 1:
982 argv = mkIRExprVec_1(addr_expr);
983 di = unsafeIRDirty_0_N(/*regparms*/1,
984 "drd_trace_store_1",
985 VG_(fnptr_to_fnentry)(drd_trace_store_1),
986 argv);
987 break;
988 case 2:
989 argv = mkIRExprVec_1(addr_expr);
990 di = unsafeIRDirty_0_N(/*regparms*/1,
991 "drd_trace_store_2",
992 VG_(fnptr_to_fnentry)(drd_trace_store_2),
993 argv);
994 break;
995 case 4:
996 argv = mkIRExprVec_1(addr_expr);
997 di = unsafeIRDirty_0_N(/*regparms*/1,
998 "drd_trace_store_4",
999 VG_(fnptr_to_fnentry)(drd_trace_store_4),
1000 argv);
1001 break;
1002 case 8:
1003 argv = mkIRExprVec_1(addr_expr);
1004 di = unsafeIRDirty_0_N(/*regparms*/1,
1005 "drd_trace_store_8",
1006 VG_(fnptr_to_fnentry)(drd_trace_store_8),
1007 argv);
1008 break;
1009 default:
1010 size_expr = mkIRExpr_HWord(size);
1011 argv = mkIRExprVec_2(addr_expr, size_expr);
1012 di = unsafeIRDirty_0_N(/*regparms*/2,
1013 "drd_trace_store",
1014 VG_(fnptr_to_fnentry)(drd_trace_store),
1015 argv);
1016 break;
1017 }
1018 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +00001019}
1020
sewardjaf44c822007-11-25 14:01:38 +00001021static
1022IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +00001023 IRSB* const bb_in,
1024 VexGuestLayout* const layout,
1025 VexGuestExtents* const vge,
1026 IRType const gWordTy,
1027 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +00001028{
bart3772a982008-03-15 08:11:03 +00001029 IRDirty* di;
1030 Int i;
1031 IRSB* bb;
1032 IRExpr** argv;
1033 Bool instrument = True;
1034 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +00001035
bart3772a982008-03-15 08:11:03 +00001036 /* Set up BB */
1037 bb = emptyIRSB();
1038 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
1039 bb->next = deepCopyIRExpr(bb_in->next);
1040 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +00001041
bart3772a982008-03-15 08:11:03 +00001042 for (i = 0; i < bb_in->stmts_used; i++)
1043 {
1044 IRStmt* const st = bb_in->stmts[i];
1045 tl_assert(st);
1046 if (st->tag == Ist_NoOp)
1047 continue;
sewardjaf44c822007-11-25 14:01:38 +00001048
bart3772a982008-03-15 08:11:03 +00001049 switch (st->tag)
1050 {
bart3772a982008-03-15 08:11:03 +00001051 case Ist_MBE:
1052 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001053 {
bart3772a982008-03-15 08:11:03 +00001054 case Imbe_Fence:
1055 break; /* not interesting */
1056 case Imbe_BusLock:
1057 tl_assert(! bus_locked);
1058 bus_locked = True;
1059 break;
1060 case Imbe_BusUnlock:
1061 tl_assert(bus_locked);
1062 bus_locked = False;
1063 break;
sewardjaf44c822007-11-25 14:01:38 +00001064 default:
bart3772a982008-03-15 08:11:03 +00001065 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001066 }
bart3772a982008-03-15 08:11:03 +00001067 addStmtToIRSB(bb, st);
1068 break;
sewardjaf44c822007-11-25 14:01:38 +00001069
bart3772a982008-03-15 08:11:03 +00001070 case Ist_Store:
1071 if (instrument && ! bus_locked)
1072 {
1073 instrument_store(bb,
1074 st->Ist.Store.addr,
1075 sizeofIRType(typeOfIRExpr(bb->tyenv,
1076 st->Ist.Store.data)));
1077 }
1078 addStmtToIRSB(bb, st);
1079 break;
barta47b3512008-03-07 17:22:26 +00001080
bart3772a982008-03-15 08:11:03 +00001081 case Ist_WrTmp:
1082 if (instrument)
1083 {
1084 const IRExpr* const data = st->Ist.WrTmp.data;
1085 if (data->tag == Iex_Load)
1086 {
1087 instrument_load(bb,
1088 data->Iex.Load.addr,
1089 sizeofIRType(data->Iex.Load.ty));
1090 }
1091 }
1092 addStmtToIRSB(bb, st);
1093 break;
1094
1095 case Ist_Dirty:
1096 if (instrument)
1097 {
1098 IRDirty* d = st->Ist.Dirty.details;
1099 IREffect const mFx = d->mFx;
1100 switch (mFx) {
1101 case Ifx_None:
1102 break;
1103 case Ifx_Read:
1104 case Ifx_Write:
1105 case Ifx_Modify:
1106 tl_assert(d->mAddr);
1107 tl_assert(d->mSize > 0);
1108 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1109 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1110 di = unsafeIRDirty_0_N(
1111 /*regparms*/2,
1112 "drd_trace_load",
1113 VG_(fnptr_to_fnentry)(drd_trace_load),
1114 argv);
1115 addStmtToIRSB(bb, IRStmt_Dirty(di));
1116 }
1117 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1118 && ! bus_locked)
1119 {
1120 di = unsafeIRDirty_0_N(
1121 /*regparms*/2,
1122 "drd_trace_store",
1123 VG_(fnptr_to_fnentry)(drd_trace_store),
1124 argv);
1125 addStmtToIRSB(bb, IRStmt_Dirty(di));
1126 }
1127 break;
1128 default:
1129 tl_assert(0);
1130 }
1131 }
1132 addStmtToIRSB(bb, st);
1133 break;
1134
1135 default:
1136 addStmtToIRSB(bb, st);
1137 break;
1138 }
1139 }
1140
1141 tl_assert(! bus_locked);
1142
1143 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001144}
1145
sewardjaf44c822007-11-25 14:01:38 +00001146static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1147{
bart3772a982008-03-15 08:11:03 +00001148 tl_assert(tid == VG_(get_running_tid)());
1149 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001150}
1151
1152static
1153void drd_fini(Int exitcode)
1154{
bart3772a982008-03-15 08:11:03 +00001155 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001156 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001157 {
barte73b0aa2008-06-28 07:19:56 +00001158 ULong update_conflict_set_count;
barte4504dd2008-04-06 15:02:58 +00001159 ULong dsnsc;
1160 ULong dscvc;
1161
barte73b0aa2008-06-28 07:19:56 +00001162 update_conflict_set_count
1163 = thread_get_update_conflict_set_count(&dsnsc, &dscvc);
barte4504dd2008-04-06 15:02:58 +00001164
bartbd7e56e2008-03-31 18:14:12 +00001165 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001166 " thread: %lld context switches"
barte73b0aa2008-06-28 07:19:56 +00001167 " / %lld updates of the conflict set",
bart3772a982008-03-15 08:11:03 +00001168 thread_get_context_switch_count(),
barte73b0aa2008-06-28 07:19:56 +00001169 update_conflict_set_count);
barte4504dd2008-04-06 15:02:58 +00001170 VG_(message)(Vg_UserMsg,
1171 " (%lld new sg + %lld combine vc + %lld csw).",
1172 dsnsc,
1173 dscvc,
barte73b0aa2008-06-28 07:19:56 +00001174 update_conflict_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001175 VG_(message)(Vg_UserMsg,
1176 " segments: created %lld segments, max %lld alive,"
1177 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001178 sg_get_created_segments_count(),
1179 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001180 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001181 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001182 " (%lld m, %lld rw, %lld s, %lld b)",
1183 get_mutex_segment_creation_count(),
1184 get_rwlock_segment_creation_count(),
1185 get_semaphore_segment_creation_count(),
1186 get_barrier_segment_creation_count());
1187 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001188 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001189 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001190 bm_get_bitmap2_node_creation_count());
1191 VG_(message)(Vg_UserMsg,
1192 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001193 bm_get_bitmap2_creation_count());
1194 VG_(message)(Vg_UserMsg,
1195 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001196 get_mutex_lock_count());
1197 drd_print_malloc_stats();
1198 }
sewardjaf44c822007-11-25 14:01:38 +00001199}
1200
sewardjaf44c822007-11-25 14:01:38 +00001201static
1202void drd_pre_clo_init(void)
1203{
bart3772a982008-03-15 08:11:03 +00001204 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001205
bart3772a982008-03-15 08:11:03 +00001206 VG_(details_name) ("exp-drd");
1207 VG_(details_version) (NULL);
1208 VG_(details_description) ("a data race detector");
1209 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1210 " by Bart Van Assche.");
1211 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001212
bart3772a982008-03-15 08:11:03 +00001213 VG_(basic_tool_funcs) (drd_post_clo_init,
1214 drd_instrument,
1215 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001216
bart3772a982008-03-15 08:11:03 +00001217 // Command line stuff.
1218 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1219 drd_print_usage,
1220 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001221
bart3772a982008-03-15 08:11:03 +00001222 // Error handling.
1223 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001224
bart3772a982008-03-15 08:11:03 +00001225 // Core event tracking.
1226 VG_(track_pre_mem_read) (drd_pre_mem_read);
1227 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1228 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001229 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001230 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1231 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1232 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1233 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001234 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1235 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001236 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1237 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1238 VG_(track_start_client_code) (drd_start_client_code);
1239 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1240 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1241 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001242
bart3772a982008-03-15 08:11:03 +00001243 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001244 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001245 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001246
bart3772a982008-03-15 08:11:03 +00001247 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001248
bart3772a982008-03-15 08:11:03 +00001249 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001250
bart3772a982008-03-15 08:11:03 +00001251 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001252}
1253
1254
1255VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)