mib updated this revision to Diff 449472.
mib added a comment.

Address @JDevlieghere comments:

- Use exception approach / Remove `error` helper function
- Add test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129614/new/

https://reviews.llvm.org/D129614

Files:
  lldb/examples/python/crashlog.py
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  
lldb/test/Shell/ScriptInterpreter/Python/Crashlog/interactive_crashlog_invalid_target.test

Index: lldb/test/Shell/ScriptInterpreter/Python/Crashlog/interactive_crashlog_invalid_target.test
===================================================================
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/Crashlog/interactive_crashlog_invalid_target.test
@@ -0,0 +1,8 @@
+# REQUIRES: python, native && target-aarch64 && system-darwin
+
+# RUN: %lldb -o 'command script import lldb.macosx.crashlog' \
+# RUN: -o 'crashlog -a -i -t /this_file_does_not_exist %S/Inputs/interactive_crashlog/multithread-test.ips' 2>&1 | FileCheck %s
+
+# CHECK: "crashlog" {{.*}} commands have been installed, use the "--help" options on these commands
+
+# CHECK: error: couldn't create target provided by the user (/this_file_does_not_exist)
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -2888,9 +2888,10 @@
 
   if (!ret_val)
     error.SetErrorString("unable to execute script function");
-  else
-    error.Clear();
+  else if (cmd_retobj.GetStatus() == eReturnStatusFailed)
+    return false;
 
+  error.Clear();
   return ret_val;
 }
 
@@ -2932,9 +2933,10 @@
 
   if (!ret_val)
     error.SetErrorString("unable to execute script function");
-  else
-    error.Clear();
+  else if (cmd_retobj.GetStatus() == eReturnStatusFailed)
+    return false;
 
+  error.Clear();
   return ret_val;
 }
 
Index: lldb/examples/python/crashlog.py
===================================================================
--- lldb/examples/python/crashlog.py
+++ lldb/examples/python/crashlog.py
@@ -923,7 +923,7 @@
         pass
 
     def __call__(self, debugger, command, exe_ctx, result):
-        SymbolicateCrashLogs(debugger, shlex.split(command))
+        SymbolicateCrashLogs(debugger, shlex.split(command), result)
 
     def get_short_help(self):
         return "Symbolicate one or more darwin crash log files."
@@ -1008,12 +1008,10 @@
         for error in crash_log.errors:
             print(error)
 
-def load_crashlog_in_scripted_process(debugger, crash_log_file, options):
-    result = lldb.SBCommandReturnObject()
-
+def load_crashlog_in_scripted_process(debugger, crash_log_file, options, result):
     crashlog_path = os.path.expanduser(crash_log_file)
     if not os.path.exists(crashlog_path):
-        result.PutCString("error: crashlog file %s does not exist" % crashlog_path)
+        raise Exception("crashlog file %s does not exist" % crashlog_path)
 
     crashlog = CrashLogParser().parse(debugger, crashlog_path, False)
 
@@ -1022,8 +1020,8 @@
     if options.target_path:
         target = debugger.CreateTarget(options.target_path)
         if not target:
-            result.PutCString("error: couldn't create target provided by the user ({option.target_path})")
-            return
+            raise Exception(f"couldn't create target provided by the user ({options.target_path})")
+
     # 2. If the user didn't provide a target, try to create a target using the symbolicator
     if not target or not target.IsValid():
         target = crashlog.create_target()
@@ -1032,19 +1030,15 @@
         target = debugger.GetTargetAtIndex(0)
     # 4. Fail
     if target is None or not target.IsValid():
-        result.PutCString("error: couldn't create target")
-        return
+        raise Exception("couldn't create target")
 
     ci = debugger.GetCommandInterpreter()
     if not ci:
-        result.PutCString("error: couldn't get command interpreter")
-        return
+        raise Exception("couldn't get command interpreter")
 
-    res = lldb.SBCommandReturnObject()
-    ci.HandleCommand('script from lldb.macosx import crashlog_scripted_process', res)
-    if not res.Succeeded():
-        result.PutCString("error: couldn't import crashlog scripted process module")
-        return
+    ci.HandleCommand('script from lldb.macosx import crashlog_scripted_process', result)
+    if not result.Succeeded():
+        raise Exception("couldn't import crashlog scripted process module")
 
     structured_data = lldb.SBStructuredData()
     structured_data.SetFromJSON(json.dumps({ "crashlog_path" : crashlog_path,
@@ -1057,7 +1051,7 @@
     process = target.Launch(launch_info, error)
 
     if not process or error.Fail():
-        return
+        raise Exception("couldn't launch Scripted Process", error)
 
     @contextlib.contextmanager
     def synchronous(debugger):
@@ -1213,7 +1207,7 @@
 be disassembled and lookups can be performed using the addresses found in the crash log.'''
     return CreateSymbolicateCrashLogOptions('crashlog', description, True)
 
-def SymbolicateCrashLogs(debugger, command_args):
+def SymbolicateCrashLogs(debugger, command_args, result):
     option_parser = CrashLogOptionParser()
 
     if not len(command_args):
@@ -1250,16 +1244,22 @@
     if args:
         for crash_log_file in args:
             if should_run_in_interactive_mode(options, ci):
-                load_crashlog_in_scripted_process(debugger, crash_log_file,
-                                                  options)
+                try:
+                    load_crashlog_in_scripted_process(debugger, crash_log_file,
+                                                      options, result)
+                except Exception as e:
+                    err = lldb.SBError()
+                    err.SetErrorString(str(e))
+                    result.SetError(err)
             else:
                 crash_log = CrashLogParser().parse(debugger, crash_log_file, options.verbose)
-                SymbolicateCrashLog(crash_log, options)
+                SymbolicateCrashLog(crash_log, options, result)
 
 if __name__ == '__main__':
     # Create a new debugger instance
     debugger = lldb.SBDebugger.Create()
-    SymbolicateCrashLogs(debugger, sys.argv[1:])
+    result = lldb.SBCommandReturnObject()
+    SymbolicateCrashLogs(debugger, sys.argv[1:], result)
     lldb.SBDebugger.Destroy(debugger)
 
 def __lldb_init_module(debugger, internal_dict):
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to