1 /* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
26 #include "drd_clientobj.h" /* struct mutex_info */
27 #include "drd_error.h"
28 #include "drd_malloc_wrappers.h"
29 #include "drd_mutex.h"
30 #include "drd_suppression.h" /* drd_start_suppression() */
31 #include "pub_drd_bitmap.h" /* LHS_W, ... */
32 #include "pub_tool_vki.h"
33 #include "pub_tool_basics.h"
34 #include "pub_tool_libcassert.h" /* tl_assert() */
35 #include "pub_tool_libcbase.h" /* strlen() */
36 #include "pub_tool_libcfile.h" /* VG_(get_startup_wd)() */
37 #include "pub_tool_libcprint.h" /* VG_(printf)() */
38 #include "pub_tool_machine.h"
39 #include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */
40 #include "pub_tool_threadstate.h" /* VG_(get_pthread_id)() */
41 #include "pub_tool_tooliface.h" /* VG_(needs_tool_errors)() */
44 /* Local variables. */
46 static Bool s_show_conflicting_segments = True;
49 void DRD_(set_show_conflicting_segments)(const Bool scs)
51 s_show_conflicting_segments = scs;
55 * Describe the client address a as good as possible, putting the result in ai.
58 void describe_malloced_addr(Addr const a, AddrInfo* const ai)
60 Addr heap_block_start;
62 if (DRD_(heap_addrinfo)(a, &heap_block_start, &ai->size, &ai->lastchange))
65 ai->rwoffset = a - heap_block_start;
74 * Report where a client synchronization object has been observed for the first
75 * time. The printed call stack will either refer to a pthread_*_init() or a
76 * pthread_*lock() call.
78 static void first_observed(const Addr obj)
82 cl = DRD_(clientobj_get_any)(obj);
85 tl_assert(cl->any.first_observed_at);
86 VG_(message)(Vg_UserMsg,
87 "%s 0x%lx was first observed at:\n",
88 DRD_(clientobj_type_name)(cl->any.type),
90 VG_(pp_ExeContext)(cl->any.first_observed_at);
95 void drd_report_data_race(Error* const err, const DataRaceErrInfo* const dri)
99 XArray* /* of HChar */ descr1
100 = VG_(newXA)( VG_(malloc), "drd.error.drdr2.1",
101 VG_(free), sizeof(HChar) );
102 XArray* /* of HChar */ descr2
103 = VG_(newXA)( VG_(malloc), "drd.error.drdr2.2",
104 VG_(free), sizeof(HChar) );
107 tl_assert(dri->addr);
108 tl_assert(dri->size > 0);
112 (void) VG_(get_data_description)(descr1, descr2, dri->addr);
113 /* If there's nothing in descr1/2, free them. Why is it safe to to
114 VG_(indexXA) at zero here? Because VG_(get_data_description)
115 guarantees to zero terminate descr1/2 regardless of the outcome
116 of the call. So there's always at least one element in each XA
119 if (0 == VG_(strlen)( VG_(indexXA)( descr1, 0 ))) {
120 VG_(deleteXA)( descr1 );
123 if (0 == VG_(strlen)( VG_(indexXA)( descr2, 0 ))) {
124 VG_(deleteXA)( descr2 );
127 /* Assume (assert) that VG_(get_data_description) fills in descr1
128 before it fills in descr2 */
130 tl_assert(descr2 == NULL);
131 /* So anyway. Do we have something useful? */
135 describe_malloced_addr(dri->addr, &ai);
137 VG_(message)(Vg_UserMsg,
138 "Conflicting %s by thread %d at 0x%08lx size %ld\n",
139 dri->access_type == eStore ? "store" : "load",
143 VG_(pp_ExeContext)(VG_(get_error_where)(err));
146 VG_(message)(Vg_UserMsg, "%s\n", (HChar*)VG_(indexXA)(descr1, 0));
148 VG_(message)(Vg_UserMsg, "%s\n", (HChar*)VG_(indexXA)(descr2, 0));
150 else if (ai.akind == eMallocd && ai.lastchange)
152 VG_(message)(Vg_UserMsg,
153 "Address 0x%lx is at offset %ld from 0x%lx."
154 " Allocation context:\n",
155 dri->addr, ai.rwoffset, dri->addr - ai.rwoffset);
156 VG_(pp_ExeContext)(ai.lastchange);
161 VgSectKind sect_kind;
163 sect_kind = VG_(DebugInfo_sect_kind)(sect_name, sizeof(sect_name),
165 if (sect_kind != Vg_SectUnknown)
167 VG_(message)(Vg_UserMsg,
168 "Allocation context: %s section of %s\n",
169 VG_(pp_SectKind)(sect_kind),
174 VG_(message)(Vg_UserMsg, "Allocation context: unknown.\n");
177 if (s_show_conflicting_segments)
179 DRD_(thread_report_conflicting_segments)(dri->tid,
180 dri->addr, dri->size,
185 VG_(deleteXA)(descr2);
187 VG_(deleteXA)(descr1);
191 * Compare two error contexts. The core function VG_(maybe_record_error)()
192 * calls this function to compare error contexts such that errors that occur
193 * repeatedly are only printed once. This function is only called by the core
194 * if the error kind of e1 and e2 matches and if the ExeContext's of e1 and
197 static Bool drd_compare_error_contexts(VgRes res, Error* e1, Error* e2)
199 tl_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2));
201 switch (VG_(get_error_kind)(e1))
205 const DataRaceErrInfo* const dri1 = VG_(get_error_extra)(e1);
206 const DataRaceErrInfo* const dri2 = VG_(get_error_extra)(e2);
207 return dri1->access_type == dri2->access_type
208 && dri1->size == dri2->size;
212 const MutexErrInfo* const mei1 = VG_(get_error_extra)(e1);
213 const MutexErrInfo* const mei2 = VG_(get_error_extra)(e2);
214 return mei1->mutex == mei2->mutex;
222 * Called by the core just before an error message will be printed. Used by
223 * DRD to print the thread number as a preamble.
225 static void drd_tool_error_before_pp(Error* const e)
227 static DrdThreadId s_last_tid_printed = 1;
228 DrdThreadId* err_extra;
230 err_extra = VG_(get_error_extra)(e);
232 if (err_extra && *err_extra != s_last_tid_printed)
234 VG_(umsg)("%s:\n", DRD_(thread_get_name)(*err_extra));
235 s_last_tid_printed = *err_extra;
239 /** Report an error to the user. */
240 static void drd_tool_error_pp(Error* const e)
242 switch (VG_(get_error_kind)(e))
245 drd_report_data_race(e, VG_(get_error_extra)(e));
249 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
251 if (p->recursion_count >= 0)
253 VG_(message)(Vg_UserMsg,
254 "%s: mutex 0x%lx, recursion count %d, owner %d.\n",
255 VG_(get_error_string)(e),
262 VG_(message)(Vg_UserMsg,
263 "The object at address 0x%lx is not a mutex.\n",
266 VG_(pp_ExeContext)(VG_(get_error_where)(e));
267 first_observed(p->mutex);
271 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
272 VG_(message)(Vg_UserMsg,
274 VG_(get_error_string)(e),
276 VG_(pp_ExeContext)(VG_(get_error_where)(e));
277 first_observed(cdei->cond);
281 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
282 VG_(message)(Vg_UserMsg,
283 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d\n",
284 VG_(get_error_string)(e),
285 cdi->cond, cdi->mutex,
287 VG_(pp_ExeContext)(VG_(get_error_where)(e));
288 first_observed(cdi->mutex);
292 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
293 VG_(message)(Vg_UserMsg,
294 "Probably a race condition: condition variable 0x%lx has"
295 " been signaled but the associated mutex 0x%lx is not"
296 " locked by the signalling thread.\n",
297 cei->cond, cei->mutex);
298 VG_(pp_ExeContext)(VG_(get_error_where)(e));
299 first_observed(cei->cond);
300 first_observed(cei->mutex);
304 CondWaitErrInfo* cwei = (CondWaitErrInfo*)(VG_(get_error_extra)(e));
305 VG_(message)(Vg_UserMsg,
306 "%s: condition variable 0x%lx, mutexes 0x%lx and 0x%lx\n",
307 VG_(get_error_string)(e),
311 VG_(pp_ExeContext)(VG_(get_error_where)(e));
312 first_observed(cwei->cond);
313 first_observed(cwei->mutex1);
314 first_observed(cwei->mutex2);
318 SemaphoreErrInfo* sei = (SemaphoreErrInfo*)(VG_(get_error_extra)(e));
320 VG_(message)(Vg_UserMsg,
321 "%s: semaphore 0x%lx\n",
322 VG_(get_error_string)(e),
324 VG_(pp_ExeContext)(VG_(get_error_where)(e));
325 first_observed(sei->semaphore);
329 BarrierErrInfo* bei = (BarrierErrInfo*)(VG_(get_error_extra)(e));
331 VG_(message)(Vg_UserMsg,
332 "%s: barrier 0x%lx\n",
333 VG_(get_error_string)(e),
335 VG_(pp_ExeContext)(VG_(get_error_where)(e));
336 if (bei->other_context)
338 VG_(message)(Vg_UserMsg,
339 "Conflicting wait call by thread %d:\n",
341 VG_(pp_ExeContext)(bei->other_context);
343 first_observed(bei->barrier);
347 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
349 VG_(message)(Vg_UserMsg,
350 "%s: rwlock 0x%lx.\n",
351 VG_(get_error_string)(e),
353 VG_(pp_ExeContext)(VG_(get_error_where)(e));
354 first_observed(p->rwlock);
358 HoldtimeErrInfo* p =(HoldtimeErrInfo*)(VG_(get_error_extra)(e));
360 tl_assert(p->acquired_at);
361 VG_(message)(Vg_UserMsg, "Acquired at:\n");
362 VG_(pp_ExeContext)(p->acquired_at);
363 VG_(message)(Vg_UserMsg,
364 "Lock on %s 0x%lx was held during %d ms (threshold: %d ms).\n",
365 VG_(get_error_string)(e),
366 p->synchronization_object,
369 VG_(pp_ExeContext)(VG_(get_error_where)(e));
370 first_observed(p->synchronization_object);
374 GenericErrInfo* gei = (GenericErrInfo*)(VG_(get_error_extra)(e));
375 VG_(message)(Vg_UserMsg, "%s\n", VG_(get_error_string)(e));
376 VG_(pp_ExeContext)(VG_(get_error_where)(e));
378 first_observed(gei->addr);
381 case InvalidThreadId: {
382 InvalidThreadIdInfo* iti =(InvalidThreadIdInfo*)(VG_(get_error_extra)(e));
383 VG_(message)(Vg_UserMsg,
384 "%s 0x%llx\n", VG_(get_error_string)(e), iti->ptid);
385 VG_(pp_ExeContext)(VG_(get_error_where)(e));
389 UnimpClReqInfo* uicr =(UnimpClReqInfo*)(VG_(get_error_extra)(e));
390 VG_(message)(Vg_UserMsg,
391 "The annotation macro %s has not yet been implemented in"
392 " <valgrind/helgrind.h>\n",
393 /*VG_(get_error_string)(e),*/ uicr->descr);
394 VG_(pp_ExeContext)(VG_(get_error_where)(e));
397 case UnimpDrdClReq: {
398 UnimpClReqInfo* uicr =(UnimpClReqInfo*)(VG_(get_error_extra)(e));
399 VG_(message)(Vg_UserMsg,
400 "The annotation macro %s has not yet been implemented in"
401 " <valgrind/drd.h>\n",
403 VG_(pp_ExeContext)(VG_(get_error_where)(e));
407 VG_(message)(Vg_UserMsg,
409 VG_(get_error_string)(e));
410 VG_(pp_ExeContext)(VG_(get_error_where)(e));
415 static UInt drd_tool_error_update_extra(Error* e)
417 switch (VG_(get_error_kind)(e))
420 return sizeof(DataRaceErrInfo);
422 return sizeof(MutexErrInfo);
424 return sizeof(CondErrInfo);
426 return sizeof(CondDestrErrInfo);
428 return sizeof(CondRaceErrInfo);
430 return sizeof(CondWaitErrInfo);
432 return sizeof(SemaphoreErrInfo);
434 return sizeof(BarrierErrInfo);
436 return sizeof(RwlockErrInfo);
438 return sizeof(HoldtimeErrInfo);
440 return sizeof(GenericErrInfo);
441 case InvalidThreadId:
442 return sizeof(InvalidThreadIdInfo);
444 return sizeof(UnimpClReqInfo);
446 return sizeof(UnimpClReqInfo);
454 * Parse suppression name.
456 * The suppression types recognized by DRD are the same types as the error
457 * types supported by DRD. So try to match the suppression name against the
458 * names of DRD error types.
460 static Bool drd_is_recognized_suppression(Char* const name, Supp* const supp)
462 DrdErrorKind skind = 0;
464 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
466 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
468 else if (VG_(strcmp)(name, STR_CondErr) == 0)
470 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
471 skind = CondDestrErr;
472 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
474 else if (VG_(strcmp)(name, STR_CondWaitErr) == 0)
476 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
477 skind = SemaphoreErr;
478 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
480 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
482 else if (VG_(strcmp)(name, STR_HoldtimeErr) == 0)
484 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
486 else if (VG_(strcmp)(name, STR_InvalidThreadId) == 0)
487 skind = InvalidThreadId;
488 else if (VG_(strcmp)(name, STR_UnimpHgClReq) == 0)
489 skind = UnimpHgClReq;
490 else if (VG_(strcmp)(name, STR_UnimpDrdClReq) == 0)
491 skind = UnimpDrdClReq;
495 VG_(set_supp_kind)(supp, skind);
500 * Read additional suppression information from the suppression file.
502 * None of the suppression patterns recognized by DRD has 'extra' lines
503 * of information in the suppression file, so just return True to indicate
504 * that reading the 'extra' lines succeeded.
507 Bool drd_read_extra_suppression_info(Int fd, Char** bufpp,
508 SizeT* nBufp, Supp* supp)
514 * Determine whether or not the types of the given error message and the
515 * given suppression match.
517 static Bool drd_error_matches_suppression(Error* const e, Supp* const supp)
519 return VG_(get_supp_kind)(supp) == VG_(get_error_kind)(e);
522 static Char* drd_get_error_name(Error* e)
524 switch (VG_(get_error_kind)(e))
526 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
527 case MutexErr: return VGAPPEND(STR_, MutexErr);
528 case CondErr: return VGAPPEND(STR_, CondErr);
529 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
530 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
531 case CondWaitErr: return VGAPPEND(STR_, CondWaitErr);
532 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
533 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
534 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
535 case HoldtimeErr: return VGAPPEND(STR_, HoldtimeErr);
536 case GenericErr: return VGAPPEND(STR_, GenericErr);
537 case InvalidThreadId: return VGAPPEND(STR_, InvalidThreadId);
538 case UnimpHgClReq: return VGAPPEND(STR_, UnimpHgClReq);
539 case UnimpDrdClReq: return VGAPPEND(STR_, UnimpDrdClReq);
547 * Return extra suppression information.
549 * Invoked while printing a suppression pattern because the user
550 * specified --gen-suppressions=yes or all on the command line. DRD does not
551 * define any 'extra' suppression information.
554 Bool drd_get_extra_suppression_info(Error* e,
555 /*OUT*/Char* buf, Int nBuf)
560 /** Tell the Valgrind core about DRD's error handlers. */
561 void DRD_(register_error_handlers)(void)
563 VG_(needs_tool_errors)(drd_compare_error_contexts,
564 drd_tool_error_before_pp,
567 drd_tool_error_update_extra,
568 drd_is_recognized_suppression,
569 drd_read_extra_suppression_info,
570 drd_error_matches_suppression,
572 drd_get_extra_suppression_info);