diff --git a/lldb/unittests/CMakeLists.txt b/lldb/unittests/CMakeLists.txt index 76d8b7391c5c..41e1c29c8093 100644 --- a/lldb/unittests/CMakeLists.txt +++ b/lldb/unittests/CMakeLists.txt @@ -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) diff --git a/lldb/unittests/SBTestingSupport/CMakeLists.txt b/lldb/unittests/SBTestingSupport/CMakeLists.txt new file mode 100644 index 000000000000..4e259e4870a6 --- /dev/null +++ b/lldb/unittests/SBTestingSupport/CMakeLists.txt @@ -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 + ) diff --git a/lldb/unittests/SBTestingSupport/SBTestUtilities.cpp b/lldb/unittests/SBTestingSupport/SBTestUtilities.cpp new file mode 100644 index 000000000000..b22b76b33a26 --- /dev/null +++ b/lldb/unittests/SBTestingSupport/SBTestUtilities.cpp @@ -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_private::LoadCore(lldb::SBDebugger &debugger, llvm::StringRef binary_path, + llvm::StringRef core_path) { + EXPECT_TRUE(debugger); + + llvm::Expected binary_yaml = + lldb_private::TestFile::fromYamlFile(binary_path); + EXPECT_THAT_EXPECTED(binary_yaml, llvm::Succeeded()); + llvm::Expected 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 core_yaml = + lldb_private::TestFile::fromYamlFile(core_path); + EXPECT_THAT_EXPECTED(core_yaml, llvm::Succeeded()); + llvm::Expected 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); +} diff --git a/lldb/unittests/SBTestingSupport/SBTestUtilities.h b/lldb/unittests/SBTestingSupport/SBTestUtilities.h new file mode 100644 index 000000000000..2a8f8e12a963 --- /dev/null +++ b/lldb/unittests/SBTestingSupport/SBTestUtilities.h @@ -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 + +namespace lldb_private { + +/// Check if the debugger supports the given platform. +bool DebuggerSupportsLLVMTarget(llvm::StringRef target); + +std::pair LoadCore(lldb::SBDebugger &debugger, + llvm::StringRef binary_path, + llvm::StringRef core_path); + +} // namespace lldb_private + +#endif