blob: cb6d7313c1fb387d23665f5e1238748df073adb4 [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"
njnd2b17112005-04-19 04:10:25 +000032#include "pub_core_errormgr.h"
njnd01fef72005-03-25 23:35:48 +000033#include "pub_core_execontext.h"
njn20242342005-05-16 23:31:24 +000034#include "pub_core_options.h"
njnd2b17112005-04-19 04:10:25 +000035#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000036#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000037#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000038
39/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000040/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000041/*------------------------------------------------------------*/
42
njn14319cc2005-03-13 06:26:22 +000043/* After this many different unsuppressed errors have been observed,
44 be more conservative about collecting new ones. */
45#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
46
47/* After this many different unsuppressed errors have been observed,
48 stop collecting errors at all, and tell the user their program is
49 evidently a steaming pile of camel dung. */
50#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
51
52/* After this many total errors have been observed, stop collecting
53 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
54#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
55
sewardjde4a1d02002-03-22 01:27:54 +000056/* The list of error contexts found, both suppressed and unsuppressed.
57 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000058static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000059
60/* The list of suppression directives, as read from the specified
61 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000062static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000063
64/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000065static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000066
67/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000068static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000069
70/* forwards ... */
njn810086f2002-11-14 12:42:47 +000071static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000072
sewardjb5f6f512005-03-10 23:59:00 +000073static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000074
75/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000076/*--- Error type ---*/
77/*------------------------------------------------------------*/
78
nethercote996901a2004-08-03 13:29:09 +000079/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000080 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000081
82/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000083 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000084 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000085 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000086
87 When errors are found and recorded with VG_(maybe_record_error)(), all
88 the tool must do is pass in the four parameters; core will
89 allocate/initialise the error record.
90*/
91struct _Error {
92 struct _Error* next;
93 // NULL if unsuppressed; or ptr to suppression record.
94 Supp* supp;
95 Int count;
96 ThreadId tid;
97
98 // The tool-specific part
99 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000100 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000101 Addr addr; // Used frequently
102 Char* string; // Used frequently
103 void* extra; // For any tool-specific extras
104};
105
106ExeContext* VG_(get_error_where) ( Error* err )
107{
108 return err->where;
109}
110
111ErrorKind VG_(get_error_kind) ( Error* err )
112{
113 return err->ekind;
114}
115
116Addr VG_(get_error_address) ( Error* err )
117{
118 return err->addr;
119}
120
121Char* VG_(get_error_string) ( Error* err )
122{
123 return err->string;
124}
125
126void* VG_(get_error_extra) ( Error* err )
127{
128 return err->extra;
129}
130
nethercotef2b11482004-08-02 12:36:01 +0000131UInt VG_(get_n_errs_found)( void )
132{
133 return n_errs_found;
134}
135
nethercote4a184902004-08-02 12:21:09 +0000136/*------------------------------------------------------------*/
137/*--- Suppression type ---*/
138/*------------------------------------------------------------*/
139
140/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
141 * effectively extend it by defining their own enums in the (0..) range. */
142typedef
143 enum {
144 PThreadSupp = -1, /* Matches PThreadErr */
145 }
146 CoreSuppKind;
147
sewardjb5f6f512005-03-10 23:59:00 +0000148/* Max number of callers for context in a suppression. */
149#define VG_MAX_SUPP_CALLERS 24
150
nethercote4a184902004-08-02 12:21:09 +0000151/* For each caller specified for a suppression, record the nature of
152 the caller name. Not of interest to tools. */
153typedef
154 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000155 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000156 ObjName, /* Name is of an shared object file. */
157 FunName /* Name is of a function. */
158 }
159 SuppLocTy;
160
sewardjb5f6f512005-03-10 23:59:00 +0000161typedef
162 struct {
163 SuppLocTy ty;
164 Char* name;
165 }
166 SuppLoc;
167
nethercote4a184902004-08-02 12:21:09 +0000168/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000169 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000170 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000171 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000172struct _Supp {
173 struct _Supp* next;
174 Int count; // The number of times this error has been suppressed.
175 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000176
177 // Length of 'callers'
178 Int n_callers;
179 // Array of callers, for matching stack traces. First one (name of fn
180 // where err occurs) is mandatory; rest are optional.
181 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000182
183 /* The tool-specific part */
184 SuppKind skind; // What kind of suppression. Must use the range (0..).
185 Char* string; // String -- use is optional. NULL by default.
186 void* extra; // Anything else -- use is optional. NULL by default.
187};
188
189SuppKind VG_(get_supp_kind) ( Supp* su )
190{
191 return su->skind;
192}
193
194Char* VG_(get_supp_string) ( Supp* su )
195{
196 return su->string;
197}
198
199void* VG_(get_supp_extra) ( Supp* su )
200{
201 return su->extra;
202}
203
204
205void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
206{
207 su->skind = skind;
208}
209
210void VG_(set_supp_string) ( Supp* su, Char* string )
211{
212 su->string = string;
213}
214
215void VG_(set_supp_extra) ( Supp* su, void* extra )
216{
217 su->extra = extra;
218}
219
220
221/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000222/*--- Helper fns ---*/
223/*------------------------------------------------------------*/
224
sewardjde4a1d02002-03-22 01:27:54 +0000225/* Compare error contexts, to detect duplicates. Note that if they
226 are otherwise the same, the faulting addrs and associated rwoffsets
227 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000228static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000229{
njn810086f2002-11-14 12:42:47 +0000230 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000231 return False;
njn25e49d8e72002-09-23 09:36:25 +0000232 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000233 return False;
234
njn810086f2002-11-14 12:42:47 +0000235 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000236 // case ThreadErr:
237 // case MutexErr:
238 // vg_assert(VG_(needs).core_errors);
239 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000240 default:
njn51d827b2005-05-09 01:02:08 +0000241 if (VG_(needs).tool_errors) {
242 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
243 } else {
njn95ec8702004-11-22 16:46:13 +0000244 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000245 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000246 e1->ekind);
njn67993252004-11-22 18:02:32 +0000247 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000248 }
sewardjde4a1d02002-03-22 01:27:54 +0000249 }
250}
251
njn810086f2002-11-14 12:42:47 +0000252static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000253{
sewardj71bc3cb2005-05-19 00:25:45 +0000254 if (VG_(clo_xml)) {
255 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000256 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
257 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000258 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
259 }
260
261 if (!VG_(clo_xml)) {
262 if (printCount)
263 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
264 if (err->tid > 0 && err->tid != last_tid_printed) {
265 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
266 last_tid_printed = err->tid;
267 }
sewardjb5f6f512005-03-10 23:59:00 +0000268 }
njn25e49d8e72002-09-23 09:36:25 +0000269
njn810086f2002-11-14 12:42:47 +0000270 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000271 // case ThreadErr:
272 // case MutexErr:
273 // vg_assert(VG_(needs).core_errors);
274 // VG_(tm_error_print)(err);
275 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000276 default:
njn95ec8702004-11-22 16:46:13 +0000277 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000278 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000279 else {
njn95ec8702004-11-22 16:46:13 +0000280 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000281 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000282 err->ekind);
njn67993252004-11-22 18:02:32 +0000283 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000284 }
sewardjde4a1d02002-03-22 01:27:54 +0000285 }
sewardj71bc3cb2005-05-19 00:25:45 +0000286
287 if (VG_(clo_xml))
288 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000289}
290
nethercote04d0fbc2004-01-26 16:48:06 +0000291/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000292 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000293Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000294{
295 Char ch, ch2;
296 Int res;
297
njn43c799e2003-04-08 00:08:52 +0000298 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000299 return False;
300
301 VG_(message)(Vg_UserMsg, "");
302
303 again:
304 VG_(printf)(
305 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000306 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
307 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000308 );
309
sewardj6024b212003-07-13 10:54:33 +0000310 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000311 if (res != 1) goto ioerror;
312 /* res == 1 */
313 if (ch == '\n') return False;
314 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
315 && ch != 'C' && ch != 'c') goto again;
316
sewardj6024b212003-07-13 10:54:33 +0000317 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000318 if (res != 1) goto ioerror;
319 if (ch2 != '\n') goto again;
320
njn43c799e2003-04-08 00:08:52 +0000321 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000322 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000323 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000324 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000325 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000326 vg_assert(ch == 'c' || ch == 'C');
327
328 ioerror:
njn43c799e2003-04-08 00:08:52 +0000329 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000330 return False;
331}
332
333
sewardjb5f6f512005-03-10 23:59:00 +0000334/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000335static __inline__
njn72718642003-07-24 08:45:32 +0000336void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
337 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000338{
njnca82cc02004-11-22 17:18:48 +0000339 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000340
njn810086f2002-11-14 12:42:47 +0000341 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000342 err->next = NULL;
343 err->supp = NULL;
344 err->count = 1;
njn72718642003-07-24 08:45:32 +0000345 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000346 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000347 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000348 else
349 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000350
nethercote996901a2004-08-03 13:29:09 +0000351 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000352 err->ekind = ekind;
353 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000354 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000355 err->string = s;
356
njn25e49d8e72002-09-23 09:36:25 +0000357 /* sanity... */
njn72718642003-07-24 08:45:32 +0000358 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000359}
360
njnf4261312005-03-20 23:45:36 +0000361static void printSuppForIp(UInt n, Addr ip)
362{
njn47b209a2005-03-25 23:47:16 +0000363 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000364
njn47b209a2005-03-25 23:47:16 +0000365 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000366 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000367 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000368 VG_(printf)(" obj:%s\n", buf);
369 } else {
370 VG_(printf)(" ???:??? "
371 "# unknown, suppression will not work, sorry\n");
372 }
373}
374
nethercote10d481a2004-01-25 20:33:53 +0000375static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000376{
njn43c799e2003-04-08 00:08:52 +0000377 ExeContext* ec = VG_(get_error_where)(err);
378 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000379
sewardjb5f6f512005-03-10 23:59:00 +0000380 /* At most VG_MAX_SUPP_CALLERS names */
381 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000382 vg_assert(stop_at > 0);
383
384 VG_(printf)("{\n");
385 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000386
sewardjb5f6f512005-03-10 23:59:00 +0000387 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000388 VG_(printf)(" core:PThread\n");
389
390 } else {
njn51d827b2005-05-09 01:02:08 +0000391 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000392 if (NULL == name) {
393 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000394 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000395 return;
396 }
397 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000398 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000399 }
njn43c799e2003-04-08 00:08:52 +0000400
njnf4261312005-03-20 23:45:36 +0000401 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000402 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000403
404 VG_(printf)("}\n");
405}
406
njnb4aee052003-04-15 14:09:58 +0000407static
nethercote04d0fbc2004-01-26 16:48:06 +0000408void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000409{
sewardjd153fae2005-01-10 17:24:47 +0000410 Bool still_noisy = True;
411
nethercote04d0fbc2004-01-26 16:48:06 +0000412 /* Perhaps we want a debugger attach at this point? */
413 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000414 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
415 {
nethercote04d0fbc2004-01-26 16:48:06 +0000416 VG_(printf)("starting debugger\n");
417 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000418 }
njn43c799e2003-04-08 00:08:52 +0000419 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000420 if (VG_(clo_gen_suppressions) == 2
421 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000422 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000423 ) {
nethercote42602b12004-01-25 19:30:29 +0000424 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000425 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
426 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000427 }
428}
429
430/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
431 just for pretty printing purposes. */
432static Bool is_first_shown_context = True;
433
njn25e49d8e72002-09-23 09:36:25 +0000434/* Top-level entry point to the error management subsystem.
435 All detected errors are notified here; this routine decides if/when the
436 user should see the error. */
njn72718642003-07-24 08:45:32 +0000437void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000438 ErrorKind ekind, Addr a, Char* s, void* extra )
439{
njn810086f2002-11-14 12:42:47 +0000440 Error err;
441 Error* p;
442 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000443 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000444 VgRes exe_res = Vg_MedRes;
445 static Bool stopping_message = False;
446 static Bool slowdown_message = False;
447 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000448
njn14319cc2005-03-13 06:26:22 +0000449 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
450 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000451 have been found, just refuse to collect any more. This stops
452 the burden of the error-management system becoming excessive in
453 extremely buggy programs, although it does make it pretty
454 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000455 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000456 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000457 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000458 if (!stopping_message) {
459 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000460
njn695c16e2005-03-27 03:40:28 +0000461 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000462 VG_(message)(Vg_UserMsg,
463 "More than %d different errors detected. "
464 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000465 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000466 } else {
467 VG_(message)(Vg_UserMsg,
468 "More than %d total errors detected. "
469 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000470 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000471 }
472
sewardjde4a1d02002-03-22 01:27:54 +0000473 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000474 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000475 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000476 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000477 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000478 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000479 VG_(message)(Vg_UserMsg,
480 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000481 VG_(message)(Vg_UserMsg, "");
482 stopping_message = True;
483 }
484 return;
485 }
486
njn14319cc2005-03-13 06:26:22 +0000487 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000488 been found, be much more conservative about collecting new
489 ones. */
njn695c16e2005-03-27 03:40:28 +0000490 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000491 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000492 if (!slowdown_message) {
493 VG_(message)(Vg_UserMsg, "");
494 VG_(message)(Vg_UserMsg,
495 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000496 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000497 VG_(message)(Vg_UserMsg,
498 "will still be recorded, but in less detail than before.");
499 slowdown_message = True;
500 }
501 }
502
njn25e49d8e72002-09-23 09:36:25 +0000503 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000504 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000505
506 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000507 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000508 p_prev = NULL;
509 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000510 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000511 /* Found it. */
512 p->count++;
513 if (p->supp != NULL) {
514 /* Deal correctly with suppressed errors. */
515 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000516 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000517 } else {
nethercotef2b11482004-08-02 12:36:01 +0000518 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000519 }
520
521 /* Move p to the front of the list so that future searches
522 for it are faster. */
523 if (p_prev != NULL) {
524 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000525 p_prev->next = p->next;
526 p->next = errors;
527 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000528 }
sewardj7ebf7c32003-07-24 21:29:40 +0000529
sewardjde4a1d02002-03-22 01:27:54 +0000530 return;
531 }
532 p_prev = p;
533 p = p->next;
534 }
535
536 /* Didn't see it. Copy and add. */
537
njn43c799e2003-04-08 00:08:52 +0000538 /* OK, we're really going to collect it. The context is on the stack and
539 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000540 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000541
njn02bc4b82005-05-15 17:28:26 +0000542 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000543 is for when there are more details to fill in which take time to work
544 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000545 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000546 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000547
njn02bc4b82005-05-15 17:28:26 +0000548 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000549 VG_(tdict).tool_update_extra returned. Also allow for people using
550 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000551 */
552
553 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000554 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000555 *p = err;
njn43c799e2003-04-08 00:08:52 +0000556
njn02bc4b82005-05-15 17:28:26 +0000557 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000558 switch (ekind) {
559 // case ThreadErr:
560 // case MutexErr:
561 // vg_assert(VG_(needs).core_errors);
562 // extra_size = VG_(tm_error_update_extra)(p);
563 // break;
564 default:
565 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000566 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000567 break;
568 }
njn43c799e2003-04-08 00:08:52 +0000569
njn02bc4b82005-05-15 17:28:26 +0000570 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000571 if (NULL != p->extra && 0 != extra_size) {
572 void* new_extra = VG_(malloc)(extra_size);
573 VG_(memcpy)(new_extra, p->extra, extra_size);
574 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000575 }
576
njn695c16e2005-03-27 03:40:28 +0000577 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000578 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000579 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000580 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000581 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000582 if (!is_first_shown_context)
583 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000584 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000585 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000586 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000587 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000588 } else {
nethercotef2b11482004-08-02 12:36:01 +0000589 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000590 p->supp->count++;
591 }
592}
593
njn43c799e2003-04-08 00:08:52 +0000594/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000595 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000596 guaranteed to only happen once. This avoids all the recording and
597 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000598 suppressed. Bool 'print_error' dictates whether to print the error.
599 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000600*/
njn72718642003-07-24 08:45:32 +0000601Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000602 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000603 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000604{
605 Error err;
606
607 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000608 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000609
610 /* Unless it's suppressed, we're going to show it. Don't need to make
611 a copy, because it's only temporary anyway.
612
njn02bc4b82005-05-15 17:28:26 +0000613 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000614 because that can have an affect on whether it's suppressed. Ignore
615 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000616 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000617 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000618
619 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000620 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000621 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000622
623 if (print_error) {
624 if (!is_first_shown_context)
625 VG_(message)(Vg_UserMsg, "");
626 pp_Error(&err, False);
627 is_first_shown_context = False;
628 }
nethercote04d0fbc2004-01-26 16:48:06 +0000629 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000630
631 return False;
632
633 } else {
nethercotef2b11482004-08-02 12:36:01 +0000634 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000635 return True;
636 }
637}
638
sewardjde4a1d02002-03-22 01:27:54 +0000639
sewardjde4a1d02002-03-22 01:27:54 +0000640/*------------------------------------------------------------*/
641/*--- Exported fns ---*/
642/*------------------------------------------------------------*/
643
sewardj71bc3cb2005-05-19 00:25:45 +0000644/* Show the used suppressions. Returns False if no suppression
645 got used. */
646static Bool show_used_suppressions ( void )
647{
648 Supp *su;
649 Bool any_supp;
650
sewardj7c9e57c2005-05-24 14:21:45 +0000651 if (VG_(clo_xml))
652 VG_(message)(Vg_DebugMsg, "<suppcounts>");
653
sewardj71bc3cb2005-05-19 00:25:45 +0000654 any_supp = False;
655 for (su = suppressions; su != NULL; su = su->next) {
656 if (su->count <= 0)
657 continue;
658 any_supp = True;
659 if (VG_(clo_xml)) {
660 VG_(message)(Vg_DebugMsg,
sewardj7c9e57c2005-05-24 14:21:45 +0000661 " <pair><count>%d</count><name>%s</name></pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000662 su->count, su->sname);
663 } else {
664 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
665 }
666 }
667
sewardj7c9e57c2005-05-24 14:21:45 +0000668 if (VG_(clo_xml))
669 VG_(message)(Vg_DebugMsg, "<suppcounts>");
670
sewardj71bc3cb2005-05-19 00:25:45 +0000671 return any_supp;
672}
673
674
sewardj9f297ca2005-05-20 02:29:52 +0000675/* Show all the errors that occurred, and possibly also the
676 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000677void VG_(show_all_errors) ( void )
678{
njn810086f2002-11-14 12:42:47 +0000679 Int i, n_min;
680 Int n_err_contexts, n_supp_contexts;
681 Error *p, *p_min;
682 Supp *su;
683 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000684
685 if (VG_(clo_verbosity) == 0)
686 return;
687
688 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000689 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000690 if (p->supp == NULL)
691 n_err_contexts++;
692 }
693
694 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000695 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000696 if (su->count > 0)
697 n_supp_contexts++;
698 }
sewardj71bc3cb2005-05-19 00:25:45 +0000699
700 /* If we're printing XML, just show the suppressions and stop.
701 */
702 if (VG_(clo_xml)) {
703 (void)show_used_suppressions();
704 return;
705 }
706
707 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000708 VG_(message)(Vg_UserMsg,
709 "ERROR SUMMARY: "
710 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000711 n_errs_found, n_err_contexts,
712 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000713
714 if (VG_(clo_verbosity) <= 1)
715 return;
716
717 /* Print the contexts in order of increasing error count. */
718 for (i = 0; i < n_err_contexts; i++) {
719 n_min = (1 << 30) - 1;
720 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000721 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000722 if (p->supp != NULL) continue;
723 if (p->count < n_min) {
724 n_min = p->count;
725 p_min = p;
726 }
727 }
njn67993252004-11-22 18:02:32 +0000728 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000729
730 VG_(message)(Vg_UserMsg, "");
731 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
732 p_min->count,
733 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000734 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000735
736 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000737 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000738 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000739 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000740 }
741
742 p_min->count = 1 << 30;
743 }
744
745 if (n_supp_contexts > 0)
746 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000747 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000748
749 if (n_err_contexts > 0) {
750 if (any_supp)
751 VG_(message)(Vg_UserMsg, "");
752 VG_(message)(Vg_UserMsg,
753 "IN SUMMARY: "
754 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000755 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000756 n_supp_contexts );
757 VG_(message)(Vg_UserMsg, "");
758 }
759}
760
sewardj9f297ca2005-05-20 02:29:52 +0000761
762/* Show occurrence counts of all errors, in XML form. */
763void VG_(show_error_counts_as_XML) ( void )
764{
765 Error* err;
766 VG_(message)(Vg_UserMsg, "<errorcounts>");
767 for (err = errors; err != NULL; err = err->next) {
768 if (err->supp != NULL)
769 continue;
770 if (err->count <= 0)
771 continue;
772 VG_(message)(
sewardj7c9e57c2005-05-24 14:21:45 +0000773 Vg_UserMsg, " <pair><count>%d</count>"
774 "<unique>0x%llx</unique></pair>",
775 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000776 );
777 }
778 VG_(message)(Vg_UserMsg, "</errorcounts>");
779}
780
781
sewardjde4a1d02002-03-22 01:27:54 +0000782/*------------------------------------------------------------*/
783/*--- Standard suppressions ---*/
784/*------------------------------------------------------------*/
785
786/* Get a non-blank, non-comment line of at most nBuf chars from fd.
787 Skips leading spaces on the line. Return True if EOF was hit instead.
788*/
njn4ba5a792002-09-30 10:23:54 +0000789Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000790{
791 Char ch;
792 Int n, i;
793 while (True) {
794 /* First, read until a non-blank char appears. */
795 while (True) {
796 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000797 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000798 if (n == 0) return True;
799 }
800
801 /* Now, read the line into buf. */
802 i = 0;
803 buf[i++] = ch; buf[i] = 0;
804 while (True) {
805 n = VG_(read)(fd, &ch, 1);
806 if (n == 0) return False; /* the next call will return True */
807 if (ch == '\n') break;
808 if (i > 0 && i == nBuf-1) i--;
809 buf[i++] = ch; buf[i] = 0;
810 }
njn0c0f32a2005-03-26 04:14:01 +0000811 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000812 i--; buf[i] = 0;
813 };
814
njn02bc4b82005-05-15 17:28:26 +0000815 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000816 /* Ok, we have a line. If a non-comment line, return.
817 If a comment line, start all over again. */
818 if (buf[0] != '#') return False;
819 }
820}
821
822
823/* *p_caller contains the raw name of a caller, supposedly either
824 fun:some_function_name or
825 obj:some_object_name.
826 Set *p_ty accordingly and advance *p_caller over the descriptor
827 (fun: or obj:) part.
828 Returns False if failed.
829*/
sewardjb5f6f512005-03-10 23:59:00 +0000830static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000831{
sewardjb5f6f512005-03-10 23:59:00 +0000832 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
833 p->name += 4;
834 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000835 return True;
836 }
sewardjb5f6f512005-03-10 23:59:00 +0000837 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
838 p->name += 4;
839 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000840 return True;
841 }
842 VG_(printf)("location should start with fun: or obj:\n");
843 return False;
844}
845
846
nethercote7cc9c232004-01-21 15:08:04 +0000847/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000848static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000849Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000850{
851 Bool found;
852 Char *s = NULL; /* Shut gcc up */
853 Int len = VG_(strlen)(name);
854
855 found = (NULL != (s = VG_(strstr)(names, name)) &&
856 (s == names || *(s-1) == ',') &&
857 (*(s+len) == ',' || *(s+len) == '\0')
858 );
859
860 return found;
861}
862
njn695c16e2005-03-27 03:40:28 +0000863/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000864 and place them in the suppressions list. If there's any difficulty
865 doing this, just give up -- there's no point in trying to recover.
866*/
sewardjde4a1d02002-03-22 01:27:54 +0000867static void load_one_suppressions_file ( Char* filename )
868{
869# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000870 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000871 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000872 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000873 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000874 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000875 Char* err_str = NULL;
876 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000877
njn25e49d8e72002-09-23 09:36:25 +0000878 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000879 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000880 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000881 filename );
882 VG_(exit)(1);
883 }
884
sewardjb5f6f512005-03-10 23:59:00 +0000885#define BOMB(S) { err_str = S; goto syntax_error; }
886
sewardjde4a1d02002-03-22 01:27:54 +0000887 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000888 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000889 Supp* supp;
890 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000891 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000892
893 // Initialise temporary reading-in buffer.
894 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
895 tmp_callers[i].ty = NoName;
896 tmp_callers[i].name = NULL;
897 }
898
njn810086f2002-11-14 12:42:47 +0000899 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000900
njn4ba5a792002-09-30 10:23:54 +0000901 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000902 if (eof) break;
903
sewardjb5f6f512005-03-10 23:59:00 +0000904 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000905
njn4ba5a792002-09-30 10:23:54 +0000906 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000907
908 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
909
njn25e49d8e72002-09-23 09:36:25 +0000910 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000911
njn4ba5a792002-09-30 10:23:54 +0000912 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000913
sewardjb5f6f512005-03-10 23:59:00 +0000914 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000915
njn94065fd2004-11-22 19:26:27 +0000916 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000917 i = 0;
918 while (True) {
919 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000920 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000921 i++;
njn25e49d8e72002-09-23 09:36:25 +0000922 }
njnc40c3a82002-10-02 11:02:27 +0000923 buf[i] = '\0'; /* Replace ':', splitting into two strings */
924
nethercote7cc9c232004-01-21 15:08:04 +0000925 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000926 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000927
nethercote7cc9c232004-01-21 15:08:04 +0000928 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000929 {
sewardjb5f6f512005-03-10 23:59:00 +0000930 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000931 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000932 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000933 else
sewardjb5f6f512005-03-10 23:59:00 +0000934 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000935 }
njn95ec8702004-11-22 16:46:13 +0000936 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000937 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000938 {
sewardjb5f6f512005-03-10 23:59:00 +0000939 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000940 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000941 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000942 } else {
943 BOMB("unknown tool suppression type");
944 }
njnc40c3a82002-10-02 11:02:27 +0000945 }
njn25e49d8e72002-09-23 09:36:25 +0000946 else {
sewardjb5f6f512005-03-10 23:59:00 +0000947 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000948 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000949 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000950 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000951 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000952 break;
953 }
954 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000955 }
956
njn95ec8702004-11-22 16:46:13 +0000957 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000958 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000959 {
960 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000961 }
962
sewardjb5f6f512005-03-10 23:59:00 +0000963 i = 0;
964 while (True) {
965 eof = VG_(get_line) ( fd, buf, N_BUF );
966 if (eof)
967 BOMB("unexpected end-of-file");
968 if (VG_STREQ(buf, "}")) {
969 if (i > 0) {
970 break;
971 } else {
972 BOMB("missing stack trace");
973 }
974 }
975 if (i == VG_MAX_SUPP_CALLERS)
976 BOMB("too many callers in stack trace");
977 if (i > 0 && i >= VG_(clo_backtrace_size))
978 break;
979 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
980 if (!setLocationTy(&(tmp_callers[i])))
981 BOMB("location should start with 'fun:' or 'obj:'");
982 i++;
983 }
984
985 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
986 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000987 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000988 do {
989 eof = VG_(get_line) ( fd, buf, N_BUF );
990 } while (!eof && !VG_STREQ(buf, "}"));
991 }
992
993 // Copy tmp_callers[] into supp->callers[]
994 supp->n_callers = i;
995 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
996 for (i = 0; i < supp->n_callers; i++) {
997 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +0000998 }
999
njn695c16e2005-03-27 03:40:28 +00001000 supp->next = suppressions;
1001 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001002 }
sewardjde4a1d02002-03-22 01:27:54 +00001003 VG_(close)(fd);
1004 return;
1005
1006 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001007 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001008 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001009
sewardjde4a1d02002-03-22 01:27:54 +00001010 VG_(close)(fd);
1011 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001012 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001013
sewardjb5f6f512005-03-10 23:59:00 +00001014# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001015# undef N_BUF
1016}
1017
1018
1019void VG_(load_suppressions) ( void )
1020{
1021 Int i;
njn695c16e2005-03-27 03:40:28 +00001022 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001023 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1024 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001025 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1026 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001027 }
1028 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1029 }
1030}
1031
sewardjb5f6f512005-03-10 23:59:00 +00001032static
njn810086f2002-11-14 12:42:47 +00001033Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001034{
njn810086f2002-11-14 12:42:47 +00001035 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001036 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001037 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001038 default:
njn95ec8702004-11-22 16:46:13 +00001039 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001040 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001041 } else {
1042 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001043 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001044 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001045 err->ekind);
njn67993252004-11-22 18:02:32 +00001046 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001047 }
1048 }
1049}
1050
sewardjb5f6f512005-03-10 23:59:00 +00001051static
1052Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001053{
1054 Int i;
njn47b209a2005-03-25 23:47:16 +00001055 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001056 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001057
sewardjb5f6f512005-03-10 23:59:00 +00001058 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001059 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001060 vg_assert(su->callers[i].name != NULL);
1061 switch (su->callers[i].ty) {
1062 case ObjName:
njn47b209a2005-03-25 23:47:16 +00001063 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001064 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001065 break;
1066
1067 case FunName:
1068 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001069 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001070 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001071 break;
njn67993252004-11-22 18:02:32 +00001072 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001073 }
sewardjb5f6f512005-03-10 23:59:00 +00001074 if (!VG_(string_match)(su->callers[i].name, caller_name))
1075 return False;
njn25e49d8e72002-09-23 09:36:25 +00001076 }
1077
1078 /* If we reach here, it's a match */
1079 return True;
1080}
sewardjde4a1d02002-03-22 01:27:54 +00001081
njn810086f2002-11-14 12:42:47 +00001082/* Does an error context match a suppression? ie is this a suppressible
1083 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001084 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001085*/
njn810086f2002-11-14 12:42:47 +00001086static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001087{
njn810086f2002-11-14 12:42:47 +00001088 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001089
sewardjde4a1d02002-03-22 01:27:54 +00001090 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001091 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001092 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001093 supp_matches_callers(err, su))
1094 {
njn25e49d8e72002-09-23 09:36:25 +00001095 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001096 }
sewardjde4a1d02002-03-22 01:27:54 +00001097 }
njn25e49d8e72002-09-23 09:36:25 +00001098 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001099}
1100
sewardjde4a1d02002-03-22 01:27:54 +00001101/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00001102/*--- end m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001103/*--------------------------------------------------------------------*/