blob: 12bfeff4915b6109970b2c69b04425cfeaf417f9 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bartd4bab992013-10-04 05:55:30 +00004 Copyright (C) 2006-2013 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
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.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
bart886b87c2008-06-28 13:40:41 +000025#include "drd_clientobj.h" /* struct mutex_info */
sewardjaf44c822007-11-25 14:01:38 +000026#include "drd_error.h"
27#include "drd_malloc_wrappers.h"
bart886b87c2008-06-28 13:40:41 +000028#include "drd_mutex.h"
29#include "drd_suppression.h" /* drd_start_suppression() */
30#include "pub_drd_bitmap.h" /* LHS_W, ... */
sewardjaf44c822007-11-25 14:01:38 +000031#include "pub_tool_vki.h"
32#include "pub_tool_basics.h"
bart886b87c2008-06-28 13:40:41 +000033#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)() */
sewardjaf44c822007-11-25 14:01:38 +000037#include "pub_tool_machine.h"
bart886b87c2008-06-28 13:40:41 +000038#include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */
bartb92ff0f2011-10-08 08:29:29 +000039#include "pub_tool_options.h" /* VG_(clo_xml) */
bart886b87c2008-06-28 13:40:41 +000040#include "pub_tool_threadstate.h" /* VG_(get_pthread_id)() */
41#include "pub_tool_tooliface.h" /* VG_(needs_tool_errors)() */
sewardjaf44c822007-11-25 14:01:38 +000042
43
barte7086002011-10-11 19:08:39 +000044/* Local function declarations. */
45
floriane543f302012-10-21 19:43:43 +000046static const HChar* drd_get_error_name(Error* e);
barte7086002011-10-11 19:08:39 +000047
48
bart16d76e52008-03-18 17:08:08 +000049/* Local variables. */
50
bartd2c5eae2009-02-21 15:27:04 +000051static Bool s_show_conflicting_segments = True;
bart16d76e52008-03-18 17:08:08 +000052
53
bart246fbf22009-02-15 14:46:17 +000054void DRD_(set_show_conflicting_segments)(const Bool scs)
bart16d76e52008-03-18 17:08:08 +000055{
bartbedfd232009-03-26 19:07:15 +000056 s_show_conflicting_segments = scs;
bart16d76e52008-03-18 17:08:08 +000057}
58
florian19f91bb2012-11-10 22:29:54 +000059void DRD_(trace_msg)(const HChar* format, ...)
bartb92ff0f2011-10-08 08:29:29 +000060{
61 va_list vargs;
62 va_start(vargs, format);
barte7086002011-10-11 19:08:39 +000063 if (VG_(clo_xml)) {
bartad994e82011-10-13 18:04:30 +000064 VG_(printf_xml)(" <trace><text>");
barte7086002011-10-11 19:08:39 +000065 VG_(vprintf_xml)(format, vargs);
bartad994e82011-10-13 18:04:30 +000066 VG_(printf_xml)("</text></trace>\n");
barte7086002011-10-11 19:08:39 +000067 } else {
68 VG_(vmessage)(Vg_UserMsg, format, vargs);
bartad994e82011-10-13 18:04:30 +000069 VG_(message)(Vg_UserMsg, "\n");
barte7086002011-10-11 19:08:39 +000070 }
71 va_end(vargs);
72}
73
florian19f91bb2012-11-10 22:29:54 +000074void DRD_(trace_msg_w_bt)(const HChar* format, ...)
bartad994e82011-10-13 18:04:30 +000075{
76 va_list vargs;
77 va_start(vargs, format);
78 if (VG_(clo_xml)) {
79 VG_(printf_xml)(" <trace><text>");
80 VG_(vprintf_xml)(format, vargs);
81 VG_(printf_xml)("</text>\n");
82 } else {
83 VG_(vmessage)(Vg_UserMsg, format, vargs);
84 VG_(message)(Vg_UserMsg, "\n");
85 }
86 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), VG_(clo_backtrace_size));
87 va_end(vargs);
88 if (VG_(clo_xml))
89 VG_(printf_xml)(" </trace>\n");
90}
91
barte7086002011-10-11 19:08:39 +000092/**
93 * Emit error message detail in the format requested by the user.
94 */
florian19f91bb2012-11-10 22:29:54 +000095static void print_err_detail(const HChar* format, ...) PRINTF_CHECK(1, 2);
96static void print_err_detail(const HChar* format, ...)
barte7086002011-10-11 19:08:39 +000097{
98 va_list vargs;
99 va_start(vargs, format);
100 if (VG_(clo_xml))
101 VG_(vprintf_xml)(format, vargs);
102 else
103 VG_(vmessage)(Vg_UserMsg, format, vargs);
bartb92ff0f2011-10-08 08:29:29 +0000104 va_end(vargs);
105}
106
bart246fbf22009-02-15 14:46:17 +0000107/**
bart61d36ff2009-07-27 14:17:33 +0000108 * Describe the client address a as good as possible, putting the result in ai.
bart886b87c2008-06-28 13:40:41 +0000109 */
bartff1252a2008-03-16 17:27:25 +0000110static
bart61d36ff2009-07-27 14:17:33 +0000111void describe_malloced_addr(Addr const a, AddrInfo* const ai)
sewardjaf44c822007-11-25 14:01:38 +0000112{
bart61d36ff2009-07-27 14:17:33 +0000113 Addr heap_block_start;
sewardjaf44c822007-11-25 14:01:38 +0000114
bart61d36ff2009-07-27 14:17:33 +0000115 if (DRD_(heap_addrinfo)(a, &heap_block_start, &ai->size, &ai->lastchange))
bartbedfd232009-03-26 19:07:15 +0000116 {
117 ai->akind = eMallocd;
bart61d36ff2009-07-27 14:17:33 +0000118 ai->rwoffset = a - heap_block_start;
bartbedfd232009-03-26 19:07:15 +0000119 }
120 else
121 {
122 ai->akind = eUnknown;
123 }
sewardjaf44c822007-11-25 14:01:38 +0000124}
125
bart246fbf22009-02-15 14:46:17 +0000126/**
bart61d36ff2009-07-27 14:17:33 +0000127 * Report where a client synchronization object has been observed for the first
128 * time. The printed call stack will either refer to a pthread_*_init() or a
129 * pthread_*lock() call.
bart886b87c2008-06-28 13:40:41 +0000130 */
bartd2c5eae2009-02-21 15:27:04 +0000131static void first_observed(const Addr obj)
bart886b87c2008-06-28 13:40:41 +0000132{
bartbedfd232009-03-26 19:07:15 +0000133 DrdClientobj* cl;
bart886b87c2008-06-28 13:40:41 +0000134
bartbedfd232009-03-26 19:07:15 +0000135 cl = DRD_(clientobj_get_any)(obj);
barte7086002011-10-11 19:08:39 +0000136 if (cl) {
bartbedfd232009-03-26 19:07:15 +0000137 tl_assert(cl->any.first_observed_at);
barte7086002011-10-11 19:08:39 +0000138 if (VG_(clo_xml)) {
139 print_err_detail(" <first_observed_at>\n"
140 " <what>%pS</what>\n"
141 " <address>0x%lx</address>\n",
142 DRD_(clientobj_type_name)(cl->any.type), obj);
143 VG_(pp_ExeContext)(cl->any.first_observed_at);
144 print_err_detail(" </first_observed_at>\n");
145 } else {
146 print_err_detail("%s 0x%lx was first observed at:\n",
147 DRD_(clientobj_type_name)(cl->any.type), obj);
148 VG_(pp_ExeContext)(cl->any.first_observed_at);
149 }
bartbedfd232009-03-26 19:07:15 +0000150 }
bart886b87c2008-06-28 13:40:41 +0000151}
152
sewardjaf44c822007-11-25 14:01:38 +0000153static
bartd2c5eae2009-02-21 15:27:04 +0000154void drd_report_data_race(Error* const err, const DataRaceErrInfo* const dri)
sewardjaf44c822007-11-25 14:01:38 +0000155{
barte7086002011-10-11 19:08:39 +0000156 const Bool xml = VG_(clo_xml);
florian19f91bb2012-11-10 22:29:54 +0000157 const HChar* const what_prefix = xml ? " <what>" : "";
158 const HChar* const what_suffix = xml ? "</what>" : "";
159 const HChar* const auxwhat_prefix = xml ? " <auxwhat>" : "";
160 const HChar* const auxwhat_suffix = xml ? "</auxwhat>" : "";
161 const HChar* const indent = xml ? " " : "";
bartbedfd232009-03-26 19:07:15 +0000162 AddrInfo ai;
sewardj1e29ebc2009-07-15 14:49:17 +0000163
164 XArray* /* of HChar */ descr1
165 = VG_(newXA)( VG_(malloc), "drd.error.drdr2.1",
166 VG_(free), sizeof(HChar) );
167 XArray* /* of HChar */ descr2
168 = VG_(newXA)( VG_(malloc), "drd.error.drdr2.2",
169 VG_(free), sizeof(HChar) );
sewardjaf44c822007-11-25 14:01:38 +0000170
bartbedfd232009-03-26 19:07:15 +0000171 tl_assert(dri);
172 tl_assert(dri->addr);
173 tl_assert(dri->size > 0);
174 tl_assert(descr1);
175 tl_assert(descr2);
bartb515eb12008-03-07 18:52:38 +0000176
sewardj1e29ebc2009-07-15 14:49:17 +0000177 (void) VG_(get_data_description)(descr1, descr2, dri->addr);
178 /* If there's nothing in descr1/2, free them. Why is it safe to 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
182 after the call.
183 */
184 if (0 == VG_(strlen)( VG_(indexXA)( descr1, 0 ))) {
185 VG_(deleteXA)( descr1 );
186 descr1 = NULL;
187 }
188 if (0 == VG_(strlen)( VG_(indexXA)( descr2, 0 ))) {
189 VG_(deleteXA)( descr2 );
190 descr2 = NULL;
191 }
192 /* Assume (assert) that VG_(get_data_description) fills in descr1
193 before it fills in descr2 */
194 if (descr1 == NULL)
195 tl_assert(descr2 == NULL);
196 /* So anyway. Do we have something useful? */
197 if (descr1 == NULL)
bartbedfd232009-03-26 19:07:15 +0000198 {
sewardj1e29ebc2009-07-15 14:49:17 +0000199 /* No. Do Plan B. */
bart61d36ff2009-07-27 14:17:33 +0000200 describe_malloced_addr(dri->addr, &ai);
bartbedfd232009-03-26 19:07:15 +0000201 }
barte7086002011-10-11 19:08:39 +0000202
203 print_err_detail("%sConflicting %s by thread %d at 0x%08lx size %ld%s\n",
204 what_prefix, dri->access_type == eStore ? "store" : "load",
205 dri->tid, dri->addr, dri->size, what_suffix);
206
bartbedfd232009-03-26 19:07:15 +0000207 VG_(pp_ExeContext)(VG_(get_error_where)(err));
barte7086002011-10-11 19:08:39 +0000208 if (descr1 != NULL) {
209 print_err_detail("%s%s\n", indent, (HChar*)VG_(indexXA)(descr1, 0));
sewardj1e29ebc2009-07-15 14:49:17 +0000210 if (descr2 != NULL)
barte7086002011-10-11 19:08:39 +0000211 print_err_detail("%s%s\n", indent, (HChar*)VG_(indexXA)(descr2, 0));
212 } else if (ai.akind == eMallocd && ai.lastchange) {
bartca7285e2011-10-13 16:42:49 +0000213 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,
216 xml ? "\n" : "");
barte7086002011-10-11 19:08:39 +0000217 if (xml)
bartca7285e2011-10-13 16:42:49 +0000218 print_err_detail(" <allocation_context>\n");
barte7086002011-10-11 19:08:39 +0000219 else
220 print_err_detail(" Allocation context:\n");
bartbedfd232009-03-26 19:07:15 +0000221 VG_(pp_ExeContext)(ai.lastchange);
barte7086002011-10-11 19:08:39 +0000222 if (xml)
bartca7285e2011-10-13 16:42:49 +0000223 print_err_detail(" </allocation_context>\n");
barte7086002011-10-11 19:08:39 +0000224 } else {
florian19f91bb2012-11-10 22:29:54 +0000225 HChar sect_name[64];
bartbedfd232009-03-26 19:07:15 +0000226 VgSectKind sect_kind;
bartf993b872008-04-01 18:19:50 +0000227
sewardje3f1e592009-07-31 09:41:29 +0000228 sect_kind = VG_(DebugInfo_sect_kind)(sect_name, sizeof(sect_name),
229 dri->addr);
bartca7285e2011-10-13 16:42:49 +0000230 if (sect_kind != Vg_SectUnknown) {
231 print_err_detail("%sAllocation context: %ps section of %ps%s\n",
232 auxwhat_prefix, VG_(pp_SectKind)(sect_kind),
233 sect_name, auxwhat_suffix);
barte7086002011-10-11 19:08:39 +0000234 } else {
bartca7285e2011-10-13 16:42:49 +0000235 print_err_detail("%sAllocation context: unknown.%s\n",
236 auxwhat_prefix, auxwhat_suffix);
bartbedfd232009-03-26 19:07:15 +0000237 }
238 }
239 if (s_show_conflicting_segments)
240 {
241 DRD_(thread_report_conflicting_segments)(dri->tid,
242 dri->addr, dri->size,
243 dri->access_type);
244 }
245
sewardj1e29ebc2009-07-15 14:49:17 +0000246 if (descr2)
247 VG_(deleteXA)(descr2);
248 if (descr1)
249 VG_(deleteXA)(descr1);
sewardjaf44c822007-11-25 14:01:38 +0000250}
251
bart61d36ff2009-07-27 14:17:33 +0000252/**
253 * Compare two error contexts. The core function VG_(maybe_record_error)()
254 * calls this function to compare error contexts such that errors that occur
bart31b983d2010-02-21 14:52:59 +0000255 * repeatedly are only printed once. This function is only called by the core
bart61d36ff2009-07-27 14:17:33 +0000256 * if the error kind of e1 and e2 matches and if the ExeContext's of e1 and
257 * e2 also match.
258 */
259static Bool drd_compare_error_contexts(VgRes res, Error* e1, Error* e2)
sewardjaf44c822007-11-25 14:01:38 +0000260{
bartb80fa962009-07-31 08:45:02 +0000261 tl_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2));
262
263 switch (VG_(get_error_kind)(e1))
264 {
265 case DataRaceErr:
266 {
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;
271 }
bart94fb8d22009-07-31 18:45:49 +0000272 case MutexErr:
273 {
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;
277 }
bartb80fa962009-07-31 08:45:02 +0000278 default:
279 return True;
280 }
sewardjaf44c822007-11-25 14:01:38 +0000281}
282
bart61d36ff2009-07-27 14:17:33 +0000283/**
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.
286 */
sewardj1e29ebc2009-07-15 14:49:17 +0000287static void drd_tool_error_before_pp(Error* const e)
288{
bartd45d9952009-05-31 18:53:54 +0000289 static DrdThreadId s_last_tid_printed = 1;
290 DrdThreadId* err_extra;
291
292 err_extra = VG_(get_error_extra)(e);
293
barte7086002011-10-11 19:08:39 +0000294 if (err_extra && *err_extra != s_last_tid_printed && !VG_(clo_xml)) {
sewardj1e29ebc2009-07-15 14:49:17 +0000295 VG_(umsg)("%s:\n", DRD_(thread_get_name)(*err_extra));
bartd45d9952009-05-31 18:53:54 +0000296 s_last_tid_printed = *err_extra;
297 }
bart61d36ff2009-07-27 14:17:33 +0000298}
bartd45d9952009-05-31 18:53:54 +0000299
bart61d36ff2009-07-27 14:17:33 +0000300/** Report an error to the user. */
301static void drd_tool_error_pp(Error* const e)
302{
barte7086002011-10-11 19:08:39 +0000303 const Bool xml = VG_(clo_xml);
florian19f91bb2012-11-10 22:29:54 +0000304 const HChar* const what_prefix = xml ? " <what>" : "";
305 const HChar* const what_suffix = xml ? "</what>" : "";
barte7086002011-10-11 19:08:39 +0000306
307 if (xml)
308 VG_(printf_xml)( " <kind>%pS</kind>\n", drd_get_error_name(e));
309
bartbedfd232009-03-26 19:07:15 +0000310 switch (VG_(get_error_kind)(e))
311 {
312 case DataRaceErr: {
313 drd_report_data_race(e, VG_(get_error_extra)(e));
314 break;
315 }
316 case MutexErr: {
317 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
318 tl_assert(p);
barte7086002011-10-11 19:08:39 +0000319 if (p->recursion_count >= 0) {
320 print_err_detail("%s%s: mutex 0x%lx, recursion count %d, owner %d."
321 "%s\n", what_prefix, VG_(get_error_string)(e),
322 p->mutex, p->recursion_count, p->owner, what_suffix);
323 } else {
324 print_err_detail("%sThe object at address 0x%lx is not a mutex.%s\n",
325 what_prefix, p->mutex, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000326 }
327 VG_(pp_ExeContext)(VG_(get_error_where)(e));
328 first_observed(p->mutex);
329 break;
330 }
331 case CondErr: {
332 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000333 print_err_detail("%s%s: cond 0x%lx%s\n", what_prefix,
334 VG_(get_error_string)(e), cdei->cond, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000335 VG_(pp_ExeContext)(VG_(get_error_where)(e));
336 first_observed(cdei->cond);
337 break;
338 }
339 case CondDestrErr: {
340 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000341 print_err_detail("%s%s: cond 0x%lx, mutex 0x%lx locked by thread %d%s\n",
342 what_prefix, VG_(get_error_string)(e), cdi->cond,
343 cdi->mutex, cdi->owner, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000344 VG_(pp_ExeContext)(VG_(get_error_where)(e));
345 first_observed(cdi->mutex);
346 break;
347 }
348 case CondRaceErr: {
349 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000350 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);
bartbedfd232009-03-26 19:07:15 +0000354 VG_(pp_ExeContext)(VG_(get_error_where)(e));
355 first_observed(cei->cond);
356 first_observed(cei->mutex);
357 break;
358 }
359 case CondWaitErr: {
360 CondWaitErrInfo* cwei = (CondWaitErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000361 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);
bartbedfd232009-03-26 19:07:15 +0000364 VG_(pp_ExeContext)(VG_(get_error_where)(e));
365 first_observed(cwei->cond);
366 first_observed(cwei->mutex1);
367 first_observed(cwei->mutex2);
368 break;
369 }
370 case SemaphoreErr: {
371 SemaphoreErrInfo* sei = (SemaphoreErrInfo*)(VG_(get_error_extra)(e));
372 tl_assert(sei);
bartb6ab69e2011-10-13 16:47:53 +0000373 print_err_detail("%s%s: semaphore 0x%lx%s\n", what_prefix,
374 VG_(get_error_string)(e), sei->semaphore, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000375 VG_(pp_ExeContext)(VG_(get_error_where)(e));
376 first_observed(sei->semaphore);
377 break;
378 }
379 case BarrierErr: {
380 BarrierErrInfo* bei = (BarrierErrInfo*)(VG_(get_error_extra)(e));
381 tl_assert(bei);
barte7086002011-10-11 19:08:39 +0000382 print_err_detail("%s%s: barrier 0x%lx%s\n", what_prefix,
383 VG_(get_error_string)(e), bei->barrier, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000384 VG_(pp_ExeContext)(VG_(get_error_where)(e));
barte7086002011-10-11 19:08:39 +0000385 if (bei->other_context) {
386 if (xml)
387 print_err_detail(" <confl_wait_call>\n");
388 print_err_detail("%sConflicting wait call by thread %d:%s\n",
389 what_prefix, bei->other_tid, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000390 VG_(pp_ExeContext)(bei->other_context);
barte7086002011-10-11 19:08:39 +0000391 if (xml)
392 print_err_detail(" </confl_wait_call>\n");
bartbedfd232009-03-26 19:07:15 +0000393 }
394 first_observed(bei->barrier);
395 break;
396 }
397 case RwlockErr: {
398 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
399 tl_assert(p);
barte7086002011-10-11 19:08:39 +0000400 print_err_detail("%s%s: rwlock 0x%lx.%s\n", what_prefix,
401 VG_(get_error_string)(e), p->rwlock, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000402 VG_(pp_ExeContext)(VG_(get_error_where)(e));
403 first_observed(p->rwlock);
404 break;
405 }
406 case HoldtimeErr: {
407 HoldtimeErrInfo* p =(HoldtimeErrInfo*)(VG_(get_error_extra)(e));
408 tl_assert(p);
409 tl_assert(p->acquired_at);
barte7086002011-10-11 19:08:39 +0000410 if (xml)
411 print_err_detail(" <acquired_at>\n");
412 else
413 print_err_detail("Acquired at:\n");
bartbedfd232009-03-26 19:07:15 +0000414 VG_(pp_ExeContext)(p->acquired_at);
bartb6ab69e2011-10-13 16:47:53 +0000415 if (xml)
416 print_err_detail(" </acquired_at>\n");
barte7086002011-10-11 19:08:39 +0000417 print_err_detail("%sLock on %s 0x%lx was held during %d ms"
418 " (threshold: %d ms).%s\n", what_prefix,
419 VG_(get_error_string)(e), p->synchronization_object,
420 p->hold_time_ms, p->threshold_ms, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000421 VG_(pp_ExeContext)(VG_(get_error_where)(e));
422 first_observed(p->synchronization_object);
423 break;
424 }
425 case GenericErr: {
bart62cc2322010-03-07 10:54:21 +0000426 GenericErrInfo* gei = (GenericErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000427 print_err_detail("%s%s%s\n", what_prefix, VG_(get_error_string)(e),
428 what_suffix);
bartbedfd232009-03-26 19:07:15 +0000429 VG_(pp_ExeContext)(VG_(get_error_where)(e));
bart62cc2322010-03-07 10:54:21 +0000430 if (gei->addr)
431 first_observed(gei->addr);
bartbedfd232009-03-26 19:07:15 +0000432 break;
433 }
bartb48bde22009-07-31 08:26:17 +0000434 case InvalidThreadId: {
435 InvalidThreadIdInfo* iti =(InvalidThreadIdInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000436 print_err_detail("%s%s 0x%llx%s\n", what_prefix, VG_(get_error_string)(e),
437 iti->ptid, what_suffix);
bartb48bde22009-07-31 08:26:17 +0000438 VG_(pp_ExeContext)(VG_(get_error_where)(e));
439 break;
440 }
bart141a7ed2010-03-21 17:28:10 +0000441 case UnimpHgClReq: {
bart66f196d2009-08-15 10:50:35 +0000442 UnimpClReqInfo* uicr =(UnimpClReqInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000443 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);
bart66f196d2009-08-15 10:50:35 +0000446 VG_(pp_ExeContext)(VG_(get_error_where)(e));
447 break;
448 }
bart141a7ed2010-03-21 17:28:10 +0000449 case UnimpDrdClReq: {
450 UnimpClReqInfo* uicr =(UnimpClReqInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000451 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);
bart141a7ed2010-03-21 17:28:10 +0000454 VG_(pp_ExeContext)(VG_(get_error_where)(e));
455 break;
456 }
bartbedfd232009-03-26 19:07:15 +0000457 default:
barte7086002011-10-11 19:08:39 +0000458 print_err_detail("%s%s%s\n", what_prefix, VG_(get_error_string)(e),
459 what_suffix);
bartbedfd232009-03-26 19:07:15 +0000460 VG_(pp_ExeContext)(VG_(get_error_where)(e));
461 break;
462 }
sewardjaf44c822007-11-25 14:01:38 +0000463}
464
bartd2c5eae2009-02-21 15:27:04 +0000465static UInt drd_tool_error_update_extra(Error* e)
sewardjaf44c822007-11-25 14:01:38 +0000466{
bartbedfd232009-03-26 19:07:15 +0000467 switch (VG_(get_error_kind)(e))
468 {
469 case DataRaceErr:
470 return sizeof(DataRaceErrInfo);
471 case MutexErr:
472 return sizeof(MutexErrInfo);
473 case CondErr:
474 return sizeof(CondErrInfo);
475 case CondDestrErr:
476 return sizeof(CondDestrErrInfo);
477 case CondRaceErr:
478 return sizeof(CondRaceErrInfo);
479 case CondWaitErr:
480 return sizeof(CondWaitErrInfo);
481 case SemaphoreErr:
482 return sizeof(SemaphoreErrInfo);
483 case BarrierErr:
484 return sizeof(BarrierErrInfo);
485 case RwlockErr:
486 return sizeof(RwlockErrInfo);
487 case HoldtimeErr:
488 return sizeof(HoldtimeErrInfo);
489 case GenericErr:
490 return sizeof(GenericErrInfo);
bartb48bde22009-07-31 08:26:17 +0000491 case InvalidThreadId:
492 return sizeof(InvalidThreadIdInfo);
bart141a7ed2010-03-21 17:28:10 +0000493 case UnimpHgClReq:
494 return sizeof(UnimpClReqInfo);
495 case UnimpDrdClReq:
bart66f196d2009-08-15 10:50:35 +0000496 return sizeof(UnimpClReqInfo);
bartbedfd232009-03-26 19:07:15 +0000497 default:
498 tl_assert(False);
499 break;
500 }
sewardjaf44c822007-11-25 14:01:38 +0000501}
502
bart61d36ff2009-07-27 14:17:33 +0000503/**
504 * Parse suppression name.
505 *
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.
509 */
florian19f91bb2012-11-10 22:29:54 +0000510static Bool drd_is_recognized_suppression(const HChar* const name,
511 Supp* const supp)
sewardjaf44c822007-11-25 14:01:38 +0000512{
bart61d36ff2009-07-27 14:17:33 +0000513 DrdErrorKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000514
bartbedfd232009-03-26 19:07:15 +0000515 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000516 skind = DataRaceErr;
bartbedfd232009-03-26 19:07:15 +0000517 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000518 skind = MutexErr;
bartbedfd232009-03-26 19:07:15 +0000519 else if (VG_(strcmp)(name, STR_CondErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000520 skind = CondErr;
bartbedfd232009-03-26 19:07:15 +0000521 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000522 skind = CondDestrErr;
bartbedfd232009-03-26 19:07:15 +0000523 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000524 skind = CondRaceErr;
bartbedfd232009-03-26 19:07:15 +0000525 else if (VG_(strcmp)(name, STR_CondWaitErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000526 skind = CondWaitErr;
bartbedfd232009-03-26 19:07:15 +0000527 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000528 skind = SemaphoreErr;
bartbedfd232009-03-26 19:07:15 +0000529 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000530 skind = BarrierErr;
bartbedfd232009-03-26 19:07:15 +0000531 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000532 skind = RwlockErr;
bartbedfd232009-03-26 19:07:15 +0000533 else if (VG_(strcmp)(name, STR_HoldtimeErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000534 skind = HoldtimeErr;
bartbedfd232009-03-26 19:07:15 +0000535 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000536 skind = GenericErr;
bartb48bde22009-07-31 08:26:17 +0000537 else if (VG_(strcmp)(name, STR_InvalidThreadId) == 0)
538 skind = InvalidThreadId;
bart141a7ed2010-03-21 17:28:10 +0000539 else if (VG_(strcmp)(name, STR_UnimpHgClReq) == 0)
540 skind = UnimpHgClReq;
541 else if (VG_(strcmp)(name, STR_UnimpDrdClReq) == 0)
542 skind = UnimpDrdClReq;
bartbedfd232009-03-26 19:07:15 +0000543 else
544 return False;
sewardjaf44c822007-11-25 14:01:38 +0000545
bartbedfd232009-03-26 19:07:15 +0000546 VG_(set_supp_kind)(supp, skind);
547 return True;
sewardjaf44c822007-11-25 14:01:38 +0000548}
549
bart61d36ff2009-07-27 14:17:33 +0000550/**
551 * Read additional suppression information from the suppression file.
552 *
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.
556 */
bart246fbf22009-02-15 14:46:17 +0000557static
florian19f91bb2012-11-10 22:29:54 +0000558Bool drd_read_extra_suppression_info(Int fd, HChar** bufpp,
philippe362441d2013-07-22 22:00:13 +0000559 SizeT* nBufp, Int* lineno, Supp* supp)
sewardjaf44c822007-11-25 14:01:38 +0000560{
bartbedfd232009-03-26 19:07:15 +0000561 return True;
sewardjaf44c822007-11-25 14:01:38 +0000562}
563
bart61d36ff2009-07-27 14:17:33 +0000564/**
565 * Determine whether or not the types of the given error message and the
566 * given suppression match.
567 */
568static Bool drd_error_matches_suppression(Error* const e, Supp* const supp)
sewardjaf44c822007-11-25 14:01:38 +0000569{
bart61d36ff2009-07-27 14:17:33 +0000570 return VG_(get_supp_kind)(supp) == VG_(get_error_kind)(e);
sewardjaf44c822007-11-25 14:01:38 +0000571}
572
floriane543f302012-10-21 19:43:43 +0000573static const HChar* drd_get_error_name(Error* e)
sewardjaf44c822007-11-25 14:01:38 +0000574{
bartbedfd232009-03-26 19:07:15 +0000575 switch (VG_(get_error_kind)(e))
576 {
577 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
578 case MutexErr: return VGAPPEND(STR_, MutexErr);
579 case CondErr: return VGAPPEND(STR_, CondErr);
580 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
581 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
582 case CondWaitErr: return VGAPPEND(STR_, CondWaitErr);
583 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
584 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
585 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
586 case HoldtimeErr: return VGAPPEND(STR_, HoldtimeErr);
587 case GenericErr: return VGAPPEND(STR_, GenericErr);
bartb48bde22009-07-31 08:26:17 +0000588 case InvalidThreadId: return VGAPPEND(STR_, InvalidThreadId);
bart141a7ed2010-03-21 17:28:10 +0000589 case UnimpHgClReq: return VGAPPEND(STR_, UnimpHgClReq);
590 case UnimpDrdClReq: return VGAPPEND(STR_, UnimpDrdClReq);
bartbedfd232009-03-26 19:07:15 +0000591 default:
592 tl_assert(0);
593 }
594 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000595}
596
bart61d36ff2009-07-27 14:17:33 +0000597/**
sewardj588adef2009-08-15 22:41:51 +0000598 * Return extra suppression information.
bart61d36ff2009-07-27 14:17:33 +0000599 *
600 * Invoked while printing a suppression pattern because the user
601 * specified --gen-suppressions=yes or all on the command line. DRD does not
602 * define any 'extra' suppression information.
603 */
sewardj588adef2009-08-15 22:41:51 +0000604static
605Bool drd_get_extra_suppression_info(Error* e,
floriandbb35842012-10-27 18:39:11 +0000606 /*OUT*/HChar* buf, Int nBuf)
sewardj588adef2009-08-15 22:41:51 +0000607{
608 return False;
609}
sewardjaf44c822007-11-25 14:01:38 +0000610
philippe4e32d672013-10-17 22:10:41 +0000611static
612Bool drd_print_extra_suppression_use(Supp* su,
613 /*OUT*/HChar* buf, Int nBuf)
614{
615 return False;
616}
617
618static
619void drd_update_extra_suppresion_use(Error* e, Supp* supp)
620{
621 return;
622}
623
bart61d36ff2009-07-27 14:17:33 +0000624/** Tell the Valgrind core about DRD's error handlers. */
bart1335ecc2009-02-14 16:10:53 +0000625void DRD_(register_error_handlers)(void)
sewardjaf44c822007-11-25 14:01:38 +0000626{
bart61d36ff2009-07-27 14:17:33 +0000627 VG_(needs_tool_errors)(drd_compare_error_contexts,
sewardj1e29ebc2009-07-15 14:49:17 +0000628 drd_tool_error_before_pp,
bartbedfd232009-03-26 19:07:15 +0000629 drd_tool_error_pp,
bartd45d9952009-05-31 18:53:54 +0000630 False,
bartbedfd232009-03-26 19:07:15 +0000631 drd_tool_error_update_extra,
bart61d36ff2009-07-27 14:17:33 +0000632 drd_is_recognized_suppression,
633 drd_read_extra_suppression_info,
634 drd_error_matches_suppression,
635 drd_get_error_name,
philippe4e32d672013-10-17 22:10:41 +0000636 drd_get_extra_suppression_info,
637 drd_print_extra_suppression_use,
638 drd_update_extra_suppresion_use);
sewardjaf44c822007-11-25 14:01:38 +0000639}