[gn build] Port 0154dce8d39d
[llvm-project.git] / lldb / unittests / Callback / TestBreakpointSetCallback.cpp
blob3dba4a9eb719e3eebf1fae7e976852ea435c2656
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 //===----------------------------------------------------------------------===//
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>
27 using namespace lldb_private;
28 using namespace lldb;
30 static constexpr lldb::user_id_t expected_breakpoint_id = 1;
31 static constexpr lldb::user_id_t expected_breakpoint_location_id = 0;
33 int baton_value;
35 class BreakpointSetCallbackTest : public ::testing::Test {
36 public:
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);
47 protected:
48 void SetUp() override {
49 std::call_once(TestUtilities::g_debugger_initialize_flag,
50 []() { Debugger::Initialize(nullptr); });
53 DebuggerSP m_debugger_sp;
54 PlatformSP m_platform_sp;
55 SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
56 subsystems;
59 TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
60 // Set up the debugger, make sure that was done properly.
61 TargetSP target_sp;
62 ArchSpec arch("x86_64-apple-macosx-");
63 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
65 m_debugger_sp = Debugger::CreateInstance();
67 // Create target
68 m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
69 lldb_private::eLoadDependentsNo,
70 m_platform_sp, target_sp);
72 // Create breakpoint
73 BreakpointSP breakpoint_sp =
74 target_sp->CreateBreakpoint(0xDEADBEEF, false, false);
76 breakpoint_sp->SetCallback(
77 [target_sp](void *baton, StoppointCallbackContext *context,
78 lldb::user_id_t break_id, lldb::user_id_t break_loc_id) {
79 CheckCallbackArgs(baton, context, break_id, break_loc_id, target_sp);
80 return true;
82 (void *)&baton_value, true);
83 ExecutionContext exe_ctx(target_sp, false);
84 StoppointCallbackContext context(nullptr, exe_ctx, true);
85 breakpoint_sp->InvokeCallback(&context, 0);