Debian Codegraph @

lldb/unittests/DAP/TestBase.cpp

raw
1 //===-- TestBase.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
9 #include "TestBase.h"
10 #include "Protocol/ProtocolBase.h"
11 #include "TestingSupport/TestUtilities.h"
12 #include "lldb/API/SBDefines.h"
13 #include "lldb/API/SBStructuredData.h"
14 #include "lldb/Host/File.h"
15 #include "lldb/Host/Pipe.h"
16 #include "lldb/lldb-forward.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Testing/Support/Error.h"
19 #include "gtest/gtest.h"
20 #include <memory>
21
22 using namespace llvm;
23 using namespace lldb;
24 using namespace lldb_dap;
25 using namespace lldb_dap::protocol;
26 using namespace lldb_dap_tests;
27 using lldb_private::File;
28 using lldb_private::NativeFile;
29 using lldb_private::Pipe;
30
31 void TransportBase::SetUp() {
32 PipePairTest::SetUp();
33 to_dap = std::make_unique<Transport>(
34 "to_dap", nullptr,
35 std::make_shared<NativeFile>(input.GetReadFileDescriptor(),
36 File::eOpenOptionReadOnly,
37 NativeFile::Unowned),
38 std::make_shared<NativeFile>(output.GetWriteFileDescriptor(),
39 File::eOpenOptionWriteOnly,
40 NativeFile::Unowned));
41 from_dap = std::make_unique<Transport>(
42 "from_dap", nullptr,
43 std::make_shared<NativeFile>(output.GetReadFileDescriptor(),
44 File::eOpenOptionReadOnly,
45 NativeFile::Unowned),
46 std::make_shared<NativeFile>(input.GetWriteFileDescriptor(),
47 File::eOpenOptionWriteOnly,
48 NativeFile::Unowned));
49 }
50
51 void DAPTestBase::SetUp() {
52 TransportBase::SetUp();
53 dap = std::make_unique<DAP>(
54 /*log=*/nullptr,
55 /*default_repl_mode=*/ReplMode::Auto,
56 /*pre_init_commands=*/std::vector<std::string>(),
57 /*transport=*/*to_dap);
58 }
59
60 void DAPTestBase::TearDown() {
61 if (core)
62 ASSERT_THAT_ERROR(core->discard(), Succeeded());
63 if (binary)
64 ASSERT_THAT_ERROR(binary->discard(), Succeeded());
65 }
66
67 void DAPTestBase::SetUpTestSuite() {
68 lldb::SBError error = SBDebugger::InitializeWithErrorHandling();
69 EXPECT_TRUE(error.Success());
70 }
71 void DAPTestBase::TeatUpTestSuite() { SBDebugger::Terminate(); }
72
73 bool DAPTestBase::GetDebuggerSupportsTarget(llvm::StringRef platform) {
74 EXPECT_TRUE(dap->debugger);
75
76 lldb::SBStructuredData data = dap->debugger.GetBuildConfiguration()
77 .GetValueForKey("targets")
78 .GetValueForKey("value");
79 for (size_t i = 0; i < data.GetSize(); i++) {
80 char buf[100] = {0};
81 size_t size = data.GetItemAtIndex(i).GetStringValue(buf, sizeof(buf));
82 if (llvm::StringRef(buf, size) == platform)
83 return true;
84 }
85
86 return false;
87 }
88
89 void DAPTestBase::CreateDebugger() {
90 dap->debugger = lldb::SBDebugger::Create();
91 ASSERT_TRUE(dap->debugger);
92 }
93
94 void DAPTestBase::LoadCore() {
95 ASSERT_TRUE(dap->debugger);
96 llvm::Expected<lldb_private::TestFile> binary_yaml =
97 lldb_private::TestFile::fromYamlFile(k_linux_binary);
98 ASSERT_THAT_EXPECTED(binary_yaml, Succeeded());
99 llvm::Expected<llvm::sys::fs::TempFile> binary_file =
100 binary_yaml->writeToTemporaryFile();
101 ASSERT_THAT_EXPECTED(binary_file, Succeeded());
102 binary = std::move(*binary_file);
103 dap->target = dap->debugger.CreateTarget(binary->TmpName.data());
104 ASSERT_TRUE(dap->target);
105 llvm::Expected<lldb_private::TestFile> core_yaml =
106 lldb_private::TestFile::fromYamlFile(k_linux_core);
107 ASSERT_THAT_EXPECTED(core_yaml, Succeeded());
108 llvm::Expected<llvm::sys::fs::TempFile> core_file =
109 core_yaml->writeToTemporaryFile();
110 ASSERT_THAT_EXPECTED(core_file, Succeeded());
111 this->core = std::move(*core_file);
112 SBProcess process = dap->target.LoadCore(this->core->TmpName.data());
113 ASSERT_TRUE(process);
114 }
115
116 std::vector<Message> DAPTestBase::DrainOutput() {
117 std::vector<Message> msgs;
118 output.CloseWriteFileDescriptor();
119 while (true) {
120 Expected<Message> next =
121 from_dap->Read<protocol::Message>(std::chrono::milliseconds(1));
122 if (!next) {
123 consumeError(next.takeError());
124 break;
125 }
126 msgs.push_back(*next);
127 }
128 return msgs;
129 }