xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp (revision 2fe8327406050d2585d2ced910a678e28caefcf5)
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(std::launch::async, [&] {
79     return client.WriteRegister(tid, reg_num, one_register);
80   });
81 
82   Handle_QThreadSuffixSupported(server, true);
83 
84   HandlePacket(server, "P4=" + one_register_hex + ";thread:0047;", "OK");
85   ASSERT_TRUE(write_result.get());
86 
87   write_result = std::async(std::launch::async, [&] {
88     return client.WriteAllRegisters(tid, all_registers);
89   });
90 
91   HandlePacket(server, "G" + all_registers_hex + ";thread:0047;", "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(std::launch::async, [&] {
99     return client.WriteRegister(tid, reg_num, one_register);
100   });
101 
102   Handle_QThreadSuffixSupported(server, false);
103   HandlePacket(server, "Hg47", "OK");
104   HandlePacket(server, "P4=" + one_register_hex, "OK");
105   ASSERT_TRUE(write_result.get());
106 
107   write_result = std::async(std::launch::async, [&] {
108     return client.WriteAllRegisters(tid, all_registers);
109   });
110 
111   HandlePacket(server, "G" + all_registers_hex, "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       std::launch::async, [&] { return client.GetpPacketSupported(tid); });
120   Handle_QThreadSuffixSupported(server, true);
121   HandlePacket(server, "p0;thread:0047;", one_register_hex);
122   ASSERT_TRUE(async_result.get());
123 
124   std::future<DataBufferSP> read_result = std::async(
125       std::launch::async, [&] { return client.ReadRegister(tid, reg_num); });
126   HandlePacket(server, "p4;thread:0047;", "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(std::launch::async,
133                            [&] { return client.ReadAllRegisters(tid); });
134   HandlePacket(server, "g;thread:0047;", 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(std::launch::async, [&] {
145     return client.SaveRegisterState(tid, save_id);
146   });
147   Handle_QThreadSuffixSupported(server, false);
148   HandlePacket(server, "Hg47", "OK");
149   HandlePacket(server, "QSaveRegisterState", "1");
150   ASSERT_TRUE(async_result.get());
151   EXPECT_EQ(1u, save_id);
152 
153   async_result = std::async(std::launch::async, [&] {
154     return client.RestoreRegisterState(tid, save_id);
155   });
156   HandlePacket(server, "QRestoreRegisterState:1", "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       std::launch::async, [&] { return client.SyncThreadState(tid); });
164   HandlePacket(server, "qSyncThreadStateSupported", "OK");
165   HandlePacket(server, "QSyncThreadState:0047;", "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(std::launch::async,
184                  [&] { return client.GetModulesInfo(file_specs, triple); });
185   HandlePacket(
186       server, "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       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(std::launch::async,
209                  [&] { return client.GetModulesInfo(file_spec, triple); });
210   HandlePacket(
211       server,
212       "jModulesInfo:["
213       R"({"file":"/foo/bar.so","triple":"i386-pc-linux"}])",
214       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(std::launch::async,
255                    [&] { return client.GetModulesInfo(file_spec, triple); });
256     HandlePacket(
257         server,
258         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(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(10, 32, 32, 4096, true, ss);
286   client.Disconnect();
287   server_thread.join();
288 
289   GTEST_LOG_(INFO) << "Formatted output: " << ss.GetData();
290   auto object_sp = StructuredData::ParseJSON(std::string(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("packet_speeds");
296   ASSERT_TRUE(bool(object_sp));
297   dict_sp = object_sp->GetAsDictionary();
298   ASSERT_TRUE(bool(dict_sp));
299 
300   int num_packets;
301   ASSERT_TRUE(dict_sp->GetValueForKeyAsInteger("num_packets", num_packets))
302       << ss.GetString();
303   ASSERT_EQ(10, num_packets);
304 }
305 
306 TEST_F(GDBRemoteCommunicationClientTest, SendSignalsToIgnore) {
307   std::future<Status> result = std::async(std::launch::async, [&] {
308     return client.SendSignalsToIgnore({2, 3, 5, 7, 0xB, 0xD, 0x11});
309   });
310 
311   HandlePacket(server, "QPassSignals:02;03;05;07;0b;0d;11", "OK");
312   EXPECT_TRUE(result.get().Success());
313 
314   result = std::async(std::launch::async, [&] {
315     return client.SendSignalsToIgnore(std::vector<int32_t>());
316   });
317 
318   HandlePacket(server, "QPassSignals:", "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(std::launch::async, [&] {
326     return client.GetMemoryRegionInfo(addr, region_info);
327   });
328 
329   HandlePacket(server,
330       "qMemoryRegionInfo:a000",
331       "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, testing::StartsWith("qSupported:"), "");
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(std::launch::async, [&] {
348     return client.GetMemoryRegionInfo(addr, region_info);
349   });
350 
351   HandlePacket(server, "qMemoryRegionInfo:a000",
352                "start:a000;size:2000;flags:;");
353   EXPECT_TRUE(result.get().Success());
354   EXPECT_EQ(MemoryRegionInfo::eNo, region_info.GetMemoryTagged());
355 
356   result = std::async(std::launch::async, [&] {
357     return client.GetMemoryRegionInfo(addr, region_info);
358   });
359 
360   HandlePacket(server, "qMemoryRegionInfo:a000",
361                "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(std::launch::async, [&] {
370     return client.GetMemoryRegionInfo(addr, region_info);
371   });
372 
373   HandlePacket(server, "qMemoryRegionInfo:4000", "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, testing::StartsWith("qSupported:"), "");
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(timeout)) {
390       trace_type = *trace_type_or_err;
391       error_message = "";
392       return true;
393     } else {
394       trace_type = {};
395       error_message = llvm::toString(trace_type_or_err.takeError());
396       return false;
397     }
398   };
399 
400   // Success response
401   {
402     std::future<bool> result = std::async(std::launch::async, callback);
403 
404     HandlePacket(
405         server, "jLLDBTraceSupported",
406         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(std::launch::async, callback);
416 
417     HandlePacket(server, "jLLDBTraceSupported", 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(std::launch::async, callback);
426 
427     HandlePacket(server, "jLLDBTraceSupported", "E23");
428 
429     EXPECT_FALSE(result.get());
430   }
431 
432   // Error response with error message
433   {
434     std::future<bool> result = std::async(std::launch::async, callback);
435 
436     HandlePacket(server, "jLLDBTraceSupported",
437                  "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(std::launch::async, [&] { return client.GetQOffsets(); });
448 
449     HandlePacket(server, "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(std::launch::async, [&] {
476       return client.ReadMemoryTags(0xDEF0, read_len, type);
477     });
478 
479     HandlePacket(server, 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, 0, 1, "qMemTags:def0,0:1", "m",
497                  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, 0, -1, "qMemTags:def0,0:ffffffff", "m",
502                  std::vector<uint8_t>{});
503   check_qmemtags(client, server, 0, std::numeric_limits<int32_t>::min(),
504                  "qMemTags:def0,0:80000000", "m", std::vector<uint8_t>{});
505   check_qmemtags(client, server, 0, std::numeric_limits<int32_t>::max(),
506                  "qMemTags:def0,0:7fffffff", "m", 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, 32, 2, "qMemTags:def0,20:2", "m09",
511                  std::vector<uint8_t>{0x9});
512 
513   // Zero length response is fine as long as the "m" is present
514   check_qmemtags(client, server, 0, 0x34, "qMemTags:def0,0:34", "m",
515                  std::vector<uint8_t>{});
516 
517   // Normal responses
518   check_qmemtags(client, server, 16, 1, "qMemTags:def0,10:1", "m66",
519                  std::vector<uint8_t>{0x66});
520   check_qmemtags(client, server, 32, 1, "qMemTags:def0,20:1", "m0102",
521                  std::vector<uint8_t>{0x1, 0x2});
522 
523   // Empty response is an error
524   check_qmemtags(client, server, 17, 1, "qMemTags:def0,11:1", "", std::nullopt);
525   // Usual error response
526   check_qmemtags(client, server, 17, 1, "qMemTags:def0,11:1", "E01",
527                  std::nullopt);
528   // Leading m missing
529   check_qmemtags(client, server, 17, 1, "qMemTags:def0,11:1", "01",
530                  std::nullopt);
531   // Anything other than m is an error
532   check_qmemtags(client, server, 17, 1, "qMemTags:def0,11:1", "z01",
533                  std::nullopt);
534   // Decoding tag data doesn't use all the chars in the packet
535   check_qmemtags(client, server, 32, 1, "qMemTags:def0,20:1", "m09zz",
536                  std::nullopt);
537   // Data that is not hex bytes
538   check_qmemtags(client, server, 32, 1, "qMemTags:def0,20:1", "mhello",
539                  std::nullopt);
540   // Data is not a complete hex char
541   check_qmemtags(client, server, 32, 1, "qMemTags:def0,20:1", "m9",
542                  std::nullopt);
543   // Data has a trailing hex char
544   check_qmemtags(client, server, 32, 1, "qMemTags:def0,20:1", "m01020",
545                  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(std::launch::async, [&] {
554       return client.WriteMemoryTags(addr, len, type, tags);
555     });
556 
557     HandlePacket(server, 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, 0xABCD, 0x20, 1,
570                  std::vector<uint8_t>{0x12, 0x34}, "QMemTags:abcd,20:1:1234",
571                  "OK", 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, 0x4321, 0x20, 9, std::vector<uint8_t>{},
576                  "QMemTags:4321,20:9:", "OK", true);
577 
578   check_Qmemtags(client, server, 0x8877, 0x123, 0x34,
579                  std::vector<uint8_t>{0x55, 0x66, 0x77},
580                  "QMemTags:8877,123:34:556677", "E01", 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, 0x456789, 0, -1, std::vector<uint8_t>{0x99},
585                  "QMemTags:456789,0:ffffffff:99", "E03", false);
586   check_Qmemtags(client, server, 0x456789, 0,
587                  std::numeric_limits<int32_t>::max(),
588                  std::vector<uint8_t>{0x99}, "QMemTags:456789,0:7fffffff:99",
589                  "E03", false);
590   check_Qmemtags(client, server, 0x456789, 0,
591                  std::numeric_limits<int32_t>::min(),
592                  std::vector<uint8_t>{0x99}, "QMemTags:456789,0:80000000:99",
593                  "E03", false);
594 }
595