LLDB mainline
StackFrameList.cpp
Go to the documentation of this file.
1//===-- StackFrameList.cpp ------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
12#include "lldb/Core/Debugger.h"
15#include "lldb/Symbol/Block.h"
17#include "lldb/Symbol/Symbol.h"
18#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Target/Thread.h"
25#include "lldb/Target/Unwind.h"
27#include "lldb/Utility/Log.h"
28#include "llvm/ADT/SmallPtrSet.h"
29
30#include <memory>
31
32//#define DEBUG_STACK_FRAMES 1
33
34using namespace lldb;
35using namespace lldb_private;
36
37// StackFrameList constructor
39 const lldb::StackFrameListSP &prev_frames_sp,
40 bool show_inline_frames)
41 : m_thread(thread), m_prev_frames_sp(prev_frames_sp), m_mutex(), m_frames(),
42 m_selected_frame_idx(), m_concrete_frames_fetched(0),
43 m_current_inlined_depth(UINT32_MAX),
44 m_current_inlined_pc(LLDB_INVALID_ADDRESS),
45 m_show_inlined_frames(show_inline_frames) {
46 if (prev_frames_sp) {
47 m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth;
48 m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc;
49 }
50}
51
53 // Call clear since this takes a lock and clears the stack frame list in case
54 // another thread is currently using this stack frame list
55 Clear();
56}
57
59 uint32_t cur_inlined_depth = GetCurrentInlinedDepth();
60 if (cur_inlined_depth == UINT32_MAX) {
62 }
63}
64
67 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
68 if (cur_pc != m_current_inlined_pc) {
71 Log *log = GetLog(LLDBLog::Step);
72 if (log && log->GetVerbose())
74 log,
75 "GetCurrentInlinedDepth: invalidating current inlined depth.\n");
76 }
78 } else {
79 return UINT32_MAX;
80 }
81}
82
85 return;
86
87 std::lock_guard<std::recursive_mutex> guard(m_mutex);
88
90 if (m_frames.empty())
91 return;
92 if (!m_frames[0]->IsInlined()) {
95 Log *log = GetLog(LLDBLog::Step);
96 if (log && log->GetVerbose())
98 log,
99 "ResetCurrentInlinedDepth: Invalidating current inlined depth.\n");
100 return;
101 }
102
103 // We only need to do something special about inlined blocks when we are
104 // at the beginning of an inlined function:
105 // FIXME: We probably also have to do something special if the PC is at
106 // the END of an inlined function, which coincides with the end of either
107 // its containing function or another inlined function.
108
109 Block *block_ptr = m_frames[0]->GetFrameBlock();
110 if (!block_ptr)
111 return;
112
113 Address pc_as_address;
114 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
115 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget()));
116 AddressRange containing_range;
117 if (!block_ptr->GetRangeContainingAddress(pc_as_address, containing_range) ||
118 pc_as_address != containing_range.GetBaseAddress())
119 return;
120
121 // If we got here because of a breakpoint hit, then set the inlined depth
122 // depending on where the breakpoint was set. If we got here because of a
123 // crash, then set the inlined depth to the deepest most block. Otherwise,
124 // we stopped here naturally as the result of a step, so set ourselves in the
125 // containing frame of the whole set of nested inlines, so the user can then
126 // "virtually" step into the frames one by one, or next over the whole mess.
127 // Note: We don't have to handle being somewhere in the middle of the stack
128 // here, since ResetCurrentInlinedDepth doesn't get called if there is a
129 // valid inlined depth set.
130 StopInfoSP stop_info_sp = m_thread.GetStopInfo();
131 if (!stop_info_sp)
132 return;
133 switch (stop_info_sp->GetStopReason()) {
136 case eStopReasonExec:
137 case eStopReasonFork:
138 case eStopReasonVFork:
141 // In all these cases we want to stop in the deepest frame.
142 m_current_inlined_pc = curr_pc;
144 break;
146 // FIXME: Figure out what this break point is doing, and set the inline
147 // depth appropriately. Be careful to take into account breakpoints that
148 // implement step over prologue, since that should do the default
149 // calculation. For now, if the breakpoints corresponding to this hit are
150 // all internal, I set the stop location to the top of the inlined stack,
151 // since that will make things like stepping over prologues work right.
152 // But if there are any non-internal breakpoints I do to the bottom of the
153 // stack, since that was the old behavior.
154 uint32_t bp_site_id = stop_info_sp->GetValue();
155 BreakpointSiteSP bp_site_sp(
156 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id));
157 bool all_internal = true;
158 if (bp_site_sp) {
159 uint32_t num_owners = bp_site_sp->GetNumberOfConstituents();
160 for (uint32_t i = 0; i < num_owners; i++) {
161 Breakpoint &bp_ref =
162 bp_site_sp->GetConstituentAtIndex(i)->GetBreakpoint();
163 if (!bp_ref.IsInternal()) {
164 all_internal = false;
165 }
166 }
167 }
168 if (!all_internal) {
169 m_current_inlined_pc = curr_pc;
171 break;
172 }
173 }
174 [[fallthrough]];
175 default: {
176 // Otherwise, we should set ourselves at the container of the inlining, so
177 // that the user can descend into them. So first we check whether we have
178 // more than one inlined block sharing this PC:
179 int num_inlined_functions = 0;
180
181 for (Block *container_ptr = block_ptr->GetInlinedParent();
182 container_ptr != nullptr;
183 container_ptr = container_ptr->GetInlinedParent()) {
184 if (!container_ptr->GetRangeContainingAddress(pc_as_address,
185 containing_range))
186 break;
187 if (pc_as_address != containing_range.GetBaseAddress())
188 break;
189
190 num_inlined_functions++;
191 }
192 m_current_inlined_pc = curr_pc;
193 m_current_inlined_depth = num_inlined_functions + 1;
194 Log *log = GetLog(LLDBLog::Step);
195 if (log && log->GetVerbose())
196 LLDB_LOGF(log,
197 "ResetCurrentInlinedDepth: setting inlined "
198 "depth: %d 0x%" PRIx64 ".\n",
199 m_current_inlined_depth, curr_pc);
200
201 break;
202 }
203 }
204}
205
208 uint32_t current_inlined_depth = GetCurrentInlinedDepth();
209 if (current_inlined_depth != UINT32_MAX) {
210 if (current_inlined_depth > 0) {
212 return true;
213 }
214 }
215 }
216 return false;
217}
218
220 m_current_inlined_depth = new_depth;
221 if (new_depth == UINT32_MAX)
223 else
225}
226
228 Unwind &unwinder) {
229 assert(m_thread.IsValid() && "Expected valid thread");
230 assert(m_frames.size() <= end_idx && "Expected there to be frames to fill");
231
232 if (end_idx < m_concrete_frames_fetched)
233 return;
234
235 uint32_t num_frames = unwinder.GetFramesUpTo(end_idx);
236 if (num_frames <= end_idx + 1) {
237 // Done unwinding.
239 }
240
241 // Don't create the frames eagerly. Defer this work to GetFrameAtIndex,
242 // which can lazily query the unwinder to create frames.
243 m_frames.resize(num_frames);
244}
245
246/// A sequence of calls that comprise some portion of a backtrace. Each frame
247/// is represented as a pair of a callee (Function *) and an address within the
248/// callee.
251 CallEdge::AddrType address_type = CallEdge::AddrType::Call;
253};
254using CallSequence = std::vector<CallDescriptor>;
255
256/// Find the unique path through the call graph from \p begin (with return PC
257/// \p return_pc) to \p end. On success this path is stored into \p path, and
258/// on failure \p path is unchanged.
259static void FindInterveningFrames(Function &begin, Function &end,
260 ExecutionContext &exe_ctx, Target &target,
261 addr_t return_pc, CallSequence &path,
262 ModuleList &images, Log *log) {
263 LLDB_LOG(log, "Finding frames between {0} and {1}, retn-pc={2:x}",
264 begin.GetDisplayName(), end.GetDisplayName(), return_pc);
265
266 // Find a non-tail calling edge with the correct return PC.
267 if (log)
268 for (const auto &edge : begin.GetCallEdges())
269 LLDB_LOG(log, "FindInterveningFrames: found call with retn-PC = {0:x}",
270 edge->GetReturnPCAddress(begin, target));
271 CallEdge *first_edge = begin.GetCallEdgeForReturnAddress(return_pc, target);
272 if (!first_edge) {
273 LLDB_LOG(log, "No call edge outgoing from {0} with retn-PC == {1:x}",
274 begin.GetDisplayName(), return_pc);
275 return;
276 }
277
278 // The first callee may not be resolved, or there may be nothing to fill in.
279 Function *first_callee = first_edge->GetCallee(images, exe_ctx);
280 if (!first_callee) {
281 LLDB_LOG(log, "Could not resolve callee");
282 return;
283 }
284 if (first_callee == &end) {
285 LLDB_LOG(log, "Not searching further, first callee is {0} (retn-PC: {1:x})",
286 end.GetDisplayName(), return_pc);
287 return;
288 }
289
290 // Run DFS on the tail-calling edges out of the first callee to find \p end.
291 // Fully explore the set of functions reachable from the first edge via tail
292 // calls in order to detect ambiguous executions.
293 struct DFS {
294 CallSequence active_path = {};
295 CallSequence solution_path = {};
296 llvm::SmallPtrSet<Function *, 2> visited_nodes = {};
297 bool ambiguous = false;
298 Function *end;
299 ModuleList &images;
300 Target &target;
301 ExecutionContext &context;
302
303 DFS(Function *end, ModuleList &images, Target &target,
304 ExecutionContext &context)
305 : end(end), images(images), target(target), context(context) {}
306
307 void search(CallEdge &first_edge, Function &first_callee,
308 CallSequence &path) {
309 dfs(first_edge, first_callee);
310 if (!ambiguous)
311 path = std::move(solution_path);
312 }
313
314 void dfs(CallEdge &current_edge, Function &callee) {
315 // Found a path to the target function.
316 if (&callee == end) {
317 if (solution_path.empty())
318 solution_path = active_path;
319 else
320 ambiguous = true;
321 return;
322 }
323
324 // Terminate the search if tail recursion is found, or more generally if
325 // there's more than one way to reach a target. This errs on the side of
326 // caution: it conservatively stops searching when some solutions are
327 // still possible to save time in the average case.
328 if (!visited_nodes.insert(&callee).second) {
329 ambiguous = true;
330 return;
331 }
332
333 // Search the calls made from this callee.
334 active_path.push_back(CallDescriptor{&callee});
335 for (const auto &edge : callee.GetTailCallingEdges()) {
336 Function *next_callee = edge->GetCallee(images, context);
337 if (!next_callee)
338 continue;
339
340 std::tie(active_path.back().address_type, active_path.back().address) =
341 edge->GetCallerAddress(callee, target);
342
343 dfs(*edge, *next_callee);
344 if (ambiguous)
345 return;
346 }
347 active_path.pop_back();
348 }
349 };
350
351 DFS(&end, images, target, exe_ctx).search(*first_edge, *first_callee, path);
352}
353
354/// Given that \p next_frame will be appended to the frame list, synthesize
355/// tail call frames between the current end of the list and \p next_frame.
356/// If any frames are added, adjust the frame index of \p next_frame.
357///
358/// --------------
359/// | ... | <- Completed frames.
360/// --------------
361/// | prev_frame |
362/// --------------
363/// | ... | <- Artificial frames inserted here.
364/// --------------
365/// | next_frame |
366/// --------------
367/// | ... | <- Not-yet-visited frames.
368/// --------------
370 // Cannot synthesize tail call frames when the stack is empty (there is no
371 // "previous" frame).
372 if (m_frames.empty())
373 return;
374
375 TargetSP target_sp = next_frame.CalculateTarget();
376 if (!target_sp)
377 return;
378
379 lldb::RegisterContextSP next_reg_ctx_sp = next_frame.GetRegisterContext();
380 if (!next_reg_ctx_sp)
381 return;
382
383 Log *log = GetLog(LLDBLog::Step);
384
385 StackFrame &prev_frame = *m_frames.back().get();
386
387 // Find the functions prev_frame and next_frame are stopped in. The function
388 // objects are needed to search the lazy call graph for intervening frames.
389 Function *prev_func =
390 prev_frame.GetSymbolContext(eSymbolContextFunction).function;
391 if (!prev_func) {
392 LLDB_LOG(log, "SynthesizeTailCallFrames: can't find previous function");
393 return;
394 }
395 Function *next_func =
396 next_frame.GetSymbolContext(eSymbolContextFunction).function;
397 if (!next_func) {
398 LLDB_LOG(log, "SynthesizeTailCallFrames: can't find next function");
399 return;
400 }
401
402 // Try to find the unique sequence of (tail) calls which led from next_frame
403 // to prev_frame.
404 CallSequence path;
405 addr_t return_pc = next_reg_ctx_sp->GetPC();
406 Target &target = *target_sp.get();
407 ModuleList &images = next_frame.CalculateTarget()->GetImages();
408 ExecutionContext exe_ctx(target_sp, /*get_process=*/true);
409 exe_ctx.SetFramePtr(&next_frame);
410 FindInterveningFrames(*next_func, *prev_func, exe_ctx, target, return_pc,
411 path, images, log);
412
413 // Push synthetic tail call frames.
414 for (auto calleeInfo : llvm::reverse(path)) {
415 Function *callee = calleeInfo.func;
416 uint32_t frame_idx = m_frames.size();
417 uint32_t concrete_frame_idx = next_frame.GetConcreteFrameIndex();
419 bool cfa_is_valid = false;
420 addr_t pc = calleeInfo.address;
421 // If the callee address refers to the call instruction, we do not want to
422 // subtract 1 from this value.
423 const bool behaves_like_zeroth_frame =
424 calleeInfo.address_type == CallEdge::AddrType::Call;
425 SymbolContext sc;
426 callee->CalculateSymbolContext(&sc);
427 auto synth_frame = std::make_shared<StackFrame>(
428 m_thread.shared_from_this(), frame_idx, concrete_frame_idx, cfa,
429 cfa_is_valid, pc, StackFrame::Kind::Artificial,
430 behaves_like_zeroth_frame, &sc);
431 m_frames.push_back(synth_frame);
432 LLDB_LOG(log, "Pushed frame {0} at {1:x}", callee->GetDisplayName(), pc);
433 }
434
435 // If any frames were created, adjust next_frame's index.
436 if (!path.empty())
437 next_frame.SetFrameIndex(m_frames.size());
438}
439
440bool StackFrameList::GetFramesUpTo(uint32_t end_idx,
441 InterruptionControl allow_interrupt) {
442 // Do not fetch frames for an invalid thread.
443 bool was_interrupted = false;
444 if (!m_thread.IsValid())
445 return false;
446
447 // We've already gotten more frames than asked for, or we've already finished
448 // unwinding, return.
449 if (m_frames.size() > end_idx || GetAllFramesFetched())
450 return false;
451
452 Unwind &unwinder = m_thread.GetUnwinder();
453
455 GetOnlyConcreteFramesUpTo(end_idx, unwinder);
456 return false;
457 }
458
459#if defined(DEBUG_STACK_FRAMES)
460 StreamFile s(stdout, false);
461#endif
462 // If we are hiding some frames from the outside world, we need to add
463 // those onto the total count of frames to fetch. However, we don't need
464 // to do that if end_idx is 0 since in that case we always get the first
465 // concrete frame and all the inlined frames below it... And of course, if
466 // end_idx is UINT32_MAX that means get all, so just do that...
467
468 uint32_t inlined_depth = 0;
469 if (end_idx > 0 && end_idx != UINT32_MAX) {
470 inlined_depth = GetCurrentInlinedDepth();
471 if (inlined_depth != UINT32_MAX) {
472 if (end_idx > 0)
473 end_idx += inlined_depth;
474 }
475 }
476
477 StackFrameSP unwind_frame_sp;
478 Debugger &dbg = m_thread.GetProcess()->GetTarget().GetDebugger();
479 do {
480 uint32_t idx = m_concrete_frames_fetched++;
483 bool behaves_like_zeroth_frame = (idx == 0);
484 if (idx == 0) {
485 // We might have already created frame zero, only create it if we need
486 // to.
487 if (m_frames.empty()) {
489
490 if (reg_ctx_sp) {
491 const bool success = unwinder.GetFrameInfoAtIndex(
492 idx, cfa, pc, behaves_like_zeroth_frame);
493 // There shouldn't be any way not to get the frame info for frame
494 // 0. But if the unwinder can't make one, lets make one by hand
495 // with the SP as the CFA and see if that gets any further.
496 if (!success) {
497 cfa = reg_ctx_sp->GetSP();
498 pc = reg_ctx_sp->GetPC();
499 }
500
501 unwind_frame_sp = std::make_shared<StackFrame>(
502 m_thread.shared_from_this(), m_frames.size(), idx, reg_ctx_sp,
503 cfa, pc, behaves_like_zeroth_frame, nullptr);
504 m_frames.push_back(unwind_frame_sp);
505 }
506 } else {
507 unwind_frame_sp = m_frames.front();
508 cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
509 }
510 } else {
511 // Check for interruption when building the frames.
512 // Do the check in idx > 0 so that we'll always create a 0th frame.
513 if (allow_interrupt
514 && INTERRUPT_REQUESTED(dbg, "Interrupted having fetched {0} frames",
515 m_frames.size())) {
516 was_interrupted = true;
517 break;
518 }
519
520 const bool success =
521 unwinder.GetFrameInfoAtIndex(idx, cfa, pc, behaves_like_zeroth_frame);
522 if (!success) {
523 // We've gotten to the end of the stack.
525 break;
526 }
527 const bool cfa_is_valid = true;
528 unwind_frame_sp = std::make_shared<StackFrame>(
529 m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid,
530 pc, StackFrame::Kind::Regular, behaves_like_zeroth_frame, nullptr);
531
532 // Create synthetic tail call frames between the previous frame and the
533 // newly-found frame. The new frame's index may change after this call,
534 // although its concrete index will stay the same.
535 SynthesizeTailCallFrames(*unwind_frame_sp.get());
536
537 m_frames.push_back(unwind_frame_sp);
538 }
539
540 assert(unwind_frame_sp);
541 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext(
542 eSymbolContextBlock | eSymbolContextFunction);
543 Block *unwind_block = unwind_sc.block;
544 TargetSP target_sp = m_thread.CalculateTarget();
545 if (unwind_block) {
546 Address curr_frame_address(
547 unwind_frame_sp->GetFrameCodeAddressForSymbolication());
548
549 SymbolContext next_frame_sc;
550 Address next_frame_address;
551
552 while (unwind_sc.GetParentOfInlinedScope(
553 curr_frame_address, next_frame_sc, next_frame_address)) {
554 next_frame_sc.line_entry.ApplyFileMappings(target_sp);
555 behaves_like_zeroth_frame = false;
556 StackFrameSP frame_sp(new StackFrame(
557 m_thread.shared_from_this(), m_frames.size(), idx,
558 unwind_frame_sp->GetRegisterContextSP(), cfa, next_frame_address,
559 behaves_like_zeroth_frame, &next_frame_sc));
560
561 m_frames.push_back(frame_sp);
562 unwind_sc = next_frame_sc;
563 curr_frame_address = next_frame_address;
564 }
565 }
566 } while (m_frames.size() - 1 < end_idx);
567
568 // Don't try to merge till you've calculated all the frames in this stack.
570 StackFrameList *prev_frames = m_prev_frames_sp.get();
571 StackFrameList *curr_frames = this;
572
573#if defined(DEBUG_STACK_FRAMES)
574 s.PutCString("\nprev_frames:\n");
575 prev_frames->Dump(&s);
576 s.PutCString("\ncurr_frames:\n");
577 curr_frames->Dump(&s);
578 s.EOL();
579#endif
580 size_t curr_frame_num, prev_frame_num;
581
582 for (curr_frame_num = curr_frames->m_frames.size(),
583 prev_frame_num = prev_frames->m_frames.size();
584 curr_frame_num > 0 && prev_frame_num > 0;
585 --curr_frame_num, --prev_frame_num) {
586 const size_t curr_frame_idx = curr_frame_num - 1;
587 const size_t prev_frame_idx = prev_frame_num - 1;
588 StackFrameSP curr_frame_sp(curr_frames->m_frames[curr_frame_idx]);
589 StackFrameSP prev_frame_sp(prev_frames->m_frames[prev_frame_idx]);
590
591#if defined(DEBUG_STACK_FRAMES)
592 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
593 if (curr_frame_sp)
594 curr_frame_sp->Dump(&s, true, false);
595 else
596 s.PutCString("NULL");
597 s.Printf("\nPrev frame #%u ", prev_frame_idx);
598 if (prev_frame_sp)
599 prev_frame_sp->Dump(&s, true, false);
600 else
601 s.PutCString("NULL");
602#endif
603
604 StackFrame *curr_frame = curr_frame_sp.get();
605 StackFrame *prev_frame = prev_frame_sp.get();
606
607 if (curr_frame == nullptr || prev_frame == nullptr)
608 break;
609
610 // Check the stack ID to make sure they are equal.
611 if (curr_frame->GetStackID() != prev_frame->GetStackID())
612 break;
613
614 prev_frame->UpdatePreviousFrameFromCurrentFrame(*curr_frame);
615 // Now copy the fixed up previous frame into the current frames so the
616 // pointer doesn't change.
617 m_frames[curr_frame_idx] = prev_frame_sp;
618
619#if defined(DEBUG_STACK_FRAMES)
620 s.Printf("\n Copying previous frame to current frame");
621#endif
622 }
623 // We are done with the old stack frame list, we can release it now.
624 m_prev_frames_sp.reset();
625 }
626
627#if defined(DEBUG_STACK_FRAMES)
628 s.PutCString("\n\nNew frames:\n");
629 Dump(&s);
630 s.EOL();
631#endif
632 // Don't report interrupted if we happen to have gotten all the frames:
633 if (!GetAllFramesFetched())
634 return was_interrupted;
635 return false;
636}
637
638uint32_t StackFrameList::GetNumFrames(bool can_create) {
639 std::lock_guard<std::recursive_mutex> guard(m_mutex);
640
641 if (can_create) {
642 // Don't allow interrupt or we might not return the correct count
644 }
645 return GetVisibleStackFrameIndex(m_frames.size());
646}
647
649 if (s == nullptr)
650 return;
651
652 std::lock_guard<std::recursive_mutex> guard(m_mutex);
653
654 const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
655 for (pos = begin; pos != end; ++pos) {
656 StackFrame *frame = (*pos).get();
657 s->Printf("%p: ", static_cast<void *>(frame));
658 if (frame) {
659 frame->GetStackID().Dump(s);
660 frame->DumpUsingSettingsFormat(s);
661 } else
662 s->Printf("frame #%u", (uint32_t)std::distance(begin, pos));
663 s->EOL();
664 }
665 s->EOL();
666}
667
669 StackFrameSP frame_sp;
670 std::lock_guard<std::recursive_mutex> guard(m_mutex);
671 uint32_t original_idx = idx;
672
673 uint32_t inlined_depth = GetCurrentInlinedDepth();
674 if (inlined_depth != UINT32_MAX)
675 idx += inlined_depth;
676
677 if (idx < m_frames.size())
678 frame_sp = m_frames[idx];
679
680 if (frame_sp)
681 return frame_sp;
682
683 // GetFramesUpTo will fill m_frames with as many frames as you asked for, if
684 // there are that many. If there weren't then you asked for too many frames.
685 // GetFramesUpTo returns true if interrupted:
686 if (GetFramesUpTo(idx)) {
687 Log *log = GetLog(LLDBLog::Thread);
688 LLDB_LOG(log, "GetFrameAtIndex was interrupted");
689 return {};
690 }
691
692 if (idx < m_frames.size()) {
694 // When inline frames are enabled we actually create all the frames in
695 // GetFramesUpTo.
696 frame_sp = m_frames[idx];
697 } else {
698 addr_t pc, cfa;
699 bool behaves_like_zeroth_frame = (idx == 0);
701 idx, cfa, pc, behaves_like_zeroth_frame)) {
702 const bool cfa_is_valid = true;
703 frame_sp = std::make_shared<StackFrame>(
704 m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc,
705 StackFrame::Kind::Regular, behaves_like_zeroth_frame, nullptr);
706
707 Function *function =
708 frame_sp->GetSymbolContext(eSymbolContextFunction).function;
709 if (function) {
710 // When we aren't showing inline functions we always use the top
711 // most function block as the scope.
712 frame_sp->SetSymbolContextScope(&function->GetBlock(false));
713 } else {
714 // Set the symbol scope from the symbol regardless if it is nullptr
715 // or valid.
716 frame_sp->SetSymbolContextScope(
717 frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol);
718 }
719 SetFrameAtIndex(idx, frame_sp);
720 }
721 }
722 } else if (original_idx == 0) {
723 // There should ALWAYS be a frame at index 0. If something went wrong with
724 // the CurrentInlinedDepth such that there weren't as many frames as we
725 // thought taking that into account, then reset the current inlined depth
726 // and return the real zeroth frame.
727 if (m_frames.empty()) {
728 // Why do we have a thread with zero frames, that should not ever
729 // happen...
730 assert(!m_thread.IsValid() && "A valid thread has no frames.");
731 } else {
733 frame_sp = m_frames[original_idx];
734 }
735 }
736
737 return frame_sp;
738}
739
742 // First try assuming the unwind index is the same as the frame index. The
743 // unwind index is always greater than or equal to the frame index, so it is
744 // a good place to start. If we have inlined frames we might have 5 concrete
745 // frames (frame unwind indexes go from 0-4), but we might have 15 frames
746 // after we make all the inlined frames. Most of the time the unwind frame
747 // index (or the concrete frame index) is the same as the frame index.
748 uint32_t frame_idx = unwind_idx;
749 StackFrameSP frame_sp(GetFrameAtIndex(frame_idx));
750 while (frame_sp) {
751 if (frame_sp->GetFrameIndex() == unwind_idx)
752 break;
753 frame_sp = GetFrameAtIndex(++frame_idx);
754 }
755 return frame_sp;
756}
757
758static bool CompareStackID(const StackFrameSP &stack_sp,
759 const StackID &stack_id) {
760 return stack_sp->GetStackID() < stack_id;
761}
762
764 StackFrameSP frame_sp;
765
766 if (stack_id.IsValid()) {
767 std::lock_guard<std::recursive_mutex> guard(m_mutex);
768 uint32_t frame_idx = 0;
769 // Do a binary search in case the stack frame is already in our cache
770 collection::const_iterator begin = m_frames.begin();
771 collection::const_iterator end = m_frames.end();
772 if (begin != end) {
773 collection::const_iterator pos =
774 std::lower_bound(begin, end, stack_id, CompareStackID);
775 if (pos != end) {
776 if ((*pos)->GetStackID() == stack_id)
777 return *pos;
778 }
779 }
780 do {
781 frame_sp = GetFrameAtIndex(frame_idx);
782 if (frame_sp && frame_sp->GetStackID() == stack_id)
783 break;
784 frame_idx++;
785 } while (frame_sp);
786 }
787 return frame_sp;
788}
789
790bool StackFrameList::SetFrameAtIndex(uint32_t idx, StackFrameSP &frame_sp) {
791 if (idx >= m_frames.size())
792 m_frames.resize(idx + 1);
793 // Make sure allocation succeeded by checking bounds again
794 if (idx < m_frames.size()) {
795 m_frames[idx] = frame_sp;
796 return true;
797 }
798 return false; // resize failed, out of memory?
799}
800
802 // Don't call into the frame recognizers on the private state thread as
803 // they can cause code to run in the target, and that can cause deadlocks
804 // when fetching stop events for the expression.
805 if (m_thread.GetProcess()->CurrentThreadIsPrivateStateThread())
806 return;
807
808 Log *log = GetLog(LLDBLog::Thread);
809
810 // Only the top frame should be recognized.
811 StackFrameSP frame_sp = GetFrameAtIndex(0);
812 if (!frame_sp) {
813 LLDB_LOG(log, "Failed to construct Frame #0");
814 return;
815 }
816
817 RecognizedStackFrameSP recognized_frame_sp = frame_sp->GetRecognizedFrame();
818
819 if (!recognized_frame_sp) {
820 LLDB_LOG(log, "Frame #0 not recognized");
821 return;
822 }
823
824 if (StackFrameSP most_relevant_frame_sp =
825 recognized_frame_sp->GetMostRelevantFrame()) {
826 LLDB_LOG(log, "Found most relevant frame at index {0}",
827 most_relevant_frame_sp->GetFrameIndex());
828 SetSelectedFrame(most_relevant_frame_sp.get());
829 } else {
830 LLDB_LOG(log, "No relevant frame!");
831 }
832}
833
835 SelectMostRelevant select_most_relevant) {
836 std::lock_guard<std::recursive_mutex> guard(m_mutex);
837 if (!m_selected_frame_idx && select_most_relevant)
840 // If we aren't selecting the most relevant frame, and the selected frame
841 // isn't set, then don't force a selection here, just return 0.
842 if (!select_most_relevant)
843 return 0;
845 }
846 return *m_selected_frame_idx;
847}
848
850 std::lock_guard<std::recursive_mutex> guard(m_mutex);
851 const_iterator pos;
852 const_iterator begin = m_frames.begin();
853 const_iterator end = m_frames.end();
855
856 for (pos = begin; pos != end; ++pos) {
857 if (pos->get() == frame) {
858 m_selected_frame_idx = std::distance(begin, pos);
859 uint32_t inlined_depth = GetCurrentInlinedDepth();
860 if (inlined_depth != UINT32_MAX)
862 break;
863 }
864 }
865
867 return *m_selected_frame_idx;
868}
869
871 std::lock_guard<std::recursive_mutex> guard(m_mutex);
872 StackFrameSP frame_sp(GetFrameAtIndex(idx));
873 if (frame_sp) {
874 SetSelectedFrame(frame_sp.get());
875 return true;
876 } else
877 return false;
878}
879
881 if (m_thread.GetID() ==
882 m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) {
883 StackFrameSP frame_sp(
885 if (frame_sp) {
886 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
887 if (sc.line_entry.GetFile())
888 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine(
890 }
891 }
892}
893
894// The thread has been run, reset the number stack frames to zero so we can
895// determine how many frames we have lazily.
896// Note, we don't actually re-use StackFrameLists, we always make a new
897// StackFrameList every time we stop, and then copy frame information frame
898// by frame from the old to the new StackFrameList. So the comment above,
899// does not describe how StackFrameLists are currently used.
900// Clear is currently only used to clear the list in the destructor.
902 std::lock_guard<std::recursive_mutex> guard(m_mutex);
903 m_frames.clear();
905 m_selected_frame_idx.reset();
906}
907
910 const_iterator pos;
911 const_iterator begin = m_frames.begin();
912 const_iterator end = m_frames.end();
913 lldb::StackFrameSP ret_sp;
914
915 for (pos = begin; pos != end; ++pos) {
916 if (pos->get() == stack_frame_ptr) {
917 ret_sp = (*pos);
918 break;
919 }
920 }
921 return ret_sp;
922}
923
924size_t StackFrameList::GetStatus(Stream &strm, uint32_t first_frame,
925 uint32_t num_frames, bool show_frame_info,
926 uint32_t num_frames_with_source,
927 bool show_unique, bool show_hidden,
928 const char *selected_frame_marker) {
929 size_t num_frames_displayed = 0;
930
931 if (num_frames == 0)
932 return 0;
933
934 StackFrameSP frame_sp;
935 uint32_t frame_idx = 0;
936 uint32_t last_frame;
937
938 // Don't let the last frame wrap around...
939 if (num_frames == UINT32_MAX)
940 last_frame = UINT32_MAX;
941 else
942 last_frame = first_frame + num_frames;
943
944 StackFrameSP selected_frame_sp =
946 const char *unselected_marker = nullptr;
947 std::string buffer;
948 if (selected_frame_marker) {
949 size_t len = strlen(selected_frame_marker);
950 buffer.insert(buffer.begin(), len, ' ');
951 unselected_marker = buffer.c_str();
952 }
953 const char *marker = nullptr;
954 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) {
955 frame_sp = GetFrameAtIndex(frame_idx);
956 if (!frame_sp)
957 break;
958
959 if (selected_frame_marker != nullptr) {
960 if (frame_sp == selected_frame_sp)
961 marker = selected_frame_marker;
962 else
963 marker = unselected_marker;
964 }
965
966 // Hide uninteresting frames unless it's the selected frame.
967 if (!show_hidden && frame_sp != selected_frame_sp && frame_sp->IsHidden())
968 continue;
969
970 // Check for interruption here. If we're fetching arguments, this loop
971 // can go slowly:
972 Debugger &dbg = m_thread.GetProcess()->GetTarget().GetDebugger();
974 dbg, "Interrupted dumping stack for thread {0:x} with {1} shown.",
975 m_thread.GetID(), num_frames_displayed))
976 break;
977
978
979 if (!frame_sp->GetStatus(strm, show_frame_info,
980 num_frames_with_source > (first_frame - frame_idx),
981 show_unique, marker))
982 break;
983 ++num_frames_displayed;
984 }
985
986 strm.IndentLess();
987 return num_frames_displayed;
988}
#define INTERRUPT_REQUESTED(debugger,...)
This handy define will keep you from having to generate a report for the interruption by hand.
Definition: Debugger.h:446
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:359
#define LLDB_LOGF(log,...)
Definition: Log.h:366
static void FindInterveningFrames(Function &begin, Function &end, ExecutionContext &exe_ctx, Target &target, addr_t return_pc, CallSequence &path, ModuleList &images, Log *log)
Find the unique path through the call graph from begin (with return PC return_pc) to end.
static bool CompareStackID(const StackFrameSP &stack_sp, const StackID &stack_id)
std::vector< CallDescriptor > CallSequence
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
A section + offset based address class.
Definition: Address.h:62
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition: Address.cpp:1047
A class that describes a single lexical block.
Definition: Block.h:41
bool GetRangeContainingAddress(const Address &addr, AddressRange &range)
Definition: Block.cpp:250
Block * GetInlinedParent()
Get the inlined parent block for this block.
Definition: Block.cpp:214
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:81
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:249
Represent a call made within a Function.
Definition: Function.h:267
virtual Function * GetCallee(ModuleList &images, ExecutionContext &exe_ctx)=0
Get the callee's definition.
A class to manage flag bits.
Definition: Debugger.h:80
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetFramePtr(StackFrame *frame)
Set accessor to set only the frame shared pointer from a frame pointer.
A class that describes a function.
Definition: Function.h:399
CallEdge * GetCallEdgeForReturnAddress(lldb::addr_t return_pc, Target &target)
Get the outgoing call edge from this function which has the given return address return_pc,...
Definition: Function.cpp:356
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetCallEdges()
Get the outgoing call edges from this function, sorted by their return PC addresses (in increasing or...
Definition: Function.cpp:319
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
Definition: Function.cpp:438
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetTailCallingEdges()
Get the outgoing tail-calling edges from this function.
Definition: Function.cpp:349
ConstString GetDisplayName() const
Definition: Function.cpp:521
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:371
bool GetVerbose() const
Definition: Log.cpp:314
A collection class for Module objects.
Definition: ModuleList.h:103
lldb::addr_t m_current_inlined_pc
The program counter value at the currently selected synthetic activation.
collection m_frames
A cache of frames.
lldb::StackFrameSP GetFrameWithStackID(const StackID &stack_id)
Retrieve the stack frame with the given ID stack_id.
lldb::StackFrameSP GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)
Get the first concrete frame with index greater than or equal to idx.
void Clear()
Clear the cache of frames.
lldb::StackFrameSP GetFrameAtIndex(uint32_t idx)
Get the frame at index idx. Invisible frames cannot be indexed.
std::optional< uint32_t > m_selected_frame_idx
The currently selected frame.
lldb::StackFrameListSP m_prev_frames_sp
The old stack frame list.
uint32_t m_concrete_frames_fetched
The number of concrete frames fetched while filling the frame list.
size_t GetStatus(Stream &strm, uint32_t first_frame, uint32_t num_frames, bool show_frame_info, uint32_t num_frames_with_source, bool show_unique=false, bool show_hidden=false, const char *frame_marker=nullptr)
std::recursive_mutex m_mutex
A mutex for this frame list.
Thread & m_thread
The thread this frame list describes.
const bool m_show_inlined_frames
Whether or not to show synthetic (inline) frames. Immutable.
bool SetFrameAtIndex(uint32_t idx, lldb::StackFrameSP &frame_sp)
void GetOnlyConcreteFramesUpTo(uint32_t end_idx, Unwind &unwinder)
uint32_t m_current_inlined_depth
The number of synthetic function activations (invisible frames) expanded from the concrete frame #0 a...
void SetDefaultFileAndLineToSelectedFrame()
If the currently selected frame comes from the currently selected thread, point the default file and ...
uint32_t GetVisibleStackFrameIndex(uint32_t idx)
If the current inline depth (i.e the number of invisible frames) is valid, subtract it from idx.
uint32_t GetNumFrames(bool can_create=true)
Get the number of visible frames.
collection::const_iterator const_iterator
void SynthesizeTailCallFrames(StackFrame &next_frame)
Given that next_frame will be appended to the frame list, synthesize tail call frames between the cur...
StackFrameList(Thread &thread, const lldb::StackFrameListSP &prev_frames_sp, bool show_inline_frames)
void SetCurrentInlinedDepth(uint32_t new_depth)
uint32_t GetSelectedFrameIndex(SelectMostRelevant select_most_relevant_frame)
Get the currently selected frame index.
uint32_t SetSelectedFrame(lldb_private::StackFrame *frame)
Mark a stack frame as the currently selected frame and return its index.
bool GetFramesUpTo(uint32_t end_idx, InterruptionControl allow_interrupt=AllowInterruption)
Realizes frames up to (and including) end_idx (which can be greater than the actual number of frame...
void CalculateCurrentInlinedDepth()
Calculate and set the current inline depth.
lldb::StackFrameSP GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr)
If stack_frame_ptr is contained in this StackFrameList, return its wrapping shared pointer.
bool SetSelectedFrameByIndex(uint32_t idx)
Mark a stack frame as the currently selected frame using the frame index idx.
This base class provides an interface to stack frames.
Definition: StackFrame.h:43
void UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame)
uint32_t GetConcreteFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList, not counting inlined frame...
Definition: StackFrame.h:434
void SetFrameIndex(uint32_t index)
Set this frame's synthetic frame index.
Definition: StackFrame.h:424
void DumpUsingSettingsFormat(Stream *strm, bool show_unique=false, const char *frame_marker=nullptr)
Print a description for this frame using the frame-format formatter settings.
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
@ Artificial
An artificial stack frame (e.g.
@ Regular
A regular stack frame with access to registers and local variables.
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
Definition: StackFrame.cpp:300
lldb::TargetSP CalculateTarget() override
void Dump(Stream *s)
Definition: StackID.cpp:17
bool IsValid() const
Definition: StackID.h:47
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:198
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
Function * function
The Function for a given query.
bool GetParentOfInlinedScope(const Address &curr_frame_pc, SymbolContext &next_frame_sc, Address &inlined_frame_addr) const
Find the block containing the inlined block that contains this block.
Block * block
The Block for a given query.
LineEntry line_entry
The LineEntry for a given query.
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb::StackFrameSP GetSelectedFrame(SelectMostRelevant select_most_relevant)
Definition: Thread.cpp:271
virtual Unwind & GetUnwinder()
Definition: Thread.cpp:1903
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1401
lldb::ProcessSP GetProcess() const
Definition: Thread.h:157
bool IsValid() const
Definition: Thread.h:1141
lldb::StopInfoSP GetStopInfo()
Definition: Thread.cpp:347
bool GetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa, lldb::addr_t &pc, bool &behaves_like_zeroth_frame)
Definition: Unwind.h:50
uint32_t GetFramesUpTo(uint32_t end_idx)
Definition: Unwind.h:36
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
@ DoNotAllowInterruption
@ DoNoSelectMostRelevantFrame
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
Definition: lldb-forward.h:401
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
Definition: lldb-forward.h:321
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:427
uint64_t addr_t
Definition: lldb-types.h:80
@ eStopReasonBreakpoint
@ eStopReasonExec
Program was re-exec'ed.
@ eStopReasonVForkDone
@ eStopReasonException
@ eStopReasonWatchpoint
@ eStopReasonVFork
@ eStopReasonSignal
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:392
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
Definition: lldb-forward.h:422
A sequence of calls that comprise some portion of a backtrace.
uint32_t line
The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line number information.
Definition: LineEntry.h:147
const FileSpec & GetFile() const
Helper to access the file.
Definition: LineEntry.h:134
void ApplyFileMappings(lldb::TargetSP target_sp)
Apply file mappings from target.source-map to the LineEntry's file.
Definition: LineEntry.cpp:244
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47