summaryrefslogtreecommitdiff
path: root/source/Host
diff options
context:
space:
mode:
Diffstat (limited to 'source/Host')
-rw-r--r--source/Host/common/Host.cpp10
-rw-r--r--source/Host/common/NativeProcessProtocol.cpp22
-rw-r--r--source/Host/common/StringConvert.cpp142
-rw-r--r--source/Host/freebsd/Host.cpp9
-rw-r--r--source/Host/posix/MainLoopPosix.cpp193
5 files changed, 301 insertions, 75 deletions
diff --git a/source/Host/common/Host.cpp b/source/Host/common/Host.cpp
index 20d6355e61952..94c78a015651d 100644
--- a/source/Host/common/Host.cpp
+++ b/source/Host/common/Host.cpp
@@ -1070,13 +1070,9 @@ Host::SetCrashDescription (const char *description)
#endif
-#if !defined (__linux__) && !defined (__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined (__NetBSD__)
-
-const lldb_private::UnixSignalsSP&
-Host::GetUnixSignals ()
+const UnixSignalsSP &
+Host::GetUnixSignals()
{
- static UnixSignalsSP s_unix_signals_sp (new UnixSignals ());
+ static const auto s_unix_signals_sp = UnixSignals::Create(HostInfo::GetArchitecture());
return s_unix_signals_sp;
}
-
-#endif
diff --git a/source/Host/common/NativeProcessProtocol.cpp b/source/Host/common/NativeProcessProtocol.cpp
index 35003f5d02072..818d69bdabdc9 100644
--- a/source/Host/common/NativeProcessProtocol.cpp
+++ b/source/Host/common/NativeProcessProtocol.cpp
@@ -441,3 +441,25 @@ NativeProcessProtocol::Terminate ()
{
// Default implementation does nothing.
}
+
+#ifndef __linux__
+// These need to be implemented to support lldb-gdb-server on a given platform. Stubs are
+// provided to make the rest of the code link on non-supported platforms.
+
+Error
+NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
+ NativeDelegate &native_delegate,
+ NativeProcessProtocolSP &process_sp)
+{
+ llvm_unreachable("Platform has no NativeProcessProtocol support");
+}
+
+Error
+NativeProcessProtocol::Attach (lldb::pid_t pid,
+ NativeDelegate &native_delegate,
+ NativeProcessProtocolSP &process_sp)
+{
+ llvm_unreachable("Platform has no NativeProcessProtocol support");
+}
+
+#endif
diff --git a/source/Host/common/StringConvert.cpp b/source/Host/common/StringConvert.cpp
index 0a8e75f4b8773..c4ff67515d4e4 100644
--- a/source/Host/common/StringConvert.cpp
+++ b/source/Host/common/StringConvert.cpp
@@ -15,79 +15,103 @@
// Project includes
#include "lldb/Host/StringConvert.h"
-namespace lldb_private {
-
-namespace StringConvert {
-
-int32_t
-ToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr)
+namespace lldb_private
{
- if (s && s[0])
+ namespace StringConvert
{
- char *end = nullptr;
- const long sval = ::strtol (s, &end, base);
- if (*end == '\0')
+
+ int32_t
+ ToSInt32 (const char *s, int32_t fail_value, int base, bool *success_ptr)
{
+ if (s && s[0])
+ {
+ char *end = nullptr;
+ const long sval = ::strtol (s, &end, base);
+ if (*end == '\0')
+ {
+ if (success_ptr)
+ *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
+ return (int32_t)sval; // All characters were used, return the result
+ }
+ }
if (success_ptr)
- *success_ptr = ((sval <= INT32_MAX) && (sval >= INT32_MIN));
- return (int32_t)sval; // All characters were used, return the result
+ *success_ptr = false;
+ return fail_value;
}
- }
- if (success_ptr) *success_ptr = false;
- return fail_value;
-}
-uint32_t
-ToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr)
-{
- if (s && s[0])
- {
- char *end = nullptr;
- const unsigned long uval = ::strtoul (s, &end, base);
- if (*end == '\0')
+ uint32_t
+ ToUInt32 (const char *s, uint32_t fail_value, int base, bool *success_ptr)
{
+ if (s && s[0])
+ {
+ char *end = nullptr;
+ const unsigned long uval = ::strtoul (s, &end, base);
+ if (*end == '\0')
+ {
+ if (success_ptr)
+ *success_ptr = (uval <= UINT32_MAX);
+ return (uint32_t)uval; // All characters were used, return the result
+ }
+ }
if (success_ptr)
- *success_ptr = (uval <= UINT32_MAX);
- return (uint32_t)uval; // All characters were used, return the result
+ *success_ptr = false;
+ return fail_value;
}
- }
- if (success_ptr) *success_ptr = false;
- return fail_value;
-}
-int64_t
-ToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr)
-{
- if (s && s[0])
- {
- char *end = nullptr;
- int64_t uval = ::strtoll (s, &end, base);
- if (*end == '\0')
+ int64_t
+ ToSInt64 (const char *s, int64_t fail_value, int base, bool *success_ptr)
{
- if (success_ptr) *success_ptr = true;
- return uval; // All characters were used, return the result
+ if (s && s[0])
+ {
+ char *end = nullptr;
+ int64_t uval = ::strtoll (s, &end, base);
+ if (*end == '\0')
+ {
+ if (success_ptr)
+ *success_ptr = true;
+ return uval; // All characters were used, return the result
+ }
+ }
+ if (success_ptr)
+ *success_ptr = false;
+ return fail_value;
}
- }
- if (success_ptr) *success_ptr = false;
- return fail_value;
-}
-uint64_t
-ToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr)
-{
- if (s && s[0])
- {
- char *end = nullptr;
- uint64_t uval = ::strtoull (s, &end, base);
- if (*end == '\0')
+ uint64_t
+ ToUInt64 (const char *s, uint64_t fail_value, int base, bool *success_ptr)
{
- if (success_ptr) *success_ptr = true;
- return uval; // All characters were used, return the result
+ if (s && s[0])
+ {
+ char *end = nullptr;
+ uint64_t uval = ::strtoull (s, &end, base);
+ if (*end == '\0')
+ {
+ if (success_ptr)
+ *success_ptr = true;
+ return uval; // All characters were used, return the result
+ }
+ }
+ if (success_ptr) *success_ptr = false;
+ return fail_value;
}
- }
- if (success_ptr) *success_ptr = false;
- return fail_value;
-}
-}
+ double
+ ToDouble (const char *s, double fail_value, bool *success_ptr)
+ {
+ if (s && s[0])
+ {
+ char *end = nullptr;
+ double val = strtod (s, &end);
+ if (*end == '\0')
+ {
+ if (success_ptr)
+ *success_ptr = true;
+ return val; // All characters were used, return the result
+ }
+ }
+ if (success_ptr)
+ *success_ptr = false;
+ return fail_value;
+ }
+ }
}
diff --git a/source/Host/freebsd/Host.cpp b/source/Host/freebsd/Host.cpp
index 8b1c580af27d0..7f8d7ae96e7a7 100644
--- a/source/Host/freebsd/Host.cpp
+++ b/source/Host/freebsd/Host.cpp
@@ -40,8 +40,6 @@
#include "lldb/Utility/CleanUp.h"
#include "lldb/Utility/NameMatches.h"
-#include "Plugins/Process/Utility/FreeBSDSignals.h"
-
#include "llvm/Support/Host.h"
extern "C" {
@@ -277,13 +275,6 @@ Host::GetAuxvData(lldb_private::Process *process)
return buf_sp;
}
-const UnixSignalsSP&
-Host::GetUnixSignals ()
-{
- static const lldb_private::UnixSignalsSP s_unix_signals_sp (new FreeBSDSignals ());
- return s_unix_signals_sp;
-}
-
Error
Host::ShellExpandArguments (ProcessLaunchInfo &launch_info)
{
diff --git a/source/Host/posix/MainLoopPosix.cpp b/source/Host/posix/MainLoopPosix.cpp
new file mode 100644
index 0000000000000..cb213b9b79f1c
--- /dev/null
+++ b/source/Host/posix/MainLoopPosix.cpp
@@ -0,0 +1,193 @@
+//===-- MainLoopPosix.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/Host/posix/MainLoopPosix.h"
+
+#include <vector>
+
+#include "lldb/Core/Error.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+static sig_atomic_t g_signal_flags[NSIG];
+
+static void
+SignalHandler(int signo, siginfo_t *info, void *)
+{
+ assert(signo < NSIG);
+ g_signal_flags[signo] = 1;
+}
+
+
+MainLoopPosix::~MainLoopPosix()
+{
+ assert(m_read_fds.size() == 0);
+ assert(m_signals.size() == 0);
+}
+
+MainLoopPosix::ReadHandleUP
+MainLoopPosix::RegisterReadObject(const IOObjectSP &object_sp, const Callback &callback, Error &error)
+{
+ if (!object_sp || !object_sp->IsValid())
+ {
+ error.SetErrorString("IO object is not valid.");
+ return nullptr;
+ }
+
+ const bool inserted = m_read_fds.insert({object_sp->GetWaitableHandle(), callback}).second;
+ if (! inserted)
+ {
+ error.SetErrorStringWithFormat("File descriptor %d already monitored.",
+ object_sp->GetWaitableHandle());
+ return nullptr;
+ }
+
+ return CreateReadHandle(object_sp);
+}
+
+// We shall block the signal, then install the signal handler. The signal will be unblocked in
+// the Run() function to check for signal delivery.
+MainLoopPosix::SignalHandleUP
+MainLoopPosix::RegisterSignal(int signo, const Callback &callback, Error &error)
+{
+ if (m_signals.find(signo) != m_signals.end())
+ {
+ error.SetErrorStringWithFormat("Signal %d already monitored.", signo);
+ return nullptr;
+ }
+
+ SignalInfo info;
+ info.callback = callback;
+ struct sigaction new_action;
+ new_action.sa_sigaction = &SignalHandler;
+ new_action.sa_flags = SA_SIGINFO;
+ sigemptyset(&new_action.sa_mask);
+ sigaddset(&new_action.sa_mask, signo);
+
+ sigset_t old_set;
+ if (int ret = pthread_sigmask(SIG_BLOCK, &new_action.sa_mask, &old_set))
+ {
+ error.SetErrorStringWithFormat("pthread_sigmask failed with error %d\n", ret);
+ return nullptr;
+ }
+
+ info.was_blocked = sigismember(&old_set, signo);
+ if (sigaction(signo, &new_action, &info.old_action) == -1)
+ {
+ error.SetErrorToErrno();
+ if (!info.was_blocked)
+ pthread_sigmask(SIG_UNBLOCK, &new_action.sa_mask, nullptr);
+ return nullptr;
+ }
+
+ m_signals.insert({signo, info});
+ g_signal_flags[signo] = 0;
+
+ return SignalHandleUP(new SignalHandle(*this, signo));
+}
+
+void
+MainLoopPosix::UnregisterReadObject(const lldb::IOObjectSP &object_sp)
+{
+ bool erased = m_read_fds.erase(object_sp->GetWaitableHandle());
+ (void) erased;
+ assert(erased);
+}
+
+void
+MainLoopPosix::UnregisterSignal(int signo)
+{
+ // We undo the actions of RegisterSignal on a best-effort basis.
+ auto it = m_signals.find(signo);
+ assert(it != m_signals.end());
+
+ sigaction(signo, &it->second.old_action, nullptr);
+
+ sigset_t set;
+ sigemptyset(&set);
+ sigaddset(&set, signo);
+ pthread_sigmask(it->second.was_blocked ? SIG_BLOCK : SIG_UNBLOCK, &set, nullptr);
+
+ m_signals.erase(it);
+}
+
+Error
+MainLoopPosix::Run()
+{
+ std::vector<int> signals;
+ sigset_t sigmask;
+ std::vector<int> read_fds;
+ fd_set read_fd_set;
+ m_terminate_request = false;
+
+ // run until termination or until we run out of things to listen to
+ while (! m_terminate_request && (!m_read_fds.empty() || !m_signals.empty()))
+ {
+ // To avoid problems with callbacks changing the things we're supposed to listen to, we
+ // will store the *real* list of events separately.
+ signals.clear();
+ read_fds.clear();
+ FD_ZERO(&read_fd_set);
+ int nfds = 0;
+
+ if (int ret = pthread_sigmask(SIG_SETMASK, nullptr, &sigmask))
+ return Error("pthread_sigmask failed with error %d\n", ret);
+
+ for (const auto &fd: m_read_fds)
+ {
+ read_fds.push_back(fd.first);
+ FD_SET(fd.first, &read_fd_set);
+ nfds = std::max(nfds, fd.first+1);
+ }
+
+ for (const auto &sig: m_signals)
+ {
+ signals.push_back(sig.first);
+ sigdelset(&sigmask, sig.first);
+ }
+
+ if (pselect(nfds, &read_fd_set, nullptr, nullptr, nullptr, &sigmask) == -1 && errno != EINTR)
+ return Error(errno, eErrorTypePOSIX);
+
+ for (int sig: signals)
+ {
+ if (g_signal_flags[sig] == 0)
+ continue; // No signal
+ g_signal_flags[sig] = 0;
+
+ auto it = m_signals.find(sig);
+ if (it == m_signals.end())
+ continue; // Signal must have gotten unregistered in the meantime
+
+ it->second.callback(*this); // Do the work
+
+ if (m_terminate_request)
+ return Error();
+ }
+
+ for (int fd: read_fds)
+ {
+ if (!FD_ISSET(fd, &read_fd_set))
+ continue; // Not ready
+
+ auto it = m_read_fds.find(fd);
+ if (it == m_read_fds.end())
+ continue; // File descriptor must have gotten unregistered in the meantime
+
+ it->second(*this); // Do the work
+
+ if (m_terminate_request)
+ return Error();
+ }
+ }
+ return Error();
+}
+
+