Skip to content

Commit 22dfe9c

Browse files
authored
[lldb-dap] Reuse source object logics (#141426)
Refactor code revolving source objects such that most logics will be reused. The main change is to expose a single `CreateSource(addr, target)` that can return either a normal or an assembly source object, and call `ShouldDisplayAssemblySource()` only from this function instead of multiple places across the code. Other functions can use `source.IsAssemblySource()` in order to check which type the source is.
1 parent ad0a522 commit 22dfe9c

13 files changed

+237
-207
lines changed

lldb/source/API/SBTarget.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,17 @@ SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
636636
uint32_t resolve_scope) {
637637
LLDB_INSTRUMENT_VA(this, addr, resolve_scope);
638638

639-
SBSymbolContext sc;
639+
SBSymbolContext sb_sc;
640640
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
641641
if (addr.IsValid()) {
642-
if (TargetSP target_sp = GetSP())
642+
if (TargetSP target_sp = GetSP()) {
643+
lldb_private::SymbolContext &sc = sb_sc.ref();
644+
sc.target_sp = target_sp;
643645
target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
644-
sc.ref());
646+
sc);
647+
}
645648
}
646-
return sc;
649+
return sb_sc;
647650
}
648651

649652
size_t SBTarget::ReadMemory(const SBAddress addr, void *buf, size_t size,

lldb/source/Core/Module.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ uint32_t Module::ResolveSymbolContextForAddress(
483483
symfile->SetLoadDebugInfoEnabled();
484484
resolved_flags |=
485485
symfile->ResolveSymbolContext(so_addr, resolve_scope, sc);
486+
487+
if ((resolve_scope & eSymbolContextLineEntry) && sc.line_entry.IsValid())
488+
sc.line_entry.ApplyFileMappings(sc.target_sp);
486489
}
487490

488491
// Resolve the symbol if requested, but don't re-look it up if we've

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
#include "Breakpoint.h"
1010
#include "DAP.h"
11-
#include "JSONUtils.h"
12-
#include "LLDBUtils.h"
11+
#include "ProtocolUtils.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.target);
68+
if (!IsAssemblySource(source)) {
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/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_lldb_library(lldbDAP
2121
LLDBUtils.cpp
2222
OutputRedirector.cpp
2323
ProgressEvent.cpp
24+
ProtocolUtils.cpp
2425
RunInTerminal.cpp
2526
SourceBreakpoint.cpp
2627
Transport.cpp

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
#include "DAP.h"
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
12+
#include "LLDBUtils.h"
1213
#include "Protocol/ProtocolRequests.h"
1314
#include "Protocol/ProtocolTypes.h"
15+
#include "ProtocolUtils.h"
1416
#include "RequestHandler.h"
1517
#include "lldb/API/SBAddress.h"
1618
#include "lldb/API/SBInstruction.h"
19+
#include "lldb/API/SBLineEntry.h"
1720
#include "lldb/API/SBTarget.h"
1821
#include "lldb/lldb-types.h"
1922
#include "llvm/ADT/StringExtras.h"
@@ -139,15 +142,16 @@ static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
139142

140143
disassembled_inst.instruction = std::move(instruction);
141144

142-
auto line_entry = addr.GetLineEntry();
145+
protocol::Source source = CreateSource(addr, target);
146+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, addr);
147+
143148
// If the line number is 0 then the entry represents a compiler generated
144149
// location.
150+
if (!IsAssemblySource(source) && line_entry.GetStartAddress() == addr &&
151+
line_entry.IsValid() && line_entry.GetFileSpec().IsValid() &&
152+
line_entry.GetLine() != 0) {
145153

146-
if (line_entry.GetStartAddress() == addr && line_entry.IsValid() &&
147-
line_entry.GetFileSpec().IsValid() && line_entry.GetLine() != 0) {
148-
auto source = CreateSource(line_entry);
149154
disassembled_inst.location = std::move(source);
150-
151155
const auto line = line_entry.GetLine();
152156
if (line != 0 && line != LLDB_INVALID_LINE_NUMBER)
153157
disassembled_inst.line = line;
@@ -156,7 +160,8 @@ static DisassembledInstruction ConvertSBInstructionToDisassembledInstruction(
156160
if (column != 0 && column != LLDB_INVALID_COLUMN_NUMBER)
157161
disassembled_inst.column = column;
158162

159-
auto end_line_entry = line_entry.GetEndAddress().GetLineEntry();
163+
lldb::SBAddress end_addr = line_entry.GetEndAddress();
164+
auto end_line_entry = GetLineEntryForAddress(target, end_addr);
160165
if (end_line_entry.IsValid() &&
161166
end_line_entry.GetFileSpec() == line_entry.GetFileSpec()) {
162167
const auto end_line = end_line_entry.GetLine();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
#include "DAP.h"
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
12+
#include "LLDBUtils.h"
13+
#include "ProtocolUtils.h"
1214
#include "RequestHandler.h"
15+
#include "lldb/API/SBAddress.h"
1316
#include "lldb/API/SBDeclaration.h"
17+
#include "lldb/API/SBLineEntry.h"
1418

1519
namespace lldb_dap {
1620

@@ -122,9 +126,9 @@ void LocationsRequestHandler::operator()(
122126
return;
123127
}
124128

125-
lldb::addr_t addr = variable.GetValueAsAddress();
126-
lldb::SBLineEntry line_entry =
127-
dap.target.ResolveLoadAddress(addr).GetLineEntry();
129+
lldb::addr_t raw_addr = variable.GetValueAsAddress();
130+
lldb::SBAddress addr = dap.target.ResolveLoadAddress(raw_addr);
131+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(dap.target, addr);
128132

129133
if (!line_entry.IsValid()) {
130134
response["success"] = false;

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

Lines changed: 1 addition & 6 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++) {
@@ -73,8 +69,7 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
7369
break;
7470
}
7571

76-
stack_frames.emplace_back(
77-
CreateStackFrame(frame, frame_format, stop_disassembly_display));
72+
stack_frames.emplace_back(CreateStackFrame(frame, frame_format));
7873
}
7974

8075
if (include_all && reached_end_of_stack) {

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 13 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "DAP.h"
1111
#include "ExceptionBreakpoint.h"
1212
#include "LLDBUtils.h"
13+
#include "ProtocolUtils.h"
1314
#include "lldb/API/SBAddress.h"
1415
#include "lldb/API/SBCompileUnit.h"
1516
#include "lldb/API/SBDeclaration.h"
@@ -490,98 +491,6 @@ CreateExceptionBreakpointFilter(const ExceptionBreakpoint &bp) {
490491
return filter;
491492
}
492493

493-
static std::string GetLoadAddressString(const lldb::addr_t addr) {
494-
std::string result;
495-
llvm::raw_string_ostream os(result);
496-
os << llvm::format_hex(addr, 18);
497-
return result;
498-
}
499-
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-
}
513-
514-
protocol::Source CreateSource(const lldb::SBLineEntry &line_entry) {
515-
return CreateSource(line_entry.GetFileSpec());
516-
}
517-
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;
524-
}
525-
526-
protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
527-
lldb::SBAddress &address) {
528-
protocol::Source source;
529-
530-
auto symbol = address.GetSymbol();
531-
std::string name;
532-
if (symbol.IsValid()) {
533-
source.sourceReference = symbol.GetStartAddress().GetLoadAddress(target);
534-
name = symbol.GetName();
535-
} else {
536-
const auto load_addr = address.GetLoadAddress(target);
537-
source.sourceReference = load_addr;
538-
name = GetLoadAddressString(load_addr);
539-
}
540-
541-
lldb::SBModule module = address.GetModule();
542-
if (module.IsValid()) {
543-
lldb::SBFileSpec file_spec = module.GetFileSpec();
544-
if (file_spec.IsValid()) {
545-
std::string path = GetSBFileSpecPath(file_spec);
546-
if (!path.empty())
547-
source.path = path + '`' + name;
548-
}
549-
}
550-
551-
source.name = std::move(name);
552-
553-
// Mark the source as deemphasized since users will only be able to view
554-
// assembly for these frames.
555-
source.presentationHint =
556-
protocol::Source::PresentationHint::eSourcePresentationHintDeemphasize;
557-
558-
return source;
559-
}
560-
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;
569-
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;
576-
577-
if (stop_disassembly_display == lldb::eStopDisassemblyTypeNoSource &&
578-
!file_spec.Exists()) {
579-
return true;
580-
}
581-
582-
return false;
583-
}
584-
585494
// "StackFrame": {
586495
// "type": "object",
587496
// "description": "A Stackframe contains the source location.",
@@ -643,9 +552,8 @@ bool ShouldDisplayAssemblySource(
643552
// },
644553
// "required": [ "id", "name", "line", "column" ]
645554
// }
646-
llvm::json::Value
647-
CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
648-
lldb::StopDisassemblyType stop_disassembly_display) {
555+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
556+
lldb::SBFormat &format) {
649557
llvm::json::Object object;
650558
int64_t frame_id = MakeDAPFrameID(frame);
651559
object.try_emplace("id", frame_id);
@@ -673,22 +581,18 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
673581

674582
EmplaceSafeString(object, "name", frame_name);
675583

676-
auto line_entry = frame.GetLineEntry();
677-
if (!ShouldDisplayAssemblySource(line_entry, stop_disassembly_display)) {
678-
object.try_emplace("source", CreateSource(line_entry));
584+
auto target = frame.GetThread().GetProcess().GetTarget();
585+
auto source = CreateSource(frame.GetPCAddress(), target);
586+
if (!IsAssemblySource(source)) {
587+
// This is a normal source with a valid line entry.
588+
auto line_entry = frame.GetLineEntry();
679589
object.try_emplace("line", line_entry.GetLine());
680590
auto column = line_entry.GetColumn();
681591
object.try_emplace("column", column);
682592
} 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.
593+
// This is a source where the disassembly is used, but there is a valid
594+
// symbol. Calculate the line of the current PC from the start of the
595+
// current symbol.
692596
lldb::SBTarget target = frame.GetThread().GetProcess().GetTarget();
693597
lldb::SBInstructionList inst_list = target.ReadInstructions(
694598
frame.GetSymbol().GetStartAddress(), frame.GetPCAddress(), nullptr);
@@ -699,14 +603,12 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
699603
object.try_emplace("column", 1);
700604
} else {
701605
// 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));
706606
object.try_emplace("line", 1);
707607
object.try_emplace("column", 1);
708608
}
709609

610+
object.try_emplace("source", std::move(source));
611+
710612
const auto pc = frame.GetPC();
711613
if (pc != LLDB_INVALID_ADDRESS) {
712614
std::string formatted_addr = "0x" + llvm::utohexstr(pc);

0 commit comments

Comments
 (0)