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