blob: 1115f852c6d394b32536b98fbe2775f6c98e9584 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- Management of error messages. vg_errcontext.c ---*/
4/*--------------------------------------------------------------------*/
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"
sewardjde4a1d02002-03-22 01:27:54 +000032
33/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000034/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000035/*------------------------------------------------------------*/
36
sewardjde4a1d02002-03-22 01:27:54 +000037/* The list of error contexts found, both suppressed and unsuppressed.
38 Initially empty, and grows as errors are detected. */
njn810086f2002-11-14 12:42:47 +000039static Error* vg_errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000040
41/* The list of suppression directives, as read from the specified
42 suppressions file. */
njn810086f2002-11-14 12:42:47 +000043static Supp* vg_suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000044
45/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000046static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000047
48/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000049static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000050
51/* forwards ... */
njn810086f2002-11-14 12:42:47 +000052static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000053
sewardjb5f6f512005-03-10 23:59:00 +000054static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000055
56/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000057/*--- Error type ---*/
58/*------------------------------------------------------------*/
59
nethercote996901a2004-08-03 13:29:09 +000060/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000061 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000062
63/* Errors. Extensible (via the 'extra' field). Tools can use a normal
64 enum (with element values in the normal range (0..)) for `ekind'.
65 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000066 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000067
68 When errors are found and recorded with VG_(maybe_record_error)(), all
69 the tool must do is pass in the four parameters; core will
70 allocate/initialise the error record.
71*/
72struct _Error {
73 struct _Error* next;
74 // NULL if unsuppressed; or ptr to suppression record.
75 Supp* supp;
76 Int count;
77 ThreadId tid;
78
79 // The tool-specific part
80 ExeContext* where; // Initialised by core
81 Int ekind; // Used by ALL. Must be in the range (0..)
82 Addr addr; // Used frequently
83 Char* string; // Used frequently
84 void* extra; // For any tool-specific extras
85};
86
87ExeContext* VG_(get_error_where) ( Error* err )
88{
89 return err->where;
90}
91
92ErrorKind VG_(get_error_kind) ( Error* err )
93{
94 return err->ekind;
95}
96
97Addr VG_(get_error_address) ( Error* err )
98{
99 return err->addr;
100}
101
102Char* VG_(get_error_string) ( Error* err )
103{
104 return err->string;
105}
106
107void* VG_(get_error_extra) ( Error* err )
108{
109 return err->extra;
110}
111
nethercotef2b11482004-08-02 12:36:01 +0000112UInt VG_(get_n_errs_found)( void )
113{
114 return n_errs_found;
115}
116
nethercote4a184902004-08-02 12:21:09 +0000117/*------------------------------------------------------------*/
118/*--- Suppression type ---*/
119/*------------------------------------------------------------*/
120
121/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
122 * effectively extend it by defining their own enums in the (0..) range. */
123typedef
124 enum {
125 PThreadSupp = -1, /* Matches PThreadErr */
126 }
127 CoreSuppKind;
128
sewardjb5f6f512005-03-10 23:59:00 +0000129/* Max number of callers for context in a suppression. */
130#define VG_MAX_SUPP_CALLERS 24
131
nethercote4a184902004-08-02 12:21:09 +0000132/* For each caller specified for a suppression, record the nature of
133 the caller name. Not of interest to tools. */
134typedef
135 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000136 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000137 ObjName, /* Name is of an shared object file. */
138 FunName /* Name is of a function. */
139 }
140 SuppLocTy;
141
sewardjb5f6f512005-03-10 23:59:00 +0000142typedef
143 struct {
144 SuppLocTy ty;
145 Char* name;
146 }
147 SuppLoc;
148
nethercote4a184902004-08-02 12:21:09 +0000149/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000150 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000151 Tools can use a normal enum (with element values in the normal range
152 (0..)) for `skind'. */
153struct _Supp {
154 struct _Supp* next;
155 Int count; // The number of times this error has been suppressed.
156 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000157
158 // Length of 'callers'
159 Int n_callers;
160 // Array of callers, for matching stack traces. First one (name of fn
161 // where err occurs) is mandatory; rest are optional.
162 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000163
164 /* The tool-specific part */
165 SuppKind skind; // What kind of suppression. Must use the range (0..).
166 Char* string; // String -- use is optional. NULL by default.
167 void* extra; // Anything else -- use is optional. NULL by default.
168};
169
170SuppKind VG_(get_supp_kind) ( Supp* su )
171{
172 return su->skind;
173}
174
175Char* VG_(get_supp_string) ( Supp* su )
176{
177 return su->string;
178}
179
180void* VG_(get_supp_extra) ( Supp* su )
181{
182 return su->extra;
183}
184
185
186void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
187{
188 su->skind = skind;
189}
190
191void VG_(set_supp_string) ( Supp* su, Char* string )
192{
193 su->string = string;
194}
195
196void VG_(set_supp_extra) ( Supp* su, void* extra )
197{
198 su->extra = extra;
199}
200
201
202/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000203/*--- Helper fns ---*/
204/*------------------------------------------------------------*/
205
sewardjde4a1d02002-03-22 01:27:54 +0000206/* Compare error contexts, to detect duplicates. Note that if they
207 are otherwise the same, the faulting addrs and associated rwoffsets
208 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000209static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000210{
njn810086f2002-11-14 12:42:47 +0000211 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000212 return False;
njn25e49d8e72002-09-23 09:36:25 +0000213 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000214 return False;
215
njn810086f2002-11-14 12:42:47 +0000216 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000217 // case ThreadErr:
218 // case MutexErr:
219 // vg_assert(VG_(needs).core_errors);
220 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000221 default:
njn95ec8702004-11-22 16:46:13 +0000222 if (VG_(needs).tool_errors)
njn26f02512004-11-22 18:33:15 +0000223 return TL_(eq_Error)(res, e1, e2);
njn25e49d8e72002-09-23 09:36:25 +0000224 else {
njn95ec8702004-11-22 16:46:13 +0000225 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000226 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000227 e1->ekind);
njn67993252004-11-22 18:02:32 +0000228 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000229 }
sewardjde4a1d02002-03-22 01:27:54 +0000230 }
231}
232
njn810086f2002-11-14 12:42:47 +0000233static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000234{
sewardjde4a1d02002-03-22 01:27:54 +0000235 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +0000236 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
sewardjb5f6f512005-03-10 23:59:00 +0000237 if (err->tid > 0 && err->tid != last_tid_printed) {
njn25e49d8e72002-09-23 09:36:25 +0000238 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
sewardjb5f6f512005-03-10 23:59:00 +0000239 last_tid_printed = err->tid;
240 }
njn25e49d8e72002-09-23 09:36:25 +0000241
njn810086f2002-11-14 12:42:47 +0000242 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000243 // case ThreadErr:
244 // case MutexErr:
245 // vg_assert(VG_(needs).core_errors);
246 // VG_(tm_error_print)(err);
247 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000248 default:
njn95ec8702004-11-22 16:46:13 +0000249 if (VG_(needs).tool_errors)
njn26f02512004-11-22 18:33:15 +0000250 TL_(pp_Error)( err );
njn25e49d8e72002-09-23 09:36:25 +0000251 else {
njn95ec8702004-11-22 16:46:13 +0000252 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000253 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000254 err->ekind);
njn67993252004-11-22 18:02:32 +0000255 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000256 }
sewardjde4a1d02002-03-22 01:27:54 +0000257 }
258}
259
nethercote04d0fbc2004-01-26 16:48:06 +0000260/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000261 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000262Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000263{
264 Char ch, ch2;
265 Int res;
266
njn43c799e2003-04-08 00:08:52 +0000267 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000268 return False;
269
270 VG_(message)(Vg_UserMsg, "");
271
272 again:
273 VG_(printf)(
274 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000275 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
276 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000277 );
278
sewardj6024b212003-07-13 10:54:33 +0000279 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000280 if (res != 1) goto ioerror;
281 /* res == 1 */
282 if (ch == '\n') return False;
283 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
284 && ch != 'C' && ch != 'c') goto again;
285
sewardj6024b212003-07-13 10:54:33 +0000286 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000287 if (res != 1) goto ioerror;
288 if (ch2 != '\n') goto again;
289
njn43c799e2003-04-08 00:08:52 +0000290 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000291 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000292 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000293 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000294 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000295 vg_assert(ch == 'c' || ch == 'C');
296
297 ioerror:
njn43c799e2003-04-08 00:08:52 +0000298 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000299 return False;
300}
301
302
sewardjb5f6f512005-03-10 23:59:00 +0000303/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000304static __inline__
njn72718642003-07-24 08:45:32 +0000305void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
306 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000307{
njnca82cc02004-11-22 17:18:48 +0000308 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000309
njn810086f2002-11-14 12:42:47 +0000310 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000311 err->next = NULL;
312 err->supp = NULL;
313 err->count = 1;
njn72718642003-07-24 08:45:32 +0000314 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000315 if (NULL == where)
njn72718642003-07-24 08:45:32 +0000316 err->where = VG_(get_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000317 else
318 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000319
nethercote996901a2004-08-03 13:29:09 +0000320 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000321 err->ekind = ekind;
322 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000323 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000324 err->string = s;
325
njn25e49d8e72002-09-23 09:36:25 +0000326 /* sanity... */
njn72718642003-07-24 08:45:32 +0000327 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000328}
329
nethercote10d481a2004-01-25 20:33:53 +0000330static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000331{
sewardj05bcdcb2003-05-18 10:05:38 +0000332 Int i;
nethercote3b38c1d2004-10-18 15:47:18 +0000333 static UChar buf[M_VG_ERRTXT];
nethercote77eba602003-11-13 17:35:04 +0000334 Bool main_done = False;
njn43c799e2003-04-08 00:08:52 +0000335 ExeContext* ec = VG_(get_error_where)(err);
336 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000337
sewardjb5f6f512005-03-10 23:59:00 +0000338 /* At most VG_MAX_SUPP_CALLERS names */
339 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000340 vg_assert(stop_at > 0);
341
342 VG_(printf)("{\n");
343 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000344
sewardjb5f6f512005-03-10 23:59:00 +0000345 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000346 VG_(printf)(" core:PThread\n");
347
348 } else {
njn26f02512004-11-22 18:33:15 +0000349 Char* name = TL_(get_error_name)(err);
njn6a230532003-07-21 10:38:23 +0000350 if (NULL == name) {
351 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000352 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000353 return;
354 }
355 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn26f02512004-11-22 18:33:15 +0000356 TL_(print_extra_suppression_info)(err);
njn6a230532003-07-21 10:38:23 +0000357 }
njn43c799e2003-04-08 00:08:52 +0000358
359 /* This loop condensed from VG_(mini_stack_dump)() */
360 i = 0;
361 do {
nethercote86c5dcb2004-09-05 21:32:37 +0000362 Addr eip = ec->ips[i];
nethercotefbfc1082004-09-04 15:28:37 +0000363 if (i > 0)
364 eip -= MIN_INSTR_SIZE; // point to calling line
njn43c799e2003-04-08 00:08:52 +0000365 if ( VG_(get_fnname_nodemangle) (eip, buf, M_VG_ERRTXT) ) {
nethercote77eba602003-11-13 17:35:04 +0000366 // Stop after "main"; if main() is recursive, stop after last main().
367
368 if ( ! VG_(clo_show_below_main)) {
369 if (VG_STREQ(buf, "main"))
370 main_done = True;
371 else if (main_done)
372 break;
373 }
njn43c799e2003-04-08 00:08:52 +0000374 VG_(printf)(" fun:%s\n", buf);
375 } else if ( VG_(get_objname)(eip, buf, M_VG_ERRTXT) ) {
376 VG_(printf)(" obj:%s\n", buf);
377 } else {
378 VG_(printf)(" ???:??? "
nethercote125c91e2004-06-27 12:38:17 +0000379 "# unknown, suppression will not work, sorry\n");
njn43c799e2003-04-08 00:08:52 +0000380 }
381 i++;
nethercote86c5dcb2004-09-05 21:32:37 +0000382 } while (i < stop_at && ec->ips[i] != 0);
njn43c799e2003-04-08 00:08:52 +0000383
384 VG_(printf)("}\n");
385}
386
njnb4aee052003-04-15 14:09:58 +0000387static
nethercote04d0fbc2004-01-26 16:48:06 +0000388void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000389{
sewardjd153fae2005-01-10 17:24:47 +0000390 Bool still_noisy = True;
391
nethercote04d0fbc2004-01-26 16:48:06 +0000392 /* Perhaps we want a debugger attach at this point? */
393 if (allow_db_attach &&
394 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
njn3e884182003-04-15 13:03:23 +0000395 {
nethercote04d0fbc2004-01-26 16:48:06 +0000396 VG_(printf)("starting debugger\n");
397 VG_(start_debugger)( err->tid );
njn43c799e2003-04-08 00:08:52 +0000398 }
399 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000400 if (VG_(clo_gen_suppressions) == 2
401 || (VG_(clo_gen_suppressions) == 1
402 && VG_(is_action_requested)( "Print suppression",
403 &still_noisy ))
404 ) {
nethercote42602b12004-01-25 19:30:29 +0000405 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000406 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
407 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000408 }
409}
410
411/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
412 just for pretty printing purposes. */
413static Bool is_first_shown_context = True;
414
njn25e49d8e72002-09-23 09:36:25 +0000415/* Top-level entry point to the error management subsystem.
416 All detected errors are notified here; this routine decides if/when the
417 user should see the error. */
njn72718642003-07-24 08:45:32 +0000418void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000419 ErrorKind ekind, Addr a, Char* s, void* extra )
420{
njn810086f2002-11-14 12:42:47 +0000421 Error err;
422 Error* p;
423 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000424 UInt extra_size;
njn810086f2002-11-14 12:42:47 +0000425 VgRes exe_res = Vg_MedRes;
njn810086f2002-11-14 12:42:47 +0000426 static Bool stopping_message = False;
427 static Bool slowdown_message = False;
428 static Int vg_n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000429
sewardjf2537be2002-04-24 21:03:47 +0000430 /* After M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
431 been found, or M_VG_COLLECT_NO_ERRORS_AFTER_FOUND total errors
432 have been found, just refuse to collect any more. This stops
433 the burden of the error-management system becoming excessive in
434 extremely buggy programs, although it does make it pretty
435 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000436 if (VG_(clo_error_limit)
sewardj72f98ff2002-06-13 17:23:38 +0000437 && (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN
nethercotef2b11482004-08-02 12:36:01 +0000438 || n_errs_found >= M_VG_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000439 if (!stopping_message) {
440 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000441
442 if (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN) {
443 VG_(message)(Vg_UserMsg,
444 "More than %d different errors detected. "
445 "I'm not reporting any more.",
446 M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN );
447 } else {
448 VG_(message)(Vg_UserMsg,
449 "More than %d total errors detected. "
450 "I'm not reporting any more.",
451 M_VG_COLLECT_NO_ERRORS_AFTER_FOUND );
452 }
453
sewardjde4a1d02002-03-22 01:27:54 +0000454 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000455 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000456 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000457 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000458 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000459 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000460 VG_(message)(Vg_UserMsg,
461 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000462 VG_(message)(Vg_UserMsg, "");
463 stopping_message = True;
464 }
465 return;
466 }
467
468 /* After M_VG_COLLECT_ERRORS_SLOWLY_AFTER different errors have
469 been found, be much more conservative about collecting new
470 ones. */
471 if (vg_n_errs_shown >= M_VG_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000472 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000473 if (!slowdown_message) {
474 VG_(message)(Vg_UserMsg, "");
475 VG_(message)(Vg_UserMsg,
476 "More than %d errors detected. Subsequent errors",
477 M_VG_COLLECT_ERRORS_SLOWLY_AFTER);
478 VG_(message)(Vg_UserMsg,
479 "will still be recorded, but in less detail than before.");
480 slowdown_message = True;
481 }
482 }
483
njn25e49d8e72002-09-23 09:36:25 +0000484 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000485 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000486
487 /* First, see if we've got an error record matching this one. */
njn25e49d8e72002-09-23 09:36:25 +0000488 p = vg_errors;
sewardjde4a1d02002-03-22 01:27:54 +0000489 p_prev = NULL;
490 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000491 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000492 /* Found it. */
493 p->count++;
494 if (p->supp != NULL) {
495 /* Deal correctly with suppressed errors. */
496 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000497 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000498 } else {
nethercotef2b11482004-08-02 12:36:01 +0000499 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000500 }
501
502 /* Move p to the front of the list so that future searches
503 for it are faster. */
504 if (p_prev != NULL) {
505 vg_assert(p_prev->next == p);
506 p_prev->next = p->next;
njn25e49d8e72002-09-23 09:36:25 +0000507 p->next = vg_errors;
508 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000509 }
sewardj7ebf7c32003-07-24 21:29:40 +0000510
sewardjde4a1d02002-03-22 01:27:54 +0000511 return;
512 }
513 p_prev = p;
514 p = p->next;
515 }
516
517 /* Didn't see it. Copy and add. */
518
njn43c799e2003-04-08 00:08:52 +0000519 /* OK, we're really going to collect it. The context is on the stack and
520 will disappear shortly, so we must copy it. First do the main
521 (non-`extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000522
njn26f02512004-11-22 18:33:15 +0000523 Then TL_(update_extra) can update the `extra' part. This is for when
njn43c799e2003-04-08 00:08:52 +0000524 there are more details to fill in which take time to work out but
525 don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000526 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000527 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000528
529 Then, if there is an `extra' part, copy it too, using the size that
njn26f02512004-11-22 18:33:15 +0000530 TL_(update_extra) returned. Also allow for people using the void*
njna70114c2003-08-19 16:14:42 +0000531 extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000532 */
533
534 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000535 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000536 *p = err;
njn43c799e2003-04-08 00:08:52 +0000537
sewardjb5f6f512005-03-10 23:59:00 +0000538 /* update `extra' */
539 switch (ekind) {
540 // case ThreadErr:
541 // case MutexErr:
542 // vg_assert(VG_(needs).core_errors);
543 // extra_size = VG_(tm_error_update_extra)(p);
544 // break;
545 default:
546 vg_assert(VG_(needs).tool_errors);
547 extra_size = TL_(update_extra)(p);
548 break;
549 }
njn43c799e2003-04-08 00:08:52 +0000550
sewardjb5f6f512005-03-10 23:59:00 +0000551 /* copy block pointed to by `extra', if there is one */
552 if (NULL != p->extra && 0 != extra_size) {
553 void* new_extra = VG_(malloc)(extra_size);
554 VG_(memcpy)(new_extra, p->extra, extra_size);
555 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000556 }
557
njn25e49d8e72002-09-23 09:36:25 +0000558 p->next = vg_errors;
559 p->supp = is_suppressible_error(&err);
560 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000561 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000562 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000563 if (!is_first_shown_context)
564 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000565 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000566 is_first_shown_context = False;
567 vg_n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000568 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000569 } else {
nethercotef2b11482004-08-02 12:36:01 +0000570 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000571 p->supp->count++;
572 }
573}
574
njn43c799e2003-04-08 00:08:52 +0000575/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000576 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000577 guaranteed to only happen once. This avoids all the recording and
578 comparing stuff. But they can be suppressed; returns True if it is
njn47363ab2003-04-21 13:24:40 +0000579 suppressed. Bool `print_error' dictates whether to print the error.
nethercotef2b11482004-08-02 12:36:01 +0000580 Bool `count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000581*/
njn72718642003-07-24 08:45:32 +0000582Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000583 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000584 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000585{
586 Error err;
587
588 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000589 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000590
591 /* Unless it's suppressed, we're going to show it. Don't need to make
592 a copy, because it's only temporary anyway.
593
njn26f02512004-11-22 18:33:15 +0000594 Then update the `extra' part with TL_(update_extra), because that can
njn43c799e2003-04-08 00:08:52 +0000595 have an affect on whether it's suppressed. Ignore the size return
njn26f02512004-11-22 18:33:15 +0000596 value of TL_(update_extra), because we're not copying `extra'. */
597 (void)TL_(update_extra)(&err);
njn43c799e2003-04-08 00:08:52 +0000598
599 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000600 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000601 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000602
603 if (print_error) {
604 if (!is_first_shown_context)
605 VG_(message)(Vg_UserMsg, "");
606 pp_Error(&err, False);
607 is_first_shown_context = False;
608 }
nethercote04d0fbc2004-01-26 16:48:06 +0000609 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000610
611 return False;
612
613 } else {
nethercotef2b11482004-08-02 12:36:01 +0000614 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000615 return True;
616 }
617}
618
sewardjde4a1d02002-03-22 01:27:54 +0000619
sewardjde4a1d02002-03-22 01:27:54 +0000620/*------------------------------------------------------------*/
621/*--- Exported fns ---*/
622/*------------------------------------------------------------*/
623
njn25e49d8e72002-09-23 09:36:25 +0000624/* These are called not from generated code but from the scheduler */
sewardj8c824512002-04-14 04:16:48 +0000625
sewardjde4a1d02002-03-22 01:27:54 +0000626void VG_(show_all_errors) ( void )
627{
njn810086f2002-11-14 12:42:47 +0000628 Int i, n_min;
629 Int n_err_contexts, n_supp_contexts;
630 Error *p, *p_min;
631 Supp *su;
632 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000633
634 if (VG_(clo_verbosity) == 0)
635 return;
636
637 n_err_contexts = 0;
njn25e49d8e72002-09-23 09:36:25 +0000638 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000639 if (p->supp == NULL)
640 n_err_contexts++;
641 }
642
643 n_supp_contexts = 0;
644 for (su = vg_suppressions; su != NULL; su = su->next) {
645 if (su->count > 0)
646 n_supp_contexts++;
647 }
sewardjde4a1d02002-03-22 01:27:54 +0000648 VG_(message)(Vg_UserMsg,
649 "ERROR SUMMARY: "
650 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000651 n_errs_found, n_err_contexts,
652 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000653
654 if (VG_(clo_verbosity) <= 1)
655 return;
656
657 /* Print the contexts in order of increasing error count. */
658 for (i = 0; i < n_err_contexts; i++) {
659 n_min = (1 << 30) - 1;
660 p_min = NULL;
njn25e49d8e72002-09-23 09:36:25 +0000661 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000662 if (p->supp != NULL) continue;
663 if (p->count < n_min) {
664 n_min = p->count;
665 p_min = p;
666 }
667 }
njn67993252004-11-22 18:02:32 +0000668 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000669
670 VG_(message)(Vg_UserMsg, "");
671 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
672 p_min->count,
673 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000674 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000675
676 if ((i+1 == VG_(clo_dump_error))) {
sewardjfa8ec112005-01-19 11:55:34 +0000677 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
678 p_min->where->ips[0], /*debugging*/True,
679 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000680 }
681
682 p_min->count = 1 << 30;
683 }
684
685 if (n_supp_contexts > 0)
686 VG_(message)(Vg_DebugMsg, "");
687 any_supp = False;
688 for (su = vg_suppressions; su != NULL; su = su->next) {
689 if (su->count > 0) {
690 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000691 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000692 }
693 }
694
695 if (n_err_contexts > 0) {
696 if (any_supp)
697 VG_(message)(Vg_UserMsg, "");
698 VG_(message)(Vg_UserMsg,
699 "IN SUMMARY: "
700 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000701 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000702 n_supp_contexts );
703 VG_(message)(Vg_UserMsg, "");
704 }
705}
706
707/*------------------------------------------------------------*/
708/*--- Standard suppressions ---*/
709/*------------------------------------------------------------*/
710
711/* Get a non-blank, non-comment line of at most nBuf chars from fd.
712 Skips leading spaces on the line. Return True if EOF was hit instead.
713*/
714
715#define VG_ISSPACE(ch) (((ch)==' ') || ((ch)=='\n') || ((ch)=='\t'))
716
njn4ba5a792002-09-30 10:23:54 +0000717Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000718{
719 Char ch;
720 Int n, i;
721 while (True) {
722 /* First, read until a non-blank char appears. */
723 while (True) {
724 n = VG_(read)(fd, &ch, 1);
725 if (n == 1 && !VG_ISSPACE(ch)) break;
726 if (n == 0) return True;
727 }
728
729 /* Now, read the line into buf. */
730 i = 0;
731 buf[i++] = ch; buf[i] = 0;
732 while (True) {
733 n = VG_(read)(fd, &ch, 1);
734 if (n == 0) return False; /* the next call will return True */
735 if (ch == '\n') break;
736 if (i > 0 && i == nBuf-1) i--;
737 buf[i++] = ch; buf[i] = 0;
738 }
739 while (i > 1 && VG_ISSPACE(buf[i-1])) {
740 i--; buf[i] = 0;
741 };
742
743 /* VG_(printf)("The line is `%s'\n", buf); */
744 /* Ok, we have a line. If a non-comment line, return.
745 If a comment line, start all over again. */
746 if (buf[0] != '#') return False;
747 }
748}
749
750
751/* *p_caller contains the raw name of a caller, supposedly either
752 fun:some_function_name or
753 obj:some_object_name.
754 Set *p_ty accordingly and advance *p_caller over the descriptor
755 (fun: or obj:) part.
756 Returns False if failed.
757*/
sewardjb5f6f512005-03-10 23:59:00 +0000758static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000759{
sewardjb5f6f512005-03-10 23:59:00 +0000760 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
761 p->name += 4;
762 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000763 return True;
764 }
sewardjb5f6f512005-03-10 23:59:00 +0000765 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
766 p->name += 4;
767 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000768 return True;
769 }
770 VG_(printf)("location should start with fun: or obj:\n");
771 return False;
772}
773
774
nethercote7cc9c232004-01-21 15:08:04 +0000775/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000776static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000777Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000778{
779 Bool found;
780 Char *s = NULL; /* Shut gcc up */
781 Int len = VG_(strlen)(name);
782
783 found = (NULL != (s = VG_(strstr)(names, name)) &&
784 (s == names || *(s-1) == ',') &&
785 (*(s+len) == ',' || *(s+len) == '\0')
786 );
787
788 return found;
789}
790
sewardjde4a1d02002-03-22 01:27:54 +0000791/* Read suppressions from the file specified in vg_clo_suppressions
792 and place them in the suppressions list. If there's any difficulty
793 doing this, just give up -- there's no point in trying to recover.
794*/
sewardjde4a1d02002-03-22 01:27:54 +0000795static void load_one_suppressions_file ( Char* filename )
796{
797# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000798 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000799 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000800 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000801 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000802 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000803 Char* err_str = NULL;
804 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000805
njn25e49d8e72002-09-23 09:36:25 +0000806 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000807 if (fd < 0) {
njn25e49d8e72002-09-23 09:36:25 +0000808 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file `%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000809 filename );
810 VG_(exit)(1);
811 }
812
sewardjb5f6f512005-03-10 23:59:00 +0000813#define BOMB(S) { err_str = S; goto syntax_error; }
814
sewardjde4a1d02002-03-22 01:27:54 +0000815 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000816 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000817 Supp* supp;
818 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000819 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000820
821 // Initialise temporary reading-in buffer.
822 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
823 tmp_callers[i].ty = NoName;
824 tmp_callers[i].name = NULL;
825 }
826
njn810086f2002-11-14 12:42:47 +0000827 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000828
njn4ba5a792002-09-30 10:23:54 +0000829 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000830 if (eof) break;
831
sewardjb5f6f512005-03-10 23:59:00 +0000832 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000833
njn4ba5a792002-09-30 10:23:54 +0000834 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000835
836 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
837
njn25e49d8e72002-09-23 09:36:25 +0000838 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000839
njn4ba5a792002-09-30 10:23:54 +0000840 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000841
sewardjb5f6f512005-03-10 23:59:00 +0000842 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000843
njn94065fd2004-11-22 19:26:27 +0000844 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000845 i = 0;
846 while (True) {
847 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000848 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000849 i++;
njn25e49d8e72002-09-23 09:36:25 +0000850 }
njnc40c3a82002-10-02 11:02:27 +0000851 buf[i] = '\0'; /* Replace ':', splitting into two strings */
852
nethercote7cc9c232004-01-21 15:08:04 +0000853 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000854 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000855
nethercote7cc9c232004-01-21 15:08:04 +0000856 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000857 {
sewardjb5f6f512005-03-10 23:59:00 +0000858 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000859 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000860 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000861 else
sewardjb5f6f512005-03-10 23:59:00 +0000862 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000863 }
njn95ec8702004-11-22 16:46:13 +0000864 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000865 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000866 {
sewardjb5f6f512005-03-10 23:59:00 +0000867 // A tool suppression
868 if (TL_(recognised_suppression)(supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000869 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000870 } else {
871 BOMB("unknown tool suppression type");
872 }
njnc40c3a82002-10-02 11:02:27 +0000873 }
njn25e49d8e72002-09-23 09:36:25 +0000874 else {
sewardjb5f6f512005-03-10 23:59:00 +0000875 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000876 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000877 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000878 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000879 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000880 break;
881 }
882 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000883 }
884
njn95ec8702004-11-22 16:46:13 +0000885 if (VG_(needs).tool_errors &&
sewardjb5f6f512005-03-10 23:59:00 +0000886 !TL_(read_extra_suppression_info)(fd, buf, N_BUF, supp))
887 {
888 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000889 }
890
sewardjb5f6f512005-03-10 23:59:00 +0000891 i = 0;
892 while (True) {
893 eof = VG_(get_line) ( fd, buf, N_BUF );
894 if (eof)
895 BOMB("unexpected end-of-file");
896 if (VG_STREQ(buf, "}")) {
897 if (i > 0) {
898 break;
899 } else {
900 BOMB("missing stack trace");
901 }
902 }
903 if (i == VG_MAX_SUPP_CALLERS)
904 BOMB("too many callers in stack trace");
905 if (i > 0 && i >= VG_(clo_backtrace_size))
906 break;
907 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
908 if (!setLocationTy(&(tmp_callers[i])))
909 BOMB("location should start with 'fun:' or 'obj:'");
910 i++;
911 }
912
913 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
914 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000915 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000916 do {
917 eof = VG_(get_line) ( fd, buf, N_BUF );
918 } while (!eof && !VG_STREQ(buf, "}"));
919 }
920
921 // Copy tmp_callers[] into supp->callers[]
922 supp->n_callers = i;
923 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
924 for (i = 0; i < supp->n_callers; i++) {
925 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +0000926 }
927
sewardjde4a1d02002-03-22 01:27:54 +0000928 supp->next = vg_suppressions;
929 vg_suppressions = supp;
930 }
sewardjde4a1d02002-03-22 01:27:54 +0000931 VG_(close)(fd);
932 return;
933
934 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +0000935 VG_(message)(Vg_UserMsg,
936 "FATAL: in suppressions file `%s': %s", filename, err_str );
937
sewardjde4a1d02002-03-22 01:27:54 +0000938 VG_(close)(fd);
939 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +0000940 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +0000941
sewardjb5f6f512005-03-10 23:59:00 +0000942# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +0000943# undef N_BUF
944}
945
946
947void VG_(load_suppressions) ( void )
948{
949 Int i;
950 vg_suppressions = NULL;
951 for (i = 0; i < VG_(clo_n_suppressions); i++) {
952 if (VG_(clo_verbosity) > 1) {
953 VG_(message)(Vg_UserMsg, "Reading suppressions file: %s",
954 VG_(clo_suppressions)[i] );
955 }
956 load_one_suppressions_file( VG_(clo_suppressions)[i] );
957 }
958}
959
sewardjb5f6f512005-03-10 23:59:00 +0000960static
njn810086f2002-11-14 12:42:47 +0000961Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000962{
njn810086f2002-11-14 12:42:47 +0000963 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000964 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +0000965 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +0000966 default:
njn95ec8702004-11-22 16:46:13 +0000967 if (VG_(needs).tool_errors) {
njn26f02512004-11-22 18:33:15 +0000968 return TL_(error_matches_suppression)(err, su);
njn25e49d8e72002-09-23 09:36:25 +0000969 } else {
970 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +0000971 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000972 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000973 err->ekind);
njn67993252004-11-22 18:02:32 +0000974 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000975 }
976 }
977}
978
sewardjb5f6f512005-03-10 23:59:00 +0000979static
980Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +0000981{
982 Int i;
sewardjb5f6f512005-03-10 23:59:00 +0000983 Char caller_name[M_VG_ERRTXT];
njn25e49d8e72002-09-23 09:36:25 +0000984
sewardjb5f6f512005-03-10 23:59:00 +0000985 for (i = 0; i < su->n_callers; i++) {
986 Addr a = err->where->ips[i];
987 vg_assert(su->callers[i].name != NULL);
988 switch (su->callers[i].ty) {
989 case ObjName:
njn58c9f812005-03-11 04:46:09 +0000990 if (!VG_(get_objname)(a, caller_name, M_VG_ERRTXT))
991 return False;
sewardjb5f6f512005-03-10 23:59:00 +0000992 break;
993
994 case FunName:
995 // Nb: mangled names used in suppressions
njn58c9f812005-03-11 04:46:09 +0000996 if (!VG_(get_fnname_nodemangle)(a, caller_name, M_VG_ERRTXT))
997 return False;
sewardjb5f6f512005-03-10 23:59:00 +0000998 break;
njn67993252004-11-22 18:02:32 +0000999 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001000 }
sewardjb5f6f512005-03-10 23:59:00 +00001001 if (!VG_(string_match)(su->callers[i].name, caller_name))
1002 return False;
njn25e49d8e72002-09-23 09:36:25 +00001003 }
1004
1005 /* If we reach here, it's a match */
1006 return True;
1007}
sewardjde4a1d02002-03-22 01:27:54 +00001008
njn810086f2002-11-14 12:42:47 +00001009/* Does an error context match a suppression? ie is this a suppressible
1010 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001011 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001012*/
njn810086f2002-11-14 12:42:47 +00001013static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001014{
njn810086f2002-11-14 12:42:47 +00001015 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001016
sewardjde4a1d02002-03-22 01:27:54 +00001017 /* See if the error context matches any suppression. */
1018 for (su = vg_suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001019 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001020 supp_matches_callers(err, su))
1021 {
njn25e49d8e72002-09-23 09:36:25 +00001022 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001023 }
sewardjde4a1d02002-03-22 01:27:54 +00001024 }
njn25e49d8e72002-09-23 09:36:25 +00001025 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001026}
1027
sewardjde4a1d02002-03-22 01:27:54 +00001028/*--------------------------------------------------------------------*/
sewardjb5f6f512005-03-10 23:59:00 +00001029/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001030/*--------------------------------------------------------------------*/