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
27using namespace lldb_private;
28using namespace lldb;
29
30static constexpr lldb::user_id_t expected_breakpoint_id = 1;
31static constexpr lldb::user_id_t expected_breakpoint_location_id = 0;
32
33int baton_value;
34
35class BreakpointSetCallbackTest : public ::testing::Test {
36public:
37 static void CheckCallbackArgs(void *baton, StoppointCallbackContext *context,
38 lldb::user_id_t break_id,
39 lldb::user_id_t break_loc_id,
40 TargetSP expected_target_sp) {
41 EXPECT_EQ(context->exe_ctx_ref.GetTargetSP(), expected_target_sp);
42 EXPECT_EQ(baton, &baton_value);
43 EXPECT_EQ(break_id, expected_breakpoint_id);
44 EXPECT_EQ(break_loc_id, expected_breakpoint_location_id);
45 }
46
47protected:
48 void SetUp() override {
49 std::call_once(once&: TestUtilities::g_debugger_initialize_flag,
50 f: []() { Debugger::Initialize(load_plugin_callback: nullptr); });
51 };
52
53 DebuggerSP m_debugger_sp;
54 PlatformSP m_platform_sp;
55 SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX> subsystems;
56};
57
58TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
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(force: true, arch: &arch));
63
64 m_debugger_sp = Debugger::CreateInstance();
65
66 // Create target
67 m_debugger_sp->GetTargetList().CreateTarget(debugger&: *m_debugger_sp, user_exe_path: "", arch,
68 get_dependent_modules: lldb_private::eLoadDependentsNo,
69 platform_sp&: m_platform_sp, target_sp);
70
71 // Create breakpoint
72 BreakpointSP breakpoint_sp =
73 target_sp->CreateBreakpoint(load_addr: 0xDEADBEEF, internal: false, request_hardware: false);
74
75 breakpoint_sp->SetCallback(
76 callback: [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, expected_target_sp: target_sp);
79 return true;
80 },
81 baton: (void *)&baton_value, is_synchronous: true);
82 ExecutionContext exe_ctx(target_sp, false);
83 StoppointCallbackContext context(nullptr, exe_ctx, true);
84 breakpoint_sp->InvokeCallback(context: &context, bp_loc_id: 0);
85}
86

source code of lldb/unittests/Callback/TestBreakpointSetCallback.cpp