blob: 34a3219bb3c82ed6f29e7673c0a5286abc81cbf2 [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"
njn04e16982005-05-31 00:23:43 +000036#include "pub_core_main.h" // for VG_(start_debugger)()
njn20242342005-05-16 23:31:24 +000037#include "pub_core_options.h"
njnd2b17112005-04-19 04:10:25 +000038#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000039#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000040#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000041
42/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000043/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000044/*------------------------------------------------------------*/
45
njn14319cc2005-03-13 06:26:22 +000046/* After this many different unsuppressed errors have been observed,
47 be more conservative about collecting new ones. */
48#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
49
50/* After this many different unsuppressed errors have been observed,
51 stop collecting errors at all, and tell the user their program is
52 evidently a steaming pile of camel dung. */
53#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
54
55/* After this many total errors have been observed, stop collecting
56 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
57#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
58
sewardjde4a1d02002-03-22 01:27:54 +000059/* The list of error contexts found, both suppressed and unsuppressed.
60 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000061static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000062
63/* The list of suppression directives, as read from the specified
64 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000065static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000066
67/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000068static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000069
70/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000071static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000072
73/* forwards ... */
njn810086f2002-11-14 12:42:47 +000074static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000075
sewardjb5f6f512005-03-10 23:59:00 +000076static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000077
78/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000079/*--- Error type ---*/
80/*------------------------------------------------------------*/
81
nethercote996901a2004-08-03 13:29:09 +000082/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000083 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000084
85/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000086 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000087 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000088 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000089
90 When errors are found and recorded with VG_(maybe_record_error)(), all
91 the tool must do is pass in the four parameters; core will
92 allocate/initialise the error record.
93*/
94struct _Error {
95 struct _Error* next;
96 // NULL if unsuppressed; or ptr to suppression record.
97 Supp* supp;
98 Int count;
99 ThreadId tid;
100
101 // The tool-specific part
102 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000103 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000104 Addr addr; // Used frequently
105 Char* string; // Used frequently
106 void* extra; // For any tool-specific extras
107};
108
109ExeContext* VG_(get_error_where) ( Error* err )
110{
111 return err->where;
112}
113
114ErrorKind VG_(get_error_kind) ( Error* err )
115{
116 return err->ekind;
117}
118
119Addr VG_(get_error_address) ( Error* err )
120{
121 return err->addr;
122}
123
124Char* VG_(get_error_string) ( Error* err )
125{
126 return err->string;
127}
128
129void* VG_(get_error_extra) ( Error* err )
130{
131 return err->extra;
132}
133
nethercotef2b11482004-08-02 12:36:01 +0000134UInt VG_(get_n_errs_found)( void )
135{
136 return n_errs_found;
137}
138
nethercote4a184902004-08-02 12:21:09 +0000139/*------------------------------------------------------------*/
140/*--- Suppression type ---*/
141/*------------------------------------------------------------*/
142
143/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
144 * effectively extend it by defining their own enums in the (0..) range. */
145typedef
146 enum {
147 PThreadSupp = -1, /* Matches PThreadErr */
148 }
149 CoreSuppKind;
150
sewardjb5f6f512005-03-10 23:59:00 +0000151/* Max number of callers for context in a suppression. */
152#define VG_MAX_SUPP_CALLERS 24
153
nethercote4a184902004-08-02 12:21:09 +0000154/* For each caller specified for a suppression, record the nature of
155 the caller name. Not of interest to tools. */
156typedef
157 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000158 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000159 ObjName, /* Name is of an shared object file. */
160 FunName /* Name is of a function. */
161 }
162 SuppLocTy;
163
sewardjb5f6f512005-03-10 23:59:00 +0000164typedef
165 struct {
166 SuppLocTy ty;
167 Char* name;
168 }
169 SuppLoc;
170
nethercote4a184902004-08-02 12:21:09 +0000171/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000172 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000173 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000174 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000175struct _Supp {
176 struct _Supp* next;
177 Int count; // The number of times this error has been suppressed.
178 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000179
180 // Length of 'callers'
181 Int n_callers;
182 // Array of callers, for matching stack traces. First one (name of fn
183 // where err occurs) is mandatory; rest are optional.
184 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000185
186 /* The tool-specific part */
187 SuppKind skind; // What kind of suppression. Must use the range (0..).
188 Char* string; // String -- use is optional. NULL by default.
189 void* extra; // Anything else -- use is optional. NULL by default.
190};
191
192SuppKind VG_(get_supp_kind) ( Supp* su )
193{
194 return su->skind;
195}
196
197Char* VG_(get_supp_string) ( Supp* su )
198{
199 return su->string;
200}
201
202void* VG_(get_supp_extra) ( Supp* su )
203{
204 return su->extra;
205}
206
207
208void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
209{
210 su->skind = skind;
211}
212
213void VG_(set_supp_string) ( Supp* su, Char* string )
214{
215 su->string = string;
216}
217
218void VG_(set_supp_extra) ( Supp* su, void* extra )
219{
220 su->extra = extra;
221}
222
223
224/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000225/*--- Helper fns ---*/
226/*------------------------------------------------------------*/
227
sewardjde4a1d02002-03-22 01:27:54 +0000228/* Compare error contexts, to detect duplicates. Note that if they
229 are otherwise the same, the faulting addrs and associated rwoffsets
230 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000231static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000232{
njn810086f2002-11-14 12:42:47 +0000233 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000234 return False;
njn25e49d8e72002-09-23 09:36:25 +0000235 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000236 return False;
237
njn810086f2002-11-14 12:42:47 +0000238 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000239 // case ThreadErr:
240 // case MutexErr:
241 // vg_assert(VG_(needs).core_errors);
242 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000243 default:
njn51d827b2005-05-09 01:02:08 +0000244 if (VG_(needs).tool_errors) {
245 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
246 } else {
njn95ec8702004-11-22 16:46:13 +0000247 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000248 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000249 e1->ekind);
njn67993252004-11-22 18:02:32 +0000250 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000251 }
sewardjde4a1d02002-03-22 01:27:54 +0000252 }
253}
254
njn810086f2002-11-14 12:42:47 +0000255static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000256{
sewardj71bc3cb2005-05-19 00:25:45 +0000257 if (VG_(clo_xml)) {
258 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000259 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
260 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000261 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
262 }
263
264 if (!VG_(clo_xml)) {
265 if (printCount)
266 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
267 if (err->tid > 0 && err->tid != last_tid_printed) {
268 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
269 last_tid_printed = err->tid;
270 }
sewardjb5f6f512005-03-10 23:59:00 +0000271 }
njn25e49d8e72002-09-23 09:36:25 +0000272
njn810086f2002-11-14 12:42:47 +0000273 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000274 // case ThreadErr:
275 // case MutexErr:
276 // vg_assert(VG_(needs).core_errors);
277 // VG_(tm_error_print)(err);
278 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000279 default:
njn95ec8702004-11-22 16:46:13 +0000280 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000281 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000282 else {
njn95ec8702004-11-22 16:46:13 +0000283 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000284 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000285 err->ekind);
njn67993252004-11-22 18:02:32 +0000286 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000287 }
sewardjde4a1d02002-03-22 01:27:54 +0000288 }
sewardj71bc3cb2005-05-19 00:25:45 +0000289
290 if (VG_(clo_xml))
291 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000292}
293
nethercote04d0fbc2004-01-26 16:48:06 +0000294/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000295 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000296Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000297{
298 Char ch, ch2;
299 Int res;
300
njn43c799e2003-04-08 00:08:52 +0000301 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000302 return False;
303
304 VG_(message)(Vg_UserMsg, "");
305
306 again:
307 VG_(printf)(
308 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000309 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
310 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000311 );
312
sewardj6024b212003-07-13 10:54:33 +0000313 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000314 if (res != 1) goto ioerror;
315 /* res == 1 */
316 if (ch == '\n') return False;
317 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
318 && ch != 'C' && ch != 'c') goto again;
319
sewardj6024b212003-07-13 10:54:33 +0000320 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000321 if (res != 1) goto ioerror;
322 if (ch2 != '\n') goto again;
323
njn43c799e2003-04-08 00:08:52 +0000324 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000325 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000326 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000327 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000328 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000329 vg_assert(ch == 'c' || ch == 'C');
330
331 ioerror:
njn43c799e2003-04-08 00:08:52 +0000332 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000333 return False;
334}
335
336
sewardjb5f6f512005-03-10 23:59:00 +0000337/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000338static __inline__
njn72718642003-07-24 08:45:32 +0000339void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
340 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000341{
njnca82cc02004-11-22 17:18:48 +0000342 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000343
njn810086f2002-11-14 12:42:47 +0000344 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000345 err->next = NULL;
346 err->supp = NULL;
347 err->count = 1;
njn72718642003-07-24 08:45:32 +0000348 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000349 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000350 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000351 else
352 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000353
nethercote996901a2004-08-03 13:29:09 +0000354 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000355 err->ekind = ekind;
356 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000357 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000358 err->string = s;
359
njn25e49d8e72002-09-23 09:36:25 +0000360 /* sanity... */
njn72718642003-07-24 08:45:32 +0000361 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000362}
363
njnf4261312005-03-20 23:45:36 +0000364static void printSuppForIp(UInt n, Addr ip)
365{
njn47b209a2005-03-25 23:47:16 +0000366 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000367
njn47b209a2005-03-25 23:47:16 +0000368 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000369 VG_(printf)(" fun:%s\n", buf);
njnbc93cc02005-05-15 21:09:40 +0000370 } else if ( VG_(get_objname)(ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000371 VG_(printf)(" obj:%s\n", buf);
372 } else {
373 VG_(printf)(" ???:??? "
374 "# unknown, suppression will not work, sorry\n");
375 }
376}
377
nethercote10d481a2004-01-25 20:33:53 +0000378static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000379{
njn43c799e2003-04-08 00:08:52 +0000380 ExeContext* ec = VG_(get_error_where)(err);
381 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000382
sewardjb5f6f512005-03-10 23:59:00 +0000383 /* At most VG_MAX_SUPP_CALLERS names */
384 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000385 vg_assert(stop_at > 0);
386
387 VG_(printf)("{\n");
388 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000389
sewardjb5f6f512005-03-10 23:59:00 +0000390 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000391 VG_(printf)(" core:PThread\n");
392
393 } else {
njn51d827b2005-05-09 01:02:08 +0000394 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000395 if (NULL == name) {
396 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000397 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000398 return;
399 }
400 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000401 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000402 }
njn43c799e2003-04-08 00:08:52 +0000403
njnf4261312005-03-20 23:45:36 +0000404 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000405 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000406
407 VG_(printf)("}\n");
408}
409
njnb4aee052003-04-15 14:09:58 +0000410static
nethercote04d0fbc2004-01-26 16:48:06 +0000411void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000412{
sewardjd153fae2005-01-10 17:24:47 +0000413 Bool still_noisy = True;
414
nethercote04d0fbc2004-01-26 16:48:06 +0000415 /* Perhaps we want a debugger attach at this point? */
416 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000417 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
418 {
nethercote04d0fbc2004-01-26 16:48:06 +0000419 VG_(printf)("starting debugger\n");
420 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000421 }
njn43c799e2003-04-08 00:08:52 +0000422 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000423 if (VG_(clo_gen_suppressions) == 2
424 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000425 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000426 ) {
nethercote42602b12004-01-25 19:30:29 +0000427 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000428 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
429 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000430 }
431}
432
433/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
434 just for pretty printing purposes. */
435static Bool is_first_shown_context = True;
436
njn25e49d8e72002-09-23 09:36:25 +0000437/* Top-level entry point to the error management subsystem.
438 All detected errors are notified here; this routine decides if/when the
439 user should see the error. */
njn72718642003-07-24 08:45:32 +0000440void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000441 ErrorKind ekind, Addr a, Char* s, void* extra )
442{
njn810086f2002-11-14 12:42:47 +0000443 Error err;
444 Error* p;
445 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000446 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000447 VgRes exe_res = Vg_MedRes;
448 static Bool stopping_message = False;
449 static Bool slowdown_message = False;
450 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000451
njn14319cc2005-03-13 06:26:22 +0000452 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
453 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000454 have been found, just refuse to collect any more. This stops
455 the burden of the error-management system becoming excessive in
456 extremely buggy programs, although it does make it pretty
457 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000458 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000459 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000460 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000461 if (!stopping_message) {
462 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000463
njn695c16e2005-03-27 03:40:28 +0000464 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000465 VG_(message)(Vg_UserMsg,
466 "More than %d different errors detected. "
467 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000468 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000469 } else {
470 VG_(message)(Vg_UserMsg,
471 "More than %d total errors detected. "
472 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000473 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000474 }
475
sewardjde4a1d02002-03-22 01:27:54 +0000476 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000477 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000478 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000479 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000480 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000481 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000482 VG_(message)(Vg_UserMsg,
483 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000484 VG_(message)(Vg_UserMsg, "");
485 stopping_message = True;
486 }
487 return;
488 }
489
njn14319cc2005-03-13 06:26:22 +0000490 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000491 been found, be much more conservative about collecting new
492 ones. */
njn695c16e2005-03-27 03:40:28 +0000493 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000494 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000495 if (!slowdown_message) {
496 VG_(message)(Vg_UserMsg, "");
497 VG_(message)(Vg_UserMsg,
498 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000499 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000500 VG_(message)(Vg_UserMsg,
501 "will still be recorded, but in less detail than before.");
502 slowdown_message = True;
503 }
504 }
505
njn25e49d8e72002-09-23 09:36:25 +0000506 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000507 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000508
509 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000510 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000511 p_prev = NULL;
512 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000513 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000514 /* Found it. */
515 p->count++;
516 if (p->supp != NULL) {
517 /* Deal correctly with suppressed errors. */
518 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000519 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000520 } else {
nethercotef2b11482004-08-02 12:36:01 +0000521 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000522 }
523
524 /* Move p to the front of the list so that future searches
525 for it are faster. */
526 if (p_prev != NULL) {
527 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000528 p_prev->next = p->next;
529 p->next = errors;
530 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000531 }
sewardj7ebf7c32003-07-24 21:29:40 +0000532
sewardjde4a1d02002-03-22 01:27:54 +0000533 return;
534 }
535 p_prev = p;
536 p = p->next;
537 }
538
539 /* Didn't see it. Copy and add. */
540
njn43c799e2003-04-08 00:08:52 +0000541 /* OK, we're really going to collect it. The context is on the stack and
542 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000543 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000544
njn02bc4b82005-05-15 17:28:26 +0000545 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000546 is for when there are more details to fill in which take time to work
547 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000548 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000549 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000550
njn02bc4b82005-05-15 17:28:26 +0000551 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000552 VG_(tdict).tool_update_extra returned. Also allow for people using
553 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000554 */
555
556 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000557 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000558 *p = err;
njn43c799e2003-04-08 00:08:52 +0000559
njn02bc4b82005-05-15 17:28:26 +0000560 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000561 switch (ekind) {
562 // case ThreadErr:
563 // case MutexErr:
564 // vg_assert(VG_(needs).core_errors);
565 // extra_size = VG_(tm_error_update_extra)(p);
566 // break;
567 default:
568 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000569 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000570 break;
571 }
njn43c799e2003-04-08 00:08:52 +0000572
njn02bc4b82005-05-15 17:28:26 +0000573 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000574 if (NULL != p->extra && 0 != extra_size) {
575 void* new_extra = VG_(malloc)(extra_size);
576 VG_(memcpy)(new_extra, p->extra, extra_size);
577 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000578 }
579
njn695c16e2005-03-27 03:40:28 +0000580 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000581 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000582 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000583 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000584 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000585 if (!is_first_shown_context)
586 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000587 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000588 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000589 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000590 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000591 } else {
nethercotef2b11482004-08-02 12:36:01 +0000592 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000593 p->supp->count++;
594 }
595}
596
njn43c799e2003-04-08 00:08:52 +0000597/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000598 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000599 guaranteed to only happen once. This avoids all the recording and
600 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000601 suppressed. Bool 'print_error' dictates whether to print the error.
602 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000603*/
njn72718642003-07-24 08:45:32 +0000604Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000605 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000606 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000607{
608 Error err;
609
610 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000611 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000612
613 /* Unless it's suppressed, we're going to show it. Don't need to make
614 a copy, because it's only temporary anyway.
615
njn02bc4b82005-05-15 17:28:26 +0000616 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000617 because that can have an affect on whether it's suppressed. Ignore
618 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000619 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000620 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000621
622 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000623 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000624 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000625
626 if (print_error) {
627 if (!is_first_shown_context)
628 VG_(message)(Vg_UserMsg, "");
629 pp_Error(&err, False);
630 is_first_shown_context = False;
631 }
nethercote04d0fbc2004-01-26 16:48:06 +0000632 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000633
634 return False;
635
636 } else {
nethercotef2b11482004-08-02 12:36:01 +0000637 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000638 return True;
639 }
640}
641
sewardjde4a1d02002-03-22 01:27:54 +0000642
sewardjde4a1d02002-03-22 01:27:54 +0000643/*------------------------------------------------------------*/
644/*--- Exported fns ---*/
645/*------------------------------------------------------------*/
646
sewardj71bc3cb2005-05-19 00:25:45 +0000647/* Show the used suppressions. Returns False if no suppression
648 got used. */
649static Bool show_used_suppressions ( void )
650{
651 Supp *su;
652 Bool any_supp;
653
sewardj7c9e57c2005-05-24 14:21:45 +0000654 if (VG_(clo_xml))
655 VG_(message)(Vg_DebugMsg, "<suppcounts>");
656
sewardj71bc3cb2005-05-19 00:25:45 +0000657 any_supp = False;
658 for (su = suppressions; su != NULL; su = su->next) {
659 if (su->count <= 0)
660 continue;
661 any_supp = True;
662 if (VG_(clo_xml)) {
663 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000664 " <pair>\n"
665 " <count>%d</count>\n"
666 " <name>%s</name>\n"
667 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000668 su->count, su->sname);
669 } else {
670 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
671 }
672 }
673
sewardj7c9e57c2005-05-24 14:21:45 +0000674 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000675 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000676
sewardj71bc3cb2005-05-19 00:25:45 +0000677 return any_supp;
678}
679
680
sewardj9f297ca2005-05-20 02:29:52 +0000681/* Show all the errors that occurred, and possibly also the
682 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000683void VG_(show_all_errors) ( void )
684{
njn810086f2002-11-14 12:42:47 +0000685 Int i, n_min;
686 Int n_err_contexts, n_supp_contexts;
687 Error *p, *p_min;
688 Supp *su;
689 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000690
691 if (VG_(clo_verbosity) == 0)
692 return;
693
694 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000695 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000696 if (p->supp == NULL)
697 n_err_contexts++;
698 }
699
700 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000701 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000702 if (su->count > 0)
703 n_supp_contexts++;
704 }
sewardj71bc3cb2005-05-19 00:25:45 +0000705
706 /* If we're printing XML, just show the suppressions and stop.
707 */
708 if (VG_(clo_xml)) {
709 (void)show_used_suppressions();
710 return;
711 }
712
713 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000714 VG_(message)(Vg_UserMsg,
715 "ERROR SUMMARY: "
716 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000717 n_errs_found, n_err_contexts,
718 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000719
720 if (VG_(clo_verbosity) <= 1)
721 return;
722
723 /* Print the contexts in order of increasing error count. */
724 for (i = 0; i < n_err_contexts; i++) {
725 n_min = (1 << 30) - 1;
726 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000727 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000728 if (p->supp != NULL) continue;
729 if (p->count < n_min) {
730 n_min = p->count;
731 p_min = p;
732 }
733 }
njn67993252004-11-22 18:02:32 +0000734 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000735
736 VG_(message)(Vg_UserMsg, "");
737 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
738 p_min->count,
739 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000740 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000741
742 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000743 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000744 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000745 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000746 }
747
748 p_min->count = 1 << 30;
749 }
750
751 if (n_supp_contexts > 0)
752 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000753 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000754
755 if (n_err_contexts > 0) {
756 if (any_supp)
757 VG_(message)(Vg_UserMsg, "");
758 VG_(message)(Vg_UserMsg,
759 "IN SUMMARY: "
760 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000761 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000762 n_supp_contexts );
763 VG_(message)(Vg_UserMsg, "");
764 }
765}
766
sewardj9f297ca2005-05-20 02:29:52 +0000767
768/* Show occurrence counts of all errors, in XML form. */
769void VG_(show_error_counts_as_XML) ( void )
770{
771 Error* err;
772 VG_(message)(Vg_UserMsg, "<errorcounts>");
773 for (err = errors; err != NULL; err = err->next) {
774 if (err->supp != NULL)
775 continue;
776 if (err->count <= 0)
777 continue;
778 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000779 Vg_UserMsg, " <pair> <count>%d</count> "
780 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000781 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000782 );
783 }
784 VG_(message)(Vg_UserMsg, "</errorcounts>");
785}
786
787
sewardjde4a1d02002-03-22 01:27:54 +0000788/*------------------------------------------------------------*/
789/*--- Standard suppressions ---*/
790/*------------------------------------------------------------*/
791
792/* Get a non-blank, non-comment line of at most nBuf chars from fd.
793 Skips leading spaces on the line. Return True if EOF was hit instead.
794*/
njn4ba5a792002-09-30 10:23:54 +0000795Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000796{
797 Char ch;
798 Int n, i;
799 while (True) {
800 /* First, read until a non-blank char appears. */
801 while (True) {
802 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000803 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000804 if (n == 0) return True;
805 }
806
807 /* Now, read the line into buf. */
808 i = 0;
809 buf[i++] = ch; buf[i] = 0;
810 while (True) {
811 n = VG_(read)(fd, &ch, 1);
812 if (n == 0) return False; /* the next call will return True */
813 if (ch == '\n') break;
814 if (i > 0 && i == nBuf-1) i--;
815 buf[i++] = ch; buf[i] = 0;
816 }
njn0c0f32a2005-03-26 04:14:01 +0000817 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000818 i--; buf[i] = 0;
819 };
820
njn02bc4b82005-05-15 17:28:26 +0000821 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000822 /* Ok, we have a line. If a non-comment line, return.
823 If a comment line, start all over again. */
824 if (buf[0] != '#') return False;
825 }
826}
827
828
829/* *p_caller contains the raw name of a caller, supposedly either
830 fun:some_function_name or
831 obj:some_object_name.
832 Set *p_ty accordingly and advance *p_caller over the descriptor
833 (fun: or obj:) part.
834 Returns False if failed.
835*/
sewardjb5f6f512005-03-10 23:59:00 +0000836static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000837{
sewardjb5f6f512005-03-10 23:59:00 +0000838 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
839 p->name += 4;
840 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000841 return True;
842 }
sewardjb5f6f512005-03-10 23:59:00 +0000843 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
844 p->name += 4;
845 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000846 return True;
847 }
848 VG_(printf)("location should start with fun: or obj:\n");
849 return False;
850}
851
852
nethercote7cc9c232004-01-21 15:08:04 +0000853/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000854static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000855Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000856{
857 Bool found;
858 Char *s = NULL; /* Shut gcc up */
859 Int len = VG_(strlen)(name);
860
861 found = (NULL != (s = VG_(strstr)(names, name)) &&
862 (s == names || *(s-1) == ',') &&
863 (*(s+len) == ',' || *(s+len) == '\0')
864 );
865
866 return found;
867}
868
njn695c16e2005-03-27 03:40:28 +0000869/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000870 and place them in the suppressions list. If there's any difficulty
871 doing this, just give up -- there's no point in trying to recover.
872*/
sewardjde4a1d02002-03-22 01:27:54 +0000873static void load_one_suppressions_file ( Char* filename )
874{
875# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000876 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000877 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000878 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000879 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000880 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000881 Char* err_str = NULL;
882 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000883
njn25e49d8e72002-09-23 09:36:25 +0000884 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000885 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000886 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000887 filename );
888 VG_(exit)(1);
889 }
890
sewardjb5f6f512005-03-10 23:59:00 +0000891#define BOMB(S) { err_str = S; goto syntax_error; }
892
sewardjde4a1d02002-03-22 01:27:54 +0000893 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000894 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000895 Supp* supp;
896 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000897 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000898
899 // Initialise temporary reading-in buffer.
900 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
901 tmp_callers[i].ty = NoName;
902 tmp_callers[i].name = NULL;
903 }
904
njn810086f2002-11-14 12:42:47 +0000905 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000906
njn4ba5a792002-09-30 10:23:54 +0000907 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000908 if (eof) break;
909
sewardjb5f6f512005-03-10 23:59:00 +0000910 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000911
njn4ba5a792002-09-30 10:23:54 +0000912 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000913
914 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
915
njn25e49d8e72002-09-23 09:36:25 +0000916 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000917
njn4ba5a792002-09-30 10:23:54 +0000918 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000919
sewardjb5f6f512005-03-10 23:59:00 +0000920 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000921
njn94065fd2004-11-22 19:26:27 +0000922 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000923 i = 0;
924 while (True) {
925 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000926 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000927 i++;
njn25e49d8e72002-09-23 09:36:25 +0000928 }
njnc40c3a82002-10-02 11:02:27 +0000929 buf[i] = '\0'; /* Replace ':', splitting into two strings */
930
nethercote7cc9c232004-01-21 15:08:04 +0000931 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000932 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000933
nethercote7cc9c232004-01-21 15:08:04 +0000934 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000935 {
sewardjb5f6f512005-03-10 23:59:00 +0000936 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000937 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000938 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000939 else
sewardjb5f6f512005-03-10 23:59:00 +0000940 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000941 }
njn95ec8702004-11-22 16:46:13 +0000942 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000943 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000944 {
sewardjb5f6f512005-03-10 23:59:00 +0000945 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000946 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000947 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000948 } else {
949 BOMB("unknown tool suppression type");
950 }
njnc40c3a82002-10-02 11:02:27 +0000951 }
njn25e49d8e72002-09-23 09:36:25 +0000952 else {
sewardjb5f6f512005-03-10 23:59:00 +0000953 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000954 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000955 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000956 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000957 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000958 break;
959 }
960 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000961 }
962
njn95ec8702004-11-22 16:46:13 +0000963 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000964 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000965 {
966 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000967 }
968
sewardjb5f6f512005-03-10 23:59:00 +0000969 i = 0;
970 while (True) {
971 eof = VG_(get_line) ( fd, buf, N_BUF );
972 if (eof)
973 BOMB("unexpected end-of-file");
974 if (VG_STREQ(buf, "}")) {
975 if (i > 0) {
976 break;
977 } else {
978 BOMB("missing stack trace");
979 }
980 }
981 if (i == VG_MAX_SUPP_CALLERS)
982 BOMB("too many callers in stack trace");
983 if (i > 0 && i >= VG_(clo_backtrace_size))
984 break;
985 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
986 if (!setLocationTy(&(tmp_callers[i])))
987 BOMB("location should start with 'fun:' or 'obj:'");
988 i++;
989 }
990
991 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
992 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000993 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000994 do {
995 eof = VG_(get_line) ( fd, buf, N_BUF );
996 } while (!eof && !VG_STREQ(buf, "}"));
997 }
998
999 // Copy tmp_callers[] into supp->callers[]
1000 supp->n_callers = i;
1001 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1002 for (i = 0; i < supp->n_callers; i++) {
1003 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001004 }
1005
njn695c16e2005-03-27 03:40:28 +00001006 supp->next = suppressions;
1007 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001008 }
sewardjde4a1d02002-03-22 01:27:54 +00001009 VG_(close)(fd);
1010 return;
1011
1012 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001013 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001014 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001015
sewardjde4a1d02002-03-22 01:27:54 +00001016 VG_(close)(fd);
1017 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001018 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001019
sewardjb5f6f512005-03-10 23:59:00 +00001020# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001021# undef N_BUF
1022}
1023
1024
1025void VG_(load_suppressions) ( void )
1026{
1027 Int i;
njn695c16e2005-03-27 03:40:28 +00001028 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001029 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1030 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001031 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1032 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001033 }
1034 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1035 }
1036}
1037
sewardjb5f6f512005-03-10 23:59:00 +00001038static
njn810086f2002-11-14 12:42:47 +00001039Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001040{
njn810086f2002-11-14 12:42:47 +00001041 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001042 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001043 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001044 default:
njn95ec8702004-11-22 16:46:13 +00001045 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001046 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001047 } else {
1048 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001049 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001050 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001051 err->ekind);
njn67993252004-11-22 18:02:32 +00001052 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001053 }
1054 }
1055}
1056
sewardjb5f6f512005-03-10 23:59:00 +00001057static
1058Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001059{
1060 Int i;
njn47b209a2005-03-25 23:47:16 +00001061 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001062 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001063
sewardjb5f6f512005-03-10 23:59:00 +00001064 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001065 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001066 vg_assert(su->callers[i].name != NULL);
1067 switch (su->callers[i].ty) {
1068 case ObjName:
njn47b209a2005-03-25 23:47:16 +00001069 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001070 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001071 break;
1072
1073 case FunName:
1074 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001075 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001076 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001077 break;
njn67993252004-11-22 18:02:32 +00001078 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001079 }
sewardjb5f6f512005-03-10 23:59:00 +00001080 if (!VG_(string_match)(su->callers[i].name, caller_name))
1081 return False;
njn25e49d8e72002-09-23 09:36:25 +00001082 }
1083
1084 /* If we reach here, it's a match */
1085 return True;
1086}
sewardjde4a1d02002-03-22 01:27:54 +00001087
njn810086f2002-11-14 12:42:47 +00001088/* Does an error context match a suppression? ie is this a suppressible
1089 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001090 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001091*/
njn810086f2002-11-14 12:42:47 +00001092static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001093{
njn810086f2002-11-14 12:42:47 +00001094 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001095
sewardjde4a1d02002-03-22 01:27:54 +00001096 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001097 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001098 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001099 supp_matches_callers(err, su))
1100 {
njn25e49d8e72002-09-23 09:36:25 +00001101 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001102 }
sewardjde4a1d02002-03-22 01:27:54 +00001103 }
njn25e49d8e72002-09-23 09:36:25 +00001104 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001105}
1106
sewardjde4a1d02002-03-22 01:27:54 +00001107/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00001108/*--- end m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001109/*--------------------------------------------------------------------*/