OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 # memcheck_analyze.py | 6 # memcheck_analyze.py |
7 | 7 |
8 ''' Given a valgrind XML file, parses errors and uniques them.''' | 8 ''' Given a valgrind XML file, parses errors and uniques them.''' |
9 | 9 |
10 import gdb_helper | 10 import gdb_helper |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 return False | 304 return False |
305 if '</valgrindoutput>' in line: | 305 if '</valgrindoutput>' in line: |
306 # valgrind often has garbage after </valgrindoutput> upon crash | 306 # valgrind often has garbage after </valgrindoutput> upon crash |
307 f.truncate() | 307 f.truncate() |
308 return True | 308 return True |
309 | 309 |
310 class MemcheckAnalyze: | 310 class MemcheckAnalyze: |
311 ''' Given a set of Valgrind XML files, parse all the errors out of them, | 311 ''' Given a set of Valgrind XML files, parse all the errors out of them, |
312 unique them and output the results.''' | 312 unique them and output the results.''' |
313 | 313 |
314 SANITY_TEST_SUPPRESSION = "Memcheck sanity test" | 314 SANITY_TEST_SUPPRESSIONS = [ |
| 315 "Memcheck sanity test (array deleted without []).", |
| 316 "Memcheck sanity test (malloc/read left).", |
| 317 "Memcheck sanity test (malloc/read right).", |
| 318 "Memcheck sanity test (malloc/write left).", |
| 319 "Memcheck sanity test (malloc/write right).", |
| 320 "Memcheck sanity test (memory leak).", |
| 321 "Memcheck sanity test (new/read left).", |
| 322 "Memcheck sanity test (new/read right).", |
| 323 "Memcheck sanity test (new/write left).", |
| 324 "Memcheck sanity test (new/write right).", |
| 325 "Memcheck sanity test (single element deleted with []).", |
| 326 "Memcheck sanity test (write after delete).", |
| 327 "Memcheck sanity test (write after free).", |
| 328 ] |
| 329 |
315 def __init__(self, source_dir, files, show_all_leaks=False, use_gdb=False): | 330 def __init__(self, source_dir, files, show_all_leaks=False, use_gdb=False): |
316 '''Reads in a set of files. | 331 '''Reads in a set of files. |
317 | 332 |
318 Args: | 333 Args: |
319 source_dir: Path to top of source tree for this build | 334 source_dir: Path to top of source tree for this build |
320 files: A list of filenames. | 335 files: A list of filenames. |
321 show_all_leaks: whether to show even less important leaks | 336 show_all_leaks: whether to show even less important leaks |
322 ''' | 337 ''' |
323 | 338 |
324 # Beyond the detailed errors parsed by ValgrindError above, | 339 # Beyond the detailed errors parsed by ValgrindError above, |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 | 447 |
433 def Report(self, check_sanity=False): | 448 def Report(self, check_sanity=False): |
434 if self._parse_failed: | 449 if self._parse_failed: |
435 logging.error("FAIL! Couldn't parse Valgrind output file") | 450 logging.error("FAIL! Couldn't parse Valgrind output file") |
436 return -2 | 451 return -2 |
437 | 452 |
438 is_sane = False | 453 is_sane = False |
439 print "-----------------------------------------------------" | 454 print "-----------------------------------------------------" |
440 print "Suppressions used:" | 455 print "Suppressions used:" |
441 print " count name" | 456 print " count name" |
| 457 remaining_sanity_supp = set(MemcheckAnalyze.SANITY_TEST_SUPPRESSIONS) |
442 for item in sorted(self._suppcounts.items(), key=lambda (k,v): (v,k)): | 458 for item in sorted(self._suppcounts.items(), key=lambda (k,v): (v,k)): |
443 print "%7s %s" % (item[1], item[0]) | 459 print "%7s %s" % (item[1], item[0]) |
444 if item[0].startswith(MemcheckAnalyze.SANITY_TEST_SUPPRESSION): | 460 if item[0] in remaining_sanity_supp: |
445 is_sane = True | 461 remaining_sanity_supp.remove(item[0]) |
| 462 if len(remaining_sanity_supp) == 0: |
| 463 is_sane = True |
446 print "-----------------------------------------------------" | 464 print "-----------------------------------------------------" |
447 sys.stdout.flush() | 465 sys.stdout.flush() |
448 | 466 |
449 retcode = 0 | 467 retcode = 0 |
450 if self._errors: | 468 if self._errors: |
451 logging.error("FAIL! There were %s errors: " % len(self._errors)) | 469 logging.error("FAIL! There were %s errors: " % len(self._errors)) |
452 | 470 |
453 global TheAddressTable | 471 global TheAddressTable |
454 if TheAddressTable != None: | 472 if TheAddressTable != None: |
455 TheAddressTable.ResolveAll() | 473 TheAddressTable.ResolveAll() |
456 | 474 |
457 for error in self._errors: | 475 for error in self._errors: |
458 logging.error(error) | 476 logging.error(error) |
459 | 477 |
460 retcode = -1 | 478 retcode = -1 |
461 | 479 |
462 # Report tool's insanity even if there were errors. | 480 # Report tool's insanity even if there were errors. |
463 if check_sanity and not is_sane: | 481 if check_sanity and not is_sane: |
464 logging.error("FAIL! Sanity check failed!") | 482 logging.error("FAIL! Sanity check failed!") |
| 483 logging.info("The following test errors were not handled: ") |
| 484 for supp in remaining_sanity_supp: |
| 485 logging.info(" " + supp) |
465 retcode = -3 | 486 retcode = -3 |
466 | 487 |
467 if retcode != 0: | 488 if retcode != 0: |
468 return retcode | 489 return retcode |
469 | 490 |
470 logging.info("PASS! No errors found!") | 491 logging.info("PASS! No errors found!") |
471 return 0 | 492 return 0 |
472 | 493 |
473 def _main(): | 494 def _main(): |
474 '''For testing only. The MemcheckAnalyze class should be imported instead.''' | 495 '''For testing only. The MemcheckAnalyze class should be imported instead.''' |
475 retcode = 0 | 496 retcode = 0 |
476 parser = optparse.OptionParser("usage: %prog [options] <files to analyze>") | 497 parser = optparse.OptionParser("usage: %prog [options] <files to analyze>") |
477 parser.add_option("", "--source_dir", | 498 parser.add_option("", "--source_dir", |
478 help="path to top of source tree for this build" | 499 help="path to top of source tree for this build" |
479 "(used to normalize source paths in baseline)") | 500 "(used to normalize source paths in baseline)") |
480 | 501 |
481 (options, args) = parser.parse_args() | 502 (options, args) = parser.parse_args() |
482 if not len(args) >= 1: | 503 if not len(args) >= 1: |
483 parser.error("no filename specified") | 504 parser.error("no filename specified") |
484 filenames = args | 505 filenames = args |
485 | 506 |
486 analyzer = MemcheckAnalyze(options.source_dir, filenames, use_gdb=True) | 507 analyzer = MemcheckAnalyze(options.source_dir, filenames, use_gdb=True) |
487 retcode = analyzer.Report() | 508 retcode = analyzer.Report() |
488 | 509 |
489 sys.exit(retcode) | 510 sys.exit(retcode) |
490 | 511 |
491 if __name__ == "__main__": | 512 if __name__ == "__main__": |
492 _main() | 513 _main() |
OLD | NEW |