sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 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" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 27 | #include "drd_clientreq.h" |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 28 | #include "drd_clientobj.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 29 | #include "drd_cond.h" |
| 30 | #include "drd_error.h" |
| 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" |
| 38 | #include "drd_track.h" |
| 39 | #include "drd_vc.h" |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 40 | #include "priv_drd_clientreq.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" |
| 55 | |
| 56 | |
bart | bf80e12 | 2008-06-06 10:18:24 +0000 | [diff] [blame] | 57 | /* Include several source files here in order to allow the compiler to */ |
| 58 | /* do more inlining. */ |
| 59 | #include "drd_bitmap.c" |
| 60 | #include "drd_segment.c" |
| 61 | #include "drd_thread.c" |
| 62 | #include "drd_vc.c" |
| 63 | |
| 64 | |
| 65 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 66 | // Function declarations. |
| 67 | |
| 68 | static void drd_start_client_code(const ThreadId tid, const ULong bbs_done); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | // Local variables. |
| 72 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 73 | static Bool s_drd_check_stack_accesses = False; |
| 74 | static Bool s_drd_print_stats = False; |
| 75 | static Bool s_drd_trace_fork_join = False; |
| 76 | static Bool s_drd_var_info = False; |
| 77 | static Bool s_show_stack_usage = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | // |
| 81 | // Implement the needs_command_line_options for drd. |
| 82 | // |
| 83 | |
| 84 | static Bool drd_process_cmd_line_option(Char* arg) |
| 85 | { |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 86 | int exclusive_threshold_ms = -1; |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 87 | int segment_merging = -1; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 88 | int shared_threshold_ms = -1; |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 89 | int show_confl_seg = -1; |
| 90 | int trace_barrier = -1; |
| 91 | int trace_clientobj = -1; |
| 92 | int trace_cond = -1; |
| 93 | int trace_csw = -1; |
| 94 | int trace_danger_set = -1; |
| 95 | int trace_mutex = -1; |
| 96 | int trace_rwlock = -1; |
| 97 | int trace_segment = -1; |
| 98 | int trace_semaphore = -1; |
| 99 | int trace_suppression = -1; |
| 100 | Char* trace_address = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 101 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 102 | VG_BOOL_CLO (arg, "--check-stack-var", s_drd_check_stack_accesses) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 103 | else VG_BOOL_CLO(arg, "--drd-stats", s_drd_print_stats) |
| 104 | else VG_BOOL_CLO(arg, "--segment-merging", segment_merging) |
| 105 | else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) |
| 106 | else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) |
| 107 | else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) |
| 108 | else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) |
| 109 | else VG_BOOL_CLO(arg, "--trace-cond", trace_cond) |
| 110 | else VG_BOOL_CLO(arg, "--trace-csw", trace_csw) |
| 111 | else VG_BOOL_CLO(arg, "--trace-danger-set", trace_danger_set) |
| 112 | else VG_BOOL_CLO(arg, "--trace-fork-join", s_drd_trace_fork_join) |
| 113 | else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) |
| 114 | else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) |
| 115 | else VG_BOOL_CLO(arg, "--trace-segment", trace_segment) |
| 116 | else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) |
| 117 | else VG_BOOL_CLO(arg, "--trace-suppr", trace_suppression) |
| 118 | else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info) |
| 119 | else VG_NUM_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) |
| 120 | else VG_NUM_CLO (arg, "--shared-threshold", shared_threshold_ms) |
| 121 | else VG_STR_CLO (arg, "--trace-addr", trace_address) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 122 | else |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 123 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 124 | |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 125 | if (exclusive_threshold_ms != -1) |
| 126 | { |
| 127 | mutex_set_lock_threshold(exclusive_threshold_ms); |
| 128 | rwlock_set_exclusive_threshold(exclusive_threshold_ms); |
| 129 | } |
| 130 | if (shared_threshold_ms != -1) |
| 131 | { |
| 132 | rwlock_set_shared_threshold(shared_threshold_ms); |
| 133 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 134 | if (segment_merging != -1) |
| 135 | thread_set_segment_merging(segment_merging); |
| 136 | if (show_confl_seg != -1) |
bart | 16d76e5 | 2008-03-18 17:08:08 +0000 | [diff] [blame] | 137 | set_show_conflicting_segments(show_confl_seg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 138 | if (trace_address) |
| 139 | { |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 140 | const Addr addr = VG_(strtoll16)(trace_address, 0); |
| 141 | drd_start_tracing_address_range(addr, addr + 1); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 142 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 143 | if (trace_barrier != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 144 | barrier_set_trace(trace_barrier); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 145 | if (trace_clientobj != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 146 | clientobj_set_trace(trace_clientobj); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 147 | if (trace_cond != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 148 | cond_set_trace(trace_cond); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 149 | if (trace_csw != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 150 | thread_trace_context_switches(trace_csw); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 151 | if (trace_danger_set != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 152 | thread_trace_danger_set(trace_danger_set); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 153 | if (trace_mutex != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 154 | mutex_set_trace(trace_mutex); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 155 | if (trace_rwlock != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 156 | rwlock_set_trace(trace_rwlock); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 157 | if (trace_segment != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 158 | sg_set_trace(trace_segment); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 159 | if (trace_semaphore != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 160 | semaphore_set_trace(trace_semaphore); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 161 | if (trace_suppression != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 162 | suppression_set_trace(trace_suppression); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 163 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 164 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static void drd_print_usage(void) |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 168 | { |
| 169 | VG_(printf)( |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 170 | " --check-stack-var=yes|no Whether or not to report data races on\n" |
| 171 | " stack variables [no].\n" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 172 | " --exclusive-threshold=<n> Print an error message if any mutex or\n" |
| 173 | " writer lock is held longer than the specified time (in milliseconds).\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 174 | " --segment-merging=yes|no Controls segment merging [yes].\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 175 | " Segment merging is an algorithm to limit memory usage of the\n" |
| 176 | " data race detection algorithm. Disabling segment merging may\n" |
| 177 | " improve the accuracy of the so-called 'other segments' displayed\n" |
| 178 | " in race reports but can also trigger an out of memory error.\n" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 179 | " --shared-threshold=<n> Print an error message if a reader lock\n" |
| 180 | " is held longer than the specified time (in milliseconds).\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 181 | " --show-confl-seg=yes|no Show conflicting segments in race reports [yes].\n" |
| 182 | " --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n" |
| 183 | " --var-info=yes|no Display the names of global, static and\n" |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 184 | " stack variables when a race is reported on such a variable. This\n" |
| 185 | " information is by default not displayed since for big programs\n" |
| 186 | " reading in all debug information for all variables at once may\n" |
| 187 | " cause an out of memory error [no].\n" |
| 188 | "\n" |
bart | 2cb48d6 | 2008-04-01 16:57:42 +0000 | [diff] [blame] | 189 | " exp-drd options for monitoring process behavior:\n" |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 190 | " --trace-addr=<address> Trace all load and store activity for the.\n" |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 191 | " specified address [off].\n" |
| 192 | " --trace-barrier=yes|no Trace all barrier activity [no].\n" |
| 193 | " --trace-cond=yes|no Trace all condition variable activity [no].\n" |
| 194 | " --trace-fork-join=yes|no Trace all thread fork/join activity [no].\n" |
| 195 | " --trace-mutex=yes|no Trace all mutex activity [no].\n" |
| 196 | " --trace-rwlock=yes|no Trace all reader-writer lock activity[no].\n" |
| 197 | " --trace-segment=yes|no Trace segment actions [no].\n" |
| 198 | " --trace-semaphore=yes|no Trace all semaphore activity [no].\n" |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 199 | ); |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 200 | VG_(replacement_malloc_print_usage)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void drd_print_debug_usage(void) |
| 204 | { |
bart | 130463a | 2008-04-01 17:03:33 +0000 | [diff] [blame] | 205 | VG_(printf)( |
| 206 | " --drd-stats=yes|no Print statistics about DRD activity [no].\n" |
| 207 | " --trace-clientobj=yes|no Trace all client object activity [no].\n" |
| 208 | " --trace-csw=yes|no Trace all scheduler context switches [no].\n" |
| 209 | " --trace-danger-set=yes|no Trace all danger set updates [no].\n" |
| 210 | " --trace-suppr=yes|no Trace all address suppression actions [no].\n" |
| 211 | ); |
| 212 | VG_(replacement_malloc_print_debug_usage)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
| 216 | // |
| 217 | // Implements the thread-related core callbacks. |
| 218 | // |
| 219 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 220 | static void drd_trace_mem_access(const Addr addr, const SizeT size, |
| 221 | const BmAccessTypeT access_type) |
| 222 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 223 | char vc[80]; |
| 224 | vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid())); |
| 225 | VG_(message)(Vg_UserMsg, |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 226 | "%s 0x%lx size %ld (vg %d / drd %d / vc %s)", |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 227 | access_type == eLoad |
| 228 | ? "load " |
| 229 | : access_type == eStore |
| 230 | ? "store" |
| 231 | : access_type == eStart |
| 232 | ? "start" |
| 233 | : access_type == eEnd |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 234 | ? "end " |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 235 | : "????", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 236 | addr, |
| 237 | size, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 238 | VG_(get_running_tid)(), |
| 239 | thread_get_running_tid(), |
| 240 | vc); |
| 241 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 242 | VG_(clo_backtrace_size)); |
| 243 | tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid()) |
| 244 | == VG_(get_running_tid)()); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static void drd_report_race(const Addr addr, const SizeT size, |
| 248 | const BmAccessTypeT access_type) |
| 249 | { |
bart | 49c3a11 | 2008-03-15 10:28:36 +0000 | [diff] [blame] | 250 | DataRaceErrInfo drei; |
| 251 | |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 252 | if (drd_is_suppressed(addr, addr + size)) |
| 253 | return; |
| 254 | |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 255 | drei.tid = thread_get_running_tid(); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 256 | drei.addr = addr; |
| 257 | drei.size = size; |
| 258 | drei.access_type = access_type; |
| 259 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 260 | DataRaceErr, |
| 261 | VG_(get_IP)(VG_(get_running_tid)()), |
| 262 | "Conflicting accesses", |
| 263 | &drei); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 267 | { |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 268 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 269 | /* The assert below has been commented out because of performance reasons.*/ |
| 270 | tl_assert(thread_get_running_tid() |
| 271 | == VgThreadIdToDrdThreadId(VG_(get_running_tid()))); |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 272 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 273 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 274 | if (! running_thread_is_recording()) |
| 275 | return; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 276 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 277 | if (range_any_is_traced(addr, size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 278 | { |
| 279 | drd_trace_mem_access(addr, size, eLoad); |
| 280 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 281 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 282 | && bm_access_load_triggers_conflict(addr, addr + size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 283 | { |
| 284 | drd_report_race(addr, size, eLoad); |
| 285 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static VG_REGPARM(1) void drd_trace_load_1(Addr addr) |
| 289 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 290 | if (! running_thread_is_recording()) |
| 291 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 292 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 293 | if (range_any_is_traced(addr, 1)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 294 | { |
| 295 | drd_trace_mem_access(addr, 1, eLoad); |
| 296 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 297 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 298 | && bm_access_load_1_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 299 | { |
| 300 | drd_report_race(addr, 1, eLoad); |
| 301 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static VG_REGPARM(1) void drd_trace_load_2(Addr addr) |
| 305 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 306 | if (! running_thread_is_recording()) |
| 307 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 308 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 309 | if (range_any_is_traced(addr, 2)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 310 | { |
| 311 | drd_trace_mem_access(addr, 2, eLoad); |
| 312 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 313 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 314 | && bm_access_load_2_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 315 | { |
| 316 | drd_report_race(addr, 2, eLoad); |
| 317 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static VG_REGPARM(1) void drd_trace_load_4(Addr addr) |
| 321 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 322 | if (! running_thread_is_recording()) |
| 323 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 324 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 325 | if (range_any_is_traced(addr, 4)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 326 | { |
| 327 | drd_trace_mem_access(addr, 4, eLoad); |
| 328 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 329 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 330 | && bm_access_load_4_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 331 | { |
| 332 | drd_report_race(addr, 4, eLoad); |
| 333 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static VG_REGPARM(1) void drd_trace_load_8(Addr addr) |
| 337 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 338 | if (! running_thread_is_recording()) |
| 339 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 340 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 341 | if (range_any_is_traced(addr, 8)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 342 | { |
| 343 | drd_trace_mem_access(addr, 8, eLoad); |
| 344 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 345 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 346 | && bm_access_load_8_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 347 | { |
| 348 | drd_report_race(addr, 8, eLoad); |
| 349 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static |
| 353 | VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size) |
| 354 | { |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 355 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 356 | /* The assert below has been commented out because of performance reasons.*/ |
| 357 | tl_assert(thread_get_running_tid() |
| 358 | == VgThreadIdToDrdThreadId(VG_(get_running_tid()))); |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 359 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 360 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 361 | if (! running_thread_is_recording()) |
| 362 | return; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 363 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 364 | if (range_any_is_traced(addr, size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 365 | { |
| 366 | drd_trace_mem_access(addr, size, eStore); |
| 367 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 368 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 369 | && bm_access_store_triggers_conflict(addr, addr + size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 370 | { |
| 371 | drd_report_race(addr, size, eStore); |
| 372 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static VG_REGPARM(1) void drd_trace_store_1(Addr addr) |
| 376 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 377 | if (! running_thread_is_recording()) |
| 378 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 379 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 380 | if (range_any_is_traced(addr, 1)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 381 | { |
| 382 | drd_trace_mem_access(addr, 1, eStore); |
| 383 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 384 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 385 | && bm_access_store_1_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 386 | { |
| 387 | drd_report_race(addr, 1, eStore); |
| 388 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static VG_REGPARM(1) void drd_trace_store_2(Addr addr) |
| 392 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 393 | if (! running_thread_is_recording()) |
| 394 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 395 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 396 | if (range_any_is_traced(addr, 2)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 397 | { |
| 398 | drd_trace_mem_access(addr, 2, eStore); |
| 399 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 400 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 401 | && bm_access_store_2_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 402 | { |
| 403 | drd_report_race(addr, 2, eStore); |
| 404 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static VG_REGPARM(1) void drd_trace_store_4(Addr addr) |
| 408 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 409 | if (! running_thread_is_recording()) |
| 410 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 411 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 412 | if (range_any_is_traced(addr, 4)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 413 | { |
| 414 | drd_trace_mem_access(addr, 4, eStore); |
| 415 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 416 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 417 | && bm_access_store_4_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 418 | { |
| 419 | drd_report_race(addr, 4, eStore); |
| 420 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static VG_REGPARM(1) void drd_trace_store_8(Addr addr) |
| 424 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 425 | if (! running_thread_is_recording()) |
| 426 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 427 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 428 | if (range_any_is_traced(addr, 8)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 429 | { |
| 430 | drd_trace_mem_access(addr, 8, eStore); |
| 431 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 432 | if ((s_drd_check_stack_accesses || ! thread_address_on_stack(addr)) |
| 433 | && bm_access_store_8_triggers_conflict(addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 434 | { |
| 435 | drd_report_race(addr, 8, eStore); |
| 436 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static void drd_pre_mem_read(const CorePart part, |
| 440 | const ThreadId tid, |
| 441 | Char* const s, |
| 442 | const Addr a, |
| 443 | const SizeT size) |
| 444 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 445 | if (size > 0) |
| 446 | { |
| 447 | drd_trace_load(a, size); |
| 448 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 449 | } |
| 450 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 451 | static void drd_pre_mem_read_asciiz(const CorePart part, |
| 452 | const ThreadId tid, |
| 453 | Char* const s, |
| 454 | const Addr a) |
| 455 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 456 | const char* p = (void*)a; |
| 457 | SizeT size = 0; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 458 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 459 | /* Note: the expression '*p' reads client memory and may crash if the */ |
| 460 | /* client provided an invalid pointer ! */ |
| 461 | while (*p) |
| 462 | { |
| 463 | p++; |
| 464 | size++; |
| 465 | } |
| 466 | // To do: find out what a reasonable upper limit on 'size' is. |
| 467 | tl_assert(size < 4096); |
| 468 | if (size > 0) |
| 469 | { |
| 470 | drd_trace_load(a, size); |
| 471 | } |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 472 | } |
| 473 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 474 | static void drd_post_mem_write(const CorePart part, |
| 475 | const ThreadId tid, |
| 476 | const Addr a, |
| 477 | const SizeT size) |
| 478 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 479 | thread_set_vg_running_tid(VG_(get_running_tid)()); |
| 480 | if (size > 0) |
| 481 | { |
| 482 | drd_trace_store(a, size); |
| 483 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 484 | } |
| 485 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 486 | static __inline__ |
| 487 | void drd_start_using_mem(const Addr a1, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 488 | { |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 489 | tl_assert(a1 < a1 + len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 490 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 491 | if (range_any_is_traced(a1, len)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 492 | { |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 493 | drd_trace_mem_access(a1, len, eStart); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 494 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 495 | } |
| 496 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 497 | static void drd_start_using_mem_w_ecu(const Addr a1, |
| 498 | const SizeT len, |
| 499 | UInt ec_uniq) |
| 500 | { |
| 501 | drd_start_using_mem(a1, len); |
| 502 | } |
| 503 | |
| 504 | static void drd_start_using_mem_w_tid(const Addr a1, |
| 505 | const SizeT len, |
| 506 | ThreadId tid) |
| 507 | { |
| 508 | drd_start_using_mem(a1, len); |
| 509 | } |
| 510 | |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 511 | static __inline__ |
| 512 | void drd_stop_using_mem(const Addr a1, const SizeT len, |
| 513 | const Bool is_stack_mem) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 514 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 515 | const Addr a2 = a1 + len; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 516 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 517 | tl_assert(a1 < a2); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 518 | |
bart | 005dc97 | 2008-03-29 14:42:59 +0000 | [diff] [blame] | 519 | if (range_any_is_traced(a1, len)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 520 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 521 | drd_trace_mem_access(a1, len, eEnd); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 522 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 523 | if (! is_stack_mem || s_drd_check_stack_accesses) |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 524 | { |
| 525 | thread_stop_using_mem(a1, a2); |
| 526 | clientobj_stop_using_mem(a1, a2); |
| 527 | drd_suppression_stop_using_mem(a1, a2); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static __inline__ |
| 532 | void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len) |
| 533 | { |
| 534 | drd_stop_using_mem(a1, len, False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 535 | } |
| 536 | |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 537 | /** Suppress data race reports on all addresses contained in .plt and |
| 538 | * .got.plt sections inside the address range [ a, a + len [. The data in |
| 539 | * these sections is modified by _dl_relocate_object() every time a function |
| 540 | * in a shared library is called for the first time. Since the first call |
| 541 | * to a function in a shared library can happen from a multithreaded context, |
| 542 | * such calls can cause conflicting accesses. See also Ulrich Drepper's |
| 543 | * paper "How to Write Shared Libraries" for more information about relocation |
| 544 | * (http://people.redhat.com/drepper/dsohowto.pdf). |
| 545 | */ |
| 546 | static void suppress_relocation_conflicts(const Addr a, const SizeT len) |
| 547 | { |
| 548 | const DebugInfo* di; |
| 549 | |
| 550 | #if 0 |
| 551 | VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len); |
| 552 | #endif |
| 553 | |
| 554 | for (di = VG_(next_seginfo)(0); di; di = VG_(next_seginfo)(di)) |
| 555 | { |
| 556 | Addr avma; |
| 557 | SizeT size; |
| 558 | |
| 559 | avma = VG_(seginfo_get_plt_avma)(di); |
| 560 | size = VG_(seginfo_get_plt_size)(di); |
| 561 | if (a <= avma && avma + size <= a + len) |
| 562 | { |
| 563 | #if 0 |
| 564 | VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size); |
| 565 | #endif |
| 566 | tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT); |
| 567 | drd_start_suppression(avma, avma + size, ".plt"); |
| 568 | } |
| 569 | |
| 570 | avma = VG_(seginfo_get_gotplt_avma)(di); |
| 571 | size = VG_(seginfo_get_gotplt_size)(di); |
| 572 | if (a <= avma && avma + size <= a + len) |
| 573 | { |
| 574 | #if 0 |
| 575 | VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size); |
| 576 | #endif |
| 577 | tl_assert(VG_(seginfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT); |
| 578 | drd_start_suppression(avma, avma + size, ".gotplt"); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 583 | static |
| 584 | void drd_start_using_mem_w_perms(const Addr a, const SizeT len, |
| 585 | const Bool rr, const Bool ww, const Bool xx) |
| 586 | { |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 587 | thread_set_vg_running_tid(VG_(get_running_tid)()); |
| 588 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 589 | drd_start_using_mem(a, len); |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 590 | |
| 591 | suppress_relocation_conflicts(a, len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 592 | } |
| 593 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 594 | /* Called by the core when the stack of a thread grows, to indicate that */ |
| 595 | /* the addresses in range [ a, a + len [ may now be used by the client. */ |
| 596 | /* Assumption: stacks grow downward. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 597 | static __inline__ |
| 598 | void drd_start_using_mem_stack(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 599 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 600 | thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 601 | drd_start_using_mem(a - VG_STACK_REDZONE_SZB, |
| 602 | len + VG_STACK_REDZONE_SZB); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* Called by the core when the stack of a thread shrinks, to indicate that */ |
| 606 | /* the addresses [ a, a + len [ are no longer accessible for the client. */ |
| 607 | /* Assumption: stacks grow downward. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 608 | static __inline__ |
| 609 | void drd_stop_using_mem_stack(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 610 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 611 | thread_set_stack_min(thread_get_running_tid(), |
| 612 | a + len - VG_STACK_REDZONE_SZB); |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 613 | drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB, |
| 614 | True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 615 | } |
| 616 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 617 | static void drd_start_using_mem_stack_signal( |
| 618 | const Addr a, const SizeT len, |
| 619 | ThreadId tid_for_whom_the_signal_frame_is_being_constructed) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 620 | { |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 621 | thread_set_vg_running_tid(VG_(get_running_tid)()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 622 | drd_start_using_mem(a, len); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 623 | } |
| 624 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 625 | static void drd_stop_using_mem_stack_signal(Addr a, SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 626 | { |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 627 | drd_stop_using_mem(a, len, True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static |
| 631 | void drd_pre_thread_create(const ThreadId creator, const ThreadId created) |
| 632 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 633 | const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator); |
| 634 | tl_assert(created != VG_INVALID_THREADID); |
| 635 | thread_pre_create(drd_creator, created); |
| 636 | if (IsValidDrdThreadId(drd_creator)) |
| 637 | { |
| 638 | thread_new_segment(drd_creator); |
| 639 | } |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 640 | if (s_drd_trace_fork_join) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 641 | { |
| 642 | VG_(message)(Vg_DebugMsg, |
| 643 | "drd_pre_thread_create creator = %d/%d, created = %d", |
| 644 | creator, drd_creator, created); |
| 645 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Called by Valgrind's core before any loads or stores are performed on */ |
| 649 | /* the context of thread "created". At startup, this function is called */ |
| 650 | /* with arguments (0,1). */ |
| 651 | static |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 652 | void drd_post_thread_create(const ThreadId vg_created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 653 | { |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 654 | DrdThreadId drd_created; |
| 655 | |
| 656 | tl_assert(vg_created != VG_INVALID_THREADID); |
| 657 | |
| 658 | drd_created = thread_post_create(vg_created); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 659 | if (s_drd_trace_fork_join) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 660 | { |
| 661 | VG_(message)(Vg_DebugMsg, |
| 662 | "drd_post_thread_create created = %d/%d", |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 663 | vg_created, drd_created); |
| 664 | } |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 665 | if (! s_drd_check_stack_accesses) |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 666 | { |
| 667 | drd_start_suppression(thread_get_stack_max(drd_created) |
| 668 | - thread_get_stack_size(drd_created), |
| 669 | thread_get_stack_max(drd_created), |
| 670 | "stack"); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 671 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */ |
| 675 | /* after thread drd_joiner joined thread drd_joinee. */ |
| 676 | void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 677 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 678 | tl_assert(IsValidDrdThreadId(drd_joiner)); |
| 679 | tl_assert(IsValidDrdThreadId(drd_joinee)); |
| 680 | thread_new_segment(drd_joinee); |
| 681 | thread_combine_vc(drd_joiner, drd_joinee); |
| 682 | thread_new_segment(drd_joiner); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 683 | |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 684 | if (s_drd_trace_fork_join) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 685 | { |
bart | fdd8d4e | 2008-04-01 18:38:29 +0000 | [diff] [blame] | 686 | const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner); |
| 687 | const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee); |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 688 | const unsigned msg_size = 256; |
| 689 | char* msg; |
| 690 | |
| 691 | msg = VG_(malloc)(msg_size); |
bart | fdd8d4e | 2008-04-01 18:38:29 +0000 | [diff] [blame] | 692 | tl_assert(msg); |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 693 | VG_(snprintf)(msg, msg_size, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 694 | "drd_post_thread_join joiner = %d/%d, joinee = %d/%d", |
| 695 | joiner, drd_joiner, joinee, drd_joinee); |
| 696 | if (joiner) |
| 697 | { |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 698 | VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 699 | ", new vc: "); |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 700 | vc_snprint(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 701 | thread_get_vc(drd_joiner)); |
| 702 | } |
| 703 | VG_(message)(Vg_DebugMsg, msg); |
bart | 024a95a | 2008-04-01 18:27:41 +0000 | [diff] [blame] | 704 | VG_(free)(msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 705 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 706 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 707 | if (! s_drd_check_stack_accesses) |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 708 | { |
| 709 | drd_finish_suppression(thread_get_stack_max(drd_joinee) |
| 710 | - thread_get_stack_size(drd_joinee), |
| 711 | thread_get_stack_max(drd_joinee)); |
| 712 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 713 | thread_delete(drd_joinee); |
| 714 | mutex_thread_delete(drd_joinee); |
| 715 | cond_thread_delete(drd_joinee); |
| 716 | semaphore_thread_delete(drd_joinee); |
| 717 | barrier_thread_delete(drd_joinee); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 718 | } |
| 719 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 720 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 721 | /* Called after a thread has performed its last memory access. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 722 | static void drd_thread_finished(ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 723 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 724 | DrdThreadId drd_tid; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 725 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 726 | tl_assert(VG_(get_running_tid)() == vg_tid); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 727 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 728 | drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 729 | if (s_drd_trace_fork_join) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 730 | { |
| 731 | VG_(message)(Vg_DebugMsg, |
| 732 | "drd_thread_finished tid = %d/%d%s", |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 733 | vg_tid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 734 | drd_tid, |
| 735 | thread_get_joinable(drd_tid) |
| 736 | ? "" |
| 737 | : " (which is a detached thread)"); |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame] | 738 | } |
| 739 | if (s_show_stack_usage) |
| 740 | { |
| 741 | const SizeT stack_size = thread_get_stack_size(drd_tid); |
| 742 | const SizeT used_stack |
| 743 | = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid); |
| 744 | VG_(message)(Vg_UserMsg, |
| 745 | "thread %d/%d%s finished and used %ld bytes out of %ld" |
| 746 | " on its stack. Margin: %ld bytes.", |
| 747 | vg_tid, |
| 748 | drd_tid, |
| 749 | thread_get_joinable(drd_tid) |
| 750 | ? "" |
| 751 | : " (which is a detached thread)", |
| 752 | used_stack, |
| 753 | stack_size, |
| 754 | stack_size - used_stack); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 755 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 756 | } |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 757 | drd_stop_using_mem(thread_get_stack_min(drd_tid), |
| 758 | thread_get_stack_max(drd_tid) |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 759 | - thread_get_stack_min(drd_tid), |
| 760 | True); |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 761 | thread_stop_recording(drd_tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 762 | thread_finished(drd_tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 763 | } |
| 764 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 765 | void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 766 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 767 | mutex_init(mutex, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 768 | } |
| 769 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 770 | void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 771 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 772 | mutex_post_destroy(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 773 | } |
| 774 | |
bart | 2e3a3c1 | 2008-03-24 08:33:47 +0000 | [diff] [blame] | 775 | void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type, |
| 776 | const Bool trylock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 777 | { |
bart | 2e3a3c1 | 2008-03-24 08:33:47 +0000 | [diff] [blame] | 778 | mutex_pre_lock(mutex, mutex_type, trylock); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 779 | } |
| 780 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 781 | void drd_post_mutex_lock(const Addr mutex, const Bool took_lock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 782 | { |
bart | 4a975e1 | 2008-03-30 13:28:33 +0000 | [diff] [blame] | 783 | mutex_post_lock(mutex, took_lock, False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 784 | } |
| 785 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 786 | void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 787 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 788 | mutex_unlock(mutex, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 789 | } |
| 790 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 791 | void drd_pre_cond_init(Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 792 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 793 | cond_pre_init(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 794 | } |
| 795 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 796 | void drd_post_cond_destroy(Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 797 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 798 | cond_post_destroy(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 799 | } |
| 800 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 801 | void drd_semaphore_init(const Addr semaphore, |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 802 | const Word pshared, const Word value) |
| 803 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 804 | semaphore_init(semaphore, pshared, value); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | void drd_semaphore_destroy(const Addr semaphore) |
| 808 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 809 | semaphore_destroy(semaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 810 | } |
| 811 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 812 | void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 813 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 814 | semaphore_pre_wait(semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore, |
| 818 | const Bool waited) |
| 819 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 820 | semaphore_post_wait(tid, semaphore, waited); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 821 | } |
| 822 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 823 | void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 824 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 825 | semaphore_pre_post(tid, semaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 829 | const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 830 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 831 | semaphore_post_post(tid, semaphore, waited); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 835 | void drd_barrier_init(const Addr barrier, |
| 836 | const BarrierT barrier_type, const Word count, |
| 837 | const Bool reinitialization) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 838 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 839 | barrier_init(barrier, barrier_type, count, reinitialization); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 840 | } |
| 841 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 842 | void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 843 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 844 | barrier_destroy(barrier, barrier_type); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 845 | } |
| 846 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 847 | void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier, |
| 848 | const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 849 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 850 | barrier_pre_wait(tid, barrier, barrier_type); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 854 | const BarrierT barrier_type, const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 855 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 856 | barrier_post_wait(tid, barrier, barrier_type, waited); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 857 | } |
| 858 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 859 | |
| 860 | // |
| 861 | // Implementation of the tool interface. |
| 862 | // |
| 863 | |
| 864 | static |
| 865 | void drd_post_clo_init(void) |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 866 | { |
| 867 | # if defined(VGP_x86_linux) || defined(VGP_amd64_linux) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 868 | /* fine */ |
bart | d5c6352 | 2008-06-01 07:33:14 +0000 | [diff] [blame] | 869 | # elif defined(VGP_ppc32_linux) || defined(VGP_ppc64_linux) |
bart | cb2d007 | 2008-05-31 07:55:51 +0000 | [diff] [blame] | 870 | VG_(printf)( |
| 871 | "\nWARNING: support for PowerPC-specific atomic instructions like lwarx and\n" |
| 872 | "stwcx is not yet complete. As a result, false positives will be reported on\n" |
| 873 | "code that uses these instructions. This will happen e.g. when printf() is\n" |
| 874 | "called from more than one thread.\n\n"); |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 875 | # else |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 876 | VG_(printf)("\nWARNING: DRD has only been tested on x86-linux and amd64-linux.\n\n"); |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 877 | # endif |
bart | 95761b5 | 2008-03-29 08:34:03 +0000 | [diff] [blame] | 878 | |
| 879 | if (s_drd_var_info) |
| 880 | { |
| 881 | VG_(needs_var_info)(); |
| 882 | } |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 883 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 884 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 885 | static void instrument_load(IRSB* const bb, |
| 886 | IRExpr* const addr_expr, |
| 887 | const HWord size) |
| 888 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 889 | IRExpr* size_expr; |
| 890 | IRExpr** argv; |
| 891 | IRDirty* di; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 892 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 893 | switch (size) |
| 894 | { |
| 895 | case 1: |
| 896 | argv = mkIRExprVec_1(addr_expr); |
| 897 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 898 | "drd_trace_load_1", |
| 899 | VG_(fnptr_to_fnentry)(drd_trace_load_1), |
| 900 | argv); |
| 901 | break; |
| 902 | case 2: |
| 903 | argv = mkIRExprVec_1(addr_expr); |
| 904 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 905 | "drd_trace_load_2", |
| 906 | VG_(fnptr_to_fnentry)(drd_trace_load_2), |
| 907 | argv); |
| 908 | break; |
| 909 | case 4: |
| 910 | argv = mkIRExprVec_1(addr_expr); |
| 911 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 912 | "drd_trace_load_4", |
| 913 | VG_(fnptr_to_fnentry)(drd_trace_load_4), |
| 914 | argv); |
| 915 | break; |
| 916 | case 8: |
| 917 | argv = mkIRExprVec_1(addr_expr); |
| 918 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 919 | "drd_trace_load_8", |
| 920 | VG_(fnptr_to_fnentry)(drd_trace_load_8), |
| 921 | argv); |
| 922 | break; |
| 923 | default: |
| 924 | size_expr = mkIRExpr_HWord(size); |
| 925 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 926 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 927 | "drd_trace_load", |
| 928 | VG_(fnptr_to_fnentry)(drd_trace_load), |
| 929 | argv); |
| 930 | break; |
| 931 | } |
| 932 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | static void instrument_store(IRSB* const bb, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 936 | IRExpr* const addr_expr, |
| 937 | const HWord size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 938 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 939 | IRExpr* size_expr; |
| 940 | IRExpr** argv; |
| 941 | IRDirty* di; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 942 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 943 | switch (size) |
| 944 | { |
| 945 | case 1: |
| 946 | argv = mkIRExprVec_1(addr_expr); |
| 947 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 948 | "drd_trace_store_1", |
| 949 | VG_(fnptr_to_fnentry)(drd_trace_store_1), |
| 950 | argv); |
| 951 | break; |
| 952 | case 2: |
| 953 | argv = mkIRExprVec_1(addr_expr); |
| 954 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 955 | "drd_trace_store_2", |
| 956 | VG_(fnptr_to_fnentry)(drd_trace_store_2), |
| 957 | argv); |
| 958 | break; |
| 959 | case 4: |
| 960 | argv = mkIRExprVec_1(addr_expr); |
| 961 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 962 | "drd_trace_store_4", |
| 963 | VG_(fnptr_to_fnentry)(drd_trace_store_4), |
| 964 | argv); |
| 965 | break; |
| 966 | case 8: |
| 967 | argv = mkIRExprVec_1(addr_expr); |
| 968 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 969 | "drd_trace_store_8", |
| 970 | VG_(fnptr_to_fnentry)(drd_trace_store_8), |
| 971 | argv); |
| 972 | break; |
| 973 | default: |
| 974 | size_expr = mkIRExpr_HWord(size); |
| 975 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 976 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 977 | "drd_trace_store", |
| 978 | VG_(fnptr_to_fnentry)(drd_trace_store), |
| 979 | argv); |
| 980 | break; |
| 981 | } |
| 982 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 983 | } |
| 984 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 985 | static |
| 986 | IRSB* drd_instrument(VgCallbackClosure* const closure, |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 987 | IRSB* const bb_in, |
| 988 | VexGuestLayout* const layout, |
| 989 | VexGuestExtents* const vge, |
| 990 | IRType const gWordTy, |
| 991 | IRType const hWordTy) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 992 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 993 | IRDirty* di; |
| 994 | Int i; |
| 995 | IRSB* bb; |
| 996 | IRExpr** argv; |
| 997 | Bool instrument = True; |
| 998 | Bool bus_locked = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 999 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1000 | /* Set up BB */ |
| 1001 | bb = emptyIRSB(); |
| 1002 | bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv); |
| 1003 | bb->next = deepCopyIRExpr(bb_in->next); |
| 1004 | bb->jumpkind = bb_in->jumpkind; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1005 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1006 | for (i = 0; i < bb_in->stmts_used; i++) |
| 1007 | { |
| 1008 | IRStmt* const st = bb_in->stmts[i]; |
| 1009 | tl_assert(st); |
| 1010 | if (st->tag == Ist_NoOp) |
| 1011 | continue; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1012 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1013 | switch (st->tag) |
| 1014 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1015 | case Ist_MBE: |
| 1016 | switch (st->Ist.MBE.event) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1017 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1018 | case Imbe_Fence: |
| 1019 | break; /* not interesting */ |
| 1020 | case Imbe_BusLock: |
| 1021 | tl_assert(! bus_locked); |
| 1022 | bus_locked = True; |
| 1023 | break; |
| 1024 | case Imbe_BusUnlock: |
| 1025 | tl_assert(bus_locked); |
| 1026 | bus_locked = False; |
| 1027 | break; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1028 | default: |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1029 | tl_assert(0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1030 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1031 | addStmtToIRSB(bb, st); |
| 1032 | break; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1033 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1034 | case Ist_Store: |
| 1035 | if (instrument && ! bus_locked) |
| 1036 | { |
| 1037 | instrument_store(bb, |
| 1038 | st->Ist.Store.addr, |
| 1039 | sizeofIRType(typeOfIRExpr(bb->tyenv, |
| 1040 | st->Ist.Store.data))); |
| 1041 | } |
| 1042 | addStmtToIRSB(bb, st); |
| 1043 | break; |
bart | a47b351 | 2008-03-07 17:22:26 +0000 | [diff] [blame] | 1044 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1045 | case Ist_WrTmp: |
| 1046 | if (instrument) |
| 1047 | { |
| 1048 | const IRExpr* const data = st->Ist.WrTmp.data; |
| 1049 | if (data->tag == Iex_Load) |
| 1050 | { |
| 1051 | instrument_load(bb, |
| 1052 | data->Iex.Load.addr, |
| 1053 | sizeofIRType(data->Iex.Load.ty)); |
| 1054 | } |
| 1055 | } |
| 1056 | addStmtToIRSB(bb, st); |
| 1057 | break; |
| 1058 | |
| 1059 | case Ist_Dirty: |
| 1060 | if (instrument) |
| 1061 | { |
| 1062 | IRDirty* d = st->Ist.Dirty.details; |
| 1063 | IREffect const mFx = d->mFx; |
| 1064 | switch (mFx) { |
| 1065 | case Ifx_None: |
| 1066 | break; |
| 1067 | case Ifx_Read: |
| 1068 | case Ifx_Write: |
| 1069 | case Ifx_Modify: |
| 1070 | tl_assert(d->mAddr); |
| 1071 | tl_assert(d->mSize > 0); |
| 1072 | argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize)); |
| 1073 | if (mFx == Ifx_Read || mFx == Ifx_Modify) { |
| 1074 | di = unsafeIRDirty_0_N( |
| 1075 | /*regparms*/2, |
| 1076 | "drd_trace_load", |
| 1077 | VG_(fnptr_to_fnentry)(drd_trace_load), |
| 1078 | argv); |
| 1079 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 1080 | } |
| 1081 | if ((mFx == Ifx_Write || mFx == Ifx_Modify) |
| 1082 | && ! bus_locked) |
| 1083 | { |
| 1084 | di = unsafeIRDirty_0_N( |
| 1085 | /*regparms*/2, |
| 1086 | "drd_trace_store", |
| 1087 | VG_(fnptr_to_fnentry)(drd_trace_store), |
| 1088 | argv); |
| 1089 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 1090 | } |
| 1091 | break; |
| 1092 | default: |
| 1093 | tl_assert(0); |
| 1094 | } |
| 1095 | } |
| 1096 | addStmtToIRSB(bb, st); |
| 1097 | break; |
| 1098 | |
| 1099 | default: |
| 1100 | addStmtToIRSB(bb, st); |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | tl_assert(! bus_locked); |
| 1106 | |
| 1107 | return bb; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1110 | static void drd_start_client_code(const ThreadId tid, const ULong bbs_done) |
| 1111 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1112 | tl_assert(tid == VG_(get_running_tid)()); |
| 1113 | thread_set_vg_running_tid(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | static |
| 1117 | void drd_fini(Int exitcode) |
| 1118 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1119 | // thread_print_all(); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 1120 | if (VG_(clo_verbosity) > 1 || s_drd_print_stats) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1121 | { |
bart | e4504dd | 2008-04-06 15:02:58 +0000 | [diff] [blame] | 1122 | ULong update_danger_set_count; |
| 1123 | ULong dsnsc; |
| 1124 | ULong dscvc; |
| 1125 | |
| 1126 | update_danger_set_count |
| 1127 | = thread_get_update_danger_set_count(&dsnsc, &dscvc); |
| 1128 | |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 1129 | VG_(message)(Vg_UserMsg, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1130 | " thread: %lld context switches" |
bart | e4504dd | 2008-04-06 15:02:58 +0000 | [diff] [blame] | 1131 | " / %lld updates of the danger set", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1132 | thread_get_context_switch_count(), |
bart | e4504dd | 2008-04-06 15:02:58 +0000 | [diff] [blame] | 1133 | update_danger_set_count); |
| 1134 | VG_(message)(Vg_UserMsg, |
| 1135 | " (%lld new sg + %lld combine vc + %lld csw).", |
| 1136 | dsnsc, |
| 1137 | dscvc, |
| 1138 | update_danger_set_count - dsnsc - dscvc); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 1139 | VG_(message)(Vg_UserMsg, |
| 1140 | " segments: created %lld segments, max %lld alive," |
| 1141 | " %lld discard points.", |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 1142 | sg_get_created_segments_count(), |
| 1143 | sg_get_max_alive_segments_count(), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1144 | thread_get_discard_ordered_segments_count()); |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 1145 | VG_(message)(Vg_UserMsg, |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 1146 | " (%lld m, %lld rw, %lld s, %lld b)", |
| 1147 | get_mutex_segment_creation_count(), |
| 1148 | get_rwlock_segment_creation_count(), |
| 1149 | get_semaphore_segment_creation_count(), |
| 1150 | get_barrier_segment_creation_count()); |
| 1151 | VG_(message)(Vg_UserMsg, |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 1152 | " bitmaps: %lld level 1 / %lld level 2 bitmap refs", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1153 | bm_get_bitmap_creation_count(), |
bart | 952e1a0 | 2008-04-06 13:06:36 +0000 | [diff] [blame] | 1154 | bm_get_bitmap2_node_creation_count()); |
| 1155 | VG_(message)(Vg_UserMsg, |
| 1156 | " and %lld level 2 bitmaps were allocated.", |
bart | bd7e56e | 2008-03-31 18:14:12 +0000 | [diff] [blame] | 1157 | bm_get_bitmap2_creation_count()); |
| 1158 | VG_(message)(Vg_UserMsg, |
| 1159 | " mutex: %lld non-recursive lock/unlock events.", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1160 | get_mutex_lock_count()); |
| 1161 | drd_print_malloc_stats(); |
| 1162 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1165 | static |
| 1166 | void drd_pre_clo_init(void) |
| 1167 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1168 | // Basic tool stuff. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1169 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1170 | VG_(details_name) ("exp-drd"); |
| 1171 | VG_(details_version) (NULL); |
| 1172 | VG_(details_description) ("a data race detector"); |
| 1173 | VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd," |
| 1174 | " by Bart Van Assche."); |
| 1175 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1176 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1177 | VG_(basic_tool_funcs) (drd_post_clo_init, |
| 1178 | drd_instrument, |
| 1179 | drd_fini); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1180 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1181 | // Command line stuff. |
| 1182 | VG_(needs_command_line_options)(drd_process_cmd_line_option, |
| 1183 | drd_print_usage, |
| 1184 | drd_print_debug_usage); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1185 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1186 | // Error handling. |
| 1187 | drd_register_error_handlers(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1188 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1189 | // Core event tracking. |
| 1190 | VG_(track_pre_mem_read) (drd_pre_mem_read); |
| 1191 | VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz); |
| 1192 | VG_(track_post_mem_write) (drd_post_mem_write); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1193 | VG_(track_new_mem_brk) (drd_start_using_mem_w_tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1194 | VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms); |
| 1195 | VG_(track_new_mem_stack) (drd_start_using_mem_stack); |
| 1196 | VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal); |
| 1197 | VG_(track_new_mem_startup) (drd_start_using_mem_w_perms); |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 1198 | VG_(track_die_mem_brk) (drd_stop_using_nonstack_mem); |
| 1199 | VG_(track_die_mem_munmap) (drd_stop_using_nonstack_mem); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1200 | VG_(track_die_mem_stack) (drd_stop_using_mem_stack); |
| 1201 | VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal); |
| 1202 | VG_(track_start_client_code) (drd_start_client_code); |
| 1203 | VG_(track_pre_thread_ll_create) (drd_pre_thread_create); |
| 1204 | VG_(track_pre_thread_first_insn)(drd_post_thread_create); |
| 1205 | VG_(track_pre_thread_ll_exit) (drd_thread_finished); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1206 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1207 | // Other stuff. |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1208 | drd_register_malloc_wrappers(drd_start_using_mem_w_ecu, |
bart | 0ffa483 | 2008-04-05 12:57:01 +0000 | [diff] [blame] | 1209 | drd_stop_using_nonstack_mem); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1210 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1211 | drd_clientreq_init(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1212 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1213 | drd_suppression_init(); |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 1214 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1215 | clientobj_init(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init) |