blob: df05c20e26f8e0f1c36f28af90fa1b98ca2f49f4 [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{
sewardjde4a1d02002-03-22 01:27:54 +0000254 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +0000255 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
sewardjb5f6f512005-03-10 23:59:00 +0000256 if (err->tid > 0 && err->tid != last_tid_printed) {
njn25e49d8e72002-09-23 09:36:25 +0000257 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
sewardjb5f6f512005-03-10 23:59:00 +0000258 last_tid_printed = err->tid;
259 }
njn25e49d8e72002-09-23 09:36:25 +0000260
njn810086f2002-11-14 12:42:47 +0000261 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000262 // case ThreadErr:
263 // case MutexErr:
264 // vg_assert(VG_(needs).core_errors);
265 // VG_(tm_error_print)(err);
266 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000267 default:
njn95ec8702004-11-22 16:46:13 +0000268 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000269 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000270 else {
njn95ec8702004-11-22 16:46:13 +0000271 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000272 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000273 err->ekind);
njn67993252004-11-22 18:02:32 +0000274 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000275 }
sewardjde4a1d02002-03-22 01:27:54 +0000276 }
277}
278
nethercote04d0fbc2004-01-26 16:48:06 +0000279/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000280 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000281Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000282{
283 Char ch, ch2;
284 Int res;
285
njn43c799e2003-04-08 00:08:52 +0000286 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000287 return False;
288
289 VG_(message)(Vg_UserMsg, "");
290
291 again:
292 VG_(printf)(
293 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000294 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
295 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000296 );
297
sewardj6024b212003-07-13 10:54:33 +0000298 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000299 if (res != 1) goto ioerror;
300 /* res == 1 */
301 if (ch == '\n') return False;
302 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
303 && ch != 'C' && ch != 'c') goto again;
304
sewardj6024b212003-07-13 10:54:33 +0000305 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000306 if (res != 1) goto ioerror;
307 if (ch2 != '\n') goto again;
308
njn43c799e2003-04-08 00:08:52 +0000309 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000310 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000311 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000312 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000313 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000314 vg_assert(ch == 'c' || ch == 'C');
315
316 ioerror:
njn43c799e2003-04-08 00:08:52 +0000317 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000318 return False;
319}
320
321
sewardjb5f6f512005-03-10 23:59:00 +0000322/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000323static __inline__
njn72718642003-07-24 08:45:32 +0000324void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
325 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000326{
njnca82cc02004-11-22 17:18:48 +0000327 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000328
njn810086f2002-11-14 12:42:47 +0000329 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000330 err->next = NULL;
331 err->supp = NULL;
332 err->count = 1;
njn72718642003-07-24 08:45:32 +0000333 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000334 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000335 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000336 else
337 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000338
nethercote996901a2004-08-03 13:29:09 +0000339 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000340 err->ekind = ekind;
341 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000342 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000343 err->string = s;
344
njn25e49d8e72002-09-23 09:36:25 +0000345 /* sanity... */
njn72718642003-07-24 08:45:32 +0000346 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000347}
348
njnf4261312005-03-20 23:45:36 +0000349static void printSuppForIp(UInt n, Addr ip)
350{
njn47b209a2005-03-25 23:47:16 +0000351 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000352
njn47b209a2005-03-25 23:47:16 +0000353 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000354 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000355 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000356 VG_(printf)(" obj:%s\n", buf);
357 } else {
358 VG_(printf)(" ???:??? "
359 "# unknown, suppression will not work, sorry\n");
360 }
361}
362
nethercote10d481a2004-01-25 20:33:53 +0000363static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000364{
njn43c799e2003-04-08 00:08:52 +0000365 ExeContext* ec = VG_(get_error_where)(err);
366 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000367
sewardjb5f6f512005-03-10 23:59:00 +0000368 /* At most VG_MAX_SUPP_CALLERS names */
369 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000370 vg_assert(stop_at > 0);
371
372 VG_(printf)("{\n");
373 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000374
sewardjb5f6f512005-03-10 23:59:00 +0000375 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000376 VG_(printf)(" core:PThread\n");
377
378 } else {
njn51d827b2005-05-09 01:02:08 +0000379 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000380 if (NULL == name) {
381 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000382 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000383 return;
384 }
385 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000386 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000387 }
njn43c799e2003-04-08 00:08:52 +0000388
njnf4261312005-03-20 23:45:36 +0000389 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000390 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000391
392 VG_(printf)("}\n");
393}
394
njnb4aee052003-04-15 14:09:58 +0000395static
nethercote04d0fbc2004-01-26 16:48:06 +0000396void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000397{
sewardjd153fae2005-01-10 17:24:47 +0000398 Bool still_noisy = True;
399
nethercote04d0fbc2004-01-26 16:48:06 +0000400 /* Perhaps we want a debugger attach at this point? */
401 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000402 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
403 {
nethercote04d0fbc2004-01-26 16:48:06 +0000404 VG_(printf)("starting debugger\n");
405 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000406 }
njn43c799e2003-04-08 00:08:52 +0000407 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000408 if (VG_(clo_gen_suppressions) == 2
409 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000410 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000411 ) {
nethercote42602b12004-01-25 19:30:29 +0000412 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000413 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
414 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000415 }
416}
417
418/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
419 just for pretty printing purposes. */
420static Bool is_first_shown_context = True;
421
njn25e49d8e72002-09-23 09:36:25 +0000422/* Top-level entry point to the error management subsystem.
423 All detected errors are notified here; this routine decides if/when the
424 user should see the error. */
njn72718642003-07-24 08:45:32 +0000425void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000426 ErrorKind ekind, Addr a, Char* s, void* extra )
427{
njn810086f2002-11-14 12:42:47 +0000428 Error err;
429 Error* p;
430 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000431 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000432 VgRes exe_res = Vg_MedRes;
433 static Bool stopping_message = False;
434 static Bool slowdown_message = False;
435 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000436
njn14319cc2005-03-13 06:26:22 +0000437 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
438 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000439 have been found, just refuse to collect any more. This stops
440 the burden of the error-management system becoming excessive in
441 extremely buggy programs, although it does make it pretty
442 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000443 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000444 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000445 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000446 if (!stopping_message) {
447 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000448
njn695c16e2005-03-27 03:40:28 +0000449 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000450 VG_(message)(Vg_UserMsg,
451 "More than %d different errors detected. "
452 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000453 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000454 } else {
455 VG_(message)(Vg_UserMsg,
456 "More than %d total errors detected. "
457 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000458 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000459 }
460
sewardjde4a1d02002-03-22 01:27:54 +0000461 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000462 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000463 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000464 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000465 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000466 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000467 VG_(message)(Vg_UserMsg,
468 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000469 VG_(message)(Vg_UserMsg, "");
470 stopping_message = True;
471 }
472 return;
473 }
474
njn14319cc2005-03-13 06:26:22 +0000475 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000476 been found, be much more conservative about collecting new
477 ones. */
njn695c16e2005-03-27 03:40:28 +0000478 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000479 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000480 if (!slowdown_message) {
481 VG_(message)(Vg_UserMsg, "");
482 VG_(message)(Vg_UserMsg,
483 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000484 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000485 VG_(message)(Vg_UserMsg,
486 "will still be recorded, but in less detail than before.");
487 slowdown_message = True;
488 }
489 }
490
njn25e49d8e72002-09-23 09:36:25 +0000491 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000492 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000493
494 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000495 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000496 p_prev = NULL;
497 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000498 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000499 /* Found it. */
500 p->count++;
501 if (p->supp != NULL) {
502 /* Deal correctly with suppressed errors. */
503 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000504 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000505 } else {
nethercotef2b11482004-08-02 12:36:01 +0000506 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000507 }
508
509 /* Move p to the front of the list so that future searches
510 for it are faster. */
511 if (p_prev != NULL) {
512 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000513 p_prev->next = p->next;
514 p->next = errors;
515 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000516 }
sewardj7ebf7c32003-07-24 21:29:40 +0000517
sewardjde4a1d02002-03-22 01:27:54 +0000518 return;
519 }
520 p_prev = p;
521 p = p->next;
522 }
523
524 /* Didn't see it. Copy and add. */
525
njn43c799e2003-04-08 00:08:52 +0000526 /* OK, we're really going to collect it. The context is on the stack and
527 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000528 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000529
njn02bc4b82005-05-15 17:28:26 +0000530 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000531 is for when there are more details to fill in which take time to work
532 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000533 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000534 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000535
njn02bc4b82005-05-15 17:28:26 +0000536 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000537 VG_(tdict).tool_update_extra returned. Also allow for people using
538 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000539 */
540
541 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000542 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000543 *p = err;
njn43c799e2003-04-08 00:08:52 +0000544
njn02bc4b82005-05-15 17:28:26 +0000545 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000546 switch (ekind) {
547 // case ThreadErr:
548 // case MutexErr:
549 // vg_assert(VG_(needs).core_errors);
550 // extra_size = VG_(tm_error_update_extra)(p);
551 // break;
552 default:
553 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000554 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000555 break;
556 }
njn43c799e2003-04-08 00:08:52 +0000557
njn02bc4b82005-05-15 17:28:26 +0000558 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000559 if (NULL != p->extra && 0 != extra_size) {
560 void* new_extra = VG_(malloc)(extra_size);
561 VG_(memcpy)(new_extra, p->extra, extra_size);
562 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000563 }
564
njn695c16e2005-03-27 03:40:28 +0000565 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000566 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000567 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000568 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000569 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000570 if (!is_first_shown_context)
571 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000572 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000573 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000574 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000575 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000576 } else {
nethercotef2b11482004-08-02 12:36:01 +0000577 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000578 p->supp->count++;
579 }
580}
581
njn43c799e2003-04-08 00:08:52 +0000582/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000583 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000584 guaranteed to only happen once. This avoids all the recording and
585 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000586 suppressed. Bool 'print_error' dictates whether to print the error.
587 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000588*/
njn72718642003-07-24 08:45:32 +0000589Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000590 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000591 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000592{
593 Error err;
594
595 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000596 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000597
598 /* Unless it's suppressed, we're going to show it. Don't need to make
599 a copy, because it's only temporary anyway.
600
njn02bc4b82005-05-15 17:28:26 +0000601 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000602 because that can have an affect on whether it's suppressed. Ignore
603 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000604 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000605 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000606
607 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000608 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000609 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000610
611 if (print_error) {
612 if (!is_first_shown_context)
613 VG_(message)(Vg_UserMsg, "");
614 pp_Error(&err, False);
615 is_first_shown_context = False;
616 }
nethercote04d0fbc2004-01-26 16:48:06 +0000617 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000618
619 return False;
620
621 } else {
nethercotef2b11482004-08-02 12:36:01 +0000622 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000623 return True;
624 }
625}
626
sewardjde4a1d02002-03-22 01:27:54 +0000627
sewardjde4a1d02002-03-22 01:27:54 +0000628/*------------------------------------------------------------*/
629/*--- Exported fns ---*/
630/*------------------------------------------------------------*/
631
njnb9ecfe32005-03-13 05:27:57 +0000632/* This is called not from generated code but from the scheduler */
sewardjde4a1d02002-03-22 01:27:54 +0000633void VG_(show_all_errors) ( void )
634{
njn810086f2002-11-14 12:42:47 +0000635 Int i, n_min;
636 Int n_err_contexts, n_supp_contexts;
637 Error *p, *p_min;
638 Supp *su;
639 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000640
641 if (VG_(clo_verbosity) == 0)
642 return;
643
644 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000645 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000646 if (p->supp == NULL)
647 n_err_contexts++;
648 }
649
650 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000651 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000652 if (su->count > 0)
653 n_supp_contexts++;
654 }
sewardjde4a1d02002-03-22 01:27:54 +0000655 VG_(message)(Vg_UserMsg,
656 "ERROR SUMMARY: "
657 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000658 n_errs_found, n_err_contexts,
659 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000660
661 if (VG_(clo_verbosity) <= 1)
662 return;
663
664 /* Print the contexts in order of increasing error count. */
665 for (i = 0; i < n_err_contexts; i++) {
666 n_min = (1 << 30) - 1;
667 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000668 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000669 if (p->supp != NULL) continue;
670 if (p->count < n_min) {
671 n_min = p->count;
672 p_min = p;
673 }
674 }
njn67993252004-11-22 18:02:32 +0000675 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000676
677 VG_(message)(Vg_UserMsg, "");
678 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
679 p_min->count,
680 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000681 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000682
683 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000684 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000685 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000686 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000687 }
688
689 p_min->count = 1 << 30;
690 }
691
692 if (n_supp_contexts > 0)
693 VG_(message)(Vg_DebugMsg, "");
694 any_supp = False;
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 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000698 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000699 }
700 }
701
702 if (n_err_contexts > 0) {
703 if (any_supp)
704 VG_(message)(Vg_UserMsg, "");
705 VG_(message)(Vg_UserMsg,
706 "IN SUMMARY: "
707 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000708 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000709 n_supp_contexts );
710 VG_(message)(Vg_UserMsg, "");
711 }
712}
713
714/*------------------------------------------------------------*/
715/*--- Standard suppressions ---*/
716/*------------------------------------------------------------*/
717
718/* Get a non-blank, non-comment line of at most nBuf chars from fd.
719 Skips leading spaces on the line. Return True if EOF was hit instead.
720*/
njn4ba5a792002-09-30 10:23:54 +0000721Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000722{
723 Char ch;
724 Int n, i;
725 while (True) {
726 /* First, read until a non-blank char appears. */
727 while (True) {
728 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000729 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000730 if (n == 0) return True;
731 }
732
733 /* Now, read the line into buf. */
734 i = 0;
735 buf[i++] = ch; buf[i] = 0;
736 while (True) {
737 n = VG_(read)(fd, &ch, 1);
738 if (n == 0) return False; /* the next call will return True */
739 if (ch == '\n') break;
740 if (i > 0 && i == nBuf-1) i--;
741 buf[i++] = ch; buf[i] = 0;
742 }
njn0c0f32a2005-03-26 04:14:01 +0000743 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000744 i--; buf[i] = 0;
745 };
746
njn02bc4b82005-05-15 17:28:26 +0000747 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000748 /* Ok, we have a line. If a non-comment line, return.
749 If a comment line, start all over again. */
750 if (buf[0] != '#') return False;
751 }
752}
753
754
755/* *p_caller contains the raw name of a caller, supposedly either
756 fun:some_function_name or
757 obj:some_object_name.
758 Set *p_ty accordingly and advance *p_caller over the descriptor
759 (fun: or obj:) part.
760 Returns False if failed.
761*/
sewardjb5f6f512005-03-10 23:59:00 +0000762static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000763{
sewardjb5f6f512005-03-10 23:59:00 +0000764 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
765 p->name += 4;
766 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000767 return True;
768 }
sewardjb5f6f512005-03-10 23:59:00 +0000769 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
770 p->name += 4;
771 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000772 return True;
773 }
774 VG_(printf)("location should start with fun: or obj:\n");
775 return False;
776}
777
778
nethercote7cc9c232004-01-21 15:08:04 +0000779/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000780static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000781Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000782{
783 Bool found;
784 Char *s = NULL; /* Shut gcc up */
785 Int len = VG_(strlen)(name);
786
787 found = (NULL != (s = VG_(strstr)(names, name)) &&
788 (s == names || *(s-1) == ',') &&
789 (*(s+len) == ',' || *(s+len) == '\0')
790 );
791
792 return found;
793}
794
njn695c16e2005-03-27 03:40:28 +0000795/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000796 and place them in the suppressions list. If there's any difficulty
797 doing this, just give up -- there's no point in trying to recover.
798*/
sewardjde4a1d02002-03-22 01:27:54 +0000799static void load_one_suppressions_file ( Char* filename )
800{
801# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000802 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000803 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000804 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000805 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000806 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000807 Char* err_str = NULL;
808 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000809
njn25e49d8e72002-09-23 09:36:25 +0000810 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000811 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000812 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000813 filename );
814 VG_(exit)(1);
815 }
816
sewardjb5f6f512005-03-10 23:59:00 +0000817#define BOMB(S) { err_str = S; goto syntax_error; }
818
sewardjde4a1d02002-03-22 01:27:54 +0000819 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000820 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000821 Supp* supp;
822 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000823 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000824
825 // Initialise temporary reading-in buffer.
826 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
827 tmp_callers[i].ty = NoName;
828 tmp_callers[i].name = NULL;
829 }
830
njn810086f2002-11-14 12:42:47 +0000831 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000832
njn4ba5a792002-09-30 10:23:54 +0000833 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000834 if (eof) break;
835
sewardjb5f6f512005-03-10 23:59:00 +0000836 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000837
njn4ba5a792002-09-30 10:23:54 +0000838 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000839
840 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
841
njn25e49d8e72002-09-23 09:36:25 +0000842 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000843
njn4ba5a792002-09-30 10:23:54 +0000844 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000845
sewardjb5f6f512005-03-10 23:59:00 +0000846 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000847
njn94065fd2004-11-22 19:26:27 +0000848 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000849 i = 0;
850 while (True) {
851 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000852 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000853 i++;
njn25e49d8e72002-09-23 09:36:25 +0000854 }
njnc40c3a82002-10-02 11:02:27 +0000855 buf[i] = '\0'; /* Replace ':', splitting into two strings */
856
nethercote7cc9c232004-01-21 15:08:04 +0000857 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000858 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000859
nethercote7cc9c232004-01-21 15:08:04 +0000860 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000861 {
sewardjb5f6f512005-03-10 23:59:00 +0000862 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000863 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000864 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000865 else
sewardjb5f6f512005-03-10 23:59:00 +0000866 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000867 }
njn95ec8702004-11-22 16:46:13 +0000868 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000869 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000870 {
sewardjb5f6f512005-03-10 23:59:00 +0000871 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000872 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000873 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000874 } else {
875 BOMB("unknown tool suppression type");
876 }
njnc40c3a82002-10-02 11:02:27 +0000877 }
njn25e49d8e72002-09-23 09:36:25 +0000878 else {
sewardjb5f6f512005-03-10 23:59:00 +0000879 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000880 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000881 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000882 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000883 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000884 break;
885 }
886 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000887 }
888
njn95ec8702004-11-22 16:46:13 +0000889 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000890 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000891 {
892 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000893 }
894
sewardjb5f6f512005-03-10 23:59:00 +0000895 i = 0;
896 while (True) {
897 eof = VG_(get_line) ( fd, buf, N_BUF );
898 if (eof)
899 BOMB("unexpected end-of-file");
900 if (VG_STREQ(buf, "}")) {
901 if (i > 0) {
902 break;
903 } else {
904 BOMB("missing stack trace");
905 }
906 }
907 if (i == VG_MAX_SUPP_CALLERS)
908 BOMB("too many callers in stack trace");
909 if (i > 0 && i >= VG_(clo_backtrace_size))
910 break;
911 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
912 if (!setLocationTy(&(tmp_callers[i])))
913 BOMB("location should start with 'fun:' or 'obj:'");
914 i++;
915 }
916
917 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
918 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000919 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000920 do {
921 eof = VG_(get_line) ( fd, buf, N_BUF );
922 } while (!eof && !VG_STREQ(buf, "}"));
923 }
924
925 // Copy tmp_callers[] into supp->callers[]
926 supp->n_callers = i;
927 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
928 for (i = 0; i < supp->n_callers; i++) {
929 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +0000930 }
931
njn695c16e2005-03-27 03:40:28 +0000932 supp->next = suppressions;
933 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +0000934 }
sewardjde4a1d02002-03-22 01:27:54 +0000935 VG_(close)(fd);
936 return;
937
938 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +0000939 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000940 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +0000941
sewardjde4a1d02002-03-22 01:27:54 +0000942 VG_(close)(fd);
943 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +0000944 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +0000945
sewardjb5f6f512005-03-10 23:59:00 +0000946# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +0000947# undef N_BUF
948}
949
950
951void VG_(load_suppressions) ( void )
952{
953 Int i;
njn695c16e2005-03-27 03:40:28 +0000954 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000955 for (i = 0; i < VG_(clo_n_suppressions); i++) {
956 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +0000957 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
958 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +0000959 }
960 load_one_suppressions_file( VG_(clo_suppressions)[i] );
961 }
962}
963
sewardjb5f6f512005-03-10 23:59:00 +0000964static
njn810086f2002-11-14 12:42:47 +0000965Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000966{
njn810086f2002-11-14 12:42:47 +0000967 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000968 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +0000969 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +0000970 default:
njn95ec8702004-11-22 16:46:13 +0000971 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +0000972 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +0000973 } else {
974 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +0000975 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000976 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000977 err->ekind);
njn67993252004-11-22 18:02:32 +0000978 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000979 }
980 }
981}
982
sewardjb5f6f512005-03-10 23:59:00 +0000983static
984Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +0000985{
986 Int i;
njn47b209a2005-03-25 23:47:16 +0000987 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +0000988 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +0000989
sewardjb5f6f512005-03-10 23:59:00 +0000990 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +0000991 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +0000992 vg_assert(su->callers[i].name != NULL);
993 switch (su->callers[i].ty) {
994 case ObjName:
njn47b209a2005-03-25 23:47:16 +0000995 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +0000996 return False;
sewardjb5f6f512005-03-10 23:59:00 +0000997 break;
998
999 case FunName:
1000 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001001 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001002 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001003 break;
njn67993252004-11-22 18:02:32 +00001004 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001005 }
sewardjb5f6f512005-03-10 23:59:00 +00001006 if (!VG_(string_match)(su->callers[i].name, caller_name))
1007 return False;
njn25e49d8e72002-09-23 09:36:25 +00001008 }
1009
1010 /* If we reach here, it's a match */
1011 return True;
1012}
sewardjde4a1d02002-03-22 01:27:54 +00001013
njn810086f2002-11-14 12:42:47 +00001014/* Does an error context match a suppression? ie is this a suppressible
1015 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001016 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001017*/
njn810086f2002-11-14 12:42:47 +00001018static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001019{
njn810086f2002-11-14 12:42:47 +00001020 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001021
sewardjde4a1d02002-03-22 01:27:54 +00001022 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001023 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001024 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001025 supp_matches_callers(err, su))
1026 {
njn25e49d8e72002-09-23 09:36:25 +00001027 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001028 }
sewardjde4a1d02002-03-22 01:27:54 +00001029 }
njn25e49d8e72002-09-23 09:36:25 +00001030 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001031}
1032
sewardjde4a1d02002-03-22 01:27:54 +00001033/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00001034/*--- end m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001035/*--------------------------------------------------------------------*/