blob: 6ba7343a9115c77bdcecd2cd16b29497b20014e4 [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
10 Copyright (C) 2000-2002 Julian Seward
11 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
sewardj99aac972002-12-26 01:53:45 +000045/* And a helper function so the leak detector can get hold of it. */
46Supp* VG_(get_suppressions) ( void ) { return vg_suppressions; }
47
sewardjde4a1d02002-03-22 01:27:54 +000048/* Running count of unsuppressed errors detected. */
49static UInt vg_n_errs_found = 0;
50
51/* Running count of suppressed errors detected. */
52static UInt vg_n_errs_suppressed = 0;
53
54/* forwards ... */
njn810086f2002-11-14 12:42:47 +000055static Supp* is_suppressible_error ( Error* err );
sewardjde4a1d02002-03-22 01:27:54 +000056
57
58/*------------------------------------------------------------*/
59/*--- Helper fns ---*/
60/*------------------------------------------------------------*/
61
sewardjde4a1d02002-03-22 01:27:54 +000062/* Compare error contexts, to detect duplicates. Note that if they
63 are otherwise the same, the faulting addrs and associated rwoffsets
64 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +000065static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +000066{
njn810086f2002-11-14 12:42:47 +000067 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +000068 return False;
njn25e49d8e72002-09-23 09:36:25 +000069 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +000070 return False;
71
njn810086f2002-11-14 12:42:47 +000072 switch (e1->ekind) {
sewardj4dced352002-06-04 22:54:20 +000073 case PThreadErr:
njn25e49d8e72002-09-23 09:36:25 +000074 vg_assert(VG_(needs).core_errors);
njn810086f2002-11-14 12:42:47 +000075 if (e1->string == e2->string)
sewardj4dced352002-06-04 22:54:20 +000076 return True;
njn810086f2002-11-14 12:42:47 +000077 if (0 == VG_(strcmp)(e1->string, e2->string))
sewardj4dced352002-06-04 22:54:20 +000078 return True;
79 return False;
sewardjde4a1d02002-03-22 01:27:54 +000080 default:
njn25e49d8e72002-09-23 09:36:25 +000081 if (VG_(needs).skin_errors)
njn810086f2002-11-14 12:42:47 +000082 return SK_(eq_SkinError)(res, e1, e2);
njn25e49d8e72002-09-23 09:36:25 +000083 else {
84 VG_(printf)("\nUnhandled error type: %u. VG_(needs).skin_errors\n"
85 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +000086 e1->ekind);
njne427a662002-10-02 11:08:25 +000087 VG_(skin_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +000088 }
sewardjde4a1d02002-03-22 01:27:54 +000089 }
90}
91
njn810086f2002-11-14 12:42:47 +000092static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +000093{
njn25e49d8e72002-09-23 09:36:25 +000094 /* Closure for printing where the error occurred. Abstracts details
95 about the `where' field away from the skin. */
96 void pp_ExeContextClosure(void)
97 {
98 VG_(pp_ExeContext) ( err->where );
sewardjde4a1d02002-03-22 01:27:54 +000099 }
njn25e49d8e72002-09-23 09:36:25 +0000100
sewardjde4a1d02002-03-22 01:27:54 +0000101 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +0000102 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
103 if (err->tid > 1)
104 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
105
njn810086f2002-11-14 12:42:47 +0000106 switch (err->ekind) {
sewardj4dced352002-06-04 22:54:20 +0000107 case PThreadErr:
njn25e49d8e72002-09-23 09:36:25 +0000108 vg_assert(VG_(needs).core_errors);
njn810086f2002-11-14 12:42:47 +0000109 VG_(message)(Vg_UserMsg, "%s", err->string );
njn25e49d8e72002-09-23 09:36:25 +0000110 VG_(pp_ExeContext)(err->where);
sewardj4dced352002-06-04 22:54:20 +0000111 break;
sewardjde4a1d02002-03-22 01:27:54 +0000112 default:
njn25e49d8e72002-09-23 09:36:25 +0000113 if (VG_(needs).skin_errors)
njn810086f2002-11-14 12:42:47 +0000114 SK_(pp_SkinError)( err, &pp_ExeContextClosure );
njn25e49d8e72002-09-23 09:36:25 +0000115 else {
116 VG_(printf)("\nUnhandled error type: %u. VG_(needs).skin_errors\n"
117 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000118 err->ekind);
njne427a662002-10-02 11:08:25 +0000119 VG_(skin_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000120 }
sewardjde4a1d02002-03-22 01:27:54 +0000121 }
122}
123
sewardjde4a1d02002-03-22 01:27:54 +0000124/* Figure out if we want to attach for GDB for this error, possibly
125 by asking the user. */
126static
127Bool vg_is_GDB_attach_requested ( void )
128{
129 Char ch, ch2;
130 Int res;
131
132 if (VG_(clo_GDB_attach) == False)
133 return False;
134
135 VG_(message)(Vg_UserMsg, "");
136
137 again:
138 VG_(printf)(
139 "==%d== "
140 "---- Attach to GDB ? --- [Return/N/n/Y/y/C/c] ---- ",
141 VG_(getpid)()
142 );
143
144 res = VG_(read)(0 /*stdin*/, &ch, 1);
145 if (res != 1) goto ioerror;
146 /* res == 1 */
147 if (ch == '\n') return False;
148 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
149 && ch != 'C' && ch != 'c') goto again;
150
151 res = VG_(read)(0 /*stdin*/, &ch2, 1);
152 if (res != 1) goto ioerror;
153 if (ch2 != '\n') goto again;
154
155 /* No, don't want to attach. */
156 if (ch == 'n' || ch == 'N') return False;
157 /* Yes, want to attach. */
158 if (ch == 'y' || ch == 'Y') return True;
159 /* No, don't want to attach, and don't ask again either. */
160 vg_assert(ch == 'c' || ch == 'C');
161
162 ioerror:
163 VG_(clo_GDB_attach) = False;
164 return False;
165}
166
167
njn25e49d8e72002-09-23 09:36:25 +0000168/* I've gone all object-oriented... initialisation depends on where the
169 error comes from:
170
171 - If from generated code (tst == NULL), the %EIP/%EBP values that we
172 need in order to create proper error messages are picked up out of
173 VG_(baseBlock) rather than from the thread table (vg_threads in
174 vg_scheduler.c).
175
176 - If not from generated code but in response to requests passed back to
177 the scheduler (tst != NULL), we pick up %EIP/%EBP values from the
178 stored thread state, not from VG_(baseBlock).
179*/
180static __inline__
njn810086f2002-11-14 12:42:47 +0000181void construct_error ( Error* err, ThreadState* tst,
njn25e49d8e72002-09-23 09:36:25 +0000182 ErrorKind ekind, Addr a, Char* s, void* extra )
sewardjde4a1d02002-03-22 01:27:54 +0000183{
njn810086f2002-11-14 12:42:47 +0000184 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000185 err->next = NULL;
186 err->supp = NULL;
187 err->count = 1;
njn1d6c4bc2002-11-21 13:38:08 +0000188 err->where = VG_(get_ExeContext)( tst );
189
njn25e49d8e72002-09-23 09:36:25 +0000190 if (NULL == tst) {
191 err->tid = VG_(get_current_tid)();
njn25e49d8e72002-09-23 09:36:25 +0000192 err->m_eip = VG_(baseBlock)[VGOFF_(m_eip)];
193 err->m_esp = VG_(baseBlock)[VGOFF_(m_esp)];
194 err->m_ebp = VG_(baseBlock)[VGOFF_(m_ebp)];
195 } else {
njn25e49d8e72002-09-23 09:36:25 +0000196 err->tid = tst->tid;
197 err->m_eip = tst->m_eip;
198 err->m_esp = tst->m_esp;
199 err->m_ebp = tst->m_ebp;
200 }
201
njn810086f2002-11-14 12:42:47 +0000202 /* Skin-relevant parts */
203 err->ekind = ekind;
204 err->addr = a;
205 err->string = s;
206 err->extra = extra;
njn25e49d8e72002-09-23 09:36:25 +0000207
208 /* sanity... */
209 vg_assert(err->tid >= 0 && err->tid < VG_N_THREADS);
210}
211
212/* Top-level entry point to the error management subsystem.
213 All detected errors are notified here; this routine decides if/when the
214 user should see the error. */
215void VG_(maybe_record_error) ( ThreadState* tst,
216 ErrorKind ekind, Addr a, Char* s, void* extra )
217{
njn810086f2002-11-14 12:42:47 +0000218 Error err;
219 Error* p;
220 Error* p_prev;
221 VgRes exe_res = Vg_MedRes;
222 static Bool is_first_shown_context = True;
223 static Bool stopping_message = False;
224 static Bool slowdown_message = False;
225 static Int vg_n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000226
sewardjf2537be2002-04-24 21:03:47 +0000227 /* After M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
228 been found, or M_VG_COLLECT_NO_ERRORS_AFTER_FOUND total errors
229 have been found, just refuse to collect any more. This stops
230 the burden of the error-management system becoming excessive in
231 extremely buggy programs, although it does make it pretty
232 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000233 if (VG_(clo_error_limit)
sewardj72f98ff2002-06-13 17:23:38 +0000234 && (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN
235 || vg_n_errs_found >= M_VG_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000236 if (!stopping_message) {
237 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000238
239 if (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN) {
240 VG_(message)(Vg_UserMsg,
241 "More than %d different errors detected. "
242 "I'm not reporting any more.",
243 M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN );
244 } else {
245 VG_(message)(Vg_UserMsg,
246 "More than %d total errors detected. "
247 "I'm not reporting any more.",
248 M_VG_COLLECT_NO_ERRORS_AFTER_FOUND );
249 }
250
sewardjde4a1d02002-03-22 01:27:54 +0000251 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000252 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000253 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000254 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000255 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000256 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000257 VG_(message)(Vg_UserMsg,
258 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000259 VG_(message)(Vg_UserMsg, "");
260 stopping_message = True;
261 }
262 return;
263 }
264
265 /* After M_VG_COLLECT_ERRORS_SLOWLY_AFTER different errors have
266 been found, be much more conservative about collecting new
267 ones. */
268 if (vg_n_errs_shown >= M_VG_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000269 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000270 if (!slowdown_message) {
271 VG_(message)(Vg_UserMsg, "");
272 VG_(message)(Vg_UserMsg,
273 "More than %d errors detected. Subsequent errors",
274 M_VG_COLLECT_ERRORS_SLOWLY_AFTER);
275 VG_(message)(Vg_UserMsg,
276 "will still be recorded, but in less detail than before.");
277 slowdown_message = True;
278 }
279 }
280
njn25e49d8e72002-09-23 09:36:25 +0000281 /* Build ourselves the error */
282 construct_error ( &err, tst, ekind, a, s, extra );
sewardjde4a1d02002-03-22 01:27:54 +0000283
284 /* First, see if we've got an error record matching this one. */
njn25e49d8e72002-09-23 09:36:25 +0000285 p = vg_errors;
sewardjde4a1d02002-03-22 01:27:54 +0000286 p_prev = NULL;
287 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000288 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000289 /* Found it. */
290 p->count++;
291 if (p->supp != NULL) {
292 /* Deal correctly with suppressed errors. */
293 p->supp->count++;
294 vg_n_errs_suppressed++;
295 } else {
296 vg_n_errs_found++;
297 }
298
299 /* Move p to the front of the list so that future searches
300 for it are faster. */
301 if (p_prev != NULL) {
302 vg_assert(p_prev->next == p);
303 p_prev->next = p->next;
njn25e49d8e72002-09-23 09:36:25 +0000304 p->next = vg_errors;
305 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000306 }
307 return;
308 }
309 p_prev = p;
310 p = p->next;
311 }
312
313 /* Didn't see it. Copy and add. */
314
njn25e49d8e72002-09-23 09:36:25 +0000315 /* OK, we're really going to collect it. First make a copy,
316 because the error context is on the stack and will disappear shortly.
317 We can duplicate the main part ourselves, but use
njnae17bec2003-01-28 19:59:38 +0000318 SK_(dup_extra_and_update) to duplicate the `extra' part.
njn25e49d8e72002-09-23 09:36:25 +0000319
njn810086f2002-11-14 12:42:47 +0000320 SK_(dup_extra_and_update) can also update the `extra' part. This is
njn25e49d8e72002-09-23 09:36:25 +0000321 for when there are more details to fill in which take time to work out
322 but don't affect our earlier decision to include the error -- by
323 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000324 case where we ignore the error. Ugly.
njn25e49d8e72002-09-23 09:36:25 +0000325 */
njn810086f2002-11-14 12:42:47 +0000326 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000327 *p = err;
njnae17bec2003-01-28 19:59:38 +0000328 p->extra = SK_(dup_extra_and_update)(p);
njn25e49d8e72002-09-23 09:36:25 +0000329 p->next = vg_errors;
330 p->supp = is_suppressible_error(&err);
331 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000332 if (p->supp == NULL) {
333 vg_n_errs_found++;
334 if (!is_first_shown_context)
335 VG_(message)(Vg_UserMsg, "");
njn810086f2002-11-14 12:42:47 +0000336 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000337 is_first_shown_context = False;
338 vg_n_errs_shown++;
339 /* Perhaps we want a GDB attach at this point? */
340 if (vg_is_GDB_attach_requested()) {
sewardj35805422002-04-21 13:05:34 +0000341 VG_(swizzle_esp_then_start_GDB)(
njn25e49d8e72002-09-23 09:36:25 +0000342 err.m_eip, err.m_esp, err.m_ebp);
sewardjde4a1d02002-03-22 01:27:54 +0000343 }
344 } else {
345 vg_n_errs_suppressed++;
346 p->supp->count++;
347 }
348}
349
350
sewardjde4a1d02002-03-22 01:27:54 +0000351/*------------------------------------------------------------*/
352/*--- Exported fns ---*/
353/*------------------------------------------------------------*/
354
njn25e49d8e72002-09-23 09:36:25 +0000355/* These are called not from generated code but from the scheduler */
sewardj8c824512002-04-14 04:16:48 +0000356
njn25e49d8e72002-09-23 09:36:25 +0000357void VG_(record_pthread_error) ( ThreadId tid, Char* msg )
sewardjde4a1d02002-03-22 01:27:54 +0000358{
njn25e49d8e72002-09-23 09:36:25 +0000359 if (! VG_(needs).core_errors) return;
360 VG_(maybe_record_error)( &VG_(threads)[tid], PThreadErr, /*addr*/0, msg,
361 /*extra*/NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000362}
363
sewardj8c824512002-04-14 04:16:48 +0000364/*------------------------------*/
365
sewardjde4a1d02002-03-22 01:27:54 +0000366void VG_(show_all_errors) ( void )
367{
njn810086f2002-11-14 12:42:47 +0000368 Int i, n_min;
369 Int n_err_contexts, n_supp_contexts;
370 Error *p, *p_min;
371 Supp *su;
372 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000373
374 if (VG_(clo_verbosity) == 0)
375 return;
376
377 n_err_contexts = 0;
njn25e49d8e72002-09-23 09:36:25 +0000378 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000379 if (p->supp == NULL)
380 n_err_contexts++;
381 }
382
383 n_supp_contexts = 0;
384 for (su = vg_suppressions; su != NULL; su = su->next) {
385 if (su->count > 0)
386 n_supp_contexts++;
387 }
388
389 VG_(message)(Vg_UserMsg,
390 "ERROR SUMMARY: "
391 "%d errors from %d contexts (suppressed: %d from %d)",
392 vg_n_errs_found, n_err_contexts,
393 vg_n_errs_suppressed, n_supp_contexts );
394
395 if (VG_(clo_verbosity) <= 1)
396 return;
397
398 /* Print the contexts in order of increasing error count. */
399 for (i = 0; i < n_err_contexts; i++) {
400 n_min = (1 << 30) - 1;
401 p_min = NULL;
njn25e49d8e72002-09-23 09:36:25 +0000402 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000403 if (p->supp != NULL) continue;
404 if (p->count < n_min) {
405 n_min = p->count;
406 p_min = p;
407 }
408 }
njne427a662002-10-02 11:08:25 +0000409 if (p_min == NULL) VG_(skin_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000410
411 VG_(message)(Vg_UserMsg, "");
412 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
413 p_min->count,
414 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000415 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000416
417 if ((i+1 == VG_(clo_dump_error))) {
sewardj1e8cdc92002-04-18 11:37:52 +0000418 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to below NULLs */,
sewardj22854b92002-11-30 14:00:47 +0000419 p_min->where->eips[0], NULL, NULL, NULL, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000420 }
421
422 p_min->count = 1 << 30;
423 }
424
425 if (n_supp_contexts > 0)
426 VG_(message)(Vg_DebugMsg, "");
427 any_supp = False;
428 for (su = vg_suppressions; su != NULL; su = su->next) {
429 if (su->count > 0) {
430 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000431 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000432 }
433 }
434
435 if (n_err_contexts > 0) {
436 if (any_supp)
437 VG_(message)(Vg_UserMsg, "");
438 VG_(message)(Vg_UserMsg,
439 "IN SUMMARY: "
440 "%d errors from %d contexts (suppressed: %d from %d)",
441 vg_n_errs_found, n_err_contexts,
442 vg_n_errs_suppressed,
443 n_supp_contexts );
444 VG_(message)(Vg_UserMsg, "");
445 }
446}
447
448/*------------------------------------------------------------*/
449/*--- Standard suppressions ---*/
450/*------------------------------------------------------------*/
451
452/* Get a non-blank, non-comment line of at most nBuf chars from fd.
453 Skips leading spaces on the line. Return True if EOF was hit instead.
454*/
455
456#define VG_ISSPACE(ch) (((ch)==' ') || ((ch)=='\n') || ((ch)=='\t'))
457
njn4ba5a792002-09-30 10:23:54 +0000458Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000459{
460 Char ch;
461 Int n, i;
462 while (True) {
463 /* First, read until a non-blank char appears. */
464 while (True) {
465 n = VG_(read)(fd, &ch, 1);
466 if (n == 1 && !VG_ISSPACE(ch)) break;
467 if (n == 0) return True;
468 }
469
470 /* Now, read the line into buf. */
471 i = 0;
472 buf[i++] = ch; buf[i] = 0;
473 while (True) {
474 n = VG_(read)(fd, &ch, 1);
475 if (n == 0) return False; /* the next call will return True */
476 if (ch == '\n') break;
477 if (i > 0 && i == nBuf-1) i--;
478 buf[i++] = ch; buf[i] = 0;
479 }
480 while (i > 1 && VG_ISSPACE(buf[i-1])) {
481 i--; buf[i] = 0;
482 };
483
484 /* VG_(printf)("The line is `%s'\n", buf); */
485 /* Ok, we have a line. If a non-comment line, return.
486 If a comment line, start all over again. */
487 if (buf[0] != '#') return False;
488 }
489}
490
491
492/* *p_caller contains the raw name of a caller, supposedly either
493 fun:some_function_name or
494 obj:some_object_name.
495 Set *p_ty accordingly and advance *p_caller over the descriptor
496 (fun: or obj:) part.
497 Returns False if failed.
498*/
njn25e49d8e72002-09-23 09:36:25 +0000499static Bool setLocationTy ( Char** p_caller, SuppLocTy* p_ty )
sewardjde4a1d02002-03-22 01:27:54 +0000500{
501 if (VG_(strncmp)(*p_caller, "fun:", 4) == 0) {
502 (*p_caller) += 4;
503 *p_ty = FunName;
504 return True;
505 }
506 if (VG_(strncmp)(*p_caller, "obj:", 4) == 0) {
507 (*p_caller) += 4;
508 *p_ty = ObjName;
509 return True;
510 }
511 VG_(printf)("location should start with fun: or obj:\n");
512 return False;
513}
514
515
njn11cc9252002-10-07 14:42:59 +0000516/* Look for "skin" in a string like "skin1,skin2,skin3" */
517static __inline__
518Bool skin_name_present(Char *name, Char *names)
519{
520 Bool found;
521 Char *s = NULL; /* Shut gcc up */
522 Int len = VG_(strlen)(name);
523
524 found = (NULL != (s = VG_(strstr)(names, name)) &&
525 (s == names || *(s-1) == ',') &&
526 (*(s+len) == ',' || *(s+len) == '\0')
527 );
528
529 return found;
530}
531
532#define STREQ(s1,s2) (s1 != NULL && s2 != NULL \
533 && VG_(strcmp)((s1),(s2))==0)
534
sewardjde4a1d02002-03-22 01:27:54 +0000535/* Read suppressions from the file specified in vg_clo_suppressions
536 and place them in the suppressions list. If there's any difficulty
537 doing this, just give up -- there's no point in trying to recover.
538*/
sewardjde4a1d02002-03-22 01:27:54 +0000539static void load_one_suppressions_file ( Char* filename )
540{
541# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000542 Int fd, i;
543 Bool eof;
544 Char buf[N_BUF+1];
njn11cc9252002-10-07 14:42:59 +0000545 Char* skin_names;
njnc40c3a82002-10-02 11:02:27 +0000546 Char* supp_name;
547
njn25e49d8e72002-09-23 09:36:25 +0000548 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
sewardjde4a1d02002-03-22 01:27:54 +0000549 if (fd == -1) {
njn25e49d8e72002-09-23 09:36:25 +0000550 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file `%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000551 filename );
552 VG_(exit)(1);
553 }
554
555 while (True) {
njn25e49d8e72002-09-23 09:36:25 +0000556 /* Assign and initialise the two suppression halves (core and skin) */
njn810086f2002-11-14 12:42:47 +0000557 Supp* supp;
558 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000559 supp->count = 0;
njn25e49d8e72002-09-23 09:36:25 +0000560 for (i = 0; i < VG_N_SUPP_CALLERS; i++) supp->caller[i] = NULL;
njn810086f2002-11-14 12:42:47 +0000561 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000562
njn4ba5a792002-09-30 10:23:54 +0000563 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000564 if (eof) break;
565
566 if (!STREQ(buf, "{")) goto syntax_error;
567
njn4ba5a792002-09-30 10:23:54 +0000568 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000569 if (eof || STREQ(buf, "}")) goto syntax_error;
njn25e49d8e72002-09-23 09:36:25 +0000570 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000571
njn4ba5a792002-09-30 10:23:54 +0000572 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000573
sewardjde4a1d02002-03-22 01:27:54 +0000574 if (eof) goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000575
njn11cc9252002-10-07 14:42:59 +0000576 /* Check it has the "skin1,skin2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000577 i = 0;
578 while (True) {
579 if (buf[i] == ':') break;
580 if (buf[i] == '\0') goto syntax_error;
581 i++;
njn25e49d8e72002-09-23 09:36:25 +0000582 }
njnc40c3a82002-10-02 11:02:27 +0000583 buf[i] = '\0'; /* Replace ':', splitting into two strings */
584
njn11cc9252002-10-07 14:42:59 +0000585 skin_names = & buf[0];
586 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000587
njn11cc9252002-10-07 14:42:59 +0000588 /* Is it a core suppression? */
589 if (VG_(needs).core_errors && skin_name_present("core", skin_names))
njnc40c3a82002-10-02 11:02:27 +0000590 {
njn11cc9252002-10-07 14:42:59 +0000591 if (STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000592 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000593 else
594 goto syntax_error;
595 }
596
njn11cc9252002-10-07 14:42:59 +0000597 /* Is it a skin suppression? */
598 else if (VG_(needs).skin_errors &&
599 skin_name_present(VG_(details).name, skin_names))
njnc40c3a82002-10-02 11:02:27 +0000600 {
njn810086f2002-11-14 12:42:47 +0000601 if (SK_(recognised_suppression)(supp_name, supp))
njnc40c3a82002-10-02 11:02:27 +0000602 {
njn810086f2002-11-14 12:42:47 +0000603 /* Do nothing, function fills in supp->skind */
njnc40c3a82002-10-02 11:02:27 +0000604 } else
605 goto syntax_error;
606 }
607
njn25e49d8e72002-09-23 09:36:25 +0000608 else {
njnc40c3a82002-10-02 11:02:27 +0000609 /* Ignore rest of suppression */
njn25e49d8e72002-09-23 09:36:25 +0000610 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000611 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000612 if (eof) goto syntax_error;
613 if (STREQ(buf, "}"))
614 break;
615 }
616 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000617 }
618
njn25e49d8e72002-09-23 09:36:25 +0000619 if (VG_(needs).skin_errors &&
njn810086f2002-11-14 12:42:47 +0000620 !SK_(read_extra_suppression_info)(fd, buf, N_BUF, supp))
sewardjde4a1d02002-03-22 01:27:54 +0000621 goto syntax_error;
622
njn25e49d8e72002-09-23 09:36:25 +0000623 /* "i > 0" ensures at least one caller read. */
624 for (i = 0; i < VG_N_SUPP_CALLERS; i++) {
njn4ba5a792002-09-30 10:23:54 +0000625 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000626 if (eof) goto syntax_error;
njn25e49d8e72002-09-23 09:36:25 +0000627 if (i > 0 && STREQ(buf, "}"))
628 break;
629 supp->caller[i] = VG_(arena_strdup)(VG_AR_CORE, buf);
630 if (!setLocationTy(&(supp->caller[i]), &(supp->caller_ty[i])))
631 goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000632 }
633
634 supp->next = vg_suppressions;
635 vg_suppressions = supp;
636 }
sewardjde4a1d02002-03-22 01:27:54 +0000637 VG_(close)(fd);
638 return;
639
640 syntax_error:
641 if (eof) {
642 VG_(message)(Vg_UserMsg,
643 "FATAL: in suppressions file `%s': unexpected EOF",
644 filename );
645 } else {
646 VG_(message)(Vg_UserMsg,
njn11cc9252002-10-07 14:42:59 +0000647 "FATAL: in suppressions file: `%s': syntax error on: %s",
sewardjde4a1d02002-03-22 01:27:54 +0000648 filename, buf );
649 }
650 VG_(close)(fd);
651 VG_(message)(Vg_UserMsg, "exiting now.");
652 VG_(exit)(1);
653
654# undef N_BUF
655}
656
657
658void VG_(load_suppressions) ( void )
659{
660 Int i;
661 vg_suppressions = NULL;
662 for (i = 0; i < VG_(clo_n_suppressions); i++) {
663 if (VG_(clo_verbosity) > 1) {
664 VG_(message)(Vg_UserMsg, "Reading suppressions file: %s",
665 VG_(clo_suppressions)[i] );
666 }
667 load_one_suppressions_file( VG_(clo_suppressions)[i] );
668 }
669}
670
njn25e49d8e72002-09-23 09:36:25 +0000671/* Return the name of an erring fn in a way which is useful
672 for comparing against the contents of a suppressions file.
673 Doesn't demangle the fn name, because we want to refer to
674 mangled names in the suppressions file.
sewardj99aac972002-12-26 01:53:45 +0000675*/
676void VG_(get_objname_fnname) ( Addr a,
677 Char* obj_buf, Int n_obj_buf,
678 Char* fun_buf, Int n_fun_buf )
njn25e49d8e72002-09-23 09:36:25 +0000679{
680 (void)VG_(get_objname) ( a, obj_buf, n_obj_buf );
681 (void)VG_(get_fnname_nodemangle)( a, fun_buf, n_fun_buf );
682}
683
684static __inline__
njn810086f2002-11-14 12:42:47 +0000685Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000686{
njn810086f2002-11-14 12:42:47 +0000687 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000688 case PThreadSupp:
njn810086f2002-11-14 12:42:47 +0000689 return (err->ekind == PThreadErr);
njn25e49d8e72002-09-23 09:36:25 +0000690 default:
691 if (VG_(needs).skin_errors) {
njn810086f2002-11-14 12:42:47 +0000692 return SK_(error_matches_suppression)(err, su);
njn25e49d8e72002-09-23 09:36:25 +0000693 } else {
694 VG_(printf)(
695 "\nUnhandled suppression type: %u. VG_(needs).skin_errors\n"
696 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000697 err->ekind);
njne427a662002-10-02 11:08:25 +0000698 VG_(skin_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000699 }
700 }
701}
702
703static __inline__
njn810086f2002-11-14 12:42:47 +0000704Bool supp_matches_callers(Supp* su, Char caller_obj[][M_VG_ERRTXT],
705 Char caller_fun[][M_VG_ERRTXT])
njn25e49d8e72002-09-23 09:36:25 +0000706{
707 Int i;
708
709 for (i = 0; su->caller[i] != NULL; i++) {
710 switch (su->caller_ty[i]) {
njn4ba5a792002-09-30 10:23:54 +0000711 case ObjName: if (VG_(string_match)(su->caller[i],
712 caller_obj[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000713 return False;
njn4ba5a792002-09-30 10:23:54 +0000714 case FunName: if (VG_(string_match)(su->caller[i],
715 caller_fun[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000716 return False;
njne427a662002-10-02 11:08:25 +0000717 default: VG_(skin_panic)("is_suppressible_error");
njn25e49d8e72002-09-23 09:36:25 +0000718 }
719 }
720
721 /* If we reach here, it's a match */
722 return True;
723}
sewardjde4a1d02002-03-22 01:27:54 +0000724
njn810086f2002-11-14 12:42:47 +0000725/* Does an error context match a suppression? ie is this a suppressible
726 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +0000727 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +0000728*/
njn810086f2002-11-14 12:42:47 +0000729static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +0000730{
njn25e49d8e72002-09-23 09:36:25 +0000731 Int i;
sewardjde4a1d02002-03-22 01:27:54 +0000732
njn25e49d8e72002-09-23 09:36:25 +0000733 Char caller_obj[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
734 Char caller_fun[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
sewardjde4a1d02002-03-22 01:27:54 +0000735
njn810086f2002-11-14 12:42:47 +0000736 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +0000737
njn25e49d8e72002-09-23 09:36:25 +0000738 /* get_objname_fnname() writes the function name and object name if
739 it finds them in the debug info. so the strings in the suppression
740 file should match these.
sewardjde4a1d02002-03-22 01:27:54 +0000741 */
742
743 /* Initialise these strs so they are always safe to compare, even
njn25e49d8e72002-09-23 09:36:25 +0000744 if get_objname_fnname doesn't write anything to them. */
745 for (i = 0; i < VG_N_SUPP_CALLERS; i++)
746 caller_obj[i][0] = caller_fun[i][0] = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000747
njn25e49d8e72002-09-23 09:36:25 +0000748 for (i = 0; i < VG_N_SUPP_CALLERS && i < VG_(clo_backtrace_size); i++) {
sewardj99aac972002-12-26 01:53:45 +0000749 VG_(get_objname_fnname) ( err->where->eips[i],
750 caller_obj[i], M_VG_ERRTXT,
751 caller_fun[i], M_VG_ERRTXT );
sewardjde4a1d02002-03-22 01:27:54 +0000752 }
753
754 /* See if the error context matches any suppression. */
755 for (su = vg_suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +0000756 if (supp_matches_error(su, err) &&
757 supp_matches_callers(su, caller_obj, caller_fun)) {
758 return su;
sewardjde4a1d02002-03-22 01:27:54 +0000759 }
sewardjde4a1d02002-03-22 01:27:54 +0000760 }
njn25e49d8e72002-09-23 09:36:25 +0000761 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +0000762}
763
njn11cc9252002-10-07 14:42:59 +0000764#undef STREQ
765
sewardjde4a1d02002-03-22 01:27:54 +0000766/*--------------------------------------------------------------------*/
767/*--- end vg_errcontext.c ---*/
768/*--------------------------------------------------------------------*/