Kostya Serebryany | 019b76f | 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 | #===------------------------------------------------------------------------===# |
| 10 | import os |
| 11 | import re |
| 12 | import sys |
| 13 | import string |
| 14 | import subprocess |
| 15 | |
| 16 | pipes = {} |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 17 | filetypes = {} |
| 18 | DEBUG=False |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 19 | |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 20 | def fix_filename(file_name): |
| 21 | for path_to_cut in sys.argv[1:]: |
| 22 | file_name = re.sub(".*" + path_to_cut, "", file_name) |
| 23 | file_name = re.sub(".*asan_[a-z_]*.cc:[0-9]*", "_asan_rtl_", file_name) |
| 24 | file_name = re.sub(".*crtstuff.c:0", "???:0", file_name) |
| 25 | return file_name |
| 26 | |
| 27 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 28 | # TODO(glider): need some refactoring here |
| 29 | def symbolize_addr2line(line): |
| 30 | #0 0x7f6e35cf2e45 (/blah/foo.so+0x11fe45) |
| 31 | match = re.match('^( *#([0-9]+) *0x[0-9a-f]+) *\((.*)\+(0x[0-9a-f]+)\)', line) |
| 32 | if match: |
| 33 | frameno = match.group(2) |
| 34 | binary = match.group(3) |
| 35 | addr = match.group(4) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 36 | if not pipes.has_key(binary): |
| 37 | pipes[binary] = subprocess.Popen(["addr2line", "-f", "-e", binary], |
| 38 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 39 | p = pipes[binary] |
| 40 | try: |
| 41 | print >>p.stdin, addr |
| 42 | function_name = p.stdout.readline().rstrip() |
| 43 | file_name = p.stdout.readline().rstrip() |
| 44 | except: |
| 45 | function_name = "" |
| 46 | file_name = "" |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 47 | file_name = fix_filename(file_name) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 48 | |
| 49 | print match.group(1), "in", function_name, file_name |
| 50 | else: |
| 51 | print line.rstrip() |
| 52 | |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 53 | |
| 54 | def get_macho_filetype(binary): |
| 55 | if not filetypes.has_key(binary): |
| 56 | otool_pipe = subprocess.Popen(["otool", "-Vh", binary], |
| 57 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 58 | otool_line = "".join(otool_pipe.stdout.readlines()) |
| 59 | for t in ["DYLIB", "EXECUTE"]: |
| 60 | if t in otool_line: |
| 61 | filetypes[binary] = t |
| 62 | otool_pipe.stdin.close() |
| 63 | return filetypes[binary] |
| 64 | |
| 65 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 66 | def symbolize_atos(line): |
| 67 | #0 0x7f6e35cf2e45 (/blah/foo.so+0x11fe45) |
| 68 | match = re.match('^( *#([0-9]+) *)(0x[0-9a-f]+) *\((.*)\+(0x[0-9a-f]+)\)', line) |
| 69 | if match: |
| 70 | #print line |
| 71 | prefix = match.group(1) |
| 72 | frameno = match.group(2) |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 73 | orig_addr = match.group(3) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 74 | binary = match.group(4) |
| 75 | offset = match.group(5) |
Alexey Samsonov | 3735faa | 2012-07-19 15:07:26 +0000 | [diff] [blame^] | 76 | addr = orig_addr |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 77 | load_addr = hex(int(orig_addr, 16) - int(offset, 16)) |
| 78 | filetype = get_macho_filetype(binary) |
| 79 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 80 | if not pipes.has_key(binary): |
Alexander Potapenko | 1f397fb | 2012-01-24 10:44:44 +0000 | [diff] [blame] | 81 | # Guess which arch we're running. 10 = len("0x") + 8 hex digits. |
| 82 | if len(addr) > 10: |
| 83 | arch = "x86_64" |
| 84 | else: |
| 85 | arch = "i386" |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 86 | |
| 87 | if filetype == "DYLIB": |
| 88 | load_addr = "0x0" |
| 89 | if DEBUG: |
| 90 | print "atos -o %s -arch %s -l %s" % (binary, arch, load_addr) |
Alexander Potapenko | 628b349 | 2012-07-19 12:01:07 +0000 | [diff] [blame] | 91 | cmd = ["atos", "-o", binary, "-arch", arch, "-l", load_addr] |
| 92 | pipes[binary] = subprocess.Popen(cmd, |
| 93 | stdin=subprocess.PIPE, |
| 94 | stdout=subprocess.PIPE, |
| 95 | stderr=subprocess.PIPE) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | p = pipes[binary] |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 97 | if filetype == "DYLIB": |
| 98 | print >>p.stdin, "%s" % offset |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 99 | else: |
Alexander Potapenko | 1f397fb | 2012-01-24 10:44:44 +0000 | [diff] [blame] | 100 | print >>p.stdin, "%s" % addr |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 101 | # TODO(glider): it's more efficient to make a batch atos run for each binary. |
| 102 | p.stdin.close() |
| 103 | atos_line = p.stdout.readline().rstrip() |
Alexander Potapenko | 02a7162 | 2012-01-26 17:06:50 +0000 | [diff] [blame] | 104 | # A well-formed atos response looks like this: |
| 105 | # foo(type1, type2) (in object.name) (filename.cc:80) |
| 106 | match = re.match('^(.*) \(in (.*)\) \((.*:\d*)\)$', atos_line) |
| 107 | #print "atos_line: ", atos_line |
| 108 | if match: |
| 109 | function_name = match.group(1) |
| 110 | function_name = re.sub("\(.*?\)", "", function_name) |
| 111 | file_name = fix_filename(match.group(3)) |
| 112 | print "%s%s in %s %s" % (prefix, addr, function_name, file_name) |
| 113 | else: |
| 114 | print "%s%s in %s" % (prefix, addr, atos_line) |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 115 | del pipes[binary] |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 116 | else: |
| 117 | print line.rstrip() |
| 118 | |
| 119 | system = os.uname()[0] |
| 120 | if system in ['Linux', 'Darwin']: |
| 121 | for line in sys.stdin: |
| 122 | if system == 'Linux': |
| 123 | symbolize_addr2line(line) |
| 124 | elif system == 'Darwin': |
| 125 | symbolize_atos(line) |
| 126 | else: |
| 127 | print 'Unknown system: ', system |