summaryrefslogtreecommitdiffstats
path: root/tools/heapcheck
diff options
context:
space:
mode:
authortimurrrr@chromium.org <timurrrr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-01 17:56:01 +0000
committertimurrrr@chromium.org <timurrrr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-01 17:56:01 +0000
commit741e1971f600c76a8ac47a548352578da981cbe0 (patch)
tree25a03e8c307b7b280f994dda4641f10d25b2b0d6 /tools/heapcheck
parentf2c20fa96403a8db390bc7790b67e1906dad179b (diff)
downloadchromium_src-741e1971f600c76a8ac47a548352578da981cbe0.zip
chromium_src-741e1971f600c76a8ac47a548352578da981cbe0.tar.gz
chromium_src-741e1971f600c76a8ac47a548352578da981cbe0.tar.bz2
Created tools/heapcheck with scripts for running heapchecker-enabled tests.
chrome_tests.sh -- a frontend for the buildbot slave to run the tests under heap leak checker. chrome_tests.py -- almost copied from the similar scripts in ../valgrind/ and ../purify/, invokes heapcheck_test.py for each target. heapcheck_test.py -- runs a single test, processes the logs and applies suppressions to the reports returned by the heap checker. heapcheck_std.sh -- a tiny wrapper for IO redirection. suppressions.py -- provides the support for Valgrind-style stack trace suppressions. This patch was originally prepared by Alexander Potapenko (cc'ed) as http://codereview.chromium.org/400010 TBR=dank,erikkay Review URL: http://codereview.chromium.org/455021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33451 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/heapcheck')
-rw-r--r--tools/heapcheck/chrome_tests.py449
-rw-r--r--tools/heapcheck/chrome_tests.sh8
-rw-r--r--tools/heapcheck/heapcheck_std.sh6
-rw-r--r--tools/heapcheck/heapcheck_test.py166
-rw-r--r--tools/heapcheck/suppressions.py171
5 files changed, 800 insertions, 0 deletions
diff --git a/tools/heapcheck/chrome_tests.py b/tools/heapcheck/chrome_tests.py
new file mode 100644
index 0000000..e9c1e8f
--- /dev/null
+++ b/tools/heapcheck/chrome_tests.py
@@ -0,0 +1,449 @@
+#!/usr/bin/python
+
+# Copyright (c) 2009 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+''' Runs various chrome tests through heapcheck_test.py.
+
+Most of this code is copied from ../valgrind/chrome_tests.py.
+TODO(glider): put common functions to a standalone module.
+'''
+
+import glob
+import logging
+import optparse
+import os
+import stat
+import sys
+
+import google.logging_utils
+import google.path_utils
+
+# Import the platform_utils up in the layout tests which have been modified to
+# work under non-Windows platforms instead of the ones that are in the
+# tools/python/google directory. (See chrome_tests.sh which sets PYTHONPATH
+# correctly.)
+#
+# TODO(erg): Copy/Move the relevant functions from the layout_package version
+# of platform_utils back up to google.platform_utils
+# package. http://crbug.com/6164
+import layout_package.path_utils
+
+import common
+import heapcheck_test
+
+class TestNotFound(Exception): pass
+
+def Dir2IsNewer(dir1, dir2):
+ if dir2 is None or not os.path.isdir(dir2):
+ return False
+ if dir1 is None or not os.path.isdir(dir1):
+ return True
+ return os.stat(dir2)[stat.ST_MTIME] > os.stat(dir1)[stat.ST_MTIME]
+
+def FindNewestDir(dirs):
+ newest_dir = None
+ for dir in dirs:
+ if Dir2IsNewer(newest_dir, dir):
+ newest_dir = dir
+ return newest_dir
+
+def File2IsNewer(file1, file2):
+ if file2 is None or not os.path.isfile(file2):
+ return False
+ if file1 is None or not os.path.isfile(file1):
+ return True
+ return os.stat(file2)[stat.ST_MTIME] > os.stat(file1)[stat.ST_MTIME]
+
+def FindDirContainingNewestFile(dirs, file):
+ """Searches for the directory containing the newest copy of |file|.
+
+ Args:
+ dirs: A list of paths to the directories to search among.
+ file: A string containing the file name to search.
+
+ Returns:
+ The string representing the the directory containing the newest copy of
+ |file|.
+
+ Raises:
+ IOError: |file| was not found.
+ """
+ newest_dir = None
+ newest_file = None
+ for dir in dirs:
+ the_file = os.path.join(dir, file)
+ if File2IsNewer(newest_file, the_file):
+ newest_dir = dir
+ newest_file = the_file
+ if newest_dir is None:
+ raise IOError("cannot find file %s anywhere, have you built it?" % file)
+ return newest_dir
+
+class ChromeTests(object):
+ '''This class is derived from the chrome_tests.py file in ../purify/.
+ '''
+
+ def __init__(self, options, args, test):
+ # The known list of tests.
+ # Recognise the original abbreviations as well as full executable names.
+ self._test_list = {
+ "base": self.TestBase, "base_unittests": self.TestBase,
+ "browser": self.TestBrowser, "browser_tests": self.TestBrowser,
+ "googleurl": self.TestGURL, "googleurl_unittests": self.TestGURL,
+ "ipc": self.TestIpc, "ipc_tests": self.TestIpc,
+ "layout": self.TestLayout, "layout_tests": self.TestLayout,
+ "media": self.TestMedia, "media_unittests": self.TestMedia,
+ "net": self.TestNet, "net_unittests": self.TestNet,
+ "printing": self.TestPrinting, "printing_unittests": self.TestPrinting,
+ "startup": self.TestStartup, "startup_tests": self.TestStartup,
+ "test_shell": self.TestTestShell, "test_shell_tests": self.TestTestShell,
+ "ui": self.TestUI, "ui_tests": self.TestUI,
+ "unit": self.TestUnit, "unit_tests": self.TestUnit,
+ "app": self.TestApp, "app_unittests": self.TestApp,
+ }
+
+ if test not in self._test_list:
+ raise TestNotFound("Unknown test: %s" % test)
+
+ self._options = options
+ self._args = args
+ self._test = test
+
+ script_dir = google.path_utils.ScriptDir()
+
+ # Compute the top of the tree (the "source dir") from the script dir (where
+ # this script lives). We assume that the script dir is in tools/heapcheck/
+ # relative to the top of the tree.
+ self._source_dir = os.path.dirname(os.path.dirname(script_dir))
+
+ # Since this path is used for string matching, make sure it's always
+ # an absolute Windows-style path.
+ self._source_dir = layout_package.path_utils.GetAbsolutePath(
+ self._source_dir)
+ heapcheck_test_script = os.path.join(script_dir, "heapcheck_test.py")
+ self._command_preamble = [heapcheck_test_script]
+
+ def _DefaultCommand(self, module, exe=None, heapcheck_test_args=None):
+ '''Generates the default command array that most tests will use.
+
+ Args:
+ module: The module name (corresponds to the dir in src/ where the test
+ data resides).
+ exe: The executable name.
+ heapcheck_test_args: additional arguments to append to the command line.
+ Returns:
+ A string with the command to run the test.
+ '''
+ module_dir = os.path.join(self._source_dir, module)
+
+ # We need multiple data dirs, the current script directory and a module
+ # specific one. The global suppression file lives in our directory, and the
+ # module specific suppression file lives with the module.
+ self._data_dirs = [google.path_utils.ScriptDir()]
+
+ if module == "chrome":
+ # Unfortunately, not all modules have the same directory structure.
+ self._data_dirs.append(os.path.join(module_dir, "test", "data",
+ "heapcheck"))
+ else:
+ self._data_dirs.append(os.path.join(module_dir, "data", "heapcheck"))
+
+ if not self._options.build_dir:
+ dirs = [
+ os.path.join(self._source_dir, "xcodebuild", "Debug"),
+ os.path.join(self._source_dir, "sconsbuild", "Debug"),
+ os.path.join(self._source_dir, "out", "Debug"),
+ ]
+ if exe:
+ self._options.build_dir = FindDirContainingNewestFile(dirs, exe)
+ else:
+ self._options.build_dir = FindNewestDir(dirs)
+
+ cmd = list(self._command_preamble)
+
+ if heapcheck_test_args != None:
+ for arg in heapcheck_test_args:
+ cmd.append(arg)
+ if exe:
+ cmd.append(os.path.join(self._options.build_dir, exe))
+ # Heapcheck runs tests slowly, so slow tests hurt more; show elapased time
+ # so we can find the slowpokes.
+ cmd.append("--gtest_print_time")
+ if self._options.gtest_repeat:
+ cmd.append("--gtest_repeat=%s" % self._options.gtest_repeat)
+ return cmd
+
+ def Suppressions(self):
+ '''Builds the list of available suppressions files.'''
+ ret = []
+ for directory in self._data_dirs:
+ suppression_file = os.path.join(directory, "suppressions.txt")
+ if os.path.exists(suppression_file):
+ ret.append(suppression_file)
+ suppression_file = os.path.join(directory, "suppressions_linux.txt")
+ if os.path.exists(suppression_file):
+ ret.append(suppression_file)
+ return ret
+
+ def Run(self):
+ '''Runs the test specified by command-line argument --test.'''
+ logging.info("running test %s" % (self._test))
+ return self._test_list[self._test]()
+
+ def _ReadGtestFilterFile(self, name, cmd):
+ '''Reads files which contain lists of tests to filter out with --gtest_filter
+ and appends the command-line option to |cmd|.
+
+ Args:
+ name: the test executable name.
+ cmd: the test running command line to be modified.
+ '''
+ filters = []
+ for directory in self._data_dirs:
+ gtest_filter_files = [
+ os.path.join(directory, name + ".gtest.txt"),
+ os.path.join(directory, name + ".gtest-heapcheck.txt"),
+ os.path.join(directory, name + ".gtest_linux.txt")]
+ for filename in gtest_filter_files:
+ if os.path.exists(filename):
+ logging.info("reading gtest filters from %s" % filename)
+ f = open(filename, 'r')
+ for line in f.readlines():
+ if line.startswith("#") or line.startswith("//") or line.isspace():
+ continue
+ line = line.rstrip()
+ filters.append(line)
+ gtest_filter = self._options.gtest_filter
+ if len(filters):
+ if gtest_filter:
+ gtest_filter += ":"
+ if gtest_filter.find("-") < 0:
+ gtest_filter += "-"
+ else:
+ gtest_filter = "-"
+ gtest_filter += ":".join(filters)
+ if gtest_filter:
+ cmd.append("--gtest_filter=%s" % gtest_filter)
+
+ def SimpleTest(self, module, name, heapcheck_test_args=None, cmd_args=None):
+ '''Builds the command line and runs the specified test.
+
+ Args:
+ module: The module name (corresponds to the dir in src/ where the test
+ data resides).
+ name: The executable name.
+ heapcheck_test_args: Additional command line args for heap checker.
+ cmd_args: Additional command line args for the test.
+ '''
+ cmd = self._DefaultCommand(module, name, heapcheck_test_args)
+ supp = self.Suppressions()
+ self._ReadGtestFilterFile(name, cmd)
+ if cmd_args:
+ cmd.extend(["--"])
+ cmd.extend(cmd_args)
+
+ # Sets LD_LIBRARY_PATH to the build folder so external libraries can be
+ # loaded.
+ os.putenv("LD_LIBRARY_PATH", self._options.build_dir)
+ return heapcheck_test.RunTool(cmd, supp)
+
+ def TestBase(self):
+ return self.SimpleTest("base", "base_unittests")
+
+ def TestBrowser(self):
+ return self.SimpleTest("chrome", "browser_tests")
+
+ def TestGURL(self):
+ return self.SimpleTest("chrome", "googleurl_unittests")
+
+ def TestMedia(self):
+ return self.SimpleTest("chrome", "media_unittests")
+
+ def TestPrinting(self):
+ return self.SimpleTest("chrome", "printing_unittests")
+
+ def TestIpc(self):
+ return self.SimpleTest("ipc", "ipc_tests")
+
+ def TestNet(self):
+ return self.SimpleTest("net", "net_unittests")
+
+ def TestStartup(self):
+ # We don't need the performance results, we're just looking for pointer
+ # errors, so set number of iterations down to the minimum.
+ os.putenv("STARTUP_TESTS_NUMCYCLES", "1")
+ logging.info("export STARTUP_TESTS_NUMCYCLES=1");
+ return self.SimpleTest("chrome", "startup_tests")
+
+ def TestTestShell(self):
+ return self.SimpleTest("webkit", "test_shell_tests")
+
+ def TestUnit(self):
+ return self.SimpleTest("chrome", "unit_tests")
+
+ def TestApp(self):
+ return self.SimpleTest("chrome", "app_unittests")
+
+ def TestUI(self):
+ return self.SimpleTest("chrome", "ui_tests",
+ cmd_args=[
+ "--ui-test-timeout=120000",
+ "--ui-test-action-timeout=80000",
+ "--ui-test-action-max-timeout=180000",
+ "--ui-test-terminate-timeout=60000"])
+
+ def TestLayoutChunk(self, chunk_num, chunk_size):
+ '''Runs tests [chunk_num*chunk_size .. (chunk_num+1)*chunk_size).
+
+ Wrap around to beginning of list at end. If chunk_size is zero, run all
+ tests in the list once. If a text file is given as argument, it is used as
+ the list of tests.
+ '''
+ # Build the ginormous commandline in 'cmd'.
+ # It's going to be roughly
+ # python heapcheck_test.py ... python run_webkit_tests.py ...
+ # but we'll use the --indirect flag to heapcheck_test.py
+ # to avoid heapchecking python.
+ # Start by building the heapcheck_test.py commandline.
+ cmd = self._DefaultCommand("webkit")
+
+ # Now build script_cmd, the run_webkits_tests.py commandline
+ # Store each chunk in its own directory so that we can find the data later
+ chunk_dir = os.path.join("layout", "chunk_%05d" % chunk_num)
+ test_shell = os.path.join(self._options.build_dir, "test_shell")
+ out_dir = os.path.join(google.path_utils.ScriptDir(), "latest")
+ out_dir = os.path.join(out_dir, chunk_dir)
+ if os.path.exists(out_dir):
+ old_files = glob.glob(os.path.join(out_dir, "*.txt"))
+ for f in old_files:
+ os.remove(f)
+ else:
+ os.makedirs(out_dir)
+
+ script = os.path.join(self._source_dir, "webkit", "tools", "layout_tests",
+ "run_webkit_tests.py")
+ script_cmd = ["python", script, "--run-singly", "-v",
+ "--noshow-results", "--time-out-ms=200000",
+ "--nocheck-sys-deps"]
+
+ # Pass build mode to run_webkit_tests.py. We aren't passed it directly,
+ # so parse it out of build_dir. run_webkit_tests.py can only handle
+ # the two values "Release" and "Debug".
+ # TODO(Hercules): unify how all our scripts pass around build mode
+ # (--mode / --target / --build_dir / --debug)
+ if self._options.build_dir.endswith("Debug"):
+ script_cmd.append("--debug");
+ if (chunk_size > 0):
+ script_cmd.append("--run-chunk=%d:%d" % (chunk_num, chunk_size))
+ if len(self._args):
+ # if the arg is a txt file, then treat it as a list of tests
+ if os.path.isfile(self._args[0]) and self._args[0][-4:] == ".txt":
+ script_cmd.append("--test-list=%s" % self._args[0])
+ else:
+ script_cmd.extend(self._args)
+ self._ReadGtestFilterFile("layout", script_cmd)
+
+ # Now run script_cmd with the wrapper in cmd
+ cmd.extend(["--"])
+ cmd.extend(script_cmd)
+ supp = self.Suppressions()
+ return heapcheck_test.RunTool(cmd, supp)
+
+ def TestLayout(self):
+ '''Runs the layout tests.'''
+ # A "chunk file" is maintained in the local directory so that each test
+ # runs a slice of the layout tests of size chunk_size that increments with
+ # each run. Since tests can be added and removed from the layout tests at
+ # any time, this is not going to give exact coverage, but it will allow us
+ # to continuously run small slices of the layout tests under purify rather
+ # than having to run all of them in one shot.
+ chunk_size = self._options.num_tests
+ if (chunk_size == 0):
+ return self.TestLayoutChunk(0, 0)
+ chunk_num = 0
+ chunk_file = os.path.join("heapcheck_layout_chunk.txt")
+
+ logging.info("Reading state from " + chunk_file)
+ try:
+ f = open(chunk_file)
+ if f:
+ str = f.read()
+ if len(str):
+ chunk_num = int(str)
+ # This should be enough so that we have a couple of complete runs
+ # of test data stored in the archive (although note that when we loop
+ # that we almost guaranteed won't be at the end of the test list)
+ if chunk_num > 10000:
+ chunk_num = 0
+ f.close()
+ except IOError, (errno, strerror):
+ logging.error("error reading from file %s (%d, %s)" % (chunk_file,
+ errno, strerror))
+ ret = self.TestLayoutChunk(chunk_num, chunk_size)
+
+ # Wait until after the test runs to completion to write out the new chunk
+ # number. This way, if the bot is killed, we'll start running again from
+ # the current chunk rather than skipping it.
+ logging.info("Saving state to " + chunk_file)
+ try:
+ f = open(chunk_file, "w")
+ chunk_num += 1
+ f.write("%d" % chunk_num)
+ f.close()
+ except IOError, (errno, strerror):
+ logging.error("error writing to file %s (%d, %s)" % (chunk_file, errno,
+ strerror))
+
+ # Since we're running small chunks of the layout tests, it's important to
+ # mark the ones that have errors in them. These won't be visible in the
+ # summary list for long, but will be useful for someone reviewing this bot.
+ return ret
+
+def _main(_):
+ parser = optparse.OptionParser("usage: %prog -b <dir> -t <test> "
+ "[-t <test> ...]")
+ parser.disable_interspersed_args()
+ parser.add_option("-b", "--build_dir",
+ help="the location of the output of the compiler output")
+ parser.add_option("-t", "--test", action="append",
+ help="which test to run")
+ parser.add_option("", "--gtest_filter",
+ help="additional arguments to --gtest_filter")
+ parser.add_option("", "--gtest_repeat",
+ help="argument for --gtest_repeat")
+ parser.add_option("-v", "--verbose", action="store_true", default=False,
+ help="verbose output - enable debug log messages")
+ # My machine can do about 120 layout tests/hour in release mode.
+ # Let's do 30 minutes worth per run.
+ # The CPU is mostly idle, so perhaps we can raise this when
+ # we figure out how to run them more efficiently.
+ parser.add_option("-n", "--num_tests", default=60, type="int",
+ help="for layout tests: # of subtests per run. 0 for all.")
+
+ options, args = parser.parse_args()
+
+ if options.verbose:
+ google.logging_utils.config_root(logging.DEBUG)
+ else:
+ google.logging_utils.config_root()
+
+ if not options.test or not len(options.test):
+ parser.error("--test not specified")
+
+ for t in options.test:
+ tests = ChromeTests(options, args, t)
+ ret = tests.Run()
+ if ret:
+ return ret
+ return 0
+
+
+if __name__ == "__main__":
+ if sys.platform == 'linux2':
+ ret = _main(sys.argv)
+ else:
+ logging.error("Heap checking works only on Linux at the moment.")
+ ret = 1
+ sys.exit(ret)
diff --git a/tools/heapcheck/chrome_tests.sh b/tools/heapcheck/chrome_tests.sh
new file mode 100644
index 0000000..2c23980
--- /dev/null
+++ b/tools/heapcheck/chrome_tests.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# Copyright (c) 2009 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+THISDIR=$(dirname "${0}")
+PYTHONPATH="${THISDIR}/../../webkit/tools/layout_tests/:${THISDIR}/../valgrind:${THISDIR}/../python" exec "${THISDIR}/chrome_tests.py" "${@}"
diff --git a/tools/heapcheck/heapcheck_std.sh b/tools/heapcheck/heapcheck_std.sh
new file mode 100644
index 0000000..012a169
--- /dev/null
+++ b/tools/heapcheck/heapcheck_std.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+# Copyright (c) 2009 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+exec "${@}" 2>heapcheck.log
diff --git a/tools/heapcheck/heapcheck_test.py b/tools/heapcheck/heapcheck_test.py
new file mode 100644
index 0000000..50ccfc5
--- /dev/null
+++ b/tools/heapcheck/heapcheck_test.py
@@ -0,0 +1,166 @@
+#!/usr/bin/python
+# Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# heapcheck_test.py
+
+"""Wrapper for running the test under heapchecker and analyzing the output."""
+
+import datetime
+import logging
+import os
+import re
+
+import common
+import google.path_utils
+import suppressions
+
+
+class HeapcheckWrapper(object):
+ TMP_FILE = 'heapcheck.log'
+
+ def __init__(self, supp_files):
+ self._mode = 'strict'
+ self._timeout = 1200
+ self._nocleanup_on_exit = False
+ self._suppressions = []
+ for fname in supp_files:
+ self._suppressions.extend(suppressions.ReadSuppressionsFromFile(fname))
+ if os.path.exists(self.TMP_FILE):
+ os.remove(self.TMP_FILE)
+
+ def PutEnvAndLog(self, env_name, env_value):
+ """Sets the env var |env_name| to |env_value| and writes to logging.info.
+ """
+ os.putenv(env_name, env_value)
+ logging.info('export %s=%s', env_name, env_value)
+
+ def Execute(self):
+ """Executes the app to be tested."""
+ logging.info('starting execution...')
+ proc = ['sh', google.path_utils.ScriptDir() + '/heapcheck_std.sh']
+ proc += self._args
+ self.PutEnvAndLog('G_SLICE', 'always-malloc')
+ self.PutEnvAndLog('NSS_DISABLE_ARENA_FREE_LIST', '1')
+ self.PutEnvAndLog('GTEST_DEATH_TEST_USE_FORK', '1')
+ self.PutEnvAndLog('HEAPCHECK', self._mode)
+
+ common.RunSubprocess(proc, self._timeout)
+
+ # Always return true, even if running the subprocess failed. We depend on
+ # Analyze to determine if the run was valid. (This behaviour copied from
+ # the purify_test.py script.)
+ return True
+
+ def Analyze(self, log_lines):
+ """Analyzes the app's output and applies suppressions to the reports.
+
+ Analyze() searches the logs for leak reports and tries to apply
+ suppressions to them. Unsuppressed reports and other log messages are
+ dumped as is.
+
+ Args:
+ log_lines: An iterator over the app's log lines.
+ Returns:
+ 1, if unsuppressed reports remain in the output, 0 otherwise.
+ """
+ leak_report = re.compile(
+ 'Leak of ([0-9]*) bytes in ([0-9]*) objects allocated from:')
+ stack_line = re.compile('\s*@\s*0x[0-9a-fA-F]*\s*(\S*)')
+ return_code = 0
+ # leak signature: [number of bytes, number of objects]
+ cur_leak_signature = None
+ cur_stack = []
+ cur_report = []
+ # Statistics grouped by suppression description:
+ # [hit count, bytes, objects].
+ used_suppressions = {}
+ for line in log_lines:
+ line = line.rstrip() # remove the trailing \n
+ match = stack_line.match(line)
+ if match:
+ cur_stack.append(match.groups()[0])
+ cur_report.append(line)
+ continue
+ else:
+ if cur_stack:
+ # Try to find the suppression that applies to the current leak stack.
+ description = ''
+ for supp in self._suppressions:
+ if supp.Match(cur_stack):
+ cur_stack = []
+ description = supp.description
+ break
+ if cur_stack:
+ # Print the report and set the return code to 1.
+ print ('Leak of %d bytes in %d objects allocated from:'
+ % tuple(cur_leak_signature))
+ print '\n'.join(cur_report)
+ return_code = 1
+ else:
+ # Update the suppressions histogram.
+ if description in used_suppressions:
+ hits, bytes, objects = used_suppressions[description]
+ hits += 1
+ bytes += cur_leak_signature[0]
+ objects += cur_leak_signature[1]
+ used_suppressions[description] = [hits, bytes, objects]
+ else:
+ used_suppressions[description] = [1] + cur_leak_signature
+ cur_stack = []
+ cur_report = []
+ cur_leak_signature = None
+ match = leak_report.match(line)
+ if match:
+ cur_leak_signature = map(int, match.groups())
+ else:
+ print line
+ # Print the list of suppressions used.
+ if used_suppressions:
+ print
+ print '-----------------------------------------------------'
+ print 'Suppressions used:'
+ print ' count bytes objects name'
+ histo = {}
+ for description in used_suppressions:
+ hits, bytes, objects = used_suppressions[description]
+ line = '%8d %8d %8d %s' % (hits, bytes, objects, description)
+ if hits in histo:
+ histo[hits].append(line)
+ else:
+ histo[hits] = [line]
+ keys = histo.keys()
+ keys.sort()
+ for count in keys:
+ for line in histo[count]:
+ print line
+ print '-----------------------------------------------------'
+
+ return return_code
+
+ def RunTestsAndAnalyze(self):
+ self.Execute()
+ log_file = file(self.TMP_FILE, 'r')
+ ret = self.Analyze(log_file)
+ log_file.close()
+ return ret
+
+ def Main(self, args):
+ self._args = args
+ start = datetime.datetime.now()
+ retcode = -1
+ retcode = self.RunTestsAndAnalyze()
+ end = datetime.datetime.now()
+ seconds = (end - start).seconds
+ hours = seconds / 3600
+ seconds %= 3600
+ minutes = seconds / 60
+ seconds %= 60
+ logging.info('elapsed time: %02d:%02d:%02d', hours, minutes, seconds)
+ return retcode
+
+
+def RunTool(args, supp_files):
+ tool = HeapcheckWrapper(supp_files)
+ return tool.Main(args[1:])
diff --git a/tools/heapcheck/suppressions.py b/tools/heapcheck/suppressions.py
new file mode 100644
index 0000000..e1b3498
--- /dev/null
+++ b/tools/heapcheck/suppressions.py
@@ -0,0 +1,171 @@
+#!/usr/bin/python
+# Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# suppressions.py
+
+"""Valgrind-style suppressions for heapchecker reports.
+
+Suppressions are defined as follows:
+
+# optional one-line comments anywhere in the suppressions file.
+{
+ Toolname:Errortype
+ Short description of the error.
+ fun:function_name
+ fun:wildcarded_fun*_name
+ # an ellipsis wildcards zero or more functions in a stack.
+ ...
+ fun:some_other_function_name
+}
+
+Note that only a 'fun:' prefix is allowed, i.e. we can't suppress objects and
+source files.
+
+If ran from the command line, suppressions.py does a self-test of the
+Suppression class.
+"""
+
+import re
+
+ELLIPSIS = '...'
+
+
+class Suppression(object):
+ """This class represents a single stack trace suppression.
+
+ Attributes:
+ type: A string representing the error type, e.g. Heapcheck:Leak.
+ description: A string representing the error description.
+ """
+
+ def __init__(self, kind, description, stack):
+ """Inits Suppression.
+
+ stack is a list of function names and/or wildcards.
+
+ Args:
+ kind:
+ description: Same as class attributes.
+ stack: A list of strings.
+ """
+ self.type = kind
+ self.description = description
+ self._stack = stack
+ re_line = ''
+ re_bucket = ''
+ for line in stack:
+ if line == ELLIPSIS:
+ re_line += re.escape(re_bucket)
+ re_bucket = ''
+ re_line += '(.*\n)*'
+ else:
+ for char in line:
+ if char == '*':
+ re_line += re.escape(re_bucket)
+ re_bucket = ''
+ re_line += '.*'
+ else: # there can't be any '\*'s in a stack trace
+ re_bucket += char
+ re_line += re.escape(re_bucket)
+ re_bucket = ''
+ re_line += '\n'
+ self._re = re.compile(re_line, re.MULTILINE)
+
+ def Match(self, report):
+ """Returns bool indicating whether the suppression matches the given report.
+
+ Args:
+ report: list of strings (function names).
+ Returns:
+ True if the suppression is not empty and matches the report.
+ """
+ if not self._stack:
+ return False
+ if self._re.match('\n'.join(report) + '\n'):
+ return True
+ else:
+ return False
+
+
+class SuppressionError(Exception):
+ def __init__(self, filename, line, report=''):
+ Exception.__init__(self, filename, line, report)
+ self._file = filename
+ self._line = line
+ self._report = report
+
+ def __str__(self):
+ return 'Error reading suppressions from "%s" (line %d): %s.' % (
+ self._file, self._line, self._report)
+
+
+def ReadSuppressionsFromFile(filename):
+ """Given a file, returns a list of suppressions."""
+ input_file = file(filename, 'r')
+ result = []
+ cur_descr = ''
+ cur_type = ''
+ cur_stack = []
+ nline = 0
+ try:
+ for line in input_file:
+ nline += 1
+ line = line.strip()
+ if line.startswith('#'):
+ continue
+ elif line.startswith('{'):
+ pass
+ elif line.startswith('}'):
+ result.append(Suppression(cur_type, cur_descr, cur_stack))
+ cur_descr = ''
+ cur_type = ''
+ cur_stack = []
+ elif not cur_type:
+ cur_type = line
+ continue
+ elif not cur_descr:
+ cur_descr = line
+ continue
+ elif line.startswith('fun:'):
+ line = line[4:]
+ cur_stack.append(line.strip())
+ elif line.startswith(ELLIPSIS):
+ cur_stack.append(ELLIPSIS)
+ else:
+ raise SuppressionError(filename, nline,
+ '"fun:function_name" or "..." expected')
+ except SuppressionError:
+ input_file.close()
+ raise
+ return result
+
+
+def MatchTest():
+ """Tests the Suppression.Match() capabilities."""
+
+ def GenSupp(*lines):
+ return Suppression('', '', list(lines))
+ empty = GenSupp()
+ assert not empty.Match([])
+ assert not empty.Match(['foo', 'bar'])
+ asterisk = GenSupp('*bar')
+ assert asterisk.Match(['foobar', 'foobaz'])
+ assert not asterisk.Match(['foobaz', 'foobar'])
+ ellipsis = GenSupp('...', 'foo')
+ assert ellipsis.Match(['foo', 'bar'])
+ assert ellipsis.Match(['bar', 'baz', 'foo'])
+ assert not ellipsis.Match(['bar', 'baz', 'bah'])
+ mixed = GenSupp('...', 'foo*', 'function')
+ assert mixed.Match(['foobar', 'foobaz', 'function'])
+ assert not mixed.Match(['foobar', 'blah', 'function'])
+ at_and_dollar = GenSupp('foo@GLIBC', 'bar@NOCANCEL')
+ assert at_and_dollar.Match(['foo@GLIBC', 'bar@NOCANCEL'])
+ re_chars = GenSupp('.*')
+ assert re_chars.Match(['.foobar'])
+ assert not re_chars.Match(['foobar'])
+ print 'PASS'
+
+if __name__ == '__main__':
+ MatchTest()