[lldb-dev] [PATCH] Add API control of the signal disposition.

Russell Harmon eatnumber1 at google.com
Thu Jun 5 18:16:52 PDT 2014


This commit allows you to control the signals that lldb will suppress, stop or
forward using the Python and C++ APIs.
---
 include/lldb/API/SBDefines.h              |   1 +
 include/lldb/API/SBProcess.h              |   3 +
 include/lldb/API/SBUnixSignals.h          |  84 +++++++++++++
 include/lldb/lldb-defines.h               |   4 +
 include/lldb/lldb-forward.h               |   1 +
 scripts/Python/build-swig-Python.sh       |   6 +-
 scripts/Python/interface/SBProcess.i      |   3 +
 scripts/Python/interface/SBUnixSignals.i  |  74 ++++++++++++
 scripts/lldb.swig                         |   5 +
 source/API/CMakeLists.txt                 |   1 +
 source/API/SBProcess.cpp                  |  14 +++
 source/API/SBUnixSignals.cpp              | 192 ++++++++++++++++++++++++++++++
 test/python_api/signals/Makefile          |   5 +
 test/python_api/signals/TestSignalsAPI.py |  49 ++++++++
 test/python_api/signals/main.cpp          |  20 ++++
 15 files changed, 460 insertions(+), 2 deletions(-)
 create mode 100644 include/lldb/API/SBUnixSignals.h
 create mode 100644 scripts/Python/interface/SBUnixSignals.i
 create mode 100644 source/API/SBUnixSignals.cpp
 create mode 100644 test/python_api/signals/Makefile
 create mode 100644 test/python_api/signals/TestSignalsAPI.py
 create mode 100644 test/python_api/signals/main.cpp

diff --git a/include/lldb/API/SBDefines.h b/include/lldb/API/SBDefines.h
index 15d8d1f..30ea0df 100644
--- a/include/lldb/API/SBDefines.h
+++ b/include/lldb/API/SBDefines.h
@@ -85,6 +85,7 @@ class LLDB_API SBTypeList;
 class LLDB_API SBValue;
 class LLDB_API SBValueList;
 class LLDB_API SBWatchpoint;
+class LLDB_API SBUnixSignals;
 
 }
 
diff --git a/include/lldb/API/SBProcess.h b/include/lldb/API/SBProcess.h
index 448e959..4b59462 100644
--- a/include/lldb/API/SBProcess.h
+++ b/include/lldb/API/SBProcess.h
@@ -221,6 +221,9 @@ public:
     lldb::SBError
     Signal (int signal);
 
+    lldb::SBUnixSignals
+    GetUnixSignals();
+
     void
     SendAsyncInterrupt();
     
diff --git a/include/lldb/API/SBUnixSignals.h b/include/lldb/API/SBUnixSignals.h
new file mode 100644
index 0000000..d95453a
--- /dev/null
+++ b/include/lldb/API/SBUnixSignals.h
@@ -0,0 +1,84 @@
+//===-- SBUnixSignals.h -----------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SBUnixSignals_h_
+#define LLDB_SBUnixSignals_h_
+
+#include "lldb/API/SBDefines.h"
+
+namespace lldb {
+
+class SBUnixSignals {
+public:
+    SBUnixSignals ();
+
+    SBUnixSignals (const lldb::SBUnixSignals &rhs);
+
+    ~SBUnixSignals();
+
+    const SBUnixSignals &
+    operator =(const lldb::SBUnixSignals &rhs);
+
+    void
+    Clear ();
+
+    bool
+    IsValid () const;
+
+    const char *
+    GetSignalAsCString (int32_t signo) const;
+
+    int32_t
+    GetSignalNumberFromName (const char *name) const;
+
+    bool
+    GetShouldSuppress (int32_t signo) const;
+
+    bool
+    SetShouldSuppress (int32_t signo,
+                       bool value);
+
+    bool
+    GetShouldStop (int32_t signo) const;
+
+    bool
+    SetShouldStop (int32_t signo,
+                   bool value);
+
+    bool
+    GetShouldNotify (int32_t signo) const;
+
+    bool
+    SetShouldNotify (int32_t signo, bool value);
+
+    int32_t
+    GetNumSignals () const;
+
+    int32_t
+    GetSignalAtIndex (int32_t index) const;
+
+protected:
+    friend class SBProcess;
+
+    SBUnixSignals (lldb_private::UnixSignals *unix_signals_ptr);
+
+    lldb_private::UnixSignals *
+    GetPtr() const;
+
+    void
+    SetPtr (lldb_private::UnixSignals *unix_signals_ptr);
+
+private:
+    lldb_private::UnixSignals *m_opaque_ptr;
+};
+
+
+} // namespace lldb
+
+#endif // LLDB_SBUnixSignals_h_
diff --git a/include/lldb/lldb-defines.h b/include/lldb/lldb-defines.h
index 1cfbe4e..f8a0257 100644
--- a/include/lldb/lldb-defines.h
+++ b/include/lldb/lldb-defines.h
@@ -24,6 +24,10 @@
     #define LLDB_API
 #endif
 
