Skip to content

Commit 2cfcef6

Browse files
committed
add unit test for breakpoint::setcallback
1 parent 2c6f486 commit 2cfcef6

File tree

5 files changed

+120
-17
lines changed

5 files changed

+120
-17
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ 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+
// typedef bool (*BreakpointHitCallback)(void *baton,
103+
// StoppointCallbackContext *context,
104+
// lldb::user_id_t break_id,
105+
// lldb::user_id_t break_loc_id);
106+
typedef std::function<bool(void *baton, StoppointCallbackContext *context,
107+
lldb::user_id_t break_id,
108+
lldb::user_id_t break_loc_id)>
109+
BreakpointHitCallback;
110+
106111
typedef bool (*WatchpointHitCallback)(void *baton,
107112
StoppointCallbackContext *context,
108113
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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
#define EXPECTED_BREAKPOINT_ID 1
31+
#define 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+
lldb::user_id_t expected_breakpoint_id,
39+
lldb::user_id_t expected_breakpoint_loc_id,
40+
TargetSP expected_target_sp) {
41+
EXPECT_EQ(context->exe_ctx_ref.GetTargetSP(), expected_target_sp);
42+
EXPECT_EQ(baton, "hello");
43+
EXPECT_EQ(break_id, expected_breakpoint_id);
44+
EXPECT_EQ(break_loc_id, expected_breakpoint_loc_id);
45+
}
46+
47+
protected:
48+
void SetUp() override {
49+
std::call_once(TestUtilities::g_debugger_initialize_flag,
50+
[]() { Debugger::Initialize(nullptr); });
51+
52+
// Set up the debugger, make sure that was done properly.
53+
ArchSpec arch("x86_64-apple-macosx-");
54+
Platform::SetHostPlatform(
55+
PlatformRemoteMacOSX::CreateInstance(true, &arch));
56+
57+
m_debugger_sp = Debugger::CreateInstance();
58+
59+
// Create target
60+
m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
61+
lldb_private::eLoadDependentsNo,
62+
m_platform_sp, m_target_sp);
63+
// Create breakpoint
64+
m_breakpoint_sp = m_target_sp->CreateBreakpoint(0xDEADBEEF, false, false);
65+
};
66+
67+
DebuggerSP m_debugger_sp;
68+
PlatformSP m_platform_sp;
69+
TargetSP m_target_sp;
70+
BreakpointSP m_breakpoint_sp;
71+
lldb::user_id_t expected_breakpoint_id;
72+
lldb::user_id_t expected_breakpoint_loc_id;
73+
SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
74+
subsystems;
75+
};
76+
77+
TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
78+
void *baton = (void *)"hello";
79+
TargetSP current_target = m_target_sp;
80+
m_breakpoint_sp->SetCallback(
81+
[&current_target](void *baton, StoppointCallbackContext *context,
82+
lldb::user_id_t break_id,
83+
lldb::user_id_t break_loc_id) {
84+
CheckCallbackArgs(baton, context, break_id, break_loc_id,
85+
EXPECTED_BREAKPOINT_ID,
86+
EXPECTED_BREAKPOINT_LOCATION_ID, current_target);
87+
return true;
88+
},
89+
baton, true);
90+
ExecutionContext m_exe_ctx(m_target_sp, false);
91+
StoppointCallbackContext context(nullptr, m_exe_ctx, true);
92+
m_breakpoint_sp->InvokeCallback(&context, 0);
93+
}

0 commit comments

Comments
 (0)