blob: 3cc6348ffd27442c856bd779eeff896aa141dab1 [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;
73 int shared_threshold_ms = -1;
74 int show_confl_seg = -1;
75 int trace_barrier = -1;
76 int trace_clientobj = -1;
77 int trace_cond = -1;
78 int trace_csw = -1;
79 int trace_fork_join = -1;
80 int trace_conflict_set = -1;
81 int trace_mutex = -1;
82 int trace_rwlock = -1;
83 int trace_segment = -1;
84 int trace_semaphore = -1;
85 int trace_suppression = -1;
86 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +000087
bartbedfd232009-03-26 19:07:15 +000088 if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {}
89 else if VG_BOOL_CLO(arg, "--drd-stats", DRD_(s_print_stats)) {}
bartf98a5692009-05-03 17:17:37 +000090 else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {}
bartbedfd232009-03-26 19:07:15 +000091 else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked) {}
92 else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {}
93 else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {}
94 else if VG_BOOL_CLO(arg, "--show-stack-usage",
95 DRD_(s_show_stack_usage)) {}
96 else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {}
97 else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {}
98 else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {}
99 else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {}
100 else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {}
101 else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {}
102 else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {}
103 else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {}
104 else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {}
105 else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {}
106 else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {}
107 else if VG_BOOL_CLO(arg, "--var-info", DRD_(s_var_info)) {}
108 else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
109 else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {}
110 else if VG_STR_CLO (arg, "--trace-addr", trace_address) {}
111 else
112 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000113
bartbedfd232009-03-26 19:07:15 +0000114 if (check_stack_accesses != -1)
115 DRD_(set_check_stack_accesses)(check_stack_accesses);
116 if (exclusive_threshold_ms != -1)
117 {
118 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
119 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
120 }
bartf98a5692009-05-03 17:17:37 +0000121 if (first_race_only != -1)
122 {
123 DRD_(set_first_race_only)(first_race_only);
124 }
bartbedfd232009-03-26 19:07:15 +0000125 if (report_signal_unlocked != -1)
126 {
127 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
128 }
129 if (shared_threshold_ms != -1)
130 {
131 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
132 }
133 if (segment_merging != -1)
134 DRD_(thread_set_segment_merging)(segment_merging);
135 if (show_confl_seg != -1)
136 DRD_(set_show_conflicting_segments)(show_confl_seg);
137 if (trace_address)
138 {
139 const Addr addr = VG_(strtoll16)(trace_address, 0);
140 DRD_(start_tracing_address_range)(addr, addr + 1);
141 }
142 if (trace_barrier != -1)
143 DRD_(barrier_set_trace)(trace_barrier);
144 if (trace_clientobj != -1)
145 DRD_(clientobj_set_trace)(trace_clientobj);
146 if (trace_cond != -1)
147 DRD_(cond_set_trace)(trace_cond);
148 if (trace_csw != -1)
149 DRD_(thread_trace_context_switches)(trace_csw);
150 if (trace_fork_join != -1)
151 DRD_(thread_set_trace_fork_join)(trace_fork_join);
152 if (trace_conflict_set != -1)
153 DRD_(thread_trace_conflict_set)(trace_conflict_set);
154 if (trace_mutex != -1)
155 DRD_(mutex_set_trace)(trace_mutex);
156 if (trace_rwlock != -1)
157 DRD_(rwlock_set_trace)(trace_rwlock);
158 if (trace_segment != -1)
159 DRD_(sg_set_trace)(trace_segment);
160 if (trace_semaphore != -1)
161 DRD_(semaphore_set_trace)(trace_semaphore);
162 if (trace_suppression != -1)
163 DRD_(suppression_set_trace)(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000164
bartbedfd232009-03-26 19:07:15 +0000165 return True;
sewardjaf44c822007-11-25 14:01:38 +0000166}
167
bart1335ecc2009-02-14 16:10:53 +0000168static void DRD_(print_usage)(void)
bartbd7e56e2008-03-31 18:14:12 +0000169{
bartbedfd232009-03-26 19:07:15 +0000170 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000171" --check-stack-var=yes|no Whether or not to report data races on\n"
172" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000173" --exclusive-threshold=<n> Print an error message if any mutex or\n"
174" writer lock is held longer than the specified time (in milliseconds).\n"
bart1a3b0b32009-05-03 17:07:34 +0000175" --first-race-only=yes|no Only report the first data race that occurs on\n"
176" a memory location instead of all races [no].\n"
bart46b5fce2008-06-28 13:01:30 +0000177" --report-signal-unlocked=yes|no Whether to report calls to\n"
178" pthread_cond_signal() where the mutex associated\n"
179" with the signal via pthread_cond_wait() is not\n"
180" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000181" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000182" Segment merging is an algorithm to limit memory usage of the\n"
183" data race detection algorithm. Disabling segment merging may\n"
184" improve the accuracy of the so-called 'other segments' displayed\n"
185" in race reports but can also trigger an out of memory error.\n"
bart9d5b7962008-05-14 12:25:00 +0000186" --shared-threshold=<n> Print an error message if a reader lock\n"
187" is held longer than the specified time (in milliseconds).\n"
bart130463a2008-04-01 17:03:33 +0000188" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
189" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
190" --var-info=yes|no Display the names of global, static and\n"
bartbd7e56e2008-03-31 18:14:12 +0000191" stack variables when a race is reported on such a variable. This\n"
192" information is by default not displayed since for big programs\n"
bart85d22532008-06-26 07:30:32 +0000193" reading in all debug information at once may cause an out of\n"
194" memory error [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000195"\n"
bartef1b9722008-07-04 15:34:23 +0000196" drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000197" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000198" specified address [off].\n"
199" --trace-barrier=yes|no Trace all barrier activity [no].\n"
200" --trace-cond=yes|no Trace all condition variable activity [no].\n"
201" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
202" --trace-mutex=yes|no Trace all mutex activity [no].\n"
203" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart130463a2008-04-01 17:03:33 +0000204" --trace-semaphore=yes|no Trace all semaphore activity [no].\n"
bartbedfd232009-03-26 19:07:15 +0000205);
bart130463a2008-04-01 17:03:33 +0000206 VG_(replacement_malloc_print_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000207}
208
bart1335ecc2009-02-14 16:10:53 +0000209static void DRD_(print_debug_usage)(void)
sewardjaf44c822007-11-25 14:01:38 +0000210{
bartbedfd232009-03-26 19:07:15 +0000211 VG_(printf)(
bart130463a2008-04-01 17:03:33 +0000212" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
213" --trace-clientobj=yes|no Trace all client object activity [no].\n"
214" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000215" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart987781d2008-06-27 15:00:07 +0000216" --trace-segment=yes|no Trace segment actions [no].\n"
217" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bartbedfd232009-03-26 19:07:15 +0000218);
bart130463a2008-04-01 17:03:33 +0000219 VG_(replacement_malloc_print_debug_usage)();
sewardjaf44c822007-11-25 14:01:38 +0000220}
221
222
223//
224// Implements the thread-related core callbacks.
225//
226
sewardjaf44c822007-11-25 14:01:38 +0000227static void drd_pre_mem_read(const CorePart part,
228 const ThreadId tid,
229 Char* const s,
230 const Addr a,
231 const SizeT size)
232{
bartbedfd232009-03-26 19:07:15 +0000233 if (size > 0)
234 {
235 DRD_(trace_load)(a, size);
236 }
sewardjaf44c822007-11-25 14:01:38 +0000237}
238
bart5e85d262008-03-01 10:49:37 +0000239static void drd_pre_mem_read_asciiz(const CorePart part,
240 const ThreadId tid,
241 Char* const s,
242 const Addr a)
243{
bartbedfd232009-03-26 19:07:15 +0000244 const char* p = (void*)a;
245 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000246
bartbedfd232009-03-26 19:07:15 +0000247 /* Note: the expression '*p' reads client memory and may crash if the */
248 /* client provided an invalid pointer ! */
249 while (*p)
250 {
251 p++;
252 size++;
253 }
254 // To do: find out what a reasonable upper limit on 'size' is.
255 tl_assert(size < 4096);
256 if (size > 0)
257 {
258 DRD_(trace_load)(a, size);
259 }
bart5e85d262008-03-01 10:49:37 +0000260}
261
sewardjaf44c822007-11-25 14:01:38 +0000262static void drd_post_mem_write(const CorePart part,
263 const ThreadId tid,
264 const Addr a,
265 const SizeT size)
266{
bartbedfd232009-03-26 19:07:15 +0000267 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
268 if (size > 0)
269 {
270 DRD_(trace_store)(a, size);
271 }
sewardjaf44c822007-11-25 14:01:38 +0000272}
273
bart08865622008-06-06 14:31:36 +0000274static __inline__
275void drd_start_using_mem(const Addr a1, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000276{
bartbedfd232009-03-26 19:07:15 +0000277 tl_assert(a1 < a1 + len);
bart5e85d262008-03-01 10:49:37 +0000278
bartbedfd232009-03-26 19:07:15 +0000279 if (UNLIKELY(DRD_(any_address_is_traced)()))
280 {
281 DRD_(trace_mem_access)(a1, len, eStart);
282 }
sewardjaf44c822007-11-25 14:01:38 +0000283}
284
sewardj7cf4e6b2008-05-01 20:24:26 +0000285static void drd_start_using_mem_w_ecu(const Addr a1,
286 const SizeT len,
287 UInt ec_uniq)
288{
bartbedfd232009-03-26 19:07:15 +0000289 drd_start_using_mem(a1, len);
sewardj7cf4e6b2008-05-01 20:24:26 +0000290}
291
292static void drd_start_using_mem_w_tid(const Addr a1,
293 const SizeT len,
294 ThreadId tid)
295{
bartbedfd232009-03-26 19:07:15 +0000296 drd_start_using_mem(a1, len);
sewardj7cf4e6b2008-05-01 20:24:26 +0000297}
298
bart0ffa4832008-04-05 12:57:01 +0000299static __inline__
300void drd_stop_using_mem(const Addr a1, const SizeT len,
301 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000302{
bartbedfd232009-03-26 19:07:15 +0000303 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000304
bartbedfd232009-03-26 19:07:15 +0000305 tl_assert(a1 < a2);
bart5e85d262008-03-01 10:49:37 +0000306
bartbedfd232009-03-26 19:07:15 +0000307 if (UNLIKELY(DRD_(any_address_is_traced)()))
308 {
309 DRD_(trace_mem_access)(a1, len, eEnd);
310 }
311 if (! is_stack_mem || DRD_(get_check_stack_accesses)())
312 {
313 DRD_(thread_stop_using_mem)(a1, a2);
314 DRD_(clientobj_stop_using_mem)(a1, a2);
315 DRD_(suppression_stop_using_mem)(a1, a2);
316 }
bart0ffa4832008-04-05 12:57:01 +0000317}
318
319static __inline__
320void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
321{
bartbedfd232009-03-26 19:07:15 +0000322 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000323}
324
bart1335ecc2009-02-14 16:10:53 +0000325/**
bartfdaa0182009-03-10 09:25:32 +0000326 * Discard all information DRD has about memory accesses and client objects
327 * in the specified address range.
328 */
329void DRD_(clean_memory)(const Addr a1, const SizeT len)
330{
bartbedfd232009-03-26 19:07:15 +0000331 const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
332 drd_stop_using_mem(a1, len, is_stack_memory);
333 drd_start_using_mem(a1, len);
bartfdaa0182009-03-10 09:25:32 +0000334}
335
336/**
bart1335ecc2009-02-14 16:10:53 +0000337 * Suppress data race reports on all addresses contained in .plt and
338 * .got.plt sections inside the address range [ a, a + len [. The data in
339 * these sections is modified by _dl_relocate_object() every time a function
340 * in a shared library is called for the first time. Since the first call
341 * to a function in a shared library can happen from a multithreaded context,
342 * such calls can cause conflicting accesses. See also Ulrich Drepper's
343 * paper "How to Write Shared Libraries" for more information about relocation
344 * (http://people.redhat.com/drepper/dsohowto.pdf).
bartcb2d0072008-05-31 07:55:51 +0000345 */
bart1335ecc2009-02-14 16:10:53 +0000346static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
bartcb2d0072008-05-31 07:55:51 +0000347{
bartbedfd232009-03-26 19:07:15 +0000348 const DebugInfo* di;
bartcb2d0072008-05-31 07:55:51 +0000349
350#if 0
bartbedfd232009-03-26 19:07:15 +0000351 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
bartcb2d0072008-05-31 07:55:51 +0000352#endif
353
bartbedfd232009-03-26 19:07:15 +0000354 for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di))
355 {
356 Addr avma;
357 SizeT size;
bartcb2d0072008-05-31 07:55:51 +0000358
bartbedfd232009-03-26 19:07:15 +0000359 avma = VG_(seginfo_get_plt_avma)(di);
360 size = VG_(seginfo_get_plt_size)(di);
361 tl_assert((avma && size) || (avma == 0 && size == 0));
362 if (size > 0)
363 {
bartcb2d0072008-05-31 07:55:51 +0000364#if 0
bartbedfd232009-03-26 19:07:15 +0000365 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
bartcb2d0072008-05-31 07:55:51 +0000366#endif
bartbedfd232009-03-26 19:07:15 +0000367 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
368 DRD_(start_suppression)(avma, avma + size, ".plt");
369 }
bartcb2d0072008-05-31 07:55:51 +0000370
bartbedfd232009-03-26 19:07:15 +0000371 avma = VG_(seginfo_get_gotplt_avma)(di);
372 size = VG_(seginfo_get_gotplt_size)(di);
373 tl_assert((avma && size) || (avma == 0 && size == 0));
374 if (size > 0)
375 {
bartcb2d0072008-05-31 07:55:51 +0000376#if 0
bartbedfd232009-03-26 19:07:15 +0000377 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
bartcb2d0072008-05-31 07:55:51 +0000378#endif
bartbedfd232009-03-26 19:07:15 +0000379 tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
380 DRD_(start_suppression)(avma, avma + size, ".gotplt");
381 }
382 }
bartcb2d0072008-05-31 07:55:51 +0000383}
384
bart5e85d262008-03-01 10:49:37 +0000385static
386void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
sewardj9c606bd2008-09-18 18:12:50 +0000387 const Bool rr, const Bool ww, const Bool xx,
388 ULong di_handle)
bart5e85d262008-03-01 10:49:37 +0000389{
bartbedfd232009-03-26 19:07:15 +0000390 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bartd5765912008-03-16 08:40:55 +0000391
bartbedfd232009-03-26 19:07:15 +0000392 drd_start_using_mem(a, len);
bartcb2d0072008-05-31 07:55:51 +0000393
bartbedfd232009-03-26 19:07:15 +0000394 DRD_(suppress_relocation_conflicts)(a, len);
bart5e85d262008-03-01 10:49:37 +0000395}
396
sewardjaf44c822007-11-25 14:01:38 +0000397/* Called by the core when the stack of a thread grows, to indicate that */
398/* the addresses in range [ a, a + len [ may now be used by the client. */
399/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000400static __inline__
401void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000402{
bartbedfd232009-03-26 19:07:15 +0000403 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
404 a - VG_STACK_REDZONE_SZB);
405 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
406 len + VG_STACK_REDZONE_SZB);
sewardjaf44c822007-11-25 14:01:38 +0000407}
408
409/* Called by the core when the stack of a thread shrinks, to indicate that */
410/* the addresses [ a, a + len [ are no longer accessible for the client. */
411/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000412static __inline__
413void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000414{
bartbedfd232009-03-26 19:07:15 +0000415 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
416 a + len - VG_STACK_REDZONE_SZB);
417 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
418 True);
sewardjaf44c822007-11-25 14:01:38 +0000419}
420
bartbedfd232009-03-26 19:07:15 +0000421/**
422 * Callback function called by the Valgrind core before a stack area is
423 * being used by a signal handler.
424 *
425 * @param[in] a Start of address range.
426 * @param[in] len Address range length.
427 * @param[in] tid Valgrind thread ID for whom the signal frame is being
428 * constructed.
429 */
430static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
431 ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000432{
bartbedfd232009-03-26 19:07:15 +0000433 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
434 drd_start_using_mem(a, len);
sewardjaf44c822007-11-25 14:01:38 +0000435}
436
bart5e85d262008-03-01 10:49:37 +0000437static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000438{
bartbedfd232009-03-26 19:07:15 +0000439 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000440}
441
442static
443void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
444{
bartbedfd232009-03-26 19:07:15 +0000445 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
446 tl_assert(created != VG_INVALID_THREADID);
447 DRD_(thread_pre_create)(drd_creator, created);
448 if (DRD_(IsValidDrdThreadId)(drd_creator))
449 {
450 DRD_(thread_new_segment)(drd_creator);
451 }
452 if (DRD_(thread_get_trace_fork_join)())
453 {
454 VG_(message)(Vg_DebugMsg,
455 "drd_pre_thread_create creator = %d/%d, created = %d",
456 creator, drd_creator, created);
457 }
sewardjaf44c822007-11-25 14:01:38 +0000458}
459
460/* Called by Valgrind's core before any loads or stores are performed on */
461/* the context of thread "created". At startup, this function is called */
462/* with arguments (0,1). */
463static
bart0ffa4832008-04-05 12:57:01 +0000464void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000465{
bartbedfd232009-03-26 19:07:15 +0000466 DrdThreadId drd_created;
bart0ffa4832008-04-05 12:57:01 +0000467
bartbedfd232009-03-26 19:07:15 +0000468 tl_assert(vg_created != VG_INVALID_THREADID);
bart0ffa4832008-04-05 12:57:01 +0000469
bartbedfd232009-03-26 19:07:15 +0000470 drd_created = DRD_(thread_post_create)(vg_created);
471 if (DRD_(thread_get_trace_fork_join)())
472 {
473 VG_(message)(Vg_DebugMsg,
474 "drd_post_thread_create created = %d/%d",
475 vg_created, drd_created);
476 }
477 if (! DRD_(get_check_stack_accesses)())
478 {
479 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
480 - DRD_(thread_get_stack_size)(drd_created),
481 DRD_(thread_get_stack_max)(drd_created),
482 "stack");
483 }
sewardjaf44c822007-11-25 14:01:38 +0000484}
485
sewardjaf44c822007-11-25 14:01:38 +0000486/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000487static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000488{
bartbedfd232009-03-26 19:07:15 +0000489 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000490
bartbedfd232009-03-26 19:07:15 +0000491 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000492
bartbedfd232009-03-26 19:07:15 +0000493 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
494 if (DRD_(thread_get_trace_fork_join)())
495 {
496 VG_(message)(Vg_DebugMsg,
497 "drd_thread_finished tid = %d/%d%s",
498 vg_tid,
499 drd_tid,
500 DRD_(thread_get_joinable)(drd_tid)
501 ? ""
502 : " (which is a detached thread)");
503 }
504 if (DRD_(s_show_stack_usage))
505 {
506 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
507 const SizeT used_stack
508 = (DRD_(thread_get_stack_max)(drd_tid)
509 - DRD_(thread_get_stack_min_min)(drd_tid));
510 VG_(message)(Vg_UserMsg,
511 "thread %d/%d%s finished and used %ld bytes out of %ld"
512 " on its stack. Margin: %ld bytes.",
513 vg_tid,
514 drd_tid,
515 DRD_(thread_get_joinable)(drd_tid)
516 ? ""
517 : " (which is a detached thread)",
518 used_stack,
519 stack_size,
520 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000521
bartbedfd232009-03-26 19:07:15 +0000522 }
523 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
524 DRD_(thread_get_stack_max)(drd_tid)
525 - DRD_(thread_get_stack_min)(drd_tid),
526 True);
527 DRD_(thread_stop_recording)(drd_tid);
528 DRD_(thread_finished)(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000529}
530
sewardjaf44c822007-11-25 14:01:38 +0000531//
532// Implementation of the tool interface.
533//
534
bart1335ecc2009-02-14 16:10:53 +0000535static void DRD_(post_clo_init)(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000536{
bartbedfd232009-03-26 19:07:15 +0000537# if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \
538 || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux)
539 /* fine */
sewardjdcbb8d32007-11-26 21:34:30 +0000540# else
bartbedfd232009-03-26 19:07:15 +0000541 VG_(printf)("\nWARNING: DRD has only been tested on Linux.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000542# endif
bart95761b52008-03-29 08:34:03 +0000543
bartbedfd232009-03-26 19:07:15 +0000544 if (DRD_(s_var_info))
545 {
546 VG_(needs_var_info)();
547 }
sewardjdcbb8d32007-11-26 21:34:30 +0000548}
sewardjaf44c822007-11-25 14:01:38 +0000549
sewardjaf44c822007-11-25 14:01:38 +0000550static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
551{
bartbedfd232009-03-26 19:07:15 +0000552 tl_assert(tid == VG_(get_running_tid)());
553 DRD_(thread_set_vg_running_tid)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000554}
555
bart1335ecc2009-02-14 16:10:53 +0000556static void DRD_(fini)(Int exitcode)
sewardjaf44c822007-11-25 14:01:38 +0000557{
bartbedfd232009-03-26 19:07:15 +0000558 // thread_print_all();
559 if (VG_(clo_verbosity) > 1 || DRD_(s_print_stats))
560 {
561 ULong update_conflict_set_count;
562 ULong dsnsc;
563 ULong dscvc;
barte4504dd2008-04-06 15:02:58 +0000564
bartbedfd232009-03-26 19:07:15 +0000565 update_conflict_set_count
566 = DRD_(thread_get_update_conflict_set_count)(&dsnsc, &dscvc);
barte4504dd2008-04-06 15:02:58 +0000567
bartbedfd232009-03-26 19:07:15 +0000568 VG_(message)(Vg_UserMsg,
569 " thread: %lld context switches"
570 " / %lld updates of the conflict set",
571 DRD_(thread_get_context_switch_count)(),
572 update_conflict_set_count);
573 VG_(message)(Vg_UserMsg,
574 " (%lld new sg + %lld combine vc + %lld csw).",
575 dsnsc,
576 dscvc,
577 update_conflict_set_count - dsnsc - dscvc);
578 VG_(message)(Vg_UserMsg,
579 " segments: created %lld segments, max %lld alive,"
580 " %lld discard points.",
581 DRD_(sg_get_segments_created_count)(),
582 DRD_(sg_get_max_segments_alive_count)(),
583 DRD_(thread_get_discard_ordered_segments_count)());
584 VG_(message)(Vg_UserMsg,
bart1a3b0b32009-05-03 17:07:34 +0000585 " %lld merges.",
586 DRD_(sg_get_segment_merge_count)());
587 VG_(message)(Vg_UserMsg,
bartbedfd232009-03-26 19:07:15 +0000588 " (%lld m, %lld rw, %lld s, %lld b)",
589 DRD_(get_mutex_segment_creation_count)(),
590 DRD_(get_rwlock_segment_creation_count)(),
591 DRD_(get_semaphore_segment_creation_count)(),
592 DRD_(get_barrier_segment_creation_count)());
593 VG_(message)(Vg_UserMsg,
594 " bitmaps: %lld level 1 / %lld level 2 bitmap refs",
595 DRD_(bm_get_bitmap_creation_count)(),
596 DRD_(bm_get_bitmap2_node_creation_count)());
597 VG_(message)(Vg_UserMsg,
598 " and %lld level 2 bitmaps were allocated.",
599 DRD_(bm_get_bitmap2_creation_count)());
600 VG_(message)(Vg_UserMsg,
601 " mutex: %lld non-recursive lock/unlock events.",
602 DRD_(get_mutex_lock_count)());
603 DRD_(print_malloc_stats)();
604 }
sewardjaf44c822007-11-25 14:01:38 +0000605}
606
sewardjaf44c822007-11-25 14:01:38 +0000607static
608void drd_pre_clo_init(void)
609{
njnf76d27a2009-05-28 01:53:07 +0000610#if defined(VGO_darwin)
611 // This makes the (all-failing) regtests run much faster.
612 VG_(printf)("DRD doesn't work on Darwin yet, sorry.\n");
613 VG_(exit)(1);
614#endif
sewardjaf44c822007-11-25 14:01:38 +0000615
njnf76d27a2009-05-28 01:53:07 +0000616 // Basic tool stuff.
bartbedfd232009-03-26 19:07:15 +0000617 VG_(details_name) ("drd");
618 VG_(details_version) (NULL);
619 VG_(details_description) ("a thread error detector");
620 VG_(details_copyright_author)("Copyright (C) 2006-2009, and GNU GPL'd,"
621 " by Bart Van Assche.");
622 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +0000623
bartbedfd232009-03-26 19:07:15 +0000624 VG_(basic_tool_funcs) (DRD_(post_clo_init),
625 DRD_(instrument),
626 DRD_(fini));
sewardjaf44c822007-11-25 14:01:38 +0000627
bartbedfd232009-03-26 19:07:15 +0000628 // Command line stuff.
629 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
630 DRD_(print_usage),
631 DRD_(print_debug_usage));
sewardjaf44c822007-11-25 14:01:38 +0000632
bartbedfd232009-03-26 19:07:15 +0000633 // Error handling.
634 DRD_(register_error_handlers)();
sewardjaf44c822007-11-25 14:01:38 +0000635
bartbedfd232009-03-26 19:07:15 +0000636 // Core event tracking.
637 VG_(track_pre_mem_read) (drd_pre_mem_read);
638 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
639 VG_(track_post_mem_write) (drd_post_mem_write);
640 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
641 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
642 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
643 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
644 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
645 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
646 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
647 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
648 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
649 VG_(track_start_client_code) (drd_start_client_code);
650 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
651 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
652 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +0000653
bartbedfd232009-03-26 19:07:15 +0000654 // Other stuff.
655 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
656 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +0000657
bartbedfd232009-03-26 19:07:15 +0000658 DRD_(clientreq_init)();
sewardjaf44c822007-11-25 14:01:38 +0000659
bartbedfd232009-03-26 19:07:15 +0000660 DRD_(suppression_init)();
bart4bb53d82008-02-28 19:06:34 +0000661
bartbedfd232009-03-26 19:07:15 +0000662 DRD_(clientobj_init)();
sewardjaf44c822007-11-25 14:01:38 +0000663}
664
665
666VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)