2010-06-26 01:47:19 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-06-26 01:47:19 +04:00
|
|
|
|
2014-09-02 09:56:05 +04:00
|
|
|
# This script uses breakpad symbols to post-process the entries produced by
|
|
|
|
# NS_FormatCodeAddress(), which on TBPL builds often lack a file name and a
|
|
|
|
# line number (and on Linux even the symbol is often bad).
|
|
|
|
|
2010-06-26 01:47:19 +04:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
2015-10-22 02:37:42 +03:00
|
|
|
import subprocess
|
2010-06-26 01:47:19 +04:00
|
|
|
import bisect
|
|
|
|
|
2015-10-22 02:37:42 +03:00
|
|
|
here = os.path.dirname(__file__)
|
|
|
|
|
2010-06-26 01:47:19 +04:00
|
|
|
def prettyFileName(name):
|
|
|
|
if name.startswith("../") or name.startswith("..\\"):
|
|
|
|
# dom_quickstubs.cpp and many .h files show up with relative paths that are useless
|
|
|
|
# and/or don't correspond to the layout of the source tree.
|
|
|
|
return os.path.basename(name) + ":"
|
|
|
|
elif name.startswith("hg:"):
|
2013-03-11 22:21:53 +04:00
|
|
|
bits = name.split(":")
|
|
|
|
if len(bits) == 4:
|
|
|
|
(junk, repo, path, rev) = bits
|
|
|
|
# We could construct an hgweb URL with /file/ or /annotate/, like this:
|
|
|
|
# return "http://%s/annotate/%s/%s#l" % (repo, rev, path)
|
|
|
|
return path + ":"
|
2010-06-26 01:47:19 +04:00
|
|
|
return name + ":"
|
|
|
|
|
2013-03-11 22:21:53 +04:00
|
|
|
class SymbolFile:
|
2010-06-26 01:47:19 +04:00
|
|
|
def __init__(self, fn):
|
|
|
|
addrs = [] # list of addresses, which will be sorted once we're done initializing
|
|
|
|
funcs = {} # hash: address --> (function name + possible file/line)
|
|
|
|
files = {} # hash: filenum (string) --> prettified filename ready to have a line number appended
|
|
|
|
with open(fn) as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.rstrip()
|
2016-05-25 20:01:52 +03:00
|
|
|
# https://chromium.googlesource.com/breakpad/breakpad/+/master/docs/symbol_files.md
|
2010-06-26 01:47:19 +04:00
|
|
|
if line.startswith("FUNC "):
|
|
|
|
# FUNC <address> <size> <stack_param_size> <name>
|
2016-05-06 01:29:17 +03:00
|
|
|
bits = line.split(None, 4)
|
|
|
|
if len(bits) < 5:
|
|
|
|
bits.append('unnamed_function')
|
|
|
|
(junk, rva, size, ss, name) = bits
|
2010-06-26 01:47:19 +04:00
|
|
|
rva = int(rva,16)
|
|
|
|
funcs[rva] = name
|
|
|
|
addrs.append(rva)
|
|
|
|
lastFuncName = name
|
|
|
|
elif line.startswith("PUBLIC "):
|
|
|
|
# PUBLIC <address> <stack_param_size> <name>
|
|
|
|
(junk, rva, ss, name) = line.split(None, 3)
|
|
|
|
rva = int(rva,16)
|
|
|
|
funcs[rva] = name
|
|
|
|
addrs.append(rva)
|
|
|
|
elif line.startswith("FILE "):
|
|
|
|
# FILE <number> <name>
|
|
|
|
(junk, filenum, name) = line.split(None, 2)
|
|
|
|
files[filenum] = prettyFileName(name)
|
|
|
|
elif line[0] in "0123456789abcdef":
|
|
|
|
# This is one of the "line records" corresponding to the last FUNC record
|
|
|
|
# <address> <size> <line> <filenum>
|
|
|
|
(rva, size, line, filenum) = line.split(None)
|
|
|
|
rva = int(rva,16)
|
|
|
|
file = files[filenum]
|
|
|
|
name = lastFuncName + " [" + file + line + "]"
|
|
|
|
funcs[rva] = name
|
|
|
|
addrs.append(rva)
|
|
|
|
# skip everything else
|
|
|
|
#print "Loaded %d functions from symbol file %s" % (len(funcs), os.path.basename(fn))
|
|
|
|
self.addrs = sorted(addrs)
|
|
|
|
self.funcs = funcs
|
2013-03-11 22:21:53 +04:00
|
|
|
|
2010-06-26 01:47:19 +04:00
|
|
|
def addrToSymbol(self, address):
|
|
|
|
i = bisect.bisect(self.addrs, address) - 1
|
|
|
|
if i > 0:
|
|
|
|
#offset = address - self.addrs[i]
|
|
|
|
return self.funcs[self.addrs[i]]
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2015-10-22 02:37:42 +03:00
|
|
|
def findIdForPath(path):
|
|
|
|
"""Finds the breakpad id for the object file at the given path."""
|
|
|
|
# We should always be packaged with a "fileid" executable.
|
|
|
|
fileid_exe = os.path.join(here, 'fileid')
|
|
|
|
if not os.path.isfile(fileid_exe):
|
2015-10-29 23:25:03 +03:00
|
|
|
fileid_exe = fileid_exe + '.exe'
|
|
|
|
if not os.path.isfile(fileid_exe):
|
|
|
|
raise Exception("Could not find fileid executable in %s" % here)
|
|
|
|
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
for suffix in ('.exe', '.dll'):
|
|
|
|
if os.path.isfile(path + suffix):
|
|
|
|
path = path + suffix
|
2015-10-22 02:37:42 +03:00
|
|
|
try:
|
|
|
|
return subprocess.check_output([fileid_exe, path]).rstrip()
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
raise Exception("Error getting fileid for %s: %s" %
|
|
|
|
(path, e.output))
|
|
|
|
|
|
|
|
def guessSymbolFile(full_path, symbolsDir):
|
2010-06-26 01:47:19 +04:00
|
|
|
"""Guess a symbol file based on an object file's basename, ignoring the path and UUID."""
|
2015-10-22 02:37:42 +03:00
|
|
|
fn = os.path.basename(full_path)
|
2010-06-26 01:47:19 +04:00
|
|
|
d1 = os.path.join(symbolsDir, fn)
|
2015-10-29 23:25:03 +03:00
|
|
|
root, _ = os.path.splitext(fn)
|
|
|
|
if os.path.exists(os.path.join(symbolsDir, root) + '.pdb'):
|
|
|
|
d1 = os.path.join(symbolsDir, root) + '.pdb'
|
|
|
|
fn = root
|
2010-06-26 01:47:19 +04:00
|
|
|
if not os.path.exists(d1):
|
2015-10-29 23:25:03 +03:00
|
|
|
return None
|
2010-06-26 01:47:19 +04:00
|
|
|
uuids = os.listdir(d1)
|
|
|
|
if len(uuids) == 0:
|
|
|
|
raise Exception("Missing symbol file for " + fn)
|
|
|
|
if len(uuids) > 1:
|
2015-10-22 02:37:42 +03:00
|
|
|
uuid = findIdForPath(full_path)
|
|
|
|
else:
|
|
|
|
uuid = uuids[0]
|
|
|
|
return os.path.join(d1, uuid, fn + ".sym")
|
2010-06-26 01:47:19 +04:00
|
|
|
|
|
|
|
parsedSymbolFiles = {}
|
2013-03-11 22:21:53 +04:00
|
|
|
def getSymbolFile(file, symbolsDir):
|
2010-06-26 01:47:19 +04:00
|
|
|
p = None
|
|
|
|
if not file in parsedSymbolFiles:
|
|
|
|
symfile = guessSymbolFile(file, symbolsDir)
|
|
|
|
if symfile:
|
2013-03-11 22:21:53 +04:00
|
|
|
p = SymbolFile(symfile)
|
2010-06-26 01:47:19 +04:00
|
|
|
else:
|
|
|
|
p = None
|
|
|
|
parsedSymbolFiles[file] = p
|
|
|
|
else:
|
|
|
|
p = parsedSymbolFiles[file]
|
2013-03-11 22:21:53 +04:00
|
|
|
return p
|
|
|
|
|
|
|
|
def addressToSymbol(file, address, symbolsDir):
|
|
|
|
p = getSymbolFile(file, symbolsDir)
|
2010-06-26 01:47:19 +04:00
|
|
|
if p:
|
|
|
|
return p.addrToSymbol(address)
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2014-09-02 09:56:05 +04:00
|
|
|
# Matches lines produced by NS_FormatCodeAddress().
|
|
|
|
line_re = re.compile("^(.*#\d+: )(.+)\[(.+) \+(0x[0-9A-Fa-f]+)\](.*)$")
|
2010-06-26 01:47:19 +04:00
|
|
|
|
|
|
|
def fixSymbols(line, symbolsDir):
|
|
|
|
result = line_re.match(line)
|
|
|
|
if result is not None:
|
2014-09-02 09:56:05 +04:00
|
|
|
(before, fn, file, address, after) = result.groups()
|
2010-06-26 01:47:19 +04:00
|
|
|
address = int(address, 16)
|
|
|
|
symbol = addressToSymbol(file, address, symbolsDir)
|
|
|
|
if not symbol:
|
|
|
|
symbol = "%s + 0x%x" % (os.path.basename(file), address)
|
|
|
|
return before + symbol + after + "\n"
|
|
|
|
else:
|
2013-03-11 22:21:53 +04:00
|
|
|
return line
|
2010-06-26 01:47:19 +04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
symbolsDir = sys.argv[1]
|
|
|
|
for line in iter(sys.stdin.readline, ''):
|
|
|
|
print fixSymbols(line, symbolsDir),
|