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 PipeBase::SetUp() { |
32 | ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), Succeeded()); |
33 | ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), Succeeded()); |
34 | } |
35 | |
36 | void TransportBase::SetUp() { |
37 | PipeBase::SetUp(); |
38 | to_dap = std::make_unique<Transport>( |
39 | args: "to_dap" , args: nullptr, |
40 | args: std::make_shared<NativeFile>(args: input.GetReadFileDescriptor(), |
41 | args: File::eOpenOptionReadOnly, |
42 | args: NativeFile::Unowned), |
43 | args: std::make_shared<NativeFile>(args: output.GetWriteFileDescriptor(), |
44 | args: File::eOpenOptionWriteOnly, |
45 | args: NativeFile::Unowned)); |
46 | from_dap = std::make_unique<Transport>( |
47 | args: "from_dap" , args: nullptr, |
48 | args: std::make_shared<NativeFile>(args: output.GetReadFileDescriptor(), |
49 | args: File::eOpenOptionReadOnly, |
50 | args: NativeFile::Unowned), |
51 | args: std::make_shared<NativeFile>(args: input.GetWriteFileDescriptor(), |
52 | args: File::eOpenOptionWriteOnly, |
53 | args: NativeFile::Unowned)); |
54 | } |
55 | |
56 | void DAPTestBase::SetUp() { |
57 | TransportBase::SetUp(); |
58 | dap = std::make_unique<DAP>( |
59 | /*log=*/args: nullptr, |
60 | /*default_repl_mode=*/args: ReplMode::Auto, |
61 | /*pre_init_commands=*/args: std::vector<std::string>(), |
62 | /*transport=*/args&: *to_dap); |
63 | } |
64 | |
65 | void DAPTestBase::TearDown() { |
66 | if (core) |
67 | ASSERT_THAT_ERROR(core->discard(), Succeeded()); |
68 | if (binary) |
69 | ASSERT_THAT_ERROR(binary->discard(), Succeeded()); |
70 | } |
71 | |
72 | void DAPTestBase::SetUpTestSuite() { |
73 | lldb::SBError error = SBDebugger::InitializeWithErrorHandling(); |
74 | EXPECT_TRUE(error.Success()); |
75 | } |
76 | void DAPTestBase::TeatUpTestSuite() { SBDebugger::Terminate(); } |
77 | |
78 | bool DAPTestBase::GetDebuggerSupportsTarget(llvm::StringRef platform) { |
79 | EXPECT_TRUE(dap->debugger); |
80 | |
81 | lldb::SBStructuredData data = dap->debugger.GetBuildConfiguration() |
82 | .GetValueForKey(key: "targets" ) |
83 | .GetValueForKey(key: "value" ); |
84 | for (size_t i = 0; i < data.GetSize(); i++) { |
85 | char buf[100] = {0}; |
86 | size_t size = data.GetItemAtIndex(idx: i).GetStringValue(dst: buf, dst_len: sizeof(buf)); |
87 | if (llvm::StringRef(buf, size) == platform) |
88 | return true; |
89 | } |
90 | |
91 | return false; |
92 | } |
93 | |
94 | void DAPTestBase::CreateDebugger() { |
95 | dap->debugger = lldb::SBDebugger::Create(); |
96 | ASSERT_TRUE(dap->debugger); |
97 | } |
98 | |
99 | void DAPTestBase::LoadCore() { |
100 | ASSERT_TRUE(dap->debugger); |
101 | llvm::Expected<lldb_private::TestFile> binary_yaml = |
102 | lldb_private::TestFile::fromYamlFile(Name: k_linux_binary); |
103 | ASSERT_THAT_EXPECTED(binary_yaml, Succeeded()); |
104 | llvm::Expected<llvm::sys::fs::TempFile> binary_file = |
105 | binary_yaml->writeToTemporaryFile(); |
106 | ASSERT_THAT_EXPECTED(binary_file, Succeeded()); |
107 | binary = std::move(*binary_file); |
108 | dap->target = dap->debugger.CreateTarget(filename: binary->TmpName.data()); |
109 | ASSERT_TRUE(dap->target); |
110 | llvm::Expected<lldb_private::TestFile> core_yaml = |
111 | lldb_private::TestFile::fromYamlFile(Name: k_linux_core); |
112 | ASSERT_THAT_EXPECTED(core_yaml, Succeeded()); |
113 | llvm::Expected<llvm::sys::fs::TempFile> core_file = |
114 | core_yaml->writeToTemporaryFile(); |
115 | ASSERT_THAT_EXPECTED(core_file, Succeeded()); |
116 | this->core = std::move(*core_file); |
117 | SBProcess process = dap->target.LoadCore(core_file: this->core->TmpName.data()); |
118 | ASSERT_TRUE(process); |
119 | } |
120 | |
121 | std::vector<Message> DAPTestBase::DrainOutput() { |
122 | std::vector<Message> msgs; |
123 | output.CloseWriteFileDescriptor(); |
124 | while (true) { |
125 | Expected<Message> next = from_dap->Read(timeout: std::chrono::milliseconds(1)); |
126 | if (!next) { |
127 | consumeError(Err: next.takeError()); |
128 | break; |
129 | } |
130 | msgs.push_back(x: *next); |
131 | } |
132 | return msgs; |
133 | } |
134 | |