xref: /llvm-project/lldb/unittests/Process/gdb-remote/GDBRemoteCommunicationClientTest.cpp (revision 4b6f9591d390c317084e63ed3e695c7a19c64543)
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 const char all_registers[] = "404142434445464748494a4b4c4d4e4f";
58 
59 } // end anonymous namespace
60 
61 class GDBRemoteCommunicationClientTest : public GDBRemoteTest
62 {
63 };
64 
65 TEST_F(GDBRemoteCommunicationClientTest, WriteRegister)
66 {
67     TestClient client;
68     MockServer server;
69     Connect(client, server);
70     if (HasFailure())
71         return;
72 
73     const lldb::tid_t tid = 0x47;
74     const uint32_t reg_num = 4;
75     std::future<bool> write_result =
76         std::async(std::launch::async, [&] { return client.WriteRegister(tid, reg_num, "ABCD"); });
77 
78     Handle_QThreadSuffixSupported(server, true);
79 
80     HandlePacket(server, "P4=41424344;thread:0047;", "OK");
81     ASSERT_TRUE(write_result.get());
82 
83     write_result = std::async(std::launch::async, [&] { return client.WriteAllRegisters(tid, all_registers); });
84 
85     HandlePacket(server, std::string("G") + all_registers + ";thread:0047;", "OK");
86     ASSERT_TRUE(write_result.get());
87 }
88 
89 TEST_F(GDBRemoteCommunicationClientTest, WriteRegisterNoSuffix)
90 {
91     TestClient client;
92     MockServer server;
93     Connect(client, server);
94     if (HasFailure())
95         return;
96 
97     const lldb::tid_t tid = 0x47;
98     const uint32_t reg_num = 4;
99     std::future<bool> write_result =
100         std::async(std::launch::async, [&] { return client.WriteRegister(tid, reg_num, "ABCD"); });
101 
102     Handle_QThreadSuffixSupported(server, false);
103     HandlePacket(server, "Hg47", "OK");
104     HandlePacket(server, "P4=41424344", "OK");
105     ASSERT_TRUE(write_result.get());
106 
107     write_result = std::async(std::launch::async, [&] { return client.WriteAllRegisters(tid, all_registers); });
108 
109     HandlePacket(server, std::string("G") + all_registers, "OK");
110     ASSERT_TRUE(write_result.get());
111 }
112 
113 TEST_F(GDBRemoteCommunicationClientTest, ReadRegister)
114 {
115     TestClient client;
116     MockServer server;
117     Connect(client, server);
118     if (HasFailure())
119         return;
120 
121     const lldb::tid_t tid = 0x47;
122     const uint32_t reg_num = 4;
123     std::future<bool> async_result = std::async(std::launch::async, [&] { return client.GetpPacketSupported(tid); });
124     Handle_QThreadSuffixSupported(server, true);
125     HandlePacket(server, "p0;thread:0047;", "41424344");
126     ASSERT_TRUE(async_result.get());
127 
128     StringExtractorGDBRemote response;
129     async_result = std::async(std::launch::async, [&] { return client.ReadRegister(tid, reg_num, response); });
130     HandlePacket(server, "p4;thread:0047;", "41424344");
131     ASSERT_TRUE(async_result.get());
132     ASSERT_EQ("41424344", response.GetStringRef());
133 
134     async_result = std::async(std::launch::async, [&] { return client.ReadAllRegisters(tid, response); });
135     HandlePacket(server, "g;thread:0047;", all_registers);
136     ASSERT_TRUE(async_result.get());
137     ASSERT_EQ(all_registers, response.GetStringRef());
138 }
139 
140 TEST_F(GDBRemoteCommunicationClientTest, SaveRestoreRegistersNoSuffix)
141 {
142     TestClient client;
143     MockServer server;
144     Connect(client, server);
145     if (HasFailure())
146         return;
147 
148     const lldb::tid_t tid = 0x47;
149     uint32_t save_id;
150     std::future<bool> async_result =
151         std::async(std::launch::async, [&] { return client.SaveRegisterState(tid, save_id); });
152     Handle_QThreadSuffixSupported(server, false);
153     HandlePacket(server, "Hg47", "OK");
154     HandlePacket(server, "QSaveRegisterState", "1");
155     ASSERT_TRUE(async_result.get());
156     EXPECT_EQ(1u, save_id);
157 
158     async_result = std::async(std::launch::async, [&] { return client.RestoreRegisterState(tid, save_id); });
159     HandlePacket(server, "QRestoreRegisterState:1", "OK");
160     ASSERT_TRUE(async_result.get());
161 }
162