Skip to content

Commit 4523e78

Browse files
authored
Merge pull request #8924 from chelcassanova/cherrypick/breakpoint-callback-unittest
Add a unit test for SBBreakpoint::SetCallback (llvm#96001)
2 parents 3602748 + 5db68bf commit 4523e78

File tree

5 files changed

+107
-17
lines changed

5 files changed

+107
-17
lines changed

lldb/include/lldb/lldb-private-interfaces.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ typedef std::optional<FileSpec> (*SymbolLocatorLocateExecutableSymbolFile)(
101101
typedef bool (*SymbolLocatorDownloadObjectAndSymbolFile)(
102102
ModuleSpec &module_spec, Status &error, bool force_lookup,
103103
bool copy_executable);
104-
typedef bool (*BreakpointHitCallback)(void *baton,
105-
StoppointCallbackContext *context,
106-
lldb::user_id_t break_id,
107-
lldb::user_id_t break_loc_id);
104+
using BreakpointHitCallback =
105+
std::function<bool(void *baton, StoppointCallbackContext *context,
106+
lldb::user_id_t break_id, lldb::user_id_t break_loc_id)>;
107+
108108
typedef bool (*WatchpointHitCallback)(void *baton,
109109
StoppointCallbackContext *context,
110110
lldb::user_id_t watch_id);

lldb/source/Breakpoint/BreakpointOptions.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,11 @@ const char *BreakpointOptions::g_option_names[(
103103
"ConditionText", "IgnoreCount",
104104
"EnabledState", "OneShotState", "AutoContinue"};
105105

106-
bool BreakpointOptions::NullCallback(void *baton,
107-
StoppointCallbackContext *context,
108-
lldb::user_id_t break_id,
109-
lldb::user_id_t break_loc_id) {
110-
return true;
111-
}
112-
113106
// BreakpointOptions constructor
114107
BreakpointOptions::BreakpointOptions(bool all_flags_set)
115-
: m_callback(BreakpointOptions::NullCallback),
116-
m_baton_is_command_baton(false), m_callback_is_synchronous(false),
117-
m_enabled(true), m_one_shot(false), m_ignore_count(0),
118-
m_condition_text_hash(0), m_inject_condition(false),
108+
: m_callback(nullptr), m_baton_is_command_baton(false),
109+
m_callback_is_synchronous(false), m_enabled(true), m_one_shot(false),
110+
m_ignore_count(0), m_condition_text_hash(0), m_inject_condition(false),
119111
m_auto_continue(false), m_set_flags(0) {
120112
if (all_flags_set)
121113
m_set_flags.Set(~((Flags::ValueType)0));
@@ -421,7 +413,7 @@ void BreakpointOptions::SetCallback(
421413
}
422414

423415
void BreakpointOptions::ClearCallback() {
424-
m_callback = BreakpointOptions::NullCallback;
416+
m_callback = nullptr;
425417
m_callback_is_synchronous = false;
426418
m_callback_baton_sp.reset();
427419
m_baton_is_command_baton = false;
@@ -450,7 +442,7 @@ bool BreakpointOptions::InvokeCallback(StoppointCallbackContext *context,
450442
}
451443

452444
bool BreakpointOptions::HasCallback() const {
453-
return m_callback != BreakpointOptions::NullCallback;
445+
return static_cast<bool>(m_callback);
454446
}
455447

456448
bool BreakpointOptions::GetCommandLineCallbacks(StringList &command_list) {

lldb/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
5858
add_subdirectory(API)
5959
endif()
6060
add_subdirectory(Breakpoint)
61+
add_subdirectory(Callback)
6162
add_subdirectory(Core)
6263
add_subdirectory(DataFormatter)
6364
add_subdirectory(Disassembler)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_lldb_unittest(LLDBCallbackTests
2+
TestBreakpointSetCallback.cpp
3+
4+
LINK_LIBS
5+
lldbBreakpoint
6+
lldbCore
7+
LLVMTestingSupport
8+
lldbUtilityHelpers
9+
lldbPluginPlatformMacOSX
10+
LINK_COMPONENTS
11+
Support
12+
)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===-- TestBreakpointSetCallback.cpp
2+
//--------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
11+
#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
12+
#include "TestingSupport/SubsystemRAII.h"
13+
#include "TestingSupport/TestUtilities.h"
14+
#include "lldb/Breakpoint/StoppointCallbackContext.h"
15+
#include "lldb/Core/Debugger.h"
16+
#include "lldb/Core/Progress.h"
17+
#include "lldb/Host/FileSystem.h"
18+
#include "lldb/Host/HostInfo.h"
19+
#include "lldb/Target/ExecutionContext.h"
20+
#include "lldb/lldb-private-enumerations.h"
21+
#include "lldb/lldb-types.h"
22+
#include "gtest/gtest.h"
23+
#include <iostream>
24+
#include <memory>
25+
#include <mutex>
26+
27+
using namespace lldb_private;
28+
using namespace lldb;
29+
30+
static constexpr lldb::user_id_t expected_breakpoint_id = 1;
31+
static constexpr lldb::user_id_t expected_breakpoint_location_id = 0;
32+
33+
class BreakpointSetCallbackTest : public ::testing::Test {
34+
public:
35+
static void CheckCallbackArgs(void *baton, StoppointCallbackContext *context,
36+
lldb::user_id_t break_id,
37+
lldb::user_id_t break_loc_id,
38+
TargetSP expected_target_sp) {
39+
EXPECT_EQ(context->exe_ctx_ref.GetTargetSP(), expected_target_sp);
40+
EXPECT_EQ(baton, "hello");
41+
EXPECT_EQ(break_id, expected_breakpoint_id);
42+
EXPECT_EQ(break_loc_id, expected_breakpoint_location_id);
43+
}
44+
45+
protected:
46+
void SetUp() override {
47+
std::call_once(TestUtilities::g_debugger_initialize_flag,
48+
[]() { Debugger::Initialize(nullptr); });
49+
};
50+
51+
DebuggerSP m_debugger_sp;
52+
PlatformSP m_platform_sp;
53+
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
54+
subsystems;
55+
};
56+
57+
TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
58+
void *baton = (void *)"hello";
59+
// Set up the debugger, make sure that was done properly.
60+
TargetSP target_sp;
61+
ArchSpec arch("x86_64-apple-macosx-");
62+
Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
63+
64+
m_debugger_sp = Debugger::CreateInstance();
65+
66+
// Create target
67+
m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
68+
lldb_private::eLoadDependentsNo,
69+
m_platform_sp, target_sp);
70+
71+
// Create breakpoint
72+
BreakpointSP breakpoint_sp =
73+
target_sp->CreateBreakpoint(0xDEADBEEF, false, false);
74+
75+
breakpoint_sp->SetCallback(
76+
[target_sp](void *baton, StoppointCallbackContext *context,
77+
lldb::user_id_t break_id, lldb::user_id_t break_loc_id) {
78+
CheckCallbackArgs(baton, context, break_id, break_loc_id, target_sp);
79+
return true;
80+
},
81+
baton, true);
82+
ExecutionContext exe_ctx(target_sp, false);
83+
StoppointCallbackContext context(nullptr, exe_ctx, true);
84+
breakpoint_sp->InvokeCallback(&context, 0);
85+
}

0 commit comments

Comments
 (0)