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 | |
| 26 | #include "drd_error.h" |
| 27 | #include "drd_malloc_wrappers.h" |
| 28 | #include "drd_mutex.h" // struct mutex_info |
| 29 | #include "drd_suppression.h" // drd_start_suppression() |
| 30 | #include "pub_drd_bitmap.h" // LHS_W, ... |
| 31 | #include "pub_tool_vki.h" |
| 32 | #include "pub_tool_basics.h" |
| 33 | #include "pub_tool_libcassert.h" // tl_assert() |
| 34 | #include "pub_tool_libcbase.h" // strlen() |
| 35 | #include "pub_tool_libcfile.h" // VG_(get_startup_wd)() |
| 36 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 37 | #include "pub_tool_machine.h" |
bart | f993b87 | 2008-04-01 18:19:50 +0000 | [diff] [blame] | 38 | #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 39 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 40 | #include "pub_tool_tooliface.h" // VG_(needs_tool_errors)() |
| 41 | |
| 42 | |
bart | 16d76e5 | 2008-03-18 17:08:08 +0000 | [diff] [blame] | 43 | /* Local variables. */ |
| 44 | |
| 45 | static Bool s_drd_show_conflicting_segments = True; |
| 46 | |
| 47 | |
| 48 | void set_show_conflicting_segments(const Bool scs) |
| 49 | { |
| 50 | s_drd_show_conflicting_segments = scs; |
| 51 | } |
| 52 | |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 53 | /* Describe a data address range [a,a+len[ as good as possible, for error */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 54 | /* messages, putting the result in ai. */ |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 55 | static |
| 56 | void describe_malloced_addr(Addr const a, SizeT const len, AddrInfo* const ai) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 57 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 58 | Addr data; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 60 | if (drd_heap_addrinfo(a, &data, &ai->size, &ai->lastchange)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 61 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 62 | ai->akind = eMallocd; |
| 63 | ai->rwoffset = a - data; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 64 | } |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 65 | else |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 66 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 67 | ai->akind = eUnknown; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 68 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | } |
| 70 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 71 | static |
| 72 | void drd_report_data_race2(Error* const err, const DataRaceErrInfo* const dri) |
| 73 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 74 | AddrInfo ai; |
bart | f993b87 | 2008-04-01 18:19:50 +0000 | [diff] [blame] | 75 | const unsigned descr_size = 256; |
| 76 | Char* descr1 = VG_(malloc)(descr_size); |
| 77 | Char* descr2 = VG_(malloc)(descr_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 79 | tl_assert(dri); |
| 80 | tl_assert(dri->addr); |
| 81 | tl_assert(dri->size > 0); |
bart | fdd8d4e | 2008-04-01 18:38:29 +0000 | [diff] [blame] | 82 | tl_assert(descr1); |
| 83 | tl_assert(descr2); |
bart | b515eb1 | 2008-03-07 18:52:38 +0000 | [diff] [blame] | 84 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 85 | descr1[0] = 0; |
| 86 | descr2[0] = 0; |
bart | f993b87 | 2008-04-01 18:19:50 +0000 | [diff] [blame] | 87 | VG_(get_data_description)(descr1, descr2, descr_size, dri->addr); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 88 | if (descr1[0] == 0) |
| 89 | { |
bart | ff1252a | 2008-03-16 17:27:25 +0000 | [diff] [blame] | 90 | describe_malloced_addr(dri->addr, dri->size, &ai); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 91 | } |
| 92 | VG_(message)(Vg_UserMsg, |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 93 | "Conflicting %s by thread %d/%d at 0x%08lx size %ld", |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 94 | dri->access_type == eStore ? "store" : "load", |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 95 | DrdThreadIdToVgThreadId(dri->tid), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 96 | dri->tid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 97 | dri->addr, |
| 98 | dri->size); |
| 99 | VG_(pp_ExeContext)(VG_(get_error_where)(err)); |
| 100 | if (descr1[0]) |
| 101 | { |
| 102 | VG_(message)(Vg_UserMsg, "%s", descr1); |
| 103 | VG_(message)(Vg_UserMsg, "%s", descr2); |
| 104 | } |
| 105 | else if (ai.akind == eMallocd && ai.lastchange) |
| 106 | { |
| 107 | VG_(message)(Vg_UserMsg, |
| 108 | "Address 0x%lx is at offset %ld from 0x%lx." |
| 109 | " Allocation context:", |
| 110 | dri->addr, ai.rwoffset, dri->addr - ai.rwoffset); |
| 111 | VG_(pp_ExeContext)(ai.lastchange); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | VG_(message)(Vg_UserMsg, "Allocation context: unknown."); |
| 116 | } |
bart | 16d76e5 | 2008-03-18 17:08:08 +0000 | [diff] [blame] | 117 | if (s_drd_show_conflicting_segments) |
| 118 | { |
| 119 | thread_report_conflicting_segments(dri->tid, |
| 120 | dri->addr, dri->size, dri->access_type); |
| 121 | } |
bart | f993b87 | 2008-04-01 18:19:50 +0000 | [diff] [blame] | 122 | |
| 123 | VG_(free)(descr2); |
| 124 | VG_(free)(descr1); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2) |
| 128 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 129 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void drd_tool_error_pp(Error* const e) |
| 133 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 134 | switch (VG_(get_error_kind)(e)) |
| 135 | { |
| 136 | case DataRaceErr: { |
| 137 | drd_report_data_race2(e, VG_(get_error_extra)(e)); |
| 138 | break; |
| 139 | } |
| 140 | case MutexErr: { |
| 141 | MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e)); |
| 142 | tl_assert(p); |
bart | 6b71761 | 2008-03-24 09:29:38 +0000 | [diff] [blame] | 143 | if (p->recursion_count >= 0) |
| 144 | { |
| 145 | VG_(message)(Vg_UserMsg, |
| 146 | "%s: mutex 0x%lx, recursion count %d, owner %d.", |
| 147 | VG_(get_error_string)(e), |
| 148 | p->mutex, |
| 149 | p->recursion_count, |
| 150 | p->owner); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | VG_(message)(Vg_UserMsg, |
bart | 52e8291 | 2008-03-24 19:31:33 +0000 | [diff] [blame] | 155 | "The object at address 0x%lx is not a mutex.", |
bart | 6b71761 | 2008-03-24 09:29:38 +0000 | [diff] [blame] | 156 | p->mutex); |
| 157 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 158 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 159 | break; |
| 160 | } |
| 161 | case CondErr: { |
| 162 | CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e)); |
| 163 | VG_(message)(Vg_UserMsg, |
| 164 | "%s: cond 0x%lx", |
bart | be8a12c | 2008-03-17 18:36:55 +0000 | [diff] [blame] | 165 | VG_(get_error_string)(e), |
| 166 | cdei->cond); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 167 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 168 | break; |
| 169 | } |
| 170 | case CondRaceErr: { |
| 171 | CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e)); |
| 172 | VG_(message)(Vg_UserMsg, |
| 173 | "Race condition: condition variable 0x%lx has been" |
| 174 | " signalled but the associated mutex 0x%lx is not locked" |
| 175 | " by the signalling thread", |
| 176 | cei->cond, cei->mutex); |
| 177 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 178 | break; |
| 179 | } |
| 180 | case CondDestrErr: { |
| 181 | CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e)); |
| 182 | VG_(message)(Vg_UserMsg, |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 183 | "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d", |
bart | be8a12c | 2008-03-17 18:36:55 +0000 | [diff] [blame] | 184 | VG_(get_error_string)(e), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 185 | cdi->cond, cdi->mutex, |
| 186 | DrdThreadIdToVgThreadId(cdi->tid), cdi->tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 187 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 188 | break; |
| 189 | } |
| 190 | case SemaphoreErr: { |
| 191 | SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e)); |
| 192 | tl_assert(sei); |
| 193 | VG_(message)(Vg_UserMsg, |
| 194 | "%s: semaphore 0x%lx", |
| 195 | VG_(get_error_string)(e), |
| 196 | sei->semaphore); |
| 197 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 198 | break; |
| 199 | } |
| 200 | case BarrierErr: { |
| 201 | BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e)); |
| 202 | tl_assert(sei); |
| 203 | VG_(message)(Vg_UserMsg, |
| 204 | "%s: barrier 0x%lx", |
| 205 | VG_(get_error_string)(e), |
| 206 | sei->barrier); |
| 207 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 208 | break; |
| 209 | } |
| 210 | case RwlockErr: { |
| 211 | RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e)); |
| 212 | tl_assert(p); |
| 213 | VG_(message)(Vg_UserMsg, |
| 214 | "%s: rwlock 0x%lx.", |
| 215 | VG_(get_error_string)(e), |
| 216 | p->rwlock); |
| 217 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 218 | break; |
| 219 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame^] | 220 | case HoldtimeErr: { |
| 221 | HoldtimeErrInfo* p =(HoldtimeErrInfo*)(VG_(get_error_extra)(e)); |
| 222 | tl_assert(p); |
| 223 | tl_assert(p->acquired_at); |
| 224 | VG_(message)(Vg_UserMsg, "Acquired at:"); |
| 225 | VG_(pp_ExeContext)(p->acquired_at); |
| 226 | VG_(message)(Vg_UserMsg, |
| 227 | "Lock on %s 0x%lx was held during %d ms (threshold: %d ms).", |
| 228 | VG_(get_error_string)(e), |
| 229 | p->synchronization_object, |
| 230 | p->hold_time_ms, |
| 231 | p->threshold_ms); |
| 232 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 233 | break; |
| 234 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 235 | case GenericErr: { |
| 236 | //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e)); |
| 237 | VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e)); |
| 238 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 239 | break; |
| 240 | } |
| 241 | default: |
| 242 | VG_(message)(Vg_UserMsg, |
| 243 | "%s", |
| 244 | VG_(get_error_string)(e)); |
| 245 | VG_(pp_ExeContext)(VG_(get_error_where)(e)); |
| 246 | break; |
| 247 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static UInt drd_tool_error_update_extra(Error* e) |
| 251 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 252 | switch (VG_(get_error_kind)(e)) |
| 253 | { |
| 254 | case DataRaceErr: |
| 255 | return sizeof(DataRaceErrInfo); |
| 256 | case MutexErr: |
| 257 | return sizeof(MutexErrInfo); |
| 258 | case CondErr: |
| 259 | return sizeof(CondErrInfo); |
| 260 | case CondRaceErr: |
| 261 | return sizeof(CondRaceErrInfo); |
| 262 | case CondDestrErr: |
| 263 | return sizeof(CondDestrErrInfo); |
| 264 | case SemaphoreErr: |
| 265 | return sizeof(SemaphoreErrInfo); |
| 266 | case BarrierErr: |
| 267 | return sizeof(BarrierErrInfo); |
| 268 | case RwlockErr: |
| 269 | return sizeof(RwlockErrInfo); |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame^] | 270 | case HoldtimeErr: |
| 271 | return sizeof(HoldtimeErrInfo); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 272 | case GenericErr: |
| 273 | return sizeof(GenericErrInfo); |
| 274 | default: |
| 275 | tl_assert(False); |
| 276 | break; |
| 277 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static Bool drd_tool_error_recog(Char* const name, Supp* const supp) |
| 281 | { |
bart | 5fc70e6 | 2008-03-23 07:54:02 +0000 | [diff] [blame] | 282 | SuppKind skind = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 283 | |
bart | 5fc70e6 | 2008-03-23 07:54:02 +0000 | [diff] [blame] | 284 | if (VG_(strcmp)(name, STR_DataRaceErr) == 0) |
| 285 | ; |
| 286 | else if (VG_(strcmp)(name, STR_MutexErr) == 0) |
| 287 | ; |
| 288 | else if (VG_(strcmp)(name, STR_CondErr) == 0) |
| 289 | ; |
| 290 | else if (VG_(strcmp)(name, STR_CondRaceErr) == 0) |
| 291 | ; |
| 292 | else if (VG_(strcmp)(name, STR_CondDestrErr) == 0) |
| 293 | ; |
| 294 | else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0) |
| 295 | ; |
| 296 | else if (VG_(strcmp)(name, STR_BarrierErr) == 0) |
| 297 | ; |
| 298 | else if (VG_(strcmp)(name, STR_RwlockErr) == 0) |
| 299 | ; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame^] | 300 | else if (VG_(strcmp)(name, STR_HoldtimeErr) == 0) |
| 301 | ; |
bart | 5fc70e6 | 2008-03-23 07:54:02 +0000 | [diff] [blame] | 302 | else if (VG_(strcmp)(name, STR_GenericErr) == 0) |
| 303 | ; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 304 | else |
| 305 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 306 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 307 | VG_(set_supp_kind)(supp, skind); |
| 308 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp) |
| 312 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 313 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static Bool drd_tool_error_matches(Error* const e, Supp* const supp) |
| 317 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 318 | switch (VG_(get_supp_kind)(supp)) |
| 319 | { |
| 320 | } |
| 321 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static Char* drd_tool_error_name(Error* e) |
| 325 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 326 | switch (VG_(get_error_kind)(e)) |
| 327 | { |
bart | 5fc70e6 | 2008-03-23 07:54:02 +0000 | [diff] [blame] | 328 | case DataRaceErr: return VGAPPEND(STR_, DataRaceErr); |
| 329 | case MutexErr: return VGAPPEND(STR_, MutexErr); |
| 330 | case CondErr: return VGAPPEND(STR_, CondErr); |
| 331 | case CondRaceErr: return VGAPPEND(STR_, CondRaceErr); |
| 332 | case CondDestrErr: return VGAPPEND(STR_, CondDestrErr); |
| 333 | case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr); |
| 334 | case BarrierErr: return VGAPPEND(STR_, BarrierErr); |
| 335 | case RwlockErr: return VGAPPEND(STR_, RwlockErr); |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame^] | 336 | case HoldtimeErr: return VGAPPEND(STR_, HoldtimeErr); |
bart | 5fc70e6 | 2008-03-23 07:54:02 +0000 | [diff] [blame] | 337 | case GenericErr: return VGAPPEND(STR_, GenericErr); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 338 | default: |
| 339 | tl_assert(0); |
| 340 | } |
| 341 | return 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static void drd_tool_error_print_extra(Error* e) |
| 345 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 346 | switch (VG_(get_error_kind)(e)) |
| 347 | { |
| 348 | // VG_(printf)(" %s\n", VG_(get_error_string)(err)); |
| 349 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void drd_register_error_handlers(void) |
| 353 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 354 | // Tool error reporting. |
| 355 | VG_(needs_tool_errors)(drd_tool_error_eq, |
| 356 | drd_tool_error_pp, |
| 357 | True, |
| 358 | drd_tool_error_update_extra, |
| 359 | drd_tool_error_recog, |
| 360 | drd_tool_error_read_extra, |
| 361 | drd_tool_error_matches, |
| 362 | drd_tool_error_name, |
| 363 | drd_tool_error_print_extra); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 364 | } |