ncval-annotate: Go faster: use --start-address with objdump
[nativeclient.git] / ncv / ncval-annotate.py
blobe711d59b294561c6692c850ab2a78db9773b010c
2 # Annotate the output of ncval with line numbers, taken from debugging
3 # information using binutils' addr2line.
5 import linecache
6 import os
7 import re
8 import subprocess
9 import sys
12 ncval = os.path.join(os.path.dirname(__file__),
13 "../scons-out/dbg-linux/staging/ncval")
16 objdump_regexp = re.compile(r"\s*([0-9a-f]+):")
19 def main(args):
20 obj_file = args[0]
22 def disassemble_address(addr):
23 proc = subprocess.Popen(
24 ["objdump", "-d", obj_file, "--start-address", "0x" + addr,
25 "--stop-address", "0x%x" % (int(addr, 16) + 16)],
26 stdout=subprocess.PIPE)
27 for line in proc.stdout:
28 match = objdump_regexp.match(line)
29 if match is not None:
30 sys.stdout.write(line)
31 break
33 def decode_address(addr):
34 proc = subprocess.Popen(
35 ["addr2line", "-f", "-i", "-e", obj_file, addr],
36 stdout=subprocess.PIPE)
37 for info in proc.stdout:
38 sys.stdout.write(" %s: %s" % (addr, info))
39 if ":" in info:
40 filename, lineno = info.split(":", 1)
41 src_line = linecache.getline(filename, int(lineno))
42 if src_line != "":
43 sys.stdout.write(" " + src_line.lstrip())
45 proc = subprocess.Popen([ncval, obj_file], stdout=subprocess.PIPE)
46 regexp = re.compile("[0-9a-f]{3,}")
47 for line in proc.stdout:
48 sys.stdout.write(line)
49 if line.startswith("VALIDATOR:"):
50 match = regexp.search(line)
51 if match is not None:
52 addr = match.group()
53 disassemble_address(addr)
54 decode_address(addr)
57 if __name__ == "__main__":
58 main(sys.argv[1:])