Skip to content

Commit 6905394

Browse files
committed
[lldb/DWARF] Use DW_AT_call_pc to determine artificial frame address
lldb currently guesses the address to use when creating an artificial frame (i.e., a frame constructed by determining the sequence of (tail) calls which must have happened). Guessing the address creates problems -- use the actual address provided by the DW_AT_call_pc attribute instead. Depends on D76336. rdar://60307600 Differential Revision: https://reviews.llvm.org/D76337
1 parent f7052da commit 6905394

File tree

5 files changed

+95
-41
lines changed

5 files changed

+95
-41
lines changed

lldb/include/lldb/Symbol/Function.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,33 @@ class CallEdge {
284284
/// Like \ref GetReturnPCAddress, but returns an unresolved file address.
285285
lldb::addr_t GetUnresolvedReturnPCAddress() const { return return_pc; }
286286

287+
/// Get the load PC address of the call instruction (or LLDB_INVALID_ADDRESS).
288+
lldb::addr_t GetCallInstPC(Function &caller, Target &target) const;
289+
287290
/// Get the call site parameters available at this call edge.
288291
llvm::ArrayRef<CallSiteParameter> GetCallSiteParameters() const {
289292
return parameters;
290293
}
291294

292295
protected:
293-
CallEdge(lldb::addr_t return_pc, CallSiteParameterArray &&parameters)
294-
: return_pc(return_pc), parameters(std::move(parameters)) {}
296+
CallEdge(lldb::addr_t return_pc, lldb::addr_t call_inst_pc,
297+
CallSiteParameterArray &&parameters)
298+
: return_pc(return_pc), call_inst_pc(call_inst_pc),
299+
parameters(std::move(parameters)) {}
300+
301+
/// Helper that finds the load address of \p unresolved_pc, a file address
302+
/// which refers to an instruction within \p caller.
303+
static lldb::addr_t GetLoadAddress(lldb::addr_t unresolved_pc,
304+
Function &caller, Target &target);
295305

296306
/// An invalid address if this is a tail call. Otherwise, the return PC for
297307
/// the call. Note that this is a file address which must be resolved.
298308
lldb::addr_t return_pc;
299309

310+
/// The address of the call instruction. Usually an invalid address, unless
311+
/// this is a tail call.
312+
lldb::addr_t call_inst_pc;
313+
300314
CallSiteParameterArray parameters;
301315
};
302316

