blob: 9f14f23e472a2b0791f540733aa84b4146b6a655 [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"
njneb8896b2005-06-04 20:03:55 +000037#include "pub_core_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_core_libcprint.h"
njn04e16982005-05-31 00:23:43 +000039#include "pub_core_main.h" // for VG_(start_debugger)()
njn20242342005-05-16 23:31:24 +000040#include "pub_core_options.h"
njnd2b17112005-04-19 04:10:25 +000041#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000042#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000043#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000044
45/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000046/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000047/*------------------------------------------------------------*/
48
njn14319cc2005-03-13 06:26:22 +000049/* After this many different unsuppressed errors have been observed,
50 be more conservative about collecting new ones. */
51#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
52
53/* After this many different unsuppressed errors have been observed,
54 stop collecting errors at all, and tell the user their program is
55 evidently a steaming pile of camel dung. */
56#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
57
58/* After this many total errors have been observed, stop collecting
59 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
60#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
61
sewardjde4a1d02002-03-22 01:27:54 +000062/* The list of error contexts found, both suppressed and unsuppressed.
63 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000064static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000065
66/* The list of suppression directives, as read from the specified
67 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000068static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000069
70/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000071static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000072
73/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000074static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000075
76/* forwards ... */
njn810086f2002-11-14 12:42:47 +000077static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000078
sewardjb5f6f512005-03-10 23:59:00 +000079static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000080
81/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000082/*--- Error type ---*/
83/*------------------------------------------------------------*/
84
nethercote996901a2004-08-03 13:29:09 +000085/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000086 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000087
88/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000089 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000090 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000091 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000092
93 When errors are found and recorded with VG_(maybe_record_error)(), all
94 the tool must do is pass in the four parameters; core will
95 allocate/initialise the error record.
96*/
97struct _Error {
98 struct _Error* next;
99 // NULL if unsuppressed; or ptr to suppression record.
100 Supp* supp;
101 Int count;
102 ThreadId tid;
103
104 // The tool-specific part
105 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000106 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000107 Addr addr; // Used frequently
108 Char* string; // Used frequently
109 void* extra; // For any tool-specific extras
110};
111
112ExeContext* VG_(get_error_where) ( Error* err )
113{
114 return err->where;
115}
116
117ErrorKind VG_(get_error_kind) ( Error* err )
118{
119 return err->ekind;
120}
121
122Addr VG_(get_error_address) ( Error* err )
123{
124 return err->addr;
125}
126
127Char* VG_(get_error_string) ( Error* err )
128{
129 return err->string;
130}
131
132void* VG_(get_error_extra) ( Error* err )
133{
134 return err->extra;
135}
136
nethercotef2b11482004-08-02 12:36:01 +0000137UInt VG_(get_n_errs_found)( void )
138{
139 return n_errs_found;
140}
141
nethercote4a184902004-08-02 12:21:09 +0000142/*------------------------------------------------------------*/
143/*--- Suppression type ---*/
144/*------------------------------------------------------------*/
145
146/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
147 * effectively extend it by defining their own enums in the (0..) range. */
148typedef
149 enum {
150 PThreadSupp = -1, /* Matches PThreadErr */
151 }
152 CoreSuppKind;
153
sewardjb5f6f512005-03-10 23:59:00 +0000154/* Max number of callers for context in a suppression. */
155#define VG_MAX_SUPP_CALLERS 24
156
nethercote4a184902004-08-02 12:21:09 +0000157/* For each caller specified for a suppression, record the nature of
158 the caller name. Not of interest to tools. */
159typedef
160 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000161 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000162 ObjName, /* Name is of an shared object file. */
163 FunName /* Name is of a function. */
164 }
165 SuppLocTy;
166
sewardjb5f6f512005-03-10 23:59:00 +0000167typedef
168 struct {
169 SuppLocTy ty;
170 Char* name;
171 }
172 SuppLoc;
173
nethercote4a184902004-08-02 12:21:09 +0000174/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000175 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000176 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000177 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000178struct _Supp {
179 struct _Supp* next;
180 Int count; // The number of times this error has been suppressed.
181 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000182
183 // Length of 'callers'
184 Int n_callers;
185 // Array of callers, for matching stack traces. First one (name of fn
186 // where err occurs) is mandatory; rest are optional.
187 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000188
189 /* The tool-specific part */
190 SuppKind skind; // What kind of suppression. Must use the range (0..).
191 Char* string; // String -- use is optional. NULL by default.
192 void* extra; // Anything else -- use is optional. NULL by default.
193};
194
195SuppKind VG_(get_supp_kind) ( Supp* su )
196{
197 return su->skind;
198}
199
200Char* VG_(get_supp_string) ( Supp* su )
201{
202 return su->string;
203}
204
205void* VG_(get_supp_extra) ( Supp* su )
206{
207 return su->extra;
208}
209
210
211void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
212{
213 su->skind = skind;
214}
215
216void VG_(set_supp_string) ( Supp* su, Char* string )
217{
218 su->string = string;
219}
220
221void VG_(set_supp_extra) ( Supp* su, void* extra )
222{
223 su->extra = extra;
224}
225
226
227/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000228/*--- Helper fns ---*/
229/*------------------------------------------------------------*/
230
sewardjde4a1d02002-03-22 01:27:54 +0000231/* Compare error contexts, to detect duplicates. Note that if they
232 are otherwise the same, the faulting addrs and associated rwoffsets
233 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000234static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000235{
njn810086f2002-11-14 12:42:47 +0000236 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000237 return False;
njn25e49d8e72002-09-23 09:36:25 +0000238 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000239 return False;
240
njn810086f2002-11-14 12:42:47 +0000241 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000242 // case ThreadErr:
243 // case MutexErr:
244 // vg_assert(VG_(needs).core_errors);
245 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000246 default:
njn51d827b2005-05-09 01:02:08 +0000247 if (VG_(needs).tool_errors) {
248 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
249 } else {
njn95ec8702004-11-22 16:46:13 +0000250 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000251 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000252 e1->ekind);
njn67993252004-11-22 18:02:32 +0000253 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000254 }
sewardjde4a1d02002-03-22 01:27:54 +0000255 }
256}
257
njn810086f2002-11-14 12:42:47 +0000258static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000259{
sewardj71bc3cb2005-05-19 00:25:45 +0000260 if (VG_(clo_xml)) {
261 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000262 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
263 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000264 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
265 }
266
267 if (!VG_(clo_xml)) {
268 if (printCount)
269 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
270 if (err->tid > 0 && err->tid != last_tid_printed) {
271 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
272 last_tid_printed = err->tid;
273 }
sewardjb5f6f512005-03-10 23:59:00 +0000274 }
njn25e49d8e72002-09-23 09:36:25 +0000275
njn810086f2002-11-14 12:42:47 +0000276 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000277 // case ThreadErr:
278 // case MutexErr:
279 // vg_assert(VG_(needs).core_errors);
280 // VG_(tm_error_print)(err);
281 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000282 default:
njn95ec8702004-11-22 16:46:13 +0000283 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000284 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000285 else {
njn95ec8702004-11-22 16:46:13 +0000286 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000287 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000288 err->ekind);
njn67993252004-11-22 18:02:32 +0000289 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000290 }
sewardjde4a1d02002-03-22 01:27:54 +0000291 }
sewardj71bc3cb2005-05-19 00:25:45 +0000292
293 if (VG_(clo_xml))
294 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000295}
296
nethercote04d0fbc2004-01-26 16:48:06 +0000297/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000298 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000299Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000300{
301 Char ch, ch2;
302 Int res;
303
njn43c799e2003-04-08 00:08:52 +0000304 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000305 return False;
306
307 VG_(message)(Vg_UserMsg, "");
308
309 again:
310 VG_(printf)(
311 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000312 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
313 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000314 );
315
sewardj6024b212003-07-13 10:54:33 +0000316 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000317 if (res != 1) goto ioerror;
318 /* res == 1 */
319 if (ch == '\n') return False;
320 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
321 && ch != 'C' && ch != 'c') goto again;
322
sewardj6024b212003-07-13 10:54:33 +0000323 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000324 if (res != 1) goto ioerror;
325 if (ch2 != '\n') goto again;
326
njn43c799e2003-04-08 00:08:52 +0000327 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000328 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000329 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000330 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000331 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000332 vg_assert(ch == 'c' || ch == 'C');
333
334 ioerror:
njn43c799e2003-04-08 00:08:52 +0000335 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000336 return False;
337}
338
339
sewardjb5f6f512005-03-10 23:59:00 +0000340/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000341static __inline__
njn72718642003-07-24 08:45:32 +0000342void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
343 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000344{
njnca82cc02004-11-22 17:18:48 +0000345 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000346
njn810086f2002-11-14 12:42:47 +0000347 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000348 err->next = NULL;
349 err->supp = NULL;
350 err->count = 1;
njn72718642003-07-24 08:45:32 +0000351 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000352 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000353 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000354 else
355 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000356
nethercote996901a2004-08-03 13:29:09 +0000357 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000358 err->ekind = ekind;
359 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000360 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000361 err->string = s;
362
njn25e49d8e72002-09-23 09:36:25 +0000363 /* sanity... */
njn72718642003-07-24 08:45:32 +0000364 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000365}
366
njnf4261312005-03-20 23:45:36 +0000367static void printSuppForIp(UInt n, Addr ip)
368{
njn47b209a2005-03-25 23:47:16 +0000369 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000370
njn47b209a2005-03-25 23:47:16 +0000371 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000372 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000373 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000374 VG_(printf)(" obj:%s\n", buf);
375 } else {
376 VG_(printf)(" ???:??? "
377 "# unknown, suppression will not work, sorry\n");
378 }
379}
380
nethercote10d481a2004-01-25 20:33:53 +0000381static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000382{
njn43c799e2003-04-08 00:08:52 +0000383 ExeContext* ec = VG_(get_error_where)(err);
384 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000385
sewardjb5f6f512005-03-10 23:59:00 +0000386 /* At most VG_MAX_SUPP_CALLERS names */
387 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000388 vg_assert(stop_at > 0);
389
390 VG_(printf)("{\n");
391 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000392
sewardjb5f6f512005-03-10 23:59:00 +0000393 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000394 VG_(printf)(" core:PThread\n");
395
396 } else {
njn51d827b2005-05-09 01:02:08 +0000397 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000398 if (NULL == name) {
399 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000400 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000401 return;
402 }
403 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000404 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000405 }
njn43c799e2003-04-08 00:08:52 +0000406
njnf4261312005-03-20 23:45:36 +0000407 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000408 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000409
410 VG_(printf)("}\n");
411}
412
njnb4aee052003-04-15 14:09:58 +0000413static
nethercote04d0fbc2004-01-26 16:48:06 +0000414void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000415{
sewardjd153fae2005-01-10 17:24:47 +0000416 Bool still_noisy = True;
417
nethercote04d0fbc2004-01-26 16:48:06 +0000418 /* Perhaps we want a debugger attach at this point? */
419 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000420 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
421 {
nethercote04d0fbc2004-01-26 16:48:06 +0000422 VG_(printf)("starting debugger\n");
423 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000424 }
njn43c799e2003-04-08 00:08:52 +0000425 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000426 if (VG_(clo_gen_suppressions) == 2
427 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000428 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000429 ) {
nethercote42602b12004-01-25 19:30:29 +0000430 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000431 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
432 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000433 }
434}
435
436/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
437 just for pretty printing purposes. */
438static Bool is_first_shown_context = True;
439
njn25e49d8e72002-09-23 09:36:25 +0000440/* Top-level entry point to the error management subsystem.
441 All detected errors are notified here; this routine decides if/when the
442 user should see the error. */
njn72718642003-07-24 08:45:32 +0000443void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000444 ErrorKind ekind, Addr a, Char* s, void* extra )
445{
njn810086f2002-11-14 12:42:47 +0000446 Error err;
447 Error* p;
448 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000449 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000450 VgRes exe_res = Vg_MedRes;
451 static Bool stopping_message = False;
452 static Bool slowdown_message = False;
453 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000454
njn14319cc2005-03-13 06:26:22 +0000455 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
456 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000457 have been found, just refuse to collect any more. This stops
458 the burden of the error-management system becoming excessive in
459 extremely buggy programs, although it does make it pretty
460 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000461 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000462 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000463 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000464 if (!stopping_message) {
465 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000466
njn695c16e2005-03-27 03:40:28 +0000467 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000468 VG_(message)(Vg_UserMsg,
469 "More than %d different errors detected. "
470 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000471 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000472 } else {
473 VG_(message)(Vg_UserMsg,
474 "More than %d total errors detected. "
475 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000476 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000477 }
478
sewardjde4a1d02002-03-22 01:27:54 +0000479 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000480 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000481 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000482 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000483 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000484 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000485 VG_(message)(Vg_UserMsg,
486 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000487 VG_(message)(Vg_UserMsg, "");
488 stopping_message = True;
489 }
490 return;
491 }
492
njn14319cc2005-03-13 06:26:22 +0000493 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000494 been found, be much more conservative about collecting new
495 ones. */
njn695c16e2005-03-27 03:40:28 +0000496 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000497 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000498 if (!slowdown_message) {
499 VG_(message)(Vg_UserMsg, "");
500 VG_(message)(Vg_UserMsg,
501 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000502 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000503 VG_(message)(Vg_UserMsg,
504 "will still be recorded, but in less detail than before.");
505 slowdown_message = True;
506 }
507 }
508
njn25e49d8e72002-09-23 09:36:25 +0000509 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000510 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000511
512 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000513 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000514 p_prev = NULL;
515 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000516 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000517 /* Found it. */
518 p->count++;
519 if (p->supp != NULL) {
520 /* Deal correctly with suppressed errors. */
521 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000522 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000523 } else {
nethercotef2b11482004-08-02 12:36:01 +0000524 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000525 }
526
527 /* Move p to the front of the list so that future searches
528 for it are faster. */
529 if (p_prev != NULL) {
530 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000531 p_prev->next = p->next;
532 p->next = errors;
533 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000534 }
sewardj7ebf7c32003-07-24 21:29:40 +0000535
sewardjde4a1d02002-03-22 01:27:54 +0000536 return;
537 }
538 p_prev = p;
539 p = p->next;
540 }
541
542 /* Didn't see it. Copy and add. */
543
njn43c799e2003-04-08 00:08:52 +0000544 /* OK, we're really going to collect it. The context is on the stack and
545 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000546 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000547
njn02bc4b82005-05-15 17:28:26 +0000548 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000549 is for when there are more details to fill in which take time to work
550 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000551 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000552 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000553
njn02bc4b82005-05-15 17:28:26 +0000554 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000555 VG_(tdict).tool_update_extra returned. Also allow for people using
556 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000557 */
558
559 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000560 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000561 *p = err;
njn43c799e2003-04-08 00:08:52 +0000562
njn02bc4b82005-05-15 17:28:26 +0000563 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000564 switch (ekind) {
565 // case ThreadErr:
566 // case MutexErr:
567 // vg_assert(VG_(needs).core_errors);
568 // extra_size = VG_(tm_error_update_extra)(p);
569 // break;
570 default:
571 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000572 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000573 break;
574 }
njn43c799e2003-04-08 00:08:52 +0000575
njn02bc4b82005-05-15 17:28:26 +0000576 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000577 if (NULL != p->extra && 0 != extra_size) {
578 void* new_extra = VG_(malloc)(extra_size);
579 VG_(memcpy)(new_extra, p->extra, extra_size);
580 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000581 }
582
njn695c16e2005-03-27 03:40:28 +0000583 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000584 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000585 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000586 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000587 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000588 if (!is_first_shown_context)
589 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000590 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000591 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000592 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000593 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000594 } else {
nethercotef2b11482004-08-02 12:36:01 +0000595 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000596 p->supp->count++;
597 }
598}
599
njn43c799e2003-04-08 00:08:52 +0000600/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000601 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000602 guaranteed to only happen once. This avoids all the recording and
603 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000604 suppressed. Bool 'print_error' dictates whether to print the error.
605 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000606*/
njn72718642003-07-24 08:45:32 +0000607Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000608 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000609 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000610{
611 Error err;
612
613 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000614 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000615
616 /* Unless it's suppressed, we're going to show it. Don't need to make
617 a copy, because it's only temporary anyway.
618
njn02bc4b82005-05-15 17:28:26 +0000619 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000620 because that can have an affect on whether it's suppressed. Ignore
621 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000622 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000623 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000624
625 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000626 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000627 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000628
629 if (print_error) {
630 if (!is_first_shown_context)
631 VG_(message)(Vg_UserMsg, "");
632 pp_Error(&err, False);
633 is_first_shown_context = False;
634 }
nethercote04d0fbc2004-01-26 16:48:06 +0000635 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000636
637 return False;
638
639 } else {
nethercotef2b11482004-08-02 12:36:01 +0000640 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000641 return True;
642 }
643}
644
sewardjde4a1d02002-03-22 01:27:54 +0000645
sewardjde4a1d02002-03-22 01:27:54 +0000646/*------------------------------------------------------------*/
647/*--- Exported fns ---*/
648/*------------------------------------------------------------*/
649
sewardj71bc3cb2005-05-19 00:25:45 +0000650/* Show the used suppressions. Returns False if no suppression
651 got used. */
652static Bool show_used_suppressions ( void )
653{
654 Supp *su;
655 Bool any_supp;
656
sewardj7c9e57c2005-05-24 14:21:45 +0000657 if (VG_(clo_xml))
658 VG_(message)(Vg_DebugMsg, "<suppcounts>");
659
sewardj71bc3cb2005-05-19 00:25:45 +0000660 any_supp = False;
661 for (su = suppressions; su != NULL; su = su->next) {
662 if (su->count <= 0)
663 continue;
664 any_supp = True;
665 if (VG_(clo_xml)) {
666 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000667 " <pair>\n"
668 " <count>%d</count>\n"
669 " <name>%s</name>\n"
670 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000671 su->count, su->sname);
672 } else {
673 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
674 }
675 }
676
sewardj7c9e57c2005-05-24 14:21:45 +0000677 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000678 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000679
sewardj71bc3cb2005-05-19 00:25:45 +0000680 return any_supp;
681}
682
683
sewardj9f297ca2005-05-20 02:29:52 +0000684/* Show all the errors that occurred, and possibly also the
685 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000686void VG_(show_all_errors) ( void )
687{
njn810086f2002-11-14 12:42:47 +0000688 Int i, n_min;
689 Int n_err_contexts, n_supp_contexts;
690 Error *p, *p_min;
691 Supp *su;
692 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000693
694 if (VG_(clo_verbosity) == 0)
695 return;
696
697 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000698 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000699 if (p->supp == NULL)
700 n_err_contexts++;
701 }
702
703 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000704 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000705 if (su->count > 0)
706 n_supp_contexts++;
707 }
sewardj71bc3cb2005-05-19 00:25:45 +0000708
709 /* If we're printing XML, just show the suppressions and stop.
710 */
711 if (VG_(clo_xml)) {
712 (void)show_used_suppressions();
713 return;
714 }
715
716 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000717 VG_(message)(Vg_UserMsg,
718 "ERROR SUMMARY: "
719 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000720 n_errs_found, n_err_contexts,
721 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000722
723 if (VG_(clo_verbosity) <= 1)
724 return;
725
726 /* Print the contexts in order of increasing error count. */
727 for (i = 0; i < n_err_contexts; i++) {
728 n_min = (1 << 30) - 1;
729 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000730 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000731 if (p->supp != NULL) continue;
732 if (p->count < n_min) {
733 n_min = p->count;
734 p_min = p;
735 }
736 }
njn67993252004-11-22 18:02:32 +0000737 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000738
739 VG_(message)(Vg_UserMsg, "");
740 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
741 p_min->count,
742 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000743 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000744
745 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000746 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000747 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000748 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000749 }
750
751 p_min->count = 1 << 30;
752 }
753
754 if (n_supp_contexts > 0)
755 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000756 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000757
758 if (n_err_contexts > 0) {
759 if (any_supp)
760 VG_(message)(Vg_UserMsg, "");
761 VG_(message)(Vg_UserMsg,
762 "IN SUMMARY: "
763 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000764 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000765 n_supp_contexts );
766 VG_(message)(Vg_UserMsg, "");
767 }
768}
769
sewardj9f297ca2005-05-20 02:29:52 +0000770
771/* Show occurrence counts of all errors, in XML form. */
772void VG_(show_error_counts_as_XML) ( void )
773{
774 Error* err;
775 VG_(message)(Vg_UserMsg, "<errorcounts>");
776 for (err = errors; err != NULL; err = err->next) {
777 if (err->supp != NULL)
778 continue;
779 if (err->count <= 0)
780 continue;
781 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000782 Vg_UserMsg, " <pair> <count>%d</count> "
783 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000784 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000785 );
786 }
787 VG_(message)(Vg_UserMsg, "</errorcounts>");
788}
789
790
sewardjde4a1d02002-03-22 01:27:54 +0000791/*------------------------------------------------------------*/
792/*--- Standard suppressions ---*/
793/*------------------------------------------------------------*/
794
795/* Get a non-blank, non-comment line of at most nBuf chars from fd.
796 Skips leading spaces on the line. Return True if EOF was hit instead.
797*/
njn4ba5a792002-09-30 10:23:54 +0000798Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000799{
800 Char ch;
801 Int n, i;
802 while (True) {
803 /* First, read until a non-blank char appears. */
804 while (True) {
805 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000806 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000807 if (n == 0) return True;
808 }
809
810 /* Now, read the line into buf. */
811 i = 0;
812 buf[i++] = ch; buf[i] = 0;
813 while (True) {
814 n = VG_(read)(fd, &ch, 1);
815 if (n == 0) return False; /* the next call will return True */
816 if (ch == '\n') break;
817 if (i > 0 && i == nBuf-1) i--;
818 buf[i++] = ch; buf[i] = 0;
819 }
njn0c0f32a2005-03-26 04:14:01 +0000820 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000821 i--; buf[i] = 0;
822 };
823
njn02bc4b82005-05-15 17:28:26 +0000824 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000825 /* Ok, we have a line. If a non-comment line, return.
826 If a comment line, start all over again. */
827 if (buf[0] != '#') return False;
828 }
829}
830
831
832/* *p_caller contains the raw name of a caller, supposedly either
833 fun:some_function_name or
834 obj:some_object_name.
835 Set *p_ty accordingly and advance *p_caller over the descriptor
836 (fun: or obj:) part.
837 Returns False if failed.
838*/
sewardjb5f6f512005-03-10 23:59:00 +0000839static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000840{
sewardjb5f6f512005-03-10 23:59:00 +0000841 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
842 p->name += 4;
843 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000844 return True;
845 }
sewardjb5f6f512005-03-10 23:59:00 +0000846 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
847 p->name += 4;
848 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000849 return True;
850 }
851 VG_(printf)("location should start with fun: or obj:\n");
852 return False;
853}
854
855
nethercote7cc9c232004-01-21 15:08:04 +0000856/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000857static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000858Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000859{
860 Bool found;
861 Char *s = NULL; /* Shut gcc up */
862 Int len = VG_(strlen)(name);
863
864 found = (NULL != (s = VG_(strstr)(names, name)) &&
865 (s == names || *(s-1) == ',') &&
866 (*(s+len) == ',' || *(s+len) == '\0')
867 );
868
869 return found;
870}
871
njn695c16e2005-03-27 03:40:28 +0000872/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000873 and place them in the suppressions list. If there's any difficulty
874 doing this, just give up -- there's no point in trying to recover.
875*/
sewardjde4a1d02002-03-22 01:27:54 +0000876static void load_one_suppressions_file ( Char* filename )
877{
878# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000879 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000880 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000881 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000882 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000883 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000884 Char* err_str = NULL;
885 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000886
njn25e49d8e72002-09-23 09:36:25 +0000887 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000888 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000889 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000890 filename );
891 VG_(exit)(1);
892 }
893
sewardjb5f6f512005-03-10 23:59:00 +0000894#define BOMB(S) { err_str = S; goto syntax_error; }
895
sewardjde4a1d02002-03-22 01:27:54 +0000896 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000897 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000898 Supp* supp;
899 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000900 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000901
902 // Initialise temporary reading-in buffer.
903 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
904 tmp_callers[i].ty = NoName;
905 tmp_callers[i].name = NULL;
906 }
907
njn810086f2002-11-14 12:42:47 +0000908 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000909
njn4ba5a792002-09-30 10:23:54 +0000910 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000911 if (eof) break;
912
sewardjb5f6f512005-03-10 23:59:00 +0000913 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000914
njn4ba5a792002-09-30 10:23:54 +0000915 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000916
917 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
918
njn25e49d8e72002-09-23 09:36:25 +0000919 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000920
njn4ba5a792002-09-30 10:23:54 +0000921 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000922
sewardjb5f6f512005-03-10 23:59:00 +0000923 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000924
njn94065fd2004-11-22 19:26:27 +0000925 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000926 i = 0;
927 while (True) {
928 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000929 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000930 i++;
njn25e49d8e72002-09-23 09:36:25 +0000931 }
njnc40c3a82002-10-02 11:02:27 +0000932 buf[i] = '\0'; /* Replace ':', splitting into two strings */
933
nethercote7cc9c232004-01-21 15:08:04 +0000934 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000935 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000936
nethercote7cc9c232004-01-21 15:08:04 +0000937 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000938 {
sewardjb5f6f512005-03-10 23:59:00 +0000939 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000940 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000941 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000942 else
sewardjb5f6f512005-03-10 23:59:00 +0000943 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000944 }
njn95ec8702004-11-22 16:46:13 +0000945 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000946 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000947 {
sewardjb5f6f512005-03-10 23:59:00 +0000948 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000949 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000950 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000951 } else {
952 BOMB("unknown tool suppression type");
953 }
njnc40c3a82002-10-02 11:02:27 +0000954 }
njn25e49d8e72002-09-23 09:36:25 +0000955 else {
sewardjb5f6f512005-03-10 23:59:00 +0000956 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000957 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000958 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000959 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000960 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000961 break;
962 }
963 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000964 }
965
njn95ec8702004-11-22 16:46:13 +0000966 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000967 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000968 {
969 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000970 }
971
sewardjb5f6f512005-03-10 23:59:00 +0000972 i = 0;
973 while (True) {
974 eof = VG_(get_line) ( fd, buf, N_BUF );
975 if (eof)
976 BOMB("unexpected end-of-file");
977 if (VG_STREQ(buf, "}")) {
978 if (i > 0) {
979 break;
980 } else {
981 BOMB("missing stack trace");
982 }
983 }
984 if (i == VG_MAX_SUPP_CALLERS)
985 BOMB("too many callers in stack trace");
986 if (i > 0 && i >= VG_(clo_backtrace_size))
987 break;
988 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
989 if (!setLocationTy(&(tmp_callers[i])))
990 BOMB("location should start with 'fun:' or 'obj:'");
991 i++;
992 }
993
994 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
995 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000996 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000997 do {
998 eof = VG_(get_line) ( fd, buf, N_BUF );
999 } while (!eof && !VG_STREQ(buf, "}"));
1000 }
1001
1002 // Copy tmp_callers[] into supp->callers[]
1003 supp->n_callers = i;
1004 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1005 for (i = 0; i < supp->n_callers; i++) {
1006 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001007 }
1008
njn695c16e2005-03-27 03:40:28 +00001009 supp->next = suppressions;
1010 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001011 }
sewardjde4a1d02002-03-22 01:27:54 +00001012 VG_(close)(fd);
1013 return;
1014
1015 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001016 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001017 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001018
sewardjde4a1d02002-03-22 01:27:54 +00001019 VG_(close)(fd);
1020 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001021 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001022
sewardjb5f6f512005-03-10 23:59:00 +00001023# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001024# undef N_BUF
1025}
1026
1027
1028void VG_(load_suppressions) ( void )
1029{
1030 Int i;
njn695c16e2005-03-27 03:40:28 +00001031 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001032 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1033 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001034 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1035 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001036 }
1037 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1038 }
1039}
1040
sewardjb5f6f512005-03-10 23:59:00 +00001041static
njn810086f2002-11-14 12:42:47 +00001042Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001043{
njn810086f2002-11-14 12:42:47 +00001044 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001045 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001046 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001047 default:
njn95ec8702004-11-22 16:46:13 +00001048 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001049 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001050 } else {
1051 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001052 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001053 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001054 err->ekind);
njn67993252004-11-22 18:02:32 +00001055 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001056 }
1057 }
1058}
1059
sewardjb5f6f512005-03-10 23:59:00 +00001060static
1061Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001062{
1063 Int i;
njn47b209a2005-03-25 23:47:16 +00001064 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001065 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001066
sewardjb5f6f512005-03-10 23:59:00 +00001067 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001068 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001069 vg_assert(su->callers[i].name != NULL);
1070 switch (su->callers[i].ty) {
1071 case ObjName:
njn47b209a2005-03-25 23:47:16 +00001072 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001073 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001074 break;
1075
1076 case FunName:
1077 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001078 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001079 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001080 break;
njn67993252004-11-22 18:02:32 +00001081 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001082 }
sewardjb5f6f512005-03-10 23:59:00 +00001083 if (!VG_(string_match)(su->callers[i].name, caller_name))
1084 return False;
njn25e49d8e72002-09-23 09:36:25 +00001085 }
1086
1087 /* If we reach here, it's a match */
1088 return True;
1089}
sewardjde4a1d02002-03-22 01:27:54 +00001090
njn810086f2002-11-14 12:42:47 +00001091/* Does an error context match a suppression? ie is this a suppressible
1092 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001093 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001094*/
njn810086f2002-11-14 12:42:47 +00001095static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001096{
njn810086f2002-11-14 12:42:47 +00001097 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001098
sewardjde4a1d02002-03-22 01:27:54 +00001099 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001100 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001101 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001102 supp_matches_callers(err, su))
1103 {
njn25e49d8e72002-09-23 09:36:25 +00001104 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001105 }
sewardjde4a1d02002-03-22 01:27:54 +00001106 }
njn25e49d8e72002-09-23 09:36:25 +00001107 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001108}
1109
sewardjde4a1d02002-03-22 01:27:54 +00001110/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001111/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001112/*--------------------------------------------------------------------*/