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> 12 // when 13 // exceptions are disabled. 14 #include <eh.h> 15 #endif 16 #include <future> 17 18 #include "GDBRemoteTestUtils.h" 19 20 #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h" 21 #include "lldb/Core/DataBuffer.h" 22 #include "lldb/Core/ModuleSpec.h" 23 24 #include "llvm/ADT/ArrayRef.h" 25 26 using namespace lldb_private::process_gdb_remote; 27 using namespace lldb_private; 28 using namespace lldb; 29 30 namespace { 31 32 typedef GDBRemoteCommunication::PacketResult PacketResult; 33 34 struct TestClient : public GDBRemoteCommunicationClient { 35 TestClient() { m_send_acks = false; } 36 }; 37 38 void Handle_QThreadSuffixSupported(MockServer &server, bool supported) { 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 HandlePacket(MockServer &server, llvm::StringRef expected, 49 llvm::StringRef response) { 50 StringExtractorGDBRemote request; 51 ASSERT_EQ(PacketResult::Success, server.GetPacket(request)); 52 ASSERT_EQ(expected, request.GetStringRef()); 53 ASSERT_EQ(PacketResult::Success, server.SendPacket(response)); 54 } 55 56 uint8_t all_registers[] = {'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 57 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'}; 58 std::string all_registers_hex = "404142434445464748494a4b4c4d4e4f"; 59 uint8_t one_register[] = {'A', 'B', 'C', 'D'}; 60 std::string one_register_hex = "41424344"; 61 62 } // end anonymous namespace 63 64 class GDBRemoteCommunicationClientTest : public GDBRemoteTest {}; 65 66 TEST_F(GDBRemoteCommunicationClientTest, WriteRegister) { 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 = std::async(std::launch::async, [&] { 76 return client.WriteRegister(tid, reg_num, one_register); 77 }); 78 79 Handle_QThreadSuffixSupported(server, true); 80 81 HandlePacket(server, "P4=" + one_register_hex + ";thread:0047;", "OK"); 82 ASSERT_TRUE(write_result.get()); 83 84 write_result = std::async(std::launch::async, [&] { 85 return client.WriteAllRegisters(tid, all_registers); 86 }); 87 88 HandlePacket(server, "G" + all_registers_hex + ";thread:0047;", "OK"); 89 ASSERT_TRUE(write_result.get()); 90 } 91 92 TEST_F(GDBRemoteCommunicationClientTest, WriteRegisterNoSuffix) { 93 TestClient client; 94 MockServer server; 95 Connect(client, server); 96 if (HasFailure()) 97 return; 98 99 const lldb::tid_t tid = 0x47; 100 const uint32_t reg_num = 4; 101 std::future<bool> write_result = std::async(std::launch::async, [&] { 102 return client.WriteRegister(tid, reg_num, one_register); 103 }); 104 105 Handle_QThreadSuffixSupported(server, false); 106 HandlePacket(server, "Hg47", "OK"); 107 HandlePacket(server, "P4=" + one_register_hex, "OK"); 108 ASSERT_TRUE(write_result.get()); 109 110 write_result = std::async(std::launch::async, [&] { 111 return client.WriteAllRegisters(tid, all_registers); 112 }); 113 114 HandlePacket(server, "G" + all_registers_hex, "OK"); 115 ASSERT_TRUE(write_result.get()); 116 } 117 118 TEST_F(GDBRemoteCommunicationClientTest, ReadRegister) { 119 TestClient client; 120 MockServer server; 121 Connect(client, server); 122 if (HasFailure()) 123 return; 124 125 const lldb::tid_t tid = 0x47; 126 const uint32_t reg_num = 4; 127 std::future<bool> async_result = std::async( 128 std::launch::async, [&] { return client.GetpPacketSupported(tid); }); 129 Handle_QThreadSuffixSupported(server, true); 130 HandlePacket(server, "p0;thread:0047;", one_register_hex); 131 ASSERT_TRUE(async_result.get()); 132 133 std::future<DataBufferSP> read_result = std::async( 134 std::launch::async, [&] { return client.ReadRegister(tid, reg_num); }); 135 HandlePacket(server, "p4;thread:0047;", "41424344"); 136 auto buffer_sp = read_result.get(); 137 ASSERT_TRUE(bool(buffer_sp)); 138 ASSERT_EQ(0, 139 memcmp(buffer_sp->GetBytes(), one_register, sizeof one_register)); 140 141 read_result = std::async(std::launch::async, 142 [&] { return client.ReadAllRegisters(tid); }); 143 HandlePacket(server, "g;thread:0047;", all_registers_hex); 144 buffer_sp = read_result.get(); 145 ASSERT_TRUE(bool(buffer_sp)); 146 ASSERT_EQ(0, 147 memcmp(buffer_sp->GetBytes(), all_registers, sizeof all_registers)); 148 } 149 150 TEST_F(GDBRemoteCommunicationClientTest, SaveRestoreRegistersNoSuffix) { 151 TestClient client; 152 MockServer server; 153 Connect(client, server); 154 if (HasFailure()) 155 return; 156 157 const lldb::tid_t tid = 0x47; 158 uint32_t save_id; 159 std::future<bool> async_result = std::async(std::launch::async, [&] { 160 return client.SaveRegisterState(tid, save_id); 161 }); 162 Handle_QThreadSuffixSupported(server, false); 163 HandlePacket(server, "Hg47", "OK"); 164 HandlePacket(server, "QSaveRegisterState", "1"); 165 ASSERT_TRUE(async_result.get()); 166 EXPECT_EQ(1u, save_id); 167 168 async_result = std::async(std::launch::async, [&] { 169 return client.RestoreRegisterState(tid, save_id); 170 }); 171 HandlePacket(server, "QRestoreRegisterState:1", "OK"); 172 ASSERT_TRUE(async_result.get()); 173 } 174 175 TEST_F(GDBRemoteCommunicationClientTest, SyncThreadState) { 176 TestClient client; 177 MockServer server; 178 Connect(client, server); 179 if (HasFailure()) 180 return; 181 182 const lldb::tid_t tid = 0x47; 183 std::future<bool> async_result = std::async( 184 std::launch::async, [&] { return client.SyncThreadState(tid); }); 185 HandlePacket(server, "qSyncThreadStateSupported", "OK"); 186 HandlePacket(server, "QSyncThreadState:0047;", "OK"); 187 ASSERT_TRUE(async_result.get()); 188 } 189 190 TEST_F(GDBRemoteCommunicationClientTest, GetModulesInfo) { 191 TestClient client; 192 MockServer server; 193 Connect(client, server); 194 if (HasFailure()) 195 return; 196 197 llvm::Triple triple("i386-pc-linux"); 198 199 FileSpec file_specs[] = {FileSpec("/foo/bar.so", false), 200 FileSpec("/foo/baz.so", false)}; 201 std::future<llvm::Optional<std::vector<ModuleSpec>>> async_result = 202 std::async(std::launch::async, 203 [&] { return client.GetModulesInfo(file_specs, triple); }); 204 HandlePacket( 205 server, "jModulesInfo:[" 206 R"({"file":"/foo/bar.so","triple":"i386-pc-linux"},)" 207 R"({"file":"/foo/baz.so","triple":"i386-pc-linux"}])", 208 R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" 209 R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}]])"); 210 211 auto result = async_result.get(); 212 ASSERT_TRUE(result.hasValue()); 213 ASSERT_EQ(1u, result->size()); 214 EXPECT_EQ("/foo/bar.so", result.getValue()[0].GetFileSpec().GetPath()); 215 EXPECT_EQ(triple, result.getValue()[0].GetArchitecture().GetTriple()); 216 EXPECT_EQ(UUID("@ABCDEFGHIJKLMNO", 16), result.getValue()[0].GetUUID()); 217 EXPECT_EQ(0u, result.getValue()[0].GetObjectOffset()); 218 EXPECT_EQ(1234u, result.getValue()[0].GetObjectSize()); 219 } 220 221 TEST_F(GDBRemoteCommunicationClientTest, GetModulesInfoInvalidResponse) { 222 TestClient client; 223 MockServer server; 224 Connect(client, server); 225 if (HasFailure()) 226 return; 227 228 llvm::Triple triple("i386-pc-linux"); 229 FileSpec file_spec("/foo/bar.so", false); 230 231 const char *invalid_responses[] = { 232 "OK", "E47", "[]", 233 // no UUID 234 R"([{"triple":"i386-pc-linux",)" 235 R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}])", 236 // no triple 237 R"([{"uuid":"404142434445464748494a4b4c4d4e4f",)" 238 R"("file_path":"/foo/bar.so","file_offset":0,"file_size":1234}])", 239 // no file_path 240 R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" 241 R"("file_offset":0,"file_size":1234}])", 242 // no file_offset 243 R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" 244 R"("file_path":"/foo/bar.so","file_size":1234}])", 245 // no file_size 246 R"([{"uuid":"404142434445464748494a4b4c4d4e4f","triple":"i386-pc-linux",)" 247 R"("file_path":"/foo/bar.so","file_offset":0}])", 248 }; 249 250 for (const char *response : invalid_responses) { 251 std::future<llvm::Optional<std::vector<ModuleSpec>>> async_result = 252 std::async(std::launch::async, 253 [&] { return client.GetModulesInfo(file_spec, triple); }); 254 HandlePacket( 255 server, 256 R"(jModulesInfo:[{"file":"/foo/bar.so","triple":"i386-pc-linux"}])", 257 response); 258 259 ASSERT_FALSE(async_result.get().hasValue()) << "response was: " << response; 260 } 261 } 262