[Lldb-commits] [lldb] Add a unit test for SBBreakpoint::SetCallback (PR #96001)

Chelsea Cassanova via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 21 16:19:01 PDT 2024


https://github.com/chelcassanova updated https://github.com/llvm/llvm-project/pull/96001

>From 44ea5e0d0a319fa12463129ff072bcaef6112544 Mon Sep 17 00:00:00 2001
From: Chelsea Cassanova <chelsea_cassanova at apple.com>
Date: Thu, 13 Jun 2024 16:02:07 -0700
Subject: [PATCH] add unit test for breakpoint::setcallback

---
 lldb/include/lldb/lldb-private-interfaces.h   |  8 +-
 lldb/source/Breakpoint/BreakpointOptions.cpp  | 18 ++--
 lldb/unittests/CMakeLists.txt                 |  1 +
 lldb/unittests/Callback/CMakeLists.txt        | 12 +++
 .../Callback/TestBreakpointSetCallback.cpp    | 89 +++++++++++++++++++
 5 files changed, 111 insertions(+), 17 deletions(-)
 create mode 100644 lldb/unittests/Callback/CMakeLists.txt
 create mode 100644 lldb/unittests/Callback/TestBreakpointSetCallback.cpp

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..d82ca660ab73f
--- /dev/null
+++ b/lldb/unittests/Callback/TestBreakpointSetCallback.cpp
@@ -0,0 +1,89 @@
+//===-- 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,
+                                lldb::user_id_t expected_breakpoint_id,
+                                lldb::user_id_t expected_breakpoint_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_loc_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,
+                          expected_breakpoint_id,
+                          expected_breakpoint_location_id, target_sp);
+        return true;
+      },
+      baton, true);
+  ExecutionContext exe_ctx(target_sp, false);
+  StoppointCallbackContext context(nullptr, exe_ctx, true);
+  breakpoint_sp->InvokeCallback(&context, 0);
+}



More information about the lldb-commits mailing list