blob: de40eebfec42fae2e3eee0f241b9af70a1a12bb7 [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
nethercotef1e5e152004-09-01 23:58:16 +000031#include "core.h"
njnea27e462005-05-31 02:38:09 +000032#include "pub_core_debuginfo.h"
njnd2b17112005-04-19 04:10:25 +000033#include "pub_core_errormgr.h"
njnd01fef72005-03-25 23:35:48 +000034#include "pub_core_execontext.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000036#include "pub_core_libcassert.h"
njneb8896b2005-06-04 20:03:55 +000037#include "pub_core_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_core_libcprint.h"
njn04e16982005-05-31 00:23:43 +000039#include "pub_core_main.h" // for VG_(start_debugger)()
njnaf1d7df2005-06-11 01:31:52 +000040#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000041#include "pub_core_options.h"
njnd2b17112005-04-19 04:10:25 +000042#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000043#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000044#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000045
46/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000047/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000048/*------------------------------------------------------------*/
49
njn14319cc2005-03-13 06:26:22 +000050/* After this many different unsuppressed errors have been observed,
51 be more conservative about collecting new ones. */
52#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
53
54/* After this many different unsuppressed errors have been observed,
55 stop collecting errors at all, and tell the user their program is
56 evidently a steaming pile of camel dung. */
57#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
58
59/* After this many total errors have been observed, stop collecting
60 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
61#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
62
sewardjde4a1d02002-03-22 01:27:54 +000063/* The list of error contexts found, both suppressed and unsuppressed.
64 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000065static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000066
67/* The list of suppression directives, as read from the specified
68 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000069static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000070
71/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000072static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000073
74/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000075static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000076
77/* forwards ... */
njn810086f2002-11-14 12:42:47 +000078static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000079
sewardjb5f6f512005-03-10 23:59:00 +000080static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000081
82/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000083/*--- Error type ---*/
84/*------------------------------------------------------------*/
85
nethercote996901a2004-08-03 13:29:09 +000086/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000087 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000088
89/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000090 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000091 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000092 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000093
94 When errors are found and recorded with VG_(maybe_record_error)(), all
95 the tool must do is pass in the four parameters; core will
96 allocate/initialise the error record.
97*/
98struct _Error {
99 struct _Error* next;
100 // NULL if unsuppressed; or ptr to suppression record.
101 Supp* supp;
102 Int count;
103 ThreadId tid;
104
105 // The tool-specific part
106 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000107 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000108 Addr addr; // Used frequently
109 Char* string; // Used frequently
110 void* extra; // For any tool-specific extras
111};
112
113ExeContext* VG_(get_error_where) ( Error* err )
114{
115 return err->where;
116}
117
118ErrorKind VG_(get_error_kind) ( Error* err )
119{
120 return err->ekind;
121}
122
123Addr VG_(get_error_address) ( Error* err )
124{
125 return err->addr;
126}
127
128Char* VG_(get_error_string) ( Error* err )
129{
130 return err->string;
131}
132
133void* VG_(get_error_extra) ( Error* err )
134{
135 return err->extra;
136}
137
nethercotef2b11482004-08-02 12:36:01 +0000138UInt VG_(get_n_errs_found)( void )
139{
140 return n_errs_found;
141}
142
nethercote4a184902004-08-02 12:21:09 +0000143/*------------------------------------------------------------*/
144/*--- Suppression type ---*/
145/*------------------------------------------------------------*/
146
147/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
148 * effectively extend it by defining their own enums in the (0..) range. */
149typedef
150 enum {
151 PThreadSupp = -1, /* Matches PThreadErr */
152 }
153 CoreSuppKind;
154
sewardjb5f6f512005-03-10 23:59:00 +0000155/* Max number of callers for context in a suppression. */
156#define VG_MAX_SUPP_CALLERS 24
157
nethercote4a184902004-08-02 12:21:09 +0000158/* For each caller specified for a suppression, record the nature of
159 the caller name. Not of interest to tools. */
160typedef
161 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000162 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000163 ObjName, /* Name is of an shared object file. */
164 FunName /* Name is of a function. */
165 }
166 SuppLocTy;
167
sewardjb5f6f512005-03-10 23:59:00 +0000168typedef
169 struct {
170 SuppLocTy ty;
171 Char* name;
172 }
173 SuppLoc;
174
nethercote4a184902004-08-02 12:21:09 +0000175/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000176 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000177 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000178 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000179struct _Supp {
180 struct _Supp* next;
181 Int count; // The number of times this error has been suppressed.
182 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000183
184 // Length of 'callers'
185 Int n_callers;
186 // Array of callers, for matching stack traces. First one (name of fn
187 // where err occurs) is mandatory; rest are optional.
188 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000189
190 /* The tool-specific part */
191 SuppKind skind; // What kind of suppression. Must use the range (0..).
192 Char* string; // String -- use is optional. NULL by default.
193 void* extra; // Anything else -- use is optional. NULL by default.
194};
195
196SuppKind VG_(get_supp_kind) ( Supp* su )
197{
198 return su->skind;
199}
200
201Char* VG_(get_supp_string) ( Supp* su )
202{
203 return su->string;
204}
205
206void* VG_(get_supp_extra) ( Supp* su )
207{
208 return su->extra;
209}
210
211
212void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
213{
214 su->skind = skind;
215}
216
217void VG_(set_supp_string) ( Supp* su, Char* string )
218{
219 su->string = string;
220}
221
222void VG_(set_supp_extra) ( Supp* su, void* extra )
223{
224 su->extra = extra;
225}
226
227
228/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000229/*--- Helper fns ---*/
230/*------------------------------------------------------------*/
231
sewardjde4a1d02002-03-22 01:27:54 +0000232/* Compare error contexts, to detect duplicates. Note that if they
233 are otherwise the same, the faulting addrs and associated rwoffsets
234 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000235static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000236{
njn810086f2002-11-14 12:42:47 +0000237 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000238 return False;
njn25e49d8e72002-09-23 09:36:25 +0000239 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000240 return False;
241
njn810086f2002-11-14 12:42:47 +0000242 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000243 // case ThreadErr:
244 // case MutexErr:
245 // vg_assert(VG_(needs).core_errors);
246 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000247 default:
njn51d827b2005-05-09 01:02:08 +0000248 if (VG_(needs).tool_errors) {
249 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
250 } else {
njn95ec8702004-11-22 16:46:13 +0000251 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000252 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000253 e1->ekind);
njn67993252004-11-22 18:02:32 +0000254 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000255 }
sewardjde4a1d02002-03-22 01:27:54 +0000256 }
257}
258
njn810086f2002-11-14 12:42:47 +0000259static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000260{
sewardj71bc3cb2005-05-19 00:25:45 +0000261 if (VG_(clo_xml)) {
262 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000263 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
264 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000265 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
266 }
267
268 if (!VG_(clo_xml)) {
269 if (printCount)
270 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
271 if (err->tid > 0 && err->tid != last_tid_printed) {
272 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
273 last_tid_printed = err->tid;
274 }
sewardjb5f6f512005-03-10 23:59:00 +0000275 }
njn25e49d8e72002-09-23 09:36:25 +0000276
njn810086f2002-11-14 12:42:47 +0000277 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000278 // case ThreadErr:
279 // case MutexErr:
280 // vg_assert(VG_(needs).core_errors);
281 // VG_(tm_error_print)(err);
282 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000283 default:
njn95ec8702004-11-22 16:46:13 +0000284 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000285 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000286 else {
njn95ec8702004-11-22 16:46:13 +0000287 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000288 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000289 err->ekind);
njn67993252004-11-22 18:02:32 +0000290 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000291 }
sewardjde4a1d02002-03-22 01:27:54 +0000292 }
sewardj71bc3cb2005-05-19 00:25:45 +0000293
294 if (VG_(clo_xml))
295 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000296}
297
nethercote04d0fbc2004-01-26 16:48:06 +0000298/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000299 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000300Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000301{
302 Char ch, ch2;
303 Int res;
304
njn43c799e2003-04-08 00:08:52 +0000305 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000306 return False;
307
308 VG_(message)(Vg_UserMsg, "");
309
310 again:
311 VG_(printf)(
312 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000313 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
314 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000315 );
316
sewardj6024b212003-07-13 10:54:33 +0000317 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000318 if (res != 1) goto ioerror;
319 /* res == 1 */
320 if (ch == '\n') return False;
321 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
322 && ch != 'C' && ch != 'c') goto again;
323
sewardj6024b212003-07-13 10:54:33 +0000324 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000325 if (res != 1) goto ioerror;
326 if (ch2 != '\n') goto again;
327
njn43c799e2003-04-08 00:08:52 +0000328 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000329 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000330 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000331 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000332 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000333 vg_assert(ch == 'c' || ch == 'C');
334
335 ioerror:
njn43c799e2003-04-08 00:08:52 +0000336 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000337 return False;
338}
339
340
sewardjb5f6f512005-03-10 23:59:00 +0000341/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000342static __inline__
njn72718642003-07-24 08:45:32 +0000343void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
344 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000345{
njnca82cc02004-11-22 17:18:48 +0000346 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000347
njn810086f2002-11-14 12:42:47 +0000348 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000349 err->next = NULL;
350 err->supp = NULL;
351 err->count = 1;
njn72718642003-07-24 08:45:32 +0000352 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000353 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000354 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000355 else
356 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000357
nethercote996901a2004-08-03 13:29:09 +0000358 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000359 err->ekind = ekind;
360 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000361 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000362 err->string = s;
363
njn25e49d8e72002-09-23 09:36:25 +0000364 /* sanity... */
njn72718642003-07-24 08:45:32 +0000365 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000366}
367
njn83f9e792005-06-11 05:04:09 +0000368#define ERRTXT_LEN 4096
369
njnf4261312005-03-20 23:45:36 +0000370static void printSuppForIp(UInt n, Addr ip)
371{
njn83f9e792005-06-11 05:04:09 +0000372 static UChar buf[ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000373
njn83f9e792005-06-11 05:04:09 +0000374 if ( VG_(get_fnname_nodemangle) (ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000375 VG_(printf)(" fun:%s\n", buf);
njn83f9e792005-06-11 05:04:09 +0000376 } else if ( VG_(get_objname)(ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000377 VG_(printf)(" obj:%s\n", buf);
378 } else {
379 VG_(printf)(" ???:??? "
380 "# unknown, suppression will not work, sorry\n");
381 }
382}
383
nethercote10d481a2004-01-25 20:33:53 +0000384static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000385{
njn43c799e2003-04-08 00:08:52 +0000386 ExeContext* ec = VG_(get_error_where)(err);
387 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000388
sewardjb5f6f512005-03-10 23:59:00 +0000389 /* At most VG_MAX_SUPP_CALLERS names */
390 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000391 vg_assert(stop_at > 0);
392
393 VG_(printf)("{\n");
394 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000395
sewardjb5f6f512005-03-10 23:59:00 +0000396 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000397 VG_(printf)(" core:PThread\n");
398
399 } else {
njn51d827b2005-05-09 01:02:08 +0000400 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000401 if (NULL == name) {
402 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000403 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000404 return;
405 }
406 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000407 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000408 }
njn43c799e2003-04-08 00:08:52 +0000409
njnf4261312005-03-20 23:45:36 +0000410 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000411 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000412
413 VG_(printf)("}\n");
414}
415
njnb4aee052003-04-15 14:09:58 +0000416static
nethercote04d0fbc2004-01-26 16:48:06 +0000417void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000418{
sewardjd153fae2005-01-10 17:24:47 +0000419 Bool still_noisy = True;
420
nethercote04d0fbc2004-01-26 16:48:06 +0000421 /* Perhaps we want a debugger attach at this point? */
422 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000423 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
424 {
nethercote04d0fbc2004-01-26 16:48:06 +0000425 VG_(printf)("starting debugger\n");
426 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000427 }
njn43c799e2003-04-08 00:08:52 +0000428 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000429 if (VG_(clo_gen_suppressions) == 2
430 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000431 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000432 ) {
nethercote42602b12004-01-25 19:30:29 +0000433 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000434 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
435 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000436 }
437}
438
439/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
440 just for pretty printing purposes. */
441static Bool is_first_shown_context = True;
442
njn25e49d8e72002-09-23 09:36:25 +0000443/* Top-level entry point to the error management subsystem.
444 All detected errors are notified here; this routine decides if/when the
445 user should see the error. */
njn72718642003-07-24 08:45:32 +0000446void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000447 ErrorKind ekind, Addr a, Char* s, void* extra )
448{
njn810086f2002-11-14 12:42:47 +0000449 Error err;
450 Error* p;
451 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000452 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000453 VgRes exe_res = Vg_MedRes;
454 static Bool stopping_message = False;
455 static Bool slowdown_message = False;
456 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000457
njn14319cc2005-03-13 06:26:22 +0000458 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
459 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000460 have been found, just refuse to collect any more. This stops
461 the burden of the error-management system becoming excessive in
462 extremely buggy programs, although it does make it pretty
463 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000464 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000465 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000466 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000467 if (!stopping_message) {
468 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000469
njn695c16e2005-03-27 03:40:28 +0000470 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000471 VG_(message)(Vg_UserMsg,
472 "More than %d different errors detected. "
473 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000474 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000475 } else {
476 VG_(message)(Vg_UserMsg,
477 "More than %d total errors detected. "
478 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000479 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000480 }
481
sewardjde4a1d02002-03-22 01:27:54 +0000482 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000483 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000484 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000485 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000486 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000487 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000488 VG_(message)(Vg_UserMsg,
489 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000490 VG_(message)(Vg_UserMsg, "");
491 stopping_message = True;
492 }
493 return;
494 }
495
njn14319cc2005-03-13 06:26:22 +0000496 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000497 been found, be much more conservative about collecting new
498 ones. */
njn695c16e2005-03-27 03:40:28 +0000499 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000500 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000501 if (!slowdown_message) {
502 VG_(message)(Vg_UserMsg, "");
503 VG_(message)(Vg_UserMsg,
504 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000505 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000506 VG_(message)(Vg_UserMsg,
507 "will still be recorded, but in less detail than before.");
508 slowdown_message = True;
509 }
510 }
511
njn25e49d8e72002-09-23 09:36:25 +0000512 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000513 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000514
515 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000516 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000517 p_prev = NULL;
518 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000519 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000520 /* Found it. */
521 p->count++;
522 if (p->supp != NULL) {
523 /* Deal correctly with suppressed errors. */
524 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000525 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000526 } else {
nethercotef2b11482004-08-02 12:36:01 +0000527 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000528 }
529
530 /* Move p to the front of the list so that future searches
531 for it are faster. */
532 if (p_prev != NULL) {
533 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000534 p_prev->next = p->next;
535 p->next = errors;
536 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000537 }
sewardj7ebf7c32003-07-24 21:29:40 +0000538
sewardjde4a1d02002-03-22 01:27:54 +0000539 return;
540 }
541 p_prev = p;
542 p = p->next;
543 }
544
545 /* Didn't see it. Copy and add. */
546
njn43c799e2003-04-08 00:08:52 +0000547 /* OK, we're really going to collect it. The context is on the stack and
548 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000549 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000550
njn02bc4b82005-05-15 17:28:26 +0000551 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000552 is for when there are more details to fill in which take time to work
553 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000554 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000555 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000556
njn02bc4b82005-05-15 17:28:26 +0000557 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000558 VG_(tdict).tool_update_extra returned. Also allow for people using
559 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000560 */
561
562 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000563 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000564 *p = err;
njn43c799e2003-04-08 00:08:52 +0000565
njn02bc4b82005-05-15 17:28:26 +0000566 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000567 switch (ekind) {
568 // case ThreadErr:
569 // case MutexErr:
570 // vg_assert(VG_(needs).core_errors);
571 // extra_size = VG_(tm_error_update_extra)(p);
572 // break;
573 default:
574 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000575 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000576 break;
577 }
njn43c799e2003-04-08 00:08:52 +0000578
njn02bc4b82005-05-15 17:28:26 +0000579 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000580 if (NULL != p->extra && 0 != extra_size) {
581 void* new_extra = VG_(malloc)(extra_size);
582 VG_(memcpy)(new_extra, p->extra, extra_size);
583 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000584 }
585
njn695c16e2005-03-27 03:40:28 +0000586 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000587 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000588 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000589 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000590 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000591 if (!is_first_shown_context)
592 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000593 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000594 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000595 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000596 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000597 } else {
nethercotef2b11482004-08-02 12:36:01 +0000598 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000599 p->supp->count++;
600 }
601}
602
njn43c799e2003-04-08 00:08:52 +0000603/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000604 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000605 guaranteed to only happen once. This avoids all the recording and
606 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000607 suppressed. Bool 'print_error' dictates whether to print the error.
608 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000609*/
njn72718642003-07-24 08:45:32 +0000610Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000611 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000612 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000613{
614 Error err;
615
616 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000617 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000618
619 /* Unless it's suppressed, we're going to show it. Don't need to make
620 a copy, because it's only temporary anyway.
621
njn02bc4b82005-05-15 17:28:26 +0000622 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000623 because that can have an affect on whether it's suppressed. Ignore
624 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000625 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000626 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000627
628 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000629 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000630 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000631
632 if (print_error) {
633 if (!is_first_shown_context)
634 VG_(message)(Vg_UserMsg, "");
635 pp_Error(&err, False);
636 is_first_shown_context = False;
637 }
nethercote04d0fbc2004-01-26 16:48:06 +0000638 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000639
640 return False;
641
642 } else {
nethercotef2b11482004-08-02 12:36:01 +0000643 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000644 return True;
645 }
646}
647
sewardjde4a1d02002-03-22 01:27:54 +0000648
sewardjde4a1d02002-03-22 01:27:54 +0000649/*------------------------------------------------------------*/
650/*--- Exported fns ---*/
651/*------------------------------------------------------------*/
652
sewardj71bc3cb2005-05-19 00:25:45 +0000653/* Show the used suppressions. Returns False if no suppression
654 got used. */
655static Bool show_used_suppressions ( void )
656{
657 Supp *su;
658 Bool any_supp;
659
sewardj7c9e57c2005-05-24 14:21:45 +0000660 if (VG_(clo_xml))
661 VG_(message)(Vg_DebugMsg, "<suppcounts>");
662
sewardj71bc3cb2005-05-19 00:25:45 +0000663 any_supp = False;
664 for (su = suppressions; su != NULL; su = su->next) {
665 if (su->count <= 0)
666 continue;
667 any_supp = True;
668 if (VG_(clo_xml)) {
669 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000670 " <pair>\n"
671 " <count>%d</count>\n"
672 " <name>%s</name>\n"
673 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000674 su->count, su->sname);
675 } else {
676 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
677 }
678 }
679
sewardj7c9e57c2005-05-24 14:21:45 +0000680 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000681 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000682
sewardj71bc3cb2005-05-19 00:25:45 +0000683 return any_supp;
684}
685
686
sewardj9f297ca2005-05-20 02:29:52 +0000687/* Show all the errors that occurred, and possibly also the
688 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000689void VG_(show_all_errors) ( void )
690{
njn810086f2002-11-14 12:42:47 +0000691 Int i, n_min;
692 Int n_err_contexts, n_supp_contexts;
693 Error *p, *p_min;
694 Supp *su;
695 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000696
697 if (VG_(clo_verbosity) == 0)
698 return;
699
700 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000701 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000702 if (p->supp == NULL)
703 n_err_contexts++;
704 }
705
706 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000707 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000708 if (su->count > 0)
709 n_supp_contexts++;
710 }
sewardj71bc3cb2005-05-19 00:25:45 +0000711
712 /* If we're printing XML, just show the suppressions and stop.
713 */
714 if (VG_(clo_xml)) {
715 (void)show_used_suppressions();
716 return;
717 }
718
719 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000720 VG_(message)(Vg_UserMsg,
721 "ERROR SUMMARY: "
722 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000723 n_errs_found, n_err_contexts,
724 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000725
726 if (VG_(clo_verbosity) <= 1)
727 return;
728
729 /* Print the contexts in order of increasing error count. */
730 for (i = 0; i < n_err_contexts; i++) {
731 n_min = (1 << 30) - 1;
732 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000733 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000734 if (p->supp != NULL) continue;
735 if (p->count < n_min) {
736 n_min = p->count;
737 p_min = p;
738 }
739 }
njn67993252004-11-22 18:02:32 +0000740 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000741
742 VG_(message)(Vg_UserMsg, "");
743 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
744 p_min->count,
745 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000746 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000747
748 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000749 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000750 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000751 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000752 }
753
754 p_min->count = 1 << 30;
755 }
756
757 if (n_supp_contexts > 0)
758 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000759 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000760
761 if (n_err_contexts > 0) {
762 if (any_supp)
763 VG_(message)(Vg_UserMsg, "");
764 VG_(message)(Vg_UserMsg,
765 "IN SUMMARY: "
766 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000767 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000768 n_supp_contexts );
769 VG_(message)(Vg_UserMsg, "");
770 }
771}
772
sewardj9f297ca2005-05-20 02:29:52 +0000773
774/* Show occurrence counts of all errors, in XML form. */
775void VG_(show_error_counts_as_XML) ( void )
776{
777 Error* err;
778 VG_(message)(Vg_UserMsg, "<errorcounts>");
779 for (err = errors; err != NULL; err = err->next) {
780 if (err->supp != NULL)
781 continue;
782 if (err->count <= 0)
783 continue;
784 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000785 Vg_UserMsg, " <pair> <count>%d</count> "
786 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000787 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000788 );
789 }
790 VG_(message)(Vg_UserMsg, "</errorcounts>");
791}
792
793
sewardjde4a1d02002-03-22 01:27:54 +0000794/*------------------------------------------------------------*/
795/*--- Standard suppressions ---*/
796/*------------------------------------------------------------*/
797
798/* Get a non-blank, non-comment line of at most nBuf chars from fd.
799 Skips leading spaces on the line. Return True if EOF was hit instead.
800*/
njn4ba5a792002-09-30 10:23:54 +0000801Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000802{
803 Char ch;
804 Int n, i;
805 while (True) {
806 /* First, read until a non-blank char appears. */
807 while (True) {
808 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000809 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000810 if (n == 0) return True;
811 }
812
813 /* Now, read the line into buf. */
814 i = 0;
815 buf[i++] = ch; buf[i] = 0;
816 while (True) {
817 n = VG_(read)(fd, &ch, 1);
818 if (n == 0) return False; /* the next call will return True */
819 if (ch == '\n') break;
820 if (i > 0 && i == nBuf-1) i--;
821 buf[i++] = ch; buf[i] = 0;
822 }
njn0c0f32a2005-03-26 04:14:01 +0000823 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000824 i--; buf[i] = 0;
825 };
826
njn02bc4b82005-05-15 17:28:26 +0000827 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000828 /* Ok, we have a line. If a non-comment line, return.
829 If a comment line, start all over again. */
830 if (buf[0] != '#') return False;
831 }
832}
833
834
835/* *p_caller contains the raw name of a caller, supposedly either
836 fun:some_function_name or
837 obj:some_object_name.
838 Set *p_ty accordingly and advance *p_caller over the descriptor
839 (fun: or obj:) part.
840 Returns False if failed.
841*/
sewardjb5f6f512005-03-10 23:59:00 +0000842static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000843{
sewardjb5f6f512005-03-10 23:59:00 +0000844 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
845 p->name += 4;
846 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000847 return True;
848 }
sewardjb5f6f512005-03-10 23:59:00 +0000849 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
850 p->name += 4;
851 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000852 return True;
853 }
854 VG_(printf)("location should start with fun: or obj:\n");
855 return False;
856}
857
858
nethercote7cc9c232004-01-21 15:08:04 +0000859/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000860static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000861Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000862{
863 Bool found;
864 Char *s = NULL; /* Shut gcc up */
865 Int len = VG_(strlen)(name);
866
867 found = (NULL != (s = VG_(strstr)(names, name)) &&
868 (s == names || *(s-1) == ',') &&
869 (*(s+len) == ',' || *(s+len) == '\0')
870 );
871
872 return found;
873}
874
njn695c16e2005-03-27 03:40:28 +0000875/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000876 and place them in the suppressions list. If there's any difficulty
877 doing this, just give up -- there's no point in trying to recover.
878*/
sewardjde4a1d02002-03-22 01:27:54 +0000879static void load_one_suppressions_file ( Char* filename )
880{
881# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000882 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000883 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000884 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000885 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000886 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000887 Char* err_str = NULL;
888 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000889
njn25e49d8e72002-09-23 09:36:25 +0000890 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000891 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000892 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000893 filename );
894 VG_(exit)(1);
895 }
896
sewardjb5f6f512005-03-10 23:59:00 +0000897#define BOMB(S) { err_str = S; goto syntax_error; }
898
sewardjde4a1d02002-03-22 01:27:54 +0000899 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000900 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000901 Supp* supp;
902 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000903 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000904
905 // Initialise temporary reading-in buffer.
906 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
907 tmp_callers[i].ty = NoName;
908 tmp_callers[i].name = NULL;
909 }
910
njn810086f2002-11-14 12:42:47 +0000911 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000912
njn4ba5a792002-09-30 10:23:54 +0000913 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000914 if (eof) break;
915
sewardjb5f6f512005-03-10 23:59:00 +0000916 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000917
njn4ba5a792002-09-30 10:23:54 +0000918 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000919
920 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
921
njn25e49d8e72002-09-23 09:36:25 +0000922 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000923
njn4ba5a792002-09-30 10:23:54 +0000924 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000925
sewardjb5f6f512005-03-10 23:59:00 +0000926 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000927
njn94065fd2004-11-22 19:26:27 +0000928 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000929 i = 0;
930 while (True) {
931 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000932 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000933 i++;
njn25e49d8e72002-09-23 09:36:25 +0000934 }
njnc40c3a82002-10-02 11:02:27 +0000935 buf[i] = '\0'; /* Replace ':', splitting into two strings */
936
nethercote7cc9c232004-01-21 15:08:04 +0000937 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000938 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000939
nethercote7cc9c232004-01-21 15:08:04 +0000940 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000941 {
sewardjb5f6f512005-03-10 23:59:00 +0000942 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000943 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000944 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000945 else
sewardjb5f6f512005-03-10 23:59:00 +0000946 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000947 }
njn95ec8702004-11-22 16:46:13 +0000948 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000949 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000950 {
sewardjb5f6f512005-03-10 23:59:00 +0000951 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000952 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000953 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000954 } else {
955 BOMB("unknown tool suppression type");
956 }
njnc40c3a82002-10-02 11:02:27 +0000957 }
njn25e49d8e72002-09-23 09:36:25 +0000958 else {
sewardjb5f6f512005-03-10 23:59:00 +0000959 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000960 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000961 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000962 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000963 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000964 break;
965 }
966 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000967 }
968
njn95ec8702004-11-22 16:46:13 +0000969 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000970 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000971 {
972 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000973 }
974
sewardjb5f6f512005-03-10 23:59:00 +0000975 i = 0;
976 while (True) {
977 eof = VG_(get_line) ( fd, buf, N_BUF );
978 if (eof)
979 BOMB("unexpected end-of-file");
980 if (VG_STREQ(buf, "}")) {
981 if (i > 0) {
982 break;
983 } else {
984 BOMB("missing stack trace");
985 }
986 }
987 if (i == VG_MAX_SUPP_CALLERS)
988 BOMB("too many callers in stack trace");
989 if (i > 0 && i >= VG_(clo_backtrace_size))
990 break;
991 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
992 if (!setLocationTy(&(tmp_callers[i])))
993 BOMB("location should start with 'fun:' or 'obj:'");
994 i++;
995 }
996
997 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
998 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000999 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +00001000 do {
1001 eof = VG_(get_line) ( fd, buf, N_BUF );
1002 } while (!eof && !VG_STREQ(buf, "}"));
1003 }
1004
1005 // Copy tmp_callers[] into supp->callers[]
1006 supp->n_callers = i;
1007 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1008 for (i = 0; i < supp->n_callers; i++) {
1009 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001010 }
1011
njn695c16e2005-03-27 03:40:28 +00001012 supp->next = suppressions;
1013 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001014 }
sewardjde4a1d02002-03-22 01:27:54 +00001015 VG_(close)(fd);
1016 return;
1017
1018 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001019 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001020 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001021
sewardjde4a1d02002-03-22 01:27:54 +00001022 VG_(close)(fd);
1023 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001024 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001025
sewardjb5f6f512005-03-10 23:59:00 +00001026# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001027# undef N_BUF
1028}
1029
1030
1031void VG_(load_suppressions) ( void )
1032{
1033 Int i;
njn695c16e2005-03-27 03:40:28 +00001034 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001035 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1036 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001037 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1038 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001039 }
1040 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1041 }
1042}
1043
sewardjb5f6f512005-03-10 23:59:00 +00001044static
njn810086f2002-11-14 12:42:47 +00001045Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001046{
njn810086f2002-11-14 12:42:47 +00001047 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001048 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001049 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001050 default:
njn95ec8702004-11-22 16:46:13 +00001051 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001052 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001053 } else {
1054 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001055 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001056 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001057 err->ekind);
njn67993252004-11-22 18:02:32 +00001058 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001059 }
1060 }
1061}
1062
sewardjb5f6f512005-03-10 23:59:00 +00001063static
1064Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001065{
1066 Int i;
njn83f9e792005-06-11 05:04:09 +00001067 Char caller_name[ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001068 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001069
sewardjb5f6f512005-03-10 23:59:00 +00001070 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001071 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001072 vg_assert(su->callers[i].name != NULL);
1073 switch (su->callers[i].ty) {
1074 case ObjName:
njn83f9e792005-06-11 05:04:09 +00001075 if (!VG_(get_objname)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001076 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001077 break;
1078
1079 case FunName:
1080 // Nb: mangled names used in suppressions
njn83f9e792005-06-11 05:04:09 +00001081 if (!VG_(get_fnname_nodemangle)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001082 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001083 break;
njn67993252004-11-22 18:02:32 +00001084 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001085 }
sewardjb5f6f512005-03-10 23:59:00 +00001086 if (!VG_(string_match)(su->callers[i].name, caller_name))
1087 return False;
njn25e49d8e72002-09-23 09:36:25 +00001088 }
1089
1090 /* If we reach here, it's a match */
1091 return True;
1092}
sewardjde4a1d02002-03-22 01:27:54 +00001093
njn810086f2002-11-14 12:42:47 +00001094/* Does an error context match a suppression? ie is this a suppressible
1095 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001096 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001097*/
njn810086f2002-11-14 12:42:47 +00001098static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001099{
njn810086f2002-11-14 12:42:47 +00001100 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001101
sewardjde4a1d02002-03-22 01:27:54 +00001102 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001103 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001104 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001105 supp_matches_callers(err, su))
1106 {
njn25e49d8e72002-09-23 09:36:25 +00001107 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001108 }
sewardjde4a1d02002-03-22 01:27:54 +00001109 }
njn25e49d8e72002-09-23 09:36:25 +00001110 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001111}
1112
sewardjde4a1d02002-03-22 01:27:54 +00001113/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001114/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001115/*--------------------------------------------------------------------*/