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