Author: Chelsea Cassanova
Date: 2024-06-24T09:50:42-07:00
New Revision: 347206f9570446340da6d7dadc13d10b0aac4528

URL: 
https://github.com/llvm/llvm-project/commit/347206f9570446340da6d7dadc13d10b0aac4528
DIFF: 
https://github.com/llvm/llvm-project/commit/347206f9570446340da6d7dadc13d10b0aac4528.diff

LOG: Add a unit test for SBBreakpoint::SetCallback (#96001)

This commit adds a unit test for SBBreakpoint::SetCallback as it wasn't
being tested before.

Added: 
    lldb/unittests/Callback/CMakeLists.txt
    lldb/unittests/Callback/TestBreakpointSetCallback.cpp

Modified: 
    lldb/include/lldb/lldb-private-interfaces.h
    lldb/source/Breakpoint/BreakpointOptions.cpp
    lldb/unittests/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/lldb-private-interfaces.h 
b/lldb/include/lldb/lldb-private-interfaces.h
index 53d5fbb84cc92..cdd9b51d9329c 100644
--- a/lldb/include/lldb/lldb-private-interfaces.h
+++ b/lldb/include/lldb/lldb-private-interfaces.h
@@ -99,10 +99,10 @@ typedef std::optional<FileSpec> 
(*SymbolLocatorLocateExecutableSymbolFile)(
 typedef bool (*SymbolLocatorDownloadObjectAndSymbolFile)(
     ModuleSpec &module_spec, Status &error, bool force_lookup,
     bool copy_executable);
-typedef bool (*BreakpointHitCallback)(void *baton,
-                                      StoppointCallbackContext *context,
-                                      lldb::user_id_t break_id,
-                                      lldb::user_id_t break_loc_id);
+using BreakpointHitCallback =
+    std::function<bool(void *baton, StoppointCallbackContext *context,
+                       lldb::user_id_t break_id, lldb::user_id_t 
break_loc_id)>;
+
 typedef bool (*WatchpointHitCallback)(void *baton,
                                       StoppointCallbackContext *context,
                                       lldb::user_id_t watch_id);

diff  --git a/lldb/source/Breakpoint/BreakpointOptions.cpp 
b/lldb/source/Breakpoint/BreakpointOptions.cpp
index 6c6037dd9edd3..1db8401698114 100644
--- a/lldb/source/Breakpoint/BreakpointOptions.cpp
+++ b/lldb/source/Breakpoint/BreakpointOptions.cpp
@@ -102,19 +102,11 @@ const char *BreakpointOptions::g_option_names[(
     "ConditionText", "IgnoreCount", 
     "EnabledState", "OneShotState", "AutoContinue"};
 
-bool BreakpointOptions::NullCallback(void *baton,
-                                     StoppointCallbackContext *context,
-                                     lldb::user_id_t break_id,
-                                     lldb::user_id_t break_loc_id) {
-  return true;
-}
-
 // BreakpointOptions constructor
 BreakpointOptions::BreakpointOptions(bool all_flags_set)
-    : m_callback(BreakpointOptions::NullCallback),
-      m_baton_is_command_baton(false), m_callback_is_synchronous(false),
-      m_enabled(true), m_one_shot(false), m_ignore_count(0),
-      m_condition_text_hash(0), m_inject_condition(false),
+    : m_callback(nullptr), m_baton_is_command_baton(false),
+      m_callback_is_synchronous(false), m_enabled(true), m_one_shot(false),
+      m_ignore_count(0), m_condition_text_hash(0), m_inject_condition(false),
       m_auto_continue(false), m_set_flags(0) {
   if (all_flags_set)
     m_set_flags.Set(~((Flags::ValueType)0));
@@ -420,7 +412,7 @@ void BreakpointOptions::SetCallback(
 }
 
 void BreakpointOptions::ClearCallback() {
-  m_callback = BreakpointOptions::NullCallback;
+  m_callback = nullptr;
   m_callback_is_synchronous = false;
   m_callback_baton_sp.reset();
   m_baton_is_command_baton = false;
@@ -449,7 +441,7 @@ bool 
BreakpointOptions::InvokeCallback(StoppointCallbackContext *context,
 }
 
 bool BreakpointOptions::HasCallback() const {
-  return m_callback != BreakpointOptions::NullCallback;
+  return static_cast<bool>(m_callback);
 }
 
 bool BreakpointOptions::GetCommandLineCallbacks(StringList &command_list) {

diff  --git a/lldb/unittests/CMakeLists.txt b/lldb/unittests/CMakeLists.txt
index a2585a94b6155..cc9d45ebf981d 100644
--- a/lldb/unittests/CMakeLists.txt
+++ b/lldb/unittests/CMakeLists.txt
@@ -52,6 +52,7 @@ if (NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
   add_subdirectory(API)
 endif()
 add_subdirectory(Breakpoint)
+add_subdirectory(Callback)
 add_subdirectory(Core)
 add_subdirectory(DataFormatter)
 add_subdirectory(Disassembler)

diff  --git a/lldb/unittests/Callback/CMakeLists.txt 
b/lldb/unittests/Callback/CMakeLists.txt
new file mode 100644
index 0000000000000..b9e0ef5a396e3
--- /dev/null
+++ b/lldb/unittests/Callback/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_lldb_unittest(LLDBCallbackTests
+  TestBreakpointSetCallback.cpp
+
+  LINK_LIBS
+    lldbBreakpoint
+    lldbCore
+    LLVMTestingSupport
+    lldbUtilityHelpers
+    lldbPluginPlatformMacOSX
+  LINK_COMPONENTS
+    Support
+  )

diff  --git a/lldb/unittests/Callback/TestBreakpointSetCallback.cpp 
b/lldb/unittests/Callback/TestBreakpointSetCallback.cpp
new file mode 100644
index 0000000000000..2a7070f9349c0
--- /dev/null
+++ b/lldb/unittests/Callback/TestBreakpointSetCallback.cpp
@@ -0,0 +1,85 @@
+//===-- TestBreakpointSetCallback.cpp
+//--------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
+#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Breakpoint/StoppointCallbackContext.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/Progress.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/ExecutionContext.h"
+#include "lldb/lldb-private-enumerations.h"
+#include "lldb/lldb-types.h"
+#include "gtest/gtest.h"
+#include <iostream>
+#include <memory>
+#include <mutex>
+
+using namespace lldb_private;
+using namespace lldb;
+
+static constexpr lldb::user_id_t expected_breakpoint_id = 1;
+static constexpr lldb::user_id_t expected_breakpoint_location_id = 0;
+
+class BreakpointSetCallbackTest : public ::testing::Test {
+public:
+  static void CheckCallbackArgs(void *baton, StoppointCallbackContext *context,
+                                lldb::user_id_t break_id,
+                                lldb::user_id_t break_loc_id,
+                                TargetSP expected_target_sp) {
+    EXPECT_EQ(context->exe_ctx_ref.GetTargetSP(), expected_target_sp);
+    EXPECT_EQ(baton, "hello");
+    EXPECT_EQ(break_id, expected_breakpoint_id);
+    EXPECT_EQ(break_loc_id, expected_breakpoint_location_id);
+  }
+
+protected:
+  void SetUp() override {
+    std::call_once(TestUtilities::g_debugger_initialize_flag,
+                   []() { Debugger::Initialize(nullptr); });
+  };
+
+  DebuggerSP m_debugger_sp;
+  PlatformSP m_platform_sp;
+  SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
+      subsystems;
+};
+
+TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
+  void *baton = (void *)"hello";
+  // Set up the debugger, make sure that was done properly.
+  TargetSP target_sp;
+  ArchSpec arch("x86_64-apple-macosx-");
+  Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
+
+  m_debugger_sp = Debugger::CreateInstance();
+
+  // Create target
+  m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
+                                              lldb_private::eLoadDependentsNo,
+                                              m_platform_sp, target_sp);
+
+  // Create breakpoint
+  BreakpointSP breakpoint_sp =
+      target_sp->CreateBreakpoint(0xDEADBEEF, false, false);
+
+  breakpoint_sp->SetCallback(
+      [target_sp](void *baton, StoppointCallbackContext *context,
+                  lldb::user_id_t break_id, lldb::user_id_t break_loc_id) {
+        CheckCallbackArgs(baton, context, break_id, break_loc_id, target_sp);
+        return true;
+      },
+      baton, true);
+  ExecutionContext exe_ctx(target_sp, false);
+  StoppointCallbackContext context(nullptr, exe_ctx, true);
+  breakpoint_sp->InvokeCallback(&context, 0);
+}


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to