Skip to content

Commit 338e907

Browse files
chelcassanovaAlexisPerry
authored andcommitted
Add a unit test for SBBreakpoint::SetCallback (llvm#96001)
This commit adds a unit test for SBBreakpoint::SetCallback as it wasn't being tested before.
1 parent c582d15 commit 338e907

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
@@ -99,10 +99,10 @@ typedef std::optional<FileSpec> (*SymbolLocatorLocateExecutableSymbolFile)(
9999
typedef bool (*SymbolLocatorDownloadObjectAndSymbolFile)(
100100
ModuleSpec &module_spec, Status &error, bool force_lookup,
101101
bool copy_executable);
102-
typedef bool (*BreakpointHitCallback)(void *baton,
103-
StoppointCallbackContext *context,
104-
lldb::user_id_t break_id,
105-
lldb::user_id_t break_loc_id);
102+
using BreakpointHitCallback =
103+
std::function<bool(void *baton, StoppointCallbackContext *context,
104+
lldb::user_id_t break_id, lldb::user_id_t break_loc_id)>;
105+
106106
typedef bool (*WatchpointHitCallback)(void *baton,
107107
StoppointCallbackContext *context,
108108
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
@@ -102,19 +102,11 @@ const char *BreakpointOptions::g_option_names[(
102102
"ConditionText", "IgnoreCount",
103103
"EnabledState", "OneShotState", "AutoContinue"};
104104

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

422414
void BreakpointOptions::ClearCallback() {
423-
m_callback = BreakpointOptions::NullCallback;
415+
m_callback = nullptr;
424416
m_callback_is_synchronous = false;
425417
m_callback_baton_sp.reset();
426418
m_baton_is_command_baton = false;
@@ -449,7 +441,7 @@ bool BreakpointOptions::InvokeCallback(StoppointCallbackContext *context,
449441
}
450442

451443
bool BreakpointOptions::HasCallback() const {
452-
return m_callback != BreakpointOptions::NullCallback;
444+
return static_cast<bool>(m_callback);
453445
}
454446

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

lldb/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
5252
add_subdirectory(API)
5353
endif()
5454
add_subdirectory(Breakpoint)
55+
add_subdirectory(Callback)
5556
add_subdirectory(Core)
5657
add_subdirectory(DataFormatter)
5758
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)