blob: a27c4b0e5d4af37fee0f35bcd6185cd2a80d6e33 [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
njnf4261312005-03-20 23:45:36 +0000368static void printSuppForIp(UInt n, Addr ip)
369{
njn47b209a2005-03-25 23:47:16 +0000370 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000371
njn47b209a2005-03-25 23:47:16 +0000372 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000373 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000374 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000375 VG_(printf)(" obj:%s\n", buf);
376 } else {
377 VG_(printf)(" ???:??? "
378 "# unknown, suppression will not work, sorry\n");
379 }
380}
381
nethercote10d481a2004-01-25 20:33:53 +0000382static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000383{
njn43c799e2003-04-08 00:08:52 +0000384 ExeContext* ec = VG_(get_error_where)(err);
385 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000386
sewardjb5f6f512005-03-10 23:59:00 +0000387 /* At most VG_MAX_SUPP_CALLERS names */
388 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000389 vg_assert(stop_at > 0);
390
391 VG_(printf)("{\n");
392 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000393
sewardjb5f6f512005-03-10 23:59:00 +0000394 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000395 VG_(printf)(" core:PThread\n");
396
397 } else {
njn51d827b2005-05-09 01:02:08 +0000398 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000399 if (NULL == name) {
400 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000401 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000402 return;
403 }
404 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000405 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000406 }
njn43c799e2003-04-08 00:08:52 +0000407
njnf4261312005-03-20 23:45:36 +0000408 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000409 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000410
411 VG_(printf)("}\n");
412}
413
njnb4aee052003-04-15 14:09:58 +0000414static
nethercote04d0fbc2004-01-26 16:48:06 +0000415void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000416{
sewardjd153fae2005-01-10 17:24:47 +0000417 Bool still_noisy = True;
418
nethercote04d0fbc2004-01-26 16:48:06 +0000419 /* Perhaps we want a debugger attach at this point? */
420 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000421 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
422 {
nethercote04d0fbc2004-01-26 16:48:06 +0000423 VG_(printf)("starting debugger\n");
424 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000425 }
njn43c799e2003-04-08 00:08:52 +0000426 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000427 if (VG_(clo_gen_suppressions) == 2
428 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000429 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000430 ) {
nethercote42602b12004-01-25 19:30:29 +0000431 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000432 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
433 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000434 }
435}
436
437/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
438 just for pretty printing purposes. */
439static Bool is_first_shown_context = True;
440
njn25e49d8e72002-09-23 09:36:25 +0000441/* Top-level entry point to the error management subsystem.
442 All detected errors are notified here; this routine decides if/when the
443 user should see the error. */
njn72718642003-07-24 08:45:32 +0000444void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000445 ErrorKind ekind, Addr a, Char* s, void* extra )
446{
njn810086f2002-11-14 12:42:47 +0000447 Error err;
448 Error* p;
449 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000450 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000451 VgRes exe_res = Vg_MedRes;
452 static Bool stopping_message = False;
453 static Bool slowdown_message = False;
454 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000455
njn14319cc2005-03-13 06:26:22 +0000456 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
457 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000458 have been found, just refuse to collect any more. This stops
459 the burden of the error-management system becoming excessive in
460 extremely buggy programs, although it does make it pretty
461 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000462 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000463 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000464 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000465 if (!stopping_message) {
466 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000467
njn695c16e2005-03-27 03:40:28 +0000468 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000469 VG_(message)(Vg_UserMsg,
470 "More than %d different errors detected. "
471 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000472 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000473 } else {
474 VG_(message)(Vg_UserMsg,
475 "More than %d total errors detected. "
476 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000477 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000478 }
479
sewardjde4a1d02002-03-22 01:27:54 +0000480 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000481 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000482 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000483 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000484 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000485 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000486 VG_(message)(Vg_UserMsg,
487 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000488 VG_(message)(Vg_UserMsg, "");
489 stopping_message = True;
490 }
491 return;
492 }
493
njn14319cc2005-03-13 06:26:22 +0000494 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000495 been found, be much more conservative about collecting new
496 ones. */
njn695c16e2005-03-27 03:40:28 +0000497 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000498 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000499 if (!slowdown_message) {
500 VG_(message)(Vg_UserMsg, "");
501 VG_(message)(Vg_UserMsg,
502 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000503 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000504 VG_(message)(Vg_UserMsg,
505 "will still be recorded, but in less detail than before.");
506 slowdown_message = True;
507 }
508 }
509
njn25e49d8e72002-09-23 09:36:25 +0000510 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000511 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000512
513 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000514 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000515 p_prev = NULL;
516 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000517 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000518 /* Found it. */
519 p->count++;
520 if (p->supp != NULL) {
521 /* Deal correctly with suppressed errors. */
522 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000523 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000524 } else {
nethercotef2b11482004-08-02 12:36:01 +0000525 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000526 }
527
528 /* Move p to the front of the list so that future searches
529 for it are faster. */
530 if (p_prev != NULL) {
531 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000532 p_prev->next = p->next;
533 p->next = errors;
534 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000535 }
sewardj7ebf7c32003-07-24 21:29:40 +0000536
sewardjde4a1d02002-03-22 01:27:54 +0000537 return;
538 }
539 p_prev = p;
540 p = p->next;
541 }
542
543 /* Didn't see it. Copy and add. */
544
njn43c799e2003-04-08 00:08:52 +0000545 /* OK, we're really going to collect it. The context is on the stack and
546 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000547 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000548
njn02bc4b82005-05-15 17:28:26 +0000549 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000550 is for when there are more details to fill in which take time to work
551 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000552 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000553 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000554
njn02bc4b82005-05-15 17:28:26 +0000555 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000556 VG_(tdict).tool_update_extra returned. Also allow for people using
557 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000558 */
559
560 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000561 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000562 *p = err;
njn43c799e2003-04-08 00:08:52 +0000563
njn02bc4b82005-05-15 17:28:26 +0000564 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000565 switch (ekind) {
566 // case ThreadErr:
567 // case MutexErr:
568 // vg_assert(VG_(needs).core_errors);
569 // extra_size = VG_(tm_error_update_extra)(p);
570 // break;
571 default:
572 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000573 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000574 break;
575 }
njn43c799e2003-04-08 00:08:52 +0000576
njn02bc4b82005-05-15 17:28:26 +0000577 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000578 if (NULL != p->extra && 0 != extra_size) {
579 void* new_extra = VG_(malloc)(extra_size);
580 VG_(memcpy)(new_extra, p->extra, extra_size);
581 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000582 }
583
njn695c16e2005-03-27 03:40:28 +0000584 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000585 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000586 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000587 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000588 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000589 if (!is_first_shown_context)
590 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000591 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000592 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000593 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000594 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000595 } else {
nethercotef2b11482004-08-02 12:36:01 +0000596 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000597 p->supp->count++;
598 }
599}
600
njn43c799e2003-04-08 00:08:52 +0000601/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000602 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000603 guaranteed to only happen once. This avoids all the recording and
604 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000605 suppressed. Bool 'print_error' dictates whether to print the error.
606 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000607*/
njn72718642003-07-24 08:45:32 +0000608Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000609 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000610 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000611{
612 Error err;
613
614 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000615 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000616
617 /* Unless it's suppressed, we're going to show it. Don't need to make
618 a copy, because it's only temporary anyway.
619
njn02bc4b82005-05-15 17:28:26 +0000620 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000621 because that can have an affect on whether it's suppressed. Ignore
622 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000623 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000624 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000625
626 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000627 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000628 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000629
630 if (print_error) {
631 if (!is_first_shown_context)
632 VG_(message)(Vg_UserMsg, "");
633 pp_Error(&err, False);
634 is_first_shown_context = False;
635 }
nethercote04d0fbc2004-01-26 16:48:06 +0000636 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000637
638 return False;
639
640 } else {
nethercotef2b11482004-08-02 12:36:01 +0000641 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000642 return True;
643 }
644}
645
sewardjde4a1d02002-03-22 01:27:54 +0000646
sewardjde4a1d02002-03-22 01:27:54 +0000647/*------------------------------------------------------------*/
648/*--- Exported fns ---*/
649/*------------------------------------------------------------*/
650
sewardj71bc3cb2005-05-19 00:25:45 +0000651/* Show the used suppressions. Returns False if no suppression
652 got used. */
653static Bool show_used_suppressions ( void )
654{
655 Supp *su;
656 Bool any_supp;
657
sewardj7c9e57c2005-05-24 14:21:45 +0000658 if (VG_(clo_xml))
659 VG_(message)(Vg_DebugMsg, "<suppcounts>");
660
sewardj71bc3cb2005-05-19 00:25:45 +0000661 any_supp = False;
662 for (su = suppressions; su != NULL; su = su->next) {
663 if (su->count <= 0)
664 continue;
665 any_supp = True;
666 if (VG_(clo_xml)) {
667 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000668 " <pair>\n"
669 " <count>%d</count>\n"
670 " <name>%s</name>\n"
671 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000672 su->count, su->sname);
673 } else {
674 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
675 }
676 }
677
sewardj7c9e57c2005-05-24 14:21:45 +0000678 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000679 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000680
sewardj71bc3cb2005-05-19 00:25:45 +0000681 return any_supp;
682}
683
684
sewardj9f297ca2005-05-20 02:29:52 +0000685/* Show all the errors that occurred, and possibly also the
686 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000687void VG_(show_all_errors) ( void )
688{
njn810086f2002-11-14 12:42:47 +0000689 Int i, n_min;
690 Int n_err_contexts, n_supp_contexts;
691 Error *p, *p_min;
692 Supp *su;
693 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000694
695 if (VG_(clo_verbosity) == 0)
696 return;
697
698 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000699 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000700 if (p->supp == NULL)
701 n_err_contexts++;
702 }
703
704 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000705 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000706 if (su->count > 0)
707 n_supp_contexts++;
708 }
sewardj71bc3cb2005-05-19 00:25:45 +0000709
710 /* If we're printing XML, just show the suppressions and stop.
711 */
712 if (VG_(clo_xml)) {
713 (void)show_used_suppressions();
714 return;
715 }
716
717 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000718 VG_(message)(Vg_UserMsg,
719 "ERROR SUMMARY: "
720 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000721 n_errs_found, n_err_contexts,
722 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000723
724 if (VG_(clo_verbosity) <= 1)
725 return;
726
727 /* Print the contexts in order of increasing error count. */
728 for (i = 0; i < n_err_contexts; i++) {
729 n_min = (1 << 30) - 1;
730 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000731 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000732 if (p->supp != NULL) continue;
733 if (p->count < n_min) {
734 n_min = p->count;
735 p_min = p;
736 }
737 }
njn67993252004-11-22 18:02:32 +0000738 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000739
740 VG_(message)(Vg_UserMsg, "");
741 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
742 p_min->count,
743 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000744 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000745
746 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000747 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000748 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000749 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000750 }
751
752 p_min->count = 1 << 30;
753 }
754
755 if (n_supp_contexts > 0)
756 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000757 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000758
759 if (n_err_contexts > 0) {
760 if (any_supp)
761 VG_(message)(Vg_UserMsg, "");
762 VG_(message)(Vg_UserMsg,
763 "IN SUMMARY: "
764 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000765 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000766 n_supp_contexts );
767 VG_(message)(Vg_UserMsg, "");
768 }
769}
770
sewardj9f297ca2005-05-20 02:29:52 +0000771
772/* Show occurrence counts of all errors, in XML form. */
773void VG_(show_error_counts_as_XML) ( void )
774{
775 Error* err;
776 VG_(message)(Vg_UserMsg, "<errorcounts>");
777 for (err = errors; err != NULL; err = err->next) {
778 if (err->supp != NULL)
779 continue;
780 if (err->count <= 0)
781 continue;
782 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000783 Vg_UserMsg, " <pair> <count>%d</count> "
784 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000785 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000786 );
787 }
788 VG_(message)(Vg_UserMsg, "</errorcounts>");
789}
790
791
sewardjde4a1d02002-03-22 01:27:54 +0000792/*------------------------------------------------------------*/
793/*--- Standard suppressions ---*/
794/*------------------------------------------------------------*/
795
796/* Get a non-blank, non-comment line of at most nBuf chars from fd.
797 Skips leading spaces on the line. Return True if EOF was hit instead.
798*/
njn4ba5a792002-09-30 10:23:54 +0000799Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000800{
801 Char ch;
802 Int n, i;
803 while (True) {
804 /* First, read until a non-blank char appears. */
805 while (True) {
806 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000807 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000808 if (n == 0) return True;
809 }
810
811 /* Now, read the line into buf. */
812 i = 0;
813 buf[i++] = ch; buf[i] = 0;
814 while (True) {
815 n = VG_(read)(fd, &ch, 1);
816 if (n == 0) return False; /* the next call will return True */
817 if (ch == '\n') break;
818 if (i > 0 && i == nBuf-1) i--;
819 buf[i++] = ch; buf[i] = 0;
820 }
njn0c0f32a2005-03-26 04:14:01 +0000821 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000822 i--; buf[i] = 0;
823 };
824
njn02bc4b82005-05-15 17:28:26 +0000825 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000826 /* Ok, we have a line. If a non-comment line, return.
827 If a comment line, start all over again. */
828 if (buf[0] != '#') return False;
829 }
830}
831
832
833/* *p_caller contains the raw name of a caller, supposedly either
834 fun:some_function_name or
835 obj:some_object_name.
836 Set *p_ty accordingly and advance *p_caller over the descriptor
837 (fun: or obj:) part.
838 Returns False if failed.
839*/
sewardjb5f6f512005-03-10 23:59:00 +0000840static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000841{
sewardjb5f6f512005-03-10 23:59:00 +0000842 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
843 p->name += 4;
844 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000845 return True;
846 }
sewardjb5f6f512005-03-10 23:59:00 +0000847 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
848 p->name += 4;
849 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000850 return True;
851 }
852 VG_(printf)("location should start with fun: or obj:\n");
853 return False;
854}
855
856
nethercote7cc9c232004-01-21 15:08:04 +0000857/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000858static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000859Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000860{
861 Bool found;
862 Char *s = NULL; /* Shut gcc up */
863 Int len = VG_(strlen)(name);
864
865 found = (NULL != (s = VG_(strstr)(names, name)) &&
866 (s == names || *(s-1) == ',') &&
867 (*(s+len) == ',' || *(s+len) == '\0')
868 );
869
870 return found;
871}
872
njn695c16e2005-03-27 03:40:28 +0000873/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000874 and place them in the suppressions list. If there's any difficulty
875 doing this, just give up -- there's no point in trying to recover.
876*/
sewardjde4a1d02002-03-22 01:27:54 +0000877static void load_one_suppressions_file ( Char* filename )
878{
879# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000880 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000881 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000882 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000883 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000884 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000885 Char* err_str = NULL;
886 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000887
njn25e49d8e72002-09-23 09:36:25 +0000888 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000889 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000890 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000891 filename );
892 VG_(exit)(1);
893 }
894
sewardjb5f6f512005-03-10 23:59:00 +0000895#define BOMB(S) { err_str = S; goto syntax_error; }
896
sewardjde4a1d02002-03-22 01:27:54 +0000897 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000898 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000899 Supp* supp;
900 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000901 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000902
903 // Initialise temporary reading-in buffer.
904 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
905 tmp_callers[i].ty = NoName;
906 tmp_callers[i].name = NULL;
907 }
908
njn810086f2002-11-14 12:42:47 +0000909 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000910
njn4ba5a792002-09-30 10:23:54 +0000911 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000912 if (eof) break;
913
sewardjb5f6f512005-03-10 23:59:00 +0000914 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000915
njn4ba5a792002-09-30 10:23:54 +0000916 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000917
918 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
919
njn25e49d8e72002-09-23 09:36:25 +0000920 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000921
njn4ba5a792002-09-30 10:23:54 +0000922 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000923
sewardjb5f6f512005-03-10 23:59:00 +0000924 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000925
njn94065fd2004-11-22 19:26:27 +0000926 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000927 i = 0;
928 while (True) {
929 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000930 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000931 i++;
njn25e49d8e72002-09-23 09:36:25 +0000932 }
njnc40c3a82002-10-02 11:02:27 +0000933 buf[i] = '\0'; /* Replace ':', splitting into two strings */
934
nethercote7cc9c232004-01-21 15:08:04 +0000935 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000936 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000937
nethercote7cc9c232004-01-21 15:08:04 +0000938 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000939 {
sewardjb5f6f512005-03-10 23:59:00 +0000940 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000941 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000942 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000943 else
sewardjb5f6f512005-03-10 23:59:00 +0000944 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000945 }
njn95ec8702004-11-22 16:46:13 +0000946 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000947 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000948 {
sewardjb5f6f512005-03-10 23:59:00 +0000949 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000950 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000951 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000952 } else {
953 BOMB("unknown tool suppression type");
954 }
njnc40c3a82002-10-02 11:02:27 +0000955 }
njn25e49d8e72002-09-23 09:36:25 +0000956 else {
sewardjb5f6f512005-03-10 23:59:00 +0000957 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000958 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000959 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000960 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000961 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000962 break;
963 }
964 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000965 }
966
njn95ec8702004-11-22 16:46:13 +0000967 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000968 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000969 {
970 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000971 }
972
sewardjb5f6f512005-03-10 23:59:00 +0000973 i = 0;
974 while (True) {
975 eof = VG_(get_line) ( fd, buf, N_BUF );
976 if (eof)
977 BOMB("unexpected end-of-file");
978 if (VG_STREQ(buf, "}")) {
979 if (i > 0) {
980 break;
981 } else {
982 BOMB("missing stack trace");
983 }
984 }
985 if (i == VG_MAX_SUPP_CALLERS)
986 BOMB("too many callers in stack trace");
987 if (i > 0 && i >= VG_(clo_backtrace_size))
988 break;
989 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
990 if (!setLocationTy(&(tmp_callers[i])))
991 BOMB("location should start with 'fun:' or 'obj:'");
992 i++;
993 }
994
995 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
996 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000997 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000998 do {
999 eof = VG_(get_line) ( fd, buf, N_BUF );
1000 } while (!eof && !VG_STREQ(buf, "}"));
1001 }
1002
1003 // Copy tmp_callers[] into supp->callers[]
1004 supp->n_callers = i;
1005 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1006 for (i = 0; i < supp->n_callers; i++) {
1007 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001008 }
1009
njn695c16e2005-03-27 03:40:28 +00001010 supp->next = suppressions;
1011 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001012 }
sewardjde4a1d02002-03-22 01:27:54 +00001013 VG_(close)(fd);
1014 return;
1015
1016 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001017 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001018 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001019
sewardjde4a1d02002-03-22 01:27:54 +00001020 VG_(close)(fd);
1021 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001022 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001023
sewardjb5f6f512005-03-10 23:59:00 +00001024# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001025# undef N_BUF
1026}
1027
1028
1029void VG_(load_suppressions) ( void )
1030{
1031 Int i;
njn695c16e2005-03-27 03:40:28 +00001032 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001033 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1034 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001035 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1036 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001037 }
1038 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1039 }
1040}
1041
sewardjb5f6f512005-03-10 23:59:00 +00001042static
njn810086f2002-11-14 12:42:47 +00001043Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001044{
njn810086f2002-11-14 12:42:47 +00001045 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001046 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001047 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001048 default:
njn95ec8702004-11-22 16:46:13 +00001049 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001050 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001051 } else {
1052 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001053 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001054 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001055 err->ekind);
njn67993252004-11-22 18:02:32 +00001056 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001057 }
1058 }
1059}
1060
sewardjb5f6f512005-03-10 23:59:00 +00001061static
1062Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001063{
1064 Int i;
njn47b209a2005-03-25 23:47:16 +00001065 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001066 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001067
sewardjb5f6f512005-03-10 23:59:00 +00001068 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001069 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001070 vg_assert(su->callers[i].name != NULL);
1071 switch (su->callers[i].ty) {
1072 case ObjName:
njn47b209a2005-03-25 23:47:16 +00001073 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001074 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001075 break;
1076
1077 case FunName:
1078 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001079 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001080 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001081 break;
njn67993252004-11-22 18:02:32 +00001082 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001083 }
sewardjb5f6f512005-03-10 23:59:00 +00001084 if (!VG_(string_match)(su->callers[i].name, caller_name))
1085 return False;
njn25e49d8e72002-09-23 09:36:25 +00001086 }
1087
1088 /* If we reach here, it's a match */
1089 return True;
1090}
sewardjde4a1d02002-03-22 01:27:54 +00001091
njn810086f2002-11-14 12:42:47 +00001092/* Does an error context match a suppression? ie is this a suppressible
1093 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001094 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001095*/
njn810086f2002-11-14 12:42:47 +00001096static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001097{
njn810086f2002-11-14 12:42:47 +00001098 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001099
sewardjde4a1d02002-03-22 01:27:54 +00001100 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001101 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001102 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001103 supp_matches_callers(err, su))
1104 {
njn25e49d8e72002-09-23 09:36:25 +00001105 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001106 }
sewardjde4a1d02002-03-22 01:27:54 +00001107 }
njn25e49d8e72002-09-23 09:36:25 +00001108 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001109}
1110
sewardjde4a1d02002-03-22 01:27:54 +00001111/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001112/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001113/*--------------------------------------------------------------------*/