blob: 5b688ec4c129b988cf1b82360675903e3d7d8934 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
sewardj85642922008-01-14 11:54:56 +000026#include "drd_barrier.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_clientreq.h"
bart4bb53d82008-02-28 19:06:34 +000028#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "drd_cond.h"
30#include "drd_error.h"
31#include "drd_malloc_wrappers.h"
32#include "drd_mutex.h"
bart5bd9f2d2008-03-03 20:31:58 +000033#include "drd_rwlock.h"
sewardjaf44c822007-11-25 14:01:38 +000034#include "drd_segment.h"
sewardj85642922008-01-14 11:54:56 +000035#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "drd_suppression.h"
37#include "drd_thread.h"
bartd59bb0f2008-06-08 08:08:31 +000038#include "drd_thread_bitmap.h"
sewardjaf44c822007-11-25 14:01:38 +000039#include "drd_track.h"
40#include "drd_vc.h"
sewardj721ad7b2007-11-30 08:30:29 +000041#include "priv_drd_clientreq.h"
sewardj85642922008-01-14 11:54:56 +000042#include "pub_drd_bitmap.h"
bart024a95a2008-04-01 18:27:41 +000043#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
sewardjaf44c822007-11-25 14:01:38 +000044#include "pub_tool_basics.h"
45#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
46#include "pub_tool_libcassert.h" // tl_assert()
47#include "pub_tool_libcbase.h" // VG_(strcmp)
48#include "pub_tool_libcprint.h" // VG_(printf)
49#include "pub_tool_libcproc.h"
50#include "pub_tool_machine.h"
bart024a95a2008-04-01 18:27:41 +000051#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardjaf44c822007-11-25 14:01:38 +000052#include "pub_tool_options.h" // command line options
bartceded212008-03-26 17:39:52 +000053#include "pub_tool_replacemalloc.h"
bart72b751c2008-03-01 13:44:24 +000054#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000055#include "pub_tool_tooliface.h"
56
57
bartbf80e122008-06-06 10:18:24 +000058/* Include several source files here in order to allow the compiler to */
59/* do more inlining. */
60#include "drd_bitmap.c"
61#include "drd_segment.c"
62#include "drd_thread.c"
63#include "drd_vc.c"
64
65
66
sewardjaf44c822007-11-25 14:01:38 +000067// Function declarations.
68
69static void drd_start_client_code(const ThreadId tid, const ULong bbs_done);
sewardjaf44c822007-11-25 14:01:38 +000070
71
72// Local variables.
73
bart08865622008-06-06 14:31:36 +000074static Bool s_drd_check_stack_accesses = False;
75static Bool s_drd_print_stats = False;
76static Bool s_drd_trace_fork_join = False;
77static Bool s_drd_var_info = False;
78static Bool s_show_stack_usage = False;
sewardjaf44c822007-11-25 14:01:38 +000079
80
81//
82// Implement the needs_command_line_options for drd.
83//
84
85static Bool drd_process_cmd_line_option(Char* arg)
86{
bart9d5b7962008-05-14 12:25:00 +000087 int exclusive_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000088 int segment_merging = -1;
bart9d5b7962008-05-14 12:25:00 +000089 int shared_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000090 int show_confl_seg = -1;
91 int trace_barrier = -1;
92 int trace_clientobj = -1;
93 int trace_cond = -1;
94 int trace_csw = -1;
95 int trace_danger_set = -1;
96 int trace_mutex = -1;
97 int trace_rwlock = -1;
98 int trace_segment = -1;
99 int trace_semaphore = -1;
100 int trace_suppression = -1;
101 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +0000102
bart08865622008-06-06 14:31:36 +0000103 VG_BOOL_CLO (arg, "--check-stack-var", s_drd_check_stack_accesses)
bart9d5b7962008-05-14 12:25:00 +0000104 else VG_BOOL_CLO(arg, "--drd-stats", s_drd_print_stats)
105 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
106 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
107 else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage)
108 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
109 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
110 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
111 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
112 else VG_BOOL_CLO(arg, "--trace-danger-set", trace_danger_set)
113 else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join)
114 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
115 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
116 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
117 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
118 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
119 else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info)
120 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
121 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
122 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000123 else
bartceded212008-03-26 17:39:52 +0000124 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000125
bart9d5b7962008-05-14 12:25:00 +0000126 if (exclusive_threshold_ms != -1)
127 {
128 mutex_set_lock_threshold(exclusive_threshold_ms);
129 rwlock_set_exclusive_threshold(exclusive_threshold_ms);
130 }
131 if (shared_threshold_ms != -1)
132 {
133 rwlock_set_shared_threshold(shared_threshold_ms);
134 }
barta9c37392008-03-22 09:38:48 +0000135 if (segment_merging != -1)
136 thread_set_segment_merging(segment_merging);
137 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000138 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000139 if (trace_address)
140 {
bart005dc972008-03-29 14:42:59 +0000141 const Addr addr = VG_(strtoll16)(trace_address, 0);
142 drd_start_tracing_address_range(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000143 }
barta9c37392008-03-22 09:38:48 +0000144 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000145 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000146 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000147 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000148 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000149 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000150 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000151 thread_trace_context_switches(trace_csw);
barta9c37392008-03-22 09:38:48 +0000152 if (trace_danger_set != -1)
bart3772a982008-03-15 08:11:03 +0000153 thread_trace_danger_set(trace_danger_set);
barta9c37392008-03-22 09:38:48 +0000154 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000155 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000156 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000157 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000158 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000159 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000160 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000161 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000162 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000163 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000164
bart3772a982008-03-15 08:11:03 +0000165 return True;
sewardjaf44c822007-11-25 14:01:38 +0000166}
167
168static void drd_print_usage(void)
bartbd7e56e2008-03-31 18:14:12 +0000169{
170 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000171" --check-stack-var=yes|no Whether or not to report data races on\n"
172" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000173" --exclusive-threshold=<n> Print an error message if any mutex or\n"
174" writer lock is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000175" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000176" Segment merging is an algorithm to limit memory usage of the\n"
177" data race detection algorithm. Disabling segment merging may\n"
178" improve the accuracy of the so-called 'other segments' displayed\n"
179" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000180" --shared-threshold=<n> Print an error message if a reader lock\n"
181" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000182" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
183" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
184" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000185" stack variables when a race is reported on such a variable. This\n"
186" information is by default not displayed since for big programs\n"
187" reading in all debug information for all variables at once may\n"
188" cause an out of memory error [no].\n"
189"\n"
bart2cb48d62008-04-01 16:57:42 +0000190" exp-drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000191" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000192" specified address [off].\n"
193" --trace-barrier=yes|no Trace all barrier activity [no].\n"
194" --trace-cond=yes|no Trace all condition variable activity [no].\n"
195" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
196" --trace-mutex=yes|no Trace all mutex activity [no].\n"
197" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
198" --trace-segment=yes|no Trace segment actions [no].\n"
199" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000200 );
bart130463a2008-04-01 17:03:33 +0000201 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000202}
203
204static void drd_print_debug_usage(void)
205{
bart130463a2008-04-01 17:03:33 +0000206 VG_(printf)(
207" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
208" --trace-clientobj=yes|no Trace all client object activity [no].\n"
209" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
210" --trace-danger-set=yes|no Trace all danger set updates [no].\n"
211" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
212 );
213 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000214}
215
216
217//
218// Implements the thread-related core callbacks.
219//
220
barta79df6e2008-03-14 17:07:51 +0000221static void drd_trace_mem_access(const Addr addr, const SizeT size,
222 const BmAccessTypeT access_type)
223{
bartb9c7d742008-06-10 12:51:51 +0000224 if (drd_is_any_traced(addr, addr + size))
225 {
226 char vc[80];
227 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
228 VG_(message)(Vg_UserMsg,
229 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
230 access_type == eLoad
231 ? "load "
232 : access_type == eStore
233 ? "store"
234 : access_type == eStart
235 ? "start"
236 : access_type == eEnd
237 ? "end "
238 : "????",
239 addr,
240 size,
241 VG_(get_running_tid)(),
242 thread_get_running_tid(),
243 vc);
244 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
245 VG_(clo_backtrace_size));
246 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
247 == VG_(get_running_tid)());
248 }
barta79df6e2008-03-14 17:07:51 +0000249}
250
bart29a0e2a2008-06-10 13:55:13 +0000251static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size)
252{
253 return drd_trace_mem_access(addr, size, eLoad);
254}
255
256static VG_REGPARM(2) void drd_trace_mem_store(const Addr addr,const SizeT size)
257{
258 return drd_trace_mem_access(addr, size, eStore);
259}
260
barta79df6e2008-03-14 17:07:51 +0000261static void drd_report_race(const Addr addr, const SizeT size,
262 const BmAccessTypeT access_type)
263{
bart49c3a112008-03-15 10:28:36 +0000264 DataRaceErrInfo drei;
265
bart354009c2008-03-16 10:42:33 +0000266 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000267 drei.addr = addr;
268 drei.size = size;
269 drei.access_type = access_type;
270 VG_(maybe_record_error)(VG_(get_running_tid)(),
271 DataRaceErr,
272 VG_(get_IP)(VG_(get_running_tid)()),
273 "Conflicting accesses",
274 &drei);
barta79df6e2008-03-14 17:07:51 +0000275}
276
277static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000278{
bart8b4b2ee2008-06-11 13:17:56 +0000279#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000280 /* The assert below has been commented out because of performance reasons.*/
281 tl_assert(thread_get_running_tid()
282 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000283#endif
sewardjaf44c822007-11-25 14:01:38 +0000284
bart0e5c04f2008-06-09 15:18:59 +0000285 if (running_thread_is_recording()
286 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000287 && bm_access_load_triggers_conflict(addr, addr + size)
288 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000289 {
290 drd_report_race(addr, size, eLoad);
291 }
barta79df6e2008-03-14 17:07:51 +0000292}
293
294static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
295{
bart0e5c04f2008-06-09 15:18:59 +0000296 if (running_thread_is_recording()
297 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000298 && bm_access_load_1_triggers_conflict(addr)
299 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000300 {
301 drd_report_race(addr, 1, eLoad);
302 }
barta79df6e2008-03-14 17:07:51 +0000303}
304
305static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
306{
bart0e5c04f2008-06-09 15:18:59 +0000307 if (running_thread_is_recording()
308 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000309 && bm_access_load_2_triggers_conflict(addr)
310 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000311 {
312 drd_report_race(addr, 2, eLoad);
313 }
barta79df6e2008-03-14 17:07:51 +0000314}
315
316static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
317{
bart0e5c04f2008-06-09 15:18:59 +0000318 if (running_thread_is_recording()
319 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000320 && bm_access_load_4_triggers_conflict(addr)
321 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000322 {
323 drd_report_race(addr, 4, eLoad);
324 }
barta79df6e2008-03-14 17:07:51 +0000325}
326
327static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
328{
bart0e5c04f2008-06-09 15:18:59 +0000329 if (running_thread_is_recording()
330 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000331 && bm_access_load_8_triggers_conflict(addr)
332 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000333 {
334 drd_report_race(addr, 8, eLoad);
335 }
sewardjaf44c822007-11-25 14:01:38 +0000336}
337
338static
339VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
340{
bart8b4b2ee2008-06-11 13:17:56 +0000341#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000342 /* The assert below has been commented out because of performance reasons.*/
343 tl_assert(thread_get_running_tid()
344 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000345#endif
sewardjaf44c822007-11-25 14:01:38 +0000346
bart0e5c04f2008-06-09 15:18:59 +0000347 if (running_thread_is_recording()
348 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000349 && bm_access_store_triggers_conflict(addr, addr + size)
350 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000351 {
352 drd_report_race(addr, size, eStore);
353 }
barta79df6e2008-03-14 17:07:51 +0000354}
355
356static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
357{
bart0e5c04f2008-06-09 15:18:59 +0000358 if (running_thread_is_recording()
359 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000360 && bm_access_store_1_triggers_conflict(addr)
361 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000362 {
363 drd_report_race(addr, 1, eStore);
364 }
barta79df6e2008-03-14 17:07:51 +0000365}
366
367static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
368{
bart0e5c04f2008-06-09 15:18:59 +0000369 if (running_thread_is_recording()
370 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000371 && bm_access_store_2_triggers_conflict(addr)
372 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000373 {
374 drd_report_race(addr, 2, eStore);
375 }
barta79df6e2008-03-14 17:07:51 +0000376}
377
378static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
379{
bart0e5c04f2008-06-09 15:18:59 +0000380 if (running_thread_is_recording()
381 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000382 && bm_access_store_4_triggers_conflict(addr)
383 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000384 {
385 drd_report_race(addr, 4, eStore);
386 }
barta79df6e2008-03-14 17:07:51 +0000387}
388
389static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
390{
bart0e5c04f2008-06-09 15:18:59 +0000391 if (running_thread_is_recording()
392 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000393 && bm_access_store_8_triggers_conflict(addr)
394 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000395 {
396 drd_report_race(addr, 8, eStore);
397 }
sewardjaf44c822007-11-25 14:01:38 +0000398}
399
400static void drd_pre_mem_read(const CorePart part,
401 const ThreadId tid,
402 Char* const s,
403 const Addr a,
404 const SizeT size)
405{
bart3772a982008-03-15 08:11:03 +0000406 if (size > 0)
407 {
408 drd_trace_load(a, size);
409 }
sewardjaf44c822007-11-25 14:01:38 +0000410}
411
bart5e85d262008-03-01 10:49:37 +0000412static void drd_pre_mem_read_asciiz(const CorePart part,
413 const ThreadId tid,
414 Char* const s,
415 const Addr a)
416{
bart3772a982008-03-15 08:11:03 +0000417 const char* p = (void*)a;
418 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000419
bart3772a982008-03-15 08:11:03 +0000420 /* Note: the expression '*p' reads client memory and may crash if the */
421 /* client provided an invalid pointer ! */
422 while (*p)
423 {
424 p++;
425 size++;
426 }
427 // To do: find out what a reasonable upper limit on 'size' is.
428 tl_assert(size < 4096);
429 if (size > 0)
430 {
431 drd_trace_load(a, size);
432 }
bart5e85d262008-03-01 10:49:37 +0000433}
434
sewardjaf44c822007-11-25 14:01:38 +0000435static void drd_post_mem_write(const CorePart part,
436 const ThreadId tid,
437 const Addr a,
438 const SizeT size)
439{
bart3772a982008-03-15 08:11:03 +0000440 thread_set_vg_running_tid(VG_(get_running_tid)());
441 if (size > 0)
442 {
443 drd_trace_store(a, size);
444 }
sewardjaf44c822007-11-25 14:01:38 +0000445}
446
bart08865622008-06-06 14:31:36 +0000447static __inline__
448void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000449{
bart005dc972008-03-29 14:42:59 +0000450 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000451
bartb9c7d742008-06-10 12:51:51 +0000452 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000453 {
bartd5765912008-03-16 08:40:55 +0000454 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000455 }
sewardjaf44c822007-11-25 14:01:38 +0000456}
457
sewardj7cf4e6b2008-05-01 20:24:26 +0000458static void drd_start_using_mem_w_ecu(const Addr a1,
459 const SizeT len,
460 UInt ec_uniq)
461{
462 drd_start_using_mem(a1, len);
463}
464
465static void drd_start_using_mem_w_tid(const Addr a1,
466 const SizeT len,
467 ThreadId tid)
468{
469 drd_start_using_mem(a1, len);
470}
471
bart0ffa4832008-04-05 12:57:01 +0000472static __inline__
473void drd_stop_using_mem(const Addr a1, const SizeT len,
474 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000475{
bart3772a982008-03-15 08:11:03 +0000476 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000477
bart3772a982008-03-15 08:11:03 +0000478 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000479
bartb9c7d742008-06-10 12:51:51 +0000480 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000481 {
bartd43f8d32008-03-16 17:29:20 +0000482 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000483 }
bart08865622008-06-06 14:31:36 +0000484 if (! is_stack_mem || s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000485 {
486 thread_stop_using_mem(a1, a2);
487 clientobj_stop_using_mem(a1, a2);
488 drd_suppression_stop_using_mem(a1, a2);
489 }
490}
491
492static __inline__
493void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
494{
495 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000496}
497
bartcb2d0072008-05-31 07:55:51 +0000498/** Suppress data race reports on all addresses contained in .plt and
499 * .got.plt sections inside the address range [ a, a + len [. The data in
500 * these sections is modified by _dl_relocate_object() every time a function
501 * in a shared library is called for the first time. Since the first call
502 * to a function in a shared library can happen from a multithreaded context,
503 * such calls can cause conflicting accesses. See also Ulrich Drepper's
504 * paper "How to Write Shared Libraries" for more information about relocation
505 * (http://people.redhat.com/drepper/dsohowto.pdf).
506 */
507static void suppress_relocation_conflicts(const Addr a, const SizeT len)
508{
509 const DebugInfo* di;
510
511#if 0
512 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
513#endif
514
515 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
516 {
517 Addr avma;
518 SizeT size;
519
520 avma = VG_(seginfo_get_plt_avma)(di);
521 size = VG_(seginfo_get_plt_size)(di);
522 if (a <= avma && avma + size <= a + len)
523 {
524#if 0
525 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
526#endif
527 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
528 drd_start_suppression(avma, avma + size, ".plt");
529 }
530
531 avma = VG_(seginfo_get_gotplt_avma)(di);
532 size = VG_(seginfo_get_gotplt_size)(di);
533 if (a <= avma && avma + size <= a + len)
534 {
535#if 0
536 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
537#endif
538 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
539 drd_start_suppression(avma, avma + size, ".gotplt");
540 }
541 }
542}
543
bart5e85d262008-03-01 10:49:37 +0000544static
545void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
546 const Bool rr, const Bool ww, const Bool xx)
547{
bartd5765912008-03-16 08:40:55 +0000548 thread_set_vg_running_tid(VG_(get_running_tid)());
549
bart3772a982008-03-15 08:11:03 +0000550 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000551
552 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000553}
554
sewardjaf44c822007-11-25 14:01:38 +0000555/* Called by the core when the stack of a thread grows, to indicate that */
556/* the addresses in range [ a, a + len [ may now be used by the client. */
557/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000558static __inline__
559void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000560{
bartd43f8d32008-03-16 17:29:20 +0000561 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000562 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
563 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000564}
565
566/* Called by the core when the stack of a thread shrinks, to indicate that */
567/* the addresses [ a, a + len [ are no longer accessible for the client. */
568/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000569static __inline__
570void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000571{
bartd43f8d32008-03-16 17:29:20 +0000572 thread_set_stack_min(thread_get_running_tid(),
573 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000574 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
575 True);
sewardjaf44c822007-11-25 14:01:38 +0000576}
577
sewardj7cf4e6b2008-05-01 20:24:26 +0000578static void drd_start_using_mem_stack_signal(
579 const Addr a, const SizeT len,
580 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000581{
bartd5765912008-03-16 08:40:55 +0000582 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000583 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000584}
585
bart5e85d262008-03-01 10:49:37 +0000586static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000587{
bart0ffa4832008-04-05 12:57:01 +0000588 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000589}
590
591static
592void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
593{
bart3772a982008-03-15 08:11:03 +0000594 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
595 tl_assert(created != VG_INVALID_THREADID);
596 thread_pre_create(drd_creator, created);
597 if (IsValidDrdThreadId(drd_creator))
598 {
599 thread_new_segment(drd_creator);
600 }
bartbd7e56e2008-03-31 18:14:12 +0000601 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000602 {
603 VG_(message)(Vg_DebugMsg,
604 "drd_pre_thread_create creator = %d/%d, created = %d",
605 creator, drd_creator, created);
606 }
sewardjaf44c822007-11-25 14:01:38 +0000607}
608
609/* Called by Valgrind's core before any loads or stores are performed on */
610/* the context of thread "created". At startup, this function is called */
611/* with arguments (0,1). */
612static
bart0ffa4832008-04-05 12:57:01 +0000613void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000614{
bart0ffa4832008-04-05 12:57:01 +0000615 DrdThreadId drd_created;
616
617 tl_assert(vg_created != VG_INVALID_THREADID);
618
619 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000620 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000621 {
622 VG_(message)(Vg_DebugMsg,
623 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000624 vg_created, drd_created);
625 }
bart08865622008-06-06 14:31:36 +0000626 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000627 {
628 drd_start_suppression(thread_get_stack_max(drd_created)
629 - thread_get_stack_size(drd_created),
630 thread_get_stack_max(drd_created),
631 "stack");
bart3772a982008-03-15 08:11:03 +0000632 }
sewardjaf44c822007-11-25 14:01:38 +0000633}
634
635/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
636/* after thread drd_joiner joined thread drd_joinee. */
637void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
638{
bart3772a982008-03-15 08:11:03 +0000639 tl_assert(IsValidDrdThreadId(drd_joiner));
640 tl_assert(IsValidDrdThreadId(drd_joinee));
641 thread_new_segment(drd_joinee);
642 thread_combine_vc(drd_joiner, drd_joinee);
643 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000644
bartbd7e56e2008-03-31 18:14:12 +0000645 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000646 {
bartfdd8d4e2008-04-01 18:38:29 +0000647 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
648 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000649 const unsigned msg_size = 256;
650 char* msg;
651
652 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000653 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000654 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000655 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
656 joiner, drd_joiner, joinee, drd_joinee);
657 if (joiner)
658 {
bart024a95a2008-04-01 18:27:41 +0000659 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000660 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000661 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000662 thread_get_vc(drd_joiner));
663 }
664 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000665 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000666 }
sewardjaf44c822007-11-25 14:01:38 +0000667
bart08865622008-06-06 14:31:36 +0000668 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000669 {
670 drd_finish_suppression(thread_get_stack_max(drd_joinee)
671 - thread_get_stack_size(drd_joinee),
672 thread_get_stack_max(drd_joinee));
673 }
bart3772a982008-03-15 08:11:03 +0000674 thread_delete(drd_joinee);
675 mutex_thread_delete(drd_joinee);
676 cond_thread_delete(drd_joinee);
677 semaphore_thread_delete(drd_joinee);
678 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000679}
680
bart5bd9f2d2008-03-03 20:31:58 +0000681
sewardjaf44c822007-11-25 14:01:38 +0000682/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000683static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000684{
bart3772a982008-03-15 08:11:03 +0000685 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000686
bartd43f8d32008-03-16 17:29:20 +0000687 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000688
bartd43f8d32008-03-16 17:29:20 +0000689 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000690 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000691 {
692 VG_(message)(Vg_DebugMsg,
693 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000694 vg_tid,
bart3772a982008-03-15 08:11:03 +0000695 drd_tid,
696 thread_get_joinable(drd_tid)
697 ? ""
698 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000699 }
700 if (s_show_stack_usage)
701 {
702 const SizeT stack_size = thread_get_stack_size(drd_tid);
703 const SizeT used_stack
704 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
705 VG_(message)(Vg_UserMsg,
706 "thread %d/%d%s finished and used %ld bytes out of %ld"
707 " on its stack. Margin: %ld bytes.",
708 vg_tid,
709 drd_tid,
710 thread_get_joinable(drd_tid)
711 ? ""
712 : " (which is a detached thread)",
713 used_stack,
714 stack_size,
715 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000716
bart3772a982008-03-15 08:11:03 +0000717 }
bartd43f8d32008-03-16 17:29:20 +0000718 drd_stop_using_mem(thread_get_stack_min(drd_tid),
719 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000720 - thread_get_stack_min(drd_tid),
721 True);
bartd43f8d32008-03-16 17:29:20 +0000722 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000723 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000724}
725
bart0268dfa2008-03-11 20:10:21 +0000726void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000727{
bart3772a982008-03-15 08:11:03 +0000728 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000729}
730
sewardj347eeba2008-01-21 14:19:07 +0000731void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000732{
bart3772a982008-03-15 08:11:03 +0000733 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000734}
735
bart2e3a3c12008-03-24 08:33:47 +0000736void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
737 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000738{
bart2e3a3c12008-03-24 08:33:47 +0000739 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000740}
741
bart00344642008-03-01 15:27:41 +0000742void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000743{
bart4a975e12008-03-30 13:28:33 +0000744 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000745}
746
bart00344642008-03-01 15:27:41 +0000747void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000748{
bart3772a982008-03-15 08:11:03 +0000749 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000750}
751
bart0268dfa2008-03-11 20:10:21 +0000752void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000753{
bart3772a982008-03-15 08:11:03 +0000754 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000755}
756
bart72b751c2008-03-01 13:44:24 +0000757void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000758{
bart3772a982008-03-15 08:11:03 +0000759 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000760}
761
bart0268dfa2008-03-11 20:10:21 +0000762void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000763 const Word pshared, const Word value)
764{
bart3772a982008-03-15 08:11:03 +0000765 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000766}
767
768void drd_semaphore_destroy(const Addr semaphore)
769{
bart3772a982008-03-15 08:11:03 +0000770 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000771}
772
bart0268dfa2008-03-11 20:10:21 +0000773void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000774{
bart3772a982008-03-15 08:11:03 +0000775 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000776}
777
778void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
779 const Bool waited)
780{
bart3772a982008-03-15 08:11:03 +0000781 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000782}
783
bart0268dfa2008-03-11 20:10:21 +0000784void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000785{
bart3772a982008-03-15 08:11:03 +0000786 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000787}
788
789void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000790 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000791{
bart3772a982008-03-15 08:11:03 +0000792 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000793}
794
795
bart0268dfa2008-03-11 20:10:21 +0000796void drd_barrier_init(const Addr barrier,
797 const BarrierT barrier_type, const Word count,
798 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000799{
bart3772a982008-03-15 08:11:03 +0000800 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000801}
802
bart0268dfa2008-03-11 20:10:21 +0000803void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000804{
bart3772a982008-03-15 08:11:03 +0000805 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000806}
807
bart0268dfa2008-03-11 20:10:21 +0000808void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
809 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000810{
bart3772a982008-03-15 08:11:03 +0000811 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000812}
813
814void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000815 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000816{
bart3772a982008-03-15 08:11:03 +0000817 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000818}
819
sewardjaf44c822007-11-25 14:01:38 +0000820
821//
822// Implementation of the tool interface.
823//
824
825static
826void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000827{
828# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000829 /* fine */
bartd5c63522008-06-01 07:33:14 +0000830# elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bartcb2d0072008-05-31 07:55:51 +0000831 VG_(printf)(
832"\nWARNING: support for PowerPC-specific atomic instructions like lwarx and\n"
833"stwcx is not yet complete. As a result, false positives will be reported on\n"
834"code that uses these instructions. This will happen e.g. when printf() is\n"
835"called from more than one thread.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000836# else
bart3772a982008-03-15 08:11:03 +0000837 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000838# endif
bart95761b52008-03-29 08:34:03 +0000839
840 if (s_drd_var_info)
841 {
842 VG_(needs_var_info)();
843 }
sewardjdcbb8d32007-11-26 21:34:30 +0000844}
sewardjaf44c822007-11-25 14:01:38 +0000845
barta79df6e2008-03-14 17:07:51 +0000846static void instrument_load(IRSB* const bb,
847 IRExpr* const addr_expr,
848 const HWord size)
849{
bart3772a982008-03-15 08:11:03 +0000850 IRExpr* size_expr;
851 IRExpr** argv;
852 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000853
bart29a0e2a2008-06-10 13:55:13 +0000854 if (UNLIKELY(drd_any_address_is_traced()))
855 {
856 addStmtToIRSB(bb,
857 IRStmt_Dirty(
858 unsafeIRDirty_0_N(/*regparms*/2,
859 "drd_trace_load",
860 VG_(fnptr_to_fnentry)
861 (drd_trace_mem_load),
862 mkIRExprVec_2(addr_expr,
863 mkIRExpr_HWord(size)))));
864 }
865
bart3772a982008-03-15 08:11:03 +0000866 switch (size)
867 {
868 case 1:
869 argv = mkIRExprVec_1(addr_expr);
870 di = unsafeIRDirty_0_N(/*regparms*/1,
871 "drd_trace_load_1",
872 VG_(fnptr_to_fnentry)(drd_trace_load_1),
873 argv);
874 break;
875 case 2:
876 argv = mkIRExprVec_1(addr_expr);
877 di = unsafeIRDirty_0_N(/*regparms*/1,
878 "drd_trace_load_2",
879 VG_(fnptr_to_fnentry)(drd_trace_load_2),
880 argv);
881 break;
882 case 4:
883 argv = mkIRExprVec_1(addr_expr);
884 di = unsafeIRDirty_0_N(/*regparms*/1,
885 "drd_trace_load_4",
886 VG_(fnptr_to_fnentry)(drd_trace_load_4),
887 argv);
888 break;
889 case 8:
890 argv = mkIRExprVec_1(addr_expr);
891 di = unsafeIRDirty_0_N(/*regparms*/1,
892 "drd_trace_load_8",
893 VG_(fnptr_to_fnentry)(drd_trace_load_8),
894 argv);
895 break;
896 default:
897 size_expr = mkIRExpr_HWord(size);
898 argv = mkIRExprVec_2(addr_expr, size_expr);
899 di = unsafeIRDirty_0_N(/*regparms*/2,
900 "drd_trace_load",
901 VG_(fnptr_to_fnentry)(drd_trace_load),
902 argv);
903 break;
904 }
905 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000906}
907
908static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000909 IRExpr* const addr_expr,
910 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000911{
bart3772a982008-03-15 08:11:03 +0000912 IRExpr* size_expr;
913 IRExpr** argv;
914 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000915
bart29a0e2a2008-06-10 13:55:13 +0000916 if (UNLIKELY(drd_any_address_is_traced()))
917 {
918 addStmtToIRSB(bb,
919 IRStmt_Dirty(
920 unsafeIRDirty_0_N(/*regparms*/2,
921 "drd_trace_store",
922 VG_(fnptr_to_fnentry)
923 (drd_trace_mem_store),
924 mkIRExprVec_2(addr_expr,
925 mkIRExpr_HWord(size)))));
926 }
927
bart3772a982008-03-15 08:11:03 +0000928 switch (size)
929 {
930 case 1:
931 argv = mkIRExprVec_1(addr_expr);
932 di = unsafeIRDirty_0_N(/*regparms*/1,
933 "drd_trace_store_1",
934 VG_(fnptr_to_fnentry)(drd_trace_store_1),
935 argv);
936 break;
937 case 2:
938 argv = mkIRExprVec_1(addr_expr);
939 di = unsafeIRDirty_0_N(/*regparms*/1,
940 "drd_trace_store_2",
941 VG_(fnptr_to_fnentry)(drd_trace_store_2),
942 argv);
943 break;
944 case 4:
945 argv = mkIRExprVec_1(addr_expr);
946 di = unsafeIRDirty_0_N(/*regparms*/1,
947 "drd_trace_store_4",
948 VG_(fnptr_to_fnentry)(drd_trace_store_4),
949 argv);
950 break;
951 case 8:
952 argv = mkIRExprVec_1(addr_expr);
953 di = unsafeIRDirty_0_N(/*regparms*/1,
954 "drd_trace_store_8",
955 VG_(fnptr_to_fnentry)(drd_trace_store_8),
956 argv);
957 break;
958 default:
959 size_expr = mkIRExpr_HWord(size);
960 argv = mkIRExprVec_2(addr_expr, size_expr);
961 di = unsafeIRDirty_0_N(/*regparms*/2,
962 "drd_trace_store",
963 VG_(fnptr_to_fnentry)(drd_trace_store),
964 argv);
965 break;
966 }
967 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000968}
969
sewardjaf44c822007-11-25 14:01:38 +0000970static
971IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +0000972 IRSB* const bb_in,
973 VexGuestLayout* const layout,
974 VexGuestExtents* const vge,
975 IRType const gWordTy,
976 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +0000977{
bart3772a982008-03-15 08:11:03 +0000978 IRDirty* di;
979 Int i;
980 IRSB* bb;
981 IRExpr** argv;
982 Bool instrument = True;
983 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +0000984
bart3772a982008-03-15 08:11:03 +0000985 /* Set up BB */
986 bb = emptyIRSB();
987 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
988 bb->next = deepCopyIRExpr(bb_in->next);
989 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +0000990
bart3772a982008-03-15 08:11:03 +0000991 for (i = 0; i < bb_in->stmts_used; i++)
992 {
993 IRStmt* const st = bb_in->stmts[i];
994 tl_assert(st);
995 if (st->tag == Ist_NoOp)
996 continue;
sewardjaf44c822007-11-25 14:01:38 +0000997
bart3772a982008-03-15 08:11:03 +0000998 switch (st->tag)
999 {
bart3772a982008-03-15 08:11:03 +00001000 case Ist_MBE:
1001 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001002 {
bart3772a982008-03-15 08:11:03 +00001003 case Imbe_Fence:
1004 break; /* not interesting */
1005 case Imbe_BusLock:
1006 tl_assert(! bus_locked);
1007 bus_locked = True;
1008 break;
1009 case Imbe_BusUnlock:
1010 tl_assert(bus_locked);
1011 bus_locked = False;
1012 break;
sewardjaf44c822007-11-25 14:01:38 +00001013 default:
bart3772a982008-03-15 08:11:03 +00001014 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001015 }
bart3772a982008-03-15 08:11:03 +00001016 addStmtToIRSB(bb, st);
1017 break;
sewardjaf44c822007-11-25 14:01:38 +00001018
bart3772a982008-03-15 08:11:03 +00001019 case Ist_Store:
1020 if (instrument && ! bus_locked)
1021 {
1022 instrument_store(bb,
1023 st->Ist.Store.addr,
1024 sizeofIRType(typeOfIRExpr(bb->tyenv,
1025 st->Ist.Store.data)));
1026 }
1027 addStmtToIRSB(bb, st);
1028 break;
barta47b3512008-03-07 17:22:26 +00001029
bart3772a982008-03-15 08:11:03 +00001030 case Ist_WrTmp:
1031 if (instrument)
1032 {
1033 const IRExpr* const data = st->Ist.WrTmp.data;
1034 if (data->tag == Iex_Load)
1035 {
1036 instrument_load(bb,
1037 data->Iex.Load.addr,
1038 sizeofIRType(data->Iex.Load.ty));
1039 }
1040 }
1041 addStmtToIRSB(bb, st);
1042 break;
1043
1044 case Ist_Dirty:
1045 if (instrument)
1046 {
1047 IRDirty* d = st->Ist.Dirty.details;
1048 IREffect const mFx = d->mFx;
1049 switch (mFx) {
1050 case Ifx_None:
1051 break;
1052 case Ifx_Read:
1053 case Ifx_Write:
1054 case Ifx_Modify:
1055 tl_assert(d->mAddr);
1056 tl_assert(d->mSize > 0);
1057 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1058 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1059 di = unsafeIRDirty_0_N(
1060 /*regparms*/2,
1061 "drd_trace_load",
1062 VG_(fnptr_to_fnentry)(drd_trace_load),
1063 argv);
1064 addStmtToIRSB(bb, IRStmt_Dirty(di));
1065 }
1066 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1067 && ! bus_locked)
1068 {
1069 di = unsafeIRDirty_0_N(
1070 /*regparms*/2,
1071 "drd_trace_store",
1072 VG_(fnptr_to_fnentry)(drd_trace_store),
1073 argv);
1074 addStmtToIRSB(bb, IRStmt_Dirty(di));
1075 }
1076 break;
1077 default:
1078 tl_assert(0);
1079 }
1080 }
1081 addStmtToIRSB(bb, st);
1082 break;
1083
1084 default:
1085 addStmtToIRSB(bb, st);
1086 break;
1087 }
1088 }
1089
1090 tl_assert(! bus_locked);
1091
1092 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001093}
1094
sewardjaf44c822007-11-25 14:01:38 +00001095static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1096{
bart3772a982008-03-15 08:11:03 +00001097 tl_assert(tid == VG_(get_running_tid)());
1098 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001099}
1100
1101static
1102void drd_fini(Int exitcode)
1103{
bart3772a982008-03-15 08:11:03 +00001104 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001105 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001106 {
barte4504dd2008-04-06 15:02:58 +00001107 ULong update_danger_set_count;
1108 ULong dsnsc;
1109 ULong dscvc;
1110
1111 update_danger_set_count
1112 = thread_get_update_danger_set_count(&dsnsc, &dscvc);
1113
bartbd7e56e2008-03-31 18:14:12 +00001114 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001115 " thread: %lld context switches"
barte4504dd2008-04-06 15:02:58 +00001116 " / %lld updates of the danger set",
bart3772a982008-03-15 08:11:03 +00001117 thread_get_context_switch_count(),
barte4504dd2008-04-06 15:02:58 +00001118 update_danger_set_count);
1119 VG_(message)(Vg_UserMsg,
1120 " (%lld new sg + %lld combine vc + %lld csw).",
1121 dsnsc,
1122 dscvc,
1123 update_danger_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001124 VG_(message)(Vg_UserMsg,
1125 " segments: created %lld segments, max %lld alive,"
1126 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001127 sg_get_created_segments_count(),
1128 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001129 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001130 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001131 " (%lld m, %lld rw, %lld s, %lld b)",
1132 get_mutex_segment_creation_count(),
1133 get_rwlock_segment_creation_count(),
1134 get_semaphore_segment_creation_count(),
1135 get_barrier_segment_creation_count());
1136 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001137 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001138 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001139 bm_get_bitmap2_node_creation_count());
1140 VG_(message)(Vg_UserMsg,
1141 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001142 bm_get_bitmap2_creation_count());
1143 VG_(message)(Vg_UserMsg,
1144 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001145 get_mutex_lock_count());
1146 drd_print_malloc_stats();
1147 }
sewardjaf44c822007-11-25 14:01:38 +00001148}
1149
sewardjaf44c822007-11-25 14:01:38 +00001150static
1151void drd_pre_clo_init(void)
1152{
bart3772a982008-03-15 08:11:03 +00001153 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001154
bart3772a982008-03-15 08:11:03 +00001155 VG_(details_name) ("exp-drd");
1156 VG_(details_version) (NULL);
1157 VG_(details_description) ("a data race detector");
1158 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1159 " by Bart Van Assche.");
1160 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001161
bart3772a982008-03-15 08:11:03 +00001162 VG_(basic_tool_funcs) (drd_post_clo_init,
1163 drd_instrument,
1164 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001165
bart3772a982008-03-15 08:11:03 +00001166 // Command line stuff.
1167 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1168 drd_print_usage,
1169 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001170
bart3772a982008-03-15 08:11:03 +00001171 // Error handling.
1172 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001173
bart3772a982008-03-15 08:11:03 +00001174 // Core event tracking.
1175 VG_(track_pre_mem_read) (drd_pre_mem_read);
1176 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1177 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001178 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001179 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1180 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1181 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1182 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001183 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1184 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001185 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1186 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1187 VG_(track_start_client_code) (drd_start_client_code);
1188 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1189 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1190 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001191
bart3772a982008-03-15 08:11:03 +00001192 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001193 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001194 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001195
bart3772a982008-03-15 08:11:03 +00001196 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001197
bart3772a982008-03-15 08:11:03 +00001198 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001199
bart3772a982008-03-15 08:11:03 +00001200 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001201}
1202
1203
1204VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)