+#if !defined(INT32_MAX)
+    #define INT32_MAX 2147483647
+#endif
+
 #if !defined(UINT32_MAX)
     #define UINT32_MAX 4294967295U
 #endif
diff --git a/include/lldb/lldb-forward.h b/include/lldb/lldb-forward.h
index 9cd6adb..e4db5a1 100644
--- a/include/lldb/lldb-forward.h
+++ b/include/lldb/lldb-forward.h
@@ -249,6 +249,7 @@ class   TypeEnumMemberListImpl;
 class   TypeNameSpecifierImpl;
 class   TypePair;
 class   UUID;
+class   UnixSignals;
 class   Unwind;
 class   UnwindAssembly;
 class   UnwindPlan;
diff --git a/scripts/Python/build-swig-Python.sh b/scripts/Python/build-swig-Python.sh
index 5f9b574..3e30a6a 100755
--- a/scripts/Python/build-swig-Python.sh
+++ b/scripts/Python/build-swig-Python.sh
@@ -122,7 +122,8 @@ HEADER_FILES="${SRC_ROOT}/include/lldb/lldb.h"\
 " ${SRC_ROOT}/include/lldb/API/SBTypeSynthetic.h"\
 " ${SRC_ROOT}/include/lldb/API/SBValue.h"\
 " ${SRC_ROOT}/include/lldb/API/SBValueList.h"\
-" ${SRC_ROOT}/include/lldb/API/SBWatchpoint.h"
+" ${SRC_ROOT}/include/lldb/API/SBWatchpoint.h"\
+" ${SRC_ROOT}/include/lldb/API/SBUnixSignals.h"
 
 INTERFACE_FILES="${SRC_ROOT}/scripts/Python/interface/SBAddress.i"\
 " ${SRC_ROOT}/scripts/Python/interface/SBBlock.i"\
@@ -169,7 +170,8 @@ INTERFACE_FILES="${SRC_ROOT}/scripts/Python/interface/SBAddress.i"\
 " ${SRC_ROOT}/scripts/Python/interface/SBTypeSynthetic.i"\
 " ${SRC_ROOT}/scripts/Python/interface/SBValue.i"\
 " ${SRC_ROOT}/scripts/Python/interface/SBValueList.i"\
-" ${SRC_ROOT}/scripts/Python/interface/SBWatchpoint.i"
+" ${SRC_ROOT}/scripts/Python/interface/SBWatchpoint.i"\
+" ${SRC_ROOT}/scripts/Python/interface/SBUnixSignals.i"
 
 if [ $Debug -eq 1 ]
 then
diff --git a/scripts/Python/interface/SBProcess.i b/scripts/Python/interface/SBProcess.i
index d023b24..5cd99c0 100644
--- a/scripts/Python/interface/SBProcess.i
+++ b/scripts/Python/interface/SBProcess.i
@@ -240,6 +240,9 @@ public:
     lldb::SBError
     Signal (int signal);
 
