blob: 49d208fbff41edc40d9bab0541ceb2e6b25d2b38 [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
njn810086f2002-11-14 12:42:47 +0000318 SK_(dup_extra_and_update) to duplicate the `extra' part (unless it's
njn25e49d8e72002-09-23 09:36:25 +0000319 NULL).
320
njn810086f2002-11-14 12:42:47 +0000321 SK_(dup_extra_and_update) can also update the `extra' part. This is
njn25e49d8e72002-09-23 09:36:25 +0000322 for when there are more details to fill in which take time to work out
323 but don't affect our earlier decision to include the error -- by
324 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000325 case where we ignore the error. Ugly.
njn25e49d8e72002-09-23 09:36:25 +0000326 */
njn810086f2002-11-14 12:42:47 +0000327 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000328 *p = err;
njn810086f2002-11-14 12:42:47 +0000329 if (NULL != err.extra)
330 p->extra = SK_(dup_extra_and_update)(p);
sewardjde4a1d02002-03-22 01:27:54 +0000331
njn25e49d8e72002-09-23 09:36:25 +0000332 p->next = vg_errors;
333 p->supp = is_suppressible_error(&err);
334 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000335 if (p->supp == NULL) {
336 vg_n_errs_found++;
337 if (!is_first_shown_context)
338 VG_(message)(Vg_UserMsg, "");
njn810086f2002-11-14 12:42:47 +0000339 pp_Error(p, False);
sewardjde4a1d02002-03-22 01:27:54 +0000340 is_first_shown_context = False;
341 vg_n_errs_shown++;
342 /* Perhaps we want a GDB attach at this point? */
343 if (vg_is_GDB_attach_requested()) {
sewardj35805422002-04-21 13:05:34 +0000344 VG_(swizzle_esp_then_start_GDB)(
njn25e49d8e72002-09-23 09:36:25 +0000345 err.m_eip, err.m_esp, err.m_ebp);
sewardjde4a1d02002-03-22 01:27:54 +0000346 }
347 } else {
348 vg_n_errs_suppressed++;
349 p->supp->count++;
350 }
351}
352
353
sewardjde4a1d02002-03-22 01:27:54 +0000354/*------------------------------------------------------------*/
355/*--- Exported fns ---*/
356/*------------------------------------------------------------*/
357
njn25e49d8e72002-09-23 09:36:25 +0000358/* These are called not from generated code but from the scheduler */
sewardj8c824512002-04-14 04:16:48 +0000359
njn25e49d8e72002-09-23 09:36:25 +0000360void VG_(record_pthread_error) ( ThreadId tid, Char* msg )
sewardjde4a1d02002-03-22 01:27:54 +0000361{
njn25e49d8e72002-09-23 09:36:25 +0000362 if (! VG_(needs).core_errors) return;
363 VG_(maybe_record_error)( &VG_(threads)[tid], PThreadErr, /*addr*/0, msg,
364 /*extra*/NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000365}
366
sewardj8c824512002-04-14 04:16:48 +0000367/*------------------------------*/
368
sewardjde4a1d02002-03-22 01:27:54 +0000369void VG_(show_all_errors) ( void )
370{
njn810086f2002-11-14 12:42:47 +0000371 Int i, n_min;
372 Int n_err_contexts, n_supp_contexts;
373 Error *p, *p_min;
374 Supp *su;
375 Bool any_supp;
sewardjde4a1d02002-03-22 01:27:54 +0000376
377 if (VG_(clo_verbosity) == 0)
378 return;
379
380 n_err_contexts = 0;
njn25e49d8e72002-09-23 09:36:25 +0000381 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000382 if (p->supp == NULL)
383 n_err_contexts++;
384 }
385
386 n_supp_contexts = 0;
387 for (su = vg_suppressions; su != NULL; su = su->next) {
388 if (su->count > 0)
389 n_supp_contexts++;
390 }
391
392 VG_(message)(Vg_UserMsg,
393 "ERROR SUMMARY: "
394 "%d errors from %d contexts (suppressed: %d from %d)",
395 vg_n_errs_found, n_err_contexts,
396 vg_n_errs_suppressed, n_supp_contexts );
397
398 if (VG_(clo_verbosity) <= 1)
399 return;
400
401 /* Print the contexts in order of increasing error count. */
402 for (i = 0; i < n_err_contexts; i++) {
403 n_min = (1 << 30) - 1;
404 p_min = NULL;
njn25e49d8e72002-09-23 09:36:25 +0000405 for (p = vg_errors; p != NULL; p = p->next) {
sewardjde4a1d02002-03-22 01:27:54 +0000406 if (p->supp != NULL) continue;
407 if (p->count < n_min) {
408 n_min = p->count;
409 p_min = p;
410 }
411 }
njne427a662002-10-02 11:08:25 +0000412 if (p_min == NULL) VG_(skin_panic)("show_all_errors()");
sewardjde4a1d02002-03-22 01:27:54 +0000413
414 VG_(message)(Vg_UserMsg, "");
415 VG_(message)(Vg_UserMsg, "%d errors in context %d of %d:",
416 p_min->count,
417 i+1, n_err_contexts);
njn810086f2002-11-14 12:42:47 +0000418 pp_Error( p_min, False );
sewardjde4a1d02002-03-22 01:27:54 +0000419
420 if ((i+1 == VG_(clo_dump_error))) {
sewardj1e8cdc92002-04-18 11:37:52 +0000421 VG_(translate) ( 0 /* dummy ThreadId; irrelevant due to below NULLs */,
sewardj22854b92002-11-30 14:00:47 +0000422 p_min->where->eips[0], NULL, NULL, NULL, NULL );
sewardjde4a1d02002-03-22 01:27:54 +0000423 }
424
425 p_min->count = 1 << 30;
426 }
427
428 if (n_supp_contexts > 0)
429 VG_(message)(Vg_DebugMsg, "");
430 any_supp = False;
431 for (su = vg_suppressions; su != NULL; su = su->next) {
432 if (su->count > 0) {
433 any_supp = True;
njn25e49d8e72002-09-23 09:36:25 +0000434 VG_(message)(Vg_DebugMsg, "supp: %4d %s", su->count, su->sname);
sewardjde4a1d02002-03-22 01:27:54 +0000435 }
436 }
437
438 if (n_err_contexts > 0) {
439 if (any_supp)
440 VG_(message)(Vg_UserMsg, "");
441 VG_(message)(Vg_UserMsg,
442 "IN SUMMARY: "
443 "%d errors from %d contexts (suppressed: %d from %d)",
444 vg_n_errs_found, n_err_contexts,
445 vg_n_errs_suppressed,
446 n_supp_contexts );
447 VG_(message)(Vg_UserMsg, "");
448 }
449}
450
451/*------------------------------------------------------------*/
452/*--- Standard suppressions ---*/
453/*------------------------------------------------------------*/
454
455/* Get a non-blank, non-comment line of at most nBuf chars from fd.
456 Skips leading spaces on the line. Return True if EOF was hit instead.
457*/
458
459#define VG_ISSPACE(ch) (((ch)==' ') || ((ch)=='\n') || ((ch)=='\t'))
460
njn4ba5a792002-09-30 10:23:54 +0000461Bool VG_(get_line) ( Int fd, Char* buf, Int nBuf )
sewardjde4a1d02002-03-22 01:27:54 +0000462{
463 Char ch;
464 Int n, i;
465 while (True) {
466 /* First, read until a non-blank char appears. */
467 while (True) {
468 n = VG_(read)(fd, &ch, 1);
469 if (n == 1 && !VG_ISSPACE(ch)) break;
470 if (n == 0) return True;
471 }
472
473 /* Now, read the line into buf. */
474 i = 0;
475 buf[i++] = ch; buf[i] = 0;
476 while (True) {
477 n = VG_(read)(fd, &ch, 1);
478 if (n == 0) return False; /* the next call will return True */
479 if (ch == '\n') break;
480 if (i > 0 && i == nBuf-1) i--;
481 buf[i++] = ch; buf[i] = 0;
482 }
483 while (i > 1 && VG_ISSPACE(buf[i-1])) {
484 i--; buf[i] = 0;
485 };
486
487 /* VG_(printf)("The line is `%s'\n", buf); */
488 /* Ok, we have a line. If a non-comment line, return.
489 If a comment line, start all over again. */
490 if (buf[0] != '#') return False;
491 }
492}
493
494
495/* *p_caller contains the raw name of a caller, supposedly either
496 fun:some_function_name or
497 obj:some_object_name.
498 Set *p_ty accordingly and advance *p_caller over the descriptor
499 (fun: or obj:) part.
500 Returns False if failed.
501*/
njn25e49d8e72002-09-23 09:36:25 +0000502static Bool setLocationTy ( Char** p_caller, SuppLocTy* p_ty )
sewardjde4a1d02002-03-22 01:27:54 +0000503{
504 if (VG_(strncmp)(*p_caller, "fun:", 4) == 0) {
505 (*p_caller) += 4;
506 *p_ty = FunName;
507 return True;
508 }
509 if (VG_(strncmp)(*p_caller, "obj:", 4) == 0) {
510 (*p_caller) += 4;
511 *p_ty = ObjName;
512 return True;
513 }
514 VG_(printf)("location should start with fun: or obj:\n");
515 return False;
516}
517
518
njn11cc9252002-10-07 14:42:59 +0000519/* Look for "skin" in a string like "skin1,skin2,skin3" */
520static __inline__
521Bool skin_name_present(Char *name, Char *names)
522{
523 Bool found;
524 Char *s = NULL; /* Shut gcc up */
525 Int len = VG_(strlen)(name);
526
527 found = (NULL != (s = VG_(strstr)(names, name)) &&
528 (s == names || *(s-1) == ',') &&
529 (*(s+len) == ',' || *(s+len) == '\0')
530 );
531
532 return found;
533}
534
535#define STREQ(s1,s2) (s1 != NULL && s2 != NULL \
536 && VG_(strcmp)((s1),(s2))==0)
537
sewardjde4a1d02002-03-22 01:27:54 +0000538/* Read suppressions from the file specified in vg_clo_suppressions
539 and place them in the suppressions list. If there's any difficulty
540 doing this, just give up -- there's no point in trying to recover.
541*/
sewardjde4a1d02002-03-22 01:27:54 +0000542static void load_one_suppressions_file ( Char* filename )
543{
544# define N_BUF 200
njnc40c3a82002-10-02 11:02:27 +0000545 Int fd, i;
546 Bool eof;
547 Char buf[N_BUF+1];
njn11cc9252002-10-07 14:42:59 +0000548 Char* skin_names;
njnc40c3a82002-10-02 11:02:27 +0000549 Char* supp_name;
550
njn25e49d8e72002-09-23 09:36:25 +0000551 fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
sewardjde4a1d02002-03-22 01:27:54 +0000552 if (fd == -1) {
njn25e49d8e72002-09-23 09:36:25 +0000553 VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file `%s'",
sewardjde4a1d02002-03-22 01:27:54 +0000554 filename );
555 VG_(exit)(1);
556 }
557
558 while (True) {
njn25e49d8e72002-09-23 09:36:25 +0000559 /* Assign and initialise the two suppression halves (core and skin) */
njn810086f2002-11-14 12:42:47 +0000560 Supp* supp;
561 supp = VG_(arena_malloc)(VG_AR_CORE, sizeof(Supp));
sewardjde4a1d02002-03-22 01:27:54 +0000562 supp->count = 0;
njn25e49d8e72002-09-23 09:36:25 +0000563 for (i = 0; i < VG_N_SUPP_CALLERS; i++) supp->caller[i] = NULL;
njn810086f2002-11-14 12:42:47 +0000564 supp->string = supp->extra = NULL;
sewardjde4a1d02002-03-22 01:27:54 +0000565
njn4ba5a792002-09-30 10:23:54 +0000566 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000567 if (eof) break;
568
569 if (!STREQ(buf, "{")) goto syntax_error;
570
njn4ba5a792002-09-30 10:23:54 +0000571 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000572 if (eof || STREQ(buf, "}")) goto syntax_error;
njn25e49d8e72002-09-23 09:36:25 +0000573 supp->sname = VG_(arena_strdup)(VG_AR_CORE, buf);
sewardjde4a1d02002-03-22 01:27:54 +0000574
njn4ba5a792002-09-30 10:23:54 +0000575 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000576
sewardjde4a1d02002-03-22 01:27:54 +0000577 if (eof) goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000578
njn11cc9252002-10-07 14:42:59 +0000579 /* Check it has the "skin1,skin2,...:supp" form (look for ':') */
njnc40c3a82002-10-02 11:02:27 +0000580 i = 0;
581 while (True) {
582 if (buf[i] == ':') break;
583 if (buf[i] == '\0') goto syntax_error;
584 i++;
njn25e49d8e72002-09-23 09:36:25 +0000585 }
njnc40c3a82002-10-02 11:02:27 +0000586 buf[i] = '\0'; /* Replace ':', splitting into two strings */
587
njn11cc9252002-10-07 14:42:59 +0000588 skin_names = & buf[0];
589 supp_name = & buf[i+1];
njnc40c3a82002-10-02 11:02:27 +0000590
njn11cc9252002-10-07 14:42:59 +0000591 /* Is it a core suppression? */
592 if (VG_(needs).core_errors && skin_name_present("core", skin_names))
njnc40c3a82002-10-02 11:02:27 +0000593 {
njn11cc9252002-10-07 14:42:59 +0000594 if (STREQ(supp_name, "PThread"))
njn810086f2002-11-14 12:42:47 +0000595 supp->skind = PThreadSupp;
njnc40c3a82002-10-02 11:02:27 +0000596 else
597 goto syntax_error;
598 }
599
njn11cc9252002-10-07 14:42:59 +0000600 /* Is it a skin suppression? */
601 else if (VG_(needs).skin_errors &&
602 skin_name_present(VG_(details).name, skin_names))
njnc40c3a82002-10-02 11:02:27 +0000603 {
njn810086f2002-11-14 12:42:47 +0000604 if (SK_(recognised_suppression)(supp_name, supp))
njnc40c3a82002-10-02 11:02:27 +0000605 {
njn810086f2002-11-14 12:42:47 +0000606 /* Do nothing, function fills in supp->skind */
njnc40c3a82002-10-02 11:02:27 +0000607 } else
608 goto syntax_error;
609 }
610
njn25e49d8e72002-09-23 09:36:25 +0000611 else {
njnc40c3a82002-10-02 11:02:27 +0000612 /* Ignore rest of suppression */
njn25e49d8e72002-09-23 09:36:25 +0000613 while (True) {
njn4ba5a792002-09-30 10:23:54 +0000614 eof = VG_(get_line) ( fd, buf, N_BUF );
njn25e49d8e72002-09-23 09:36:25 +0000615 if (eof) goto syntax_error;
616 if (STREQ(buf, "}"))
617 break;
618 }
619 continue;
sewardjde4a1d02002-03-22 01:27:54 +0000620 }
621
njn25e49d8e72002-09-23 09:36:25 +0000622 if (VG_(needs).skin_errors &&
njn810086f2002-11-14 12:42:47 +0000623 !SK_(read_extra_suppression_info)(fd, buf, N_BUF, supp))
sewardjde4a1d02002-03-22 01:27:54 +0000624 goto syntax_error;
625
njn25e49d8e72002-09-23 09:36:25 +0000626 /* "i > 0" ensures at least one caller read. */
627 for (i = 0; i < VG_N_SUPP_CALLERS; i++) {
njn4ba5a792002-09-30 10:23:54 +0000628 eof = VG_(get_line) ( fd, buf, N_BUF );
sewardjde4a1d02002-03-22 01:27:54 +0000629 if (eof) goto syntax_error;
njn25e49d8e72002-09-23 09:36:25 +0000630 if (i > 0 && STREQ(buf, "}"))
631 break;
632 supp->caller[i] = VG_(arena_strdup)(VG_AR_CORE, buf);
633 if (!setLocationTy(&(supp->caller[i]), &(supp->caller_ty[i])))
634 goto syntax_error;
sewardjde4a1d02002-03-22 01:27:54 +0000635 }
636
637 supp->next = vg_suppressions;
638 vg_suppressions = supp;
639 }
sewardjde4a1d02002-03-22 01:27:54 +0000640 VG_(close)(fd);
641 return;
642
643 syntax_error:
644 if (eof) {
645 VG_(message)(Vg_UserMsg,
646 "FATAL: in suppressions file `%s': unexpected EOF",
647 filename );
648 } else {
649 VG_(message)(Vg_UserMsg,
njn11cc9252002-10-07 14:42:59 +0000650 "FATAL: in suppressions file: `%s': syntax error on: %s",
sewardjde4a1d02002-03-22 01:27:54 +0000651 filename, buf );
652 }
653 VG_(close)(fd);
654 VG_(message)(Vg_UserMsg, "exiting now.");
655 VG_(exit)(1);
656
657# undef N_BUF
658}
659
660
661void VG_(load_suppressions) ( void )
662{
663 Int i;
664 vg_suppressions = NULL;
665 for (i = 0; i < VG_(clo_n_suppressions); i++) {
666 if (VG_(clo_verbosity) > 1) {
667 VG_(message)(Vg_UserMsg, "Reading suppressions file: %s",
668 VG_(clo_suppressions)[i] );
669 }
670 load_one_suppressions_file( VG_(clo_suppressions)[i] );
671 }
672}
673
njn25e49d8e72002-09-23 09:36:25 +0000674/* Return the name of an erring fn in a way which is useful
675 for comparing against the contents of a suppressions file.
676 Doesn't demangle the fn name, because we want to refer to
677 mangled names in the suppressions file.
sewardj99aac972002-12-26 01:53:45 +0000678*/
679void VG_(get_objname_fnname) ( Addr a,
680 Char* obj_buf, Int n_obj_buf,
681 Char* fun_buf, Int n_fun_buf )
njn25e49d8e72002-09-23 09:36:25 +0000682{
683 (void)VG_(get_objname) ( a, obj_buf, n_obj_buf );
684 (void)VG_(get_fnname_nodemangle)( a, fun_buf, n_fun_buf );
685}
686
687static __inline__
njn810086f2002-11-14 12:42:47 +0000688Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000689{
njn810086f2002-11-14 12:42:47 +0000690 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000691 case PThreadSupp:
njn810086f2002-11-14 12:42:47 +0000692 return (err->ekind == PThreadErr);
njn25e49d8e72002-09-23 09:36:25 +0000693 default:
694 if (VG_(needs).skin_errors) {
njn810086f2002-11-14 12:42:47 +0000695 return SK_(error_matches_suppression)(err, su);
njn25e49d8e72002-09-23 09:36:25 +0000696 } else {
697 VG_(printf)(
698 "\nUnhandled suppression type: %u. VG_(needs).skin_errors\n"
699 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000700 err->ekind);
njne427a662002-10-02 11:08:25 +0000701 VG_(skin_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000702 }
703 }
704}
705
706static __inline__
njn810086f2002-11-14 12:42:47 +0000707Bool supp_matches_callers(Supp* su, Char caller_obj[][M_VG_ERRTXT],
708 Char caller_fun[][M_VG_ERRTXT])
njn25e49d8e72002-09-23 09:36:25 +0000709{
710 Int i;
711
712 for (i = 0; su->caller[i] != NULL; i++) {
713 switch (su->caller_ty[i]) {
njn4ba5a792002-09-30 10:23:54 +0000714 case ObjName: if (VG_(string_match)(su->caller[i],
715 caller_obj[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000716 return False;
njn4ba5a792002-09-30 10:23:54 +0000717 case FunName: if (VG_(string_match)(su->caller[i],
718 caller_fun[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000719 return False;
njne427a662002-10-02 11:08:25 +0000720 default: VG_(skin_panic)("is_suppressible_error");
njn25e49d8e72002-09-23 09:36:25 +0000721 }
722 }
723
724 /* If we reach here, it's a match */
725 return True;
726}
sewardjde4a1d02002-03-22 01:27:54 +0000727
njn810086f2002-11-14 12:42:47 +0000728/* Does an error context match a suppression? ie is this a suppressible
729 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +0000730 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +0000731*/
njn810086f2002-11-14 12:42:47 +0000732static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +0000733{
njn25e49d8e72002-09-23 09:36:25 +0000734 Int i;
sewardjde4a1d02002-03-22 01:27:54 +0000735
njn25e49d8e72002-09-23 09:36:25 +0000736 Char caller_obj[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
737 Char caller_fun[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
sewardjde4a1d02002-03-22 01:27:54 +0000738
njn810086f2002-11-14 12:42:47 +0000739 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +0000740
njn25e49d8e72002-09-23 09:36:25 +0000741 /* get_objname_fnname() writes the function name and object name if
742 it finds them in the debug info. so the strings in the suppression
743 file should match these.
sewardjde4a1d02002-03-22 01:27:54 +0000744 */
745
746 /* Initialise these strs so they are always safe to compare, even
njn25e49d8e72002-09-23 09:36:25 +0000747 if get_objname_fnname doesn't write anything to them. */
748 for (i = 0; i < VG_N_SUPP_CALLERS; i++)
749 caller_obj[i][0] = caller_fun[i][0] = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000750
njn25e49d8e72002-09-23 09:36:25 +0000751 for (i = 0; i < VG_N_SUPP_CALLERS && i < VG_(clo_backtrace_size); i++) {
sewardj99aac972002-12-26 01:53:45 +0000752 VG_(get_objname_fnname) ( err->where->eips[i],
753 caller_obj[i], M_VG_ERRTXT,
754 caller_fun[i], M_VG_ERRTXT );
sewardjde4a1d02002-03-22 01:27:54 +0000755 }
756
757 /* See if the error context matches any suppression. */
758 for (su = vg_suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +0000759 if (supp_matches_error(su, err) &&
760 supp_matches_callers(su, caller_obj, caller_fun)) {
761 return su;
sewardjde4a1d02002-03-22 01:27:54 +0000762 }
sewardjde4a1d02002-03-22 01:27:54 +0000763 }
njn25e49d8e72002-09-23 09:36:25 +0000764 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +0000765}
766
njn11cc9252002-10-07 14:42:59 +0000767#undef STREQ
768
sewardjde4a1d02002-03-22 01:27:54 +0000769/*--------------------------------------------------------------------*/
770/*--- end vg_errcontext.c ---*/
771/*--------------------------------------------------------------------*/