blob: 5f49203eb648519f93d3e88791313a5a8ad44b78 [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,
sewardj9c606bd2008-09-18 18:12:50 +0000551 const Bool rr, const Bool ww, const Bool xx,
552 ULong di_handle)
bart5e85d262008-03-01 10:49:37 +0000553{
bartd5765912008-03-16 08:40:55 +0000554 thread_set_vg_running_tid(VG_(get_running_tid)());
555
bart3772a982008-03-15 08:11:03 +0000556 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000557
558 suppress_relocation_conflicts(a, len);
bart5e85d262008-03-01 10:49:37 +0000559}
560
sewardjaf44c822007-11-25 14:01:38 +0000561/* Called by the core when the stack of a thread grows, to indicate that */
562/* the addresses in range [ a, a + len [ may now be used by the client. */
563/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000564static __inline__
565void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000566{
bartd43f8d32008-03-16 17:29:20 +0000567 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000568 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
569 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000570}
571
572/* Called by the core when the stack of a thread shrinks, to indicate that */
573/* the addresses [ a, a + len [ are no longer accessible for the client. */
574/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000575static __inline__
576void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000577{
bartd43f8d32008-03-16 17:29:20 +0000578 thread_set_stack_min(thread_get_running_tid(),
579 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000580 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
581 True);
sewardjaf44c822007-11-25 14:01:38 +0000582}
583
sewardj7cf4e6b2008-05-01 20:24:26 +0000584static void drd_start_using_mem_stack_signal(
585 const Addr a, const SizeT len,
586 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000587{
bartd5765912008-03-16 08:40:55 +0000588 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000589 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000590}
591
bart5e85d262008-03-01 10:49:37 +0000592static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000593{
bart0ffa4832008-04-05 12:57:01 +0000594 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000595}
596
597static
598void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
599{
bart3772a982008-03-15 08:11:03 +0000600 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
601 tl_assert(created != VG_INVALID_THREADID);
602 thread_pre_create(drd_creator, created);
603 if (IsValidDrdThreadId(drd_creator))
604 {
605 thread_new_segment(drd_creator);
606 }
bartbd7e56e2008-03-31 18:14:12 +0000607 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000608 {
609 VG_(message)(Vg_DebugMsg,
610 "drd_pre_thread_create creator = %d/%d, created = %d",
611 creator, drd_creator, created);
612 }
sewardjaf44c822007-11-25 14:01:38 +0000613}
614
615/* Called by Valgrind's core before any loads or stores are performed on */
616/* the context of thread "created". At startup, this function is called */
617/* with arguments (0,1). */
618static
bart0ffa4832008-04-05 12:57:01 +0000619void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000620{
bart0ffa4832008-04-05 12:57:01 +0000621 DrdThreadId drd_created;
622
623 tl_assert(vg_created != VG_INVALID_THREADID);
624
625 drd_created = thread_post_create(vg_created);
bartbd7e56e2008-03-31 18:14:12 +0000626 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000627 {
628 VG_(message)(Vg_DebugMsg,
629 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000630 vg_created, drd_created);
631 }
bart08865622008-06-06 14:31:36 +0000632 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000633 {
634 drd_start_suppression(thread_get_stack_max(drd_created)
635 - thread_get_stack_size(drd_created),
636 thread_get_stack_max(drd_created),
637 "stack");
bart3772a982008-03-15 08:11:03 +0000638 }
sewardjaf44c822007-11-25 14:01:38 +0000639}
640
641/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
642/* after thread drd_joiner joined thread drd_joinee. */
643void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
644{
bart3772a982008-03-15 08:11:03 +0000645 tl_assert(IsValidDrdThreadId(drd_joiner));
646 tl_assert(IsValidDrdThreadId(drd_joinee));
647 thread_new_segment(drd_joinee);
648 thread_combine_vc(drd_joiner, drd_joinee);
649 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000650
bartbd7e56e2008-03-31 18:14:12 +0000651 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000652 {
bartfdd8d4e2008-04-01 18:38:29 +0000653 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
654 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
bart024a95a2008-04-01 18:27:41 +0000655 const unsigned msg_size = 256;
656 char* msg;
657
sewardj9c606bd2008-09-18 18:12:50 +0000658 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
bartfdd8d4e2008-04-01 18:38:29 +0000659 tl_assert(msg);
bart024a95a2008-04-01 18:27:41 +0000660 VG_(snprintf)(msg, msg_size,
bart3772a982008-03-15 08:11:03 +0000661 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
662 joiner, drd_joiner, joinee, drd_joinee);
663 if (joiner)
664 {
bart024a95a2008-04-01 18:27:41 +0000665 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000666 ", new vc: ");
bart024a95a2008-04-01 18:27:41 +0000667 vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000668 thread_get_vc(drd_joiner));
669 }
bartd6dce012008-07-05 16:22:36 +0000670 VG_(message)(Vg_DebugMsg, "%s", msg);
bart024a95a2008-04-01 18:27:41 +0000671 VG_(free)(msg);
bart3772a982008-03-15 08:11:03 +0000672 }
sewardjaf44c822007-11-25 14:01:38 +0000673
bart08865622008-06-06 14:31:36 +0000674 if (! s_drd_check_stack_accesses)
bart0ffa4832008-04-05 12:57:01 +0000675 {
676 drd_finish_suppression(thread_get_stack_max(drd_joinee)
677 - thread_get_stack_size(drd_joinee),
678 thread_get_stack_max(drd_joinee));
679 }
bart3772a982008-03-15 08:11:03 +0000680 thread_delete(drd_joinee);
681 mutex_thread_delete(drd_joinee);
682 cond_thread_delete(drd_joinee);
683 semaphore_thread_delete(drd_joinee);
684 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000685}
686
bart5bd9f2d2008-03-03 20:31:58 +0000687
sewardjaf44c822007-11-25 14:01:38 +0000688/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000689static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000690{
bart3772a982008-03-15 08:11:03 +0000691 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000692
bartd43f8d32008-03-16 17:29:20 +0000693 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000694
bartd43f8d32008-03-16 17:29:20 +0000695 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bartbd7e56e2008-03-31 18:14:12 +0000696 if (s_drd_trace_fork_join)
bart3772a982008-03-15 08:11:03 +0000697 {
698 VG_(message)(Vg_DebugMsg,
699 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000700 vg_tid,
bart3772a982008-03-15 08:11:03 +0000701 drd_tid,
702 thread_get_joinable(drd_tid)
703 ? ""
704 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000705 }
706 if (s_show_stack_usage)
707 {
708 const SizeT stack_size = thread_get_stack_size(drd_tid);
709 const SizeT used_stack
710 = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid);
711 VG_(message)(Vg_UserMsg,
712 "thread %d/%d%s finished and used %ld bytes out of %ld"
713 " on its stack. Margin: %ld bytes.",
714 vg_tid,
715 drd_tid,
716 thread_get_joinable(drd_tid)
717 ? ""
718 : " (which is a detached thread)",
719 used_stack,
720 stack_size,
721 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000722
bart3772a982008-03-15 08:11:03 +0000723 }
bartd43f8d32008-03-16 17:29:20 +0000724 drd_stop_using_mem(thread_get_stack_min(drd_tid),
725 thread_get_stack_max(drd_tid)
bart0ffa4832008-04-05 12:57:01 +0000726 - thread_get_stack_min(drd_tid),
727 True);
bartd43f8d32008-03-16 17:29:20 +0000728 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000729 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000730}
731
bart0268dfa2008-03-11 20:10:21 +0000732void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000733{
bart3772a982008-03-15 08:11:03 +0000734 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000735}
736
sewardj347eeba2008-01-21 14:19:07 +0000737void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000738{
bart3772a982008-03-15 08:11:03 +0000739 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000740}
741
bart2e3a3c12008-03-24 08:33:47 +0000742void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
743 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000744{
bart2e3a3c12008-03-24 08:33:47 +0000745 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000746}
747
bart00344642008-03-01 15:27:41 +0000748void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000749{
bart4a975e12008-03-30 13:28:33 +0000750 mutex_post_lock(mutex, took_lock, False);
sewardjaf44c822007-11-25 14:01:38 +0000751}
752
bart00344642008-03-01 15:27:41 +0000753void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000754{
bart3772a982008-03-15 08:11:03 +0000755 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000756}
757
bart0268dfa2008-03-11 20:10:21 +0000758void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000759{
bart3772a982008-03-15 08:11:03 +0000760 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000761}
762
bart72b751c2008-03-01 13:44:24 +0000763void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000764{
bart3772a982008-03-15 08:11:03 +0000765 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000766}
767
bart0268dfa2008-03-11 20:10:21 +0000768void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000769 const Word pshared, const Word value)
770{
bart3772a982008-03-15 08:11:03 +0000771 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000772}
773
774void drd_semaphore_destroy(const Addr semaphore)
775{
bart3772a982008-03-15 08:11:03 +0000776 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000777}
778
bart0268dfa2008-03-11 20:10:21 +0000779void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000780{
bart3772a982008-03-15 08:11:03 +0000781 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000782}
783
784void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
785 const Bool waited)
786{
bart3772a982008-03-15 08:11:03 +0000787 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000788}
789
bart0268dfa2008-03-11 20:10:21 +0000790void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000791{
bart3772a982008-03-15 08:11:03 +0000792 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000793}
794
795void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000796 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000797{
bart3772a982008-03-15 08:11:03 +0000798 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000799}
800
801
bart0268dfa2008-03-11 20:10:21 +0000802void drd_barrier_init(const Addr barrier,
803 const BarrierT barrier_type, const Word count,
804 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000805{
bart3772a982008-03-15 08:11:03 +0000806 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000807}
808
bart0268dfa2008-03-11 20:10:21 +0000809void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000810{
bart3772a982008-03-15 08:11:03 +0000811 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000812}
813
bart0268dfa2008-03-11 20:10:21 +0000814void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
815 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000816{
bart3772a982008-03-15 08:11:03 +0000817 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000818}
819
820void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000821 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000822{
bart3772a982008-03-15 08:11:03 +0000823 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000824}
825
sewardjaf44c822007-11-25 14:01:38 +0000826
827//
828// Implementation of the tool interface.
829//
830
831static
832void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000833{
bart5403f212008-06-30 10:56:18 +0000834# if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \
835 || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bart3772a982008-03-15 08:11:03 +0000836 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000837# else
bart5403f212008-06-30 10:56:18 +0000838 VG_(printf)("\nWARNING: DRD has only been tested on Linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000839# endif
bart95761b52008-03-29 08:34:03 +0000840
841 if (s_drd_var_info)
842 {
843 VG_(needs_var_info)();
844 }
sewardjdcbb8d32007-11-26 21:34:30 +0000845}
sewardjaf44c822007-11-25 14:01:38 +0000846
barteacd9162008-06-16 20:22:18 +0000847#if defined(VGA_x86)
848#define STACK_POINTER_OFFSET OFFSET_x86_ESP
849#elif defined(VGA_amd64)
850#define STACK_POINTER_OFFSET OFFSET_amd64_RSP
851#elif defined(VGA_ppc32)
852#define STACK_POINTER_OFFSET ((OFFSET_ppc32_GPR0 + OFFSET_ppc32_GPR2) / 2)
853#elif defined(VGA_ppc64)
854#define STACK_POINTER_OFFSET ((OFFSET_ppc64_GPR0 + OFFSET_ppc64_GPR2) / 2)
855#else
856#error Unknown architecture.
857#endif
858
859
860/** Return true if and only if addr_expr matches the pattern (SP) or
861 * <offset>(SP).
862 */
863static Bool is_stack_access(IRSB* const bb, IRExpr* const addr_expr)
864{
865 Bool result = False;
866
867 if (addr_expr->tag == Iex_RdTmp)
868 {
869 int i;
870 for (i = 0; i < bb->stmts_size; i++)
871 {
872 if (bb->stmts[i]
873 && bb->stmts[i]->tag == Ist_WrTmp
874 && bb->stmts[i]->Ist.WrTmp.tmp == addr_expr->Iex.RdTmp.tmp)
875 {
876 IRExpr* e = bb->stmts[i]->Ist.WrTmp.data;
877 if (e->tag == Iex_Get && e->Iex.Get.offset == STACK_POINTER_OFFSET)
878 {
879 result = True;
880 }
881
882 //ppIRExpr(e);
883 //VG_(printf)(" (%s)\n", result ? "True" : "False");
884 break;
885 }
886 }
887 }
888 return result;
889}
890
barta79df6e2008-03-14 17:07:51 +0000891static void instrument_load(IRSB* const bb,
892 IRExpr* const addr_expr,
893 const HWord size)
894{
bart3772a982008-03-15 08:11:03 +0000895 IRExpr* size_expr;
896 IRExpr** argv;
897 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000898
bart29a0e2a2008-06-10 13:55:13 +0000899 if (UNLIKELY(drd_any_address_is_traced()))
900 {
901 addStmtToIRSB(bb,
902 IRStmt_Dirty(
903 unsafeIRDirty_0_N(/*regparms*/2,
904 "drd_trace_load",
905 VG_(fnptr_to_fnentry)
906 (drd_trace_mem_load),
907 mkIRExprVec_2(addr_expr,
908 mkIRExpr_HWord(size)))));
909 }
910
barteacd9162008-06-16 20:22:18 +0000911 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
912 return;
913
bart3772a982008-03-15 08:11:03 +0000914 switch (size)
915 {
916 case 1:
917 argv = mkIRExprVec_1(addr_expr);
918 di = unsafeIRDirty_0_N(/*regparms*/1,
919 "drd_trace_load_1",
920 VG_(fnptr_to_fnentry)(drd_trace_load_1),
921 argv);
922 break;
923 case 2:
924 argv = mkIRExprVec_1(addr_expr);
925 di = unsafeIRDirty_0_N(/*regparms*/1,
926 "drd_trace_load_2",
927 VG_(fnptr_to_fnentry)(drd_trace_load_2),
928 argv);
929 break;
930 case 4:
931 argv = mkIRExprVec_1(addr_expr);
932 di = unsafeIRDirty_0_N(/*regparms*/1,
933 "drd_trace_load_4",
934 VG_(fnptr_to_fnentry)(drd_trace_load_4),
935 argv);
936 break;
937 case 8:
938 argv = mkIRExprVec_1(addr_expr);
939 di = unsafeIRDirty_0_N(/*regparms*/1,
940 "drd_trace_load_8",
941 VG_(fnptr_to_fnentry)(drd_trace_load_8),
942 argv);
943 break;
944 default:
945 size_expr = mkIRExpr_HWord(size);
946 argv = mkIRExprVec_2(addr_expr, size_expr);
947 di = unsafeIRDirty_0_N(/*regparms*/2,
948 "drd_trace_load",
949 VG_(fnptr_to_fnentry)(drd_trace_load),
950 argv);
951 break;
952 }
953 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000954}
955
956static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000957 IRExpr* const addr_expr,
958 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000959{
bart3772a982008-03-15 08:11:03 +0000960 IRExpr* size_expr;
961 IRExpr** argv;
962 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000963
bart29a0e2a2008-06-10 13:55:13 +0000964 if (UNLIKELY(drd_any_address_is_traced()))
965 {
966 addStmtToIRSB(bb,
967 IRStmt_Dirty(
968 unsafeIRDirty_0_N(/*regparms*/2,
969 "drd_trace_store",
970 VG_(fnptr_to_fnentry)
971 (drd_trace_mem_store),
972 mkIRExprVec_2(addr_expr,
973 mkIRExpr_HWord(size)))));
974 }
975
barteacd9162008-06-16 20:22:18 +0000976 if (! s_drd_check_stack_accesses && is_stack_access(bb, addr_expr))
977 return;
978
bart3772a982008-03-15 08:11:03 +0000979 switch (size)
980 {
981 case 1:
982 argv = mkIRExprVec_1(addr_expr);
983 di = unsafeIRDirty_0_N(/*regparms*/1,
984 "drd_trace_store_1",
985 VG_(fnptr_to_fnentry)(drd_trace_store_1),
986 argv);
987 break;
988 case 2:
989 argv = mkIRExprVec_1(addr_expr);
990 di = unsafeIRDirty_0_N(/*regparms*/1,
991 "drd_trace_store_2",
992 VG_(fnptr_to_fnentry)(drd_trace_store_2),
993 argv);
994 break;
995 case 4:
996 argv = mkIRExprVec_1(addr_expr);
997 di = unsafeIRDirty_0_N(/*regparms*/1,
998 "drd_trace_store_4",
999 VG_(fnptr_to_fnentry)(drd_trace_store_4),
1000 argv);
1001 break;
1002 case 8:
1003 argv = mkIRExprVec_1(addr_expr);
1004 di = unsafeIRDirty_0_N(/*regparms*/1,
1005 "drd_trace_store_8",
1006 VG_(fnptr_to_fnentry)(drd_trace_store_8),
1007 argv);
1008 break;
1009 default:
1010 size_expr = mkIRExpr_HWord(size);
1011 argv = mkIRExprVec_2(addr_expr, size_expr);
1012 di = unsafeIRDirty_0_N(/*regparms*/2,
1013 "drd_trace_store",
1014 VG_(fnptr_to_fnentry)(drd_trace_store),
1015 argv);
1016 break;
1017 }
1018 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +00001019}
1020
sewardjaf44c822007-11-25 14:01:38 +00001021static
1022IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +00001023 IRSB* const bb_in,
1024 VexGuestLayout* const layout,
1025 VexGuestExtents* const vge,
1026 IRType const gWordTy,
1027 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +00001028{
bart3772a982008-03-15 08:11:03 +00001029 IRDirty* di;
1030 Int i;
1031 IRSB* bb;
1032 IRExpr** argv;
1033 Bool instrument = True;
1034 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +00001035
bart3772a982008-03-15 08:11:03 +00001036 /* Set up BB */
1037 bb = emptyIRSB();
1038 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
1039 bb->next = deepCopyIRExpr(bb_in->next);
1040 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +00001041
bart3772a982008-03-15 08:11:03 +00001042 for (i = 0; i < bb_in->stmts_used; i++)
1043 {
1044 IRStmt* const st = bb_in->stmts[i];
1045 tl_assert(st);
1046 if (st->tag == Ist_NoOp)
1047 continue;
sewardjaf44c822007-11-25 14:01:38 +00001048
bart3772a982008-03-15 08:11:03 +00001049 switch (st->tag)
1050 {
bart7c972182008-06-30 13:15:33 +00001051 /* Note: the code for not instrumenting the code in .plt */
1052 /* sections is only necessary on CentOS 3.0 x86 (kernel 2.4.21 */
1053 /* + glibc 2.3.2 + NPTL 0.60 + binutils 2.14.90.0.4). */
1054 /* This is because on this platform dynamic library symbols are */
1055 /* relocated in another way than by later binutils versions. The */
1056 /* linker e.g. does not generate .got.plt sections on CentOS 3.0. */
1057 case Ist_IMark:
1058 instrument = VG_(seginfo_sect_kind)(NULL, 0, st->Ist.IMark.addr)
1059 != Vg_SectPLT;
1060 addStmtToIRSB(bb, st);
1061 break;
1062
bart3772a982008-03-15 08:11:03 +00001063 case Ist_MBE:
1064 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +00001065 {
bart3772a982008-03-15 08:11:03 +00001066 case Imbe_Fence:
1067 break; /* not interesting */
1068 case Imbe_BusLock:
sewardj9df35c22008-06-30 10:32:54 +00001069 case Imbe_SnoopedStoreBegin:
bart3772a982008-03-15 08:11:03 +00001070 tl_assert(! bus_locked);
1071 bus_locked = True;
1072 break;
1073 case Imbe_BusUnlock:
sewardj9df35c22008-06-30 10:32:54 +00001074 case Imbe_SnoopedStoreEnd:
bart3772a982008-03-15 08:11:03 +00001075 tl_assert(bus_locked);
1076 bus_locked = False;
1077 break;
sewardjaf44c822007-11-25 14:01:38 +00001078 default:
bart3772a982008-03-15 08:11:03 +00001079 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +00001080 }
bart3772a982008-03-15 08:11:03 +00001081 addStmtToIRSB(bb, st);
1082 break;
sewardjaf44c822007-11-25 14:01:38 +00001083
bart3772a982008-03-15 08:11:03 +00001084 case Ist_Store:
1085 if (instrument && ! bus_locked)
1086 {
1087 instrument_store(bb,
1088 st->Ist.Store.addr,
1089 sizeofIRType(typeOfIRExpr(bb->tyenv,
1090 st->Ist.Store.data)));
1091 }
1092 addStmtToIRSB(bb, st);
1093 break;
barta47b3512008-03-07 17:22:26 +00001094
bart3772a982008-03-15 08:11:03 +00001095 case Ist_WrTmp:
1096 if (instrument)
1097 {
1098 const IRExpr* const data = st->Ist.WrTmp.data;
1099 if (data->tag == Iex_Load)
1100 {
1101 instrument_load(bb,
1102 data->Iex.Load.addr,
1103 sizeofIRType(data->Iex.Load.ty));
1104 }
1105 }
1106 addStmtToIRSB(bb, st);
1107 break;
1108
1109 case Ist_Dirty:
1110 if (instrument)
1111 {
1112 IRDirty* d = st->Ist.Dirty.details;
1113 IREffect const mFx = d->mFx;
1114 switch (mFx) {
1115 case Ifx_None:
1116 break;
1117 case Ifx_Read:
1118 case Ifx_Write:
1119 case Ifx_Modify:
1120 tl_assert(d->mAddr);
1121 tl_assert(d->mSize > 0);
1122 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
1123 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
1124 di = unsafeIRDirty_0_N(
1125 /*regparms*/2,
1126 "drd_trace_load",
1127 VG_(fnptr_to_fnentry)(drd_trace_load),
1128 argv);
1129 addStmtToIRSB(bb, IRStmt_Dirty(di));
1130 }
1131 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
1132 && ! bus_locked)
1133 {
1134 di = unsafeIRDirty_0_N(
1135 /*regparms*/2,
1136 "drd_trace_store",
1137 VG_(fnptr_to_fnentry)(drd_trace_store),
1138 argv);
1139 addStmtToIRSB(bb, IRStmt_Dirty(di));
1140 }
1141 break;
1142 default:
1143 tl_assert(0);
1144 }
1145 }
1146 addStmtToIRSB(bb, st);
1147 break;
1148
1149 default:
1150 addStmtToIRSB(bb, st);
1151 break;
1152 }
1153 }
1154
1155 tl_assert(! bus_locked);
1156
1157 return bb;
sewardjaf44c822007-11-25 14:01:38 +00001158}
1159
sewardjaf44c822007-11-25 14:01:38 +00001160static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
1161{
bart3772a982008-03-15 08:11:03 +00001162 tl_assert(tid == VG_(get_running_tid)());
1163 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +00001164}
1165
1166static
1167void drd_fini(Int exitcode)
1168{
bart3772a982008-03-15 08:11:03 +00001169 // thread_print_all();
bartbd7e56e2008-03-31 18:14:12 +00001170 if (VG_(clo_verbosity) > 1 || s_drd_print_stats)
bart3772a982008-03-15 08:11:03 +00001171 {
barte73b0aa2008-06-28 07:19:56 +00001172 ULong update_conflict_set_count;
barte4504dd2008-04-06 15:02:58 +00001173 ULong dsnsc;
1174 ULong dscvc;
1175
barte73b0aa2008-06-28 07:19:56 +00001176 update_conflict_set_count
1177 = thread_get_update_conflict_set_count(&dsnsc, &dscvc);
barte4504dd2008-04-06 15:02:58 +00001178
bartbd7e56e2008-03-31 18:14:12 +00001179 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +00001180 " thread: %lld context switches"
barte73b0aa2008-06-28 07:19:56 +00001181 " / %lld updates of the conflict set",
bart3772a982008-03-15 08:11:03 +00001182 thread_get_context_switch_count(),
barte73b0aa2008-06-28 07:19:56 +00001183 update_conflict_set_count);
barte4504dd2008-04-06 15:02:58 +00001184 VG_(message)(Vg_UserMsg,
1185 " (%lld new sg + %lld combine vc + %lld csw).",
1186 dsnsc,
1187 dscvc,
barte73b0aa2008-06-28 07:19:56 +00001188 update_conflict_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +00001189 VG_(message)(Vg_UserMsg,
1190 " segments: created %lld segments, max %lld alive,"
1191 " %lld discard points.",
bart7102f102008-03-17 17:37:53 +00001192 sg_get_created_segments_count(),
1193 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +00001194 thread_get_discard_ordered_segments_count());
bartbd7e56e2008-03-31 18:14:12 +00001195 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +00001196 " (%lld m, %lld rw, %lld s, %lld b)",
1197 get_mutex_segment_creation_count(),
1198 get_rwlock_segment_creation_count(),
1199 get_semaphore_segment_creation_count(),
1200 get_barrier_segment_creation_count());
1201 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +00001202 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart3772a982008-03-15 08:11:03 +00001203 bm_get_bitmap_creation_count(),
bart952e1a02008-04-06 13:06:36 +00001204 bm_get_bitmap2_node_creation_count());
1205 VG_(message)(Vg_UserMsg,
1206 " and %lld level 2 bitmaps were allocated.",
bartbd7e56e2008-03-31 18:14:12 +00001207 bm_get_bitmap2_creation_count());
1208 VG_(message)(Vg_UserMsg,
1209 " mutex: %lld non-recursive lock/unlock events.",
bart3772a982008-03-15 08:11:03 +00001210 get_mutex_lock_count());
1211 drd_print_malloc_stats();
1212 }
sewardjaf44c822007-11-25 14:01:38 +00001213}
1214
sewardjaf44c822007-11-25 14:01:38 +00001215static
1216void drd_pre_clo_init(void)
1217{
bart3772a982008-03-15 08:11:03 +00001218 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001219
bartef1b9722008-07-04 15:34:23 +00001220 VG_(details_name) ("drd");
bart3772a982008-03-15 08:11:03 +00001221 VG_(details_version) (NULL);
bartc821a942008-07-04 14:48:39 +00001222 VG_(details_description) ("a thread error detector");
bart3772a982008-03-15 08:11:03 +00001223 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1224 " by Bart Van Assche.");
1225 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001226
bart3772a982008-03-15 08:11:03 +00001227 VG_(basic_tool_funcs) (drd_post_clo_init,
1228 drd_instrument,
1229 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001230
bart3772a982008-03-15 08:11:03 +00001231 // Command line stuff.
1232 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1233 drd_print_usage,
1234 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001235
bart3772a982008-03-15 08:11:03 +00001236 // Error handling.
1237 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001238
bart3772a982008-03-15 08:11:03 +00001239 // Core event tracking.
1240 VG_(track_pre_mem_read) (drd_pre_mem_read);
1241 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1242 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +00001243 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +00001244 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1245 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1246 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1247 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +00001248 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
1249 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +00001250 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1251 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1252 VG_(track_start_client_code) (drd_start_client_code);
1253 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1254 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1255 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001256
bart3772a982008-03-15 08:11:03 +00001257 // Other stuff.
sewardj7cf4e6b2008-05-01 20:24:26 +00001258 drd_register_malloc_wrappers(drd_start_using_mem_w_ecu,
bart0ffa4832008-04-05 12:57:01 +00001259 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +00001260
bart3772a982008-03-15 08:11:03 +00001261 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001262
bart3772a982008-03-15 08:11:03 +00001263 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001264
bart3772a982008-03-15 08:11:03 +00001265 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001266}
1267
1268
1269VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)