blob: 0dfab2c4775a741ade61cb449d528256f339ec4b [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
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 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() */
bart886b87c2008-06-28 13:40:41 +000035#include "pub_tool_libcprint.h" /* VG_(printf)() */
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_machine.h"
bart886b87c2008-06-28 13:40:41 +000037#include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */
bartb92ff0f2011-10-08 08:29:29 +000038#include "pub_tool_options.h" /* VG_(clo_xml) */
bart886b87c2008-06-28 13:40:41 +000039#include "pub_tool_threadstate.h" /* VG_(get_pthread_id)() */
40#include "pub_tool_tooliface.h" /* VG_(needs_tool_errors)() */
sewardjaf44c822007-11-25 14:01:38 +000041
42
barte7086002011-10-11 19:08:39 +000043/* Local function declarations. */
44
florian8e3fbb52014-10-20 19:02:38 +000045static const HChar* drd_get_error_name(const Error* e);
barte7086002011-10-11 19:08:39 +000046
47
bart16d76e52008-03-18 17:08:08 +000048/* Local variables. */
49
bartd2c5eae2009-02-21 15:27:04 +000050static Bool s_show_conflicting_segments = True;
bart16d76e52008-03-18 17:08:08 +000051
52
bart246fbf22009-02-15 14:46:17 +000053void DRD_(set_show_conflicting_segments)(const Bool scs)
bart16d76e52008-03-18 17:08:08 +000054{
bartbedfd232009-03-26 19:07:15 +000055 s_show_conflicting_segments = scs;
bart16d76e52008-03-18 17:08:08 +000056}
57
florian19f91bb2012-11-10 22:29:54 +000058void DRD_(trace_msg)(const HChar* format, ...)
bartb92ff0f2011-10-08 08:29:29 +000059{
60 va_list vargs;
61 va_start(vargs, format);
barte7086002011-10-11 19:08:39 +000062 if (VG_(clo_xml)) {
bartad994e82011-10-13 18:04:30 +000063 VG_(printf_xml)(" <trace><text>");
barte7086002011-10-11 19:08:39 +000064 VG_(vprintf_xml)(format, vargs);
bartad994e82011-10-13 18:04:30 +000065 VG_(printf_xml)("</text></trace>\n");
barte7086002011-10-11 19:08:39 +000066 } else {
67 VG_(vmessage)(Vg_UserMsg, format, vargs);
bartad994e82011-10-13 18:04:30 +000068 VG_(message)(Vg_UserMsg, "\n");
barte7086002011-10-11 19:08:39 +000069 }
70 va_end(vargs);
71}
72
florian19f91bb2012-11-10 22:29:54 +000073void DRD_(trace_msg_w_bt)(const HChar* format, ...)
bartad994e82011-10-13 18:04:30 +000074{
75 va_list vargs;
76 va_start(vargs, format);
77 if (VG_(clo_xml)) {
78 VG_(printf_xml)(" <trace><text>");
79 VG_(vprintf_xml)(format, vargs);
80 VG_(printf_xml)("</text>\n");
81 } else {
82 VG_(vmessage)(Vg_UserMsg, format, vargs);
83 VG_(message)(Vg_UserMsg, "\n");
84 }
85 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), VG_(clo_backtrace_size));
86 va_end(vargs);
87 if (VG_(clo_xml))
88 VG_(printf_xml)(" </trace>\n");
89}
90
barte7086002011-10-11 19:08:39 +000091/**
92 * Emit error message detail in the format requested by the user.
93 */
florian19f91bb2012-11-10 22:29:54 +000094static void print_err_detail(const HChar* format, ...) PRINTF_CHECK(1, 2);
95static void print_err_detail(const HChar* format, ...)
barte7086002011-10-11 19:08:39 +000096{
97 va_list vargs;
98 va_start(vargs, format);
99 if (VG_(clo_xml))
100 VG_(vprintf_xml)(format, vargs);
101 else
102 VG_(vmessage)(Vg_UserMsg, format, vargs);
bartb92ff0f2011-10-08 08:29:29 +0000103 va_end(vargs);
104}
105
bart246fbf22009-02-15 14:46:17 +0000106/**
bart61d36ff2009-07-27 14:17:33 +0000107 * Describe the client address a as good as possible, putting the result in ai.
bart886b87c2008-06-28 13:40:41 +0000108 */
bartff1252a2008-03-16 17:27:25 +0000109static
bart61d36ff2009-07-27 14:17:33 +0000110void describe_malloced_addr(Addr const a, AddrInfo* const ai)
sewardjaf44c822007-11-25 14:01:38 +0000111{
bart61d36ff2009-07-27 14:17:33 +0000112 Addr heap_block_start;
sewardjaf44c822007-11-25 14:01:38 +0000113
bart61d36ff2009-07-27 14:17:33 +0000114 if (DRD_(heap_addrinfo)(a, &heap_block_start, &ai->size, &ai->lastchange))
bartbedfd232009-03-26 19:07:15 +0000115 {
116 ai->akind = eMallocd;
bart61d36ff2009-07-27 14:17:33 +0000117 ai->rwoffset = a - heap_block_start;
bartbedfd232009-03-26 19:07:15 +0000118 }
119 else
120 {
121 ai->akind = eUnknown;
122 }
sewardjaf44c822007-11-25 14:01:38 +0000123}
124
bart246fbf22009-02-15 14:46:17 +0000125/**
bart61d36ff2009-07-27 14:17:33 +0000126 * Report where a client synchronization object has been observed for the first
127 * time. The printed call stack will either refer to a pthread_*_init() or a
128 * pthread_*lock() call.
bart886b87c2008-06-28 13:40:41 +0000129 */
bartd2c5eae2009-02-21 15:27:04 +0000130static void first_observed(const Addr obj)
bart886b87c2008-06-28 13:40:41 +0000131{
bartbedfd232009-03-26 19:07:15 +0000132 DrdClientobj* cl;
bart886b87c2008-06-28 13:40:41 +0000133
bartbedfd232009-03-26 19:07:15 +0000134 cl = DRD_(clientobj_get_any)(obj);
barte7086002011-10-11 19:08:39 +0000135 if (cl) {
bartbedfd232009-03-26 19:07:15 +0000136 tl_assert(cl->any.first_observed_at);
barte7086002011-10-11 19:08:39 +0000137 if (VG_(clo_xml)) {
138 print_err_detail(" <first_observed_at>\n"
139 " <what>%pS</what>\n"
140 " <address>0x%lx</address>\n",
141 DRD_(clientobj_type_name)(cl->any.type), obj);
142 VG_(pp_ExeContext)(cl->any.first_observed_at);
143 print_err_detail(" </first_observed_at>\n");
144 } else {
145 print_err_detail("%s 0x%lx was first observed at:\n",
146 DRD_(clientobj_type_name)(cl->any.type), obj);
147 VG_(pp_ExeContext)(cl->any.first_observed_at);
148 }
bartbedfd232009-03-26 19:07:15 +0000149 }
bart886b87c2008-06-28 13:40:41 +0000150}
151
sewardjaf44c822007-11-25 14:01:38 +0000152static
florian8e3fbb52014-10-20 19:02:38 +0000153void drd_report_data_race(const Error* const err,
154 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);
bartb515eb12008-03-07 18:52:38 +0000174
sewardj1e29ebc2009-07-15 14:49:17 +0000175 (void) VG_(get_data_description)(descr1, descr2, dri->addr);
florianad4e9792015-07-05 21:53:33 +0000176 /* If there's nothing in descr1/2, free them. Why is it safe to
sewardj1e29ebc2009-07-15 14:49:17 +0000177 VG_(indexXA) at zero here? Because VG_(get_data_description)
178 guarantees to zero terminate descr1/2 regardless of the outcome
179 of the call. So there's always at least one element in each XA
180 after the call.
181 */
182 if (0 == VG_(strlen)( VG_(indexXA)( descr1, 0 ))) {
183 VG_(deleteXA)( descr1 );
184 descr1 = NULL;
185 }
186 if (0 == VG_(strlen)( VG_(indexXA)( descr2, 0 ))) {
187 VG_(deleteXA)( descr2 );
188 descr2 = NULL;
189 }
190 /* Assume (assert) that VG_(get_data_description) fills in descr1
191 before it fills in descr2 */
192 if (descr1 == NULL)
193 tl_assert(descr2 == NULL);
194 /* So anyway. Do we have something useful? */
195 if (descr1 == NULL)
bartbedfd232009-03-26 19:07:15 +0000196 {
sewardj1e29ebc2009-07-15 14:49:17 +0000197 /* No. Do Plan B. */
bart61d36ff2009-07-27 14:17:33 +0000198 describe_malloced_addr(dri->addr, &ai);
bartbedfd232009-03-26 19:07:15 +0000199 }
barte7086002011-10-11 19:08:39 +0000200
florianea71ffb2015-08-05 14:38:57 +0000201 print_err_detail("%sConflicting %s by thread %u at 0x%08lx size %lu%s\n",
barte7086002011-10-11 19:08:39 +0000202 what_prefix, dri->access_type == eStore ? "store" : "load",
203 dri->tid, dri->addr, dri->size, what_suffix);
204
bartbedfd232009-03-26 19:07:15 +0000205 VG_(pp_ExeContext)(VG_(get_error_where)(err));
barte7086002011-10-11 19:08:39 +0000206 if (descr1 != NULL) {
207 print_err_detail("%s%s\n", indent, (HChar*)VG_(indexXA)(descr1, 0));
sewardj1e29ebc2009-07-15 14:49:17 +0000208 if (descr2 != NULL)
barte7086002011-10-11 19:08:39 +0000209 print_err_detail("%s%s\n", indent, (HChar*)VG_(indexXA)(descr2, 0));
210 } else if (ai.akind == eMallocd && ai.lastchange) {
bartca7285e2011-10-13 16:42:49 +0000211 print_err_detail("%sAddress 0x%lx is at offset %ld from 0x%lx.%s%s",
212 auxwhat_prefix, dri->addr, ai.rwoffset,
213 dri->addr - ai.rwoffset, auxwhat_suffix,
214 xml ? "\n" : "");
barte7086002011-10-11 19:08:39 +0000215 if (xml)
bartca7285e2011-10-13 16:42:49 +0000216 print_err_detail(" <allocation_context>\n");
barte7086002011-10-11 19:08:39 +0000217 else
218 print_err_detail(" Allocation context:\n");
bartbedfd232009-03-26 19:07:15 +0000219 VG_(pp_ExeContext)(ai.lastchange);
barte7086002011-10-11 19:08:39 +0000220 if (xml)
bartca7285e2011-10-13 16:42:49 +0000221 print_err_detail(" </allocation_context>\n");
barte7086002011-10-11 19:08:39 +0000222 } else {
floriane08950b2014-11-13 21:41:28 +0000223 const HChar *sect_name;
bartbedfd232009-03-26 19:07:15 +0000224 VgSectKind sect_kind;
bartf993b872008-04-01 18:19:50 +0000225
floriane08950b2014-11-13 21:41:28 +0000226 sect_kind = VG_(DebugInfo_sect_kind)(&sect_name, dri->addr);
bartca7285e2011-10-13 16:42:49 +0000227 if (sect_kind != Vg_SectUnknown) {
228 print_err_detail("%sAllocation context: %ps section of %ps%s\n",
229 auxwhat_prefix, VG_(pp_SectKind)(sect_kind),
230 sect_name, auxwhat_suffix);
barte7086002011-10-11 19:08:39 +0000231 } else {
bartca7285e2011-10-13 16:42:49 +0000232 print_err_detail("%sAllocation context: unknown.%s\n",
233 auxwhat_prefix, auxwhat_suffix);
bartbedfd232009-03-26 19:07:15 +0000234 }
235 }
236 if (s_show_conflicting_segments)
237 {
238 DRD_(thread_report_conflicting_segments)(dri->tid,
239 dri->addr, dri->size,
240 dri->access_type);
241 }
242
sewardj1e29ebc2009-07-15 14:49:17 +0000243 if (descr2)
244 VG_(deleteXA)(descr2);
245 if (descr1)
246 VG_(deleteXA)(descr1);
sewardjaf44c822007-11-25 14:01:38 +0000247}
248
bart61d36ff2009-07-27 14:17:33 +0000249/**
250 * Compare two error contexts. The core function VG_(maybe_record_error)()
251 * calls this function to compare error contexts such that errors that occur
bart31b983d2010-02-21 14:52:59 +0000252 * repeatedly are only printed once. This function is only called by the core
bart61d36ff2009-07-27 14:17:33 +0000253 * if the error kind of e1 and e2 matches and if the ExeContext's of e1 and
254 * e2 also match.
255 */
florian8e3fbb52014-10-20 19:02:38 +0000256static Bool drd_compare_error_contexts(VgRes res, const Error* e1,
257 const Error* e2)
sewardjaf44c822007-11-25 14:01:38 +0000258{
bartb80fa962009-07-31 08:45:02 +0000259 tl_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2));
260
261 switch (VG_(get_error_kind)(e1))
262 {
263 case DataRaceErr:
264 {
265 const DataRaceErrInfo* const dri1 = VG_(get_error_extra)(e1);
266 const DataRaceErrInfo* const dri2 = VG_(get_error_extra)(e2);
267 return dri1->access_type == dri2->access_type
268 && dri1->size == dri2->size;
269 }
bart94fb8d22009-07-31 18:45:49 +0000270 case MutexErr:
271 {
272 const MutexErrInfo* const mei1 = VG_(get_error_extra)(e1);
273 const MutexErrInfo* const mei2 = VG_(get_error_extra)(e2);
274 return mei1->mutex == mei2->mutex;
275 }
bartb80fa962009-07-31 08:45:02 +0000276 default:
277 return True;
278 }
sewardjaf44c822007-11-25 14:01:38 +0000279}
280
bart61d36ff2009-07-27 14:17:33 +0000281/**
282 * Called by the core just before an error message will be printed. Used by
283 * DRD to print the thread number as a preamble.
284 */
florian8e3fbb52014-10-20 19:02:38 +0000285static void drd_tool_error_before_pp(const Error* const e)
sewardj1e29ebc2009-07-15 14:49:17 +0000286{
bartd45d9952009-05-31 18:53:54 +0000287 static DrdThreadId s_last_tid_printed = 1;
288 DrdThreadId* err_extra;
289
290 err_extra = VG_(get_error_extra)(e);
291
barte7086002011-10-11 19:08:39 +0000292 if (err_extra && *err_extra != s_last_tid_printed && !VG_(clo_xml)) {
sewardj1e29ebc2009-07-15 14:49:17 +0000293 VG_(umsg)("%s:\n", DRD_(thread_get_name)(*err_extra));
bartd45d9952009-05-31 18:53:54 +0000294 s_last_tid_printed = *err_extra;
295 }
bart61d36ff2009-07-27 14:17:33 +0000296}
bartd45d9952009-05-31 18:53:54 +0000297
bart61d36ff2009-07-27 14:17:33 +0000298/** Report an error to the user. */
florian8e3fbb52014-10-20 19:02:38 +0000299static void drd_tool_error_pp(const Error* const e)
bart61d36ff2009-07-27 14:17:33 +0000300{
barte7086002011-10-11 19:08:39 +0000301 const Bool xml = VG_(clo_xml);
florian19f91bb2012-11-10 22:29:54 +0000302 const HChar* const what_prefix = xml ? " <what>" : "";
303 const HChar* const what_suffix = xml ? "</what>" : "";
barte7086002011-10-11 19:08:39 +0000304
305 if (xml)
306 VG_(printf_xml)( " <kind>%pS</kind>\n", drd_get_error_name(e));
307
bartbedfd232009-03-26 19:07:15 +0000308 switch (VG_(get_error_kind)(e))
309 {
310 case DataRaceErr: {
311 drd_report_data_race(e, VG_(get_error_extra)(e));
312 break;
313 }
314 case MutexErr: {
315 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
316 tl_assert(p);
barte7086002011-10-11 19:08:39 +0000317 if (p->recursion_count >= 0) {
florianea71ffb2015-08-05 14:38:57 +0000318 print_err_detail("%s%s: mutex 0x%lx, recursion count %d, owner %u."
barte7086002011-10-11 19:08:39 +0000319 "%s\n", what_prefix, VG_(get_error_string)(e),
320 p->mutex, p->recursion_count, p->owner, what_suffix);
321 } else {
322 print_err_detail("%sThe object at address 0x%lx is not a mutex.%s\n",
323 what_prefix, p->mutex, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000324 }
325 VG_(pp_ExeContext)(VG_(get_error_where)(e));
326 first_observed(p->mutex);
327 break;
328 }
329 case CondErr: {
330 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000331 print_err_detail("%s%s: cond 0x%lx%s\n", what_prefix,
332 VG_(get_error_string)(e), cdei->cond, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000333 VG_(pp_ExeContext)(VG_(get_error_where)(e));
334 first_observed(cdei->cond);
335 break;
336 }
337 case CondDestrErr: {
338 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
florianea71ffb2015-08-05 14:38:57 +0000339 print_err_detail("%s%s: cond 0x%lx, mutex 0x%lx locked by thread %u%s\n",
barte7086002011-10-11 19:08:39 +0000340 what_prefix, VG_(get_error_string)(e), cdi->cond,
341 cdi->mutex, cdi->owner, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000342 VG_(pp_ExeContext)(VG_(get_error_where)(e));
343 first_observed(cdi->mutex);
344 break;
345 }
346 case CondRaceErr: {
347 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000348 print_err_detail("%sProbably a race condition: condition variable 0x%lx"
349 " has been signaled but the associated mutex 0x%lx is"
350 " not locked by the signalling thread.%s\n",
351 what_prefix, cei->cond, cei->mutex, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000352 VG_(pp_ExeContext)(VG_(get_error_where)(e));
353 first_observed(cei->cond);
354 first_observed(cei->mutex);
355 break;
356 }
357 case CondWaitErr: {
358 CondWaitErrInfo* cwei = (CondWaitErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000359 print_err_detail("%s%s: condition variable 0x%lx, mutexes 0x%lx and"
360 " 0x%lx%s\n", what_prefix, VG_(get_error_string)(e),
361 cwei->cond, cwei->mutex1, cwei->mutex2, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000362 VG_(pp_ExeContext)(VG_(get_error_where)(e));
363 first_observed(cwei->cond);
364 first_observed(cwei->mutex1);
365 first_observed(cwei->mutex2);
366 break;
367 }
368 case SemaphoreErr: {
369 SemaphoreErrInfo* sei = (SemaphoreErrInfo*)(VG_(get_error_extra)(e));
370 tl_assert(sei);
bartb6ab69e2011-10-13 16:47:53 +0000371 print_err_detail("%s%s: semaphore 0x%lx%s\n", what_prefix,
372 VG_(get_error_string)(e), sei->semaphore, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000373 VG_(pp_ExeContext)(VG_(get_error_where)(e));
374 first_observed(sei->semaphore);
375 break;
376 }
377 case BarrierErr: {
378 BarrierErrInfo* bei = (BarrierErrInfo*)(VG_(get_error_extra)(e));
379 tl_assert(bei);
barte7086002011-10-11 19:08:39 +0000380 print_err_detail("%s%s: barrier 0x%lx%s\n", what_prefix,
381 VG_(get_error_string)(e), bei->barrier, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000382 VG_(pp_ExeContext)(VG_(get_error_where)(e));
barte7086002011-10-11 19:08:39 +0000383 if (bei->other_context) {
384 if (xml)
385 print_err_detail(" <confl_wait_call>\n");
florianea71ffb2015-08-05 14:38:57 +0000386 print_err_detail("%sConflicting wait call by thread %u:%s\n",
barte7086002011-10-11 19:08:39 +0000387 what_prefix, bei->other_tid, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000388 VG_(pp_ExeContext)(bei->other_context);
barte7086002011-10-11 19:08:39 +0000389 if (xml)
390 print_err_detail(" </confl_wait_call>\n");
bartbedfd232009-03-26 19:07:15 +0000391 }
392 first_observed(bei->barrier);
393 break;
394 }
395 case RwlockErr: {
396 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
397 tl_assert(p);
barte7086002011-10-11 19:08:39 +0000398 print_err_detail("%s%s: rwlock 0x%lx.%s\n", what_prefix,
399 VG_(get_error_string)(e), p->rwlock, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000400 VG_(pp_ExeContext)(VG_(get_error_where)(e));
401 first_observed(p->rwlock);
402 break;
403 }
404 case HoldtimeErr: {
405 HoldtimeErrInfo* p =(HoldtimeErrInfo*)(VG_(get_error_extra)(e));
406 tl_assert(p);
407 tl_assert(p->acquired_at);
barte7086002011-10-11 19:08:39 +0000408 if (xml)
409 print_err_detail(" <acquired_at>\n");
410 else
411 print_err_detail("Acquired at:\n");
bartbedfd232009-03-26 19:07:15 +0000412 VG_(pp_ExeContext)(p->acquired_at);
bartb6ab69e2011-10-13 16:47:53 +0000413 if (xml)
414 print_err_detail(" </acquired_at>\n");
florianea71ffb2015-08-05 14:38:57 +0000415 print_err_detail("%sLock on %s 0x%lx was held during %u ms"
416 " (threshold: %u ms).%s\n", what_prefix,
barte7086002011-10-11 19:08:39 +0000417 VG_(get_error_string)(e), p->synchronization_object,
418 p->hold_time_ms, p->threshold_ms, what_suffix);
bartbedfd232009-03-26 19:07:15 +0000419 VG_(pp_ExeContext)(VG_(get_error_where)(e));
420 first_observed(p->synchronization_object);
421 break;
422 }
423 case GenericErr: {
bart62cc2322010-03-07 10:54:21 +0000424 GenericErrInfo* gei = (GenericErrInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000425 print_err_detail("%s%s%s\n", what_prefix, VG_(get_error_string)(e),
426 what_suffix);
bartbedfd232009-03-26 19:07:15 +0000427 VG_(pp_ExeContext)(VG_(get_error_where)(e));
bart62cc2322010-03-07 10:54:21 +0000428 if (gei->addr)
429 first_observed(gei->addr);
bartbedfd232009-03-26 19:07:15 +0000430 break;
431 }
bartb48bde22009-07-31 08:26:17 +0000432 case InvalidThreadId: {
433 InvalidThreadIdInfo* iti =(InvalidThreadIdInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000434 print_err_detail("%s%s 0x%llx%s\n", what_prefix, VG_(get_error_string)(e),
435 iti->ptid, what_suffix);
bartb48bde22009-07-31 08:26:17 +0000436 VG_(pp_ExeContext)(VG_(get_error_where)(e));
437 break;
438 }
bart141a7ed2010-03-21 17:28:10 +0000439 case UnimpHgClReq: {
bart66f196d2009-08-15 10:50:35 +0000440 UnimpClReqInfo* uicr =(UnimpClReqInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000441 print_err_detail("%sThe annotation macro %s has not yet been implemented"
442 " in %ps%s\n", what_prefix, uicr->descr,
443 "<valgrind/helgrind.h>", what_suffix);
bart66f196d2009-08-15 10:50:35 +0000444 VG_(pp_ExeContext)(VG_(get_error_where)(e));
445 break;
446 }
bart141a7ed2010-03-21 17:28:10 +0000447 case UnimpDrdClReq: {
448 UnimpClReqInfo* uicr =(UnimpClReqInfo*)(VG_(get_error_extra)(e));
barte7086002011-10-11 19:08:39 +0000449 print_err_detail("%sThe annotation macro %s has not yet been implemented"
450 " in %ps%s\n", what_prefix, uicr->descr,
451 "<valgrind/drd.h>", what_suffix);
bart141a7ed2010-03-21 17:28:10 +0000452 VG_(pp_ExeContext)(VG_(get_error_where)(e));
453 break;
454 }
bartbedfd232009-03-26 19:07:15 +0000455 default:
barte7086002011-10-11 19:08:39 +0000456 print_err_detail("%s%s%s\n", what_prefix, VG_(get_error_string)(e),
457 what_suffix);
bartbedfd232009-03-26 19:07:15 +0000458 VG_(pp_ExeContext)(VG_(get_error_where)(e));
459 break;
460 }
sewardjaf44c822007-11-25 14:01:38 +0000461}
462
florian8e3fbb52014-10-20 19:02:38 +0000463static UInt drd_tool_error_update_extra(const Error* e)
sewardjaf44c822007-11-25 14:01:38 +0000464{
bartbedfd232009-03-26 19:07:15 +0000465 switch (VG_(get_error_kind)(e))
466 {
467 case DataRaceErr:
468 return sizeof(DataRaceErrInfo);
469 case MutexErr:
470 return sizeof(MutexErrInfo);
471 case CondErr:
472 return sizeof(CondErrInfo);
473 case CondDestrErr:
474 return sizeof(CondDestrErrInfo);
475 case CondRaceErr:
476 return sizeof(CondRaceErrInfo);
477 case CondWaitErr:
478 return sizeof(CondWaitErrInfo);
479 case SemaphoreErr:
480 return sizeof(SemaphoreErrInfo);
481 case BarrierErr:
482 return sizeof(BarrierErrInfo);
483 case RwlockErr:
484 return sizeof(RwlockErrInfo);
485 case HoldtimeErr:
486 return sizeof(HoldtimeErrInfo);
487 case GenericErr:
488 return sizeof(GenericErrInfo);
bartb48bde22009-07-31 08:26:17 +0000489 case InvalidThreadId:
490 return sizeof(InvalidThreadIdInfo);
bart141a7ed2010-03-21 17:28:10 +0000491 case UnimpHgClReq:
492 return sizeof(UnimpClReqInfo);
493 case UnimpDrdClReq:
bart66f196d2009-08-15 10:50:35 +0000494 return sizeof(UnimpClReqInfo);
bartbedfd232009-03-26 19:07:15 +0000495 default:
496 tl_assert(False);
497 break;
498 }
sewardjaf44c822007-11-25 14:01:38 +0000499}
500
bart61d36ff2009-07-27 14:17:33 +0000501/**
502 * Parse suppression name.
503 *
504 * The suppression types recognized by DRD are the same types as the error
505 * types supported by DRD. So try to match the suppression name against the
506 * names of DRD error types.
507 */
florian19f91bb2012-11-10 22:29:54 +0000508static Bool drd_is_recognized_suppression(const HChar* const name,
509 Supp* const supp)
sewardjaf44c822007-11-25 14:01:38 +0000510{
bart61d36ff2009-07-27 14:17:33 +0000511 DrdErrorKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000512
bartbedfd232009-03-26 19:07:15 +0000513 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000514 skind = DataRaceErr;
bartbedfd232009-03-26 19:07:15 +0000515 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000516 skind = MutexErr;
bartbedfd232009-03-26 19:07:15 +0000517 else if (VG_(strcmp)(name, STR_CondErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000518 skind = CondErr;
bartbedfd232009-03-26 19:07:15 +0000519 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000520 skind = CondDestrErr;
bartbedfd232009-03-26 19:07:15 +0000521 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000522 skind = CondRaceErr;
bartbedfd232009-03-26 19:07:15 +0000523 else if (VG_(strcmp)(name, STR_CondWaitErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000524 skind = CondWaitErr;
bartbedfd232009-03-26 19:07:15 +0000525 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000526 skind = SemaphoreErr;
bartbedfd232009-03-26 19:07:15 +0000527 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000528 skind = BarrierErr;
bartbedfd232009-03-26 19:07:15 +0000529 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000530 skind = RwlockErr;
bartbedfd232009-03-26 19:07:15 +0000531 else if (VG_(strcmp)(name, STR_HoldtimeErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000532 skind = HoldtimeErr;
bartbedfd232009-03-26 19:07:15 +0000533 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
bart61d36ff2009-07-27 14:17:33 +0000534 skind = GenericErr;
bartb48bde22009-07-31 08:26:17 +0000535 else if (VG_(strcmp)(name, STR_InvalidThreadId) == 0)
536 skind = InvalidThreadId;
bart141a7ed2010-03-21 17:28:10 +0000537 else if (VG_(strcmp)(name, STR_UnimpHgClReq) == 0)
538 skind = UnimpHgClReq;
539 else if (VG_(strcmp)(name, STR_UnimpDrdClReq) == 0)
540 skind = UnimpDrdClReq;
bartbedfd232009-03-26 19:07:15 +0000541 else
542 return False;
sewardjaf44c822007-11-25 14:01:38 +0000543
bartbedfd232009-03-26 19:07:15 +0000544 VG_(set_supp_kind)(supp, skind);
545 return True;
sewardjaf44c822007-11-25 14:01:38 +0000546}
547
bart61d36ff2009-07-27 14:17:33 +0000548/**
549 * Read additional suppression information from the suppression file.
550 *
551 * None of the suppression patterns recognized by DRD has 'extra' lines
552 * of information in the suppression file, so just return True to indicate
553 * that reading the 'extra' lines succeeded.
554 */
bart246fbf22009-02-15 14:46:17 +0000555static
florian19f91bb2012-11-10 22:29:54 +0000556Bool drd_read_extra_suppression_info(Int fd, HChar** bufpp,
philippe362441d2013-07-22 22:00:13 +0000557 SizeT* nBufp, Int* lineno, Supp* supp)
sewardjaf44c822007-11-25 14:01:38 +0000558{
bartbedfd232009-03-26 19:07:15 +0000559 return True;
sewardjaf44c822007-11-25 14:01:38 +0000560}
561
bart61d36ff2009-07-27 14:17:33 +0000562/**
563 * Determine whether or not the types of the given error message and the
564 * given suppression match.
565 */
florian8e3fbb52014-10-20 19:02:38 +0000566static Bool drd_error_matches_suppression(const Error* const e,
567 const Supp* const supp)
sewardjaf44c822007-11-25 14:01:38 +0000568{
bart61d36ff2009-07-27 14:17:33 +0000569 return VG_(get_supp_kind)(supp) == VG_(get_error_kind)(e);
sewardjaf44c822007-11-25 14:01:38 +0000570}
571
florian8e3fbb52014-10-20 19:02:38 +0000572static const HChar* drd_get_error_name(const Error* e)
sewardjaf44c822007-11-25 14:01:38 +0000573{
bartbedfd232009-03-26 19:07:15 +0000574 switch (VG_(get_error_kind)(e))
575 {
576 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
577 case MutexErr: return VGAPPEND(STR_, MutexErr);
578 case CondErr: return VGAPPEND(STR_, CondErr);
579 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
580 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
581 case CondWaitErr: return VGAPPEND(STR_, CondWaitErr);
582 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
583 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
584 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
585 case HoldtimeErr: return VGAPPEND(STR_, HoldtimeErr);
586 case GenericErr: return VGAPPEND(STR_, GenericErr);
bartb48bde22009-07-31 08:26:17 +0000587 case InvalidThreadId: return VGAPPEND(STR_, InvalidThreadId);
bart141a7ed2010-03-21 17:28:10 +0000588 case UnimpHgClReq: return VGAPPEND(STR_, UnimpHgClReq);
589 case UnimpDrdClReq: return VGAPPEND(STR_, UnimpDrdClReq);
bartbedfd232009-03-26 19:07:15 +0000590 default:
591 tl_assert(0);
592 }
593 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000594}
595
bart61d36ff2009-07-27 14:17:33 +0000596/**
sewardj588adef2009-08-15 22:41:51 +0000597 * Return extra suppression information.
bart61d36ff2009-07-27 14:17:33 +0000598 *
599 * Invoked while printing a suppression pattern because the user
600 * specified --gen-suppressions=yes or all on the command line. DRD does not
601 * define any 'extra' suppression information.
602 */
sewardj588adef2009-08-15 22:41:51 +0000603static
florian8e3fbb52014-10-20 19:02:38 +0000604SizeT drd_get_extra_suppression_info(const Error* e,
florian3e81b8b2014-10-07 14:28:52 +0000605 /*OUT*/HChar* buf, Int nBuf)
sewardj588adef2009-08-15 22:41:51 +0000606{
florian3e81b8b2014-10-07 14:28:52 +0000607 tl_assert(nBuf >= 1);
608 buf[0] = '\0';
609 return 0;
sewardj588adef2009-08-15 22:41:51 +0000610}
sewardjaf44c822007-11-25 14:01:38 +0000611
philippe4e32d672013-10-17 22:10:41 +0000612static
florian8e3fbb52014-10-20 19:02:38 +0000613SizeT drd_print_extra_suppression_use(const Supp* su,
florian3e81b8b2014-10-07 14:28:52 +0000614 /*OUT*/HChar* buf, Int nBuf)
philippe4e32d672013-10-17 22:10:41 +0000615{
florian3e81b8b2014-10-07 14:28:52 +0000616 tl_assert(nBuf >= 1);
617 buf[0] = '\0';
618 return 0;
philippe4e32d672013-10-17 22:10:41 +0000619}
620
621static
florian8e3fbb52014-10-20 19:02:38 +0000622void drd_update_extra_suppresion_use(const Error* e, const Supp* supp)
philippe4e32d672013-10-17 22:10:41 +0000623{
624 return;
625}
626
bart61d36ff2009-07-27 14:17:33 +0000627/** Tell the Valgrind core about DRD's error handlers. */
bart1335ecc2009-02-14 16:10:53 +0000628void DRD_(register_error_handlers)(void)
sewardjaf44c822007-11-25 14:01:38 +0000629{
bart61d36ff2009-07-27 14:17:33 +0000630 VG_(needs_tool_errors)(drd_compare_error_contexts,
sewardj1e29ebc2009-07-15 14:49:17 +0000631 drd_tool_error_before_pp,
bartbedfd232009-03-26 19:07:15 +0000632 drd_tool_error_pp,
bartd45d9952009-05-31 18:53:54 +0000633 False,
bartbedfd232009-03-26 19:07:15 +0000634 drd_tool_error_update_extra,
bart61d36ff2009-07-27 14:17:33 +0000635 drd_is_recognized_suppression,
636 drd_read_extra_suppression_info,
637 drd_error_matches_suppression,
638 drd_get_error_name,
philippe4e32d672013-10-17 22:10:41 +0000639 drd_get_extra_suppression_info,
640 drd_print_extra_suppression_use,
641 drd_update_extra_suppresion_use);
sewardjaf44c822007-11-25 14:01:38 +0000642}