sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 26 | #include "drd_barrier.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 27 | #include "drd_clientreq.h" |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 28 | #include "drd_clientobj.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 29 | #include "drd_cond.h" |
| 30 | #include "drd_error.h" |
| 31 | #include "drd_malloc_wrappers.h" |
| 32 | #include "drd_mutex.h" |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 33 | #include "drd_rwlock.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 34 | #include "drd_segment.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 35 | #include "drd_semaphore.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | #include "drd_suppression.h" |
| 37 | #include "drd_thread.h" |
| 38 | #include "drd_track.h" |
| 39 | #include "drd_vc.h" |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 40 | #include "priv_drd_clientreq.h" |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 41 | #include "pub_drd_bitmap.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 42 | #include "pub_tool_basics.h" |
| 43 | #include "pub_tool_debuginfo.h" // VG_(describe_IP)() |
| 44 | #include "pub_tool_libcassert.h" // tl_assert() |
| 45 | #include "pub_tool_libcbase.h" // VG_(strcmp) |
| 46 | #include "pub_tool_libcprint.h" // VG_(printf) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 47 | #include "pub_tool_vki.h" // Must be included before pub_tool_libcproc |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | #include "pub_tool_libcproc.h" |
| 49 | #include "pub_tool_machine.h" |
| 50 | #include "pub_tool_options.h" // command line options |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 51 | #include "pub_tool_replacemalloc.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 52 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 53 | #include "pub_tool_tooliface.h" |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 54 | #include "pub_tool_replacemalloc.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | |
| 56 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 57 | // Function declarations. |
| 58 | |
| 59 | static void drd_start_client_code(const ThreadId tid, const ULong bbs_done); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | // Local variables. |
| 63 | |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame^] | 64 | static Bool drd_print_stats = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | static Bool drd_trace_fork_join = False; |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame^] | 66 | static Bool drd_trace_mem = False; |
| 67 | static Addr drd_trace_address = 0; |
| 68 | static Bool s_drd_var_info = False; |
| 69 | static Bool s_show_stack_usage = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | // |
| 73 | // Implement the needs_command_line_options for drd. |
| 74 | // |
| 75 | |
| 76 | static Bool drd_process_cmd_line_option(Char* arg) |
| 77 | { |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 78 | int segment_merging = -1; |
| 79 | int show_confl_seg = -1; |
| 80 | int trace_barrier = -1; |
| 81 | int trace_clientobj = -1; |
| 82 | int trace_cond = -1; |
| 83 | int trace_csw = -1; |
| 84 | int trace_danger_set = -1; |
| 85 | int trace_mutex = -1; |
| 86 | int trace_rwlock = -1; |
| 87 | int trace_segment = -1; |
| 88 | int trace_semaphore = -1; |
| 89 | int trace_suppression = -1; |
| 90 | Char* trace_address = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 91 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 92 | VG_BOOL_CLO (arg, "--drd-stats", drd_print_stats) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 93 | else VG_BOOL_CLO(arg, "--segment-merging", segment_merging) |
bart | 16d76e5 | 2008-03-18 17:08:08 +0000 | [diff] [blame] | 94 | else VG_BOOL_CLO(arg, "--show-confl-seg", show_confl_seg) |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame^] | 95 | else VG_BOOL_CLO(arg, "--show-stack-usage", s_show_stack_usage) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 96 | else VG_BOOL_CLO(arg, "--trace-barrier", trace_barrier) |
| 97 | else VG_BOOL_CLO(arg, "--trace-clientobj", trace_clientobj) |
| 98 | else VG_BOOL_CLO(arg, "--trace-cond", trace_cond) |
| 99 | else VG_BOOL_CLO(arg, "--trace-csw", trace_csw) |
| 100 | else VG_BOOL_CLO(arg, "--trace-danger-set", trace_danger_set) |
| 101 | else VG_BOOL_CLO(arg, "--trace-fork-join", drd_trace_fork_join) |
| 102 | else VG_BOOL_CLO(arg, "--trace-mem", drd_trace_mem) |
| 103 | else VG_BOOL_CLO(arg, "--trace-mutex", trace_mutex) |
| 104 | else VG_BOOL_CLO(arg, "--trace-rwlock", trace_rwlock) |
| 105 | else VG_BOOL_CLO(arg, "--trace-segment", trace_segment) |
| 106 | else VG_BOOL_CLO(arg, "--trace-semaphore", trace_semaphore) |
| 107 | else VG_BOOL_CLO(arg, "--trace-suppression", trace_suppression) |
bart | 95761b5 | 2008-03-29 08:34:03 +0000 | [diff] [blame] | 108 | else VG_BOOL_CLO(arg, "--var-info", s_drd_var_info) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 109 | else VG_STR_CLO (arg, "--trace-address", trace_address) |
| 110 | else |
bart | ceded21 | 2008-03-26 17:39:52 +0000 | [diff] [blame] | 111 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 112 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 113 | if (segment_merging != -1) |
| 114 | thread_set_segment_merging(segment_merging); |
| 115 | if (show_confl_seg != -1) |
bart | 16d76e5 | 2008-03-18 17:08:08 +0000 | [diff] [blame] | 116 | set_show_conflicting_segments(show_confl_seg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 117 | if (trace_address) |
| 118 | { |
| 119 | drd_trace_address = VG_(strtoll16)(trace_address, 0); |
| 120 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 121 | if (trace_barrier != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 122 | barrier_set_trace(trace_barrier); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 123 | if (trace_clientobj != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 124 | clientobj_set_trace(trace_clientobj); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 125 | if (trace_cond != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 126 | cond_set_trace(trace_cond); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 127 | if (trace_csw != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 128 | thread_trace_context_switches(trace_csw); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 129 | if (trace_danger_set != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 130 | thread_trace_danger_set(trace_danger_set); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 131 | if (trace_mutex != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 132 | mutex_set_trace(trace_mutex); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 133 | if (trace_rwlock != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 134 | rwlock_set_trace(trace_rwlock); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 135 | if (trace_segment != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 136 | sg_set_trace(trace_segment); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 137 | if (trace_semaphore != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 138 | semaphore_set_trace(trace_semaphore); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 139 | if (trace_suppression != -1) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 140 | suppression_set_trace(trace_suppression); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 141 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 142 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void drd_print_usage(void) |
| 146 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 147 | VG_(printf)(" --trace-mem=no|yes Trace all memory accesses to stdout[no]" |
| 148 | "\n" |
| 149 | " --trace-fork-join=no|yes Trace all thread creation and join" |
| 150 | " activity\n" |
| 151 | " --trace-mutex=no|yes Trace all mutex activity\n" |
| 152 | " --trace-segment=no|yes Trace segment actions\n" |
| 153 | ); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void drd_print_debug_usage(void) |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | |
| 161 | // |
| 162 | // Implements the thread-related core callbacks. |
| 163 | // |
| 164 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 165 | static void drd_trace_mem_access(const Addr addr, const SizeT size, |
| 166 | const BmAccessTypeT access_type) |
| 167 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 168 | char vc[80]; |
| 169 | vc_snprint(vc, sizeof(vc), thread_get_vc(thread_get_running_tid())); |
| 170 | VG_(message)(Vg_UserMsg, |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 171 | "%s 0x%lx size %ld (vg %d / drd %d / vc %s)", |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 172 | access_type == eLoad |
| 173 | ? "load " |
| 174 | : access_type == eStore |
| 175 | ? "store" |
| 176 | : access_type == eStart |
| 177 | ? "start" |
| 178 | : access_type == eEnd |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 179 | ? "end " |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 180 | : "????", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 181 | addr, |
| 182 | size, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 183 | VG_(get_running_tid)(), |
| 184 | thread_get_running_tid(), |
| 185 | vc); |
| 186 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 187 | VG_(clo_backtrace_size)); |
| 188 | tl_assert(DrdThreadIdToVgThreadId(thread_get_running_tid()) |
| 189 | == VG_(get_running_tid)()); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void drd_report_race(const Addr addr, const SizeT size, |
| 193 | const BmAccessTypeT access_type) |
| 194 | { |
bart | 49c3a11 | 2008-03-15 10:28:36 +0000 | [diff] [blame] | 195 | DataRaceErrInfo drei; |
| 196 | |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 197 | if (drd_is_suppressed(addr, addr + size)) |
| 198 | return; |
| 199 | |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 200 | drei.tid = thread_get_running_tid(); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 201 | drei.addr = addr; |
| 202 | drei.size = size; |
| 203 | drei.access_type = access_type; |
| 204 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 205 | DataRaceErr, |
| 206 | VG_(get_IP)(VG_(get_running_tid)()), |
| 207 | "Conflicting accesses", |
| 208 | &drei); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static VG_REGPARM(2) void drd_trace_load(Addr addr, SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 212 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 213 | Segment* sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 214 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 215 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 216 | /* The assert below has been commented out because of performance reasons.*/ |
| 217 | tl_assert(thread_get_running_tid() |
| 218 | == VgThreadIdToDrdThreadId(VG_(get_running_tid()))); |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 219 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 220 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 221 | if (! running_thread_is_recording()) |
| 222 | return; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 223 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 224 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 225 | { |
| 226 | drd_trace_mem_access(addr, size, eLoad); |
| 227 | } |
| 228 | sg = running_thread_get_segment(); |
| 229 | bm_access_range_load(sg->bm, addr, addr + size); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 230 | if (bm_load_has_conflict_with(thread_get_danger_set(), addr, addr + size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 231 | { |
| 232 | drd_report_race(addr, size, eLoad); |
| 233 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static VG_REGPARM(1) void drd_trace_load_1(Addr addr) |
| 237 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 238 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 239 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 240 | if (! running_thread_is_recording()) |
| 241 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 242 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 243 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 244 | { |
| 245 | drd_trace_mem_access(addr, 1, eLoad); |
| 246 | } |
| 247 | sg = running_thread_get_segment(); |
| 248 | bm_access_load_1(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 249 | if (bm_load_1_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 250 | { |
| 251 | drd_report_race(addr, 1, eLoad); |
| 252 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static VG_REGPARM(1) void drd_trace_load_2(Addr addr) |
| 256 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 257 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 258 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 259 | if (! running_thread_is_recording()) |
| 260 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 261 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 262 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 263 | { |
| 264 | drd_trace_mem_access(addr, 2, eLoad); |
| 265 | } |
| 266 | sg = running_thread_get_segment(); |
| 267 | bm_access_load_2(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 268 | if (bm_load_2_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 269 | { |
| 270 | drd_report_race(addr, 2, eLoad); |
| 271 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static VG_REGPARM(1) void drd_trace_load_4(Addr addr) |
| 275 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 276 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 277 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 278 | if (! running_thread_is_recording()) |
| 279 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 280 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 281 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 282 | { |
| 283 | drd_trace_mem_access(addr, 4, eLoad); |
| 284 | } |
| 285 | sg = running_thread_get_segment(); |
| 286 | bm_access_load_4(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 287 | if (bm_load_4_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 288 | { |
| 289 | drd_report_race(addr, 4, eLoad); |
| 290 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static VG_REGPARM(1) void drd_trace_load_8(Addr addr) |
| 294 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 295 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 296 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 297 | if (! running_thread_is_recording()) |
| 298 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 299 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 300 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 301 | { |
| 302 | drd_trace_mem_access(addr, 8, eLoad); |
| 303 | } |
| 304 | sg = running_thread_get_segment(); |
| 305 | bm_access_load_8(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 306 | if (bm_load_8_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 307 | { |
| 308 | drd_report_race(addr, 8, eLoad); |
| 309 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static |
| 313 | VG_REGPARM(2) void drd_trace_store(Addr addr, SizeT size) |
| 314 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 315 | Segment* sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 316 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 317 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 318 | /* The assert below has been commented out because of performance reasons.*/ |
| 319 | tl_assert(thread_get_running_tid() |
| 320 | == VgThreadIdToDrdThreadId(VG_(get_running_tid()))); |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 321 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 323 | if (! running_thread_is_recording()) |
| 324 | return; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 325 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 326 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 327 | { |
| 328 | drd_trace_mem_access(addr, size, eStore); |
| 329 | } |
| 330 | sg = running_thread_get_segment(); |
| 331 | bm_access_range_store(sg->bm, addr, addr + size); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 332 | if (bm_store_has_conflict_with(thread_get_danger_set(), addr, addr + size)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 333 | { |
| 334 | drd_report_race(addr, size, eStore); |
| 335 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static VG_REGPARM(1) void drd_trace_store_1(Addr addr) |
| 339 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 340 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 341 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 342 | if (! running_thread_is_recording()) |
| 343 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 344 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 345 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 346 | { |
| 347 | drd_trace_mem_access(addr, 1, eStore); |
| 348 | } |
| 349 | sg = running_thread_get_segment(); |
| 350 | bm_access_store_1(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 351 | if (bm_store_1_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 352 | { |
| 353 | drd_report_race(addr, 1, eStore); |
| 354 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static VG_REGPARM(1) void drd_trace_store_2(Addr addr) |
| 358 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 359 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 360 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 361 | if (! running_thread_is_recording()) |
| 362 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 363 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 364 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 365 | { |
| 366 | drd_trace_mem_access(addr, 2, eStore); |
| 367 | } |
| 368 | sg = running_thread_get_segment(); |
| 369 | bm_access_store_2(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 370 | if (bm_store_2_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 371 | { |
| 372 | drd_report_race(addr, 2, eStore); |
| 373 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static VG_REGPARM(1) void drd_trace_store_4(Addr addr) |
| 377 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 378 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 379 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 380 | if (! running_thread_is_recording()) |
| 381 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 382 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 383 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 384 | { |
| 385 | drd_trace_mem_access(addr, 4, eStore); |
| 386 | } |
| 387 | sg = running_thread_get_segment(); |
| 388 | bm_access_store_4(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 389 | if (bm_store_4_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 390 | { |
| 391 | drd_report_race(addr, 4, eStore); |
| 392 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static VG_REGPARM(1) void drd_trace_store_8(Addr addr) |
| 396 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 397 | Segment* sg; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 398 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 399 | if (! running_thread_is_recording()) |
| 400 | return; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 401 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 402 | if (drd_trace_mem || (addr == drd_trace_address)) |
| 403 | { |
| 404 | drd_trace_mem_access(addr, 8, eStore); |
| 405 | } |
| 406 | sg = running_thread_get_segment(); |
| 407 | bm_access_store_8(sg->bm, addr); |
bart | 7a28214 | 2008-03-15 08:34:23 +0000 | [diff] [blame] | 408 | if (bm_store_8_has_conflict_with(thread_get_danger_set(), addr)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 409 | { |
| 410 | drd_report_race(addr, 8, eStore); |
| 411 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static void drd_pre_mem_read(const CorePart part, |
| 415 | const ThreadId tid, |
| 416 | Char* const s, |
| 417 | const Addr a, |
| 418 | const SizeT size) |
| 419 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 420 | if (size > 0) |
| 421 | { |
| 422 | drd_trace_load(a, size); |
| 423 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 424 | } |
| 425 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 426 | static void drd_pre_mem_read_asciiz(const CorePart part, |
| 427 | const ThreadId tid, |
| 428 | Char* const s, |
| 429 | const Addr a) |
| 430 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 431 | const char* p = (void*)a; |
| 432 | SizeT size = 0; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 433 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 434 | /* Note: the expression '*p' reads client memory and may crash if the */ |
| 435 | /* client provided an invalid pointer ! */ |
| 436 | while (*p) |
| 437 | { |
| 438 | p++; |
| 439 | size++; |
| 440 | } |
| 441 | // To do: find out what a reasonable upper limit on 'size' is. |
| 442 | tl_assert(size < 4096); |
| 443 | if (size > 0) |
| 444 | { |
| 445 | drd_trace_load(a, size); |
| 446 | } |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 447 | } |
| 448 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 449 | static void drd_post_mem_write(const CorePart part, |
| 450 | const ThreadId tid, |
| 451 | const Addr a, |
| 452 | const SizeT size) |
| 453 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 454 | thread_set_vg_running_tid(VG_(get_running_tid)()); |
| 455 | if (size > 0) |
| 456 | { |
| 457 | drd_trace_store(a, size); |
| 458 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 459 | } |
| 460 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 461 | static void drd_start_using_mem(const Addr a1, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 462 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 463 | const Addr a2 = a1 + len; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 464 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 465 | tl_assert(a1 < a2); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 466 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 467 | if (a1 <= drd_trace_address && drd_trace_address < a2) |
| 468 | { |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 469 | drd_trace_mem_access(a1, len, eStart); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 470 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 471 | } |
| 472 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 473 | static void drd_stop_using_mem(const Addr a1, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 474 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 475 | const Addr a2 = a1 + len; |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 476 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 477 | tl_assert(a1 < a2); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 478 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 479 | if (a1 <= drd_trace_address && drd_trace_address < a2) |
| 480 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 481 | drd_trace_mem_access(a1, len, eEnd); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 482 | } |
| 483 | thread_stop_using_mem(a1, a2); |
| 484 | clientobj_stop_using_mem(a1, a2); |
| 485 | drd_suppression_stop_using_mem(a1, a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 486 | } |
| 487 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 488 | static |
| 489 | void drd_start_using_mem_w_perms(const Addr a, const SizeT len, |
| 490 | const Bool rr, const Bool ww, const Bool xx) |
| 491 | { |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 492 | thread_set_vg_running_tid(VG_(get_running_tid)()); |
| 493 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 494 | drd_start_using_mem(a, len); |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 495 | } |
| 496 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 497 | /* Called by the core when the stack of a thread grows, to indicate that */ |
| 498 | /* the addresses in range [ a, a + len [ may now be used by the client. */ |
| 499 | /* Assumption: stacks grow downward. */ |
| 500 | static void drd_start_using_mem_stack(const Addr a, const SizeT len) |
| 501 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 502 | thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB); |
| 503 | drd_start_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* Called by the core when the stack of a thread shrinks, to indicate that */ |
| 507 | /* the addresses [ a, a + len [ are no longer accessible for the client. */ |
| 508 | /* Assumption: stacks grow downward. */ |
| 509 | static void drd_stop_using_mem_stack(const Addr a, const SizeT len) |
| 510 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 511 | thread_set_stack_min(thread_get_running_tid(), |
| 512 | a + len - VG_STACK_REDZONE_SZB); |
| 513 | drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 514 | } |
| 515 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 516 | static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 517 | { |
bart | d576591 | 2008-03-16 08:40:55 +0000 | [diff] [blame] | 518 | thread_set_vg_running_tid(VG_(get_running_tid)()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 519 | drd_start_using_mem(a, len); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 520 | } |
| 521 | |
bart | 5e85d26 | 2008-03-01 10:49:37 +0000 | [diff] [blame] | 522 | static void drd_stop_using_mem_stack_signal(Addr a, SizeT len) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 523 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 524 | drd_stop_using_mem(a, len); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static |
| 528 | void drd_pre_thread_create(const ThreadId creator, const ThreadId created) |
| 529 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 530 | const DrdThreadId drd_creator = VgThreadIdToDrdThreadId(creator); |
| 531 | tl_assert(created != VG_INVALID_THREADID); |
| 532 | thread_pre_create(drd_creator, created); |
| 533 | if (IsValidDrdThreadId(drd_creator)) |
| 534 | { |
| 535 | thread_new_segment(drd_creator); |
| 536 | } |
| 537 | if (drd_trace_fork_join) |
| 538 | { |
| 539 | VG_(message)(Vg_DebugMsg, |
| 540 | "drd_pre_thread_create creator = %d/%d, created = %d", |
| 541 | creator, drd_creator, created); |
| 542 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* Called by Valgrind's core before any loads or stores are performed on */ |
| 546 | /* the context of thread "created". At startup, this function is called */ |
| 547 | /* with arguments (0,1). */ |
| 548 | static |
| 549 | void drd_post_thread_create(const ThreadId created) |
| 550 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 551 | const DrdThreadId drd_created = thread_post_create(created); |
| 552 | tl_assert(created != VG_INVALID_THREADID); |
| 553 | if (drd_trace_fork_join) |
| 554 | { |
| 555 | VG_(message)(Vg_DebugMsg, |
| 556 | "drd_post_thread_create created = %d/%d", |
| 557 | created, drd_created); |
| 558 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just */ |
| 562 | /* after thread drd_joiner joined thread drd_joinee. */ |
| 563 | void drd_post_thread_join(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 564 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 565 | tl_assert(IsValidDrdThreadId(drd_joiner)); |
| 566 | tl_assert(IsValidDrdThreadId(drd_joinee)); |
| 567 | thread_new_segment(drd_joinee); |
| 568 | thread_combine_vc(drd_joiner, drd_joinee); |
| 569 | thread_new_segment(drd_joiner); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 570 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 571 | if (drd_trace_fork_join) |
| 572 | { |
| 573 | char msg[256]; |
| 574 | const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner); |
| 575 | const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee); |
| 576 | VG_(snprintf)(msg, sizeof(msg), |
| 577 | "drd_post_thread_join joiner = %d/%d, joinee = %d/%d", |
| 578 | joiner, drd_joiner, joinee, drd_joinee); |
| 579 | if (joiner) |
| 580 | { |
| 581 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 582 | ", new vc: "); |
| 583 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 584 | thread_get_vc(drd_joiner)); |
| 585 | } |
| 586 | VG_(message)(Vg_DebugMsg, msg); |
| 587 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 588 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 589 | thread_delete(drd_joinee); |
| 590 | mutex_thread_delete(drd_joinee); |
| 591 | cond_thread_delete(drd_joinee); |
| 592 | semaphore_thread_delete(drd_joinee); |
| 593 | barrier_thread_delete(drd_joinee); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 594 | } |
| 595 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 596 | void drd_trace_addr(const Addr addr) |
| 597 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 598 | drd_trace_address = addr; |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 599 | } |
| 600 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 601 | /* Called after a thread has performed its last memory access. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 602 | static void drd_thread_finished(ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 603 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 604 | DrdThreadId drd_tid; |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 605 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 606 | tl_assert(VG_(get_running_tid)() == vg_tid); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 607 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 608 | drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 609 | if (drd_trace_fork_join) |
| 610 | { |
| 611 | VG_(message)(Vg_DebugMsg, |
| 612 | "drd_thread_finished tid = %d/%d%s", |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 613 | vg_tid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 614 | drd_tid, |
| 615 | thread_get_joinable(drd_tid) |
| 616 | ? "" |
| 617 | : " (which is a detached thread)"); |
bart | 912ab8d | 2008-03-29 09:31:43 +0000 | [diff] [blame^] | 618 | } |
| 619 | if (s_show_stack_usage) |
| 620 | { |
| 621 | const SizeT stack_size = thread_get_stack_size(drd_tid); |
| 622 | const SizeT used_stack |
| 623 | = thread_get_stack_max(drd_tid) - thread_get_stack_min_min(drd_tid); |
| 624 | VG_(message)(Vg_UserMsg, |
| 625 | "thread %d/%d%s finished and used %ld bytes out of %ld" |
| 626 | " on its stack. Margin: %ld bytes.", |
| 627 | vg_tid, |
| 628 | drd_tid, |
| 629 | thread_get_joinable(drd_tid) |
| 630 | ? "" |
| 631 | : " (which is a detached thread)", |
| 632 | used_stack, |
| 633 | stack_size, |
| 634 | stack_size - used_stack); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 635 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 636 | } |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 637 | drd_stop_using_mem(thread_get_stack_min(drd_tid), |
| 638 | thread_get_stack_max(drd_tid) |
| 639 | - thread_get_stack_min(drd_tid)); |
| 640 | thread_stop_recording(drd_tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 641 | thread_finished(drd_tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 642 | } |
| 643 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 644 | void drd_pre_mutex_init(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 645 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 646 | mutex_init(mutex, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 647 | } |
| 648 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 649 | void drd_post_mutex_destroy(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 650 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 651 | mutex_post_destroy(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 652 | } |
| 653 | |
bart | 2e3a3c1 | 2008-03-24 08:33:47 +0000 | [diff] [blame] | 654 | void drd_pre_mutex_lock(const Addr mutex, const MutexT mutex_type, |
| 655 | const Bool trylock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 656 | { |
bart | 2e3a3c1 | 2008-03-24 08:33:47 +0000 | [diff] [blame] | 657 | mutex_pre_lock(mutex, mutex_type, trylock); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 658 | } |
| 659 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 660 | void drd_post_mutex_lock(const Addr mutex, const Bool took_lock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 661 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 662 | mutex_post_lock(mutex, took_lock); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 663 | } |
| 664 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 665 | void drd_pre_mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 666 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 667 | mutex_unlock(mutex, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 668 | } |
| 669 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 670 | void drd_pre_cond_init(Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 671 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 672 | cond_pre_init(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 673 | } |
| 674 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 675 | void drd_post_cond_destroy(Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 676 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 677 | cond_post_destroy(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 678 | } |
| 679 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 680 | void drd_semaphore_init(const Addr semaphore, |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 681 | const Word pshared, const Word value) |
| 682 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 683 | semaphore_init(semaphore, pshared, value); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | void drd_semaphore_destroy(const Addr semaphore) |
| 687 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 688 | semaphore_destroy(semaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 689 | } |
| 690 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 691 | void drd_semaphore_pre_wait(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 692 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 693 | semaphore_pre_wait(semaphore); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void drd_semaphore_post_wait(const DrdThreadId tid, const Addr semaphore, |
| 697 | const Bool waited) |
| 698 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 699 | semaphore_post_wait(tid, semaphore, waited); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 700 | } |
| 701 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 702 | void drd_semaphore_pre_post(const DrdThreadId tid, const Addr semaphore) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 703 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 704 | semaphore_pre_post(tid, semaphore); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | void drd_semaphore_post_post(const DrdThreadId tid, const Addr semaphore, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 708 | const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 709 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 710 | semaphore_post_post(tid, semaphore, waited); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 714 | void drd_barrier_init(const Addr barrier, |
| 715 | const BarrierT barrier_type, const Word count, |
| 716 | const Bool reinitialization) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 717 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 718 | barrier_init(barrier, barrier_type, count, reinitialization); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 719 | } |
| 720 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 721 | void drd_barrier_destroy(const Addr barrier, const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 722 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 723 | barrier_destroy(barrier, barrier_type); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 724 | } |
| 725 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 726 | void drd_barrier_pre_wait(const DrdThreadId tid, const Addr barrier, |
| 727 | const BarrierT barrier_type) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 728 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 729 | barrier_pre_wait(tid, barrier, barrier_type); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | void drd_barrier_post_wait(const DrdThreadId tid, const Addr barrier, |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 733 | const BarrierT barrier_type, const Bool waited) |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 734 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 735 | barrier_post_wait(tid, barrier, barrier_type, waited); |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 736 | } |
| 737 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 738 | |
| 739 | // |
| 740 | // Implementation of the tool interface. |
| 741 | // |
| 742 | |
| 743 | static |
| 744 | void drd_post_clo_init(void) |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 745 | { |
| 746 | # if defined(VGP_x86_linux) || defined(VGP_amd64_linux) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 747 | /* fine */ |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 748 | # else |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 749 | 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] | 750 | # endif |
bart | 95761b5 | 2008-03-29 08:34:03 +0000 | [diff] [blame] | 751 | |
| 752 | if (s_drd_var_info) |
| 753 | { |
| 754 | VG_(needs_var_info)(); |
| 755 | } |
sewardj | dcbb8d3 | 2007-11-26 21:34:30 +0000 | [diff] [blame] | 756 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 757 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 758 | static void instrument_load(IRSB* const bb, |
| 759 | IRExpr* const addr_expr, |
| 760 | const HWord size) |
| 761 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 762 | IRExpr* size_expr; |
| 763 | IRExpr** argv; |
| 764 | IRDirty* di; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 765 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 766 | switch (size) |
| 767 | { |
| 768 | case 1: |
| 769 | argv = mkIRExprVec_1(addr_expr); |
| 770 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 771 | "drd_trace_load_1", |
| 772 | VG_(fnptr_to_fnentry)(drd_trace_load_1), |
| 773 | argv); |
| 774 | break; |
| 775 | case 2: |
| 776 | argv = mkIRExprVec_1(addr_expr); |
| 777 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 778 | "drd_trace_load_2", |
| 779 | VG_(fnptr_to_fnentry)(drd_trace_load_2), |
| 780 | argv); |
| 781 | break; |
| 782 | case 4: |
| 783 | argv = mkIRExprVec_1(addr_expr); |
| 784 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 785 | "drd_trace_load_4", |
| 786 | VG_(fnptr_to_fnentry)(drd_trace_load_4), |
| 787 | argv); |
| 788 | break; |
| 789 | case 8: |
| 790 | argv = mkIRExprVec_1(addr_expr); |
| 791 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 792 | "drd_trace_load_8", |
| 793 | VG_(fnptr_to_fnentry)(drd_trace_load_8), |
| 794 | argv); |
| 795 | break; |
| 796 | default: |
| 797 | size_expr = mkIRExpr_HWord(size); |
| 798 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 799 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 800 | "drd_trace_load", |
| 801 | VG_(fnptr_to_fnentry)(drd_trace_load), |
| 802 | argv); |
| 803 | break; |
| 804 | } |
| 805 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static void instrument_store(IRSB* const bb, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 809 | IRExpr* const addr_expr, |
| 810 | const HWord size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 811 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 812 | IRExpr* size_expr; |
| 813 | IRExpr** argv; |
| 814 | IRDirty* di; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 815 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 816 | switch (size) |
| 817 | { |
| 818 | case 1: |
| 819 | argv = mkIRExprVec_1(addr_expr); |
| 820 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 821 | "drd_trace_store_1", |
| 822 | VG_(fnptr_to_fnentry)(drd_trace_store_1), |
| 823 | argv); |
| 824 | break; |
| 825 | case 2: |
| 826 | argv = mkIRExprVec_1(addr_expr); |
| 827 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 828 | "drd_trace_store_2", |
| 829 | VG_(fnptr_to_fnentry)(drd_trace_store_2), |
| 830 | argv); |
| 831 | break; |
| 832 | case 4: |
| 833 | argv = mkIRExprVec_1(addr_expr); |
| 834 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 835 | "drd_trace_store_4", |
| 836 | VG_(fnptr_to_fnentry)(drd_trace_store_4), |
| 837 | argv); |
| 838 | break; |
| 839 | case 8: |
| 840 | argv = mkIRExprVec_1(addr_expr); |
| 841 | di = unsafeIRDirty_0_N(/*regparms*/1, |
| 842 | "drd_trace_store_8", |
| 843 | VG_(fnptr_to_fnentry)(drd_trace_store_8), |
| 844 | argv); |
| 845 | break; |
| 846 | default: |
| 847 | size_expr = mkIRExpr_HWord(size); |
| 848 | argv = mkIRExprVec_2(addr_expr, size_expr); |
| 849 | di = unsafeIRDirty_0_N(/*regparms*/2, |
| 850 | "drd_trace_store", |
| 851 | VG_(fnptr_to_fnentry)(drd_trace_store), |
| 852 | argv); |
| 853 | break; |
| 854 | } |
| 855 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 856 | } |
| 857 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 858 | static |
| 859 | IRSB* drd_instrument(VgCallbackClosure* const closure, |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 860 | IRSB* const bb_in, |
| 861 | VexGuestLayout* const layout, |
| 862 | VexGuestExtents* const vge, |
| 863 | IRType const gWordTy, |
| 864 | IRType const hWordTy) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 865 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 866 | IRDirty* di; |
| 867 | Int i; |
| 868 | IRSB* bb; |
| 869 | IRExpr** argv; |
| 870 | Bool instrument = True; |
| 871 | Bool bus_locked = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 872 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 873 | /* Set up BB */ |
| 874 | bb = emptyIRSB(); |
| 875 | bb->tyenv = deepCopyIRTypeEnv(bb_in->tyenv); |
| 876 | bb->next = deepCopyIRExpr(bb_in->next); |
| 877 | bb->jumpkind = bb_in->jumpkind; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 878 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 879 | for (i = 0; i < bb_in->stmts_used; i++) |
| 880 | { |
| 881 | IRStmt* const st = bb_in->stmts[i]; |
| 882 | tl_assert(st); |
| 883 | if (st->tag == Ist_NoOp) |
| 884 | continue; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 885 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 886 | switch (st->tag) |
| 887 | { |
| 888 | case Ist_IMark: |
| 889 | instrument = VG_(seginfo_sect_kind)(NULL, 0, st->Ist.IMark.addr) |
| 890 | != Vg_SectPLT; |
| 891 | break; |
| 892 | |
| 893 | case Ist_MBE: |
| 894 | switch (st->Ist.MBE.event) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 895 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 896 | case Imbe_Fence: |
| 897 | break; /* not interesting */ |
| 898 | case Imbe_BusLock: |
| 899 | tl_assert(! bus_locked); |
| 900 | bus_locked = True; |
| 901 | break; |
| 902 | case Imbe_BusUnlock: |
| 903 | tl_assert(bus_locked); |
| 904 | bus_locked = False; |
| 905 | break; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 906 | default: |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 907 | tl_assert(0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 908 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 909 | addStmtToIRSB(bb, st); |
| 910 | break; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 911 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 912 | case Ist_Store: |
| 913 | if (instrument && ! bus_locked) |
| 914 | { |
| 915 | instrument_store(bb, |
| 916 | st->Ist.Store.addr, |
| 917 | sizeofIRType(typeOfIRExpr(bb->tyenv, |
| 918 | st->Ist.Store.data))); |
| 919 | } |
| 920 | addStmtToIRSB(bb, st); |
| 921 | break; |
bart | a47b351 | 2008-03-07 17:22:26 +0000 | [diff] [blame] | 922 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 923 | case Ist_WrTmp: |
| 924 | if (instrument) |
| 925 | { |
| 926 | const IRExpr* const data = st->Ist.WrTmp.data; |
| 927 | if (data->tag == Iex_Load) |
| 928 | { |
| 929 | instrument_load(bb, |
| 930 | data->Iex.Load.addr, |
| 931 | sizeofIRType(data->Iex.Load.ty)); |
| 932 | } |
| 933 | } |
| 934 | addStmtToIRSB(bb, st); |
| 935 | break; |
| 936 | |
| 937 | case Ist_Dirty: |
| 938 | if (instrument) |
| 939 | { |
| 940 | IRDirty* d = st->Ist.Dirty.details; |
| 941 | IREffect const mFx = d->mFx; |
| 942 | switch (mFx) { |
| 943 | case Ifx_None: |
| 944 | break; |
| 945 | case Ifx_Read: |
| 946 | case Ifx_Write: |
| 947 | case Ifx_Modify: |
| 948 | tl_assert(d->mAddr); |
| 949 | tl_assert(d->mSize > 0); |
| 950 | argv = mkIRExprVec_2(d->mAddr, mkIRExpr_HWord(d->mSize)); |
| 951 | if (mFx == Ifx_Read || mFx == Ifx_Modify) { |
| 952 | di = unsafeIRDirty_0_N( |
| 953 | /*regparms*/2, |
| 954 | "drd_trace_load", |
| 955 | VG_(fnptr_to_fnentry)(drd_trace_load), |
| 956 | argv); |
| 957 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 958 | } |
| 959 | if ((mFx == Ifx_Write || mFx == Ifx_Modify) |
| 960 | && ! bus_locked) |
| 961 | { |
| 962 | di = unsafeIRDirty_0_N( |
| 963 | /*regparms*/2, |
| 964 | "drd_trace_store", |
| 965 | VG_(fnptr_to_fnentry)(drd_trace_store), |
| 966 | argv); |
| 967 | addStmtToIRSB(bb, IRStmt_Dirty(di)); |
| 968 | } |
| 969 | break; |
| 970 | default: |
| 971 | tl_assert(0); |
| 972 | } |
| 973 | } |
| 974 | addStmtToIRSB(bb, st); |
| 975 | break; |
| 976 | |
| 977 | default: |
| 978 | addStmtToIRSB(bb, st); |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | tl_assert(! bus_locked); |
| 984 | |
| 985 | return bb; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 986 | } |
| 987 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 988 | static void drd_start_client_code(const ThreadId tid, const ULong bbs_done) |
| 989 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 990 | tl_assert(tid == VG_(get_running_tid)()); |
| 991 | thread_set_vg_running_tid(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static |
| 995 | void drd_fini(Int exitcode) |
| 996 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 997 | // thread_print_all(); |
| 998 | if (VG_(clo_verbosity) > 1 || drd_print_stats) |
| 999 | { |
| 1000 | VG_(message)(Vg_DebugMsg, |
| 1001 | " thread: %lld context switches" |
| 1002 | " / %lld updates of the danger set", |
| 1003 | thread_get_context_switch_count(), |
| 1004 | thread_get_update_danger_set_count()); |
| 1005 | VG_(message)(Vg_DebugMsg, |
| 1006 | " segments: %lld total, %lld max, %lld discard points", |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 1007 | sg_get_created_segments_count(), |
| 1008 | sg_get_max_alive_segments_count(), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1009 | thread_get_discard_ordered_segments_count()); |
| 1010 | VG_(message)(Vg_DebugMsg, |
| 1011 | " bitmaps: %lld / %lld bitmaps were allocated" |
| 1012 | " and %lld / %lld for danger set updates", |
| 1013 | bm_get_bitmap_creation_count(), |
| 1014 | bm_get_bitmap2_creation_count(), |
| 1015 | thread_get_danger_set_bitmap_creation_count(), |
| 1016 | thread_get_danger_set_bitmap2_creation_count()); |
| 1017 | VG_(message)(Vg_DebugMsg, |
| 1018 | " mutex: %lld non-recursive lock/unlock events", |
| 1019 | get_mutex_lock_count()); |
| 1020 | drd_print_malloc_stats(); |
| 1021 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1024 | static |
| 1025 | void drd_pre_clo_init(void) |
| 1026 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1027 | // Basic tool stuff. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1028 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1029 | VG_(details_name) ("exp-drd"); |
| 1030 | VG_(details_version) (NULL); |
| 1031 | VG_(details_description) ("a data race detector"); |
| 1032 | VG_(details_copyright_author)("Copyright (C) 2006-2008, and GNU GPL'd," |
| 1033 | " by Bart Van Assche."); |
| 1034 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1035 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1036 | VG_(basic_tool_funcs) (drd_post_clo_init, |
| 1037 | drd_instrument, |
| 1038 | drd_fini); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1039 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1040 | // Command line stuff. |
| 1041 | VG_(needs_command_line_options)(drd_process_cmd_line_option, |
| 1042 | drd_print_usage, |
| 1043 | drd_print_debug_usage); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1044 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1045 | // Error handling. |
| 1046 | drd_register_error_handlers(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1047 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1048 | // Core event tracking. |
| 1049 | VG_(track_pre_mem_read) (drd_pre_mem_read); |
| 1050 | VG_(track_pre_mem_read_asciiz) (drd_pre_mem_read_asciiz); |
| 1051 | VG_(track_post_mem_write) (drd_post_mem_write); |
| 1052 | VG_(track_new_mem_brk) (drd_start_using_mem); |
| 1053 | VG_(track_new_mem_mmap) (drd_start_using_mem_w_perms); |
| 1054 | VG_(track_new_mem_stack) (drd_start_using_mem_stack); |
| 1055 | VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal); |
| 1056 | VG_(track_new_mem_startup) (drd_start_using_mem_w_perms); |
| 1057 | VG_(track_die_mem_brk) (drd_stop_using_mem); |
| 1058 | VG_(track_die_mem_munmap) (drd_stop_using_mem); |
| 1059 | VG_(track_die_mem_stack) (drd_stop_using_mem_stack); |
| 1060 | VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal); |
| 1061 | VG_(track_start_client_code) (drd_start_client_code); |
| 1062 | VG_(track_pre_thread_ll_create) (drd_pre_thread_create); |
| 1063 | VG_(track_pre_thread_first_insn)(drd_post_thread_create); |
| 1064 | VG_(track_pre_thread_ll_exit) (drd_thread_finished); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1065 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1066 | // Other stuff. |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1067 | drd_register_malloc_wrappers(drd_start_using_mem, drd_stop_using_mem); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1068 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1069 | drd_clientreq_init(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1070 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1071 | drd_suppression_init(); |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 1072 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1073 | clientobj_init(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init) |