Skip to content

Commit b99d411

Browse files
ashgtilabath
andauthored
[lldb-dap] Refactoring breakpoints to not use the g_dap reference. (#115208)
Refactoring breakpoints to not use the `g_dap` reference. Instead, when a breakpoint is constructed it will be passed a DAP reference that it should use for its lifetime. This is part of a larger refactor to remove the global `g_dap` variable to allow us to create multiple DAP instances. --------- Co-authored-by: Pavel Labath <[email protected]>
1 parent dbad941 commit b99d411

19 files changed

+420
-447
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Breakpoint.h"
10-
#include "DAP.h"
1110
#include "JSONUtils.h"
11+
#include "lldb/API/SBAddress.h"
1212
#include "lldb/API/SBBreakpointLocation.h"
13+
#include "lldb/API/SBLineEntry.h"
1314
#include "llvm/ADT/StringExtras.h"
15+
#include "llvm/Support/JSON.h"
16+
#include <cstddef>
17+
#include <cstdint>
18+
#include <string>
1419

1520
using namespace lldb_dap;
1621

@@ -51,7 +56,7 @@ void Breakpoint::CreateJsonObject(llvm::json::Object &object) {
5156

5257
if (bp_addr.IsValid()) {
5358
std::string formatted_addr =
54-
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(g_dap.target));
59+
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(bp.GetTarget()));
5560
object.try_emplace("instructionReference", formatted_addr);
5661
auto line_entry = bp_addr.GetLineEntry();
5762
const auto line = line_entry.GetLine();

lldb/tools/lldb-dap/Breakpoint.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_TOOLS_LLDB_DAP_BREAKPOINT_H
1111

1212
#include "BreakpointBase.h"
13+
#include "DAPForward.h"
1314
#include "lldb/API/SBBreakpoint.h"
1415

1516
namespace lldb_dap {
@@ -18,9 +19,8 @@ struct Breakpoint : public BreakpointBase {
1819
// The LLDB breakpoint associated wit this source breakpoint
1920
lldb::SBBreakpoint bp;
2021

21-
Breakpoint() = default;
22-
Breakpoint(const llvm::json::Object &obj) : BreakpointBase(obj){};
23-
Breakpoint(lldb::SBBreakpoint bp) : bp(bp) {}
22+
Breakpoint(DAP &d, const llvm::json::Object &obj) : BreakpointBase(d, obj) {}
23+
Breakpoint(DAP &d, lldb::SBBreakpoint bp) : BreakpointBase(d), bp(bp) {}
2424

2525
void SetCondition() override;
2626
void SetHitCondition() override;

lldb/tools/lldb-dap/BreakpointBase.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
#include "BreakpointBase.h"
1010
#include "JSONUtils.h"
11+
#include "llvm/ADT/StringRef.h"
1112

1213
using namespace lldb_dap;
1314

14-
BreakpointBase::BreakpointBase(const llvm::json::Object &obj)
15-
: condition(std::string(GetString(obj, "condition"))),
15+
BreakpointBase::BreakpointBase(DAP &d, const llvm::json::Object &obj)
16+
: dap(d), condition(std::string(GetString(obj, "condition"))),
1617
hitCondition(std::string(GetString(obj, "hitCondition"))) {}
1718

1819
void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {

lldb/tools/lldb-dap/BreakpointBase.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@
99
#ifndef LLDB_TOOLS_LLDB_DAP_BREAKPOINTBASE_H
1010
#define LLDB_TOOLS_LLDB_DAP_BREAKPOINTBASE_H
1111

12-
#include "llvm/Support/JSON.h"
12+
#include "DAPForward.h"
1313
#include <string>
1414

1515
namespace lldb_dap {
1616

1717
struct BreakpointBase {
18+
// Associated DAP session.
19+
DAP &dap;
1820

1921
// An optional expression for conditional breakpoints.
2022
std::string condition;
2123
// An optional expression that controls how many hits of the breakpoint are
2224
// ignored. The backend is expected to interpret the expression as needed
2325
std::string hitCondition;
2426

25-
BreakpointBase() = default;
26-
BreakpointBase(const llvm::json::Object &obj);
27+
explicit BreakpointBase(DAP &d) : dap(d) {}
28+
BreakpointBase(DAP &d, const llvm::json::Object &obj);
2729
virtual ~BreakpointBase() = default;
2830

2931
virtual void SetCondition() = 0;

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ void DAP::PopulateExceptionBreakpoints() {
7474
exception_breakpoints = std::vector<ExceptionBreakpoint>{};
7575

7676
if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeC_plus_plus)) {
77-
exception_breakpoints->emplace_back("cpp_catch", "C++ Catch",
77+
exception_breakpoints->emplace_back(*this, "cpp_catch", "C++ Catch",
7878
lldb::eLanguageTypeC_plus_plus);
79-
exception_breakpoints->emplace_back("cpp_throw", "C++ Throw",
79+
exception_breakpoints->emplace_back(*this, "cpp_throw", "C++ Throw",
8080
lldb::eLanguageTypeC_plus_plus);
8181
}
8282
if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeObjC)) {
83-
exception_breakpoints->emplace_back("objc_catch", "Objective-C Catch",
84-
lldb::eLanguageTypeObjC);
85-
exception_breakpoints->emplace_back("objc_throw", "Objective-C Throw",
86-
lldb::eLanguageTypeObjC);
83+
exception_breakpoints->emplace_back(
84+
*this, "objc_catch", "Objective-C Catch", lldb::eLanguageTypeObjC);
85+
exception_breakpoints->emplace_back(
86+
*this, "objc_throw", "Objective-C Throw", lldb::eLanguageTypeObjC);
8787
}
8888
if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeSwift)) {
89-
exception_breakpoints->emplace_back("swift_catch", "Swift Catch",
89+
exception_breakpoints->emplace_back(*this, "swift_catch", "Swift Catch",
9090
lldb::eLanguageTypeSwift);
91-
exception_breakpoints->emplace_back("swift_throw", "Swift Throw",
91+
exception_breakpoints->emplace_back(*this, "swift_throw", "Swift Throw",
9292
lldb::eLanguageTypeSwift);
9393
}
9494
// Besides handling the hardcoded list of languages from above, we try to
@@ -119,7 +119,7 @@ void DAP::PopulateExceptionBreakpoints() {
119119
raw_throw_keyword ? raw_throw_keyword : "throw";
120120

121121
exception_breakpoints->emplace_back(
122-
raw_lang_name + "_" + throw_keyword,
122+
*this, raw_lang_name + "_" + throw_keyword,
123123
capitalized_lang_name + " " + capitalize(throw_keyword), lang);
124124
}
125125

@@ -130,7 +130,7 @@ void DAP::PopulateExceptionBreakpoints() {
130130
raw_catch_keyword ? raw_catch_keyword : "catch";
131131

132132
exception_breakpoints->emplace_back(
133-
raw_lang_name + "_" + catch_keyword,
133+
*this, raw_lang_name + "_" + catch_keyword,
134134
capitalized_lang_name + " " + capitalize(catch_keyword), lang);
135135
}
136136
}
@@ -1060,7 +1060,7 @@ void DAP::SetThreadFormat(llvm::StringRef format) {
10601060
InstructionBreakpoint *
10611061
DAP::GetInstructionBreakpoint(const lldb::break_id_t bp_id) {
10621062
for (auto &bp : instruction_breakpoints) {
1063-
if (bp.second.id == bp_id)
1063+
if (bp.second.bp.GetID() == bp_id)
10641064
return &bp.second;
10651065
}
10661066
return nullptr;

lldb/tools/lldb-dap/DAPForward.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
#ifndef LLDB_TOOLS_LLDB_DAP_DAPFORWARD_H
1010
#define LLDB_TOOLS_LLDB_DAP_DAPFORWARD_H
1111

12+
// IWYU pragma: begin_exports
13+
1214
namespace lldb_dap {
1315
struct BreakpointBase;
1416
struct ExceptionBreakpoint;
1517
struct FunctionBreakpoint;
1618
struct SourceBreakpoint;
1719
struct Watchpoint;
1820
struct InstructionBreakpoint;
21+
struct DAP;
1922
} // namespace lldb_dap
2023

2124
namespace lldb {
@@ -35,6 +38,7 @@ class SBLanguageRuntime;
3538
class SBLaunchInfo;
3639
class SBLineEntry;
3740
class SBListener;
41+
class SBModule;
3842
class SBProcess;
3943
class SBStream;
4044
class SBStringList;
@@ -44,4 +48,12 @@ class SBValue;
4448
class SBWatchpoint;
4549
} // namespace lldb
4650

51+
namespace llvm {
52+
namespace json {
53+
class Object;
54+
} // namespace json
55+
} // namespace llvm
56+
57+
// IWYU pragma: end_exports
58+
4759
#endif

lldb/tools/lldb-dap/ExceptionBreakpoint.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ExceptionBreakpoint.h"
1010
#include "BreakpointBase.h"
1111
#include "DAP.h"
12+
#include "lldb/API/SBTarget.h"
1213

1314
namespace lldb_dap {
1415

@@ -17,8 +18,8 @@ void ExceptionBreakpoint::SetBreakpoint() {
1718
return;
1819
bool catch_value = filter.find("_catch") != std::string::npos;
1920
bool throw_value = filter.find("_throw") != std::string::npos;
20-
bp = g_dap.target.BreakpointCreateForException(language, catch_value,
21-
throw_value);
21+
bp = dap.target.BreakpointCreateForException(language, catch_value,
22+
throw_value);
2223
// See comments in BreakpointBase::GetBreakpointLabel() for details of why
2324
// we add a label to our breakpoints.
2425
bp.AddName(BreakpointBase::GetBreakpointLabel());
@@ -27,7 +28,7 @@ void ExceptionBreakpoint::SetBreakpoint() {
2728
void ExceptionBreakpoint::ClearBreakpoint() {
2829
if (!bp.IsValid())
2930
return;
30-
g_dap.target.BreakpointDelete(bp.GetID());
31+
dap.target.BreakpointDelete(bp.GetID());
3132
bp = lldb::SBBreakpoint();
3233
}
3334

lldb/tools/lldb-dap/ExceptionBreakpoint.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@
99
#ifndef LLDB_TOOLS_LLDB_DAP_EXCEPTIONBREAKPOINT_H
1010
#define LLDB_TOOLS_LLDB_DAP_EXCEPTIONBREAKPOINT_H
1111

12-
#include <string>
13-
12+
#include "DAPForward.h"
1413
#include "lldb/API/SBBreakpoint.h"
14+
#include "lldb/lldb-enumerations.h"
15+
#include <string>
16+
#include <utility>
1517

1618
namespace lldb_dap {
1719

1820
struct ExceptionBreakpoint {
21+
DAP &dap;
1922
std::string filter;
2023
std::string label;
2124
lldb::LanguageType language;
22-
bool default_value;
25+
bool default_value = false;
2326
lldb::SBBreakpoint bp;
24-
ExceptionBreakpoint(std::string f, std::string l, lldb::LanguageType lang)
25-
: filter(std::move(f)), label(std::move(l)), language(lang),
26-
default_value(false), bp() {}
27+
ExceptionBreakpoint(DAP &d, std::string f, std::string l,
28+
lldb::LanguageType lang)
29+
: dap(d), filter(std::move(f)), label(std::move(l)), language(lang),
30+
bp() {}
2731

2832
void SetBreakpoint();
2933
void ClearBreakpoint();

lldb/tools/lldb-dap/FunctionBreakpoint.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
namespace lldb_dap {
1414

15-
FunctionBreakpoint::FunctionBreakpoint(const llvm::json::Object &obj)
16-
: Breakpoint(obj), functionName(std::string(GetString(obj, "name"))) {}
15+
FunctionBreakpoint::FunctionBreakpoint(DAP &d, const llvm::json::Object &obj)
16+
: Breakpoint(d, obj), functionName(std::string(GetString(obj, "name"))) {}
1717

1818
void FunctionBreakpoint::SetBreakpoint() {
1919
if (functionName.empty())
2020
return;
21-
bp = g_dap.target.BreakpointCreateByName(functionName.c_str());
21+
bp = dap.target.BreakpointCreateByName(functionName.c_str());
2222
Breakpoint::SetBreakpoint();
2323
}
2424

lldb/tools/lldb-dap/FunctionBreakpoint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#define LLDB_TOOLS_LLDB_DAP_FUNCTIONBREAKPOINT_H
1111

1212
#include "Breakpoint.h"
13+
#include "DAPForward.h"
1314

1415
namespace lldb_dap {
1516

1617
struct FunctionBreakpoint : public Breakpoint {
1718
std::string functionName;
1819

19-
FunctionBreakpoint() = default;
20-
FunctionBreakpoint(const llvm::json::Object &obj);
20+
FunctionBreakpoint(DAP &dap, const llvm::json::Object &obj);
2121

2222
// Set this breakpoint in LLDB as a new breakpoint
2323
void SetBreakpoint();

lldb/tools/lldb-dap/InstructionBreakpoint.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@
1010
#include "InstructionBreakpoint.h"
1111
#include "DAP.h"
1212
#include "JSONUtils.h"
13+
#include "lldb/API/SBBreakpoint.h"
14+
#include "lldb/API/SBTarget.h"
15+
#include "llvm/ADT/StringRef.h"
1316

1417
namespace lldb_dap {
1518

1619
// Instruction Breakpoint
17-
InstructionBreakpoint::InstructionBreakpoint(const llvm::json::Object &obj)
18-
: Breakpoint(obj), instructionAddressReference(LLDB_INVALID_ADDRESS), id(0),
20+
InstructionBreakpoint::InstructionBreakpoint(DAP &d,
21+
const llvm::json::Object &obj)
22+
: Breakpoint(d, obj), instructionAddressReference(LLDB_INVALID_ADDRESS),
1923
offset(GetSigned(obj, "offset", 0)) {
2024
GetString(obj, "instructionReference")
2125
.getAsInteger(0, instructionAddressReference);
2226
instructionAddressReference += offset;
2327
}
2428

25-
void InstructionBreakpoint::SetInstructionBreakpoint() {
26-
bp = g_dap.target.BreakpointCreateByAddress(instructionAddressReference);
27-
id = bp.GetID();
29+
void InstructionBreakpoint::SetBreakpoint() {
30+
bp = dap.target.BreakpointCreateByAddress(instructionAddressReference);
31+
Breakpoint::SetBreakpoint();
2832
}
33+
2934
} // namespace lldb_dap

lldb/tools/lldb-dap/InstructionBreakpoint.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@
1111
#define LLDB_TOOLS_LLDB_DAP_INSTRUCTIONBREAKPOINT_H
1212

1313
#include "Breakpoint.h"
14+
#include "DAPForward.h"
15+
#include "lldb/lldb-types.h"
16+
#include <cstdint>
1417

1518
namespace lldb_dap {
1619

1720
// Instruction Breakpoint
1821
struct InstructionBreakpoint : public Breakpoint {
1922

2023
lldb::addr_t instructionAddressReference;
21-
int32_t id;
2224
int32_t offset;
2325

24-
InstructionBreakpoint()
25-
: Breakpoint(), instructionAddressReference(LLDB_INVALID_ADDRESS), id(0),
26-
offset(0) {}
27-
InstructionBreakpoint(const llvm::json::Object &obj);
26+
InstructionBreakpoint(DAP &d, const llvm::json::Object &obj);
2827

2928
// Set instruction breakpoint in LLDB as a new breakpoint
30-
void SetInstructionBreakpoint();
29+
void SetBreakpoint();
3130
};
3231

3332
} // namespace lldb_dap

0 commit comments

Comments
 (0)