blob: 9a9c8fca03440bc63d11c8929456a304ecee7181 [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"
njnd01fef72005-03-25 23:35:48 +000032#include "pub_core_execontext.h"
sewardjde4a1d02002-03-22 01:27:54 +000033
34/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000035/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000036/*------------------------------------------------------------*/
37
njn14319cc2005-03-13 06:26:22 +000038/* After this many different unsuppressed errors have been observed,
39 be more conservative about collecting new ones. */
40#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
41
42/* After this many different unsuppressed errors have been observed,
43 stop collecting errors at all, and tell the user their program is
44 evidently a steaming pile of camel dung. */
45#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
46
47/* After this many total errors have been observed, stop collecting
48 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
49#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
50
sewardjde4a1d02002-03-22 01:27:54 +000051/* The list of error contexts found, both suppressed and unsuppressed.
52 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000053static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000054
55/* The list of suppression directives, as read from the specified
56 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000057static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000058
59/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000060static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000061
62/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000063static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000064
65/* forwards ... */
njn810086f2002-11-14 12:42:47 +000066static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000067
sewardjb5f6f512005-03-10 23:59:00 +000068static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000069
70/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000071/*--- Error type ---*/
72/*------------------------------------------------------------*/
73
nethercote996901a2004-08-03 13:29:09 +000074/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000075 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000076
77/* Errors. Extensible (via the 'extra' field). Tools can use a normal
78 enum (with element values in the normal range (0..)) for `ekind'.
79 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000080 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000081
82 When errors are found and recorded with VG_(maybe_record_error)(), all
83 the tool must do is pass in the four parameters; core will
84 allocate/initialise the error record.
85*/
86struct _Error {
87 struct _Error* next;
88 // NULL if unsuppressed; or ptr to suppression record.
89 Supp* supp;
90 Int count;
91 ThreadId tid;
92
93 // The tool-specific part
94 ExeContext* where; // Initialised by core
95 Int ekind; // Used by ALL. Must be in the range (0..)
96 Addr addr; // Used frequently
97 Char* string; // Used frequently
98 void* extra; // For any tool-specific extras
99};
100
101ExeContext* VG_(get_error_where) ( Error* err )
102{
103 return err->where;
104}
105
106ErrorKind VG_(get_error_kind) ( Error* err )
107{
108 return err->ekind;
109}
110
111Addr VG_(get_error_address) ( Error* err )
112{
113 return err->addr;
114}
115
116Char* VG_(get_error_string) ( Error* err )
117{
118 return err->string;
119}
120
121void* VG_(get_error_extra) ( Error* err )
122{
123 return err->extra;
124}
125
nethercotef2b11482004-08-02 12:36:01 +0000126UInt VG_(get_n_errs_found)( void )
127{
128 return n_errs_found;
129}
130
nethercote4a184902004-08-02 12:21:09 +0000131/*------------------------------------------------------------*/
132/*--- Suppression type ---*/
133/*------------------------------------------------------------*/
134
135/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
136 * effectively extend it by defining their own enums in the (0..) range. */
137typedef
138 enum {
139 PThreadSupp = -1, /* Matches PThreadErr */
140 }
141 CoreSuppKind;
142
sewardjb5f6f512005-03-10 23:59:00 +0000143/* Max number of callers for context in a suppression. */
144#define VG_MAX_SUPP_CALLERS 24
145
nethercote4a184902004-08-02 12:21:09 +0000146/* For each caller specified for a suppression, record the nature of
147 the caller name. Not of interest to tools. */
148typedef
149 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000150 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000151 ObjName, /* Name is of an shared object file. */
152 FunName /* Name is of a function. */
153 }
154 SuppLocTy;
155
sewardjb5f6f512005-03-10 23:59:00 +0000156typedef
157 struct {
158 SuppLocTy ty;
159 Char* name;
160 }
161 SuppLoc;
162
nethercote4a184902004-08-02 12:21:09 +0000163/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000164 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000165 Tools can use a normal enum (with element values in the normal range
166 (0..)) for `skind'. */
167struct _Supp {
168 struct _Supp* next;
169 Int count; // The number of times this error has been suppressed.
170 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000171
172 // Length of 'callers'
173 Int n_callers;
174 // Array of callers, for matching stack traces. First one (name of fn
175 // where err occurs) is mandatory; rest are optional.
176 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000177
178 /* The tool-specific part */
179 SuppKind skind; // What kind of suppression. Must use the range (0..).
180 Char* string; // String -- use is optional. NULL by default.
181 void* extra; // Anything else -- use is optional. NULL by default.
182};
183
184SuppKind VG_(get_supp_kind) ( Supp* su )
185{
186 return su->skind;
187}
188
189Char* VG_(get_supp_string) ( Supp* su )
190{
191 return su->string;
192}
193
194void* VG_(get_supp_extra) ( Supp* su )
195{
196 return su->extra;
197}
198
199
200void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
201{
202 su->skind = skind;
203}
204
205void VG_(set_supp_string) ( Supp* su, Char* string )
206{
207 su->string = string;
208}
209
210void VG_(set_supp_extra) ( Supp* su, void* extra )
211{
212 su->extra = extra;
213}
214
215
216/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000217/*--- Helper fns ---*/
218/*------------------------------------------------------------*/
219
sewardjde4a1d02002-03-22 01:27:54 +0000220/* Compare error contexts, to detect duplicates. Note that if they
221 are otherwise the same, the faulting addrs and associated rwoffsets
222 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000223static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000224{
njn810086f2002-11-14 12:42:47 +0000225 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000226 return False;
njn25e49d8e72002-09-23 09:36:25 +0000227 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000228 return False;
229
njn810086f2002-11-14 12:42:47 +0000230 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000231 // case ThreadErr:
232 // case MutexErr:
233 // vg_assert(VG_(needs).core_errors);
234 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000235 default:
njn95ec8702004-11-22 16:46:13 +0000236 if (VG_(needs).tool_errors)
njn26f02512004-11-22 18:33:15 +0000237 return TL_(eq_Error)(res, e1, e2);
njn25e49d8e72002-09-23 09:36:25 +0000238 else {
njn95ec8702004-11-22 16:46:13 +0000239 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000240 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000241 e1->ekind);
njn67993252004-11-22 18:02:32 +0000242 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000243 }
sewardjde4a1d02002-03-22 01:27:54 +0000244 }
245}
246
njn810086f2002-11-14 12:42:47 +0000247static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000248{
sewardjde4a1d02002-03-22 01:27:54 +0000249 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +0000250 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
sewardjb5f6f512005-03-10 23:59:00 +0000251 if (err->tid > 0 && err->tid != last_tid_printed) {
njn25e49d8e72002-09-23 09:36:25 +0000252 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
sewardjb5f6f512005-03-10 23:59:00 +0000253 last_tid_printed = err->tid;
254 }
njn25e49d8e72002-09-23 09:36:25 +0000255
njn810086f2002-11-14 12:42:47 +0000256 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000257 // case ThreadErr:
258 // case MutexErr:
259 // vg_assert(VG_(needs).core_errors);
260 // VG_(tm_error_print)(err);
261 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000262 default:
njn95ec8702004-11-22 16:46:13 +0000263 if (VG_(needs).tool_errors)
njn26f02512004-11-22 18:33:15 +0000264 TL_(pp_Error)( err );
njn25e49d8e72002-09-23 09:36:25 +0000265 else {
njn95ec8702004-11-22 16:46:13 +0000266 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000267 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000268 err->ekind);
njn67993252004-11-22 18:02:32 +0000269 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000270 }
sewardjde4a1d02002-03-22 01:27:54 +0000271 }
272}
273
nethercote04d0fbc2004-01-26 16:48:06 +0000274/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000275 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000276Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000277{
278 Char ch, ch2;
279 Int res;
280
njn43c799e2003-04-08 00:08:52 +0000281 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000282 return False;
283
284 VG_(message)(Vg_UserMsg, "");
285
286 again:
287 VG_(printf)(
288 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000289 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
290 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000291 );
292
sewardj6024b212003-07-13 10:54:33 +0000293 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000294 if (res != 1) goto ioerror;
295 /* res == 1 */
296 if (ch == '\n') return False;
297 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
298 && ch != 'C' && ch != 'c') goto again;
299
sewardj6024b212003-07-13 10:54:33 +0000300 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000301 if (res != 1) goto ioerror;
302 if (ch2 != '\n') goto again;
303
njn43c799e2003-04-08 00:08:52 +0000304 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000305 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000306 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000307 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000308 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000309 vg_assert(ch == 'c' || ch == 'C');
310
311 ioerror:
njn43c799e2003-04-08 00:08:52 +0000312 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000313 return False;
314}
315
316
sewardjb5f6f512005-03-10 23:59:00 +0000317/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000318static __inline__
njn72718642003-07-24 08:45:32 +0000319void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
320 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000321{
njnca82cc02004-11-22 17:18:48 +0000322 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000323
njn810086f2002-11-14 12:42:47 +0000324 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000325 err->next = NULL;
326 err->supp = NULL;
327 err->count = 1;
njn72718642003-07-24 08:45:32 +0000328 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000329 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000330 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000331 else
332 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000333
nethercote996901a2004-08-03 13:29:09 +0000334 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000335 err->ekind = ekind;
336 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000337 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000338 err->string = s;
339
njn25e49d8e72002-09-23 09:36:25 +0000340 /* sanity... */
njn72718642003-07-24 08:45:32 +0000341 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000342}
343
njnf4261312005-03-20 23:45:36 +0000344static void printSuppForIp(UInt n, Addr ip)
345{
njn47b209a2005-03-25 23:47:16 +0000346 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000347
njn47b209a2005-03-25 23:47:16 +0000348 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000349 VG_(printf)(" fun:%s\n", buf);
njn47b209a2005-03-25 23:47:16 +0000350 } else if ( VG_(get_objname)(ip, buf+7, VG_ERRTXT_LEN-7) ) {
njnf4261312005-03-20 23:45:36 +0000351 VG_(printf)(" obj:%s\n", buf);
352 } else {
353 VG_(printf)(" ???:??? "
354 "# unknown, suppression will not work, sorry\n");
355 }
356}
357
nethercote10d481a2004-01-25 20:33:53 +0000358static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000359{
njn43c799e2003-04-08 00:08:52 +0000360 ExeContext* ec = VG_(get_error_where)(err);
361 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000362
sewardjb5f6f512005-03-10 23:59:00 +0000363 /* At most VG_MAX_SUPP_CALLERS names */
364 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000365 vg_assert(stop_at > 0);
366
367 VG_(printf)("{\n");
368 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000369
sewardjb5f6f512005-03-10 23:59:00 +0000370 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000371 VG_(printf)(" core:PThread\n");
372
373 } else {
njn26f02512004-11-22 18:33:15 +0000374 Char* name = TL_(get_error_name)(err);
njn6a230532003-07-21 10:38:23 +0000375 if (NULL == name) {
376 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000377 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000378 return;
379 }
380 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn26f02512004-11-22 18:33:15 +0000381 TL_(print_extra_suppression_info)(err);
njn6a230532003-07-21 10:38:23 +0000382 }
njn43c799e2003-04-08 00:08:52 +0000383
njnf4261312005-03-20 23:45:36 +0000384 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000385 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000386
387 VG_(printf)("}\n");
388}
389
njnb4aee052003-04-15 14:09:58 +0000390static
nethercote04d0fbc2004-01-26 16:48:06 +0000391void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000392{
sewardjd153fae2005-01-10 17:24:47 +0000393 Bool still_noisy = True;
394
nethercote04d0fbc2004-01-26 16:48:06 +0000395 /* Perhaps we want a debugger attach at this point? */
396 if (allow_db_attach &&
397 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
njn3e884182003-04-15 13:03:23 +0000398 {
nethercote04d0fbc2004-01-26 16:48:06 +0000399 VG_(printf)("starting debugger\n");
400 VG_(start_debugger)( err->tid );
njn43c799e2003-04-08 00:08:52 +0000401 }
402 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000403 if (VG_(clo_gen_suppressions) == 2
404 || (VG_(clo_gen_suppressions) == 1
405 && VG_(is_action_requested)( "Print suppression",
406 &still_noisy ))
407 ) {
nethercote42602b12004-01-25 19:30:29 +0000408 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000409 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
410 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000411 }
412}
413
414/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
415 just for pretty printing purposes. */
416static Bool is_first_shown_context = True;
417
njn25e49d8e72002-09-23 09:36:25 +0000418/* Top-level entry point to the error management subsystem.
419 All detected errors are notified here; this routine decides if/when the
420 user should see the error. */
njn72718642003-07-24 08:45:32 +0000421void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000422 ErrorKind ekind, Addr a, Char* s, void* extra )
423{
njn810086f2002-11-14 12:42:47 +0000424 Error err;
425 Error* p;
426 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000427 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000428 VgRes exe_res = Vg_MedRes;
429 static Bool stopping_message = False;
430 static Bool slowdown_message = False;
431 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000432
njn14319cc2005-03-13 06:26:22 +0000433 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
434 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000435 have been found, just refuse to collect any more. This stops
436 the burden of the error-management system becoming excessive in
437 extremely buggy programs, although it does make it pretty
438 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000439 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000440 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000441 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000442 if (!stopping_message) {
443 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000444
njn695c16e2005-03-27 03:40:28 +0000445 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000446 VG_(message)(Vg_UserMsg,
447 "More than %d different errors detected. "
448 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000449 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000450 } else {
451 VG_(message)(Vg_UserMsg,
452 "More than %d total errors detected. "
453 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000454 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000455 }
456
sewardjde4a1d02002-03-22 01:27:54 +0000457 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000458 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000459 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000460 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000461 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000462 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000463 VG_(message)(Vg_UserMsg,
464 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000465 VG_(message)(Vg_UserMsg, "");
466 stopping_message = True;
467 }
468 return;
469 }
470
njn14319cc2005-03-13 06:26:22 +0000471 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000472 been found, be much more conservative about collecting new
473 ones. */
njn695c16e2005-03-27 03:40:28 +0000474 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000475 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000476 if (!slowdown_message) {
477 VG_(message)(Vg_UserMsg, "");
478 VG_(message)(Vg_UserMsg,
479 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000480 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000481 VG_(message)(Vg_UserMsg,
482 "will still be recorded, but in less detail than before.");
483 slowdown_message = True;
484 }
485 }
486
njn25e49d8e72002-09-23 09:36:25 +0000487 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000488 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000489
490 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000491 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000492 p_prev = NULL;
493 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000494 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000495 /* Found it. */
496 p->count++;
497 if (p->supp != NULL) {
498 /* Deal correctly with suppressed errors. */
499 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000500 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000501 } else {
nethercotef2b11482004-08-02 12:36:01 +0000502 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000503 }
504
505 /* Move p to the front of the list so that future searches
506 for it are faster. */
507 if (p_prev != NULL) {
508 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000509 p_prev->next = p->next;
510 p->next = errors;
511 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000512 }
sewardj7ebf7c32003-07-24 21:29:40 +0000513
sewardjde4a1d02002-03-22 01:27:54 +0000514 return;
515 }
516 p_prev = p;
517 p = p->next;
518 }
519
520 /* Didn't see it. Copy and add. */
521
njn43c799e2003-04-08 00:08:52 +0000522 /* OK, we're really going to collect it. The context is on the stack and
523 will disappear shortly, so we must copy it. First do the main
524 (non-`extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000525
njn26f02512004-11-22 18:33:15 +0000526 Then TL_(update_extra) can update the `extra' part. This is for when
njn43c799e2003-04-08 00:08:52 +0000527 there are more details to fill in which take time to work out but
528 don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000529 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000530 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000531
532 Then, if there is an `extra' part, copy it too, using the size that
njn26f02512004-11-22 18:33:15 +0000533 TL_(update_extra) returned. Also allow for people using the void*
njna70114c2003-08-19 16:14:42 +0000534 extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000535 */
536
537 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000538 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000539 *p = err;
njn43c799e2003-04-08 00:08:52 +0000540
sewardjb5f6f512005-03-10 23:59:00 +0000541 /* update `extra' */
542 switch (ekind) {
543 // case ThreadErr:
544 // case MutexErr:
545 // vg_assert(VG_(needs).core_errors);
546 // extra_size = VG_(tm_error_update_extra)(p);
547 // break;
548 default:
549 vg_assert(VG_(needs).tool_errors);
550 extra_size = TL_(update_extra)(p);
551 break;
552 }
njn43c799e2003-04-08 00:08:52 +0000553
sewardjb5f6f512005-03-10 23:59:00 +0000554 /* copy block pointed to by `extra', if there is one */
555 if (NULL != p->extra && 0 != extra_size) {
556 void* new_extra = VG_(malloc)(extra_size);
557 VG_(memcpy)(new_extra, p->extra, extra_size);
558 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000559 }
560
njn695c16e2005-03-27 03:40:28 +0000561 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000562 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000563 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000564 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000565 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000566 if (!is_first_shown_context)
567 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000568 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000569 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000570 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000571 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000572 } else {
nethercotef2b11482004-08-02 12:36:01 +0000573 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000574 p->supp->count++;
575 }
576}
577
njn43c799e2003-04-08 00:08:52 +0000578/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000579 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000580 guaranteed to only happen once. This avoids all the recording and
581 comparing stuff. But they can be suppressed; returns True if it is
njn47363ab2003-04-21 13:24:40 +0000582 suppressed. Bool `print_error' dictates whether to print the error.
nethercotef2b11482004-08-02 12:36:01 +0000583 Bool `count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000584*/
njn72718642003-07-24 08:45:32 +0000585Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000586 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000587 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000588{
589 Error err;
590
591 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000592 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000593
594 /* Unless it's suppressed, we're going to show it. Don't need to make
595 a copy, because it's only temporary anyway.
596
njn26f02512004-11-22 18:33:15 +0000597 Then update the `extra' part with TL_(update_extra), because that can
njn43c799e2003-04-08 00:08:52 +0000598 have an affect on whether it's suppressed. Ignore the size return
njn26f02512004-11-22 18:33:15 +0000599 value of TL_(update_extra), because we're not copying `extra'. */
600 (void)TL_(update_extra)(&err);
njn43c799e2003-04-08 00:08:52 +0000601
602 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000603 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000604 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000605
606 if (print_error) {
607 if (!is_first_shown_context)
608 VG_(message)(Vg_UserMsg, "");
609 pp_Error(&err, False);
610 is_first_shown_context = False;
611 }
nethercote04d0fbc2004-01-26 16:48:06 +0000612 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000613
614 return False;
615
616 } else {
nethercotef2b11482004-08-02 12:36:01 +0000617 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000618 return True;
619 }
620}
621
sewardjde4a1d02002-03-22 01:27:54 +0000622
sewardjde4a1d02002-03-22 01:27:54 +0000623/*------------------------------------------------------------*/
624/*--- Exported fns ---*/
625/*------------------------------------------------------------*/
626
njnb9ecfe32005-03-13 05:27:57 +0000627/* This is called not from generated code but from the scheduler */
sewardjde4a1d02002-03-22 01:27:54 +0000628void VG_(show_all_errors) ( void )
629{
njn810086f2002-11-14 12:42:47 +0000630 Int i, n_min;
631 Int n_err_contexts, n_supp_contexts;
632 Error *p, *p_min;
633 Supp *su;
634 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000635
636 if (VG_(clo_verbosity) == 0)
637 return;
638
639 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000640 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000641 if (p->supp == NULL)
642 n_err_contexts++;
643 }
644
645 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000646 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000647 if (su->count > 0)
648 n_supp_contexts++;
649 }
sewardjde4a1d02002-03-22 01:27:54 +0000650 VG_(message)(Vg_UserMsg,
651 "ERROR SUMMARY: "
652 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000653 n_errs_found, n_err_contexts,
654 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000655
656 if (VG_(clo_verbosity) <= 1)
657 return;
658
659 /* Print the contexts in order of increasing error count. */
660 for (i = 0; i < n_err_contexts; i++) {
661 n_min = (1 << 30) - 1;
662 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000663 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000664 if (p->supp != NULL) continue;
665 if (p->count < n_min) {
666 n_min = p->count;
667 p_min = p;
668 }
669 }
njn67993252004-11-22 18:02:32 +0000670 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000671
672 VG_(message)(Vg_UserMsg, "");
673 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
674 p_min->count,
675 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000676 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000677
678 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000679 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000680 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000681 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000682 }
683
684 p_min->count = 1 << 30;
685 }
686
687 if (n_supp_contexts > 0)
688 VG_(message)(Vg_DebugMsg, "");
689 any_supp = False;
njn695c16e2005-03-27 03:40:28 +0000690 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000691 if (su->count > 0) {
692 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000693 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000694 }
695 }
696
697 if (n_err_contexts > 0) {
698 if (any_supp)
699 VG_(message)(Vg_UserMsg, "");
700 VG_(message)(Vg_UserMsg,
701 "IN SUMMARY: "
702 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000703 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000704 n_supp_contexts );
705 VG_(message)(Vg_UserMsg, "");
706 }
707}
708
709/*------------------------------------------------------------*/
710/*--- Standard suppressions ---*/
711/*------------------------------------------------------------*/
712
713/* Get a non-blank, non-comment line of at most nBuf chars from fd.
714 Skips leading spaces on the line. Return True if EOF was hit instead.
715*/
njn4ba5a792002-09-30 10:23:54 +0000716Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000717{
718 Char ch;
719 Int n, i;
720 while (True) {
721 /* First, read until a non-blank char appears. */
722 while (True) {
723 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000724 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000725 if (n == 0) return True;
726 }
727
728 /* Now, read the line into buf. */
729 i = 0;
730 buf[i++] = ch; buf[i] = 0;
731 while (True) {
732 n = VG_(read)(fd, &ch, 1);
733 if (n == 0) return False; /* the next call will return True */
734 if (ch == '\n') break;
735 if (i > 0 && i == nBuf-1) i--;
736 buf[i++] = ch; buf[i] = 0;
737 }
njn0c0f32a2005-03-26 04:14:01 +0000738 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000739 i--; buf[i] = 0;
740 };
741
742 /* VG_(printf)("The line is `%s'\n", buf); */
743 /* Ok, we have a line. If a non-comment line, return.
744 If a comment line, start all over again. */
745 if (buf[0] != '#') return False;
746 }
747}
748
749
750/* *p_caller contains the raw name of a caller, supposedly either
751 fun:some_function_name or
752 obj:some_object_name.
753 Set *p_ty accordingly and advance *p_caller over the descriptor
754 (fun: or obj:) part.
755 Returns False if failed.
756*/
sewardjb5f6f512005-03-10 23:59:00 +0000757static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000758{
sewardjb5f6f512005-03-10 23:59:00 +0000759 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
760 p->name += 4;
761 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000762 return True;
763 }
sewardjb5f6f512005-03-10 23:59:00 +0000764 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
765 p->name += 4;
766 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000767 return True;
768 }
769 VG_(printf)("location should start with fun: or obj:\n");
770 return False;
771}
772
773
nethercote7cc9c232004-01-21 15:08:04 +0000774/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000775static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000776Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000777{
778 Bool found;
779 Char *s = NULL; /* Shut gcc up */
780 Int len = VG_(strlen)(name);
781
782 found = (NULL != (s = VG_(strstr)(names, name)) &&
783 (s == names || *(s-1) == ',') &&
784 (*(s+len) == ',' || *(s+len) == '\0')
785 );
786
787 return found;
788}
789
njn695c16e2005-03-27 03:40:28 +0000790/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000791 and place them in the suppressions list. If there's any difficulty
792 doing this, just give up -- there's no point in trying to recover.
793*/
sewardjde4a1d02002-03-22 01:27:54 +0000794static void load_one_suppressions_file ( Char* filename )
795{
796# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000797 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000798 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000799 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000800 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000801 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000802 Char* err_str = NULL;
803 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000804
njn25e49d8e72002-09-23 09:36:25 +0000805 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000806 if (fd < 0) {
njn25e49d8e72002-09-23 09:36:25 +0000807 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file `%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000808 filename );
809 VG_(exit)(1);
810 }
811
sewardjb5f6f512005-03-10 23:59:00 +0000812#define BOMB(S) { err_str = S; goto syntax_error; }
813
sewardjde4a1d02002-03-22 01:27:54 +0000814 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000815 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000816 Supp* supp;
817 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000818 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000819
820 // Initialise temporary reading-in buffer.
821 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
822 tmp_callers[i].ty = NoName;
823 tmp_callers[i].name = NULL;
824 }
825
njn810086f2002-11-14 12:42:47 +0000826 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000827
njn4ba5a792002-09-30 10:23:54 +0000828 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000829 if (eof) break;
830
sewardjb5f6f512005-03-10 23:59:00 +0000831 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000832
njn4ba5a792002-09-30 10:23:54 +0000833 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000834
835 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
836
njn25e49d8e72002-09-23 09:36:25 +0000837 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000838
njn4ba5a792002-09-30 10:23:54 +0000839 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000840
sewardjb5f6f512005-03-10 23:59:00 +0000841 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000842
njn94065fd2004-11-22 19:26:27 +0000843 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000844 i = 0;
845 while (True) {
846 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000847 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000848 i++;
njn25e49d8e72002-09-23 09:36:25 +0000849 }
njnc40c3a82002-10-02 11:02:27 +0000850 buf[i] = '\0'; /* Replace ':', splitting into two strings */
851
nethercote7cc9c232004-01-21 15:08:04 +0000852 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000853 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000854
nethercote7cc9c232004-01-21 15:08:04 +0000855 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000856 {
sewardjb5f6f512005-03-10 23:59:00 +0000857 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000858 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000859 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000860 else
sewardjb5f6f512005-03-10 23:59:00 +0000861 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000862 }
njn95ec8702004-11-22 16:46:13 +0000863 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000864 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000865 {
sewardjb5f6f512005-03-10 23:59:00 +0000866 // A tool suppression
867 if (TL_(recognised_suppression)(supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000868 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000869 } else {
870 BOMB("unknown tool suppression type");
871 }
njnc40c3a82002-10-02 11:02:27 +0000872 }
njn25e49d8e72002-09-23 09:36:25 +0000873 else {
sewardjb5f6f512005-03-10 23:59:00 +0000874 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000875 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000876 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000877 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000878 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000879 break;
880 }
881 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000882 }
883
njn95ec8702004-11-22 16:46:13 +0000884 if (VG_(needs).tool_errors &&
sewardjb5f6f512005-03-10 23:59:00 +0000885 !TL_(read_extra_suppression_info)(fd, buf, N_BUF, supp))
886 {
887 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000888 }
889
sewardjb5f6f512005-03-10 23:59:00 +0000890 i = 0;
891 while (True) {
892 eof = VG_(get_line) ( fd, buf, N_BUF );
893 if (eof)
894 BOMB("unexpected end-of-file");
895 if (VG_STREQ(buf, "}")) {
896 if (i > 0) {
897 break;
898 } else {
899 BOMB("missing stack trace");
900 }
901 }
902 if (i == VG_MAX_SUPP_CALLERS)
903 BOMB("too many callers in stack trace");
904 if (i > 0 && i >= VG_(clo_backtrace_size))
905 break;
906 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
907 if (!setLocationTy(&(tmp_callers[i])))
908 BOMB("location should start with 'fun:' or 'obj:'");
909 i++;
910 }
911
912 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
913 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000914 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000915 do {
916 eof = VG_(get_line) ( fd, buf, N_BUF );
917 } while (!eof && !VG_STREQ(buf, "}"));
918 }
919
920 // Copy tmp_callers[] into supp->callers[]
921 supp->n_callers = i;
922 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
923 for (i = 0; i < supp->n_callers; i++) {
924 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +0000925 }
926
njn695c16e2005-03-27 03:40:28 +0000927 supp->next = suppressions;
928 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +0000929 }
sewardjde4a1d02002-03-22 01:27:54 +0000930 VG_(close)(fd);
931 return;
932
933 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +0000934 VG_(message)(Vg_UserMsg,
935 "FATAL: in suppressions file `%s': %s", filename, err_str );
936
sewardjde4a1d02002-03-22 01:27:54 +0000937 VG_(close)(fd);
938 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +0000939 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +0000940
sewardjb5f6f512005-03-10 23:59:00 +0000941# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +0000942# undef N_BUF
943}
944
945
946void VG_(load_suppressions) ( void )
947{
948 Int i;
njn695c16e2005-03-27 03:40:28 +0000949 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000950 for (i = 0; i < VG_(clo_n_suppressions); i++) {
951 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +0000952 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
953 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +0000954 }
955 load_one_suppressions_file( VG_(clo_suppressions)[i] );
956 }
957}
958
sewardjb5f6f512005-03-10 23:59:00 +0000959static
njn810086f2002-11-14 12:42:47 +0000960Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000961{
njn810086f2002-11-14 12:42:47 +0000962 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000963 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +0000964 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +0000965 default:
njn95ec8702004-11-22 16:46:13 +0000966 if (VG_(needs).tool_errors) {
njn26f02512004-11-22 18:33:15 +0000967 return TL_(error_matches_suppression)(err, su);
njn25e49d8e72002-09-23 09:36:25 +0000968 } else {
969 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +0000970 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000971 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000972 err->ekind);
njn67993252004-11-22 18:02:32 +0000973 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000974 }
975 }
976}
977
sewardjb5f6f512005-03-10 23:59:00 +0000978static
979Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +0000980{
981 Int i;
njn47b209a2005-03-25 23:47:16 +0000982 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +0000983 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +0000984
sewardjb5f6f512005-03-10 23:59:00 +0000985 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +0000986 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +0000987 vg_assert(su->callers[i].name != NULL);
988 switch (su->callers[i].ty) {
989 case ObjName:
njn47b209a2005-03-25 23:47:16 +0000990 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +0000991 return False;
sewardjb5f6f512005-03-10 23:59:00 +0000992 break;
993
994 case FunName:
995 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +0000996 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +0000997 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. */
njn695c16e2005-03-27 03:40:28 +00001018 for (su = 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/*--------------------------------------------------------------------*/