blob: 758df251948e4819809aaed1e2ae9bb5d9a7e422 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00003/*--- Management of error messages. m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njn24a6efb2005-06-20 03:36:51 +000032#include "pub_core_threadstate.h" // For VG_N_THREADS
njn75b65aa2005-06-19 19:25:44 +000033#include "pub_core_debugger.h"
njnea27e462005-05-31 02:38:09 +000034#include "pub_core_debuginfo.h"
njnd2b17112005-04-19 04:10:25 +000035#include "pub_core_errormgr.h"
njnd01fef72005-03-25 23:35:48 +000036#include "pub_core_execontext.h"
njn97405b22005-06-02 03:39:33 +000037#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000038#include "pub_core_libcassert.h"
njneb8896b2005-06-04 20:03:55 +000039#include "pub_core_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000040#include "pub_core_libcprint.h"
njn24a6efb2005-06-20 03:36:51 +000041#include "pub_core_libcproc.h" // For VG_(getpid)()
njnaf1d7df2005-06-11 01:31:52 +000042#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000043#include "pub_core_options.h"
njnd0d7c1f2005-06-21 00:33:19 +000044#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000045#include "pub_core_tooliface.h"
njn24a6efb2005-06-20 03:36:51 +000046#include "pub_core_translate.h" // for VG_(translate)()
sewardjde4a1d02002-03-22 01:27:54 +000047
48/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000049/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000050/*------------------------------------------------------------*/
51
njn14319cc2005-03-13 06:26:22 +000052/* After this many different unsuppressed errors have been observed,
53 be more conservative about collecting new ones. */
sewardj1d1a2262005-10-20 01:57:29 +000054#define M_COLLECT_ERRORS_SLOWLY_AFTER 100
njn14319cc2005-03-13 06:26:22 +000055
56/* After this many different unsuppressed errors have been observed,
57 stop collecting errors at all, and tell the user their program is
58 evidently a steaming pile of camel dung. */
sewardj1d1a2262005-10-20 01:57:29 +000059#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 1000
njn14319cc2005-03-13 06:26:22 +000060
61/* After this many total errors have been observed, stop collecting
62 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
sewardj1d1a2262005-10-20 01:57:29 +000063#define M_COLLECT_NO_ERRORS_AFTER_FOUND 100000
njn14319cc2005-03-13 06:26:22 +000064
sewardjde4a1d02002-03-22 01:27:54 +000065/* The list of error contexts found, both suppressed and unsuppressed.
66 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000067static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000068
69/* The list of suppression directives, as read from the specified
70 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000071static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000072
73/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000074static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000075
76/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000077static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000078
79/* forwards ... */
njn810086f2002-11-14 12:42:47 +000080static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000081
sewardjb5f6f512005-03-10 23:59:00 +000082static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000083
84/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000085/*--- Error type ---*/
86/*------------------------------------------------------------*/
87
nethercote996901a2004-08-03 13:29:09 +000088/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000089 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000090
91/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000092 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000093 Functions for getting/setting the tool-relevant fields are in
njnc7561b92005-06-19 01:24:32 +000094 include/pub_tool_errormgr.h.
nethercote4a184902004-08-02 12:21:09 +000095
96 When errors are found and recorded with VG_(maybe_record_error)(), all
97 the tool must do is pass in the four parameters; core will
98 allocate/initialise the error record.
99*/
100struct _Error {
101 struct _Error* next;
sewardjdbada272005-07-02 21:16:30 +0000102 // Unique tag. This gives the error a unique identity (handle) by
103 // which it can be referred to afterwords. Currently only used for
104 // XML printing.
105 UInt unique;
nethercote4a184902004-08-02 12:21:09 +0000106 // NULL if unsuppressed; or ptr to suppression record.
107 Supp* supp;
108 Int count;
109 ThreadId tid;
110
111 // The tool-specific part
112 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000113 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000114 Addr addr; // Used frequently
115 Char* string; // Used frequently
116 void* extra; // For any tool-specific extras
117};
118
119ExeContext* VG_(get_error_where) ( Error* err )
120{
121 return err->where;
122}
123
124ErrorKind VG_(get_error_kind) ( Error* err )
125{
126 return err->ekind;
127}
128
129Addr VG_(get_error_address) ( Error* err )
130{
131 return err->addr;
132}
133
134Char* VG_(get_error_string) ( Error* err )
135{
136 return err->string;
137}
138
139void* VG_(get_error_extra) ( Error* err )
140{
141 return err->extra;
142}
143
nethercotef2b11482004-08-02 12:36:01 +0000144UInt VG_(get_n_errs_found)( void )
145{
146 return n_errs_found;
147}
148
nethercote4a184902004-08-02 12:21:09 +0000149/*------------------------------------------------------------*/
150/*--- Suppression type ---*/
151/*------------------------------------------------------------*/
152
153/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
154 * effectively extend it by defining their own enums in the (0..) range. */
155typedef
156 enum {
157 PThreadSupp = -1, /* Matches PThreadErr */
158 }
159 CoreSuppKind;
160
sewardjb5f6f512005-03-10 23:59:00 +0000161/* Max number of callers for context in a suppression. */
162#define VG_MAX_SUPP_CALLERS 24
163
nethercote4a184902004-08-02 12:21:09 +0000164/* For each caller specified for a suppression, record the nature of
165 the caller name. Not of interest to tools. */
166typedef
167 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000168 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000169 ObjName, /* Name is of an shared object file. */
170 FunName /* Name is of a function. */
171 }
172 SuppLocTy;
173
sewardjb5f6f512005-03-10 23:59:00 +0000174typedef
175 struct {
176 SuppLocTy ty;
177 Char* name;
178 }
179 SuppLoc;
180
nethercote4a184902004-08-02 12:21:09 +0000181/* Suppressions. Tools can get/set tool-relevant parts with functions
njnc7561b92005-06-19 01:24:32 +0000182 declared in include/pub_tool_errormgr.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000183 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000184 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000185struct _Supp {
186 struct _Supp* next;
187 Int count; // The number of times this error has been suppressed.
188 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000189
190 // Length of 'callers'
191 Int n_callers;
192 // Array of callers, for matching stack traces. First one (name of fn
193 // where err occurs) is mandatory; rest are optional.
194 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000195
196 /* The tool-specific part */
197 SuppKind skind; // What kind of suppression. Must use the range (0..).
198 Char* string; // String -- use is optional. NULL by default.
199 void* extra; // Anything else -- use is optional. NULL by default.
200};
201
202SuppKind VG_(get_supp_kind) ( Supp* su )
203{
204 return su->skind;
205}
206
207Char* VG_(get_supp_string) ( Supp* su )
208{
209 return su->string;
210}
211
212void* VG_(get_supp_extra) ( Supp* su )
213{
214 return su->extra;
215}
216
217
218void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
219{
220 su->skind = skind;
221}
222
223void VG_(set_supp_string) ( Supp* su, Char* string )
224{
225 su->string = string;
226}
227
228void VG_(set_supp_extra) ( Supp* su, void* extra )
229{
230 su->extra = extra;
231}
232
233
234/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000235/*--- Helper fns ---*/
236/*------------------------------------------------------------*/
237
njn0087c502005-07-01 04:15:36 +0000238// Only show core errors if the tool wants to, we're not running with -q,
239// and were not outputting XML.
240Bool VG_(showing_core_errors)(void)
241{
242 return VG_(needs).core_errors && VG_(clo_verbosity) >= 1 && !VG_(clo_xml);
243}
244
sewardjde4a1d02002-03-22 01:27:54 +0000245/* Compare error contexts, to detect duplicates. Note that if they
246 are otherwise the same, the faulting addrs and associated rwoffsets
247 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000248static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000249{
njn810086f2002-11-14 12:42:47 +0000250 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000251 return False;
njn25e49d8e72002-09-23 09:36:25 +0000252 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000253 return False;
254
njn810086f2002-11-14 12:42:47 +0000255 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000256 // case ThreadErr:
257 // case MutexErr:
258 // vg_assert(VG_(needs).core_errors);
259 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000260 default:
njn51d827b2005-05-09 01:02:08 +0000261 if (VG_(needs).tool_errors) {
262 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
263 } else {
njn95ec8702004-11-22 16:46:13 +0000264 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000265 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000266 e1->ekind);
njn67993252004-11-22 18:02:32 +0000267 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000268 }
sewardjde4a1d02002-03-22 01:27:54 +0000269 }
270}
271
njnb0eb66e2005-07-10 16:57:24 +0000272static void pp_Error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +0000273{
sewardj71bc3cb2005-05-19 00:25:45 +0000274 if (VG_(clo_xml)) {
275 VG_(message)(Vg_UserMsg, "<error>");
sewardjdbada272005-07-02 21:16:30 +0000276 VG_(message)(Vg_UserMsg, " <unique>0x%x</unique>",
277 err->unique);
sewardj71bc3cb2005-05-19 00:25:45 +0000278 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
279 }
280
281 if (!VG_(clo_xml)) {
sewardj71bc3cb2005-05-19 00:25:45 +0000282 if (err->tid > 0 && err->tid != last_tid_printed) {
283 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
284 last_tid_printed = err->tid;
285 }
sewardjb5f6f512005-03-10 23:59:00 +0000286 }
njn25e49d8e72002-09-23 09:36:25 +0000287
njn810086f2002-11-14 12:42:47 +0000288 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000289 // case ThreadErr:
290 // case MutexErr:
291 // vg_assert(VG_(needs).core_errors);
292 // VG_(tm_error_print)(err);
293 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000294 default:
njn95ec8702004-11-22 16:46:13 +0000295 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000296 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000297 else {
njn95ec8702004-11-22 16:46:13 +0000298 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000299 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000300 err->ekind);
njn67993252004-11-22 18:02:32 +0000301 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000302 }
sewardjde4a1d02002-03-22 01:27:54 +0000303 }
sewardj71bc3cb2005-05-19 00:25:45 +0000304
305 if (VG_(clo_xml))
306 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000307}
308
nethercote04d0fbc2004-01-26 16:48:06 +0000309/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000310 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000311Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000312{
313 Char ch, ch2;
314 Int res;
315
njn43c799e2003-04-08 00:08:52 +0000316 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000317 return False;
318
319 VG_(message)(Vg_UserMsg, "");
320
321 again:
322 VG_(printf)(
323 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000324 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
325 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000326 );
327
sewardj6024b212003-07-13 10:54:33 +0000328 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000329 if (res != 1) goto ioerror;
330 /* res == 1 */
331 if (ch == '\n') return False;
332 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
333 && ch != 'C' && ch != 'c') goto again;
334
sewardj6024b212003-07-13 10:54:33 +0000335 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000336 if (res != 1) goto ioerror;
337 if (ch2 != '\n') goto again;
338
njn43c799e2003-04-08 00:08:52 +0000339 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000340 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000341 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000342 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000343 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000344 vg_assert(ch == 'c' || ch == 'C');
345
346 ioerror:
njn43c799e2003-04-08 00:08:52 +0000347 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000348 return False;
349}
350
351
sewardjb5f6f512005-03-10 23:59:00 +0000352/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000353static __inline__
njn72718642003-07-24 08:45:32 +0000354void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
355 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000356{
sewardjdbada272005-07-02 21:16:30 +0000357 /* DO NOT MAKE unique_counter NON-STATIC */
358 static UInt unique_counter = 0;
359
njnca82cc02004-11-22 17:18:48 +0000360 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000361
njn810086f2002-11-14 12:42:47 +0000362 /* Core-only parts */
sewardjdbada272005-07-02 21:16:30 +0000363 err->unique = unique_counter++;
njn25e49d8e72002-09-23 09:36:25 +0000364 err->next = NULL;
365 err->supp = NULL;
366 err->count = 1;
njn72718642003-07-24 08:45:32 +0000367 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000368 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000369 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000370 else
371 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000372
nethercote996901a2004-08-03 13:29:09 +0000373 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000374 err->ekind = ekind;
375 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000376 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000377 err->string = s;
378
njn25e49d8e72002-09-23 09:36:25 +0000379 /* sanity... */
njn72718642003-07-24 08:45:32 +0000380 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000381}
382
njn83f9e792005-06-11 05:04:09 +0000383#define ERRTXT_LEN 4096
384
njnf4261312005-03-20 23:45:36 +0000385static void printSuppForIp(UInt n, Addr ip)
386{
njn83f9e792005-06-11 05:04:09 +0000387 static UChar buf[ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000388
njn83f9e792005-06-11 05:04:09 +0000389 if ( VG_(get_fnname_nodemangle) (ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000390 VG_(printf)(" fun:%s\n", buf);
njn83f9e792005-06-11 05:04:09 +0000391 } else if ( VG_(get_objname)(ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000392 VG_(printf)(" obj:%s\n", buf);
393 } else {
njn966d6632005-08-18 15:49:21 +0000394 VG_(printf)(" obj:*\n");
njnf4261312005-03-20 23:45:36 +0000395 }
396}
397
nethercote10d481a2004-01-25 20:33:53 +0000398static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000399{
njn43c799e2003-04-08 00:08:52 +0000400 ExeContext* ec = VG_(get_error_where)(err);
401 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000402
sewardjb5f6f512005-03-10 23:59:00 +0000403 /* At most VG_MAX_SUPP_CALLERS names */
404 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000405 vg_assert(stop_at > 0);
406
sewardjb5f6f512005-03-10 23:59:00 +0000407 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njnf7ba7032005-12-19 19:40:12 +0000408 VG_(printf)("{\n");
409 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000410 VG_(printf)(" core:PThread\n");
411
412 } else {
njn51d827b2005-05-09 01:02:08 +0000413 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000414 if (NULL == name) {
415 VG_(message)(Vg_UserMsg,
njnf7ba7032005-12-19 19:40:12 +0000416 "(%s does not allow error to be suppressed)",
417 VG_(details).name);
njn6a230532003-07-21 10:38:23 +0000418 return;
419 }
njnf7ba7032005-12-19 19:40:12 +0000420 VG_(printf)("{\n");
421 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000422 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000423 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000424 }
njn43c799e2003-04-08 00:08:52 +0000425
njnf4261312005-03-20 23:45:36 +0000426 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000427 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000428
429 VG_(printf)("}\n");
430}
431
njnb4aee052003-04-15 14:09:58 +0000432static
nethercote04d0fbc2004-01-26 16:48:06 +0000433void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000434{
sewardjd153fae2005-01-10 17:24:47 +0000435 Bool still_noisy = True;
436
nethercote04d0fbc2004-01-26 16:48:06 +0000437 /* Perhaps we want a debugger attach at this point? */
438 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000439 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
440 {
nethercote04d0fbc2004-01-26 16:48:06 +0000441 VG_(printf)("starting debugger\n");
442 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000443 }
njn43c799e2003-04-08 00:08:52 +0000444 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000445 if (VG_(clo_gen_suppressions) == 2
446 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000447 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000448 ) {
nethercote42602b12004-01-25 19:30:29 +0000449 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000450 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
451 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000452 }
453}
454
455/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
456 just for pretty printing purposes. */
457static Bool is_first_shown_context = True;
458
njn25e49d8e72002-09-23 09:36:25 +0000459/* Top-level entry point to the error management subsystem.
460 All detected errors are notified here; this routine decides if/when the
461 user should see the error. */
njn72718642003-07-24 08:45:32 +0000462void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000463 ErrorKind ekind, Addr a, Char* s, void* extra )
464{
njn810086f2002-11-14 12:42:47 +0000465 Error err;
466 Error* p;
467 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000468 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000469 VgRes exe_res = Vg_MedRes;
470 static Bool stopping_message = False;
471 static Bool slowdown_message = False;
472 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000473
njn14319cc2005-03-13 06:26:22 +0000474 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
475 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000476 have been found, just refuse to collect any more. This stops
477 the burden of the error-management system becoming excessive in
478 extremely buggy programs, although it does make it pretty
479 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000480 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000481 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
sewardj8a051722005-06-30 00:10:16 +0000482 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)
483 && !VG_(clo_xml)) {
sewardjde4a1d02002-03-22 01:27:54 +0000484 if (!stopping_message) {
485 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000486
njn695c16e2005-03-27 03:40:28 +0000487 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000488 VG_(message)(Vg_UserMsg,
489 "More than %d different errors detected. "
490 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000491 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000492 } else {
493 VG_(message)(Vg_UserMsg,
494 "More than %d total errors detected. "
495 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000496 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000497 }
498
sewardjde4a1d02002-03-22 01:27:54 +0000499 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000500 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000501 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000502 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000503 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000504 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000505 VG_(message)(Vg_UserMsg,
506 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000507 VG_(message)(Vg_UserMsg, "");
508 stopping_message = True;
509 }
510 return;
511 }
512
njn14319cc2005-03-13 06:26:22 +0000513 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000514 been found, be much more conservative about collecting new
515 ones. */
sewardj8a051722005-06-30 00:10:16 +0000516 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER
517 && !VG_(clo_xml)) {
njn25e49d8e72002-09-23 09:36:25 +0000518 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000519 if (!slowdown_message) {
520 VG_(message)(Vg_UserMsg, "");
521 VG_(message)(Vg_UserMsg,
522 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000523 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000524 VG_(message)(Vg_UserMsg,
525 "will still be recorded, but in less detail than before.");
526 slowdown_message = True;
527 }
528 }
529
njn25e49d8e72002-09-23 09:36:25 +0000530 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000531 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000532
533 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000534 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000535 p_prev = NULL;
536 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000537 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000538 /* Found it. */
539 p->count++;
540 if (p->supp != NULL) {
541 /* Deal correctly with suppressed errors. */
542 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000543 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000544 } else {
nethercotef2b11482004-08-02 12:36:01 +0000545 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000546 }
547
548 /* Move p to the front of the list so that future searches
549 for it are faster. */
550 if (p_prev != NULL) {
551 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000552 p_prev->next = p->next;
553 p->next = errors;
554 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000555 }
sewardj7ebf7c32003-07-24 21:29:40 +0000556
sewardjde4a1d02002-03-22 01:27:54 +0000557 return;
558 }
559 p_prev = p;
560 p = p->next;
561 }
562
563 /* Didn't see it. Copy and add. */
564
njn43c799e2003-04-08 00:08:52 +0000565 /* OK, we're really going to collect it. The context is on the stack and
566 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000567 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000568
njn02bc4b82005-05-15 17:28:26 +0000569 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000570 is for when there are more details to fill in which take time to work
571 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000572 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000573 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000574
njn02bc4b82005-05-15 17:28:26 +0000575 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000576 VG_(tdict).tool_update_extra returned. Also allow for people using
577 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000578 */
579
580 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000581 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000582 *p = err;
njn43c799e2003-04-08 00:08:52 +0000583
njn02bc4b82005-05-15 17:28:26 +0000584 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000585 switch (ekind) {
586 // case ThreadErr:
587 // case MutexErr:
588 // vg_assert(VG_(needs).core_errors);
589 // extra_size = VG_(tm_error_update_extra)(p);
590 // break;
591 default:
592 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000593 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000594 break;
595 }
njn43c799e2003-04-08 00:08:52 +0000596
njn02bc4b82005-05-15 17:28:26 +0000597 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000598 if (NULL != p->extra && 0 != extra_size) {
599 void* new_extra = VG_(malloc)(extra_size);
600 VG_(memcpy)(new_extra, p->extra, extra_size);
601 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000602 }
603
njn695c16e2005-03-27 03:40:28 +0000604 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000605 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000606 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000607 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000608 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000609 if (!is_first_shown_context)
610 VG_(message)(Vg_UserMsg, "");
njnb0eb66e2005-07-10 16:57:24 +0000611 pp_Error(p);
sewardjde4a1d02002-03-22 01:27:54 +0000612 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000613 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000614 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000615 } else {
nethercotef2b11482004-08-02 12:36:01 +0000616 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000617 p->supp->count++;
618 }
619}
620
njn43c799e2003-04-08 00:08:52 +0000621/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000622 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000623 guaranteed to only happen once. This avoids all the recording and
624 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000625 suppressed. Bool 'print_error' dictates whether to print the error.
626 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000627*/
njn72718642003-07-24 08:45:32 +0000628Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000629 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000630 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000631{
632 Error err;
633
634 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000635 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000636
637 /* Unless it's suppressed, we're going to show it. Don't need to make
638 a copy, because it's only temporary anyway.
639
njn02bc4b82005-05-15 17:28:26 +0000640 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000641 because that can have an affect on whether it's suppressed. Ignore
642 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000643 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000644 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000645
646 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000647 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000648 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000649
650 if (print_error) {
651 if (!is_first_shown_context)
652 VG_(message)(Vg_UserMsg, "");
njnb0eb66e2005-07-10 16:57:24 +0000653 pp_Error(&err);
njn43c799e2003-04-08 00:08:52 +0000654 is_first_shown_context = False;
655 }
nethercote04d0fbc2004-01-26 16:48:06 +0000656 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000657
658 return False;
659
660 } else {
nethercotef2b11482004-08-02 12:36:01 +0000661 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000662 return True;
663 }
664}
665
sewardjde4a1d02002-03-22 01:27:54 +0000666
sewardjde4a1d02002-03-22 01:27:54 +0000667/*------------------------------------------------------------*/
668/*--- Exported fns ---*/
669/*------------------------------------------------------------*/
670
sewardj71bc3cb2005-05-19 00:25:45 +0000671/* Show the used suppressions. Returns False if no suppression
672 got used. */
673static Bool show_used_suppressions ( void )
674{
675 Supp *su;
676 Bool any_supp;
677
sewardj7c9e57c2005-05-24 14:21:45 +0000678 if (VG_(clo_xml))
679 VG_(message)(Vg_DebugMsg, "<suppcounts>");
680
sewardj71bc3cb2005-05-19 00:25:45 +0000681 any_supp = False;
682 for (su = suppressions; su != NULL; su = su->next) {
683 if (su->count <= 0)
684 continue;
685 any_supp = True;
686 if (VG_(clo_xml)) {
687 VG_(message)(Vg_DebugMsg,
sewardj753673f2005-08-09 22:03:08 +0000688 " <pair>\n"
689 " <count>%d</count>\n"
690 " <name>%t</name>\n"
691 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000692 su->count, su->sname);
693 } else {
694 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
695 }
696 }
697
sewardj7c9e57c2005-05-24 14:21:45 +0000698 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000699 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000700
sewardj71bc3cb2005-05-19 00:25:45 +0000701 return any_supp;
702}
703
704
sewardj9f297ca2005-05-20 02:29:52 +0000705/* Show all the errors that occurred, and possibly also the
706 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000707void VG_(show_all_errors) ( void )
708{
njn810086f2002-11-14 12:42:47 +0000709 Int i, n_min;
710 Int n_err_contexts, n_supp_contexts;
711 Error *p, *p_min;
712 Supp *su;
713 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000714
715 if (VG_(clo_verbosity) == 0)
716 return;
717
718 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000719 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000720 if (p->supp == NULL)
721 n_err_contexts++;
722 }
723
724 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000725 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000726 if (su->count > 0)
727 n_supp_contexts++;
728 }
sewardj71bc3cb2005-05-19 00:25:45 +0000729
730 /* If we're printing XML, just show the suppressions and stop.
731 */
732 if (VG_(clo_xml)) {
733 (void)show_used_suppressions();
734 return;
735 }
736
737 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000738 VG_(message)(Vg_UserMsg,
739 "ERROR SUMMARY: "
740 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000741 n_errs_found, n_err_contexts,
742 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000743
744 if (VG_(clo_verbosity) <= 1)
745 return;
746
747 /* Print the contexts in order of increasing error count. */
748 for (i = 0; i < n_err_contexts; i++) {
749 n_min = (1 << 30) - 1;
750 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000751 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000752 if (p->supp != NULL) continue;
753 if (p->count < n_min) {
754 n_min = p->count;
755 p_min = p;
756 }
757 }
njn67993252004-11-22 18:02:32 +0000758 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000759
760 VG_(message)(Vg_UserMsg, "");
761 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
762 p_min->count,
763 i+1, n_err_contexts);
njnb0eb66e2005-07-10 16:57:24 +0000764 pp_Error( p_min );
sewardjde4a1d02002-03-22 01:27:54 +0000765
766 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000767 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000768 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njn394213a2005-06-19 18:38:24 +0000769 ips[0], /*debugging*/True, 0xFE/*verbosity*/,
sewardj0ec07f32006-01-12 12:32:32 +0000770 /*bbs_done*/0,
771 /*allow redir?*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000772 }
773
774 p_min->count = 1 << 30;
775 }
776
777 if (n_supp_contexts > 0)
778 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000779 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000780
781 if (n_err_contexts > 0) {
782 if (any_supp)
783 VG_(message)(Vg_UserMsg, "");
784 VG_(message)(Vg_UserMsg,
785 "IN SUMMARY: "
786 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000787 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000788 n_supp_contexts );
789 VG_(message)(Vg_UserMsg, "");
790 }
791}
792
sewardj9f297ca2005-05-20 02:29:52 +0000793
794/* Show occurrence counts of all errors, in XML form. */
795void VG_(show_error_counts_as_XML) ( void )
796{
797 Error* err;
798 VG_(message)(Vg_UserMsg, "<errorcounts>");
799 for (err = errors; err != NULL; err = err->next) {
800 if (err->supp != NULL)
801 continue;
802 if (err->count <= 0)
803 continue;
804 VG_(message)(
sewardj753673f2005-08-09 22:03:08 +0000805 Vg_UserMsg, " <pair>\n"
806 " <count>%d</count>\n"
cerion3a24e722005-08-09 22:34:18 +0000807 " <unique>0x%x</unique>\n"
sewardj753673f2005-08-09 22:03:08 +0000808 " </pair>",
sewardj39618202005-08-09 21:50:18 +0000809 err->count, err->unique
sewardj9f297ca2005-05-20 02:29:52 +0000810 );
811 }
812 VG_(message)(Vg_UserMsg, "</errorcounts>");
813}
814
815
sewardjde4a1d02002-03-22 01:27:54 +0000816/*------------------------------------------------------------*/
817/*--- Standard suppressions ---*/
818/*------------------------------------------------------------*/
819
820/* Get a non-blank, non-comment line of at most nBuf chars from fd.
821 Skips leading spaces on the line. Return True if EOF was hit instead.
822*/
njn4ba5a792002-09-30 10:23:54 +0000823Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000824{
825 Char ch;
826 Int n, i;
827 while (True) {
828 /* First, read until a non-blank char appears. */
829 while (True) {
830 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000831 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000832 if (n == 0) return True;
833 }
834
835 /* Now, read the line into buf. */
836 i = 0;
837 buf[i++] = ch; buf[i] = 0;
838 while (True) {
839 n = VG_(read)(fd, &ch, 1);
840 if (n == 0) return False; /* the next call will return True */
841 if (ch == '\n') break;
842 if (i > 0 && i == nBuf-1) i--;
843 buf[i++] = ch; buf[i] = 0;
844 }
njn0c0f32a2005-03-26 04:14:01 +0000845 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000846 i--; buf[i] = 0;
847 };
848
njn02bc4b82005-05-15 17:28:26 +0000849 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000850 /* Ok, we have a line. If a non-comment line, return.
851 If a comment line, start all over again. */
852 if (buf[0] != '#') return False;
853 }
854}
855
856
857/* *p_caller contains the raw name of a caller, supposedly either
858 fun:some_function_name or
859 obj:some_object_name.
860 Set *p_ty accordingly and advance *p_caller over the descriptor
861 (fun: or obj:) part.
862 Returns False if failed.
863*/
sewardjb5f6f512005-03-10 23:59:00 +0000864static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000865{
sewardjb5f6f512005-03-10 23:59:00 +0000866 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
867 p->name += 4;
868 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000869 return True;
870 }
sewardjb5f6f512005-03-10 23:59:00 +0000871 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
872 p->name += 4;
873 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000874 return True;
875 }
876 VG_(printf)("location should start with fun: or obj:\n");
877 return False;
878}
879
880
nethercote7cc9c232004-01-21 15:08:04 +0000881/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000882static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000883Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000884{
885 Bool found;
886 Char *s = NULL; /* Shut gcc up */
887 Int len = VG_(strlen)(name);
888
889 found = (NULL != (s = VG_(strstr)(names, name)) &&
890 (s == names || *(s-1) == ',') &&
891 (*(s+len) == ',' || *(s+len) == '\0')
892 );
893
894 return found;
895}
896
njn695c16e2005-03-27 03:40:28 +0000897/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000898 and place them in the suppressions list. If there's any difficulty
899 doing this, just give up -- there's no point in trying to recover.
900*/
sewardjde4a1d02002-03-22 01:27:54 +0000901static void load_one_suppressions_file ( Char* filename )
902{
903# define N_BUF 200
sewardj92645592005-07-23 09:18:34 +0000904 SysRes sres;
905 Int fd, i;
906 Bool eof;
907 Char buf[N_BUF+1];
908 Char* tool_names;
909 Char* supp_name;
910 Char* err_str = NULL;
sewardjb5f6f512005-03-10 23:59:00 +0000911 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000912
sewardj92645592005-07-23 09:18:34 +0000913 fd = -1;
914 sres = VG_(open)( filename, VKI_O_RDONLY, 0 );
915 if (sres.isError) {
sewardjf349d552005-11-14 17:01:01 +0000916 if (VG_(clo_xml))
917 VG_(message)(Vg_UserMsg, "</valgrindoutput>\n");
njn02bc4b82005-05-15 17:28:26 +0000918 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000919 filename );
920 VG_(exit)(1);
921 }
sewardj92645592005-07-23 09:18:34 +0000922 fd = sres.val;
sewardjde4a1d02002-03-22 01:27:54 +0000923
sewardj92645592005-07-23 09:18:34 +0000924# define BOMB(S) { err_str = S; goto syntax_error; }
sewardjb5f6f512005-03-10 23:59:00 +0000925
sewardjde4a1d02002-03-22 01:27:54 +0000926 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000927 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000928 Supp* supp;
929 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000930 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000931
932 // Initialise temporary reading-in buffer.
933 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
934 tmp_callers[i].ty = NoName;
935 tmp_callers[i].name = NULL;
936 }
937
njn810086f2002-11-14 12:42:47 +0000938 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000939
njn4ba5a792002-09-30 10:23:54 +0000940 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000941 if (eof) break;
942
sewardjb5f6f512005-03-10 23:59:00 +0000943 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000944
njn4ba5a792002-09-30 10:23:54 +0000945 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000946
947 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
948
njn25e49d8e72002-09-23 09:36:25 +0000949 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000950
njn4ba5a792002-09-30 10:23:54 +0000951 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000952
sewardjb5f6f512005-03-10 23:59:00 +0000953 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000954
njn94065fd2004-11-22 19:26:27 +0000955 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000956 i = 0;
957 while (True) {
958 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000959 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000960 i++;
njn25e49d8e72002-09-23 09:36:25 +0000961 }
njnc40c3a82002-10-02 11:02:27 +0000962 buf[i] = '\0'; /* Replace ':', splitting into two strings */
963
nethercote7cc9c232004-01-21 15:08:04 +0000964 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000965 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000966
nethercote7cc9c232004-01-21 15:08:04 +0000967 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000968 {
sewardjb5f6f512005-03-10 23:59:00 +0000969 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000970 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000971 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000972 else
sewardjb5f6f512005-03-10 23:59:00 +0000973 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000974 }
njn95ec8702004-11-22 16:46:13 +0000975 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000976 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000977 {
sewardjb5f6f512005-03-10 23:59:00 +0000978 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000979 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000980 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000981 } else {
982 BOMB("unknown tool suppression type");
983 }
njnc40c3a82002-10-02 11:02:27 +0000984 }
njn25e49d8e72002-09-23 09:36:25 +0000985 else {
sewardjb5f6f512005-03-10 23:59:00 +0000986 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000987 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000988 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000989 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000990 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000991 break;
992 }
993 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000994 }
995
njn95ec8702004-11-22 16:46:13 +0000996 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000997 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000998 {
999 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +00001000 }
1001
sewardjb5f6f512005-03-10 23:59:00 +00001002 i = 0;
1003 while (True) {
1004 eof = VG_(get_line) ( fd, buf, N_BUF );
1005 if (eof)
1006 BOMB("unexpected end-of-file");
1007 if (VG_STREQ(buf, "}")) {
1008 if (i > 0) {
1009 break;
1010 } else {
1011 BOMB("missing stack trace");
1012 }
1013 }
1014 if (i == VG_MAX_SUPP_CALLERS)
1015 BOMB("too many callers in stack trace");
1016 if (i > 0 && i >= VG_(clo_backtrace_size))
1017 break;
1018 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
1019 if (!setLocationTy(&(tmp_callers[i])))
1020 BOMB("location should start with 'fun:' or 'obj:'");
1021 i++;
1022 }
1023
1024 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
1025 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +00001026 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +00001027 do {
1028 eof = VG_(get_line) ( fd, buf, N_BUF );
1029 } while (!eof && !VG_STREQ(buf, "}"));
1030 }
1031
1032 // Copy tmp_callers[] into supp->callers[]
1033 supp->n_callers = i;
1034 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1035 for (i = 0; i < supp->n_callers; i++) {
1036 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001037 }
1038
njn695c16e2005-03-27 03:40:28 +00001039 supp->next = suppressions;
1040 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001041 }
sewardjde4a1d02002-03-22 01:27:54 +00001042 VG_(close)(fd);
1043 return;
1044
1045 syntax_error:
sewardjf349d552005-11-14 17:01:01 +00001046 if (VG_(clo_xml))
1047 VG_(message)(Vg_UserMsg, "</valgrindoutput>\n");
sewardjb5f6f512005-03-10 23:59:00 +00001048 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001049 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001050
sewardjde4a1d02002-03-22 01:27:54 +00001051 VG_(close)(fd);
1052 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001053 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001054
sewardjb5f6f512005-03-10 23:59:00 +00001055# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001056# undef N_BUF
1057}
1058
1059
1060void VG_(load_suppressions) ( void )
1061{
1062 Int i;
njn695c16e2005-03-27 03:40:28 +00001063 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001064 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1065 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001066 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1067 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001068 }
1069 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1070 }
1071}
1072
sewardjb5f6f512005-03-10 23:59:00 +00001073static
njn810086f2002-11-14 12:42:47 +00001074Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001075{
njn810086f2002-11-14 12:42:47 +00001076 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001077 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001078 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001079 default:
njn95ec8702004-11-22 16:46:13 +00001080 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001081 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001082 } else {
1083 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001084 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001085 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001086 err->ekind);
njn67993252004-11-22 18:02:32 +00001087 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001088 }
1089 }
1090}
1091
sewardjb5f6f512005-03-10 23:59:00 +00001092static
1093Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001094{
1095 Int i;
njn83f9e792005-06-11 05:04:09 +00001096 Char caller_name[ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001097 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001098
sewardjb5f6f512005-03-10 23:59:00 +00001099 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001100 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001101 vg_assert(su->callers[i].name != NULL);
njn966d6632005-08-18 15:49:21 +00001102 // The string to be used in the unknown case ("???") can be anything
1103 // that couldn't be a valid function or objname. --gen-suppressions
1104 // prints 'obj:*' for such an entry, which will match any string we
1105 // use.
sewardjb5f6f512005-03-10 23:59:00 +00001106 switch (su->callers[i].ty) {
1107 case ObjName:
njn83f9e792005-06-11 05:04:09 +00001108 if (!VG_(get_objname)(a, caller_name, ERRTXT_LEN))
njn966d6632005-08-18 15:49:21 +00001109 VG_(strcpy)(caller_name, "???");
sewardjb5f6f512005-03-10 23:59:00 +00001110 break;
1111
1112 case FunName:
sewardj0c95e942006-01-18 04:23:10 +00001113 // Nb: mangled names used in suppressions. Do, though,
1114 // Z-demangle them, since otherwise it's possible to wind
1115 // up comparing "malloc" in the suppression against
1116 // "_vgrZU_libcZdsoZa_malloc" in the backtrace, and the
1117 // two of them need to be made to match.
1118 if (!VG_(get_fnname_Z_demangle_only)(a, caller_name, ERRTXT_LEN))
njn966d6632005-08-18 15:49:21 +00001119 VG_(strcpy)(caller_name, "???");
sewardjb5f6f512005-03-10 23:59:00 +00001120 break;
njn67993252004-11-22 18:02:32 +00001121 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001122 }
sewardjda646ca2006-01-19 03:36:52 +00001123 if (0) VG_(printf)("cmp %s %s\n", su->callers[i].name, caller_name);
sewardjb5f6f512005-03-10 23:59:00 +00001124 if (!VG_(string_match)(su->callers[i].name, caller_name))
1125 return False;
njn25e49d8e72002-09-23 09:36:25 +00001126 }
1127
1128 /* If we reach here, it's a match */
1129 return True;
1130}
sewardjde4a1d02002-03-22 01:27:54 +00001131
njn810086f2002-11-14 12:42:47 +00001132/* Does an error context match a suppression? ie is this a suppressible
1133 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001134 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001135*/
njn810086f2002-11-14 12:42:47 +00001136static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001137{
njn810086f2002-11-14 12:42:47 +00001138 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001139
sewardjde4a1d02002-03-22 01:27:54 +00001140 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001141 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001142 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001143 supp_matches_callers(err, su))
1144 {
njn25e49d8e72002-09-23 09:36:25 +00001145 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001146 }
sewardjde4a1d02002-03-22 01:27:54 +00001147 }
njn25e49d8e72002-09-23 09:36:25 +00001148 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001149}
1150
sewardjde4a1d02002-03-22 01:27:54 +00001151/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001152/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001153/*--------------------------------------------------------------------*/