diff options
author | Chelsea Cassanova <chelsea_cassanova@apple.com> | 2024-06-24 09:50:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-24 09:50:42 -0700 |
commit | 347206f9570446340da6d7dadc13d10b0aac4528 (patch) | |
tree | 6fa486614e6e28c8851d8c48d0812c48e3d7bb17 | |
parent | 6481dc57612671ebe77fe9c34214fba94e1b3b27 (diff) | |
download | llvm-347206f9570446340da6d7dadc13d10b0aac4528.zip llvm-347206f9570446340da6d7dadc13d10b0aac4528.tar.gz llvm-347206f9570446340da6d7dadc13d10b0aac4528.tar.bz2 |
Add a unit test for SBBreakpoint::SetCallback (#96001)
This commit adds a unit test for SBBreakpoint::SetCallback as it wasn't
being tested before.
-rw-r--r-- | lldb/include/lldb/lldb-private-interfaces.h | 8 | ||||
-rw-r--r-- | lldb/source/Breakpoint/BreakpointOptions.cpp | 18 | ||||
-rw-r--r-- | lldb/unittests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lldb/unittests/Callback/CMakeLists.txt | 12 | ||||
-rw-r--r-- | lldb/unittests/Callback/TestBreakpointSetCallback.cpp | 85 |
5 files changed, 107 insertions, 17 deletions
diff --git a/lldb/include/lldb/lldb-private-interfaces.h b/lldb/include/lldb/lldb-private-interfaces.h index 53d5fbb..cdd9b51 100644 --- a/lldb/include/lldb/lldb-private-interfaces.h +++ b/lldb/include/lldb/lldb-private-interfaces.h @@ -99,10 +99,10 @@ typedef std::optional<FileSpec> (*SymbolLocatorLocateExecutableSymbolFile)( typedef bool (*SymbolLocatorDownloadObjectAndSymbolFile)( ModuleSpec &module_spec, Status &error, bool force_lookup, bool copy_executable); -typedef bool (*BreakpointHitCallback)(void *baton, - StoppointCallbackContext *context, - lldb::user_id_t break_id, - lldb::user_id_t break_loc_id); +using BreakpointHitCallback = + std::function<bool(void *baton, StoppointCallbackContext *context, + lldb::user_id_t break_id, lldb::user_id_t break_loc_id)>; + typedef bool (*WatchpointHitCallback)(void *baton, StoppointCallbackContext *context, lldb::user_id_t watch_id); diff --git a/lldb/source/Breakpoint/BreakpointOptions.cpp b/lldb/source/Breakpoint/BreakpointOptions.cpp index 6c6037d..1db8401 100644 --- a/lldb/source/Breakpoint/BreakpointOptions.cpp +++ b/lldb/source/Breakpoint/BreakpointOptions.cpp @@ -102,19 +102,11 @@ const char *BreakpointOptions::g_option_names[( "ConditionText", "IgnoreCount", "EnabledState", "OneShotState", "AutoContinue"}; -bool BreakpointOptions::NullCallback(void *baton, - StoppointCallbackContext *context, - lldb::user_id_t break_id, - lldb::user_id_t break_loc_id) { - return true; -} - // BreakpointOptions constructor BreakpointOptions::BreakpointOptions(bool all_flags_set) - : m_callback(BreakpointOptions::NullCallback), - m_baton_is_command_baton(false), m_callback_is_synchronous(false), - m_enabled(true), m_one_shot(false), m_ignore_count(0), - m_condition_text_hash(0), m_inject_condition(false), + : m_callback(nullptr), m_baton_is_command_baton(false), + m_callback_is_synchronous(false), m_enabled(true), m_one_shot(false), + m_ignore_count(0), m_condition_text_hash(0), m_inject_condition(false), m_auto_continue(false), m_set_flags(0) { if (all_flags_set) m_set_flags.Set(~((Flags::ValueType)0)); @@ -420,7 +412,7 @@ void BreakpointOptions::SetCallback( } void BreakpointOptions::ClearCallback() { - m_callback = BreakpointOptions::NullCallback; + m_callback = nullptr; m_callback_is_synchronous = false; m_callback_baton_sp.reset(); m_baton_is_command_baton = false; @@ -449,7 +441,7 @@ bool BreakpointOptions::InvokeCallback(StoppointCallbackContext *context, } bool BreakpointOptions::HasCallback() const { - return m_callback != BreakpointOptions::NullCallback; + return static_cast<bool>(m_callback); } bool BreakpointOptions::GetCommandLineCallbacks(StringList &command_list) { diff --git a/lldb/unittests/CMakeLists.txt b/lldb/unittests/CMakeLists.txt index a2585a9..cc9d45e 100644 --- a/lldb/unittests/CMakeLists.txt +++ b/lldb/unittests/CMakeLists.txt @@ -52,6 +52,7 @@ if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows") add_subdirectory(API) endif() add_subdirectory(Breakpoint) +add_subdirectory(Callback) add_subdirectory(Core) add_subdirectory(DataFormatter) add_subdirectory(Disassembler) diff --git a/lldb/unittests/Callback/CMakeLists.txt b/lldb/unittests/Callback/CMakeLists.txt new file mode 100644 index 0000000..b9e0ef5 --- /dev/null +++ b/lldb/unittests/Callback/CMakeLists.txt @@ -0,0 +1,12 @@ +add_lldb_unittest(LLDBCallbackTests + TestBreakpointSetCallback.cpp + + LINK_LIBS + lldbBreakpoint + lldbCore + LLVMTestingSupport + lldbUtilityHelpers + lldbPluginPlatformMacOSX + LINK_COMPONENTS + Support + ) diff --git a/lldb/unittests/Callback/TestBreakpointSetCallback.cpp b/lldb/unittests/Callback/TestBreakpointSetCallback.cpp new file mode 100644 index 0000000..2a7070f --- /dev/null +++ b/lldb/unittests/Callback/TestBreakpointSetCallback.cpp @@ -0,0 +1,85 @@ +//===-- TestBreakpointSetCallback.cpp +//--------------------------------------------===// +// +// 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 "Plugins/Platform/MacOSX/PlatformMacOSX.h" +#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h" +#include "TestingSupport/SubsystemRAII.h" +#include "TestingSupport/TestUtilities.h" +#include "lldb/Breakpoint/StoppointCallbackContext.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/Progress.h" +#include "lldb/Host/FileSystem.h" +#include "lldb/Host/HostInfo.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/lldb-private-enumerations.h" +#include "lldb/lldb-types.h" +#include "gtest/gtest.h" +#include <iostream> +#include <memory> +#include <mutex> + +using namespace lldb_private; +using namespace lldb; + +static constexpr lldb::user_id_t expected_breakpoint_id = 1; +static constexpr lldb::user_id_t expected_breakpoint_location_id = 0; + +class BreakpointSetCallbackTest : public ::testing::Test { +public: + static void CheckCallbackArgs(void *baton, StoppointCallbackContext *context, + lldb::user_id_t break_id, + lldb::user_id_t break_loc_id, + TargetSP expected_target_sp) { + EXPECT_EQ(context->exe_ctx_ref.GetTargetSP(), expected_target_sp); + EXPECT_EQ(baton, "hello"); + EXPECT_EQ(break_id, expected_breakpoint_id); + EXPECT_EQ(break_loc_id, expected_breakpoint_location_id); + } + +protected: + void SetUp() override { + std::call_once(TestUtilities::g_debugger_initialize_flag, + []() { Debugger::Initialize(nullptr); }); + }; + + DebuggerSP m_debugger_sp; + PlatformSP m_platform_sp; + SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager> + subsystems; +}; + +TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) { + void *baton = (void *)"hello"; + // Set up the debugger, make sure that was done properly. + TargetSP target_sp; + ArchSpec arch("x86_64-apple-macosx-"); + Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch)); + + m_debugger_sp = Debugger::CreateInstance(); + + // Create target + m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch, + lldb_private::eLoadDependentsNo, + m_platform_sp, target_sp); + + // Create breakpoint + BreakpointSP breakpoint_sp = + target_sp->CreateBreakpoint(0xDEADBEEF, false, false); + + breakpoint_sp->SetCallback( + [target_sp](void *baton, StoppointCallbackContext *context, + lldb::user_id_t break_id, lldb::user_id_t break_loc_id) { + CheckCallbackArgs(baton, context, break_id, break_loc_id, target_sp); + return true; + }, + baton, true); + ExecutionContext exe_ctx(target_sp, false); + StoppointCallbackContext context(nullptr, exe_ctx, true); + breakpoint_sp->InvokeCallback(&context, 0); +} |