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