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