+    lldb::SBUnixSignals
+    GetUnixSignals();
+
     %feature("docstring", "
     Returns a stop id that will increase every time the process executes.  If
     include_expression_stops is true, then stops caused by expression evaluation
diff --git a/scripts/Python/interface/SBUnixSignals.i b/scripts/Python/interface/SBUnixSignals.i
new file mode 100644
index 0000000..2bd537a
--- /dev/null
+++ b/scripts/Python/interface/SBUnixSignals.i
@@ -0,0 +1,74 @@
+//===-- SWIG Interface for SBProcess ----------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+namespace lldb {
+
+%feature("docstring",
+"Allows you to manipulate LLDB's signal disposition"
+) SBUnixSignals;
+class SBUnixSignals
+{
+public:
+    SBUnixSignals ();
+
+    SBUnixSignals (const lldb::SBUnixSignals &rhs);
+
+    ~SBUnixSignals();
+
+    void
+    Clear ();
+
+    bool
+    IsValid () const;
+
+    const char *
+    GetSignalAsCString (int32_t signo) const;
+
+    int32_t
+    GetSignalNumberFromName (const char *name) const;
+
+    bool
+    GetShouldSuppress (int32_t signo) const;
+
+    bool
+    SetShouldSuppress (int32_t signo,
+                       bool value);
+
+    bool
+    GetShouldStop (int32_t signo) const;
+
+    bool
+    SetShouldStop (int32_t signo,
+                   bool value);
+
+    bool
+    GetShouldNotify (int32_t signo) const;
+
+    bool
+    SetShouldNotify (int32_t signo, bool value);
+
+    int32_t
+    GetNumSignals () const;
+
+    int32_t
+    GetSignalAtIndex (int32_t index) const;
+
+    %pythoncode %{
+        def get_unix_signals_list(self):
+            signals = []
+            for idx in range(0, self.GetNumSignals()):
+                signals.append(self.GetSignalAtIndex(sig))
+            return signals
+
+        __swig_getmethods__["signals"] = get_unix_signals_list
+        if _newclass: threads = property(get_unix_signals_list, None, doc='''A read only property that returns a list() of valid signal numbers for this platform.''')
+    %}
+};
+
+}  // namespace lldb
diff --git a/scripts/lldb.swig b/scripts/lldb.swig
index 1a230d1..94392a1 100644
--- a/scripts/lldb.swig
+++ b/scripts/lldb.swig
@@ -102,6 +102,7 @@ import os
 #include "lldb/API/SBValue.h"
 #include "lldb/API/SBValueList.h"
 #include "lldb/API/SBWatchpoint.h"
+#include "lldb/API/SBUnixSignals.h"
 
 #include "../scripts/Python/python-swigsafecast.swig"
 
@@ -109,6 +110,9 @@ import os
 
 /* Various liblldb typedefs that SWIG needs to know about.  */
 #define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
+/* The ISO C99 standard specifies that in C++ implementations limit macros such
+   as INT32_MAX should only be defined if __STDC_LIMIT_MACROS is. */
+#define __STDC_LIMIT_MACROS
 %include "stdint.i"
 %include "lldb/lldb-defines.h"
 %include "lldb/lldb-enumerations.h"
@@ -169,6 +173,7 @@ import os
 %include "./Python/interface/SBValue.i"
 %include "./Python/interface/SBValueList.i"
 %include "./Python/interface/SBWatchpoint.i"
+%include "./Python/interface/SBUnixSignals.i"
 
 %include "./Python/python-extensions.swig"
 
diff --git a/source/API/CMakeLists.txt b/source/API/CMakeLists.txt
index 63adb68..b903b4e 100644
--- a/source/API/CMakeLists.txt
+++ b/source/API/CMakeLists.txt
@@ -51,4 +51,5 @@ add_lldb_library(lldbAPI
   SBValue.cpp
   SBValueList.cpp
   SBWatchpoint.cpp
+  SBUnixSignals.cpp
   )
diff --git a/source/API/SBProcess.cpp b/source/API/SBProcess.cpp
index bb5b989..2f32f28 100644
--- a/source/API/SBProcess.cpp
+++ b/source/API/SBProcess.cpp
@@ -40,6 +40,7 @@
 #include "lldb/API/SBThread.h"
 #include "lldb/API/SBStream.h"
 #include "lldb/API/SBStringList.h"
+#include "lldb/API/SBUnixSignals.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -893,6 +894,19 @@ SBProcess::Signal (int signo)
     return sb_error;
 }
 
