Skip to content

Commit 125c897

Browse files
committed
[lldb-dap] 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 in favor of managing instances.
1 parent bcb64e1 commit 125c897

19 files changed

+318
-401
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Breakpoint.h"
10-
#include "DAP.h"
11-
#include "JSONUtils.h"
10+
1211
#include "lldb/API/SBBreakpointLocation.h"
1312
#include "llvm/ADT/StringExtras.h"
1413

14+
#include "DAP.h"
15+
#include "JSONUtils.h"
16+
1517
using namespace lldb_dap;
1618

1719
void Breakpoint::SetCondition() { bp.SetCondition(condition.c_str()); }
@@ -51,7 +53,7 @@ void Breakpoint::CreateJsonObject(llvm::json::Object &object) {
5153

5254
if (bp_addr.IsValid()) {
5355
std::string formatted_addr =
54-
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(g_dap.target));
56+
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(bp.GetTarget()));
5557
object.try_emplace("instructionReference", formatted_addr);
5658
auto line_entry = bp_addr.GetLineEntry();
5759
const auto line = line_entry.GetLine();

lldb/tools/lldb-dap/Breakpoint.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
#ifndef LLDB_TOOLS_LLDB_DAP_BREAKPOINT_H
1010
#define LLDB_TOOLS_LLDB_DAP_BREAKPOINT_H
1111

12-
#include "BreakpointBase.h"
1312
#include "lldb/API/SBBreakpoint.h"
1413

14+
#include "BreakpointBase.h"
15+
1516
namespace lldb_dap {
1617

1718
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) : BreakpointBase(d) {}
23+
Breakpoint(DAP &d, const llvm::json::Object &obj) : BreakpointBase(d, obj) {}
24+
Breakpoint(DAP &d, lldb::SBBreakpoint bp) : BreakpointBase(d), bp(bp) {}
2425

2526
void SetCondition() override;
2627
void SetHitCondition() override;

lldb/tools/lldb-dap/BreakpointBase.cpp

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

1212
using namespace lldb_dap;
1313

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

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

lldb/tools/lldb-dap/BreakpointBase.h

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

12-
#include "llvm/Support/JSON.h"
1312
#include <string>
1413

14+
#include "llvm/Support/JSON.h"
15+
16+
#include "DAPForward.h"
17+
1518
namespace lldb_dap {
1619

1720
struct BreakpointBase {
21+
// Associated DAP session.
22+
DAP &dap;
1823

1924
// An optional expression for conditional breakpoints.
2025
std::string condition;
2126
// An optional expression that controls how many hits of the breakpoint are
2227
// ignored. The backend is expected to interpret the expression as needed
2328
std::string hitCondition;
2429

25-
BreakpointBase() = default;
26-
BreakpointBase(const llvm::json::Object &obj);
30+
BreakpointBase(DAP &d) : dap(d) {}
31+
BreakpointBase(DAP &d, const llvm::json::Object &obj);
32+
BreakpointBase(const BreakpointBase &) = default;
2733
virtual ~BreakpointBase() = default;
2834

2935
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct FunctionBreakpoint;
1616
struct SourceBreakpoint;
1717
struct Watchpoint;
1818
struct InstructionBreakpoint;
19+
struct DAP;
1920
} // namespace lldb_dap
2021

2122
namespace lldb {

lldb/tools/lldb-dap/ExceptionBreakpoint.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ void ExceptionBreakpoint::SetBreakpoint() {
1717
return;
1818
bool catch_value = filter.find("_catch") != std::string::npos;
1919
bool throw_value = filter.find("_throw") != std::string::npos;
20-
bp = g_dap.target.BreakpointCreateForException(language, catch_value,
21-
throw_value);
20+
bp = dap.target.BreakpointCreateForException(language, catch_value,
21+
throw_value);
2222
// See comments in BreakpointBase::GetBreakpointLabel() for details of why
2323
// we add a label to our breakpoints.
2424
bp.AddName(BreakpointBase::GetBreakpointLabel());
@@ -27,7 +27,7 @@ void ExceptionBreakpoint::SetBreakpoint() {
2727
void ExceptionBreakpoint::ClearBreakpoint() {
2828
if (!bp.IsValid())
2929
return;
30-
g_dap.target.BreakpointDelete(bp.GetID());
30+
dap.target.BreakpointDelete(bp.GetID());
3131
bp = lldb::SBBreakpoint();
3232
}
3333

lldb/tools/lldb-dap/ExceptionBreakpoint.h

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

1414
#include "lldb/API/SBBreakpoint.h"
1515

16+
#include "DAPForward.h"
17+
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "FunctionBreakpoint.h"
10+
1011
#include "DAP.h"
1112
#include "JSONUtils.h"
1213

1314
namespace lldb_dap {
1415

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

1819
void FunctionBreakpoint::SetBreakpoint() {
1920
if (functionName.empty())
2021
return;
21-
bp = g_dap.target.BreakpointCreateByName(functionName.c_str());
22+
bp = dap.target.BreakpointCreateByName(functionName.c_str());
2223
Breakpoint::SetBreakpoint();
2324
}
2425

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();

0 commit comments

Comments
 (0)