LLDB mainline
GDBRemoteClientBase.h
Go to the documentation of this file.
1//===-- GDBRemoteClientBase.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_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H
10#define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H
11
14#include "llvm/ADT/STLFunctionalExtras.h"
15#include "llvm/ADT/StringRef.h"
16#include <chrono>
17#include <condition_variable>
18#include <cstdint>
19#include <mutex>
20
21namespace lldb_private {
23
25public:
26 enum {
28 };
29
32 virtual void HandleAsyncStdout(llvm::StringRef out) = 0;
33 virtual void HandleAsyncMisc(llvm::StringRef data) = 0;
34 virtual void HandleStopReply() = 0;
35
36 /// Process asynchronously-received structured data.
37 ///
38 /// \param[in] data
39 /// The complete data packet, expected to start with JSON-async.
40 virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data) = 0;
41 };
42
43 GDBRemoteClientBase(const char *comm_name);
44
45 bool SendAsyncSignal(int signo, std::chrono::seconds interrupt_timeout);
46
47 bool Interrupt(std::chrono::seconds interrupt_timeout);
48
50 ContinueDelegate &delegate, const UnixSignals &signals,
51 llvm::StringRef payload, std::chrono::seconds interrupt_timeout,
52 StringExtractorGDBRemote &response);
53
54 // If interrupt_timeout == 0 seconds, don't interrupt the target.
55 // Only send the packet if the target is stopped.
56 // If you want to use this mode, use the fact that the timeout is defaulted
57 // so it's clear from the call-site that you are using no-interrupt.
58 // If it is non-zero, interrupt the target if it is running, and
59 // send the packet.
60 // It the target doesn't respond within the given timeout, it returns
61 // ErrorReplyTimeout.
63 llvm::StringRef payload, StringExtractorGDBRemote &response,
64 std::chrono::seconds interrupt_timeout = std::chrono::seconds(0),
65 bool sync_on_timeout = true);
66
69 bool sync_on_timeout,
70 llvm::function_ref<void(llvm::StringRef)> output_callback);
71
73 llvm::StringRef payload, StringExtractorGDBRemote &response,
74 std::chrono::seconds interrupt_timeout,
75 llvm::function_ref<void(llvm::StringRef)> output_callback);
76
77 /// Wrapper around SendPacketAndWaitForResponse that returns an `Expected`.
78 llvm::Expected<StringExtractorGDBRemote> SendPacketAndExpectResponse(
79 llvm::StringRef payload,
80 std::chrono::seconds interrupt_timeout = std::chrono::seconds(0));
81
82 class Lock {
83 public:
84 // If interrupt_timeout == 0 seconds, only take the lock if the target is
85 // not running. If using this option, use the fact that the
86 // interrupt_timeout is defaulted so it will be obvious at the call site
87 // that you are choosing this mode. If it is non-zero, interrupt the target
88 // if it is running, waiting for the given timeout for the interrupt to
89 // succeed.
91 std::chrono::seconds interrupt_timeout = std::chrono::seconds(0));
92 ~Lock();
93
94 explicit operator bool() { return m_acquired; }
95
96 // Whether we had to interrupt the continue thread to acquire the
97 // connection.
98 bool DidInterrupt() const { return m_did_interrupt; }
99
100 private:
101 std::unique_lock<std::recursive_mutex> m_async_lock;
103 std::chrono::seconds m_interrupt_timeout;
106
108 };
109
110protected:
112 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload,
113 StringExtractorGDBRemote &response,
114 bool sync_on_timeout = true);
115
116 virtual void OnRunPacketSent(bool first);
117
118private:
119 /// Variables handling synchronization between the Continue thread and any
120 /// other threads wishing to send packets over the connection. Either the
121 /// continue thread has control over the connection (m_is_running == true) or
122 /// the connection is free for an arbitrary number of other senders to take
123 /// which indicate their interest by incrementing m_async_count.
124 ///
125 /// Semantics of individual states:
126 ///
127 /// - m_continue_packet == false, m_async_count == 0:
128 /// connection is free
129 /// - m_continue_packet == true, m_async_count == 0:
130 /// only continue thread is present
131 /// - m_continue_packet == true, m_async_count > 0:
132 /// continue thread has control, async threads should interrupt it and wait
133 /// for it to set m_continue_packet to false
134 /// - m_continue_packet == false, m_async_count > 0:
135 /// async threads have control, continue thread needs to wait for them to
136 /// finish (m_async_count goes down to 0).
137 /// @{
138 std::mutex m_mutex;
139 std::condition_variable m_cv;
140
141 /// Packet with which to resume after an async interrupt. Can be changed by
142 /// an async thread e.g. to inject a signal.
143 std::string m_continue_packet;
144
145 /// When was the interrupt packet sent. Used to make sure we time out if the
146 /// stub does not respond to interrupt requests.
147 std::chrono::time_point<std::chrono::steady_clock> m_interrupt_endpoint;
148
149 /// Number of threads interested in sending.
151
152 /// Whether the continue thread has control.
154
155 /// Whether we should resume after a stop.
157 /// @}
158
159 /// This handles the synchronization between individual async threads. For
160 /// now they just use a simple mutex.
161 std::recursive_mutex m_async_mutex;
162
163 bool ShouldStop(const UnixSignals &signals,
164 StringExtractorGDBRemote &response);
165
167 public:
169
170 explicit ContinueLock(GDBRemoteClientBase &comm);
172 explicit operator bool() { return m_acquired; }
173
175
176 void unlock();
177
178 private:
181 };
182};
183
184} // namespace process_gdb_remote
185} // namespace lldb_private
186
187#endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H
Broadcaster(lldb::BroadcasterManagerSP manager_sp, std::string name)
Construct with a broadcaster with a name.
Lock(GDBRemoteClientBase &comm, std::chrono::seconds interrupt_timeout=std::chrono::seconds(0))
std::recursive_mutex m_async_mutex
This handles the synchronization between individual async threads.
bool SendAsyncSignal(int signo, std::chrono::seconds interrupt_timeout)
std::string m_continue_packet
Packet with which to resume after an async interrupt.
uint32_t m_async_count
Number of threads interested in sending.
bool Interrupt(std::chrono::seconds interrupt_timeout)
llvm::Expected< StringExtractorGDBRemote > SendPacketAndExpectResponse(llvm::StringRef payload, std::chrono::seconds interrupt_timeout=std::chrono::seconds(0))
Wrapper around SendPacketAndWaitForResponse that returns an Expected.
PacketResult SendPacketAndReceiveResponseWithOutputSupport(llvm::StringRef payload, StringExtractorGDBRemote &response, std::chrono::seconds interrupt_timeout, llvm::function_ref< void(llvm::StringRef)> output_callback)
bool ShouldStop(const UnixSignals &signals, StringExtractorGDBRemote &response)
PacketResult SendPacketAndWaitForResponse(llvm::StringRef payload, StringExtractorGDBRemote &response, std::chrono::seconds interrupt_timeout=std::chrono::seconds(0), bool sync_on_timeout=true)
std::mutex m_mutex
Variables handling synchronization between the Continue thread and any other threads wishing to send ...
std::chrono::time_point< std::chrono::steady_clock > m_interrupt_endpoint
When was the interrupt packet sent.
bool m_is_running
Whether the continue thread has control.
PacketResult ReadPacketWithOutputSupport(StringExtractorGDBRemote &response, Timeout< std::micro > timeout, bool sync_on_timeout, llvm::function_ref< void(llvm::StringRef)> output_callback)
bool m_should_stop
Whether we should resume after a stop.
PacketResult SendPacketAndWaitForResponseNoLock(llvm::StringRef payload, StringExtractorGDBRemote &response, bool sync_on_timeout=true)
lldb::StateType SendContinuePacketAndWaitForResponse(ContinueDelegate &delegate, const UnixSignals &signals, llvm::StringRef payload, std::chrono::seconds interrupt_timeout, StringExtractorGDBRemote &response)
A class that represents a running process on the host machine.
StateType
Process and Thread States.
virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data)=0
Process asynchronously-received structured data.