LLDB mainline
MainLoopPosix.h
Go to the documentation of this file.
1//===-- MainLoopPosix.h -----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_HOST_POSIX_MAINLOOPPOSIX_H
10#define LLDB_HOST_POSIX_MAINLOOPPOSIX_H
11
12#include "lldb/Host/Config.h"
14#include "lldb/Host/Pipe.h"
15#include "llvm/ADT/DenseMap.h"
16#include <atomic>
17#include <csignal>
18#include <list>
19#include <vector>
20
21namespace lldb_private {
22
23// Implementation of the MainLoopBase class. It can monitor file descriptors for
24// readability using ppoll, kqueue, or pselect. In addition to the common base,
25// this class provides the ability to invoke a given handler when a signal is
26// received.
28private:
29 class SignalHandle;
30
31public:
32 typedef std::unique_ptr<SignalHandle> SignalHandleUP;
33
35 ~MainLoopPosix() override;
36
38 const Callback &callback,
39 Status &error) override;
40
41 // Listening for signals from multiple MainLoop instances is perfectly safe
42 // as long as they don't try to listen for the same signal. The callback
43 // function is invoked when the control returns to the Run() function, not
44 // when the hander is executed. This mean that you can treat the callback as
45 // a normal function and perform things which would not be safe in a signal
46 // handler. However, since the callback is not invoked synchronously, you
47 // cannot use this mechanism to handle SIGSEGV and the like.
48 SignalHandleUP RegisterSignal(int signo, const Callback &callback,
49 Status &error);
50
51 Status Run() override;
52
53protected:
55 void UnregisterSignal(int signo, std::list<Callback>::iterator callback_it);
56
57 void Interrupt() override;
58
59private:
61 void ProcessSignal(int signo);
62 void ProcessSignals();
63
65 public:
67
68 private:
69 SignalHandle(MainLoopPosix &mainloop, int signo,
70 std::list<Callback>::iterator callback_it)
71 : m_mainloop(mainloop), m_signo(signo), m_callback_it(callback_it) {}
72
75 std::list<Callback>::iterator m_callback_it;
76
77 friend class MainLoopPosix;
78 SignalHandle(const SignalHandle &) = delete;
79 const SignalHandle &operator=(const SignalHandle &) = delete;
80 };
81
82 struct SignalInfo {
83 std::list<Callback> callbacks;
84 struct sigaction old_action;
85 bool was_blocked : 1;
86 };
87 class RunImpl;
88
89 llvm::DenseMap<IOObject::WaitableHandle, Callback> m_read_fds;
90 llvm::DenseMap<int, SignalInfo> m_signals;
92 std::atomic<bool> m_interrupting = false;
93#if HAVE_SYS_EVENT_H
94 int m_kqueue;
95#endif
96};
97
98} // namespace lldb_private
99
100#endif // LLDB_HOST_POSIX_MAINLOOPPOSIX_H
static llvm::raw_ostream & error(Stream &strm)
std::unique_ptr< ReadHandle > ReadHandleUP
Definition: MainLoopBase.h:49
std::function< void(MainLoopBase &)> Callback
Definition: MainLoopBase.h:51
std::list< Callback >::iterator m_callback_it
Definition: MainLoopPosix.h:75
const SignalHandle & operator=(const SignalHandle &)=delete
SignalHandle(MainLoopPosix &mainloop, int signo, std::list< Callback >::iterator callback_it)
Definition: MainLoopPosix.h:69
SignalHandle(const SignalHandle &)=delete
SignalHandleUP RegisterSignal(int signo, const Callback &callback, Status &error)
void UnregisterReadObject(IOObject::WaitableHandle handle) override
ReadHandleUP RegisterReadObject(const lldb::IOObjectSP &object_sp, const Callback &callback, Status &error) override
void ProcessReadObject(IOObject::WaitableHandle handle)
std::atomic< bool > m_interrupting
Definition: MainLoopPosix.h:92
llvm::DenseMap< int, SignalInfo > m_signals
Definition: MainLoopPosix.h:90
llvm::DenseMap< IOObject::WaitableHandle, Callback > m_read_fds
Definition: MainLoopPosix.h:89
std::unique_ptr< SignalHandle > SignalHandleUP
Definition: MainLoopPosix.h:32
void UnregisterSignal(int signo, std::list< Callback >::iterator callback_it)
A posix-based implementation of Pipe, a class that abtracts unix style pipes.
Definition: PipePosix.h:21
An error handling class.
Definition: Status.h:115
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::IOObject > IOObjectSP
Definition: lldb-forward.h:362