2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2020 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #include "drd_clientobj.h" /* struct mutex_info */
24 #include "drd_error.h"
25 #include "drd_malloc_wrappers.h"
26 #include "drd_mutex.h"
27 #include "drd_suppression.h" /* drd_start_suppression() */
28 #include "pub_drd_bitmap.h" /* LHS_W, ... */
29 #include "pub_tool_vki.h"
30 #include "pub_tool_basics.h"
31 #include "pub_tool_libcassert.h" /* tl_assert() */
32 #include "pub_tool_libcbase.h" /* strlen() */
33 #include "pub_tool_libcprint.h" /* VG_(printf)() */
34 #include "pub_tool_machine.h"
35 #include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */
36 #include "pub_tool_options.h" /* VG_(clo_xml) */
37 #include "pub_tool_threadstate.h" /* VG_(get_pthread_id)() */
38 #include "pub_tool_tooliface.h" /* VG_(needs_tool_errors)() */
41 /* Local function declarations. */
43 static const HChar
* drd_get_error_name(const Error
* e
);
46 /* Local variables. */
48 static Bool s_show_conflicting_segments
= True
;
51 void DRD_(set_show_conflicting_segments
)(const Bool scs
)
53 s_show_conflicting_segments
= scs
;
56 void DRD_(trace_msg
)(const HChar
* format
, ...)
59 va_start(vargs
, format
);
61 VG_(printf_xml
)(" <trace><text>");
62 VG_(vprintf_xml
)(format
, vargs
);
63 VG_(printf_xml
)("</text></trace>\n");
65 VG_(vmessage
)(Vg_UserMsg
, format
, vargs
);
66 VG_(message
)(Vg_UserMsg
, "\n");
71 void DRD_(trace_msg_w_bt
)(const HChar
* format
, ...)
74 va_start(vargs
, format
);
76 VG_(printf_xml
)(" <trace><text>");
77 VG_(vprintf_xml
)(format
, vargs
);
78 VG_(printf_xml
)("</text>\n");
80 VG_(vmessage
)(Vg_UserMsg
, format
, vargs
);
81 VG_(message
)(Vg_UserMsg
, "\n");
83 VG_(get_and_pp_StackTrace
)(VG_(get_running_tid
)(), VG_(clo_backtrace_size
));
86 VG_(printf_xml
)(" </trace>\n");
90 * Emit error message detail in the format requested by the user.
92 static void print_err_detail(const HChar
* format
, ...) PRINTF_CHECK(1, 2);
93 static void print_err_detail(const HChar
* format
, ...)
96 va_start(vargs
, format
);
98 VG_(vprintf_xml
)(format
, vargs
);
100 VG_(vmessage
)(Vg_UserMsg
, format
, vargs
);
105 * Describe the client address a as good as possible, putting the result in ai.
108 void describe_malloced_addr(Addr
const a
, AddrInfo
* const ai
)
110 Addr heap_block_start
;
112 if (DRD_(heap_addrinfo
)(a
, &heap_block_start
, &ai
->size
, &ai
->lastchange
))
114 ai
->akind
= eMallocd
;
115 ai
->rwoffset
= a
- heap_block_start
;
119 ai
->akind
= eUnknown
;
124 * Report where a client synchronization object has been observed for the first
125 * time. The printed call stack will either refer to a pthread_*_init() or a
126 * pthread_*lock() call.
128 static void first_observed(const Addr obj
)
132 cl
= DRD_(clientobj_get_any
)(obj
);
134 tl_assert(cl
->any
.first_observed_at
);
136 print_err_detail(" <first_observed_at>\n"
137 " <what>%pS</what>\n"
138 " <address>0x%lx</address>\n",
139 DRD_(clientobj_type_name
)(cl
->any
.type
), obj
);
140 VG_(pp_ExeContext
)(cl
->any
.first_observed_at
);
141 print_err_detail(" </first_observed_at>\n");
143 print_err_detail("%s 0x%lx was first observed at:\n",
144 DRD_(clientobj_type_name
)(cl
->any
.type
), obj
);
145 VG_(pp_ExeContext
)(cl
->any
.first_observed_at
);
151 void drd_report_data_race(const Error
* const err
,
152 const DataRaceErrInfo
* const dri
)
154 const Bool xml
= VG_(clo_xml
);
155 const HChar
* const what_prefix
= xml
? " <what>" : "";
156 const HChar
* const what_suffix
= xml
? "</what>" : "";
157 const HChar
* const auxwhat_prefix
= xml
? " <auxwhat>" : "";
158 const HChar
* const auxwhat_suffix
= xml
? "</auxwhat>" : "";
159 const HChar
* const indent
= xml
? " " : "";
162 VG_(memset
)(&ai
, 0, sizeof(ai
));
163 ai
.akind
= eUnknown
; // A safe initial value (?)
165 DiEpoch cur_ep
= VG_(current_DiEpoch
)();
166 XArray
* /* of HChar */ descr1
167 = VG_(newXA
)( VG_(malloc
), "drd.error.drdr2.1",
168 VG_(free
), sizeof(HChar
) );
169 XArray
* /* of HChar */ descr2
170 = VG_(newXA
)( VG_(malloc
), "drd.error.drdr2.2",
171 VG_(free
), sizeof(HChar
) );
174 tl_assert(dri
->addr
);
175 tl_assert(dri
->size
> 0);
177 (void) VG_(get_data_description
)(descr1
, descr2
, cur_ep
, dri
->addr
);
178 /* If there's nothing in descr1/2, free them. Why is it safe to
179 VG_(indexXA) at zero here? Because VG_(get_data_description)
180 guarantees to zero terminate descr1/2 regardless of the outcome
181 of the call. So there's always at least one element in each XA
184 if (0 == VG_(strlen
)( VG_(indexXA
)( descr1
, 0 ))) {
185 VG_(deleteXA
)( descr1
);
188 if (0 == VG_(strlen
)( VG_(indexXA
)( descr2
, 0 ))) {
189 VG_(deleteXA
)( descr2
);
192 /* Assume (assert) that VG_(get_data_description) fills in descr1
193 before it fills in descr2 */
195 tl_assert(descr2
== NULL
);
196 /* So anyway. Do we have something useful? */
200 describe_malloced_addr(dri
->addr
, &ai
);
203 print_err_detail("%sConflicting %s by thread %u at 0x%08lx size %lu%s\n",
204 what_prefix
, dri
->access_type
== eStore
? "store" : "load",
205 dri
->tid
, dri
->addr
, dri
->size
, what_suffix
);
207 VG_(pp_ExeContext
)(VG_(get_error_where
)(err
));
208 if (descr1
!= NULL
) {
209 print_err_detail("%s%s\n", indent
, (HChar
*)VG_(indexXA
)(descr1
, 0));
211 print_err_detail("%s%s\n", indent
, (HChar
*)VG_(indexXA
)(descr2
, 0));
212 } else if (ai
.akind
== eMallocd
&& ai
.lastchange
) {
213 print_err_detail("%sAddress 0x%lx is at offset %ld from 0x%lx.%s%s",
214 auxwhat_prefix
, dri
->addr
, ai
.rwoffset
,
215 dri
->addr
- ai
.rwoffset
, auxwhat_suffix
,
218 print_err_detail(" <allocation_context>\n");
220 print_err_detail(" Allocation context:\n");
221 VG_(pp_ExeContext
)(ai
.lastchange
);
223 print_err_detail(" </allocation_context>\n");
225 const HChar
*sect_name
;
226 VgSectKind sect_kind
;
228 sect_kind
= VG_(DebugInfo_sect_kind
)(§_name
, dri
->addr
);
229 if (sect_kind
!= Vg_SectUnknown
) {
230 print_err_detail("%sAllocation context: %ps section of %ps%s\n",
231 auxwhat_prefix
, VG_(pp_SectKind
)(sect_kind
),
232 sect_name
, auxwhat_suffix
);
234 print_err_detail("%sAllocation context: unknown.%s\n",
235 auxwhat_prefix
, auxwhat_suffix
);
238 if (s_show_conflicting_segments
)
240 DRD_(thread_report_conflicting_segments
)(dri
->tid
,
241 dri
->addr
, dri
->size
,
246 VG_(deleteXA
)(descr2
);
248 VG_(deleteXA
)(descr1
);
252 * Compare two error contexts. The core function VG_(maybe_record_error)()
253 * calls this function to compare error contexts such that errors that occur
254 * repeatedly are only printed once. This function is only called by the core
255 * if the error kind of e1 and e2 matches and if the ExeContext's of e1 and
258 static Bool
drd_compare_error_contexts(VgRes res
, const Error
* e1
,
261 tl_assert(VG_(get_error_kind
)(e1
) == VG_(get_error_kind
)(e2
));
263 switch (VG_(get_error_kind
)(e1
))
267 const DataRaceErrInfo
* const dri1
= VG_(get_error_extra
)(e1
);
268 const DataRaceErrInfo
* const dri2
= VG_(get_error_extra
)(e2
);
269 return dri1
->access_type
== dri2
->access_type
270 && dri1
->size
== dri2
->size
;
274 const MutexErrInfo
* const mei1
= VG_(get_error_extra
)(e1
);
275 const MutexErrInfo
* const mei2
= VG_(get_error_extra
)(e2
);
276 return mei1
->mutex
== mei2
->mutex
;
284 * Called by the core just before an error message will be printed. Used by
285 * DRD to print the thread number as a preamble.
287 static void drd_tool_error_before_pp(const Error
* const e
)
289 static DrdThreadId s_last_tid_printed
= 1;
290 DrdThreadId
* err_extra
;
292 err_extra
= VG_(get_error_extra
)(e
);
294 if (err_extra
&& *err_extra
!= s_last_tid_printed
&& !VG_(clo_xml
)) {
295 VG_(umsg
)("%s:\n", DRD_(thread_get_name
)(*err_extra
));
296 s_last_tid_printed
= *err_extra
;
300 /** Report an error to the user. */
301 static void drd_tool_error_pp(const Error
* const e
)
303 const Bool xml
= VG_(clo_xml
);
304 const HChar
* const what_prefix
= xml
? " <what>" : "";
305 const HChar
* const what_suffix
= xml
? "</what>" : "";
308 VG_(printf_xml
)( " <kind>%pS</kind>\n", drd_get_error_name(e
));
310 switch (VG_(get_error_kind
)(e
))
313 drd_report_data_race(e
, VG_(get_error_extra
)(e
));
317 MutexErrInfo
* p
= (MutexErrInfo
*)(VG_(get_error_extra
)(e
));
319 if (p
->recursion_count
>= 0) {
320 print_err_detail("%s%s: mutex 0x%lx, recursion count %d, owner %u."
321 "%s\n", what_prefix
, VG_(get_error_string
)(e
),
322 p
->mutex
, p
->recursion_count
, p
->owner
, what_suffix
);
324 print_err_detail("%sThe object at address 0x%lx is not a mutex.%s\n",
325 what_prefix
, p
->mutex
, what_suffix
);
327 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
328 first_observed(p
->mutex
);
332 CondErrInfo
* cdei
=(CondErrInfo
*)(VG_(get_error_extra
)(e
));
333 print_err_detail("%s%s: cond 0x%lx%s\n", what_prefix
,
334 VG_(get_error_string
)(e
), cdei
->cond
, what_suffix
);
335 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
336 first_observed(cdei
->cond
);
340 CondDestrErrInfo
* cdi
= (CondDestrErrInfo
*)(VG_(get_error_extra
)(e
));
341 print_err_detail("%s%s: cond 0x%lx, mutex 0x%lx locked by thread %u%s\n",
342 what_prefix
, VG_(get_error_string
)(e
), cdi
->cond
,
343 cdi
->mutex
, cdi
->owner
, what_suffix
);
344 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
345 first_observed(cdi
->mutex
);
349 CondRaceErrInfo
* cei
= (CondRaceErrInfo
*)(VG_(get_error_extra
)(e
));
350 print_err_detail("%sProbably a race condition: condition variable 0x%lx"
351 " has been signaled but the associated mutex 0x%lx is"
352 " not locked by the signalling thread.%s\n",
353 what_prefix
, cei
->cond
, cei
->mutex
, what_suffix
);
354 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
355 first_observed(cei
->cond
);
356 first_observed(cei
->mutex
);
360 CondWaitErrInfo
* cwei
= (CondWaitErrInfo
*)(VG_(get_error_extra
)(e
));
361 print_err_detail("%s%s: condition variable 0x%lx, mutexes 0x%lx and"
362 " 0x%lx%s\n", what_prefix
, VG_(get_error_string
)(e
),
363 cwei
->cond
, cwei
->mutex1
, cwei
->mutex2
, what_suffix
);
364 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
365 first_observed(cwei
->cond
);
366 first_observed(cwei
->mutex1
);
367 first_observed(cwei
->mutex2
);
371 SemaphoreErrInfo
* sei
= (SemaphoreErrInfo
*)(VG_(get_error_extra
)(e
));
373 print_err_detail("%s%s: semaphore 0x%lx%s\n", what_prefix
,
374 VG_(get_error_string
)(e
), sei
->semaphore
, what_suffix
);
375 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
376 first_observed(sei
->semaphore
);
380 BarrierErrInfo
* bei
= (BarrierErrInfo
*)(VG_(get_error_extra
)(e
));
382 print_err_detail("%s%s: barrier 0x%lx%s\n", what_prefix
,
383 VG_(get_error_string
)(e
), bei
->barrier
, what_suffix
);
384 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
385 if (bei
->other_context
) {
387 print_err_detail(" <confl_wait_call>\n");
388 print_err_detail("%sConflicting wait call by thread %u:%s\n",
389 what_prefix
, bei
->other_tid
, what_suffix
);
390 VG_(pp_ExeContext
)(bei
->other_context
);
392 print_err_detail(" </confl_wait_call>\n");
394 first_observed(bei
->barrier
);
398 RwlockErrInfo
* p
= (RwlockErrInfo
*)(VG_(get_error_extra
)(e
));
400 print_err_detail("%s%s: rwlock 0x%lx.%s\n", what_prefix
,
401 VG_(get_error_string
)(e
), p
->rwlock
, what_suffix
);
402 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
403 first_observed(p
->rwlock
);
407 HoldtimeErrInfo
* p
=(HoldtimeErrInfo
*)(VG_(get_error_extra
)(e
));
409 tl_assert(p
->acquired_at
);
411 print_err_detail(" <acquired_at>\n");
413 print_err_detail("Acquired at:\n");
414 VG_(pp_ExeContext
)(p
->acquired_at
);
416 print_err_detail(" </acquired_at>\n");
417 print_err_detail("%sLock on %s 0x%lx was held during %u ms"
418 " (threshold: %u ms).%s\n", what_prefix
,
419 VG_(get_error_string
)(e
), p
->synchronization_object
,
420 p
->hold_time_ms
, p
->threshold_ms
, what_suffix
);
421 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
422 first_observed(p
->synchronization_object
);
426 GenericErrInfo
* gei
= (GenericErrInfo
*)(VG_(get_error_extra
)(e
));
427 print_err_detail("%s%s%s\n", what_prefix
, VG_(get_error_string
)(e
),
429 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
431 first_observed(gei
->addr
);
434 case InvalidThreadId
: {
435 InvalidThreadIdInfo
* iti
=(InvalidThreadIdInfo
*)(VG_(get_error_extra
)(e
));
436 print_err_detail("%s%s 0x%llx%s\n", what_prefix
, VG_(get_error_string
)(e
),
437 iti
->ptid
, what_suffix
);
438 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
442 UnimpClReqInfo
* uicr
=(UnimpClReqInfo
*)(VG_(get_error_extra
)(e
));
443 print_err_detail("%sThe annotation macro %s has not yet been implemented"
444 " in %ps%s\n", what_prefix
, uicr
->descr
,
445 "<valgrind/helgrind.h>", what_suffix
);
446 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
449 case UnimpDrdClReq
: {
450 UnimpClReqInfo
* uicr
=(UnimpClReqInfo
*)(VG_(get_error_extra
)(e
));
451 print_err_detail("%sThe annotation macro %s has not yet been implemented"
452 " in %ps%s\n", what_prefix
, uicr
->descr
,
453 "<valgrind/drd.h>", what_suffix
);
454 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
458 print_err_detail("%s%s%s\n", what_prefix
, VG_(get_error_string
)(e
),
460 VG_(pp_ExeContext
)(VG_(get_error_where
)(e
));
465 static UInt
drd_tool_error_update_extra(const Error
* e
)
467 switch (VG_(get_error_kind
)(e
))
470 return sizeof(DataRaceErrInfo
);
472 return sizeof(MutexErrInfo
);
474 return sizeof(CondErrInfo
);
476 return sizeof(CondDestrErrInfo
);
478 return sizeof(CondRaceErrInfo
);
480 return sizeof(CondWaitErrInfo
);
482 return sizeof(SemaphoreErrInfo
);
484 return sizeof(BarrierErrInfo
);
486 return sizeof(RwlockErrInfo
);
488 return sizeof(HoldtimeErrInfo
);
490 return sizeof(GenericErrInfo
);
491 case InvalidThreadId
:
492 return sizeof(InvalidThreadIdInfo
);
494 return sizeof(UnimpClReqInfo
);
496 return sizeof(UnimpClReqInfo
);
504 * Parse suppression name.
506 * The suppression types recognized by DRD are the same types as the error
507 * types supported by DRD. So try to match the suppression name against the
508 * names of DRD error types.
510 static Bool
drd_is_recognized_suppression(const HChar
* const name
,
513 DrdErrorKind skind
= 0;
515 if (VG_(strcmp
)(name
, STR_DataRaceErr
) == 0)
517 else if (VG_(strcmp
)(name
, STR_MutexErr
) == 0)
519 else if (VG_(strcmp
)(name
, STR_CondErr
) == 0)
521 else if (VG_(strcmp
)(name
, STR_CondDestrErr
) == 0)
522 skind
= CondDestrErr
;
523 else if (VG_(strcmp
)(name
, STR_CondRaceErr
) == 0)
525 else if (VG_(strcmp
)(name
, STR_CondWaitErr
) == 0)
527 else if (VG_(strcmp
)(name
, STR_SemaphoreErr
) == 0)
528 skind
= SemaphoreErr
;
529 else if (VG_(strcmp
)(name
, STR_BarrierErr
) == 0)
531 else if (VG_(strcmp
)(name
, STR_RwlockErr
) == 0)
533 else if (VG_(strcmp
)(name
, STR_HoldtimeErr
) == 0)
535 else if (VG_(strcmp
)(name
, STR_GenericErr
) == 0)
537 else if (VG_(strcmp
)(name
, STR_InvalidThreadId
) == 0)
538 skind
= InvalidThreadId
;
539 else if (VG_(strcmp
)(name
, STR_UnimpHgClReq
) == 0)
540 skind
= UnimpHgClReq
;
541 else if (VG_(strcmp
)(name
, STR_UnimpDrdClReq
) == 0)
542 skind
= UnimpDrdClReq
;
546 VG_(set_supp_kind
)(supp
, skind
);
551 * Read additional suppression information from the suppression file.
553 * None of the suppression patterns recognized by DRD has 'extra' lines
554 * of information in the suppression file, so just return True to indicate
555 * that reading the 'extra' lines succeeded.
558 Bool
drd_read_extra_suppression_info(Int fd
, HChar
** bufpp
,
559 SizeT
* nBufp
, Int
* lineno
, Supp
* supp
)
565 * Determine whether or not the types of the given error message and the
566 * given suppression match.
568 static Bool
drd_error_matches_suppression(const Error
* const e
,
569 const Supp
* const supp
)
571 return VG_(get_supp_kind
)(supp
) == VG_(get_error_kind
)(e
);
574 static const HChar
* drd_get_error_name(const Error
* e
)
576 switch (VG_(get_error_kind
)(e
))
578 case DataRaceErr
: return VGAPPEND(STR_
, DataRaceErr
);
579 case MutexErr
: return VGAPPEND(STR_
, MutexErr
);
580 case CondErr
: return VGAPPEND(STR_
, CondErr
);
581 case CondDestrErr
: return VGAPPEND(STR_
, CondDestrErr
);
582 case CondRaceErr
: return VGAPPEND(STR_
, CondRaceErr
);
583 case CondWaitErr
: return VGAPPEND(STR_
, CondWaitErr
);
584 case SemaphoreErr
: return VGAPPEND(STR_
, SemaphoreErr
);
585 case BarrierErr
: return VGAPPEND(STR_
, BarrierErr
);
586 case RwlockErr
: return VGAPPEND(STR_
, RwlockErr
);
587 case HoldtimeErr
: return VGAPPEND(STR_
, HoldtimeErr
);
588 case GenericErr
: return VGAPPEND(STR_
, GenericErr
);
589 case InvalidThreadId
: return VGAPPEND(STR_
, InvalidThreadId
);
590 case UnimpHgClReq
: return VGAPPEND(STR_
, UnimpHgClReq
);
591 case UnimpDrdClReq
: return VGAPPEND(STR_
, UnimpDrdClReq
);
599 * Return extra suppression information.
601 * Invoked while printing a suppression pattern because the user
602 * specified --gen-suppressions=yes or all on the command line. DRD does not
603 * define any 'extra' suppression information.
606 SizeT
drd_get_extra_suppression_info(const Error
* e
,
607 /*OUT*/HChar
* buf
, Int nBuf
)
609 tl_assert(nBuf
>= 1);
615 SizeT
drd_print_extra_suppression_use(const Supp
* su
,
616 /*OUT*/HChar
* buf
, Int nBuf
)
618 tl_assert(nBuf
>= 1);
624 void drd_update_extra_suppresion_use(const Error
* e
, const Supp
* supp
)
629 /** Tell the Valgrind core about DRD's error handlers. */
630 void DRD_(register_error_handlers
)(void)
632 VG_(needs_core_errors
)(True
);
633 VG_(needs_tool_errors
)(drd_compare_error_contexts
,
634 drd_tool_error_before_pp
,
637 drd_tool_error_update_extra
,
638 drd_is_recognized_suppression
,
639 drd_read_extra_suppression_info
,
640 drd_error_matches_suppression
,
642 drd_get_extra_suppression_info
,
643 drd_print_extra_suppression_use
,
644 drd_update_extra_suppresion_use
);