Skip to content

Add a unit test for SBBreakpoint::SetCallback #96001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

chelcassanova
Copy link
Contributor

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

@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2024

@llvm/pr-subscribers-lldb

Author: Chelsea Cassanova (chelcassanova)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/96001.diff

2 Files Affected:

  • (modified) lldb/unittests/Breakpoint/CMakeLists.txt (+4)
  • (added) lldb/unittests/Breakpoint/TestBreakpointSetCallback.cpp (+90)
diff --git a/lldb/unittests/Breakpoint/CMakeLists.txt b/lldb/unittests/Breakpoint/CMakeLists.txt
index 757c2da1a4d9d..858a2151c503b 100644
--- a/lldb/unittests/Breakpoint/CMakeLists.txt
+++ b/lldb/unittests/Breakpoint/CMakeLists.txt
@@ -1,10 +1,14 @@
 add_lldb_unittest(LLDBBreakpointTests
   BreakpointIDTest.cpp
   WatchpointAlgorithmsTests.cpp
+  TestBreakpointSetCallback.cpp
 
   LINK_LIBS
     lldbBreakpoint
     lldbCore
+    LLVMTestingSupport
+    lldbUtilityHelpers
+    lldbPluginPlatformMacOSX
   LINK_COMPONENTS
     Support
   )
diff --git a/lldb/unittests/Breakpoint/TestBreakpointSetCallback.cpp b/lldb/unittests/Breakpoint/TestBreakpointSetCallback.cpp
new file mode 100644
index 0000000000000..351384230f15f
--- /dev/null
+++ b/lldb/unittests/Breakpoint/TestBreakpointSetCallback.cpp
@@ -0,0 +1,90 @@
+//===-- 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;
+
+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) {
+    std::cout << "HELLO" << std::endl;
+    EXPECT_TRUE(baton);
+    EXPECT_TRUE(context);
+    EXPECT_EQ(break_id, expected_breakpoint_id);
+    EXPECT_EQ(break_loc_id, expected_breakpoint_loc_id);
+  }
+
+protected:
+  // The debugger's initialization function can't be called with no arguments
+  // so calling it using SubsystemRAII will cause the test build to fail as
+  // SubsystemRAII will call Initialize with no arguments. As such we set it up
+  // here the usual way.
+  void SetUp() override {
+    std::call_once(TestUtilities::g_debugger_initialize_flag,
+                   []() { Debugger::Initialize(nullptr); });
+
+    // Set up the debugger, make sure that was done properly.
+    ArchSpec arch("x86_64-apple-macosx-");
+    Platform::SetHostPlatform(
+        PlatformRemoteMacOSX::CreateInstance(true, &arch));
+
+    m_debugger_sp = Debugger::CreateInstance();
+    m_debugger_sp->GetTargetList().CreateTarget(*m_debugger_sp, "", arch,
+                                                lldb_private::eLoadDependentsNo,
+                                                m_platform_sp, m_target_sp);
+    m_breakpoint_sp = m_target_sp->CreateBreakpoint(0xDEADBEEF, false, false);
+  };
+
+  static bool callback(void *baton, StoppointCallbackContext *context,
+                       lldb::user_id_t break_id, lldb::user_id_t break_loc_id) {
+    BreakpointSetCallbackTest::CheckCallbackArgs(baton, context, break_id,
+                                                 break_loc_id, 0, 0);
+    return true;
+  }
+
+  DebuggerSP m_debugger_sp;
+  PlatformSP m_platform_sp;
+  TargetSP m_target_sp;
+  BreakpointSP m_breakpoint_sp;
+  Event *m_event;
+  const ExecutionContext m_exe_ctx;
+  lldb::user_id_t expected_breakpoint_id;
+  lldb::user_id_t expected_breakpoint_loc_id;
+  SubsystemRAII<FileSystem, HostInfo, PlatformMacOSX, ProgressManager>
+      subsystems;
+};
+
+TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
+  void *baton = (void *)"hello";
+  StoppointCallbackContext context(m_event, m_exe_ctx, true);
+  m_breakpoint_sp->SetCallback(BreakpointSetCallbackTest::callback, baton,
+                               false);
+  m_breakpoint_sp->InvokeCallback(&context, 0);
+}

@chelcassanova chelcassanova force-pushed the breakpoint-callback-unittest branch from dc51494 to 0dc8040 Compare June 18, 2024 22:34
Copy link
Member

