Add toolchain_test.py to naclbuild.py
[nacl-build.git] / debugger.py
blobc863e29a3eb49665e43927bc594c38550de2a081
2 # This is a wrapper for sel_ldr that annotates the backtrace it
3 # outputs with source code info.
5 import re
6 import subprocess
7 import sys
10 def addr2line(obj_file, addr, indent=""):
11 proc = subprocess.Popen(["addr2line", "-f", "-e", obj_file, "%x" % addr],
12 stdout=subprocess.PIPE)
13 for line in proc.stdout:
14 print indent + line,
15 assert proc.wait() == 0
18 def main(argv):
19 executable = argv[0]
20 maps = [{"base": 0x20000, "offset": 0x20000, "size": 0x2d000,
21 "path": "install-stubout/lib/ld-linux.so.2", "file": "ld.so"}]
22 # TODO: don't assume executable's load address and size
23 maps.append({"base": 0x1000000, "offset": 0, "size": 0x10000,
24 "path": executable, "file": executable})
26 def match():
27 last_path = "?"
28 last_file = "?"
29 while True:
30 line = yield
31 m = re.search("trying file=(.*)", line)
32 if m is not None:
33 last_path = m.group(1)
35 m = re.search("file=(\S+).*generating link map", line)
36 if m is not None:
37 last_file = m.group(1)
39 m = re.search("base: (\S+)\s+size: (\S+)", line)
40 if m is not None:
41 # base value assumes this is an ET_DYN object.
42 info = {"base": int(m.group(1), 0),
43 "offset": int(m.group(1), 0),
44 "size": int(m.group(2), 0),
45 "path": last_path,
46 "file": last_file}
47 maps.append(info)
49 args = ["sel_ldr",
50 "-E", "LD_DEBUG=all",
51 "-d",
52 "install-stubout/lib/ld-linux.so.2",
53 "--", "--library-path", "install-stubout/lib",
54 ] + argv
55 proc = subprocess.Popen(
56 args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
57 matcher = match()
58 matcher.send(None)
60 def decode_addr(addr):
61 for obj in maps:
62 if obj["base"] <= addr < obj["base"] + obj["size"]:
63 offset = addr - obj["offset"]
64 print " in %s: offset=%x" % (obj["path"], offset)
65 addr2line(obj["path"], offset, " ")
67 for line in proc.stdout:
68 print line,
69 matcher.send(line)
70 m = re.search("code_addr=(\S+)", line)
71 if m is not None:
72 decode_addr(int(m.group(1), 16))
74 print "exit status:", proc.wait()
75 print "libraries loaded:"
76 fmt = " %(base)08x-%(end)08x size=%(size)08x file=%(file)s path=%(path)s"
77 for obj in sorted(maps, key=lambda obj: obj["base"]):
78 obj["end"] = obj["base"] + obj["size"]
79 print fmt % obj
82 if __name__ == "__main__":
83 main(sys.argv[1:])