[LLDB] Add SB API test helpers in separate library (#185866)

This adds the helpers from #184656 for the SB API in a separate library
(`lldbSBUtilityHelpers`).
This commit is contained in:
Nerixyz
2026-03-12 19:18:47 +01:00
committed by GitHub
parent e3f9aacf64
commit 6bfbf920e4
4 changed files with 108 additions and 0 deletions

View File

@@ -24,6 +24,9 @@ function(add_lldb_unittest test_name)
if ("liblldb" IN_LIST ARG_LINK_LIBS AND NOT ARG_SBAPITEST)
message(FATAL_ERROR "The ${test_name} are not allowed to link liblldb.")
endif()
if ("lldbSBUtilityHelpers" IN_LIST ARG_LINK_LIBS AND NOT ARG_SBAPITEST)
message(FATAL_ERROR "The ${test_name} are not allowed to link lldbSBUtilityHelpers.")
endif()
list(APPEND LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS})
@@ -51,6 +54,7 @@ function(add_unittest_inputs test_name inputs)
endfunction()
add_subdirectory(TestingSupport)
add_subdirectory(SBTestingSupport)
if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
# FIXME: Tests linking against libLLDB don't work on Windows.
add_subdirectory(API)

View File

@@ -0,0 +1,11 @@
set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON)
add_lldb_library(lldbSBUtilityHelpers
SBTestUtilities.cpp
LINK_COMPONENTS
Support
LINK_LIBS
liblldb
lldbUtilityHelpers
llvm_gtest
)

View File

@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "SBTestUtilities.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/API/SBStructuredData.h"
#include "lldb/API/SBTarget.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace lldb_private;
bool lldb_private::DebuggerSupportsLLVMTarget(llvm::StringRef target) {
lldb::SBStructuredData data = lldb::SBDebugger::GetBuildConfiguration()
.GetValueForKey("targets")
.GetValueForKey("value");
for (size_t i = 0; i < data.GetSize(); i++) {
char buf[100] = {0};
size_t size = data.GetItemAtIndex(i).GetStringValue(buf, sizeof(buf));
if (llvm::StringRef(buf, size) == target)
return true;
}
return false;
}
std::pair<lldb::SBTarget, lldb::SBProcess>
lldb_private::LoadCore(lldb::SBDebugger &debugger, llvm::StringRef binary_path,
llvm::StringRef core_path) {
EXPECT_TRUE(debugger);
llvm::Expected<lldb_private::TestFile> binary_yaml =
lldb_private::TestFile::fromYamlFile(binary_path);
EXPECT_THAT_EXPECTED(binary_yaml, llvm::Succeeded());
llvm::Expected<llvm::sys::fs::TempFile> binary_file =
binary_yaml->writeToTemporaryFile();
EXPECT_THAT_EXPECTED(binary_file, llvm::Succeeded());
lldb::SBError error;
lldb::SBTarget target = debugger.CreateTarget(
/*filename=*/binary_file->TmpName.data(), /*target_triple=*/"",
/*platform_name=*/"", /*add_dependent_modules=*/false, /*error=*/error);
EXPECT_TRUE(target);
EXPECT_TRUE(error.Success()) << error.GetCString();
debugger.SetSelectedTarget(target);
llvm::Expected<lldb_private::TestFile> core_yaml =
lldb_private::TestFile::fromYamlFile(core_path);
EXPECT_THAT_EXPECTED(core_yaml, llvm::Succeeded());
llvm::Expected<llvm::sys::fs::TempFile> core_file =
core_yaml->writeToTemporaryFile();
EXPECT_THAT_EXPECTED(core_file, llvm::Succeeded());
lldb::SBProcess process = target.LoadCore(core_file->TmpName.data());
EXPECT_TRUE(process);
EXPECT_THAT_ERROR(binary_file->discard(), llvm::Succeeded());
EXPECT_THAT_ERROR(core_file->discard(), llvm::Succeeded());
return std::make_pair(target, process);
}

View File

@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_UNITTESTS_SBTESTINGSUPPORT_SBTESTUTILITIES_H
#define LLDB_UNITTESTS_SBTESTINGSUPPORT_SBTESTUTILITIES_H
#include "lldb/API/SBDebugger.h"
#include "llvm/ADT/StringRef.h"
#include <utility>
namespace lldb_private {
/// Check if the debugger supports the given platform.
bool DebuggerSupportsLLVMTarget(llvm::StringRef target);
std::pair<lldb::SBTarget, lldb::SBProcess> LoadCore(lldb::SBDebugger &debugger,
llvm::StringRef binary_path,
llvm::StringRef core_path);
} // namespace lldb_private
#endif