+SBUnixSignals
+SBProcess::GetUnixSignals()
+{
+    SBUnixSignals sb_unix_signals;
+    ProcessSP process_sp(GetSP());
+    if (process_sp)
+    {
+        sb_unix_signals.SetPtr(&process_sp->GetUnixSignals());
+    }
+
+    return sb_unix_signals;
+}
+
 void
 SBProcess::SendAsyncInterrupt ()
 {
diff --git a/source/API/SBUnixSignals.cpp b/source/API/SBUnixSignals.cpp
new file mode 100644
index 0000000..21c508d
--- /dev/null
+++ b/source/API/SBUnixSignals.cpp
@@ -0,0 +1,192 @@
+//===-- SBUnixSignals.cpp -------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/lldb-defines.h"
+#include "lldb/Target/UnixSignals.h"
+#include "lldb/Core/Log.h"
+
+#include "lldb/API/SBUnixSignals.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+SBUnixSignals::SBUnixSignals () :
+    m_opaque_ptr(NULL)
+{
+}
+
+SBUnixSignals::SBUnixSignals (const SBUnixSignals &rhs) :
+    m_opaque_ptr(rhs.m_opaque_ptr)
+{
+}
+
+SBUnixSignals::SBUnixSignals (UnixSignals *unix_signals_ptr) :
+    m_opaque_ptr(unix_signals_ptr)
+{
+}
+
+const SBUnixSignals&
+SBUnixSignals::operator = (const SBUnixSignals& rhs)
+{
+    if (this != &rhs)
+        m_opaque_ptr = rhs.m_opaque_ptr;
+    return *this;
+}
+
+SBUnixSignals::~SBUnixSignals()
+{
+}
+
+UnixSignals *
+SBUnixSignals::GetPtr() const
+{
+    return m_opaque_ptr;
+}
+
+void
+SBUnixSignals::SetPtr (UnixSignals *unix_signals_ptr)
+{
+    m_opaque_ptr = unix_signals_ptr;
+}
+
+void
+SBUnixSignals::Clear ()
+{
+    m_opaque_ptr = NULL;
+}
+
+bool
+SBUnixSignals::IsValid() const
+{
+    return m_opaque_ptr != NULL;
+}
+
+const char *
+SBUnixSignals::GetSignalAsCString (int32_t signo) const
+{
+    if (m_opaque_ptr) return m_opaque_ptr->GetSignalAsCString(signo);
+    return NULL;
+}
+
+int32_t
+SBUnixSignals::GetSignalNumberFromName (const char *name) const
+{
+    if (m_opaque_ptr) return m_opaque_ptr->GetSignalNumberFromName(name);
+    return -1;
+}
+
+bool
+SBUnixSignals::GetShouldSuppress (int32_t signo) const
+{
+    if (m_opaque_ptr) return m_opaque_ptr->GetShouldSuppress(signo);
+    return false;
+}
+
+bool
+SBUnixSignals::SetShouldSuppress (int32_t signo, bool value)
+{
+    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
+
+    if (log)
+    {
+        log->Printf ("SBUnixSignals(%p)::SetShouldSuppress (signo=%d, value=%d)",
+                     m_opaque_ptr,
+                     signo,
+                     value);
+    }
+
+    if (m_opaque_ptr) return m_opaque_ptr->SetShouldSuppress(signo, value);
+    return false;
+}
+
+bool
+SBUnixSignals::GetShouldStop (int32_t signo) const
+{
+    if (m_opaque_ptr) return m_opaque_ptr->GetShouldStop(signo);
+    return false;
+}
+
+bool
+SBUnixSignals::SetShouldStop (int32_t signo, bool value)
+{
+    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
+
+    if (log)
+    {
+        log->Printf ("SBUnixSignals(%p)::SetShouldStop (signo=%d, value=%d)",
+                     m_opaque_ptr,
+                     signo,
+                     value);
+    }
+
+    if (m_opaque_ptr) return m_opaque_ptr->SetShouldStop(signo, value);
+    return false;
+}
+
+bool
+SBUnixSignals::GetShouldNotify (int32_t signo) const
+{
+    if (m_opaque_ptr) return m_opaque_ptr->GetShouldNotify(signo);
+    return false;
+}
+
+bool
+SBUnixSignals::SetShouldNotify (int32_t signo, bool value)
+{
+    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
+
+    if (log)
+    {
+        log->Printf ("SBUnixSignals(%p)::SetShouldNotify (signo=%d, value=%d)",
+                     m_opaque_ptr,
+                     signo,
+                     value);
+    }
+
+    if (m_opaque_ptr) return m_opaque_ptr->SetShouldNotify(signo, value);
+    return false;
+}
+
+int32_t
+SBUnixSignals::GetNumSignals () const
+{
+    if (m_opaque_ptr)
+    {
+        int32_t num_signals = 0;
+        for (
+            int32_t signo = m_opaque_ptr->GetFirstSignalNumber();
+            signo != LLDB_INVALID_SIGNAL_NUMBER;
+            signo = m_opaque_ptr->GetNextSignalNumber(signo)
+        )
+        {
+            num_signals++;
+        }
+        return num_signals;
+    }
+    return LLDB_INVALID_SIGNAL_NUMBER;
+}
+
+int32_t
+SBUnixSignals::GetSignalAtIndex (int32_t index) const
+{
+    if (m_opaque_ptr)
+    {
+        int32_t idx = 0;
+        for (
+            int32_t signo = m_opaque_ptr->GetFirstSignalNumber();
+            signo != LLDB_INVALID_SIGNAL_NUMBER;
+            signo = m_opaque_ptr->GetNextSignalNumber(signo)
+        )
+        {
+            if (index == idx) return signo;
+            idx++;
+        }
+    }
+    return LLDB_INVALID_SIGNAL_NUMBER;
+}
diff --git a/test/python_api/signals/Makefile b/test/python_api/signals/Makefile
new file mode 100644
index 0000000..8a7102e
--- /dev/null
+++ b/test/python_api/signals/Makefile
@@ -0,0 +1,5 @@
+LEVEL = ../../make
+
+CXX_SOURCES := main.cpp
+
+include $(LEVEL)/Makefile.rules
diff --git a/test/python_api/signals/TestSignalsAPI.py b/test/python_api/signals/TestSignalsAPI.py
new file mode 100644
index 0000000..9702284
--- /dev/null
+++ b/test/python_api/signals/TestSignalsAPI.py
@@ -0,0 +1,49 @@
+"""
+Test SBProcess APIs, including ReadMemory(), WriteMemory(), and others.
+"""
+
+import os, time
+import unittest2
+import lldb
+from lldbutil import get_stopped_thread, state_type_to_str
+from lldbtest import *
+
+class SignalsAPITestCase(TestBase):
+    mydir = os.path.join("python_api", "signals")
+
+    @python_api_test
+    def test_ignore_signal(self):
+        """Test Python SBUnixSignals.Suppress/Stop/Notify() API."""
+        self.buildDefault()
+        exe = os.path.join(os.getcwd(), "a.out")
+        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
+
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target, VALID_TARGET)
+
+        line = line_number("main.cpp", "// Set break point at this line and setup signal ignores.")
+        breakpoint = target.BreakpointCreateByLocation("main.cpp", line)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
+
+        # Launch the process, and do not stop at the entry point.
+        process = target.LaunchSimple (None, None, self.get_process_working_directory())
+
+        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
+        self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
+
+        unix_signals = process.GetUnixSignals()
+        sigint = unix_signals.GetSignalNumberFromName("SIGINT")
+        unix_signals.SetShouldSuppress(sigint, True)
+        unix_signals.SetShouldStop(sigint, False)
+        unix_signals.SetShouldNotify(sigint, False)
+
+        process.Continue()
+        self.assertTrue(process.state == lldb.eStateExited, "The process should have exited")
+        self.assertTrue(process.GetExitStatus() == 0, "The process should have returned 0")
+
+
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()
diff --git a/test/python_api/signals/main.cpp b/test/python_api/signals/main.cpp
new file mode 100644
index 0000000..ddd2ffe
--- /dev/null
+++ b/test/python_api/signals/main.cpp
@@ -0,0 +1,20 @@
+//===-- main.c --------------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+
+// This simple program is to test the lldb Python API related to process.
+
+int main (int argc, char const *argv[])
+{
+    kill(getpid(), SIGINT); // Set break point at this line and setup signal ignores.
+    return 0;
+}
-- 
2.0.0.526.g5318336




More information about the lldb-dev mailing list