blob: fe36d2291ebf654926d4f244b1b5100e97f9f4de [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"
njn36a20fa2005-06-03 03:08:39 +000036#include "pub_core_libcprint.h"
njn04e16982005-05-31 00:23:43 +000037#include "pub_core_main.h" // for VG_(start_debugger)()
njn20242342005-05-16 23:31:24 +000038#include "pub_core_options.h"
njnd2b17112005-04-19 04:10:25 +000039#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000040#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000041#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000042
43/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000044/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000045/*------------------------------------------------------------*/
46
njn14319cc2005-03-13 06:26:22 +000047/* After this many different unsuppressed errors have been observed,
48 be more conservative about collecting new ones. */
49#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
50
51/* After this many different unsuppressed errors have been observed,
52 stop collecting errors at all, and tell the user their program is
53 evidently a steaming pile of camel dung. */
54#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
55
56/* After this many total errors have been observed, stop collecting
57 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
58#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
59
sewardjde4a1d02002-03-22 01:27:54 +000060/* The list of error contexts found, both suppressed and unsuppressed.
61 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000062static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000063
64/* The list of suppression directives, as read from the specified
65 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000066static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000067
68/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000069static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000070
71/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000072static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000073
74/* forwards ... */
njn810086f2002-11-14 12:42:47 +000075static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000076
sewardjb5f6f512005-03-10 23:59:00 +000077static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000078
79/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000080/*--- Error type ---*/
81/*------------------------------------------------------------*/
82
nethercote996901a2004-08-03 13:29:09 +000083/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000084 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000085
86/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000087 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000088 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000089 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000090
91 When errors are found and recorded with VG_(maybe_record_error)(), all
92 the tool must do is pass in the four parameters; core will
93 allocate/initialise the error record.
94*/
95struct _Error {
96 struct _Error* next;
97 // NULL if unsuppressed; or ptr to suppression record.
98 Supp* supp;
99 Int count;
100 ThreadId tid;
101
102 // The tool-specific part
103 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000104 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000105 Addr addr; // Used frequently
106 Char* string; // Used frequently
107 void* extra; // For any tool-specific extras
108};
109
110ExeContext* VG_(get_error_where) ( Error* err )
111{
112 return err->where;
113}
114
115ErrorKind VG_(get_error_kind) ( Error* err )
116{
117 return err->ekind;
118}
119
120Addr VG_(get_error_address) ( Error* err )
121{
122 return err->addr;
123}
124
125Char* VG_(get_error_string) ( Error* err )
126{
127 return err->string;
128}
129
130void* VG_(get_error_extra) ( Error* err )
131{
132 return err->extra;
133}
134
nethercotef2b11482004-08-02 12:36:01 +0000135UInt VG_(get_n_errs_found)( void )
136{
137 return n_errs_found;
138}
139
nethercote4a184902004-08-02 12:21:09 +0000140/*------------------------------------------------------------*/
141/*--- Suppression type ---*/
142/*------------------------------------------------------------*/
143
144/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
145 * effectively extend it by defining their own enums in the (0..) range. */
146typedef
147 enum {
148 PThreadSupp = -1, /* Matches PThreadErr */
149 }
150 CoreSuppKind;
151
sewardjb5f6f512005-03-10 23:59:00 +0000152/* Max number of callers for context in a suppression. */
153#define VG_MAX_SUPP_CALLERS 24
154
nethercote4a184902004-08-02 12:21:09 +0000155/* For each caller specified for a suppression, record the nature of
156 the caller name. Not of interest to tools. */
157typedef
158 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000159 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000160 ObjName, /* Name is of an shared object file. */
161 FunName /* Name is of a function. */
162 }
163 SuppLocTy;
164
sewardjb5f6f512005-03-10 23:59:00 +0000165typedef
166 struct {
167 SuppLocTy ty;
168 Char* name;
169 }
170 SuppLoc;
171
nethercote4a184902004-08-02 12:21:09 +0000172/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000173 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000174 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000175 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000176struct _Supp {
177 struct _Supp* next;
178 Int count; // The number of times this error has been suppressed.
179 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000180
181 // Length of 'callers'
182 Int n_callers;
183 // Array of callers, for matching stack traces. First one (name of fn
184 // where err occurs) is mandatory; rest are optional.
185 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000186
187 /* The tool-specific part */
188 SuppKind skind; // What kind of suppression. Must use the range (0..).
189 Char* string; // String -- use is optional. NULL by default.
190 void* extra; // Anything else -- use is optional. NULL by default.
191};
192
193SuppKind VG_(get_supp_kind) ( Supp* su )
194{
195 return su->skind;
196}
197
198Char* VG_(get_supp_string) ( Supp* su )
199{
200 return su->string;
201}
202
203void* VG_(get_supp_extra) ( Supp* su )
204{
205 return su->extra;
206}
207
208
209void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
210{
211 su->skind = skind;
212}
213
214void VG_(set_supp_string) ( Supp* su, Char* string )
215{
216 su->string = string;
217}
218
219void VG_(set_supp_extra) ( Supp* su, void* extra )
220{
221 su->extra = extra;
222}
223
224
225/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000226/*--- Helper fns ---*/
227/*------------------------------------------------------------*/
228
sewardjde4a1d02002-03-22 01:27:54 +0000229/* Compare error contexts, to detect duplicates. Note that if they
230 are otherwise the same, the faulting addrs and associated rwoffsets
231 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000232static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000233{
njn810086f2002-11-14 12:42:47 +0000234 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000235 return False;
njn25e49d8e72002-09-23 09:36:25 +0000236 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000237 return False;
238
njn810086f2002-11-14 12:42:47 +0000239 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000240 // case ThreadErr:
241 // case MutexErr:
242 // vg_assert(VG_(needs).core_errors);
243 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000244 default:
njn51d827b2005-05-09 01:02:08 +0000245 if (VG_(needs).tool_errors) {
246 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
247 } else {
njn95ec8702004-11-22 16:46:13 +0000248 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000249 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000250 e1->ekind);
njn67993252004-11-22 18:02:32 +0000251 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000252 }
sewardjde4a1d02002-03-22 01:27:54 +0000253 }
254}
255
njn810086f2002-11-14 12:42:47 +0000256static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000257{
sewardj71bc3cb2005-05-19 00:25:45 +0000258 if (VG_(clo_xml)) {
259 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000260 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
261 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000262 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
263 }
264
265 if (!VG_(clo_xml)) {
266 if (printCount)
267 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
268 if (err->tid > 0 && err->tid != last_tid_printed) {
269 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
270 last_tid_printed = err->tid;
271 }
sewardjb5f6f512005-03-10 23:59:00 +0000272 }
njn25e49d8e72002-09-23 09:36:25 +0000273
njn810086f2002-11-14 12:42:47 +0000274 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000275 // case ThreadErr:
276 // case MutexErr:
277 // vg_assert(VG_(needs).core_errors);
278 // VG_(tm_error_print)(err);
279 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000280 default:
njn95ec8702004-11-22 16:46:13 +0000281 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000282 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000283 else {
njn95ec8702004-11-22 16:46:13 +0000284 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000285 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000286 err->ekind);
njn67993252004-11-22 18:02:32 +0000287 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000288 }
sewardjde4a1d02002-03-22 01:27:54 +0000289 }
sewardj71bc3cb2005-05-19 00:25:45 +0000290
291 if (VG_(clo_xml))
292 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000293}
294
nethercote04d0fbc2004-01-26 16:48:06 +0000295/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000296 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000297Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000298{
299 Char ch, ch2;
300 Int res;
301
njn43c799e2003-04-08 00:08:52 +0000302 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000303 return False;
304
305 VG_(message)(Vg_UserMsg, "");
306
307 again:
308 VG_(printf)(
309 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000310 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
311 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000312 );
313
sewardj6024b212003-07-13 10:54:33 +0000314 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000315 if (res != 1) goto ioerror;
316 /* res == 1 */
317 if (ch == '\n') return False;
318 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
319 && ch != 'C' && ch != 'c') goto again;
320
sewardj6024b212003-07-13 10:54:33 +0000321 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000322 if (res != 1) goto ioerror;
323 if (ch2 != '\n') goto again;
324
njn43c799e2003-04-08 00:08:52 +0000325 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000326 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000327 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000328 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000329 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000330 vg_assert(ch == 'c' || ch == 'C');
331
332 ioerror:
njn43c799e2003-04-08 00:08:52 +0000333 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000334 return False;
335}
336
337
sewardjb5f6f512005-03-10 23:59:00 +0000338/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000339static __inline__
njn72718642003-07-24 08:45:32 +0000340void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
341 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000342{
njnca82cc02004-11-22 17:18:48 +0000343 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000344
njn810086f2002-11-14 12:42:47 +0000345 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000346 err->next = NULL;
347 err->supp = NULL;
348 err->count = 1;
njn72718642003-07-24 08:45:32 +0000349 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000350 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000351 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000352 else
353 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000354
nethercote996901a2004-08-03 13:29:09 +0000355 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000356 err->ekind = ekind;
357 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000358 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000359 err->string = s;
360
njn25e49d8e72002-09-23 09:36:25 +0000361 /* sanity... */
njn72718642003-07-24 08:45:32 +0000362 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000363}
364
njnf4261312005-03-20 23:45:36 +0000365static void printSuppForIp(UInt n, Addr ip)
366{
njn47b209a2005-03-25 23:47:16 +0000367 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000368
njn47b209a2005-03-25 23:47:16 +0000369 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000370 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000371 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000372 VG_(printf)(" obj:%s\n", buf);
373 } else {
374 VG_(printf)(" ???:??? "
375 "# unknown, suppression will not work, sorry\n");
376 }
377}
378
nethercote10d481a2004-01-25 20:33:53 +0000379static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000380{
njn43c799e2003-04-08 00:08:52 +0000381 ExeContext* ec = VG_(get_error_where)(err);
382 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000383
sewardjb5f6f512005-03-10 23:59:00 +0000384 /* At most VG_MAX_SUPP_CALLERS names */
385 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000386 vg_assert(stop_at > 0);
387
388 VG_(printf)("{\n");
389 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000390
sewardjb5f6f512005-03-10 23:59:00 +0000391 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000392 VG_(printf)(" core:PThread\n");
393
394 } else {
njn51d827b2005-05-09 01:02:08 +0000395 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000396 if (NULL == name) {
397 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000398 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000399 return;
400 }
401 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000402 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000403 }
njn43c799e2003-04-08 00:08:52 +0000404
njnf4261312005-03-20 23:45:36 +0000405 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000406 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000407
408 VG_(printf)("}\n");
409}
410
njnb4aee052003-04-15 14:09:58 +0000411static
nethercote04d0fbc2004-01-26 16:48:06 +0000412void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000413{
sewardjd153fae2005-01-10 17:24:47 +0000414 Bool still_noisy = True;
415
nethercote04d0fbc2004-01-26 16:48:06 +0000416 /* Perhaps we want a debugger attach at this point? */
417 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000418 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
419 {
nethercote04d0fbc2004-01-26 16:48:06 +0000420 VG_(printf)("starting debugger\n");
421 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000422 }
njn43c799e2003-04-08 00:08:52 +0000423 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000424 if (VG_(clo_gen_suppressions) == 2
425 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000426 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000427 ) {
nethercote42602b12004-01-25 19:30:29 +0000428 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000429 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
430 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000431 }
432}
433
434/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
435 just for pretty printing purposes. */
436static Bool is_first_shown_context = True;
437
njn25e49d8e72002-09-23 09:36:25 +0000438/* Top-level entry point to the error management subsystem.
439 All detected errors are notified here; this routine decides if/when the
440 user should see the error. */
njn72718642003-07-24 08:45:32 +0000441void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000442 ErrorKind ekind, Addr a, Char* s, void* extra )
443{
njn810086f2002-11-14 12:42:47 +0000444 Error err;
445 Error* p;
446 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000447 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000448 VgRes exe_res = Vg_MedRes;
449 static Bool stopping_message = False;
450 static Bool slowdown_message = False;
451 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000452
njn14319cc2005-03-13 06:26:22 +0000453 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
454 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000455 have been found, just refuse to collect any more. This stops
456 the burden of the error-management system becoming excessive in
457 extremely buggy programs, although it does make it pretty
458 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000459 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000460 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000461 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000462 if (!stopping_message) {
463 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000464
njn695c16e2005-03-27 03:40:28 +0000465 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000466 VG_(message)(Vg_UserMsg,
467 "More than %d different errors detected. "
468 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000469 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000470 } else {
471 VG_(message)(Vg_UserMsg,
472 "More than %d total errors detected. "
473 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000474 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000475 }
476
sewardjde4a1d02002-03-22 01:27:54 +0000477 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000478 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000479 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000480 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000481 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000482 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000483 VG_(message)(Vg_UserMsg,
484 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000485 VG_(message)(Vg_UserMsg, "");
486 stopping_message = True;
487 }
488 return;
489 }
490
njn14319cc2005-03-13 06:26:22 +0000491 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000492 been found, be much more conservative about collecting new
493 ones. */
njn695c16e2005-03-27 03:40:28 +0000494 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000495 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000496 if (!slowdown_message) {
497 VG_(message)(Vg_UserMsg, "");
498 VG_(message)(Vg_UserMsg,
499 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000500 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000501 VG_(message)(Vg_UserMsg,
502 "will still be recorded, but in less detail than before.");
503 slowdown_message = True;
504 }
505 }
506
njn25e49d8e72002-09-23 09:36:25 +0000507 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000508 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000509
510 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000511 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000512 p_prev = NULL;
513 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000514 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000515 /* Found it. */
516 p->count++;
517 if (p->supp != NULL) {
518 /* Deal correctly with suppressed errors. */
519 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000520 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000521 } else {
nethercotef2b11482004-08-02 12:36:01 +0000522 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000523 }
524
525 /* Move p to the front of the list so that future searches
526 for it are faster. */
527 if (p_prev != NULL) {
528 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000529 p_prev->next = p->next;
530 p->next = errors;
531 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000532 }
sewardj7ebf7c32003-07-24 21:29:40 +0000533
sewardjde4a1d02002-03-22 01:27:54 +0000534 return;
535 }
536 p_prev = p;
537 p = p->next;
538 }
539
540 /* Didn't see it. Copy and add. */
541
njn43c799e2003-04-08 00:08:52 +0000542 /* OK, we're really going to collect it. The context is on the stack and
543 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000544 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000545
njn02bc4b82005-05-15 17:28:26 +0000546 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000547 is for when there are more details to fill in which take time to work
548 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000549 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000550 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000551
njn02bc4b82005-05-15 17:28:26 +0000552 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000553 VG_(tdict).tool_update_extra returned. Also allow for people using
554 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000555 */
556
557 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000558 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000559 *p = err;
njn43c799e2003-04-08 00:08:52 +0000560
njn02bc4b82005-05-15 17:28:26 +0000561 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000562 switch (ekind) {
563 // case ThreadErr:
564 // case MutexErr:
565 // vg_assert(VG_(needs).core_errors);
566 // extra_size = VG_(tm_error_update_extra)(p);
567 // break;
568 default:
569 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000570 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000571 break;
572 }
njn43c799e2003-04-08 00:08:52 +0000573
njn02bc4b82005-05-15 17:28:26 +0000574 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000575 if (NULL != p->extra && 0 != extra_size) {
576 void* new_extra = VG_(malloc)(extra_size);
577 VG_(memcpy)(new_extra, p->extra, extra_size);
578 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000579 }
580
njn695c16e2005-03-27 03:40:28 +0000581 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000582 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000583 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000584 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000585 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000586 if (!is_first_shown_context)
587 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000588 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000589 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000590 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000591 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000592 } else {
nethercotef2b11482004-08-02 12:36:01 +0000593 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000594 p->supp->count++;
595 }
596}
597
njn43c799e2003-04-08 00:08:52 +0000598/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000599 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000600 guaranteed to only happen once. This avoids all the recording and
601 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000602 suppressed. Bool 'print_error' dictates whether to print the error.
603 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000604*/
njn72718642003-07-24 08:45:32 +0000605Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000606 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000607 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000608{
609 Error err;
610
611 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000612 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000613
614 /* Unless it's suppressed, we're going to show it. Don't need to make
615 a copy, because it's only temporary anyway.
616
njn02bc4b82005-05-15 17:28:26 +0000617 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000618 because that can have an affect on whether it's suppressed. Ignore
619 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000620 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000621 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000622
623 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000624 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000625 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000626
627 if (print_error) {
628 if (!is_first_shown_context)
629 VG_(message)(Vg_UserMsg, "");
630 pp_Error(&err, False);
631 is_first_shown_context = False;
632 }
nethercote04d0fbc2004-01-26 16:48:06 +0000633 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000634
635 return False;
636
637 } else {
nethercotef2b11482004-08-02 12:36:01 +0000638 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000639 return True;
640 }
641}
642
sewardjde4a1d02002-03-22 01:27:54 +0000643
sewardjde4a1d02002-03-22 01:27:54 +0000644/*------------------------------------------------------------*/
645/*--- Exported fns ---*/
646/*------------------------------------------------------------*/
647
sewardj71bc3cb2005-05-19 00:25:45 +0000648/* Show the used suppressions. Returns False if no suppression
649 got used. */
650static Bool show_used_suppressions ( void )
651{
652 Supp *su;
653 Bool any_supp;
654
sewardj7c9e57c2005-05-24 14:21:45 +0000655 if (VG_(clo_xml))
656 VG_(message)(Vg_DebugMsg, "<suppcounts>");
657
sewardj71bc3cb2005-05-19 00:25:45 +0000658 any_supp = False;
659 for (su = suppressions; su != NULL; su = su->next) {
660 if (su->count <= 0)
661 continue;
662 any_supp = True;
663 if (VG_(clo_xml)) {
664 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000665 " <pair>\n"
666 " <count>%d</count>\n"
667 " <name>%s</name>\n"
668 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000669 su->count, su->sname);
670 } else {
671 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
672 }
673 }
674
sewardj7c9e57c2005-05-24 14:21:45 +0000675 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000676 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000677
sewardj71bc3cb2005-05-19 00:25:45 +0000678 return any_supp;
679}
680
681
sewardj9f297ca2005-05-20 02:29:52 +0000682/* Show all the errors that occurred, and possibly also the
683 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000684void VG_(show_all_errors) ( void )
685{
njn810086f2002-11-14 12:42:47 +0000686 Int i, n_min;
687 Int n_err_contexts, n_supp_contexts;
688 Error *p, *p_min;
689 Supp *su;
690 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000691
692 if (VG_(clo_verbosity) == 0)
693 return;
694
695 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000696 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000697 if (p->supp == NULL)
698 n_err_contexts++;
699 }
700
701 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000702 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000703 if (su->count > 0)
704 n_supp_contexts++;
705 }
sewardj71bc3cb2005-05-19 00:25:45 +0000706
707 /* If we're printing XML, just show the suppressions and stop.
708 */
709 if (VG_(clo_xml)) {
710 (void)show_used_suppressions();
711 return;
712 }
713
714 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000715 VG_(message)(Vg_UserMsg,
716 "ERROR SUMMARY: "
717 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000718 n_errs_found, n_err_contexts,
719 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000720
721 if (VG_(clo_verbosity) <= 1)
722 return;
723
724 /* Print the contexts in order of increasing error count. */
725 for (i = 0; i < n_err_contexts; i++) {
726 n_min = (1 << 30) - 1;
727 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000728 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000729 if (p->supp != NULL) continue;
730 if (p->count < n_min) {
731 n_min = p->count;
732 p_min = p;
733 }
734 }
njn67993252004-11-22 18:02:32 +0000735 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000736
737 VG_(message)(Vg_UserMsg, "");
738 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
739 p_min->count,
740 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000741 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000742
743 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000744 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000745 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000746 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000747 }
748
749 p_min->count = 1 << 30;
750 }
751
752 if (n_supp_contexts > 0)
753 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000754 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000755
756 if (n_err_contexts > 0) {
757 if (any_supp)
758 VG_(message)(Vg_UserMsg, "");
759 VG_(message)(Vg_UserMsg,
760 "IN SUMMARY: "
761 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000762 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000763 n_supp_contexts );
764 VG_(message)(Vg_UserMsg, "");
765 }
766}
767
sewardj9f297ca2005-05-20 02:29:52 +0000768
769/* Show occurrence counts of all errors, in XML form. */
770void VG_(show_error_counts_as_XML) ( void )
771{
772 Error* err;
773 VG_(message)(Vg_UserMsg, "<errorcounts>");
774 for (err = errors; err != NULL; err = err->next) {
775 if (err->supp != NULL)
776 continue;
777 if (err->count <= 0)
778 continue;
779 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000780 Vg_UserMsg, " <pair> <count>%d</count> "
781 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000782 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000783 );
784 }
785 VG_(message)(Vg_UserMsg, "</errorcounts>");
786}
787
788
sewardjde4a1d02002-03-22 01:27:54 +0000789/*------------------------------------------------------------*/
790/*--- Standard suppressions ---*/
791/*------------------------------------------------------------*/
792
793/* Get a non-blank, non-comment line of at most nBuf chars from fd.
794 Skips leading spaces on the line. Return True if EOF was hit instead.
795*/
njn4ba5a792002-09-30 10:23:54 +0000796Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000797{
798 Char ch;
799 Int n, i;
800 while (True) {
801 /* First, read until a non-blank char appears. */
802 while (True) {
803 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000804 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000805 if (n == 0) return True;
806 }
807
808 /* Now, read the line into buf. */
809 i = 0;
810 buf[i++] = ch; buf[i] = 0;
811 while (True) {
812 n = VG_(read)(fd, &ch, 1);
813 if (n == 0) return False; /* the next call will return True */
814 if (ch == '\n') break;
815 if (i > 0 && i == nBuf-1) i--;
816 buf[i++] = ch; buf[i] = 0;
817 }
njn0c0f32a2005-03-26 04:14:01 +0000818 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000819 i--; buf[i] = 0;
820 };
821
njn02bc4b82005-05-15 17:28:26 +0000822 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000823 /* Ok, we have a line. If a non-comment line, return.
824 If a comment line, start all over again. */
825 if (buf[0] != '#') return False;
826 }
827}
828
829
830/* *p_caller contains the raw name of a caller, supposedly either
831 fun:some_function_name or
832 obj:some_object_name.
833 Set *p_ty accordingly and advance *p_caller over the descriptor
834 (fun: or obj:) part.
835 Returns False if failed.
836*/
sewardjb5f6f512005-03-10 23:59:00 +0000837static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000838{
sewardjb5f6f512005-03-10 23:59:00 +0000839 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
840 p->name += 4;
841 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000842 return True;
843 }
sewardjb5f6f512005-03-10 23:59:00 +0000844 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
845 p->name += 4;
846 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000847 return True;
848 }
849 VG_(printf)("location should start with fun: or obj:\n");
850 return False;
851}
852
853
nethercote7cc9c232004-01-21 15:08:04 +0000854/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000855static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000856Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000857{
858 Bool found;
859 Char *s = NULL; /* Shut gcc up */
860 Int len = VG_(strlen)(name);
861
862 found = (NULL != (s = VG_(strstr)(names, name)) &&
863 (s == names || *(s-1) == ',') &&
864 (*(s+len) == ',' || *(s+len) == '\0')
865 );
866
867 return found;
868}
869
njn695c16e2005-03-27 03:40:28 +0000870/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000871 and place them in the suppressions list. If there's any difficulty
872 doing this, just give up -- there's no point in trying to recover.
873*/
sewardjde4a1d02002-03-22 01:27:54 +0000874static void load_one_suppressions_file ( Char* filename )
875{
876# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000877 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000878 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000879 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000880 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000881 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000882 Char* err_str = NULL;
883 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000884
njn25e49d8e72002-09-23 09:36:25 +0000885 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000886 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000887 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000888 filename );
889 VG_(exit)(1);
890 }
891
sewardjb5f6f512005-03-10 23:59:00 +0000892#define BOMB(S) { err_str = S; goto syntax_error; }
893
sewardjde4a1d02002-03-22 01:27:54 +0000894 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000895 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000896 Supp* supp;
897 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000898 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000899
900 // Initialise temporary reading-in buffer.
901 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
902 tmp_callers[i].ty = NoName;
903 tmp_callers[i].name = NULL;
904 }
905
njn810086f2002-11-14 12:42:47 +0000906 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000907
njn4ba5a792002-09-30 10:23:54 +0000908 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000909 if (eof) break;
910
sewardjb5f6f512005-03-10 23:59:00 +0000911 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000912
njn4ba5a792002-09-30 10:23:54 +0000913 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000914
915 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
916
njn25e49d8e72002-09-23 09:36:25 +0000917 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000918
njn4ba5a792002-09-30 10:23:54 +0000919 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000920
sewardjb5f6f512005-03-10 23:59:00 +0000921 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000922
njn94065fd2004-11-22 19:26:27 +0000923 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000924 i = 0;
925 while (True) {
926 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000927 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000928 i++;
njn25e49d8e72002-09-23 09:36:25 +0000929 }
njnc40c3a82002-10-02 11:02:27 +0000930 buf[i] = '\0'; /* Replace ':', splitting into two strings */
931
nethercote7cc9c232004-01-21 15:08:04 +0000932 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000933 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000934
nethercote7cc9c232004-01-21 15:08:04 +0000935 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000936 {
sewardjb5f6f512005-03-10 23:59:00 +0000937 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000938 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000939 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000940 else
sewardjb5f6f512005-03-10 23:59:00 +0000941 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000942 }
njn95ec8702004-11-22 16:46:13 +0000943 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000944 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000945 {
sewardjb5f6f512005-03-10 23:59:00 +0000946 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000947 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000948 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000949 } else {
950 BOMB("unknown tool suppression type");
951 }
njnc40c3a82002-10-02 11:02:27 +0000952 }
njn25e49d8e72002-09-23 09:36:25 +0000953 else {
sewardjb5f6f512005-03-10 23:59:00 +0000954 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000955 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000956 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000957 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000958 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000959 break;
960 }
961 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000962 }
963
njn95ec8702004-11-22 16:46:13 +0000964 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000965 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000966 {
967 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000968 }
969
sewardjb5f6f512005-03-10 23:59:00 +0000970 i = 0;
971 while (True) {
972 eof = VG_(get_line) ( fd, buf, N_BUF );
973 if (eof)
974 BOMB("unexpected end-of-file");
975 if (VG_STREQ(buf, "}")) {
976 if (i > 0) {
977 break;
978 } else {
979 BOMB("missing stack trace");
980 }
981 }
982 if (i == VG_MAX_SUPP_CALLERS)
983 BOMB("too many callers in stack trace");
984 if (i > 0 && i >= VG_(clo_backtrace_size))
985 break;
986 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
987 if (!setLocationTy(&(tmp_callers[i])))
988 BOMB("location should start with 'fun:' or 'obj:'");
989 i++;
990 }
991
992 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
993 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000994 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000995 do {
996 eof = VG_(get_line) ( fd, buf, N_BUF );
997 } while (!eof && !VG_STREQ(buf, "}"));
998 }
999
1000 // Copy tmp_callers[] into supp->callers[]
1001 supp->n_callers = i;
1002 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1003 for (i = 0; i < supp->n_callers; i++) {
1004 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001005 }
1006
njn695c16e2005-03-27 03:40:28 +00001007 supp->next = suppressions;
1008 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001009 }
sewardjde4a1d02002-03-22 01:27:54 +00001010 VG_(close)(fd);
1011 return;
1012
1013 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001014 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001015 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001016
sewardjde4a1d02002-03-22 01:27:54 +00001017 VG_(close)(fd);
1018 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001019 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001020
sewardjb5f6f512005-03-10 23:59:00 +00001021# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001022# undef N_BUF
1023}
1024
1025
1026void VG_(load_suppressions) ( void )
1027{
1028 Int i;
njn695c16e2005-03-27 03:40:28 +00001029 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001030 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1031 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001032 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1033 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001034 }
1035 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1036 }
1037}
1038
sewardjb5f6f512005-03-10 23:59:00 +00001039static
njn810086f2002-11-14 12:42:47 +00001040Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001041{
njn810086f2002-11-14 12:42:47 +00001042 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001043 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001044 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001045 default:
njn95ec8702004-11-22 16:46:13 +00001046 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001047 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001048 } else {
1049 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001050 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001051 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001052 err->ekind);
njn67993252004-11-22 18:02:32 +00001053 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001054 }
1055 }
1056}
1057
sewardjb5f6f512005-03-10 23:59:00 +00001058static
1059Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001060{
1061 Int i;
njn47b209a2005-03-25 23:47:16 +00001062 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001063 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001064
sewardjb5f6f512005-03-10 23:59:00 +00001065 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001066 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001067 vg_assert(su->callers[i].name != NULL);
1068 switch (su->callers[i].ty) {
1069 case ObjName:
njn47b209a2005-03-25 23:47:16 +00001070 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001071 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001072 break;
1073
1074 case FunName:
1075 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001076 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001077 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001078 break;
njn67993252004-11-22 18:02:32 +00001079 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001080 }
sewardjb5f6f512005-03-10 23:59:00 +00001081 if (!VG_(string_match)(su->callers[i].name, caller_name))
1082 return False;
njn25e49d8e72002-09-23 09:36:25 +00001083 }
1084
1085 /* If we reach here, it's a match */
1086 return True;
1087}
sewardjde4a1d02002-03-22 01:27:54 +00001088
njn810086f2002-11-14 12:42:47 +00001089/* Does an error context match a suppression? ie is this a suppressible
1090 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001091 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001092*/
njn810086f2002-11-14 12:42:47 +00001093static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001094{
njn810086f2002-11-14 12:42:47 +00001095 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001096
sewardjde4a1d02002-03-22 01:27:54 +00001097 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001098 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001099 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001100 supp_matches_callers(err, su))
1101 {
njn25e49d8e72002-09-23 09:36:25 +00001102 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001103 }
sewardjde4a1d02002-03-22 01:27:54 +00001104 }
njn25e49d8e72002-09-23 09:36:25 +00001105 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001106}
1107
sewardjde4a1d02002-03-22 01:27:54 +00001108/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00001109/*--- end m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001110/*--------------------------------------------------------------------*/