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
22using namespace llvm;
23using namespace lldb;
24using namespace lldb_dap;
25using namespace lldb_dap::protocol;
26using namespace lldb_dap_tests;
27using lldb_private::File;
28using lldb_private::NativeFile;
29using lldb_private::Pipe;
30
31void TransportBase::SetUp() {
32 PipePairTest::SetUp();
33 to_dap = std::make_unique<Transport>(
34 args: "to_dap", args: nullptr,
35 args: std::make_shared<NativeFile>(args: input.GetReadFileDescriptor(),
36 args: File::eOpenOptionReadOnly,
37 args: NativeFile::Unowned),
38 args: std::make_shared<NativeFile>(args: output.GetWriteFileDescriptor(),
39 args: File::eOpenOptionWriteOnly,
40 args: NativeFile::Unowned));
41 from_dap = std::make_unique<Transport>(
42 args: "from_dap", args: nullptr,
43 args: std::make_shared<NativeFile>(args: output.GetReadFileDescriptor(),
44 args: File::eOpenOptionReadOnly,
45 args: NativeFile::Unowned),
46 args: std::make_shared<NativeFile>(args: input.GetWriteFileDescriptor(),
47 args: File::eOpenOptionWriteOnly,
48 args: NativeFile::Unowned));
49}
50
51void DAPTestBase::SetUp() {
52 TransportBase::SetUp();
53 dap = std::make_unique<DAP>(
54 /*log=*/args: nullptr,
55 /*default_repl_mode=*/args: ReplMode::Auto,
56 /*pre_init_commands=*/args: std::vector<std::string>(),
57 /*transport=*/args&: *to_dap);
58}
59
60void DAPTestBase::TearDown() {
61 if (core)
62 ASSERT_THAT_ERROR(core->discard(), Succeeded());
63 if (binary)
64 ASSERT_THAT_ERROR(binary->discard(), Succeeded());
65}
66
67void DAPTestBase::SetUpTestSuite() {
68 lldb::SBError error = SBDebugger::InitializeWithErrorHandling();
69 EXPECT_TRUE(error.Success());
70}
71void DAPTestBase::TeatUpTestSuite() { SBDebugger::Terminate(); }
72
73bool DAPTestBase::GetDebuggerSupportsTarget(llvm::StringRef platform) {
74 EXPECT_TRUE(dap->debugger);
75
76 lldb::SBStructuredData data = dap->debugger.GetBuildConfiguration()
77 .GetValueForKey(key: "targets")
78 .GetValueForKey(key: "value");
79 for (size_t i = 0; i < data.GetSize(); i++) {
80 char buf[100] = {0};
81 size_t size = data.GetItemAtIndex(idx: i).GetStringValue(dst: buf, dst_len: sizeof(buf));
82 if (llvm::StringRef(buf, size) == platform)
83 return true;
84 }
85
86 return false;
87}
88
89void DAPTestBase::CreateDebugger() {
90 dap->debugger = lldb::SBDebugger::Create();
91 ASSERT_TRUE(dap->debugger);
92}
93
94void DAPTestBase::LoadCore() {
95 ASSERT_TRUE(dap->debugger);
96 llvm::Expected<lldb_private::TestFile> binary_yaml =
97 lldb_private::TestFile::fromYamlFile(Name: 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(filename: binary->TmpName.data());
104 ASSERT_TRUE(dap->target);
105 llvm::Expected<lldb_private::TestFile> core_yaml =
106 lldb_private::TestFile::fromYamlFile(Name: 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(core_file: this->core->TmpName.data());
113 ASSERT_TRUE(process);
114}
115
116std::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>(timeout: std::chrono::milliseconds(1));
122 if (!next) {
123 consumeError(Err: next.takeError());
124 break;
125 }
126 msgs.push_back(x: *next);
127 }
128 return msgs;
129}
130

source code of lldb/unittests/DAP/TestBase.cpp