@medismailben medismailben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start but still requires some work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not anymore, I'll remove these.

static bool callback(void *baton, StoppointCallbackContext *context,
lldb::user_id_t break_id, lldb::user_id_t break_loc_id) {
BreakpointSetCallbackTest::CheckCallbackArgs(baton, context, break_id,
break_loc_id, 0, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the breakpoint (location) ids started at 1, how come your expected break_id & break_loc_id are 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, those should be 1 😅

Comment on lines 85 to 86
StoppointCallbackContext context(m_event, m_exe_ctx, true);
m_breakpoint_sp->SetCallback(BreakpointSetCallbackTest::callback, baton,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can swap these 2 lines.

Comment on lines 37 to 38
EXPECT_TRUE(baton);
EXPECT_TRUE(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do more with these (i.e. check that actually baton point to the "hello" string).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An EXPECT_EQ for baton being "hello" right? I'll add that. Also, should I check anything more for the StoppointCallbackContext being true here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could check that the ExecutionContext's target matches the m_target_sp if you want.


TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
void *baton = (void *)"hello";
StoppointCallbackContext context(m_event, m_exe_ctx, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like m_event never gets initialized so I'd just pass a nullptr here and remove it from the test class.

Comment on lines 37 to 38
EXPECT_TRUE(baton);
EXPECT_TRUE(context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could check that the ExecutionContext's target matches the m_target_sp if you want.

@chelcassanova chelcassanova force-pushed the breakpoint-callback-unittest branch 2 times, most recently from aa575bb to 2cfcef6 Compare June 20, 2024 21:28
Copy link
Member

@medismailben medismailben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with comments addressed.

StoppointCallbackContext *context,
lldb::user_id_t break_id,
lldb::user_id_t break_loc_id);
// typedef bool (*BreakpointHitCallback)(void *baton,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deadcode ?

// StoppointCallbackContext *context,
// lldb::user_id_t break_id,
// lldb::user_id_t break_loc_id);
typedef std::function<bool(void *baton, StoppointCallbackContext *context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do using instead of typedef here ?


TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
void *baton = (void *)"hello";
TargetSP current_target = m_target_sp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you don't need to make a new various from m_target_sp, just capture it directly in the lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That only works if I capture this, trying to capture just m_target_sp won't work (unless we move the target setup into the test body)

@chelcassanova chelcassanova force-pushed the breakpoint-callback-unittest branch from 2cfcef6 to 86dcffc Compare June 21, 2024 22:05
using namespace lldb_private;
using namespace lldb;

#define EXPECTED_BREAKPOINT_ID 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make these static constexpr lldb::user_id_t if you want

TEST_F(BreakpointSetCallbackTest, TestBreakpointSetCallback) {
void *baton = (void *)"hello";
// Set up the debugger, make sure that was done properly.
TargetSP m_target_sp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an class member, why prepend the m_

return true;
},
baton, true);
ExecutionContext m_exe_ctx(m_target_sp, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

m_platform_sp, m_target_sp);

// Create breakpoint
m_breakpoint_sp = m_target_sp->CreateBreakpoint(0xDEADBEEF, false, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you don't need to make the breakpoint a class member here

@chelcassanova chelcassanova force-pushed the breakpoint-callback-unittest branch from 86dcffc to 44ea5e0 Compare June 21, 2024 23:18
Copy link
Member

@medismailben medismailben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comment

Comment on lines 38 to 39
lldb::user_id_t expected_breakpoint_id,
lldb::user_id_t expected_breakpoint_loc_id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to pass them here, since they're static

@chelcassanova chelcassanova force-pushed the breakpoint-callback-unittest branch from 44ea5e0 to a59949c Compare June 22, 2024 01:04
@medismailben
Copy link
Member

Awesome! 🚢 it!

@chelcassanova chelcassanova merged commit 347206f into llvm:main Jun 24, 2024
6 checks passed
@chelcassanova chelcassanova deleted the breakpoint-callback-unittest branch June 24, 2024 16:51
chelcassanova added a commit to chelcassanova/llvm-project that referenced this pull request Jun 24, 2024
This commit adds a unit test for SBBreakpoint::SetCallback as it wasn't
being tested before.

(cherry picked from commit 347206f)
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Jul 8, 2024
…lback-unittest

Add a unit test for SBBreakpoint::SetCallback (llvm#96001)
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
This commit adds a unit test for SBBreakpoint::SetCallback as it wasn't
being tested before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants