Skip to content

[lldb-dap] Refactoring breakpoints to not use the g_dap reference. #115208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 8, 2024
Merged
9 changes: 7 additions & 2 deletions lldb/tools/lldb-dap/Breakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
//===----------------------------------------------------------------------===//

#include "Breakpoint.h"
#include "DAP.h"
#include "JSONUtils.h"
#include "lldb/API/SBAddress.h"
#include "lldb/API/SBBreakpointLocation.h"
#include "lldb/API/SBLineEntry.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/JSON.h"
#include <cstddef>
#include <cstdint>
#include <string>

using namespace lldb_dap;

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

if (bp_addr.IsValid()) {
std::string formatted_addr =
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(g_dap.target));
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(bp.GetTarget()));
object.try_emplace("instructionReference", formatted_addr);
auto line_entry = bp_addr.GetLineEntry();
const auto line = line_entry.GetLine();
Expand Down
6 changes: 3 additions & 3 deletions lldb/tools/lldb-dap/Breakpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLDB_TOOLS_LLDB_DAP_BREAKPOINT_H

#include "BreakpointBase.h"
#include "DAPForward.h"
#include "lldb/API/SBBreakpoint.h"

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

Breakpoint() = default;
Breakpoint(const llvm::json::Object &obj) : BreakpointBase(obj){};
Breakpoint(lldb::SBBreakpoint bp) : bp(bp) {}
Breakpoint(DAP &d, const llvm::json::Object &obj) : BreakpointBase(d, obj) {}
Breakpoint(DAP &d, lldb::SBBreakpoint bp) : BreakpointBase(d), bp(bp) {}

void SetCondition() override;
void SetHitCondition() override;
Expand Down
5 changes: 3 additions & 2 deletions lldb/tools/lldb-dap/BreakpointBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

#include "BreakpointBase.h"
#include "JSONUtils.h"
#include "llvm/ADT/StringRef.h"

using namespace lldb_dap;

BreakpointBase::BreakpointBase(const llvm::json::Object &obj)
: condition(std::string(GetString(obj, "condition"))),
BreakpointBase::BreakpointBase(DAP &d, const llvm::json::Object &obj)
: dap(d), condition(std::string(GetString(obj, "condition"))),
hitCondition(std::string(GetString(obj, "hitCondition"))) {}

