sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 2 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 3 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU General Public License as |
| 8 | published by the Free Software Foundation; either version 2 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307, USA. |
| 20 | |
| 21 | The GNU General Public License is contained in the file COPYING. |
| 22 | */ |
| 23 | |
| 24 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 25 | #include "drd_barrier.h" |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 26 | #include "drd_clientobj.h" |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 27 | #include "drd_clientreq.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 28 | #include "drd_cond.h" |
| 29 | #include "drd_error.h" |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 30 | #include "drd_load_store.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | #include "drd_malloc_wrappers.h" |
| 32 | #include "drd_mutex.h" |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 33 | #include "drd_rwlock.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 34 | #include "drd_segment.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 35 | #include "drd_semaphore.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | #include "drd_suppression.h" |
| 37 | #include "drd_thread.h" |
bart | eacd916 | 2008-06-16 20:22:18 +0000 | [diff] [blame] | 38 | #include "libvex_guest_offsets.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 39 | #include "pub_drd_bitmap.h" |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 40 | #include "pub_tool_vki.h" // Must be included before pub_tool_libcproc |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 41 | #include "pub_tool_basics.h" |
| 42 | #include "pub_tool_debuginfo.h" // VG_(describe_IP)() |
| 43 | #include "pub_tool_libcassert.h" // tl_assert() |
| 44 | #include "pub_tool_libcbase.h" // VG_(strcmp) |
| 45 | #include "pub_tool_libcprint.h" // VG_(printf) |
| 46 | #include "pub_tool_libcproc.h" |
| 47 | #include "pub_tool_machine.h" |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 48 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 49 | #include "pub_tool_options.h" // command line options |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 50 | #include "pub_tool_replacemalloc.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 51 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | #include "pub_tool_tooliface.h" |
| 53 | |
| 54 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 55 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 57 | static Bool DRD_(s_print_stats) = False; |
| 58 | static Bool DRD_(s_var_info) = False; |
| 59 | static Bool DRD_(s_show_stack_usage) = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 60 | |
| 61 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Implement the needs_command_line_options for drd. |
| 64 | */ |
| 65 | static Bool DRD_(process_cmd_line_option)(Char* arg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 66 | { |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 67 | int check_stack_accesses = -1; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 68 | int exclusive_threshold_ms = -1; |
bart | 764dea2 | 2009-02-15 13:16:52 +0000 | [diff] [blame] | 69 | int report_signal_unlocked = -1; |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 70 | int segment_merging = -1; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 71 | int shared_threshold_ms = -1; |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 72 | int show_confl_seg = -1; |
| 73 | int trace_barrier = -1; |
| 74 | int trace_clientobj = -1; |
| 75 | int trace_cond = -1; |
| 76 | int trace_csw = -1; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 77 | int trace_fork_join = -1; |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 78 | int trace_conflict_set = -1; |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 79 | int trace_mutex = -1; |
| 80 | int trace_rwlock = -1; |
| 81 | int trace_segment = -1; |
| 82 | int trace_semaphore = -1; |
| 83 | int trace_suppression = -1; |
| 84 | Char* trace_address = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 85 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 86 | if VG_BOOL_CLO(arg, "--check-stack-var", check_stack_accesses) {} |
| 87 | else if VG_BOOL_CLO(arg, "--drd-stats", DRD_(s_print_stats)) {} |
| 88 | else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked) {} |
| 89 | else if VG_BOOL_CLO(arg, "--segment-merging", segment_merging) {} |
| 90 | else if VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) {} |
| 91 | else if VG_BOOL_CLO(arg, "--show-stack-usage", DRD_(s_show_stack_usage)) {} |
| 92 | else if VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) {} |
| 93 | else if VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) {} |
| 94 | else if VG_BOOL_CLO(arg, "--trace-cond", trace_cond) {} |
| 95 | else if VG_BOOL_CLO(arg, "--trace-conflict-set", trace_conflict_set) {} |
| 96 | else if VG_BOOL_CLO(arg, "--trace-csw", trace_csw) {} |
| 97 | else if VG_BOOL_CLO(arg, "--trace-fork-join", trace_fork_join) {} |
| 98 | else if VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) {} |
| 99 | else if VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) {} |
| 100 | else if VG_BOOL_CLO(arg, "--trace-segment", trace_segment) {} |
| 101 | else if VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) {} |
| 102 | else if VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) {} |
| 103 | else if VG_BOOL_CLO(arg, "--var-info", DRD_(s_var_info)) {} |
| 104 | else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {} |
| 105 | else if VG_INT_CLO (arg, "--shared-threshold", shared_threshold_ms) {} |
| 106 | else if VG_STR_CLO (arg, "--trace-addr", trace_address) {} |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 107 | else |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 108 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 109 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 110 | if (check_stack_accesses != -1) |
| 111 | DRD_(set_check_stack_accesses)(check_stack_accesses); |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 112 | if (exclusive_threshold_ms != -1) |
| 113 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 114 | DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms); |
| 115 | DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms); |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 116 | } |
bart | 764dea2 | 2009-02-15 13:16:52 +0000 | [diff] [blame] | 117 | if (report_signal_unlocked != -1) |
| 118 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 119 | DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked); |
bart | 764dea2 | 2009-02-15 13:16:52 +0000 | [diff] [blame] | 120 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 121 | if (shared_threshold_ms != -1) |
| 122 | { |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 123 | DRD_(rwlock_set_shared_threshold)(shared_threshold_ms); |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 124 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 125 | if (segment_merging != -1) |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 126 | DRD_(thread_set_segment_merging)(segment_merging); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 127 | if (show_confl_seg != -1) |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 128 | DRD_(set_show_conflicting_segments)(show_confl_seg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 129 | if (trace_address) |
| 130 | { |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 131 | const Addr addr = VG_(strtoll16)(trace_address, 0); |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 132 | DRD_(start_tracing_address_range)(addr, addr + 1); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 133 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 134 | if (trace_barrier != -1) |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 135 | DRD_(barrier_set_trace)(trace_barrier); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 136 | if (trace_clientobj != -1) |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 137 | DRD_(clientobj_set_trace)(trace_clientobj); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 138 | if (trace_cond != -1) |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 139 | DRD_(cond_set_trace)(trace_cond); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 140 | if (trace_csw != -1) |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 141 | DRD_(thread_trace_context_switches)(trace_csw); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 142 | if (trace_fork_join != -1) |
| 143 | DRD_(thread_set_trace_fork_join)(trace_fork_join); |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 144 | if (trace_conflict_set != -1) |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 145 | DRD_(thread_trace_conflict_set)(trace_conflict_set); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 146 | if (trace_mutex != -1) |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 147 | DRD_(mutex_set_trace)(trace_mutex); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 148 | if (trace_rwlock != -1) |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 149 | DRD_(rwlock_set_trace)(trace_rwlock); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 150 | if (trace_segment != -1) |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 151 | DRD_(sg_set_trace)(trace_segment); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 152 | if (trace_semaphore != -1) |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 153 | DRD_(semaphore_set_trace)(trace_semaphore); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 154 | if (trace_suppression != -1) |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 155 | DRD_(suppression_set_trace)(trace_suppression); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 156 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 157 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 158 | } |
| 159 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 160 | static void DRD_(print_usage)(void) |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 161 | { |
| 162 | VG_(printf)( |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 163 | " --check-stack-var=yes|no Whether or not to report data races on\n" |
| 164 | " stack variables [no].\n" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 165 | " --exclusive-threshold=<n> Print an error message if any mutex or\n" |
| 166 | " writer lock is held longer than the specified time (in milliseconds).\n" |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 167 | " --report-signal-unlocked=yes|no Whether to report calls to\n" |
| 168 | " pthread_cond_signal() where the mutex associated\n" |
| 169 | " with the signal via pthread_cond_wait() is not\n" |
| 170 | " locked at the time the signal is sent [yes].\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 171 | " --segment-merging=yes|no Controls segment merging [yes].\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 172 | " Segment merging is an algorithm to limit memory usage of the\n" |
| 173 | " data race detection algorithm. Disabling segment merging may\n" |
| 174 | " improve the accuracy of the so-called 'other segments' displayed\n" |
| 175 | " in race reports but can also trigger an out of memory error.\n" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 176 | " --shared-threshold=<n> Print an error message if a reader lock\n" |
| 177 | " is held longer than the specified time (in milliseconds).\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 178 | " --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n" |
| 179 | " --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n" |
| 180 | " --var-info=yes|no Display the names of global, static and\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 181 | " stack variables when a race is reported on such a variable. This\n" |
| 182 | " information is by default not displayed since for big programs\n" |
bart | 85d2253 | 2008-06-26 07:30:32 +0000 | [diff] [blame] | 183 | " reading in all debug information at once may cause an out of\n" |
| 184 | " memory error [no].\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 185 | "\n" |
bart | ef1b972 | 2008-07-04 15:34:23 +0000 | [diff] [blame] | 186 | " drd options for monitoring process behavior:\n" |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 187 | " --trace-addr=<address> Trace all load and store activity for the.\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 188 | " specified address [off].\n" |
| 189 | " --trace-barrier=yes|no Trace all barrier activity [no].\n" |
| 190 | " --trace-cond=yes|no Trace all condition variable activity [no].\n" |
| 191 | " --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n" |
| 192 | " --trace-mutex=yes|no Trace all mutex activity [no].\n" |
| 193 | " --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 194 | " --trace-semaphore=yes|no Trace all semaphore activity [no].\n" |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 195 | ); |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 196 | VG_(replacement_malloc_print_usage)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 199 | static void DRD_(print_debug_usage)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 200 | { |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 201 | VG_(printf)( |
| 202 | " --drd-stats=yes|no Print statistics about DRD activity [no].\n" |
| 203 | " --trace-clientobj=yes|no Trace all client object activity [no].\n" |
| 204 | " --trace-csw=yes|no Trace all scheduler context switches [no].\n" |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 205 | " --trace-conflict-set=yes|no Trace all conflict set updates [no].\n" |
bart | 987781d | 2008-06-27 15:00:07 +0000 | [diff] [blame] | 206 | " --trace-segment=yes|no Trace segment actions [no].\n" |
| 207 | " --trace-suppr=yes|no Trace all address suppression actions [no].\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 208 | ); |
| 209 | VG_(replacement_malloc_print_debug_usage)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | // |
| 214 | // Implements the thread-related core callbacks. |
| 215 | // |
| 216 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 217 | static void drd_pre_mem_read(const CorePart part, |
| 218 | const ThreadId tid, |
| 219 | Char* const s, |
| 220 | const Addr a, |
| 221 | const SizeT size) |
| 222 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 223 | if (size > 0) |
| 224 | { |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 225 | DRD_(trace_load)(a, size); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 226 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 227 | } |
| 228 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 229 | static void drd_pre_mem_read_asciiz(const CorePart part, |
| 230 | const ThreadId tid, |
| 231 | Char* const s, |
| 232 | const Addr a) |
| 233 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 234 | const char* p = (void*)a; |
| 235 | SizeT size = 0; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 236 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 237 | /* Note: the expression '*p' reads client memory and may crash if the */ |
| 238 | /* client provided an invalid pointer ! */ |
| 239 | while (*p) |
| 240 | { |
| 241 | p++; |
| 242 | size++; |
| 243 | } |
| 244 | // To do: find out what a reasonable upper limit on 'size' is. |
| 245 | tl_assert(size < 4096); |
| 246 | if (size > 0) |
| 247 | { |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 248 | DRD_(trace_load)(a, size); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 249 | } |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 250 | } |
| 251 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 252 | static void drd_post_mem_write(const CorePart part, |
| 253 | const ThreadId tid, |
| 254 | const Addr a, |
| 255 | const SizeT size) |
| 256 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 257 | DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 258 | if (size > 0) |
| 259 | { |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 260 | DRD_(trace_store)(a, size); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 261 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 264 | static __inline__ |
| 265 | void drd_start_using_mem(const Addr a1, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 266 | { |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 267 | tl_assert(a1 < a1 + len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 268 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 269 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 270 | { |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 271 | DRD_(trace_mem_access)(a1, len, eStart); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 272 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 273 | } |
| 274 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 275 | static void drd_start_using_mem_w_ecu(const Addr a1, |
| 276 | const SizeT len, |
| 277 | UInt ec_uniq) |
| 278 | { |
| 279 | drd_start_using_mem(a1, len); |
| 280 | } |
| 281 | |
| 282 | static void drd_start_using_mem_w_tid(const Addr a1, |
| 283 | const SizeT len, |
| 284 | ThreadId tid) |
| 285 | { |
| 286 | drd_start_using_mem(a1, len); |
| 287 | } |
| 288 | |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 289 | static __inline__ |
| 290 | void drd_stop_using_mem(const Addr a1, const SizeT len, |
| 291 | const Bool is_stack_mem) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 292 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 293 | const Addr a2 = a1 + len; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 294 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 295 | tl_assert(a1 < a2); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 296 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 297 | if (UNLIKELY(DRD_(any_address_is_traced)())) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 298 | { |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 299 | DRD_(trace_mem_access)(a1, len, eEnd); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 300 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 301 | if (! is_stack_mem || DRD_(get_check_stack_accesses)()) |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 302 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 303 | DRD_(thread_stop_using_mem)(a1, a2); |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 304 | DRD_(clientobj_stop_using_mem)(a1, a2); |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 305 | DRD_(suppression_stop_using_mem)(a1, a2); |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | static __inline__ |
| 310 | void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len) |
| 311 | { |
| 312 | drd_stop_using_mem(a1, len, False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 313 | } |
| 314 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 315 | /** |
bart | fdaa018 | 2009-03-10 09:25:32 +0000 | [diff] [blame^] | 316 | * Discard all information DRD has about memory accesses and client objects |
| 317 | * in the specified address range. |
| 318 | */ |
| 319 | void DRD_(clean_memory)(const Addr a1, const SizeT len) |
| 320 | { |
| 321 | const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1); |
| 322 | drd_stop_using_mem(a1, len, is_stack_memory); |
| 323 | drd_start_using_mem(a1, len); |
| 324 | } |
| 325 | |
| 326 | /** |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 327 | * Suppress data race reports on all addresses contained in .plt and |
| 328 | * .got.plt sections inside the address range [ a, a + len [. The data in |
| 329 | * these sections is modified by _dl_relocate_object() every time a function |
| 330 | * in a shared library is called for the first time. Since the first call |
| 331 | * to a function in a shared library can happen from a multithreaded context, |
| 332 | * such calls can cause conflicting accesses. See also Ulrich Drepper's |
| 333 | * paper "How to Write Shared Libraries" for more information about relocation |
| 334 | * (http://people.redhat.com/drepper/dsohowto.pdf). |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 335 | */ |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 336 | static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len) |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 337 | { |
| 338 | const DebugInfo* di; |
| 339 | |
| 340 | #if 0 |
| 341 | VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len); |
| 342 | #endif |
| 343 | |
| 344 | for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di)) |
| 345 | { |
| 346 | Addr avma; |
| 347 | SizeT size; |
| 348 | |
| 349 | avma = VG_(seginfo_get_plt_avma)(di); |
| 350 | size = VG_(seginfo_get_plt_size)(di); |
bart | 7f42a3c | 2009-01-30 17:52:21 +0000 | [diff] [blame] | 351 | tl_assert((avma && size) || (avma == 0 && size == 0)); |
| 352 | if (size > 0) |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 353 | { |
| 354 | #if 0 |
| 355 | VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size); |
| 356 | #endif |
| 357 | tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT); |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 358 | DRD_(start_suppression)(avma, avma + size, ".plt"); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | avma = VG_(seginfo_get_gotplt_avma)(di); |
| 362 | size = VG_(seginfo_get_gotplt_size)(di); |
bart | 7f42a3c | 2009-01-30 17:52:21 +0000 | [diff] [blame] | 363 | tl_assert((avma && size) || (avma == 0 && size == 0)); |
| 364 | if (size > 0) |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 365 | { |
| 366 | #if 0 |
| 367 | VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size); |
| 368 | #endif |
| 369 | tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT); |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 370 | DRD_(start_suppression)(avma, avma + size, ".gotplt"); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 375 | static |
| 376 | void drd_start_using_mem_w_perms(const Addr a, const SizeT len, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 377 | const Bool rr, const Bool ww, const Bool xx, |
| 378 | ULong di_handle) |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 379 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 380 | DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)()); |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 381 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 382 | drd_start_using_mem(a, len); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 383 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 384 | DRD_(suppress_relocation_conflicts)(a, len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 385 | } |
| 386 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 387 | /* Called by the core when the stack of a thread grows, to indicate that */ |
| 388 | /* the addresses in range [ a, a + len [ may now be used by the client. */ |
| 389 | /* Assumption: stacks grow downward. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 390 | static __inline__ |
| 391 | void drd_start_using_mem_stack(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 392 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 393 | DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(), |
| 394 | a - VG_STACK_REDZONE_SZB); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 395 | drd_start_using_mem(a - VG_STACK_REDZONE_SZB, |
| 396 | len + VG_STACK_REDZONE_SZB); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Called by the core when the stack of a thread shrinks, to indicate that */ |
| 400 | /* the addresses [ a, a + len [ are no longer accessible for the client. */ |
| 401 | /* Assumption: stacks grow downward. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 402 | static __inline__ |
| 403 | void drd_stop_using_mem_stack(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 404 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 405 | DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(), |
| 406 | a + len - VG_STACK_REDZONE_SZB); |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 407 | drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB, |
| 408 | True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 409 | } |
| 410 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 411 | static void drd_start_using_mem_stack_signal( |
| 412 | const Addr a, const SizeT len, |
| 413 | ThreadId tid_for_whom_the_signal_frame_is_being_constructed) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 414 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 415 | DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 416 | drd_start_using_mem(a, len); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 417 | } |
| 418 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 419 | static void drd_stop_using_mem_stack_signal(Addr a, SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 420 | { |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 421 | drd_stop_using_mem(a, len, True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static |
| 425 | void drd_pre_thread_create(const ThreadId creator, const ThreadId created) |
| 426 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 427 | const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 428 | tl_assert(created != VG_INVALID_THREADID); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 429 | DRD_(thread_pre_create)(drd_creator, created); |
| 430 | if (DRD_(IsValidDrdThreadId)(drd_creator)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 431 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 432 | DRD_(thread_new_segment)(drd_creator); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 433 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 434 | if (DRD_(thread_get_trace_fork_join)()) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 435 | { |
| 436 | VG_(message)(Vg_DebugMsg, |
| 437 | "drd_pre_thread_create creator = %d/%d, created = %d", |
| 438 | creator, drd_creator, created); |
| 439 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Called by Valgrind's core before any loads or stores are performed on */ |
| 443 | /* the context of thread "created". At startup, this function is called */ |
| 444 | /* with arguments (0,1). */ |
| 445 | static |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 446 | void drd_post_thread_create(const ThreadId vg_created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 447 | { |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 448 | DrdThreadId drd_created; |
| 449 | |
| 450 | tl_assert(vg_created != VG_INVALID_THREADID); |
| 451 | |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 452 | drd_created = DRD_(thread_post_create)(vg_created); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 453 | if (DRD_(thread_get_trace_fork_join)()) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 454 | { |
| 455 | VG_(message)(Vg_DebugMsg, |
| 456 | "drd_post_thread_create created = %d/%d", |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 457 | vg_created, drd_created); |
| 458 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 459 | if (! DRD_(get_check_stack_accesses)()) |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 460 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 461 | DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created) |
| 462 | - DRD_(thread_get_stack_size)(drd_created), |
| 463 | DRD_(thread_get_stack_max)(drd_created), |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 464 | "stack"); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 465 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 466 | } |
| 467 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 468 | /* Called after a thread has performed its last memory access. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 469 | static void drd_thread_finished(ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 470 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 471 | DrdThreadId drd_tid; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 472 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 473 | tl_assert(VG_(get_running_tid)() == vg_tid); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 474 | |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 475 | drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 476 | if (DRD_(thread_get_trace_fork_join)()) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 477 | { |
| 478 | VG_(message)(Vg_DebugMsg, |
| 479 | "drd_thread_finished tid = %d/%d%s", |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 480 | vg_tid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 481 | drd_tid, |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 482 | DRD_(thread_get_joinable)(drd_tid) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 483 | ? "" |
| 484 | : " (which is a detached thread)"); |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame] | 485 | } |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 486 | if (DRD_(s_show_stack_usage)) |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame] | 487 | { |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 488 | const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid); |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame] | 489 | const SizeT used_stack |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 490 | = (DRD_(thread_get_stack_max)(drd_tid) |
| 491 | - DRD_(thread_get_stack_min_min)(drd_tid)); |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame] | 492 | VG_(message)(Vg_UserMsg, |
| 493 | "thread %d/%d%s finished and used %ld bytes out of %ld" |
| 494 | " on its stack. Margin: %ld bytes.", |
| 495 | vg_tid, |
| 496 | drd_tid, |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 497 | DRD_(thread_get_joinable)(drd_tid) |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame] | 498 | ? "" |
| 499 | : " (which is a detached thread)", |
| 500 | used_stack, |
| 501 | stack_size, |
| 502 | stack_size - used_stack); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 503 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 504 | } |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 505 | drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid), |
| 506 | DRD_(thread_get_stack_max)(drd_tid) |
| 507 | - DRD_(thread_get_stack_min)(drd_tid), |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 508 | True); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 509 | DRD_(thread_stop_recording)(drd_tid); |
| 510 | DRD_(thread_finished)(drd_tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 511 | } |
| 512 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 513 | // |
| 514 | // Implementation of the tool interface. |
| 515 | // |
| 516 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 517 | static void DRD_(post_clo_init)(void) |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 518 | { |
bart | 5403f21 | 2008-06-30 10:56:18 +0000 | [diff] [blame] | 519 | # if defined(VGP_x86_linux) || defined(VGP_amd64_linux) \ |
| 520 | || defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 521 | /* fine */ |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 522 | # else |
bart | 5403f21 | 2008-06-30 10:56:18 +0000 | [diff] [blame] | 523 | VG_(printf)("\nWARNING: DRD has only been tested on Linux.\n\n"); |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 524 | # endif |
bart | 95761b5 | 2008-03-29 08:34:03 +0000 | [diff] [blame] | 525 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 526 | if (DRD_(s_var_info)) |
bart | 95761b5 | 2008-03-29 08:34:03 +0000 | [diff] [blame] | 527 | { |
| 528 | VG_(needs_var_info)(); |
| 529 | } |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 530 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 531 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 532 | static void drd_start_client_code(const ThreadId tid, const ULong bbs_done) |
| 533 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 534 | tl_assert(tid == VG_(get_running_tid)()); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 535 | DRD_(thread_set_vg_running_tid)(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 536 | } |
| 537 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 538 | static void DRD_(fini)(Int exitcode) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 539 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 540 | // thread_print_all(); |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 541 | if (VG_(clo_verbosity) > 1 || DRD_(s_print_stats)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 542 | { |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 543 | ULong update_conflict_set_count; |
bart | e4504dd | 2008-04-06 15:02:58 +0000 | [diff] [blame] | 544 | ULong dsnsc; |
| 545 | ULong dscvc; |
| 546 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 547 | update_conflict_set_count |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 548 | = DRD_(thread_get_update_conflict_set_count)(&dsnsc, &dscvc); |
bart | e4504dd | 2008-04-06 15:02:58 +0000 | [diff] [blame] | 549 | |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 550 | VG_(message)(Vg_UserMsg, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 551 | " thread: %lld context switches" |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 552 | " / %lld updates of the conflict set", |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 553 | DRD_(thread_get_context_switch_count)(), |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 554 | update_conflict_set_count); |
bart | e4504dd | 2008-04-06 15:02:58 +0000 | [diff] [blame] | 555 | VG_(message)(Vg_UserMsg, |
| 556 | " (%lld new sg + %lld combine vc + %lld csw).", |
| 557 | dsnsc, |
| 558 | dscvc, |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 559 | update_conflict_set_count - dsnsc - dscvc); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 560 | VG_(message)(Vg_UserMsg, |
| 561 | " segments: created %lld segments, max %lld alive," |
| 562 | " %lld discard points.", |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 563 | DRD_(sg_get_segments_created_count)(), |
| 564 | DRD_(sg_get_max_segments_alive_count)(), |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 565 | DRD_(thread_get_discard_ordered_segments_count)()); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 566 | VG_(message)(Vg_UserMsg, |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 567 | " (%lld m, %lld rw, %lld s, %lld b)", |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 568 | DRD_(get_mutex_segment_creation_count)(), |
| 569 | DRD_(get_rwlock_segment_creation_count)(), |
| 570 | DRD_(get_semaphore_segment_creation_count)(), |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 571 | DRD_(get_barrier_segment_creation_count)()); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 572 | VG_(message)(Vg_UserMsg, |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 573 | " bitmaps: %lld level 1 / %lld level 2 bitmap refs", |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 574 | DRD_(bm_get_bitmap_creation_count)(), |
| 575 | DRD_(bm_get_bitmap2_node_creation_count)()); |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 576 | VG_(message)(Vg_UserMsg, |
| 577 | " and %lld level 2 bitmaps were allocated.", |
bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 578 | DRD_(bm_get_bitmap2_creation_count)()); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 579 | VG_(message)(Vg_UserMsg, |
| 580 | " mutex: %lld non-recursive lock/unlock events.", |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 581 | DRD_(get_mutex_lock_count)()); |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 582 | DRD_(print_malloc_stats)(); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 583 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 584 | } |
| 585 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 586 | static |
| 587 | void drd_pre_clo_init(void) |
| 588 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 589 | // Basic tool stuff. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 590 | |
bart | ef1b972 | 2008-07-04 15:34:23 +0000 | [diff] [blame] | 591 | VG_(details_name) ("drd"); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 592 | VG_(details_version) (NULL); |
bart | c821a94 | 2008-07-04 14:48:39 +0000 | [diff] [blame] | 593 | VG_(details_description) ("a thread error detector"); |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 594 | VG_(details_copyright_author)("Copyright (C) 2006-2009, and GNU GPL'd," |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 595 | " by Bart Van Assche."); |
| 596 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 597 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 598 | VG_(basic_tool_funcs) (DRD_(post_clo_init), |
| 599 | DRD_(instrument), |
| 600 | DRD_(fini)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 601 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 602 | // Command line stuff. |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 603 | VG_(needs_command_line_options)(DRD_(process_cmd_line_option), |
| 604 | DRD_(print_usage), |
| 605 | DRD_(print_debug_usage)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 606 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 607 | // Error handling. |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 608 | DRD_(register_error_handlers)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 609 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 610 | // Core event tracking. |
| 611 | VG_(track_pre_mem_read) (drd_pre_mem_read); |
| 612 | VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz); |
| 613 | VG_(track_post_mem_write) (drd_post_mem_write); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 614 | VG_(track_new_mem_brk) (drd_start_using_mem_w_tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 615 | VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms); |
| 616 | VG_(track_new_mem_stack) (drd_start_using_mem_stack); |
| 617 | VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal); |
| 618 | VG_(track_new_mem_startup) (drd_start_using_mem_w_perms); |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 619 | VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem); |
| 620 | VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 621 | VG_(track_die_mem_stack) (drd_stop_using_mem_stack); |
| 622 | VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal); |
| 623 | VG_(track_start_client_code) (drd_start_client_code); |
| 624 | VG_(track_pre_thread_ll_create) (drd_pre_thread_create); |
| 625 | VG_(track_pre_thread_first_insn)(drd_post_thread_create); |
| 626 | VG_(track_pre_thread_ll_exit) (drd_thread_finished); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 627 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 628 | // Other stuff. |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 629 | DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu, |
| 630 | drd_stop_using_nonstack_mem); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 631 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 632 | DRD_(clientreq_init)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 633 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 634 | DRD_(suppression_init)(); |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 635 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 636 | DRD_(clientobj_init)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
| 640 | VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init) |