Skip to content

Add a unit test for SBBreakpoint::SetCallback (#96001) #8924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lldb/include/lldb/lldb-private-interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,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);
Expand Down
18 changes: 5 additions & 13 deletions lldb/source/Breakpoint/BreakpointOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,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));
Expand Down Expand Up @@ -421,7 +413,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;
Expand Down Expand Up @@ -450,7 +442,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) {
Expand Down
1 change: 1 addition & 0 deletions lldb/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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)
Expand Down
12 changes: 12 additions & 0 deletions lldb/unittests/Callback/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_lldb_unittest(LLDBCallbackTests
TestBreakpointSetCallback.cpp

LINK_LIBS
lldbBreakpoint
lldbCore
LLVMTestingSupport
lldbUtilityHelpers
lldbPluginPlatformMacOSX
LINK_COMPONENTS
Support
)
85 changes: 85 additions & 0 deletions lldb/unittests/Callback/TestBreakpointSetCallback.cpp
Original file line number Diff line number Diff line change
@@ -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);
}