blob: e2de78bc38b061a4de1c1631659fd008caf17a77 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
sewardj85642922008-01-14 11:54:56 +000026#include "drd_barrier.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_clientreq.h"
bart4bb53d82008-02-28 19:06:34 +000028#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "drd_cond.h"
30#include "drd_error.h"
31#include "drd_malloc_wrappers.h"
32#include "drd_mutex.h"
bart5bd9f2d2008-03-03 20:31:58 +000033#include "drd_rwlock.h"
sewardjaf44c822007-11-25 14:01:38 +000034#include "drd_segment.h"
sewardj85642922008-01-14 11:54:56 +000035#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "drd_suppression.h"
37#include "drd_thread.h"
38#include "drd_track.h"
39#include "drd_vc.h"
sewardj721ad7b2007-11-30 08:30:29 +000040#include "priv_drd_clientreq.h"
sewardj85642922008-01-14 11:54:56 +000041#include "pub_drd_bitmap.h"
bart024a95a2008-04-01 18:27:41 +000042#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
sewardjaf44c822007-11-25 14:01:38 +000043#include "pub_tool_basics.h"
44#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
45#include "pub_tool_libcassert.h" // tl_assert()
46#include "pub_tool_libcbase.h" // VG_(strcmp)
47#include "pub_tool_libcprint.h" // VG_(printf)
48#include "pub_tool_libcproc.h"
49#include "pub_tool_machine.h"
bart024a95a2008-04-01 18:27:41 +000050#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardjaf44c822007-11-25 14:01:38 +000051#include "pub_tool_options.h" // command line options
bartceded212008-03-26 17:39:52 +000052#include "pub_tool_replacemalloc.h"
bart72b751c2008-03-01 13:44:24 +000053#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000054#include "pub_tool_tooliface.h"
55
56
sewardjaf44c822007-11-25 14:01:38 +000057// Function declarations.
58
59static void drd_start_client_code(const ThreadId tid, const ULong bbs_done);
sewardjaf44c822007-11-25 14:01:38 +000060
61
62// Local variables.
63
bart0ffa4832008-04-05 12:57:01 +000064static Bool s_drd_check_stack_var = False;
bartbd7e56e2008-03-31 18:14:12 +000065static Bool s_drd_print_stats = False;
66static Bool s_drd_trace_fork_join = False;
67static Bool s_drd_var_info = False;
68static Bool s_show_stack_usage = False;
sewardjaf44c822007-11-25 14:01:38 +000069
70
71//
72// Implement the needs_command_line_options for drd.
73//
74
75static Bool drd_process_cmd_line_option(Char* arg)
76{
bart9d5b7962008-05-14 12:25:00 +000077 int exclusive_threshold_ms = -1;
78 int segment_merging = -1;
79 int shared_threshold_ms = -1;
80 int show_confl_seg = -1;
81 int trace_barrier = -1;
82 int trace_clientobj = -1;
83 int trace_cond = -1;
84 int trace_csw = -1;
85 int trace_danger_set = -1;
86 int trace_mutex = -1;
87 int trace_rwlock = -1;
88 int trace_segment = -1;
89 int trace_semaphore = -1;
90 int trace_suppression = -1;
91 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +000092
bart9d5b7962008-05-14 12:25:00 +000093 VG_BOOL_CLO (arg, "--check-stack-var", s_drd_check_stack_var)
94 else VG_BOOL_CLO(arg, "--drd-stats", s_drd_print_stats)
95 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
96 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
97 else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage)
98 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
99 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
100 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
101 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
102 else VG_BOOL_CLO(arg, "--trace-danger-set", trace_danger_set)
103 else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join)
104 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
105 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
106 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
107 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
108 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
109 else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info)
110 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
111 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
112 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000113 else
bartceded212008-03-26 17:39:52 +0000114 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000115
bart9d5b7962008-05-14 12:25:00 +0000116 if (exclusive_threshold_ms != -1)
117 {
118 mutex_set_lock_threshold(exclusive_threshold_ms);
119 rwlock_set_exclusive_threshold(exclusive_threshold_ms);
120 }
121 if (shared_threshold_ms != -1)
122 {
123 rwlock_set_shared_threshold(shared_threshold_ms);
124 }
barta9c37392008-03-22 09:38:48 +0000125 if (segment_merging != -1)
126 thread_set_segment_merging(segment_merging);
127 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000128 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000129 if (trace_address)
130 {
bart005dc972008-03-29 14:42:59 +0000131 const Addr addr = VG_(strtoll16)(trace_address, 0);
132 drd_start_tracing_address_range(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000133 }
barta9c37392008-03-22 09:38:48 +0000134 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000135 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000136 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000137 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000138 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000139 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000140 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000141 thread_trace_context_switches(trace_csw);
barta9c37392008-03-22 09:38:48 +0000142 if (trace_danger_set != -1)
bart3772a982008-03-15 08:11:03 +0000143 thread_trace_danger_set(trace_danger_set);
barta9c37392008-03-22 09:38:48 +0000144 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000145 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000146 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000147 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000148 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000149 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000150 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000151 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000152 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000153 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000154
bart3772a982008-03-15 08:11:03 +0000155 return True;
sewardjaf44c822007-11-25 14:01:38 +0000156}
157
158static void drd_print_usage(void)
bartbd7e56e2008-03-31 18:14:12 +0000159{
160 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000161" --check-stack-var=yes|no Whether or not to report data races on\n"
162" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000163" --exclusive-threshold=<n> Print an error message if any mutex or\n"
164" writer lock is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000165" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000166" Segment merging is an algorithm to limit memory usage of the\n"
167" data race detection algorithm. Disabling segment merging may\n"
168" improve the accuracy of the so-called 'other segments' displayed\n"
169" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000170" --shared-threshold=<n> Print an error message if a reader lock\n"
171" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000172" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
173" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
174" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000175" stack variables when a race is reported on such a variable. This\n"
176" information is by default not displayed since for big programs\n"
177" reading in all debug information for all variables at once may\n"
178" cause an out of memory error [no].\n"
179"\n"
bart2cb48d62008-04-01 16:57:42 +0000180" exp-drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000181" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000182" specified address [off].\n"
183" --trace-barrier=yes|no Trace all barrier activity [no].\n"
184" --trace-cond=yes|no Trace all condition variable activity [no].\n"
185" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
186" --trace-mutex=yes|no Trace all mutex activity [no].\n"
187" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
188" --trace-segment=yes|no Trace segment actions [no].\n"
189" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000190 );
bart130463a2008-04-01 17:03:33 +0000191 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000192}
193
194static void drd_print_debug_usage(void)
195{
bart130463a2008-04-01 17:03:33 +0000196 VG_(printf)(
197" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
198" --trace-clientobj=yes|no Trace all client object activity [no].\n"
199" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
200" --trace-danger-set=yes|no Trace all danger set updates [no].\n"
201" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
202 );
203 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000204}
205
206
207//
208// Implements the thread-related core callbacks.
209//
210
barta79df6e2008-03-14 17:07:51 +0000211static void drd_trace_mem_access(const Addr addr, const SizeT size,
212 const BmAccessTypeT access_type)
213{
bart3772a982008-03-15 08:11:03 +0000214 char vc[80];
215 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
216 VG_(message)(Vg_UserMsg,
bart354009c2008-03-16 10:42:33 +0000217 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
bartd5765912008-03-16 08:40:55 +0000218 access_type == eLoad
219 ? "load "
220 : access_type == eStore
221 ? "store"
222 : access_type == eStart
223 ? "start"
224 : access_type == eEnd
bartd43f8d32008-03-16 17:29:20 +0000225 ? "end "
bartd5765912008-03-16 08:40:55 +0000226 : "????",
bart3772a982008-03-15 08:11:03 +0000227 addr,
228 size,
bart3772a982008-03-15 08:11:03 +0000229 VG_(get_running_tid)(),
230 thread_get_running_tid(),
231 vc);
232 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
233 VG_(clo_backtrace_size));
234 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
235 == VG_(get_running_tid)());
barta79df6e2008-03-14 17:07:51 +0000236}
237
238static void drd_report_race(const Addr addr, const SizeT size,
239 const BmAccessTypeT access_type)
240{
bart49c3a112008-03-15 10:28:36 +0000241 DataRaceErrInfo drei;
242
bart7a282142008-03-15 08:34:23 +0000243 if (drd_is_suppressed(addr, addr + size))
244 return;
245
bart354009c2008-03-16 10:42:33 +0000246 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000247 drei.addr = addr;
248 drei.size = size;
249 drei.access_type = access_type;
250 VG_(maybe_record_error)(VG_(get_running_tid)(),
251 DataRaceErr,
252 VG_(get_IP)(VG_(get_running_tid)()),
253 "Conflicting accesses",
254 &drei);
barta79df6e2008-03-14 17:07:51 +0000255}
256
257static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000258{
bart3772a982008-03-15 08:11:03 +0000259 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000260
bartf00a85b2008-03-13 18:49:23 +0000261#if 0
bart3772a982008-03-15 08:11:03 +0000262 /* The assert below has been commented out because of performance reasons.*/
263 tl_assert(thread_get_running_tid()
264 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000265#endif
sewardjaf44c822007-11-25 14:01:38 +0000266
bart3772a982008-03-15 08:11:03 +0000267 if (! running_thread_is_recording())
268 return;
bart0268dfa2008-03-11 20:10:21 +0000269
bart005dc972008-03-29 14:42:59 +0000270 if (range_any_is_traced(addr, size))
bart3772a982008-03-15 08:11:03 +0000271 {
272 drd_trace_mem_access(addr, size, eLoad);
273 }
274 sg = running_thread_get_segment();
275 bm_access_range_load(sg->bm, addr, addr + size);
bart7a282142008-03-15 08:34:23 +0000276 if (bm_load_has_conflict_with(thread_get_danger_set(), addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000277 {
278 drd_report_race(addr, size, eLoad);
279 }
barta79df6e2008-03-14 17:07:51 +0000280}
281
282static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
283{
bart3772a982008-03-15 08:11:03 +0000284 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000285
bart3772a982008-03-15 08:11:03 +0000286 if (! running_thread_is_recording())
287 return;
barta79df6e2008-03-14 17:07:51 +0000288
bart005dc972008-03-29 14:42:59 +0000289 if (range_any_is_traced(addr, 1))
bart3772a982008-03-15 08:11:03 +0000290 {
291 drd_trace_mem_access(addr, 1, eLoad);
292 }
293 sg = running_thread_get_segment();
294 bm_access_load_1(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000295 if (bm_load_1_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000296 {
297 drd_report_race(addr, 1, eLoad);
298 }
barta79df6e2008-03-14 17:07:51 +0000299}
300
301static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
302{
bart3772a982008-03-15 08:11:03 +0000303 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000304
bart3772a982008-03-15 08:11:03 +0000305 if (! running_thread_is_recording())
306 return;
barta79df6e2008-03-14 17:07:51 +0000307
bart005dc972008-03-29 14:42:59 +0000308 if (range_any_is_traced(addr, 2))
bart3772a982008-03-15 08:11:03 +0000309 {
310 drd_trace_mem_access(addr, 2, eLoad);
311 }
312 sg = running_thread_get_segment();
313 bm_access_load_2(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000314 if (bm_load_2_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000315 {
316 drd_report_race(addr, 2, eLoad);
317 }
barta79df6e2008-03-14 17:07:51 +0000318}
319
320static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
321{
bart3772a982008-03-15 08:11:03 +0000322 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000323
bart3772a982008-03-15 08:11:03 +0000324 if (! running_thread_is_recording())
325 return;
barta79df6e2008-03-14 17:07:51 +0000326
bart005dc972008-03-29 14:42:59 +0000327 if (range_any_is_traced(addr, 4))
bart3772a982008-03-15 08:11:03 +0000328 {
329 drd_trace_mem_access(addr, 4, eLoad);
330 }
331 sg = running_thread_get_segment();
332 bm_access_load_4(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000333 if (bm_load_4_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000334 {
335 drd_report_race(addr, 4, eLoad);
336 }
barta79df6e2008-03-14 17:07:51 +0000337}
338
339static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
340{
bart3772a982008-03-15 08:11:03 +0000341 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000342
bart3772a982008-03-15 08:11:03 +0000343 if (! running_thread_is_recording())
344 return;
barta79df6e2008-03-14 17:07:51 +0000345
bart005dc972008-03-29 14:42:59 +0000346 if (range_any_is_traced(addr, 8))
bart3772a982008-03-15 08:11:03 +0000347 {
348 drd_trace_mem_access(addr, 8, eLoad);
349 }
350 sg = running_thread_get_segment();
351 bm_access_load_8(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000352 if (bm_load_8_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000353 {
354 drd_report_race(addr, 8, eLoad);
355 }
sewardjaf44c822007-11-25 14:01:38 +0000356}
357
358static
359VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
360{
bart3772a982008-03-15 08:11:03 +0000361 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000362
bartf00a85b2008-03-13 18:49:23 +0000363#if 0
bart3772a982008-03-15 08:11:03 +0000364 /* The assert below has been commented out because of performance reasons.*/
365 tl_assert(thread_get_running_tid()
366 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000367#endif
sewardjaf44c822007-11-25 14:01:38 +0000368
bart3772a982008-03-15 08:11:03 +0000369 if (! running_thread_is_recording())
370 return;
bart0268dfa2008-03-11 20:10:21 +0000371
bart005dc972008-03-29 14:42:59 +0000372 if (range_any_is_traced(addr, size))
bart3772a982008-03-15 08:11:03 +0000373 {
374 drd_trace_mem_access(addr, size, eStore);
375 }
376 sg = running_thread_get_segment();
377 bm_access_range_store(sg->bm, addr, addr + size);
bart7a282142008-03-15 08:34:23 +0000378 if (bm_store_has_conflict_with(thread_get_danger_set(), addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000379 {
380 drd_report_race(addr, size, eStore);
381 }
barta79df6e2008-03-14 17:07:51 +0000382}
383
384static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
385{
bart3772a982008-03-15 08:11:03 +0000386 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000387
bart3772a982008-03-15 08:11:03 +0000388 if (! running_thread_is_recording())
389 return;
barta79df6e2008-03-14 17:07:51 +0000390
bart005dc972008-03-29 14:42:59 +0000391 if (range_any_is_traced(addr, 1))
bart3772a982008-03-15 08:11:03 +0000392 {
393 drd_trace_mem_access(addr, 1, eStore);
394 }
395 sg = running_thread_get_segment();
396 bm_access_store_1(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000397 if (bm_store_1_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000398 {
399 drd_report_race(addr, 1, eStore);
400 }
barta79df6e2008-03-14 17:07:51 +0000401}
402
403static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
404{
bart3772a982008-03-15 08:11:03 +0000405 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000406
bart3772a982008-03-15 08:11:03 +0000407 if (! running_thread_is_recording())
408 return;
barta79df6e2008-03-14 17:07:51 +0000409
bart005dc972008-03-29 14:42:59 +0000410 if (range_any_is_traced(addr, 2))
bart3772a982008-03-15 08:11:03 +0000411 {
412 drd_trace_mem_access(addr, 2, eStore);
413 }
414 sg = running_thread_get_segment();
415 bm_access_store_2(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000416 if (bm_store_2_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000417 {
418 drd_report_race(addr, 2, eStore);
419 }
barta79df6e2008-03-14 17:07:51 +0000420}
421
422static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
423{
bart3772a982008-03-15 08:11:03 +0000424 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000425
bart3772a982008-03-15 08:11:03 +0000426 if (! running_thread_is_recording())
427 return;
barta79df6e2008-03-14 17:07:51 +0000428
bart005dc972008-03-29 14:42:59 +0000429 if (range_any_is_traced(addr, 4))
bart3772a982008-03-15 08:11:03 +0000430 {
431 drd_trace_mem_access(addr, 4, eStore);
432 }
433 sg = running_thread_get_segment();
434 bm_access_store_4(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000435 if (bm_store_4_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000436 {
437 drd_report_race(addr, 4, eStore);
438 }
barta79df6e2008-03-14 17:07:51 +0000439}
440
441static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
442{
bart3772a982008-03-15 08:11:03 +0000443 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000444
bart3772a982008-03-15 08:11:03 +0000445 if (! running_thread_is_recording())
446 return;
barta79df6e2008-03-14 17:07:51 +0000447
bart005dc972008-03-29 14:42:59 +0000448 if (range_any_is_traced(addr, 8))
bart3772a982008-03-15 08:11:03 +0000449 {
450 drd_trace_mem_access(addr, 8, eStore);
451 }
452 sg = running_thread_get_segment();
453 bm_access_store_8(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000454 if (bm_store_8_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000455 {
456 drd_report_race(addr, 8, eStore);
457 }
sewardjaf44c822007-11-25 14:01:38 +0000458}
459
460static void drd_pre_mem_read(const CorePart part,
461 const ThreadId tid,
462 Char* const s,
463 const Addr a,
464 const SizeT size)
465{
bart3772a982008-03-15 08:11:03 +0000466 if (size > 0)
467 {
468 drd_trace_load(a, size);
469 }
sewardjaf44c822007-11-25 14:01:38 +0000470}
471
bart5e85d262008-03-01 10:49:37 +0000472static void drd_pre_mem_read_asciiz(const CorePart part,
473 const ThreadId tid,
474 Char* const s,
475 const Addr a)
476{
bart3772a982008-03-15 08:11:03 +0000477 const char* p = (void*)a;
478 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000479
bart3772a982008-03-15 08:11:03 +0000480 /* Note: the expression '*p' reads client memory and may crash if the */
481 /* client provided an invalid pointer ! */
482 while (*p)
483 {
484 p++;
485 size++;
486 }
487 // To do: find out what a reasonable upper limit on 'size' is.
488 tl_assert(size < 4096);
489 if (size > 0)
490 {
491 drd_trace_load(a, size);
492 }
bart5e85d262008-03-01 10:49:37 +0000493}
494
sewardjaf44c822007-11-25 14:01:38 +0000495static void drd_post_mem_write(const CorePart part,
496 const ThreadId tid,
497 const Addr a,
498 const SizeT size)
499{
bart3772a982008-03-15 08:11:03 +0000500 thread_set_vg_running_tid(VG_(get_running_tid)());
501 if (size > 0)
502 {
503 drd_trace_store(a, size);
504 }
sewardjaf44c822007-11-25 14:01:38 +0000505}
506
bart5e85d262008-03-01 10:49:37 +0000507static void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000508{
bart005dc972008-03-29 14:42:59 +0000509 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000510
bart005dc972008-03-29 14:42:59 +0000511 if (range_any_is_traced(a1, len))
bart3772a982008-03-15 08:11:03 +0000512 {
bartd5765912008-03-16 08:40:55 +0000513 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000514 }
sewardjaf44c822007-11-25 14:01:38 +0000515}
516
sewardj7cf4e6b2008-05-01 20:24:26 +0000517static void drd_start_using_mem_w_ecu(const Addr a1,
518 const SizeT len,
519 UInt ec_uniq)
520{
521 drd_start_using_mem(a1, len);
522}
523
524static void drd_start_using_mem_w_tid(const Addr a1,
525 const SizeT len,
526 ThreadId tid)
527{
528 drd_start_using_mem(a1, len);
529}
530
bart0ffa4832008-04-05 12:57:01 +0000531static __inline__
532void drd_stop_using_mem(const Addr a1, const SizeT len,
533 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000534{
bart3772a982008-03-15 08:11:03 +0000535 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000536
bart3772a982008-03-15 08:11:03 +0000537 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000538
bart005dc972008-03-29 14:42:59 +0000539 if (range_any_is_traced(a1, len))
bart3772a982008-03-15 08:11:03 +0000540 {
bartd43f8d32008-03-16 17:29:20 +0000541 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000542 }
bart0ffa4832008-04-05 12:57:01 +0000543 if (! is_stack_mem || s_drd_check_stack_var)
544 {
545 thread_stop_using_mem(a1, a2);
546 clientobj_stop_using_mem(a1, a2);
547 drd_suppression_stop_using_mem(a1, a2);
548 }
549}
550
551static __inline__
552void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
553{
554 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000555}
556
bartcb2d0072008-05-31 07:55:51 +0000557/** Suppress data race reports on all addresses contained in .plt and
558 * .got.plt sections inside the address range [ a, a + len [. The data in
559 * these sections is modified by _dl_relocate_object() every time a function
560 * in a shared library is called for the first time. Since the first call
561 * to a function in a shared library can happen from a multithreaded context,
562 * such calls can cause conflicting accesses. See also Ulrich Drepper's
563 * paper "How to Write Shared Libraries" for more information about relocation
564 * (http://people.redhat.com/drepper/dsohowto.pdf).
565 */
566static void suppress_relocation_conflicts(const Addr a, const SizeT len)
567{
568 const DebugInfo* di;
569
570#if 0
571 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
572#endif
573
574 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
575 {
576 Addr avma;
577 SizeT size;
578
579 avma = VG_(seginfo_get_plt_avma)(di);
580 size = VG_(seginfo_get_plt_size)(di);
581 if (a <= avma && avma + size <= a + len)
582 {
583#if 0
584 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
585#endif
586 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
587 drd_start_suppression(avma, avma + size, ".plt");
588 }
589
590 avma = VG_(seginfo_get_gotplt_avma)(di);
591 size = VG_(seginfo_get_gotplt_size)(di);
592 if (a <= avma && avma + size <= a + len)
593 {
594#if 0
595 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
596#endif
597 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
598 drd_start_suppression(avma, avma + size, ".gotplt");
599 }
600 }
601}
602
bart5e85d262008-03-01 10:49:37 +0000603static
604void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
605 const Bool rr, const Bool ww, const Bool xx)
606{
bartd5765912008-03-16 08:40:55 +0000607 thread_set_vg_running_tid(VG_(get_running_tid)());
608
bart3772a982008-03-15 08:11:03 +0000609 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000610
611 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000612}
613
sewardjaf44c822007-11-25 14:01:38 +0000614/* Called by the core when the stack of a thread grows, to indicate that */
615/* the addresses in range [ a, a + len [ may now be used by the client. */
616/* Assumption: stacks grow downward. */
617static void drd_start_using_mem_stack(const Addr a, const SizeT len)
618{
bartd43f8d32008-03-16 17:29:20 +0000619 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000620 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
621 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000622}
623
624/* Called by the core when the stack of a thread shrinks, to indicate that */
625/* the addresses [ a, a + len [ are no longer accessible for the client. */
626/* Assumption: stacks grow downward. */
627static void drd_stop_using_mem_stack(const Addr a, const SizeT len)
628{
bartd43f8d32008-03-16 17:29:20 +0000629 thread_set_stack_min(thread_get_running_tid(),
630 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000631 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
632 True);
sewardjaf44c822007-11-25 14:01:38 +0000633}
634
sewardj7cf4e6b2008-05-01 20:24:26 +0000635static void drd_start_using_mem_stack_signal(
636 const Addr a, const SizeT len,
637 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000638{
bartd5765912008-03-16 08:40:55 +0000639 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000640 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000641}
642
bart5e85d262008-03-01 10:49:37 +0000643static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000644{
bart0ffa4832008-04-05 12:57:01 +0000645 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000646}
647
648static
649void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
650{
bart3772a982008-03-15 08:11:03 +0000651 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
652 tl_assert(created != VG_INVALID_THREADID);
653 thread_pre_create(drd_creator, created);
654 if (IsValidDrdThreadId(drd_creator))
655 {
656 thread_new_segment(drd_creator);
657 }
bartbd7e56e2008-03-31 18:14:12 +0000658 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000659 {
660 VG_(message)(Vg_DebugMsg,
661 "drd_pre_thread_create creator = %d/%d, created = %d",
662 creator, drd_creator, created);
663 }
sewardjaf44c822007-11-25 14:01:38 +0000664}
665
666/* Called by Valgrind's core before any loads or stores are performed on */
667/* the context of thread "created". At startup, this function is called */
668/* with arguments (0,1). */
669static
bart0ffa4832008-04-05 12:57:01 +0000670void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000671{
bart0ffa4832008-04-05 12:57:01 +0000672 DrdThreadId drd_created;
673
674 tl_assert(vg_created != VG_INVALID_THREADID);
675
676 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000677 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000678 {
679 VG_(message)(Vg_DebugMsg,
680 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000681 vg_created, drd_created);
682 }
683 if (! s_drd_check_stack_var)
684 {
685 drd_start_suppression(thread_get_stack_max(drd_created)
686 - thread_get_stack_size(drd_created),
687 thread_get_stack_max(drd_created),
688 "stack");
bart3772a982008-03-15 08:11:03 +0000689 }
sewardjaf44c822007-11-25 14:01:38 +0000690}
691
692/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
693/* after thread drd_joiner joined thread drd_joinee. */
694void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
695{
bart3772a982008-03-15 08:11:03 +0000696 tl_assert(IsValidDrdThreadId(drd_joiner));
697 tl_assert(IsValidDrdThreadId(drd_joinee));
698 thread_new_segment(drd_joinee);
699 thread_combine_vc(drd_joiner, drd_joinee);
700 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000701
bartbd7e56e2008-03-31 18:14:12 +0000702 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000703 {
bartfdd8d4e2008-04-01 18:38:29 +0000704 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
705 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000706 const unsigned msg_size = 256;
707 char* msg;
708
709 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000710 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000711 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000712 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
713 joiner, drd_joiner, joinee, drd_joinee);
714 if (joiner)
715 {
bart024a95a2008-04-01 18:27:41 +0000716 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000717 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000718 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000719 thread_get_vc(drd_joiner));
720 }
721 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000722 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000723 }
sewardjaf44c822007-11-25 14:01:38 +0000724
bart0ffa4832008-04-05 12:57:01 +0000725 if (! s_drd_check_stack_var)
726 {
727 drd_finish_suppression(thread_get_stack_max(drd_joinee)
728 - thread_get_stack_size(drd_joinee),
729 thread_get_stack_max(drd_joinee));
730 }
bart3772a982008-03-15 08:11:03 +0000731 thread_delete(drd_joinee);
732 mutex_thread_delete(drd_joinee);
733 cond_thread_delete(drd_joinee);
734 semaphore_thread_delete(drd_joinee);
735 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000736}
737
bart5bd9f2d2008-03-03 20:31:58 +0000738
sewardjaf44c822007-11-25 14:01:38 +0000739/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000740static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000741{
bart3772a982008-03-15 08:11:03 +0000742 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000743
bartd43f8d32008-03-16 17:29:20 +0000744 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000745
bartd43f8d32008-03-16 17:29:20 +0000746 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000747 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000748 {
749 VG_(message)(Vg_DebugMsg,
750 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000751 vg_tid,
bart3772a982008-03-15 08:11:03 +0000752 drd_tid,
753 thread_get_joinable(drd_tid)
754 ? ""
755 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000756 }
757 if (s_show_stack_usage)
758 {
759 const SizeT stack_size = thread_get_stack_size(drd_tid);
760 const SizeT used_stack
761 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
762 VG_(message)(Vg_UserMsg,
763 "thread %d/%d%s finished and used %ld bytes out of %ld"
764 " on its stack. Margin: %ld bytes.",
765 vg_tid,
766 drd_tid,
767 thread_get_joinable(drd_tid)
768 ? ""
769 : " (which is a detached thread)",
770 used_stack,
771 stack_size,
772 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000773
bart3772a982008-03-15 08:11:03 +0000774 }
bartd43f8d32008-03-16 17:29:20 +0000775 drd_stop_using_mem(thread_get_stack_min(drd_tid),
776 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000777 - thread_get_stack_min(drd_tid),
778 True);
bartd43f8d32008-03-16 17:29:20 +0000779 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000780 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000781}
782
bart0268dfa2008-03-11 20:10:21 +0000783void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000784{
bart3772a982008-03-15 08:11:03 +0000785 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000786}
787
sewardj347eeba2008-01-21 14:19:07 +0000788void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000789{
bart3772a982008-03-15 08:11:03 +0000790 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000791}
792
bart2e3a3c12008-03-24 08:33:47 +0000793void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
794 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000795{
bart2e3a3c12008-03-24 08:33:47 +0000796 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000797}
798
bart00344642008-03-01 15:27:41 +0000799void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000800{
bart4a975e12008-03-30 13:28:33 +0000801 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000802}
803
bart00344642008-03-01 15:27:41 +0000804void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000805{
bart3772a982008-03-15 08:11:03 +0000806 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000807}
808
bart0268dfa2008-03-11 20:10:21 +0000809void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000810{
bart3772a982008-03-15 08:11:03 +0000811 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000812}
813
bart72b751c2008-03-01 13:44:24 +0000814void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000815{
bart3772a982008-03-15 08:11:03 +0000816 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000817}
818
bart0268dfa2008-03-11 20:10:21 +0000819void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000820 const Word pshared, const Word value)
821{
bart3772a982008-03-15 08:11:03 +0000822 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000823}
824
825void drd_semaphore_destroy(const Addr semaphore)
826{
bart3772a982008-03-15 08:11:03 +0000827 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000828}
829
bart0268dfa2008-03-11 20:10:21 +0000830void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000831{
bart3772a982008-03-15 08:11:03 +0000832 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000833}
834
835void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
836 const Bool waited)
837{
bart3772a982008-03-15 08:11:03 +0000838 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000839}
840
bart0268dfa2008-03-11 20:10:21 +0000841void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000842{
bart3772a982008-03-15 08:11:03 +0000843 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000844}
845
846void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000847 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000848{
bart3772a982008-03-15 08:11:03 +0000849 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000850}
851
852
bart0268dfa2008-03-11 20:10:21 +0000853void drd_barrier_init(const Addr barrier,
854 const BarrierT barrier_type, const Word count,
855 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000856{
bart3772a982008-03-15 08:11:03 +0000857 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000858}
859
bart0268dfa2008-03-11 20:10:21 +0000860void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000861{
bart3772a982008-03-15 08:11:03 +0000862 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000863}
864
bart0268dfa2008-03-11 20:10:21 +0000865void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
866 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000867{
bart3772a982008-03-15 08:11:03 +0000868 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000869}
870
871void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000872 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000873{
bart3772a982008-03-15 08:11:03 +0000874 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000875}
876
sewardjaf44c822007-11-25 14:01:38 +0000877
878//
879// Implementation of the tool interface.
880//
881
882static
883void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000884{
885# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000886 /* fine */
bartcb2d0072008-05-31 07:55:51 +0000887# elif defined(VGP_ppc64_linux) || defined(VGP_ppc64_linux)
888 VG_(printf)(
889"\nWARNING: support for PowerPC-specific atomic instructions like lwarx and\n"
890"stwcx is not yet complete. As a result, false positives will be reported on\n"
891"code that uses these instructions. This will happen e.g. when printf() is\n"
892"called from more than one thread.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000893# else
bart3772a982008-03-15 08:11:03 +0000894 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000895# endif
bart95761b52008-03-29 08:34:03 +0000896
897 if (s_drd_var_info)
898 {
899 VG_(needs_var_info)();
900 }
sewardjdcbb8d32007-11-26 21:34:30 +0000901}
sewardjaf44c822007-11-25 14:01:38 +0000902
barta79df6e2008-03-14 17:07:51 +0000903static void instrument_load(IRSB* const bb,
904 IRExpr* const addr_expr,
905 const HWord size)
906{
bart3772a982008-03-15 08:11:03 +0000907 IRExpr* size_expr;
908 IRExpr** argv;
909 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000910
bart3772a982008-03-15 08:11:03 +0000911 switch (size)
912 {
913 case 1:
914 argv = mkIRExprVec_1(addr_expr);
915 di = unsafeIRDirty_0_N(/*regparms*/1,
916 "drd_trace_load_1",
917 VG_(fnptr_to_fnentry)(drd_trace_load_1),
918 argv);
919 break;
920 case 2:
921 argv = mkIRExprVec_1(addr_expr);
922 di = unsafeIRDirty_0_N(/*regparms*/1,
923 "drd_trace_load_2",
924 VG_(fnptr_to_fnentry)(drd_trace_load_2),
925 argv);
926 break;
927 case 4:
928 argv = mkIRExprVec_1(addr_expr);
929 di = unsafeIRDirty_0_N(/*regparms*/1,
930 "drd_trace_load_4",
931 VG_(fnptr_to_fnentry)(drd_trace_load_4),
932 argv);
933 break;
934 case 8:
935 argv = mkIRExprVec_1(addr_expr);
936 di = unsafeIRDirty_0_N(/*regparms*/1,
937 "drd_trace_load_8",
938 VG_(fnptr_to_fnentry)(drd_trace_load_8),
939 argv);
940 break;
941 default:
942 size_expr = mkIRExpr_HWord(size);
943 argv = mkIRExprVec_2(addr_expr, size_expr);
944 di = unsafeIRDirty_0_N(/*regparms*/2,
945 "drd_trace_load",
946 VG_(fnptr_to_fnentry)(drd_trace_load),
947 argv);
948 break;
949 }
950 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000951}
952
953static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000954 IRExpr* const addr_expr,
955 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000956{
bart3772a982008-03-15 08:11:03 +0000957 IRExpr* size_expr;
958 IRExpr** argv;
959 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000960
bart3772a982008-03-15 08:11:03 +0000961 switch (size)
962 {
963 case 1:
964 argv = mkIRExprVec_1(addr_expr);
965 di = unsafeIRDirty_0_N(/*regparms*/1,
966 "drd_trace_store_1",
967 VG_(fnptr_to_fnentry)(drd_trace_store_1),
968 argv);
969 break;
970 case 2:
971 argv = mkIRExprVec_1(addr_expr);
972 di = unsafeIRDirty_0_N(/*regparms*/1,
973 "drd_trace_store_2",
974 VG_(fnptr_to_fnentry)(drd_trace_store_2),
975 argv);
976 break;
977 case 4:
978 argv = mkIRExprVec_1(addr_expr);
979 di = unsafeIRDirty_0_N(/*regparms*/1,
980 "drd_trace_store_4",
981 VG_(fnptr_to_fnentry)(drd_trace_store_4),
982 argv);
983 break;
984 case 8:
985 argv = mkIRExprVec_1(addr_expr);
986 di = unsafeIRDirty_0_N(/*regparms*/1,
987 "drd_trace_store_8",
988 VG_(fnptr_to_fnentry)(drd_trace_store_8),
989 argv);
990 break;
991 default:
992 size_expr = mkIRExpr_HWord(size);
993 argv = mkIRExprVec_2(addr_expr, size_expr);
994 di = unsafeIRDirty_0_N(/*regparms*/2,
995 "drd_trace_store",
996 VG_(fnptr_to_fnentry)(drd_trace_store),
997 argv);
998 break;
999 }
1000 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +00001001}
1002
sewardjaf44c822007-11-25 14:01:38 +00001003static
1004IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +00001005 IRSB* const bb_in,
1006 VexGuestLayout* const layout,
1007 VexGuestExtents* const vge,
1008 IRType const gWordTy,
1009 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +00001010{
bart3772a982008-03-15 08:11:03 +00001011 IRDirty* di;
1012 Int i;
1013 IRSB* bb;
1014 IRExpr** argv;
1015 Bool instrument = True;
1016 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +00001017
bart3772a982008-03-15 08:11:03 +00001018 /* Set up BB */
1019 bb = emptyIRSB();
1020 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
1021 bb->next = deepCopyIRExpr(bb_in->next);
1022 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +00001023
bart3772a982008-03-15 08:11:03 +00001024 for (i = 0; i < bb_in->stmts_used; i++)
1025 {
1026 IRStmt* const st = bb_in->stmts[i];
1027 tl_assert(st);
1028 if (st->tag == Ist_NoOp)
1029 continue;
sewardjaf44c822007-11-25 14:01:38 +00001030
bart3772a982008-03-15 08:11:03 +00001031 switch (st->tag)
1032 {
bart3772a982008-03-15 08:11:03 +00001033 case Ist_MBE:
1034 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001035 {
bart3772a982008-03-15 08:11:03 +00001036 case Imbe_Fence:
1037 break; /* not interesting */
1038 case Imbe_BusLock:
1039 tl_assert(! bus_locked);
1040 bus_locked = True;
1041 break;
1042 case Imbe_BusUnlock:
1043 tl_assert(bus_locked);
1044 bus_locked = False;
1045 break;
sewardjaf44c822007-11-25 14:01:38 +00001046 default:
bart3772a982008-03-15 08:11:03 +00001047 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001048 }
bart3772a982008-03-15 08:11:03 +00001049 addStmtToIRSB(bb, st);
1050 break;
sewardjaf44c822007-11-25 14:01:38 +00001051
bart3772a982008-03-15 08:11:03 +00001052 case Ist_Store:
1053 if (instrument && ! bus_locked)
1054 {
1055 instrument_store(bb,
1056 st->Ist.Store.addr,
1057 sizeofIRType(typeOfIRExpr(bb->tyenv,
1058 st->Ist.Store.data)));
1059 }
1060 addStmtToIRSB(bb, st);
1061 break;
barta47b3512008-03-07 17:22:26 +00001062
bart3772a982008-03-15 08:11:03 +00001063 case Ist_WrTmp:
1064 if (instrument)
1065 {
1066 const IRExpr* const data = st->Ist.WrTmp.data;
1067 if (data->tag == Iex_Load)
1068 {
1069 instrument_load(bb,
1070 data->Iex.Load.addr,
1071 sizeofIRType(data->Iex.Load.ty));
1072 }
1073 }
1074 addStmtToIRSB(bb, st);
1075 break;
1076
1077 case Ist_Dirty:
1078 if (instrument)
1079 {
1080 IRDirty* d = st->Ist.Dirty.details;
1081 IREffect const mFx = d->mFx;
1082 switch (mFx) {
1083 case Ifx_None:
1084 break;
1085 case Ifx_Read:
1086 case Ifx_Write:
1087 case Ifx_Modify:
1088 tl_assert(d->mAddr);
1089 tl_assert(d->mSize > 0);
1090 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1091 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1092 di = unsafeIRDirty_0_N(
1093 /*regparms*/2,
1094 "drd_trace_load",
1095 VG_(fnptr_to_fnentry)(drd_trace_load),
1096 argv);
1097 addStmtToIRSB(bb, IRStmt_Dirty(di));
1098 }
1099 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1100 && ! bus_locked)
1101 {
1102 di = unsafeIRDirty_0_N(
1103 /*regparms*/2,
1104 "drd_trace_store",
1105 VG_(fnptr_to_fnentry)(drd_trace_store),
1106 argv);
1107 addStmtToIRSB(bb, IRStmt_Dirty(di));
1108 }
1109 break;
1110 default:
1111 tl_assert(0);
1112 }
1113 }
1114 addStmtToIRSB(bb, st);
1115 break;
1116
1117 default:
1118 addStmtToIRSB(bb, st);
1119 break;
1120 }
1121 }
1122
1123 tl_assert(! bus_locked);
1124
1125 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001126}
1127
sewardjaf44c822007-11-25 14:01:38 +00001128static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1129{
bart3772a982008-03-15 08:11:03 +00001130 tl_assert(tid == VG_(get_running_tid)());
1131 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001132}
1133
1134static
1135void drd_fini(Int exitcode)
1136{
bart3772a982008-03-15 08:11:03 +00001137 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001138 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001139 {
barte4504dd2008-04-06 15:02:58 +00001140 ULong update_danger_set_count;
1141 ULong dsnsc;
1142 ULong dscvc;
1143
1144 update_danger_set_count
1145 = thread_get_update_danger_set_count(&dsnsc, &dscvc);
1146
bartbd7e56e2008-03-31 18:14:12 +00001147 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001148 " thread: %lld context switches"
barte4504dd2008-04-06 15:02:58 +00001149 " / %lld updates of the danger set",
bart3772a982008-03-15 08:11:03 +00001150 thread_get_context_switch_count(),
barte4504dd2008-04-06 15:02:58 +00001151 update_danger_set_count);
1152 VG_(message)(Vg_UserMsg,
1153 " (%lld new sg + %lld combine vc + %lld csw).",
1154 dsnsc,
1155 dscvc,
1156 update_danger_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001157 VG_(message)(Vg_UserMsg,
1158 " segments: created %lld segments, max %lld alive,"
1159 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001160 sg_get_created_segments_count(),
1161 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001162 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001163 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001164 " (%lld m, %lld rw, %lld s, %lld b)",
1165 get_mutex_segment_creation_count(),
1166 get_rwlock_segment_creation_count(),
1167 get_semaphore_segment_creation_count(),
1168 get_barrier_segment_creation_count());
1169 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001170 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001171 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001172 bm_get_bitmap2_node_creation_count());
1173 VG_(message)(Vg_UserMsg,
1174 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001175 bm_get_bitmap2_creation_count());
1176 VG_(message)(Vg_UserMsg,
1177 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001178 get_mutex_lock_count());
1179 drd_print_malloc_stats();
1180 }
sewardjaf44c822007-11-25 14:01:38 +00001181}
1182
sewardjaf44c822007-11-25 14:01:38 +00001183static
1184void drd_pre_clo_init(void)
1185{
bart3772a982008-03-15 08:11:03 +00001186 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001187
bart3772a982008-03-15 08:11:03 +00001188 VG_(details_name) ("exp-drd");
1189 VG_(details_version) (NULL);
1190 VG_(details_description) ("a data race detector");
1191 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1192 " by Bart Van Assche.");
1193 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001194
bart3772a982008-03-15 08:11:03 +00001195 VG_(basic_tool_funcs) (drd_post_clo_init,
1196 drd_instrument,
1197 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001198
bart3772a982008-03-15 08:11:03 +00001199 // Command line stuff.
1200 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1201 drd_print_usage,
1202 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001203
bart3772a982008-03-15 08:11:03 +00001204 // Error handling.
1205 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001206
bart3772a982008-03-15 08:11:03 +00001207 // Core event tracking.
1208 VG_(track_pre_mem_read) (drd_pre_mem_read);
1209 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1210 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001211 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001212 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1213 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1214 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1215 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001216 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1217 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001218 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1219 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1220 VG_(track_start_client_code) (drd_start_client_code);
1221 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1222 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1223 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001224
bart3772a982008-03-15 08:11:03 +00001225 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001226 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001227 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001228
bart3772a982008-03-15 08:11:03 +00001229 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001230
bart3772a982008-03-15 08:11:03 +00001231 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001232
bart3772a982008-03-15 08:11:03 +00001233 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001234}
1235
1236
1237VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)