blob: 71ae7ceb2bcfd00db534759761c1d917727470cb [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00003/*--- Management of error messages. m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
nethercotef1e5e152004-09-01 23:58:16 +000031#include "core.h"
njnea27e462005-05-31 02:38:09 +000032#include "pub_core_debuginfo.h"
njnd2b17112005-04-19 04:10:25 +000033#include "pub_core_errormgr.h"
njnd01fef72005-03-25 23:35:48 +000034#include "pub_core_execontext.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000036#include "pub_core_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000037#include "pub_core_libcprint.h"
njn04e16982005-05-31 00:23:43 +000038#include "pub_core_main.h" // for VG_(start_debugger)()
njn20242342005-05-16 23:31:24 +000039#include "pub_core_options.h"
njnd2b17112005-04-19 04:10:25 +000040#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000041#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000042#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000043
44/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000045/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000046/*------------------------------------------------------------*/
47
njn14319cc2005-03-13 06:26:22 +000048/* After this many different unsuppressed errors have been observed,
49 be more conservative about collecting new ones. */
50#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
51
52/* After this many different unsuppressed errors have been observed,
53 stop collecting errors at all, and tell the user their program is
54 evidently a steaming pile of camel dung. */
55#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
56
57/* After this many total errors have been observed, stop collecting
58 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
59#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
60
sewardjde4a1d02002-03-22 01:27:54 +000061/* The list of error contexts found, both suppressed and unsuppressed.
62 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000063static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000064
65/* The list of suppression directives, as read from the specified
66 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000067static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000068
69/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000070static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000071
72/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000073static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000074
75/* forwards ... */
njn810086f2002-11-14 12:42:47 +000076static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000077
sewardjb5f6f512005-03-10 23:59:00 +000078static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000079
80/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000081/*--- Error type ---*/
82/*------------------------------------------------------------*/
83
nethercote996901a2004-08-03 13:29:09 +000084/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000085 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000086
87/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000088 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000089 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000090 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000091
92 When errors are found and recorded with VG_(maybe_record_error)(), all
93 the tool must do is pass in the four parameters; core will
94 allocate/initialise the error record.
95*/
96struct _Error {
97 struct _Error* next;
98 // NULL if unsuppressed; or ptr to suppression record.
99 Supp* supp;
100 Int count;
101 ThreadId tid;
102
103 // The tool-specific part
104 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000105 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000106 Addr addr; // Used frequently
107 Char* string; // Used frequently
108 void* extra; // For any tool-specific extras
109};
110
111ExeContext* VG_(get_error_where) ( Error* err )
112{
113 return err->where;
114}
115
116ErrorKind VG_(get_error_kind) ( Error* err )
117{
118 return err->ekind;
119}
120
121Addr VG_(get_error_address) ( Error* err )
122{
123 return err->addr;
124}
125
126Char* VG_(get_error_string) ( Error* err )
127{
128 return err->string;
129}
130
131void* VG_(get_error_extra) ( Error* err )
132{
133 return err->extra;
134}
135
nethercotef2b11482004-08-02 12:36:01 +0000136UInt VG_(get_n_errs_found)( void )
137{
138 return n_errs_found;
139}
140
nethercote4a184902004-08-02 12:21:09 +0000141/*------------------------------------------------------------*/
142/*--- Suppression type ---*/
143/*------------------------------------------------------------*/
144
145/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
146 * effectively extend it by defining their own enums in the (0..) range. */
147typedef
148 enum {
149 PThreadSupp = -1, /* Matches PThreadErr */
150 }
151 CoreSuppKind;
152
sewardjb5f6f512005-03-10 23:59:00 +0000153/* Max number of callers for context in a suppression. */
154#define VG_MAX_SUPP_CALLERS 24
155
nethercote4a184902004-08-02 12:21:09 +0000156/* For each caller specified for a suppression, record the nature of
157 the caller name. Not of interest to tools. */
158typedef
159 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000160 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000161 ObjName, /* Name is of an shared object file. */
162 FunName /* Name is of a function. */
163 }
164 SuppLocTy;
165
sewardjb5f6f512005-03-10 23:59:00 +0000166typedef
167 struct {
168 SuppLocTy ty;
169 Char* name;
170 }
171 SuppLoc;
172
nethercote4a184902004-08-02 12:21:09 +0000173/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000174 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000175 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000176 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000177struct _Supp {
178 struct _Supp* next;
179 Int count; // The number of times this error has been suppressed.
180 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000181
182 // Length of 'callers'
183 Int n_callers;
184 // Array of callers, for matching stack traces. First one (name of fn
185 // where err occurs) is mandatory; rest are optional.
186 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000187
188 /* The tool-specific part */
189 SuppKind skind; // What kind of suppression. Must use the range (0..).
190 Char* string; // String -- use is optional. NULL by default.
191 void* extra; // Anything else -- use is optional. NULL by default.
192};
193
194SuppKind VG_(get_supp_kind) ( Supp* su )
195{
196 return su->skind;
197}
198
199Char* VG_(get_supp_string) ( Supp* su )
200{
201 return su->string;
202}
203
204void* VG_(get_supp_extra) ( Supp* su )
205{
206 return su->extra;
207}
208
209
210void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
211{
212 su->skind = skind;
213}
214
215void VG_(set_supp_string) ( Supp* su, Char* string )
216{
217 su->string = string;
218}
219
220void VG_(set_supp_extra) ( Supp* su, void* extra )
221{
222 su->extra = extra;
223}
224
225
226/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000227/*--- Helper fns ---*/
228/*------------------------------------------------------------*/
229
sewardjde4a1d02002-03-22 01:27:54 +0000230/* Compare error contexts, to detect duplicates. Note that if they
231 are otherwise the same, the faulting addrs and associated rwoffsets
232 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000233static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000234{
njn810086f2002-11-14 12:42:47 +0000235 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000236 return False;
njn25e49d8e72002-09-23 09:36:25 +0000237 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000238 return False;
239
njn810086f2002-11-14 12:42:47 +0000240 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000241 // case ThreadErr:
242 // case MutexErr:
243 // vg_assert(VG_(needs).core_errors);
244 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000245 default:
njn51d827b2005-05-09 01:02:08 +0000246 if (VG_(needs).tool_errors) {
247 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
248 } else {
njn95ec8702004-11-22 16:46:13 +0000249 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000250 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000251 e1->ekind);
njn67993252004-11-22 18:02:32 +0000252 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000253 }
sewardjde4a1d02002-03-22 01:27:54 +0000254 }
255}
256
njn810086f2002-11-14 12:42:47 +0000257static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000258{
sewardj71bc3cb2005-05-19 00:25:45 +0000259 if (VG_(clo_xml)) {
260 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000261 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
262 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000263 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
264 }
265
266 if (!VG_(clo_xml)) {
267 if (printCount)
268 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
269 if (err->tid > 0 && err->tid != last_tid_printed) {
270 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
271 last_tid_printed = err->tid;
272 }
sewardjb5f6f512005-03-10 23:59:00 +0000273 }
njn25e49d8e72002-09-23 09:36:25 +0000274
njn810086f2002-11-14 12:42:47 +0000275 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000276 // case ThreadErr:
277 // case MutexErr:
278 // vg_assert(VG_(needs).core_errors);
279 // VG_(tm_error_print)(err);
280 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000281 default:
njn95ec8702004-11-22 16:46:13 +0000282 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000283 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000284 else {
njn95ec8702004-11-22 16:46:13 +0000285 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000286 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000287 err->ekind);
njn67993252004-11-22 18:02:32 +0000288 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000289 }
sewardjde4a1d02002-03-22 01:27:54 +0000290 }
sewardj71bc3cb2005-05-19 00:25:45 +0000291
292 if (VG_(clo_xml))
293 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000294}
295
nethercote04d0fbc2004-01-26 16:48:06 +0000296/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000297 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000298Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000299{
300 Char ch, ch2;
301 Int res;
302
njn43c799e2003-04-08 00:08:52 +0000303 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000304 return False;
305
306 VG_(message)(Vg_UserMsg, "");
307
308 again:
309 VG_(printf)(
310 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000311 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
312 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000313 );
314
sewardj6024b212003-07-13 10:54:33 +0000315 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000316 if (res != 1) goto ioerror;
317 /* res == 1 */
318 if (ch == '\n') return False;
319 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
320 && ch != 'C' && ch != 'c') goto again;
321
sewardj6024b212003-07-13 10:54:33 +0000322 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000323 if (res != 1) goto ioerror;
324 if (ch2 != '\n') goto again;
325
njn43c799e2003-04-08 00:08:52 +0000326 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000327 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000328 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000329 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000330 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000331 vg_assert(ch == 'c' || ch == 'C');
332
333 ioerror:
njn43c799e2003-04-08 00:08:52 +0000334 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000335 return False;
336}
337
338
sewardjb5f6f512005-03-10 23:59:00 +0000339/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000340static __inline__
njn72718642003-07-24 08:45:32 +0000341void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
342 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000343{
njnca82cc02004-11-22 17:18:48 +0000344 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000345
njn810086f2002-11-14 12:42:47 +0000346 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000347 err->next = NULL;
348 err->supp = NULL;
349 err->count = 1;
njn72718642003-07-24 08:45:32 +0000350 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000351 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000352 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000353 else
354 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000355
nethercote996901a2004-08-03 13:29:09 +0000356 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000357 err->ekind = ekind;
358 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000359 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000360 err->string = s;
361
njn25e49d8e72002-09-23 09:36:25 +0000362 /* sanity... */
njn72718642003-07-24 08:45:32 +0000363 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000364}
365
njnf4261312005-03-20 23:45:36 +0000366static void printSuppForIp(UInt n, Addr ip)
367{
njn47b209a2005-03-25 23:47:16 +0000368 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000369
njn47b209a2005-03-25 23:47:16 +0000370 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000371 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000372 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000373 VG_(printf)(" obj:%s\n", buf);
374 } else {
375 VG_(printf)(" ???:??? "
376 "# unknown, suppression will not work, sorry\n");
377 }
378}
379
nethercote10d481a2004-01-25 20:33:53 +0000380static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000381{
njn43c799e2003-04-08 00:08:52 +0000382 ExeContext* ec = VG_(get_error_where)(err);
383 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000384
sewardjb5f6f512005-03-10 23:59:00 +0000385 /* At most VG_MAX_SUPP_CALLERS names */
386 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000387 vg_assert(stop_at > 0);
388
389 VG_(printf)("{\n");
390 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000391
sewardjb5f6f512005-03-10 23:59:00 +0000392 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000393 VG_(printf)(" core:PThread\n");
394
395 } else {
njn51d827b2005-05-09 01:02:08 +0000396 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000397 if (NULL == name) {
398 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000399 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000400 return;
401 }
402 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000403 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000404 }
njn43c799e2003-04-08 00:08:52 +0000405
njnf4261312005-03-20 23:45:36 +0000406 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000407 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000408
409 VG_(printf)("}\n");
410}
411
njnb4aee052003-04-15 14:09:58 +0000412static
nethercote04d0fbc2004-01-26 16:48:06 +0000413void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000414{
sewardjd153fae2005-01-10 17:24:47 +0000415 Bool still_noisy = True;
416
nethercote04d0fbc2004-01-26 16:48:06 +0000417 /* Perhaps we want a debugger attach at this point? */
418 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000419 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
420 {
nethercote04d0fbc2004-01-26 16:48:06 +0000421 VG_(printf)("starting debugger\n");
422 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000423 }
njn43c799e2003-04-08 00:08:52 +0000424 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000425 if (VG_(clo_gen_suppressions) == 2
426 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000427 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000428 ) {
nethercote42602b12004-01-25 19:30:29 +0000429 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000430 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
431 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000432 }
433}
434
435/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
436 just for pretty printing purposes. */
437static Bool is_first_shown_context = True;
438
njn25e49d8e72002-09-23 09:36:25 +0000439/* Top-level entry point to the error management subsystem.
440 All detected errors are notified here; this routine decides if/when the
441 user should see the error. */
njn72718642003-07-24 08:45:32 +0000442void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000443 ErrorKind ekind, Addr a, Char* s, void* extra )
444{
njn810086f2002-11-14 12:42:47 +0000445 Error err;
446 Error* p;
447 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000448 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000449 VgRes exe_res = Vg_MedRes;
450 static Bool stopping_message = False;
451 static Bool slowdown_message = False;
452 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000453
njn14319cc2005-03-13 06:26:22 +0000454 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
455 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000456 have been found, just refuse to collect any more. This stops
457 the burden of the error-management system becoming excessive in
458 extremely buggy programs, although it does make it pretty
459 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000460 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000461 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000462 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000463 if (!stopping_message) {
464 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000465
njn695c16e2005-03-27 03:40:28 +0000466 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000467 VG_(message)(Vg_UserMsg,
468 "More than %d different errors detected. "
469 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000470 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000471 } else {
472 VG_(message)(Vg_UserMsg,
473 "More than %d total errors detected. "
474 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000475 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000476 }
477
sewardjde4a1d02002-03-22 01:27:54 +0000478 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000479 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000480 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000481 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000482 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000483 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000484 VG_(message)(Vg_UserMsg,
485 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000486 VG_(message)(Vg_UserMsg, "");
487 stopping_message = True;
488 }
489 return;
490 }
491
njn14319cc2005-03-13 06:26:22 +0000492 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000493 been found, be much more conservative about collecting new
494 ones. */
njn695c16e2005-03-27 03:40:28 +0000495 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000496 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000497 if (!slowdown_message) {
498 VG_(message)(Vg_UserMsg, "");
499 VG_(message)(Vg_UserMsg,
500 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000501 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000502 VG_(message)(Vg_UserMsg,
503 "will still be recorded, but in less detail than before.");
504 slowdown_message = True;
505 }
506 }
507
njn25e49d8e72002-09-23 09:36:25 +0000508 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000509 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000510
511 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000512 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000513 p_prev = NULL;
514 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000515 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000516 /* Found it. */
517 p->count++;
518 if (p->supp != NULL) {
519 /* Deal correctly with suppressed errors. */
520 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000521 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000522 } else {
nethercotef2b11482004-08-02 12:36:01 +0000523 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000524 }
525
526 /* Move p to the front of the list so that future searches
527 for it are faster. */
528 if (p_prev != NULL) {
529 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000530 p_prev->next = p->next;
531 p->next = errors;
532 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000533 }
sewardj7ebf7c32003-07-24 21:29:40 +0000534
sewardjde4a1d02002-03-22 01:27:54 +0000535 return;
536 }
537 p_prev = p;
538 p = p->next;
539 }
540
541 /* Didn't see it. Copy and add. */
542
njn43c799e2003-04-08 00:08:52 +0000543 /* OK, we're really going to collect it. The context is on the stack and
544 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000545 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000546
njn02bc4b82005-05-15 17:28:26 +0000547 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000548 is for when there are more details to fill in which take time to work
549 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000550 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000551 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000552
njn02bc4b82005-05-15 17:28:26 +0000553 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000554 VG_(tdict).tool_update_extra returned. Also allow for people using
555 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000556 */
557
558 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000559 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000560 *p = err;
njn43c799e2003-04-08 00:08:52 +0000561
njn02bc4b82005-05-15 17:28:26 +0000562 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000563 switch (ekind) {
564 // case ThreadErr:
565 // case MutexErr:
566 // vg_assert(VG_(needs).core_errors);
567 // extra_size = VG_(tm_error_update_extra)(p);
568 // break;
569 default:
570 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000571 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000572 break;
573 }
njn43c799e2003-04-08 00:08:52 +0000574
njn02bc4b82005-05-15 17:28:26 +0000575 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000576 if (NULL != p->extra && 0 != extra_size) {
577 void* new_extra = VG_(malloc)(extra_size);
578 VG_(memcpy)(new_extra, p->extra, extra_size);
579 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000580 }
581
njn695c16e2005-03-27 03:40:28 +0000582 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000583 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000584 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000585 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000586 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000587 if (!is_first_shown_context)
588 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000589 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000590 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000591 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000592 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000593 } else {
nethercotef2b11482004-08-02 12:36:01 +0000594 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000595 p->supp->count++;
596 }
597}
598
njn43c799e2003-04-08 00:08:52 +0000599/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000600 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000601 guaranteed to only happen once. This avoids all the recording and
602 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000603 suppressed. Bool 'print_error' dictates whether to print the error.
604 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000605*/
njn72718642003-07-24 08:45:32 +0000606Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000607 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000608 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000609{
610 Error err;
611
612 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000613 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000614
615 /* Unless it's suppressed, we're going to show it. Don't need to make
616 a copy, because it's only temporary anyway.
617
njn02bc4b82005-05-15 17:28:26 +0000618 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000619 because that can have an affect on whether it's suppressed. Ignore
620 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000621 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000622 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000623
624 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000625 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000626 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000627
628 if (print_error) {
629 if (!is_first_shown_context)
630 VG_(message)(Vg_UserMsg, "");
631 pp_Error(&err, False);
632 is_first_shown_context = False;
633 }
nethercote04d0fbc2004-01-26 16:48:06 +0000634 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000635
636 return False;
637
638 } else {
nethercotef2b11482004-08-02 12:36:01 +0000639 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000640 return True;
641 }
642}
643
sewardjde4a1d02002-03-22 01:27:54 +0000644
sewardjde4a1d02002-03-22 01:27:54 +0000645/*------------------------------------------------------------*/
646/*--- Exported fns ---*/
647/*------------------------------------------------------------*/
648
sewardj71bc3cb2005-05-19 00:25:45 +0000649/* Show the used suppressions. Returns False if no suppression
650 got used. */
651static Bool show_used_suppressions ( void )
652{
653 Supp *su;
654 Bool any_supp;
655
sewardj7c9e57c2005-05-24 14:21:45 +0000656 if (VG_(clo_xml))
657 VG_(message)(Vg_DebugMsg, "<suppcounts>");
658
sewardj71bc3cb2005-05-19 00:25:45 +0000659 any_supp = False;
660 for (su = suppressions; su != NULL; su = su->next) {
661 if (su->count <= 0)
662 continue;
663 any_supp = True;
664 if (VG_(clo_xml)) {
665 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000666 " <pair>\n"
667 " <count>%d</count>\n"
668 " <name>%s</name>\n"
669 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000670 su->count, su->sname);
671 } else {
672 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
673 }
674 }
675
sewardj7c9e57c2005-05-24 14:21:45 +0000676 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000677 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000678
sewardj71bc3cb2005-05-19 00:25:45 +0000679 return any_supp;
680}
681
682
sewardj9f297ca2005-05-20 02:29:52 +0000683/* Show all the errors that occurred, and possibly also the
684 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000685void VG_(show_all_errors) ( void )
686{
njn810086f2002-11-14 12:42:47 +0000687 Int i, n_min;
688 Int n_err_contexts, n_supp_contexts;
689 Error *p, *p_min;
690 Supp *su;
691 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000692
693 if (VG_(clo_verbosity) == 0)
694 return;
695
696 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000697 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000698 if (p->supp == NULL)
699 n_err_contexts++;
700 }
701
702 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000703 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000704 if (su->count > 0)
705 n_supp_contexts++;
706 }
sewardj71bc3cb2005-05-19 00:25:45 +0000707
708 /* If we're printing XML, just show the suppressions and stop.
709 */
710 if (VG_(clo_xml)) {
711 (void)show_used_suppressions();
712 return;
713 }
714
715 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000716 VG_(message)(Vg_UserMsg,
717 "ERROR SUMMARY: "
718 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000719 n_errs_found, n_err_contexts,
720 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000721
722 if (VG_(clo_verbosity) <= 1)
723 return;
724
725 /* Print the contexts in order of increasing error count. */
726 for (i = 0; i < n_err_contexts; i++) {
727 n_min = (1 << 30) - 1;
728 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000729 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000730 if (p->supp != NULL) continue;
731 if (p->count < n_min) {
732 n_min = p->count;
733 p_min = p;
734 }
735 }
njn67993252004-11-22 18:02:32 +0000736 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000737
738 VG_(message)(Vg_UserMsg, "");
739 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
740 p_min->count,
741 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000742 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000743
744 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000745 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000746 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000747 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000748 }
749
750 p_min->count = 1 << 30;
751 }
752
753 if (n_supp_contexts > 0)
754 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000755 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000756
757 if (n_err_contexts > 0) {
758 if (any_supp)
759 VG_(message)(Vg_UserMsg, "");
760 VG_(message)(Vg_UserMsg,
761 "IN SUMMARY: "
762 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000763 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000764 n_supp_contexts );
765 VG_(message)(Vg_UserMsg, "");
766 }
767}
768
sewardj9f297ca2005-05-20 02:29:52 +0000769
770/* Show occurrence counts of all errors, in XML form. */
771void VG_(show_error_counts_as_XML) ( void )
772{
773 Error* err;
774 VG_(message)(Vg_UserMsg, "<errorcounts>");
775 for (err = errors; err != NULL; err = err->next) {
776 if (err->supp != NULL)
777 continue;
778 if (err->count <= 0)
779 continue;
780 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000781 Vg_UserMsg, " <pair> <count>%d</count> "
782 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000783 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000784 );
785 }
786 VG_(message)(Vg_UserMsg, "</errorcounts>");
787}
788
789
sewardjde4a1d02002-03-22 01:27:54 +0000790/*------------------------------------------------------------*/
791/*--- Standard suppressions ---*/
792/*------------------------------------------------------------*/
793
794/* Get a non-blank, non-comment line of at most nBuf chars from fd.
795 Skips leading spaces on the line. Return True if EOF was hit instead.
796*/
njn4ba5a792002-09-30 10:23:54 +0000797Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000798{
799 Char ch;
800 Int n, i;
801 while (True) {
802 /* First, read until a non-blank char appears. */
803 while (True) {
804 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000805 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000806 if (n == 0) return True;
807 }
808
809 /* Now, read the line into buf. */
810 i = 0;
811 buf[i++] = ch; buf[i] = 0;
812 while (True) {
813 n = VG_(read)(fd, &ch, 1);
814 if (n == 0) return False; /* the next call will return True */
815 if (ch == '\n') break;
816 if (i > 0 && i == nBuf-1) i--;
817 buf[i++] = ch; buf[i] = 0;
818 }
njn0c0f32a2005-03-26 04:14:01 +0000819 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000820 i--; buf[i] = 0;
821 };
822
njn02bc4b82005-05-15 17:28:26 +0000823 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000824 /* Ok, we have a line. If a non-comment line, return.
825 If a comment line, start all over again. */
826 if (buf[0] != '#') return False;
827 }
828}
829
830
831/* *p_caller contains the raw name of a caller, supposedly either
832 fun:some_function_name or
833 obj:some_object_name.
834 Set *p_ty accordingly and advance *p_caller over the descriptor
835 (fun: or obj:) part.
836 Returns False if failed.
837*/
sewardjb5f6f512005-03-10 23:59:00 +0000838static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000839{
sewardjb5f6f512005-03-10 23:59:00 +0000840 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
841 p->name += 4;
842 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000843 return True;
844 }
sewardjb5f6f512005-03-10 23:59:00 +0000845 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
846 p->name += 4;
847 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000848 return True;
849 }
850 VG_(printf)("location should start with fun: or obj:\n");
851 return False;
852}
853
854
nethercote7cc9c232004-01-21 15:08:04 +0000855/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000856static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000857Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000858{
859 Bool found;
860 Char *s = NULL; /* Shut gcc up */
861 Int len = VG_(strlen)(name);
862
863 found = (NULL != (s = VG_(strstr)(names, name)) &&
864 (s == names || *(s-1) == ',') &&
865 (*(s+len) == ',' || *(s+len) == '\0')
866 );
867
868 return found;
869}
870
njn695c16e2005-03-27 03:40:28 +0000871/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000872 and place them in the suppressions list. If there's any difficulty
873 doing this, just give up -- there's no point in trying to recover.
874*/
sewardjde4a1d02002-03-22 01:27:54 +0000875static void load_one_suppressions_file ( Char* filename )
876{
877# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000878 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000879 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000880 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000881 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000882 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000883 Char* err_str = NULL;
884 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000885
njn25e49d8e72002-09-23 09:36:25 +0000886 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000887 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000888 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000889 filename );
890 VG_(exit)(1);
891 }
892
sewardjb5f6f512005-03-10 23:59:00 +0000893#define BOMB(S) { err_str = S; goto syntax_error; }
894
sewardjde4a1d02002-03-22 01:27:54 +0000895 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000896 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000897 Supp* supp;
898 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000899 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000900
901 // Initialise temporary reading-in buffer.
902 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
903 tmp_callers[i].ty = NoName;
904 tmp_callers[i].name = NULL;
905 }
906
njn810086f2002-11-14 12:42:47 +0000907 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000908
njn4ba5a792002-09-30 10:23:54 +0000909 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000910 if (eof) break;
911
sewardjb5f6f512005-03-10 23:59:00 +0000912 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000913
njn4ba5a792002-09-30 10:23:54 +0000914 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000915
916 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
917
njn25e49d8e72002-09-23 09:36:25 +0000918 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000919
njn4ba5a792002-09-30 10:23:54 +0000920 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000921
sewardjb5f6f512005-03-10 23:59:00 +0000922 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000923
njn94065fd2004-11-22 19:26:27 +0000924 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000925 i = 0;
926 while (True) {
927 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000928 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000929 i++;
njn25e49d8e72002-09-23 09:36:25 +0000930 }
njnc40c3a82002-10-02 11:02:27 +0000931 buf[i] = '\0'; /* Replace ':', splitting into two strings */
932
nethercote7cc9c232004-01-21 15:08:04 +0000933 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000934 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000935
nethercote7cc9c232004-01-21 15:08:04 +0000936 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000937 {
sewardjb5f6f512005-03-10 23:59:00 +0000938 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000939 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000940 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000941 else
sewardjb5f6f512005-03-10 23:59:00 +0000942 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000943 }
njn95ec8702004-11-22 16:46:13 +0000944 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000945 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000946 {
sewardjb5f6f512005-03-10 23:59:00 +0000947 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000948 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000949 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000950 } else {
951 BOMB("unknown tool suppression type");
952 }
njnc40c3a82002-10-02 11:02:27 +0000953 }
njn25e49d8e72002-09-23 09:36:25 +0000954 else {
sewardjb5f6f512005-03-10 23:59:00 +0000955 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000956 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000957 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000958 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000959 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000960 break;
961 }
962 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000963 }
964
njn95ec8702004-11-22 16:46:13 +0000965 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000966 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000967 {
968 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000969 }
970
sewardjb5f6f512005-03-10 23:59:00 +0000971 i = 0;
972 while (True) {
973 eof = VG_(get_line) ( fd, buf, N_BUF );
974 if (eof)
975 BOMB("unexpected end-of-file");
976 if (VG_STREQ(buf, "}")) {
977 if (i > 0) {
978 break;
979 } else {
980 BOMB("missing stack trace");
981 }
982 }
983 if (i == VG_MAX_SUPP_CALLERS)
984 BOMB("too many callers in stack trace");
985 if (i > 0 && i >= VG_(clo_backtrace_size))
986 break;
987 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
988 if (!setLocationTy(&(tmp_callers[i])))
989 BOMB("location should start with 'fun:' or 'obj:'");
990 i++;
991 }
992
993 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
994 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000995 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000996 do {
997 eof = VG_(get_line) ( fd, buf, N_BUF );
998 } while (!eof && !VG_STREQ(buf, "}"));
999 }
1000
1001 // Copy tmp_callers[] into supp->callers[]
1002 supp->n_callers = i;
1003 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1004 for (i = 0; i < supp->n_callers; i++) {
1005 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001006 }
1007
njn695c16e2005-03-27 03:40:28 +00001008 supp->next = suppressions;
1009 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001010 }
sewardjde4a1d02002-03-22 01:27:54 +00001011 VG_(close)(fd);
1012 return;
1013
1014 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001015 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001016 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001017
sewardjde4a1d02002-03-22 01:27:54 +00001018 VG_(close)(fd);
1019 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001020 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001021
sewardjb5f6f512005-03-10 23:59:00 +00001022# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001023# undef N_BUF
1024}
1025
1026
1027void VG_(load_suppressions) ( void )
1028{
1029 Int i;
njn695c16e2005-03-27 03:40:28 +00001030 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001031 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1032 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001033 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1034 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001035 }
1036 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1037 }
1038}
1039
sewardjb5f6f512005-03-10 23:59:00 +00001040static
njn810086f2002-11-14 12:42:47 +00001041Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001042{
njn810086f2002-11-14 12:42:47 +00001043 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001044 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001045 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001046 default:
njn95ec8702004-11-22 16:46:13 +00001047 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001048 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001049 } else {
1050 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001051 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001052 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001053 err->ekind);
njn67993252004-11-22 18:02:32 +00001054 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001055 }
1056 }
1057}
1058
sewardjb5f6f512005-03-10 23:59:00 +00001059static
1060Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001061{
1062 Int i;
njn47b209a2005-03-25 23:47:16 +00001063 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001064 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001065
sewardjb5f6f512005-03-10 23:59:00 +00001066 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001067 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001068 vg_assert(su->callers[i].name != NULL);
1069 switch (su->callers[i].ty) {
1070 case ObjName:
njn47b209a2005-03-25 23:47:16 +00001071 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001072 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001073 break;
1074
1075 case FunName:
1076 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001077 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001078 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001079 break;
njn67993252004-11-22 18:02:32 +00001080 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001081 }
sewardjb5f6f512005-03-10 23:59:00 +00001082 if (!VG_(string_match)(su->callers[i].name, caller_name))
1083 return False;
njn25e49d8e72002-09-23 09:36:25 +00001084 }
1085
1086 /* If we reach here, it's a match */
1087 return True;
1088}
sewardjde4a1d02002-03-22 01:27:54 +00001089
njn810086f2002-11-14 12:42:47 +00001090/* Does an error context match a suppression? ie is this a suppressible
1091 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001092 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001093*/
njn810086f2002-11-14 12:42:47 +00001094static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001095{
njn810086f2002-11-14 12:42:47 +00001096 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001097
sewardjde4a1d02002-03-22 01:27:54 +00001098 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001099 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001100 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001101 supp_matches_callers(err, su))
1102 {
njn25e49d8e72002-09-23 09:36:25 +00001103 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001104 }
sewardjde4a1d02002-03-22 01:27:54 +00001105 }
njn25e49d8e72002-09-23 09:36:25 +00001106 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001107}
1108
sewardjde4a1d02002-03-22 01:27:54 +00001109/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00001110/*--- end m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001111/*--------------------------------------------------------------------*/