bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
bart | 876cafd | 2010-10-10 18:07:31 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2010 Bart Van Assche <bvanassche@acm.org>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 6 | |
| 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 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 26 | #include "drd_barrier.h" |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 27 | #include "drd_clientobj.h" |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 28 | #include "drd_clientreq.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 29 | #include "drd_cond.h" |
| 30 | #include "drd_error.h" |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 31 | #include "drd_load_store.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 32 | #include "drd_malloc_wrappers.h" |
| 33 | #include "drd_mutex.h" |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 34 | #include "drd_rwlock.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 35 | #include "drd_segment.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 36 | #include "drd_semaphore.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | #include "drd_suppression.h" |
| 38 | #include "drd_thread.h" |
bart | eacd916 | 2008-06-16 20:22:18 +0000 | [diff] [blame] | 39 | #include "libvex_guest_offsets.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 40 | #include "pub_drd_bitmap.h" |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 41 | #include "pub_tool_vki.h" // Must be included before pub_tool_libcproc |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 42 | #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" |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 49 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 50 | #include "pub_tool_options.h" // command line options |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 51 | #include "pub_tool_replacemalloc.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 52 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 53 | #include "pub_tool_tooliface.h" |
sewardj | 234e558 | 2011-02-09 12:47:23 +0000 | [diff] [blame^] | 54 | #include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | |
| 56 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 57 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 58 | |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 59 | static Bool s_print_stats = False; |
| 60 | static Bool s_var_info = False; |
| 61 | static Bool s_show_stack_usage = False; |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 62 | static Bool s_trace_alloc = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 63 | |
| 64 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Implement the needs_command_line_options for drd. |
| 67 | */ |
| 68 | static Bool DRD_(process_cmd_line_option)(Char* arg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | int check_stack_accesses = -1; |
| 71 | int exclusive_threshold_ms = -1; |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 72 | int first_race_only = -1; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 73 | int report_signal_unlocked = -1; |
| 74 | int segment_merging = -1; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 75 | int segment_merge_interval = -1; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 76 | 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; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 84 | int trace_conflict_set_bm = -1; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 85 | 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; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 91 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 92 | if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {} |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 93 | else if VG_BOOL_CLO(arg, "--drd-stats", s_print_stats) {} |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 94 | else if VG_BOOL_CLO(arg, "--first-race-only", first_race_only) {} |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 95 | else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked) |
| 96 | {} |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 97 | else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {} |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 98 | else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval) |
| 99 | {} |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 100 | else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {} |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 101 | else if VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) {} |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 102 | else if VG_BOOL_CLO(arg, "--trace-alloc", s_trace_alloc) {} |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 103 | else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {} |
| 104 | else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {} |
| 105 | else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {} |
| 106 | else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {} |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 107 | else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){} |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 108 | else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {} |
| 109 | else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {} |
| 110 | else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {} |
| 111 | else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {} |
| 112 | else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {} |
| 113 | else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {} |
| 114 | else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {} |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 115 | else if VG_BOOL_CLO(arg, "--var-info", s_var_info) {} |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 116 | else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {} |
| 117 | else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {} |
| 118 | else if VG_STR_CLO (arg, "--trace-addr", trace_address) {} |
| 119 | else |
| 120 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 121 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 122 | if (check_stack_accesses != -1) |
| 123 | DRD_(set_check_stack_accesses)(check_stack_accesses); |
| 124 | if (exclusive_threshold_ms != -1) |
| 125 | { |
| 126 | DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms); |
| 127 | DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms); |
| 128 | } |
bart | f98a569 | 2009-05-03 17:17:37 +0000 | [diff] [blame] | 129 | if (first_race_only != -1) |
| 130 | { |
| 131 | DRD_(set_first_race_only)(first_race_only); |
| 132 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 133 | if (report_signal_unlocked != -1) |
| 134 | { |
| 135 | DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked); |
| 136 | } |
| 137 | if (shared_threshold_ms != -1) |
| 138 | { |
| 139 | DRD_(rwlock_set_shared_threshold)(shared_threshold_ms); |
| 140 | } |
| 141 | if (segment_merging != -1) |
| 142 | DRD_(thread_set_segment_merging)(segment_merging); |
bart | 109d5da | 2009-06-22 18:06:29 +0000 | [diff] [blame] | 143 | if (segment_merge_interval != -1) |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 144 | DRD_(thread_set_segment_merge_interval)(segment_merge_interval); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 145 | if (show_confl_seg != -1) |
| 146 | DRD_(set_show_conflicting_segments)(show_confl_seg); |
| 147 | if (trace_address) |
| 148 | { |
| 149 | const Addr addr = VG_(strtoll16)(trace_address, 0); |
| 150 | DRD_(start_tracing_address_range)(addr, addr + 1); |
| 151 | } |
| 152 | if (trace_barrier != -1) |
| 153 | DRD_(barrier_set_trace)(trace_barrier); |
| 154 | if (trace_clientobj != -1) |
| 155 | DRD_(clientobj_set_trace)(trace_clientobj); |
| 156 | if (trace_cond != -1) |
| 157 | DRD_(cond_set_trace)(trace_cond); |
| 158 | if (trace_csw != -1) |
| 159 | DRD_(thread_trace_context_switches)(trace_csw); |
| 160 | if (trace_fork_join != -1) |
| 161 | DRD_(thread_set_trace_fork_join)(trace_fork_join); |
| 162 | if (trace_conflict_set != -1) |
| 163 | DRD_(thread_trace_conflict_set)(trace_conflict_set); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 164 | if (trace_conflict_set_bm != -1) |
| 165 | DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 166 | if (trace_mutex != -1) |
| 167 | DRD_(mutex_set_trace)(trace_mutex); |
| 168 | if (trace_rwlock != -1) |
| 169 | DRD_(rwlock_set_trace)(trace_rwlock); |
| 170 | if (trace_segment != -1) |
| 171 | DRD_(sg_set_trace)(trace_segment); |
| 172 | if (trace_semaphore != -1) |
| 173 | DRD_(semaphore_set_trace)(trace_semaphore); |
| 174 | if (trace_suppression != -1) |
| 175 | DRD_(suppression_set_trace)(trace_suppression); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 176 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 177 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 180 | static void DRD_(print_usage)(void) |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 181 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 182 | VG_(printf)( |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 183 | " --check-stack-var=yes|no Whether or not to report data races on\n" |
| 184 | " stack variables [no].\n" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 185 | " --exclusive-threshold=<n> Print an error message if any mutex or\n" |
bart | 8600c42 | 2010-10-25 18:18:54 +0000 | [diff] [blame] | 186 | " writer lock is held longer than the specified\n" |
| 187 | " time (in milliseconds) [off].\n" |
bart | 1a3b0b3 | 2009-05-03 17:07:34 +0000 | [diff] [blame] | 188 | " --first-race-only=yes|no Only report the first data race that occurs on\n" |
| 189 | " a memory location instead of all races [no].\n" |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 190 | " --report-signal-unlocked=yes|no Whether to report calls to\n" |
| 191 | " pthread_cond_signal() where the mutex associated\n" |
| 192 | " with the signal via pthread_cond_wait() is not\n" |
| 193 | " locked at the time the signal is sent [yes].\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 194 | " --segment-merging=yes|no Controls segment merging [yes].\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 195 | " Segment merging is an algorithm to limit memory usage of the\n" |
| 196 | " data race detection algorithm. Disabling segment merging may\n" |
| 197 | " improve the accuracy of the so-called 'other segments' displayed\n" |
| 198 | " in race reports but can also trigger an out of memory error.\n" |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 199 | " --segment-merging-interval=<n> Perform segment merging every time n new\n" |
| 200 | " segments have been created. Default: %d.\n" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 201 | " --shared-threshold=<n> Print an error message if a reader lock\n" |
bart | 8600c42 | 2010-10-25 18:18:54 +0000 | [diff] [blame] | 202 | " is held longer than the specified time (in\n" |
| 203 | " milliseconds) [off]\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 204 | " --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n" |
| 205 | " --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 206 | "\n" |
bart | ef1b972 | 2008-07-04 15:34:23 +0000 | [diff] [blame] | 207 | " drd options for monitoring process behavior:\n" |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 208 | " --trace-addr=<address> Trace all load and store activity for the.\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 209 | " specified address [off].\n" |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 210 | " --trace-alloc=yes|no Trace all memory allocations and deallocations\n"" [no].\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 211 | " --trace-barrier=yes|no Trace all barrier activity [no].\n" |
| 212 | " --trace-cond=yes|no Trace all condition variable activity [no].\n" |
| 213 | " --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n" |
| 214 | " --trace-mutex=yes|no Trace all mutex activity [no].\n" |
| 215 | " --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n" |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 216 | " --trace-semaphore=yes|no Trace all semaphore activity [no].\n", |
| 217 | DRD_(thread_get_segment_merge_interval)() |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 218 | ); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 219 | } |
| 220 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 221 | static void DRD_(print_debug_usage)(void) |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 222 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 223 | VG_(printf)( |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 224 | " --drd-stats=yes|no Print statistics about DRD activity [no].\n" |
| 225 | " --trace-clientobj=yes|no Trace all client object activity [no].\n" |
| 226 | " --trace-csw=yes|no Trace all scheduler context switches [no].\n" |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 227 | " --trace-conflict-set=yes|no Trace all conflict set updates [no].\n" |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 228 | " --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n" |
| 229 | " updates [no]. Note: enabling this option\n" |
| 230 | " will generate a lot of output !\n" |
bart | 987781d | 2008-06-27 15:00:07 +0000 | [diff] [blame] | 231 | " --trace-segment=yes|no Trace segment actions [no].\n" |
| 232 | " --trace-suppr=yes|no Trace all address suppression actions [no].\n" |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 233 | ); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | |
| 237 | // |
| 238 | // Implements the thread-related core callbacks. |
| 239 | // |
| 240 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 241 | static void drd_pre_mem_read(const CorePart part, |
| 242 | const ThreadId tid, |
| 243 | Char* const s, |
| 244 | const Addr a, |
| 245 | const SizeT size) |
| 246 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 247 | if (size > 0) |
| 248 | { |
| 249 | DRD_(trace_load)(a, size); |
| 250 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 253 | static void drd_pre_mem_read_asciiz(const CorePart part, |
| 254 | const ThreadId tid, |
| 255 | Char* const s, |
| 256 | const Addr a) |
| 257 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 258 | const char* p = (void*)a; |
| 259 | SizeT size = 0; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 260 | |
sewardj | 234e558 | 2011-02-09 12:47:23 +0000 | [diff] [blame^] | 261 | // Don't segfault if the string starts in an obviously stupid |
| 262 | // place. Actually we should check the whole string, not just |
| 263 | // the start address, but that's too much trouble. At least |
| 264 | // checking the first byte is better than nothing. See #255009. |
| 265 | if (!VG_(am_is_valid_for_client) (a, 1, VKI_PROT_READ)) |
| 266 | return; |
| 267 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 268 | /* Note: the expression '*p' reads client memory and may crash if the */ |
| 269 | /* client provided an invalid pointer ! */ |
| 270 | while (*p) |
| 271 | { |
| 272 | p++; |
| 273 | size++; |
| 274 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 275 | if (size > 0) |
| 276 | { |
| 277 | DRD_(trace_load)(a, size); |
| 278 | } |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 279 | } |
| 280 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 281 | static void drd_post_mem_write(const CorePart part, |
| 282 | const ThreadId tid, |
| 283 | const Addr a, |
| 284 | const SizeT size) |
| 285 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 286 | DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)()); |
| 287 | if (size > 0) |
| 288 | { |
| 289 | DRD_(trace_store)(a, size); |
| 290 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 291 | } |
| 292 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 293 | static __inline__ |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 294 | void drd_start_using_mem(const Addr a1, const SizeT len, |
| 295 | const Bool is_stack_mem) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 296 | { |
bart | a300398 | 2010-09-08 16:29:17 +0000 | [diff] [blame] | 297 | tl_assert(a1 <= a1 + len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 298 | |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 299 | if (!is_stack_mem && s_trace_alloc) |
| 300 | VG_(message)(Vg_UserMsg, "Started using memory range 0x%lx + %ld%s\n", |
| 301 | a1, len, DRD_(running_thread_inside_pthread_create)() |
| 302 | ? " (inside pthread_create())" : ""); |
| 303 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 304 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
| 305 | { |
| 306 | DRD_(trace_mem_access)(a1, len, eStart); |
| 307 | } |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 308 | |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 309 | if (UNLIKELY(DRD_(running_thread_inside_pthread_create)())) |
| 310 | { |
| 311 | DRD_(start_suppression)(a1, a1 + len, "pthread_create()"); |
| 312 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 313 | } |
| 314 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 315 | static void drd_start_using_mem_w_ecu(const Addr a1, |
| 316 | const SizeT len, |
| 317 | UInt ec_uniq) |
| 318 | { |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 319 | drd_start_using_mem(a1, len, False); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void drd_start_using_mem_w_tid(const Addr a1, |
| 323 | const SizeT len, |
| 324 | ThreadId tid) |
| 325 | { |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 326 | drd_start_using_mem(a1, len, False); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 327 | } |
| 328 | |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 329 | static __inline__ |
| 330 | void drd_stop_using_mem(const Addr a1, const SizeT len, |
| 331 | const Bool is_stack_mem) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 332 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 333 | const Addr a2 = a1 + len; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 334 | |
bart | a300398 | 2010-09-08 16:29:17 +0000 | [diff] [blame] | 335 | tl_assert(a1 <= a2); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 336 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 337 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 338 | DRD_(trace_mem_access)(a1, len, eEnd); |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 339 | |
| 340 | if (!is_stack_mem && s_trace_alloc) |
| 341 | VG_(message)(Vg_UserMsg, "Stopped using memory range 0x%lx + %ld\n", |
| 342 | a1, len); |
| 343 | |
bart | 0759503 | 2010-08-29 09:51:06 +0000 | [diff] [blame] | 344 | if (!is_stack_mem || DRD_(get_check_stack_accesses)()) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 345 | { |
bart | 8e0d2c1 | 2011-02-04 19:07:11 +0000 | [diff] [blame] | 346 | DRD_(thread_stop_using_mem)(a1, a2, False); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 347 | DRD_(clientobj_stop_using_mem)(a1, a2); |
| 348 | DRD_(suppression_stop_using_mem)(a1, a2); |
| 349 | } |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static __inline__ |
| 353 | void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len) |
| 354 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 355 | drd_stop_using_mem(a1, len, False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 356 | } |
| 357 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 358 | /** |
bart | fdaa018 | 2009-03-10 09:25:32 +0000 | [diff] [blame] | 359 | * Discard all information DRD has about memory accesses and client objects |
| 360 | * in the specified address range. |
| 361 | */ |
| 362 | void DRD_(clean_memory)(const Addr a1, const SizeT len) |
| 363 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 364 | const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1); |
| 365 | drd_stop_using_mem(a1, len, is_stack_memory); |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 366 | drd_start_using_mem(a1, len, is_stack_memory); |
bart | fdaa018 | 2009-03-10 09:25:32 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 370 | * Suppress data race reports on all addresses contained in .plt and |
| 371 | * .got.plt sections inside the address range [ a, a + len [. The data in |
| 372 | * these sections is modified by _dl_relocate_object() every time a function |
| 373 | * in a shared library is called for the first time. Since the first call |
| 374 | * to a function in a shared library can happen from a multithreaded context, |
| 375 | * such calls can cause conflicting accesses. See also Ulrich Drepper's |
| 376 | * paper "How to Write Shared Libraries" for more information about relocation |
| 377 | * (http://people.redhat.com/drepper/dsohowto.pdf). |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 378 | */ |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 379 | static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len) |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 380 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 381 | const DebugInfo* di; |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 382 | |
| 383 | #if 0 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 384 | VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 385 | #endif |
| 386 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 387 | for (di = VG_(next_DebugInfo)(0); di; di = VG_(next_DebugInfo)(di)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 388 | { |
| 389 | Addr avma; |
| 390 | SizeT size; |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 391 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 392 | avma = VG_(DebugInfo_get_plt_avma)(di); |
| 393 | size = VG_(DebugInfo_get_plt_size)(di); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 394 | tl_assert((avma && size) || (avma == 0 && size == 0)); |
| 395 | if (size > 0) |
| 396 | { |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 397 | #if 0 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 398 | VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 399 | #endif |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 400 | tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 401 | DRD_(start_suppression)(avma, avma + size, ".plt"); |
| 402 | } |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 403 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 404 | avma = VG_(DebugInfo_get_gotplt_avma)(di); |
| 405 | size = VG_(DebugInfo_get_gotplt_size)(di); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 406 | tl_assert((avma && size) || (avma == 0 && size == 0)); |
| 407 | if (size > 0) |
| 408 | { |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 409 | #if 0 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 410 | VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 411 | #endif |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 412 | tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 413 | DRD_(start_suppression)(avma, avma + size, ".gotplt"); |
| 414 | } |
| 415 | } |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 416 | } |
| 417 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 418 | static |
| 419 | void drd_start_using_mem_w_perms(const Addr a, const SizeT len, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 420 | const Bool rr, const Bool ww, const Bool xx, |
| 421 | ULong di_handle) |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 422 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 423 | DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)()); |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 424 | |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 425 | drd_start_using_mem(a, len, False); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 426 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 427 | DRD_(suppress_relocation_conflicts)(a, len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 428 | } |
| 429 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 430 | /* Called by the core when the stack of a thread grows, to indicate that */ |
| 431 | /* the addresses in range [ a, a + len [ may now be used by the client. */ |
| 432 | /* Assumption: stacks grow downward. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 433 | static __inline__ |
| 434 | void drd_start_using_mem_stack(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 435 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 436 | DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(), |
| 437 | a - VG_STACK_REDZONE_SZB); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 438 | drd_start_using_mem(a - VG_STACK_REDZONE_SZB, |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 439 | len + VG_STACK_REDZONE_SZB, |
| 440 | True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* Called by the core when the stack of a thread shrinks, to indicate that */ |
| 444 | /* the addresses [ a, a + len [ are no longer accessible for the client. */ |
| 445 | /* Assumption: stacks grow downward. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 446 | static __inline__ |
| 447 | void drd_stop_using_mem_stack(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 448 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 449 | DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(), |
| 450 | a + len - VG_STACK_REDZONE_SZB); |
| 451 | drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB, |
| 452 | True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 453 | } |
| 454 | |
bart | 68a8afa | 2010-09-02 14:50:41 +0000 | [diff] [blame] | 455 | static |
| 456 | Bool on_alt_stack(const Addr a) |
| 457 | { |
| 458 | ThreadId vg_tid; |
| 459 | Addr alt_min; |
| 460 | SizeT alt_size; |
| 461 | |
| 462 | vg_tid = VG_(get_running_tid)(); |
| 463 | alt_min = VG_(thread_get_altstack_min)(vg_tid); |
| 464 | alt_size = VG_(thread_get_altstack_size)(vg_tid); |
| 465 | return (SizeT)(a - alt_min) < alt_size; |
| 466 | } |
| 467 | |
| 468 | static |
| 469 | void drd_start_using_mem_alt_stack(const Addr a, const SizeT len) |
| 470 | { |
| 471 | if (!on_alt_stack(a)) |
| 472 | drd_start_using_mem_stack(a, len); |
| 473 | } |
| 474 | |
| 475 | static |
| 476 | void drd_stop_using_mem_alt_stack(const Addr a, const SizeT len) |
| 477 | { |
| 478 | if (!on_alt_stack(a)) |
| 479 | drd_stop_using_mem_stack(a, len); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Callback function invoked by the Valgrind core before a signal is delivered. |
| 484 | */ |
| 485 | static |
| 486 | void drd_pre_deliver_signal(const ThreadId vg_tid, const Int sigNo, |
| 487 | const Bool alt_stack) |
| 488 | { |
| 489 | DrdThreadId drd_tid; |
| 490 | |
| 491 | drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid); |
| 492 | DRD_(thread_set_on_alt_stack)(drd_tid, alt_stack); |
| 493 | if (alt_stack) |
| 494 | { |
| 495 | /* |
| 496 | * As soon a signal handler has been invoked on the alternate stack, |
| 497 | * switch to stack memory handling functions that can handle the |
| 498 | * alternate stack. |
| 499 | */ |
| 500 | VG_(track_new_mem_stack)(drd_start_using_mem_alt_stack); |
| 501 | VG_(track_die_mem_stack)(drd_stop_using_mem_alt_stack); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Callback function invoked by the Valgrind core after a signal is delivered, |
| 507 | * at least if the signal handler did not longjmp(). |
| 508 | */ |
| 509 | static |
| 510 | void drd_post_deliver_signal(const ThreadId vg_tid, const Int sigNo) |
| 511 | { |
| 512 | DrdThreadId drd_tid; |
| 513 | |
| 514 | drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid); |
| 515 | DRD_(thread_set_on_alt_stack)(drd_tid, False); |
| 516 | if (DRD_(thread_get_threads_on_alt_stack)() == 0) |
| 517 | { |
| 518 | VG_(track_new_mem_stack)(drd_start_using_mem_stack); |
| 519 | VG_(track_die_mem_stack)(drd_stop_using_mem_stack); |
| 520 | } |
| 521 | } |
| 522 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 523 | /** |
| 524 | * Callback function called by the Valgrind core before a stack area is |
| 525 | * being used by a signal handler. |
| 526 | * |
| 527 | * @param[in] a Start of address range. |
| 528 | * @param[in] len Address range length. |
| 529 | * @param[in] tid Valgrind thread ID for whom the signal frame is being |
| 530 | * constructed. |
| 531 | */ |
| 532 | static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len, |
| 533 | ThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 534 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 535 | DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)()); |
bart | f9427fd | 2010-08-29 09:19:07 +0000 | [diff] [blame] | 536 | drd_start_using_mem(a, len, True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 537 | } |
| 538 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 539 | static void drd_stop_using_mem_stack_signal(Addr a, SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 540 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 541 | drd_stop_using_mem(a, len, True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static |
| 545 | void drd_pre_thread_create(const ThreadId creator, const ThreadId created) |
| 546 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 547 | const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator); |
| 548 | tl_assert(created != VG_INVALID_THREADID); |
| 549 | DRD_(thread_pre_create)(drd_creator, created); |
| 550 | if (DRD_(IsValidDrdThreadId)(drd_creator)) |
| 551 | { |
| 552 | DRD_(thread_new_segment)(drd_creator); |
| 553 | } |
| 554 | if (DRD_(thread_get_trace_fork_join)()) |
| 555 | { |
| 556 | VG_(message)(Vg_DebugMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 557 | "drd_pre_thread_create creator = %d, created = %d\n", |
| 558 | drd_creator, created); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 559 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /* Called by Valgrind's core before any loads or stores are performed on */ |
| 563 | /* the context of thread "created". At startup, this function is called */ |
| 564 | /* with arguments (0,1). */ |
| 565 | static |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 566 | void drd_post_thread_create(const ThreadId vg_created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 567 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 568 | DrdThreadId drd_created; |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 569 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 570 | tl_assert(vg_created != VG_INVALID_THREADID); |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 571 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 572 | drd_created = DRD_(thread_post_create)(vg_created); |
| 573 | if (DRD_(thread_get_trace_fork_join)()) |
| 574 | { |
| 575 | VG_(message)(Vg_DebugMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 576 | "drd_post_thread_create created = %d\n", |
| 577 | drd_created); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 578 | } |
| 579 | if (! DRD_(get_check_stack_accesses)()) |
| 580 | { |
| 581 | DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created) |
| 582 | - DRD_(thread_get_stack_size)(drd_created), |
| 583 | DRD_(thread_get_stack_max)(drd_created), |
| 584 | "stack"); |
| 585 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 586 | } |
| 587 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 588 | /* Called after a thread has performed its last memory access. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 589 | static void drd_thread_finished(ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 590 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 591 | DrdThreadId drd_tid; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 592 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 593 | tl_assert(VG_(get_running_tid)() == vg_tid); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 594 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 595 | drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid); |
| 596 | if (DRD_(thread_get_trace_fork_join)()) |
| 597 | { |
| 598 | VG_(message)(Vg_DebugMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 599 | "drd_thread_finished tid = %d%s\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 600 | drd_tid, |
| 601 | DRD_(thread_get_joinable)(drd_tid) |
| 602 | ? "" |
| 603 | : " (which is a detached thread)"); |
| 604 | } |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 605 | if (s_show_stack_usage) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 606 | { |
| 607 | const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid); |
| 608 | const SizeT used_stack |
| 609 | = (DRD_(thread_get_stack_max)(drd_tid) |
| 610 | - DRD_(thread_get_stack_min_min)(drd_tid)); |
| 611 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 612 | "thread %d%s finished and used %ld bytes out of %ld" |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 613 | " on its stack. Margin: %ld bytes.\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 614 | drd_tid, |
| 615 | DRD_(thread_get_joinable)(drd_tid) |
| 616 | ? "" |
| 617 | : " (which is a detached thread)", |
| 618 | used_stack, |
| 619 | stack_size, |
| 620 | stack_size - used_stack); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 621 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 622 | } |
| 623 | drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid), |
| 624 | DRD_(thread_get_stack_max)(drd_tid) |
| 625 | - DRD_(thread_get_stack_min)(drd_tid), |
| 626 | True); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 627 | DRD_(thread_set_record_loads)(drd_tid, False); |
| 628 | DRD_(thread_set_record_stores)(drd_tid, False); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 629 | DRD_(thread_finished)(drd_tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 630 | } |
| 631 | |
bart | 5c7e6b6 | 2011-02-03 17:47:50 +0000 | [diff] [blame] | 632 | /* |
| 633 | * Called immediately after fork for the child process only. 'tid' is the |
| 634 | * only surviving thread in the child process. Cleans up thread state. |
| 635 | * See also http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_atfork.html for a detailed discussion of using fork() in combination with mutexes. |
| 636 | */ |
| 637 | static |
| 638 | void drd__atfork_child(ThreadId tid) |
| 639 | { |
| 640 | DRD_(drd_thread_atfork_child)(tid); |
| 641 | } |
| 642 | |
| 643 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 644 | // |
| 645 | // Implementation of the tool interface. |
| 646 | // |
| 647 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 648 | static void DRD_(post_clo_init)(void) |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 649 | { |
bart | 0c6bf35 | 2009-07-25 14:03:53 +0000 | [diff] [blame] | 650 | #if defined(VGO_linux) || defined(VGO_darwin) |
bart | c6ef4af | 2009-07-23 10:10:30 +0000 | [diff] [blame] | 651 | /* fine */ |
bart | c6ef4af | 2009-07-23 10:10:30 +0000 | [diff] [blame] | 652 | #else |
| 653 | VG_(printf)("\nWARNING: DRD has not yet been tested on this operating system.\n\n"); |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 654 | # endif |
bart | 95761b5 | 2008-03-29 08:34:03 +0000 | [diff] [blame] | 655 | |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 656 | if (s_var_info) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 657 | { |
| 658 | VG_(needs_var_info)(); |
| 659 | } |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 660 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 661 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 662 | static void drd_start_client_code(const ThreadId tid, const ULong bbs_done) |
| 663 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 664 | tl_assert(tid == VG_(get_running_tid)()); |
| 665 | DRD_(thread_set_vg_running_tid)(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 666 | } |
| 667 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 668 | static void DRD_(fini)(Int exitcode) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 669 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 670 | // thread_print_all(); |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 671 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) { |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 672 | VG_(message)(Vg_UserMsg, |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 673 | "For counts of detected and suppressed errors, " |
| 674 | "rerun with: -v\n"); |
| 675 | } |
| 676 | |
bart | 75c1cba | 2010-08-29 07:20:30 +0000 | [diff] [blame] | 677 | if (VG_(clo_stats) || s_print_stats) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 678 | { |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 679 | ULong pu = DRD_(thread_get_update_conflict_set_count)(); |
| 680 | ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)(); |
| 681 | ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)(); |
| 682 | ULong pu_join = DRD_(thread_get_update_conflict_set_join_count)(); |
| 683 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 684 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 685 | " thread: %lld context switches.\n", |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 686 | DRD_(thread_get_context_switch_count)()); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 687 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 688 | "confl set: %lld full updates and %lld partial updates;\n", |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 689 | DRD_(thread_get_compute_conflict_set_count)(), |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 690 | pu); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 691 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 692 | " %lld partial updates during segment creation,\n", |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 693 | pu_seg_cr); |
| 694 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 695 | " %lld because of mutex/sema/cond.var. operations,\n", |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 696 | pu_mtx_cv); |
| 697 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 698 | " %lld because of barrier/rwlock operations and\n", |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 699 | pu - pu_seg_cr - pu_mtx_cv - pu_join); |
| 700 | VG_(message)(Vg_UserMsg, |
| 701 | " %lld partial updates because of thread join" |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 702 | " operations.\n", |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 703 | pu_join); |
| 704 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 705 | " segments: created %lld segments, max %lld alive,\n", |
bart | 9dedafe | 2009-06-21 18:09:02 +0000 | [diff] [blame] | 706 | DRD_(sg_get_segments_created_count)(), |
| 707 | DRD_(sg_get_max_segments_alive_count)()); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 708 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 709 | " %lld discard points and %lld merges.\n", |
bart | 9dedafe | 2009-06-21 18:09:02 +0000 | [diff] [blame] | 710 | DRD_(thread_get_discard_ordered_segments_count)(), |
bart | 1a3b0b3 | 2009-05-03 17:07:34 +0000 | [diff] [blame] | 711 | DRD_(sg_get_segment_merge_count)()); |
| 712 | VG_(message)(Vg_UserMsg, |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 713 | "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and" |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 714 | " %lld barrier.\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 715 | DRD_(get_mutex_segment_creation_count)(), |
| 716 | DRD_(get_rwlock_segment_creation_count)(), |
| 717 | DRD_(get_semaphore_segment_creation_count)(), |
| 718 | DRD_(get_barrier_segment_creation_count)()); |
| 719 | VG_(message)(Vg_UserMsg, |
bart | 9dedafe | 2009-06-21 18:09:02 +0000 | [diff] [blame] | 720 | " bitmaps: %lld level one" |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 721 | " and %lld level two bitmaps were allocated.\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 722 | DRD_(bm_get_bitmap_creation_count)(), |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 723 | DRD_(bm_get_bitmap2_creation_count)()); |
| 724 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 725 | " mutex: %lld non-recursive lock/unlock events.\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 726 | DRD_(get_mutex_lock_count)()); |
| 727 | DRD_(print_malloc_stats)(); |
| 728 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 729 | } |
| 730 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 731 | static |
| 732 | void drd_pre_clo_init(void) |
| 733 | { |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 734 | // Basic tool stuff. |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 735 | VG_(details_name) ("drd"); |
| 736 | VG_(details_version) (NULL); |
| 737 | VG_(details_description) ("a thread error detector"); |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 738 | VG_(details_copyright_author)("Copyright (C) 2006-2010, and GNU GPL'd," |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 739 | " by Bart Van Assche."); |
| 740 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 741 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 742 | VG_(basic_tool_funcs) (DRD_(post_clo_init), |
| 743 | DRD_(instrument), |
| 744 | DRD_(fini)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 745 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 746 | // Command line stuff. |
| 747 | VG_(needs_command_line_options)(DRD_(process_cmd_line_option), |
| 748 | DRD_(print_usage), |
| 749 | DRD_(print_debug_usage)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 750 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 751 | // Error handling. |
| 752 | DRD_(register_error_handlers)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 753 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 754 | // Core event tracking. |
| 755 | VG_(track_pre_mem_read) (drd_pre_mem_read); |
| 756 | VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz); |
| 757 | VG_(track_post_mem_write) (drd_post_mem_write); |
| 758 | VG_(track_new_mem_brk) (drd_start_using_mem_w_tid); |
| 759 | VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms); |
| 760 | VG_(track_new_mem_stack) (drd_start_using_mem_stack); |
| 761 | VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal); |
| 762 | VG_(track_new_mem_startup) (drd_start_using_mem_w_perms); |
| 763 | VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem); |
| 764 | VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem); |
| 765 | VG_(track_die_mem_stack) (drd_stop_using_mem_stack); |
| 766 | VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal); |
bart | 68a8afa | 2010-09-02 14:50:41 +0000 | [diff] [blame] | 767 | VG_(track_pre_deliver_signal) (drd_pre_deliver_signal); |
| 768 | VG_(track_post_deliver_signal) (drd_post_deliver_signal); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 769 | VG_(track_start_client_code) (drd_start_client_code); |
| 770 | VG_(track_pre_thread_ll_create) (drd_pre_thread_create); |
| 771 | VG_(track_pre_thread_first_insn)(drd_post_thread_create); |
| 772 | VG_(track_pre_thread_ll_exit) (drd_thread_finished); |
bart | 5c7e6b6 | 2011-02-03 17:47:50 +0000 | [diff] [blame] | 773 | VG_(atfork) (NULL/*pre*/, NULL/*parent*/, |
| 774 | drd__atfork_child/*child*/); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 775 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 776 | // Other stuff. |
| 777 | DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu, |
| 778 | drd_stop_using_nonstack_mem); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 779 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 780 | DRD_(clientreq_init)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 781 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 782 | DRD_(suppression_init)(); |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 783 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 784 | DRD_(clientobj_init)(); |
bart | e5e0efa | 2009-06-24 18:35:02 +0000 | [diff] [blame] | 785 | |
| 786 | { |
| 787 | Char* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL"); |
| 788 | if (smi) |
| 789 | DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL)); |
| 790 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
| 794 | VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init) |