@@ -308,8 +322,8 @@ class DirectCallEdge : public CallEdge {
308322
/// Construct a call edge using a symbol name to identify the callee, and a
309323
/// return PC within the calling function to identify a specific call site.
310324
DirectCallEdge(const char *symbol_name, lldb::addr_t return_pc,
311-
CallSiteParameterArray &&parameters)
312-
: CallEdge(return_pc, std::move(parameters)) {
325+
lldb::addr_t call_inst_pc, CallSiteParameterArray &&parameters)
326+
: CallEdge(return_pc, call_inst_pc, std::move(parameters)) {
313327
lazy_callee.symbol_name = symbol_name;
314328
}
315329

@@ -339,8 +353,9 @@ class IndirectCallEdge : public CallEdge {
339353
/// Construct a call edge using a DWARFExpression to identify the callee, and
340354
/// a return PC within the calling function to identify a specific call site.
341355
IndirectCallEdge(DWARFExpression call_target, lldb::addr_t return_pc,
356+
lldb::addr_t call_inst_pc,
342357
CallSiteParameterArray &&parameters)
343-
: CallEdge(return_pc, std::move(parameters)),
358+
: CallEdge(return_pc, call_inst_pc, std::move(parameters)),
344359
call_target(std::move(call_target)) {}
345360

346361
Function *GetCallee(ModuleList &images, ExecutionContext &exe_ctx) override;

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3737,6 +3737,7 @@ SymbolFileDWARF::CollectCallEdges(ModuleSP module, DWARFDIE function_die) {
37373737
llvm::Optional<DWARFDIE> call_origin;
37383738
llvm::Optional<DWARFExpression> call_target;
37393739
addr_t return_pc = LLDB_INVALID_ADDRESS;
3740+
addr_t call_inst_pc = LLDB_INVALID_ADDRESS;
37403741

37413742
DWARFAttributes attributes;
37423743
const size_t num_attributes = child.GetAttributes(attributes);
@@ -3765,6 +3766,12 @@ SymbolFileDWARF::CollectCallEdges(ModuleSP module, DWARFDIE function_die) {
37653766
if (attr == DW_AT_call_return_pc)
37663767
return_pc = form_value.Address();
37673768

3769+
// Extract DW_AT_call_pc (the PC at the call/branch instruction). It
3770+
// should only ever be unavailable for non-tail calls, in which case use
3771+
// LLDB_INVALID_ADDRESS.
3772+
if (attr == DW_AT_call_pc)
3773+
call_inst_pc = form_value.Address();
3774+
37683775
// Extract DW_AT_call_target (the location of the address of the indirect
37693776
// call).
37703777
if (attr == DW_AT_call_target) {
@@ -3787,21 +3794,25 @@ SymbolFileDWARF::CollectCallEdges(ModuleSP module, DWARFDIE function_die) {
37873794
continue;
37883795
}
37893796

3790-
// Adjust the return PC. It needs to be fixed up if the main executable
3797+
// Adjust any PC forms. It needs to be fixed up if the main executable
37913798
// contains a debug map (i.e. pointers to object files), because we need a
37923799
// file address relative to the executable's text section.
37933800
return_pc = FixupAddress(return_pc);
3801+
call_inst_pc = FixupAddress(call_inst_pc);
37943802

37953803
// Extract call site parameters.
37963804
CallSiteParameterArray parameters =
37973805
CollectCallSiteParameters(module, child);
37983806

37993807
std::unique_ptr<CallEdge> edge;
38003808
if (call_origin) {
3801-
LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})",
3802-
call_origin->GetPubname(), return_pc);
3809+
LLDB_LOG(log,
3810+
"CollectCallEdges: Found call origin: {0} (retn-PC: {1:x}) "
3811+
"(call-PC: {2:x})",
3812+
call_origin->GetPubname(), return_pc, call_inst_pc);
38033813
edge = std::make_unique<DirectCallEdge>(call_origin->GetMangledName(),
3804-
return_pc, std::move(parameters));
3814+
return_pc, call_inst_pc,
3815+
std::move(parameters));
38053816
} else {
38063817
if (log) {
38073818
StreamString call_target_desc;
@@ -3810,8 +3821,8 @@ SymbolFileDWARF::CollectCallEdges(ModuleSP module, DWARFDIE function_die) {
38103821
LLDB_LOG(log, "CollectCallEdges: Found indirect call target: {0}",
38113822
call_target_desc.GetString());
38123823
}
3813-
edge = std::make_unique<IndirectCallEdge>(*call_target, return_pc,
3814-
std::move(parameters));
3824+
edge = std::make_unique<IndirectCallEdge>(
3825+
*call_target, return_pc, call_inst_pc, std::move(parameters));
38153826
}
38163827

38173828
if (log && parameters.size()) {

lldb/source/Symbol/Function.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,36 @@ size_t InlineFunctionInfo::MemorySize() const {
120120
/// @name Call site related structures
121121
/// @{
122122

123-
lldb::addr_t CallEdge::GetReturnPCAddress(Function &caller,
124-
Target &target) const {
123+
lldb::addr_t CallEdge::GetLoadAddress(lldb::addr_t unresolved_pc,
124+
Function &caller, Target &target) {
125125
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
126126

127127
const Address &caller_start_addr = caller.GetAddressRange().GetBaseAddress();
128128

129129
ModuleSP caller_module_sp = caller_start_addr.GetModule();
130130
if (!caller_module_sp) {
131-
LLDB_LOG(log, "GetReturnPCAddress: cannot get Module for caller");
131+
LLDB_LOG(log, "GetLoadAddress: cannot get Module for caller");
132132
return LLDB_INVALID_ADDRESS;
133133
}
134134

135135
SectionList *section_list = caller_module_sp->GetSectionList();
136136
if (!section_list) {
137-
LLDB_LOG(log, "GetReturnPCAddress: cannot get SectionList for Module");
137+
LLDB_LOG(log, "GetLoadAddress: cannot get SectionList for Module");
138138
return LLDB_INVALID_ADDRESS;
139139
}
140140

141-
Address return_pc_addr = Address(return_pc, section_list);
142-
lldb::addr_t ret_addr = return_pc_addr.GetLoadAddress(&target);
143-
return ret_addr;
141+
Address the_addr = Address(unresolved_pc, section_list);
142+
lldb::addr_t load_addr = the_addr.GetLoadAddress(&target);
143+
return load_addr;
144+
}
145+
146+
lldb::addr_t CallEdge::GetReturnPCAddress(Function &caller,
147+
Target &target) const {
148+
return GetLoadAddress(return_pc, caller, target);
149+
}
150+
151+
lldb::addr_t CallEdge::GetCallInstPC(Function &caller, Target &target) const {
152+
return GetLoadAddress(call_inst_pc, caller, target);
144153
}
145154

146155
void DirectCallEdge::ParseSymbolFileAndResolve(ModuleList &images) {

lldb/source/Target/StackFrameList.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,17 @@ void StackFrameList::GetOnlyConcreteFramesUpTo(uint32_t end_idx,
236236
m_frames.resize(num_frames);
237237
}
238238

239+
/// A sequence of calls that comprise some portion of a backtrace. Each frame
240+
/// is represented as a pair of a callee (Function *) and an address within the
241+
/// callee.
242+
using CallSequence = std::vector<std::pair<Function *, addr_t>>;
243+
239244
/// Find the unique path through the call graph from \p begin (with return PC
240245
/// \p return_pc) to \p end. On success this path is stored into \p path, and
241246
/// on failure \p path is unchanged.
242247
static void FindInterveningFrames(Function &begin, Function &end,
243248
ExecutionContext &exe_ctx, Target &target,
244-
addr_t return_pc,
245-
std::vector<Function *> &path,
249+
addr_t return_pc, CallSequence &path,
246250
ModuleList &images, Log *log) {
247251
LLDB_LOG(log, "Finding frames between {0} and {1}, retn-pc={2:x}",
248252
begin.GetDisplayName(), end.GetDisplayName(), return_pc);
@@ -275,24 +279,27 @@ static void FindInterveningFrames(Function &begin, Function &end,
275279
// Fully explore the set of functions reachable from the first edge via tail
276280
// calls in order to detect ambiguous executions.
277281
struct DFS {
278-
std::vector<Function *> active_path = {};
279-
std::vector<Function *> solution_path = {};
282+
CallSequence active_path = {};
283+
CallSequence solution_path = {};
280284
llvm::SmallPtrSet<Function *, 2> visited_nodes = {};
281285
bool ambiguous = false;
282286
Function *end;
283287
ModuleList &images;
288+
Target &target;
284289
ExecutionContext &context;
285290

286-
DFS(Function *end, ModuleList &images, ExecutionContext &context)
287-
: end(end), images(images), context(context) {}
291+
DFS(Function *end, ModuleList &images, Target &target,
292+
ExecutionContext &context)
293+
: end(end), images(images), target(target), context(context) {}
288294

289-
void search(Function &first_callee, std::vector<Function *> &path) {
290-
dfs(first_callee);
295+
void search(CallEdge &first_edge, Function &first_callee,
296+
CallSequence &path) {
297+
dfs(first_edge, first_callee);
291298
if (!ambiguous)
292299
path = std::move(solution_path);
293300
}
294301

295-
void dfs(Function &callee) {
302+
void dfs(CallEdge &current_edge, Function &callee) {
296303
// Found a path to the target function.
297304
if (&callee == end) {
298305
if (solution_path.empty())
@@ -312,21 +319,24 @@ static void FindInterveningFrames(Function &begin, Function &end,
312319
}
313320

314321
// Search the calls made from this callee.
315-
active_path.push_back(&callee);
322+
active_path.emplace_back(&callee, LLDB_INVALID_ADDRESS);
316323
for (const auto &edge : callee.GetTailCallingEdges()) {
317324
Function *next_callee = edge->GetCallee(images, context);
318325
if (!next_callee)
319326
continue;
320327

321-
dfs(*next_callee);
328+
addr_t tail_call_pc = edge->GetCallInstPC(callee, target);
329+
active_path.back().second = tail_call_pc;
330+
331+
dfs(*edge, *next_callee);
322332
if (ambiguous)
323333
return;
324334
}
325335
active_path.pop_back();
326336
}
327337
};
328338

329-
DFS(&end, images, exe_ctx).search(*first_callee, path);
339+
DFS(&end, images, target, exe_ctx).search(*first_edge, *first_callee, path);
330340
}
331341

332342
/// Given that \p next_frame will be appended to the frame list, synthesize
@@ -379,7 +389,7 @@ void StackFrameList::SynthesizeTailCallFrames(StackFrame &next_frame) {
379389

380390
// Try to find the unique sequence of (tail) calls which led from next_frame
381391
// to prev_frame.
382-
std::vector<Function *> path;
392+
CallSequence path;
383393
addr_t return_pc = next_reg_ctx_sp->GetPC();
384394
Target &target = *target_sp.get();
385395
ModuleList &images = next_frame.CalculateTarget()->GetImages();
@@ -389,13 +399,13 @@ void StackFrameList::SynthesizeTailCallFrames(StackFrame &next_frame) {
389399
path, images, log);
390400

391401
// Push synthetic tail call frames.
392-
for (Function *callee : llvm::reverse(path)) {
402+
for (auto calleeInfo : llvm::reverse(path)) {
403+
Function *callee = calleeInfo.first;
393404
uint32_t frame_idx = m_frames.size();
394405
uint32_t concrete_frame_idx = next_frame.GetConcreteFrameIndex();
395406
addr_t cfa = LLDB_INVALID_ADDRESS;
396407
bool cfa_is_valid = false;
397-
addr_t pc =
398-
callee->GetAddressRange().GetBaseAddress().GetLoadAddress(&target);
408+
addr_t pc = calleeInfo.second;
399409
constexpr bool behaves_like_zeroth_frame = false;
400410
SymbolContext sc;
401411
callee->CalculateSymbolContext(&sc);
@@ -404,7 +414,7 @@ void StackFrameList::SynthesizeTailCallFrames(StackFrame &next_frame) {
404414
cfa_is_valid, pc, StackFrame::Kind::Artificial,
405415
behaves_like_zeroth_frame, &sc);
406416
m_frames.push_back(synth_frame);
407-
LLDB_LOG(log, "Pushed frame {0}", callee->GetDisplayName());
417+
LLDB_LOG(log, "Pushed frame {0} at {1:x}", callee->GetDisplayName(), pc);
408418
}
409419

410420
// If any frames were created, adjust next_frame's index.

lldb/test/API/functionalities/tail_call_frames/unambiguous_sequence/main.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@ volatile int x;
33
void __attribute__((noinline)) sink() {
44
x++; //% self.filecheck("bt", "main.cpp", "-implicit-check-not=artificial")
55
// CHECK: frame #0: 0x{{[0-9a-f]+}} a.out`sink() at main.cpp:[[@LINE-1]]:4 [opt]
6-
// CHECK-NEXT: frame #1: 0x{{[0-9a-f]+}} a.out`func3{{.*}} [opt] [artificial]
7-
// CHECK-NEXT: frame #2: 0x{{[0-9a-f]+}} a.out`func2{{.*}} [opt]
8-
// CHECK-NEXT: frame #3: 0x{{[0-9a-f]+}} a.out`func1{{.*}} [opt] [artificial]
6+
// CHECK-NEXT: frame #1: 0x{{[0-9a-f]+}} a.out`func3() at main.cpp:14:3 [opt] [artificial]
7+
// CHECK-NEXT: frame #2: 0x{{[0-9a-f]+}} a.out`func2() {{.*}} [opt]
8+
// CHECK-NEXT: frame #3: 0x{{[0-9a-f]+}} a.out`func1() at main.cpp:23:3 [opt] [artificial]
99
// CHECK-NEXT: frame #4: 0x{{[0-9a-f]+}} a.out`main{{.*}} [opt]
1010
}
1111

12-
void __attribute__((noinline)) func3() { sink(); /* tail */ }
12+
void __attribute__((noinline)) func3() {
13+
x++;
14+
sink(); /* tail */
15+
}
1316

14-
void __attribute__((disable_tail_calls, noinline)) func2() { func3(); /* regular */ }
17+
void __attribute__((disable_tail_calls, noinline)) func2() {
18+
func3(); /* regular */
19+
}
1520

16-
void __attribute__((noinline)) func1() { func2(); /* tail */ }
21+
void __attribute__((noinline)) func1() {
22+
x++;
23+
func2(); /* tail */
24+
}
1725

1826
int __attribute__((disable_tail_calls)) main() {
27+
// DEBUG: self.runCmd("log enable lldb step -f /tmp/lldbstep.log")
1928
func1(); /* regular */
2029
return 0;
2130
}

0 commit comments

Comments
 (0)