Skip to content

Commit 79a0a7b

Browse files
committed
Reuse creation of Source objects for assembly and normal sources
1 parent 8821c38 commit 79a0a7b

File tree

7 files changed

+100
-125
lines changed

7 files changed

+100
-125
lines changed

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
#include "Breakpoint.h"
1010
#include "DAP.h"
1111
#include "JSONUtils.h"
12-
#include "LLDBUtils.h"
1312
#include "lldb/API/SBAddress.h"
1413
#include "lldb/API/SBBreakpointLocation.h"
1514
#include "lldb/API/SBLineEntry.h"
1615
#include "lldb/API/SBMutex.h"
17-
#include "lldb/lldb-enumerations.h"
1816
#include "llvm/ADT/StringExtras.h"
1917
#include <cstddef>
2018
#include <cstdint>
@@ -66,17 +64,15 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
6664
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(m_bp.GetTarget()));
6765
breakpoint.instructionReference = formatted_addr;
6866

69-
lldb::StopDisassemblyType stop_disassembly_display =
70-
GetStopDisassemblyDisplay(m_dap.debugger);
71-
auto line_entry = bp_addr.GetLineEntry();
72-
if (!ShouldDisplayAssemblySource(line_entry, stop_disassembly_display)) {
67+
auto source = CreateSource(bp_addr, m_dap.debugger);
68+
if (!source.IsAssemblySource()) {
69+
auto line_entry = bp_addr.GetLineEntry();
7370
const auto line = line_entry.GetLine();
7471
if (line != LLDB_INVALID_LINE_NUMBER)
7572
breakpoint.line = line;
7673
const auto column = line_entry.GetColumn();
7774
if (column != LLDB_INVALID_COLUMN_NUMBER)
7875
breakpoint.column = column;
79-
breakpoint.source = CreateSource(line_entry);
8076
} else {
8177
// Assembly breakpoint.
8278
auto symbol = bp_addr.GetSymbol();
@@ -86,10 +82,10 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
8682
.ReadInstructions(symbol.GetStartAddress(), bp_addr, nullptr)
8783
.GetSize() +
8884
1;
89-
90-
breakpoint.source = CreateAssemblySource(m_dap.target, bp_addr);
9185
}
9286
}
87+
88+
breakpoint.source = std::move(source);
9389
}
9490

9591
return breakpoint;

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Protocol/ProtocolTypes.h"
1414
#include "RequestHandler.h"
1515
#include "lldb/API/SBAddress.h"
16+
#include "lldb/API/SBDebugger.h"
1617
#include "lldb/API/SBInstruction.h"
1718
#include "lldb/API/SBTarget.h"
1819
#include "lldb/lldb-types.h"
@@ -81,12 +82,15 @@ static lldb::SBAddress GetDisassembleStartAddress(lldb::SBTarget target,
8182
.GetAddress();
8283
}
8384

