blob: 15607691ca10663d68681d2d88682035efd26325 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00003/*--- Management of error messages. m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjde4a1d02002-03-22 01:27:54 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000011 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
njn25e49d8e72002-09-23 09:36:25 +000028 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000029*/
30
nethercotef1e5e152004-09-01 23:58:16 +000031#include "core.h"
njnd2b17112005-04-19 04:10:25 +000032#include "pub_core_errormgr.h"
njnd01fef72005-03-25 23:35:48 +000033#include "pub_core_execontext.h"
njnd2b17112005-04-19 04:10:25 +000034#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000035#include "pub_core_tooliface.h"
njn3cbfbc12005-05-13 23:11:40 +000036#include "pub_core_translate.h"
sewardjde4a1d02002-03-22 01:27:54 +000037
38/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000039/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000040/*------------------------------------------------------------*/
41
njn14319cc2005-03-13 06:26:22 +000042/* After this many different unsuppressed errors have been observed,
43 be more conservative about collecting new ones. */
44#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
45
46/* After this many different unsuppressed errors have been observed,
47 stop collecting errors at all, and tell the user their program is
48 evidently a steaming pile of camel dung. */
49#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
50
51/* After this many total errors have been observed, stop collecting
52 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
53#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
54
sewardjde4a1d02002-03-22 01:27:54 +000055/* The list of error contexts found, both suppressed and unsuppressed.
56 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000057static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000058
59/* The list of suppression directives, as read from the specified
60 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000061static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000062
63/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000064static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000065
66/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000067static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000068
69/* forwards ... */
njn810086f2002-11-14 12:42:47 +000070static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000071
sewardjb5f6f512005-03-10 23:59:00 +000072static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000073
74/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000075/*--- Error type ---*/
76/*------------------------------------------------------------*/
77
nethercote996901a2004-08-03 13:29:09 +000078/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000079 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000080
81/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000082 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000083 Functions for getting/setting the tool-relevant fields are in
nethercote46063202004-09-02 08:51:43 +000084 include/tool.h.
nethercote4a184902004-08-02 12:21:09 +000085
86 When errors are found and recorded with VG_(maybe_record_error)(), all
87 the tool must do is pass in the four parameters; core will
88 allocate/initialise the error record.
89*/
90struct _Error {
91 struct _Error* next;
92 // NULL if unsuppressed; or ptr to suppression record.
93 Supp* supp;
94 Int count;
95 ThreadId tid;
96
97 // The tool-specific part
98 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +000099 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000100 Addr addr; // Used frequently
101 Char* string; // Used frequently
102 void* extra; // For any tool-specific extras
103};
104
105ExeContext* VG_(get_error_where) ( Error* err )
106{
107 return err->where;
108}
109
110ErrorKind VG_(get_error_kind) ( Error* err )
111{
112 return err->ekind;
113}
114
115Addr VG_(get_error_address) ( Error* err )
116{
117 return err->addr;
118}
119
120Char* VG_(get_error_string) ( Error* err )
121{
122 return err->string;
123}
124
125void* VG_(get_error_extra) ( Error* err )
126{
127 return err->extra;
128}
129
nethercotef2b11482004-08-02 12:36:01 +0000130UInt VG_(get_n_errs_found)( void )
131{
132 return n_errs_found;
133}
134
nethercote4a184902004-08-02 12:21:09 +0000135/*------------------------------------------------------------*/
136/*--- Suppression type ---*/
137/*------------------------------------------------------------*/
138
139/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
140 * effectively extend it by defining their own enums in the (0..) range. */
141typedef
142 enum {
143 PThreadSupp = -1, /* Matches PThreadErr */
144 }
145 CoreSuppKind;
146
sewardjb5f6f512005-03-10 23:59:00 +0000147/* Max number of callers for context in a suppression. */
148#define VG_MAX_SUPP_CALLERS 24
149
nethercote4a184902004-08-02 12:21:09 +0000150/* For each caller specified for a suppression, record the nature of
151 the caller name. Not of interest to tools. */
152typedef
153 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000154 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000155 ObjName, /* Name is of an shared object file. */
156 FunName /* Name is of a function. */
157 }
158 SuppLocTy;
159
sewardjb5f6f512005-03-10 23:59:00 +0000160typedef
161 struct {
162 SuppLocTy ty;
163 Char* name;
164 }
165 SuppLoc;
166
nethercote4a184902004-08-02 12:21:09 +0000167/* Suppressions. Tools can get/set tool-relevant parts with functions
nethercote46063202004-09-02 08:51:43 +0000168 declared in include/tool.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000169 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000170 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000171struct _Supp {
172 struct _Supp* next;
173 Int count; // The number of times this error has been suppressed.
174 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000175
176 // Length of 'callers'
177 Int n_callers;
178 // Array of callers, for matching stack traces. First one (name of fn
179 // where err occurs) is mandatory; rest are optional.
180 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000181
182 /* The tool-specific part */
183 SuppKind skind; // What kind of suppression. Must use the range (0..).
184 Char* string; // String -- use is optional. NULL by default.
185 void* extra; // Anything else -- use is optional. NULL by default.
186};
187
188SuppKind VG_(get_supp_kind) ( Supp* su )
189{
190 return su->skind;
191}
192
193Char* VG_(get_supp_string) ( Supp* su )
194{
195 return su->string;
196}
197
198void* VG_(get_supp_extra) ( Supp* su )
199{
200 return su->extra;
201}
202
203
204void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
205{
206 su->skind = skind;
207}
208
209void VG_(set_supp_string) ( Supp* su, Char* string )
210{
211 su->string = string;
212}
213
214void VG_(set_supp_extra) ( Supp* su, void* extra )
215{
216 su->extra = extra;
217}
218
219
220/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000221/*--- Helper fns ---*/
222/*------------------------------------------------------------*/
223
sewardjde4a1d02002-03-22 01:27:54 +0000224/* Compare error contexts, to detect duplicates. Note that if they
225 are otherwise the same, the faulting addrs and associated rwoffsets
226 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000227static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000228{
njn810086f2002-11-14 12:42:47 +0000229 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000230 return False;
njn25e49d8e72002-09-23 09:36:25 +0000231 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000232 return False;
233
njn810086f2002-11-14 12:42:47 +0000234 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000235 // case ThreadErr:
236 // case MutexErr:
237 // vg_assert(VG_(needs).core_errors);
238 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000239 default:
njn51d827b2005-05-09 01:02:08 +0000240 if (VG_(needs).tool_errors) {
241 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
242 } else {
njn95ec8702004-11-22 16:46:13 +0000243 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000244 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000245 e1->ekind);
njn67993252004-11-22 18:02:32 +0000246 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000247 }
sewardjde4a1d02002-03-22 01:27:54 +0000248 }
249}
250
njn810086f2002-11-14 12:42:47 +0000251static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000252{
sewardjde4a1d02002-03-22 01:27:54 +0000253 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +0000254 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
sewardjb5f6f512005-03-10 23:59:00 +0000255 if (err->tid > 0 && err->tid != last_tid_printed) {
njn25e49d8e72002-09-23 09:36:25 +0000256 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
sewardjb5f6f512005-03-10 23:59:00 +0000257 last_tid_printed = err->tid;
258 }
njn25e49d8e72002-09-23 09:36:25 +0000259
njn810086f2002-11-14 12:42:47 +0000260 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000261 // case ThreadErr:
262 // case MutexErr:
263 // vg_assert(VG_(needs).core_errors);
264 // VG_(tm_error_print)(err);
265 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000266 default:
njn95ec8702004-11-22 16:46:13 +0000267 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000268 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000269 else {
njn95ec8702004-11-22 16:46:13 +0000270 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000271 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000272 err->ekind);
njn67993252004-11-22 18:02:32 +0000273 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000274 }
sewardjde4a1d02002-03-22 01:27:54 +0000275 }
276}
277
nethercote04d0fbc2004-01-26 16:48:06 +0000278/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000279 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000280Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000281{
282 Char ch, ch2;
283 Int res;
284
njn43c799e2003-04-08 00:08:52 +0000285 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000286 return False;
287
288 VG_(message)(Vg_UserMsg, "");
289
290 again:
291 VG_(printf)(
292 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000293 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
294 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000295 );
296
sewardj6024b212003-07-13 10:54:33 +0000297 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000298 if (res != 1) goto ioerror;
299 /* res == 1 */
300 if (ch == '\n') return False;
301 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
302 && ch != 'C' && ch != 'c') goto again;
303
sewardj6024b212003-07-13 10:54:33 +0000304 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000305 if (res != 1) goto ioerror;
306 if (ch2 != '\n') goto again;
307
njn43c799e2003-04-08 00:08:52 +0000308 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000309 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000310 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000311 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000312 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000313 vg_assert(ch == 'c' || ch == 'C');
314
315 ioerror:
njn43c799e2003-04-08 00:08:52 +0000316 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000317 return False;
318}
319
320
sewardjb5f6f512005-03-10 23:59:00 +0000321/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000322static __inline__
njn72718642003-07-24 08:45:32 +0000323void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
324 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000325{
njnca82cc02004-11-22 17:18:48 +0000326 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000327
njn810086f2002-11-14 12:42:47 +0000328 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000329 err->next = NULL;
330 err->supp = NULL;
331 err->count = 1;
njn72718642003-07-24 08:45:32 +0000332 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000333 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000334 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000335 else
336 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000337
nethercote996901a2004-08-03 13:29:09 +0000338 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000339 err->ekind = ekind;
340 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000341 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000342 err->string = s;
343
njn25e49d8e72002-09-23 09:36:25 +0000344 /* sanity... */
njn72718642003-07-24 08:45:32 +0000345 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000346}
347
njnf4261312005-03-20 23:45:36 +0000348static void printSuppForIp(UInt n, Addr ip)
349{
njn47b209a2005-03-25 23:47:16 +0000350 static UChar buf[VG_ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000351
njn47b209a2005-03-25 23:47:16 +0000352 if ( VG_(get_fnname_nodemangle) (ip, buf, VG_ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000353 VG_(printf)(" fun:%s\n", buf);
njn47b209a2005-03-25 23:47:16 +0000354 } else if ( VG_(get_objname)(ip, buf+7, VG_ERRTXT_LEN-7) ) {
njnf4261312005-03-20 23:45:36 +0000355 VG_(printf)(" obj:%s\n", buf);
356 } else {
357 VG_(printf)(" ???:??? "
358 "# unknown, suppression will not work, sorry\n");
359 }
360}
361
nethercote10d481a2004-01-25 20:33:53 +0000362static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000363{
njn43c799e2003-04-08 00:08:52 +0000364 ExeContext* ec = VG_(get_error_where)(err);
365 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000366
sewardjb5f6f512005-03-10 23:59:00 +0000367 /* At most VG_MAX_SUPP_CALLERS names */
368 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000369 vg_assert(stop_at > 0);
370
371 VG_(printf)("{\n");
372 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000373
sewardjb5f6f512005-03-10 23:59:00 +0000374 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000375 VG_(printf)(" core:PThread\n");
376
377 } else {
njn51d827b2005-05-09 01:02:08 +0000378 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000379 if (NULL == name) {
380 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000381 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000382 return;
383 }
384 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000385 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000386 }
njn43c799e2003-04-08 00:08:52 +0000387
njnf4261312005-03-20 23:45:36 +0000388 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000389 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000390
391 VG_(printf)("}\n");
392}
393
njnb4aee052003-04-15 14:09:58 +0000394static
nethercote04d0fbc2004-01-26 16:48:06 +0000395void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000396{
sewardjd153fae2005-01-10 17:24:47 +0000397 Bool still_noisy = True;
398
nethercote04d0fbc2004-01-26 16:48:06 +0000399 /* Perhaps we want a debugger attach at this point? */
400 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000401 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
402 {
nethercote04d0fbc2004-01-26 16:48:06 +0000403 VG_(printf)("starting debugger\n");
404 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000405 }
njn43c799e2003-04-08 00:08:52 +0000406 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000407 if (VG_(clo_gen_suppressions) == 2
408 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000409 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000410 ) {
nethercote42602b12004-01-25 19:30:29 +0000411 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000412 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
413 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000414 }
415}
416
417/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
418 just for pretty printing purposes. */
419static Bool is_first_shown_context = True;
420
njn25e49d8e72002-09-23 09:36:25 +0000421/* Top-level entry point to the error management subsystem.
422 All detected errors are notified here; this routine decides if/when the
423 user should see the error. */
njn72718642003-07-24 08:45:32 +0000424void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000425 ErrorKind ekind, Addr a, Char* s, void* extra )
426{
njn810086f2002-11-14 12:42:47 +0000427 Error err;
428 Error* p;
429 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000430 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000431 VgRes exe_res = Vg_MedRes;
432 static Bool stopping_message = False;
433 static Bool slowdown_message = False;
434 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000435
njn14319cc2005-03-13 06:26:22 +0000436 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
437 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000438 have been found, just refuse to collect any more. This stops
439 the burden of the error-management system becoming excessive in
440 extremely buggy programs, although it does make it pretty
441 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000442 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000443 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000444 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000445 if (!stopping_message) {
446 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000447
njn695c16e2005-03-27 03:40:28 +0000448 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000449 VG_(message)(Vg_UserMsg,
450 "More than %d different errors detected. "
451 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000452 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000453 } else {
454 VG_(message)(Vg_UserMsg,
455 "More than %d total errors detected. "
456 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000457 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000458 }
459
sewardjde4a1d02002-03-22 01:27:54 +0000460 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000461 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000462 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000463 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000464 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000465 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000466 VG_(message)(Vg_UserMsg,
467 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000468 VG_(message)(Vg_UserMsg, "");
469 stopping_message = True;
470 }
471 return;
472 }
473
njn14319cc2005-03-13 06:26:22 +0000474 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000475 been found, be much more conservative about collecting new
476 ones. */
njn695c16e2005-03-27 03:40:28 +0000477 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000478 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000479 if (!slowdown_message) {
480 VG_(message)(Vg_UserMsg, "");
481 VG_(message)(Vg_UserMsg,
482 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000483 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000484 VG_(message)(Vg_UserMsg,
485 "will still be recorded, but in less detail than before.");
486 slowdown_message = True;
487 }
488 }
489
njn25e49d8e72002-09-23 09:36:25 +0000490 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000491 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000492
493 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000494 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000495 p_prev = NULL;
496 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000497 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000498 /* Found it. */
499 p->count++;
500 if (p->supp != NULL) {
501 /* Deal correctly with suppressed errors. */
502 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000503 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000504 } else {
nethercotef2b11482004-08-02 12:36:01 +0000505 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000506 }
507
508 /* Move p to the front of the list so that future searches
509 for it are faster. */
510 if (p_prev != NULL) {
511 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000512 p_prev->next = p->next;
513 p->next = errors;
514 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000515 }
sewardj7ebf7c32003-07-24 21:29:40 +0000516
sewardjde4a1d02002-03-22 01:27:54 +0000517 return;
518 }
519 p_prev = p;
520 p = p->next;
521 }
522
523 /* Didn't see it. Copy and add. */
524
njn43c799e2003-04-08 00:08:52 +0000525 /* OK, we're really going to collect it. The context is on the stack and
526 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000527 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000528
njn02bc4b82005-05-15 17:28:26 +0000529 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000530 is for when there are more details to fill in which take time to work
531 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000532 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000533 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000534
njn02bc4b82005-05-15 17:28:26 +0000535 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000536 VG_(tdict).tool_update_extra returned. Also allow for people using
537 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000538 */
539
540 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000541 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000542 *p = err;
njn43c799e2003-04-08 00:08:52 +0000543
njn02bc4b82005-05-15 17:28:26 +0000544 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000545 switch (ekind) {
546 // case ThreadErr:
547 // case MutexErr:
548 // vg_assert(VG_(needs).core_errors);
549 // extra_size = VG_(tm_error_update_extra)(p);
550 // break;
551 default:
552 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000553 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000554 break;
555 }
njn43c799e2003-04-08 00:08:52 +0000556
njn02bc4b82005-05-15 17:28:26 +0000557 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000558 if (NULL != p->extra && 0 != extra_size) {
559 void* new_extra = VG_(malloc)(extra_size);
560 VG_(memcpy)(new_extra, p->extra, extra_size);
561 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000562 }
563
njn695c16e2005-03-27 03:40:28 +0000564 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000565 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000566 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000567 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000568 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000569 if (!is_first_shown_context)
570 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000571 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000572 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000573 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000574 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000575 } else {
nethercotef2b11482004-08-02 12:36:01 +0000576 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000577 p->supp->count++;
578 }
579}
580
njn43c799e2003-04-08 00:08:52 +0000581/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000582 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000583 guaranteed to only happen once. This avoids all the recording and
584 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000585 suppressed. Bool 'print_error' dictates whether to print the error.
586 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000587*/
njn72718642003-07-24 08:45:32 +0000588Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000589 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000590 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000591{
592 Error err;
593
594 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000595 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000596
597 /* Unless it's suppressed, we're going to show it. Don't need to make
598 a copy, because it's only temporary anyway.
599
njn02bc4b82005-05-15 17:28:26 +0000600 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000601 because that can have an affect on whether it's suppressed. Ignore
602 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000603 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000604 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000605
606 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000607 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000608 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000609
610 if (print_error) {
611 if (!is_first_shown_context)
612 VG_(message)(Vg_UserMsg, "");
613 pp_Error(&err, False);
614 is_first_shown_context = False;
615 }
nethercote04d0fbc2004-01-26 16:48:06 +0000616 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000617
618 return False;
619
620 } else {
nethercotef2b11482004-08-02 12:36:01 +0000621 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000622 return True;
623 }
624}
625
sewardjde4a1d02002-03-22 01:27:54 +0000626
sewardjde4a1d02002-03-22 01:27:54 +0000627/*------------------------------------------------------------*/
628/*--- Exported fns ---*/
629/*------------------------------------------------------------*/
630
njnb9ecfe32005-03-13 05:27:57 +0000631/* This is called not from generated code but from the scheduler */
sewardjde4a1d02002-03-22 01:27:54 +0000632void VG_(show_all_errors) ( void )
633{
njn810086f2002-11-14 12:42:47 +0000634 Int i, n_min;
635 Int n_err_contexts, n_supp_contexts;
636 Error *p, *p_min;
637 Supp *su;
638 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000639
640 if (VG_(clo_verbosity) == 0)
641 return;
642
643 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000644 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000645 if (p->supp == NULL)
646 n_err_contexts++;
647 }
648
649 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000650 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000651 if (su->count > 0)
652 n_supp_contexts++;
653 }
sewardjde4a1d02002-03-22 01:27:54 +0000654 VG_(message)(Vg_UserMsg,
655 "ERROR SUMMARY: "
656 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000657 n_errs_found, n_err_contexts,
658 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000659
660 if (VG_(clo_verbosity) <= 1)
661 return;
662
663 /* Print the contexts in order of increasing error count. */
664 for (i = 0; i < n_err_contexts; i++) {
665 n_min = (1 << 30) - 1;
666 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000667 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000668 if (p->supp != NULL) continue;
669 if (p->count < n_min) {
670 n_min = p->count;
671 p_min = p;
672 }
673 }
njn67993252004-11-22 18:02:32 +0000674 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000675
676 VG_(message)(Vg_UserMsg, "");
677 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
678 p_min->count,
679 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000680 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000681
682 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000683 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000684 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njnd01fef72005-03-25 23:35:48 +0000685 ips[0], /*debugging*/True, 0xFE/*verbosity*/);
sewardjde4a1d02002-03-22 01:27:54 +0000686 }
687
688 p_min->count = 1 << 30;
689 }
690
691 if (n_supp_contexts > 0)
692 VG_(message)(Vg_DebugMsg, "");
693 any_supp = False;
njn695c16e2005-03-27 03:40:28 +0000694 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000695 if (su->count > 0) {
696 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000697 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000698 }
699 }
700
701 if (n_err_contexts > 0) {
702 if (any_supp)
703 VG_(message)(Vg_UserMsg, "");
704 VG_(message)(Vg_UserMsg,
705 "IN SUMMARY: "
706 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000707 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000708 n_supp_contexts );
709 VG_(message)(Vg_UserMsg, "");
710 }
711}
712
713/*------------------------------------------------------------*/
714/*--- Standard suppressions ---*/
715/*------------------------------------------------------------*/
716
717/* Get a non-blank, non-comment line of at most nBuf chars from fd.
718 Skips leading spaces on the line. Return True if EOF was hit instead.
719*/
njn4ba5a792002-09-30 10:23:54 +0000720Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000721{
722 Char ch;
723 Int n, i;
724 while (True) {
725 /* First, read until a non-blank char appears. */
726 while (True) {
727 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000728 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000729 if (n == 0) return True;
730 }
731
732 /* Now, read the line into buf. */
733 i = 0;
734 buf[i++] = ch; buf[i] = 0;
735 while (True) {
736 n = VG_(read)(fd, &ch, 1);
737 if (n == 0) return False; /* the next call will return True */
738 if (ch == '\n') break;
739 if (i > 0 && i == nBuf-1) i--;
740 buf[i++] = ch; buf[i] = 0;
741 }
njn0c0f32a2005-03-26 04:14:01 +0000742 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000743 i--; buf[i] = 0;
744 };
745
njn02bc4b82005-05-15 17:28:26 +0000746 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000747 /* Ok, we have a line. If a non-comment line, return.
748 If a comment line, start all over again. */
749 if (buf[0] != '#') return False;
750 }
751}
752
753
754/* *p_caller contains the raw name of a caller, supposedly either
755 fun:some_function_name or
756 obj:some_object_name.
757 Set *p_ty accordingly and advance *p_caller over the descriptor
758 (fun: or obj:) part.
759 Returns False if failed.
760*/
sewardjb5f6f512005-03-10 23:59:00 +0000761static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000762{
sewardjb5f6f512005-03-10 23:59:00 +0000763 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
764 p->name += 4;
765 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000766 return True;
767 }
sewardjb5f6f512005-03-10 23:59:00 +0000768 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
769 p->name += 4;
770 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000771 return True;
772 }
773 VG_(printf)("location should start with fun: or obj:\n");
774 return False;
775}
776
777
nethercote7cc9c232004-01-21 15:08:04 +0000778/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000779static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000780Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000781{
782 Bool found;
783 Char *s = NULL; /* Shut gcc up */
784 Int len = VG_(strlen)(name);
785
786 found = (NULL != (s = VG_(strstr)(names, name)) &&
787 (s == names || *(s-1) == ',') &&
788 (*(s+len) == ',' || *(s+len) == '\0')
789 );
790
791 return found;
792}
793
njn695c16e2005-03-27 03:40:28 +0000794/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000795 and place them in the suppressions list. If there's any difficulty
796 doing this, just give up -- there's no point in trying to recover.
797*/
sewardjde4a1d02002-03-22 01:27:54 +0000798static void load_one_suppressions_file ( Char* filename )
799{
800# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000801 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000802 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000803 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000804 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000805 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000806 Char* err_str = NULL;
807 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000808
njn25e49d8e72002-09-23 09:36:25 +0000809 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000810 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000811 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000812 filename );
813 VG_(exit)(1);
814 }
815
sewardjb5f6f512005-03-10 23:59:00 +0000816#define BOMB(S) { err_str = S; goto syntax_error; }
817
sewardjde4a1d02002-03-22 01:27:54 +0000818 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000819 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000820 Supp* supp;
821 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000822 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000823
824 // Initialise temporary reading-in buffer.
825 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
826 tmp_callers[i].ty = NoName;
827 tmp_callers[i].name = NULL;
828 }
829
njn810086f2002-11-14 12:42:47 +0000830 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000831
njn4ba5a792002-09-30 10:23:54 +0000832 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000833 if (eof) break;
834
sewardjb5f6f512005-03-10 23:59:00 +0000835 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000836
njn4ba5a792002-09-30 10:23:54 +0000837 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000838
839 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
840
njn25e49d8e72002-09-23 09:36:25 +0000841 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000842
njn4ba5a792002-09-30 10:23:54 +0000843 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000844
sewardjb5f6f512005-03-10 23:59:00 +0000845 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000846
njn94065fd2004-11-22 19:26:27 +0000847 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000848 i = 0;
849 while (True) {
850 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000851 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000852 i++;
njn25e49d8e72002-09-23 09:36:25 +0000853 }
njnc40c3a82002-10-02 11:02:27 +0000854 buf[i] = '\0'; /* Replace ':', splitting into two strings */
855
nethercote7cc9c232004-01-21 15:08:04 +0000856 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000857 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000858
nethercote7cc9c232004-01-21 15:08:04 +0000859 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000860 {
sewardjb5f6f512005-03-10 23:59:00 +0000861 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000862 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000863 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000864 else
sewardjb5f6f512005-03-10 23:59:00 +0000865 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000866 }
njn95ec8702004-11-22 16:46:13 +0000867 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000868 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000869 {
sewardjb5f6f512005-03-10 23:59:00 +0000870 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000871 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000872 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000873 } else {
874 BOMB("unknown tool suppression type");
875 }
njnc40c3a82002-10-02 11:02:27 +0000876 }
njn25e49d8e72002-09-23 09:36:25 +0000877 else {
sewardjb5f6f512005-03-10 23:59:00 +0000878 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000879 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000880 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000881 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000882 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000883 break;
884 }
885 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000886 }
887
njn95ec8702004-11-22 16:46:13 +0000888 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000889 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000890 {
891 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000892 }
893
sewardjb5f6f512005-03-10 23:59:00 +0000894 i = 0;
895 while (True) {
896 eof = VG_(get_line) ( fd, buf, N_BUF );
897 if (eof)
898 BOMB("unexpected end-of-file");
899 if (VG_STREQ(buf, "}")) {
900 if (i > 0) {
901 break;
902 } else {
903 BOMB("missing stack trace");
904 }
905 }
906 if (i == VG_MAX_SUPP_CALLERS)
907 BOMB("too many callers in stack trace");
908 if (i > 0 && i >= VG_(clo_backtrace_size))
909 break;
910 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
911 if (!setLocationTy(&(tmp_callers[i])))
912 BOMB("location should start with 'fun:' or 'obj:'");
913 i++;
914 }
915
916 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
917 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +0000918 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +0000919 do {
920 eof = VG_(get_line) ( fd, buf, N_BUF );
921 } while (!eof && !VG_STREQ(buf, "}"));
922 }
923
924 // Copy tmp_callers[] into supp->callers[]
925 supp->n_callers = i;
926 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
927 for (i = 0; i < supp->n_callers; i++) {
928 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +0000929 }
930
njn695c16e2005-03-27 03:40:28 +0000931 supp->next = suppressions;
932 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +0000933 }
sewardjde4a1d02002-03-22 01:27:54 +0000934 VG_(close)(fd);
935 return;
936
937 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +0000938 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000939 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +0000940
sewardjde4a1d02002-03-22 01:27:54 +0000941 VG_(close)(fd);
942 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +0000943 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +0000944
sewardjb5f6f512005-03-10 23:59:00 +0000945# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +0000946# undef N_BUF
947}
948
949
950void VG_(load_suppressions) ( void )
951{
952 Int i;
njn695c16e2005-03-27 03:40:28 +0000953 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000954 for (i = 0; i < VG_(clo_n_suppressions); i++) {
955 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +0000956 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
957 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +0000958 }
959 load_one_suppressions_file( VG_(clo_suppressions)[i] );
960 }
961}
962
sewardjb5f6f512005-03-10 23:59:00 +0000963static
njn810086f2002-11-14 12:42:47 +0000964Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000965{
njn810086f2002-11-14 12:42:47 +0000966 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000967 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +0000968 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +0000969 default:
njn95ec8702004-11-22 16:46:13 +0000970 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +0000971 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +0000972 } else {
973 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +0000974 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000975 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000976 err->ekind);
njn67993252004-11-22 18:02:32 +0000977 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000978 }
979 }
980}
981
sewardjb5f6f512005-03-10 23:59:00 +0000982static
983Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +0000984{
985 Int i;
njn47b209a2005-03-25 23:47:16 +0000986 Char caller_name[VG_ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +0000987 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +0000988
sewardjb5f6f512005-03-10 23:59:00 +0000989 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +0000990 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +0000991 vg_assert(su->callers[i].name != NULL);
992 switch (su->callers[i].ty) {
993 case ObjName:
njn47b209a2005-03-25 23:47:16 +0000994 if (!VG_(get_objname)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +0000995 return False;
sewardjb5f6f512005-03-10 23:59:00 +0000996 break;
997
998 case FunName:
999 // Nb: mangled names used in suppressions
njn47b209a2005-03-25 23:47:16 +00001000 if (!VG_(get_fnname_nodemangle)(a, caller_name, VG_ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001001 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001002 break;
njn67993252004-11-22 18:02:32 +00001003 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001004 }
sewardjb5f6f512005-03-10 23:59:00 +00001005 if (!VG_(string_match)(su->callers[i].name, caller_name))
1006 return False;
njn25e49d8e72002-09-23 09:36:25 +00001007 }
1008
1009 /* If we reach here, it's a match */
1010 return True;
1011}
sewardjde4a1d02002-03-22 01:27:54 +00001012
njn810086f2002-11-14 12:42:47 +00001013/* Does an error context match a suppression? ie is this a suppressible
1014 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001015 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001016*/
njn810086f2002-11-14 12:42:47 +00001017static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001018{
njn810086f2002-11-14 12:42:47 +00001019 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001020
sewardjde4a1d02002-03-22 01:27:54 +00001021 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001022 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001023 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001024 supp_matches_callers(err, su))
1025 {
njn25e49d8e72002-09-23 09:36:25 +00001026 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001027 }
sewardjde4a1d02002-03-22 01:27:54 +00001028 }
njn25e49d8e72002-09-23 09:36:25 +00001029 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001030}
1031
sewardjde4a1d02002-03-22 01:27:54 +00001032/*--------------------------------------------------------------------*/
sewardj267100d2005-04-24 12:33:12 +00001033/*--- end m_errormgr.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001034/*--------------------------------------------------------------------*/