[Lldb-commits] [PATCH] D91835: [lldb] Add Python bindings to print stack traces on crashes.

Jordan Rupprecht via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 19 17:49:26 PST 2020


rupprecht created this revision.
rupprecht added reviewers: teemperor, JDevlieghere, labath, vsk.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.
rupprecht requested review of this revision.

As noticed in D87637 <https://reviews.llvm.org/D87637>, when LLDB crashes, we only print stack traces if LLDB is directly executed, not when used via Python bindings. Enabling this by default may be undesirable (libraries shouldn't be messing with signal handlers), so make this an explicit opt-in.

When adding an `abort()` to `CommandInterpreter::HandleCommand()`, this prints:

  $ ninja check-lldb-api-commands-apropos-basic
  FAIL: lldb-api :: commands/apropos/basic/TestApropos.py (1 of 1)
  ******************** TEST 'lldb-api :: commands/apropos/basic/TestApropos.py' FAILED ********************
  ...
  Command Output (stderr):
  --
  python3.8: /home/rupprecht/src/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp:1655: bool lldb_private::CommandInterpreter::HandleCommand(const char *, lldb_private::LazyBool, lldb_private::CommandReturnObject &, lldb_private::ExecutionContext *, bool, bool): Assertion `false && "crash!"' failed.
  PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
  Stack dump:
  0.      HandleCommand(command = "settings clear -all")
   #0 0x00007f1e9a0ff4ea llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/rupprecht/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:563:11
   #1 0x00007f1e9a0ff6bb PrintStackTraceSignalHandler(void*) /home/rupprecht/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:630:1
   #2 0x00007f1e9a0fdcdb llvm::sys::RunSignalHandlers() /home/rupprecht/src/llvm-project/llvm/lib/Support/Signals.cpp:70:5
   #3 0x00007f1e9a0ffded SignalHandler(int) /home/rupprecht/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:405:1
  ...
   #9 0x00007f1e997e6228 /home/rupprecht/src/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp:1655:3


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91835

Files:
  lldb/bindings/interface/SBDebugger.i
  lldb/include/lldb/API/SBDebugger.h
  lldb/packages/Python/lldbsuite/test/dotest.py
  lldb/source/API/SBDebugger.cpp


Index: lldb/source/API/SBDebugger.cpp
===================================================================
--- lldb/source/API/SBDebugger.cpp
+++ lldb/source/API/SBDebugger.cpp
@@ -56,6 +56,8 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Signals.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -166,6 +168,14 @@
   return LLDB_RECORD_RESULT(error);
 }
 
+void SBDebugger::PrintStackTraceOnError() {
+  LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, PrintStackTraceOnError);
+  llvm::EnablePrettyStackTrace();
+  // We don't have a meaningful argv[0] to use, so use "SBDebugger" as a
+  // substitute.
+  llvm::sys::PrintStackTraceOnErrorSignal("SBDebugger");
+}
+
 void SBDebugger::Terminate() {
   LLDB_RECORD_STATIC_METHOD_NO_ARGS(void, SBDebugger, Terminate);
 
Index: lldb/packages/Python/lldbsuite/test/dotest.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/dotest.py
+++ lldb/packages/Python/lldbsuite/test/dotest.py
@@ -880,6 +880,9 @@
         lldbconfig.INITIALIZE = False
     import lldb
 
+    # Print stack traces from coming from inside lldb.
+    lldb.SBDebugger.PrintStackTraceOnError()
+
     if configuration.capture_path:
         lldb.SBReproducer.Capture(configuration.capture_path)
         lldb.SBReproducer.SetAutoGenerate(True)
Index: lldb/include/lldb/API/SBDebugger.h
===================================================================
--- lldb/include/lldb/API/SBDebugger.h
+++ lldb/include/lldb/API/SBDebugger.h
@@ -47,6 +47,8 @@
 
   static lldb::SBError InitializeWithErrorHandling();
 
+  static void PrintStackTraceOnError();
+
   static void Terminate();
 
   // Deprecated, use the one that takes a source_init_files bool.
Index: lldb/bindings/interface/SBDebugger.i
===================================================================
--- lldb/bindings/interface/SBDebugger.i
+++ lldb/bindings/interface/SBDebugger.i
@@ -124,6 +124,8 @@
     static SBError
     InitializeWithErrorHandling();
 
+    static void PrintStackTraceOnError();
+
     static void
     Terminate();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91835.306571.patch
Type: text/x-patch
Size: 2234 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20201120/21c380ad/attachment.bin>


More information about the lldb-commits mailing list