Skip to content

Commit f3e2c20

Browse files
authored
Make SBBreakpoint/SBBreakpointLocation.SetCondition(nullptr) work again. (#162370)
The addition of the StopCondition in the lldb_private layer meant that clearing a breakpoint condition with: sb_break.SetCondition(nullptr); now crashes. Also, GetCondition for an empty condition used to return a nullptr, but now it returns "". This patch fixes that crash and makes the SB GetCondition always return nullptr for an empty condition.
1 parent 6359980 commit f3e2c20

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

lldb/source/API/SBBreakpoint.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ void SBBreakpoint::SetCondition(const char *condition) {
275275
if (bkpt_sp) {
276276
std::lock_guard<std::recursive_mutex> guard(
277277
bkpt_sp->GetTarget().GetAPIMutex());
278-
bkpt_sp->SetCondition(StopCondition(condition));
278+
// Treat a null pointer as resetting the condition.
279+
if (!condition)
280+
bkpt_sp->SetCondition(StopCondition());
281+
else
282+
bkpt_sp->SetCondition(StopCondition(condition));
279283
}
280284
}
281285

@@ -288,7 +292,10 @@ const char *SBBreakpoint::GetCondition() {
288292

289293
std::lock_guard<std::recursive_mutex> guard(
290294
bkpt_sp->GetTarget().GetAPIMutex());
291-
return ConstString(bkpt_sp->GetCondition().GetText()).GetCString();
295+
StopCondition cond = bkpt_sp->GetCondition();
296+
if (!cond)
297+
return nullptr;
298+
return ConstString(cond.GetText()).GetCString();
292299
}
293300

294301
void SBBreakpoint::SetAutoContinue(bool auto_continue) {

lldb/source/API/SBBreakpointLocation.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ void SBBreakpointLocation::SetCondition(const char *condition) {
160160
if (loc_sp) {
161161
std::lock_guard<std::recursive_mutex> guard(
162162
loc_sp->GetTarget().GetAPIMutex());
163-
loc_sp->SetCondition(StopCondition(condition));
163+
// Treat a nullptr as clearing the condition
164+
if (!condition)
165+
loc_sp->SetCondition(StopCondition());
166+
else
167+
loc_sp->SetCondition(StopCondition(condition));
164168
}
165169
}
166170

@@ -173,7 +177,10 @@ const char *SBBreakpointLocation::GetCondition() {
173177

174178
std::lock_guard<std::recursive_mutex> guard(
175179
loc_sp->GetTarget().GetAPIMutex());
176-
return ConstString(loc_sp->GetCondition().GetText()).GetCString();
180+
StopCondition cond = loc_sp->GetCondition();
181+
if (!cond)
182+
return nullptr;
183+
return ConstString(cond.GetText()).GetCString();
177184
}
178185

179186
void SBBreakpointLocation::SetAutoContinue(bool auto_continue) {

lldb/unittests/API/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_lldb_unittest(APITests
22
SBCommandInterpreterTest.cpp
33
SBLineEntryTest.cpp
44
SBMutexTest.cpp
5+
SBBreakpointClearConditionTest.cpp
56

67
SBAPITEST
78

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Use the umbrella header for -Wdocumentation.
9+
#include "lldb/API/LLDB.h"
10+
11+
#include "TestingSupport/SubsystemRAII.h"
12+
#include "lldb/API/SBBreakpoint.h"
13+
#include "lldb/API/SBBreakpointLocation.h"
14+
#include "lldb/API/SBDebugger.h"
15+
#include "lldb/API/SBTarget.h"
16+
#include "gtest/gtest.h"
17+
#include <memory>
18+
#include <mutex>
19+
20+
using namespace lldb_private;
21+
using namespace lldb;
22+
23+
class BreakpointClearConditionTest : public ::testing::Test {
24+
public:
25+
void SetUp() override {
26+
m_sb_debugger = SBDebugger::Create(/*source_init_files=*/false);
27+
};
28+
29+
void TearDown() override { SBDebugger::Destroy(m_sb_debugger); }
30+
SBDebugger m_sb_debugger;
31+
SubsystemRAII<lldb::SBDebugger> subsystems;
32+
};
33+
34+
template <typename T> void test_condition(T sb_object) {
35+
const char *in_cond_str = "Here is a condition";
36+
sb_object.SetCondition(in_cond_str);
37+
// Make sure we set the condition correctly:
38+
const char *out_cond_str = sb_object.GetCondition();
39+
EXPECT_STREQ(in_cond_str, out_cond_str);
40+
// Now unset it by passing in nullptr and make sure that works:
41+
const char *empty_tokens[2] = {nullptr, ""};
42+
for (auto token : empty_tokens) {
43+
sb_object.SetCondition(token);
44+
out_cond_str = sb_object.GetCondition();
45+
// And make sure an unset condition returns nullptr:
46+
EXPECT_EQ(nullptr, out_cond_str);
47+
}
48+
}
49+
50+
TEST_F(BreakpointClearConditionTest, BreakpointClearConditionTest) {
51+
// Create target
52+
SBTarget sb_target;
53+
SBError error;
54+
sb_target =
55+
m_sb_debugger.CreateTarget("", "x86_64-apple-macosx-", "remote-macosx",
56+
/*add_dependent=*/false, error);
57+
58+
EXPECT_EQ(sb_target.IsValid(), true);
59+
60+
// Create breakpoint
61+
SBBreakpoint sb_breakpoint = sb_target.BreakpointCreateByAddress(0xDEADBEEF);
62+
test_condition(sb_breakpoint);
63+
64+
// Address breakpoints always have one location, so we can also use this
65+
// to test the location:
66+
SBBreakpointLocation sb_loc = sb_breakpoint.GetLocationAtIndex(0);
67+
EXPECT_EQ(sb_loc.IsValid(), true);
68+
test_condition(sb_loc);
69+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
add_lldb_unittest(LLDBBreakpointTests
1+
add_lldb_unittest(LLDBBreakpointTests
22
BreakpointIDTest.cpp
33
WatchpointAlgorithmsTests.cpp
44

55
LINK_COMPONENTS
66
Support
77
LINK_LIBS
8+
liblldb
89
lldbBreakpoint
910
lldbCore
11+
LLVMTestingSupport
12+
lldbUtilityHelpers
13+
lldbPluginPlatformMacOSX
1014
)

0 commit comments

Comments
 (0)