Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / valgrind / asan / asan_symbolize.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 from third_party import asan_symbolize
8
9 import os
10 import sys
11
12 class LineBuffered(object):
13   """Disable buffering on a file object."""
14   def __init__(self, stream):
15     self.stream = stream
16
17   def write(self, data):
18     self.stream.write(data)
19     if '\n' in data:
20       self.stream.flush()
21
22   def __getattr__(self, attr):
23     return getattr(self.stream, attr)
24
25
26 def disable_buffering():
27   """Makes this process and child processes stdout unbuffered."""
28   if not os.environ.get('PYTHONUNBUFFERED'):
29     # Since sys.stdout is a C++ object, it's impossible to do
30     # sys.stdout.write = lambda...
31     sys.stdout = LineBuffered(sys.stdout)
32     os.environ['PYTHONUNBUFFERED'] = 'x'
33
34
35 def set_symbolizer_path():
36   """Set the path to the llvm-symbolize binary in the Chromium source tree."""
37   if not os.environ.get('LLVM_SYMBOLIZER_PATH'):
38     script_dir = os.path.dirname(os.path.abspath(__file__))
39     # Assume this script resides three levels below src/ (i.e.
40     # src/tools/valgrind/asan/).
41     src_root = os.path.join(script_dir, "..", "..", "..")
42     symbolizer_path = os.path.join(src_root, 'third_party',
43         'llvm-build', 'Release+Asserts', 'bin', 'llvm-symbolizer')
44     assert(os.path.isfile(symbolizer_path))
45     os.environ['LLVM_SYMBOLIZER_PATH'] = os.path.abspath(symbolizer_path)
46
47
48 def main():
49   disable_buffering()
50   set_symbolizer_path()
51   asan_symbolize.demangle = True
52   asan_symbolize.fix_filename_patterns = sys.argv[1:]
53   asan_symbolize.logfile = sys.stdin
54   loop = asan_symbolize.SymbolizationLoop()
55   loop.process_logfile()
56
57 if __name__ == '__main__':
58   main()