blob: 364c38c0d1ab7919acc65d86925c2d132f20227a [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. */
54#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
55
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. */
59#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
60
61/* After this many total errors have been observed, stop collecting
62 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
63#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
64
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;
102 // NULL if unsuppressed; or ptr to suppression record.
103 Supp* supp;
104 Int count;
105 ThreadId tid;
106
107 // The tool-specific part
108 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000109 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000110 Addr addr; // Used frequently
111 Char* string; // Used frequently
112 void* extra; // For any tool-specific extras
113};
114
115ExeContext* VG_(get_error_where) ( Error* err )
116{
117 return err->where;
118}
119
120ErrorKind VG_(get_error_kind) ( Error* err )
121{
122 return err->ekind;
123}
124
125Addr VG_(get_error_address) ( Error* err )
126{
127 return err->addr;
128}
129
130Char* VG_(get_error_string) ( Error* err )
131{
132 return err->string;
133}
134
135void* VG_(get_error_extra) ( Error* err )
136{
137 return err->extra;
138}
139
nethercotef2b11482004-08-02 12:36:01 +0000140UInt VG_(get_n_errs_found)( void )
141{
142 return n_errs_found;
143}
144
nethercote4a184902004-08-02 12:21:09 +0000145/*------------------------------------------------------------*/
146/*--- Suppression type ---*/
147/*------------------------------------------------------------*/
148
149/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
150 * effectively extend it by defining their own enums in the (0..) range. */
151typedef
152 enum {
153 PThreadSupp = -1, /* Matches PThreadErr */
154 }
155 CoreSuppKind;
156
sewardjb5f6f512005-03-10 23:59:00 +0000157/* Max number of callers for context in a suppression. */
158#define VG_MAX_SUPP_CALLERS 24
159
nethercote4a184902004-08-02 12:21:09 +0000160/* For each caller specified for a suppression, record the nature of
161 the caller name. Not of interest to tools. */
162typedef
163 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000164 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000165 ObjName, /* Name is of an shared object file. */
166 FunName /* Name is of a function. */
167 }
168 SuppLocTy;
169
sewardjb5f6f512005-03-10 23:59:00 +0000170typedef
171 struct {
172 SuppLocTy ty;
173 Char* name;
174 }
175 SuppLoc;
176
nethercote4a184902004-08-02 12:21:09 +0000177/* Suppressions. Tools can get/set tool-relevant parts with functions
njnc7561b92005-06-19 01:24:32 +0000178 declared in include/pub_tool_errormgr.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000179 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000180 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000181struct _Supp {
182 struct _Supp* next;
183 Int count; // The number of times this error has been suppressed.
184 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000185
186 // Length of 'callers'
187 Int n_callers;
188 // Array of callers, for matching stack traces. First one (name of fn
189 // where err occurs) is mandatory; rest are optional.
190 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000191
192 /* The tool-specific part */
193 SuppKind skind; // What kind of suppression. Must use the range (0..).
194 Char* string; // String -- use is optional. NULL by default.
195 void* extra; // Anything else -- use is optional. NULL by default.
196};
197
198SuppKind VG_(get_supp_kind) ( Supp* su )
199{
200 return su->skind;
201}
202
203Char* VG_(get_supp_string) ( Supp* su )
204{
205 return su->string;
206}
207
208void* VG_(get_supp_extra) ( Supp* su )
209{
210 return su->extra;
211}
212
213
214void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
215{
216 su->skind = skind;
217}
218
219void VG_(set_supp_string) ( Supp* su, Char* string )
220{
221 su->string = string;
222}
223
224void VG_(set_supp_extra) ( Supp* su, void* extra )
225{
226 su->extra = extra;
227}
228
229
230/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000231/*--- Helper fns ---*/
232/*------------------------------------------------------------*/
233
njn0087c502005-07-01 04:15:36 +0000234// Only show core errors if the tool wants to, we're not running with -q,
235// and were not outputting XML.
236Bool VG_(showing_core_errors)(void)
237{
238 return VG_(needs).core_errors && VG_(clo_verbosity) >= 1 && !VG_(clo_xml);
239}
240
sewardjde4a1d02002-03-22 01:27:54 +0000241/* Compare error contexts, to detect duplicates. Note that if they
242 are otherwise the same, the faulting addrs and associated rwoffsets
243 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000244static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000245{
njn810086f2002-11-14 12:42:47 +0000246 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000247 return False;
njn25e49d8e72002-09-23 09:36:25 +0000248 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000249 return False;
250
njn810086f2002-11-14 12:42:47 +0000251 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000252 // case ThreadErr:
253 // case MutexErr:
254 // vg_assert(VG_(needs).core_errors);
255 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000256 default:
njn51d827b2005-05-09 01:02:08 +0000257 if (VG_(needs).tool_errors) {
258 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
259 } else {
njn95ec8702004-11-22 16:46:13 +0000260 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000261 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000262 e1->ekind);
njn67993252004-11-22 18:02:32 +0000263 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000264 }
sewardjde4a1d02002-03-22 01:27:54 +0000265 }
266}
267
njn810086f2002-11-14 12:42:47 +0000268static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000269{
sewardj71bc3cb2005-05-19 00:25:45 +0000270 if (VG_(clo_xml)) {
271 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000272 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
273 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000274 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
275 }
276
277 if (!VG_(clo_xml)) {
278 if (printCount)
279 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
280 if (err->tid > 0 && err->tid != last_tid_printed) {
281 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
282 last_tid_printed = err->tid;
283 }
sewardjb5f6f512005-03-10 23:59:00 +0000284 }
njn25e49d8e72002-09-23 09:36:25 +0000285
njn810086f2002-11-14 12:42:47 +0000286 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000287 // case ThreadErr:
288 // case MutexErr:
289 // vg_assert(VG_(needs).core_errors);
290 // VG_(tm_error_print)(err);
291 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000292 default:
njn95ec8702004-11-22 16:46:13 +0000293 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000294 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000295 else {
njn95ec8702004-11-22 16:46:13 +0000296 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000297 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000298 err->ekind);
njn67993252004-11-22 18:02:32 +0000299 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000300 }
sewardjde4a1d02002-03-22 01:27:54 +0000301 }
sewardj71bc3cb2005-05-19 00:25:45 +0000302
303 if (VG_(clo_xml))
304 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000305}
306
nethercote04d0fbc2004-01-26 16:48:06 +0000307/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000308 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000309Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000310{
311 Char ch, ch2;
312 Int res;
313
njn43c799e2003-04-08 00:08:52 +0000314 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000315 return False;
316
317 VG_(message)(Vg_UserMsg, "");
318
319 again:
320 VG_(printf)(
321 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000322 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
323 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000324 );
325
sewardj6024b212003-07-13 10:54:33 +0000326 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000327 if (res != 1) goto ioerror;
328 /* res == 1 */
329 if (ch == '\n') return False;
330 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
331 && ch != 'C' && ch != 'c') goto again;
332
sewardj6024b212003-07-13 10:54:33 +0000333 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000334 if (res != 1) goto ioerror;
335 if (ch2 != '\n') goto again;
336
njn43c799e2003-04-08 00:08:52 +0000337 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000338 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000339 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000340 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000341 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000342 vg_assert(ch == 'c' || ch == 'C');
343
344 ioerror:
njn43c799e2003-04-08 00:08:52 +0000345 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000346 return False;
347}
348
349
sewardjb5f6f512005-03-10 23:59:00 +0000350/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000351static __inline__
njn72718642003-07-24 08:45:32 +0000352void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
353 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000354{
njnca82cc02004-11-22 17:18:48 +0000355 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000356
njn810086f2002-11-14 12:42:47 +0000357 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000358 err->next = NULL;
359 err->supp = NULL;
360 err->count = 1;
njn72718642003-07-24 08:45:32 +0000361 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000362 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000363 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000364 else
365 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000366
nethercote996901a2004-08-03 13:29:09 +0000367 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000368 err->ekind = ekind;
369 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000370 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000371 err->string = s;
372
njn25e49d8e72002-09-23 09:36:25 +0000373 /* sanity... */
njn72718642003-07-24 08:45:32 +0000374 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000375}
376
njn83f9e792005-06-11 05:04:09 +0000377#define ERRTXT_LEN 4096
378
njnf4261312005-03-20 23:45:36 +0000379static void printSuppForIp(UInt n, Addr ip)
380{
njn83f9e792005-06-11 05:04:09 +0000381 static UChar buf[ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000382
njn83f9e792005-06-11 05:04:09 +0000383 if ( VG_(get_fnname_nodemangle) (ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000384 VG_(printf)(" fun:%s\n", buf);
njn83f9e792005-06-11 05:04:09 +0000385 } else if ( VG_(get_objname)(ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000386 VG_(printf)(" obj:%s\n", buf);
387 } else {
388 VG_(printf)(" ???:??? "
389 "# unknown, suppression will not work, sorry\n");
390 }
391}
392
nethercote10d481a2004-01-25 20:33:53 +0000393static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000394{
njn43c799e2003-04-08 00:08:52 +0000395 ExeContext* ec = VG_(get_error_where)(err);
396 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000397
sewardjb5f6f512005-03-10 23:59:00 +0000398 /* At most VG_MAX_SUPP_CALLERS names */
399 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000400 vg_assert(stop_at > 0);
401
402 VG_(printf)("{\n");
403 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000404
sewardjb5f6f512005-03-10 23:59:00 +0000405 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000406 VG_(printf)(" core:PThread\n");
407
408 } else {
njn51d827b2005-05-09 01:02:08 +0000409 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000410 if (NULL == name) {
411 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000412 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000413 return;
414 }
415 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000416 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000417 }
njn43c799e2003-04-08 00:08:52 +0000418
njnf4261312005-03-20 23:45:36 +0000419 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000420 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000421
422 VG_(printf)("}\n");
423}
424
njnb4aee052003-04-15 14:09:58 +0000425static
nethercote04d0fbc2004-01-26 16:48:06 +0000426void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000427{
sewardjd153fae2005-01-10 17:24:47 +0000428 Bool still_noisy = True;
429
nethercote04d0fbc2004-01-26 16:48:06 +0000430 /* Perhaps we want a debugger attach at this point? */
431 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000432 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
433 {
nethercote04d0fbc2004-01-26 16:48:06 +0000434 VG_(printf)("starting debugger\n");
435 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000436 }
njn43c799e2003-04-08 00:08:52 +0000437 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000438 if (VG_(clo_gen_suppressions) == 2
439 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000440 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000441 ) {
nethercote42602b12004-01-25 19:30:29 +0000442 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000443 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
444 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000445 }
446}
447
448/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
449 just for pretty printing purposes. */
450static Bool is_first_shown_context = True;
451
njn25e49d8e72002-09-23 09:36:25 +0000452/* Top-level entry point to the error management subsystem.
453 All detected errors are notified here; this routine decides if/when the
454 user should see the error. */
njn72718642003-07-24 08:45:32 +0000455void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000456 ErrorKind ekind, Addr a, Char* s, void* extra )
457{
njn810086f2002-11-14 12:42:47 +0000458 Error err;
459 Error* p;
460 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000461 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000462 VgRes exe_res = Vg_MedRes;
463 static Bool stopping_message = False;
464 static Bool slowdown_message = False;
465 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000466
njn14319cc2005-03-13 06:26:22 +0000467 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
468 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000469 have been found, just refuse to collect any more. This stops
470 the burden of the error-management system becoming excessive in
471 extremely buggy programs, although it does make it pretty
472 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000473 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000474 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
sewardj8a051722005-06-30 00:10:16 +0000475 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)
476 && !VG_(clo_xml)) {
sewardjde4a1d02002-03-22 01:27:54 +0000477 if (!stopping_message) {
478 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000479
njn695c16e2005-03-27 03:40:28 +0000480 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000481 VG_(message)(Vg_UserMsg,
482 "More than %d different errors detected. "
483 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000484 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000485 } else {
486 VG_(message)(Vg_UserMsg,
487 "More than %d total errors detected. "
488 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000489 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000490 }
491
sewardjde4a1d02002-03-22 01:27:54 +0000492 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000493 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000494 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000495 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000496 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000497 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000498 VG_(message)(Vg_UserMsg,
499 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000500 VG_(message)(Vg_UserMsg, "");
501 stopping_message = True;
502 }
503 return;
504 }
505
njn14319cc2005-03-13 06:26:22 +0000506 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000507 been found, be much more conservative about collecting new
508 ones. */
sewardj8a051722005-06-30 00:10:16 +0000509 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER
510 && !VG_(clo_xml)) {
njn25e49d8e72002-09-23 09:36:25 +0000511 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000512 if (!slowdown_message) {
513 VG_(message)(Vg_UserMsg, "");
514 VG_(message)(Vg_UserMsg,
515 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000516 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000517 VG_(message)(Vg_UserMsg,
518 "will still be recorded, but in less detail than before.");
519 slowdown_message = True;
520 }
521 }
522
njn25e49d8e72002-09-23 09:36:25 +0000523 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000524 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000525
526 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000527 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000528 p_prev = NULL;
529 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000530 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000531 /* Found it. */
532 p->count++;
533 if (p->supp != NULL) {
534 /* Deal correctly with suppressed errors. */
535 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000536 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000537 } else {
nethercotef2b11482004-08-02 12:36:01 +0000538 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000539 }
540
541 /* Move p to the front of the list so that future searches
542 for it are faster. */
543 if (p_prev != NULL) {
544 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000545 p_prev->next = p->next;
546 p->next = errors;
547 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000548 }
sewardj7ebf7c32003-07-24 21:29:40 +0000549
sewardjde4a1d02002-03-22 01:27:54 +0000550 return;
551 }
552 p_prev = p;
553 p = p->next;
554 }
555
556 /* Didn't see it. Copy and add. */
557
njn43c799e2003-04-08 00:08:52 +0000558 /* OK, we're really going to collect it. The context is on the stack and
559 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000560 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000561
njn02bc4b82005-05-15 17:28:26 +0000562 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000563 is for when there are more details to fill in which take time to work
564 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000565 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000566 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000567
njn02bc4b82005-05-15 17:28:26 +0000568 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000569 VG_(tdict).tool_update_extra returned. Also allow for people using
570 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000571 */
572
573 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000574 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000575 *p = err;
njn43c799e2003-04-08 00:08:52 +0000576
njn02bc4b82005-05-15 17:28:26 +0000577 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000578 switch (ekind) {
579 // case ThreadErr:
580 // case MutexErr:
581 // vg_assert(VG_(needs).core_errors);
582 // extra_size = VG_(tm_error_update_extra)(p);
583 // break;
584 default:
585 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000586 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000587 break;
588 }
njn43c799e2003-04-08 00:08:52 +0000589
njn02bc4b82005-05-15 17:28:26 +0000590 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000591 if (NULL != p->extra && 0 != extra_size) {
592 void* new_extra = VG_(malloc)(extra_size);
593 VG_(memcpy)(new_extra, p->extra, extra_size);
594 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000595 }
596
njn695c16e2005-03-27 03:40:28 +0000597 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000598 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000599 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000600 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000601 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000602 if (!is_first_shown_context)
603 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000604 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000605 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000606 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000607 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000608 } else {
nethercotef2b11482004-08-02 12:36:01 +0000609 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000610 p->supp->count++;
611 }
612}
613
njn43c799e2003-04-08 00:08:52 +0000614/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000615 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000616 guaranteed to only happen once. This avoids all the recording and
617 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000618 suppressed. Bool 'print_error' dictates whether to print the error.
619 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000620*/
njn72718642003-07-24 08:45:32 +0000621Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000622 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000623 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000624{
625 Error err;
626
627 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000628 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000629
630 /* Unless it's suppressed, we're going to show it. Don't need to make
631 a copy, because it's only temporary anyway.
632
njn02bc4b82005-05-15 17:28:26 +0000633 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000634 because that can have an affect on whether it's suppressed. Ignore
635 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000636 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000637 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000638
639 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000640 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000641 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000642
643 if (print_error) {
644 if (!is_first_shown_context)
645 VG_(message)(Vg_UserMsg, "");
646 pp_Error(&err, False);
647 is_first_shown_context = False;
648 }
nethercote04d0fbc2004-01-26 16:48:06 +0000649 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000650
651 return False;
652
653 } else {
nethercotef2b11482004-08-02 12:36:01 +0000654 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000655 return True;
656 }
657}
658
sewardjde4a1d02002-03-22 01:27:54 +0000659
sewardjde4a1d02002-03-22 01:27:54 +0000660/*------------------------------------------------------------*/
661/*--- Exported fns ---*/
662/*------------------------------------------------------------*/
663
sewardj71bc3cb2005-05-19 00:25:45 +0000664/* Show the used suppressions. Returns False if no suppression
665 got used. */
666static Bool show_used_suppressions ( void )
667{
668 Supp *su;
669 Bool any_supp;
670
sewardj7c9e57c2005-05-24 14:21:45 +0000671 if (VG_(clo_xml))
672 VG_(message)(Vg_DebugMsg, "<suppcounts>");
673
sewardj71bc3cb2005-05-19 00:25:45 +0000674 any_supp = False;
675 for (su = suppressions; su != NULL; su = su->next) {
676 if (su->count <= 0)
677 continue;
678 any_supp = True;
679 if (VG_(clo_xml)) {
680 VG_(message)(Vg_DebugMsg,
sewardj0407f652005-06-29 23:38:33 +0000681 " <pair> <count>%d</count> "
682 "<name>%s</name> </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000683 su->count, su->sname);
684 } else {
685 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
686 }
687 }
688
sewardj7c9e57c2005-05-24 14:21:45 +0000689 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000690 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000691
sewardj71bc3cb2005-05-19 00:25:45 +0000692 return any_supp;
693}
694
695
sewardj9f297ca2005-05-20 02:29:52 +0000696/* Show all the errors that occurred, and possibly also the
697 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000698void VG_(show_all_errors) ( void )
699{
njn810086f2002-11-14 12:42:47 +0000700 Int i, n_min;
701 Int n_err_contexts, n_supp_contexts;
702 Error *p, *p_min;
703 Supp *su;
704 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000705
706 if (VG_(clo_verbosity) == 0)
707 return;
708
709 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000710 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000711 if (p->supp == NULL)
712 n_err_contexts++;
713 }
714
715 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000716 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000717 if (su->count > 0)
718 n_supp_contexts++;
719 }
sewardj71bc3cb2005-05-19 00:25:45 +0000720
721 /* If we're printing XML, just show the suppressions and stop.
722 */
723 if (VG_(clo_xml)) {
724 (void)show_used_suppressions();
725 return;
726 }
727
728 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000729 VG_(message)(Vg_UserMsg,
730 "ERROR SUMMARY: "
731 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000732 n_errs_found, n_err_contexts,
733 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000734
735 if (VG_(clo_verbosity) <= 1)
736 return;
737
738 /* Print the contexts in order of increasing error count. */
739 for (i = 0; i < n_err_contexts; i++) {
740 n_min = (1 << 30) - 1;
741 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000742 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000743 if (p->supp != NULL) continue;
744 if (p->count < n_min) {
745 n_min = p->count;
746 p_min = p;
747 }
748 }
njn67993252004-11-22 18:02:32 +0000749 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000750
751 VG_(message)(Vg_UserMsg, "");
752 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
753 p_min->count,
754 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000755 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000756
757 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000758 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000759 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njn394213a2005-06-19 18:38:24 +0000760 ips[0], /*debugging*/True, 0xFE/*verbosity*/,
761 /*bbs_done*/0);
sewardjde4a1d02002-03-22 01:27:54 +0000762 }
763
764 p_min->count = 1 << 30;
765 }
766
767 if (n_supp_contexts > 0)
768 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000769 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000770
771 if (n_err_contexts > 0) {
772 if (any_supp)
773 VG_(message)(Vg_UserMsg, "");
774 VG_(message)(Vg_UserMsg,
775 "IN SUMMARY: "
776 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000777 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000778 n_supp_contexts );
779 VG_(message)(Vg_UserMsg, "");
780 }
781}
782
sewardj9f297ca2005-05-20 02:29:52 +0000783
784/* Show occurrence counts of all errors, in XML form. */
785void VG_(show_error_counts_as_XML) ( void )
786{
787 Error* err;
788 VG_(message)(Vg_UserMsg, "<errorcounts>");
789 for (err = errors; err != NULL; err = err->next) {
790 if (err->supp != NULL)
791 continue;
792 if (err->count <= 0)
793 continue;
794 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000795 Vg_UserMsg, " <pair> <count>%d</count> "
796 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000797 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000798 );
799 }
800 VG_(message)(Vg_UserMsg, "</errorcounts>");
801}
802
803
sewardjde4a1d02002-03-22 01:27:54 +0000804/*------------------------------------------------------------*/
805/*--- Standard suppressions ---*/
806/*------------------------------------------------------------*/
807
808/* Get a non-blank, non-comment line of at most nBuf chars from fd.
809 Skips leading spaces on the line. Return True if EOF was hit instead.
810*/
njn4ba5a792002-09-30 10:23:54 +0000811Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000812{
813 Char ch;
814 Int n, i;
815 while (True) {
816 /* First, read until a non-blank char appears. */
817 while (True) {
818 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000819 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000820 if (n == 0) return True;
821 }
822
823 /* Now, read the line into buf. */
824 i = 0;
825 buf[i++] = ch; buf[i] = 0;
826 while (True) {
827 n = VG_(read)(fd, &ch, 1);
828 if (n == 0) return False; /* the next call will return True */
829 if (ch == '\n') break;
830 if (i > 0 && i == nBuf-1) i--;
831 buf[i++] = ch; buf[i] = 0;
832 }
njn0c0f32a2005-03-26 04:14:01 +0000833 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000834 i--; buf[i] = 0;
835 };
836
njn02bc4b82005-05-15 17:28:26 +0000837 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000838 /* Ok, we have a line. If a non-comment line, return.
839 If a comment line, start all over again. */
840 if (buf[0] != '#') return False;
841 }
842}
843
844
845/* *p_caller contains the raw name of a caller, supposedly either
846 fun:some_function_name or
847 obj:some_object_name.
848 Set *p_ty accordingly and advance *p_caller over the descriptor
849 (fun: or obj:) part.
850 Returns False if failed.
851*/
sewardjb5f6f512005-03-10 23:59:00 +0000852static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000853{
sewardjb5f6f512005-03-10 23:59:00 +0000854 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
855 p->name += 4;
856 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000857 return True;
858 }
sewardjb5f6f512005-03-10 23:59:00 +0000859 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
860 p->name += 4;
861 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000862 return True;
863 }
864 VG_(printf)("location should start with fun: or obj:\n");
865 return False;
866}
867
868
nethercote7cc9c232004-01-21 15:08:04 +0000869/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000870static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000871Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000872{
873 Bool found;
874 Char *s = NULL; /* Shut gcc up */
875 Int len = VG_(strlen)(name);
876
877 found = (NULL != (s = VG_(strstr)(names, name)) &&
878 (s == names || *(s-1) == ',') &&
879 (*(s+len) == ',' || *(s+len) == '\0')
880 );
881
882 return found;
883}
884
njn695c16e2005-03-27 03:40:28 +0000885/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000886 and place them in the suppressions list. If there's any difficulty
887 doing this, just give up -- there's no point in trying to recover.
888*/
sewardjde4a1d02002-03-22 01:27:54 +0000889static void load_one_suppressions_file ( Char* filename )
890{
891# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000892 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000893 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000894 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000895 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000896 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000897 Char* err_str = NULL;
898 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000899
njn25e49d8e72002-09-23 09:36:25 +0000900 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000901 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000902 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000903 filename );
904 VG_(exit)(1);
905 }
906
sewardjb5f6f512005-03-10 23:59:00 +0000907#define BOMB(S) { err_str = S; goto syntax_error; }
908
sewardjde4a1d02002-03-22 01:27:54 +0000909 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000910 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000911 Supp* supp;
912 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000913 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000914
915 // Initialise temporary reading-in buffer.
916 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
917 tmp_callers[i].ty = NoName;
918 tmp_callers[i].name = NULL;
919 }
920
njn810086f2002-11-14 12:42:47 +0000921 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000922
njn4ba5a792002-09-30 10:23:54 +0000923 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000924 if (eof) break;
925
sewardjb5f6f512005-03-10 23:59:00 +0000926 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000927
njn4ba5a792002-09-30 10:23:54 +0000928 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000929
930 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
931
njn25e49d8e72002-09-23 09:36:25 +0000932 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000933
njn4ba5a792002-09-30 10:23:54 +0000934 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000935
sewardjb5f6f512005-03-10 23:59:00 +0000936 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000937
njn94065fd2004-11-22 19:26:27 +0000938 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000939 i = 0;
940 while (True) {
941 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000942 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000943 i++;
njn25e49d8e72002-09-23 09:36:25 +0000944 }
njnc40c3a82002-10-02 11:02:27 +0000945 buf[i] = '\0'; /* Replace ':', splitting into two strings */
946
nethercote7cc9c232004-01-21 15:08:04 +0000947 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000948 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000949
nethercote7cc9c232004-01-21 15:08:04 +0000950 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000951 {
sewardjb5f6f512005-03-10 23:59:00 +0000952 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000953 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000954 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000955 else
sewardjb5f6f512005-03-10 23:59:00 +0000956 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000957 }
njn95ec8702004-11-22 16:46:13 +0000958 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000959 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000960 {
sewardjb5f6f512005-03-10 23:59:00 +0000961 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000962 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000963 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000964 } else {
965 BOMB("unknown tool suppression type");
966 }
njnc40c3a82002-10-02 11:02:27 +0000967 }
njn25e49d8e72002-09-23 09:36:25 +0000968 else {
sewardjb5f6f512005-03-10 23:59:00 +0000969 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000970 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000971 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000972 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000973 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000974 break;
975 }
976 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000977 }
978
njn95ec8702004-11-22 16:46:13 +0000979 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000980 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000981 {
982 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000983 }
984
sewardjb5f6f512005-03-10 23:59:00 +0000985 i = 0;
986 while (True) {
987 eof = VG_(get_line) ( fd, buf, N_BUF );
988 if (eof)
989 BOMB("unexpected end-of-file");
990 if (VG_STREQ(buf, "}")) {
991 if (i > 0) {
992 break;
993 } else {
994 BOMB("missing stack trace");
995 }
996 }
997 if (i == VG_MAX_SUPP_CALLERS)
998 BOMB("too many callers in stack trace");
999 if (i > 0 && i >= VG_(clo_backtrace_size))
1000 break;
1001 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
1002 if (!setLocationTy(&(tmp_callers[i])))
1003 BOMB("location should start with 'fun:' or 'obj:'");
1004 i++;
1005 }
1006
1007 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
1008 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +00001009 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +00001010 do {
1011 eof = VG_(get_line) ( fd, buf, N_BUF );
1012 } while (!eof && !VG_STREQ(buf, "}"));
1013 }
1014
1015 // Copy tmp_callers[] into supp->callers[]
1016 supp->n_callers = i;
1017 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1018 for (i = 0; i < supp->n_callers; i++) {
1019 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001020 }
1021
njn695c16e2005-03-27 03:40:28 +00001022 supp->next = suppressions;
1023 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001024 }
sewardjde4a1d02002-03-22 01:27:54 +00001025 VG_(close)(fd);
1026 return;
1027
1028 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001029 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001030 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001031
sewardjde4a1d02002-03-22 01:27:54 +00001032 VG_(close)(fd);
1033 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001034 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001035
sewardjb5f6f512005-03-10 23:59:00 +00001036# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001037# undef N_BUF
1038}
1039
1040
1041void VG_(load_suppressions) ( void )
1042{
1043 Int i;
njn695c16e2005-03-27 03:40:28 +00001044 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001045 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1046 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001047 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1048 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001049 }
1050 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1051 }
1052}
1053
sewardjb5f6f512005-03-10 23:59:00 +00001054static
njn810086f2002-11-14 12:42:47 +00001055Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001056{
njn810086f2002-11-14 12:42:47 +00001057 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001058 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001059 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001060 default:
njn95ec8702004-11-22 16:46:13 +00001061 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001062 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001063 } else {
1064 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001065 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001066 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001067 err->ekind);
njn67993252004-11-22 18:02:32 +00001068 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001069 }
1070 }
1071}
1072
sewardjb5f6f512005-03-10 23:59:00 +00001073static
1074Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001075{
1076 Int i;
njn83f9e792005-06-11 05:04:09 +00001077 Char caller_name[ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001078 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001079
sewardjb5f6f512005-03-10 23:59:00 +00001080 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001081 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001082 vg_assert(su->callers[i].name != NULL);
1083 switch (su->callers[i].ty) {
1084 case ObjName:
njn83f9e792005-06-11 05:04:09 +00001085 if (!VG_(get_objname)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001086 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001087 break;
1088
1089 case FunName:
1090 // Nb: mangled names used in suppressions
njn83f9e792005-06-11 05:04:09 +00001091 if (!VG_(get_fnname_nodemangle)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001092 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001093 break;
njn67993252004-11-22 18:02:32 +00001094 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001095 }
sewardjb5f6f512005-03-10 23:59:00 +00001096 if (!VG_(string_match)(su->callers[i].name, caller_name))
1097 return False;
njn25e49d8e72002-09-23 09:36:25 +00001098 }
1099
1100 /* If we reach here, it's a match */
1101 return True;
1102}
sewardjde4a1d02002-03-22 01:27:54 +00001103
njn810086f2002-11-14 12:42:47 +00001104/* Does an error context match a suppression? ie is this a suppressible
1105 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001106 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001107*/
njn810086f2002-11-14 12:42:47 +00001108static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001109{
njn810086f2002-11-14 12:42:47 +00001110 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001111
sewardjde4a1d02002-03-22 01:27:54 +00001112 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001113 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001114 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001115 supp_matches_callers(err, su))
1116 {
njn25e49d8e72002-09-23 09:36:25 +00001117 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001118 }
sewardjde4a1d02002-03-22 01:27:54 +00001119 }
njn25e49d8e72002-09-23 09:36:25 +00001120 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001121}
1122
sewardjde4a1d02002-03-22 01:27:54 +00001123/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001124/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001125/*--------------------------------------------------------------------*/