Skip to content

Commit 6a4a95b

Browse files
committed
apply source mapping when getting line entry from an address
1 parent 4c20703 commit 6a4a95b

File tree

11 files changed

+84
-38
lines changed

11 files changed

+84
-38
lines changed

lldb/include/lldb/API/SBAddress.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "lldb/API/SBDefines.h"
1313
#include "lldb/API/SBModule.h"
14+
#include "lldb/API/SBTarget.h"
1415

1516
namespace lldb {
1617

@@ -58,6 +59,9 @@ class LLDB_API SBAddress {
5859
// "lldb::SBAddress SBTarget::ResolveLoadAddress (...)".
5960
lldb::SBSymbolContext GetSymbolContext(uint32_t resolve_scope);
6061

62+
lldb::SBSymbolContext GetSymbolContext(const SBTarget &target,
63+
uint32_t resolve_scope);
64+
6165
// The following functions grab individual objects for a given address and
6266
// are less efficient if you want more than one symbol related objects. Use
6367
// one of the following when you want multiple debug symbol related objects
@@ -122,6 +126,9 @@ class LLDB_API SBAddress {
122126

123127
void SetAddress(const lldb_private::Address &address);
124128

129+
void CalculateSymbolContext(lldb_private::SymbolContext &sc,
130+
uint32_t resolve_scope);
131+
125132
private:
126133
std::unique_ptr<lldb_private::Address> m_opaque_up;
127134
};

lldb/source/API/SBAddress.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,18 @@ SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
213213
LLDB_INSTRUMENT_VA(this, resolve_scope);
214214

215215
SBSymbolContext sb_sc;
216-
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
217-
if (m_opaque_up->IsValid())
218-
m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
216+
CalculateSymbolContext(sb_sc.ref(), resolve_scope);
217+
return sb_sc;
218+
}
219+
220+
SBSymbolContext SBAddress::GetSymbolContext(const SBTarget &target,
221+
uint32_t resolve_scope) {
222+
LLDB_INSTRUMENT_VA(this, target, resolve_scope);
223+
224+
SBSymbolContext sb_sc;
225+
lldb_private::SymbolContext &sc = sb_sc.ref();
226+
sc.target_sp = target.GetSP();
227+
CalculateSymbolContext(sc, resolve_scope);
219228
return sb_sc;
220229
}
221230

@@ -266,3 +275,10 @@ SBLineEntry SBAddress::GetLineEntry() {
266275
}
267276
return sb_line_entry;
268277
}
278+
279+
void SBAddress::CalculateSymbolContext(lldb_private::SymbolContext &sc,
280+
uint32_t resolve_scope) {
281+
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
282+
if (m_opaque_up->IsValid())
283+
m_opaque_up->CalculateSymbolContext(&sc, scope);
284+
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protocol::Breakpoint Breakpoint::ToProtocolBreakpoint() {
6464
"0x" + llvm::utohexstr(bp_addr.GetLoadAddress(m_bp.GetTarget()));
6565
breakpoint.instructionReference = formatted_addr;
6666

67-
auto source = CreateSource(bp_addr, m_dap.debugger);
67+
auto source = CreateSource(bp_addr, m_dap.target);
6868
if (!source.IsAssemblySource()) {
6969
auto line_entry = bp_addr.GetLineEntry();
7070
const auto line = line_entry.GetLine();

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
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"
1415
#include "RequestHandler.h"
1516
#include "lldb/API/SBAddress.h"
16-
#include "lldb/API/SBDebugger.h"
1717
#include "lldb/API/SBInstruction.h"
1818
#include "lldb/API/SBTarget.h"
1919
#include "lldb/lldb-types.h"
@@ -82,15 +82,12 @@ static lldb::SBAddress GetDisassembleStartAddress(lldb::SBTarget target,
8282
.GetAddress();
8383
}
8484

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

92-
lldb::SBTarget target = debugger.GetSelectedTarget();
93-
lldb::SBAddress addr = inst.GetAddress();
90+
auto addr = inst.GetAddress();
9491
const auto inst_addr = addr.GetLoadAddress(target);
9592

9693
// FIXME: This is a workaround - this address might come from
@@ -143,9 +140,11 @@ ConvertSBInstructionToDisassembledInstruction(lldb::SBDebugger &debugger,
143140

144141
disassembled_inst.instruction = std::move(instruction);
145142

146-
auto source = CreateSource(addr, debugger);
147-
auto line_entry = addr.GetLineEntry();
143+
auto source = CreateSource(addr, target);
144+
auto line_entry = GetLineEntryForAddress(target, addr);
148145

146+
// If the line number is 0 then the entry represents a compiler generated
147+
// location.
149148
if (!source.IsAssemblySource() && line_entry.IsValid() &&
150149
line_entry.GetStartAddress() == addr && line_entry.IsValid() &&
151150
line_entry.GetFileSpec().IsValid() && line_entry.GetLine() != 0) {
@@ -159,7 +158,8 @@ ConvertSBInstructionToDisassembledInstruction(lldb::SBDebugger &debugger,
159158
if (column != 0 && column != LLDB_INVALID_COLUMN_NUMBER)
160159
disassembled_inst.column = column;
161160

162-
auto end_line_entry = line_entry.GetEndAddress().GetLineEntry();
161+
lldb::SBAddress end_addr = line_entry.GetEndAddress();
162+
auto end_line_entry = GetLineEntryForAddress(target, end_addr);
163163
if (end_line_entry.IsValid() &&
164164
end_line_entry.GetFileSpec() == line_entry.GetFileSpec()) {
165165
const auto end_line = end_line_entry.GetLine();
@@ -238,7 +238,7 @@ DisassembleRequestHandler::Run(const DisassembleArguments &args) const {
238238
original_address_index = i;
239239

240240
instructions.push_back(ConvertSBInstructionToDisassembledInstruction(
241-
dap.debugger, inst, resolve_symbols));
241+
dap.target, inst, resolve_symbols));
242242
}
243243

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DAP.h"
1010
#include "EventHelper.h"
1111
#include "JSONUtils.h"
12+
#include "LLDBUtils.h"
1213
#include "RequestHandler.h"
1314
#include "lldb/API/SBAddress.h"
1415
#include "lldb/API/SBDeclaration.h"
@@ -126,7 +127,7 @@ void LocationsRequestHandler::operator()(
126127

127128
lldb::addr_t raw_addr = variable.GetValueAsAddress();
128129
lldb::SBAddress addr = dap.target.ResolveLoadAddress(raw_addr);
129-
lldb::SBLineEntry line_entry = addr.GetLineEntry();
130+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(dap.target, addr);
130131

131132
if (!line_entry.IsValid()) {
132133
response["success"] = false;
@@ -135,7 +136,7 @@ void LocationsRequestHandler::operator()(
135136
return;
136137
}
137138

138-
body.try_emplace("source", CreateSource(addr, dap.debugger));
139+
body.try_emplace("source", CreateSource(line_entry.GetFileSpec()));
139140
if (int line = line_entry.GetLine())
140141
body.try_emplace("line", line);
141142
if (int column = line_entry.GetColumn())

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ static bool FillStackFrames(DAP &dap, lldb::SBThread &thread,
6969
break;
7070
}
7171

72-
stack_frames.emplace_back(
73-
CreateStackFrame(frame, frame_format, dap.debugger));
72+
stack_frames.emplace_back(CreateStackFrame(frame, frame_format));
7473
}
7574

7675
if (include_all && reached_end_of_stack) {

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "LLDBUtils.h"
1313
#include "lldb/API/SBAddress.h"
1414
#include "lldb/API/SBCompileUnit.h"
15+
#include "lldb/API/SBDebugger.h"
1516
#include "lldb/API/SBDeclaration.h"
1617
#include "lldb/API/SBEnvironment.h"
1718
#include "lldb/API/SBError.h"
@@ -571,14 +572,16 @@ protocol::Source CreateSource(const lldb::SBFileSpec &file) {
571572
return source;
572573
}
573574

574-
protocol::Source CreateSource(lldb::SBAddress address,
575-
lldb::SBDebugger &debugger) {
575+
protocol::Source CreateSource(lldb::SBAddress address, lldb::SBTarget &target) {
576+
lldb::SBDebugger debugger = target.GetDebugger();
576577
lldb::StopDisassemblyType stop_disassembly_display =
577578
GetStopDisassemblyDisplay(debugger);
578-
if (!ShouldDisplayAssemblySource(address, stop_disassembly_display))
579-
return CreateSource(address.GetLineEntry().GetFileSpec());
579+
if (!ShouldDisplayAssemblySource(address, stop_disassembly_display)) {
580+
lldb::SBLineEntry line_entry = GetLineEntryForAddress(target, address);
581+
return CreateSource(line_entry.GetFileSpec());
582+
}
580583

581-
return CreateAssemblySource(debugger.GetSelectedTarget(), address);
584+
return CreateAssemblySource(target, address);
582585
}
583586

584587
protocol::Source CreateSource(llvm::StringRef source_path) {
@@ -650,8 +653,8 @@ protocol::Source CreateSource(llvm::StringRef source_path) {
650653
// },
651654
// "required": [ "id", "name", "line", "column" ]
652655
// }
653-
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
654-
lldb::SBDebugger &debugger) {
656+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
657+
lldb::SBFormat &format) {
655658
llvm::json::Object object;
656659
int64_t frame_id = MakeDAPFrameID(frame);
657660
object.try_emplace("id", frame_id);
@@ -679,9 +682,10 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
679682

680683
EmplaceSafeString(object, "name", frame_name);
681684

682-
auto source = CreateSource(frame.GetPCAddress(), debugger);
685+
auto target = frame.GetThread().GetProcess().GetTarget();
686+
auto source = CreateSource(frame.GetPCAddress(), target);
683687
if (!source.IsAssemblySource()) {
684-
// This is a normal source with valid line entry.
688+
// This is a normal source with a valid line entry.
685689
auto line_entry = frame.GetLineEntry();
686690
object.try_emplace("line", line_entry.GetLine());
687691
auto column = line_entry.GetColumn();

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,13 @@ protocol::Source CreateSource(const lldb::SBFileSpec &file);
255255
/// \param[in] address
256256
/// The address to use when populating out the "Source" object.
257257
///
258-
/// \param[in] debugger
259-
/// The debugger, used to get relevant settings.
258+
/// \param[in] target
259+
/// The target that has the address.
260260
///
261261
/// \return
262262
/// A "Source" JSON object that follows the formal JSON
263263
/// definition outlined by Microsoft.
264-
protocol::Source CreateSource(lldb::SBAddress address,
265-
lldb::SBDebugger &debugger);
264+
protocol::Source CreateSource(lldb::SBAddress address, lldb::SBTarget &target);
266265

267266
/// Create a "Source" object for a given source path.
268267
///
@@ -292,15 +291,11 @@ protocol::Source CreateSource(llvm::StringRef source_path);
292291
/// The LLDB format to use when populating out the "StackFrame"
293292
/// object.
294293
///
295-
/// \param[in] debugger
296-
/// The LLDB debugger to use when populating out the "StackFrame"
297-
/// object.
298-
///
299294
/// \return
300295
/// A "StackFrame" JSON object with that follows the formal JSON
301296
/// definition outlined by Microsoft.
302-
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame, lldb::SBFormat &format,
303-
lldb::SBDebugger &debugger);
297+
llvm::json::Value CreateStackFrame(lldb::SBFrame &frame,
298+
lldb::SBFormat &format);
304299

305300
/// Create a "StackFrame" label object for a LLDB thread.
306301
///

lldb/tools/lldb-dap/LLDBUtils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,11 @@ std::string GetSBFileSpecPath(const lldb::SBFileSpec &file_spec) {
252252
return path;
253253
}
254254

255+
lldb::SBLineEntry GetLineEntryForAddress(lldb::SBTarget &target,
256+
lldb::SBAddress &address) {
257+
lldb::SBSymbolContext sc =
258+
address.GetSymbolContext(target, lldb::eSymbolContextLineEntry);
259+
return sc.GetLineEntry();
260+
}
261+
255262
} // namespace lldb_dap

lldb/tools/lldb-dap/LLDBUtils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "lldb/API/SBEnvironment.h"
1515
#include "lldb/API/SBError.h"
1616
#include "lldb/API/SBFileSpec.h"
17+
#include "lldb/API/SBLineEntry.h"
18+
#include "lldb/API/SBTarget.h"
1719
#include "llvm/ADT/ArrayRef.h"
1820
#include "llvm/ADT/StringRef.h"
1921
#include "llvm/Support/Error.h"
@@ -171,6 +173,18 @@ GetEnvironmentFromArguments(const llvm::json::Object &arguments);
171173
/// The file path as a string.
172174
std::string GetSBFileSpecPath(const lldb::SBFileSpec &file_spec);
173175

176+
/// Gets the line entry for a given address.
177+
/// \param[in] target
178+
/// The target that has the address.
179+
///
180+
/// \param[in] address
181+
/// The address for which to get the line entry.
182+
///
183+
/// \return
184+
/// The line entry for the given address.
185+
lldb::SBLineEntry GetLineEntryForAddress(lldb::SBTarget &target,
186+
lldb::SBAddress &address);
187+
174188
/// Helper for sending telemetry to lldb server, if client-telemetry is enabled.
175189
class TelemetryDispatcher {
176190
public:

0 commit comments

Comments
 (0)