[crashlog] Improve patch-crashlog.py script
authorJonas Devlieghere <jonas@devlieghere.com>
Tue, 17 Nov 2020 07:26:35 +0000 (23:26 -0800)
committerJonas Devlieghere <jonas@devlieghere.com>
Tue, 17 Nov 2020 07:27:40 +0000 (23:27 -0800)
Compute the real addresses and offsets for the json crashlog test.

lldb/test/Shell/ScriptInterpreter/Python/Crashlog/Inputs/a.out.ips
lldb/test/Shell/ScriptInterpreter/Python/Crashlog/json.test
lldb/test/Shell/ScriptInterpreter/Python/Crashlog/patch-crashlog.py
lldb/test/Shell/ScriptInterpreter/Python/Crashlog/text.test

index 703acce..13feba1 100644 (file)
@@ -34,7 +34,7 @@
   "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,
@@ -64,7 +64,7 @@
   "usedImages" : [
   [
     "@UUID@",
-    0,
+    4294967296,
     "P"
   ],
   [
index c92be0b..fbae32a 100644 (file)
@@ -1,10 +1,9 @@
 # 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
index ba69547..a8aeb35 100644 (file)
@@ -5,6 +5,7 @@ import os
 import re
 import subprocess
 import sys
+import argparse
 
 
 class CrashLogPatcher:
@@ -12,10 +13,11 @@ 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)
@@ -39,22 +41,32 @@ class CrashLogPatcher:
                 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)
index 1375784..7c6e1fc 100644 (file)
@@ -1,10 +1,9 @@
 # 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