blob: 16709fc0525e9f210f1783aa2d9724fac4f97782 [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
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njn24a6efb2005-06-20 03:36:51 +000032#include "pub_core_threadstate.h" // For VG_N_THREADS
njn75b65aa2005-06-19 19:25:44 +000033#include "pub_core_debugger.h"
njnea27e462005-05-31 02:38:09 +000034#include "pub_core_debuginfo.h"
njnd2b17112005-04-19 04:10:25 +000035#include "pub_core_errormgr.h"
njnd01fef72005-03-25 23:35:48 +000036#include "pub_core_execontext.h"
njn97405b22005-06-02 03:39:33 +000037#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000038#include "pub_core_libcassert.h"
njneb8896b2005-06-04 20:03:55 +000039#include "pub_core_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000040#include "pub_core_libcprint.h"
njn24a6efb2005-06-20 03:36:51 +000041#include "pub_core_libcproc.h" // For VG_(getpid)()
njnaf1d7df2005-06-11 01:31:52 +000042#include "pub_core_mallocfree.h"
njn20242342005-05-16 23:31:24 +000043#include "pub_core_options.h"
njnd0d7c1f2005-06-21 00:33:19 +000044#include "pub_core_stacktrace.h"
njn43b9a8a2005-05-10 04:37:01 +000045#include "pub_core_tooliface.h"
njn24a6efb2005-06-20 03:36:51 +000046#include "pub_core_translate.h" // for VG_(translate)()
sewardjde4a1d02002-03-22 01:27:54 +000047
48/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000049/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000050/*------------------------------------------------------------*/
51
njn14319cc2005-03-13 06:26:22 +000052/* After this many different unsuppressed errors have been observed,
53 be more conservative about collecting new ones. */
54#define M_COLLECT_ERRORS_SLOWLY_AFTER 50
55
56/* After this many different unsuppressed errors have been observed,
57 stop collecting errors at all, and tell the user their program is
58 evidently a steaming pile of camel dung. */
59#define M_COLLECT_NO_ERRORS_AFTER_SHOWN 300
60
61/* After this many total errors have been observed, stop collecting
62 errors at all. Counterpart to M_COLLECT_NO_ERRORS_AFTER_SHOWN. */
63#define M_COLLECT_NO_ERRORS_AFTER_FOUND 30000
64
sewardjde4a1d02002-03-22 01:27:54 +000065/* The list of error contexts found, both suppressed and unsuppressed.
66 Initially empty, and grows as errors are detected. */
njn695c16e2005-03-27 03:40:28 +000067static Error* errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000068
69/* The list of suppression directives, as read from the specified
70 suppressions file. */
njn695c16e2005-03-27 03:40:28 +000071static Supp* suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000072
73/* Running count of unsuppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000074static UInt n_errs_found = 0;
sewardjde4a1d02002-03-22 01:27:54 +000075
76/* Running count of suppressed errors detected. */
nethercotef2b11482004-08-02 12:36:01 +000077static UInt n_errs_suppressed = 0;
sewardjde4a1d02002-03-22 01:27:54 +000078
79/* forwards ... */
njn810086f2002-11-14 12:42:47 +000080static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000081
sewardjb5f6f512005-03-10 23:59:00 +000082static ThreadId last_tid_printed = 1;
sewardjde4a1d02002-03-22 01:27:54 +000083
84/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000085/*--- Error type ---*/
86/*------------------------------------------------------------*/
87
nethercote996901a2004-08-03 13:29:09 +000088/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
nethercote4a184902004-08-02 12:21:09 +000089 * effectively extend it by defining their own enums in the (0..) range. */
nethercote4a184902004-08-02 12:21:09 +000090
91/* Errors. Extensible (via the 'extra' field). Tools can use a normal
njn02bc4b82005-05-15 17:28:26 +000092 enum (with element values in the normal range (0..)) for 'ekind'.
nethercote4a184902004-08-02 12:21:09 +000093 Functions for getting/setting the tool-relevant fields are in
njnc7561b92005-06-19 01:24:32 +000094 include/pub_tool_errormgr.h.
nethercote4a184902004-08-02 12:21:09 +000095
96 When errors are found and recorded with VG_(maybe_record_error)(), all
97 the tool must do is pass in the four parameters; core will
98 allocate/initialise the error record.
99*/
100struct _Error {
101 struct _Error* next;
102 // NULL if unsuppressed; or ptr to suppression record.
103 Supp* supp;
104 Int count;
105 ThreadId tid;
106
107 // The tool-specific part
108 ExeContext* where; // Initialised by core
njnd2b17112005-04-19 04:10:25 +0000109 ErrorKind ekind; // Used by ALL. Must be in the range (0..)
nethercote4a184902004-08-02 12:21:09 +0000110 Addr addr; // Used frequently
111 Char* string; // Used frequently
112 void* extra; // For any tool-specific extras
113};
114
115ExeContext* VG_(get_error_where) ( Error* err )
116{
117 return err->where;
118}
119
120ErrorKind VG_(get_error_kind) ( Error* err )
121{
122 return err->ekind;
123}
124
125Addr VG_(get_error_address) ( Error* err )
126{
127 return err->addr;
128}
129
130Char* VG_(get_error_string) ( Error* err )
131{
132 return err->string;
133}
134
135void* VG_(get_error_extra) ( Error* err )
136{
137 return err->extra;
138}
139
nethercotef2b11482004-08-02 12:36:01 +0000140UInt VG_(get_n_errs_found)( void )
141{
142 return n_errs_found;
143}
144
nethercote4a184902004-08-02 12:21:09 +0000145/*------------------------------------------------------------*/
146/*--- Suppression type ---*/
147/*------------------------------------------------------------*/
148
149/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
150 * effectively extend it by defining their own enums in the (0..) range. */
151typedef
152 enum {
153 PThreadSupp = -1, /* Matches PThreadErr */
154 }
155 CoreSuppKind;
156
sewardjb5f6f512005-03-10 23:59:00 +0000157/* Max number of callers for context in a suppression. */
158#define VG_MAX_SUPP_CALLERS 24
159
nethercote4a184902004-08-02 12:21:09 +0000160/* For each caller specified for a suppression, record the nature of
161 the caller name. Not of interest to tools. */
162typedef
163 enum {
sewardjb5f6f512005-03-10 23:59:00 +0000164 NoName, /* Error case */
nethercote4a184902004-08-02 12:21:09 +0000165 ObjName, /* Name is of an shared object file. */
166 FunName /* Name is of a function. */
167 }
168 SuppLocTy;
169
sewardjb5f6f512005-03-10 23:59:00 +0000170typedef
171 struct {
172 SuppLocTy ty;
173 Char* name;
174 }
175 SuppLoc;
176
nethercote4a184902004-08-02 12:21:09 +0000177/* Suppressions. Tools can get/set tool-relevant parts with functions
njnc7561b92005-06-19 01:24:32 +0000178 declared in include/pub_tool_errormgr.h. Extensible via the 'extra' field.
nethercote4a184902004-08-02 12:21:09 +0000179 Tools can use a normal enum (with element values in the normal range
njn02bc4b82005-05-15 17:28:26 +0000180 (0..)) for 'skind'. */
nethercote4a184902004-08-02 12:21:09 +0000181struct _Supp {
182 struct _Supp* next;
183 Int count; // The number of times this error has been suppressed.
184 Char* sname; // The name by which the suppression is referred to.
sewardjb5f6f512005-03-10 23:59:00 +0000185
186 // Length of 'callers'
187 Int n_callers;
188 // Array of callers, for matching stack traces. First one (name of fn
189 // where err occurs) is mandatory; rest are optional.
190 SuppLoc* callers;
nethercote4a184902004-08-02 12:21:09 +0000191
192 /* The tool-specific part */
193 SuppKind skind; // What kind of suppression. Must use the range (0..).
194 Char* string; // String -- use is optional. NULL by default.
195 void* extra; // Anything else -- use is optional. NULL by default.
196};
197
198SuppKind VG_(get_supp_kind) ( Supp* su )
199{
200 return su->skind;
201}
202
203Char* VG_(get_supp_string) ( Supp* su )
204{
205 return su->string;
206}
207
208void* VG_(get_supp_extra) ( Supp* su )
209{
210 return su->extra;
211}
212
213
214void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
215{
216 su->skind = skind;
217}
218
219void VG_(set_supp_string) ( Supp* su, Char* string )
220{
221 su->string = string;
222}
223
224void VG_(set_supp_extra) ( Supp* su, void* extra )
225{
226 su->extra = extra;
227}
228
229
230/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000231/*--- Helper fns ---*/
232/*------------------------------------------------------------*/
233
sewardjde4a1d02002-03-22 01:27:54 +0000234/* Compare error contexts, to detect duplicates. Note that if they
235 are otherwise the same, the faulting addrs and associated rwoffsets
236 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000237static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000238{
njn810086f2002-11-14 12:42:47 +0000239 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000240 return False;
njn25e49d8e72002-09-23 09:36:25 +0000241 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000242 return False;
243
njn810086f2002-11-14 12:42:47 +0000244 switch (e1->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000245 // case ThreadErr:
246 // case MutexErr:
247 // vg_assert(VG_(needs).core_errors);
248 // return VG_(tm_error_equal)(res, e1, e2);
sewardjde4a1d02002-03-22 01:27:54 +0000249 default:
njn51d827b2005-05-09 01:02:08 +0000250 if (VG_(needs).tool_errors) {
251 return VG_TDICT_CALL(tool_eq_Error, res, e1, e2);
252 } else {
njn95ec8702004-11-22 16:46:13 +0000253 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000254 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000255 e1->ekind);
njn67993252004-11-22 18:02:32 +0000256 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000257 }
sewardjde4a1d02002-03-22 01:27:54 +0000258 }
259}
260
njn810086f2002-11-14 12:42:47 +0000261static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000262{
sewardj71bc3cb2005-05-19 00:25:45 +0000263 if (VG_(clo_xml)) {
264 VG_(message)(Vg_UserMsg, "<error>");
sewardj9f297ca2005-05-20 02:29:52 +0000265 VG_(message)(Vg_UserMsg, " <unique>0x%llx</unique>",
266 Ptr_to_ULong(err));
sewardj71bc3cb2005-05-19 00:25:45 +0000267 VG_(message)(Vg_UserMsg, " <tid>%d</tid>", err->tid);
268 }
269
270 if (!VG_(clo_xml)) {
271 if (printCount)
272 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
273 if (err->tid > 0 && err->tid != last_tid_printed) {
274 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
275 last_tid_printed = err->tid;
276 }
sewardjb5f6f512005-03-10 23:59:00 +0000277 }
njn25e49d8e72002-09-23 09:36:25 +0000278
njn810086f2002-11-14 12:42:47 +0000279 switch (err->ekind) {
sewardjb5f6f512005-03-10 23:59:00 +0000280 // case ThreadErr:
281 // case MutexErr:
282 // vg_assert(VG_(needs).core_errors);
283 // VG_(tm_error_print)(err);
284 // break;
sewardjde4a1d02002-03-22 01:27:54 +0000285 default:
njn95ec8702004-11-22 16:46:13 +0000286 if (VG_(needs).tool_errors)
njn51d827b2005-05-09 01:02:08 +0000287 VG_TDICT_CALL( tool_pp_Error, err );
njn25e49d8e72002-09-23 09:36:25 +0000288 else {
njn95ec8702004-11-22 16:46:13 +0000289 VG_(printf)("\nUnhandled error type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +0000290 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000291 err->ekind);
njn67993252004-11-22 18:02:32 +0000292 VG_(tool_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000293 }
sewardjde4a1d02002-03-22 01:27:54 +0000294 }
sewardj71bc3cb2005-05-19 00:25:45 +0000295
296 if (VG_(clo_xml))
297 VG_(message)(Vg_UserMsg, "</error>");
sewardjde4a1d02002-03-22 01:27:54 +0000298}
299
nethercote04d0fbc2004-01-26 16:48:06 +0000300/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000301 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000302Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000303{
304 Char ch, ch2;
305 Int res;
306
njn43c799e2003-04-08 00:08:52 +0000307 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000308 return False;
309
310 VG_(message)(Vg_UserMsg, "");
311
312 again:
313 VG_(printf)(
314 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000315 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
316 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000317 );
318
sewardj6024b212003-07-13 10:54:33 +0000319 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000320 if (res != 1) goto ioerror;
321 /* res == 1 */
322 if (ch == '\n') return False;
323 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
324 && ch != 'C' && ch != 'c') goto again;
325
sewardj6024b212003-07-13 10:54:33 +0000326 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000327 if (res != 1) goto ioerror;
328 if (ch2 != '\n') goto again;
329
njn43c799e2003-04-08 00:08:52 +0000330 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000331 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000332 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000333 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000334 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000335 vg_assert(ch == 'c' || ch == 'C');
336
337 ioerror:
njn43c799e2003-04-08 00:08:52 +0000338 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000339 return False;
340}
341
342
sewardjb5f6f512005-03-10 23:59:00 +0000343/* Construct an error */
njn25e49d8e72002-09-23 09:36:25 +0000344static __inline__
njn72718642003-07-24 08:45:32 +0000345void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
346 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000347{
njnca82cc02004-11-22 17:18:48 +0000348 tl_assert(tid < VG_N_THREADS);
njn72718642003-07-24 08:45:32 +0000349
njn810086f2002-11-14 12:42:47 +0000350 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000351 err->next = NULL;
352 err->supp = NULL;
353 err->count = 1;
njn72718642003-07-24 08:45:32 +0000354 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000355 if (NULL == where)
njnd01fef72005-03-25 23:35:48 +0000356 err->where = VG_(record_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000357 else
358 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000359
nethercote996901a2004-08-03 13:29:09 +0000360 /* Tool-relevant parts */
njn810086f2002-11-14 12:42:47 +0000361 err->ekind = ekind;
362 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000363 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000364 err->string = s;
365
njn25e49d8e72002-09-23 09:36:25 +0000366 /* sanity... */
njn72718642003-07-24 08:45:32 +0000367 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000368}
369
njn83f9e792005-06-11 05:04:09 +0000370#define ERRTXT_LEN 4096
371
njnf4261312005-03-20 23:45:36 +0000372static void printSuppForIp(UInt n, Addr ip)
373{
njn83f9e792005-06-11 05:04:09 +0000374 static UChar buf[ERRTXT_LEN];
njnf4261312005-03-20 23:45:36 +0000375
njn83f9e792005-06-11 05:04:09 +0000376 if ( VG_(get_fnname_nodemangle) (ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000377 VG_(printf)(" fun:%s\n", buf);
njn83f9e792005-06-11 05:04:09 +0000378 } else if ( VG_(get_objname)(ip, buf, ERRTXT_LEN) ) {
njnf4261312005-03-20 23:45:36 +0000379 VG_(printf)(" obj:%s\n", buf);
380 } else {
381 VG_(printf)(" ???:??? "
382 "# unknown, suppression will not work, sorry\n");
383 }
384}
385
nethercote10d481a2004-01-25 20:33:53 +0000386static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000387{
njn43c799e2003-04-08 00:08:52 +0000388 ExeContext* ec = VG_(get_error_where)(err);
389 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000390
sewardjb5f6f512005-03-10 23:59:00 +0000391 /* At most VG_MAX_SUPP_CALLERS names */
392 if (stop_at > VG_MAX_SUPP_CALLERS) stop_at = VG_MAX_SUPP_CALLERS;
njn43c799e2003-04-08 00:08:52 +0000393 vg_assert(stop_at > 0);
394
395 VG_(printf)("{\n");
396 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000397
sewardjb5f6f512005-03-10 23:59:00 +0000398 if (ThreadErr == err->ekind || MutexErr == err->ekind) {
njn6a230532003-07-21 10:38:23 +0000399 VG_(printf)(" core:PThread\n");
400
401 } else {
njn51d827b2005-05-09 01:02:08 +0000402 Char* name = VG_TDICT_CALL(tool_get_error_name, err);
njn6a230532003-07-21 10:38:23 +0000403 if (NULL == name) {
404 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000405 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000406 return;
407 }
408 VG_(printf)(" %s:%s\n", VG_(details).name, name);
njn51d827b2005-05-09 01:02:08 +0000409 VG_TDICT_CALL(tool_print_extra_suppression_info, err);
njn6a230532003-07-21 10:38:23 +0000410 }
njn43c799e2003-04-08 00:08:52 +0000411
njnf4261312005-03-20 23:45:36 +0000412 // Print stack trace elements
njnd01fef72005-03-25 23:35:48 +0000413 VG_(apply_StackTrace)(printSuppForIp, VG_(extract_StackTrace)(ec), stop_at);
njn43c799e2003-04-08 00:08:52 +0000414
415 VG_(printf)("}\n");
416}
417
njnb4aee052003-04-15 14:09:58 +0000418static
nethercote04d0fbc2004-01-26 16:48:06 +0000419void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000420{
sewardjd153fae2005-01-10 17:24:47 +0000421 Bool still_noisy = True;
422
nethercote04d0fbc2004-01-26 16:48:06 +0000423 /* Perhaps we want a debugger attach at this point? */
424 if (allow_db_attach &&
njnd2b17112005-04-19 04:10:25 +0000425 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
426 {
nethercote04d0fbc2004-01-26 16:48:06 +0000427 VG_(printf)("starting debugger\n");
428 VG_(start_debugger)( err->tid );
njnd2b17112005-04-19 04:10:25 +0000429 }
njn43c799e2003-04-08 00:08:52 +0000430 /* Or maybe we want to generate the error's suppression? */
sewardjd153fae2005-01-10 17:24:47 +0000431 if (VG_(clo_gen_suppressions) == 2
432 || (VG_(clo_gen_suppressions) == 1
njnd2b17112005-04-19 04:10:25 +0000433 && VG_(is_action_requested)( "Print suppression", &still_noisy ))
sewardjd153fae2005-01-10 17:24:47 +0000434 ) {
nethercote42602b12004-01-25 19:30:29 +0000435 gen_suppression(err);
sewardjd153fae2005-01-10 17:24:47 +0000436 if (VG_(clo_gen_suppressions) == 1 && !still_noisy)
437 VG_(clo_gen_suppressions) = 0;
njn43c799e2003-04-08 00:08:52 +0000438 }
439}
440
441/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
442 just for pretty printing purposes. */
443static Bool is_first_shown_context = True;
444
njn25e49d8e72002-09-23 09:36:25 +0000445/* Top-level entry point to the error management subsystem.
446 All detected errors are notified here; this routine decides if/when the
447 user should see the error. */
njn72718642003-07-24 08:45:32 +0000448void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000449 ErrorKind ekind, Addr a, Char* s, void* extra )
450{
njn810086f2002-11-14 12:42:47 +0000451 Error err;
452 Error* p;
453 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000454 UInt extra_size;
njn695c16e2005-03-27 03:40:28 +0000455 VgRes exe_res = Vg_MedRes;
456 static Bool stopping_message = False;
457 static Bool slowdown_message = False;
458 static Int n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000459
njn14319cc2005-03-13 06:26:22 +0000460 /* After M_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
461 been found, or M_COLLECT_NO_ERRORS_AFTER_FOUND total errors
sewardjf2537be2002-04-24 21:03:47 +0000462 have been found, just refuse to collect any more. This stops
463 the burden of the error-management system becoming excessive in
464 extremely buggy programs, although it does make it pretty
465 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000466 if (VG_(clo_error_limit)
njn695c16e2005-03-27 03:40:28 +0000467 && (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN
njn14319cc2005-03-13 06:26:22 +0000468 || n_errs_found >= M_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000469 if (!stopping_message) {
470 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000471
njn695c16e2005-03-27 03:40:28 +0000472 if (n_errs_shown >= M_COLLECT_NO_ERRORS_AFTER_SHOWN) {
sewardjf2537be2002-04-24 21:03:47 +0000473 VG_(message)(Vg_UserMsg,
474 "More than %d different errors detected. "
475 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000476 M_COLLECT_NO_ERRORS_AFTER_SHOWN );
sewardjf2537be2002-04-24 21:03:47 +0000477 } else {
478 VG_(message)(Vg_UserMsg,
479 "More than %d total errors detected. "
480 "I'm not reporting any more.",
njn14319cc2005-03-13 06:26:22 +0000481 M_COLLECT_NO_ERRORS_AFTER_FOUND );
sewardjf2537be2002-04-24 21:03:47 +0000482 }
483
sewardjde4a1d02002-03-22 01:27:54 +0000484 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000485 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000486 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000487 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000488 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000489 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000490 VG_(message)(Vg_UserMsg,
491 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000492 VG_(message)(Vg_UserMsg, "");
493 stopping_message = True;
494 }
495 return;
496 }
497
njn14319cc2005-03-13 06:26:22 +0000498 /* After M_COLLECT_ERRORS_SLOWLY_AFTER different errors have
sewardjde4a1d02002-03-22 01:27:54 +0000499 been found, be much more conservative about collecting new
500 ones. */
njn695c16e2005-03-27 03:40:28 +0000501 if (n_errs_shown >= M_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000502 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000503 if (!slowdown_message) {
504 VG_(message)(Vg_UserMsg, "");
505 VG_(message)(Vg_UserMsg,
506 "More than %d errors detected. Subsequent errors",
njn14319cc2005-03-13 06:26:22 +0000507 M_COLLECT_ERRORS_SLOWLY_AFTER);
sewardjde4a1d02002-03-22 01:27:54 +0000508 VG_(message)(Vg_UserMsg,
509 "will still be recorded, but in less detail than before.");
510 slowdown_message = True;
511 }
512 }
513
njn25e49d8e72002-09-23 09:36:25 +0000514 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000515 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000516
517 /* First, see if we've got an error record matching this one. */
njn695c16e2005-03-27 03:40:28 +0000518 p = errors;
sewardjde4a1d02002-03-22 01:27:54 +0000519 p_prev = NULL;
520 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000521 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000522 /* Found it. */
523 p->count++;
524 if (p->supp != NULL) {
525 /* Deal correctly with suppressed errors. */
526 p->supp->count++;
nethercotef2b11482004-08-02 12:36:01 +0000527 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000528 } else {
nethercotef2b11482004-08-02 12:36:01 +0000529 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000530 }
531
532 /* Move p to the front of the list so that future searches
533 for it are faster. */
534 if (p_prev != NULL) {
535 vg_assert(p_prev->next == p);
njn695c16e2005-03-27 03:40:28 +0000536 p_prev->next = p->next;
537 p->next = errors;
538 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000539 }
sewardj7ebf7c32003-07-24 21:29:40 +0000540
sewardjde4a1d02002-03-22 01:27:54 +0000541 return;
542 }
543 p_prev = p;
544 p = p->next;
545 }
546
547 /* Didn't see it. Copy and add. */
548
njn43c799e2003-04-08 00:08:52 +0000549 /* OK, we're really going to collect it. The context is on the stack and
550 will disappear shortly, so we must copy it. First do the main
njn02bc4b82005-05-15 17:28:26 +0000551 (non-'extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000552
njn02bc4b82005-05-15 17:28:26 +0000553 Then VG_(tdict).tool_update_extra can update the 'extra' part. This
njn51d827b2005-05-09 01:02:08 +0000554 is for when there are more details to fill in which take time to work
555 out but don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000556 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000557 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000558
njn02bc4b82005-05-15 17:28:26 +0000559 Then, if there is an 'extra' part, copy it too, using the size that
njn51d827b2005-05-09 01:02:08 +0000560 VG_(tdict).tool_update_extra returned. Also allow for people using
561 the void* extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000562 */
563
564 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000565 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000566 *p = err;
njn43c799e2003-04-08 00:08:52 +0000567
njn02bc4b82005-05-15 17:28:26 +0000568 /* update 'extra' */
sewardjb5f6f512005-03-10 23:59:00 +0000569 switch (ekind) {
570 // case ThreadErr:
571 // case MutexErr:
572 // vg_assert(VG_(needs).core_errors);
573 // extra_size = VG_(tm_error_update_extra)(p);
574 // break;
575 default:
576 vg_assert(VG_(needs).tool_errors);
njn51d827b2005-05-09 01:02:08 +0000577 extra_size = VG_TDICT_CALL(tool_update_extra, p);
sewardjb5f6f512005-03-10 23:59:00 +0000578 break;
579 }
njn43c799e2003-04-08 00:08:52 +0000580
njn02bc4b82005-05-15 17:28:26 +0000581 /* copy block pointed to by 'extra', if there is one */
sewardjb5f6f512005-03-10 23:59:00 +0000582 if (NULL != p->extra && 0 != extra_size) {
583 void* new_extra = VG_(malloc)(extra_size);
584 VG_(memcpy)(new_extra, p->extra, extra_size);
585 p->extra = new_extra;
njn43c799e2003-04-08 00:08:52 +0000586 }
587
njn695c16e2005-03-27 03:40:28 +0000588 p->next = errors;
njn25e49d8e72002-09-23 09:36:25 +0000589 p->supp = is_suppressible_error(&err);
njn695c16e2005-03-27 03:40:28 +0000590 errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000591 if (p->supp == NULL) {
nethercotef2b11482004-08-02 12:36:01 +0000592 n_errs_found++;
sewardjde4a1d02002-03-22 01:27:54 +0000593 if (!is_first_shown_context)
594 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000595 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000596 is_first_shown_context = False;
njn695c16e2005-03-27 03:40:28 +0000597 n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000598 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000599 } else {
nethercotef2b11482004-08-02 12:36:01 +0000600 n_errs_suppressed++;
sewardjde4a1d02002-03-22 01:27:54 +0000601 p->supp->count++;
602 }
603}
604
njn43c799e2003-04-08 00:08:52 +0000605/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000606 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000607 guaranteed to only happen once. This avoids all the recording and
608 comparing stuff. But they can be suppressed; returns True if it is
njn02bc4b82005-05-15 17:28:26 +0000609 suppressed. Bool 'print_error' dictates whether to print the error.
610 Bool 'count_error' dictates whether to count the error in n_errs_found.
njn47363ab2003-04-21 13:24:40 +0000611*/
njn72718642003-07-24 08:45:32 +0000612Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000613 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000614 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000615{
616 Error err;
617
618 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000619 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000620
621 /* Unless it's suppressed, we're going to show it. Don't need to make
622 a copy, because it's only temporary anyway.
623
njn02bc4b82005-05-15 17:28:26 +0000624 Then update the 'extra' part with VG_(tdict).tool_update_extra),
njn51d827b2005-05-09 01:02:08 +0000625 because that can have an affect on whether it's suppressed. Ignore
626 the size return value of VG_(tdict).tool_update_extra, because we're
njn02bc4b82005-05-15 17:28:26 +0000627 not copying 'extra'. */
njn51d827b2005-05-09 01:02:08 +0000628 (void)VG_TDICT_CALL(tool_update_extra, &err);
njn43c799e2003-04-08 00:08:52 +0000629
630 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000631 if (count_error)
nethercotef2b11482004-08-02 12:36:01 +0000632 n_errs_found++;
njn43c799e2003-04-08 00:08:52 +0000633
634 if (print_error) {
635 if (!is_first_shown_context)
636 VG_(message)(Vg_UserMsg, "");
637 pp_Error(&err, False);
638 is_first_shown_context = False;
639 }
nethercote04d0fbc2004-01-26 16:48:06 +0000640 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000641
642 return False;
643
644 } else {
nethercotef2b11482004-08-02 12:36:01 +0000645 n_errs_suppressed++;
njn43c799e2003-04-08 00:08:52 +0000646 return True;
647 }
648}
649
sewardjde4a1d02002-03-22 01:27:54 +0000650
sewardjde4a1d02002-03-22 01:27:54 +0000651/*------------------------------------------------------------*/
652/*--- Exported fns ---*/
653/*------------------------------------------------------------*/
654
sewardj71bc3cb2005-05-19 00:25:45 +0000655/* Show the used suppressions. Returns False if no suppression
656 got used. */
657static Bool show_used_suppressions ( void )
658{
659 Supp *su;
660 Bool any_supp;
661
sewardj7c9e57c2005-05-24 14:21:45 +0000662 if (VG_(clo_xml))
663 VG_(message)(Vg_DebugMsg, "<suppcounts>");
664
sewardj71bc3cb2005-05-19 00:25:45 +0000665 any_supp = False;
666 for (su = suppressions; su != NULL; su = su->next) {
667 if (su->count <= 0)
668 continue;
669 any_supp = True;
670 if (VG_(clo_xml)) {
671 VG_(message)(Vg_DebugMsg,
sewardj8665d8e2005-06-01 17:35:23 +0000672 " <pair>\n"
673 " <count>%d</count>\n"
674 " <name>%s</name>\n"
675 " </pair>",
sewardj71bc3cb2005-05-19 00:25:45 +0000676 su->count, su->sname);
677 } else {
678 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
679 }
680 }
681
sewardj7c9e57c2005-05-24 14:21:45 +0000682 if (VG_(clo_xml))
sewardj8665d8e2005-06-01 17:35:23 +0000683 VG_(message)(Vg_DebugMsg, "</suppcounts>");
sewardj7c9e57c2005-05-24 14:21:45 +0000684
sewardj71bc3cb2005-05-19 00:25:45 +0000685 return any_supp;
686}
687
688
sewardj9f297ca2005-05-20 02:29:52 +0000689/* Show all the errors that occurred, and possibly also the
690 suppressions used. */
sewardjde4a1d02002-03-22 01:27:54 +0000691void VG_(show_all_errors) ( void )
692{
njn810086f2002-11-14 12:42:47 +0000693 Int i, n_min;
694 Int n_err_contexts, n_supp_contexts;
695 Error *p, *p_min;
696 Supp *su;
697 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000698
699 if (VG_(clo_verbosity) == 0)
700 return;
701
702 n_err_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000703 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000704 if (p->supp == NULL)
705 n_err_contexts++;
706 }
707
708 n_supp_contexts = 0;
njn695c16e2005-03-27 03:40:28 +0000709 for (su = suppressions; su != NULL; su = su->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000710 if (su->count > 0)
711 n_supp_contexts++;
712 }
sewardj71bc3cb2005-05-19 00:25:45 +0000713
714 /* If we're printing XML, just show the suppressions and stop.
715 */
716 if (VG_(clo_xml)) {
717 (void)show_used_suppressions();
718 return;
719 }
720
721 /* We only get here if not printing XML. */
sewardjde4a1d02002-03-22 01:27:54 +0000722 VG_(message)(Vg_UserMsg,
723 "ERROR SUMMARY: "
724 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000725 n_errs_found, n_err_contexts,
726 n_errs_suppressed, n_supp_contexts );
sewardjde4a1d02002-03-22 01:27:54 +0000727
728 if (VG_(clo_verbosity) <= 1)
729 return;
730
731 /* Print the contexts in order of increasing error count. */
732 for (i = 0; i < n_err_contexts; i++) {
733 n_min = (1 << 30) - 1;
734 p_min = NULL;
njn695c16e2005-03-27 03:40:28 +0000735 for (p = errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000736 if (p->supp != NULL) continue;
737 if (p->count < n_min) {
738 n_min = p->count;
739 p_min = p;
740 }
741 }
njn67993252004-11-22 18:02:32 +0000742 if (p_min == NULL) VG_(tool_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000743
744 VG_(message)(Vg_UserMsg, "");
745 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
746 p_min->count,
747 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000748 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000749
750 if ((i+1 == VG_(clo_dump_error))) {
njnd01fef72005-03-25 23:35:48 +0000751 StackTrace ips = VG_(extract_StackTrace)(p_min->where);
sewardjfa8ec112005-01-19 11:55:34 +0000752 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to debugging*/,
njn394213a2005-06-19 18:38:24 +0000753 ips[0], /*debugging*/True, 0xFE/*verbosity*/,
754 /*bbs_done*/0);
sewardjde4a1d02002-03-22 01:27:54 +0000755 }
756
757 p_min->count = 1 << 30;
758 }
759
760 if (n_supp_contexts > 0)
761 VG_(message)(Vg_DebugMsg, "");
sewardj71bc3cb2005-05-19 00:25:45 +0000762 any_supp = show_used_suppressions();
sewardjde4a1d02002-03-22 01:27:54 +0000763
764 if (n_err_contexts > 0) {
765 if (any_supp)
766 VG_(message)(Vg_UserMsg, "");
767 VG_(message)(Vg_UserMsg,
768 "IN SUMMARY: "
769 "%d errors from %d contexts (suppressed: %d from %d)",
nethercotef2b11482004-08-02 12:36:01 +0000770 n_errs_found, n_err_contexts, n_errs_suppressed,
sewardjde4a1d02002-03-22 01:27:54 +0000771 n_supp_contexts );
772 VG_(message)(Vg_UserMsg, "");
773 }
774}
775
sewardj9f297ca2005-05-20 02:29:52 +0000776
777/* Show occurrence counts of all errors, in XML form. */
778void VG_(show_error_counts_as_XML) ( void )
779{
780 Error* err;
781 VG_(message)(Vg_UserMsg, "<errorcounts>");
782 for (err = errors; err != NULL; err = err->next) {
783 if (err->supp != NULL)
784 continue;
785 if (err->count <= 0)
786 continue;
787 VG_(message)(
sewardj8665d8e2005-06-01 17:35:23 +0000788 Vg_UserMsg, " <pair> <count>%d</count> "
789 "<unique>0x%llx</unique> </pair>",
sewardj7c9e57c2005-05-24 14:21:45 +0000790 err->count, Ptr_to_ULong(err)
sewardj9f297ca2005-05-20 02:29:52 +0000791 );
792 }
793 VG_(message)(Vg_UserMsg, "</errorcounts>");
794}
795
796
sewardjde4a1d02002-03-22 01:27:54 +0000797/*------------------------------------------------------------*/
798/*--- Standard suppressions ---*/
799/*------------------------------------------------------------*/
800
801/* Get a non-blank, non-comment line of at most nBuf chars from fd.
802 Skips leading spaces on the line. Return True if EOF was hit instead.
803*/
njn4ba5a792002-09-30 10:23:54 +0000804Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000805{
806 Char ch;
807 Int n, i;
808 while (True) {
809 /* First, read until a non-blank char appears. */
810 while (True) {
811 n = VG_(read)(fd, &ch, 1);
njn0c0f32a2005-03-26 04:14:01 +0000812 if (n == 1 && !VG_(isspace)(ch)) break;
sewardjde4a1d02002-03-22 01:27:54 +0000813 if (n == 0) return True;
814 }
815
816 /* Now, read the line into buf. */
817 i = 0;
818 buf[i++] = ch; buf[i] = 0;
819 while (True) {
820 n = VG_(read)(fd, &ch, 1);
821 if (n == 0) return False; /* the next call will return True */
822 if (ch == '\n') break;
823 if (i > 0 && i == nBuf-1) i--;
824 buf[i++] = ch; buf[i] = 0;
825 }
njn0c0f32a2005-03-26 04:14:01 +0000826 while (i > 1 && VG_(isspace)(buf[i-1])) {
sewardjde4a1d02002-03-22 01:27:54 +0000827 i--; buf[i] = 0;
828 };
829
njn02bc4b82005-05-15 17:28:26 +0000830 /* VG_(printf)("The line is '%s'\n", buf); */
sewardjde4a1d02002-03-22 01:27:54 +0000831 /* Ok, we have a line. If a non-comment line, return.
832 If a comment line, start all over again. */
833 if (buf[0] != '#') return False;
834 }
835}
836
837
838/* *p_caller contains the raw name of a caller, supposedly either
839 fun:some_function_name or
840 obj:some_object_name.
841 Set *p_ty accordingly and advance *p_caller over the descriptor
842 (fun: or obj:) part.
843 Returns False if failed.
844*/
sewardjb5f6f512005-03-10 23:59:00 +0000845static Bool setLocationTy ( SuppLoc* p )
sewardjde4a1d02002-03-22 01:27:54 +0000846{
sewardjb5f6f512005-03-10 23:59:00 +0000847 if (VG_(strncmp)(p->name, "fun:", 4) == 0) {
848 p->name += 4;
849 p->ty = FunName;
sewardjde4a1d02002-03-22 01:27:54 +0000850 return True;
851 }
sewardjb5f6f512005-03-10 23:59:00 +0000852 if (VG_(strncmp)(p->name, "obj:", 4) == 0) {
853 p->name += 4;
854 p->ty = ObjName;
sewardjde4a1d02002-03-22 01:27:54 +0000855 return True;
856 }
857 VG_(printf)("location should start with fun: or obj:\n");
858 return False;
859}
860
861
nethercote7cc9c232004-01-21 15:08:04 +0000862/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000863static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000864Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000865{
866 Bool found;
867 Char *s = NULL; /* Shut gcc up */
868 Int len = VG_(strlen)(name);
869
870 found = (NULL != (s = VG_(strstr)(names, name)) &&
871 (s == names || *(s-1) == ',') &&
872 (*(s+len) == ',' || *(s+len) == '\0')
873 );
874
875 return found;
876}
877
njn695c16e2005-03-27 03:40:28 +0000878/* Read suppressions from the file specified in VG_(clo_suppressions)
sewardjde4a1d02002-03-22 01:27:54 +0000879 and place them in the suppressions list. If there's any difficulty
880 doing this, just give up -- there's no point in trying to recover.
881*/
sewardjde4a1d02002-03-22 01:27:54 +0000882static void load_one_suppressions_file ( Char* filename )
883{
884# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000885 Int fd, i;
sewardjb5f6f512005-03-10 23:59:00 +0000886 Bool eof;
njnc40c3a82002-10-02 11:02:27 +0000887 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000888 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000889 Char* supp_name;
sewardjb5f6f512005-03-10 23:59:00 +0000890 Char* err_str = NULL;
891 SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
njnc40c3a82002-10-02 11:02:27 +0000892
njn25e49d8e72002-09-23 09:36:25 +0000893 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000894 if (fd < 0) {
njn02bc4b82005-05-15 17:28:26 +0000895 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000896 filename );
897 VG_(exit)(1);
898 }
899
sewardjb5f6f512005-03-10 23:59:00 +0000900#define BOMB(S) { err_str = S; goto syntax_error; }
901
sewardjde4a1d02002-03-22 01:27:54 +0000902 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000903 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000904 Supp* supp;
905 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000906 supp->count = 0;
sewardjb5f6f512005-03-10 23:59:00 +0000907
908 // Initialise temporary reading-in buffer.
909 for (i = 0; i < VG_MAX_SUPP_CALLERS; i++) {
910 tmp_callers[i].ty = NoName;
911 tmp_callers[i].name = NULL;
912 }
913
njn810086f2002-11-14 12:42:47 +0000914 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000915
njn4ba5a792002-09-30 10:23:54 +0000916 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000917 if (eof) break;
918
sewardjb5f6f512005-03-10 23:59:00 +0000919 if (!VG_STREQ(buf, "{")) BOMB("expected '{' or end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000920
njn4ba5a792002-09-30 10:23:54 +0000921 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000922
923 if (eof || VG_STREQ(buf, "}")) BOMB("unexpected '}'");
924
njn25e49d8e72002-09-23 09:36:25 +0000925 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000926
njn4ba5a792002-09-30 10:23:54 +0000927 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000928
sewardjb5f6f512005-03-10 23:59:00 +0000929 if (eof) BOMB("unexpected end-of-file");
sewardjde4a1d02002-03-22 01:27:54 +0000930
njn94065fd2004-11-22 19:26:27 +0000931 /* Check it has the "tool1,tool2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000932 i = 0;
933 while (True) {
934 if (buf[i] == ':') break;
sewardjb5f6f512005-03-10 23:59:00 +0000935 if (buf[i] == '\0') BOMB("malformed 'tool1,tool2,...:supp' line");
njnc40c3a82002-10-02 11:02:27 +0000936 i++;
njn25e49d8e72002-09-23 09:36:25 +0000937 }
njnc40c3a82002-10-02 11:02:27 +0000938 buf[i] = '\0'; /* Replace ':', splitting into two strings */
939
nethercote7cc9c232004-01-21 15:08:04 +0000940 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000941 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000942
nethercote7cc9c232004-01-21 15:08:04 +0000943 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000944 {
sewardjb5f6f512005-03-10 23:59:00 +0000945 // A core suppression
njn43c799e2003-04-08 00:08:52 +0000946 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000947 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000948 else
sewardjb5f6f512005-03-10 23:59:00 +0000949 BOMB("unknown core suppression type");
njnc40c3a82002-10-02 11:02:27 +0000950 }
njn95ec8702004-11-22 16:46:13 +0000951 else if (VG_(needs).tool_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000952 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000953 {
sewardjb5f6f512005-03-10 23:59:00 +0000954 // A tool suppression
njn51d827b2005-05-09 01:02:08 +0000955 if (VG_TDICT_CALL(tool_recognised_suppression, supp_name, supp)) {
njn810086f2002-11-14 12:42:47 +0000956 /* Do nothing, function fills in supp->skind */
sewardjb5f6f512005-03-10 23:59:00 +0000957 } else {
958 BOMB("unknown tool suppression type");
959 }
njnc40c3a82002-10-02 11:02:27 +0000960 }
njn25e49d8e72002-09-23 09:36:25 +0000961 else {
sewardjb5f6f512005-03-10 23:59:00 +0000962 // Ignore rest of suppression
njn25e49d8e72002-09-23 09:36:25 +0000963 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000964 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjb5f6f512005-03-10 23:59:00 +0000965 if (eof) BOMB("unexpected end-of-file");
njn43c799e2003-04-08 00:08:52 +0000966 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000967 break;
968 }
969 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000970 }
971
njn95ec8702004-11-22 16:46:13 +0000972 if (VG_(needs).tool_errors &&
njn51d827b2005-05-09 01:02:08 +0000973 !VG_TDICT_CALL(tool_read_extra_suppression_info, fd, buf, N_BUF, supp))
sewardjb5f6f512005-03-10 23:59:00 +0000974 {
975 BOMB("bad or missing extra suppression info");
sewardjde4a1d02002-03-22 01:27:54 +0000976 }
977
sewardjb5f6f512005-03-10 23:59:00 +0000978 i = 0;
979 while (True) {
980 eof = VG_(get_line) ( fd, buf, N_BUF );
981 if (eof)
982 BOMB("unexpected end-of-file");
983 if (VG_STREQ(buf, "}")) {
984 if (i > 0) {
985 break;
986 } else {
987 BOMB("missing stack trace");
988 }
989 }
990 if (i == VG_MAX_SUPP_CALLERS)
991 BOMB("too many callers in stack trace");
992 if (i > 0 && i >= VG_(clo_backtrace_size))
993 break;
994 tmp_callers[i].name = VG_(arena_strdup)(VG_AR_CORE, buf);
995 if (!setLocationTy(&(tmp_callers[i])))
996 BOMB("location should start with 'fun:' or 'obj:'");
997 i++;
998 }
999
1000 // If the num callers is >= VG_(clo_backtrace_size), ignore any extra
1001 // lines and grab the '}'.
sewardj57a8f5f2003-07-06 01:40:11 +00001002 if (!VG_STREQ(buf, "}")) {
sewardjb5f6f512005-03-10 23:59:00 +00001003 do {
1004 eof = VG_(get_line) ( fd, buf, N_BUF );
1005 } while (!eof && !VG_STREQ(buf, "}"));
1006 }
1007
1008 // Copy tmp_callers[] into supp->callers[]
1009 supp->n_callers = i;
1010 supp->callers = VG_(arena_malloc)(VG_AR_CORE, i*sizeof(SuppLoc));
1011 for (i = 0; i < supp->n_callers; i++) {
1012 supp->callers[i] = tmp_callers[i];
sewardj57a8f5f2003-07-06 01:40:11 +00001013 }
1014
njn695c16e2005-03-27 03:40:28 +00001015 supp->next = suppressions;
1016 suppressions = supp;
sewardjde4a1d02002-03-22 01:27:54 +00001017 }
sewardjde4a1d02002-03-22 01:27:54 +00001018 VG_(close)(fd);
1019 return;
1020
1021 syntax_error:
sewardjb5f6f512005-03-10 23:59:00 +00001022 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +00001023 "FATAL: in suppressions file '%s': %s", filename, err_str );
sewardjb5f6f512005-03-10 23:59:00 +00001024
sewardjde4a1d02002-03-22 01:27:54 +00001025 VG_(close)(fd);
1026 VG_(message)(Vg_UserMsg, "exiting now.");
nethercote8ed8a892004-11-08 13:24:25 +00001027 VG_(exit)(1);
sewardjde4a1d02002-03-22 01:27:54 +00001028
sewardjb5f6f512005-03-10 23:59:00 +00001029# undef BOMB
sewardjde4a1d02002-03-22 01:27:54 +00001030# undef N_BUF
1031}
1032
1033
1034void VG_(load_suppressions) ( void )
1035{
1036 Int i;
njn695c16e2005-03-27 03:40:28 +00001037 suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +00001038 for (i = 0; i < VG_(clo_n_suppressions); i++) {
1039 if (VG_(clo_verbosity) > 1) {
njn3f04d242005-03-20 18:21:14 +00001040 VG_(message)(Vg_DebugMsg, "Reading suppressions file: %s",
1041 VG_(clo_suppressions)[i] );
sewardjde4a1d02002-03-22 01:27:54 +00001042 }
1043 load_one_suppressions_file( VG_(clo_suppressions)[i] );
1044 }
1045}
1046
sewardjb5f6f512005-03-10 23:59:00 +00001047static
njn810086f2002-11-14 12:42:47 +00001048Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +00001049{
njn810086f2002-11-14 12:42:47 +00001050 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +00001051 case PThreadSupp:
sewardjb5f6f512005-03-10 23:59:00 +00001052 return (err->ekind == ThreadErr || err->ekind == MutexErr);
njn25e49d8e72002-09-23 09:36:25 +00001053 default:
njn95ec8702004-11-22 16:46:13 +00001054 if (VG_(needs).tool_errors) {
njn51d827b2005-05-09 01:02:08 +00001055 return VG_TDICT_CALL(tool_error_matches_suppression, err, su);
njn25e49d8e72002-09-23 09:36:25 +00001056 } else {
1057 VG_(printf)(
njn95ec8702004-11-22 16:46:13 +00001058 "\nUnhandled suppression type: %u. VG_(needs).tool_errors\n"
njn25e49d8e72002-09-23 09:36:25 +00001059 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +00001060 err->ekind);
njn67993252004-11-22 18:02:32 +00001061 VG_(tool_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +00001062 }
1063 }
1064}
1065
sewardjb5f6f512005-03-10 23:59:00 +00001066static
1067Bool supp_matches_callers(Error* err, Supp* su)
njn25e49d8e72002-09-23 09:36:25 +00001068{
1069 Int i;
njn83f9e792005-06-11 05:04:09 +00001070 Char caller_name[ERRTXT_LEN];
njnd01fef72005-03-25 23:35:48 +00001071 StackTrace ips = VG_(extract_StackTrace)(err->where);
njn25e49d8e72002-09-23 09:36:25 +00001072
sewardjb5f6f512005-03-10 23:59:00 +00001073 for (i = 0; i < su->n_callers; i++) {
njnd01fef72005-03-25 23:35:48 +00001074 Addr a = ips[i];
sewardjb5f6f512005-03-10 23:59:00 +00001075 vg_assert(su->callers[i].name != NULL);
1076 switch (su->callers[i].ty) {
1077 case ObjName:
njn83f9e792005-06-11 05:04:09 +00001078 if (!VG_(get_objname)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001079 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001080 break;
1081
1082 case FunName:
1083 // Nb: mangled names used in suppressions
njn83f9e792005-06-11 05:04:09 +00001084 if (!VG_(get_fnname_nodemangle)(a, caller_name, ERRTXT_LEN))
njn58c9f812005-03-11 04:46:09 +00001085 return False;
sewardjb5f6f512005-03-10 23:59:00 +00001086 break;
njn67993252004-11-22 18:02:32 +00001087 default: VG_(tool_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +00001088 }
sewardjb5f6f512005-03-10 23:59:00 +00001089 if (!VG_(string_match)(su->callers[i].name, caller_name))
1090 return False;
njn25e49d8e72002-09-23 09:36:25 +00001091 }
1092
1093 /* If we reach here, it's a match */
1094 return True;
1095}
sewardjde4a1d02002-03-22 01:27:54 +00001096
njn810086f2002-11-14 12:42:47 +00001097/* Does an error context match a suppression? ie is this a suppressible
1098 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +00001099 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +00001100*/
njn810086f2002-11-14 12:42:47 +00001101static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +00001102{
njn810086f2002-11-14 12:42:47 +00001103 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +00001104
sewardjde4a1d02002-03-22 01:27:54 +00001105 /* See if the error context matches any suppression. */
njn695c16e2005-03-27 03:40:28 +00001106 for (su = suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001107 if (supp_matches_error(su, err) &&
sewardjb5f6f512005-03-10 23:59:00 +00001108 supp_matches_callers(err, su))
1109 {
njn25e49d8e72002-09-23 09:36:25 +00001110 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001111 }
sewardjde4a1d02002-03-22 01:27:54 +00001112 }
njn25e49d8e72002-09-23 09:36:25 +00001113 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001114}
1115
sewardjde4a1d02002-03-22 01:27:54 +00001116/*--------------------------------------------------------------------*/
njneb8896b2005-06-04 20:03:55 +00001117/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +00001118/*--------------------------------------------------------------------*/