michael@0: #!/usr/bin/env python
michael@0:
michael@0: # This Source Code Form is subject to the terms of the Mozilla Public
michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0:
michael@0: from __future__ import with_statement
michael@0:
michael@0: import sys
michael@0: import os
michael@0: import re
michael@0: import bisect
michael@0:
michael@0: def prettyFileName(name):
michael@0: if name.startswith("../") or name.startswith("..\\"):
michael@0: # dom_quickstubs.cpp and many .h files show up with relative paths that are useless
michael@0: # and/or don't correspond to the layout of the source tree.
michael@0: return os.path.basename(name) + ":"
michael@0: elif name.startswith("hg:"):
michael@0: bits = name.split(":")
michael@0: if len(bits) == 4:
michael@0: (junk, repo, path, rev) = bits
michael@0: # We could construct an hgweb URL with /file/ or /annotate/, like this:
michael@0: # return "http://%s/annotate/%s/%s#l" % (repo, rev, path)
michael@0: return path + ":"
michael@0: return name + ":"
michael@0:
michael@0: class SymbolFile:
michael@0: def __init__(self, fn):
michael@0: addrs = [] # list of addresses, which will be sorted once we're done initializing
michael@0: funcs = {} # hash: address --> (function name + possible file/line)
michael@0: files = {} # hash: filenum (string) --> prettified filename ready to have a line number appended
michael@0: with open(fn) as f:
michael@0: for line in f:
michael@0: line = line.rstrip()
michael@0: # http://code.google.com/p/google-breakpad/wiki/SymbolFiles
michael@0: if line.startswith("FUNC "):
michael@0: # FUNC
michael@0: (junk, rva, size, ss, name) = line.split(None, 4)
michael@0: rva = int(rva,16)
michael@0: funcs[rva] = name
michael@0: addrs.append(rva)
michael@0: lastFuncName = name
michael@0: elif line.startswith("PUBLIC "):
michael@0: # PUBLIC
michael@0: (junk, rva, ss, name) = line.split(None, 3)
michael@0: rva = int(rva,16)
michael@0: funcs[rva] = name
michael@0: addrs.append(rva)
michael@0: elif line.startswith("FILE "):
michael@0: # FILE
michael@0: (junk, filenum, name) = line.split(None, 2)
michael@0: files[filenum] = prettyFileName(name)
michael@0: elif line[0] in "0123456789abcdef":
michael@0: # This is one of the "line records" corresponding to the last FUNC record
michael@0: #
michael@0: (rva, size, line, filenum) = line.split(None)
michael@0: rva = int(rva,16)
michael@0: file = files[filenum]
michael@0: name = lastFuncName + " [" + file + line + "]"
michael@0: funcs[rva] = name
michael@0: addrs.append(rva)
michael@0: # skip everything else
michael@0: #print "Loaded %d functions from symbol file %s" % (len(funcs), os.path.basename(fn))
michael@0: self.addrs = sorted(addrs)
michael@0: self.funcs = funcs
michael@0:
michael@0: def addrToSymbol(self, address):
michael@0: i = bisect.bisect(self.addrs, address) - 1
michael@0: if i > 0:
michael@0: #offset = address - self.addrs[i]
michael@0: return self.funcs[self.addrs[i]]
michael@0: else:
michael@0: return ""
michael@0:
michael@0: def guessSymbolFile(fn, symbolsDir):
michael@0: """Guess a symbol file based on an object file's basename, ignoring the path and UUID."""
michael@0: fn = os.path.basename(fn)
michael@0: d1 = os.path.join(symbolsDir, fn)
michael@0: if not os.path.exists(d1):
michael@0: fn = fn + ".pdb"
michael@0: d1 = os.path.join(symbolsDir, fn)
michael@0: if not os.path.exists(d1):
michael@0: return None
michael@0: uuids = os.listdir(d1)
michael@0: if len(uuids) == 0:
michael@0: raise Exception("Missing symbol file for " + fn)
michael@0: if len(uuids) > 1:
michael@0: raise Exception("Ambiguous symbol file for " + fn)
michael@0: if fn.endswith(".pdb"):
michael@0: fn = fn[:-4]
michael@0: return os.path.join(d1, uuids[0], fn + ".sym")
michael@0:
michael@0: parsedSymbolFiles = {}
michael@0: def getSymbolFile(file, symbolsDir):
michael@0: p = None
michael@0: if not file in parsedSymbolFiles:
michael@0: symfile = guessSymbolFile(file, symbolsDir)
michael@0: if symfile:
michael@0: p = SymbolFile(symfile)
michael@0: else:
michael@0: p = None
michael@0: parsedSymbolFiles[file] = p
michael@0: else:
michael@0: p = parsedSymbolFiles[file]
michael@0: return p
michael@0:
michael@0: def addressToSymbol(file, address, symbolsDir):
michael@0: p = getSymbolFile(file, symbolsDir)
michael@0: if p:
michael@0: return p.addrToSymbol(address)
michael@0: else:
michael@0: return ""
michael@0:
michael@0: line_re = re.compile("^(.*) ?\[([^ ]*) \+(0x[0-9A-F]{1,16})\](.*)$")
michael@0: balance_tree_re = re.compile("^([ \|0-9-]*)")
michael@0:
michael@0: def fixSymbols(line, symbolsDir):
michael@0: result = line_re.match(line)
michael@0: if result is not None:
michael@0: # before allows preservation of balance trees
michael@0: # after allows preservation of counts
michael@0: (before, file, address, after) = result.groups()
michael@0: address = int(address, 16)
michael@0: # throw away the bad symbol, but keep balance tree structure
michael@0: before = balance_tree_re.match(before).groups()[0]
michael@0: symbol = addressToSymbol(file, address, symbolsDir)
michael@0: if not symbol:
michael@0: symbol = "%s + 0x%x" % (os.path.basename(file), address)
michael@0: return before + symbol + after + "\n"
michael@0: else:
michael@0: return line
michael@0:
michael@0: if __name__ == "__main__":
michael@0: symbolsDir = sys.argv[1]
michael@0: for line in iter(sys.stdin.readline, ''):
michael@0: print fixSymbols(line, symbolsDir),