Skip to content

Commit 46457ed

Browse files
authored
[lldb] Convert Breakpoint & Watchpoints structs to classes (NFC) (#133780)
Convert Breakpoint & Watchpoints structs to classes to provide proper access control. This is in preparation for adopting SBMutex to protect the underlying SBBreakpoint and SBWatchpoint.
1 parent 40c859a commit 46457ed

24 files changed

+217
-178
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919

2020
using namespace lldb_dap;
2121

22-
void Breakpoint::SetCondition() { bp.SetCondition(condition.c_str()); }
22+
void Breakpoint::SetCondition() { m_bp.SetCondition(m_condition.c_str()); }
2323

2424
void Breakpoint::SetHitCondition() {
2525
uint64_t hitCount = 0;
26-
if (llvm::to_integer(hitCondition, hitCount))
27-
bp.SetIgnoreCount(hitCount - 1);
26+
if (llvm::to_integer(m_hit_condition, hitCount))
27+
m_bp.SetIgnoreCount(hitCount - 1);
2828
}
2929

3030
void Breakpoint::CreateJsonObject(llvm::json::Object &object) {
3131
// Each breakpoint location is treated as a separate breakpoint for VS code.
3232
// They don't have the notion of a single breakpoint with multiple locations.
33-
if (!bp.IsValid())
33+
if (!m_bp.IsValid())
3434
return;
35-
object.try_emplace("verified", bp.GetNumResolvedLocations() > 0);
36-
object.try_emplace("id", bp.GetID());
35+
object.try_emplace("verified", m_bp.GetNumResolvedLocations() > 0);
36+
object.try_emplace("id", m_bp.GetID());
3737
// VS Code DAP doesn't currently allow one breakpoint to have multiple
3838
// locations so we just report the first one. If we report all locations
3939
// then the IDE starts showing the wrong line numbers and locations for
@@ -43,20 +43,20 @@ void Breakpoint::CreateJsonObject(llvm::json::Object &object) {
4343
// this as the breakpoint location since it will have a complete location
4444
// that is at least loaded in the current process.
4545
lldb::SBBreakpointLocation bp_loc;
46-
const auto num_locs = bp.GetNumLocations();
46+
const auto num_locs = m_bp.GetNumLocations();
4747
for (size_t i = 0; i < num_locs; ++i) {
48-
bp_loc = bp.GetLocationAtIndex(i);
48+
bp_loc = m_bp.GetLocationAtIndex(i);
4949
if (bp_loc.IsResolved())
5050
break;
5151
}
5252
// If not locations are resolved, use the first location.
5353
if (!bp_loc.IsResolved())
54-
bp_loc = bp.GetLocationAtIndex(0);
54+
bp_loc = m_bp.GetLocationAtIndex(0);
5555
auto bp_addr = bp_loc.GetAddress();
5656

5757
if (bp_addr.IsValid()) {
5858
std::string formatted_addr =
59-
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(bp.GetTarget()));
59+
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(m_bp.GetTarget()));
6060
object.try_emplace("instructionReference", formatted_addr);
6161
auto line_entry = bp_addr.GetLineEntry();
6262
const auto line = line_entry.GetLine();
@@ -69,12 +69,14 @@ void Breakpoint::CreateJsonObject(llvm::json::Object &object) {
6969
}
7070
}
7171

72-
bool Breakpoint::MatchesName(const char *name) { return bp.MatchesName(name); }
72+
bool Breakpoint::MatchesName(const char *name) {
73+
return m_bp.MatchesName(name);
74+
}
7375

7476
void Breakpoint::SetBreakpoint() {
75-
bp.AddName(kDAPBreakpointLabel);
76-
if (!condition.empty())
77+
m_bp.AddName(kDAPBreakpointLabel);
78+
if (!m_condition.empty())
7779
SetCondition();
78-
if (!hitCondition.empty())
80+
if (!m_hit_condition.empty())
7981
SetHitCondition();
8082
}

lldb/tools/lldb-dap/Breakpoint.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515

1616
namespace lldb_dap {
1717

18-
struct Breakpoint : public BreakpointBase {
19-
// The LLDB breakpoint associated wit this source breakpoint
20-
lldb::SBBreakpoint bp;
21-
18+
class Breakpoint : public BreakpointBase {
19+
public:
2220
Breakpoint(DAP &d, const llvm::json::Object &obj) : BreakpointBase(d, obj) {}
23-
Breakpoint(DAP &d, lldb::SBBreakpoint bp) : BreakpointBase(d), bp(bp) {}
21+
Breakpoint(DAP &d, lldb::SBBreakpoint bp) : BreakpointBase(d), m_bp(bp) {}
22+
23+
lldb::break_id_t GetID() const { return m_bp.GetID(); }
2424

2525
void SetCondition() override;
2626
void SetHitCondition() override;
2727
void CreateJsonObject(llvm::json::Object &object) override;
2828

2929
bool MatchesName(const char *name);
3030
void SetBreakpoint();
31+
32+
protected:
33+
/// The LLDB breakpoint associated wit this source breakpoint.
34+
lldb::SBBreakpoint m_bp;
3135
};
3236
} // namespace lldb_dap
3337

lldb/tools/lldb-dap/BreakpointBase.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
using namespace lldb_dap;
1414

1515
BreakpointBase::BreakpointBase(DAP &d, const llvm::json::Object &obj)
16-
: dap(d), condition(std::string(GetString(obj, "condition").value_or(""))),
17-
hitCondition(std::string(GetString(obj, "hitCondition").value_or(""))) {}
16+
: m_dap(d),
17+
m_condition(std::string(GetString(obj, "condition").value_or(""))),
18+
m_hit_condition(
19+
std::string(GetString(obj, "hitCondition").value_or(""))) {}
1820

1921
void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {
20-
if (condition != request_bp.condition) {
21-
condition = request_bp.condition;
22+
if (m_condition != request_bp.m_condition) {
23+
m_condition = request_bp.m_condition;
2224
SetCondition();
2325
}
24-
if (hitCondition != request_bp.hitCondition) {
25-
hitCondition = request_bp.hitCondition;
26+
if (m_hit_condition != request_bp.m_hit_condition) {
27+
m_hit_condition = request_bp.m_hit_condition;
2628
SetHitCondition();
2729
}
2830
}

lldb/tools/lldb-dap/BreakpointBase.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@
1515

1616
namespace lldb_dap {
1717

18-
struct BreakpointBase {
19-
// Associated DAP session.
20-
DAP &dap;
21-
22-
// An optional expression for conditional breakpoints.
23-
std::string condition;
24-
// An optional expression that controls how many hits of the breakpoint are
25-
// ignored. The backend is expected to interpret the expression as needed
26-
std::string hitCondition;
27-
28-
explicit BreakpointBase(DAP &d) : dap(d) {}
18+
class BreakpointBase {
19+
public:
20+
explicit BreakpointBase(DAP &d) : m_dap(d) {}
2921
BreakpointBase(DAP &d, const llvm::json::Object &obj);
3022
virtual ~BreakpointBase() = default;
3123

@@ -49,6 +41,17 @@ struct BreakpointBase {
4941
/// breakpoint in one of the DAP breakpoints that we should report changes
5042
/// for.
5143
static constexpr const char *kDAPBreakpointLabel = "dap";
44+
45+
protected:
46+
/// Associated DAP session.
47+
DAP &m_dap;
48+
49+
/// An optional expression for conditional breakpoints.
50+
std::string m_condition;
51+
52+
/// An optional expression that controls how many hits of the breakpoint are
53+
/// ignored. The backend is expected to interpret the expression as needed
54+
std::string m_hit_condition;
5255
};
5356

5457
} // namespace lldb_dap

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void DAP::PopulateExceptionBreakpoints() {
162162
});
163163
}
164164

165-
ExceptionBreakpoint *DAP::GetExceptionBreakpoint(const std::string &filter) {
165+
ExceptionBreakpoint *DAP::GetExceptionBreakpoint(llvm::StringRef filter) {
166166
// PopulateExceptionBreakpoints() is called after g_dap.debugger is created
167167
// in a request-initialize.
168168
//
@@ -181,7 +181,7 @@ ExceptionBreakpoint *DAP::GetExceptionBreakpoint(const std::string &filter) {
181181
PopulateExceptionBreakpoints();
182182

183183
for (auto &bp : *exception_breakpoints) {
184-
if (bp.filter == filter)
184+
if (bp.GetFilter() == filter)
185185
return &bp;
186186
}
187187
return nullptr;
@@ -192,7 +192,7 @@ ExceptionBreakpoint *DAP::GetExceptionBreakpoint(const lldb::break_id_t bp_id) {
192192
PopulateExceptionBreakpoints();
193193

194194
for (auto &bp : *exception_breakpoints) {
195-
if (bp.bp.GetID() == bp_id)
195+
if (bp.GetID() == bp_id)
196196
return &bp;
197197
}
198198
return nullptr;
@@ -1066,7 +1066,7 @@ void DAP::SetThreadFormat(llvm::StringRef format) {
10661066
InstructionBreakpoint *
10671067
DAP::GetInstructionBreakpoint(const lldb::break_id_t bp_id) {
10681068
for (auto &bp : instruction_breakpoints) {
1069-
if (bp.second.bp.GetID() == bp_id)
1069+
if (bp.second.GetID() == bp_id)
10701070
return &bp.second;
10711071
}
10721072
return nullptr;

lldb/tools/lldb-dap/DAP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct DAP {
236236
void operator=(const DAP &rhs) = delete;
237237
/// @}
238238

239-
ExceptionBreakpoint *GetExceptionBreakpoint(const std::string &filter);
239+
ExceptionBreakpoint *GetExceptionBreakpoint(llvm::StringRef filter);
240240
ExceptionBreakpoint *GetExceptionBreakpoint(const lldb::break_id_t bp_id);
241241

242242
/// Redirect stdout and stderr fo the IDE's console output.

lldb/tools/lldb-dap/DAPForward.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
// IWYU pragma: begin_exports
1313

1414
namespace lldb_dap {
15-
struct BreakpointBase;
16-
struct ExceptionBreakpoint;
17-
struct FunctionBreakpoint;
18-
struct SourceBreakpoint;
19-
struct Watchpoint;
20-
struct InstructionBreakpoint;
21-
struct DAP;
22-
class Log;
2315
class BaseRequestHandler;
16+
class BreakpointBase;
17+
class ExceptionBreakpoint;
18+
class FunctionBreakpoint;
19+
class InstructionBreakpoint;
20+
class Log;
2421
class ResponseHandler;
22+
class SourceBreakpoint;
23+
class Watchpoint;
24+
struct DAP;
2525
} // namespace lldb_dap
2626

2727
namespace lldb {

lldb/tools/lldb-dap/ExceptionBreakpoint.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
namespace lldb_dap {
1515

1616
void ExceptionBreakpoint::SetBreakpoint() {
17-
if (bp.IsValid())
17+
if (m_bp.IsValid())
1818
return;
19-
bool catch_value = filter.find("_catch") != std::string::npos;
20-
bool throw_value = filter.find("_throw") != std::string::npos;
21-
bp = dap.target.BreakpointCreateForException(language, catch_value,
22-
throw_value);
23-
bp.AddName(BreakpointBase::kDAPBreakpointLabel);
19+
bool catch_value = m_filter.find("_catch") != std::string::npos;
20+
bool throw_value = m_filter.find("_throw") != std::string::npos;
21+
m_bp = m_dap.target.BreakpointCreateForException(m_language, catch_value,
22+
throw_value);
23+
m_bp.AddName(BreakpointBase::kDAPBreakpointLabel);
2424
}
2525

2626
void ExceptionBreakpoint::ClearBreakpoint() {
27-
if (!bp.IsValid())
27+
if (!m_bp.IsValid())
2828
return;
29-
dap.target.BreakpointDelete(bp.GetID());
30-
bp = lldb::SBBreakpoint();
29+
m_dap.target.BreakpointDelete(m_bp.GetID());
30+
m_bp = lldb::SBBreakpoint();
3131
}
3232

3333
} // namespace lldb_dap

lldb/tools/lldb-dap/ExceptionBreakpoint.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,34 @@
1212
#include "DAPForward.h"
1313
#include "lldb/API/SBBreakpoint.h"
1414
#include "lldb/lldb-enumerations.h"
15+
#include "llvm/ADT/StringRef.h"
1516
#include <string>
1617
#include <utility>
1718

1819
namespace lldb_dap {
1920

20-
struct ExceptionBreakpoint {
21-
DAP &dap;
22-
std::string filter;
23-
std::string label;
24-
lldb::LanguageType language;
25-
bool default_value = false;
26-
lldb::SBBreakpoint bp;
21+
class ExceptionBreakpoint {
22+
public:
2723
ExceptionBreakpoint(DAP &d, std::string f, std::string l,
2824
lldb::LanguageType lang)
29-
: dap(d), filter(std::move(f)), label(std::move(l)), language(lang),
30-
bp() {}
25+
: m_dap(d), m_filter(std::move(f)), m_label(std::move(l)),
26+
m_language(lang), m_bp() {}
3127

3228
void SetBreakpoint();
3329
void ClearBreakpoint();
30+
31+
lldb::break_id_t GetID() const { return m_bp.GetID(); }
32+
llvm::StringRef GetFilter() const { return m_filter; }
33+
llvm::StringRef GetLabel() const { return m_label; }
34+
35+
static constexpr bool kDefaultValue = false;
36+
37+
protected:
38+
DAP &m_dap;
39+
std::string m_filter;
40+
std::string m_label;
41+
lldb::LanguageType m_language;
42+
lldb::SBBreakpoint m_bp;
3443
};
3544

3645
} // namespace lldb_dap

lldb/tools/lldb-dap/FunctionBreakpoint.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace lldb_dap {
1414

1515
FunctionBreakpoint::FunctionBreakpoint(DAP &d, const llvm::json::Object &obj)
1616
: Breakpoint(d, obj),
17-
functionName(std::string(GetString(obj, "name").value_or(""))) {}
17+
m_function_name(std::string(GetString(obj, "name").value_or(""))) {}
1818

1919
void FunctionBreakpoint::SetBreakpoint() {
20-
if (functionName.empty())
20+
if (m_function_name.empty())
2121
return;
22-
bp = dap.target.BreakpointCreateByName(functionName.c_str());
22+
m_bp = m_dap.target.BreakpointCreateByName(m_function_name.c_str());
2323
Breakpoint::SetBreakpoint();
2424
}
2525

lldb/tools/lldb-dap/FunctionBreakpoint.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
namespace lldb_dap {
1616

17-
struct FunctionBreakpoint : public Breakpoint {
18-
std::string functionName;
19-
17+
class FunctionBreakpoint : public Breakpoint {
18+
public:
2019
FunctionBreakpoint(DAP &dap, const llvm::json::Object &obj);
2120

22-
// Set this breakpoint in LLDB as a new breakpoint
21+
/// Set this breakpoint in LLDB as a new breakpoint.
2322
void SetBreakpoint();
23+
24+
llvm::StringRef GetFunctionName() const { return m_function_name; }
25+
26+
protected:
27+
std::string m_function_name;
2428
};
2529

2630
} // namespace lldb_dap

lldb/tools/lldb-dap/Handler/ExceptionInfoRequestHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ void ExceptionInfoRequestHandler::operator()(
125125
else if (stopReason == lldb::eStopReasonBreakpoint) {
126126
ExceptionBreakpoint *exc_bp = dap.GetExceptionBPFromStopReason(thread);
127127
if (exc_bp) {
128-
EmplaceSafeString(body, "exceptionId", exc_bp->filter);
129-
EmplaceSafeString(body, "description", exc_bp->label);
128+
EmplaceSafeString(body, "exceptionId", exc_bp->GetFilter());
129+
EmplaceSafeString(body, "description", exc_bp->GetLabel());
130130
} else {
131131
body.try_emplace("exceptionId", "exception");
132132
}

lldb/tools/lldb-dap/Handler/SetBreakpointsRequestHandler.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ void SetBreakpointsRequestHandler::operator()(
143143
const auto *bp_obj = bp.getAsObject();
144144
if (bp_obj) {
145145
SourceBreakpoint src_bp(dap, *bp_obj);
146-
std::pair<uint32_t, uint32_t> bp_pos(src_bp.line, src_bp.column);
146+
std::pair<uint32_t, uint32_t> bp_pos(src_bp.GetLine(),
147+
src_bp.GetColumn());
147148
request_bps.try_emplace(bp_pos, src_bp);
148149
const auto [iv, inserted] =
149150
dap.source_breakpoints[path].try_emplace(bp_pos, src_bp);
@@ -153,7 +154,7 @@ void SetBreakpointsRequestHandler::operator()(
153154
else
154155
iv->getSecond().UpdateBreakpoint(src_bp);
155156
AppendBreakpoint(&iv->getSecond(), response_breakpoints, path,
156-
src_bp.line);
157+
src_bp.GetLine());
157158
}
158159
}
159160
}
@@ -167,7 +168,7 @@ void SetBreakpointsRequestHandler::operator()(
167168
auto request_pos = request_bps.find(old_bp.first);
168169
if (request_pos == request_bps.end()) {
169170
// This breakpoint no longer exists in this source file, delete it
170-
dap.target.BreakpointDelete(old_bp.second.bp.GetID());
171+
dap.target.BreakpointDelete(old_bp.second.GetID());
171172
old_src_bp_pos->second.erase(old_bp.first);
172173
}
173174
}

lldb/tools/lldb-dap/Handler/SetDataBreakpointsRequestHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ void SetDataBreakpointsRequestHandler::operator()(
9797
// backward.
9898
std::set<lldb::addr_t> addresses;
9999
for (auto iter = watchpoints.rbegin(); iter != watchpoints.rend(); ++iter) {
100-
if (addresses.count(iter->addr) == 0) {
100+
if (addresses.count(iter->GetAddress()) == 0) {
101101
iter->SetWatchpoint();
102-
addresses.insert(iter->addr);
102+
addresses.insert(iter->GetAddress());
103103
}
104104
}
105105
for (auto wp : watchpoints)

0 commit comments

Comments
 (0)