blob: f00e6b6ede52c8e35995c8e2237324d8be4c0f16 [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
45/* Running count of unsuppressed errors detected. */
46static UInt vg_n_errs_found = 0;
47
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/*------------------------------------------------------------*/
56/*--- Helper fns ---*/
57/*------------------------------------------------------------*/
58
sewardjde4a1d02002-03-22 01:27:54 +000059/* Compare error contexts, to detect duplicates. Note that if they
60 are otherwise the same, the faulting addrs and associated rwoffsets
61 are allowed to be different. */
njn810086f2002-11-14 12:42:47 +000062static Bool eq_Error ( VgRes res, Error* e1, Error* e2 )
sewardjde4a1d02002-03-22 01:27:54 +000063{
njn810086f2002-11-14 12:42:47 +000064 if (e1->ekind != e2->ekind)
sewardjde4a1d02002-03-22 01:27:54 +000065 return False;
njn25e49d8e72002-09-23 09:36:25 +000066 if (!VG_(eq_ExeContext)(res, e1->where, e2->where))
sewardjde4a1d02002-03-22 01:27:54 +000067 return False;
68
njn810086f2002-11-14 12:42:47 +000069 switch (e1->ekind) {
sewardj4dced352002-06-04 22:54:20 +000070 case PThreadErr:
njn25e49d8e72002-09-23 09:36:25 +000071 vg_assert(VG_(needs).core_errors);
njn810086f2002-11-14 12:42:47 +000072 if (e1->string == e2->string)
sewardj4dced352002-06-04 22:54:20 +000073 return True;
njn810086f2002-11-14 12:42:47 +000074 if (0 == VG_(strcmp)(e1->string, e2->string))
sewardj4dced352002-06-04 22:54:20 +000075 return True;
76 return False;
sewardjde4a1d02002-03-22 01:27:54 +000077 default:
njn25e49d8e72002-09-23 09:36:25 +000078 if (VG_(needs).skin_errors)
njn810086f2002-11-14 12:42:47 +000079 return SK_(eq_SkinError)(res, e1, e2);
njn25e49d8e72002-09-23 09:36:25 +000080 else {
81 VG_(printf)("\nUnhandled error type: %u. VG_(needs).skin_errors\n"
82 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +000083 e1->ekind);
njne427a662002-10-02 11:08:25 +000084 VG_(skin_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +000085 }
sewardjde4a1d02002-03-22 01:27:54 +000086 }
87}
88
njn810086f2002-11-14 12:42:47 +000089static void pp_Error ( Error* err, Bool printCount )
sewardjde4a1d02002-03-22 01:27:54 +000090{
njn25e49d8e72002-09-23 09:36:25 +000091 /* Closure for printing where the error occurred. Abstracts details
92 about the `where' field away from the skin. */
93 void pp_ExeContextClosure(void)
94 {
95 VG_(pp_ExeContext) ( err->where );
sewardjde4a1d02002-03-22 01:27:54 +000096 }
njn25e49d8e72002-09-23 09:36:25 +000097
sewardjde4a1d02002-03-22 01:27:54 +000098 if (printCount)
njn25e49d8e72002-09-23 09:36:25 +000099 VG_(message)(Vg_UserMsg, "Observed %d times:", err->count );
100 if (err->tid > 1)
101 VG_(message)(Vg_UserMsg, "Thread %d:", err->tid );
102
njn810086f2002-11-14 12:42:47 +0000103 switch (err->ekind) {
sewardj4dced352002-06-04 22:54:20 +0000104 case PThreadErr:
njn25e49d8e72002-09-23 09:36:25 +0000105 vg_assert(VG_(needs).core_errors);
njn810086f2002-11-14 12:42:47 +0000106 VG_(message)(Vg_UserMsg, "%s", err->string );
njn25e49d8e72002-09-23 09:36:25 +0000107 VG_(pp_ExeContext)(err->where);
sewardj4dced352002-06-04 22:54:20 +0000108 break;
sewardjde4a1d02002-03-22 01:27:54 +0000109 default:
njn25e49d8e72002-09-23 09:36:25 +0000110 if (VG_(needs).skin_errors)
njn810086f2002-11-14 12:42:47 +0000111 SK_(pp_SkinError)( err, &pp_ExeContextClosure );
njn25e49d8e72002-09-23 09:36:25 +0000112 else {
113 VG_(printf)("\nUnhandled error type: %u. VG_(needs).skin_errors\n"
114 "probably needs to be set?\n",
njn810086f2002-11-14 12:42:47 +0000115 err->ekind);
njne427a662002-10-02 11:08:25 +0000116 VG_(skin_panic)("unhandled error type");
njn25e49d8e72002-09-23 09:36:25 +0000117 }
sewardjde4a1d02002-03-22 01:27:54 +0000118 }
119}
120
sewardjde4a1d02002-03-22 01:27:54 +0000121/* Figure out if we want to attach for GDB for this error, possibly
122 by asking the user. */
123static
124Bool vg_is_GDB_attach_requested ( void )
125{
126 Char ch, ch2;
127 Int res;
128
129 if (VG_(clo_GDB_attach) == False)
130 return False;
131
132 VG_(message)(Vg_UserMsg, "");
133
134 again:
135 VG_(printf)(
136 "==%d== "
137 "---- Attach to GDB ? --- [Return/N/n/Y/y/C/c] ---- ",
138 VG_(getpid)()
139 );
140
141 res = VG_(read)(0 /*stdin*/, &ch, 1);
142 if (res != 1) goto ioerror;
143 /* res == 1 */
144 if (ch == '\n') return False;
145 if (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y'
146 && ch != 'C' && ch != 'c') goto again;
147
148 res = VG_(read)(0 /*stdin*/, &ch2, 1);
149 if (res != 1) goto ioerror;
150 if (ch2 != '\n') goto again;
151
152 /* No, don't want to attach. */
153 if (ch == 'n' || ch == 'N') return False;
154 /* Yes, want to attach. */
155 if (ch == 'y' || ch == 'Y') return True;
156 /* No, don't want to attach, and don't ask again either. */
157 vg_assert(ch == 'c' || ch == 'C');
158
159 ioerror:
160 VG_(clo_GDB_attach) = False;
161 return False;
162}
163
164
njn25e49d8e72002-09-23 09:36:25 +0000165/* I've gone all object-oriented... initialisation depends on where the
166 error comes from:
167
168 - If from generated code (tst == NULL), the %EIP/%EBP values that we
169 need in order to create proper error messages are picked up out of
170 VG_(baseBlock) rather than from the thread table (vg_threads in
171 vg_scheduler.c).
172
173 - If not from generated code but in response to requests passed back to
174 the scheduler (tst != NULL), we pick up %EIP/%EBP values from the
175 stored thread state, not from VG_(baseBlock).
176*/
177static __inline__
njn810086f2002-11-14 12:42:47 +0000178void construct_error ( Error* err, ThreadState* tst,
njn25e49d8e72002-09-23 09:36:25 +0000179 ErrorKind ekind, Addr a, Char* s, void* extra )
sewardjde4a1d02002-03-22 01:27:54 +0000180{
njn810086f2002-11-14 12:42:47 +0000181 /* Core-only parts */
njn25e49d8e72002-09-23 09:36:25 +0000182 err->next = NULL;
183 err->supp = NULL;
184 err->count = 1;
njn1d6c4bc2002-11-21 13:38:08 +0000185 err->where = VG_(get_ExeContext)( tst );
186
njn25e49d8e72002-09-23 09:36:25 +0000187 if (NULL == tst) {
188 err->tid = VG_(get_current_tid)();
njn25e49d8e72002-09-23 09:36:25 +0000189 err->m_eip = VG_(baseBlock)[VGOFF_(m_eip)];
190 err->m_esp = VG_(baseBlock)[VGOFF_(m_esp)];
191 err->m_ebp = VG_(baseBlock)[VGOFF_(m_ebp)];
192 } else {
njn25e49d8e72002-09-23 09:36:25 +0000193 err->tid = tst->tid;
194 err->m_eip = tst->m_eip;
195 err->m_esp = tst->m_esp;
196 err->m_ebp = tst->m_ebp;
197 }
198
njn810086f2002-11-14 12:42:47 +0000199 /* Skin-relevant parts */
200 err->ekind = ekind;
201 err->addr = a;
202 err->string = s;
203 err->extra = extra;
njn25e49d8e72002-09-23 09:36:25 +0000204
205 /* sanity... */
206 vg_assert(err->tid >= 0 && err->tid < VG_N_THREADS);
207}
208
209/* Top-level entry point to the error management subsystem.
210 All detected errors are notified here; this routine decides if/when the
211 user should see the error. */
212void VG_(maybe_record_error) ( ThreadState* tst,
213 ErrorKind ekind, Addr a, Char* s, void* extra )
214{
njn810086f2002-11-14 12:42:47 +0000215 Error err;
216 Error* p;
217 Error* p_prev;
218 VgRes exe_res = Vg_MedRes;
219 static Bool is_first_shown_context = True;
220 static Bool stopping_message = False;
221 static Bool slowdown_message = False;
222 static Int vg_n_errs_shown = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000223
sewardjf2537be2002-04-24 21:03:47 +0000224 /* After M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN different errors have
225 been found, or M_VG_COLLECT_NO_ERRORS_AFTER_FOUND total errors
226 have been found, just refuse to collect any more. This stops
227 the burden of the error-management system becoming excessive in
228 extremely buggy programs, although it does make it pretty
229 pointless to continue the Valgrind run after this point. */
sewardj2e432902002-06-13 20:44:00 +0000230 if (VG_(clo_error_limit)
sewardj72f98ff2002-06-13 17:23:38 +0000231 && (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN
232 || vg_n_errs_found >= M_VG_COLLECT_NO_ERRORS_AFTER_FOUND)) {
sewardjde4a1d02002-03-22 01:27:54 +0000233 if (!stopping_message) {
234 VG_(message)(Vg_UserMsg, "");
sewardjf2537be2002-04-24 21:03:47 +0000235
236 if (vg_n_errs_shown >= M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN) {
237 VG_(message)(Vg_UserMsg,
238 "More than %d different errors detected. "
239 "I'm not reporting any more.",
240 M_VG_COLLECT_NO_ERRORS_AFTER_SHOWN );
241 } else {
242 VG_(message)(Vg_UserMsg,
243 "More than %d total errors detected. "
244 "I'm not reporting any more.",
245 M_VG_COLLECT_NO_ERRORS_AFTER_FOUND );
246 }
247
sewardjde4a1d02002-03-22 01:27:54 +0000248 VG_(message)(Vg_UserMsg,
sewardjf2537be2002-04-24 21:03:47 +0000249 "Final error counts will be inaccurate. Go fix your program!");
sewardj72f98ff2002-06-13 17:23:38 +0000250 VG_(message)(Vg_UserMsg,
sewardj2e432902002-06-13 20:44:00 +0000251 "Rerun with --error-limit=no to disable this cutoff. Note");
sewardj72f98ff2002-06-13 17:23:38 +0000252 VG_(message)(Vg_UserMsg,
njn25e49d8e72002-09-23 09:36:25 +0000253 "that errors may occur in your program without prior warning from");
sewardj72f98ff2002-06-13 17:23:38 +0000254 VG_(message)(Vg_UserMsg,
255 "Valgrind, because errors are no longer being displayed.");
sewardjde4a1d02002-03-22 01:27:54 +0000256 VG_(message)(Vg_UserMsg, "");
257 stopping_message = True;
258 }
259 return;
260 }
261
262 /* After M_VG_COLLECT_ERRORS_SLOWLY_AFTER different errors have
263 been found, be much more conservative about collecting new
264 ones. */
265 if (vg_n_errs_shown >= M_VG_COLLECT_ERRORS_SLOWLY_AFTER) {
njn25e49d8e72002-09-23 09:36:25 +0000266 exe_res = Vg_LowRes;
sewardjde4a1d02002-03-22 01:27:54 +0000267 if (!slowdown_message) {
268 VG_(message)(Vg_UserMsg, "");
269 VG_(message)(Vg_UserMsg,
270 "More than %d errors detected. Subsequent errors",
271 M_VG_COLLECT_ERRORS_SLOWLY_AFTER);
272 VG_(message)(Vg_UserMsg,
273 "will still be recorded, but in less detail than before.");
274 slowdown_message = True;
275 }
276 }
277
njn25e49d8e72002-09-23 09:36:25 +0000278 /* Build ourselves the error */
279 construct_error ( &err, tst, ekind, a, s, extra );
sewardjde4a1d02002-03-22 01:27:54 +0000280
281 /* First, see if we've got an error record matching this one. */
njn25e49d8e72002-09-23 09:36:25 +0000282 p = vg_errors;
sewardjde4a1d02002-03-22 01:27:54 +0000283 p_prev = NULL;
284 while (p != NULL) {
njn810086f2002-11-14 12:42:47 +0000285 if (eq_Error(exe_res, p, &err)) {
sewardjde4a1d02002-03-22 01:27:54 +0000286 /* Found it. */
287 p->count++;
288 if (p->supp != NULL) {
289 /* Deal correctly with suppressed errors. */
290 p->supp->count++;
291 vg_n_errs_suppressed++;
292 } else {
293 vg_n_errs_found++;
294 }
295
296 /* Move p to the front of the list so that future searches
297 for it are faster. */
298 if (p_prev != NULL) {
299 vg_assert(p_prev->next == p);
300 p_prev->next = p->next;
njn25e49d8e72002-09-23 09:36:25 +0000301 p->next = vg_errors;
302 vg_errors = p;
sewardjde4a1d02002-03-22 01:27:54 +0000303 }
304 return;
305 }
306 p_prev = p;
307 p = p->next;
308 }
309
310 /* Didn't see it. Copy and add. */
311
njn25e49d8e72002-09-23 09:36:25 +0000312 /* OK, we're really going to collect it. First make a copy,
313 because the error context is on the stack and will disappear shortly.
314 We can duplicate the main part ourselves, but use
njn810086f2002-11-14 12:42:47 +0000315 SK_(dup_extra_and_update) to duplicate the `extra' part (unless it's
njn25e49d8e72002-09-23 09:36:25 +0000316 NULL).
317
njn810086f2002-11-14 12:42:47 +0000318 SK_(dup_extra_and_update) can also update the `extra' part. This is
njn25e49d8e72002-09-23 09:36:25 +0000319 for when there are more details to fill in which take time to work out
320 but don't affect our earlier decision to include the error -- by
321 postponing those details until now, we avoid the extra work in the
njn810086f2002-11-14 12:42:47 +0000322 case where we ignore the error. Ugly.
njn25e49d8e72002-09-23 09:36:25 +0000323 */
njn810086f2002-11-14 12:42:47 +0000324 p = VG_(arena_malloc)(VG_AR_ERRORS, sizeof(Error));
njn25e49d8e72002-09-23 09:36:25 +0000325 *p = err;
njn810086f2002-11-14 12:42:47 +0000326 if (NULL != err.extra)
327 p->extra = SK_(dup_extra_and_update)(p);
sewardjde4a1d02002-03-22 01:27:54 +0000328
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 */,
419 p_min->where->eips[0], 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.
675*/
676static
677void get_objname_fnname ( Addr a,
678 Char* obj_buf, Int n_obj_buf,
679 Char* fun_buf, Int n_fun_buf )
680{
681 (void)VG_(get_objname) ( a, obj_buf, n_obj_buf );
682 (void)VG_(get_fnname_nodemangle)( a, fun_buf, n_fun_buf );
683}
684
685static __inline__
njn810086f2002-11-14 12:42:47 +0000686Bool supp_matches_error(Supp* su, Error* err)
njn25e49d8e72002-09-23 09:36:25 +0000687{
njn810086f2002-11-14 12:42:47 +0000688 switch (su->skind) {
njn25e49d8e72002-09-23 09:36:25 +0000689 case PThreadSupp:
njn810086f2002-11-14 12:42:47 +0000690 return (err->ekind == PThreadErr);
njn25e49d8e72002-09-23 09:36:25 +0000691 default:
692 if (VG_(needs).skin_errors) {
njn810086f2002-11-14 12:42:47 +0000693 return SK_(error_matches_suppression)(err, su);
njn25e49d8e72002-09-23 09:36:25 +0000694 } else {
695 VG_(printf)(
696 "\nUnhandled suppression type: %u. VG_(needs).skin_errors\n"
697 "probably needs to be set.\n",
njn810086f2002-11-14 12:42:47 +0000698 err->ekind);
njne427a662002-10-02 11:08:25 +0000699 VG_(skin_panic)("unhandled suppression type");
njn25e49d8e72002-09-23 09:36:25 +0000700 }
701 }
702}
703
704static __inline__
njn810086f2002-11-14 12:42:47 +0000705Bool supp_matches_callers(Supp* su, Char caller_obj[][M_VG_ERRTXT],
706 Char caller_fun[][M_VG_ERRTXT])
njn25e49d8e72002-09-23 09:36:25 +0000707{
708 Int i;
709
710 for (i = 0; su->caller[i] != NULL; i++) {
711 switch (su->caller_ty[i]) {
njn4ba5a792002-09-30 10:23:54 +0000712 case ObjName: if (VG_(string_match)(su->caller[i],
713 caller_obj[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000714 return False;
njn4ba5a792002-09-30 10:23:54 +0000715 case FunName: if (VG_(string_match)(su->caller[i],
716 caller_fun[i])) break;
njn25e49d8e72002-09-23 09:36:25 +0000717 return False;
njne427a662002-10-02 11:08:25 +0000718 default: VG_(skin_panic)("is_suppressible_error");
njn25e49d8e72002-09-23 09:36:25 +0000719 }
720 }
721
722 /* If we reach here, it's a match */
723 return True;
724}
sewardjde4a1d02002-03-22 01:27:54 +0000725
njn810086f2002-11-14 12:42:47 +0000726/* Does an error context match a suppression? ie is this a suppressible
727 error? If so, return a pointer to the Supp record, otherwise NULL.
njn25e49d8e72002-09-23 09:36:25 +0000728 Tries to minimise the number of symbol searches since they are expensive.
sewardjde4a1d02002-03-22 01:27:54 +0000729*/
njn810086f2002-11-14 12:42:47 +0000730static Supp* is_suppressible_error ( Error* err )
sewardjde4a1d02002-03-22 01:27:54 +0000731{
njn25e49d8e72002-09-23 09:36:25 +0000732 Int i;
sewardjde4a1d02002-03-22 01:27:54 +0000733
njn25e49d8e72002-09-23 09:36:25 +0000734 Char caller_obj[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
735 Char caller_fun[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
sewardjde4a1d02002-03-22 01:27:54 +0000736
njn810086f2002-11-14 12:42:47 +0000737 Supp* su;
sewardjde4a1d02002-03-22 01:27:54 +0000738
njn25e49d8e72002-09-23 09:36:25 +0000739 /* get_objname_fnname() writes the function name and object name if
740 it finds them in the debug info. so the strings in the suppression
741 file should match these.
sewardjde4a1d02002-03-22 01:27:54 +0000742 */
743
744 /* Initialise these strs so they are always safe to compare, even
njn25e49d8e72002-09-23 09:36:25 +0000745 if get_objname_fnname doesn't write anything to them. */
746 for (i = 0; i < VG_N_SUPP_CALLERS; i++)
747 caller_obj[i][0] = caller_fun[i][0] = 0;
sewardjde4a1d02002-03-22 01:27:54 +0000748
njn25e49d8e72002-09-23 09:36:25 +0000749 for (i = 0; i < VG_N_SUPP_CALLERS && i < VG_(clo_backtrace_size); i++) {
750 get_objname_fnname ( err->where->eips[i],
751 caller_obj[i], M_VG_ERRTXT,
752 caller_fun[i], M_VG_ERRTXT );
sewardjde4a1d02002-03-22 01:27:54 +0000753 }
754
755 /* See if the error context matches any suppression. */
756 for (su = vg_suppressions; su != NULL; su = su->next) {
njn25e49d8e72002-09-23 09:36:25 +0000757 if (supp_matches_error(su, err) &&
758 supp_matches_callers(su, caller_obj, caller_fun)) {
759 return su;
sewardjde4a1d02002-03-22 01:27:54 +0000760 }
sewardjde4a1d02002-03-22 01:27:54 +0000761 }
njn25e49d8e72002-09-23 09:36:25 +0000762 return NULL; /* no matches */
sewardjde4a1d02002-03-22 01:27:54 +0000763}
764
njn11cc9252002-10-07 14:42:59 +0000765#undef STREQ
766
sewardjde4a1d02002-03-22 01:27:54 +0000767/*--------------------------------------------------------------------*/
768/*--- end vg_errcontext.c ---*/
769/*--------------------------------------------------------------------*/