xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp (revision 8e8e5061b856d5ba509bf5a31c808688e478466b)
1 //===-- GDBRemoteCommunicationClientTest.cpp --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
11 // Workaround for MSVC standard library bug, which fails to include <thread> when
12 // exceptions are disabled.
13 #include <eh.h>
14 #endif
15 #include <future>
16 
17 #include "GDBRemoteTestUtils.h"
18 #include "gtest/gtest.h"
19 
20 #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h"
21 
22 using namespace lldb_private::process_gdb_remote;
23 using namespace lldb_private;
24 using namespace lldb;
25 
26 namespace
27 {
28 
29 typedef GDBRemoteCommunication::PacketResult PacketResult;
30 
31 struct TestClient : public GDBRemoteCommunicationClient
32 {
33     TestClient() { m_send_acks = false; }
34 };
35 
36 void
37 Handle_QThreadSuffixSupported(MockServer &server, bool supported)
38 {
39     StringExtractorGDBRemote request;
40     ASSERT_EQ(PacketResult::Success, server.GetPacket(request));
41     ASSERT_EQ("QThreadSuffixSupported", request.GetStringRef());
42     if (supported)
43         ASSERT_EQ(PacketResult::Success, server.SendOKResponse());
44     else
45         ASSERT_EQ(PacketResult::Success, server.SendUnimplementedResponse(nullptr));
46 }
47 
48 void
49 HandlePacket(MockServer &server, llvm::StringRef expected, llvm::StringRef response)
50 {
51     StringExtractorGDBRemote request;
52     ASSERT_EQ(PacketResult::Success, server.GetPacket(request));
53     ASSERT_EQ(expected, request.GetStringRef());
54     ASSERT_EQ(PacketResult::Success, server.SendPacket(response));
55 }
56 
57 } // end anonymous namespace
58 
59 class GDBRemoteCommunicationClientTest : public GDBRemoteTest
60 {
61 };
62 
63 TEST_F(GDBRemoteCommunicationClientTest, WriteRegister)
64 {
65     TestClient client;
66     MockServer server;
67     Connect(client, server);
68     if (HasFailure())
69         return;
70 
71     const lldb::tid_t tid = 0x47;
72     const uint32_t reg_num = 4;
73     std::future<bool> write_result =
74         std::async(std::launch::async, [&] { return client.WriteRegister(tid, reg_num, "ABCD"); });
75 
76     Handle_QThreadSuffixSupported(server, true);
77 
78     HandlePacket(server, "P4=41424344;thread:0047;", "OK");
79     ASSERT_TRUE(write_result.get());
80 
81     write_result = std::async(std::launch::async,
82                               [&] { return client.WriteAllRegisters(tid, "404142434445464748494a4b4c4d4e4f"); });
83 
84     HandlePacket(server, "G404142434445464748494a4b4c4d4e4f;thread:0047;", "OK");
85     ASSERT_TRUE(write_result.get());
86 }
87 
88 TEST_F(GDBRemoteCommunicationClientTest, WriteRegisterNoSuffix)
89 {
90     TestClient client;
91     MockServer server;
92     Connect(client, server);
93     if (HasFailure())
94         return;
95 
96     const lldb::tid_t tid = 0x47;
97     const uint32_t reg_num = 4;
98     std::future<bool> write_result =
99         std::async(std::launch::async, [&] { return client.WriteRegister(tid, reg_num, "ABCD"); });
100 
101     Handle_QThreadSuffixSupported(server, false);
102     HandlePacket(server, "Hg47", "OK");
103     HandlePacket(server, "P4=41424344", "OK");
104     ASSERT_TRUE(write_result.get());
105 
106     write_result = std::async(std::launch::async,
107                               [&] { return client.WriteAllRegisters(tid, "404142434445464748494a4b4c4d4e4f"); });
108 
109     HandlePacket(server, "G404142434445464748494a4b4c4d4e4f", "OK");
110     ASSERT_TRUE(write_result.get());
111 }
112