Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / ncv / ncval_stubout.py
blob4878d45e7c0964ec31a552ce10dfeabde0fd03d7
1 #!/usr/bin/env python
3 # Force an executable or library to pass the validator by overwriting
4 # any non-validating instruction with hlt instructions.
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 def fix_up_file(obj_file):
17 proc = subprocess.Popen([ncval, obj_file], stdout=subprocess.PIPE)
18 regexp = re.compile(
19 "VALIDATOR: [0-9a-f]+ \((?P<file_addr>[0-9a-f]+):(?P<size>[0-9a-f]+)\)")
20 rewrites = []
21 for line in proc.stdout:
22 match = regexp.match(line)
23 if match is not None:
24 sys.stdout.write("%s: %s" % (obj_file, line))
25 offset = int(match.group("file_addr"), 16)
26 size = int(match.group("size"), 16)
27 rewrites.append((offset, size))
28 fh = os.fdopen(os.open(obj_file, os.O_WRONLY), "w")
29 for offset, size in rewrites:
30 fh.seek(offset)
31 # Overwrite with hlt instructions
32 fh.write("\xf4" * size)
33 fh.close()
36 def main(args):
37 for filename in args:
38 fix_up_file(filename)
41 if __name__ == "__main__":
42 main(sys.argv[1:])