blob: 5ff2aace87807990efbb78b6158ca059186eec25 [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
bart5e85d262008-03-01 10:49:37 +0000557static
558void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
559 const Bool rr, const Bool ww, const Bool xx)
560{
bartd5765912008-03-16 08:40:55 +0000561 thread_set_vg_running_tid(VG_(get_running_tid)());
562
bart3772a982008-03-15 08:11:03 +0000563 drd_start_using_mem(a, len);
bart5e85d262008-03-01 10:49:37 +0000564}
565
sewardjaf44c822007-11-25 14:01:38 +0000566/* Called by the core when the stack of a thread grows, to indicate that */
567/* the addresses in range [ a, a + len [ may now be used by the client. */
568/* Assumption: stacks grow downward. */
569static void drd_start_using_mem_stack(const Addr a, const SizeT len)
570{
bartd43f8d32008-03-16 17:29:20 +0000571 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000572 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
573 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000574}
575
576/* Called by the core when the stack of a thread shrinks, to indicate that */
577/* the addresses [ a, a + len [ are no longer accessible for the client. */
578/* Assumption: stacks grow downward. */
579static void drd_stop_using_mem_stack(const Addr a, const SizeT len)
580{
bartd43f8d32008-03-16 17:29:20 +0000581 thread_set_stack_min(thread_get_running_tid(),
582 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000583 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
584 True);
sewardjaf44c822007-11-25 14:01:38 +0000585}
586
sewardj7cf4e6b2008-05-01 20:24:26 +0000587static void drd_start_using_mem_stack_signal(
588 const Addr a, const SizeT len,
589 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000590{
bartd5765912008-03-16 08:40:55 +0000591 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000592 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000593}
594
bart5e85d262008-03-01 10:49:37 +0000595static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000596{
bart0ffa4832008-04-05 12:57:01 +0000597 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000598}
599
600static
601void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
602{
bart3772a982008-03-15 08:11:03 +0000603 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
604 tl_assert(created != VG_INVALID_THREADID);
605 thread_pre_create(drd_creator, created);
606 if (IsValidDrdThreadId(drd_creator))
607 {
608 thread_new_segment(drd_creator);
609 }
bartbd7e56e2008-03-31 18:14:12 +0000610 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000611 {
612 VG_(message)(Vg_DebugMsg,
613 "drd_pre_thread_create creator = %d/%d, created = %d",
614 creator, drd_creator, created);
615 }
sewardjaf44c822007-11-25 14:01:38 +0000616}
617
618/* Called by Valgrind's core before any loads or stores are performed on */
619/* the context of thread "created". At startup, this function is called */
620/* with arguments (0,1). */
621static
bart0ffa4832008-04-05 12:57:01 +0000622void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000623{
bart0ffa4832008-04-05 12:57:01 +0000624 DrdThreadId drd_created;
625
626 tl_assert(vg_created != VG_INVALID_THREADID);
627
628 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000629 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000630 {
631 VG_(message)(Vg_DebugMsg,
632 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000633 vg_created, drd_created);
634 }
635 if (! s_drd_check_stack_var)
636 {
637 drd_start_suppression(thread_get_stack_max(drd_created)
638 - thread_get_stack_size(drd_created),
639 thread_get_stack_max(drd_created),
640 "stack");
bart3772a982008-03-15 08:11:03 +0000641 }
sewardjaf44c822007-11-25 14:01:38 +0000642}
643
644/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
645/* after thread drd_joiner joined thread drd_joinee. */
646void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
647{
bart3772a982008-03-15 08:11:03 +0000648 tl_assert(IsValidDrdThreadId(drd_joiner));
649 tl_assert(IsValidDrdThreadId(drd_joinee));
650 thread_new_segment(drd_joinee);
651 thread_combine_vc(drd_joiner, drd_joinee);
652 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000653
bartbd7e56e2008-03-31 18:14:12 +0000654 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000655 {
bartfdd8d4e2008-04-01 18:38:29 +0000656 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
657 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000658 const unsigned msg_size = 256;
659 char* msg;
660
661 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000662 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000663 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000664 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
665 joiner, drd_joiner, joinee, drd_joinee);
666 if (joiner)
667 {
bart024a95a2008-04-01 18:27:41 +0000668 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000669 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000670 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000671 thread_get_vc(drd_joiner));
672 }
673 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000674 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000675 }
sewardjaf44c822007-11-25 14:01:38 +0000676
bart0ffa4832008-04-05 12:57:01 +0000677 if (! s_drd_check_stack_var)
678 {
679 drd_finish_suppression(thread_get_stack_max(drd_joinee)
680 - thread_get_stack_size(drd_joinee),
681 thread_get_stack_max(drd_joinee));
682 }
bart3772a982008-03-15 08:11:03 +0000683 thread_delete(drd_joinee);
684 mutex_thread_delete(drd_joinee);
685 cond_thread_delete(drd_joinee);
686 semaphore_thread_delete(drd_joinee);
687 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000688}
689
bart5bd9f2d2008-03-03 20:31:58 +0000690
sewardjaf44c822007-11-25 14:01:38 +0000691/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000692static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000693{
bart3772a982008-03-15 08:11:03 +0000694 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000695
bartd43f8d32008-03-16 17:29:20 +0000696 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000697
bartd43f8d32008-03-16 17:29:20 +0000698 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000699 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000700 {
701 VG_(message)(Vg_DebugMsg,
702 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000703 vg_tid,
bart3772a982008-03-15 08:11:03 +0000704 drd_tid,
705 thread_get_joinable(drd_tid)
706 ? ""
707 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000708 }
709 if (s_show_stack_usage)
710 {
711 const SizeT stack_size = thread_get_stack_size(drd_tid);
712 const SizeT used_stack
713 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
714 VG_(message)(Vg_UserMsg,
715 "thread %d/%d%s finished and used %ld bytes out of %ld"
716 " on its stack. Margin: %ld bytes.",
717 vg_tid,
718 drd_tid,
719 thread_get_joinable(drd_tid)
720 ? ""
721 : " (which is a detached thread)",
722 used_stack,
723 stack_size,
724 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000725
bart3772a982008-03-15 08:11:03 +0000726 }
bartd43f8d32008-03-16 17:29:20 +0000727 drd_stop_using_mem(thread_get_stack_min(drd_tid),
728 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000729 - thread_get_stack_min(drd_tid),
730 True);
bartd43f8d32008-03-16 17:29:20 +0000731 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000732 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000733}
734
bart0268dfa2008-03-11 20:10:21 +0000735void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000736{
bart3772a982008-03-15 08:11:03 +0000737 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000738}
739
sewardj347eeba2008-01-21 14:19:07 +0000740void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000741{
bart3772a982008-03-15 08:11:03 +0000742 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000743}
744
bart2e3a3c12008-03-24 08:33:47 +0000745void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
746 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000747{
bart2e3a3c12008-03-24 08:33:47 +0000748 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000749}
750
bart00344642008-03-01 15:27:41 +0000751void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000752{
bart4a975e12008-03-30 13:28:33 +0000753 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000754}
755
bart00344642008-03-01 15:27:41 +0000756void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000757{
bart3772a982008-03-15 08:11:03 +0000758 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000759}
760
bart0268dfa2008-03-11 20:10:21 +0000761void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000762{
bart3772a982008-03-15 08:11:03 +0000763 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000764}
765
bart72b751c2008-03-01 13:44:24 +0000766void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000767{
bart3772a982008-03-15 08:11:03 +0000768 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000769}
770
bart0268dfa2008-03-11 20:10:21 +0000771void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000772 const Word pshared, const Word value)
773{
bart3772a982008-03-15 08:11:03 +0000774 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000775}
776
777void drd_semaphore_destroy(const Addr semaphore)
778{
bart3772a982008-03-15 08:11:03 +0000779 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000780}
781
bart0268dfa2008-03-11 20:10:21 +0000782void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000783{
bart3772a982008-03-15 08:11:03 +0000784 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000785}
786
787void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
788 const Bool waited)
789{
bart3772a982008-03-15 08:11:03 +0000790 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000791}
792
bart0268dfa2008-03-11 20:10:21 +0000793void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000794{
bart3772a982008-03-15 08:11:03 +0000795 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000796}
797
798void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000799 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000800{
bart3772a982008-03-15 08:11:03 +0000801 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000802}
803
804
bart0268dfa2008-03-11 20:10:21 +0000805void drd_barrier_init(const Addr barrier,
806 const BarrierT barrier_type, const Word count,
807 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000808{
bart3772a982008-03-15 08:11:03 +0000809 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000810}
811
bart0268dfa2008-03-11 20:10:21 +0000812void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000813{
bart3772a982008-03-15 08:11:03 +0000814 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000815}
816
bart0268dfa2008-03-11 20:10:21 +0000817void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
818 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000819{
bart3772a982008-03-15 08:11:03 +0000820 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000821}
822
823void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000824 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000825{
bart3772a982008-03-15 08:11:03 +0000826 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000827}
828
sewardjaf44c822007-11-25 14:01:38 +0000829
830//
831// Implementation of the tool interface.
832//
833
834static
835void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000836{
837# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000838 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000839# else
bart3772a982008-03-15 08:11:03 +0000840 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000841# endif
bart95761b52008-03-29 08:34:03 +0000842
843 if (s_drd_var_info)
844 {
845 VG_(needs_var_info)();
846 }
sewardjdcbb8d32007-11-26 21:34:30 +0000847}
sewardjaf44c822007-11-25 14:01:38 +0000848
barta79df6e2008-03-14 17:07:51 +0000849static void instrument_load(IRSB* const bb,
850 IRExpr* const addr_expr,
851 const HWord size)
852{
bart3772a982008-03-15 08:11:03 +0000853 IRExpr* size_expr;
854 IRExpr** argv;
855 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000856
bart3772a982008-03-15 08:11:03 +0000857 switch (size)
858 {
859 case 1:
860 argv = mkIRExprVec_1(addr_expr);
861 di = unsafeIRDirty_0_N(/*regparms*/1,
862 "drd_trace_load_1",
863 VG_(fnptr_to_fnentry)(drd_trace_load_1),
864 argv);
865 break;
866 case 2:
867 argv = mkIRExprVec_1(addr_expr);
868 di = unsafeIRDirty_0_N(/*regparms*/1,
869 "drd_trace_load_2",
870 VG_(fnptr_to_fnentry)(drd_trace_load_2),
871 argv);
872 break;
873 case 4:
874 argv = mkIRExprVec_1(addr_expr);
875 di = unsafeIRDirty_0_N(/*regparms*/1,
876 "drd_trace_load_4",
877 VG_(fnptr_to_fnentry)(drd_trace_load_4),
878 argv);
879 break;
880 case 8:
881 argv = mkIRExprVec_1(addr_expr);
882 di = unsafeIRDirty_0_N(/*regparms*/1,
883 "drd_trace_load_8",
884 VG_(fnptr_to_fnentry)(drd_trace_load_8),
885 argv);
886 break;
887 default:
888 size_expr = mkIRExpr_HWord(size);
889 argv = mkIRExprVec_2(addr_expr, size_expr);
890 di = unsafeIRDirty_0_N(/*regparms*/2,
891 "drd_trace_load",
892 VG_(fnptr_to_fnentry)(drd_trace_load),
893 argv);
894 break;
895 }
896 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000897}
898
899static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000900 IRExpr* const addr_expr,
901 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000902{
bart3772a982008-03-15 08:11:03 +0000903 IRExpr* size_expr;
904 IRExpr** argv;
905 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000906
bart3772a982008-03-15 08:11:03 +0000907 switch (size)
908 {
909 case 1:
910 argv = mkIRExprVec_1(addr_expr);
911 di = unsafeIRDirty_0_N(/*regparms*/1,
912 "drd_trace_store_1",
913 VG_(fnptr_to_fnentry)(drd_trace_store_1),
914 argv);
915 break;
916 case 2:
917 argv = mkIRExprVec_1(addr_expr);
918 di = unsafeIRDirty_0_N(/*regparms*/1,
919 "drd_trace_store_2",
920 VG_(fnptr_to_fnentry)(drd_trace_store_2),
921 argv);
922 break;
923 case 4:
924 argv = mkIRExprVec_1(addr_expr);
925 di = unsafeIRDirty_0_N(/*regparms*/1,
926 "drd_trace_store_4",
927 VG_(fnptr_to_fnentry)(drd_trace_store_4),
928 argv);
929 break;
930 case 8:
931 argv = mkIRExprVec_1(addr_expr);
932 di = unsafeIRDirty_0_N(/*regparms*/1,
933 "drd_trace_store_8",
934 VG_(fnptr_to_fnentry)(drd_trace_store_8),
935 argv);
936 break;
937 default:
938 size_expr = mkIRExpr_HWord(size);
939 argv = mkIRExprVec_2(addr_expr, size_expr);
940 di = unsafeIRDirty_0_N(/*regparms*/2,
941 "drd_trace_store",
942 VG_(fnptr_to_fnentry)(drd_trace_store),
943 argv);
944 break;
945 }
946 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000947}
948
sewardjaf44c822007-11-25 14:01:38 +0000949static
950IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +0000951 IRSB* const bb_in,
952 VexGuestLayout* const layout,
953 VexGuestExtents* const vge,
954 IRType const gWordTy,
955 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +0000956{
bart3772a982008-03-15 08:11:03 +0000957 IRDirty* di;
958 Int i;
959 IRSB* bb;
960 IRExpr** argv;
961 Bool instrument = True;
962 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +0000963
bart3772a982008-03-15 08:11:03 +0000964 /* Set up BB */
965 bb = emptyIRSB();
966 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
967 bb->next = deepCopyIRExpr(bb_in->next);
968 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +0000969
bart3772a982008-03-15 08:11:03 +0000970 for (i = 0; i < bb_in->stmts_used; i++)
971 {
972 IRStmt* const st = bb_in->stmts[i];
973 tl_assert(st);
974 if (st->tag == Ist_NoOp)
975 continue;
sewardjaf44c822007-11-25 14:01:38 +0000976
bart3772a982008-03-15 08:11:03 +0000977 switch (st->tag)
978 {
979 case Ist_IMark:
980 instrument = VG_(seginfo_sect_kind)(NULL, 0, st->Ist.IMark.addr)
981 != Vg_SectPLT;
sewardj7cf4e6b2008-05-01 20:24:26 +0000982 addStmtToIRSB(bb, st);
bart3772a982008-03-15 08:11:03 +0000983 break;
984
985 case Ist_MBE:
986 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +0000987 {
bart3772a982008-03-15 08:11:03 +0000988 case Imbe_Fence:
989 break; /* not interesting */
990 case Imbe_BusLock:
991 tl_assert(! bus_locked);
992 bus_locked = True;
993 break;
994 case Imbe_BusUnlock:
995 tl_assert(bus_locked);
996 bus_locked = False;
997 break;
sewardjaf44c822007-11-25 14:01:38 +0000998 default:
bart3772a982008-03-15 08:11:03 +0000999 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001000 }
bart3772a982008-03-15 08:11:03 +00001001 addStmtToIRSB(bb, st);
1002 break;
sewardjaf44c822007-11-25 14:01:38 +00001003
bart3772a982008-03-15 08:11:03 +00001004 case Ist_Store:
1005 if (instrument && ! bus_locked)
1006 {
1007 instrument_store(bb,
1008 st->Ist.Store.addr,
1009 sizeofIRType(typeOfIRExpr(bb->tyenv,
1010 st->Ist.Store.data)));
1011 }
1012 addStmtToIRSB(bb, st);
1013 break;
barta47b3512008-03-07 17:22:26 +00001014
bart3772a982008-03-15 08:11:03 +00001015 case Ist_WrTmp:
1016 if (instrument)
1017 {
1018 const IRExpr* const data = st->Ist.WrTmp.data;
1019 if (data->tag == Iex_Load)
1020 {
1021 instrument_load(bb,
1022 data->Iex.Load.addr,
1023 sizeofIRType(data->Iex.Load.ty));
1024 }
1025 }
1026 addStmtToIRSB(bb, st);
1027 break;
1028
1029 case Ist_Dirty:
1030 if (instrument)
1031 {
1032 IRDirty* d = st->Ist.Dirty.details;
1033 IREffect const mFx = d->mFx;
1034 switch (mFx) {
1035 case Ifx_None:
1036 break;
1037 case Ifx_Read:
1038 case Ifx_Write:
1039 case Ifx_Modify:
1040 tl_assert(d->mAddr);
1041 tl_assert(d->mSize > 0);
1042 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1043 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1044 di = unsafeIRDirty_0_N(
1045 /*regparms*/2,
1046 "drd_trace_load",
1047 VG_(fnptr_to_fnentry)(drd_trace_load),
1048 argv);
1049 addStmtToIRSB(bb, IRStmt_Dirty(di));
1050 }
1051 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1052 && ! bus_locked)
1053 {
1054 di = unsafeIRDirty_0_N(
1055 /*regparms*/2,
1056 "drd_trace_store",
1057 VG_(fnptr_to_fnentry)(drd_trace_store),
1058 argv);
1059 addStmtToIRSB(bb, IRStmt_Dirty(di));
1060 }
1061 break;
1062 default:
1063 tl_assert(0);
1064 }
1065 }
1066 addStmtToIRSB(bb, st);
1067 break;
1068
1069 default:
1070 addStmtToIRSB(bb, st);
1071 break;
1072 }
1073 }
1074
1075 tl_assert(! bus_locked);
1076
1077 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001078}
1079
sewardjaf44c822007-11-25 14:01:38 +00001080static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1081{
bart3772a982008-03-15 08:11:03 +00001082 tl_assert(tid == VG_(get_running_tid)());
1083 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001084}
1085
1086static
1087void drd_fini(Int exitcode)
1088{
bart3772a982008-03-15 08:11:03 +00001089 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001090 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001091 {
barte4504dd2008-04-06 15:02:58 +00001092 ULong update_danger_set_count;
1093 ULong dsnsc;
1094 ULong dscvc;
1095
1096 update_danger_set_count
1097 = thread_get_update_danger_set_count(&dsnsc, &dscvc);
1098
bartbd7e56e2008-03-31 18:14:12 +00001099 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001100 " thread: %lld context switches"
barte4504dd2008-04-06 15:02:58 +00001101 " / %lld updates of the danger set",
bart3772a982008-03-15 08:11:03 +00001102 thread_get_context_switch_count(),
barte4504dd2008-04-06 15:02:58 +00001103 update_danger_set_count);
1104 VG_(message)(Vg_UserMsg,
1105 " (%lld new sg + %lld combine vc + %lld csw).",
1106 dsnsc,
1107 dscvc,
1108 update_danger_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001109 VG_(message)(Vg_UserMsg,
1110 " segments: created %lld segments, max %lld alive,"
1111 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001112 sg_get_created_segments_count(),
1113 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001114 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001115 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001116 " (%lld m, %lld rw, %lld s, %lld b)",
1117 get_mutex_segment_creation_count(),
1118 get_rwlock_segment_creation_count(),
1119 get_semaphore_segment_creation_count(),
1120 get_barrier_segment_creation_count());
1121 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001122 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001123 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001124 bm_get_bitmap2_node_creation_count());
1125 VG_(message)(Vg_UserMsg,
1126 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001127 bm_get_bitmap2_creation_count());
1128 VG_(message)(Vg_UserMsg,
1129 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001130 get_mutex_lock_count());
1131 drd_print_malloc_stats();
1132 }
sewardjaf44c822007-11-25 14:01:38 +00001133}
1134
sewardjaf44c822007-11-25 14:01:38 +00001135static
1136void drd_pre_clo_init(void)
1137{
bart3772a982008-03-15 08:11:03 +00001138 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001139
bart3772a982008-03-15 08:11:03 +00001140 VG_(details_name) ("exp-drd");
1141 VG_(details_version) (NULL);
1142 VG_(details_description) ("a data race detector");
1143 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1144 " by Bart Van Assche.");
1145 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001146
bart3772a982008-03-15 08:11:03 +00001147 VG_(basic_tool_funcs) (drd_post_clo_init,
1148 drd_instrument,
1149 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001150
bart3772a982008-03-15 08:11:03 +00001151 // Command line stuff.
1152 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1153 drd_print_usage,
1154 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001155
bart3772a982008-03-15 08:11:03 +00001156 // Error handling.
1157 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001158
bart3772a982008-03-15 08:11:03 +00001159 // Core event tracking.
1160 VG_(track_pre_mem_read) (drd_pre_mem_read);
1161 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1162 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001163 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001164 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1165 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1166 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1167 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001168 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1169 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001170 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1171 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1172 VG_(track_start_client_code) (drd_start_client_code);
1173 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1174 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1175 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001176
bart3772a982008-03-15 08:11:03 +00001177 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001178 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001179 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001180
bart3772a982008-03-15 08:11:03 +00001181 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001182
bart3772a982008-03-15 08:11:03 +00001183 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001184
bart3772a982008-03-15 08:11:03 +00001185 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001186}
1187
1188
1189VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)