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

lldb/tools/lldb-dap/InstructionBreakpoint.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "InstructionBreakpoint.h"
11+
1112
#include "DAP.h"
1213
#include "JSONUtils.h"
1314

1415
namespace lldb_dap {
1516

1617
// Instruction Breakpoint
17-
InstructionBreakpoint::InstructionBreakpoint(const llvm::json::Object &obj)
18-
: Breakpoint(obj), instructionAddressReference(LLDB_INVALID_ADDRESS), id(0),
18+
InstructionBreakpoint::InstructionBreakpoint(DAP &d,
19+
const llvm::json::Object &obj)
20+
: Breakpoint(d, obj), instructionAddressReference(LLDB_INVALID_ADDRESS),
1921
offset(GetSigned(obj, "offset", 0)) {
2022
GetString(obj, "instructionReference")
2123
.getAsInteger(0, instructionAddressReference);
2224
instructionAddressReference += offset;
2325
}
2426

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

lldb/tools/lldb-dap/InstructionBreakpoint.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@
1010
#ifndef LLDB_TOOLS_LLDB_DAP_INSTRUCTIONBREAKPOINT_H
1111
#define LLDB_TOOLS_LLDB_DAP_INSTRUCTIONBREAKPOINT_H
1212

13+
#include <cstdint>
14+
15+
#include "lldb/lldb-types.h"
16+
1317
#include "Breakpoint.h"
18+
#include "DAPForward.h"
1419

1520
namespace lldb_dap {
1621

1722
// Instruction Breakpoint
1823
struct InstructionBreakpoint : public Breakpoint {
1924

2025
lldb::addr_t instructionAddressReference;
21-
int32_t id;
2226
int32_t offset;
2327

24-
InstructionBreakpoint()
25-
: Breakpoint(), instructionAddressReference(LLDB_INVALID_ADDRESS), id(0),
28+
InstructionBreakpoint(DAP &d)
29+
: Breakpoint(d), instructionAddressReference(LLDB_INVALID_ADDRESS),
2630
offset(0) {}
27-
InstructionBreakpoint(const llvm::json::Object &obj);
31+
InstructionBreakpoint(DAP &d, const llvm::json::Object &obj);
2832

2933
// Set instruction breakpoint in LLDB as a new breakpoint
30-
void SetInstructionBreakpoint();
34+
void SetBreakpoint();
3135
};
3236

3337
} // namespace lldb_dap

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -831,70 +831,6 @@ llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread) {
831831
{"presentationHint", "label"}});
832832
}
833833

834-
// Response to `setInstructionBreakpoints` request.
835-
// "Breakpoint": {
836-
// "type": "object",
837-
// "description": "Response to `setInstructionBreakpoints` request.",
838-
// "properties": {
839-
// "id": {
840-
// "type": "number",
841-
// "description": "The identifier for the breakpoint. It is needed if
842-
// breakpoint events are used to update or remove breakpoints."
843-
// },
844-
// "verified": {
845-
// "type": "boolean",
846-
// "description": "If true, the breakpoint could be set (but not
847-
// necessarily at the desired location."
848-
// },
849-
// "message": {
850-
// "type": "string",
851-
// "description": "A message about the state of the breakpoint.
852-
// This is shown to the user and can be used to explain why a breakpoint
853-
// could not be verified."
854-
// },
855-
// "source": {
856-
// "type": "Source",
857-
// "description": "The source where the breakpoint is located."
858-
// },
859-
// "line": {
860-
// "type": "number",
861-
// "description": "The start line of the actual range covered by the
862-
// breakpoint."
863-
// },
864-
// "column": {
865-
// "type": "number",
866-
// "description": "The start column of the actual range covered by the
867-
// breakpoint."
868-
// },
869-
// "endLine": {
870-
// "type": "number",
871-
// "description": "The end line of the actual range covered by the
872-
// breakpoint."
873-
// },
874-
// "endColumn": {
875-
// "type": "number",
876-
// "description": "The end column of the actual range covered by the
877-
// breakpoint. If no end line is given, then the end column is assumed to
878-
// be in the start line."
879-
// },
880-
// "instructionReference": {
881-
// "type": "string",
882-
// "description": "A memory reference to where the breakpoint is set."
883-
// },
884-
// "offset": {
885-
// "type": "number",
886-
// "description": "The offset from the instruction reference.
887-
// This can be negative."
888-
// },
889-
// },
890-
// "required": [ "id", "verified", "line"]
891-
// }
892-
llvm::json::Value CreateInstructionBreakpoint(BreakpointBase *ibp) {
893-
llvm::json::Object object;
894-
ibp->CreateJsonObject(object);
895-
return llvm::json::Value(std::move(object));
896-
}
897-
898834
// "Thread": {
899835
// "type": "object",
900836
// "description": "A Thread",

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,6 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame);
380380
/// definition outlined by Microsoft.
381381
llvm::json::Value CreateExtendedStackFrameLabel(lldb::SBThread &thread);
382382

383-
/// Create a "instruction" object for a LLDB disassemble object as described in
384-
/// the Visual Studio Code debug adaptor definition.
385-
///
386-
/// \param[in] bp
387-
/// The LLDB instruction object used to populate the disassembly
388-
/// instruction.
389-
/// \return
390-
/// A "Scope" JSON object with that follows the formal JSON
391-
/// definition outlined by Microsoft.
392-
llvm::json::Value CreateInstructionBreakpoint(BreakpointBase *ibp);
393-
394383
/// Create a "Thread" object for a LLDB thread object.
395384
///
396385
/// This function will fill in the following keys in the returned

0 commit comments

Comments
 (0)