Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | #===- lib/asan/scripts/asan_symbolize.py -----------------------------------===# |
| 3 | # |
| 4 | # The LLVM Compiler Infrastructure |
| 5 | # |
| 6 | # This file is distributed under the University of Illinois Open Source |
| 7 | # License. See LICENSE.TXT for details. |
| 8 | # |
| 9 | #===------------------------------------------------------------------------===# |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 10 | import argparse |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 11 | import bisect |
Kostya Serebryany | 8e32db4 | 2013-02-18 08:02:16 +0000 | [diff] [blame] | 12 | import getopt |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 13 | import os |
| 14 | import re |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 15 | import subprocess |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 16 | import sys |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 17 | |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 18 | symbolizers = {} |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 19 | DEBUG = False |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 20 | demangle = False |
| 21 | binutils_prefix = None |
| 22 | sysroot_path = None |
| 23 | binary_name_filter = None |
| 24 | fix_filename_patterns = None |
| 25 | logfile = sys.stdin |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 26 | allow_system_symbolizer = True |
Alexander Potapenko | 970a9b9 | 2012-07-31 13:51:26 +0000 | [diff] [blame] | 27 | |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 28 | # FIXME: merge the code that calls fix_filename(). |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 29 | def fix_filename(file_name): |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 30 | if fix_filename_patterns: |
| 31 | for path_to_cut in fix_filename_patterns: |
| 32 | file_name = re.sub('.*' + path_to_cut, '', file_name) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 33 | file_name = re.sub('.*asan_[a-z_]*.cc:[0-9]*', '_asan_rtl_', file_name) |
| 34 | file_name = re.sub('.*crtstuff.c:0', '???:0', file_name) |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 35 | return file_name |
| 36 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 37 | def sysroot_path_filter(binary_name): |
| 38 | return sysroot_path + binary_name |
| 39 | |
| 40 | def guess_arch(addr): |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 41 | # Guess which arch we're running. 10 = len('0x') + 8 hex digits. |
| 42 | if len(addr) > 10: |
| 43 | return 'x86_64' |
| 44 | else: |
| 45 | return 'i386' |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 46 | |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 47 | class Symbolizer(object): |
| 48 | def __init__(self): |
| 49 | pass |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 50 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 51 | def symbolize(self, addr, binary, offset): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 52 | """Symbolize the given address (pair of binary and offset). |
| 53 | |
| 54 | Overriden in subclasses. |
| 55 | Args: |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 56 | addr: virtual address of an instruction. |
| 57 | binary: path to executable/shared object containing this instruction. |
| 58 | offset: instruction offset in the @binary. |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 59 | Returns: |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 60 | list of strings (one string for each inlined frame) describing |
| 61 | the code locations for this instruction (that is, function name, file |
| 62 | name, line and column numbers). |
| 63 | """ |
| 64 | return None |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 65 | |
| 66 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 67 | class LLVMSymbolizer(Symbolizer): |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 68 | def __init__(self, symbolizer_path, default_arch, system, dsym_hints=[]): |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 69 | super(LLVMSymbolizer, self).__init__() |
| 70 | self.symbolizer_path = symbolizer_path |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 71 | self.default_arch = default_arch |
| 72 | self.system = system |
| 73 | self.dsym_hints = dsym_hints |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 74 | self.pipe = self.open_llvm_symbolizer() |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 75 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 76 | def open_llvm_symbolizer(self): |
| 77 | cmd = [self.symbolizer_path, |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 78 | '--use-symbol-table=true', |
Kostya Serebryany | 8e32db4 | 2013-02-18 08:02:16 +0000 | [diff] [blame] | 79 | '--demangle=%s' % demangle, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 80 | '--functions=short', |
| 81 | '--inlining=true', |
| 82 | '--default-arch=%s' % self.default_arch] |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 83 | if self.system == 'Darwin': |
| 84 | for hint in self.dsym_hints: |
| 85 | cmd.append('--dsym-hint=%s' % hint) |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 86 | if DEBUG: |
| 87 | print ' '.join(cmd) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 88 | try: |
| 89 | result = subprocess.Popen(cmd, stdin=subprocess.PIPE, |
| 90 | stdout=subprocess.PIPE) |
| 91 | except OSError: |
| 92 | result = None |
| 93 | return result |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 94 | |
| 95 | def symbolize(self, addr, binary, offset): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 96 | """Overrides Symbolizer.symbolize.""" |
Alexey Samsonov | 7531731 | 2012-09-19 11:43:41 +0000 | [diff] [blame] | 97 | if not self.pipe: |
| 98 | return None |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 99 | result = [] |
| 100 | try: |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 101 | symbolizer_input = '"%s" %s' % (binary, offset) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 102 | if DEBUG: |
| 103 | print symbolizer_input |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 104 | print >> self.pipe.stdin, symbolizer_input |
| 105 | while True: |
| 106 | function_name = self.pipe.stdout.readline().rstrip() |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 107 | if not function_name: |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 108 | break |
| 109 | file_name = self.pipe.stdout.readline().rstrip() |
| 110 | file_name = fix_filename(file_name) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 111 | if (not function_name.startswith('??') or |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 112 | not file_name.startswith('??')): |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 113 | # Append only non-trivial frames. |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 114 | result.append('%s in %s %s' % (addr, function_name, |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 115 | file_name)) |
| 116 | except Exception: |
| 117 | result = [] |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 118 | if not result: |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 119 | result = None |
| 120 | return result |
| 121 | |
| 122 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 123 | def LLVMSymbolizerFactory(system, default_arch, dsym_hints=[]): |
Alexey Samsonov | 444a185 | 2012-10-08 13:11:18 +0000 | [diff] [blame] | 124 | symbolizer_path = os.getenv('LLVM_SYMBOLIZER_PATH') |
| 125 | if not symbolizer_path: |
Alexander Potapenko | 2bcd3b5 | 2013-10-31 16:08:09 +0000 | [diff] [blame] | 126 | symbolizer_path = os.getenv('ASAN_SYMBOLIZER_PATH') |
| 127 | if not symbolizer_path: |
| 128 | # Assume llvm-symbolizer is in PATH. |
| 129 | symbolizer_path = 'llvm-symbolizer' |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 130 | return LLVMSymbolizer(symbolizer_path, default_arch, system, dsym_hints) |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | class Addr2LineSymbolizer(Symbolizer): |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 134 | def __init__(self, binary): |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 135 | super(Addr2LineSymbolizer, self).__init__() |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 136 | self.binary = binary |
| 137 | self.pipe = self.open_addr2line() |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 138 | |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 139 | def open_addr2line(self): |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 140 | addr2line_tool = 'addr2line' |
| 141 | if binutils_prefix: |
| 142 | addr2line_tool = binutils_prefix + addr2line_tool |
| 143 | cmd = [addr2line_tool, '-f'] |
Kostya Serebryany | 8e32db4 | 2013-02-18 08:02:16 +0000 | [diff] [blame] | 144 | if demangle: |
| 145 | cmd += ['--demangle'] |
| 146 | cmd += ['-e', self.binary] |
Alexander Potapenko | 5cfa30e | 2012-08-15 13:58:24 +0000 | [diff] [blame] | 147 | if DEBUG: |
| 148 | print ' '.join(cmd) |
| 149 | return subprocess.Popen(cmd, |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 150 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 151 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 152 | def symbolize(self, addr, binary, offset): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 153 | """Overrides Symbolizer.symbolize.""" |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 154 | if self.binary != binary: |
| 155 | return None |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 156 | try: |
Alexander Potapenko | 5cfa30e | 2012-08-15 13:58:24 +0000 | [diff] [blame] | 157 | print >> self.pipe.stdin, offset |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 158 | function_name = self.pipe.stdout.readline().rstrip() |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 159 | file_name = self.pipe.stdout.readline().rstrip() |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 160 | except Exception: |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 161 | function_name = '' |
| 162 | file_name = '' |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 163 | file_name = fix_filename(file_name) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 164 | return ['%s in %s %s' % (addr, function_name, file_name)] |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 165 | |
| 166 | |
Alexander Potapenko | f21e025 | 2013-07-01 10:51:31 +0000 | [diff] [blame] | 167 | class UnbufferedLineConverter(object): |
| 168 | """ |
| 169 | Wrap a child process that responds to each line of input with one line of |
| 170 | output. Uses pty to trick the child into providing unbuffered output. |
| 171 | """ |
Alexander Potapenko | 2e6a1fb | 2013-07-04 14:21:49 +0000 | [diff] [blame] | 172 | def __init__(self, args, close_stderr=False): |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 173 | # Local imports so that the script can start on Windows. |
| 174 | import pty |
| 175 | import termios |
Alexander Potapenko | f21e025 | 2013-07-01 10:51:31 +0000 | [diff] [blame] | 176 | pid, fd = pty.fork() |
| 177 | if pid == 0: |
Alexander Potapenko | 2e6a1fb | 2013-07-04 14:21:49 +0000 | [diff] [blame] | 178 | # We're the child. Transfer control to command. |
| 179 | if close_stderr: |
| 180 | dev_null = os.open('/dev/null', 0) |
| 181 | os.dup2(dev_null, 2) |
Alexander Potapenko | f21e025 | 2013-07-01 10:51:31 +0000 | [diff] [blame] | 182 | os.execvp(args[0], args) |
| 183 | else: |
| 184 | # Disable echoing. |
| 185 | attr = termios.tcgetattr(fd) |
| 186 | attr[3] = attr[3] & ~termios.ECHO |
| 187 | termios.tcsetattr(fd, termios.TCSANOW, attr) |
| 188 | # Set up a file()-like interface to the child process |
| 189 | self.r = os.fdopen(fd, "r", 1) |
| 190 | self.w = os.fdopen(os.dup(fd), "w", 1) |
| 191 | |
| 192 | def convert(self, line): |
| 193 | self.w.write(line + "\n") |
| 194 | return self.readline() |
| 195 | |
| 196 | def readline(self): |
| 197 | return self.r.readline().rstrip() |
| 198 | |
| 199 | |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 200 | class DarwinSymbolizer(Symbolizer): |
| 201 | def __init__(self, addr, binary): |
| 202 | super(DarwinSymbolizer, self).__init__() |
| 203 | self.binary = binary |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 204 | self.arch = guess_arch(addr) |
Alexander Potapenko | f21e025 | 2013-07-01 10:51:31 +0000 | [diff] [blame] | 205 | self.open_atos() |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 206 | |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 207 | def open_atos(self): |
| 208 | if DEBUG: |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 209 | print 'atos -o %s -arch %s' % (self.binary, self.arch) |
| 210 | cmdline = ['atos', '-o', self.binary, '-arch', self.arch] |
Alexander Potapenko | 2e6a1fb | 2013-07-04 14:21:49 +0000 | [diff] [blame] | 211 | self.atos = UnbufferedLineConverter(cmdline, close_stderr=True) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 212 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 213 | def symbolize(self, addr, binary, offset): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 214 | """Overrides Symbolizer.symbolize.""" |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 215 | if self.binary != binary: |
| 216 | return None |
Alexander Potapenko | f21e025 | 2013-07-01 10:51:31 +0000 | [diff] [blame] | 217 | atos_line = self.atos.convert('0x%x' % int(offset, 16)) |
| 218 | while "got symbolicator for" in atos_line: |
| 219 | atos_line = self.atos.readline() |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 220 | # A well-formed atos response looks like this: |
| 221 | # foo(type1, type2) (in object.name) (filename.cc:80) |
| 222 | match = re.match('^(.*) \(in (.*)\) \((.*:\d*)\)$', atos_line) |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 223 | if DEBUG: |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 224 | print 'atos_line: ', atos_line |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 225 | if match: |
| 226 | function_name = match.group(1) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 227 | function_name = re.sub('\(.*?\)', '', function_name) |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 228 | file_name = fix_filename(match.group(3)) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 229 | return ['%s in %s %s' % (addr, function_name, file_name)] |
Alexander Potapenko | 0042411 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 230 | else: |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 231 | return ['%s in %s' % (addr, atos_line)] |
Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 232 | |
Alexander Potapenko | 970a9b9 | 2012-07-31 13:51:26 +0000 | [diff] [blame] | 233 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 234 | # Chain several symbolizers so that if one symbolizer fails, we fall back |
| 235 | # to the next symbolizer in chain. |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 236 | class ChainSymbolizer(Symbolizer): |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 237 | def __init__(self, symbolizer_list): |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 238 | super(ChainSymbolizer, self).__init__() |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 239 | self.symbolizer_list = symbolizer_list |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 240 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 241 | def symbolize(self, addr, binary, offset): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 242 | """Overrides Symbolizer.symbolize.""" |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 243 | for symbolizer in self.symbolizer_list: |
| 244 | if symbolizer: |
| 245 | result = symbolizer.symbolize(addr, binary, offset) |
| 246 | if result: |
| 247 | return result |
| 248 | return None |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 249 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 250 | def append_symbolizer(self, symbolizer): |
| 251 | self.symbolizer_list.append(symbolizer) |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 252 | |
| 253 | |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 254 | def BreakpadSymbolizerFactory(binary): |
| 255 | suffix = os.getenv('BREAKPAD_SUFFIX') |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 256 | if suffix: |
| 257 | filename = binary + suffix |
| 258 | if os.access(filename, os.F_OK): |
Alexander Potapenko | 5cfa30e | 2012-08-15 13:58:24 +0000 | [diff] [blame] | 259 | return BreakpadSymbolizer(filename) |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 260 | return None |
| 261 | |
| 262 | |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 263 | def SystemSymbolizerFactory(system, addr, binary): |
| 264 | if system == 'Darwin': |
| 265 | return DarwinSymbolizer(addr, binary) |
| 266 | elif system == 'Linux': |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 267 | return Addr2LineSymbolizer(binary) |
Alexander Potapenko | 897e89f | 2012-08-02 14:58:04 +0000 | [diff] [blame] | 268 | |
| 269 | |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 270 | class BreakpadSymbolizer(Symbolizer): |
| 271 | def __init__(self, filename): |
| 272 | super(BreakpadSymbolizer, self).__init__() |
| 273 | self.filename = filename |
| 274 | lines = file(filename).readlines() |
| 275 | self.files = [] |
| 276 | self.symbols = {} |
| 277 | self.address_list = [] |
| 278 | self.addresses = {} |
| 279 | # MODULE mac x86_64 A7001116478B33F18FF9BEDE9F615F190 t |
| 280 | fragments = lines[0].rstrip().split() |
| 281 | self.arch = fragments[2] |
| 282 | self.debug_id = fragments[3] |
| 283 | self.binary = ' '.join(fragments[4:]) |
| 284 | self.parse_lines(lines[1:]) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 285 | |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 286 | def parse_lines(self, lines): |
| 287 | cur_function_addr = '' |
| 288 | for line in lines: |
| 289 | fragments = line.split() |
| 290 | if fragments[0] == 'FILE': |
| 291 | assert int(fragments[1]) == len(self.files) |
| 292 | self.files.append(' '.join(fragments[2:])) |
| 293 | elif fragments[0] == 'PUBLIC': |
| 294 | self.symbols[int(fragments[1], 16)] = ' '.join(fragments[3:]) |
Alexander Potapenko | 5cfa30e | 2012-08-15 13:58:24 +0000 | [diff] [blame] | 295 | elif fragments[0] in ['CFI', 'STACK']: |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 296 | pass |
| 297 | elif fragments[0] == 'FUNC': |
| 298 | cur_function_addr = int(fragments[1], 16) |
Alexander Potapenko | 5cfa30e | 2012-08-15 13:58:24 +0000 | [diff] [blame] | 299 | if not cur_function_addr in self.symbols.keys(): |
| 300 | self.symbols[cur_function_addr] = ' '.join(fragments[4:]) |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 301 | else: |
| 302 | # Line starting with an address. |
| 303 | addr = int(fragments[0], 16) |
| 304 | self.address_list.append(addr) |
| 305 | # Tuple of symbol address, size, line, file number. |
| 306 | self.addresses[addr] = (cur_function_addr, |
| 307 | int(fragments[1], 16), |
| 308 | int(fragments[2]), |
| 309 | int(fragments[3])) |
| 310 | self.address_list.sort() |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 311 | |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 312 | def get_sym_file_line(self, addr): |
| 313 | key = None |
| 314 | if addr in self.addresses.keys(): |
| 315 | key = addr |
| 316 | else: |
| 317 | index = bisect.bisect_left(self.address_list, addr) |
| 318 | if index == 0: |
| 319 | return None |
| 320 | else: |
| 321 | key = self.address_list[index - 1] |
| 322 | sym_id, size, line_no, file_no = self.addresses[key] |
| 323 | symbol = self.symbols[sym_id] |
| 324 | filename = self.files[file_no] |
| 325 | if addr < key + size: |
| 326 | return symbol, filename, line_no |
| 327 | else: |
| 328 | return None |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 329 | |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 330 | def symbolize(self, addr, binary, offset): |
| 331 | if self.binary != binary: |
| 332 | return None |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 333 | res = self.get_sym_file_line(int(offset, 16)) |
| 334 | if res: |
| 335 | function_name, file_name, line_no = res |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 336 | result = ['%s in %s %s:%d' % ( |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 337 | addr, function_name, file_name, line_no)] |
Alexander Potapenko | 5cfa30e | 2012-08-15 13:58:24 +0000 | [diff] [blame] | 338 | print result |
| 339 | return result |
Alexander Potapenko | 8e39869 | 2012-08-02 13:59:23 +0000 | [diff] [blame] | 340 | else: |
| 341 | return None |
| 342 | |
| 343 | |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 344 | class SymbolizationLoop(object): |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 345 | def __init__(self, binary_name_filter=None, dsym_hint_producer=None): |
| 346 | if sys.platform == 'win32': |
| 347 | # ASan on Windows uses dbghelp.dll to symbolize in-process, which works |
| 348 | # even in sandboxed processes. Nothing needs to be done here. |
| 349 | self.process_line = self.process_line_echo |
| 350 | else: |
| 351 | # Used by clients who may want to supply a different binary name. |
| 352 | # E.g. in Chrome several binaries may share a single .dSYM. |
| 353 | self.binary_name_filter = binary_name_filter |
| 354 | self.dsym_hint_producer = dsym_hint_producer |
| 355 | self.system = os.uname()[0] |
| 356 | if self.system not in ['Linux', 'Darwin', 'FreeBSD']: |
| 357 | raise Exception('Unknown system') |
| 358 | self.llvm_symbolizers = {} |
| 359 | self.last_llvm_symbolizer = None |
| 360 | self.dsym_hints = set([]) |
| 361 | self.frame_no = 0 |
| 362 | self.process_line = self.process_line_posix |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 363 | |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 364 | def symbolize_address(self, addr, binary, offset): |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 365 | # On non-Darwin (i.e. on platforms without .dSYM debug info) always use |
| 366 | # a single symbolizer binary. |
| 367 | # On Darwin, if the dsym hint producer is present: |
| 368 | # 1. check whether we've seen this binary already; if so, |
| 369 | # use |llvm_symbolizers[binary]|, which has already loaded the debug |
| 370 | # info for this binary (might not be the case for |
| 371 | # |last_llvm_symbolizer|); |
| 372 | # 2. otherwise check if we've seen all the hints for this binary already; |
| 373 | # if so, reuse |last_llvm_symbolizer| which has the full set of hints; |
| 374 | # 3. otherwise create a new symbolizer and pass all currently known |
| 375 | # .dSYM hints to it. |
| 376 | if not binary in self.llvm_symbolizers: |
| 377 | use_new_symbolizer = True |
| 378 | if self.system == 'Darwin' and self.dsym_hint_producer: |
| 379 | dsym_hints_for_binary = set(self.dsym_hint_producer(binary)) |
| 380 | use_new_symbolizer = bool(dsym_hints_for_binary - self.dsym_hints) |
| 381 | self.dsym_hints |= dsym_hints_for_binary |
| 382 | if self.last_llvm_symbolizer and not use_new_symbolizer: |
| 383 | self.llvm_symbolizers[binary] = self.last_llvm_symbolizer |
| 384 | else: |
| 385 | self.last_llvm_symbolizer = LLVMSymbolizerFactory( |
| 386 | self.system, guess_arch(addr), self.dsym_hints) |
| 387 | self.llvm_symbolizers[binary] = self.last_llvm_symbolizer |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 388 | # Use the chain of symbolizers: |
| 389 | # Breakpad symbolizer -> LLVM symbolizer -> addr2line/atos |
| 390 | # (fall back to next symbolizer if the previous one fails). |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 391 | if not binary in symbolizers: |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 392 | symbolizers[binary] = ChainSymbolizer( |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 393 | [BreakpadSymbolizerFactory(binary), self.llvm_symbolizers[binary]]) |
Alexey Samsonov | 63e4df4 | 2012-09-19 08:49:53 +0000 | [diff] [blame] | 394 | result = symbolizers[binary].symbolize(addr, binary, offset) |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 395 | if result is None: |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 396 | if not allow_system_symbolizer: |
| 397 | raise Exception('Failed to launch or use llvm-symbolizer.') |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 398 | # Initialize system symbolizer only if other symbolizers failed. |
| 399 | symbolizers[binary].append_symbolizer( |
| 400 | SystemSymbolizerFactory(self.system, addr, binary)) |
| 401 | result = symbolizers[binary].symbolize(addr, binary, offset) |
| 402 | # The system symbolizer must produce some result. |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 403 | assert result |
Alexander Potapenko | b2546c4 | 2012-09-26 12:12:41 +0000 | [diff] [blame] | 404 | return result |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 405 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 406 | def get_symbolized_lines(self, symbolized_lines): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 407 | if not symbolized_lines: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 408 | return [self.current_line] |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 409 | else: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 410 | result = [] |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 411 | for symbolized_frame in symbolized_lines: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 412 | result.append(' #%s %s' % (str(self.frame_no), symbolized_frame.rstrip())) |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 413 | self.frame_no += 1 |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 414 | return result |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 415 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 416 | def process_logfile(self): |
Alexander Potapenko | ce31aa7 | 2012-09-26 13:16:42 +0000 | [diff] [blame] | 417 | self.frame_no = 0 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 418 | for line in logfile: |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 419 | processed = self.process_line(line) |
| 420 | print '\n'.join(processed) |
| 421 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 422 | def process_line_echo(self, line): |
| 423 | return [line.rstrip()] |
| 424 | |
| 425 | def process_line_posix(self, line): |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 426 | self.current_line = line.rstrip() |
| 427 | #0 0x7f6e35cf2e45 (/blah/foo.so+0x11fe45) |
| 428 | stack_trace_line_format = ( |
| 429 | '^( *#([0-9]+) *)(0x[0-9a-f]+) *\((.*)\+(0x[0-9a-f]+)\)') |
| 430 | match = re.match(stack_trace_line_format, line) |
| 431 | if not match: |
| 432 | return [self.current_line] |
| 433 | if DEBUG: |
| 434 | print line |
| 435 | _, frameno_str, addr, binary, offset = match.groups() |
| 436 | if frameno_str == '0': |
| 437 | # Assume that frame #0 is the first frame of new stack trace. |
| 438 | self.frame_no = 0 |
| 439 | original_binary = binary |
| 440 | if self.binary_name_filter: |
| 441 | binary = self.binary_name_filter(binary) |
| 442 | symbolized_line = self.symbolize_address(addr, binary, offset) |
| 443 | if not symbolized_line: |
| 444 | if original_binary != binary: |
| 445 | symbolized_line = self.symbolize_address(addr, binary, offset) |
| 446 | return self.get_symbolized_lines(symbolized_line) |
Alexander Potapenko | 970a9b9 | 2012-07-31 13:51:26 +0000 | [diff] [blame] | 447 | |
| 448 | |
| 449 | if __name__ == '__main__': |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 450 | parser = argparse.ArgumentParser( |
| 451 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 452 | description='ASan symbolization script', |
| 453 | epilog='Example of use:\n' |
| 454 | 'asan_symbolize.py -c "$HOME/opt/cross/bin/arm-linux-gnueabi-" ' |
| 455 | '-s "$HOME/SymbolFiles" < asan.log') |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 456 | parser.add_argument('path_to_cut', nargs='*', |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 457 | help='pattern to be cut from the result file path ') |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 458 | parser.add_argument('-d','--demangle', action='store_true', |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 459 | help='demangle function names') |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 460 | parser.add_argument('-s', metavar='SYSROOT', |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 461 | help='set path to sysroot for sanitized binaries') |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 462 | parser.add_argument('-c', metavar='CROSS_COMPILE', |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 463 | help='set prefix for binutils') |
| 464 | parser.add_argument('-l','--logfile', default=sys.stdin, |
| 465 | type=argparse.FileType('r'), |
| 466 | help='set log file name to parse, default is stdin') |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 467 | args = parser.parse_args() |
| 468 | if args.path_to_cut: |
| 469 | fix_filename_patterns = args.path_to_cut |
| 470 | if args.demangle: |
| 471 | demangle = True |
| 472 | if args.s: |
| 473 | binary_name_filter = sysroot_path_filter |
| 474 | sysroot_path = args.s |
| 475 | if args.c: |
| 476 | binutils_prefix = args.c |
| 477 | if args.logfile: |
| 478 | logfile = args.logfile |
| 479 | else: |
| 480 | logfile = sys.stdin |
| 481 | loop = SymbolizationLoop(binary_name_filter) |
| 482 | loop.process_logfile() |