blob: e5bc7384c2ee8434f875f9a878eaa2ac25123e5e [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart09dc13f2009-02-14 15:13:31 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
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"
bart4bb53d82008-02-28 19:06:34 +000027#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000028#include "drd_clientreq.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "drd_cond.h"
30#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000031#include "drd_load_store.h"
sewardjaf44c822007-11-25 14:01:38 +000032#include "drd_malloc_wrappers.h"
33#include "drd_mutex.h"
bart5bd9f2d2008-03-03 20:31:58 +000034#include "drd_rwlock.h"
sewardjaf44c822007-11-25 14:01:38 +000035#include "drd_segment.h"
sewardj85642922008-01-14 11:54:56 +000036#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000037#include "drd_suppression.h"
38#include "drd_thread.h"
barteacd9162008-06-16 20:22:18 +000039#include "libvex_guest_offsets.h"
sewardj85642922008-01-14 11:54:56 +000040#include "pub_drd_bitmap.h"
bart024a95a2008-04-01 18:27:41 +000041#include "pub_tool_vki.h" // Must be included before pub_tool_libcproc
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)
47#include "pub_tool_libcproc.h"
48#include "pub_tool_machine.h"
bart024a95a2008-04-01 18:27:41 +000049#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardjaf44c822007-11-25 14:01:38 +000050#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"
54
55
bart1335ecc2009-02-14 16:10:53 +000056/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000057
bart1335ecc2009-02-14 16:10:53 +000058static Bool DRD_(s_print_stats) = False;
59static Bool DRD_(s_var_info) = False;
60static Bool DRD_(s_show_stack_usage) = False;
sewardjaf44c822007-11-25 14:01:38 +000061
62
bart1335ecc2009-02-14 16:10:53 +000063/**
64 * Implement the needs_command_line_options for drd.
65 */
66static Bool DRD_(process_cmd_line_option)(Char* arg)
sewardjaf44c822007-11-25 14:01:38 +000067{
bartbedfd232009-03-26 19:07:15 +000068 int check_stack_accesses = -1;
69 int exclusive_threshold_ms = -1;
bartf98a5692009-05-03 17:17:37 +000070 int first_race_only = -1;
bartbedfd232009-03-26 19:07:15 +000071 int report_signal_unlocked = -1;
72 int segment_merging = -1;
bart8f822af2009-06-08 18:20:42 +000073 int segment_merge_interval = -1;
bartbedfd232009-03-26 19:07:15 +000074 int shared_threshold_ms = -1;
75 int show_confl_seg = -1;
76 int trace_barrier = -1;
77 int trace_clientobj = -1;
78 int trace_cond = -1;
79 int trace_csw = -1;
80 int trace_fork_join = -1;
81 int trace_conflict_set = -1;
bart8f822af2009-06-08 18:20:42 +000082 int trace_conflict_set_bm = -1;
bartbedfd232009-03-26 19:07:15 +000083 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
bartbedfd232009-03-26 19:07:15 +000090 if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {}
91 else if VG_BOOL_CLO(arg, "--drd-stats", DRD_(s_print_stats)) {}
bartf98a5692009-05-03 17:17:37 +000092 else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {}
bart8f822af2009-06-08 18:20:42 +000093 else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
94 {}
bartbedfd232009-03-26 19:07:15 +000095 else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {}
bart8f822af2009-06-08 18:20:42 +000096 else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval)
97 {}
bartbedfd232009-03-26 19:07:15 +000098 else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {}
99 else if VG_BOOL_CLO(arg, "--show-stack-usage",
100 DRD_(s_show_stack_usage)) {}
101 else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {}
102 else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {}
103 else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {}
104 else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {}
bart8f822af2009-06-08 18:20:42 +0000105 else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){}
bartbedfd232009-03-26 19:07:15 +0000106 else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {}
107 else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {}
108 else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {}
109 else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {}
110 else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {}
111 else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {}
112 else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {}
113 else if VG_BOOL_CLO(arg, "--var-info", DRD_(s_var_info)) {}
114 else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
115 else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {}
116 else if VG_STR_CLO (arg, "--trace-addr", trace_address) {}
117 else
118 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000119
bartbedfd232009-03-26 19:07:15 +0000120 if (check_stack_accesses != -1)
121 DRD_(set_check_stack_accesses)(check_stack_accesses);
122 if (exclusive_threshold_ms != -1)
123 {
124 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
125 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
126 }
bartf98a5692009-05-03 17:17:37 +0000127 if (first_race_only != -1)
128 {
129 DRD_(set_first_race_only)(first_race_only);
130 }
bartbedfd232009-03-26 19:07:15 +0000131 if (report_signal_unlocked != -1)
132 {
133 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
134 }
135 if (shared_threshold_ms != -1)
136 {
137 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
138 }
139 if (segment_merging != -1)
140 DRD_(thread_set_segment_merging)(segment_merging);
bart109d5da2009-06-22 18:06:29 +0000141 if (segment_merge_interval != -1)
bart8f822af2009-06-08 18:20:42 +0000142 DRD_(thread_set_segment_merge_interval)(segment_merge_interval);
bartbedfd232009-03-26 19:07:15 +0000143 if (show_confl_seg != -1)
144 DRD_(set_show_conflicting_segments)(show_confl_seg);
145 if (trace_address)
146 {
147 const Addr addr = VG_(strtoll16)(trace_address, 0);
148 DRD_(start_tracing_address_range)(addr, addr + 1);
149 }
150 if (trace_barrier != -1)
151 DRD_(barrier_set_trace)(trace_barrier);
152 if (trace_clientobj != -1)
153 DRD_(clientobj_set_trace)(trace_clientobj);
154 if (trace_cond != -1)
155 DRD_(cond_set_trace)(trace_cond);
156 if (trace_csw != -1)
157 DRD_(thread_trace_context_switches)(trace_csw);
158 if (trace_fork_join != -1)
159 DRD_(thread_set_trace_fork_join)(trace_fork_join);
160 if (trace_conflict_set != -1)
161 DRD_(thread_trace_conflict_set)(trace_conflict_set);
bart8f822af2009-06-08 18:20:42 +0000162 if (trace_conflict_set_bm != -1)
163 DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm);
bartbedfd232009-03-26 19:07:15 +0000164 if (trace_mutex != -1)
165 DRD_(mutex_set_trace)(trace_mutex);
166 if (trace_rwlock != -1)
167 DRD_(rwlock_set_trace)(trace_rwlock);
168 if (trace_segment != -1)
169 DRD_(sg_set_trace)(trace_segment);
170 if (trace_semaphore != -1)
171 DRD_(semaphore_set_trace)(trace_semaphore);
172 if (trace_suppression != -1)
173 DRD_(suppression_set_trace)(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000174
bartbedfd232009-03-26 19:07:15 +0000175 return True;
sewardjaf44c822007-11-25 14:01:38 +0000176}
177
bart1335ecc2009-02-14 16:10:53 +0000178static void DRD_(print_usage)(void)
bartbd7e56e2008-03-31 18:14:12 +0000179{
bartbedfd232009-03-26 19:07:15 +0000180 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000181" --check-stack-var=yes|no Whether or not to report data races on\n"
182" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000183" --exclusive-threshold=<n> Print an error message if any mutex or\n"
184" writer lock is held longer than the specified time (in milliseconds).\n"
bart1a3b0b32009-05-03 17:07:34 +0000185" --first-race-only=yes|no Only report the first data race that occurs on\n"
186" a memory location instead of all races [no].\n"
bart46b5fce2008-06-28 13:01:30 +0000187" --report-signal-unlocked=yes|no Whether to report calls to\n"
188" pthread_cond_signal() where the mutex associated\n"
189" with the signal via pthread_cond_wait() is not\n"
190" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000191" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000192" Segment merging is an algorithm to limit memory usage of the\n"
193" data race detection algorithm. Disabling segment merging may\n"
194" improve the accuracy of the so-called 'other segments' displayed\n"
195" in race reports but can also trigger an out of memory error.\n"
bart8f822af2009-06-08 18:20:42 +0000196" --segment-merging-interval=<n> Perform segment merging every time n new\n"
197" segments have been created. Default: %d.\n"
bart9d5b7962008-05-14 12:25:00 +0000198" --shared-threshold=<n> Print an error message if a reader lock\n"
199" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000200" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
201" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
202" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000203" stack variables when a race is reported on such a variable. This\n"
204" information is by default not displayed since for big programs\n"
bart85d22532008-06-26 07:30:32 +0000205" reading in all debug information at once may cause an out of\n"
206" memory error [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000207"\n"
bartef1b9722008-07-04 15:34:23 +0000208" drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000209" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000210" specified address [off].\n"
211" --trace-barrier=yes|no Trace all barrier activity [no].\n"
212" --trace-cond=yes|no Trace all condition variable activity [no].\n"
213" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
214" --trace-mutex=yes|no Trace all mutex activity [no].\n"
215" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart8f822af2009-06-08 18:20:42 +0000216" --trace-semaphore=yes|no Trace all semaphore activity [no].\n",
217DRD_(thread_get_segment_merge_interval)()
bartbedfd232009-03-26 19:07:15 +0000218);
bart130463a2008-04-01 17:03:33 +0000219 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000220}
221
bart1335ecc2009-02-14 16:10:53 +0000222static void DRD_(print_debug_usage)(void)
sewardjaf44c822007-11-25 14:01:38 +0000223{
bartbedfd232009-03-26 19:07:15 +0000224 VG_(printf)(
bart130463a2008-04-01 17:03:33 +0000225" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
226" --trace-clientobj=yes|no Trace all client object activity [no].\n"
227" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000228" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart8f822af2009-06-08 18:20:42 +0000229" --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n"
230" updates [no]. Note: enabling this option\n"
231" will generate a lot of output !\n"
bart987781d2008-06-27 15:00:07 +0000232" --trace-segment=yes|no Trace segment actions [no].\n"
233" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bartbedfd232009-03-26 19:07:15 +0000234);
bart130463a2008-04-01 17:03:33 +0000235 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000236}
237
238
239//
240// Implements the thread-related core callbacks.
241//
242
sewardjaf44c822007-11-25 14:01:38 +0000243static void drd_pre_mem_read(const CorePart part,
244 const ThreadId tid,
245 Char* const s,
246 const Addr a,
247 const SizeT size)
248{
bartbedfd232009-03-26 19:07:15 +0000249 if (size > 0)
250 {
251 DRD_(trace_load)(a, size);
252 }
sewardjaf44c822007-11-25 14:01:38 +0000253}
254
bart5e85d262008-03-01 10:49:37 +0000255static void drd_pre_mem_read_asciiz(const CorePart part,
256 const ThreadId tid,
257 Char* const s,
258 const Addr a)
259{
bartbedfd232009-03-26 19:07:15 +0000260 const char* p = (void*)a;
261 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000262
bartbedfd232009-03-26 19:07:15 +0000263 /* Note: the expression '*p' reads client memory and may crash if the */
264 /* client provided an invalid pointer ! */
265 while (*p)
266 {
267 p++;
268 size++;
269 }
270 // To do: find out what a reasonable upper limit on 'size' is.
271 tl_assert(size < 4096);
272 if (size > 0)
273 {
274 DRD_(trace_load)(a, size);
275 }
bart5e85d262008-03-01 10:49:37 +0000276}
277
sewardjaf44c822007-11-25 14:01:38 +0000278static void drd_post_mem_write(const CorePart part,
279 const ThreadId tid,
280 const Addr a,
281 const SizeT size)
282{
bartbedfd232009-03-26 19:07:15 +0000283 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
284 if (size > 0)
285 {
286 DRD_(trace_store)(a, size);
287 }
sewardjaf44c822007-11-25 14:01:38 +0000288}
289
bart08865622008-06-06 14:31:36 +0000290static __inline__
291void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000292{
bart5fd28142009-06-10 19:24:20 +0000293 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000294
bartbedfd232009-03-26 19:07:15 +0000295 if (UNLIKELY(DRD_(any_address_is_traced)()))
296 {
297 DRD_(trace_mem_access)(a1, len, eStart);
298 }
sewardjaf44c822007-11-25 14:01:38 +0000299}
300
sewardj7cf4e6b2008-05-01 20:24:26 +0000301static void drd_start_using_mem_w_ecu(const Addr a1,
302 const SizeT len,
303 UInt ec_uniq)
304{
bartbedfd232009-03-26 19:07:15 +0000305 drd_start_using_mem(a1, len);
sewardj7cf4e6b2008-05-01 20:24:26 +0000306}
307
308static void drd_start_using_mem_w_tid(const Addr a1,
309 const SizeT len,
310 ThreadId tid)
311{
bartbedfd232009-03-26 19:07:15 +0000312 drd_start_using_mem(a1, len);
sewardj7cf4e6b2008-05-01 20:24:26 +0000313}
314
bart0ffa4832008-04-05 12:57:01 +0000315static __inline__
316void drd_stop_using_mem(const Addr a1, const SizeT len,
317 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000318{
bartbedfd232009-03-26 19:07:15 +0000319 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000320
bart5fd28142009-06-10 19:24:20 +0000321 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000322
bartbedfd232009-03-26 19:07:15 +0000323 if (UNLIKELY(DRD_(any_address_is_traced)()))
324 {
325 DRD_(trace_mem_access)(a1, len, eEnd);
326 }
327 if (! is_stack_mem || DRD_(get_check_stack_accesses)())
328 {
329 DRD_(thread_stop_using_mem)(a1, a2);
330 DRD_(clientobj_stop_using_mem)(a1, a2);
331 DRD_(suppression_stop_using_mem)(a1, a2);
332 }
bart0ffa4832008-04-05 12:57:01 +0000333}
334
335static __inline__
336void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
337{
bartbedfd232009-03-26 19:07:15 +0000338 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000339}
340
bart1335ecc2009-02-14 16:10:53 +0000341/**
bartfdaa0182009-03-10 09:25:32 +0000342 * Discard all information DRD has about memory accesses and client objects
343 * in the specified address range.
344 */
345void DRD_(clean_memory)(const Addr a1, const SizeT len)
346{
bartbedfd232009-03-26 19:07:15 +0000347 const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
348 drd_stop_using_mem(a1, len, is_stack_memory);
349 drd_start_using_mem(a1, len);
bartfdaa0182009-03-10 09:25:32 +0000350}
351
352/**
bart1335ecc2009-02-14 16:10:53 +0000353 * Suppress data race reports on all addresses contained in .plt and
354 * .got.plt sections inside the address range [ a, a + len [. The data in
355 * these sections is modified by _dl_relocate_object() every time a function
356 * in a shared library is called for the first time. Since the first call
357 * to a function in a shared library can happen from a multithreaded context,
358 * such calls can cause conflicting accesses. See also Ulrich Drepper's
359 * paper "How to Write Shared Libraries" for more information about relocation
360 * (http://people.redhat.com/drepper/dsohowto.pdf).
bartcb2d0072008-05-31 07:55:51 +0000361 */
bart1335ecc2009-02-14 16:10:53 +0000362static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
bartcb2d0072008-05-31 07:55:51 +0000363{
bartbedfd232009-03-26 19:07:15 +0000364 const DebugInfo* di;
bartcb2d0072008-05-31 07:55:51 +0000365
366#if 0
bartbedfd232009-03-26 19:07:15 +0000367 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
bartcb2d0072008-05-31 07:55:51 +0000368#endif
369
bartbedfd232009-03-26 19:07:15 +0000370 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
371 {
372 Addr avma;
373 SizeT size;
bartcb2d0072008-05-31 07:55:51 +0000374
bartbedfd232009-03-26 19:07:15 +0000375 avma = VG_(seginfo_get_plt_avma)(di);
376 size = VG_(seginfo_get_plt_size)(di);
377 tl_assert((avma && size) || (avma == 0 && size == 0));
378 if (size > 0)
379 {
bartcb2d0072008-05-31 07:55:51 +0000380#if 0
bartbedfd232009-03-26 19:07:15 +0000381 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
bartcb2d0072008-05-31 07:55:51 +0000382#endif
bartbedfd232009-03-26 19:07:15 +0000383 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
384 DRD_(start_suppression)(avma, avma + size, ".plt");
385 }
bartcb2d0072008-05-31 07:55:51 +0000386
bartbedfd232009-03-26 19:07:15 +0000387 avma = VG_(seginfo_get_gotplt_avma)(di);
388 size = VG_(seginfo_get_gotplt_size)(di);
389 tl_assert((avma && size) || (avma == 0 && size == 0));
390 if (size > 0)
391 {
bartcb2d0072008-05-31 07:55:51 +0000392#if 0
bartbedfd232009-03-26 19:07:15 +0000393 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
bartcb2d0072008-05-31 07:55:51 +0000394#endif
bartbedfd232009-03-26 19:07:15 +0000395 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
396 DRD_(start_suppression)(avma, avma + size, ".gotplt");
397 }
398 }
bartcb2d0072008-05-31 07:55:51 +0000399}
400
bart5e85d262008-03-01 10:49:37 +0000401static
402void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
sewardj9c606bd2008-09-18 18:12:50 +0000403 const Bool rr, const Bool ww, const Bool xx,
404 ULong di_handle)
bart5e85d262008-03-01 10:49:37 +0000405{
bartbedfd232009-03-26 19:07:15 +0000406 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bartd5765912008-03-16 08:40:55 +0000407
bartbedfd232009-03-26 19:07:15 +0000408 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000409
bartbedfd232009-03-26 19:07:15 +0000410 DRD_(suppress_relocation_conflicts)(a, len);
bart5e85d262008-03-01 10:49:37 +0000411}
412
sewardjaf44c822007-11-25 14:01:38 +0000413/* Called by the core when the stack of a thread grows, to indicate that */
414/* the addresses in range [ a, a + len [ may now be used by the client. */
415/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000416static __inline__
417void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000418{
bartbedfd232009-03-26 19:07:15 +0000419 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
420 a - VG_STACK_REDZONE_SZB);
421 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
422 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000423}
424
425/* Called by the core when the stack of a thread shrinks, to indicate that */
426/* the addresses [ a, a + len [ are no longer accessible for the client. */
427/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000428static __inline__
429void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000430{
bartbedfd232009-03-26 19:07:15 +0000431 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
432 a + len - VG_STACK_REDZONE_SZB);
433 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
434 True);
sewardjaf44c822007-11-25 14:01:38 +0000435}
436
bartbedfd232009-03-26 19:07:15 +0000437/**
438 * Callback function called by the Valgrind core before a stack area is
439 * being used by a signal handler.
440 *
441 * @param[in] a Start of address range.
442 * @param[in] len Address range length.
443 * @param[in] tid Valgrind thread ID for whom the signal frame is being
444 * constructed.
445 */
446static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
447 ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000448{
bartbedfd232009-03-26 19:07:15 +0000449 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
450 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000451}
452
bart5e85d262008-03-01 10:49:37 +0000453static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000454{
bartbedfd232009-03-26 19:07:15 +0000455 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000456}
457
458static
459void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
460{
bartbedfd232009-03-26 19:07:15 +0000461 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
462 tl_assert(created != VG_INVALID_THREADID);
463 DRD_(thread_pre_create)(drd_creator, created);
464 if (DRD_(IsValidDrdThreadId)(drd_creator))
465 {
466 DRD_(thread_new_segment)(drd_creator);
467 }
468 if (DRD_(thread_get_trace_fork_join)())
469 {
470 VG_(message)(Vg_DebugMsg,
471 "drd_pre_thread_create creator = %d/%d, created = %d",
472 creator, drd_creator, created);
473 }
sewardjaf44c822007-11-25 14:01:38 +0000474}
475
476/* Called by Valgrind's core before any loads or stores are performed on */
477/* the context of thread "created". At startup, this function is called */
478/* with arguments (0,1). */
479static
bart0ffa4832008-04-05 12:57:01 +0000480void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000481{
bartbedfd232009-03-26 19:07:15 +0000482 DrdThreadId drd_created;
bart0ffa4832008-04-05 12:57:01 +0000483
bartbedfd232009-03-26 19:07:15 +0000484 tl_assert(vg_created != VG_INVALID_THREADID);
bart0ffa4832008-04-05 12:57:01 +0000485
bartbedfd232009-03-26 19:07:15 +0000486 drd_created = DRD_(thread_post_create)(vg_created);
487 if (DRD_(thread_get_trace_fork_join)())
488 {
489 VG_(message)(Vg_DebugMsg,
490 "drd_post_thread_create created = %d/%d",
491 vg_created, drd_created);
492 }
493 if (! DRD_(get_check_stack_accesses)())
494 {
495 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
496 - DRD_(thread_get_stack_size)(drd_created),
497 DRD_(thread_get_stack_max)(drd_created),
498 "stack");
499 }
sewardjaf44c822007-11-25 14:01:38 +0000500}
501
sewardjaf44c822007-11-25 14:01:38 +0000502/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000503static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000504{
bartbedfd232009-03-26 19:07:15 +0000505 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000506
bartbedfd232009-03-26 19:07:15 +0000507 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000508
bartbedfd232009-03-26 19:07:15 +0000509 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
510 if (DRD_(thread_get_trace_fork_join)())
511 {
512 VG_(message)(Vg_DebugMsg,
513 "drd_thread_finished tid = %d/%d%s",
514 vg_tid,
515 drd_tid,
516 DRD_(thread_get_joinable)(drd_tid)
517 ? ""
518 : " (which is a detached thread)");
519 }
520 if (DRD_(s_show_stack_usage))
521 {
522 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
523 const SizeT used_stack
524 = (DRD_(thread_get_stack_max)(drd_tid)
525 - DRD_(thread_get_stack_min_min)(drd_tid));
526 VG_(message)(Vg_UserMsg,
527 "thread %d/%d%s finished and used %ld bytes out of %ld"
528 " on its stack. Margin: %ld bytes.",
529 vg_tid,
530 drd_tid,
531 DRD_(thread_get_joinable)(drd_tid)
532 ? ""
533 : " (which is a detached thread)",
534 used_stack,
535 stack_size,
536 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000537
bartbedfd232009-03-26 19:07:15 +0000538 }
539 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
540 DRD_(thread_get_stack_max)(drd_tid)
541 - DRD_(thread_get_stack_min)(drd_tid),
542 True);
bartd45d9952009-05-31 18:53:54 +0000543 DRD_(thread_set_record_loads)(drd_tid, False);
544 DRD_(thread_set_record_stores)(drd_tid, False);
bartbedfd232009-03-26 19:07:15 +0000545 DRD_(thread_finished)(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000546}
547
sewardjaf44c822007-11-25 14:01:38 +0000548//
549// Implementation of the tool interface.
550//
551
bart1335ecc2009-02-14 16:10:53 +0000552static void DRD_(post_clo_init)(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000553{
bartbedfd232009-03-26 19:07:15 +0000554# if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \
555 || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
556 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000557# else
bartbedfd232009-03-26 19:07:15 +0000558 VG_(printf)("\nWARNING: DRD has only been tested on Linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000559# endif
bart95761b52008-03-29 08:34:03 +0000560
bartbedfd232009-03-26 19:07:15 +0000561 if (DRD_(s_var_info))
562 {
563 VG_(needs_var_info)();
564 }
sewardjdcbb8d32007-11-26 21:34:30 +0000565}
sewardjaf44c822007-11-25 14:01:38 +0000566
sewardjaf44c822007-11-25 14:01:38 +0000567static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
568{
bartbedfd232009-03-26 19:07:15 +0000569 tl_assert(tid == VG_(get_running_tid)());
570 DRD_(thread_set_vg_running_tid)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000571}
572
bart1335ecc2009-02-14 16:10:53 +0000573static void DRD_(fini)(Int exitcode)
sewardjaf44c822007-11-25 14:01:38 +0000574{
bartbedfd232009-03-26 19:07:15 +0000575 // thread_print_all();
576 if (VG_(clo_verbosity) > 1 || DRD_(s_print_stats))
577 {
barte5214662009-06-21 11:51:23 +0000578 ULong pu = DRD_(thread_get_update_conflict_set_count)();
579 ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)();
580 ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)();
581 ULong pu_join = DRD_(thread_get_update_conflict_set_join_count)();
582
bartbedfd232009-03-26 19:07:15 +0000583 VG_(message)(Vg_UserMsg,
barte5214662009-06-21 11:51:23 +0000584 " thread: %lld context switches.",
bart54803fe2009-06-21 09:26:27 +0000585 DRD_(thread_get_context_switch_count)());
bartbedfd232009-03-26 19:07:15 +0000586 VG_(message)(Vg_UserMsg,
barte5214662009-06-21 11:51:23 +0000587 "confl set: %lld full updates and %lld partial updates;",
bart54803fe2009-06-21 09:26:27 +0000588 DRD_(thread_get_compute_conflict_set_count)(),
barte5214662009-06-21 11:51:23 +0000589 pu);
bartbedfd232009-03-26 19:07:15 +0000590 VG_(message)(Vg_UserMsg,
barte5214662009-06-21 11:51:23 +0000591 " %lld partial updates during segment creation,",
592 pu_seg_cr);
593 VG_(message)(Vg_UserMsg,
594 " %lld because of mutex/sema/cond.var. operations,",
595 pu_mtx_cv);
596 VG_(message)(Vg_UserMsg,
597 " %lld because of barrier/rwlock operations and",
598 pu - pu_seg_cr - pu_mtx_cv - pu_join);
599 VG_(message)(Vg_UserMsg,
600 " %lld partial updates because of thread join"
601 " operations.",
602 pu_join);
603 VG_(message)(Vg_UserMsg,
604 " segments: created %lld segments, max %lld alive,",
bart9dedafe2009-06-21 18:09:02 +0000605 DRD_(sg_get_segments_created_count)(),
606 DRD_(sg_get_max_segments_alive_count)());
bartbedfd232009-03-26 19:07:15 +0000607 VG_(message)(Vg_UserMsg,
barte5214662009-06-21 11:51:23 +0000608 " %lld discard points and %lld merges.",
bart9dedafe2009-06-21 18:09:02 +0000609 DRD_(thread_get_discard_ordered_segments_count)(),
bart1a3b0b32009-05-03 17:07:34 +0000610 DRD_(sg_get_segment_merge_count)());
611 VG_(message)(Vg_UserMsg,
bart54803fe2009-06-21 09:26:27 +0000612 "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and"
613 " %lld barrier.",
bartbedfd232009-03-26 19:07:15 +0000614 DRD_(get_mutex_segment_creation_count)(),
615 DRD_(get_rwlock_segment_creation_count)(),
616 DRD_(get_semaphore_segment_creation_count)(),
617 DRD_(get_barrier_segment_creation_count)());
618 VG_(message)(Vg_UserMsg,
bart9dedafe2009-06-21 18:09:02 +0000619 " bitmaps: %lld level one"
620 " and %lld level two bitmaps were allocated.",
bartbedfd232009-03-26 19:07:15 +0000621 DRD_(bm_get_bitmap_creation_count)(),
bartbedfd232009-03-26 19:07:15 +0000622 DRD_(bm_get_bitmap2_creation_count)());
623 VG_(message)(Vg_UserMsg,
624 " mutex: %lld non-recursive lock/unlock events.",
625 DRD_(get_mutex_lock_count)());
626 DRD_(print_malloc_stats)();
627 }
sewardjaf44c822007-11-25 14:01:38 +0000628}
629
sewardjaf44c822007-11-25 14:01:38 +0000630static
631void drd_pre_clo_init(void)
632{
njnf76d27a2009-05-28 01:53:07 +0000633#if defined(VGO_darwin)
634 // This makes the (all-failing) regtests run much faster.
635 VG_(printf)("DRD doesn't work on Darwin yet, sorry.\n");
636 VG_(exit)(1);
637#endif
sewardjaf44c822007-11-25 14:01:38 +0000638
njnf76d27a2009-05-28 01:53:07 +0000639 // Basic tool stuff.
bartbedfd232009-03-26 19:07:15 +0000640 VG_(details_name) ("drd");
641 VG_(details_version) (NULL);
642 VG_(details_description) ("a thread error detector");
643 VG_(details_copyright_author)("Copyright (C) 2006-2009, and GNU GPL'd,"
644 " by Bart Van Assche.");
645 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +0000646
bartbedfd232009-03-26 19:07:15 +0000647 VG_(basic_tool_funcs) (DRD_(post_clo_init),
648 DRD_(instrument),
649 DRD_(fini));
sewardjaf44c822007-11-25 14:01:38 +0000650
bartbedfd232009-03-26 19:07:15 +0000651 // Command line stuff.
652 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
653 DRD_(print_usage),
654 DRD_(print_debug_usage));
sewardjaf44c822007-11-25 14:01:38 +0000655
bartbedfd232009-03-26 19:07:15 +0000656 // Error handling.
657 DRD_(register_error_handlers)();
sewardjaf44c822007-11-25 14:01:38 +0000658
bartbedfd232009-03-26 19:07:15 +0000659 // Core event tracking.
660 VG_(track_pre_mem_read) (drd_pre_mem_read);
661 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
662 VG_(track_post_mem_write) (drd_post_mem_write);
663 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
664 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
665 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
666 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
667 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
668 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
669 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
670 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
671 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
672 VG_(track_start_client_code) (drd_start_client_code);
673 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
674 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
675 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +0000676
bartbedfd232009-03-26 19:07:15 +0000677 // Other stuff.
678 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
679 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +0000680
bartbedfd232009-03-26 19:07:15 +0000681 DRD_(clientreq_init)();
sewardjaf44c822007-11-25 14:01:38 +0000682
bartbedfd232009-03-26 19:07:15 +0000683 DRD_(suppression_init)();
bart4bb53d82008-02-28 19:06:34 +0000684
bartbedfd232009-03-26 19:07:15 +0000685 DRD_(clientobj_init)();
barte5e0efa2009-06-24 18:35:02 +0000686
687 {
688 Char* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL");
689 if (smi)
690 DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL));
691 }
sewardjaf44c822007-11-25 14:01:38 +0000692}
693
694
695VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)