blob: bc25ddb14bb0556fed691e7e1b2ab8aef3891f18 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
sewardj85642922008-01-14 11:54:56 +000026#include "drd_barrier.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_clientreq.h"
bart4bb53d82008-02-28 19:06:34 +000028#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "drd_cond.h"
30#include "drd_error.h"
31#include "drd_malloc_wrappers.h"
32#include "drd_mutex.h"
bart5bd9f2d2008-03-03 20:31:58 +000033#include "drd_rwlock.h"
sewardjaf44c822007-11-25 14:01:38 +000034#include "drd_segment.h"
sewardj85642922008-01-14 11:54:56 +000035#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "drd_suppression.h"
37#include "drd_thread.h"
bartd59bb0f2008-06-08 08:08:31 +000038#include "drd_thread_bitmap.h"
sewardjaf44c822007-11-25 14:01:38 +000039#include "drd_track.h"
40#include "drd_vc.h"
barteacd9162008-06-16 20:22:18 +000041#include "libvex_guest_offsets.h"
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;
barte73b0aa2008-06-28 07:19:56 +000095 int trace_conflict_set = -1;
bart08865622008-06-06 14:31:36 +000096 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)
bart46b5fce2008-06-28 13:01:30 +0000105 else VG_BOOL_CLO(arg,"--report-signal-unlocked",s_drd_report_signal_unlocked)
bart9d5b7962008-05-14 12:25:00 +0000106 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
107 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
108 else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage)
109 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
110 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
111 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
barte73b0aa2008-06-28 07:19:56 +0000112 else VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set)
bart46b5fce2008-06-28 13:01:30 +0000113 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
bart9d5b7962008-05-14 12:25:00 +0000114 else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join)
115 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
116 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
117 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
118 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
119 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
120 else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info)
121 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
122 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
123 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000124 else
bartceded212008-03-26 17:39:52 +0000125 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000126
bart9d5b7962008-05-14 12:25:00 +0000127 if (exclusive_threshold_ms != -1)
128 {
129 mutex_set_lock_threshold(exclusive_threshold_ms);
130 rwlock_set_exclusive_threshold(exclusive_threshold_ms);
131 }
132 if (shared_threshold_ms != -1)
133 {
134 rwlock_set_shared_threshold(shared_threshold_ms);
135 }
barta9c37392008-03-22 09:38:48 +0000136 if (segment_merging != -1)
137 thread_set_segment_merging(segment_merging);
138 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000139 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000140 if (trace_address)
141 {
bart005dc972008-03-29 14:42:59 +0000142 const Addr addr = VG_(strtoll16)(trace_address, 0);
143 drd_start_tracing_address_range(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000144 }
barta9c37392008-03-22 09:38:48 +0000145 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000146 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000147 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000148 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000149 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000150 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000151 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000152 thread_trace_context_switches(trace_csw);
barte73b0aa2008-06-28 07:19:56 +0000153 if (trace_conflict_set != -1)
154 thread_trace_conflict_set(trace_conflict_set);
barta9c37392008-03-22 09:38:48 +0000155 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000156 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000157 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000158 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000159 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000160 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000161 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000162 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000163 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000164 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000165
bart3772a982008-03-15 08:11:03 +0000166 return True;
sewardjaf44c822007-11-25 14:01:38 +0000167}
168
169static void drd_print_usage(void)
bartbd7e56e2008-03-31 18:14:12 +0000170{
171 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000172" --check-stack-var=yes|no Whether or not to report data races on\n"
173" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000174" --exclusive-threshold=<n> Print an error message if any mutex or\n"
175" writer lock is held longer than the specified time (in milliseconds).\n"
bart46b5fce2008-06-28 13:01:30 +0000176" --report-signal-unlocked=yes|no Whether to report calls to\n"
177" pthread_cond_signal() where the mutex associated\n"
178" with the signal via pthread_cond_wait() is not\n"
179" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000180" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000181" Segment merging is an algorithm to limit memory usage of the\n"
182" data race detection algorithm. Disabling segment merging may\n"
183" improve the accuracy of the so-called 'other segments' displayed\n"
184" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000185" --shared-threshold=<n> Print an error message if a reader lock\n"
186" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000187" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
188" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
189" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000190" stack variables when a race is reported on such a variable. This\n"
191" information is by default not displayed since for big programs\n"
bart85d22532008-06-26 07:30:32 +0000192" reading in all debug information at once may cause an out of\n"
193" memory error [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000194"\n"
bartef1b9722008-07-04 15:34:23 +0000195" drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000196" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000197" specified address [off].\n"
198" --trace-barrier=yes|no Trace all barrier activity [no].\n"
199" --trace-cond=yes|no Trace all condition variable activity [no].\n"
200" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
201" --trace-mutex=yes|no Trace all mutex activity [no].\n"
202" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart130463a2008-04-01 17:03:33 +0000203" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000204 );
bart130463a2008-04-01 17:03:33 +0000205 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000206}
207
208static void drd_print_debug_usage(void)
209{
bart130463a2008-04-01 17:03:33 +0000210 VG_(printf)(
211" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
212" --trace-clientobj=yes|no Trace all client object activity [no].\n"
213" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000214" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart987781d2008-06-27 15:00:07 +0000215" --trace-segment=yes|no Trace segment actions [no].\n"
216" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bart130463a2008-04-01 17:03:33 +0000217 );
218 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000219}
220
221
222//
223// Implements the thread-related core callbacks.
224//
225
barta79df6e2008-03-14 17:07:51 +0000226static void drd_trace_mem_access(const Addr addr, const SizeT size,
227 const BmAccessTypeT access_type)
228{
bartb9c7d742008-06-10 12:51:51 +0000229 if (drd_is_any_traced(addr, addr + size))
230 {
231 char vc[80];
232 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
233 VG_(message)(Vg_UserMsg,
234 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
235 access_type == eLoad
236 ? "load "
237 : access_type == eStore
238 ? "store"
239 : access_type == eStart
240 ? "start"
241 : access_type == eEnd
242 ? "end "
243 : "????",
244 addr,
245 size,
246 VG_(get_running_tid)(),
247 thread_get_running_tid(),
248 vc);
249 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
250 VG_(clo_backtrace_size));
251 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
252 == VG_(get_running_tid)());
253 }
barta79df6e2008-03-14 17:07:51 +0000254}
255
bart29a0e2a2008-06-10 13:55:13 +0000256static VG_REGPARM(2) void drd_trace_mem_load(const Addr addr, const SizeT size)
257{
258 return drd_trace_mem_access(addr, size, eLoad);
259}
260
261static VG_REGPARM(2) void drd_trace_mem_store(const Addr addr,const SizeT size)
262{
263 return drd_trace_mem_access(addr, size, eStore);
264}
265
barta79df6e2008-03-14 17:07:51 +0000266static void drd_report_race(const Addr addr, const SizeT size,
267 const BmAccessTypeT access_type)
268{
bart49c3a112008-03-15 10:28:36 +0000269 DataRaceErrInfo drei;
270
bart354009c2008-03-16 10:42:33 +0000271 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000272 drei.addr = addr;
273 drei.size = size;
274 drei.access_type = access_type;
275 VG_(maybe_record_error)(VG_(get_running_tid)(),
276 DataRaceErr,
277 VG_(get_IP)(VG_(get_running_tid)()),
278 "Conflicting accesses",
279 &drei);
barta79df6e2008-03-14 17:07:51 +0000280}
281
282static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000283{
bart8b4b2ee2008-06-11 13:17:56 +0000284#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000285 /* The assert below has been commented out because of performance reasons.*/
286 tl_assert(thread_get_running_tid()
287 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000288#endif
sewardjaf44c822007-11-25 14:01:38 +0000289
bart0e5c04f2008-06-09 15:18:59 +0000290 if (running_thread_is_recording()
291 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000292 && bm_access_load_triggers_conflict(addr, addr + size)
293 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000294 {
295 drd_report_race(addr, size, eLoad);
296 }
barta79df6e2008-03-14 17:07:51 +0000297}
298
299static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
300{
bart0e5c04f2008-06-09 15:18:59 +0000301 if (running_thread_is_recording()
302 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000303 && bm_access_load_1_triggers_conflict(addr)
304 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000305 {
306 drd_report_race(addr, 1, eLoad);
307 }
barta79df6e2008-03-14 17:07:51 +0000308}
309
310static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
311{
bart0e5c04f2008-06-09 15:18:59 +0000312 if (running_thread_is_recording()
313 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000314 && bm_access_load_2_triggers_conflict(addr)
315 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000316 {
317 drd_report_race(addr, 2, eLoad);
318 }
barta79df6e2008-03-14 17:07:51 +0000319}
320
321static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
322{
bart0e5c04f2008-06-09 15:18:59 +0000323 if (running_thread_is_recording()
324 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000325 && bm_access_load_4_triggers_conflict(addr)
326 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000327 {
328 drd_report_race(addr, 4, eLoad);
329 }
barta79df6e2008-03-14 17:07:51 +0000330}
331
332static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
333{
bart0e5c04f2008-06-09 15:18:59 +0000334 if (running_thread_is_recording()
335 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000336 && bm_access_load_8_triggers_conflict(addr)
337 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000338 {
339 drd_report_race(addr, 8, eLoad);
340 }
sewardjaf44c822007-11-25 14:01:38 +0000341}
342
343static
344VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
345{
bart8b4b2ee2008-06-11 13:17:56 +0000346#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart3772a982008-03-15 08:11:03 +0000347 /* The assert below has been commented out because of performance reasons.*/
348 tl_assert(thread_get_running_tid()
349 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000350#endif
sewardjaf44c822007-11-25 14:01:38 +0000351
bart0e5c04f2008-06-09 15:18:59 +0000352 if (running_thread_is_recording()
353 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000354 && bm_access_store_triggers_conflict(addr, addr + size)
355 && ! drd_is_suppressed(addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000356 {
357 drd_report_race(addr, size, eStore);
358 }
barta79df6e2008-03-14 17:07:51 +0000359}
360
361static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
362{
bart0e5c04f2008-06-09 15:18:59 +0000363 if (running_thread_is_recording()
364 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000365 && bm_access_store_1_triggers_conflict(addr)
366 && ! drd_is_suppressed(addr, addr + 1))
bart3772a982008-03-15 08:11:03 +0000367 {
368 drd_report_race(addr, 1, eStore);
369 }
barta79df6e2008-03-14 17:07:51 +0000370}
371
372static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
373{
bart0e5c04f2008-06-09 15:18:59 +0000374 if (running_thread_is_recording()
375 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000376 && bm_access_store_2_triggers_conflict(addr)
377 && ! drd_is_suppressed(addr, addr + 2))
bart3772a982008-03-15 08:11:03 +0000378 {
379 drd_report_race(addr, 2, eStore);
380 }
barta79df6e2008-03-14 17:07:51 +0000381}
382
383static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
384{
bart0e5c04f2008-06-09 15:18:59 +0000385 if (running_thread_is_recording()
386 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000387 && bm_access_store_4_triggers_conflict(addr)
388 && ! drd_is_suppressed(addr, addr + 4))
bart3772a982008-03-15 08:11:03 +0000389 {
390 drd_report_race(addr, 4, eStore);
391 }
barta79df6e2008-03-14 17:07:51 +0000392}
393
394static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
395{
bart0e5c04f2008-06-09 15:18:59 +0000396 if (running_thread_is_recording()
397 && (s_drd_check_stack_accesses || ! thread_address_on_stack(addr))
bart3e3296d2008-06-08 11:21:34 +0000398 && bm_access_store_8_triggers_conflict(addr)
399 && ! drd_is_suppressed(addr, addr + 8))
bart3772a982008-03-15 08:11:03 +0000400 {
401 drd_report_race(addr, 8, eStore);
402 }
sewardjaf44c822007-11-25 14:01:38 +0000403}
404
405static void drd_pre_mem_read(const CorePart part,
406 const ThreadId tid,
407 Char* const s,
408 const Addr a,
409 const SizeT size)
410{
bart3772a982008-03-15 08:11:03 +0000411 if (size > 0)
412 {
413 drd_trace_load(a, size);
414 }
sewardjaf44c822007-11-25 14:01:38 +0000415}
416
bart5e85d262008-03-01 10:49:37 +0000417static void drd_pre_mem_read_asciiz(const CorePart part,
418 const ThreadId tid,
419 Char* const s,
420 const Addr a)
421{
bart3772a982008-03-15 08:11:03 +0000422 const char* p = (void*)a;
423 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000424
bart3772a982008-03-15 08:11:03 +0000425 /* Note: the expression '*p' reads client memory and may crash if the */
426 /* client provided an invalid pointer ! */
427 while (*p)
428 {
429 p++;
430 size++;
431 }
432 // To do: find out what a reasonable upper limit on 'size' is.
433 tl_assert(size < 4096);
434 if (size > 0)
435 {
436 drd_trace_load(a, size);
437 }
bart5e85d262008-03-01 10:49:37 +0000438}
439
sewardjaf44c822007-11-25 14:01:38 +0000440static void drd_post_mem_write(const CorePart part,
441 const ThreadId tid,
442 const Addr a,
443 const SizeT size)
444{
bart3772a982008-03-15 08:11:03 +0000445 thread_set_vg_running_tid(VG_(get_running_tid)());
446 if (size > 0)
447 {
448 drd_trace_store(a, size);
449 }
sewardjaf44c822007-11-25 14:01:38 +0000450}
451
bart08865622008-06-06 14:31:36 +0000452static __inline__
453void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000454{
bart005dc972008-03-29 14:42:59 +0000455 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000456
bartb9c7d742008-06-10 12:51:51 +0000457 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000458 {
bartd5765912008-03-16 08:40:55 +0000459 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000460 }
sewardjaf44c822007-11-25 14:01:38 +0000461}
462
sewardj7cf4e6b2008-05-01 20:24:26 +0000463static void drd_start_using_mem_w_ecu(const Addr a1,
464 const SizeT len,
465 UInt ec_uniq)
466{
467 drd_start_using_mem(a1, len);
468}
469
470static void drd_start_using_mem_w_tid(const Addr a1,
471 const SizeT len,
472 ThreadId tid)
473{
474 drd_start_using_mem(a1, len);
475}
476
bart0ffa4832008-04-05 12:57:01 +0000477static __inline__
478void drd_stop_using_mem(const Addr a1, const SizeT len,
479 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000480{
bart3772a982008-03-15 08:11:03 +0000481 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000482
bart3772a982008-03-15 08:11:03 +0000483 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000484
bartb9c7d742008-06-10 12:51:51 +0000485 if (UNLIKELY(drd_any_address_is_traced()))
bart3772a982008-03-15 08:11:03 +0000486 {
bartd43f8d32008-03-16 17:29:20 +0000487 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000488 }
bart08865622008-06-06 14:31:36 +0000489 if (! is_stack_mem || s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000490 {
491 thread_stop_using_mem(a1, a2);
492 clientobj_stop_using_mem(a1, a2);
493 drd_suppression_stop_using_mem(a1, a2);
494 }
495}
496
497static __inline__
498void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
499{
500 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000501}
502
bartcb2d0072008-05-31 07:55:51 +0000503/** Suppress data race reports on all addresses contained in .plt and
504 * .got.plt sections inside the address range [ a, a + len [. The data in
505 * these sections is modified by _dl_relocate_object() every time a function
506 * in a shared library is called for the first time. Since the first call
507 * to a function in a shared library can happen from a multithreaded context,
508 * such calls can cause conflicting accesses. See also Ulrich Drepper's
509 * paper "How to Write Shared Libraries" for more information about relocation
510 * (http://people.redhat.com/drepper/dsohowto.pdf).
511 */
512static void suppress_relocation_conflicts(const Addr a, const SizeT len)
513{
514 const DebugInfo* di;
515
516#if 0
517 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
518#endif
519
520 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
521 {
522 Addr avma;
523 SizeT size;
524
525 avma = VG_(seginfo_get_plt_avma)(di);
526 size = VG_(seginfo_get_plt_size)(di);
bartd5beeac2008-07-02 11:47:46 +0000527 if (size > 0 && a <= avma && avma + size <= a + len)
bartcb2d0072008-05-31 07:55:51 +0000528 {
529#if 0
530 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
531#endif
532 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
533 drd_start_suppression(avma, avma + size, ".plt");
534 }
535
536 avma = VG_(seginfo_get_gotplt_avma)(di);
537 size = VG_(seginfo_get_gotplt_size)(di);
bartd5beeac2008-07-02 11:47:46 +0000538 if (size > 0 && a <= avma && avma + size <= a + len)
bartcb2d0072008-05-31 07:55:51 +0000539 {
540#if 0
541 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
542#endif
543 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
544 drd_start_suppression(avma, avma + size, ".gotplt");
545 }
546 }
547}
548
bart5e85d262008-03-01 10:49:37 +0000549static
550void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
551 const Bool rr, const Bool ww, const Bool xx)
552{
bartd5765912008-03-16 08:40:55 +0000553 thread_set_vg_running_tid(VG_(get_running_tid)());
554
bart3772a982008-03-15 08:11:03 +0000555 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000556
557 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000558}
559
sewardjaf44c822007-11-25 14:01:38 +0000560/* Called by the core when the stack of a thread grows, to indicate that */
561/* the addresses in range [ a, a + len [ may now be used by the client. */
562/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000563static __inline__
564void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000565{
bartd43f8d32008-03-16 17:29:20 +0000566 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000567 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
568 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000569}
570
571/* Called by the core when the stack of a thread shrinks, to indicate that */
572/* the addresses [ a, a + len [ are no longer accessible for the client. */
573/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000574static __inline__
575void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000576{
bartd43f8d32008-03-16 17:29:20 +0000577 thread_set_stack_min(thread_get_running_tid(),
578 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000579 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
580 True);
sewardjaf44c822007-11-25 14:01:38 +0000581}
582
sewardj7cf4e6b2008-05-01 20:24:26 +0000583static void drd_start_using_mem_stack_signal(
584 const Addr a, const SizeT len,
585 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000586{
bartd5765912008-03-16 08:40:55 +0000587 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000588 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000589}
590
bart5e85d262008-03-01 10:49:37 +0000591static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000592{
bart0ffa4832008-04-05 12:57:01 +0000593 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000594}
595
596static
597void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
598{
bart3772a982008-03-15 08:11:03 +0000599 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
600 tl_assert(created != VG_INVALID_THREADID);
601 thread_pre_create(drd_creator, created);
602 if (IsValidDrdThreadId(drd_creator))
603 {
604 thread_new_segment(drd_creator);
605 }
bartbd7e56e2008-03-31 18:14:12 +0000606 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000607 {
608 VG_(message)(Vg_DebugMsg,
609 "drd_pre_thread_create creator = %d/%d, created = %d",
610 creator, drd_creator, created);
611 }
sewardjaf44c822007-11-25 14:01:38 +0000612}
613
614/* Called by Valgrind's core before any loads or stores are performed on */
615/* the context of thread "created". At startup, this function is called */
616/* with arguments (0,1). */
617static
bart0ffa4832008-04-05 12:57:01 +0000618void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000619{
bart0ffa4832008-04-05 12:57:01 +0000620 DrdThreadId drd_created;
621
622 tl_assert(vg_created != VG_INVALID_THREADID);
623
624 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000625 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000626 {
627 VG_(message)(Vg_DebugMsg,
628 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000629 vg_created, drd_created);
630 }
bart08865622008-06-06 14:31:36 +0000631 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000632 {
633 drd_start_suppression(thread_get_stack_max(drd_created)
634 - thread_get_stack_size(drd_created),
635 thread_get_stack_max(drd_created),
636 "stack");
bart3772a982008-03-15 08:11:03 +0000637 }
sewardjaf44c822007-11-25 14:01:38 +0000638}
639
640/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
641/* after thread drd_joiner joined thread drd_joinee. */
642void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
643{
bart3772a982008-03-15 08:11:03 +0000644 tl_assert(IsValidDrdThreadId(drd_joiner));
645 tl_assert(IsValidDrdThreadId(drd_joinee));
646 thread_new_segment(drd_joinee);
647 thread_combine_vc(drd_joiner, drd_joinee);
648 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000649
bartbd7e56e2008-03-31 18:14:12 +0000650 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000651 {
bartfdd8d4e2008-04-01 18:38:29 +0000652 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
653 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000654 const unsigned msg_size = 256;
655 char* msg;
656
657 msg = VG_(malloc)(msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000658 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000659 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000660 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
661 joiner, drd_joiner, joinee, drd_joinee);
662 if (joiner)
663 {
bart024a95a2008-04-01 18:27:41 +0000664 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000665 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000666 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000667 thread_get_vc(drd_joiner));
668 }
669 VG_(message)(Vg_DebugMsg, msg);
bart024a95a2008-04-01 18:27:41 +0000670 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000671 }
sewardjaf44c822007-11-25 14:01:38 +0000672
bart08865622008-06-06 14:31:36 +0000673 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000674 {
675 drd_finish_suppression(thread_get_stack_max(drd_joinee)
676 - thread_get_stack_size(drd_joinee),
677 thread_get_stack_max(drd_joinee));
678 }
bart3772a982008-03-15 08:11:03 +0000679 thread_delete(drd_joinee);
680 mutex_thread_delete(drd_joinee);
681 cond_thread_delete(drd_joinee);
682 semaphore_thread_delete(drd_joinee);
683 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000684}
685
bart5bd9f2d2008-03-03 20:31:58 +0000686
sewardjaf44c822007-11-25 14:01:38 +0000687/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000688static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000689{
bart3772a982008-03-15 08:11:03 +0000690 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000691
bartd43f8d32008-03-16 17:29:20 +0000692 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000693
bartd43f8d32008-03-16 17:29:20 +0000694 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000695 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000696 {
697 VG_(message)(Vg_DebugMsg,
698 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000699 vg_tid,
bart3772a982008-03-15 08:11:03 +0000700 drd_tid,
701 thread_get_joinable(drd_tid)
702 ? ""
703 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000704 }
705 if (s_show_stack_usage)
706 {
707 const SizeT stack_size = thread_get_stack_size(drd_tid);
708 const SizeT used_stack
709 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
710 VG_(message)(Vg_UserMsg,
711 "thread %d/%d%s finished and used %ld bytes out of %ld"
712 " on its stack. Margin: %ld bytes.",
713 vg_tid,
714 drd_tid,
715 thread_get_joinable(drd_tid)
716 ? ""
717 : " (which is a detached thread)",
718 used_stack,
719 stack_size,
720 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000721
bart3772a982008-03-15 08:11:03 +0000722 }
bartd43f8d32008-03-16 17:29:20 +0000723 drd_stop_using_mem(thread_get_stack_min(drd_tid),
724 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000725 - thread_get_stack_min(drd_tid),
726 True);
bartd43f8d32008-03-16 17:29:20 +0000727 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000728 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000729}
730
bart0268dfa2008-03-11 20:10:21 +0000731void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000732{
bart3772a982008-03-15 08:11:03 +0000733 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000734}
735
sewardj347eeba2008-01-21 14:19:07 +0000736void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000737{
bart3772a982008-03-15 08:11:03 +0000738 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000739}
740
bart2e3a3c12008-03-24 08:33:47 +0000741void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
742 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000743{
bart2e3a3c12008-03-24 08:33:47 +0000744 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000745}
746
bart00344642008-03-01 15:27:41 +0000747void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000748{
bart4a975e12008-03-30 13:28:33 +0000749 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000750}
751
bart00344642008-03-01 15:27:41 +0000752void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000753{
bart3772a982008-03-15 08:11:03 +0000754 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000755}
756
bart0268dfa2008-03-11 20:10:21 +0000757void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000758{
bart3772a982008-03-15 08:11:03 +0000759 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000760}
761
bart72b751c2008-03-01 13:44:24 +0000762void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000763{
bart3772a982008-03-15 08:11:03 +0000764 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000765}
766
bart0268dfa2008-03-11 20:10:21 +0000767void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000768 const Word pshared, const Word value)
769{
bart3772a982008-03-15 08:11:03 +0000770 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000771}
772
773void drd_semaphore_destroy(const Addr semaphore)
774{
bart3772a982008-03-15 08:11:03 +0000775 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000776}
777
bart0268dfa2008-03-11 20:10:21 +0000778void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000779{
bart3772a982008-03-15 08:11:03 +0000780 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000781}
782
783void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
784 const Bool waited)
785{
bart3772a982008-03-15 08:11:03 +0000786 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000787}
788
bart0268dfa2008-03-11 20:10:21 +0000789void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000790{
bart3772a982008-03-15 08:11:03 +0000791 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000792}
793
794void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000795 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000796{
bart3772a982008-03-15 08:11:03 +0000797 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000798}
799
800
bart0268dfa2008-03-11 20:10:21 +0000801void drd_barrier_init(const Addr barrier,
802 const BarrierT barrier_type, const Word count,
803 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000804{
bart3772a982008-03-15 08:11:03 +0000805 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000806}
807
bart0268dfa2008-03-11 20:10:21 +0000808void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000809{
bart3772a982008-03-15 08:11:03 +0000810 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000811}
812
bart0268dfa2008-03-11 20:10:21 +0000813void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
814 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000815{
bart3772a982008-03-15 08:11:03 +0000816 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000817}
818
819void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000820 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000821{
bart3772a982008-03-15 08:11:03 +0000822 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000823}
824
sewardjaf44c822007-11-25 14:01:38 +0000825
826//
827// Implementation of the tool interface.
828//
829
830static
831void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000832{
bart5403f212008-06-30 10:56:18 +0000833# if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \
834 || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bart3772a982008-03-15 08:11:03 +0000835 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000836# else
bart5403f212008-06-30 10:56:18 +0000837 VG_(printf)("\nWARNING: DRD has only been tested on 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
barteacd9162008-06-16 20:22:18 +0000846#if defined(VGA_x86)
847#define STACK_POINTER_OFFSET OFFSET_x86_ESP
848#elif defined(VGA_amd64)
849#define STACK_POINTER_OFFSET OFFSET_amd64_RSP
850#elif defined(VGA_ppc32)
851#define STACK_POINTER_OFFSET ((OFFSET_ppc32_GPR0 + OFFSET_ppc32_GPR2) / 2)
852#elif defined(VGA_ppc64)
853#define STACK_POINTER_OFFSET ((OFFSET_ppc64_GPR0 + OFFSET_ppc64_GPR2) / 2)
854#else
855#error Unknown architecture.
856#endif
857
858
859/** Return true if and only if addr_expr matches the pattern (SP) or
860 * <offset>(SP).
861 */
862static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr)
863{
864 Bool result = False;
865
866 if (addr_expr->tag == Iex_RdTmp)
867 {
868 int i;
869 for (i = 0; i < bb->stmts_size; i++)
870 {
871 if (bb->stmts[i]
872 && bb->stmts[i]->tag == Ist_WrTmp
873 && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp)
874 {
875 IRExpr* e = bb->stmts[i]->Ist.WrTmp.data;
876 if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET)
877 {
878 result = True;
879 }
880
881 //ppIRExpr(e);
882 //VG_(printf)(" (%s)\n", result ? "True" : "False");
883 break;
884 }
885 }
886 }
887 return result;
888}
889
barta79df6e2008-03-14 17:07:51 +0000890static void instrument_load(IRSB* const bb,
891 IRExpr* const addr_expr,
892 const HWord size)
893{
bart3772a982008-03-15 08:11:03 +0000894 IRExpr* size_expr;
895 IRExpr** argv;
896 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000897
bart29a0e2a2008-06-10 13:55:13 +0000898 if (UNLIKELY(drd_any_address_is_traced()))
899 {
900 addStmtToIRSB(bb,
901 IRStmt_Dirty(
902 unsafeIRDirty_0_N(/*regparms*/2,
903 "drd_trace_load",
904 VG_(fnptr_to_fnentry)
905 (drd_trace_mem_load),
906 mkIRExprVec_2(addr_expr,
907 mkIRExpr_HWord(size)))));
908 }
909
barteacd9162008-06-16 20:22:18 +0000910 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
911 return;
912
bart3772a982008-03-15 08:11:03 +0000913 switch (size)
914 {
915 case 1:
916 argv = mkIRExprVec_1(addr_expr);
917 di = unsafeIRDirty_0_N(/*regparms*/1,
918 "drd_trace_load_1",
919 VG_(fnptr_to_fnentry)(drd_trace_load_1),
920 argv);
921 break;
922 case 2:
923 argv = mkIRExprVec_1(addr_expr);
924 di = unsafeIRDirty_0_N(/*regparms*/1,
925 "drd_trace_load_2",
926 VG_(fnptr_to_fnentry)(drd_trace_load_2),
927 argv);
928 break;
929 case 4:
930 argv = mkIRExprVec_1(addr_expr);
931 di = unsafeIRDirty_0_N(/*regparms*/1,
932 "drd_trace_load_4",
933 VG_(fnptr_to_fnentry)(drd_trace_load_4),
934 argv);
935 break;
936 case 8:
937 argv = mkIRExprVec_1(addr_expr);
938 di = unsafeIRDirty_0_N(/*regparms*/1,
939 "drd_trace_load_8",
940 VG_(fnptr_to_fnentry)(drd_trace_load_8),
941 argv);
942 break;
943 default:
944 size_expr = mkIRExpr_HWord(size);
945 argv = mkIRExprVec_2(addr_expr, size_expr);
946 di = unsafeIRDirty_0_N(/*regparms*/2,
947 "drd_trace_load",
948 VG_(fnptr_to_fnentry)(drd_trace_load),
949 argv);
950 break;
951 }
952 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000953}
954
955static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000956 IRExpr* const addr_expr,
957 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000958{
bart3772a982008-03-15 08:11:03 +0000959 IRExpr* size_expr;
960 IRExpr** argv;
961 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000962
bart29a0e2a2008-06-10 13:55:13 +0000963 if (UNLIKELY(drd_any_address_is_traced()))
964 {
965 addStmtToIRSB(bb,
966 IRStmt_Dirty(
967 unsafeIRDirty_0_N(/*regparms*/2,
968 "drd_trace_store",
969 VG_(fnptr_to_fnentry)
970 (drd_trace_mem_store),
971 mkIRExprVec_2(addr_expr,
972 mkIRExpr_HWord(size)))));
973 }
974
barteacd9162008-06-16 20:22:18 +0000975 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
976 return;
977
bart3772a982008-03-15 08:11:03 +0000978 switch (size)
979 {
980 case 1:
981 argv = mkIRExprVec_1(addr_expr);
982 di = unsafeIRDirty_0_N(/*regparms*/1,
983 "drd_trace_store_1",
984 VG_(fnptr_to_fnentry)(drd_trace_store_1),
985 argv);
986 break;
987 case 2:
988 argv = mkIRExprVec_1(addr_expr);
989 di = unsafeIRDirty_0_N(/*regparms*/1,
990 "drd_trace_store_2",
991 VG_(fnptr_to_fnentry)(drd_trace_store_2),
992 argv);
993 break;
994 case 4:
995 argv = mkIRExprVec_1(addr_expr);
996 di = unsafeIRDirty_0_N(/*regparms*/1,
997 "drd_trace_store_4",
998 VG_(fnptr_to_fnentry)(drd_trace_store_4),
999 argv);
1000 break;
1001 case 8:
1002 argv = mkIRExprVec_1(addr_expr);
1003 di = unsafeIRDirty_0_N(/*regparms*/1,
1004 "drd_trace_store_8",
1005 VG_(fnptr_to_fnentry)(drd_trace_store_8),
1006 argv);
1007 break;
1008 default:
1009 size_expr = mkIRExpr_HWord(size);
1010 argv = mkIRExprVec_2(addr_expr, size_expr);
1011 di = unsafeIRDirty_0_N(/*regparms*/2,
1012 "drd_trace_store",
1013 VG_(fnptr_to_fnentry)(drd_trace_store),
1014 argv);
1015 break;
1016 }
1017 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +00001018}
1019
sewardjaf44c822007-11-25 14:01:38 +00001020static
1021IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +00001022 IRSB* const bb_in,
1023 VexGuestLayout* const layout,
1024 VexGuestExtents* const vge,
1025 IRType const gWordTy,
1026 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +00001027{
bart3772a982008-03-15 08:11:03 +00001028 IRDirty* di;
1029 Int i;
1030 IRSB* bb;
1031 IRExpr** argv;
1032 Bool instrument = True;
1033 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +00001034
bart3772a982008-03-15 08:11:03 +00001035 /* Set up BB */
1036 bb = emptyIRSB();
1037 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
1038 bb->next = deepCopyIRExpr(bb_in->next);
1039 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +00001040
bart3772a982008-03-15 08:11:03 +00001041 for (i = 0; i < bb_in->stmts_used; i++)
1042 {
1043 IRStmt* const st = bb_in->stmts[i];
1044 tl_assert(st);
1045 if (st->tag == Ist_NoOp)
1046 continue;
sewardjaf44c822007-11-25 14:01:38 +00001047
bart3772a982008-03-15 08:11:03 +00001048 switch (st->tag)
1049 {
bart7c972182008-06-30 13:15:33 +00001050 /* Note: the code for not instrumenting the code in .plt */
1051 /* sections is only necessary on CentOS 3.0 x86 (kernel 2.4.21 */
1052 /* + glibc 2.3.2 + NPTL 0.60 + binutils 2.14.90.0.4). */
1053 /* This is because on this platform dynamic library symbols are */
1054 /* relocated in another way than by later binutils versions. The */
1055 /* linker e.g. does not generate .got.plt sections on CentOS 3.0. */
1056 case Ist_IMark:
1057 instrument = VG_(seginfo_sect_kind)(NULL, 0, st->Ist.IMark.addr)
1058 != Vg_SectPLT;
1059 addStmtToIRSB(bb, st);
1060 break;
1061
bart3772a982008-03-15 08:11:03 +00001062 case Ist_MBE:
1063 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001064 {
bart3772a982008-03-15 08:11:03 +00001065 case Imbe_Fence:
1066 break; /* not interesting */
1067 case Imbe_BusLock:
sewardj9df35c22008-06-30 10:32:54 +00001068 case Imbe_SnoopedStoreBegin:
bart3772a982008-03-15 08:11:03 +00001069 tl_assert(! bus_locked);
1070 bus_locked = True;
1071 break;
1072 case Imbe_BusUnlock:
sewardj9df35c22008-06-30 10:32:54 +00001073 case Imbe_SnoopedStoreEnd:
bart3772a982008-03-15 08:11:03 +00001074 tl_assert(bus_locked);
1075 bus_locked = False;
1076 break;
sewardjaf44c822007-11-25 14:01:38 +00001077 default:
bart3772a982008-03-15 08:11:03 +00001078 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001079 }
bart3772a982008-03-15 08:11:03 +00001080 addStmtToIRSB(bb, st);
1081 break;
sewardjaf44c822007-11-25 14:01:38 +00001082
bart3772a982008-03-15 08:11:03 +00001083 case Ist_Store:
1084 if (instrument && ! bus_locked)
1085 {
1086 instrument_store(bb,
1087 st->Ist.Store.addr,
1088 sizeofIRType(typeOfIRExpr(bb->tyenv,
1089 st->Ist.Store.data)));
1090 }
1091 addStmtToIRSB(bb, st);
1092 break;
barta47b3512008-03-07 17:22:26 +00001093
bart3772a982008-03-15 08:11:03 +00001094 case Ist_WrTmp:
1095 if (instrument)
1096 {
1097 const IRExpr* const data = st->Ist.WrTmp.data;
1098 if (data->tag == Iex_Load)
1099 {
1100 instrument_load(bb,
1101 data->Iex.Load.addr,
1102 sizeofIRType(data->Iex.Load.ty));
1103 }
1104 }
1105 addStmtToIRSB(bb, st);
1106 break;
1107
1108 case Ist_Dirty:
1109 if (instrument)
1110 {
1111 IRDirty* d = st->Ist.Dirty.details;
1112 IREffect const mFx = d->mFx;
1113 switch (mFx) {
1114 case Ifx_None:
1115 break;
1116 case Ifx_Read:
1117 case Ifx_Write:
1118 case Ifx_Modify:
1119 tl_assert(d->mAddr);
1120 tl_assert(d->mSize > 0);
1121 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1122 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1123 di = unsafeIRDirty_0_N(
1124 /*regparms*/2,
1125 "drd_trace_load",
1126 VG_(fnptr_to_fnentry)(drd_trace_load),
1127 argv);
1128 addStmtToIRSB(bb, IRStmt_Dirty(di));
1129 }
1130 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1131 && ! bus_locked)
1132 {
1133 di = unsafeIRDirty_0_N(
1134 /*regparms*/2,
1135 "drd_trace_store",
1136 VG_(fnptr_to_fnentry)(drd_trace_store),
1137 argv);
1138 addStmtToIRSB(bb, IRStmt_Dirty(di));
1139 }
1140 break;
1141 default:
1142 tl_assert(0);
1143 }
1144 }
1145 addStmtToIRSB(bb, st);
1146 break;
1147
1148 default:
1149 addStmtToIRSB(bb, st);
1150 break;
1151 }
1152 }
1153
1154 tl_assert(! bus_locked);
1155
1156 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001157}
1158
sewardjaf44c822007-11-25 14:01:38 +00001159static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1160{
bart3772a982008-03-15 08:11:03 +00001161 tl_assert(tid == VG_(get_running_tid)());
1162 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001163}
1164
1165static
1166void drd_fini(Int exitcode)
1167{
bart3772a982008-03-15 08:11:03 +00001168 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001169 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001170 {
barte73b0aa2008-06-28 07:19:56 +00001171 ULong update_conflict_set_count;
barte4504dd2008-04-06 15:02:58 +00001172 ULong dsnsc;
1173 ULong dscvc;
1174
barte73b0aa2008-06-28 07:19:56 +00001175 update_conflict_set_count
1176 = thread_get_update_conflict_set_count(&dsnsc, &dscvc);
barte4504dd2008-04-06 15:02:58 +00001177
bartbd7e56e2008-03-31 18:14:12 +00001178 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001179 " thread: %lld context switches"
barte73b0aa2008-06-28 07:19:56 +00001180 " / %lld updates of the conflict set",
bart3772a982008-03-15 08:11:03 +00001181 thread_get_context_switch_count(),
barte73b0aa2008-06-28 07:19:56 +00001182 update_conflict_set_count);
barte4504dd2008-04-06 15:02:58 +00001183 VG_(message)(Vg_UserMsg,
1184 " (%lld new sg + %lld combine vc + %lld csw).",
1185 dsnsc,
1186 dscvc,
barte73b0aa2008-06-28 07:19:56 +00001187 update_conflict_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001188 VG_(message)(Vg_UserMsg,
1189 " segments: created %lld segments, max %lld alive,"
1190 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001191 sg_get_created_segments_count(),
1192 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001193 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001194 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001195 " (%lld m, %lld rw, %lld s, %lld b)",
1196 get_mutex_segment_creation_count(),
1197 get_rwlock_segment_creation_count(),
1198 get_semaphore_segment_creation_count(),
1199 get_barrier_segment_creation_count());
1200 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001201 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001202 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001203 bm_get_bitmap2_node_creation_count());
1204 VG_(message)(Vg_UserMsg,
1205 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001206 bm_get_bitmap2_creation_count());
1207 VG_(message)(Vg_UserMsg,
1208 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001209 get_mutex_lock_count());
1210 drd_print_malloc_stats();
1211 }
sewardjaf44c822007-11-25 14:01:38 +00001212}
1213
sewardjaf44c822007-11-25 14:01:38 +00001214static
1215void drd_pre_clo_init(void)
1216{
bart3772a982008-03-15 08:11:03 +00001217 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001218
bartef1b9722008-07-04 15:34:23 +00001219 VG_(details_name) ("drd");
bart3772a982008-03-15 08:11:03 +00001220 VG_(details_version) (NULL);
bartc821a942008-07-04 14:48:39 +00001221 VG_(details_description) ("a thread error detector");
bart3772a982008-03-15 08:11:03 +00001222 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1223 " by Bart Van Assche.");
1224 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001225
bart3772a982008-03-15 08:11:03 +00001226 VG_(basic_tool_funcs) (drd_post_clo_init,
1227 drd_instrument,
1228 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001229
bart3772a982008-03-15 08:11:03 +00001230 // Command line stuff.
1231 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1232 drd_print_usage,
1233 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001234
bart3772a982008-03-15 08:11:03 +00001235 // Error handling.
1236 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001237
bart3772a982008-03-15 08:11:03 +00001238 // Core event tracking.
1239 VG_(track_pre_mem_read) (drd_pre_mem_read);
1240 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1241 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001242 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001243 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1244 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1245 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1246 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001247 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1248 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001249 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1250 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1251 VG_(track_start_client_code) (drd_start_client_code);
1252 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1253 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1254 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001255
bart3772a982008-03-15 08:11:03 +00001256 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001257 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001258 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001259
bart3772a982008-03-15 08:11:03 +00001260 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001261
bart3772a982008-03-15 08:11:03 +00001262 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001263
bart3772a982008-03-15 08:11:03 +00001264 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001265}
1266
1267
1268VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)