1 | //===-- GDBRemoteCommunicationClientTest.cpp ------------------------------===// |
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 | #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h" |
9 | #include "GDBRemoteTestUtils.h" |
10 | #include "lldb/Core/ModuleSpec.h" |
11 | #include "lldb/Host/XML.h" |
12 | #include "lldb/Target/MemoryRegionInfo.h" |
13 | #include "lldb/Utility/DataBuffer.h" |
14 | #include "lldb/Utility/StructuredData.h" |
15 | #include "lldb/lldb-enumerations.h" |
16 | #include "llvm/ADT/ArrayRef.h" |
17 | #include "llvm/Testing/Support/Error.h" |
18 | #include "gmock/gmock.h" |
19 | #include <future> |
20 | #include <limits> |
21 | #include <optional> |
22 | |
23 | using namespace lldb_private::process_gdb_remote; |
24 | using namespace lldb_private; |
25 | using namespace lldb; |
26 | using namespace llvm; |
27 | |
28 | namespace { |
29 | |
30 | typedef GDBRemoteCommunication::PacketResult PacketResult; |
31 | |
32 | struct TestClient : public GDBRemoteCommunicationClient { |
33 | TestClient() { m_send_acks = false; } |
34 | }; |
35 | |
36 | void Handle_QThreadSuffixSupported(MockServer &server, bool supported) { |
37 | StringExtractorGDBRemote request; |
38 | ASSERT_EQ(PacketResult::Success, server.GetPacket(request)); |
39 | ASSERT_EQ("QThreadSuffixSupported" , request.GetStringRef()); |
40 | if (supported) |
41 | ASSERT_EQ(PacketResult::Success, server.SendOKResponse()); |
42 | else |
43 | ASSERT_EQ(PacketResult::Success, server.SendUnimplementedResponse(nullptr)); |
44 | } |
45 | |
46 | void HandlePacket(MockServer &server, |
47 | const testing::Matcher<const std::string &> &expected, |
48 | StringRef response) { |
49 | StringExtractorGDBRemote request; |
50 | ASSERT_EQ(PacketResult::Success, server.GetPacket(request)); |
51 | ASSERT_THAT(std::string(request.GetStringRef()), expected); |
52 | ASSERT_EQ(PacketResult::Success, server.SendPacket(response)); |
53 | } |
54 | |
55 | uint8_t all_registers[] = {'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
56 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'}; |
57 | std::string all_registers_hex = "404142434445464748494a4b4c4d4e4f" ; |
58 | uint8_t one_register[] = {'A', 'B', 'C', 'D'}; |
59 | std::string one_register_hex = "41424344" ; |
60 | |
61 | } // end anonymous namespace |
62 | |
63 | class GDBRemoteCommunicationClientTest : public GDBRemoteTest { |
64 | public: |
65 | void SetUp() override { |
66 | ASSERT_THAT_ERROR(GDBRemoteCommunication::ConnectLocally(client, server), |
67 | llvm::Succeeded()); |
68 | } |
69 | |
70 | protected: |
71 | TestClient client; |
72 | MockServer server; |
73 | }; |
74 | |
75 | TEST_F(GDBRemoteCommunicationClientTest, WriteRegister) { |
76 | const lldb::tid_t tid = 0x47; |
77 | const uint32_t reg_num = 4; |
78 | std::future<bool> write_result = std::async(policy: std::launch::async, fn: [&] { |
79 | return client.WriteRegister(tid, reg_num, data: one_register); |
80 | }); |
81 | |
82 | Handle_QThreadSuffixSupported(server, supported: true); |
83 | |
84 | HandlePacket(server, expected: "P4=" + one_register_hex + ";thread:0047;" , response: "OK" ); |
85 | ASSERT_TRUE(write_result.get()); |
86 | |
87 | write_result = std::async(policy: std::launch::async, fn: [&] { |
88 | return client.WriteAllRegisters(tid, data: all_registers); |
89 | }); |
90 | |
91 | HandlePacket(server, expected: "G" + all_registers_hex + ";thread:0047;" , response: "OK" ); |
92 | ASSERT_TRUE(write_result.get()); |
93 | } |
94 | |
95 | TEST_F(GDBRemoteCommunicationClientTest, WriteRegisterNoSuffix) { |
96 | const lldb::tid_t tid = 0x47; |
97 | const uint32_t reg_num = 4; |
98 | std::future<bool> write_result = std::async(policy: std::launch::async, fn: [&] { |
99 | return client.WriteRegister(tid, reg_num, data: one_register); |
100 | }); |
101 | |
102 | Handle_QThreadSuffixSupported(server, supported: false); |
103 | HandlePacket(server, expected: "Hg47" , response: "OK" ); |
104 | HandlePacket(server, expected: "P4=" + one_register_hex, response: "OK" ); |
105 | ASSERT_TRUE(write_result.get()); |
106 | |
107 | write_result = std::async(policy: std::launch::async, fn: [&] { |
108 | return client.WriteAllRegisters(tid, data: all_registers); |
109 | }); |
110 | |
111 | HandlePacket(server, expected: "G" + all_registers_hex, response: "OK" ); |
112 | ASSERT_TRUE(write_result.get()); |
113 | } |
114 | |
115 | TEST_F(GDBRemoteCommunicationClientTest, ReadRegister) { |
116 | const lldb::tid_t tid = 0x47; |
117 | const uint32_t reg_num = 4; |
118 | std::future<bool> async_result = std::async( |
119 | policy: std::launch::async, fn: [&] { return client.GetpPacketSupported(tid); }); |
120 | Handle_QThreadSuffixSupported(server, supported: true); |
121 | HandlePacket(server, expected: "p0;thread:0047;" , response: one_register_hex); |
122 | ASSERT_TRUE(async_result.get()); |
123 | |
124 | std::future<DataBufferSP> read_result = std::async( |
125 | policy: std::launch::async, fn: [&] { return client.ReadRegister(tid, reg_num); }); |
126 | HandlePacket(server, expected: "p4;thread:0047;" , response: "41424344" ); |
127 | auto buffer_sp = read_result.get(); |
128 | ASSERT_TRUE(bool(buffer_sp)); |
129 | ASSERT_EQ(0, |
130 | memcmp(buffer_sp->GetBytes(), one_register, sizeof one_register)); |
131 | |
132 | read_result = std::async(policy: std::launch::async, |
133 | fn: [&] { return client.ReadAllRegisters(tid); }); |
134 | HandlePacket(server, expected: "g;thread:0047;" , response: all_registers_hex); |
135 | buffer_sp = read_result.get(); |
136 | ASSERT_TRUE(bool(buffer_sp)); |
137 | ASSERT_EQ(0, |
138 | memcmp(buffer_sp->GetBytes(), all_registers, sizeof all_registers)); |
139 | } |
140 | |
141 | TEST_F(GDBRemoteCommunicationClientTest, SaveRestoreRegistersNoSuffix) { |
142 | const lldb::tid_t tid = 0x47; |
143 | uint32_t save_id; |
144 | std::future<bool> async_result = std::async(policy: std::launch::async, fn: [&] { |
145 | return client.SaveRegisterState(tid, save_id); |
146 | }); |
147 | Handle_QThreadSuffixSupported(server, supported: false); |
148 | HandlePacket(server, expected: "Hg47" , response: "OK" ); |
149 | HandlePacket(server, expected: "QSaveRegisterState" , response: "1" ); |
150 | ASSERT_TRUE(async_result.get()); |
151 | EXPECT_EQ(1u, save_id); |
152 | |
153 | async_result = std::async(policy: std::launch::async, fn: [&] { |
154 | return client.RestoreRegisterState(tid, save_id); |
155 | }); |
156 | HandlePacket(server, expected: "QRestoreRegisterState:1" , response: "OK" ); |
157 | ASSERT_TRUE(async_result.get()); |
158 | } |
159 | |
160 | TEST_F(GDBRemoteCommunicationClientTest, SyncThreadState) { |
161 | const lldb::tid_t tid = 0x47; |
162 | std::future<bool> async_result = std::async( |
163 | policy: std::launch::async, fn: [&] { return client.SyncThreadState(tid); }); |
164 | HandlePacket(server, expected: "qSyncThreadStateSupported" , response: "OK" ); |
165 | HandlePacket(server, expected: "QSyncThreadState:0047;" , response: "OK" ); |
166 | ASSERT_TRUE(async_result.get()); |
167 | } |
168 | |
169 | TEST_F(GDBRemoteCommunicationClientTest, GetModulesInfo) { |
170 | llvm::Triple triple("i386-pc-linux" ); |
171 | |
172 | FileSpec file_specs[] = { |
173 | FileSpec("/foo/bar.so" , FileSpec::Style::posix), |
174 | FileSpec("/foo/baz.so" , FileSpec::Style::posix), |
175 | |
176 | // This is a bit dodgy but we currently depend on GetModulesInfo not |
177 | // performing denormalization. It can go away once the users |
178 | // (DynamicLoaderPOSIXDYLD, at least) correctly set the path syntax for |
179 | // the FileSpecs they create. |
180 | FileSpec("/foo/baw.so" , FileSpec::Style::windows), |
181 | }; |
182 | std::future<std::optional<std::vector<ModuleSpec>>> async_result = |
183 | std::async(policy: std::launch::async, |
184 | fn: [&] { return client.GetModulesInfo(module_file_specs: file_specs, triple); }); |
185 | HandlePacket( |
186 | server, expected: "jModulesInfo:[" |
187 | R"({"file":"/foo/bar.so","triple":"i386-pc-linux"},)" |
188 | R"({"file":"/foo/baz.so","triple":"i386-pc-linux"},)" |
189 | R"({"file":"/foo/baw.so","triple":"i386-pc-linux"}])" , |
190 | response: R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" |
191 | R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}]])" ); |
192 | |
193 | auto result = async_result.get(); |
194 | ASSERT_TRUE(result.has_value()); |
195 | ASSERT_EQ(1u, result->size()); |
196 | EXPECT_EQ("/foo/bar.so" , (*result)[0].GetFileSpec().GetPath()); |
197 | EXPECT_EQ(triple, (*result)[0].GetArchitecture().GetTriple()); |
198 | EXPECT_EQ(UUID("@ABCDEFGHIJKLMNO" , 16), (*result)[0].GetUUID()); |
199 | EXPECT_EQ(0u, (*result)[0].GetObjectOffset()); |
200 | EXPECT_EQ(1234u, (*result)[0].GetObjectSize()); |
201 | } |
202 | |
203 | TEST_F(GDBRemoteCommunicationClientTest, GetModulesInfo_UUID20) { |
204 | llvm::Triple triple("i386-pc-linux" ); |
205 | |
206 | FileSpec file_spec("/foo/bar.so" , FileSpec::Style::posix); |
207 | std::future<std::optional<std::vector<ModuleSpec>>> async_result = |
208 | std::async(policy: std::launch::async, |
209 | fn: [&] { return client.GetModulesInfo(module_file_specs: file_spec, triple); }); |
210 | HandlePacket( |
211 | server, |
212 | expected: "jModulesInfo:[" |
213 | R"({"file":"/foo/bar.so","triple":"i386-pc-linux"}])" , |
214 | response: R"([{"uuid":"404142434445464748494a4b4c4d4e4f50515253","triple":"i386-pc-linux",)" |
215 | R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}]])" ); |
216 | |
217 | auto result = async_result.get(); |
218 | ASSERT_TRUE(result.has_value()); |
219 | ASSERT_EQ(1u, result->size()); |
220 | EXPECT_EQ("/foo/bar.so" , (*result)[0].GetFileSpec().GetPath()); |
221 | EXPECT_EQ(triple, (*result)[0].GetArchitecture().GetTriple()); |
222 | EXPECT_EQ(UUID("@ABCDEFGHIJKLMNOPQRS" , 20), (*result)[0].GetUUID()); |
223 | EXPECT_EQ(0u, (*result)[0].GetObjectOffset()); |
224 | EXPECT_EQ(1234u, (*result)[0].GetObjectSize()); |
225 | } |
226 | |
227 | TEST_F(GDBRemoteCommunicationClientTest, GetModulesInfoInvalidResponse) { |
228 | llvm::Triple triple("i386-pc-linux" ); |
229 | FileSpec file_spec("/foo/bar.so" , FileSpec::Style::posix); |
230 | |
231 | const char *invalid_responses[] = { |
232 | // no UUID |
233 | R"([{"triple":"i386-pc-linux",)" |
234 | R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}]])" , |
235 | // invalid UUID |
236 | R"([{"uuid":"XXXXXX","triple":"i386-pc-linux",)" |
237 | R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}]])" , |
238 | // no triple |
239 | R"([{"uuid":"404142434445464748494a4b4c4d4e4f",)" |
240 | R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}]])" , |
241 | // no file_path |
242 | R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" |
243 | R"("file_offset":0,"file_size":1234}]])" , |
244 | // no file_offset |
245 | R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" |
246 | R"("file_path":"/foo/bar.so","file_size":1234}]])" , |
247 | // no file_size |
248 | R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" |
249 | R"("file_path":"/foo/bar.so","file_offset":0}]])" , |
250 | }; |
251 | |
252 | for (const char *response : invalid_responses) { |
253 | std::future<std::optional<std::vector<ModuleSpec>>> async_result = |
254 | std::async(policy: std::launch::async, |
255 | fn: [&] { return client.GetModulesInfo(module_file_specs: file_spec, triple); }); |
256 | HandlePacket( |
257 | server, |
258 | expected: R"(jModulesInfo:[{"file":"/foo/bar.so","triple":"i386-pc-linux"}])" , |
259 | response); |
260 | |
261 | auto result = async_result.get(); |
262 | ASSERT_TRUE(result); |
263 | ASSERT_EQ(0u, result->size()) << "response was: " << response; |
264 | } |
265 | } |
266 | |
267 | TEST_F(GDBRemoteCommunicationClientTest, TestPacketSpeedJSON) { |
268 | std::thread server_thread([this] { |
269 | for (;;) { |
270 | StringExtractorGDBRemote request; |
271 | PacketResult result = server.GetPacket(response&: request); |
272 | if (result == PacketResult::ErrorDisconnected) |
273 | return; |
274 | ASSERT_EQ(PacketResult::Success, result); |
275 | StringRef ref = request.GetStringRef(); |
276 | ASSERT_TRUE(ref.consume_front("qSpeedTest:response_size:" )); |
277 | int size; |
278 | ASSERT_FALSE(ref.consumeInteger(10, size)) << "ref: " << ref; |
279 | std::string response(size, 'X'); |
280 | ASSERT_EQ(PacketResult::Success, server.SendPacket(response)); |
281 | } |
282 | }); |
283 | |
284 | StreamString ss; |
285 | client.TestPacketSpeed(num_packets: 10, max_send: 32, max_recv: 32, recv_amount: 4096, json: true, strm&: ss); |
286 | client.Disconnect(); |
287 | server_thread.join(); |
288 | |
289 | GTEST_LOG_(INFO) << "Formatted output: " << ss.GetData(); |
290 | auto object_sp = StructuredData::ParseJSON(json_text: ss.GetString()); |
291 | ASSERT_TRUE(bool(object_sp)); |
292 | auto dict_sp = object_sp->GetAsDictionary(); |
293 | ASSERT_TRUE(bool(dict_sp)); |
294 | |
295 | object_sp = dict_sp->GetValueForKey(key: "packet_speeds" ); |
296 | ASSERT_TRUE(bool(object_sp)); |
297 | dict_sp = object_sp->GetAsDictionary(); |
298 | ASSERT_TRUE(bool(dict_sp)); |
299 | |
300 | size_t num_packets; |
301 | ASSERT_TRUE(dict_sp->GetValueForKeyAsInteger("num_packets" , num_packets)) |
302 | << ss.GetString(); |
303 | ASSERT_EQ(10, (int)num_packets); |
304 | } |
305 | |
306 | TEST_F(GDBRemoteCommunicationClientTest, SendSignalsToIgnore) { |
307 | std::future<Status> result = std::async(policy: std::launch::async, fn: [&] { |
308 | return client.SendSignalsToIgnore(signals: {2, 3, 5, 7, 0xB, 0xD, 0x11}); |
309 | }); |
310 | |
311 | HandlePacket(server, expected: "QPassSignals:02;03;05;07;0b;0d;11" , response: "OK" ); |
312 | EXPECT_TRUE(result.get().Success()); |
313 | |
314 | result = std::async(policy: std::launch::async, fn: [&] { |
315 | return client.SendSignalsToIgnore(signals: std::vector<int32_t>()); |
316 | }); |
317 | |
318 | HandlePacket(server, expected: "QPassSignals:" , response: "OK" ); |
319 | EXPECT_TRUE(result.get().Success()); |
320 | } |
321 | |
322 | TEST_F(GDBRemoteCommunicationClientTest, GetMemoryRegionInfo) { |
323 | const lldb::addr_t addr = 0xa000; |
324 | MemoryRegionInfo region_info; |
325 | std::future<Status> result = std::async(policy: std::launch::async, fn: [&] { |
326 | return client.GetMemoryRegionInfo(addr, range_info&: region_info); |
327 | }); |
328 | |
329 | HandlePacket(server, |
330 | expected: "qMemoryRegionInfo:a000" , |
331 | response: "start:a000;size:2000;permissions:rx;name:2f666f6f2f6261722e736f;" ); |
332 | if (XMLDocument::XMLEnabled()) { |
333 | // In case we have XML support, this will also do a "qXfer:memory-map". |
334 | // Preceeded by a query for supported extensions. Pretend we don't support |
335 | // that. |
336 | HandlePacket(server, expected: testing::StartsWith(prefix: "qSupported:" ), response: "" ); |
337 | } |
338 | EXPECT_TRUE(result.get().Success()); |
339 | EXPECT_EQ(addr, region_info.GetRange().GetRangeBase()); |
340 | EXPECT_EQ(0x2000u, region_info.GetRange().GetByteSize()); |
341 | EXPECT_EQ(MemoryRegionInfo::eYes, region_info.GetReadable()); |
342 | EXPECT_EQ(MemoryRegionInfo::eNo, region_info.GetWritable()); |
343 | EXPECT_EQ(MemoryRegionInfo::eYes, region_info.GetExecutable()); |
344 | EXPECT_EQ("/foo/bar.so" , region_info.GetName().GetStringRef()); |
345 | EXPECT_EQ(MemoryRegionInfo::eDontKnow, region_info.GetMemoryTagged()); |
346 | |
347 | result = std::async(policy: std::launch::async, fn: [&] { |
348 | return client.GetMemoryRegionInfo(addr, range_info&: region_info); |
349 | }); |
350 | |
351 | HandlePacket(server, expected: "qMemoryRegionInfo:a000" , |
352 | response: "start:a000;size:2000;flags:;" ); |
353 | EXPECT_TRUE(result.get().Success()); |
354 | EXPECT_EQ(MemoryRegionInfo::eNo, region_info.GetMemoryTagged()); |
355 | |
356 | result = std::async(policy: std::launch::async, fn: [&] { |
357 | return client.GetMemoryRegionInfo(addr, range_info&: region_info); |
358 | }); |
359 | |
360 | HandlePacket(server, expected: "qMemoryRegionInfo:a000" , |
361 | response: "start:a000;size:2000;flags: mt zz mt ;" ); |
362 | EXPECT_TRUE(result.get().Success()); |
363 | EXPECT_EQ(MemoryRegionInfo::eYes, region_info.GetMemoryTagged()); |
364 | } |
365 | |
366 | TEST_F(GDBRemoteCommunicationClientTest, GetMemoryRegionInfoInvalidResponse) { |
367 | const lldb::addr_t addr = 0x4000; |
368 | MemoryRegionInfo region_info; |
369 | std::future<Status> result = std::async(policy: std::launch::async, fn: [&] { |
370 | return client.GetMemoryRegionInfo(addr, range_info&: region_info); |
371 | }); |
372 | |
373 | HandlePacket(server, expected: "qMemoryRegionInfo:4000" , response: "start:4000;size:0000;" ); |
374 | if (XMLDocument::XMLEnabled()) { |
375 | // In case we have XML support, this will also do a "qXfer:memory-map". |
376 | // Preceeded by a query for supported extensions. Pretend we don't support |
377 | // that. |
378 | HandlePacket(server, expected: testing::StartsWith(prefix: "qSupported:" ), response: "" ); |
379 | } |
380 | EXPECT_FALSE(result.get().Success()); |
381 | } |
382 | |
383 | TEST_F(GDBRemoteCommunicationClientTest, SendTraceSupportedPacket) { |
384 | TraceSupportedResponse trace_type; |
385 | std::string error_message; |
386 | auto callback = [&] { |
387 | std::chrono::seconds timeout(10); |
388 | if (llvm::Expected<TraceSupportedResponse> trace_type_or_err = |
389 | client.SendTraceSupported(interrupt_timeout: timeout)) { |
390 | trace_type = *trace_type_or_err; |
391 | error_message = "" ; |
392 | return true; |
393 | } else { |
394 | trace_type = {}; |
395 | error_message = llvm::toString(E: trace_type_or_err.takeError()); |
396 | return false; |
397 | } |
398 | }; |
399 | |
400 | // Success response |
401 | { |
402 | std::future<bool> result = std::async(policy: std::launch::async, fn&: callback); |
403 | |
404 | HandlePacket( |
405 | server, expected: "jLLDBTraceSupported" , |
406 | response: R"({"name":"intel-pt","description":"Intel Processor Trace"}])" ); |
407 | |
408 | EXPECT_TRUE(result.get()); |
409 | ASSERT_STREQ(trace_type.name.c_str(), "intel-pt" ); |
410 | ASSERT_STREQ(trace_type.description.c_str(), "Intel Processor Trace" ); |
411 | } |
412 | |
413 | // Error response - wrong json |
414 | { |
415 | std::future<bool> result = std::async(policy: std::launch::async, fn&: callback); |
416 | |
417 | HandlePacket(server, expected: "jLLDBTraceSupported" , response: R"({"type":"intel-pt"}])" ); |
418 | |
419 | EXPECT_FALSE(result.get()); |
420 | ASSERT_STREQ(error_message.c_str(), "missing value at TraceSupportedResponse.description" ); |
421 | } |
422 | |
423 | // Error response |
424 | { |
425 | std::future<bool> result = std::async(policy: std::launch::async, fn&: callback); |
426 | |
427 | HandlePacket(server, expected: "jLLDBTraceSupported" , response: "E23" ); |
428 | |
429 | EXPECT_FALSE(result.get()); |
430 | } |
431 | |
432 | // Error response with error message |
433 | { |
434 | std::future<bool> result = std::async(policy: std::launch::async, fn&: callback); |
435 | |
436 | HandlePacket(server, expected: "jLLDBTraceSupported" , |
437 | response: "E23;50726F63657373206E6F742072756E6E696E672E" ); |
438 | |
439 | EXPECT_FALSE(result.get()); |
440 | ASSERT_STREQ(error_message.c_str(), "Process not running." ); |
441 | } |
442 | } |
443 | |
444 | TEST_F(GDBRemoteCommunicationClientTest, GetQOffsets) { |
445 | const auto &GetQOffsets = [&](llvm::StringRef response) { |
446 | std::future<std::optional<QOffsets>> result = |
447 | std::async(policy: std::launch::async, fn: [&] { return client.GetQOffsets(); }); |
448 | |
449 | HandlePacket(server, expected: "qOffsets" , response); |
450 | return result.get(); |
451 | }; |
452 | EXPECT_EQ((QOffsets{false, {0x1234, 0x1234}}), |
453 | GetQOffsets("Text=1234;Data=1234" )); |
454 | EXPECT_EQ((QOffsets{false, {0x1234, 0x1234, 0x1234}}), |
455 | GetQOffsets("Text=1234;Data=1234;Bss=1234" )); |
456 | EXPECT_EQ((QOffsets{true, {0x1234}}), GetQOffsets("TextSeg=1234" )); |
457 | EXPECT_EQ((QOffsets{true, {0x1234, 0x2345}}), |
458 | GetQOffsets("TextSeg=1234;DataSeg=2345" )); |
459 | |
460 | EXPECT_EQ(std::nullopt, GetQOffsets("E05" )); |
461 | EXPECT_EQ(std::nullopt, GetQOffsets("Text=bogus" )); |
462 | EXPECT_EQ(std::nullopt, GetQOffsets("Text=1234" )); |
463 | EXPECT_EQ(std::nullopt, GetQOffsets("Text=1234;Data=1234;" )); |
464 | EXPECT_EQ(std::nullopt, GetQOffsets("Text=1234;Data=1234;Bss=1234;" )); |
465 | EXPECT_EQ(std::nullopt, GetQOffsets("TEXTSEG=1234" )); |
466 | EXPECT_EQ(std::nullopt, GetQOffsets("TextSeg=0x1234" )); |
467 | EXPECT_EQ(std::nullopt, GetQOffsets("TextSeg=12345678123456789" )); |
468 | } |
469 | |
470 | static void |
471 | check_qmemtags(TestClient &client, MockServer &server, size_t read_len, |
472 | int32_t type, const char *packet, llvm::StringRef response, |
473 | std::optional<std::vector<uint8_t>> expected_tag_data) { |
474 | const auto &ReadMemoryTags = [&]() { |
475 | std::future<DataBufferSP> result = std::async(policy: std::launch::async, fn: [&] { |
476 | return client.ReadMemoryTags(addr: 0xDEF0, len: read_len, type); |
477 | }); |
478 | |
479 | HandlePacket(server, expected: packet, response); |
480 | return result.get(); |
481 | }; |
482 | |
483 | auto result = ReadMemoryTags(); |
484 | if (expected_tag_data) { |
485 | ASSERT_TRUE(result); |
486 | llvm::ArrayRef<uint8_t> expected(*expected_tag_data); |
487 | llvm::ArrayRef<uint8_t> got = result->GetData(); |
488 | ASSERT_THAT(expected, testing::ContainerEq(got)); |
489 | } else { |
490 | ASSERT_FALSE(result); |
491 | } |
492 | } |
493 | |
494 | TEST_F(GDBRemoteCommunicationClientTest, ReadMemoryTags) { |
495 | // Zero length reads are valid |
496 | check_qmemtags(client, server, read_len: 0, type: 1, packet: "qMemTags:def0,0:1" , response: "m" , |
497 | expected_tag_data: std::vector<uint8_t>{}); |
498 | |
499 | // Type can be negative. Put into the packet as the raw bytes |
500 | // (as opposed to a literal -1) |
501 | check_qmemtags(client, server, read_len: 0, type: -1, packet: "qMemTags:def0,0:ffffffff" , response: "m" , |
502 | expected_tag_data: std::vector<uint8_t>{}); |
503 | check_qmemtags(client, server, read_len: 0, type: std::numeric_limits<int32_t>::min(), |
504 | packet: "qMemTags:def0,0:80000000" , response: "m" , expected_tag_data: std::vector<uint8_t>{}); |
505 | check_qmemtags(client, server, read_len: 0, type: std::numeric_limits<int32_t>::max(), |
506 | packet: "qMemTags:def0,0:7fffffff" , response: "m" , expected_tag_data: std::vector<uint8_t>{}); |
507 | |
508 | // The client layer does not check the length of the received data. |
509 | // All we need is the "m" and for the decode to use all of the chars |
510 | check_qmemtags(client, server, read_len: 32, type: 2, packet: "qMemTags:def0,20:2" , response: "m09" , |
511 | expected_tag_data: std::vector<uint8_t>{0x9}); |
512 | |
513 | // Zero length response is fine as long as the "m" is present |
514 | check_qmemtags(client, server, read_len: 0, type: 0x34, packet: "qMemTags:def0,0:34" , response: "m" , |
515 | expected_tag_data: std::vector<uint8_t>{}); |
516 | |
517 | // Normal responses |
518 | check_qmemtags(client, server, read_len: 16, type: 1, packet: "qMemTags:def0,10:1" , response: "m66" , |
519 | expected_tag_data: std::vector<uint8_t>{0x66}); |
520 | check_qmemtags(client, server, read_len: 32, type: 1, packet: "qMemTags:def0,20:1" , response: "m0102" , |
521 | expected_tag_data: std::vector<uint8_t>{0x1, 0x2}); |
522 | |
523 | // Empty response is an error |
524 | check_qmemtags(client, server, read_len: 17, type: 1, packet: "qMemTags:def0,11:1" , response: "" , expected_tag_data: std::nullopt); |
525 | // Usual error response |
526 | check_qmemtags(client, server, read_len: 17, type: 1, packet: "qMemTags:def0,11:1" , response: "E01" , |
527 | expected_tag_data: std::nullopt); |
528 | // Leading m missing |
529 | check_qmemtags(client, server, read_len: 17, type: 1, packet: "qMemTags:def0,11:1" , response: "01" , |
530 | expected_tag_data: std::nullopt); |
531 | // Anything other than m is an error |
532 | check_qmemtags(client, server, read_len: 17, type: 1, packet: "qMemTags:def0,11:1" , response: "z01" , |
533 | expected_tag_data: std::nullopt); |
534 | // Decoding tag data doesn't use all the chars in the packet |
535 | check_qmemtags(client, server, read_len: 32, type: 1, packet: "qMemTags:def0,20:1" , response: "m09zz" , |
536 | expected_tag_data: std::nullopt); |
537 | // Data that is not hex bytes |
538 | check_qmemtags(client, server, read_len: 32, type: 1, packet: "qMemTags:def0,20:1" , response: "mhello" , |
539 | expected_tag_data: std::nullopt); |
540 | // Data is not a complete hex char |
541 | check_qmemtags(client, server, read_len: 32, type: 1, packet: "qMemTags:def0,20:1" , response: "m9" , |
542 | expected_tag_data: std::nullopt); |
543 | // Data has a trailing hex char |
544 | check_qmemtags(client, server, read_len: 32, type: 1, packet: "qMemTags:def0,20:1" , response: "m01020" , |
545 | expected_tag_data: std::nullopt); |
546 | } |
547 | |
548 | static void check_Qmemtags(TestClient &client, MockServer &server, |
549 | lldb::addr_t addr, size_t len, int32_t type, |
550 | const std::vector<uint8_t> &tags, const char *packet, |
551 | llvm::StringRef response, bool should_succeed) { |
552 | const auto &WriteMemoryTags = [&]() { |
553 | std::future<Status> result = std::async(policy: std::launch::async, fn: [&] { |
554 | return client.WriteMemoryTags(addr, len, type, tags); |
555 | }); |
556 | |
557 | HandlePacket(server, expected: packet, response); |
558 | return result.get(); |
559 | }; |
560 | |
561 | auto result = WriteMemoryTags(); |
562 | if (should_succeed) |
563 | ASSERT_TRUE(result.Success()); |
564 | else |
565 | ASSERT_TRUE(result.Fail()); |
566 | } |
567 | |
568 | TEST_F(GDBRemoteCommunicationClientTest, WriteMemoryTags) { |
569 | check_Qmemtags(client, server, addr: 0xABCD, len: 0x20, type: 1, |
570 | tags: std::vector<uint8_t>{0x12, 0x34}, packet: "QMemTags:abcd,20:1:1234" , |
571 | response: "OK" , should_succeed: true); |
572 | |
573 | // The GDB layer doesn't care that the number of tags != |
574 | // the length of the write. |
575 | check_Qmemtags(client, server, addr: 0x4321, len: 0x20, type: 9, tags: std::vector<uint8_t>{}, |
576 | packet: "QMemTags:4321,20:9:" , response: "OK" , should_succeed: true); |
577 | |
578 | check_Qmemtags(client, server, addr: 0x8877, len: 0x123, type: 0x34, |
579 | tags: std::vector<uint8_t>{0x55, 0x66, 0x77}, |
580 | packet: "QMemTags:8877,123:34:556677" , response: "E01" , should_succeed: false); |
581 | |
582 | // Type is a signed integer but is packed as its raw bytes, |
583 | // instead of having a +/-. |
584 | check_Qmemtags(client, server, addr: 0x456789, len: 0, type: -1, tags: std::vector<uint8_t>{0x99}, |
585 | packet: "QMemTags:456789,0:ffffffff:99" , response: "E03" , should_succeed: false); |
586 | check_Qmemtags(client, server, addr: 0x456789, len: 0, |
587 | type: std::numeric_limits<int32_t>::max(), |
588 | tags: std::vector<uint8_t>{0x99}, packet: "QMemTags:456789,0:7fffffff:99" , |
589 | response: "E03" , should_succeed: false); |
590 | check_Qmemtags(client, server, addr: 0x456789, len: 0, |
591 | type: std::numeric_limits<int32_t>::min(), |
592 | tags: std::vector<uint8_t>{0x99}, packet: "QMemTags:456789,0:80000000:99" , |
593 | response: "E03" , should_succeed: false); |
594 | } |
595 | |
596 | TEST_F(GDBRemoteCommunicationClientTest, CalculateMD5) { |
597 | FileSpec file_spec("/foo/bar" , FileSpec::Style::posix); |
598 | uint64_t low, high; |
599 | std::future<bool> async_result = std::async(policy: std::launch::async, fn: [&] { |
600 | return client.CalculateMD5(file_spec, low, high); |
601 | }); |
602 | |
603 | lldb_private::StreamString stream; |
604 | stream.PutCString(cstr: "vFile:MD5:" ); |
605 | stream.PutStringAsRawHex8(s: file_spec.GetPath(denormalize: false)); |
606 | HandlePacket(server, expected: stream.GetString().str(), |
607 | response: "F," |
608 | "deadbeef01020304" |
609 | "05060708deadbeef" ); |
610 | ASSERT_TRUE(async_result.get()); |
611 | |
612 | // Server and client puts/parses low, and then high |
613 | const uint64_t expected_low = 0xdeadbeef01020304; |
614 | const uint64_t expected_high = 0x05060708deadbeef; |
615 | EXPECT_EQ(expected_low, low); |
616 | EXPECT_EQ(expected_high, high); |
617 | } |
618 | |