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