blob: 4a0734bc8510654a6c45d3fc759358d29f55e880 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
sewardj85642922008-01-14 11:54:56 +000026#include "drd_barrier.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_clientreq.h"
bart4bb53d82008-02-28 19:06:34 +000028#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "drd_cond.h"
30#include "drd_error.h"
31#include "drd_malloc_wrappers.h"
32#include "drd_mutex.h"
bart5bd9f2d2008-03-03 20:31:58 +000033#include "drd_rwlock.h"
sewardjaf44c822007-11-25 14:01:38 +000034#include "drd_segment.h"
sewardj85642922008-01-14 11:54:56 +000035#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "drd_suppression.h"
37#include "drd_thread.h"
38#include "drd_track.h"
39#include "drd_vc.h"
sewardj721ad7b2007-11-30 08:30:29 +000040#include "priv_drd_clientreq.h"
sewardj85642922008-01-14 11:54:56 +000041#include "pub_drd_bitmap.h"
sewardjaf44c822007-11-25 14:01:38 +000042#include "pub_tool_basics.h"
43#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
44#include "pub_tool_libcassert.h" // tl_assert()
45#include "pub_tool_libcbase.h" // VG_(strcmp)
46#include "pub_tool_libcprint.h" // VG_(printf)
sewardj85642922008-01-14 11:54:56 +000047#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
sewardjaf44c822007-11-25 14:01:38 +000048#include "pub_tool_libcproc.h"
49#include "pub_tool_machine.h"
50#include "pub_tool_options.h" // command line options
bartceded212008-03-26 17:39:52 +000051#include "pub_tool_replacemalloc.h"
bart72b751c2008-03-01 13:44:24 +000052#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000053#include "pub_tool_tooliface.h"
bartceded212008-03-26 17:39:52 +000054#include "pub_tool_replacemalloc.h"
sewardjaf44c822007-11-25 14:01:38 +000055
56
sewardjaf44c822007-11-25 14:01:38 +000057// Function declarations.
58
59static void drd_start_client_code(const ThreadId tid, const ULong bbs_done);
sewardjaf44c822007-11-25 14:01:38 +000060
61
62// Local variables.
63
64static Bool drd_print_stats = False;
sewardjaf44c822007-11-25 14:01:38 +000065static Bool drd_trace_fork_join = False;
sewardj85642922008-01-14 11:54:56 +000066static Bool drd_trace_mem = False;
sewardjaf44c822007-11-25 14:01:38 +000067static Addr drd_trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +000068
69
70//
71// Implement the needs_command_line_options for drd.
72//
73
74static Bool drd_process_cmd_line_option(Char* arg)
75{
barta9c37392008-03-22 09:38:48 +000076 int segment_merging = -1;
77 int show_confl_seg = -1;
78 int trace_barrier = -1;
79 int trace_clientobj = -1;
80 int trace_cond = -1;
81 int trace_csw = -1;
82 int trace_danger_set = -1;
83 int trace_mutex = -1;
84 int trace_rwlock = -1;
85 int trace_segment = -1;
86 int trace_semaphore = -1;
87 int trace_suppression = -1;
88 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +000089
bart3772a982008-03-15 08:11:03 +000090 VG_BOOL_CLO (arg, "--drd-stats", drd_print_stats)
barta9c37392008-03-22 09:38:48 +000091 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
bart16d76e52008-03-18 17:08:08 +000092 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
bart3772a982008-03-15 08:11:03 +000093 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
94 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
95 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
96 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
97 else VG_BOOL_CLO(arg, "--trace-danger-set", trace_danger_set)
98 else VG_BOOL_CLO(arg, "--trace-fork-join", drd_trace_fork_join)
99 else VG_BOOL_CLO(arg, "--trace-mem", drd_trace_mem)
100 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
101 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
102 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
103 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
104 else VG_BOOL_CLO(arg, "--trace-suppression", trace_suppression)
105 else VG_STR_CLO (arg, "--trace-address", trace_address)
106 else
bartceded212008-03-26 17:39:52 +0000107 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000108
barta9c37392008-03-22 09:38:48 +0000109 if (segment_merging != -1)
110 thread_set_segment_merging(segment_merging);
111 if (show_confl_seg != -1)
bart16d76e52008-03-18 17:08:08 +0000112 set_show_conflicting_segments(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000113 if (trace_address)
114 {
115 drd_trace_address = VG_(strtoll16)(trace_address, 0);
116 }
barta9c37392008-03-22 09:38:48 +0000117 if (trace_barrier != -1)
bart3772a982008-03-15 08:11:03 +0000118 barrier_set_trace(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000119 if (trace_clientobj != -1)
bart3772a982008-03-15 08:11:03 +0000120 clientobj_set_trace(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000121 if (trace_cond != -1)
bart3772a982008-03-15 08:11:03 +0000122 cond_set_trace(trace_cond);
barta9c37392008-03-22 09:38:48 +0000123 if (trace_csw != -1)
bart3772a982008-03-15 08:11:03 +0000124 thread_trace_context_switches(trace_csw);
barta9c37392008-03-22 09:38:48 +0000125 if (trace_danger_set != -1)
bart3772a982008-03-15 08:11:03 +0000126 thread_trace_danger_set(trace_danger_set);
barta9c37392008-03-22 09:38:48 +0000127 if (trace_mutex != -1)
bart3772a982008-03-15 08:11:03 +0000128 mutex_set_trace(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000129 if (trace_rwlock != -1)
bart3772a982008-03-15 08:11:03 +0000130 rwlock_set_trace(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000131 if (trace_segment != -1)
bart3772a982008-03-15 08:11:03 +0000132 sg_set_trace(trace_segment);
barta9c37392008-03-22 09:38:48 +0000133 if (trace_semaphore != -1)
bart3772a982008-03-15 08:11:03 +0000134 semaphore_set_trace(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000135 if (trace_suppression != -1)
bart3772a982008-03-15 08:11:03 +0000136 suppression_set_trace(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000137
bart3772a982008-03-15 08:11:03 +0000138 return True;
sewardjaf44c822007-11-25 14:01:38 +0000139}
140
141static void drd_print_usage(void)
142{
bart3772a982008-03-15 08:11:03 +0000143 VG_(printf)(" --trace-mem=no|yes Trace all memory accesses to stdout[no]"
144 "\n"
145 " --trace-fork-join=no|yes Trace all thread creation and join"
146 " activity\n"
147 " --trace-mutex=no|yes Trace all mutex activity\n"
148 " --trace-segment=no|yes Trace segment actions\n"
149 );
sewardjaf44c822007-11-25 14:01:38 +0000150}
151
152static void drd_print_debug_usage(void)
153{
154}
155
156
157//
158// Implements the thread-related core callbacks.
159//
160
barta79df6e2008-03-14 17:07:51 +0000161static void drd_trace_mem_access(const Addr addr, const SizeT size,
162 const BmAccessTypeT access_type)
163{
bart3772a982008-03-15 08:11:03 +0000164 char vc[80];
165 vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid()));
166 VG_(message)(Vg_UserMsg,
bart354009c2008-03-16 10:42:33 +0000167 "%s 0x%lx size %ld (vg %d / drd %d / vc %s)",
bartd5765912008-03-16 08:40:55 +0000168 access_type == eLoad
169 ? "load "
170 : access_type == eStore
171 ? "store"
172 : access_type == eStart
173 ? "start"
174 : access_type == eEnd
bartd43f8d32008-03-16 17:29:20 +0000175 ? "end "
bartd5765912008-03-16 08:40:55 +0000176 : "????",
bart3772a982008-03-15 08:11:03 +0000177 addr,
178 size,
bart3772a982008-03-15 08:11:03 +0000179 VG_(get_running_tid)(),
180 thread_get_running_tid(),
181 vc);
182 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
183 VG_(clo_backtrace_size));
184 tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid())
185 == VG_(get_running_tid)());
barta79df6e2008-03-14 17:07:51 +0000186}
187
188static void drd_report_race(const Addr addr, const SizeT size,
189 const BmAccessTypeT access_type)
190{
bart49c3a112008-03-15 10:28:36 +0000191 DataRaceErrInfo drei;
192
bart7a282142008-03-15 08:34:23 +0000193 if (drd_is_suppressed(addr, addr + size))
194 return;
195
bart354009c2008-03-16 10:42:33 +0000196 drei.tid = thread_get_running_tid();
bart3772a982008-03-15 08:11:03 +0000197 drei.addr = addr;
198 drei.size = size;
199 drei.access_type = access_type;
200 VG_(maybe_record_error)(VG_(get_running_tid)(),
201 DataRaceErr,
202 VG_(get_IP)(VG_(get_running_tid)()),
203 "Conflicting accesses",
204 &drei);
barta79df6e2008-03-14 17:07:51 +0000205}
206
207static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000208{
bart3772a982008-03-15 08:11:03 +0000209 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000210
bartf00a85b2008-03-13 18:49:23 +0000211#if 0
bart3772a982008-03-15 08:11:03 +0000212 /* The assert below has been commented out because of performance reasons.*/
213 tl_assert(thread_get_running_tid()
214 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000215#endif
sewardjaf44c822007-11-25 14:01:38 +0000216
bart3772a982008-03-15 08:11:03 +0000217 if (! running_thread_is_recording())
218 return;
bart0268dfa2008-03-11 20:10:21 +0000219
bart3772a982008-03-15 08:11:03 +0000220 if (drd_trace_mem || (addr == drd_trace_address))
221 {
222 drd_trace_mem_access(addr, size, eLoad);
223 }
224 sg = running_thread_get_segment();
225 bm_access_range_load(sg->bm, addr, addr + size);
bart7a282142008-03-15 08:34:23 +0000226 if (bm_load_has_conflict_with(thread_get_danger_set(), addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000227 {
228 drd_report_race(addr, size, eLoad);
229 }
barta79df6e2008-03-14 17:07:51 +0000230}
231
232static VG_REGPARM(1) void drd_trace_load_1(Addr addr)
233{
bart3772a982008-03-15 08:11:03 +0000234 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000235
bart3772a982008-03-15 08:11:03 +0000236 if (! running_thread_is_recording())
237 return;
barta79df6e2008-03-14 17:07:51 +0000238
bart3772a982008-03-15 08:11:03 +0000239 if (drd_trace_mem || (addr == drd_trace_address))
240 {
241 drd_trace_mem_access(addr, 1, eLoad);
242 }
243 sg = running_thread_get_segment();
244 bm_access_load_1(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000245 if (bm_load_1_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000246 {
247 drd_report_race(addr, 1, eLoad);
248 }
barta79df6e2008-03-14 17:07:51 +0000249}
250
251static VG_REGPARM(1) void drd_trace_load_2(Addr addr)
252{
bart3772a982008-03-15 08:11:03 +0000253 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000254
bart3772a982008-03-15 08:11:03 +0000255 if (! running_thread_is_recording())
256 return;
barta79df6e2008-03-14 17:07:51 +0000257
bart3772a982008-03-15 08:11:03 +0000258 if (drd_trace_mem || (addr == drd_trace_address))
259 {
260 drd_trace_mem_access(addr, 2, eLoad);
261 }
262 sg = running_thread_get_segment();
263 bm_access_load_2(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000264 if (bm_load_2_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000265 {
266 drd_report_race(addr, 2, eLoad);
267 }
barta79df6e2008-03-14 17:07:51 +0000268}
269
270static VG_REGPARM(1) void drd_trace_load_4(Addr addr)
271{
bart3772a982008-03-15 08:11:03 +0000272 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000273
bart3772a982008-03-15 08:11:03 +0000274 if (! running_thread_is_recording())
275 return;
barta79df6e2008-03-14 17:07:51 +0000276
bart3772a982008-03-15 08:11:03 +0000277 if (drd_trace_mem || (addr == drd_trace_address))
278 {
279 drd_trace_mem_access(addr, 4, eLoad);
280 }
281 sg = running_thread_get_segment();
282 bm_access_load_4(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000283 if (bm_load_4_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000284 {
285 drd_report_race(addr, 4, eLoad);
286 }
barta79df6e2008-03-14 17:07:51 +0000287}
288
289static VG_REGPARM(1) void drd_trace_load_8(Addr addr)
290{
bart3772a982008-03-15 08:11:03 +0000291 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000292
bart3772a982008-03-15 08:11:03 +0000293 if (! running_thread_is_recording())
294 return;
barta79df6e2008-03-14 17:07:51 +0000295
bart3772a982008-03-15 08:11:03 +0000296 if (drd_trace_mem || (addr == drd_trace_address))
297 {
298 drd_trace_mem_access(addr, 8, eLoad);
299 }
300 sg = running_thread_get_segment();
301 bm_access_load_8(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000302 if (bm_load_8_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000303 {
304 drd_report_race(addr, 8, eLoad);
305 }
sewardjaf44c822007-11-25 14:01:38 +0000306}
307
308static
309VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size)
310{
bart3772a982008-03-15 08:11:03 +0000311 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000312
bartf00a85b2008-03-13 18:49:23 +0000313#if 0
bart3772a982008-03-15 08:11:03 +0000314 /* The assert below has been commented out because of performance reasons.*/
315 tl_assert(thread_get_running_tid()
316 == VgThreadIdToDrdThreadId(VG_(get_running_tid())));
bartf00a85b2008-03-13 18:49:23 +0000317#endif
sewardjaf44c822007-11-25 14:01:38 +0000318
bart3772a982008-03-15 08:11:03 +0000319 if (! running_thread_is_recording())
320 return;
bart0268dfa2008-03-11 20:10:21 +0000321
bart3772a982008-03-15 08:11:03 +0000322 if (drd_trace_mem || (addr == drd_trace_address))
323 {
324 drd_trace_mem_access(addr, size, eStore);
325 }
326 sg = running_thread_get_segment();
327 bm_access_range_store(sg->bm, addr, addr + size);
bart7a282142008-03-15 08:34:23 +0000328 if (bm_store_has_conflict_with(thread_get_danger_set(), addr, addr + size))
bart3772a982008-03-15 08:11:03 +0000329 {
330 drd_report_race(addr, size, eStore);
331 }
barta79df6e2008-03-14 17:07:51 +0000332}
333
334static VG_REGPARM(1) void drd_trace_store_1(Addr addr)
335{
bart3772a982008-03-15 08:11:03 +0000336 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000337
bart3772a982008-03-15 08:11:03 +0000338 if (! running_thread_is_recording())
339 return;
barta79df6e2008-03-14 17:07:51 +0000340
bart3772a982008-03-15 08:11:03 +0000341 if (drd_trace_mem || (addr == drd_trace_address))
342 {
343 drd_trace_mem_access(addr, 1, eStore);
344 }
345 sg = running_thread_get_segment();
346 bm_access_store_1(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000347 if (bm_store_1_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000348 {
349 drd_report_race(addr, 1, eStore);
350 }
barta79df6e2008-03-14 17:07:51 +0000351}
352
353static VG_REGPARM(1) void drd_trace_store_2(Addr addr)
354{
bart3772a982008-03-15 08:11:03 +0000355 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000356
bart3772a982008-03-15 08:11:03 +0000357 if (! running_thread_is_recording())
358 return;
barta79df6e2008-03-14 17:07:51 +0000359
bart3772a982008-03-15 08:11:03 +0000360 if (drd_trace_mem || (addr == drd_trace_address))
361 {
362 drd_trace_mem_access(addr, 2, eStore);
363 }
364 sg = running_thread_get_segment();
365 bm_access_store_2(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000366 if (bm_store_2_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000367 {
368 drd_report_race(addr, 2, eStore);
369 }
barta79df6e2008-03-14 17:07:51 +0000370}
371
372static VG_REGPARM(1) void drd_trace_store_4(Addr addr)
373{
bart3772a982008-03-15 08:11:03 +0000374 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000375
bart3772a982008-03-15 08:11:03 +0000376 if (! running_thread_is_recording())
377 return;
barta79df6e2008-03-14 17:07:51 +0000378
bart3772a982008-03-15 08:11:03 +0000379 if (drd_trace_mem || (addr == drd_trace_address))
380 {
381 drd_trace_mem_access(addr, 4, eStore);
382 }
383 sg = running_thread_get_segment();
384 bm_access_store_4(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000385 if (bm_store_4_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000386 {
387 drd_report_race(addr, 4, eStore);
388 }
barta79df6e2008-03-14 17:07:51 +0000389}
390
391static VG_REGPARM(1) void drd_trace_store_8(Addr addr)
392{
bart3772a982008-03-15 08:11:03 +0000393 Segment* sg;
barta79df6e2008-03-14 17:07:51 +0000394
bart3772a982008-03-15 08:11:03 +0000395 if (! running_thread_is_recording())
396 return;
barta79df6e2008-03-14 17:07:51 +0000397
bart3772a982008-03-15 08:11:03 +0000398 if (drd_trace_mem || (addr == drd_trace_address))
399 {
400 drd_trace_mem_access(addr, 8, eStore);
401 }
402 sg = running_thread_get_segment();
403 bm_access_store_8(sg->bm, addr);
bart7a282142008-03-15 08:34:23 +0000404 if (bm_store_8_has_conflict_with(thread_get_danger_set(), addr))
bart3772a982008-03-15 08:11:03 +0000405 {
406 drd_report_race(addr, 8, eStore);
407 }
sewardjaf44c822007-11-25 14:01:38 +0000408}
409
410static void drd_pre_mem_read(const CorePart part,
411 const ThreadId tid,
412 Char* const s,
413 const Addr a,
414 const SizeT size)
415{
bart3772a982008-03-15 08:11:03 +0000416 if (size > 0)
417 {
418 drd_trace_load(a, size);
419 }
sewardjaf44c822007-11-25 14:01:38 +0000420}
421
bart5e85d262008-03-01 10:49:37 +0000422static void drd_pre_mem_read_asciiz(const CorePart part,
423 const ThreadId tid,
424 Char* const s,
425 const Addr a)
426{
bart3772a982008-03-15 08:11:03 +0000427 const char* p = (void*)a;
428 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000429
bart3772a982008-03-15 08:11:03 +0000430 /* Note: the expression '*p' reads client memory and may crash if the */
431 /* client provided an invalid pointer ! */
432 while (*p)
433 {
434 p++;
435 size++;
436 }
437 // To do: find out what a reasonable upper limit on 'size' is.
438 tl_assert(size < 4096);
439 if (size > 0)
440 {
441 drd_trace_load(a, size);
442 }
bart5e85d262008-03-01 10:49:37 +0000443}
444
sewardjaf44c822007-11-25 14:01:38 +0000445static void drd_post_mem_write(const CorePart part,
446 const ThreadId tid,
447 const Addr a,
448 const SizeT size)
449{
bart3772a982008-03-15 08:11:03 +0000450 thread_set_vg_running_tid(VG_(get_running_tid)());
451 if (size > 0)
452 {
453 drd_trace_store(a, size);
454 }
sewardjaf44c822007-11-25 14:01:38 +0000455}
456
bart5e85d262008-03-01 10:49:37 +0000457static void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000458{
bart3772a982008-03-15 08:11:03 +0000459 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000460
bart3772a982008-03-15 08:11:03 +0000461 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000462
bart3772a982008-03-15 08:11:03 +0000463 if (a1 <= drd_trace_address && drd_trace_address < a2)
464 {
bartd5765912008-03-16 08:40:55 +0000465 drd_trace_mem_access(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000466 }
sewardjaf44c822007-11-25 14:01:38 +0000467}
468
bart5e85d262008-03-01 10:49:37 +0000469static void drd_stop_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000470{
bart3772a982008-03-15 08:11:03 +0000471 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000472
bart3772a982008-03-15 08:11:03 +0000473 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000474
bart3772a982008-03-15 08:11:03 +0000475 if (a1 <= drd_trace_address && drd_trace_address < a2)
476 {
bartd43f8d32008-03-16 17:29:20 +0000477 drd_trace_mem_access(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000478 }
479 thread_stop_using_mem(a1, a2);
480 clientobj_stop_using_mem(a1, a2);
481 drd_suppression_stop_using_mem(a1, a2);
sewardjaf44c822007-11-25 14:01:38 +0000482}
483
bart5e85d262008-03-01 10:49:37 +0000484static
485void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
486 const Bool rr, const Bool ww, const Bool xx)
487{
bartd5765912008-03-16 08:40:55 +0000488 thread_set_vg_running_tid(VG_(get_running_tid)());
489
bart3772a982008-03-15 08:11:03 +0000490 drd_start_using_mem(a, len);
bart5e85d262008-03-01 10:49:37 +0000491}
492
sewardjaf44c822007-11-25 14:01:38 +0000493/* Called by the core when the stack of a thread grows, to indicate that */
494/* the addresses in range [ a, a + len [ may now be used by the client. */
495/* Assumption: stacks grow downward. */
496static void drd_start_using_mem_stack(const Addr a, const SizeT len)
497{
bartd43f8d32008-03-16 17:29:20 +0000498 thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
499 drd_start_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000500}
501
502/* Called by the core when the stack of a thread shrinks, to indicate that */
503/* the addresses [ a, a + len [ are no longer accessible for the client. */
504/* Assumption: stacks grow downward. */
505static void drd_stop_using_mem_stack(const Addr a, const SizeT len)
506{
bartd43f8d32008-03-16 17:29:20 +0000507 thread_set_stack_min(thread_get_running_tid(),
508 a + len - VG_STACK_REDZONE_SZB);
509 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000510}
511
bart5e85d262008-03-01 10:49:37 +0000512static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000513{
bartd5765912008-03-16 08:40:55 +0000514 thread_set_vg_running_tid(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000515 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000516}
517
bart5e85d262008-03-01 10:49:37 +0000518static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000519{
bart3772a982008-03-15 08:11:03 +0000520 drd_stop_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000521}
522
523static
524void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
525{
bart3772a982008-03-15 08:11:03 +0000526 const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator);
527 tl_assert(created != VG_INVALID_THREADID);
528 thread_pre_create(drd_creator, created);
529 if (IsValidDrdThreadId(drd_creator))
530 {
531 thread_new_segment(drd_creator);
532 }
533 if (drd_trace_fork_join)
534 {
535 VG_(message)(Vg_DebugMsg,
536 "drd_pre_thread_create creator = %d/%d, created = %d",
537 creator, drd_creator, created);
538 }
sewardjaf44c822007-11-25 14:01:38 +0000539}
540
541/* Called by Valgrind's core before any loads or stores are performed on */
542/* the context of thread "created". At startup, this function is called */
543/* with arguments (0,1). */
544static
545void drd_post_thread_create(const ThreadId created)
546{
bart3772a982008-03-15 08:11:03 +0000547 const DrdThreadId drd_created = thread_post_create(created);
548 tl_assert(created != VG_INVALID_THREADID);
549 if (drd_trace_fork_join)
550 {
551 VG_(message)(Vg_DebugMsg,
552 "drd_post_thread_create created = %d/%d",
553 created, drd_created);
554 }
sewardjaf44c822007-11-25 14:01:38 +0000555}
556
557/* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */
558/* after thread drd_joiner joined thread drd_joinee. */
559void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
560{
bart3772a982008-03-15 08:11:03 +0000561 tl_assert(IsValidDrdThreadId(drd_joiner));
562 tl_assert(IsValidDrdThreadId(drd_joinee));
563 thread_new_segment(drd_joinee);
564 thread_combine_vc(drd_joiner, drd_joinee);
565 thread_new_segment(drd_joiner);
sewardjaf44c822007-11-25 14:01:38 +0000566
bart3772a982008-03-15 08:11:03 +0000567 if (drd_trace_fork_join)
568 {
569 char msg[256];
570 const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner);
571 const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee);
572 VG_(snprintf)(msg, sizeof(msg),
573 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
574 joiner, drd_joiner, joinee, drd_joinee);
575 if (joiner)
576 {
577 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
578 ", new vc: ");
579 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
580 thread_get_vc(drd_joiner));
581 }
582 VG_(message)(Vg_DebugMsg, msg);
583 }
sewardjaf44c822007-11-25 14:01:38 +0000584
bart3772a982008-03-15 08:11:03 +0000585 thread_delete(drd_joinee);
586 mutex_thread_delete(drd_joinee);
587 cond_thread_delete(drd_joinee);
588 semaphore_thread_delete(drd_joinee);
589 barrier_thread_delete(drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000590}
591
bart5bd9f2d2008-03-03 20:31:58 +0000592void drd_trace_addr(const Addr addr)
593{
bart3772a982008-03-15 08:11:03 +0000594 drd_trace_address = addr;
bart5bd9f2d2008-03-03 20:31:58 +0000595}
596
sewardjaf44c822007-11-25 14:01:38 +0000597/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000598static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000599{
bart3772a982008-03-15 08:11:03 +0000600 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000601
bartd43f8d32008-03-16 17:29:20 +0000602 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000603
bartd43f8d32008-03-16 17:29:20 +0000604 drd_tid = VgThreadIdToDrdThreadId(vg_tid);
bart3772a982008-03-15 08:11:03 +0000605 if (drd_trace_fork_join)
606 {
607 VG_(message)(Vg_DebugMsg,
608 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000609 vg_tid,
bart3772a982008-03-15 08:11:03 +0000610 drd_tid,
611 thread_get_joinable(drd_tid)
612 ? ""
613 : " (which is a detached thread)");
sewardjaf44c822007-11-25 14:01:38 +0000614
bart3772a982008-03-15 08:11:03 +0000615 }
bartd43f8d32008-03-16 17:29:20 +0000616 drd_stop_using_mem(thread_get_stack_min(drd_tid),
617 thread_get_stack_max(drd_tid)
618 - thread_get_stack_min(drd_tid));
619 thread_stop_recording(drd_tid);
bart3772a982008-03-15 08:11:03 +0000620 thread_finished(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000621}
622
bart0268dfa2008-03-11 20:10:21 +0000623void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000624{
bart3772a982008-03-15 08:11:03 +0000625 mutex_init(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000626}
627
sewardj347eeba2008-01-21 14:19:07 +0000628void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000629{
bart3772a982008-03-15 08:11:03 +0000630 mutex_post_destroy(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000631}
632
bart2e3a3c12008-03-24 08:33:47 +0000633void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type,
634 const Bool trylock)
sewardjaf44c822007-11-25 14:01:38 +0000635{
bart2e3a3c12008-03-24 08:33:47 +0000636 mutex_pre_lock(mutex, mutex_type, trylock);
sewardjaf44c822007-11-25 14:01:38 +0000637}
638
bart00344642008-03-01 15:27:41 +0000639void drd_post_mutex_lock(const Addr mutex, const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +0000640{
bart3772a982008-03-15 08:11:03 +0000641 mutex_post_lock(mutex, took_lock);
sewardjaf44c822007-11-25 14:01:38 +0000642}
643
bart00344642008-03-01 15:27:41 +0000644void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000645{
bart3772a982008-03-15 08:11:03 +0000646 mutex_unlock(mutex, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000647}
648
bart0268dfa2008-03-11 20:10:21 +0000649void drd_pre_cond_init(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000650{
bart3772a982008-03-15 08:11:03 +0000651 cond_pre_init(cond);
sewardjaf44c822007-11-25 14:01:38 +0000652}
653
bart72b751c2008-03-01 13:44:24 +0000654void drd_post_cond_destroy(Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000655{
bart3772a982008-03-15 08:11:03 +0000656 cond_post_destroy(cond);
sewardjaf44c822007-11-25 14:01:38 +0000657}
658
bart0268dfa2008-03-11 20:10:21 +0000659void drd_semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000660 const Word pshared, const Word value)
661{
bart3772a982008-03-15 08:11:03 +0000662 semaphore_init(semaphore, pshared, value);
sewardj85642922008-01-14 11:54:56 +0000663}
664
665void drd_semaphore_destroy(const Addr semaphore)
666{
bart3772a982008-03-15 08:11:03 +0000667 semaphore_destroy(semaphore);
sewardj85642922008-01-14 11:54:56 +0000668}
669
bart0268dfa2008-03-11 20:10:21 +0000670void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000671{
bart3772a982008-03-15 08:11:03 +0000672 semaphore_pre_wait(semaphore);
bart28230a32008-02-29 17:27:03 +0000673}
674
675void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
676 const Bool waited)
677{
bart3772a982008-03-15 08:11:03 +0000678 semaphore_post_wait(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000679}
680
bart0268dfa2008-03-11 20:10:21 +0000681void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000682{
bart3772a982008-03-15 08:11:03 +0000683 semaphore_pre_post(tid, semaphore);
sewardj85642922008-01-14 11:54:56 +0000684}
685
686void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000687 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000688{
bart3772a982008-03-15 08:11:03 +0000689 semaphore_post_post(tid, semaphore, waited);
sewardj85642922008-01-14 11:54:56 +0000690}
691
692
bart0268dfa2008-03-11 20:10:21 +0000693void drd_barrier_init(const Addr barrier,
694 const BarrierT barrier_type, const Word count,
695 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000696{
bart3772a982008-03-15 08:11:03 +0000697 barrier_init(barrier, barrier_type, count, reinitialization);
sewardj85642922008-01-14 11:54:56 +0000698}
699
bart0268dfa2008-03-11 20:10:21 +0000700void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000701{
bart3772a982008-03-15 08:11:03 +0000702 barrier_destroy(barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000703}
704
bart0268dfa2008-03-11 20:10:21 +0000705void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
706 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000707{
bart3772a982008-03-15 08:11:03 +0000708 barrier_pre_wait(tid, barrier, barrier_type);
sewardj85642922008-01-14 11:54:56 +0000709}
710
711void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000712 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000713{
bart3772a982008-03-15 08:11:03 +0000714 barrier_post_wait(tid, barrier, barrier_type, waited);
sewardj85642922008-01-14 11:54:56 +0000715}
716
sewardjaf44c822007-11-25 14:01:38 +0000717
718//
719// Implementation of the tool interface.
720//
721
722static
723void drd_post_clo_init(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000724{
725# if defined(VGP_x86_linux) || defined(VGP_amd64_linux)
bart3772a982008-03-15 08:11:03 +0000726 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000727# else
bart3772a982008-03-15 08:11:03 +0000728 VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000729# endif
730}
sewardjaf44c822007-11-25 14:01:38 +0000731
barta79df6e2008-03-14 17:07:51 +0000732static void instrument_load(IRSB* const bb,
733 IRExpr* const addr_expr,
734 const HWord size)
735{
bart3772a982008-03-15 08:11:03 +0000736 IRExpr* size_expr;
737 IRExpr** argv;
738 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000739
bart3772a982008-03-15 08:11:03 +0000740 switch (size)
741 {
742 case 1:
743 argv = mkIRExprVec_1(addr_expr);
744 di = unsafeIRDirty_0_N(/*regparms*/1,
745 "drd_trace_load_1",
746 VG_(fnptr_to_fnentry)(drd_trace_load_1),
747 argv);
748 break;
749 case 2:
750 argv = mkIRExprVec_1(addr_expr);
751 di = unsafeIRDirty_0_N(/*regparms*/1,
752 "drd_trace_load_2",
753 VG_(fnptr_to_fnentry)(drd_trace_load_2),
754 argv);
755 break;
756 case 4:
757 argv = mkIRExprVec_1(addr_expr);
758 di = unsafeIRDirty_0_N(/*regparms*/1,
759 "drd_trace_load_4",
760 VG_(fnptr_to_fnentry)(drd_trace_load_4),
761 argv);
762 break;
763 case 8:
764 argv = mkIRExprVec_1(addr_expr);
765 di = unsafeIRDirty_0_N(/*regparms*/1,
766 "drd_trace_load_8",
767 VG_(fnptr_to_fnentry)(drd_trace_load_8),
768 argv);
769 break;
770 default:
771 size_expr = mkIRExpr_HWord(size);
772 argv = mkIRExprVec_2(addr_expr, size_expr);
773 di = unsafeIRDirty_0_N(/*regparms*/2,
774 "drd_trace_load",
775 VG_(fnptr_to_fnentry)(drd_trace_load),
776 argv);
777 break;
778 }
779 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000780}
781
782static void instrument_store(IRSB* const bb,
bart3772a982008-03-15 08:11:03 +0000783 IRExpr* const addr_expr,
784 const HWord size)
barta79df6e2008-03-14 17:07:51 +0000785{
bart3772a982008-03-15 08:11:03 +0000786 IRExpr* size_expr;
787 IRExpr** argv;
788 IRDirty* di;
barta79df6e2008-03-14 17:07:51 +0000789
bart3772a982008-03-15 08:11:03 +0000790 switch (size)
791 {
792 case 1:
793 argv = mkIRExprVec_1(addr_expr);
794 di = unsafeIRDirty_0_N(/*regparms*/1,
795 "drd_trace_store_1",
796 VG_(fnptr_to_fnentry)(drd_trace_store_1),
797 argv);
798 break;
799 case 2:
800 argv = mkIRExprVec_1(addr_expr);
801 di = unsafeIRDirty_0_N(/*regparms*/1,
802 "drd_trace_store_2",
803 VG_(fnptr_to_fnentry)(drd_trace_store_2),
804 argv);
805 break;
806 case 4:
807 argv = mkIRExprVec_1(addr_expr);
808 di = unsafeIRDirty_0_N(/*regparms*/1,
809 "drd_trace_store_4",
810 VG_(fnptr_to_fnentry)(drd_trace_store_4),
811 argv);
812 break;
813 case 8:
814 argv = mkIRExprVec_1(addr_expr);
815 di = unsafeIRDirty_0_N(/*regparms*/1,
816 "drd_trace_store_8",
817 VG_(fnptr_to_fnentry)(drd_trace_store_8),
818 argv);
819 break;
820 default:
821 size_expr = mkIRExpr_HWord(size);
822 argv = mkIRExprVec_2(addr_expr, size_expr);
823 di = unsafeIRDirty_0_N(/*regparms*/2,
824 "drd_trace_store",
825 VG_(fnptr_to_fnentry)(drd_trace_store),
826 argv);
827 break;
828 }
829 addStmtToIRSB(bb, IRStmt_Dirty(di));
barta79df6e2008-03-14 17:07:51 +0000830}
831
sewardjaf44c822007-11-25 14:01:38 +0000832static
833IRSB* drd_instrument(VgCallbackClosure* const closure,
sewardj347eeba2008-01-21 14:19:07 +0000834 IRSB* const bb_in,
835 VexGuestLayout* const layout,
836 VexGuestExtents* const vge,
837 IRType const gWordTy,
838 IRType const hWordTy)
sewardjaf44c822007-11-25 14:01:38 +0000839{
bart3772a982008-03-15 08:11:03 +0000840 IRDirty* di;
841 Int i;
842 IRSB* bb;
843 IRExpr** argv;
844 Bool instrument = True;
845 Bool bus_locked = False;
sewardjaf44c822007-11-25 14:01:38 +0000846
bart3772a982008-03-15 08:11:03 +0000847 /* Set up BB */
848 bb = emptyIRSB();
849 bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv);
850 bb->next = deepCopyIRExpr(bb_in->next);
851 bb->jumpkind = bb_in->jumpkind;
sewardjaf44c822007-11-25 14:01:38 +0000852
bart3772a982008-03-15 08:11:03 +0000853 for (i = 0; i < bb_in->stmts_used; i++)
854 {
855 IRStmt* const st = bb_in->stmts[i];
856 tl_assert(st);
857 if (st->tag == Ist_NoOp)
858 continue;
sewardjaf44c822007-11-25 14:01:38 +0000859
bart3772a982008-03-15 08:11:03 +0000860 switch (st->tag)
861 {
862 case Ist_IMark:
863 instrument = VG_(seginfo_sect_kind)(NULL, 0, st->Ist.IMark.addr)
864 != Vg_SectPLT;
865 break;
866
867 case Ist_MBE:
868 switch (st->Ist.MBE.event)
sewardjaf44c822007-11-25 14:01:38 +0000869 {
bart3772a982008-03-15 08:11:03 +0000870 case Imbe_Fence:
871 break; /* not interesting */
872 case Imbe_BusLock:
873 tl_assert(! bus_locked);
874 bus_locked = True;
875 break;
876 case Imbe_BusUnlock:
877 tl_assert(bus_locked);
878 bus_locked = False;
879 break;
sewardjaf44c822007-11-25 14:01:38 +0000880 default:
bart3772a982008-03-15 08:11:03 +0000881 tl_assert(0);
sewardjaf44c822007-11-25 14:01:38 +0000882 }
bart3772a982008-03-15 08:11:03 +0000883 addStmtToIRSB(bb, st);
884 break;
sewardjaf44c822007-11-25 14:01:38 +0000885
bart3772a982008-03-15 08:11:03 +0000886 case Ist_Store:
887 if (instrument && ! bus_locked)
888 {
889 instrument_store(bb,
890 st->Ist.Store.addr,
891 sizeofIRType(typeOfIRExpr(bb->tyenv,
892 st->Ist.Store.data)));
893 }
894 addStmtToIRSB(bb, st);
895 break;
barta47b3512008-03-07 17:22:26 +0000896
bart3772a982008-03-15 08:11:03 +0000897 case Ist_WrTmp:
898 if (instrument)
899 {
900 const IRExpr* const data = st->Ist.WrTmp.data;
901 if (data->tag == Iex_Load)
902 {
903 instrument_load(bb,
904 data->Iex.Load.addr,
905 sizeofIRType(data->Iex.Load.ty));
906 }
907 }
908 addStmtToIRSB(bb, st);
909 break;
910
911 case Ist_Dirty:
912 if (instrument)
913 {
914 IRDirty* d = st->Ist.Dirty.details;
915 IREffect const mFx = d->mFx;
916 switch (mFx) {
917 case Ifx_None:
918 break;
919 case Ifx_Read:
920 case Ifx_Write:
921 case Ifx_Modify:
922 tl_assert(d->mAddr);
923 tl_assert(d->mSize > 0);
924 argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize));
925 if (mFx == Ifx_Read || mFx == Ifx_Modify) {
926 di = unsafeIRDirty_0_N(
927 /*regparms*/2,
928 "drd_trace_load",
929 VG_(fnptr_to_fnentry)(drd_trace_load),
930 argv);
931 addStmtToIRSB(bb, IRStmt_Dirty(di));
932 }
933 if ((mFx == Ifx_Write || mFx == Ifx_Modify)
934 && ! bus_locked)
935 {
936 di = unsafeIRDirty_0_N(
937 /*regparms*/2,
938 "drd_trace_store",
939 VG_(fnptr_to_fnentry)(drd_trace_store),
940 argv);
941 addStmtToIRSB(bb, IRStmt_Dirty(di));
942 }
943 break;
944 default:
945 tl_assert(0);
946 }
947 }
948 addStmtToIRSB(bb, st);
949 break;
950
951 default:
952 addStmtToIRSB(bb, st);
953 break;
954 }
955 }
956
957 tl_assert(! bus_locked);
958
959 return bb;
sewardjaf44c822007-11-25 14:01:38 +0000960}
961
sewardjaf44c822007-11-25 14:01:38 +0000962static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
963{
bart3772a982008-03-15 08:11:03 +0000964 tl_assert(tid == VG_(get_running_tid)());
965 thread_set_vg_running_tid(tid);
sewardjaf44c822007-11-25 14:01:38 +0000966}
967
968static
969void drd_fini(Int exitcode)
970{
bart3772a982008-03-15 08:11:03 +0000971 // thread_print_all();
972 if (VG_(clo_verbosity) > 1 || drd_print_stats)
973 {
974 VG_(message)(Vg_DebugMsg,
975 " thread: %lld context switches"
976 " / %lld updates of the danger set",
977 thread_get_context_switch_count(),
978 thread_get_update_danger_set_count());
979 VG_(message)(Vg_DebugMsg,
980 " segments: %lld total, %lld max, %lld discard points",
bart7102f102008-03-17 17:37:53 +0000981 sg_get_created_segments_count(),
982 sg_get_max_alive_segments_count(),
bart3772a982008-03-15 08:11:03 +0000983 thread_get_discard_ordered_segments_count());
984 VG_(message)(Vg_DebugMsg,
985 " bitmaps: %lld / %lld bitmaps were allocated"
986 " and %lld / %lld for danger set updates",
987 bm_get_bitmap_creation_count(),
988 bm_get_bitmap2_creation_count(),
989 thread_get_danger_set_bitmap_creation_count(),
990 thread_get_danger_set_bitmap2_creation_count());
991 VG_(message)(Vg_DebugMsg,
992 " mutex: %lld non-recursive lock/unlock events",
993 get_mutex_lock_count());
994 drd_print_malloc_stats();
995 }
sewardjaf44c822007-11-25 14:01:38 +0000996}
997
sewardjaf44c822007-11-25 14:01:38 +0000998static
999void drd_pre_clo_init(void)
1000{
bart3772a982008-03-15 08:11:03 +00001001 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +00001002
bart3772a982008-03-15 08:11:03 +00001003 VG_(details_name) ("exp-drd");
1004 VG_(details_version) (NULL);
1005 VG_(details_description) ("a data race detector");
1006 VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd,"
1007 " by Bart Van Assche.");
1008 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +00001009
bart3772a982008-03-15 08:11:03 +00001010 VG_(basic_tool_funcs) (drd_post_clo_init,
1011 drd_instrument,
1012 drd_fini);
sewardjaf44c822007-11-25 14:01:38 +00001013
bart3772a982008-03-15 08:11:03 +00001014 // Command line stuff.
1015 VG_(needs_command_line_options)(drd_process_cmd_line_option,
1016 drd_print_usage,
1017 drd_print_debug_usage);
sewardjaf44c822007-11-25 14:01:38 +00001018
bart3772a982008-03-15 08:11:03 +00001019 // Error handling.
1020 drd_register_error_handlers();
sewardjaf44c822007-11-25 14:01:38 +00001021
bart3772a982008-03-15 08:11:03 +00001022 // Core event tracking.
1023 VG_(track_pre_mem_read) (drd_pre_mem_read);
1024 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
1025 VG_(track_post_mem_write) (drd_post_mem_write);
1026 VG_(track_new_mem_brk) (drd_start_using_mem);
1027 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
1028 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
1029 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
1030 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
1031 VG_(track_die_mem_brk) (drd_stop_using_mem);
1032 VG_(track_die_mem_munmap) (drd_stop_using_mem);
1033 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
1034 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
1035 VG_(track_start_client_code) (drd_start_client_code);
1036 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
1037 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
1038 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +00001039
bart3772a982008-03-15 08:11:03 +00001040 // Other stuff.
1041 VG_(needs_var_info)();
sewardjaf44c822007-11-25 14:01:38 +00001042
bart3772a982008-03-15 08:11:03 +00001043 drd_register_malloc_wrappers(drd_start_using_mem, drd_stop_using_mem);
sewardjaf44c822007-11-25 14:01:38 +00001044
bart3772a982008-03-15 08:11:03 +00001045 drd_clientreq_init();
sewardjaf44c822007-11-25 14:01:38 +00001046
bart3772a982008-03-15 08:11:03 +00001047 drd_suppression_init();
bart4bb53d82008-02-28 19:06:34 +00001048
bart3772a982008-03-15 08:11:03 +00001049 clientobj_init();
sewardjaf44c822007-11-25 14:01:38 +00001050}
1051
1052
1053VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)