blob: fe30e08b4e1263a39ae734fac322dad84740dd20 [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"
njn43b9a8a2005-05-10 04:37:01 +000044#include "pub_core_tooliface.h"
njn24a6efb2005-06-20 03:36:51 +000045#include "pub_core_translate.h" // for VG_(translate)()
sewardjde4a1d02002-03-22 01:27:54 +000046
47/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000048/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000049/*------------------------------------------------------------*/
50
njn14319cc2005-03-13 06:26:22 +000051/* After this many different unsuppressed errors have been observed,
52 be more conservative about collecting new ones. */
53#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
54
55/* After this many different unsuppressed errors have been observed,
56 stop collecting errors at all, and tell the user their program is
57 evidently a steaming pile of camel dung. */
58#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
59
60/* After this many total errors have been observed, stop collecting
61 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
62#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
63
sewardjde4a1d02002-03-22 01:27:54 +000064/* The list of error contexts found, both suppressed and unsuppressed.
65 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000066static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000067
68/* The list of suppression directives, as read from the specified
69 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000070static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000071
72/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000073static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000074
75/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000076static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000077
78/* forwards ... */
njn810086f2002-11-14 12:42:47 +000079static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000080
sewardjb5f6f512005-03-10 23:59:00 +000081static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000082
83/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000084/*--- Error type ---*/
85/*------------------------------------------------------------*/
86
nethercote996901a2004-08-03 13:29:09 +000087/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000088 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000089
90/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000091 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000092 Functions for getting/setting the tool-relevant fields are in
njnc7561b92005-06-19 01:24:32 +000093 include/pub_tool_errormgr.h.
nethercote4a184902004-08-02 12:21:09 +000094
95 When errors are found and recorded with VG_(maybe_record_error)(), all
96 the tool must do is pass in the four parameters; core will
97 allocate/initialise the error record.
98*/
99struct _Error {
100 struct _Error* next;
101 // NULL if unsuppressed; or ptr to suppression record.
102 Supp* supp;
103 Int count;
104 ThreadId tid;
105
106 // The tool-specific part
107 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000108 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000109 Addr addr; // Used frequently
110 Char* string; // Used frequently
111 void* extra; // For any tool-specific extras
112};
113
114ExeContext* VG_(get_error_where) ( Error* err )
115{
116 return err->where;
117}
118
119ErrorKind VG_(get_error_kind) ( Error* err )
120{
121 return err->ekind;
122}
123
124Addr VG_(get_error_address) ( Error* err )
125{
126 return err->addr;
127}
128
129Char* VG_(get_error_string) ( Error* err )
130{
131 return err->string;
132}
133
134void* VG_(get_error_extra) ( Error* err )
135{
136 return err->extra;
137}
138
nethercotef2b11482004-08-02 12:36:01 +0000139UInt VG_(get_n_errs_found)( void )
140{
141 return n_errs_found;
142}
143
nethercote4a184902004-08-02 12:21:09 +0000144/*------------------------------------------------------------*/
145/*--- Suppression type ---*/
146/*------------------------------------------------------------*/
147
148/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
149 * effectively extend it by defining their own enums in the (0..) range. */
150typedef
151 enum {
152 PThreadSupp = -1, /* Matches PThreadErr */
153 }
154 CoreSuppKind;
155
sewardjb5f6f512005-03-10 23:59:00 +0000156/* Max number of callers for context in a suppression. */
157#define VG_MAX_SUPP_CALLERS 24
158
nethercote4a184902004-08-02 12:21:09 +0000159/* For each caller specified for a suppression, record the nature of
160 the caller name. Not of interest to tools. */
161typedef
162 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000163 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000164 ObjName, /* Name is of an shared object file. */
165 FunName /* Name is of a function. */
166 }
167 SuppLocTy;
168
sewardjb5f6f512005-03-10 23:59:00 +0000169typedef
170 struct {
171 SuppLocTy ty;
172 Char* name;
173 }
174 SuppLoc;
175
nethercote4a184902004-08-02 12:21:09 +0000176/* Suppressions. Tools can get/set tool-relevant parts with functions
njnc7561b92005-06-19 01:24:32 +0000177 declared in include/pub_tool_errormgr.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000178 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000179 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000180struct _Supp {
181 struct _Supp* next;
182 Int count; // The number of times this error has been suppressed.
183 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000184
185 // Length of 'callers'
186 Int n_callers;
187 // Array of callers, for matching stack traces. First one (name of fn
188 // where err occurs) is mandatory; rest are optional.
189 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000190
191 /* The tool-specific part */
192 SuppKind skind; // What kind of suppression. Must use the range (0..).
193 Char* string; // String -- use is optional. NULL by default.
194 void* extra; // Anything else -- use is optional. NULL by default.
195};
196
197SuppKind VG_(get_supp_kind) ( Supp* su )
198{
199 return su->skind;
200}
201
202Char* VG_(get_supp_string) ( Supp* su )
203{
204 return su->string;
205}
206
207void* VG_(get_supp_extra) ( Supp* su )
208{
209 return su->extra;
210}
211
212
213void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
214{
215 su->skind = skind;
216}
217
218void VG_(set_supp_string) ( Supp* su, Char* string )
219{
220 su->string = string;
221}
222
223void VG_(set_supp_extra) ( Supp* su, void* extra )
224{
225 su->extra = extra;
226}
227
228
229/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000230/*--- Helper fns ---*/
231/*------------------------------------------------------------*/
232
sewardjde4a1d02002-03-22 01:27:54 +0000233/* Compare error contexts, to detect duplicates. Note that if they
234 are otherwise the same, the faulting addrs and associated rwoffsets
235 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000236static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000237{
njn810086f2002-11-14 12:42:47 +0000238 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000239 return False;
njn25e49d8e72002-09-23 09:36:25 +0000240 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000241 return False;
242
njn810086f2002-11-14 12:42:47 +0000243 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000244 // case ThreadErr:
245 // case MutexErr:
246 // vg_assert(VG_(needs).core_errors);
247 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000248 default:
njn51d827b2005-05-09 01:02:08 +0000249 if (VG_(needs).tool_errors) {
250 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
251 } else {
njn95ec8702004-11-22 16:46:13 +0000252 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000253 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000254 e1->ekind);
njn67993252004-11-22 18:02:32 +0000255 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000256 }
sewardjde4a1d02002-03-22 01:27:54 +0000257 }
258}
259
njn810086f2002-11-14 12:42:47 +0000260static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000261{
sewardj71bc3cb2005-05-19 00:25:45 +0000262 if (VG_(clo_xml)) {
263 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000264 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
265 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000266 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
267 }
268
269 if (!VG_(clo_xml)) {
270 if (printCount)
271 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
272 if (err->tid > 0 && err->tid != last_tid_printed) {
273 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
274 last_tid_printed = err->tid;
275 }
sewardjb5f6f512005-03-10 23:59:00 +0000276 }
njn25e49d8e72002-09-23 09:36:25 +0000277
njn810086f2002-11-14 12:42:47 +0000278 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000279 // case ThreadErr:
280 // case MutexErr:
281 // vg_assert(VG_(needs).core_errors);
282 // VG_(tm_error_print)(err);
283 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000284 default:
njn95ec8702004-11-22 16:46:13 +0000285 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000286 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000287 else {
njn95ec8702004-11-22 16:46:13 +0000288 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000289 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000290 err->ekind);
njn67993252004-11-22 18:02:32 +0000291 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000292 }
sewardjde4a1d02002-03-22 01:27:54 +0000293 }
sewardj71bc3cb2005-05-19 00:25:45 +0000294
295 if (VG_(clo_xml))
296 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000297}
298
nethercote04d0fbc2004-01-26 16:48:06 +0000299/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000300 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000301Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000302{
303 Char ch, ch2;
304 Int res;
305
njn43c799e2003-04-08 00:08:52 +0000306 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000307 return False;
308
309 VG_(message)(Vg_UserMsg, "");
310
311 again:
312 VG_(printf)(
313 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000314 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
315 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000316 );
317
sewardj6024b212003-07-13 10:54:33 +0000318 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000319 if (res != 1) goto ioerror;
320 /* res == 1 */
321 if (ch == '\n') return False;
322 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
323 && ch != 'C' && ch != 'c') goto again;
324
sewardj6024b212003-07-13 10:54:33 +0000325 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000326 if (res != 1) goto ioerror;
327 if (ch2 != '\n') goto again;
328
njn43c799e2003-04-08 00:08:52 +0000329 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000330 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000331 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000332 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000333 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000334 vg_assert(ch == 'c' || ch == 'C');
335
336 ioerror:
njn43c799e2003-04-08 00:08:52 +0000337 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000338 return False;
339}
340
341
sewardjb5f6f512005-03-10 23:59:00 +0000342/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000343static __inline__
njn72718642003-07-24 08:45:32 +0000344void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
345 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000346{
njnca82cc02004-11-22 17:18:48 +0000347 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000348
njn810086f2002-11-14 12:42:47 +0000349 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000350 err->next = NULL;
351 err->supp = NULL;
352 err->count = 1;
njn72718642003-07-24 08:45:32 +0000353 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000354 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000355 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000356 else
357 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000358
nethercote996901a2004-08-03 13:29:09 +0000359 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000360 err->ekind = ekind;
361 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000362 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000363 err->string = s;
364
njn25e49d8e72002-09-23 09:36:25 +0000365 /* sanity... */
njn72718642003-07-24 08:45:32 +0000366 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000367}
368
njn83f9e792005-06-11 05:04:09 +0000369#define ERRTXT_LEN 4096
370
njnf4261312005-03-20 23:45:36 +0000371static void printSuppForIp(UInt n, Addr ip)
372{
njn83f9e792005-06-11 05:04:09 +0000373 static UChar buf[ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000374
njn83f9e792005-06-11 05:04:09 +0000375 if ( VG_(get_fnname_nodemangle) (ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000376 VG_(printf)(" fun:%s\n", buf);
njn83f9e792005-06-11 05:04:09 +0000377 } else if ( VG_(get_objname)(ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000378 VG_(printf)(" obj:%s\n", buf);
379 } else {
380 VG_(printf)(" ???:??? "
381 "# unknown, suppression will not work, sorry\n");
382 }
383}
384
nethercote10d481a2004-01-25 20:33:53 +0000385static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000386{
njn43c799e2003-04-08 00:08:52 +0000387 ExeContext* ec = VG_(get_error_where)(err);
388 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000389
sewardjb5f6f512005-03-10 23:59:00 +0000390 /* At most VG_MAX_SUPP_CALLERS names */
391 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000392 vg_assert(stop_at > 0);
393
394 VG_(printf)("{\n");
395 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000396
sewardjb5f6f512005-03-10 23:59:00 +0000397 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000398 VG_(printf)(" core:PThread\n");
399
400 } else {
njn51d827b2005-05-09 01:02:08 +0000401 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000402 if (NULL == name) {
403 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000404 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000405 return;
406 }
407 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000408 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000409 }
njn43c799e2003-04-08 00:08:52 +0000410
njnf4261312005-03-20 23:45:36 +0000411 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000412 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000413
414 VG_(printf)("}\n");
415}
416
njnb4aee052003-04-15 14:09:58 +0000417static
nethercote04d0fbc2004-01-26 16:48:06 +0000418void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000419{
sewardjd153fae2005-01-10 17:24:47 +0000420 Bool still_noisy = True;
421
nethercote04d0fbc2004-01-26 16:48:06 +0000422 /* Perhaps we want a debugger attach at this point? */
423 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000424 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
425 {
nethercote04d0fbc2004-01-26 16:48:06 +0000426 VG_(printf)("starting debugger\n");
427 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000428 }
njn43c799e2003-04-08 00:08:52 +0000429 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000430 if (VG_(clo_gen_suppressions) == 2
431 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000432 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000433 ) {
nethercote42602b12004-01-25 19:30:29 +0000434 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000435 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
436 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000437 }
438}
439
440/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
441 just for pretty printing purposes. */
442static Bool is_first_shown_context = True;
443
njn25e49d8e72002-09-23 09:36:25 +0000444/* Top-level entry point to the error management subsystem.
445 All detected errors are notified here; this routine decides if/when the
446 user should see the error. */
njn72718642003-07-24 08:45:32 +0000447void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000448 ErrorKind ekind, Addr a, Char* s, void* extra )
449{
njn810086f2002-11-14 12:42:47 +0000450 Error err;
451 Error* p;
452 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000453 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000454 VgRes exe_res = Vg_MedRes;
455 static Bool stopping_message = False;
456 static Bool slowdown_message = False;
457 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000458
njn14319cc2005-03-13 06:26:22 +0000459 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
460 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000461 have been found, just refuse to collect any more. This stops
462 the burden of the error-management system becoming excessive in
463 extremely buggy programs, although it does make it pretty
464 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000465 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000466 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000467 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000468 if (!stopping_message) {
469 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000470
njn695c16e2005-03-27 03:40:28 +0000471 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000472 VG_(message)(Vg_UserMsg,
473 "More than %d different errors detected. "
474 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000475 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000476 } else {
477 VG_(message)(Vg_UserMsg,
478 "More than %d total errors detected. "
479 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000480 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000481 }
482
sewardjde4a1d02002-03-22 01:27:54 +0000483 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000484 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000485 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000486 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000487 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000488 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000489 VG_(message)(Vg_UserMsg,
490 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000491 VG_(message)(Vg_UserMsg, "");
492 stopping_message = True;
493 }
494 return;
495 }
496
njn14319cc2005-03-13 06:26:22 +0000497 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000498 been found, be much more conservative about collecting new
499 ones. */
njn695c16e2005-03-27 03:40:28 +0000500 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000501 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000502 if (!slowdown_message) {
503 VG_(message)(Vg_UserMsg, "");
504 VG_(message)(Vg_UserMsg,
505 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000506 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000507 VG_(message)(Vg_UserMsg,
508 "will still be recorded, but in less detail than before.");
509 slowdown_message = True;
510 }
511 }
512
njn25e49d8e72002-09-23 09:36:25 +0000513 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000514 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000515
516 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000517 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000518 p_prev = NULL;
519 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000520 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000521 /* Found it. */
522 p->count++;
523 if (p->supp != NULL) {
524 /* Deal correctly with suppressed errors. */
525 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000526 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000527 } else {
nethercotef2b11482004-08-02 12:36:01 +0000528 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000529 }
530
531 /* Move p to the front of the list so that future searches
532 for it are faster. */
533 if (p_prev != NULL) {
534 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000535 p_prev->next = p->next;
536 p->next = errors;
537 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000538 }
sewardj7ebf7c32003-07-24 21:29:40 +0000539
sewardjde4a1d02002-03-22 01:27:54 +0000540 return;
541 }
542 p_prev = p;
543 p = p->next;
544 }
545
546 /* Didn't see it. Copy and add. */
547
njn43c799e2003-04-08 00:08:52 +0000548 /* OK, we're really going to collect it. The context is on the stack and
549 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000550 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000551
njn02bc4b82005-05-15 17:28:26 +0000552 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000553 is for when there are more details to fill in which take time to work
554 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000555 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000556 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000557
njn02bc4b82005-05-15 17:28:26 +0000558 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000559 VG_(tdict).tool_update_extra returned. Also allow for people using
560 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000561 */
562
563 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000564 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000565 *p = err;
njn43c799e2003-04-08 00:08:52 +0000566
njn02bc4b82005-05-15 17:28:26 +0000567 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000568 switch (ekind) {
569 // case ThreadErr:
570 // case MutexErr:
571 // vg_assert(VG_(needs).core_errors);
572 // extra_size = VG_(tm_error_update_extra)(p);
573 // break;
574 default:
575 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000576 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000577 break;
578 }
njn43c799e2003-04-08 00:08:52 +0000579
njn02bc4b82005-05-15 17:28:26 +0000580 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000581 if (NULL != p->extra && 0 != extra_size) {
582 void* new_extra = VG_(malloc)(extra_size);
583 VG_(memcpy)(new_extra, p->extra, extra_size);
584 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000585 }
586
njn695c16e2005-03-27 03:40:28 +0000587 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000588 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000589 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000590 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000591 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000592 if (!is_first_shown_context)
593 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000594 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000595 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000596 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000597 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000598 } else {
nethercotef2b11482004-08-02 12:36:01 +0000599 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000600 p->supp->count++;
601 }
602}
603
njn43c799e2003-04-08 00:08:52 +0000604/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000605 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000606 guaranteed to only happen once. This avoids all the recording and
607 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000608 suppressed. Bool 'print_error' dictates whether to print the error.
609 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000610*/
njn72718642003-07-24 08:45:32 +0000611Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000612 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000613 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000614{
615 Error err;
616
617 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000618 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000619
620 /* Unless it's suppressed, we're going to show it. Don't need to make
621 a copy, because it's only temporary anyway.
622
njn02bc4b82005-05-15 17:28:26 +0000623 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000624 because that can have an affect on whether it's suppressed. Ignore
625 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000626 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000627 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000628
629 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000630 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000631 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000632
633 if (print_error) {
634 if (!is_first_shown_context)
635 VG_(message)(Vg_UserMsg, "");
636 pp_Error(&err, False);
637 is_first_shown_context = False;
638 }
nethercote04d0fbc2004-01-26 16:48:06 +0000639 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000640
641 return False;
642
643 } else {
nethercotef2b11482004-08-02 12:36:01 +0000644 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000645 return True;
646 }
647}
648
sewardjde4a1d02002-03-22 01:27:54 +0000649
sewardjde4a1d02002-03-22 01:27:54 +0000650/*------------------------------------------------------------*/
651/*--- Exported fns ---*/
652/*------------------------------------------------------------*/
653
sewardj71bc3cb2005-05-19 00:25:45 +0000654/* Show the used suppressions. Returns False if no suppression
655 got used. */
656static Bool show_used_suppressions ( void )
657{
658 Supp *su;
659 Bool any_supp;
660
sewardj7c9e57c2005-05-24 14:21:45 +0000661 if (VG_(clo_xml))
662 VG_(message)(Vg_DebugMsg, "<suppcounts>");
663
sewardj71bc3cb2005-05-19 00:25:45 +0000664 any_supp = False;
665 for (su = suppressions; su != NULL; su = su->next) {
666 if (su->count <= 0)
667 continue;
668 any_supp = True;
669 if (VG_(clo_xml)) {
670 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000671 " <pair>\n"
672 " <count>%d</count>\n"
673 " <name>%s</name>\n"
674 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000675 su->count, su->sname);
676 } else {
677 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
678 }
679 }
680
sewardj7c9e57c2005-05-24 14:21:45 +0000681 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000682 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000683
sewardj71bc3cb2005-05-19 00:25:45 +0000684 return any_supp;
685}
686
687
sewardj9f297ca2005-05-20 02:29:52 +0000688/* Show all the errors that occurred, and possibly also the
689 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000690void VG_(show_all_errors) ( void )
691{
njn810086f2002-11-14 12:42:47 +0000692 Int i, n_min;
693 Int n_err_contexts, n_supp_contexts;
694 Error *p, *p_min;
695 Supp *su;
696 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000697
698 if (VG_(clo_verbosity) == 0)
699 return;
700
701 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000702 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000703 if (p->supp == NULL)
704 n_err_contexts++;
705 }
706
707 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000708 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000709 if (su->count > 0)
710 n_supp_contexts++;
711 }
sewardj71bc3cb2005-05-19 00:25:45 +0000712
713 /* If we're printing XML, just show the suppressions and stop.
714 */
715 if (VG_(clo_xml)) {
716 (void)show_used_suppressions();
717 return;
718 }
719
720 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000721 VG_(message)(Vg_UserMsg,
722 "ERROR SUMMARY: "
723 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000724 n_errs_found, n_err_contexts,
725 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000726
727 if (VG_(clo_verbosity) <= 1)
728 return;
729
730 /* Print the contexts in order of increasing error count. */
731 for (i = 0; i < n_err_contexts; i++) {
732 n_min = (1 << 30) - 1;
733 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000734 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000735 if (p->supp != NULL) continue;
736 if (p->count < n_min) {
737 n_min = p->count;
738 p_min = p;
739 }
740 }
njn67993252004-11-22 18:02:32 +0000741 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000742
743 VG_(message)(Vg_UserMsg, "");
744 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
745 p_min->count,
746 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000747 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000748
749 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000750 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000751 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njn394213a2005-06-19 18:38:24 +0000752 ips[0], /*debugging*/True, 0xFE/*verbosity*/,
753 /*bbs_done*/0);
sewardjde4a1d02002-03-22 01:27:54 +0000754 }
755
756 p_min->count = 1 << 30;
757 }
758
759 if (n_supp_contexts > 0)
760 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000761 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000762
763 if (n_err_contexts > 0) {
764 if (any_supp)
765 VG_(message)(Vg_UserMsg, "");
766 VG_(message)(Vg_UserMsg,
767 "IN SUMMARY: "
768 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000769 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000770 n_supp_contexts );
771 VG_(message)(Vg_UserMsg, "");
772 }
773}
774
sewardj9f297ca2005-05-20 02:29:52 +0000775
776/* Show occurrence counts of all errors, in XML form. */
777void VG_(show_error_counts_as_XML) ( void )
778{
779 Error* err;
780 VG_(message)(Vg_UserMsg, "<errorcounts>");
781 for (err = errors; err != NULL; err = err->next) {
782 if (err->supp != NULL)
783 continue;
784 if (err->count <= 0)
785 continue;
786 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000787 Vg_UserMsg, " <pair> <count>%d</count> "
788 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000789 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000790 );
791 }
792 VG_(message)(Vg_UserMsg, "</errorcounts>");
793}
794
795
sewardjde4a1d02002-03-22 01:27:54 +0000796/*------------------------------------------------------------*/
797/*--- Standard suppressions ---*/
798/*------------------------------------------------------------*/
799
800/* Get a non-blank, non-comment line of at most nBuf chars from fd.
801 Skips leading spaces on the line. Return True if EOF was hit instead.
802*/
njn4ba5a792002-09-30 10:23:54 +0000803Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000804{
805 Char ch;
806 Int n, i;
807 while (True) {
808 /* First, read until a non-blank char appears. */
809 while (True) {
810 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000811 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000812 if (n == 0) return True;
813 }
814
815 /* Now, read the line into buf. */
816 i = 0;
817 buf[i++] = ch; buf[i] = 0;
818 while (True) {
819 n = VG_(read)(fd, &ch, 1);
820 if (n == 0) return False; /* the next call will return True */
821 if (ch == '\n') break;
822 if (i > 0 && i == nBuf-1) i--;
823 buf[i++] = ch; buf[i] = 0;
824 }
njn0c0f32a2005-03-26 04:14:01 +0000825 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000826 i--; buf[i] = 0;
827 };
828
njn02bc4b82005-05-15 17:28:26 +0000829 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000830 /* Ok, we have a line. If a non-comment line, return.
831 If a comment line, start all over again. */
832 if (buf[0] != '#') return False;
833 }
834}
835
836
837/* *p_caller contains the raw name of a caller, supposedly either
838 fun:some_function_name or
839 obj:some_object_name.
840 Set *p_ty accordingly and advance *p_caller over the descriptor
841 (fun: or obj:) part.
842 Returns False if failed.
843*/
sewardjb5f6f512005-03-10 23:59:00 +0000844static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000845{
sewardjb5f6f512005-03-10 23:59:00 +0000846 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
847 p->name += 4;
848 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000849 return True;
850 }
sewardjb5f6f512005-03-10 23:59:00 +0000851 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
852 p->name += 4;
853 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000854 return True;
855 }
856 VG_(printf)("location should start with fun: or obj:\n");
857 return False;
858}
859
860
nethercote7cc9c232004-01-21 15:08:04 +0000861/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000862static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000863Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000864{
865 Bool found;
866 Char *s = NULL; /* Shut gcc up */
867 Int len = VG_(strlen)(name);
868
869 found = (NULL != (s = VG_(strstr)(names, name)) &&
870 (s == names || *(s-1) == ',') &&
871 (*(s+len) == ',' || *(s+len) == '\0')
872 );
873
874 return found;
875}
876
njn695c16e2005-03-27 03:40:28 +0000877/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000878 and place them in the suppressions list. If there's any difficulty
879 doing this, just give up -- there's no point in trying to recover.
880*/
sewardjde4a1d02002-03-22 01:27:54 +0000881static void load_one_suppressions_file ( Char* filename )
882{
883# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000884 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000885 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000886 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000887 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000888 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000889 Char* err_str = NULL;
890 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000891
njn25e49d8e72002-09-23 09:36:25 +0000892 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000893 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000894 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000895 filename );
896 VG_(exit)(1);
897 }
898
sewardjb5f6f512005-03-10 23:59:00 +0000899#define BOMB(S) { err_str = S; goto syntax_error; }
900
sewardjde4a1d02002-03-22 01:27:54 +0000901 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000902 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000903 Supp* supp;
904 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000905 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000906
907 // Initialise temporary reading-in buffer.
908 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
909 tmp_callers[i].ty = NoName;
910 tmp_callers[i].name = NULL;
911 }
912
njn810086f2002-11-14 12:42:47 +0000913 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000914
njn4ba5a792002-09-30 10:23:54 +0000915 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000916 if (eof) break;
917
sewardjb5f6f512005-03-10 23:59:00 +0000918 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000919
njn4ba5a792002-09-30 10:23:54 +0000920 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000921
922 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
923
njn25e49d8e72002-09-23 09:36:25 +0000924 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000925
njn4ba5a792002-09-30 10:23:54 +0000926 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000927
sewardjb5f6f512005-03-10 23:59:00 +0000928 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000929
njn94065fd2004-11-22 19:26:27 +0000930 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000931 i = 0;
932 while (True) {
933 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000934 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000935 i++;
njn25e49d8e72002-09-23 09:36:25 +0000936 }
njnc40c3a82002-10-02 11:02:27 +0000937 buf[i] = '\0'; /* Replace ':', splitting into two strings */
938
nethercote7cc9c232004-01-21 15:08:04 +0000939 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000940 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000941
nethercote7cc9c232004-01-21 15:08:04 +0000942 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000943 {
sewardjb5f6f512005-03-10 23:59:00 +0000944 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000945 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000946 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000947 else
sewardjb5f6f512005-03-10 23:59:00 +0000948 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000949 }
njn95ec8702004-11-22 16:46:13 +0000950 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000951 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000952 {
sewardjb5f6f512005-03-10 23:59:00 +0000953 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000954 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000955 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000956 } else {
957 BOMB("unknown tool suppression type");
958 }
njnc40c3a82002-10-02 11:02:27 +0000959 }
njn25e49d8e72002-09-23 09:36:25 +0000960 else {
sewardjb5f6f512005-03-10 23:59:00 +0000961 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000962 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000963 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000964 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000965 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000966 break;
967 }
968 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000969 }
970
njn95ec8702004-11-22 16:46:13 +0000971 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000972 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000973 {
974 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000975 }
976
sewardjb5f6f512005-03-10 23:59:00 +0000977 i = 0;
978 while (True) {
979 eof = VG_(get_line) ( fd, buf, N_BUF );
980 if (eof)
981 BOMB("unexpected end-of-file");
982 if (VG_STREQ(buf, "}")) {
983 if (i > 0) {
984 break;
985 } else {
986 BOMB("missing stack trace");
987 }
988 }
989 if (i == VG_MAX_SUPP_CALLERS)
990 BOMB("too many callers in stack trace");
991 if (i > 0 && i >= VG_(clo_backtrace_size))
992 break;
993 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
994 if (!setLocationTy(&(tmp_callers[i])))
995 BOMB("location should start with 'fun:' or 'obj:'");
996 i++;
997 }
998
999 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
1000 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +00001001 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +00001002 do {
1003 eof = VG_(get_line) ( fd, buf, N_BUF );
1004 } while (!eof && !VG_STREQ(buf, "}"));
1005 }
1006
1007 // Copy tmp_callers[] into supp->callers[]
1008 supp->n_callers = i;
1009 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1010 for (i = 0; i < supp->n_callers; i++) {
1011 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001012 }
1013
njn695c16e2005-03-27 03:40:28 +00001014 supp->next = suppressions;
1015 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001016 }
sewardjde4a1d02002-03-22 01:27:54 +00001017 VG_(close)(fd);
1018 return;
1019
1020 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001021 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001022 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001023
sewardjde4a1d02002-03-22 01:27:54 +00001024 VG_(close)(fd);
1025 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001026 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001027
sewardjb5f6f512005-03-10 23:59:00 +00001028# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001029# undef N_BUF
1030}
1031
1032
1033void VG_(load_suppressions) ( void )
1034{
1035 Int i;
njn695c16e2005-03-27 03:40:28 +00001036 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001037 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1038 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001039 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1040 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001041 }
1042 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1043 }
1044}
1045
sewardjb5f6f512005-03-10 23:59:00 +00001046static
njn810086f2002-11-14 12:42:47 +00001047Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001048{
njn810086f2002-11-14 12:42:47 +00001049 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001050 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001051 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001052 default:
njn95ec8702004-11-22 16:46:13 +00001053 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001054 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001055 } else {
1056 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001057 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001058 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001059 err->ekind);
njn67993252004-11-22 18:02:32 +00001060 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001061 }
1062 }
1063}
1064
sewardjb5f6f512005-03-10 23:59:00 +00001065static
1066Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001067{
1068 Int i;
njn83f9e792005-06-11 05:04:09 +00001069 Char caller_name[ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001070 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001071
sewardjb5f6f512005-03-10 23:59:00 +00001072 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001073 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001074 vg_assert(su->callers[i].name != NULL);
1075 switch (su->callers[i].ty) {
1076 case ObjName:
njn83f9e792005-06-11 05:04:09 +00001077 if (!VG_(get_objname)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001078 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001079 break;
1080
1081 case FunName:
1082 // Nb: mangled names used in suppressions
njn83f9e792005-06-11 05:04:09 +00001083 if (!VG_(get_fnname_nodemangle)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001084 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001085 break;
njn67993252004-11-22 18:02:32 +00001086 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001087 }
sewardjb5f6f512005-03-10 23:59:00 +00001088 if (!VG_(string_match)(su->callers[i].name, caller_name))
1089 return False;
njn25e49d8e72002-09-23 09:36:25 +00001090 }
1091
1092 /* If we reach here, it's a match */
1093 return True;
1094}
sewardjde4a1d02002-03-22 01:27:54 +00001095
njn810086f2002-11-14 12:42:47 +00001096/* Does an error context match a suppression? ie is this a suppressible
1097 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001098 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001099*/
njn810086f2002-11-14 12:42:47 +00001100static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001101{
njn810086f2002-11-14 12:42:47 +00001102 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001103
sewardjde4a1d02002-03-22 01:27:54 +00001104 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001105 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001106 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001107 supp_matches_callers(err, su))
1108 {
njn25e49d8e72002-09-23 09:36:25 +00001109 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001110 }
sewardjde4a1d02002-03-22 01:27:54 +00001111 }
njn25e49d8e72002-09-23 09:36:25 +00001112 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001113}
1114
sewardjde4a1d02002-03-22 01:27:54 +00001115/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001116/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001117/*--------------------------------------------------------------------*/