84-
static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
85-
lldb::SBTarget &target, lldb::SBInstruction &inst, bool resolve_symbols) {
85+
static DisassembledInstruction
86+
ConvertSBInstructionToDisassembledInstruction(lldb::SBDebugger &debugger,
87+
lldb::SBInstruction &inst,
88+
bool resolve_symbols) {
8689
if (!inst.IsValid())
8790
return GetInvalidInstruction();
8891

89-
auto addr = inst.GetAddress();
92+
lldb::SBTarget target = debugger.GetSelectedTarget();
93+
lldb::SBAddress addr = inst.GetAddress();
9094
const auto inst_addr = addr.GetLoadAddress(target);
9195

9296
// FIXME: This is a workaround - this address might come from
@@ -139,15 +143,14 @@ static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
139143

140144
disassembled_inst.instruction = std::move(instruction);
141145

146+
auto source = CreateSource(addr, debugger);
142147
auto line_entry = addr.GetLineEntry();
143-
// If the line number is 0 then the entry represents a compiler generated
144-
// location.
145148

146-
if (line_entry.GetStartAddress() == addr && line_entry.IsValid() &&
149+
if (!source.IsAssemblySource() && line_entry.IsValid() &&
150+
line_entry.GetStartAddress() == addr && line_entry.IsValid() &&
147151
line_entry.GetFileSpec().IsValid() && line_entry.GetLine() != 0) {
148-
auto source = CreateSource(line_entry);
149-
disassembled_inst.location = std::move(source);
150152

153+
disassembled_inst.location = std::move(source);
151154
const auto line = line_entry.GetLine();
152155
if (line != 0 && line != LLDB_INVALID_LINE_NUMBER)
153156
disassembled_inst.line = line;
@@ -235,7 +238,7 @@ DisassembleRequestHandler::Run(const DisassembleArguments &args) const {
235238
original_address_index = i;
236239

237240
instructions.push_back(ConvertSBInstructionToDisassembledInstruction(
238-
dap.target, inst, resolve_symbols));
241+
dap.debugger, inst, resolve_symbols));
239242
}
240243

241244
// Check if we miss instructions at the beginning.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
1212
#include "RequestHandler.h"
13+
#include "lldb/API/SBAddress.h"
1314
#include "lldb/API/SBDeclaration.h"
15+
#include "lldb/API/SBLineEntry.h"
1416

1517
namespace lldb_dap {
1618

@@ -122,9 +124,9 @@ void LocationsRequestHandler::operator()(
122124
return;
123125
}
124126

125-
lldb::addr_t addr = variable.GetValueAsAddress();
126-
lldb::SBLineEntry line_entry =
127-
dap.target.ResolveLoadAddress(addr).GetLineEntry();
127+
lldb::addr_t raw_addr = variable.GetValueAsAddress();
128+
lldb::SBAddress addr = dap.target.ResolveLoadAddress(raw_addr);
129+
lldb::SBLineEntry line_entry = addr.GetLineEntry();
128130

129131
if (!line_entry.IsValid()) {
130132
response["success"] = false;
@@ -133,7 +135,7 @@ void LocationsRequestHandler::operator()(
133135
return;
134136
}
135137

136-
body.try_emplace("source", CreateSource(line_entry.GetFileSpec()));
138+
body.try_emplace("source", CreateSource(addr, dap.debugger));
137139
if (int line = line_entry.GetLine())
138140
body.try_emplace("line", line);
139141
if (int column = line_entry.GetColumn())

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#include "DAP.h"
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
12-
#include "LLDBUtils.h"
1312
#include "RequestHandler.h"
14-
#include "lldb/lldb-enumerations.h"
1513

1614
namespace lldb_dap {
1715

@@ -55,8 +53,6 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
5553
llvm::json::Array &stack_frames, int64_t &offset,
5654
const int64_t start_frame, const int64_t levels,
5755
const bool include_all) {
58-
lldb::StopDisassemblyType stop_disassembly_display =
59-
GetStopDisassemblyDisplay(dap.debugger);
6056
bool reached_end_of_stack = false;
6157
for (int64_t i = start_frame;
6258
static_cast<int64_t>(stack_frames.size()) < levels; i++) {
@@ -74,7 +70,7 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
7470
}
7571

7672
stack_frames.emplace_back(
77-
CreateStackFrame(frame, frame_format, stop_disassembly_display));
73+
CreateStackFrame(frame, frame_format, dap.debugger));
7874
}
7975

8076
if (include_all && reached_end_of_stack) {

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -497,34 +497,33 @@ static std::string GetLoadAddressString(const lldb::addr_t addr) {
497497
return result;
498498
}
499499

500-
protocol::Source CreateSource(const lldb::SBFileSpec &file) {
501-
protocol::Source source;
502-
if (file.IsValid()) {
503-
const char *name = file.GetFilename();
504-
if (name)
505-
source.name = name;
506-
char path[PATH_MAX] = "";
507-
if (file.GetPath(path, sizeof(path)) &&
508-
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX))
509-
source.path = path;
510-
}
511-
return source;
512-
}
500+
static bool ShouldDisplayAssemblySource(
501+
lldb::SBAddress address,
502+
lldb::StopDisassemblyType stop_disassembly_display) {
503+
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNever)
504+
return false;
513505

514-
protocol::Source CreateSource(const lldb::SBLineEntry &line_entry) {
515-
return CreateSource(line_entry.GetFileSpec());
516-
}
506+
if (stop_disassembly_display == lldb::eStopDisassemblyTypeAlways)
507+
return true;
517508

518-
protocol::Source CreateSource(llvm::StringRef source_path) {
519-
protocol::Source source;
520-
llvm::StringRef name = llvm::sys::path::filename(source_path);
521-
source.name = name;
522-
source.path = source_path;
523-
return source;
509+
// A line entry of 0 indicates the line is compiler generated i.e. no source
510+
// file is associated with the frame.
511+
auto line_entry = address.GetLineEntry();
512+
auto file_spec = line_entry.GetFileSpec();
513+
if (!file_spec.IsValid() || line_entry.GetLine() == 0 ||
514+
line_entry.GetLine() == LLDB_INVALID_LINE_NUMBER)
515+
return true;
516+
517+
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNoSource &&
518+
!file_spec.Exists()) {
519+
return true;
520+
}
521+
522+
return false;
524523
}
525524

526-
protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
527-
lldb::SBAddress &address) {
525+
static protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
526+
lldb::SBAddress address) {
528527
protocol::Source source;
529528

530529
auto symbol = address.GetSymbol();
@@ -558,28 +557,36 @@ protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
558557
return source;
559558
}
560559

561-
bool ShouldDisplayAssemblySource(
562-
const lldb::SBLineEntry &line_entry,
563-
lldb::StopDisassemblyType stop_disassembly_display) {
564-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNever)
565-
return false;
566-
567-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeAlways)
568-
return true;
560+
protocol::Source CreateSource(const lldb::SBFileSpec &file) {
561+
protocol::Source source;
562+
if (file.IsValid()) {
563+
const char *name = file.GetFilename();
564+
if (name)
565+
source.name = name;
566+
char path[PATH_MAX] = "";
567+
if (file.GetPath(path, sizeof(path)) &&
568+
lldb::SBFileSpec::ResolvePath(path, path, PATH_MAX))
569+
source.path = path;
570+
}
571+
return source;
572+
}
569573

