blob: e317857ff34882952042c4b7b3922ab3d2c35e45 [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
bart876cafd2010-10-10 18:07:31 +00005 Copyright (C) 2006-2010 Bart Van Assche <bvanassche@acm.org>.
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
bart75c1cba2010-08-29 07:20:30 +000058static Bool s_free_is_write = False;
59static Bool s_print_stats = False;
60static Bool s_var_info = False;
61static Bool s_show_stack_usage = False;
bartf9427fd2010-08-29 09:19:07 +000062static Bool s_trace_alloc = False;
sewardjaf44c822007-11-25 14:01:38 +000063
64
bart1335ecc2009-02-14 16:10:53 +000065/**
66 * Implement the needs_command_line_options for drd.
67 */
68static Bool DRD_(process_cmd_line_option)(Char* arg)
sewardjaf44c822007-11-25 14:01:38 +000069{
bartbedfd232009-03-26 19:07:15 +000070 int check_stack_accesses = -1;
71 int exclusive_threshold_ms = -1;
bartf98a5692009-05-03 17:17:37 +000072 int first_race_only = -1;
bartbedfd232009-03-26 19:07:15 +000073 int report_signal_unlocked = -1;
74 int segment_merging = -1;
bart8f822af2009-06-08 18:20:42 +000075 int segment_merge_interval = -1;
bartbedfd232009-03-26 19:07:15 +000076 int shared_threshold_ms = -1;
77 int show_confl_seg = -1;
78 int trace_barrier = -1;
79 int trace_clientobj = -1;
80 int trace_cond = -1;
81 int trace_csw = -1;
82 int trace_fork_join = -1;
83 int trace_conflict_set = -1;
bart8f822af2009-06-08 18:20:42 +000084 int trace_conflict_set_bm = -1;
bartbedfd232009-03-26 19:07:15 +000085 int trace_mutex = -1;
86 int trace_rwlock = -1;
87 int trace_segment = -1;
88 int trace_semaphore = -1;
89 int trace_suppression = -1;
90 Char* trace_address = 0;
sewardjaf44c822007-11-25 14:01:38 +000091
bartbedfd232009-03-26 19:07:15 +000092 if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {}
bart75c1cba2010-08-29 07:20:30 +000093 else if VG_BOOL_CLO(arg, "--drd-stats", s_print_stats) {}
bartf98a5692009-05-03 17:17:37 +000094 else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {}
bart75c1cba2010-08-29 07:20:30 +000095 else if VG_BOOL_CLO(arg, "--free-is-write", s_free_is_write) {}
bart8f822af2009-06-08 18:20:42 +000096 else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
97 {}
bartbedfd232009-03-26 19:07:15 +000098 else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {}
bart8f822af2009-06-08 18:20:42 +000099 else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval)
100 {}
bartbedfd232009-03-26 19:07:15 +0000101 else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {}
bart75c1cba2010-08-29 07:20:30 +0000102 else if VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) {}
bartf9427fd2010-08-29 09:19:07 +0000103 else if VG_BOOL_CLO(arg, "--trace-alloc", s_trace_alloc) {}
bartbedfd232009-03-26 19:07:15 +0000104 else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {}
105 else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {}
106 else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {}
107 else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {}
bart8f822af2009-06-08 18:20:42 +0000108 else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){}
bartbedfd232009-03-26 19:07:15 +0000109 else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {}
110 else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {}
111 else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {}
112 else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {}
113 else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {}
114 else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {}
115 else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {}
bart75c1cba2010-08-29 07:20:30 +0000116 else if VG_BOOL_CLO(arg, "--var-info", s_var_info) {}
bartbedfd232009-03-26 19:07:15 +0000117 else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
118 else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {}
119 else if VG_STR_CLO (arg, "--trace-addr", trace_address) {}
120 else
121 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000122
bartbedfd232009-03-26 19:07:15 +0000123 if (check_stack_accesses != -1)
124 DRD_(set_check_stack_accesses)(check_stack_accesses);
125 if (exclusive_threshold_ms != -1)
126 {
127 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
128 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
129 }
bartf98a5692009-05-03 17:17:37 +0000130 if (first_race_only != -1)
131 {
132 DRD_(set_first_race_only)(first_race_only);
133 }
bartbedfd232009-03-26 19:07:15 +0000134 if (report_signal_unlocked != -1)
135 {
136 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
137 }
138 if (shared_threshold_ms != -1)
139 {
140 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
141 }
142 if (segment_merging != -1)
143 DRD_(thread_set_segment_merging)(segment_merging);
bart109d5da2009-06-22 18:06:29 +0000144 if (segment_merge_interval != -1)
bart8f822af2009-06-08 18:20:42 +0000145 DRD_(thread_set_segment_merge_interval)(segment_merge_interval);
bartbedfd232009-03-26 19:07:15 +0000146 if (show_confl_seg != -1)
147 DRD_(set_show_conflicting_segments)(show_confl_seg);
148 if (trace_address)
149 {
150 const Addr addr = VG_(strtoll16)(trace_address, 0);
151 DRD_(start_tracing_address_range)(addr, addr + 1);
152 }
153 if (trace_barrier != -1)
154 DRD_(barrier_set_trace)(trace_barrier);
155 if (trace_clientobj != -1)
156 DRD_(clientobj_set_trace)(trace_clientobj);
157 if (trace_cond != -1)
158 DRD_(cond_set_trace)(trace_cond);
159 if (trace_csw != -1)
160 DRD_(thread_trace_context_switches)(trace_csw);
161 if (trace_fork_join != -1)
162 DRD_(thread_set_trace_fork_join)(trace_fork_join);
163 if (trace_conflict_set != -1)
164 DRD_(thread_trace_conflict_set)(trace_conflict_set);
bart8f822af2009-06-08 18:20:42 +0000165 if (trace_conflict_set_bm != -1)
166 DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm);
bartbedfd232009-03-26 19:07:15 +0000167 if (trace_mutex != -1)
168 DRD_(mutex_set_trace)(trace_mutex);
169 if (trace_rwlock != -1)
170 DRD_(rwlock_set_trace)(trace_rwlock);
171 if (trace_segment != -1)
172 DRD_(sg_set_trace)(trace_segment);
173 if (trace_semaphore != -1)
174 DRD_(semaphore_set_trace)(trace_semaphore);
175 if (trace_suppression != -1)
176 DRD_(suppression_set_trace)(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000177
bartbedfd232009-03-26 19:07:15 +0000178 return True;
sewardjaf44c822007-11-25 14:01:38 +0000179}
180
bart1335ecc2009-02-14 16:10:53 +0000181static void DRD_(print_usage)(void)
bartbd7e56e2008-03-31 18:14:12 +0000182{
bartbedfd232009-03-26 19:07:15 +0000183 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000184" --check-stack-var=yes|no Whether or not to report data races on\n"
185" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000186" --exclusive-threshold=<n> Print an error message if any mutex or\n"
bart8600c422010-10-25 18:18:54 +0000187" writer lock is held longer than the specified\n"
188" time (in milliseconds) [off].\n"
bart1a3b0b32009-05-03 17:07:34 +0000189" --first-race-only=yes|no Only report the first data race that occurs on\n"
190" a memory location instead of all races [no].\n"
bart75c1cba2010-08-29 07:20:30 +0000191" --free-is-write=yes|no Whether to report races between freeing memory\n"
bartf9427fd2010-08-29 09:19:07 +0000192" and subsequent accesses of that memory[no].\n"
bart46b5fce2008-06-28 13:01:30 +0000193" --report-signal-unlocked=yes|no Whether to report calls to\n"
194" pthread_cond_signal() where the mutex associated\n"
195" with the signal via pthread_cond_wait() is not\n"
196" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000197" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000198" Segment merging is an algorithm to limit memory usage of the\n"
199" data race detection algorithm. Disabling segment merging may\n"
200" improve the accuracy of the so-called 'other segments' displayed\n"
201" in race reports but can also trigger an out of memory error.\n"
bart8f822af2009-06-08 18:20:42 +0000202" --segment-merging-interval=<n> Perform segment merging every time n new\n"
203" segments have been created. Default: %d.\n"
bart9d5b7962008-05-14 12:25:00 +0000204" --shared-threshold=<n> Print an error message if a reader lock\n"
bart8600c422010-10-25 18:18:54 +0000205" is held longer than the specified time (in\n"
206" milliseconds) [off]\n"
bart130463a2008-04-01 17:03:33 +0000207" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
208" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000209"\n"
bartef1b9722008-07-04 15:34:23 +0000210" drd options for monitoring process behavior:\n"
bart952e1a02008-04-06 13:06:36 +0000211" --trace-addr=<address> Trace all load and store activity for the.\n"
bart130463a2008-04-01 17:03:33 +0000212" specified address [off].\n"
bartf9427fd2010-08-29 09:19:07 +0000213" --trace-alloc=yes|no Trace all memory allocations and deallocations\n"" [no].\n"
bart130463a2008-04-01 17:03:33 +0000214" --trace-barrier=yes|no Trace all barrier activity [no].\n"
215" --trace-cond=yes|no Trace all condition variable activity [no].\n"
216" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
217" --trace-mutex=yes|no Trace all mutex activity [no].\n"
218" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart8f822af2009-06-08 18:20:42 +0000219" --trace-semaphore=yes|no Trace all semaphore activity [no].\n",
220DRD_(thread_get_segment_merge_interval)()
bartbedfd232009-03-26 19:07:15 +0000221);
sewardjaf44c822007-11-25 14:01:38 +0000222}
223
bart1335ecc2009-02-14 16:10:53 +0000224static void DRD_(print_debug_usage)(void)
bart31b983d2010-02-21 14:52:59 +0000225{
bartbedfd232009-03-26 19:07:15 +0000226 VG_(printf)(
bart130463a2008-04-01 17:03:33 +0000227" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
228" --trace-clientobj=yes|no Trace all client object activity [no].\n"
229" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000230" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart8f822af2009-06-08 18:20:42 +0000231" --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n"
232" updates [no]. Note: enabling this option\n"
233" will generate a lot of output !\n"
bart987781d2008-06-27 15:00:07 +0000234" --trace-segment=yes|no Trace segment actions [no].\n"
235" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bartbedfd232009-03-26 19:07:15 +0000236);
sewardjaf44c822007-11-25 14:01:38 +0000237}
238
239
240//
241// Implements the thread-related core callbacks.
242//
243
sewardjaf44c822007-11-25 14:01:38 +0000244static void drd_pre_mem_read(const CorePart part,
245 const ThreadId tid,
246 Char* const s,
247 const Addr a,
248 const SizeT size)
249{
bartbedfd232009-03-26 19:07:15 +0000250 if (size > 0)
251 {
252 DRD_(trace_load)(a, size);
253 }
sewardjaf44c822007-11-25 14:01:38 +0000254}
255
bart5e85d262008-03-01 10:49:37 +0000256static void drd_pre_mem_read_asciiz(const CorePart part,
257 const ThreadId tid,
258 Char* const s,
259 const Addr a)
260{
bartbedfd232009-03-26 19:07:15 +0000261 const char* p = (void*)a;
262 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000263
bartbedfd232009-03-26 19:07:15 +0000264 /* Note: the expression '*p' reads client memory and may crash if the */
265 /* client provided an invalid pointer ! */
266 while (*p)
267 {
268 p++;
269 size++;
270 }
bartbedfd232009-03-26 19:07:15 +0000271 if (size > 0)
272 {
273 DRD_(trace_load)(a, size);
274 }
bart5e85d262008-03-01 10:49:37 +0000275}
276
sewardjaf44c822007-11-25 14:01:38 +0000277static void drd_post_mem_write(const CorePart part,
278 const ThreadId tid,
279 const Addr a,
280 const SizeT size)
281{
bartbedfd232009-03-26 19:07:15 +0000282 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
283 if (size > 0)
284 {
285 DRD_(trace_store)(a, size);
286 }
sewardjaf44c822007-11-25 14:01:38 +0000287}
288
bart08865622008-06-06 14:31:36 +0000289static __inline__
bartf9427fd2010-08-29 09:19:07 +0000290void drd_start_using_mem(const Addr a1, const SizeT len,
291 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000292{
barta3003982010-09-08 16:29:17 +0000293 tl_assert(a1 <= a1 + len);
bart5e85d262008-03-01 10:49:37 +0000294
bartf9427fd2010-08-29 09:19:07 +0000295 if (!is_stack_mem && s_trace_alloc)
296 VG_(message)(Vg_UserMsg, "Started using memory range 0x%lx + %ld%s\n",
297 a1, len, DRD_(running_thread_inside_pthread_create)()
298 ? " (inside pthread_create())" : "");
299
bartbedfd232009-03-26 19:07:15 +0000300 if (UNLIKELY(DRD_(any_address_is_traced)()))
301 {
302 DRD_(trace_mem_access)(a1, len, eStart);
303 }
bart31b983d2010-02-21 14:52:59 +0000304
bartdd75cdf2009-07-24 08:20:10 +0000305 if (UNLIKELY(DRD_(running_thread_inside_pthread_create)()))
306 {
307 DRD_(start_suppression)(a1, a1 + len, "pthread_create()");
308 }
sewardjaf44c822007-11-25 14:01:38 +0000309}
310
sewardj7cf4e6b2008-05-01 20:24:26 +0000311static void drd_start_using_mem_w_ecu(const Addr a1,
312 const SizeT len,
313 UInt ec_uniq)
314{
bartf9427fd2010-08-29 09:19:07 +0000315 drd_start_using_mem(a1, len, False);
sewardj7cf4e6b2008-05-01 20:24:26 +0000316}
317
318static void drd_start_using_mem_w_tid(const Addr a1,
319 const SizeT len,
320 ThreadId tid)
321{
bartf9427fd2010-08-29 09:19:07 +0000322 drd_start_using_mem(a1, len, False);
sewardj7cf4e6b2008-05-01 20:24:26 +0000323}
324
bart0ffa4832008-04-05 12:57:01 +0000325static __inline__
326void drd_stop_using_mem(const Addr a1, const SizeT len,
327 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000328{
bartbedfd232009-03-26 19:07:15 +0000329 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000330
barta3003982010-09-08 16:29:17 +0000331 tl_assert(a1 <= a2);
bart5e85d262008-03-01 10:49:37 +0000332
bartbedfd232009-03-26 19:07:15 +0000333 if (UNLIKELY(DRD_(any_address_is_traced)()))
bartbedfd232009-03-26 19:07:15 +0000334 DRD_(trace_mem_access)(a1, len, eEnd);
bartf9427fd2010-08-29 09:19:07 +0000335
336 if (!is_stack_mem && s_trace_alloc)
337 VG_(message)(Vg_UserMsg, "Stopped using memory range 0x%lx + %ld\n",
338 a1, len);
339
bart07595032010-08-29 09:51:06 +0000340 if (!is_stack_mem || DRD_(get_check_stack_accesses)())
bartbedfd232009-03-26 19:07:15 +0000341 {
bartf9427fd2010-08-29 09:19:07 +0000342 DRD_(thread_stop_using_mem)(a1, a2, !is_stack_mem && s_free_is_write);
bartbedfd232009-03-26 19:07:15 +0000343 DRD_(clientobj_stop_using_mem)(a1, a2);
344 DRD_(suppression_stop_using_mem)(a1, a2);
345 }
bart07595032010-08-29 09:51:06 +0000346 if (!is_stack_mem && s_free_is_write)
347 DRD_(trace_store)(a1, len);
bart0ffa4832008-04-05 12:57:01 +0000348}
349
350static __inline__
351void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
352{
bartbedfd232009-03-26 19:07:15 +0000353 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000354}
355
bart1335ecc2009-02-14 16:10:53 +0000356/**
bartfdaa0182009-03-10 09:25:32 +0000357 * Discard all information DRD has about memory accesses and client objects
358 * in the specified address range.
359 */
360void DRD_(clean_memory)(const Addr a1, const SizeT len)
361{
bartbedfd232009-03-26 19:07:15 +0000362 const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
363 drd_stop_using_mem(a1, len, is_stack_memory);
bartf9427fd2010-08-29 09:19:07 +0000364 drd_start_using_mem(a1, len, is_stack_memory);
bartfdaa0182009-03-10 09:25:32 +0000365}
366
367/**
bart1335ecc2009-02-14 16:10:53 +0000368 * Suppress data race reports on all addresses contained in .plt and
369 * .got.plt sections inside the address range [ a, a + len [. The data in
370 * these sections is modified by _dl_relocate_object() every time a function
371 * in a shared library is called for the first time. Since the first call
372 * to a function in a shared library can happen from a multithreaded context,
373 * such calls can cause conflicting accesses. See also Ulrich Drepper's
374 * paper "How to Write Shared Libraries" for more information about relocation
375 * (http://people.redhat.com/drepper/dsohowto.pdf).
bartcb2d0072008-05-31 07:55:51 +0000376 */
bart1335ecc2009-02-14 16:10:53 +0000377static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
bartcb2d0072008-05-31 07:55:51 +0000378{
bartbedfd232009-03-26 19:07:15 +0000379 const DebugInfo* di;
bartcb2d0072008-05-31 07:55:51 +0000380
381#if 0
bartbedfd232009-03-26 19:07:15 +0000382 VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
bartcb2d0072008-05-31 07:55:51 +0000383#endif
384
sewardje3f1e592009-07-31 09:41:29 +0000385 for (di = VG_(next_DebugInfo)(0); di; di = VG_(next_DebugInfo)(di))
bartbedfd232009-03-26 19:07:15 +0000386 {
387 Addr avma;
388 SizeT size;
bartcb2d0072008-05-31 07:55:51 +0000389
sewardje3f1e592009-07-31 09:41:29 +0000390 avma = VG_(DebugInfo_get_plt_avma)(di);
391 size = VG_(DebugInfo_get_plt_size)(di);
bartbedfd232009-03-26 19:07:15 +0000392 tl_assert((avma && size) || (avma == 0 && size == 0));
393 if (size > 0)
394 {
bartcb2d0072008-05-31 07:55:51 +0000395#if 0
bartbedfd232009-03-26 19:07:15 +0000396 VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
bartcb2d0072008-05-31 07:55:51 +0000397#endif
sewardje3f1e592009-07-31 09:41:29 +0000398 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
bartbedfd232009-03-26 19:07:15 +0000399 DRD_(start_suppression)(avma, avma + size, ".plt");
400 }
bartcb2d0072008-05-31 07:55:51 +0000401
sewardje3f1e592009-07-31 09:41:29 +0000402 avma = VG_(DebugInfo_get_gotplt_avma)(di);
403 size = VG_(DebugInfo_get_gotplt_size)(di);
bartbedfd232009-03-26 19:07:15 +0000404 tl_assert((avma && size) || (avma == 0 && size == 0));
405 if (size > 0)
406 {
bartcb2d0072008-05-31 07:55:51 +0000407#if 0
bartbedfd232009-03-26 19:07:15 +0000408 VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
bartcb2d0072008-05-31 07:55:51 +0000409#endif
sewardje3f1e592009-07-31 09:41:29 +0000410 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
bartbedfd232009-03-26 19:07:15 +0000411 DRD_(start_suppression)(avma, avma + size, ".gotplt");
412 }
413 }
bartcb2d0072008-05-31 07:55:51 +0000414}
415
bart5e85d262008-03-01 10:49:37 +0000416static
417void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
sewardj9c606bd2008-09-18 18:12:50 +0000418 const Bool rr, const Bool ww, const Bool xx,
419 ULong di_handle)
bart5e85d262008-03-01 10:49:37 +0000420{
bartbedfd232009-03-26 19:07:15 +0000421 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bartd5765912008-03-16 08:40:55 +0000422
bartf9427fd2010-08-29 09:19:07 +0000423 drd_start_using_mem(a, len, False);
bartcb2d0072008-05-31 07:55:51 +0000424
bartbedfd232009-03-26 19:07:15 +0000425 DRD_(suppress_relocation_conflicts)(a, len);
bart5e85d262008-03-01 10:49:37 +0000426}
427
sewardjaf44c822007-11-25 14:01:38 +0000428/* Called by the core when the stack of a thread grows, to indicate that */
429/* the addresses in range [ a, a + len [ may now be used by the client. */
430/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000431static __inline__
432void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000433{
bartbedfd232009-03-26 19:07:15 +0000434 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
435 a - VG_STACK_REDZONE_SZB);
bart31b983d2010-02-21 14:52:59 +0000436 drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
bartf9427fd2010-08-29 09:19:07 +0000437 len + VG_STACK_REDZONE_SZB,
438 True);
sewardjaf44c822007-11-25 14:01:38 +0000439}
440
441/* Called by the core when the stack of a thread shrinks, to indicate that */
442/* the addresses [ a, a + len [ are no longer accessible for the client. */
443/* Assumption: stacks grow downward. */
bart08865622008-06-06 14:31:36 +0000444static __inline__
445void drd_stop_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000446{
bartbedfd232009-03-26 19:07:15 +0000447 DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
448 a + len - VG_STACK_REDZONE_SZB);
449 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
450 True);
sewardjaf44c822007-11-25 14:01:38 +0000451}
452
bart68a8afa2010-09-02 14:50:41 +0000453static
454Bool on_alt_stack(const Addr a)
455{
456 ThreadId vg_tid;
457 Addr alt_min;
458 SizeT alt_size;
459
460 vg_tid = VG_(get_running_tid)();
461 alt_min = VG_(thread_get_altstack_min)(vg_tid);
462 alt_size = VG_(thread_get_altstack_size)(vg_tid);
463 return (SizeT)(a - alt_min) < alt_size;
464}
465
466static
467void drd_start_using_mem_alt_stack(const Addr a, const SizeT len)
468{
469 if (!on_alt_stack(a))
470 drd_start_using_mem_stack(a, len);
471}
472
473static
474void drd_stop_using_mem_alt_stack(const Addr a, const SizeT len)
475{
476 if (!on_alt_stack(a))
477 drd_stop_using_mem_stack(a, len);
478}
479
480/**
481 * Callback function invoked by the Valgrind core before a signal is delivered.
482 */
483static
484void drd_pre_deliver_signal(const ThreadId vg_tid, const Int sigNo,
485 const Bool alt_stack)
486{
487 DrdThreadId drd_tid;
488
489 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
490 DRD_(thread_set_on_alt_stack)(drd_tid, alt_stack);
491 if (alt_stack)
492 {
493 /*
494 * As soon a signal handler has been invoked on the alternate stack,
495 * switch to stack memory handling functions that can handle the
496 * alternate stack.
497 */
498 VG_(track_new_mem_stack)(drd_start_using_mem_alt_stack);
499 VG_(track_die_mem_stack)(drd_stop_using_mem_alt_stack);
500 }
501}
502
503/**
504 * Callback function invoked by the Valgrind core after a signal is delivered,
505 * at least if the signal handler did not longjmp().
506 */
507static
508void drd_post_deliver_signal(const ThreadId vg_tid, const Int sigNo)
509{
510 DrdThreadId drd_tid;
511
512 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
513 DRD_(thread_set_on_alt_stack)(drd_tid, False);
514 if (DRD_(thread_get_threads_on_alt_stack)() == 0)
515 {
516 VG_(track_new_mem_stack)(drd_start_using_mem_stack);
517 VG_(track_die_mem_stack)(drd_stop_using_mem_stack);
518 }
519}
520
bartbedfd232009-03-26 19:07:15 +0000521/**
522 * Callback function called by the Valgrind core before a stack area is
523 * being used by a signal handler.
524 *
525 * @param[in] a Start of address range.
526 * @param[in] len Address range length.
527 * @param[in] tid Valgrind thread ID for whom the signal frame is being
528 * constructed.
529 */
530static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
531 ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000532{
bartbedfd232009-03-26 19:07:15 +0000533 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bartf9427fd2010-08-29 09:19:07 +0000534 drd_start_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000535}
536
bart5e85d262008-03-01 10:49:37 +0000537static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000538{
bartbedfd232009-03-26 19:07:15 +0000539 drd_stop_using_mem(a, len, True);
sewardjaf44c822007-11-25 14:01:38 +0000540}
541
542static
543void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
544{
bartbedfd232009-03-26 19:07:15 +0000545 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
546 tl_assert(created != VG_INVALID_THREADID);
547 DRD_(thread_pre_create)(drd_creator, created);
548 if (DRD_(IsValidDrdThreadId)(drd_creator))
549 {
550 DRD_(thread_new_segment)(drd_creator);
551 }
552 if (DRD_(thread_get_trace_fork_join)())
553 {
554 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000555 "drd_pre_thread_create creator = %d, created = %d\n",
556 drd_creator, created);
bartbedfd232009-03-26 19:07:15 +0000557 }
sewardjaf44c822007-11-25 14:01:38 +0000558}
559
560/* Called by Valgrind's core before any loads or stores are performed on */
561/* the context of thread "created". At startup, this function is called */
562/* with arguments (0,1). */
563static
bart0ffa4832008-04-05 12:57:01 +0000564void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000565{
bartbedfd232009-03-26 19:07:15 +0000566 DrdThreadId drd_created;
bart0ffa4832008-04-05 12:57:01 +0000567
bartbedfd232009-03-26 19:07:15 +0000568 tl_assert(vg_created != VG_INVALID_THREADID);
bart0ffa4832008-04-05 12:57:01 +0000569
bartbedfd232009-03-26 19:07:15 +0000570 drd_created = DRD_(thread_post_create)(vg_created);
571 if (DRD_(thread_get_trace_fork_join)())
572 {
573 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000574 "drd_post_thread_create created = %d\n",
575 drd_created);
bartbedfd232009-03-26 19:07:15 +0000576 }
577 if (! DRD_(get_check_stack_accesses)())
578 {
579 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
580 - DRD_(thread_get_stack_size)(drd_created),
581 DRD_(thread_get_stack_max)(drd_created),
582 "stack");
583 }
sewardjaf44c822007-11-25 14:01:38 +0000584}
585
sewardjaf44c822007-11-25 14:01:38 +0000586/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000587static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000588{
bartbedfd232009-03-26 19:07:15 +0000589 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000590
bartbedfd232009-03-26 19:07:15 +0000591 tl_assert(VG_(get_running_tid)() == vg_tid);
sewardj85642922008-01-14 11:54:56 +0000592
bartbedfd232009-03-26 19:07:15 +0000593 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
594 if (DRD_(thread_get_trace_fork_join)())
595 {
596 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000597 "drd_thread_finished tid = %d%s\n",
bartbedfd232009-03-26 19:07:15 +0000598 drd_tid,
599 DRD_(thread_get_joinable)(drd_tid)
600 ? ""
601 : " (which is a detached thread)");
602 }
bart75c1cba2010-08-29 07:20:30 +0000603 if (s_show_stack_usage)
bartbedfd232009-03-26 19:07:15 +0000604 {
605 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
606 const SizeT used_stack
607 = (DRD_(thread_get_stack_max)(drd_tid)
608 - DRD_(thread_get_stack_min_min)(drd_tid));
609 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000610 "thread %d%s finished and used %ld bytes out of %ld"
sewardj1e29ebc2009-07-15 14:49:17 +0000611 " on its stack. Margin: %ld bytes.\n",
bartbedfd232009-03-26 19:07:15 +0000612 drd_tid,
613 DRD_(thread_get_joinable)(drd_tid)
614 ? ""
615 : " (which is a detached thread)",
616 used_stack,
617 stack_size,
618 stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000619
bartbedfd232009-03-26 19:07:15 +0000620 }
621 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
622 DRD_(thread_get_stack_max)(drd_tid)
623 - DRD_(thread_get_stack_min)(drd_tid),
624 True);
bartd45d9952009-05-31 18:53:54 +0000625 DRD_(thread_set_record_loads)(drd_tid, False);
626 DRD_(thread_set_record_stores)(drd_tid, False);
bartbedfd232009-03-26 19:07:15 +0000627 DRD_(thread_finished)(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000628}
629
sewardjaf44c822007-11-25 14:01:38 +0000630//
631// Implementation of the tool interface.
632//
633
bart1335ecc2009-02-14 16:10:53 +0000634static void DRD_(post_clo_init)(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000635{
bart0c6bf352009-07-25 14:03:53 +0000636#if defined(VGO_linux) || defined(VGO_darwin)
bartc6ef4af2009-07-23 10:10:30 +0000637 /* fine */
bartc6ef4af2009-07-23 10:10:30 +0000638#else
639 VG_(printf)("\nWARNING: DRD has not yet been tested on this operating system.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000640# endif
bart95761b52008-03-29 08:34:03 +0000641
bart75c1cba2010-08-29 07:20:30 +0000642 if (s_var_info)
bartbedfd232009-03-26 19:07:15 +0000643 {
644 VG_(needs_var_info)();
645 }
sewardjdcbb8d32007-11-26 21:34:30 +0000646}
sewardjaf44c822007-11-25 14:01:38 +0000647
sewardjaf44c822007-11-25 14:01:38 +0000648static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
649{
bartbedfd232009-03-26 19:07:15 +0000650 tl_assert(tid == VG_(get_running_tid)());
651 DRD_(thread_set_vg_running_tid)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000652}
653
bart1335ecc2009-02-14 16:10:53 +0000654static void DRD_(fini)(Int exitcode)
sewardjaf44c822007-11-25 14:01:38 +0000655{
bartbedfd232009-03-26 19:07:15 +0000656 // thread_print_all();
sewardj2d9e8742009-08-07 15:46:56 +0000657 if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) {
bart31b983d2010-02-21 14:52:59 +0000658 VG_(message)(Vg_UserMsg,
sewardj2d9e8742009-08-07 15:46:56 +0000659 "For counts of detected and suppressed errors, "
660 "rerun with: -v\n");
661 }
662
bart75c1cba2010-08-29 07:20:30 +0000663 if (VG_(clo_stats) || s_print_stats)
bartbedfd232009-03-26 19:07:15 +0000664 {
barte5214662009-06-21 11:51:23 +0000665 ULong pu = DRD_(thread_get_update_conflict_set_count)();
666 ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)();
667 ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)();
668 ULong pu_join = DRD_(thread_get_update_conflict_set_join_count)();
669
bartbedfd232009-03-26 19:07:15 +0000670 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000671 " thread: %lld context switches.\n",
bart54803fe2009-06-21 09:26:27 +0000672 DRD_(thread_get_context_switch_count)());
bartbedfd232009-03-26 19:07:15 +0000673 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000674 "confl set: %lld full updates and %lld partial updates;\n",
bart54803fe2009-06-21 09:26:27 +0000675 DRD_(thread_get_compute_conflict_set_count)(),
barte5214662009-06-21 11:51:23 +0000676 pu);
bartbedfd232009-03-26 19:07:15 +0000677 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000678 " %lld partial updates during segment creation,\n",
barte5214662009-06-21 11:51:23 +0000679 pu_seg_cr);
680 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000681 " %lld because of mutex/sema/cond.var. operations,\n",
barte5214662009-06-21 11:51:23 +0000682 pu_mtx_cv);
683 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000684 " %lld because of barrier/rwlock operations and\n",
barte5214662009-06-21 11:51:23 +0000685 pu - pu_seg_cr - pu_mtx_cv - pu_join);
686 VG_(message)(Vg_UserMsg,
687 " %lld partial updates because of thread join"
sewardj1e29ebc2009-07-15 14:49:17 +0000688 " operations.\n",
barte5214662009-06-21 11:51:23 +0000689 pu_join);
690 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000691 " segments: created %lld segments, max %lld alive,\n",
bart9dedafe2009-06-21 18:09:02 +0000692 DRD_(sg_get_segments_created_count)(),
693 DRD_(sg_get_max_segments_alive_count)());
bartbedfd232009-03-26 19:07:15 +0000694 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000695 " %lld discard points and %lld merges.\n",
bart9dedafe2009-06-21 18:09:02 +0000696 DRD_(thread_get_discard_ordered_segments_count)(),
bart1a3b0b32009-05-03 17:07:34 +0000697 DRD_(sg_get_segment_merge_count)());
698 VG_(message)(Vg_UserMsg,
bart54803fe2009-06-21 09:26:27 +0000699 "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and"
sewardj1e29ebc2009-07-15 14:49:17 +0000700 " %lld barrier.\n",
bartbedfd232009-03-26 19:07:15 +0000701 DRD_(get_mutex_segment_creation_count)(),
702 DRD_(get_rwlock_segment_creation_count)(),
703 DRD_(get_semaphore_segment_creation_count)(),
704 DRD_(get_barrier_segment_creation_count)());
705 VG_(message)(Vg_UserMsg,
bart9dedafe2009-06-21 18:09:02 +0000706 " bitmaps: %lld level one"
sewardj1e29ebc2009-07-15 14:49:17 +0000707 " and %lld level two bitmaps were allocated.\n",
bartbedfd232009-03-26 19:07:15 +0000708 DRD_(bm_get_bitmap_creation_count)(),
bartbedfd232009-03-26 19:07:15 +0000709 DRD_(bm_get_bitmap2_creation_count)());
710 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000711 " mutex: %lld non-recursive lock/unlock events.\n",
bartbedfd232009-03-26 19:07:15 +0000712 DRD_(get_mutex_lock_count)());
713 DRD_(print_malloc_stats)();
714 }
sewardjaf44c822007-11-25 14:01:38 +0000715}
716
sewardjaf44c822007-11-25 14:01:38 +0000717static
718void drd_pre_clo_init(void)
719{
njnf76d27a2009-05-28 01:53:07 +0000720 // Basic tool stuff.
bartbedfd232009-03-26 19:07:15 +0000721 VG_(details_name) ("drd");
722 VG_(details_version) (NULL);
723 VG_(details_description) ("a thread error detector");
sewardj9eecbbb2010-05-03 21:37:12 +0000724 VG_(details_copyright_author)("Copyright (C) 2006-2010, and GNU GPL'd,"
bartbedfd232009-03-26 19:07:15 +0000725 " by Bart Van Assche.");
726 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +0000727
bartbedfd232009-03-26 19:07:15 +0000728 VG_(basic_tool_funcs) (DRD_(post_clo_init),
729 DRD_(instrument),
730 DRD_(fini));
sewardjaf44c822007-11-25 14:01:38 +0000731
bartbedfd232009-03-26 19:07:15 +0000732 // Command line stuff.
733 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
734 DRD_(print_usage),
735 DRD_(print_debug_usage));
sewardjaf44c822007-11-25 14:01:38 +0000736
bartbedfd232009-03-26 19:07:15 +0000737 // Error handling.
738 DRD_(register_error_handlers)();
sewardjaf44c822007-11-25 14:01:38 +0000739
bartbedfd232009-03-26 19:07:15 +0000740 // Core event tracking.
741 VG_(track_pre_mem_read) (drd_pre_mem_read);
742 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
743 VG_(track_post_mem_write) (drd_post_mem_write);
744 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
745 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
746 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
747 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
748 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
749 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
750 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
751 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
752 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
bart68a8afa2010-09-02 14:50:41 +0000753 VG_(track_pre_deliver_signal) (drd_pre_deliver_signal);
754 VG_(track_post_deliver_signal) (drd_post_deliver_signal);
bartbedfd232009-03-26 19:07:15 +0000755 VG_(track_start_client_code) (drd_start_client_code);
756 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
757 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
758 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
sewardjaf44c822007-11-25 14:01:38 +0000759
bartbedfd232009-03-26 19:07:15 +0000760 // Other stuff.
761 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
762 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +0000763
bartbedfd232009-03-26 19:07:15 +0000764 DRD_(clientreq_init)();
sewardjaf44c822007-11-25 14:01:38 +0000765
bartbedfd232009-03-26 19:07:15 +0000766 DRD_(suppression_init)();
bart4bb53d82008-02-28 19:06:34 +0000767
bartbedfd232009-03-26 19:07:15 +0000768 DRD_(clientobj_init)();
barte5e0efa2009-06-24 18:35:02 +0000769
770 {
771 Char* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL");
772 if (smi)
773 DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL));
774 }
sewardjaf44c822007-11-25 14:01:38 +0000775}
776
777
778VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)