Skip to content

Commit 61623de

Browse files
committed
use load address as sourceReference
1 parent 3699524 commit 61623de

File tree

8 files changed

+96
-62
lines changed

8 files changed

+96
-62
lines changed

lldb/include/lldb/API/SBFileSpec.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_API_SBFILESPEC_H
1111

1212
#include "lldb/API/SBDefines.h"
13+
#include "lldb/API/SBStream.h"
1314

1415
namespace lldb {
1516

@@ -53,6 +54,8 @@ class LLDB_API SBFileSpec {
5354

5455
uint32_t GetPath(char *dst_path, size_t dst_len) const;
5556

57+
bool GetPath(lldb::SBStream &dst_path) const;
58+
5659
static int ResolvePath(const char *src_path, char *dst_path, size_t dst_len);
5760

5861
bool GetDescription(lldb::SBStream &description) const;

lldb/source/API/SBFileSpec.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <cinttypes>
2121
#include <climits>
22+
#include <string>
2223

2324
using namespace lldb;
2425
using namespace lldb_private;
@@ -147,6 +148,13 @@ uint32_t SBFileSpec::GetPath(char *dst_path, size_t dst_len) const {
147148
return result;
148149
}
149150

151+
bool SBFileSpec::GetPath(SBStream &dst_path) const {
152+
LLDB_INSTRUMENT_VA(this, dst_path);
153+
154+
std::string path = m_opaque_up->GetPath();
155+
return dst_path->PutCString(path.c_str()) > 0;
156+
}
157+
150158
const lldb_private::FileSpec *SBFileSpec::operator->() const {
151159
return m_opaque_up.get();
152160
}

lldb/tools/lldb-dap/Breakpoint.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,15 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
7979
breakpoint.source = CreateSource(line_entry);
8080
} else {
8181
// Breakpoint made by assembly
82-
auto symbol_context = bp_addr.GetSymbolContext(
83-
lldb::eSymbolContextSymbol | lldb::eSymbolContextModule);
84-
if (symbol_context.IsValid()) {
85-
auto symbol = symbol_context.GetSymbol();
82+
auto symbol = bp_addr.GetSymbol();
83+
if (symbol.IsValid()) {
8684
breakpoint.line =
8785
m_bp.GetTarget()
8886
.ReadInstructions(symbol.GetStartAddress(), bp_addr, nullptr)
8987
.GetSize() +
9088
1;
91-
protocol::Source source;
92-
source.name = symbol.GetName();
9389

94-
auto module = symbol_context.GetModule();
95-
if (module.IsValid()) {
96-
std::string path = module.GetFileSpec().GetDirectory();
97-
path += "/";
98-
path += module.GetFileSpec().GetFilename();
99-
source.path = std::move(path);
100-
}
101-
102-
breakpoint.source = std::move(source);
90+
breakpoint.source = CreateAssemblySource(m_dap.target, bp_addr);
10391
}
10492
}
10593
}

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

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

99
#include "DAP.h"
10-
#include "LLDBUtils.h"
1110
#include "RequestHandler.h"
1211
#include <vector>
1312

@@ -108,15 +107,11 @@ template <unsigned N>
108107
void BreakpointLocationsRequestHandler::AddAssemblyBreakpointLocations(
109108
llvm::SmallVector<std::pair<uint32_t, uint32_t>, N> &locations,
110109
int64_t sourceReference, uint32_t start_line, uint32_t end_line) const {
111-
lldb::SBProcess process = dap.target.GetProcess();
112-
lldb::SBThread thread =
113-
process.GetThreadByIndexID(GetLLDBThreadIndexID(sourceReference));
114-
lldb::SBFrame frame = thread.GetFrameAtIndex(GetLLDBFrameID(sourceReference));
115-
116-
if (!frame.IsValid())
110+
lldb::SBAddress address(sourceReference, dap.target);
111+
if (!address.IsValid())
117112
return;
118113

119-
lldb::SBSymbol symbol = frame.GetSymbol();
114+
lldb::SBSymbol symbol = address.GetSymbol();
120115
if (!symbol.IsValid())
121116
return;
122117

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "DAP.h"
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
12-
#include "LLDBUtils.h"
1312
#include "Protocol/ProtocolRequests.h"
1413
#include "RequestHandler.h"
1514
#include <vector>
@@ -99,15 +98,11 @@ SetBreakpointsRequestHandler::SetAssemblyBreakpoints(
9998
std::vector<protocol::Breakpoint> response_breakpoints;
10099
int64_t sourceReference = source.sourceReference.value_or(0);
101100

102-
lldb::SBProcess process = dap.target.GetProcess();
103-
lldb::SBThread thread =
104-
process.GetThreadByIndexID(GetLLDBThreadIndexID(sourceReference));
105-
lldb::SBFrame frame = thread.GetFrameAtIndex(GetLLDBFrameID(sourceReference));
106-
107-
if (!frame.IsValid())
101+
lldb::SBAddress address(sourceReference, dap.target);
102+
if (!address.IsValid())
108103
return response_breakpoints;
109104

110-
lldb::SBSymbol symbol = frame.GetSymbol();
105+
lldb::SBSymbol symbol = address.GetSymbol();
111106
if (!symbol.IsValid())
112107
return response_breakpoints; // Not yet supporting breakpoints in assembly
113108
// without a valid symbol

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "LLDBUtils.h"
1212
#include "Protocol/ProtocolRequests.h"
1313
#include "Protocol/ProtocolTypes.h"
14+
#include "lldb/API/SBAddress.h"
1415
#include "lldb/API/SBExecutionContext.h"
1516
#include "lldb/API/SBFrame.h"
1617
#include "lldb/API/SBInstructionList.h"
@@ -19,6 +20,7 @@
1920
#include "lldb/API/SBSymbol.h"
2021
#include "lldb/API/SBTarget.h"
2122
#include "lldb/API/SBThread.h"
23+
#include "lldb/lldb-types.h"
2224
#include "llvm/Support/Error.h"
2325

2426
namespace lldb_dap {
@@ -34,26 +36,22 @@ SourceRequestHandler::Run(const protocol::SourceArguments &args) const {
3436
return llvm::make_error<DAPError>(
3537
"invalid arguments, expected source.sourceReference to be set");
3638

37-
lldb::SBProcess process = dap.target.GetProcess();
38-
// Upper 32 bits is the thread index ID
39-
lldb::SBThread thread =
40-
process.GetThreadByIndexID(GetLLDBThreadIndexID(source));
41-
// Lower 32 bits is the frame index
42-
lldb::SBFrame frame = thread.GetFrameAtIndex(GetLLDBFrameID(source));
43-
if (!frame.IsValid())
39+
lldb::SBAddress address(source, dap.target);
40+
if (!address.IsValid())
4441
return llvm::make_error<DAPError>("source not found");
4542

43+
lldb::SBSymbol symbol = address.GetSymbol();
44+
4645
lldb::SBStream stream;
47-
lldb::SBExecutionContext exe_ctx(frame);
48-
lldb::SBSymbol symbol = frame.GetSymbol();
46+
lldb::SBExecutionContext exe_ctx(dap.target);
4947

5048
if (symbol.IsValid()) {
5149
lldb::SBInstructionList insts = symbol.GetInstructions(dap.target);
5250
insts.GetDescription(stream, exe_ctx);
5351
} else {
5452
// No valid symbol, just return the disassembly.
5553
lldb::SBInstructionList insts = dap.target.ReadInstructions(
56-
frame.GetPCAddress(), dap.number_of_assembly_lines_for_nodebug);
54+
address, dap.number_of_assembly_lines_for_nodebug);
5755
insts.GetDescription(stream, exe_ctx);
5856
}
5957

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,13 @@ CreateExceptionBreakpointFilter(const ExceptionBreakpoint &bp) {
490490
return filter;
491491
}
492492

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+
493500
protocol::Source CreateSource(const lldb::SBFileSpec &file) {
494501
protocol::Source source;
495502
if (file.IsValid()) {
@@ -516,6 +523,43 @@ protocol::Source CreateSource(llvm::StringRef source_path) {
516523
return source;
517524
}
518525

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+
lldb::SBStream module_path;
546+
if (file_spec.GetPath(module_path)) {
547+
std::string path = module_path.GetData();
548+
source.path = path + '`' + name;
549+
}
550+
}
551+
}
552+
553+
source.name = std::move(name);
554+
555+
// Mark the source as deemphasized since users will only be able to view
556+
// assembly for these frames.
557+
source.presentationHint =
558+
protocol::Source::PresentationHint::eSourcePresentationHintDeemphasize;
559+
560+
return source;
561+
}
562+
519563
bool ShouldDisplayAssemblySource(
520564
const lldb::SBLineEntry &line_entry,
521565
lldb::StopDisassemblyType stop_disassembly_display) {
@@ -619,12 +663,10 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
619663
frame_name = name;
620664
}
621665

622-
if (frame_name.empty()) {
666+
if (frame_name.empty())
623667
// If the function name is unavailable, display the pc address as a 16-digit
624668
// hex string, e.g. "0x0000000000012345"
625-
llvm::raw_string_ostream os(frame_name);
626-
os << llvm::format_hex(frame.GetPC(), 18);
627-
}
669+
frame_name = GetLoadAddressString(frame.GetPC());
628670

629671
// We only include `[opt]` if a custom frame format is not specified.
630672
if (!format && frame.GetFunction().GetIsOptimized())
@@ -641,17 +683,10 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
641683
} else if (frame.GetSymbol().IsValid()) {
642684
// If no source is associated with the frame, use the DAPFrameID to track
643685
// the 'source' and generate assembly.
644-
llvm::json::Object source;
645-
EmplaceSafeString(source, "name", frame_name);
646-
char buf[PATH_MAX] = {0};
647-
size_t size = frame.GetModule().GetFileSpec().GetPath(buf, PATH_MAX);
648-
EmplaceSafeString(source, "path",
649-
std::string(buf, size) + '`' + frame_name);
650-
source.try_emplace("sourceReference", MakeDAPFrameID(frame));
651-
// Mark the source as deemphasized since users will only be able to view
652-
// assembly for these frames.
653-
EmplaceSafeString(source, "presentationHint", "deemphasize");
654-
object.try_emplace("source", std::move(source));
686+
auto frame_address = frame.GetPCAddress();
687+
object.try_emplace("source", CreateAssemblySource(
688+
frame.GetThread().GetProcess().GetTarget(),
689+
frame_address));
655690

656691
// Calculate the line of the current PC from the start of the current
657692
// symbol.
@@ -665,12 +700,10 @@ CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
665700
object.try_emplace("column", 1);
666701
} else {
667702
// No valid line entry or symbol.
668-
llvm::json::Object source;
669-
EmplaceSafeString(source, "name", frame_name);
670-
source.try_emplace("sourceReference", MakeDAPFrameID(frame));
671-
EmplaceSafeString(source, "presentationHint", "deemphasize");
672-
object.try_emplace("source", std::move(source));
673-
703+
auto frame_address = frame.GetPCAddress();
704+
object.try_emplace("source", CreateAssemblySource(
705+
frame.GetThread().GetProcess().GetTarget(),
706+
frame_address));
674707
object.try_emplace("line", 1);
675708
object.try_emplace("column", 1);
676709
}

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ protocol::Source CreateSource(const lldb::SBLineEntry &line_entry);
269269
/// definition outlined by Microsoft.
270270
protocol::Source CreateSource(llvm::StringRef source_path);
271271

272+
/// Create a "Source" object for a given frame, using its assembly for source.
273+
///
274+
/// \param[in] target
275+
/// The relevant target.
276+
///
277+
/// \param[in] address
278+
/// The address to use when creating the "Source" object.
279+
///
280+
/// \return
281+
/// A "Source" JSON object that follows the formal JSON
282+
/// definition outlined by Microsoft.
283+
protocol::Source CreateAssemblySource(const lldb::SBTarget &target,
284+
lldb::SBAddress &address);
285+
272286
/// Return true if the given line entry should be displayed as assembly.
273287
///
274288
/// \param[in] line_entry

0 commit comments

Comments
 (0)