Compute the real addresses and offsets for the json crashlog test.
"termination" : {"reason":"Namespace SIGNAL, Code 0xb","signal":"Segmentation fault: 11","byProc":"exc handler","code":11,"namespace":"SIGNAL","byPid":2187,"flags":0},
"asi" : ["dyld2 mode"],
"extMods" : {"caller":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"system":{"thread_create":0,"thread_set_state":0,"task_for_pid":2067},"targeted":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"warnings":0},
- "threads" : [{"triggered":true,"id":22172,"queue":"com.apple.main-thread","frames":[[0,16240],[0,16265],[0,16292],[1,87601]]}],
+ "threads" : [{"triggered":true,"id":22172,"queue":"com.apple.main-thread","frames":[[0,@foo@],[0,@bar@],[0,@main@],[1,87601]]}],
"threadState" : {
"r13" : 0,
"rax" : 0,
"usedImages" : [
[
"@UUID@",
- 0,
+ 4294967296,
"P"
],
[
# RUN: %clang_host -g %S/Inputs/test.c -o %t.out
# RUN: cp %S/Inputs/a.out.ips %t.crash
-# RUN: python %S/patch-crashlog.py %t.out %t.crash
+# RUN: python %S/patch-crashlog.py --binary %t.out --crashlog %t.crash --offsets '{"main":20, "bar":9, "foo":16}' --json
# RUN: %lldb %t.out -o 'command script import lldb.macosx.crashlog' -o 'crashlog %t.crash' 2>&1 | FileCheck %s
# CHECK: Thread[0] EXC_BAD_ACCESS (SIGSEGV) (KERN_INVALID_ADDRESS at 0x00000000)
# CHECK: [ 0] {{.*}}out`foo + 16 at test.c
# CHECK: [ 1] {{.*}}out`bar + 8 at test.c
# CHECK: [ 2] {{.*}}out`main + 19 at test.c
-# CHECK: [ 3] {{.*}}start
import re
import subprocess
import sys
+import argparse
class CrashLogPatcher:
SYMBOL_REGEX = re.compile(r'^([0-9a-fA-F]+) T _(.*)$')
UUID_REGEX = re.compile(r'UUID: ([-0-9a-fA-F]+) \(([^\(]+)\) .*')
- def __init__(self, data, binary, offsets):
+ def __init__(self, data, binary, offsets, json):
self.data = data
self.binary = binary
self.offsets = offsets
+ self.json = json
def patch_executable(self):
self.data = self.data.replace("@EXEC@", self.binary)
if symbol in self.offsets:
patch_addr = int(m.group(1), 16) + int(
self.offsets[symbol])
- self.data = self.data.replace("@{}@".format(symbol),
- str(hex(patch_addr)))
+ if self.json:
+ patch_addr = patch_addr - 0x100000000
+ representation = int
+ else:
+ representation = hex
+ self.data = self.data.replace(
+ "@{}@".format(symbol), str(representation(patch_addr)))
if __name__ == '__main__':
- binary = sys.argv[1]
- crashlog = sys.argv[2]
- offsets = json.loads(sys.argv[3]) if len(sys.argv) > 3 else None
+ parser = argparse.ArgumentParser(description='Crashlog Patcher')
+ parser.add_argument('--binary', required=True)
+ parser.add_argument('--crashlog', required=True)
+ parser.add_argument('--offsets', required=True)
+ parser.add_argument('--json', default=False, action='store_true')
+ args = parser.parse_args()
- with open(crashlog, 'r') as file:
+ offsets = json.loads(args.offsets)
+
+ with open(args.crashlog, 'r') as file:
data = file.read()
- p = CrashLogPatcher(data, binary, offsets)
+ p = CrashLogPatcher(data, args.binary, offsets, args.json)
p.patch_executable()
p.patch_uuid()
p.patch_addresses()
- with open(crashlog, 'w') as file:
+ with open(args.crashlog, 'w') as file:
file.write(p.data)
# RUN: %clang_host -g %S/Inputs/test.c -o %t.out
# RUN: cp %S/Inputs/a.out.crash %t.crash
-# RUN: python %S/patch-crashlog.py %t.out %t.crash '{"main":20, "bar":9, "foo":16}'
+# RUN: python %S/patch-crashlog.py --binary %t.out --crashlog %t.crash --offsets '{"main":20, "bar":9, "foo":16}'
# RUN: %lldb %t.out -o 'command script import lldb.macosx.crashlog' -o 'crashlog %t.crash' 2>&1 | FileCheck %s
# CHECK: Thread[0] EXC_BAD_ACCESS (SIGSEGV) (KERN_INVALID_ADDRESS at 0x0000000000000000)
# CHECK: [ 0] {{.*}}out`foo + 16 at test.c
# CHECK: [ 1] {{.*}}out`bar + 8 at test.c
# CHECK: [ 2] {{.*}}out`main + 19 at test.c
-# CHECK: [ 3] {{.*}}start + 1