[lldb] Reinvoke crashlog under lldb when run with -i from the command line
authorJonas Devlieghere <jonas@devlieghere.com>
Wed, 7 Jun 2023 23:17:00 +0000 (16:17 -0700)
committerJonas Devlieghere <jonas@devlieghere.com>
Wed, 7 Jun 2023 23:21:16 +0000 (16:21 -0700)
Run crashlog inside lldb when invoked in interactive mode from the
command line. Currently, when passing -i to crashlog from the command
line, we symbolicate in LLDB and immediately exit right after. This
pretty much defeats the purpose of interactive mode. That said, we
wouldn't want to re-implement the driver from the crashlog script.
Re-invoking the crashlog command from inside LLDB solves the issue.

rdar://97801509

Differential revision: https://reviews.llvm.org/D152319

lldb/examples/python/crashlog.py

index ea197e4..6f69ef5 100755 (executable)
@@ -1271,7 +1271,7 @@ class Symbolicate:
         pass
 
     def __call__(self, debugger, command, exe_ctx, result):
-        SymbolicateCrashLogs(debugger, shlex.split(command), result)
+        SymbolicateCrashLogs(debugger, shlex.split(command), result, True)
 
     def get_short_help(self):
         return "Symbolicate one or more darwin crash log files."
@@ -1596,7 +1596,7 @@ be disassembled and lookups can be performed using the addresses found in the cr
     return CreateSymbolicateCrashLogOptions("crashlog", description, True)
 
 
-def SymbolicateCrashLogs(debugger, command_args, result):
+def SymbolicateCrashLogs(debugger, command_args, result, is_command):
     option_parser = CrashLogOptionParser()
 
     if not len(command_args):
@@ -1608,6 +1608,26 @@ def SymbolicateCrashLogs(debugger, command_args, result):
     except:
         return
 
+    # Interactive mode requires running the crashlog command from inside lldb.
+    if options.interactive and not is_command:
+        lldb_exec = (
+            subprocess.check_output(["/usr/bin/xcrun", "-f", "lldb"])
+            .decode("utf-8")
+            .strip()
+        )
+        sys.exit(
+            os.execv(
+                lldb_exec,
+                [
+                    lldb_exec,
+                    "-o",
+                    "command script import lldb.macosx",
+                    "-o",
+                    "crashlog {}".format(shlex.join(command_args)),
+                ],
+            )
+        )
+
     if options.version:
         print(debugger.GetVersionString())
         return
@@ -1659,7 +1679,7 @@ if __name__ == "__main__":
     # Create a new debugger instance
     debugger = lldb.SBDebugger.Create()
     result = lldb.SBCommandReturnObject()
-    SymbolicateCrashLogs(debugger, sys.argv[1:], result)
+    SymbolicateCrashLogs(debugger, sys.argv[1:], result, False)
     lldb.SBDebugger.Destroy(debugger)