570-
// A line entry of 0 indicates the line is compiler generated i.e. no source
571-
// file is associated with the frame.
572-
auto file_spec = line_entry.GetFileSpec();
573-
if (!file_spec.IsValid() || line_entry.GetLine() == 0 ||
574-
line_entry.GetLine() == LLDB_INVALID_LINE_NUMBER)
575-
return true;
574+
protocol::Source CreateSource(lldb::SBAddress address,
575+
lldb::SBDebugger &debugger) {
576+
lldb::StopDisassemblyType stop_disassembly_display =
577+
GetStopDisassemblyDisplay(debugger);
578+
if (!ShouldDisplayAssemblySource(address, stop_disassembly_display))
579+
return CreateSource(address.GetLineEntry().GetFileSpec());
576580

577-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNoSource &&
578-
!file_spec.Exists()) {
579-
return true;
580-
}
581+
return CreateAssemblySource(debugger.GetSelectedTarget(), address);
582+
}
581583

582-
return false;
584+
protocol::Source CreateSource(llvm::StringRef source_path) {
585+
protocol::Source source;
586+
llvm::StringRef name = llvm::sys::path::filename(source_path);
587+
source.name = name;
588+
source.path = source_path;
589+
return source;
583590
}
584591

585592
// "StackFrame": {
@@ -643,9 +650,8 @@ bool ShouldDisplayAssemblySource(
643650
// },
644651
// "required": [ "id", "name", "line", "column" ]
645652
// }
646-
llvm::json::Value
647-
CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
648-
lldb::StopDisassemblyType stop_disassembly_display) {
653+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
654+
lldb::SBDebugger &debugger) {
649655
llvm::json::Object object;
650656
int64_t frame_id = MakeDAPFrameID(frame);
651657
object.try_emplace("id", frame_id);
@@ -673,22 +679,17 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
673679

674680
EmplaceSafeString(object, "name", frame_name);
675681

676-
auto line_entry = frame.GetLineEntry();
677-
if (!ShouldDisplayAssemblySource(line_entry, stop_disassembly_display)) {
678-
object.try_emplace("source", CreateSource(line_entry));
682+
auto source = CreateSource(frame.GetPCAddress(), debugger);
683+
if (!source.IsAssemblySource()) {
684+
// This is a normal source with valid line entry.
685+
auto line_entry = frame.GetLineEntry();
679686
object.try_emplace("line", line_entry.GetLine());
680687
auto column = line_entry.GetColumn();
681688
object.try_emplace("column", column);
682689
} else if (frame.GetSymbol().IsValid()) {
683-
// If no source is associated with the frame, use the DAPFrameID to track
684-
// the 'source' and generate assembly.
685-
auto frame_address = frame.GetPCAddress();
686-
object.try_emplace("source", CreateAssemblySource(
687-
frame.GetThread().GetProcess().GetTarget(),
688-
frame_address));
689-
690-
// Calculate the line of the current PC from the start of the current
691-
// symbol.
690+
// This is a source where the disassembly is used, but there is a valid
691+
// symbol. Calculate the line of the current PC from the start of the
692+
// current symbol.
692693
lldb::SBTarget target = frame.GetThread().GetProcess().GetTarget();
693694
lldb::SBInstructionList inst_list = target.ReadInstructions(
694695
frame.GetSymbol().GetStartAddress(), frame.GetPCAddress(), nullptr);
@@ -699,14 +700,12 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
699700
object.try_emplace("column", 1);
700701
} else {
701702
// No valid line entry or symbol.
702-
auto frame_address = frame.GetPCAddress();
703-
object.try_emplace("source", CreateAssemblySource(
704-
frame.GetThread().GetProcess().GetTarget(),
705-
frame_address));
706703
object.try_emplace("line", 1);
707704
object.try_emplace("column", 1);
708705
}
709706

707+
object.try_emplace("source", std::move(source));
708+
710709
const auto pc = frame.GetPC();
711710
if (pc != LLDB_INVALID_ADDRESS) {
712711
std::string formatted_addr = "0x" + llvm::utohexstr(pc);

0 commit comments

Comments
 (0)