blob: 4792ffdc53e0ccd9af32dd0220057549c96f700a [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bart09dc13f2009-02-14 15:13:31 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
sewardj85642922008-01-14 11:54:56 +000025#include "drd_barrier.h"
bart4bb53d82008-02-28 19:06:34 +000026#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000027#include "drd_clientreq.h"
sewardjaf44c822007-11-25 14:01:38 +000028#include "drd_cond.h"
29#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000030#include "drd_load_store.h"
sewardjaf44c822007-11-25 14:01:38 +000031#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"
barteacd9162008-06-16 20:22:18 +000038#include "libvex_guest_offsets.h"
sewardj85642922008-01-14 11:54:56 +000039#include "pub_drd_bitmap.h"
bart024a95a2008-04-01 18:27:41 +000040#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
sewardjaf44c822007-11-25 14:01:38 +000041#include "pub_tool_basics.h"
42#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
43#include "pub_tool_libcassert.h" // tl_assert()
44#include "pub_tool_libcbase.h" // VG_(strcmp)
45#include "pub_tool_libcprint.h" // VG_(printf)
46#include "pub_tool_libcproc.h"
47#include "pub_tool_machine.h"
bart024a95a2008-04-01 18:27:41 +000048#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardjaf44c822007-11-25 14:01:38 +000049#include "pub_tool_options.h" // command line options
bartceded212008-03-26 17:39:52 +000050#include "pub_tool_replacemalloc.h"
bart72b751c2008-03-01 13:44:24 +000051#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000052#include "pub_tool_tooliface.h"
53
54
bart1335ecc2009-02-14 16:10:53 +000055/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000056
bart1335ecc2009-02-14 16:10:53 +000057static Bool DRD_(s_print_stats) = False;
58static Bool DRD_(s_var_info) = False;
59static Bool DRD_(s_show_stack_usage) = False;
sewardjaf44c822007-11-25 14:01:38 +000060
61
bart1335ecc2009-02-14 16:10:53 +000062/**
63 * Implement the needs_command_line_options for drd.
64 */
65static Bool DRD_(process_cmd_line_option)(Char* arg)
sewardjaf44c822007-11-25 14:01:38 +000066{
bart09dc13f2009-02-14 15:13:31 +000067 int check_stack_accesses = -1;
bart9d5b7962008-05-14 12:25:00 +000068 int exclusive_threshold_ms = -1;
bart764dea22009-02-15 13:16:52 +000069 int report_signal_unlocked = -1;
bart08865622008-06-06 14:31:36 +000070 int segment_merging = -1;
bart9d5b7962008-05-14 12:25:00 +000071 int shared_threshold_ms = -1;
bart08865622008-06-06 14:31:36 +000072 int show_confl_seg = -1;
73 int trace_barrier = -1;
74 int trace_clientobj = -1;
75 int trace_cond = -1;
76 int trace_csw = -1;
bart09dc13f2009-02-14 15:13:31 +000077 int trace_fork_join = -1;
barte73b0aa2008-06-28 07:19:56 +000078 int trace_conflict_set = -1;
bart08865622008-06-06 14:31:36 +000079 int trace_mutex = -1;
80 int trace_rwlock = -1;
81 int trace_segment = -1;
82 int trace_semaphore = -1;
83 int trace_suppression = -1;
84 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +000085
bart09dc13f2009-02-14 15:13:31 +000086 VG_BOOL_CLO (arg, "--check-stack-var", check_stack_accesses)
bart1335ecc2009-02-14 16:10:53 +000087 else VG_BOOL_CLO(arg, "--drd-stats", DRD_(s_print_stats))
bart764dea22009-02-15 13:16:52 +000088 else VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
bart9d5b7962008-05-14 12:25:00 +000089 else VG_BOOL_CLO(arg, "--segment-merging", segment_merging)
90 else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg)
bart1335ecc2009-02-14 16:10:53 +000091 else VG_BOOL_CLO(arg, "--show-stack-usage", DRD_(s_show_stack_usage))
bart9d5b7962008-05-14 12:25:00 +000092 else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier)
93 else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj)
94 else VG_BOOL_CLO(arg, "--trace-cond", trace_cond)
barte73b0aa2008-06-28 07:19:56 +000095 else VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set)
bart46b5fce2008-06-28 13:01:30 +000096 else VG_BOOL_CLO(arg, "--trace-csw", trace_csw)
bart09dc13f2009-02-14 15:13:31 +000097 else VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join)
bart9d5b7962008-05-14 12:25:00 +000098 else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex)
99 else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock)
100 else VG_BOOL_CLO(arg, "--trace-segment", trace_segment)
101 else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore)
102 else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression)
bart1335ecc2009-02-14 16:10:53 +0000103 else VG_BOOL_CLO(arg, "--var-info", DRD_(s_var_info))
bart9d5b7962008-05-14 12:25:00 +0000104 else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms)
105 else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms)
106 else VG_STR_CLO (arg, "--trace-addr", trace_address)
bart3772a982008-03-15 08:11:03 +0000107 else
bartceded212008-03-26 17:39:52 +0000108 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000109
bart09dc13f2009-02-14 15:13:31 +0000110 if (check_stack_accesses != -1)
111 DRD_(set_check_stack_accesses)(check_stack_accesses);
bart9d5b7962008-05-14 12:25:00 +0000112 if (exclusive_threshold_ms != -1)
113 {
bartdc1ef032009-02-15 14:18:02 +0000114 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
115 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
bart9d5b7962008-05-14 12:25:00 +0000116 }
bart764dea22009-02-15 13:16:52 +0000117 if (report_signal_unlocked != -1)
118 {
bartdc1ef032009-02-15 14:18:02 +0000119 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
bart764dea22009-02-15 13:16:52 +0000120 }
bart9d5b7962008-05-14 12:25:00 +0000121 if (shared_threshold_ms != -1)
122 {
bartdc1ef032009-02-15 14:18:02 +0000123 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
bart9d5b7962008-05-14 12:25:00 +0000124 }
barta9c37392008-03-22 09:38:48 +0000125 if (segment_merging != -1)
bart62a784c2009-02-15 13:11:14 +0000126 DRD_(thread_set_segment_merging)(segment_merging);
barta9c37392008-03-22 09:38:48 +0000127 if (show_confl_seg != -1)
bart246fbf22009-02-15 14:46:17 +0000128 DRD_(set_show_conflicting_segments)(show_confl_seg);
bart3772a982008-03-15 08:11:03 +0000129 if (trace_address)
130 {
bart005dc972008-03-29 14:42:59 +0000131 const Addr addr = VG_(strtoll16)(trace_address, 0);
bart1335ecc2009-02-14 16:10:53 +0000132 DRD_(start_tracing_address_range)(addr, addr + 1);
bart3772a982008-03-15 08:11:03 +0000133 }
barta9c37392008-03-22 09:38:48 +0000134 if (trace_barrier != -1)
barta8cf7652009-02-15 11:00:29 +0000135 DRD_(barrier_set_trace)(trace_barrier);
barta9c37392008-03-22 09:38:48 +0000136 if (trace_clientobj != -1)
bart195e41f2009-02-15 11:34:57 +0000137 DRD_(clientobj_set_trace)(trace_clientobj);
barta9c37392008-03-22 09:38:48 +0000138 if (trace_cond != -1)
bartdc1ef032009-02-15 14:18:02 +0000139 DRD_(cond_set_trace)(trace_cond);
barta9c37392008-03-22 09:38:48 +0000140 if (trace_csw != -1)
bart62a784c2009-02-15 13:11:14 +0000141 DRD_(thread_trace_context_switches)(trace_csw);
bart09dc13f2009-02-14 15:13:31 +0000142 if (trace_fork_join != -1)
143 DRD_(thread_set_trace_fork_join)(trace_fork_join);
barte73b0aa2008-06-28 07:19:56 +0000144 if (trace_conflict_set != -1)
bart62a784c2009-02-15 13:11:14 +0000145 DRD_(thread_trace_conflict_set)(trace_conflict_set);
barta9c37392008-03-22 09:38:48 +0000146 if (trace_mutex != -1)
bartdc1ef032009-02-15 14:18:02 +0000147 DRD_(mutex_set_trace)(trace_mutex);
barta9c37392008-03-22 09:38:48 +0000148 if (trace_rwlock != -1)
bartdc1ef032009-02-15 14:18:02 +0000149 DRD_(rwlock_set_trace)(trace_rwlock);
barta9c37392008-03-22 09:38:48 +0000150 if (trace_segment != -1)
bart62ada3f2009-02-14 17:19:58 +0000151 DRD_(sg_set_trace)(trace_segment);
barta9c37392008-03-22 09:38:48 +0000152 if (trace_semaphore != -1)
bartdc1ef032009-02-15 14:18:02 +0000153 DRD_(semaphore_set_trace)(trace_semaphore);
barta9c37392008-03-22 09:38:48 +0000154 if (trace_suppression != -1)
bart1335ecc2009-02-14 16:10:53 +0000155 DRD_(suppression_set_trace)(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000156
bart3772a982008-03-15 08:11:03 +0000157 return True;
sewardjaf44c822007-11-25 14:01:38 +0000158}
159
bart1335ecc2009-02-14 16:10:53 +0000160static void DRD_(print_usage)(void)
bartbd7e56e2008-03-31 18:14:12 +0000161{
162 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000163" --check-stack-var=yes|no Whether or not to report data races on\n"
164" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000165" --exclusive-threshold=<n> Print an error message if any mutex or\n"
166" writer lock is held longer than the specified time (in milliseconds).\n"
bart46b5fce2008-06-28 13:01:30 +0000167" --report-signal-unlocked=yes|no Whether to report calls to\n"
168" pthread_cond_signal() where the mutex associated\n"
169" with the signal via pthread_cond_wait() is not\n"
170" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000171" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000172" Segment merging is an algorithm to limit memory usage of the\n"
173" data race detection algorithm. Disabling segment merging may\n"
174" improve the accuracy of the so-called 'other segments' displayed\n"
175" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000176" --shared-threshold=<n> Print an error message if a reader lock\n"
177" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000178" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
179" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
180" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000181" stack variables when a race is reported on such a variable. This\n"
182" information is by default not displayed since for big programs\n"
bart85d22532008-06-26 07:30:32 +0000183" reading in all debug information at once may cause an out of\n"
184" memory error [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000185"\n"
bartef1b9722008-07-04 15:34:23 +0000186" drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000187" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000188" specified address [off].\n"
189" --trace-barrier=yes|no Trace all barrier activity [no].\n"
190" --trace-cond=yes|no Trace all condition variable activity [no].\n"
191" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
192" --trace-mutex=yes|no Trace all mutex activity [no].\n"
193" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart130463a2008-04-01 17:03:33 +0000194" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bart3772a982008-03-15 08:11:03 +0000195 );
bart130463a2008-04-01 17:03:33 +0000196 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000197}
198
bart1335ecc2009-02-14 16:10:53 +0000199static void DRD_(print_debug_usage)(void)
sewardjaf44c822007-11-25 14:01:38 +0000200{
bart130463a2008-04-01 17:03:33 +0000201 VG_(printf)(
202" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
203" --trace-clientobj=yes|no Trace all client object activity [no].\n"
204" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000205" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart987781d2008-06-27 15:00:07 +0000206" --trace-segment=yes|no Trace segment actions [no].\n"
207" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bart130463a2008-04-01 17:03:33 +0000208 );
209 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000210}
211
212
213//
214// Implements the thread-related core callbacks.
215//
216
sewardjaf44c822007-11-25 14:01:38 +0000217static void drd_pre_mem_read(const CorePart part,
218 const ThreadId tid,
219 Char* const s,
220 const Addr a,
221 const SizeT size)
222{
bart3772a982008-03-15 08:11:03 +0000223 if (size > 0)
224 {
bart99edb292009-02-15 15:59:20 +0000225 DRD_(trace_load)(a, size);
bart3772a982008-03-15 08:11:03 +0000226 }
sewardjaf44c822007-11-25 14:01:38 +0000227}
228
bart5e85d262008-03-01 10:49:37 +0000229static void drd_pre_mem_read_asciiz(const CorePart part,
230 const ThreadId tid,
231 Char* const s,
232 const Addr a)
233{
bart3772a982008-03-15 08:11:03 +0000234 const char* p = (void*)a;
235 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000236
bart3772a982008-03-15 08:11:03 +0000237 /* Note: the expression '*p' reads client memory and may crash if the */
238 /* client provided an invalid pointer ! */
239 while (*p)
240 {
241 p++;
242 size++;
243 }
244 // To do: find out what a reasonable upper limit on 'size' is.
245 tl_assert(size < 4096);
246 if (size > 0)
247 {
bart99edb292009-02-15 15:59:20 +0000248 DRD_(trace_load)(a, size);
bart3772a982008-03-15 08:11:03 +0000249 }
bart5e85d262008-03-01 10:49:37 +0000250}
251
sewardjaf44c822007-11-25 14:01:38 +0000252static void drd_post_mem_write(const CorePart part,
253 const ThreadId tid,
254 const Addr a,
255 const SizeT size)
256{
bart62a784c2009-02-15 13:11:14 +0000257 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000258 if (size > 0)
259 {
bart99edb292009-02-15 15:59:20 +0000260 DRD_(trace_store)(a, size);
bart3772a982008-03-15 08:11:03 +0000261 }
sewardjaf44c822007-11-25 14:01:38 +0000262}
263
bart08865622008-06-06 14:31:36 +0000264static __inline__
265void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000266{
bart005dc972008-03-29 14:42:59 +0000267 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000268
bart1335ecc2009-02-14 16:10:53 +0000269 if (UNLIKELY(DRD_(any_address_is_traced)()))
bart3772a982008-03-15 08:11:03 +0000270 {
bart1335ecc2009-02-14 16:10:53 +0000271 DRD_(trace_mem_access)(a1, len, eStart);
bart3772a982008-03-15 08:11:03 +0000272 }
sewardjaf44c822007-11-25 14:01:38 +0000273}
274
sewardj7cf4e6b2008-05-01 20:24:26 +0000275static void drd_start_using_mem_w_ecu(const Addr a1,
276 const SizeT len,
277 UInt ec_uniq)
278{
279 drd_start_using_mem(a1, len);
280}
281
282static void drd_start_using_mem_w_tid(const Addr a1,
283 const SizeT len,
284 ThreadId tid)
285{
286 drd_start_using_mem(a1, len);
287}
288
bart0ffa4832008-04-05 12:57:01 +0000289static __inline__
290void drd_stop_using_mem(const Addr a1, const SizeT len,
291 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000292{
bart3772a982008-03-15 08:11:03 +0000293 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000294
bart3772a982008-03-15 08:11:03 +0000295 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000296
bart1335ecc2009-02-14 16:10:53 +0000297 if (UNLIKELY(DRD_(any_address_is_traced)()))
bart3772a982008-03-15 08:11:03 +0000298 {
bart1335ecc2009-02-14 16:10:53 +0000299 DRD_(trace_mem_access)(a1, len, eEnd);
bart3772a982008-03-15 08:11:03 +0000300 }
bart09dc13f2009-02-14 15:13:31 +0000301 if (! is_stack_mem || DRD_(get_check_stack_accesses)())
bart0ffa4832008-04-05 12:57:01 +0000302 {
bart62a784c2009-02-15 13:11:14 +0000303 DRD_(thread_stop_using_mem)(a1, a2);
bart195e41f2009-02-15 11:34:57 +0000304 DRD_(clientobj_stop_using_mem)(a1, a2);
bart1335ecc2009-02-14 16:10:53 +0000305 DRD_(suppression_stop_using_mem)(a1, a2);
bart0ffa4832008-04-05 12:57:01 +0000306 }
307}
308
309static __inline__
310void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
311{
312 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000313}
314
bart1335ecc2009-02-14 16:10:53 +0000315/**
316 * Suppress data race reports on all addresses contained in .plt and
317 * .got.plt sections inside the address range [ a, a + len [. The data in
318 * these sections is modified by _dl_relocate_object() every time a function
319 * in a shared library is called for the first time. Since the first call
320 * to a function in a shared library can happen from a multithreaded context,
321 * such calls can cause conflicting accesses. See also Ulrich Drepper's
322 * paper "How to Write Shared Libraries" for more information about relocation
323 * (http://people.redhat.com/drepper/dsohowto.pdf).
bartcb2d0072008-05-31 07:55:51 +0000324 */
bart1335ecc2009-02-14 16:10:53 +0000325static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
bartcb2d0072008-05-31 07:55:51 +0000326{
327 const DebugInfo* di;
328
329#if 0
330 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
331#endif
332
333 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
334 {
335 Addr avma;
336 SizeT size;
337
338 avma = VG_(seginfo_get_plt_avma)(di);
339 size = VG_(seginfo_get_plt_size)(di);
bart7f42a3c2009-01-30 17:52:21 +0000340 tl_assert((avma && size) || (avma == 0 && size == 0));
341 if (size > 0)
bartcb2d0072008-05-31 07:55:51 +0000342 {
343#if 0
344 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
345#endif
346 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
bart1335ecc2009-02-14 16:10:53 +0000347 DRD_(start_suppression)(avma, avma + size, ".plt");
bartcb2d0072008-05-31 07:55:51 +0000348 }
349
350 avma = VG_(seginfo_get_gotplt_avma)(di);
351 size = VG_(seginfo_get_gotplt_size)(di);
bart7f42a3c2009-01-30 17:52:21 +0000352 tl_assert((avma && size) || (avma == 0 && size == 0));
353 if (size > 0)
bartcb2d0072008-05-31 07:55:51 +0000354 {
355#if 0
356 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
357#endif
358 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
bart1335ecc2009-02-14 16:10:53 +0000359 DRD_(start_suppression)(avma, avma + size, ".gotplt");
bartcb2d0072008-05-31 07:55:51 +0000360 }
361 }
362}
363
bart5e85d262008-03-01 10:49:37 +0000364static
365void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
sewardj9c606bd2008-09-18 18:12:50 +0000366 const Bool rr, const Bool ww, const Bool xx,
367 ULong di_handle)
bart5e85d262008-03-01 10:49:37 +0000368{
bart62a784c2009-02-15 13:11:14 +0000369 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bartd5765912008-03-16 08:40:55 +0000370
bart3772a982008-03-15 08:11:03 +0000371 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000372
bart1335ecc2009-02-14 16:10:53 +0000373 DRD_(suppress_relocation_conflicts)(a, len);
bart5e85d262008-03-01 10:49:37 +0000374}
375
sewardjaf44c822007-11-25 14:01:38 +0000376/* Called by the core when the stack of a thread grows, to indicate that */
377/* the addresses in range [ a, a + len [ may now be used by the client. */
378/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000379static __inline__
380void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000381{
bart62a784c2009-02-15 13:11:14 +0000382 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
383 a - VG_STACK_REDZONE_SZB);
sewardj7cf4e6b2008-05-01 20:24:26 +0000384 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
385 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000386}
387
388/* Called by the core when the stack of a thread shrinks, to indicate that */
389/* the addresses [ a, a + len [ are no longer accessible for the client. */
390/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000391static __inline__
392void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000393{
bart62a784c2009-02-15 13:11:14 +0000394 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
395 a + len - VG_STACK_REDZONE_SZB);
bart0ffa4832008-04-05 12:57:01 +0000396 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
397 True);
sewardjaf44c822007-11-25 14:01:38 +0000398}
399
sewardj7cf4e6b2008-05-01 20:24:26 +0000400static void drd_start_using_mem_stack_signal(
401 const Addr a, const SizeT len,
402 ThreadId tid_for_whom_the_signal_frame_is_being_constructed)
sewardjaf44c822007-11-25 14:01:38 +0000403{
bart62a784c2009-02-15 13:11:14 +0000404 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000405 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000406}
407
bart5e85d262008-03-01 10:49:37 +0000408static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000409{
bart0ffa4832008-04-05 12:57:01 +0000410 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000411}
412
413static
414void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
415{
bart62a784c2009-02-15 13:11:14 +0000416 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
bart3772a982008-03-15 08:11:03 +0000417 tl_assert(created != VG_INVALID_THREADID);
bart62a784c2009-02-15 13:11:14 +0000418 DRD_(thread_pre_create)(drd_creator, created);
419 if (DRD_(IsValidDrdThreadId)(drd_creator))
bart3772a982008-03-15 08:11:03 +0000420 {
bart62a784c2009-02-15 13:11:14 +0000421 DRD_(thread_new_segment)(drd_creator);
bart3772a982008-03-15 08:11:03 +0000422 }
bart09dc13f2009-02-14 15:13:31 +0000423 if (DRD_(thread_get_trace_fork_join)())
bart3772a982008-03-15 08:11:03 +0000424 {
425 VG_(message)(Vg_DebugMsg,
426 "drd_pre_thread_create creator = %d/%d, created = %d",
427 creator, drd_creator, created);
428 }
sewardjaf44c822007-11-25 14:01:38 +0000429}
430
431/* Called by Valgrind's core before any loads or stores are performed on */
432/* the context of thread "created". At startup, this function is called */
433/* with arguments (0,1). */
434static
bart0ffa4832008-04-05 12:57:01 +0000435void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000436{
bart0ffa4832008-04-05 12:57:01 +0000437 DrdThreadId drd_created;
438
439 tl_assert(vg_created != VG_INVALID_THREADID);
440
bart62a784c2009-02-15 13:11:14 +0000441 drd_created = DRD_(thread_post_create)(vg_created);
bart09dc13f2009-02-14 15:13:31 +0000442 if (DRD_(thread_get_trace_fork_join)())
bart3772a982008-03-15 08:11:03 +0000443 {
444 VG_(message)(Vg_DebugMsg,
445 "drd_post_thread_create created = %d/%d",
bart0ffa4832008-04-05 12:57:01 +0000446 vg_created, drd_created);
447 }
bart09dc13f2009-02-14 15:13:31 +0000448 if (! DRD_(get_check_stack_accesses)())
bart0ffa4832008-04-05 12:57:01 +0000449 {
bart62a784c2009-02-15 13:11:14 +0000450 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
451 - DRD_(thread_get_stack_size)(drd_created),
452 DRD_(thread_get_stack_max)(drd_created),
bart1335ecc2009-02-14 16:10:53 +0000453 "stack");
bart3772a982008-03-15 08:11:03 +0000454 }
sewardjaf44c822007-11-25 14:01:38 +0000455}
456
sewardjaf44c822007-11-25 14:01:38 +0000457/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000458static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000459{
bart3772a982008-03-15 08:11:03 +0000460 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000461
bartd43f8d32008-03-16 17:29:20 +0000462 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000463
bart62a784c2009-02-15 13:11:14 +0000464 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
bart09dc13f2009-02-14 15:13:31 +0000465 if (DRD_(thread_get_trace_fork_join)())
bart3772a982008-03-15 08:11:03 +0000466 {
467 VG_(message)(Vg_DebugMsg,
468 "drd_thread_finished tid = %d/%d%s",
bartd43f8d32008-03-16 17:29:20 +0000469 vg_tid,
bart3772a982008-03-15 08:11:03 +0000470 drd_tid,
bart62a784c2009-02-15 13:11:14 +0000471 DRD_(thread_get_joinable)(drd_tid)
bart3772a982008-03-15 08:11:03 +0000472 ? ""
473 : " (which is a detached thread)");
bart912ab8d2008-03-29 09:31:43 +0000474 }
bart1335ecc2009-02-14 16:10:53 +0000475 if (DRD_(s_show_stack_usage))
bart912ab8d2008-03-29 09:31:43 +0000476 {
bart62a784c2009-02-15 13:11:14 +0000477 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
bart912ab8d2008-03-29 09:31:43 +0000478 const SizeT used_stack
bart62a784c2009-02-15 13:11:14 +0000479 = (DRD_(thread_get_stack_max)(drd_tid)
480 - DRD_(thread_get_stack_min_min)(drd_tid));
bart912ab8d2008-03-29 09:31:43 +0000481 VG_(message)(Vg_UserMsg,
482 "thread %d/%d%s finished and used %ld bytes out of %ld"
483 " on its stack. Margin: %ld bytes.",
484 vg_tid,
485 drd_tid,
bart62a784c2009-02-15 13:11:14 +0000486 DRD_(thread_get_joinable)(drd_tid)
bart912ab8d2008-03-29 09:31:43 +0000487 ? ""
488 : " (which is a detached thread)",
489 used_stack,
490 stack_size,
491 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000492
bart3772a982008-03-15 08:11:03 +0000493 }
bart62a784c2009-02-15 13:11:14 +0000494 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
495 DRD_(thread_get_stack_max)(drd_tid)
496 - DRD_(thread_get_stack_min)(drd_tid),
bart0ffa4832008-04-05 12:57:01 +0000497 True);
bart62a784c2009-02-15 13:11:14 +0000498 DRD_(thread_stop_recording)(drd_tid);
499 DRD_(thread_finished)(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000500}
501
sewardjaf44c822007-11-25 14:01:38 +0000502//
503// Implementation of the tool interface.
504//
505
bart1335ecc2009-02-14 16:10:53 +0000506static void DRD_(post_clo_init)(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000507{
bart5403f212008-06-30 10:56:18 +0000508# if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \
509 || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
bart3772a982008-03-15 08:11:03 +0000510 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000511# else
bart5403f212008-06-30 10:56:18 +0000512 VG_(printf)("\nWARNING: DRD has only been tested on Linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000513# endif
bart95761b52008-03-29 08:34:03 +0000514
bart1335ecc2009-02-14 16:10:53 +0000515 if (DRD_(s_var_info))
bart95761b52008-03-29 08:34:03 +0000516 {
517 VG_(needs_var_info)();
518 }
sewardjdcbb8d32007-11-26 21:34:30 +0000519}
sewardjaf44c822007-11-25 14:01:38 +0000520
sewardjaf44c822007-11-25 14:01:38 +0000521static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
522{
bart3772a982008-03-15 08:11:03 +0000523 tl_assert(tid == VG_(get_running_tid)());
bart62a784c2009-02-15 13:11:14 +0000524 DRD_(thread_set_vg_running_tid)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000525}
526
bart1335ecc2009-02-14 16:10:53 +0000527static void DRD_(fini)(Int exitcode)
sewardjaf44c822007-11-25 14:01:38 +0000528{
bart3772a982008-03-15 08:11:03 +0000529 // thread_print_all();
bart1335ecc2009-02-14 16:10:53 +0000530 if (VG_(clo_verbosity) > 1 || DRD_(s_print_stats))
bart3772a982008-03-15 08:11:03 +0000531 {
barte73b0aa2008-06-28 07:19:56 +0000532 ULong update_conflict_set_count;
barte4504dd2008-04-06 15:02:58 +0000533 ULong dsnsc;
534 ULong dscvc;
535
barte73b0aa2008-06-28 07:19:56 +0000536 update_conflict_set_count
bart62a784c2009-02-15 13:11:14 +0000537 = DRD_(thread_get_update_conflict_set_count)(&dsnsc, &dscvc);
barte4504dd2008-04-06 15:02:58 +0000538
bartbd7e56e2008-03-31 18:14:12 +0000539 VG_(message)(Vg_UserMsg,
bart3772a982008-03-15 08:11:03 +0000540 " thread: %lld context switches"
barte73b0aa2008-06-28 07:19:56 +0000541 " / %lld updates of the conflict set",
bart62a784c2009-02-15 13:11:14 +0000542 DRD_(thread_get_context_switch_count)(),
barte73b0aa2008-06-28 07:19:56 +0000543 update_conflict_set_count);
barte4504dd2008-04-06 15:02:58 +0000544 VG_(message)(Vg_UserMsg,
545 " (%lld new sg + %lld combine vc + %lld csw).",
546 dsnsc,
547 dscvc,
barte73b0aa2008-06-28 07:19:56 +0000548 update_conflict_set_count - dsnsc - dscvc);
bartbd7e56e2008-03-31 18:14:12 +0000549 VG_(message)(Vg_UserMsg,
550 " segments: created %lld segments, max %lld alive,"
551 " %lld discard points.",
bart62ada3f2009-02-14 17:19:58 +0000552 DRD_(sg_get_segments_created_count)(),
553 DRD_(sg_get_max_segments_alive_count)(),
bart62a784c2009-02-15 13:11:14 +0000554 DRD_(thread_get_discard_ordered_segments_count)());
bartbd7e56e2008-03-31 18:14:12 +0000555 VG_(message)(Vg_UserMsg,
bart6bbefaf2008-04-19 15:16:45 +0000556 " (%lld m, %lld rw, %lld s, %lld b)",
bartdc1ef032009-02-15 14:18:02 +0000557 DRD_(get_mutex_segment_creation_count)(),
558 DRD_(get_rwlock_segment_creation_count)(),
559 DRD_(get_semaphore_segment_creation_count)(),
barta8cf7652009-02-15 11:00:29 +0000560 DRD_(get_barrier_segment_creation_count)());
bart6bbefaf2008-04-19 15:16:45 +0000561 VG_(message)(Vg_UserMsg,
bart952e1a02008-04-06 13:06:36 +0000562 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
bart99edb292009-02-15 15:59:20 +0000563 DRD_(bm_get_bitmap_creation_count)(),
564 DRD_(bm_get_bitmap2_node_creation_count)());
bart952e1a02008-04-06 13:06:36 +0000565 VG_(message)(Vg_UserMsg,
566 " and %lld level 2 bitmaps were allocated.",
bart99edb292009-02-15 15:59:20 +0000567 DRD_(bm_get_bitmap2_creation_count)());
bartbd7e56e2008-03-31 18:14:12 +0000568 VG_(message)(Vg_UserMsg,
569 " mutex: %lld non-recursive lock/unlock events.",
bartdc1ef032009-02-15 14:18:02 +0000570 DRD_(get_mutex_lock_count)());
bart246fbf22009-02-15 14:46:17 +0000571 DRD_(print_malloc_stats)();
bart3772a982008-03-15 08:11:03 +0000572 }
sewardjaf44c822007-11-25 14:01:38 +0000573}
574
sewardjaf44c822007-11-25 14:01:38 +0000575static
576void drd_pre_clo_init(void)
577{
bart3772a982008-03-15 08:11:03 +0000578 // Basic tool stuff.
sewardjaf44c822007-11-25 14:01:38 +0000579
bartef1b9722008-07-04 15:34:23 +0000580 VG_(details_name) ("drd");
bart3772a982008-03-15 08:11:03 +0000581 VG_(details_version) (NULL);
bartc821a942008-07-04 14:48:39 +0000582 VG_(details_description) ("a thread error detector");
bart86562bd2009-02-16 19:43:56 +0000583 VG_(details_copyright_author)("Copyright (C) 2006-2009, and GNU GPL'd,"
bart3772a982008-03-15 08:11:03 +0000584 " by Bart Van Assche.");
585 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +0000586
bart1335ecc2009-02-14 16:10:53 +0000587 VG_(basic_tool_funcs) (DRD_(post_clo_init),
588 DRD_(instrument),
589 DRD_(fini));
sewardjaf44c822007-11-25 14:01:38 +0000590
bart3772a982008-03-15 08:11:03 +0000591 // Command line stuff.
bart1335ecc2009-02-14 16:10:53 +0000592 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
593 DRD_(print_usage),
594 DRD_(print_debug_usage));
sewardjaf44c822007-11-25 14:01:38 +0000595
bart3772a982008-03-15 08:11:03 +0000596 // Error handling.
bart1335ecc2009-02-14 16:10:53 +0000597 DRD_(register_error_handlers)();
sewardjaf44c822007-11-25 14:01:38 +0000598
bart3772a982008-03-15 08:11:03 +0000599 // Core event tracking.
600 VG_(track_pre_mem_read) (drd_pre_mem_read);
601 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
602 VG_(track_post_mem_write) (drd_post_mem_write);
sewardj7cf4e6b2008-05-01 20:24:26 +0000603 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
bart3772a982008-03-15 08:11:03 +0000604 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
605 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
606 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
607 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
bart0ffa4832008-04-05 12:57:01 +0000608 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
609 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
bart3772a982008-03-15 08:11:03 +0000610 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
611 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
612 VG_(track_start_client_code) (drd_start_client_code);
613 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
614 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
615 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +0000616
bart3772a982008-03-15 08:11:03 +0000617 // Other stuff.
bart246fbf22009-02-15 14:46:17 +0000618 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
619 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +0000620
bart09dc13f2009-02-14 15:13:31 +0000621 DRD_(clientreq_init)();
sewardjaf44c822007-11-25 14:01:38 +0000622
bart1335ecc2009-02-14 16:10:53 +0000623 DRD_(suppression_init)();
bart4bb53d82008-02-28 19:06:34 +0000624
bart195e41f2009-02-15 11:34:57 +0000625 DRD_(clientobj_init)();
sewardjaf44c822007-11-25 14:01:38 +0000626}
627
628
629VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)