blob: 646cebc8a151e662a12c68a9e175ecb5ad4f9e4a [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- Management of error messages. vg_errcontext.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
njnc9539842002-10-02 13:26:35 +00007 This file is part of Valgrind, an extensible x86 protected-mode
8 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +00009
nethercotebb1c9912004-01-04 16:43:23 +000010 Copyright (C) 2000-2004 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
31#include "vg_include.h"
sewardjde4a1d02002-03-22 01:27:54 +000032
33/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000034/*--- Globals ---*/
sewardjde4a1d02002-03-22 01:27:54 +000035/*------------------------------------------------------------*/
36
sewardjde4a1d02002-03-22 01:27:54 +000037/* The list of error contexts found, both suppressed and unsuppressed.
38 Initially empty, and grows as errors are detected. */
njn810086f2002-11-14 12:42:47 +000039static Error* vg_errors = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000040
41/* The list of suppression directives, as read from the specified
42 suppressions file. */
njn810086f2002-11-14 12:42:47 +000043static Supp* vg_suppressions = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000044
45/* Running count of unsuppressed errors detected. */
njn47363ab2003-04-21 13:24:40 +000046UInt VG_(n_errs_found) = 0;
sewardjde4a1d02002-03-22 01:27:54 +000047
48/* Running count of suppressed errors detected. */
49static UInt vg_n_errs_suppressed = 0;
50
51/* forwards ... */
njn810086f2002-11-14 12:42:47 +000052static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000053
54
55/*------------------------------------------------------------*/
nethercote4a184902004-08-02 12:21:09 +000056/*--- Error type ---*/
57/*------------------------------------------------------------*/
58
59/* Note: it is imperative this doesn't overlap with (0..) at all, as skins
60 * effectively extend it by defining their own enums in the (0..) range. */
61typedef
62 enum {
63 PThreadErr = -1, // Pthreading error
64 }
65 CoreErrorKind;
66
67/* Errors. Extensible (via the 'extra' field). Tools can use a normal
68 enum (with element values in the normal range (0..)) for `ekind'.
69 Functions for getting/setting the tool-relevant fields are in
70 include/vg_skin.h.
71
72 When errors are found and recorded with VG_(maybe_record_error)(), all
73 the tool must do is pass in the four parameters; core will
74 allocate/initialise the error record.
75*/
76struct _Error {
77 struct _Error* next;
78 // NULL if unsuppressed; or ptr to suppression record.
79 Supp* supp;
80 Int count;
81 ThreadId tid;
82
83 // The tool-specific part
84 ExeContext* where; // Initialised by core
85 Int ekind; // Used by ALL. Must be in the range (0..)
86 Addr addr; // Used frequently
87 Char* string; // Used frequently
88 void* extra; // For any tool-specific extras
89};
90
91ExeContext* VG_(get_error_where) ( Error* err )
92{
93 return err->where;
94}
95
96ErrorKind VG_(get_error_kind) ( Error* err )
97{
98 return err->ekind;
99}
100
101Addr VG_(get_error_address) ( Error* err )
102{
103 return err->addr;
104}
105
106Char* VG_(get_error_string) ( Error* err )
107{
108 return err->string;
109}
110
111void* VG_(get_error_extra) ( Error* err )
112{
113 return err->extra;
114}
115
116/*------------------------------------------------------------*/
117/*--- Suppression type ---*/
118/*------------------------------------------------------------*/
119
120/* Note: it is imperative this doesn't overlap with (0..) at all, as tools
121 * effectively extend it by defining their own enums in the (0..) range. */
122typedef
123 enum {
124 PThreadSupp = -1, /* Matches PThreadErr */
125 }
126 CoreSuppKind;
127
128/* For each caller specified for a suppression, record the nature of
129 the caller name. Not of interest to tools. */
130typedef
131 enum {
132 ObjName, /* Name is of an shared object file. */
133 FunName /* Name is of a function. */
134 }
135 SuppLocTy;
136
137/* Suppressions. Tools can get/set tool-relevant parts with functions
138 declared in include/vg_skin.h. Extensible via the 'extra' field.
139 Tools can use a normal enum (with element values in the normal range
140 (0..)) for `skind'. */
141struct _Supp {
142 struct _Supp* next;
143 Int count; // The number of times this error has been suppressed.
144 Char* sname; // The name by which the suppression is referred to.
145 /* First two (name of fn where err occurs, and immediate caller)
146 * are mandatory; extra two are optional. */
147 SuppLocTy caller_ty[VG_N_SUPP_CALLERS];
148 Char* caller [VG_N_SUPP_CALLERS];
149
150 /* The tool-specific part */
151 SuppKind skind; // What kind of suppression. Must use the range (0..).
152 Char* string; // String -- use is optional. NULL by default.
153 void* extra; // Anything else -- use is optional. NULL by default.
154};
155
156SuppKind VG_(get_supp_kind) ( Supp* su )
157{
158 return su->skind;
159}
160
161Char* VG_(get_supp_string) ( Supp* su )
162{
163 return su->string;
164}
165
166void* VG_(get_supp_extra) ( Supp* su )
167{
168 return su->extra;
169}
170
171
172void VG_(set_supp_kind) ( Supp* su, SuppKind skind )
173{
174 su->skind = skind;
175}
176
177void VG_(set_supp_string) ( Supp* su, Char* string )
178{
179 su->string = string;
180}
181
182void VG_(set_supp_extra) ( Supp* su, void* extra )
183{
184 su->extra = extra;
185}
186
187
188/*------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +0000189/*--- Helper fns ---*/
190/*------------------------------------------------------------*/
191
sewardjde4a1d02002-03-22 01:27:54 +0000192/* Compare error contexts, to detect duplicates. Note that if they
193 are otherwise the same, the faulting addrs and associated rwoffsets
194 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +0000195static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +0000196{
njn810086f2002-11-14 12:42:47 +0000197 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +0000198 return False;
njn25e49d8e72002-09-23 09:36:25 +0000199 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +0000200 return False;
201
njn810086f2002-11-14 12:42:47 +0000202 switch (e1->ekind) {
sewardj4dced352002-06-04 22:54:20 +0000203 case PThreadErr:
njn25e49d8e72002-09-23 09:36:25 +0000204 vg_assert(VG_(needs).core_errors);
njn810086f2002-11-14 12:42:47 +0000205 if (e1->string == e2->string)
sewardj4dced352002-06-04 22:54:20 +0000206 return True;
njn810086f2002-11-14 12:42:47 +0000207 if (0 == VG_(strcmp)(e1->string, e2->string))
sewardj4dced352002-06-04 22:54:20 +0000208 return True;
209 return False;
sewardjde4a1d02002-03-22 01:27:54 +0000210 default:
njn25e49d8e72002-09-23 09:36:25 +0000211 if (VG_(needs).skin_errors)
njn810086f2002-11-14 12:42:47 +0000212 return SK_(eq_SkinError)(res, e1, e2);
njn25e49d8e72002-09-23 09:36:25 +0000213 else {
214 VG_(printf)("\nUnhandled error type: %u. VG_(needs).skin_errors\n"
215 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000216 e1->ekind);
njne427a662002-10-02 11:08:25 +0000217 VG_(skin_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000218 }
sewardjde4a1d02002-03-22 01:27:54 +0000219 }
220}
221
njn810086f2002-11-14 12:42:47 +0000222static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +0000223{
sewardjde4a1d02002-03-22 01:27:54 +0000224 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +0000225 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
226 if (err->tid > 1)
227 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
228
njn810086f2002-11-14 12:42:47 +0000229 switch (err->ekind) {
sewardj4dced352002-06-04 22:54:20 +0000230 case PThreadErr:
njn25e49d8e72002-09-23 09:36:25 +0000231 vg_assert(VG_(needs).core_errors);
njn810086f2002-11-14 12:42:47 +0000232 VG_(message)(Vg_UserMsg, "%s", err->string );
njn25e49d8e72002-09-23 09:36:25 +0000233 VG_(pp_ExeContext)(err->where);
sewardj4dced352002-06-04 22:54:20 +0000234 break;
sewardjde4a1d02002-03-22 01:27:54 +0000235 default:
njn25e49d8e72002-09-23 09:36:25 +0000236 if (VG_(needs).skin_errors)
njn43c799e2003-04-08 00:08:52 +0000237 SK_(pp_SkinError)( err );
njn25e49d8e72002-09-23 09:36:25 +0000238 else {
239 VG_(printf)("\nUnhandled error type: %u. VG_(needs).skin_errors\n"
240 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000241 err->ekind);
njne427a662002-10-02 11:08:25 +0000242 VG_(skin_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000243 }
sewardjde4a1d02002-03-22 01:27:54 +0000244 }
245}
246
nethercote04d0fbc2004-01-26 16:48:06 +0000247/* Figure out if we want to perform a given action for this error, possibly
sewardjde4a1d02002-03-22 01:27:54 +0000248 by asking the user. */
njn43c799e2003-04-08 00:08:52 +0000249Bool VG_(is_action_requested) ( Char* action, Bool* clo )
sewardjde4a1d02002-03-22 01:27:54 +0000250{
251 Char ch, ch2;
252 Int res;
253
njn43c799e2003-04-08 00:08:52 +0000254 if (*clo == False)
sewardjde4a1d02002-03-22 01:27:54 +0000255 return False;
256
257 VG_(message)(Vg_UserMsg, "");
258
259 again:
260 VG_(printf)(
261 "==%d== "
njn43c799e2003-04-08 00:08:52 +0000262 "---- %s ? --- [Return/N/n/Y/y/C/c] ---- ",
263 VG_(getpid)(), action
sewardjde4a1d02002-03-22 01:27:54 +0000264 );
265
sewardj6024b212003-07-13 10:54:33 +0000266 res = VG_(read)(VG_(clo_input_fd), &ch, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000267 if (res != 1) goto ioerror;
268 /* res == 1 */
269 if (ch == '\n') return False;
270 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
271 && ch != 'C' && ch != 'c') goto again;
272
sewardj6024b212003-07-13 10:54:33 +0000273 res = VG_(read)(VG_(clo_input_fd), &ch2, 1);
sewardjde4a1d02002-03-22 01:27:54 +0000274 if (res != 1) goto ioerror;
275 if (ch2 != '\n') goto again;
276
njn43c799e2003-04-08 00:08:52 +0000277 /* No, don't want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000278 if (ch == 'n' || ch == 'N') return False;
njn43c799e2003-04-08 00:08:52 +0000279 /* Yes, want to do action. */
sewardjde4a1d02002-03-22 01:27:54 +0000280 if (ch == 'y' || ch == 'Y') return True;
njn43c799e2003-04-08 00:08:52 +0000281 /* No, don't want to do action, and don't ask again either. */
sewardjde4a1d02002-03-22 01:27:54 +0000282 vg_assert(ch == 'c' || ch == 'C');
283
284 ioerror:
njn43c799e2003-04-08 00:08:52 +0000285 *clo = False;
sewardjde4a1d02002-03-22 01:27:54 +0000286 return False;
287}
288
289
njn25e49d8e72002-09-23 09:36:25 +0000290/* I've gone all object-oriented... initialisation depends on where the
291 error comes from:
292
293 - If from generated code (tst == NULL), the %EIP/%EBP values that we
njn3e884182003-04-15 13:03:23 +0000294 need in order to attach GDB are picked up out of VG_(baseBlock) rather
295 than from the thread table (vg_threads in vg_scheduler.c).
njn25e49d8e72002-09-23 09:36:25 +0000296
297 - If not from generated code but in response to requests passed back to
298 the scheduler (tst != NULL), we pick up %EIP/%EBP values from the
299 stored thread state, not from VG_(baseBlock).
300*/
301static __inline__
njn72718642003-07-24 08:45:32 +0000302void construct_error ( Error* err, ThreadId tid, ErrorKind ekind, Addr a,
303 Char* s, void* extra, ExeContext* where )
sewardjde4a1d02002-03-22 01:27:54 +0000304{
njn72718642003-07-24 08:45:32 +0000305 sk_assert(tid < VG_N_THREADS);
306
njn810086f2002-11-14 12:42:47 +0000307 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000308 err->next = NULL;
309 err->supp = NULL;
310 err->count = 1;
njn72718642003-07-24 08:45:32 +0000311 err->tid = tid;
njn43c799e2003-04-08 00:08:52 +0000312 if (NULL == where)
njn72718642003-07-24 08:45:32 +0000313 err->where = VG_(get_ExeContext)( tid );
njn43c799e2003-04-08 00:08:52 +0000314 else
315 err->where = where;
njn1d6c4bc2002-11-21 13:38:08 +0000316
njn810086f2002-11-14 12:42:47 +0000317 /* Skin-relevant parts */
318 err->ekind = ekind;
319 err->addr = a;
njn810086f2002-11-14 12:42:47 +0000320 err->extra = extra;
sewardja6022612003-07-24 23:50:17 +0000321 err->string = s;
322
njn25e49d8e72002-09-23 09:36:25 +0000323 /* sanity... */
njn72718642003-07-24 08:45:32 +0000324 vg_assert( tid < VG_N_THREADS );
njn25e49d8e72002-09-23 09:36:25 +0000325}
326
nethercote10d481a2004-01-25 20:33:53 +0000327static void gen_suppression(Error* err)
njn43c799e2003-04-08 00:08:52 +0000328{
sewardj05bcdcb2003-05-18 10:05:38 +0000329 Int i;
njn43c799e2003-04-08 00:08:52 +0000330 UChar buf[M_VG_ERRTXT];
nethercote77eba602003-11-13 17:35:04 +0000331 Bool main_done = False;
njn43c799e2003-04-08 00:08:52 +0000332 ExeContext* ec = VG_(get_error_where)(err);
333 Int stop_at = VG_(clo_backtrace_size);
njn43c799e2003-04-08 00:08:52 +0000334
njn633de322003-05-12 20:40:13 +0000335 if (stop_at > 4) stop_at = 4; /* At most four names */
njn43c799e2003-04-08 00:08:52 +0000336 vg_assert(stop_at > 0);
337
338 VG_(printf)("{\n");
339 VG_(printf)(" <insert a suppression name here>\n");
njn6a230532003-07-21 10:38:23 +0000340
341 if (PThreadErr == err->ekind) {
342 VG_(printf)(" core:PThread\n");
343
344 } else {
345 Char* name = SK_(get_error_name)(err);
346 if (NULL == name) {
347 VG_(message)(Vg_UserMsg,
nethercote137bc552003-11-14 17:47:54 +0000348 "(tool does not allow error to be suppressed)");
njn6a230532003-07-21 10:38:23 +0000349 return;
350 }
351 VG_(printf)(" %s:%s\n", VG_(details).name, name);
352 SK_(print_extra_suppression_info)(err);
353 }
njn43c799e2003-04-08 00:08:52 +0000354
355 /* This loop condensed from VG_(mini_stack_dump)() */
356 i = 0;
357 do {
358 Addr eip = ec->eips[i];
nethercote77eba602003-11-13 17:35:04 +0000359 if (i > 0) eip--; /* point to calling line */
njn43c799e2003-04-08 00:08:52 +0000360 if ( VG_(get_fnname_nodemangle) (eip, buf, M_VG_ERRTXT) ) {
nethercote77eba602003-11-13 17:35:04 +0000361 // Stop after "main"; if main() is recursive, stop after last main().
362
363 if ( ! VG_(clo_show_below_main)) {
364 if (VG_STREQ(buf, "main"))
365 main_done = True;
366 else if (main_done)
367 break;
368 }
njn43c799e2003-04-08 00:08:52 +0000369 VG_(printf)(" fun:%s\n", buf);
370 } else if ( VG_(get_objname)(eip, buf, M_VG_ERRTXT) ) {
371 VG_(printf)(" obj:%s\n", buf);
372 } else {
373 VG_(printf)(" ???:??? "
nethercote125c91e2004-06-27 12:38:17 +0000374 "# unknown, suppression will not work, sorry\n");
njn43c799e2003-04-08 00:08:52 +0000375 }
376 i++;
377 } while (i < stop_at && ec->eips[i] != 0);
378
379 VG_(printf)("}\n");
380}
381
njnb4aee052003-04-15 14:09:58 +0000382static
nethercote04d0fbc2004-01-26 16:48:06 +0000383void do_actions_on_error(Error* err, Bool allow_db_attach)
njn43c799e2003-04-08 00:08:52 +0000384{
nethercote04d0fbc2004-01-26 16:48:06 +0000385 /* Perhaps we want a debugger attach at this point? */
386 if (allow_db_attach &&
387 VG_(is_action_requested)( "Attach to debugger", & VG_(clo_db_attach) ))
njn3e884182003-04-15 13:03:23 +0000388 {
nethercote04d0fbc2004-01-26 16:48:06 +0000389 VG_(printf)("starting debugger\n");
390 VG_(start_debugger)( err->tid );
njn43c799e2003-04-08 00:08:52 +0000391 }
392 /* Or maybe we want to generate the error's suppression? */
393 if (VG_(is_action_requested)( "Print suppression",
394 & VG_(clo_gen_suppressions) )) {
nethercote42602b12004-01-25 19:30:29 +0000395 gen_suppression(err);
njn43c799e2003-04-08 00:08:52 +0000396 }
397}
398
399/* Shared between VG_(maybe_record_error)() and VG_(unique_error)(),
400 just for pretty printing purposes. */
401static Bool is_first_shown_context = True;
402
njn25e49d8e72002-09-23 09:36:25 +0000403/* Top-level entry point to the error management subsystem.
404 All detected errors are notified here; this routine decides if/when the
405 user should see the error. */
njn72718642003-07-24 08:45:32 +0000406void VG_(maybe_record_error) ( ThreadId tid,
njn25e49d8e72002-09-23 09:36:25 +0000407 ErrorKind ekind, Addr a, Char* s, void* extra )
408{
njn810086f2002-11-14 12:42:47 +0000409 Error err;
410 Error* p;
411 Error* p_prev;
njn43c799e2003-04-08 00:08:52 +0000412 UInt extra_size;
njn810086f2002-11-14 12:42:47 +0000413 VgRes exe_res = Vg_MedRes;
njn810086f2002-11-14 12:42:47 +0000414 static Bool stopping_message = False;
415 static Bool slowdown_message = False;
416 static Int vg_n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000417
sewardjf2537be2002-04-24 21:03:47 +0000418 /* After M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
419 been found, or M_VG_COLLECT_NO_ERRORS_AFTER_FOUND total errors
420 have been found, just refuse to collect any more. This stops
421 the burden of the error-management system becoming excessive in
422 extremely buggy programs, although it does make it pretty
423 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000424 if (VG_(clo_error_limit)
sewardj72f98ff2002-06-13 17:23:38 +0000425 && (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN
njn47363ab2003-04-21 13:24:40 +0000426 || VG_(n_errs_found) >= M_VG_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000427 if (!stopping_message) {
428 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000429
430 if (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN) {
431 VG_(message)(Vg_UserMsg,
432 "More than %d different errors detected. "
433 "I'm not reporting any more.",
434 M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN );
435 } else {
436 VG_(message)(Vg_UserMsg,
437 "More than %d total errors detected. "
438 "I'm not reporting any more.",
439 M_VG_COLLECT_NO_ERRORS_AFTER_FOUND );
440 }
441
sewardjde4a1d02002-03-22 01:27:54 +0000442 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000443 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000444 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000445 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000446 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000447 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000448 VG_(message)(Vg_UserMsg,
449 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000450 VG_(message)(Vg_UserMsg, "");
451 stopping_message = True;
452 }
453 return;
454 }
455
456 /* After M_VG_COLLECT_ERRORS_SLOWLY_AFTER different errors have
457 been found, be much more conservative about collecting new
458 ones. */
459 if (vg_n_errs_shown >= M_VG_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000460 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000461 if (!slowdown_message) {
462 VG_(message)(Vg_UserMsg, "");
463 VG_(message)(Vg_UserMsg,
464 "More than %d errors detected. Subsequent errors",
465 M_VG_COLLECT_ERRORS_SLOWLY_AFTER);
466 VG_(message)(Vg_UserMsg,
467 "will still be recorded, but in less detail than before.");
468 slowdown_message = True;
469 }
470 }
471
njn25e49d8e72002-09-23 09:36:25 +0000472 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000473 construct_error ( &err, tid, ekind, a, s, extra, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000474
475 /* First, see if we've got an error record matching this one. */
njn25e49d8e72002-09-23 09:36:25 +0000476 p = vg_errors;
sewardjde4a1d02002-03-22 01:27:54 +0000477 p_prev = NULL;
478 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000479 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000480 /* Found it. */
481 p->count++;
482 if (p->supp != NULL) {
483 /* Deal correctly with suppressed errors. */
484 p->supp->count++;
485 vg_n_errs_suppressed++;
486 } else {
njn47363ab2003-04-21 13:24:40 +0000487 VG_(n_errs_found)++;
sewardjde4a1d02002-03-22 01:27:54 +0000488 }
489
490 /* Move p to the front of the list so that future searches
491 for it are faster. */
492 if (p_prev != NULL) {
493 vg_assert(p_prev->next == p);
494 p_prev->next = p->next;
njn25e49d8e72002-09-23 09:36:25 +0000495 p->next = vg_errors;
496 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000497 }
sewardj7ebf7c32003-07-24 21:29:40 +0000498
sewardjde4a1d02002-03-22 01:27:54 +0000499 return;
500 }
501 p_prev = p;
502 p = p->next;
503 }
504
505 /* Didn't see it. Copy and add. */
506
njn43c799e2003-04-08 00:08:52 +0000507 /* OK, we're really going to collect it. The context is on the stack and
508 will disappear shortly, so we must copy it. First do the main
509 (non-`extra') part.
njn25e49d8e72002-09-23 09:36:25 +0000510
njn43c799e2003-04-08 00:08:52 +0000511 Then SK_(update_extra) can update the `extra' part. This is for when
512 there are more details to fill in which take time to work out but
513 don't affect our earlier decision to include the error -- by
njn25e49d8e72002-09-23 09:36:25 +0000514 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000515 case where we ignore the error. Ugly.
njn43c799e2003-04-08 00:08:52 +0000516
517 Then, if there is an `extra' part, copy it too, using the size that
njna70114c2003-08-19 16:14:42 +0000518 SK_(update_extra) returned. Also allow for people using the void*
519 extra field for a scalar value like an integer.
njn43c799e2003-04-08 00:08:52 +0000520 */
521
522 /* copy main part */
njn810086f2002-11-14 12:42:47 +0000523 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000524 *p = err;
njn43c799e2003-04-08 00:08:52 +0000525
njn6a230532003-07-21 10:38:23 +0000526 /* update `extra', for non-core errors (core ones don't use 'extra') */
njna70114c2003-08-19 16:14:42 +0000527 if (VG_(needs).skin_errors && PThreadErr != ekind) {
njn6a230532003-07-21 10:38:23 +0000528 extra_size = SK_(update_extra)(p);
njn43c799e2003-04-08 00:08:52 +0000529
njna70114c2003-08-19 16:14:42 +0000530 /* copy block pointed to by `extra', if there is one */
531 if (NULL != p->extra && 0 != extra_size) {
njn6a230532003-07-21 10:38:23 +0000532 void* new_extra = VG_(malloc)(extra_size);
533 VG_(memcpy)(new_extra, p->extra, extra_size);
534 p->extra = new_extra;
535 }
njn43c799e2003-04-08 00:08:52 +0000536 }
537
njn25e49d8e72002-09-23 09:36:25 +0000538 p->next = vg_errors;
539 p->supp = is_suppressible_error(&err);
540 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000541 if (p->supp == NULL) {
njn47363ab2003-04-21 13:24:40 +0000542 VG_(n_errs_found)++;
sewardjde4a1d02002-03-22 01:27:54 +0000543 if (!is_first_shown_context)
544 VG_(message)(Vg_UserMsg, "");
njn43c799e2003-04-08 00:08:52 +0000545 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000546 is_first_shown_context = False;
547 vg_n_errs_shown++;
nethercote04d0fbc2004-01-26 16:48:06 +0000548 do_actions_on_error(p, /*allow_db_attach*/True);
sewardjde4a1d02002-03-22 01:27:54 +0000549 } else {
550 vg_n_errs_suppressed++;
551 p->supp->count++;
552 }
553}
554
njn43c799e2003-04-08 00:08:52 +0000555/* Second top-level entry point to the error management subsystem, for
nethercote7cc9c232004-01-21 15:08:04 +0000556 errors that the tool wants to report immediately, eg. because they're
njn43c799e2003-04-08 00:08:52 +0000557 guaranteed to only happen once. This avoids all the recording and
558 comparing stuff. But they can be suppressed; returns True if it is
njn47363ab2003-04-21 13:24:40 +0000559 suppressed. Bool `print_error' dictates whether to print the error.
560 Bool `count_error' dictates whether to count the error in VG_(n_errs_found)
561*/
njn72718642003-07-24 08:45:32 +0000562Bool VG_(unique_error) ( ThreadId tid, ErrorKind ekind, Addr a, Char* s,
njn3e884182003-04-15 13:03:23 +0000563 void* extra, ExeContext* where, Bool print_error,
nethercote04d0fbc2004-01-26 16:48:06 +0000564 Bool allow_db_attach, Bool count_error )
njn43c799e2003-04-08 00:08:52 +0000565{
566 Error err;
567
568 /* Build ourselves the error */
njn72718642003-07-24 08:45:32 +0000569 construct_error ( &err, tid, ekind, a, s, extra, where );
njn43c799e2003-04-08 00:08:52 +0000570
571 /* Unless it's suppressed, we're going to show it. Don't need to make
572 a copy, because it's only temporary anyway.
573
574 Then update the `extra' part with SK_(update_extra), because that can
575 have an affect on whether it's suppressed. Ignore the size return
576 value of SK_(update_extra), because we're not copying `extra'. */
577 (void)SK_(update_extra)(&err);
578
579 if (NULL == is_suppressible_error(&err)) {
njn47363ab2003-04-21 13:24:40 +0000580 if (count_error)
581 VG_(n_errs_found)++;
njn43c799e2003-04-08 00:08:52 +0000582
583 if (print_error) {
584 if (!is_first_shown_context)
585 VG_(message)(Vg_UserMsg, "");
586 pp_Error(&err, False);
587 is_first_shown_context = False;
588 }
nethercote04d0fbc2004-01-26 16:48:06 +0000589 do_actions_on_error(&err, allow_db_attach);
njn43c799e2003-04-08 00:08:52 +0000590
591 return False;
592
593 } else {
594 vg_n_errs_suppressed++;
595 return True;
596 }
597}
598
sewardjde4a1d02002-03-22 01:27:54 +0000599
sewardjde4a1d02002-03-22 01:27:54 +0000600/*------------------------------------------------------------*/
601/*--- Exported fns ---*/
602/*------------------------------------------------------------*/
603
njn25e49d8e72002-09-23 09:36:25 +0000604/* These are called not from generated code but from the scheduler */
sewardj8c824512002-04-14 04:16:48 +0000605
njn25e49d8e72002-09-23 09:36:25 +0000606void VG_(record_pthread_error) ( ThreadId tid, Char* msg )
sewardjde4a1d02002-03-22 01:27:54 +0000607{
njn25e49d8e72002-09-23 09:36:25 +0000608 if (! VG_(needs).core_errors) return;
njn72718642003-07-24 08:45:32 +0000609 VG_(maybe_record_error)( tid, PThreadErr, /*addr*/0, msg, /*extra*/NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000610}
611
sewardj8c824512002-04-14 04:16:48 +0000612/*------------------------------*/
613
sewardjde4a1d02002-03-22 01:27:54 +0000614void VG_(show_all_errors) ( void )
615{
njn810086f2002-11-14 12:42:47 +0000616 Int i, n_min;
617 Int n_err_contexts, n_supp_contexts;
618 Error *p, *p_min;
619 Supp *su;
620 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000621
622 if (VG_(clo_verbosity) == 0)
623 return;
624
625 n_err_contexts = 0;
njn25e49d8e72002-09-23 09:36:25 +0000626 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000627 if (p->supp == NULL)
628 n_err_contexts++;
629 }
630
631 n_supp_contexts = 0;
632 for (su = vg_suppressions; su != NULL; su = su->next) {
633 if (su->count > 0)
634 n_supp_contexts++;
635 }
sewardjde4a1d02002-03-22 01:27:54 +0000636 VG_(message)(Vg_UserMsg,
637 "ERROR SUMMARY: "
638 "%d errors from %d contexts (suppressed: %d from %d)",
njn47363ab2003-04-21 13:24:40 +0000639 VG_(n_errs_found), n_err_contexts,
sewardjde4a1d02002-03-22 01:27:54 +0000640 vg_n_errs_suppressed, n_supp_contexts );
641
642 if (VG_(clo_verbosity) <= 1)
643 return;
644
645 /* Print the contexts in order of increasing error count. */
646 for (i = 0; i < n_err_contexts; i++) {
647 n_min = (1 << 30) - 1;
648 p_min = NULL;
njn25e49d8e72002-09-23 09:36:25 +0000649 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000650 if (p->supp != NULL) continue;
651 if (p->count < n_min) {
652 n_min = p->count;
653 p_min = p;
654 }
655 }
njne427a662002-10-02 11:08:25 +0000656 if (p_min == NULL) VG_(skin_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000657
658 VG_(message)(Vg_UserMsg, "");
659 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
660 p_min->count,
661 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000662 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000663
664 if ((i+1 == VG_(clo_dump_error))) {
sewardj1e8cdc92002-04-18 11:37:52 +0000665 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to below NULLs */,
sewardj22854b92002-11-30 14:00:47 +0000666 p_min->where->eips[0], NULL, NULL, NULL, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000667 }
668
669 p_min->count = 1 << 30;
670 }
671
672 if (n_supp_contexts > 0)
673 VG_(message)(Vg_DebugMsg, "");
674 any_supp = False;
675 for (su = vg_suppressions; su != NULL; su = su->next) {
676 if (su->count > 0) {
677 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000678 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000679 }
680 }
681
682 if (n_err_contexts > 0) {
683 if (any_supp)
684 VG_(message)(Vg_UserMsg, "");
685 VG_(message)(Vg_UserMsg,
686 "IN SUMMARY: "
687 "%d errors from %d contexts (suppressed: %d from %d)",
njn47363ab2003-04-21 13:24:40 +0000688 VG_(n_errs_found), n_err_contexts,
sewardjde4a1d02002-03-22 01:27:54 +0000689 vg_n_errs_suppressed,
690 n_supp_contexts );
691 VG_(message)(Vg_UserMsg, "");
692 }
693}
694
695/*------------------------------------------------------------*/
696/*--- Standard suppressions ---*/
697/*------------------------------------------------------------*/
698
699/* Get a non-blank, non-comment line of at most nBuf chars from fd.
700 Skips leading spaces on the line. Return True if EOF was hit instead.
701*/
702
703#define VG_ISSPACE(ch) (((ch)==' ') || ((ch)=='\n') || ((ch)=='\t'))
704
njn4ba5a792002-09-30 10:23:54 +0000705Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000706{
707 Char ch;
708 Int n, i;
709 while (True) {
710 /* First, read until a non-blank char appears. */
711 while (True) {
712 n = VG_(read)(fd, &ch, 1);
713 if (n == 1 && !VG_ISSPACE(ch)) break;
714 if (n == 0) return True;
715 }
716
717 /* Now, read the line into buf. */
718 i = 0;
719 buf[i++] = ch; buf[i] = 0;
720 while (True) {
721 n = VG_(read)(fd, &ch, 1);
722 if (n == 0) return False; /* the next call will return True */
723 if (ch == '\n') break;
724 if (i > 0 && i == nBuf-1) i--;
725 buf[i++] = ch; buf[i] = 0;
726 }
727 while (i > 1 && VG_ISSPACE(buf[i-1])) {
728 i--; buf[i] = 0;
729 };
730
731 /* VG_(printf)("The line is `%s'\n", buf); */
732 /* Ok, we have a line. If a non-comment line, return.
733 If a comment line, start all over again. */
734 if (buf[0] != '#') return False;
735 }
736}
737
738
739/* *p_caller contains the raw name of a caller, supposedly either
740 fun:some_function_name or
741 obj:some_object_name.
742 Set *p_ty accordingly and advance *p_caller over the descriptor
743 (fun: or obj:) part.
744 Returns False if failed.
745*/
njn25e49d8e72002-09-23 09:36:25 +0000746static Bool setLocationTy ( Char** p_caller, SuppLocTy* p_ty )
sewardjde4a1d02002-03-22 01:27:54 +0000747{
748 if (VG_(strncmp)(*p_caller, "fun:", 4) == 0) {
749 (*p_caller) += 4;
750 *p_ty = FunName;
751 return True;
752 }
753 if (VG_(strncmp)(*p_caller, "obj:", 4) == 0) {
754 (*p_caller) += 4;
755 *p_ty = ObjName;
756 return True;
757 }
758 VG_(printf)("location should start with fun: or obj:\n");
759 return False;
760}
761
762
nethercote7cc9c232004-01-21 15:08:04 +0000763/* Look for "tool" in a string like "tool1,tool2,tool3" */
njn11cc9252002-10-07 14:42:59 +0000764static __inline__
nethercote7cc9c232004-01-21 15:08:04 +0000765Bool tool_name_present(Char *name, Char *names)
njn11cc9252002-10-07 14:42:59 +0000766{
767 Bool found;
768 Char *s = NULL; /* Shut gcc up */
769 Int len = VG_(strlen)(name);
770
771 found = (NULL != (s = VG_(strstr)(names, name)) &&
772 (s == names || *(s-1) == ',') &&
773 (*(s+len) == ',' || *(s+len) == '\0')
774 );
775
776 return found;
777}
778
sewardjde4a1d02002-03-22 01:27:54 +0000779/* Read suppressions from the file specified in vg_clo_suppressions
780 and place them in the suppressions list. If there's any difficulty
781 doing this, just give up -- there's no point in trying to recover.
782*/
sewardjde4a1d02002-03-22 01:27:54 +0000783static void load_one_suppressions_file ( Char* filename )
784{
785# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000786 Int fd, i;
787 Bool eof;
788 Char buf[N_BUF+1];
nethercote7cc9c232004-01-21 15:08:04 +0000789 Char* tool_names;
njnc40c3a82002-10-02 11:02:27 +0000790 Char* supp_name;
791
njn25e49d8e72002-09-23 09:36:25 +0000792 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
jsgff3c3f1a2003-10-14 22:13:28 +0000793 if (fd < 0) {
njn25e49d8e72002-09-23 09:36:25 +0000794 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file `%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000795 filename );
796 VG_(exit)(1);
797 }
798
799 while (True) {
nethercote7cc9c232004-01-21 15:08:04 +0000800 /* Assign and initialise the two suppression halves (core and tool) */
njn810086f2002-11-14 12:42:47 +0000801 Supp* supp;
802 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000803 supp->count = 0;
njn25e49d8e72002-09-23 09:36:25 +0000804 for (i = 0; i < VG_N_SUPP_CALLERS; i++) supp->caller[i] = NULL;
njn810086f2002-11-14 12:42:47 +0000805 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000806
njn4ba5a792002-09-30 10:23:54 +0000807 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000808 if (eof) break;
809
njn43c799e2003-04-08 00:08:52 +0000810 if (!VG_STREQ(buf, "{")) goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000811
njn4ba5a792002-09-30 10:23:54 +0000812 eof = VG_(get_line) ( fd, buf, N_BUF );
njn43c799e2003-04-08 00:08:52 +0000813 if (eof || VG_STREQ(buf, "}")) goto syntax_error;
njn25e49d8e72002-09-23 09:36:25 +0000814 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000815
njn4ba5a792002-09-30 10:23:54 +0000816 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000817
sewardjde4a1d02002-03-22 01:27:54 +0000818 if (eof) goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000819
njn11cc9252002-10-07 14:42:59 +0000820 /* Check it has the "skin1,skin2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000821 i = 0;
822 while (True) {
823 if (buf[i] == ':') break;
824 if (buf[i] == '\0') goto syntax_error;
825 i++;
njn25e49d8e72002-09-23 09:36:25 +0000826 }
njnc40c3a82002-10-02 11:02:27 +0000827 buf[i] = '\0'; /* Replace ':', splitting into two strings */
828
nethercote7cc9c232004-01-21 15:08:04 +0000829 tool_names = & buf[0];
njn11cc9252002-10-07 14:42:59 +0000830 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000831
njn11cc9252002-10-07 14:42:59 +0000832 /* Is it a core suppression? */
nethercote7cc9c232004-01-21 15:08:04 +0000833 if (VG_(needs).core_errors && tool_name_present("core", tool_names))
njnc40c3a82002-10-02 11:02:27 +0000834 {
njn43c799e2003-04-08 00:08:52 +0000835 if (VG_STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000836 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000837 else
838 goto syntax_error;
839 }
840
nethercote7cc9c232004-01-21 15:08:04 +0000841 /* Is it a tool suppression? */
njn11cc9252002-10-07 14:42:59 +0000842 else if (VG_(needs).skin_errors &&
nethercote7cc9c232004-01-21 15:08:04 +0000843 tool_name_present(VG_(details).name, tool_names))
njnc40c3a82002-10-02 11:02:27 +0000844 {
njn810086f2002-11-14 12:42:47 +0000845 if (SK_(recognised_suppression)(supp_name, supp))
njnc40c3a82002-10-02 11:02:27 +0000846 {
njn810086f2002-11-14 12:42:47 +0000847 /* Do nothing, function fills in supp->skind */
njnc40c3a82002-10-02 11:02:27 +0000848 } else
849 goto syntax_error;
850 }
851
njn25e49d8e72002-09-23 09:36:25 +0000852 else {
njnc40c3a82002-10-02 11:02:27 +0000853 /* Ignore rest of suppression */
njn25e49d8e72002-09-23 09:36:25 +0000854 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000855 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000856 if (eof) goto syntax_error;
njn43c799e2003-04-08 00:08:52 +0000857 if (VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000858 break;
859 }
860 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000861 }
862
njn25e49d8e72002-09-23 09:36:25 +0000863 if (VG_(needs).skin_errors &&
njn810086f2002-11-14 12:42:47 +0000864 !SK_(read_extra_suppression_info)(fd, buf, N_BUF, supp))
sewardjde4a1d02002-03-22 01:27:54 +0000865 goto syntax_error;
866
njn25e49d8e72002-09-23 09:36:25 +0000867 /* "i > 0" ensures at least one caller read. */
njn633de322003-05-12 20:40:13 +0000868 for (i = 0; i <= VG_N_SUPP_CALLERS; i++) {
njn4ba5a792002-09-30 10:23:54 +0000869 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000870 if (eof) goto syntax_error;
njn43c799e2003-04-08 00:08:52 +0000871 if (i > 0 && VG_STREQ(buf, "}"))
njn25e49d8e72002-09-23 09:36:25 +0000872 break;
njn633de322003-05-12 20:40:13 +0000873 if (i == VG_N_SUPP_CALLERS)
874 break;
njn25e49d8e72002-09-23 09:36:25 +0000875 supp->caller[i] = VG_(arena_strdup)(VG_AR_CORE, buf);
876 if (!setLocationTy(&(supp->caller[i]), &(supp->caller_ty[i])))
877 goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000878 }
879
sewardj57a8f5f2003-07-06 01:40:11 +0000880 /* make sure to grab the '}' if the num callers is >=
881 VG_N_SUPP_CALLERS */
882 if (!VG_STREQ(buf, "}")) {
883 do {
884 eof = VG_(get_line) ( fd, buf, N_BUF );
885 } while (!eof && !VG_STREQ(buf, "}"));
886 }
887
sewardjde4a1d02002-03-22 01:27:54 +0000888 supp->next = vg_suppressions;
889 vg_suppressions = supp;
890 }
sewardjde4a1d02002-03-22 01:27:54 +0000891 VG_(close)(fd);
892 return;
893
894 syntax_error:
895 if (eof) {
896 VG_(message)(Vg_UserMsg,
897 "FATAL: in suppressions file `%s': unexpected EOF",
898 filename );
899 } else {
900 VG_(message)(Vg_UserMsg,
njn11cc9252002-10-07 14:42:59 +0000901 "FATAL: in suppressions file: `%s': syntax error on: %s",
sewardjde4a1d02002-03-22 01:27:54 +0000902 filename, buf );
903 }
904 VG_(close)(fd);
905 VG_(message)(Vg_UserMsg, "exiting now.");
906 VG_(exit)(1);
907
908# undef N_BUF
909}
910
911
912void VG_(load_suppressions) ( void )
913{
914 Int i;
915 vg_suppressions = NULL;
916 for (i = 0; i < VG_(clo_n_suppressions); i++) {
917 if (VG_(clo_verbosity) > 1) {
918 VG_(message)(Vg_UserMsg, "Reading suppressions file: %s",
919 VG_(clo_suppressions)[i] );
920 }
921 load_one_suppressions_file( VG_(clo_suppressions)[i] );
922 }
923}
924
njn25e49d8e72002-09-23 09:36:25 +0000925/* Return the name of an erring fn in a way which is useful
926 for comparing against the contents of a suppressions file.
927 Doesn't demangle the fn name, because we want to refer to
928 mangled names in the suppressions file.
sewardj99aac972002-12-26 01:53:45 +0000929*/
njn43c799e2003-04-08 00:08:52 +0000930static void get_objname_fnname ( Addr a, Char* obj_buf, Int n_obj_buf,
931 Char* fun_buf, Int n_fun_buf )
njn25e49d8e72002-09-23 09:36:25 +0000932{
933 (void)VG_(get_objname) ( a, obj_buf, n_obj_buf );
934 (void)VG_(get_fnname_nodemangle)( a, fun_buf, n_fun_buf );
935}
936
937static __inline__
njn810086f2002-11-14 12:42:47 +0000938Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000939{
njn810086f2002-11-14 12:42:47 +0000940 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000941 case PThreadSupp:
njn810086f2002-11-14 12:42:47 +0000942 return (err->ekind == PThreadErr);
njn25e49d8e72002-09-23 09:36:25 +0000943 default:
944 if (VG_(needs).skin_errors) {
njn810086f2002-11-14 12:42:47 +0000945 return SK_(error_matches_suppression)(err, su);
njn25e49d8e72002-09-23 09:36:25 +0000946 } else {
947 VG_(printf)(
948 "\nUnhandled suppression type: %u. VG_(needs).skin_errors\n"
949 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000950 err->ekind);
njne427a662002-10-02 11:08:25 +0000951 VG_(skin_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000952 }
953 }
954}
955
956static __inline__
njn810086f2002-11-14 12:42:47 +0000957Bool supp_matches_callers(Supp* su, Char caller_obj[][M_VG_ERRTXT],
958 Char caller_fun[][M_VG_ERRTXT])
njn25e49d8e72002-09-23 09:36:25 +0000959{
960 Int i;
961
njn633de322003-05-12 20:40:13 +0000962 for (i = 0; i < VG_N_SUPP_CALLERS && su->caller[i] != NULL; i++) {
njn25e49d8e72002-09-23 09:36:25 +0000963 switch (su->caller_ty[i]) {
njn4ba5a792002-09-30 10:23:54 +0000964 case ObjName: if (VG_(string_match)(su->caller[i],
965 caller_obj[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000966 return False;
njn4ba5a792002-09-30 10:23:54 +0000967 case FunName: if (VG_(string_match)(su->caller[i],
968 caller_fun[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000969 return False;
njn43c799e2003-04-08 00:08:52 +0000970 default: VG_(skin_panic)("supp_matches_callers");
njn25e49d8e72002-09-23 09:36:25 +0000971 }
972 }
973
974 /* If we reach here, it's a match */
975 return True;
976}
sewardjde4a1d02002-03-22 01:27:54 +0000977
njn810086f2002-11-14 12:42:47 +0000978/* Does an error context match a suppression? ie is this a suppressible
979 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +0000980 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +0000981*/
njn810086f2002-11-14 12:42:47 +0000982static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +0000983{
njn25e49d8e72002-09-23 09:36:25 +0000984 Int i;
sewardjde4a1d02002-03-22 01:27:54 +0000985
njn25e49d8e72002-09-23 09:36:25 +0000986 Char caller_obj[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
987 Char caller_fun[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
sewardjde4a1d02002-03-22 01:27:54 +0000988
njn810086f2002-11-14 12:42:47 +0000989 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +0000990
njn25e49d8e72002-09-23 09:36:25 +0000991 /* get_objname_fnname() writes the function name and object name if
njn43c799e2003-04-08 00:08:52 +0000992 it finds them in the debug info. So the strings in the suppression
njn25e49d8e72002-09-23 09:36:25 +0000993 file should match these.
sewardjde4a1d02002-03-22 01:27:54 +0000994 */
995
996 /* Initialise these strs so they are always safe to compare, even
njn25e49d8e72002-09-23 09:36:25 +0000997 if get_objname_fnname doesn't write anything to them. */
998 for (i = 0; i < VG_N_SUPP_CALLERS; i++)
999 caller_obj[i][0] = caller_fun[i][0] = 0;
sewardjde4a1d02002-03-22 01:27:54 +00001000
njn25e49d8e72002-09-23 09:36:25 +00001001 for (i = 0; i < VG_N_SUPP_CALLERS && i < VG_(clo_backtrace_size); i++) {
njn43c799e2003-04-08 00:08:52 +00001002 get_objname_fnname ( err->where->eips[i], caller_obj[i], M_VG_ERRTXT,
1003 caller_fun[i], M_VG_ERRTXT );
sewardjde4a1d02002-03-22 01:27:54 +00001004 }
1005
1006 /* See if the error context matches any suppression. */
1007 for (su = vg_suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +00001008 if (supp_matches_error(su, err) &&
1009 supp_matches_callers(su, caller_obj, caller_fun)) {
1010 return su;
sewardjde4a1d02002-03-22 01:27:54 +00001011 }
sewardjde4a1d02002-03-22 01:27:54 +00001012 }
njn25e49d8e72002-09-23 09:36:25 +00001013 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +00001014}
1015
sewardjde4a1d02002-03-22 01:27:54 +00001016/*--------------------------------------------------------------------*/
1017/*--- end vg_errcontext.c ---*/
1018/*--------------------------------------------------------------------*/