blob: f4417d9ab5a343b9eb3a5b9d46e2df13fa5d8c0f [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
sewardj03f8d3f2012-08-05 15:46:46 +00004 Copyright (C) 2006-2012 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
sewardj85642922008-01-14 11:54:56 +000025#include "drd_barrier.h"
bart4bb53d82008-02-28 19:06:34 +000026#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000027#include "drd_clientreq.h"
sewardjaf44c822007-11-25 14:01:38 +000028#include "drd_cond.h"
29#include "drd_error.h"
bartc1824672011-03-12 14:07:09 +000030#include "drd_hb.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"
sewardj234e5582011-02-09 12:47:23 +000054#include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client)
sewardjaf44c822007-11-25 14:01:38 +000055
56
bart1335ecc2009-02-14 16:10:53 +000057/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000058
bart7d5f5232011-03-13 09:08:10 +000059static Bool s_print_stats;
60static Bool s_var_info;
61static Bool s_show_stack_usage;
62static Bool s_trace_alloc;
bartc0cd28f2013-08-24 17:51:18 +000063static Bool trace_sectsuppr;
sewardjaf44c822007-11-25 14:01:38 +000064
65
bart1335ecc2009-02-14 16:10:53 +000066/**
67 * Implement the needs_command_line_options for drd.
68 */
florian19f91bb2012-11-10 22:29:54 +000069static Bool DRD_(process_cmd_line_option)(const HChar* arg)
sewardjaf44c822007-11-25 14:01:38 +000070{
bartbedfd232009-03-26 19:07:15 +000071 int check_stack_accesses = -1;
bart6d956dc2011-07-28 09:54:37 +000072 int join_list_vol = -1;
bartbedfd232009-03-26 19:07:15 +000073 int exclusive_threshold_ms = -1;
bartf98a5692009-05-03 17:17:37 +000074 int first_race_only = -1;
bartbedfd232009-03-26 19:07:15 +000075 int report_signal_unlocked = -1;
76 int segment_merging = -1;
bart8f822af2009-06-08 18:20:42 +000077 int segment_merge_interval = -1;
bartbedfd232009-03-26 19:07:15 +000078 int shared_threshold_ms = -1;
79 int show_confl_seg = -1;
80 int trace_barrier = -1;
81 int trace_clientobj = -1;
82 int trace_cond = -1;
83 int trace_csw = -1;
84 int trace_fork_join = -1;
bartc1824672011-03-12 14:07:09 +000085 int trace_hb = -1;
bartbedfd232009-03-26 19:07:15 +000086 int trace_conflict_set = -1;
bart8f822af2009-06-08 18:20:42 +000087 int trace_conflict_set_bm = -1;
bartbedfd232009-03-26 19:07:15 +000088 int trace_mutex = -1;
89 int trace_rwlock = -1;
90 int trace_segment = -1;
91 int trace_semaphore = -1;
92 int trace_suppression = -1;
florian19f91bb2012-11-10 22:29:54 +000093 const HChar* trace_address = 0;
94 const HChar* ptrace_address= 0;
sewardjaf44c822007-11-25 14:01:38 +000095
bartbedfd232009-03-26 19:07:15 +000096 if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {}
bart6d956dc2011-07-28 09:54:37 +000097 else if VG_INT_CLO (arg, "--join-list-vol", join_list_vol) {}
bart75c1cba2010-08-29 07:20:30 +000098 else if VG_BOOL_CLO(arg, "--drd-stats", s_print_stats) {}
bartf98a5692009-05-03 17:17:37 +000099 else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {}
bart7d5f5232011-03-13 09:08:10 +0000100 else if VG_BOOL_CLO(arg, "--free-is-write", DRD_(g_free_is_write)) {}
bart8f822af2009-06-08 18:20:42 +0000101 else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
102 {}
bartbedfd232009-03-26 19:07:15 +0000103 else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {}
bart8f822af2009-06-08 18:20:42 +0000104 else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval)
105 {}
bartbedfd232009-03-26 19:07:15 +0000106 else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {}
bart75c1cba2010-08-29 07:20:30 +0000107 else if VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) {}
bartf9427fd2010-08-29 09:19:07 +0000108 else if VG_BOOL_CLO(arg, "--trace-alloc", s_trace_alloc) {}
bartbedfd232009-03-26 19:07:15 +0000109 else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {}
110 else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {}
111 else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {}
112 else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {}
bart8f822af2009-06-08 18:20:42 +0000113 else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){}
bartbedfd232009-03-26 19:07:15 +0000114 else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {}
115 else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {}
bartc1824672011-03-12 14:07:09 +0000116 else if VG_BOOL_CLO(arg, "--trace-hb", trace_hb) {}
bartbedfd232009-03-26 19:07:15 +0000117 else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {}
118 else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {}
bartc0cd28f2013-08-24 17:51:18 +0000119 else if VG_BOOL_CLO(arg, "--trace-sectsuppr", trace_sectsuppr) {}
bartbedfd232009-03-26 19:07:15 +0000120 else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {}
121 else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {}
122 else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {}
bart75c1cba2010-08-29 07:20:30 +0000123 else if VG_BOOL_CLO(arg, "--var-info", s_var_info) {}
bartbedfd232009-03-26 19:07:15 +0000124 else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
bart41a378a2012-01-24 18:39:29 +0000125 else if VG_STR_CLO (arg, "--ptrace-addr", ptrace_address) {}
bartbedfd232009-03-26 19:07:15 +0000126 else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {}
127 else if VG_STR_CLO (arg, "--trace-addr", trace_address) {}
128 else
129 return VG_(replacement_malloc_process_cmd_line_option)(arg);
sewardjaf44c822007-11-25 14:01:38 +0000130
bartbedfd232009-03-26 19:07:15 +0000131 if (check_stack_accesses != -1)
132 DRD_(set_check_stack_accesses)(check_stack_accesses);
133 if (exclusive_threshold_ms != -1)
134 {
135 DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
136 DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
137 }
bartf98a5692009-05-03 17:17:37 +0000138 if (first_race_only != -1)
139 {
140 DRD_(set_first_race_only)(first_race_only);
141 }
bart6d956dc2011-07-28 09:54:37 +0000142 if (join_list_vol != -1)
143 DRD_(thread_set_join_list_vol)(join_list_vol);
bartbedfd232009-03-26 19:07:15 +0000144 if (report_signal_unlocked != -1)
145 {
146 DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
147 }
148 if (shared_threshold_ms != -1)
149 {
150 DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
151 }
152 if (segment_merging != -1)
153 DRD_(thread_set_segment_merging)(segment_merging);
bart109d5da2009-06-22 18:06:29 +0000154 if (segment_merge_interval != -1)
bart8f822af2009-06-08 18:20:42 +0000155 DRD_(thread_set_segment_merge_interval)(segment_merge_interval);
bartbedfd232009-03-26 19:07:15 +0000156 if (show_confl_seg != -1)
157 DRD_(set_show_conflicting_segments)(show_confl_seg);
bart41a378a2012-01-24 18:39:29 +0000158 if (trace_address) {
bartbedfd232009-03-26 19:07:15 +0000159 const Addr addr = VG_(strtoll16)(trace_address, 0);
bart41a378a2012-01-24 18:39:29 +0000160 DRD_(start_tracing_address_range)(addr, addr + 1, False);
161 }
162 if (ptrace_address) {
163 const Addr addr = VG_(strtoll16)(ptrace_address, 0);
164 DRD_(start_tracing_address_range)(addr, addr + 1, True);
bartbedfd232009-03-26 19:07:15 +0000165 }
166 if (trace_barrier != -1)
167 DRD_(barrier_set_trace)(trace_barrier);
168 if (trace_clientobj != -1)
169 DRD_(clientobj_set_trace)(trace_clientobj);
170 if (trace_cond != -1)
171 DRD_(cond_set_trace)(trace_cond);
172 if (trace_csw != -1)
173 DRD_(thread_trace_context_switches)(trace_csw);
174 if (trace_fork_join != -1)
175 DRD_(thread_set_trace_fork_join)(trace_fork_join);
bartc1824672011-03-12 14:07:09 +0000176 if (trace_hb != -1)
177 DRD_(hb_set_trace)(trace_hb);
bartbedfd232009-03-26 19:07:15 +0000178 if (trace_conflict_set != -1)
179 DRD_(thread_trace_conflict_set)(trace_conflict_set);
bart8f822af2009-06-08 18:20:42 +0000180 if (trace_conflict_set_bm != -1)
181 DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm);
bartbedfd232009-03-26 19:07:15 +0000182 if (trace_mutex != -1)
183 DRD_(mutex_set_trace)(trace_mutex);
184 if (trace_rwlock != -1)
185 DRD_(rwlock_set_trace)(trace_rwlock);
186 if (trace_segment != -1)
187 DRD_(sg_set_trace)(trace_segment);
188 if (trace_semaphore != -1)
189 DRD_(semaphore_set_trace)(trace_semaphore);
190 if (trace_suppression != -1)
191 DRD_(suppression_set_trace)(trace_suppression);
sewardjaf44c822007-11-25 14:01:38 +0000192
bartbedfd232009-03-26 19:07:15 +0000193 return True;
sewardjaf44c822007-11-25 14:01:38 +0000194}
195
bart1335ecc2009-02-14 16:10:53 +0000196static void DRD_(print_usage)(void)
bartbd7e56e2008-03-31 18:14:12 +0000197{
bartbedfd232009-03-26 19:07:15 +0000198 VG_(printf)(
bart0ffa4832008-04-05 12:57:01 +0000199" --check-stack-var=yes|no Whether or not to report data races on\n"
200" stack variables [no].\n"
bart9d5b7962008-05-14 12:25:00 +0000201" --exclusive-threshold=<n> Print an error message if any mutex or\n"
bart8600c422010-10-25 18:18:54 +0000202" writer lock is held longer than the specified\n"
203" time (in milliseconds) [off].\n"
bart1a3b0b32009-05-03 17:07:34 +0000204" --first-race-only=yes|no Only report the first data race that occurs on\n"
205" a memory location instead of all races [no].\n"
bart639d0ad2011-03-12 14:26:01 +0000206" --free-is-write=yes|no Whether to report races between freeing memory\n"
207" and subsequent accesses of that memory[no].\n"
bart505b35c2011-10-13 18:51:47 +0000208" --join-list-vol=<n> Number of threads to delay cleanup for [10].\n"
bart46b5fce2008-06-28 13:01:30 +0000209" --report-signal-unlocked=yes|no Whether to report calls to\n"
210" pthread_cond_signal() where the mutex associated\n"
211" with the signal via pthread_cond_wait() is not\n"
212" locked at the time the signal is sent [yes].\n"
bart130463a2008-04-01 17:03:33 +0000213" --segment-merging=yes|no Controls segment merging [yes].\n"
bartbd7e56e2008-03-31 18:14:12 +0000214" Segment merging is an algorithm to limit memory usage of the\n"
215" data race detection algorithm. Disabling segment merging may\n"
216" improve the accuracy of the so-called 'other segments' displayed\n"
217" in race reports but can also trigger an out of memory error.\n"
bart8f822af2009-06-08 18:20:42 +0000218" --segment-merging-interval=<n> Perform segment merging every time n new\n"
219" segments have been created. Default: %d.\n"
bart9d5b7962008-05-14 12:25:00 +0000220" --shared-threshold=<n> Print an error message if a reader lock\n"
bart8600c422010-10-25 18:18:54 +0000221" is held longer than the specified time (in\n"
222" milliseconds) [off]\n"
bart130463a2008-04-01 17:03:33 +0000223" --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n"
224" --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
bartbd7e56e2008-03-31 18:14:12 +0000225"\n"
bartef1b9722008-07-04 15:34:23 +0000226" drd options for monitoring process behavior:\n"
bart41a378a2012-01-24 18:39:29 +0000227" --ptrace-addr=<address> Trace all load and store activity for the\n"
228" specified address and keep doing that even after\n"
229" the memory at that address has been freed and\n"
230" reallocated [off].\n"
bart8c3de752011-12-12 15:49:00 +0000231" --trace-addr=<address> Trace all load and store activity for the\n"
bart130463a2008-04-01 17:03:33 +0000232" specified address [off].\n"
bartf9427fd2010-08-29 09:19:07 +0000233" --trace-alloc=yes|no Trace all memory allocations and deallocations\n"" [no].\n"
bart130463a2008-04-01 17:03:33 +0000234" --trace-barrier=yes|no Trace all barrier activity [no].\n"
235" --trace-cond=yes|no Trace all condition variable activity [no].\n"
236" --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n"
bart505b35c2011-10-13 18:51:47 +0000237" --trace-hb=yes|no Trace ANNOTATE_HAPPENS_BEFORE() etc. [no].\n"
bart130463a2008-04-01 17:03:33 +0000238" --trace-mutex=yes|no Trace all mutex activity [no].\n"
239" --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n"
bart8f822af2009-06-08 18:20:42 +0000240" --trace-semaphore=yes|no Trace all semaphore activity [no].\n",
241DRD_(thread_get_segment_merge_interval)()
bartbedfd232009-03-26 19:07:15 +0000242);
sewardjaf44c822007-11-25 14:01:38 +0000243}
244
bart1335ecc2009-02-14 16:10:53 +0000245static void DRD_(print_debug_usage)(void)
bart31b983d2010-02-21 14:52:59 +0000246{
bartbedfd232009-03-26 19:07:15 +0000247 VG_(printf)(
bart130463a2008-04-01 17:03:33 +0000248" --drd-stats=yes|no Print statistics about DRD activity [no].\n"
249" --trace-clientobj=yes|no Trace all client object activity [no].\n"
250" --trace-csw=yes|no Trace all scheduler context switches [no].\n"
barte73b0aa2008-06-28 07:19:56 +0000251" --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
bart8f822af2009-06-08 18:20:42 +0000252" --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n"
253" updates [no]. Note: enabling this option\n"
254" will generate a lot of output !\n"
bartc0cd28f2013-08-24 17:51:18 +0000255" --trace-sectsuppr=yes|no Trace which the dynamic library sections on\n"
256" which data race detection is suppressed.\n"
bart987781d2008-06-27 15:00:07 +0000257" --trace-segment=yes|no Trace segment actions [no].\n"
258" --trace-suppr=yes|no Trace all address suppression actions [no].\n"
bartbedfd232009-03-26 19:07:15 +0000259);
sewardjaf44c822007-11-25 14:01:38 +0000260}
261
262
263//
264// Implements the thread-related core callbacks.
265//
266
sewardjaf44c822007-11-25 14:01:38 +0000267static void drd_pre_mem_read(const CorePart part,
268 const ThreadId tid,
floriane543f302012-10-21 19:43:43 +0000269 const HChar* const s,
sewardjaf44c822007-11-25 14:01:38 +0000270 const Addr a,
271 const SizeT size)
272{
bartbedfd232009-03-26 19:07:15 +0000273 if (size > 0)
274 {
275 DRD_(trace_load)(a, size);
276 }
sewardjaf44c822007-11-25 14:01:38 +0000277}
278
bart5e85d262008-03-01 10:49:37 +0000279static void drd_pre_mem_read_asciiz(const CorePart part,
280 const ThreadId tid,
floriane543f302012-10-21 19:43:43 +0000281 const HChar* const s,
bart5e85d262008-03-01 10:49:37 +0000282 const Addr a)
283{
florian19f91bb2012-11-10 22:29:54 +0000284 const HChar* p = (void*)a;
bartbedfd232009-03-26 19:07:15 +0000285 SizeT size = 0;
bart5e85d262008-03-01 10:49:37 +0000286
sewardj234e5582011-02-09 12:47:23 +0000287 // Don't segfault if the string starts in an obviously stupid
288 // place. Actually we should check the whole string, not just
289 // the start address, but that's too much trouble. At least
290 // checking the first byte is better than nothing. See #255009.
291 if (!VG_(am_is_valid_for_client) (a, 1, VKI_PROT_READ))
292 return;
293
bartbedfd232009-03-26 19:07:15 +0000294 /* Note: the expression '*p' reads client memory and may crash if the */
295 /* client provided an invalid pointer ! */
296 while (*p)
297 {
298 p++;
299 size++;
300 }
bartbedfd232009-03-26 19:07:15 +0000301 if (size > 0)
302 {
303 DRD_(trace_load)(a, size);
304 }
bart5e85d262008-03-01 10:49:37 +0000305}
306
sewardjaf44c822007-11-25 14:01:38 +0000307static void drd_post_mem_write(const CorePart part,
308 const ThreadId tid,
309 const Addr a,
310 const SizeT size)
311{
bartbedfd232009-03-26 19:07:15 +0000312 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
313 if (size > 0)
314 {
315 DRD_(trace_store)(a, size);
316 }
sewardjaf44c822007-11-25 14:01:38 +0000317}
318
bart08865622008-06-06 14:31:36 +0000319static __inline__
bartf9427fd2010-08-29 09:19:07 +0000320void drd_start_using_mem(const Addr a1, const SizeT len,
321 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000322{
bart639d0ad2011-03-12 14:26:01 +0000323 const Addr a2 = a1 + len;
324
325 tl_assert(a1 <= a2);
bart5e85d262008-03-01 10:49:37 +0000326
bartf9427fd2010-08-29 09:19:07 +0000327 if (!is_stack_mem && s_trace_alloc)
bartad994e82011-10-13 18:04:30 +0000328 DRD_(trace_msg)("Started using memory range 0x%lx + %ld%s",
bartb92ff0f2011-10-08 08:29:29 +0000329 a1, len, DRD_(running_thread_inside_pthread_create)()
330 ? " (inside pthread_create())" : "");
bartf9427fd2010-08-29 09:19:07 +0000331
bart7d5f5232011-03-13 09:08:10 +0000332 if (!is_stack_mem && DRD_(g_free_is_write))
bart639d0ad2011-03-12 14:26:01 +0000333 DRD_(thread_stop_using_mem)(a1, a2);
334
bartbedfd232009-03-26 19:07:15 +0000335 if (UNLIKELY(DRD_(any_address_is_traced)()))
336 {
bart42f32632011-12-13 11:12:05 +0000337 DRD_(trace_mem_access)(a1, len, eStart, 0, 0);
bartbedfd232009-03-26 19:07:15 +0000338 }
bart31b983d2010-02-21 14:52:59 +0000339
bart27656f82011-12-17 15:18:20 +0000340 if (UNLIKELY(DRD_(running_thread_inside_pthread_create)()))
bartdd75cdf2009-07-24 08:20:10 +0000341 {
bart639d0ad2011-03-12 14:26:01 +0000342 DRD_(start_suppression)(a1, a2, "pthread_create()");
bartdd75cdf2009-07-24 08:20:10 +0000343 }
sewardjaf44c822007-11-25 14:01:38 +0000344}
345
sewardj7cf4e6b2008-05-01 20:24:26 +0000346static void drd_start_using_mem_w_ecu(const Addr a1,
347 const SizeT len,
348 UInt ec_uniq)
349{
bartf9427fd2010-08-29 09:19:07 +0000350 drd_start_using_mem(a1, len, False);
sewardj7cf4e6b2008-05-01 20:24:26 +0000351}
352
353static void drd_start_using_mem_w_tid(const Addr a1,
354 const SizeT len,
355 ThreadId tid)
356{
bartf9427fd2010-08-29 09:19:07 +0000357 drd_start_using_mem(a1, len, False);
sewardj7cf4e6b2008-05-01 20:24:26 +0000358}
359
bart0ffa4832008-04-05 12:57:01 +0000360static __inline__
361void drd_stop_using_mem(const Addr a1, const SizeT len,
362 const Bool is_stack_mem)
sewardjaf44c822007-11-25 14:01:38 +0000363{
bartbedfd232009-03-26 19:07:15 +0000364 const Addr a2 = a1 + len;
bart5e85d262008-03-01 10:49:37 +0000365
barta3003982010-09-08 16:29:17 +0000366 tl_assert(a1 <= a2);
bart5e85d262008-03-01 10:49:37 +0000367
bartbedfd232009-03-26 19:07:15 +0000368 if (UNLIKELY(DRD_(any_address_is_traced)()))
bart42f32632011-12-13 11:12:05 +0000369 DRD_(trace_mem_access)(a1, len, eEnd, 0, 0);
bartf9427fd2010-08-29 09:19:07 +0000370
371 if (!is_stack_mem && s_trace_alloc)
bartad994e82011-10-13 18:04:30 +0000372 DRD_(trace_msg)("Stopped using memory range 0x%lx + %ld",
bartb92ff0f2011-10-08 08:29:29 +0000373 a1, len);
bartf9427fd2010-08-29 09:19:07 +0000374
bart07595032010-08-29 09:51:06 +0000375 if (!is_stack_mem || DRD_(get_check_stack_accesses)())
bartbedfd232009-03-26 19:07:15 +0000376 {
bart7d5f5232011-03-13 09:08:10 +0000377 if (is_stack_mem || !DRD_(g_free_is_write))
bart639d0ad2011-03-12 14:26:01 +0000378 DRD_(thread_stop_using_mem)(a1, a2);
bart7d5f5232011-03-13 09:08:10 +0000379 else if (DRD_(g_free_is_write))
bart639d0ad2011-03-12 14:26:01 +0000380 DRD_(trace_store)(a1, len);
bartbedfd232009-03-26 19:07:15 +0000381 DRD_(clientobj_stop_using_mem)(a1, a2);
382 DRD_(suppression_stop_using_mem)(a1, a2);
383 }
bart0ffa4832008-04-05 12:57:01 +0000384}
385
386static __inline__
387void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
388{
bartbedfd232009-03-26 19:07:15 +0000389 drd_stop_using_mem(a1, len, False);
sewardjaf44c822007-11-25 14:01:38 +0000390}
391
bart1335ecc2009-02-14 16:10:53 +0000392/**
bartfdaa0182009-03-10 09:25:32 +0000393 * Discard all information DRD has about memory accesses and client objects
394 * in the specified address range.
395 */
396void DRD_(clean_memory)(const Addr a1, const SizeT len)
397{
bartbedfd232009-03-26 19:07:15 +0000398 const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
399 drd_stop_using_mem(a1, len, is_stack_memory);
bartf9427fd2010-08-29 09:19:07 +0000400 drd_start_using_mem(a1, len, is_stack_memory);
bartfdaa0182009-03-10 09:25:32 +0000401}
402
403/**
bart68347832012-09-06 14:08:26 +0000404 * Suppress data race reports on all addresses contained in .plt, .got and
bart1335ecc2009-02-14 16:10:53 +0000405 * .got.plt sections inside the address range [ a, a + len [. The data in
406 * these sections is modified by _dl_relocate_object() every time a function
407 * in a shared library is called for the first time. Since the first call
408 * to a function in a shared library can happen from a multithreaded context,
409 * such calls can cause conflicting accesses. See also Ulrich Drepper's
410 * paper "How to Write Shared Libraries" for more information about relocation
411 * (http://people.redhat.com/drepper/dsohowto.pdf).
bart68347832012-09-06 14:08:26 +0000412 * Note: the contents of the .got section is only modified by the MIPS resolver.
bartcb2d0072008-05-31 07:55:51 +0000413 */
bart1335ecc2009-02-14 16:10:53 +0000414static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
bartcb2d0072008-05-31 07:55:51 +0000415{
bartbedfd232009-03-26 19:07:15 +0000416 const DebugInfo* di;
bartcb2d0072008-05-31 07:55:51 +0000417
barta9cc2dd2012-01-15 19:17:37 +0000418 if (trace_sectsuppr)
419 VG_(dmsg)("Evaluating range @ 0x%lx size %ld\n", a, len);
bartcb2d0072008-05-31 07:55:51 +0000420
barta9cc2dd2012-01-15 19:17:37 +0000421 for (di = VG_(next_DebugInfo)(0); di; di = VG_(next_DebugInfo)(di)) {
bartbedfd232009-03-26 19:07:15 +0000422 Addr avma;
423 SizeT size;
bartcb2d0072008-05-31 07:55:51 +0000424
barta9cc2dd2012-01-15 19:17:37 +0000425 if (trace_sectsuppr)
426 VG_(dmsg)("Examining %s / %s\n", VG_(DebugInfo_get_filename)(di),
427 VG_(DebugInfo_get_soname)(di));
428
sewardje3f1e592009-07-31 09:41:29 +0000429 avma = VG_(DebugInfo_get_plt_avma)(di);
430 size = VG_(DebugInfo_get_plt_size)(di);
bartbedfd232009-03-26 19:07:15 +0000431 tl_assert((avma && size) || (avma == 0 && size == 0));
barta9cc2dd2012-01-15 19:17:37 +0000432 if (size > 0) {
433 if (trace_sectsuppr)
434 VG_(dmsg)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
sewardje3f1e592009-07-31 09:41:29 +0000435 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
bartbedfd232009-03-26 19:07:15 +0000436 DRD_(start_suppression)(avma, avma + size, ".plt");
437 }
bartcb2d0072008-05-31 07:55:51 +0000438
sewardje3f1e592009-07-31 09:41:29 +0000439 avma = VG_(DebugInfo_get_gotplt_avma)(di);
440 size = VG_(DebugInfo_get_gotplt_size)(di);
bartbedfd232009-03-26 19:07:15 +0000441 tl_assert((avma && size) || (avma == 0 && size == 0));
barta9cc2dd2012-01-15 19:17:37 +0000442 if (size > 0) {
443 if (trace_sectsuppr)
444 VG_(dmsg)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
sewardje3f1e592009-07-31 09:41:29 +0000445 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
bartbedfd232009-03-26 19:07:15 +0000446 DRD_(start_suppression)(avma, avma + size, ".gotplt");
447 }
bart68347832012-09-06 14:08:26 +0000448
449 avma = VG_(DebugInfo_get_got_avma)(di);
450 size = VG_(DebugInfo_get_got_size)(di);
451 tl_assert((avma && size) || (avma == 0 && size == 0));
452 if (size > 0) {
453 if (trace_sectsuppr)
454 VG_(dmsg)("Suppressing .got @ 0x%lx size %ld\n", avma, size);
455 tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectGOT);
456 DRD_(start_suppression)(avma, avma + size, ".got");
457 }
bartbedfd232009-03-26 19:07:15 +0000458 }
bartcb2d0072008-05-31 07:55:51 +0000459}
460
bart5e85d262008-03-01 10:49:37 +0000461static
462void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
sewardj9c606bd2008-09-18 18:12:50 +0000463 const Bool rr, const Bool ww, const Bool xx,
464 ULong di_handle)
bart5e85d262008-03-01 10:49:37 +0000465{
bartbedfd232009-03-26 19:07:15 +0000466 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bartd5765912008-03-16 08:40:55 +0000467
bartf9427fd2010-08-29 09:19:07 +0000468 drd_start_using_mem(a, len, False);
bartcb2d0072008-05-31 07:55:51 +0000469
bartbedfd232009-03-26 19:07:15 +0000470 DRD_(suppress_relocation_conflicts)(a, len);
bart5e85d262008-03-01 10:49:37 +0000471}
472
bartad193e72012-01-15 19:02:20 +0000473/**
474 * Called by the core when the stack of a thread grows, to indicate that
475 * the addresses in range [ a, a + len [ may now be used by the client.
476 * Assumption: stacks grow downward.
477 */
bart08865622008-06-06 14:31:36 +0000478static __inline__
bart993d32f2012-01-15 19:08:13 +0000479void drd_start_using_mem_stack2(const DrdThreadId tid, const Addr a,
480 const SizeT len)
481{
482 DRD_(thread_set_stack_min)(tid, a - VG_STACK_REDZONE_SZB);
483 drd_start_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
484 True);
485}
486
487static __inline__
bart08865622008-06-06 14:31:36 +0000488void drd_start_using_mem_stack(const Addr a, const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000489{
bart993d32f2012-01-15 19:08:13 +0000490 drd_start_using_mem_stack2(DRD_(thread_get_running_tid)(), a, len);
sewardjaf44c822007-11-25 14:01:38 +0000491}
492
bartad193e72012-01-15 19:02:20 +0000493/**
494 * Called by the core when the stack of a thread shrinks, to indicate that
495 * the addresses [ a, a + len [ are no longer accessible for the client.
496 * Assumption: stacks grow downward.
497 */
bart08865622008-06-06 14:31:36 +0000498static __inline__
bart993d32f2012-01-15 19:08:13 +0000499void drd_stop_using_mem_stack2(const DrdThreadId tid, const Addr a,
500 const SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000501{
bart993d32f2012-01-15 19:08:13 +0000502 DRD_(thread_set_stack_min)(tid, a + len - VG_STACK_REDZONE_SZB);
bartbedfd232009-03-26 19:07:15 +0000503 drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
504 True);
sewardjaf44c822007-11-25 14:01:38 +0000505}
506
bart993d32f2012-01-15 19:08:13 +0000507static __inline__
508void drd_stop_using_mem_stack(const Addr a, const SizeT len)
509{
510 drd_stop_using_mem_stack2(DRD_(thread_get_running_tid)(), a, len);
511}
512
bart68a8afa2010-09-02 14:50:41 +0000513static
514Bool on_alt_stack(const Addr a)
515{
516 ThreadId vg_tid;
517 Addr alt_min;
518 SizeT alt_size;
519
520 vg_tid = VG_(get_running_tid)();
521 alt_min = VG_(thread_get_altstack_min)(vg_tid);
522 alt_size = VG_(thread_get_altstack_size)(vg_tid);
523 return (SizeT)(a - alt_min) < alt_size;
524}
525
526static
527void drd_start_using_mem_alt_stack(const Addr a, const SizeT len)
528{
529 if (!on_alt_stack(a))
530 drd_start_using_mem_stack(a, len);
531}
532
533static
534void drd_stop_using_mem_alt_stack(const Addr a, const SizeT len)
535{
536 if (!on_alt_stack(a))
537 drd_stop_using_mem_stack(a, len);
538}
539
540/**
541 * Callback function invoked by the Valgrind core before a signal is delivered.
542 */
543static
544void drd_pre_deliver_signal(const ThreadId vg_tid, const Int sigNo,
545 const Bool alt_stack)
546{
547 DrdThreadId drd_tid;
548
549 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
550 DRD_(thread_set_on_alt_stack)(drd_tid, alt_stack);
551 if (alt_stack)
552 {
553 /*
554 * As soon a signal handler has been invoked on the alternate stack,
555 * switch to stack memory handling functions that can handle the
556 * alternate stack.
557 */
558 VG_(track_new_mem_stack)(drd_start_using_mem_alt_stack);
559 VG_(track_die_mem_stack)(drd_stop_using_mem_alt_stack);
560 }
561}
562
563/**
564 * Callback function invoked by the Valgrind core after a signal is delivered,
565 * at least if the signal handler did not longjmp().
566 */
567static
568void drd_post_deliver_signal(const ThreadId vg_tid, const Int sigNo)
569{
570 DrdThreadId drd_tid;
571
572 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
573 DRD_(thread_set_on_alt_stack)(drd_tid, False);
574 if (DRD_(thread_get_threads_on_alt_stack)() == 0)
575 {
576 VG_(track_new_mem_stack)(drd_start_using_mem_stack);
577 VG_(track_die_mem_stack)(drd_stop_using_mem_stack);
578 }
579}
580
bartbedfd232009-03-26 19:07:15 +0000581/**
582 * Callback function called by the Valgrind core before a stack area is
583 * being used by a signal handler.
584 *
bart573d0742013-03-10 11:02:32 +0000585 * @param[in] a Start of address range - VG_STACK_REDZONE_SZB.
586 * @param[in] len Address range length + VG_STACK_REDZONE_SZB.
bartbedfd232009-03-26 19:07:15 +0000587 * @param[in] tid Valgrind thread ID for whom the signal frame is being
588 * constructed.
589 */
590static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
591 ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000592{
bartbedfd232009-03-26 19:07:15 +0000593 DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
bart573d0742013-03-10 11:02:32 +0000594 drd_start_using_mem(a + VG_STACK_REDZONE_SZB, len - VG_STACK_REDZONE_SZB,
595 True);
sewardjaf44c822007-11-25 14:01:38 +0000596}
597
bart5e85d262008-03-01 10:49:37 +0000598static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
sewardjaf44c822007-11-25 14:01:38 +0000599{
bart573d0742013-03-10 11:02:32 +0000600 drd_stop_using_mem(a + VG_STACK_REDZONE_SZB, len - VG_STACK_REDZONE_SZB,
601 True);
sewardjaf44c822007-11-25 14:01:38 +0000602}
603
604static
605void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
606{
bartbedfd232009-03-26 19:07:15 +0000607 const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
608 tl_assert(created != VG_INVALID_THREADID);
609 DRD_(thread_pre_create)(drd_creator, created);
610 if (DRD_(IsValidDrdThreadId)(drd_creator))
611 {
612 DRD_(thread_new_segment)(drd_creator);
613 }
614 if (DRD_(thread_get_trace_fork_join)())
615 {
bartad994e82011-10-13 18:04:30 +0000616 DRD_(trace_msg)("drd_pre_thread_create creator = %d, created = %d",
barte7086002011-10-11 19:08:39 +0000617 drd_creator, created);
bartbedfd232009-03-26 19:07:15 +0000618 }
sewardjaf44c822007-11-25 14:01:38 +0000619}
620
bartad193e72012-01-15 19:02:20 +0000621/**
622 * Called by Valgrind's core before any loads or stores are performed on
623 * the context of thread "created".
624 */
sewardjaf44c822007-11-25 14:01:38 +0000625static
bart0ffa4832008-04-05 12:57:01 +0000626void drd_post_thread_create(const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000627{
bartbedfd232009-03-26 19:07:15 +0000628 DrdThreadId drd_created;
bart864920d2012-01-15 19:13:35 +0000629 Addr stack_max;
bart0ffa4832008-04-05 12:57:01 +0000630
bartbedfd232009-03-26 19:07:15 +0000631 tl_assert(vg_created != VG_INVALID_THREADID);
bart0ffa4832008-04-05 12:57:01 +0000632
bartbedfd232009-03-26 19:07:15 +0000633 drd_created = DRD_(thread_post_create)(vg_created);
bart864920d2012-01-15 19:13:35 +0000634
635 /* Set up red zone before the code in glibc's clone.S is run. */
636 stack_max = DRD_(thread_get_stack_max)(drd_created);
637 drd_start_using_mem_stack2(drd_created, stack_max, 0);
638
bartbedfd232009-03-26 19:07:15 +0000639 if (DRD_(thread_get_trace_fork_join)())
640 {
bartad994e82011-10-13 18:04:30 +0000641 DRD_(trace_msg)("drd_post_thread_create created = %d", drd_created);
bartbedfd232009-03-26 19:07:15 +0000642 }
643 if (! DRD_(get_check_stack_accesses)())
644 {
645 DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
646 - DRD_(thread_get_stack_size)(drd_created),
647 DRD_(thread_get_stack_max)(drd_created),
648 "stack");
649 }
sewardjaf44c822007-11-25 14:01:38 +0000650}
651
sewardjaf44c822007-11-25 14:01:38 +0000652/* Called after a thread has performed its last memory access. */
bartd43f8d32008-03-16 17:29:20 +0000653static void drd_thread_finished(ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000654{
bartbedfd232009-03-26 19:07:15 +0000655 DrdThreadId drd_tid;
sewardj85642922008-01-14 11:54:56 +0000656
bart9bb46c42012-02-02 10:14:30 +0000657 /*
bart37e1dd02012-02-02 10:58:01 +0000658 * Ignore if invoked because thread creation failed. See e.g.
bart9bb46c42012-02-02 10:14:30 +0000659 * coregrind/m_syswrap/syswrap-amd64-linux.c
660 */
bart37e1dd02012-02-02 10:58:01 +0000661 if (VG_(get_running_tid)() != vg_tid)
662 return;
sewardj85642922008-01-14 11:54:56 +0000663
bartbedfd232009-03-26 19:07:15 +0000664 drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
bart37e1dd02012-02-02 10:58:01 +0000665 tl_assert(drd_tid != DRD_INVALID_THREADID);
bartbedfd232009-03-26 19:07:15 +0000666 if (DRD_(thread_get_trace_fork_join)())
667 {
bartad994e82011-10-13 18:04:30 +0000668 DRD_(trace_msg)("drd_thread_finished tid = %d%s", drd_tid,
barte7086002011-10-11 19:08:39 +0000669 DRD_(thread_get_joinable)(drd_tid)
670 ? "" : " (which is a detached thread)");
bartbedfd232009-03-26 19:07:15 +0000671 }
barte7086002011-10-11 19:08:39 +0000672 if (s_show_stack_usage && !VG_(clo_xml)) {
bartbedfd232009-03-26 19:07:15 +0000673 const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
674 const SizeT used_stack
675 = (DRD_(thread_get_stack_max)(drd_tid)
676 - DRD_(thread_get_stack_min_min)(drd_tid));
677 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000678 "thread %d%s finished and used %ld bytes out of %ld"
sewardj1e29ebc2009-07-15 14:49:17 +0000679 " on its stack. Margin: %ld bytes.\n",
bartbedfd232009-03-26 19:07:15 +0000680 drd_tid,
681 DRD_(thread_get_joinable)(drd_tid)
barte7086002011-10-11 19:08:39 +0000682 ? "" : " (which is a detached thread)",
683 used_stack, stack_size, stack_size - used_stack);
sewardjaf44c822007-11-25 14:01:38 +0000684
bartbedfd232009-03-26 19:07:15 +0000685 }
686 drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
687 DRD_(thread_get_stack_max)(drd_tid)
688 - DRD_(thread_get_stack_min)(drd_tid),
689 True);
bartd45d9952009-05-31 18:53:54 +0000690 DRD_(thread_set_record_loads)(drd_tid, False);
691 DRD_(thread_set_record_stores)(drd_tid, False);
bartbedfd232009-03-26 19:07:15 +0000692 DRD_(thread_finished)(drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000693}
694
bart5c7e6b62011-02-03 17:47:50 +0000695/*
696 * Called immediately after fork for the child process only. 'tid' is the
697 * only surviving thread in the child process. Cleans up thread state.
698 * See also http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html for a detailed discussion of using fork() in combination with mutexes.
699 */
700static
701void drd__atfork_child(ThreadId tid)
702{
703 DRD_(drd_thread_atfork_child)(tid);
704}
705
706
sewardjaf44c822007-11-25 14:01:38 +0000707//
708// Implementation of the tool interface.
709//
710
bart1335ecc2009-02-14 16:10:53 +0000711static void DRD_(post_clo_init)(void)
sewardjdcbb8d32007-11-26 21:34:30 +0000712{
bart0c6bf352009-07-25 14:03:53 +0000713#if defined(VGO_linux) || defined(VGO_darwin)
bartc6ef4af2009-07-23 10:10:30 +0000714 /* fine */
bartc6ef4af2009-07-23 10:10:30 +0000715#else
716 VG_(printf)("\nWARNING: DRD has not yet been tested on this operating system.\n\n");
sewardjdcbb8d32007-11-26 21:34:30 +0000717# endif
bart95761b52008-03-29 08:34:03 +0000718
bart75c1cba2010-08-29 07:20:30 +0000719 if (s_var_info)
bartbedfd232009-03-26 19:07:15 +0000720 {
721 VG_(needs_var_info)();
722 }
sewardjdcbb8d32007-11-26 21:34:30 +0000723}
sewardjaf44c822007-11-25 14:01:38 +0000724
sewardjaf44c822007-11-25 14:01:38 +0000725static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
726{
bartbedfd232009-03-26 19:07:15 +0000727 tl_assert(tid == VG_(get_running_tid)());
728 DRD_(thread_set_vg_running_tid)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000729}
730
bart1335ecc2009-02-14 16:10:53 +0000731static void DRD_(fini)(Int exitcode)
sewardjaf44c822007-11-25 14:01:38 +0000732{
bartbedfd232009-03-26 19:07:15 +0000733 // thread_print_all();
sewardj2d9e8742009-08-07 15:46:56 +0000734 if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) {
barte7086002011-10-11 19:08:39 +0000735 VG_(message)(Vg_UserMsg, "For counts of detected and suppressed errors, "
sewardj2d9e8742009-08-07 15:46:56 +0000736 "rerun with: -v\n");
737 }
738
barte7086002011-10-11 19:08:39 +0000739 if ((VG_(clo_stats) || s_print_stats) && !VG_(clo_xml))
bartbedfd232009-03-26 19:07:15 +0000740 {
barte5214662009-06-21 11:51:23 +0000741 ULong pu = DRD_(thread_get_update_conflict_set_count)();
742 ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)();
743 ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)();
744 ULong pu_join = DRD_(thread_get_update_conflict_set_join_count)();
745
bartbedfd232009-03-26 19:07:15 +0000746 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000747 " thread: %lld context switches.\n",
bart54803fe2009-06-21 09:26:27 +0000748 DRD_(thread_get_context_switch_count)());
bartbedfd232009-03-26 19:07:15 +0000749 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000750 "confl set: %lld full updates and %lld partial updates;\n",
barte7086002011-10-11 19:08:39 +0000751 DRD_(thread_get_compute_conflict_set_count)(),
752 pu);
bartbedfd232009-03-26 19:07:15 +0000753 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000754 " %lld partial updates during segment creation,\n",
barte7086002011-10-11 19:08:39 +0000755 pu_seg_cr);
barte5214662009-06-21 11:51:23 +0000756 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000757 " %lld because of mutex/sema/cond.var. operations,\n",
barte7086002011-10-11 19:08:39 +0000758 pu_mtx_cv);
barte5214662009-06-21 11:51:23 +0000759 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000760 " %lld because of barrier/rwlock operations and\n",
barte5214662009-06-21 11:51:23 +0000761 pu - pu_seg_cr - pu_mtx_cv - pu_join);
762 VG_(message)(Vg_UserMsg,
763 " %lld partial updates because of thread join"
barte7086002011-10-11 19:08:39 +0000764 " operations.\n",
765 pu_join);
barte5214662009-06-21 11:51:23 +0000766 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000767 " segments: created %lld segments, max %lld alive,\n",
bart9dedafe2009-06-21 18:09:02 +0000768 DRD_(sg_get_segments_created_count)(),
769 DRD_(sg_get_max_segments_alive_count)());
bartbedfd232009-03-26 19:07:15 +0000770 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000771 " %lld discard points and %lld merges.\n",
bart9dedafe2009-06-21 18:09:02 +0000772 DRD_(thread_get_discard_ordered_segments_count)(),
bart1a3b0b32009-05-03 17:07:34 +0000773 DRD_(sg_get_segment_merge_count)());
774 VG_(message)(Vg_UserMsg,
bart54803fe2009-06-21 09:26:27 +0000775 "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and"
sewardj1e29ebc2009-07-15 14:49:17 +0000776 " %lld barrier.\n",
bartbedfd232009-03-26 19:07:15 +0000777 DRD_(get_mutex_segment_creation_count)(),
778 DRD_(get_rwlock_segment_creation_count)(),
779 DRD_(get_semaphore_segment_creation_count)(),
780 DRD_(get_barrier_segment_creation_count)());
781 VG_(message)(Vg_UserMsg,
bart9dedafe2009-06-21 18:09:02 +0000782 " bitmaps: %lld level one"
sewardj1e29ebc2009-07-15 14:49:17 +0000783 " and %lld level two bitmaps were allocated.\n",
bartbedfd232009-03-26 19:07:15 +0000784 DRD_(bm_get_bitmap_creation_count)(),
bartbedfd232009-03-26 19:07:15 +0000785 DRD_(bm_get_bitmap2_creation_count)());
786 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000787 " mutex: %lld non-recursive lock/unlock events.\n",
bartbedfd232009-03-26 19:07:15 +0000788 DRD_(get_mutex_lock_count)());
789 DRD_(print_malloc_stats)();
790 }
barte44bccc2012-01-18 09:46:57 +0000791
792 DRD_(bm_module_cleanup)();
sewardjaf44c822007-11-25 14:01:38 +0000793}
794
sewardjaf44c822007-11-25 14:01:38 +0000795static
796void drd_pre_clo_init(void)
797{
njnf76d27a2009-05-28 01:53:07 +0000798 // Basic tool stuff.
bartbedfd232009-03-26 19:07:15 +0000799 VG_(details_name) ("drd");
800 VG_(details_version) (NULL);
801 VG_(details_description) ("a thread error detector");
sewardj03f8d3f2012-08-05 15:46:46 +0000802 VG_(details_copyright_author)("Copyright (C) 2006-2012, and GNU GPL'd,"
bartbedfd232009-03-26 19:07:15 +0000803 " by Bart Van Assche.");
804 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardjaf44c822007-11-25 14:01:38 +0000805
bartbedfd232009-03-26 19:07:15 +0000806 VG_(basic_tool_funcs) (DRD_(post_clo_init),
807 DRD_(instrument),
808 DRD_(fini));
sewardjaf44c822007-11-25 14:01:38 +0000809
bartbedfd232009-03-26 19:07:15 +0000810 // Command line stuff.
811 VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
812 DRD_(print_usage),
813 DRD_(print_debug_usage));
barte7086002011-10-11 19:08:39 +0000814 VG_(needs_xml_output) ();
sewardjaf44c822007-11-25 14:01:38 +0000815
bartbedfd232009-03-26 19:07:15 +0000816 // Error handling.
817 DRD_(register_error_handlers)();
sewardjaf44c822007-11-25 14:01:38 +0000818
bartbedfd232009-03-26 19:07:15 +0000819 // Core event tracking.
820 VG_(track_pre_mem_read) (drd_pre_mem_read);
821 VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz);
822 VG_(track_post_mem_write) (drd_post_mem_write);
823 VG_(track_new_mem_brk) (drd_start_using_mem_w_tid);
824 VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms);
825 VG_(track_new_mem_stack) (drd_start_using_mem_stack);
826 VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
827 VG_(track_new_mem_startup) (drd_start_using_mem_w_perms);
828 VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem);
829 VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem);
830 VG_(track_die_mem_stack) (drd_stop_using_mem_stack);
831 VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
bart68a8afa2010-09-02 14:50:41 +0000832 VG_(track_pre_deliver_signal) (drd_pre_deliver_signal);
833 VG_(track_post_deliver_signal) (drd_post_deliver_signal);
bartbedfd232009-03-26 19:07:15 +0000834 VG_(track_start_client_code) (drd_start_client_code);
835 VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
836 VG_(track_pre_thread_first_insn)(drd_post_thread_create);
837 VG_(track_pre_thread_ll_exit) (drd_thread_finished);
bart5c7e6b62011-02-03 17:47:50 +0000838 VG_(atfork) (NULL/*pre*/, NULL/*parent*/,
839 drd__atfork_child/*child*/);
sewardjaf44c822007-11-25 14:01:38 +0000840
bartbedfd232009-03-26 19:07:15 +0000841 // Other stuff.
842 DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
843 drd_stop_using_nonstack_mem);
sewardjaf44c822007-11-25 14:01:38 +0000844
barte44bccc2012-01-18 09:46:57 +0000845 DRD_(bm_module_init)();
846
bartbedfd232009-03-26 19:07:15 +0000847 DRD_(clientreq_init)();
sewardjaf44c822007-11-25 14:01:38 +0000848
bartbedfd232009-03-26 19:07:15 +0000849 DRD_(suppression_init)();
bart4bb53d82008-02-28 19:06:34 +0000850
bartbedfd232009-03-26 19:07:15 +0000851 DRD_(clientobj_init)();
barte5e0efa2009-06-24 18:35:02 +0000852
barte278ab52012-01-24 18:28:55 +0000853 DRD_(thread_init)();
854
barte5e0efa2009-06-24 18:35:02 +0000855 {
florian19f91bb2012-11-10 22:29:54 +0000856 HChar* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL");
barte5e0efa2009-06-24 18:35:02 +0000857 if (smi)
858 DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL));
859 }
sewardjaf44c822007-11-25 14:01:38 +0000860}
861
862
863VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)