void BreakpointBase::UpdateBreakpoint(const BreakpointBase &request_bp) {
Expand Down
8 changes: 5 additions & 3 deletions lldb/tools/lldb-dap/BreakpointBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
#ifndef LLDB_TOOLS_LLDB_DAP_BREAKPOINTBASE_H
#define LLDB_TOOLS_LLDB_DAP_BREAKPOINTBASE_H

#include "llvm/Support/JSON.h"
#include "DAPForward.h"
#include <string>

namespace lldb_dap {

struct BreakpointBase {
// Associated DAP session.
DAP &dap;

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

BreakpointBase() = default;
BreakpointBase(const llvm::json::Object &obj);
explicit BreakpointBase(DAP &d) : dap(d) {}
BreakpointBase(DAP &d, const llvm::json::Object &obj);
virtual ~BreakpointBase() = default;

virtual void SetCondition() = 0;
Expand Down
22 changes: 11 additions & 11 deletions lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ void DAP::PopulateExceptionBreakpoints() {
exception_breakpoints = std::vector<ExceptionBreakpoint>{};

if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeC_plus_plus)) {
exception_breakpoints->emplace_back("cpp_catch", "C++ Catch",
exception_breakpoints->emplace_back(*this, "cpp_catch", "C++ Catch",
lldb::eLanguageTypeC_plus_plus);
exception_breakpoints->emplace_back("cpp_throw", "C++ Throw",
exception_breakpoints->emplace_back(*this, "cpp_throw", "C++ Throw",
lldb::eLanguageTypeC_plus_plus);
}
if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeObjC)) {
exception_breakpoints->emplace_back("objc_catch", "Objective-C Catch",
lldb::eLanguageTypeObjC);
exception_breakpoints->emplace_back("objc_throw", "Objective-C Throw",
lldb::eLanguageTypeObjC);
exception_breakpoints->emplace_back(
*this, "objc_catch", "Objective-C Catch", lldb::eLanguageTypeObjC);
exception_breakpoints->emplace_back(
*this, "objc_throw", "Objective-C Throw", lldb::eLanguageTypeObjC);
}
if (lldb::SBDebugger::SupportsLanguage(lldb::eLanguageTypeSwift)) {
exception_breakpoints->emplace_back("swift_catch", "Swift Catch",
exception_breakpoints->emplace_back(*this, "swift_catch", "Swift Catch",
lldb::eLanguageTypeSwift);
exception_breakpoints->emplace_back("swift_throw", "Swift Throw",
exception_breakpoints->emplace_back(*this, "swift_throw", "Swift Throw",
lldb::eLanguageTypeSwift);
}
// Besides handling the hardcoded list of languages from above, we try to
Expand Down Expand Up @@ -119,7 +119,7 @@ void DAP::PopulateExceptionBreakpoints() {
raw_throw_keyword ? raw_throw_keyword : "throw";

exception_breakpoints->emplace_back(
raw_lang_name + "_" + throw_keyword,
*this, raw_lang_name + "_" + throw_keyword,
capitalized_lang_name + " " + capitalize(throw_keyword), lang);
}

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

exception_breakpoints->emplace_back(
raw_lang_name + "_" + catch_keyword,
*this, raw_lang_name + "_" + catch_keyword,
capitalized_lang_name + " " + capitalize(catch_keyword), lang);
}
}
Expand Down Expand Up @@ -1060,7 +1060,7 @@ void DAP::SetThreadFormat(llvm::StringRef format) {
InstructionBreakpoint *
DAP::GetInstructionBreakpoint(const lldb::break_id_t bp_id) {
for (auto &bp : instruction_breakpoints) {
if (bp.second.id == bp_id)
if (bp.second.bp.GetID() == bp_id)
return &bp.second;
}
return nullptr;
Expand Down
12 changes: 12 additions & 0 deletions lldb/tools/lldb-dap/DAPForward.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#ifndef LLDB_TOOLS_LLDB_DAP_DAPFORWARD_H
#define LLDB_TOOLS_LLDB_DAP_DAPFORWARD_H

// IWYU pragma: begin_exports

namespace lldb_dap {
struct BreakpointBase;
struct ExceptionBreakpoint;
struct FunctionBreakpoint;
struct SourceBreakpoint;
struct Watchpoint;
struct InstructionBreakpoint;
struct DAP;
} // namespace lldb_dap

namespace lldb {
Expand All @@ -35,6 +38,7 @@ class SBLanguageRuntime;
class SBLaunchInfo;
class SBLineEntry;
class SBListener;
class SBModule;
class SBProcess;
class SBStream;
class SBStringList;
Expand All @@ -44,4 +48,12 @@ class SBValue;
class SBWatchpoint;
} // namespace lldb

namespace llvm {
namespace json {
class Object;
} // namespace json
} // namespace llvm

// IWYU pragma: end_exports

#endif
7 changes: 4 additions & 3 deletions lldb/tools/lldb-dap/ExceptionBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ExceptionBreakpoint.h"
#include "BreakpointBase.h"
#include "DAP.h"
#include "lldb/API/SBTarget.h"

namespace lldb_dap {

Expand All @@ -17,8 +18,8 @@ void ExceptionBreakpoint::SetBreakpoint() {
return;
bool catch_value = filter.find("_catch") != std::string::npos;
bool throw_value = filter.find("_throw") != std::string::npos;
bp = g_dap.target.BreakpointCreateForException(language, catch_value,
throw_value);
bp = dap.target.BreakpointCreateForException(language, catch_value,
throw_value);
// See comments in BreakpointBase::GetBreakpointLabel() for details of why
// we add a label to our breakpoints.
bp.AddName(BreakpointBase::GetBreakpointLabel());
Expand All @@ -27,7 +28,7 @@ void ExceptionBreakpoint::SetBreakpoint() {
void ExceptionBreakpoint::ClearBreakpoint() {
if (!bp.IsValid())
return;
g_dap.target.BreakpointDelete(bp.GetID());
dap.target.BreakpointDelete(bp.GetID());
bp = lldb::SBBreakpoint();
}

Expand Down
16 changes: 10 additions & 6 deletions lldb/tools/lldb-dap/ExceptionBreakpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
#ifndef LLDB_TOOLS_LLDB_DAP_EXCEPTIONBREAKPOINT_H
#define LLDB_TOOLS_LLDB_DAP_EXCEPTIONBREAKPOINT_H

#include <string>

#include "DAPForward.h"
#include "lldb/API/SBBreakpoint.h"
#include "lldb/lldb-enumerations.h"
#include <string>
#include <utility>

namespace lldb_dap {

struct ExceptionBreakpoint {
DAP &dap;
std::string filter;
std::string label;
lldb::LanguageType language;
bool default_value;
bool default_value = false;
lldb::SBBreakpoint bp;
ExceptionBreakpoint(std::string f, std::string l, lldb::LanguageType lang)
: filter(std::move(f)), label(std::move(l)), language(lang),
default_value(false), bp() {}
ExceptionBreakpoint(DAP &d, std::string f, std::string l,
lldb::LanguageType lang)
: dap(d), filter(std::move(f)), label(std::move(l)), language(lang),
bp() {}

void SetBreakpoint();
void ClearBreakpoint();
Expand Down
6 changes: 3 additions & 3 deletions lldb/tools/lldb-dap/FunctionBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

namespace lldb_dap {

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

void FunctionBreakpoint::SetBreakpoint() {
if (functionName.empty())
return;
bp = g_dap.target.BreakpointCreateByName(functionName.c_str());
bp = dap.target.BreakpointCreateByName(functionName.c_str());
Breakpoint::SetBreakpoint();
}

Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-dap/FunctionBreakpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#define LLDB_TOOLS_LLDB_DAP_FUNCTIONBREAKPOINT_H

#include "Breakpoint.h"
#include "DAPForward.h"

namespace lldb_dap {

struct FunctionBreakpoint : public Breakpoint {
std::string functionName;

FunctionBreakpoint() = default;
FunctionBreakpoint(const llvm::json::Object &obj);
FunctionBreakpoint(DAP &dap, const llvm::json::Object &obj);

// Set this breakpoint in LLDB as a new breakpoint
void SetBreakpoint();
Expand Down
15 changes: 10 additions & 5 deletions lldb/tools/lldb-dap/InstructionBreakpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
#include "InstructionBreakpoint.h"
#include "DAP.h"
#include "JSONUtils.h"
#include "lldb/API/SBBreakpoint.h"
#include "lldb/API/SBTarget.h"
#include "llvm/ADT/StringRef.h"

namespace lldb_dap {

// Instruction Breakpoint
InstructionBreakpoint::InstructionBreakpoint(const llvm::json::Object &obj)
: Breakpoint(obj), instructionAddressReference(LLDB_INVALID_ADDRESS), id(0),
InstructionBreakpoint::InstructionBreakpoint(DAP &d,
const llvm::json::Object &obj)
: Breakpoint(d, obj), instructionAddressReference(LLDB_INVALID_ADDRESS),
offset(GetSigned(obj, "offset", 0)) {
GetString(obj, "instructionReference")
.getAsInteger(0, instructionAddressReference);
instructionAddressReference += offset;
}

void InstructionBreakpoint::SetInstructionBreakpoint() {
bp = g_dap.target.BreakpointCreateByAddress(instructionAddressReference);
id = bp.GetID();
void InstructionBreakpoint::SetBreakpoint() {
bp = dap.target.BreakpointCreateByAddress(instructionAddressReference);
Breakpoint::SetBreakpoint();
}

} // namespace lldb_dap
11 changes: 5 additions & 6 deletions lldb/tools/lldb-dap/InstructionBreakpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@
#define LLDB_TOOLS_LLDB_DAP_INSTRUCTIONBREAKPOINT_H

#include "Breakpoint.h"
#include "DAPForward.h"
#include "lldb/lldb-types.h"
#include <cstdint>

namespace lldb_dap {

// Instruction Breakpoint
struct InstructionBreakpoint : public Breakpoint {

lldb::addr_t instructionAddressReference;
int32_t id;
int32_t offset;

InstructionBreakpoint()
: Breakpoint(), instructionAddressReference(LLDB_INVALID_ADDRESS), id(0),
offset(0) {}
InstructionBreakpoint(const llvm::json::Object &obj);
InstructionBreakpoint(DAP &d, const llvm::json::Object &obj);

// Set instruction breakpoint in LLDB as a new breakpoint
void SetInstructionBreakpoint();
void SetBreakpoint();
};

} // namespace lldb_dap
Expand Down
Loading