[lldb-dev] Ignoring Signals via the API

Russell Harmon russ at eatnumber1.com
Fri Jul 26 23:38:43 PDT 2013


I poked around at this for a bit, and here's what I found.

1. The issue is probably a race condition. It only happens about 1/3 of the
time on my machine.
2. SIGINT is definitely being suppressed by lldb. I added a singal handler
for SIGINT which prints a message to main.cpp, and no such message is
printed when the test is run.

Anyway, I added some debug logging information to the SBUnixSignals class,
so I've attached a new patch below.
--
Russell Harmon

>From e66e124f06dfa17c5eb23636e3dccde4fa7738e8 Mon Sep 17 00:00:00 2001
From: Russell Harmon <russ at eatnumber1.com>
Date: Tue, 25 Jun 2013 01:02:31 -0700
Subject: [PATCH] Add the ability to control the signal disposition via the
 API.

---
 include/lldb/API/LLDB.h                   |   1 +
 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 |  51 ++++++++
 test/python_api/signals/main.cpp          |  20 ++++
 16 files changed, 463 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/LLDB.h b/include/lldb/API/LLDB.h
index 93bc3bc..4db60b0 100644
--- a/include/lldb/API/LLDB.h
+++ b/include/lldb/API/LLDB.h
@@ -50,5 +50,6 @@
 #include "lldb/API/SBType.h"
 #include "lldb/API/SBValue.h"
 #include "lldb/API/SBValueList.h"
+#include "lldb/API/SBUnixSignals.h"

 #endif  // LLDB_LLDB_h_
diff --git a/include/lldb/API/SBDefines.h b/include/lldb/API/SBDefines.h
index 2388c36..02742d3 100644
--- a/include/lldb/API/SBDefines.h
+++ b/include/lldb/API/SBDefines.h
@@ -76,6 +76,7 @@ class SBTypeList;
 class SBValue;
 class SBValueList;
 class SBWatchpoint;
+class SBUnixSignals;

 }

diff --git a/include/lldb/API/SBProcess.h b/include/lldb/API/SBProcess.h
index 784f362..a1e7353 100644
--- a/include/lldb/API/SBProcess.h
+++ b/include/lldb/API/SBProcess.h
@@ -211,6 +211,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 3318aa1..a42ba3c 100644
--- a/include/lldb/lldb-defines.h
+++ b/include/lldb/lldb-defines.h
@@ -12,6 +12,10 @@

 #include "lldb/lldb-types.h"

+#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 84af8b6..077d537 100644
--- a/include/lldb/lldb-forward.h
+++ b/include/lldb/lldb-forward.h
@@ -232,6 +232,7 @@ class   TypeListImpl;
 class   TypeMemberImpl;
 class   TypeNameSpecifierImpl;
 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 96c4b2b..4a111da 100755
--- a/scripts/Python/build-swig-Python.sh
+++ b/scripts/Python/build-swig-Python.sh
@@ -123,7 +123,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"\
@@ -167,7 +168,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 6f22d9a..7a03ac7 100644
--- a/scripts/Python/interface/SBProcess.i
+++ b/scripts/Python/interface/SBProcess.i
@@ -231,6 +231,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 b813f51..0a9a8ca 100644
--- a/scripts/lldb.swig
+++ b/scripts/lldb.swig
@@ -98,6 +98,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"

@@ -105,6 +106,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"
@@ -161,6 +165,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 3580b74..937a4c2 100644
--- a/source/API/CMakeLists.txt
+++ b/source/API/CMakeLists.txt
@@ -47,4 +47,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 259eb5e..2486360 100644
--- a/source/API/SBProcess.cpp
+++ b/source/API/SBProcess.cpp
@@ -36,6 +36,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;
@@ -833,6 +834,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..67f363a
--- /dev/null
+++ b/test/python_api/signals/TestSignalsAPI.py
@@ -0,0 +1,51 @@
+"""
+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 ProcessAPITestCase(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)
+
+        error = lldb.SBError()
+        event = lldb.SBEvent();
+        listener = lldb.SBListener("TestSignalsAPI Listener")
+        # Launch the process, and stop at the entry point.
+        process = target.Launch(listener, None, None,
+                                None, None, None,
+                                os.getcwd(), 0, True, error)
+        self.assertTrue(error.Success(), error.GetCString())
+
+        while not event.IsValid() or not
lldb.SBProcess.GetStateFromEvent(event) == lldb.eStateStopped:
+            self.assertTrue(listener.WaitForEvent(3, event), "Listener
timeout")
+
+        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..98f3e39
--- /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);
+    return 0;
+}
-- 
1.8.1.3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20130726/5a5731f0/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Add-the-ability-to-control-the-signal-disposition-vi.patch
Type: application/octet-stream
Size: 18808 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20130726/5a5731f0/attachment.obj>


More information about the lldb-dev mailing list