GDBRemoteClientBaseTest.cpp
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//===-- GDBRemoteClientBaseTest.cpp ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <future>
#include "GDBRemoteTestUtils.h"
#include "Plugins/Process/Utility/LinuxSignals.h"
#include "Plugins/Process/gdb-remote/GDBRemoteClientBase.h"
#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h"
#include "lldb/Utility/GDBRemote.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Testing/Support/Error.h"
using namespace lldb_private::process_gdb_remote;
using namespace lldb_private;
using namespace lldb;
typedef GDBRemoteCommunication::PacketResult PacketResult;
namespace {
struct MockDelegate : public GDBRemoteClientBase::ContinueDelegate {
std::string output;
std::string misc_data;
unsigned stop_reply_called = 0;
std::vector<std::string> structured_data_packets;
void HandleAsyncStdout(llvm::StringRef out) override { output += out; }
void HandleAsyncMisc(llvm::StringRef data) override { misc_data += data; }
void HandleStopReply() override { ++stop_reply_called; }
void HandleAsyncStructuredDataPacket(llvm::StringRef data) override {
structured_data_packets.push_back(std::string(data));
}
};
struct TestClient : public GDBRemoteClientBase {
TestClient() : GDBRemoteClientBase("test.client", "test.client.listener") {
m_send_acks = false;
}
};
class GDBRemoteClientBaseTest : public GDBRemoteTest {
public:
void SetUp() override {
ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client, server),
llvm::Succeeded());
ASSERT_EQ(TestClient::eBroadcastBitRunPacketSent,
listener_sp->StartListeningForEvents(
&client, TestClient::eBroadcastBitRunPacketSent));
}
protected:
TestClient client;
MockServer server;
MockDelegate delegate;
ListenerSP listener_sp = Listener::MakeListener("listener");
StateType SendCPacket(StringExtractorGDBRemote &response) {
return client.SendContinuePacketAndWaitForResponse(delegate, LinuxSignals(),
"c", response);
}
void WaitForRunEvent() {
EventSP event_sp;
listener_sp->GetEventForBroadcasterWithType(
&client, TestClient::eBroadcastBitRunPacketSent, event_sp, llvm::None);
}
};
} // end anonymous namespace
TEST_F(GDBRemoteClientBaseTest, SendContinueAndWait) {
StringExtractorGDBRemote response;
// Continue. The inferior will stop with a signal.
ASSERT_EQ(PacketResult::Success, server.SendPacket("T01"));
ASSERT_EQ(eStateStopped, SendCPacket(response));
ASSERT_EQ("T01", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
// Continue. The inferior will exit.
ASSERT_EQ(PacketResult::Success, server.SendPacket("W01"));
ASSERT_EQ(eStateExited, SendCPacket(response));
ASSERT_EQ("W01", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
// Continue. The inferior will get killed.
ASSERT_EQ(PacketResult::Success, server.SendPacket("X01"));
ASSERT_EQ(eStateExited, SendCPacket(response));
ASSERT_EQ("X01", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
}
TEST_F(GDBRemoteClientBaseTest, SendContinueAndAsyncSignal) {
StringExtractorGDBRemote continue_response, response;
// SendAsyncSignal should do nothing when we are not running.
ASSERT_FALSE(client.SendAsyncSignal(0x47));
// Continue. After the run packet is sent, send an async signal.
std::future<StateType> continue_state = std::async(
std::launch::async, [&] { return SendCPacket(continue_response); });
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
WaitForRunEvent();
std::future<bool> async_result = std::async(
std::launch::async, [&] { return client.SendAsyncSignal(0x47); });
// First we'll get interrupted.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("\x03", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("T13"));
// Then we get the signal packet.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("C47", response.GetStringRef());
ASSERT_TRUE(async_result.get());
// And we report back a signal stop.
ASSERT_EQ(PacketResult::Success, server.SendPacket("T47"));
ASSERT_EQ(eStateStopped, continue_state.get());
ASSERT_EQ("T47", continue_response.GetStringRef());
}
TEST_F(GDBRemoteClientBaseTest, SendContinueAndAsyncPacket) {
StringExtractorGDBRemote continue_response, async_response, response;
const bool send_async = true;
// Continue. After the run packet is sent, send an async packet.
std::future<StateType> continue_state = std::async(
std::launch::async, [&] { return SendCPacket(continue_response); });
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
WaitForRunEvent();
// Sending without async enabled should fail.
ASSERT_EQ(
PacketResult::ErrorSendFailed,
client.SendPacketAndWaitForResponse("qTest1", response, !send_async));
std::future<PacketResult> async_result = std::async(std::launch::async, [&] {
return client.SendPacketAndWaitForResponse("qTest2", async_response,
send_async);
});
// First we'll get interrupted.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("\x03", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("T13"));
// Then we get the async packet.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("qTest2", response.GetStringRef());
// Send the response and receive it.
ASSERT_EQ(PacketResult::Success, server.SendPacket("QTest2"));
ASSERT_EQ(PacketResult::Success, async_result.get());
ASSERT_EQ("QTest2", async_response.GetStringRef());
// And we get resumed again.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("T01"));
ASSERT_EQ(eStateStopped, continue_state.get());
ASSERT_EQ("T01", continue_response.GetStringRef());
}
TEST_F(GDBRemoteClientBaseTest, SendContinueAndInterrupt) {
StringExtractorGDBRemote continue_response, response;
// Interrupt should do nothing when we're not running.
ASSERT_FALSE(client.Interrupt());
// Continue. After the run packet is sent, send an interrupt.
std::future<StateType> continue_state = std::async(
std::launch::async, [&] { return SendCPacket(continue_response); });
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
WaitForRunEvent();
std::future<bool> async_result =
std::async(std::launch::async, [&] { return client.Interrupt(); });
// We get interrupted.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("\x03", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("T13"));
// And that's it.
ASSERT_EQ(eStateStopped, continue_state.get());
ASSERT_EQ("T13", continue_response.GetStringRef());
ASSERT_TRUE(async_result.get());
}
TEST_F(GDBRemoteClientBaseTest, SendContinueAndLateInterrupt) {
StringExtractorGDBRemote continue_response, response;
// Continue. After the run packet is sent, send an interrupt.
std::future<StateType> continue_state = std::async(
std::launch::async, [&] { return SendCPacket(continue_response); });
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
WaitForRunEvent();
std::future<bool> async_result =
std::async(std::launch::async, [&] { return client.Interrupt(); });
// However, the target stops due to a different reason than the original
// interrupt.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("\x03", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("T01"));
ASSERT_EQ(eStateStopped, continue_state.get());
ASSERT_EQ("T01", continue_response.GetStringRef());
ASSERT_TRUE(async_result.get());
// The subsequent continue packet should work normally.
ASSERT_EQ(PacketResult::Success, server.SendPacket("T01"));
ASSERT_EQ(eStateStopped, SendCPacket(response));
ASSERT_EQ("T01", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
}
TEST_F(GDBRemoteClientBaseTest, SendContinueAndInterrupt2PacketBug) {
StringExtractorGDBRemote continue_response, async_response, response;
const bool send_async = true;
// Interrupt should do nothing when we're not running.
ASSERT_FALSE(client.Interrupt());
// Continue. After the run packet is sent, send an async signal.
std::future<StateType> continue_state = std::async(
std::launch::async, [&] { return SendCPacket(continue_response); });
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
WaitForRunEvent();
std::future<bool> interrupt_result =
std::async(std::launch::async, [&] { return client.Interrupt(); });
// We get interrupted. We'll send two packets to simulate a buggy stub.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("\x03", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("T13"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("T13"));
// We should stop.
ASSERT_EQ(eStateStopped, continue_state.get());
ASSERT_EQ("T13", continue_response.GetStringRef());
ASSERT_TRUE(interrupt_result.get());
// Packet stream should remain synchronized.
std::future<PacketResult> send_result = std::async(std::launch::async, [&] {
return client.SendPacketAndWaitForResponse("qTest", async_response,
!send_async);
});
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("qTest", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.SendPacket("QTest"));
ASSERT_EQ(PacketResult::Success, send_result.get());
ASSERT_EQ("QTest", async_response.GetStringRef());
}
TEST_F(GDBRemoteClientBaseTest, SendContinueDelegateInterface) {
StringExtractorGDBRemote response;
// Continue. We'll have the server send a bunch of async packets before it
// stops.
ASSERT_EQ(PacketResult::Success, server.SendPacket("O4142"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("Apro"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("O4344"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("Afile"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("T01"));
ASSERT_EQ(eStateStopped, SendCPacket(response));
ASSERT_EQ("T01", response.GetStringRef());
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
EXPECT_EQ("ABCD", delegate.output);
EXPECT_EQ("profile", delegate.misc_data);
EXPECT_EQ(1u, delegate.stop_reply_called);
}
TEST_F(GDBRemoteClientBaseTest, SendContinueDelegateStructuredDataReceipt) {
// Build the plain-text version of the JSON data we will have the
// server send.
const std::string json_payload =
"{ \"type\": \"MyFeatureType\", "
" \"elements\": [ \"entry1\", \"entry2\" ] }";
const std::string json_packet = "JSON-async:" + json_payload;
// Escape it properly for transit.
StreamGDBRemote stream;
stream.PutEscapedBytes(json_packet.c_str(), json_packet.length());
stream.Flush();
StringExtractorGDBRemote response;
// Send async structured data packet, then stop.
ASSERT_EQ(PacketResult::Success, server.SendPacket(stream.GetData()));
ASSERT_EQ(PacketResult::Success, server.SendPacket("T01"));
ASSERT_EQ(eStateStopped, SendCPacket(response));
ASSERT_EQ("T01", response.GetStringRef());
ASSERT_EQ(1ul, delegate.structured_data_packets.size());
// Verify the packet contents. It should have been unescaped upon packet
// reception.
ASSERT_EQ(json_packet, delegate.structured_data_packets[0]);
}
TEST_F(GDBRemoteClientBaseTest, InterruptNoResponse) {
StringExtractorGDBRemote continue_response, response;
// Continue. After the run packet is sent, send an interrupt.
std::future<StateType> continue_state = std::async(
std::launch::async, [&] { return SendCPacket(continue_response); });
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("c", response.GetStringRef());
WaitForRunEvent();
std::future<bool> async_result =
std::async(std::launch::async, [&] { return client.Interrupt(); });
// We get interrupted, but we don't send a stop packet.
ASSERT_EQ(PacketResult::Success, server.GetPacket(response));
ASSERT_EQ("\x03", response.GetStringRef());
// The functions should still terminate (after a timeout).
ASSERT_TRUE(async_result.get());
ASSERT_EQ(eStateInvalid, continue_state.get());
}
TEST_F(GDBRemoteClientBaseTest, SendPacketAndReceiveResponseWithOutputSupport) {
StringExtractorGDBRemote response;
StreamString command_output;
ASSERT_EQ(PacketResult::Success, server.SendPacket("O"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("O48656c6c6f2c"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("O20"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("O"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("O776f726c64"));
ASSERT_EQ(PacketResult::Success, server.SendPacket("OK"));
PacketResult result = client.SendPacketAndReceiveResponseWithOutputSupport(
"qRcmd,test", response, true,
[&command_output](llvm::StringRef output) { command_output << output; });
ASSERT_EQ(PacketResult::Success, result);
ASSERT_EQ("OK", response.GetStringRef());
ASSERT_EQ("Hello, world", command_output.GetString().str());
}