UnwindLLDB.cpp revision 353358
1254721Semaste//===-- UnwindLLDB.cpp -------------------------------------*- C++ -*-===//
2254721Semaste//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6254721Semaste//
7254721Semaste//===----------------------------------------------------------------------===//
8254721Semaste
9254721Semaste#include "lldb/Core/Module.h"
10254721Semaste#include "lldb/Symbol/FuncUnwinders.h"
11254721Semaste#include "lldb/Symbol/Function.h"
12254721Semaste#include "lldb/Symbol/UnwindPlan.h"
13288943Sdim#include "lldb/Target/ABI.h"
14254721Semaste#include "lldb/Target/Process.h"
15254721Semaste#include "lldb/Target/RegisterContext.h"
16314564Sdim#include "lldb/Target/Target.h"
17314564Sdim#include "lldb/Target/Thread.h"
18321369Sdim#include "lldb/Utility/Log.h"
19254721Semaste
20314564Sdim#include "RegisterContextLLDB.h"
21254721Semaste#include "UnwindLLDB.h"
22254721Semaste
23254721Semasteusing namespace lldb;
24254721Semasteusing namespace lldb_private;
25254721Semaste
26314564SdimUnwindLLDB::UnwindLLDB(Thread &thread)
27314564Sdim    : Unwind(thread), m_frames(), m_unwind_complete(false),
28314564Sdim      m_user_supplied_trap_handler_functions() {
29314564Sdim  ProcessSP process_sp(thread.GetProcess());
30314564Sdim  if (process_sp) {
31314564Sdim    Args args;
32314564Sdim    process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames(args);
33314564Sdim    size_t count = args.GetArgumentCount();
34314564Sdim    for (size_t i = 0; i < count; i++) {
35314564Sdim      const char *func_name = args.GetArgumentAtIndex(i);
36314564Sdim      m_user_supplied_trap_handler_functions.push_back(ConstString(func_name));
37262528Semaste    }
38314564Sdim  }
39254721Semaste}
40254721Semaste
41314564Sdimuint32_t UnwindLLDB::DoGetFrameCount() {
42314564Sdim  if (!m_unwind_complete) {
43254721Semaste//#define DEBUG_FRAME_SPEED 1
44254721Semaste#if DEBUG_FRAME_SPEED
45254721Semaste#define FRAME_COUNT 10000
46314564Sdim    using namespace std::chrono;
47314564Sdim    auto time_value = steady_clock::now();
48254721Semaste#endif
49314564Sdim    if (!AddFirstFrame())
50314564Sdim      return 0;
51254721Semaste
52314564Sdim    ProcessSP process_sp(m_thread.GetProcess());
53353358Sdim    ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
54254721Semaste
55314564Sdim    while (AddOneMoreFrame(abi)) {
56254721Semaste#if DEBUG_FRAME_SPEED
57314564Sdim      if ((m_frames.size() % FRAME_COUNT) == 0) {
58314564Sdim        const auto now = steady_clock::now();
59314564Sdim        const auto delta_t = now - time_value;
60314564Sdim        printf("%u frames in %.9f ms (%g frames/sec)\n", FRAME_COUNT,
61314564Sdim               duration<double, std::milli>(delta_t).count(),
62314564Sdim               (float)FRAME_COUNT / duration<double>(delta_t).count());
63314564Sdim        time_value = now;
64314564Sdim      }
65254721Semaste#endif
66254721Semaste    }
67314564Sdim  }
68314564Sdim  return m_frames.size();
69254721Semaste}
70254721Semaste
71314564Sdimbool UnwindLLDB::AddFirstFrame() {
72314564Sdim  if (m_frames.size() > 0)
73314564Sdim    return true;
74296417Sdim
75314564Sdim  ProcessSP process_sp(m_thread.GetProcess());
76353358Sdim  ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
77254721Semaste
78314564Sdim  // First, set up the 0th (initial) frame
79314564Sdim  CursorSP first_cursor_sp(new Cursor());
80314564Sdim  RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB(
81314564Sdim      m_thread, RegisterContextLLDBSP(), first_cursor_sp->sctx, 0, *this));
82353358Sdim  if (reg_ctx_sp.get() == nullptr)
83314564Sdim    goto unwind_done;
84254721Semaste
85314564Sdim  if (!reg_ctx_sp->IsValid())
86314564Sdim    goto unwind_done;
87254721Semaste
88314564Sdim  if (!reg_ctx_sp->GetCFA(first_cursor_sp->cfa))
89314564Sdim    goto unwind_done;
90296417Sdim
91314564Sdim  if (!reg_ctx_sp->ReadPC(first_cursor_sp->start_pc))
92314564Sdim    goto unwind_done;
93296417Sdim
94314564Sdim  // Everything checks out, so release the auto pointer value and let the
95314564Sdim  // cursor own it in its shared pointer
96314564Sdim  first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
97314564Sdim  m_frames.push_back(first_cursor_sp);
98262528Semaste
99314564Sdim  // Update the Full Unwind Plan for this frame if not valid
100314564Sdim  UpdateUnwindPlanForFirstFrameIfInvalid(abi);
101314564Sdim
102314564Sdim  return true;
103314564Sdim
104254721Semasteunwind_done:
105314564Sdim  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
106314564Sdim  if (log) {
107314564Sdim    log->Printf("th%d Unwind of this thread is complete.",
108314564Sdim                m_thread.GetIndexID());
109314564Sdim  }
110314564Sdim  m_unwind_complete = true;
111314564Sdim  return false;
112254721Semaste}
113254721Semaste
114314564SdimUnwindLLDB::CursorSP UnwindLLDB::GetOneMoreFrame(ABI *abi) {
115314564Sdim  assert(m_frames.size() != 0 &&
116314564Sdim         "Get one more frame called with empty frame list");
117288943Sdim
118314564Sdim  // If we've already gotten to the end of the stack, don't bother to try
119314564Sdim  // again...
120314564Sdim  if (m_unwind_complete)
121314564Sdim    return nullptr;
122254721Semaste
123314564Sdim  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
124254721Semaste
125314564Sdim  CursorSP prev_frame = m_frames.back();
126314564Sdim  uint32_t cur_idx = m_frames.size();
127254721Semaste
128314564Sdim  CursorSP cursor_sp(new Cursor());
129314564Sdim  RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB(
130314564Sdim      m_thread, prev_frame->reg_ctx_lldb_sp, cursor_sp->sctx, cur_idx, *this));
131254721Semaste
132344779Sdim  uint64_t max_stack_depth = m_thread.GetMaxBacktraceDepth();
133344779Sdim
134314564Sdim  // We want to detect an unwind that cycles erroneously and stop backtracing.
135314564Sdim  // Don't want this maximum unwind limit to be too low -- if you have a
136341825Sdim  // backtrace with an "infinitely recursing" bug, it will crash when the stack
137341825Sdim  // blows out and the first 35,000 frames are uninteresting - it's the top
138341825Sdim  // most 5 frames that you actually care about.  So you can't just cap the
139341825Sdim  // unwind at 10,000 or something. Realistically anything over around 200,000
140341825Sdim  // is going to blow out the stack space. If we're still unwinding at that
141341825Sdim  // point, we're probably never going to finish.
142344779Sdim  if (cur_idx >= max_stack_depth) {
143314564Sdim    if (log)
144314564Sdim      log->Printf("%*sFrame %d unwound too many frames, assuming unwind has "
145314564Sdim                  "gone astray, stopping.",
146314564Sdim                  cur_idx < 100 ? cur_idx : 100, "", cur_idx);
147314564Sdim    return nullptr;
148314564Sdim  }
149296417Sdim
150353358Sdim  if (reg_ctx_sp.get() == nullptr) {
151314564Sdim    // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to
152341825Sdim    // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
153341825Sdim    // return false.
154314564Sdim    if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
155314564Sdim      // TryFallbackUnwindPlan for prev_frame succeeded and updated
156341825Sdim      // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
157341825Sdim      // still needs to be updated. Hence updating it.
158314564Sdim      if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
159314564Sdim        return nullptr;
160288943Sdim
161314564Sdim      return GetOneMoreFrame(abi);
162262528Semaste    }
163254721Semaste
164314564Sdim    if (log)
165314564Sdim      log->Printf("%*sFrame %d did not get a RegisterContext, stopping.",
166314564Sdim                  cur_idx < 100 ? cur_idx : 100, "", cur_idx);
167314564Sdim    return nullptr;
168314564Sdim  }
169296417Sdim
170314564Sdim  if (!reg_ctx_sp->IsValid()) {
171341825Sdim    // We failed to get a valid RegisterContext. See if the regctx below this
172341825Sdim    // on the stack has a fallback unwind plan it can use. Subsequent calls to
173341825Sdim    // TryFallbackUnwindPlan() will return false.
174314564Sdim    if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
175314564Sdim      // TryFallbackUnwindPlan for prev_frame succeeded and updated
176341825Sdim      // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
177341825Sdim      // still needs to be updated. Hence updating it.
178314564Sdim      if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
179314564Sdim        return nullptr;
180288943Sdim
181314564Sdim      return GetOneMoreFrame(abi);
182254721Semaste    }
183296417Sdim
184314564Sdim    if (log)
185314564Sdim      log->Printf("%*sFrame %d invalid RegisterContext for this frame, "
186314564Sdim                  "stopping stack walk",
187314564Sdim                  cur_idx < 100 ? cur_idx : 100, "", cur_idx);
188314564Sdim    return nullptr;
189314564Sdim  }
190314564Sdim  if (!reg_ctx_sp->GetCFA(cursor_sp->cfa)) {
191314564Sdim    // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to
192341825Sdim    // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
193341825Sdim    // return false.
194314564Sdim    if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
195314564Sdim      // TryFallbackUnwindPlan for prev_frame succeeded and updated
196341825Sdim      // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
197341825Sdim      // still needs to be updated. Hence updating it.
198314564Sdim      if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
199314564Sdim        return nullptr;
200288943Sdim
201314564Sdim      return GetOneMoreFrame(abi);
202254721Semaste    }
203296417Sdim
204314564Sdim    if (log)
205314564Sdim      log->Printf(
206314564Sdim          "%*sFrame %d did not get CFA for this frame, stopping stack walk",
207314564Sdim          cur_idx < 100 ? cur_idx : 100, "", cur_idx);
208314564Sdim    return nullptr;
209314564Sdim  }
210314564Sdim  if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa)) {
211314564Sdim    // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not
212341825Sdim    // have its (constructed) CFA aligned correctly -- don't do the abi
213341825Sdim    // alignment check for these.
214344779Sdim    if (!reg_ctx_sp->IsTrapHandlerFrame()) {
215314564Sdim      // See if we can find a fallback unwind plan for THIS frame.  It may be
216314564Sdim      // that the UnwindPlan we're using for THIS frame was bad and gave us a
217341825Sdim      // bad CFA. If that's not it, then see if we can change the UnwindPlan
218341825Sdim      // for the frame below us ("NEXT") -- see if using that other UnwindPlan
219341825Sdim      // gets us a better unwind state.
220344779Sdim      if (!reg_ctx_sp->TryFallbackUnwindPlan() ||
221344779Sdim          !reg_ctx_sp->GetCFA(cursor_sp->cfa) ||
222344779Sdim          !abi->CallFrameAddressIsValid(cursor_sp->cfa)) {
223314564Sdim        if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
224314564Sdim          // TryFallbackUnwindPlan for prev_frame succeeded and updated
225341825Sdim          // reg_ctx_lldb_sp field of prev_frame. However, cfa field of
226341825Sdim          // prev_frame still needs to be updated. Hence updating it.
227314564Sdim          if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
228314564Sdim            return nullptr;
229288943Sdim
230314564Sdim          return GetOneMoreFrame(abi);
231254721Semaste        }
232296417Sdim
233254721Semaste        if (log)
234314564Sdim          log->Printf("%*sFrame %d did not get a valid CFA for this frame, "
235314564Sdim                      "stopping stack walk",
236314564Sdim                      cur_idx < 100 ? cur_idx : 100, "", cur_idx);
237288943Sdim        return nullptr;
238314564Sdim      } else {
239314564Sdim        if (log)
240314564Sdim          log->Printf("%*sFrame %d had a bad CFA value but we switched the "
241314564Sdim                      "UnwindPlan being used and got one that looks more "
242314564Sdim                      "realistic.",
243314564Sdim                      cur_idx < 100 ? cur_idx : 100, "", cur_idx);
244314564Sdim      }
245254721Semaste    }
246314564Sdim  }
247314564Sdim  if (!reg_ctx_sp->ReadPC(cursor_sp->start_pc)) {
248314564Sdim    // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to
249341825Sdim    // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
250341825Sdim    // return false.
251314564Sdim    if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
252314564Sdim      // TryFallbackUnwindPlan for prev_frame succeeded and updated
253341825Sdim      // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
254341825Sdim      // still needs to be updated. Hence updating it.
255314564Sdim      if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
256314564Sdim        return nullptr;
257296417Sdim
258314564Sdim      return GetOneMoreFrame(abi);
259314564Sdim    }
260288943Sdim
261314564Sdim    if (log)
262314564Sdim      log->Printf(
263314564Sdim          "%*sFrame %d did not get PC for this frame, stopping stack walk",
264314564Sdim          cur_idx < 100 ? cur_idx : 100, "", cur_idx);
265314564Sdim    return nullptr;
266314564Sdim  }
267314564Sdim  if (abi && !abi->CodeAddressIsValid(cursor_sp->start_pc)) {
268314564Sdim    // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to
269341825Sdim    // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
270341825Sdim    // return false.
271314564Sdim    if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
272314564Sdim      // TryFallbackUnwindPlan for prev_frame succeeded and updated
273341825Sdim      // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
274341825Sdim      // still needs to be updated. Hence updating it.
275314564Sdim      if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
276288943Sdim        return nullptr;
277314564Sdim
278314564Sdim      return GetOneMoreFrame(abi);
279254721Semaste    }
280258054Semaste
281314564Sdim    if (log)
282314564Sdim      log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
283314564Sdim                  cur_idx < 100 ? cur_idx : 100, "", cur_idx);
284314564Sdim    return nullptr;
285314564Sdim  }
286314564Sdim  // Infinite loop where the current cursor is the same as the previous one...
287314564Sdim  if (prev_frame->start_pc == cursor_sp->start_pc &&
288314564Sdim      prev_frame->cfa == cursor_sp->cfa) {
289314564Sdim    if (log)
290314564Sdim      log->Printf("th%d pc of this frame is the same as the previous frame and "
291314564Sdim                  "CFAs for both frames are identical -- stopping unwind",
292314564Sdim                  m_thread.GetIndexID());
293314564Sdim    return nullptr;
294314564Sdim  }
295314564Sdim
296314564Sdim  cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
297314564Sdim  return cursor_sp;
298288943Sdim}
299288943Sdim
300314564Sdimvoid UnwindLLDB::UpdateUnwindPlanForFirstFrameIfInvalid(ABI *abi) {
301314564Sdim  // This function is called for First Frame only.
302314564Sdim  assert(m_frames.size() == 1 && "No. of cursor frames are not 1");
303296417Sdim
304314564Sdim  bool old_m_unwind_complete = m_unwind_complete;
305314564Sdim  CursorSP old_m_candidate_frame = m_candidate_frame;
306296417Sdim
307314564Sdim  // Try to unwind 2 more frames using the Unwinder. It uses Full UnwindPlan
308341825Sdim  // and if Full UnwindPlan fails, then uses FallBack UnwindPlan. Also update
309341825Sdim  // the cfa of Frame 0 (if required).
310314564Sdim  AddOneMoreFrame(abi);
311296417Sdim
312341825Sdim  // Remove all the frames added by above function as the purpose of using
313341825Sdim  // above function was just to check whether Unwinder of Frame 0 works or not.
314314564Sdim  for (uint32_t i = 1; i < m_frames.size(); i++)
315314564Sdim    m_frames.pop_back();
316296417Sdim
317314564Sdim  // Restore status after calling AddOneMoreFrame
318314564Sdim  m_unwind_complete = old_m_unwind_complete;
319314564Sdim  m_candidate_frame = old_m_candidate_frame;
320314564Sdim  return;
321296417Sdim}
322296417Sdim
323314564Sdimbool UnwindLLDB::AddOneMoreFrame(ABI *abi) {
324314564Sdim  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
325296417Sdim
326314564Sdim  // Frame zero is a little different
327314564Sdim  if (m_frames.empty())
328314564Sdim    return false;
329288943Sdim
330314564Sdim  // If we've already gotten to the end of the stack, don't bother to try
331314564Sdim  // again...
332314564Sdim  if (m_unwind_complete)
333314564Sdim    return false;
334288943Sdim
335314564Sdim  CursorSP new_frame = m_candidate_frame;
336314564Sdim  if (new_frame == nullptr)
337314564Sdim    new_frame = GetOneMoreFrame(abi);
338288943Sdim
339314564Sdim  if (new_frame == nullptr) {
340314564Sdim    if (log)
341314564Sdim      log->Printf("th%d Unwind of this thread is complete.",
342314564Sdim                  m_thread.GetIndexID());
343314564Sdim    m_unwind_complete = true;
344314564Sdim    return false;
345314564Sdim  }
346288943Sdim
347314564Sdim  m_frames.push_back(new_frame);
348288943Sdim
349341825Sdim  // If we can get one more frame further then accept that we get back a
350341825Sdim  // correct frame.
351314564Sdim  m_candidate_frame = GetOneMoreFrame(abi);
352314564Sdim  if (m_candidate_frame)
353314564Sdim    return true;
354288943Sdim
355314564Sdim  // We can't go further from the frame returned by GetOneMore frame. Lets try
356341825Sdim  // to get a different frame with using the fallback unwind plan.
357314564Sdim  if (!m_frames[m_frames.size() - 2]
358314564Sdim           ->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
359314564Sdim    // We don't have a valid fallback unwind plan. Accept the frame as it is.
360341825Sdim    // This is a valid situation when we are at the bottom of the stack.
361314564Sdim    return true;
362314564Sdim  }
363288943Sdim
364314564Sdim  // Remove the possibly incorrect frame from the frame list and try to add a
365341825Sdim  // different one with the newly selected fallback unwind plan.
366314564Sdim  m_frames.pop_back();
367314564Sdim  CursorSP new_frame_v2 = GetOneMoreFrame(abi);
368314564Sdim  if (new_frame_v2 == nullptr) {
369314564Sdim    // We haven't got a new frame from the fallback unwind plan. Accept the
370341825Sdim    // frame from the original unwind plan. This is a valid situation when we
371341825Sdim    // are at the bottom of the stack.
372314564Sdim    m_frames.push_back(new_frame);
373314564Sdim    return true;
374314564Sdim  }
375288943Sdim
376314564Sdim  // Push the new frame to the list and try to continue from this frame. If we
377341825Sdim  // can get a new frame then accept it as the correct one.
378314564Sdim  m_frames.push_back(new_frame_v2);
379314564Sdim  m_candidate_frame = GetOneMoreFrame(abi);
380314564Sdim  if (m_candidate_frame) {
381314564Sdim    // If control reached here then TryFallbackUnwindPlan had succeeded for
382341825Sdim    // Cursor::m_frames[m_frames.size() - 2]. It also succeeded to Unwind next
383341825Sdim    // 2 frames i.e. m_frames[m_frames.size() - 1] and a frame after that. For
384341825Sdim    // Cursor::m_frames[m_frames.size() - 2], reg_ctx_lldb_sp field was already
385341825Sdim    // updated during TryFallbackUnwindPlan call above. However, cfa field
386341825Sdim    // still needs to be updated. Hence updating it here and then returning.
387344779Sdim    return m_frames[m_frames.size() - 2]->reg_ctx_lldb_sp->GetCFA(
388344779Sdim        m_frames[m_frames.size() - 2]->cfa);
389314564Sdim  }
390288943Sdim
391314564Sdim  // The new frame hasn't helped in unwinding. Fall back to the original one as
392341825Sdim  // the default unwind plan is usually more reliable then the fallback one.
393314564Sdim  m_frames.pop_back();
394314564Sdim  m_frames.push_back(new_frame);
395314564Sdim  return true;
396254721Semaste}
397254721Semaste
398314564Sdimbool UnwindLLDB::DoGetFrameInfoAtIndex(uint32_t idx, addr_t &cfa, addr_t &pc) {
399314564Sdim  if (m_frames.size() == 0) {
400314564Sdim    if (!AddFirstFrame())
401314564Sdim      return false;
402314564Sdim  }
403254721Semaste
404314564Sdim  ProcessSP process_sp(m_thread.GetProcess());
405353358Sdim  ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
406254721Semaste
407314564Sdim  while (idx >= m_frames.size() && AddOneMoreFrame(abi))
408314564Sdim    ;
409254721Semaste
410314564Sdim  if (idx < m_frames.size()) {
411314564Sdim    cfa = m_frames[idx]->cfa;
412314564Sdim    pc = m_frames[idx]->start_pc;
413314564Sdim    return true;
414314564Sdim  }
415314564Sdim  return false;
416254721Semaste}
417254721Semaste
418254721Semastelldb::RegisterContextSP
419314564SdimUnwindLLDB::DoCreateRegisterContextForFrame(StackFrame *frame) {
420314564Sdim  lldb::RegisterContextSP reg_ctx_sp;
421314564Sdim  uint32_t idx = frame->GetConcreteFrameIndex();
422254721Semaste
423314564Sdim  if (idx == 0) {
424314564Sdim    return m_thread.GetRegisterContext();
425314564Sdim  }
426254721Semaste
427314564Sdim  if (m_frames.size() == 0) {
428314564Sdim    if (!AddFirstFrame())
429314564Sdim      return reg_ctx_sp;
430314564Sdim  }
431254721Semaste
432314564Sdim  ProcessSP process_sp(m_thread.GetProcess());
433353358Sdim  ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
434254721Semaste
435314564Sdim  while (idx >= m_frames.size()) {
436314564Sdim    if (!AddOneMoreFrame(abi))
437314564Sdim      break;
438314564Sdim  }
439254721Semaste
440314564Sdim  const uint32_t num_frames = m_frames.size();
441314564Sdim  if (idx < num_frames) {
442314564Sdim    Cursor *frame_cursor = m_frames[idx].get();
443314564Sdim    reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
444314564Sdim  }
445314564Sdim  return reg_ctx_sp;
446254721Semaste}
447254721Semaste
448254721SemasteUnwindLLDB::RegisterContextLLDBSP
449314564SdimUnwindLLDB::GetRegisterContextForFrameNum(uint32_t frame_num) {
450314564Sdim  RegisterContextLLDBSP reg_ctx_sp;
451314564Sdim  if (frame_num < m_frames.size())
452314564Sdim    reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
453314564Sdim  return reg_ctx_sp;
454254721Semaste}
455254721Semaste
456314564Sdimbool UnwindLLDB::SearchForSavedLocationForRegister(
457314564Sdim    uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc,
458314564Sdim    uint32_t starting_frame_num, bool pc_reg) {
459314564Sdim  int64_t frame_num = starting_frame_num;
460314564Sdim  if (static_cast<size_t>(frame_num) >= m_frames.size())
461314564Sdim    return false;
462254721Semaste
463341825Sdim  // Never interrogate more than one level while looking for the saved pc
464341825Sdim  // value. If the value isn't saved by frame_num, none of the frames lower on
465341825Sdim  // the stack will have a useful value.
466314564Sdim  if (pc_reg) {
467314564Sdim    UnwindLLDB::RegisterSearchResult result;
468314564Sdim    result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister(
469314564Sdim        lldb_regnum, regloc);
470344779Sdim    return result == UnwindLLDB::RegisterSearchResult::eRegisterFound;
471314564Sdim  }
472314564Sdim  while (frame_num >= 0) {
473314564Sdim    UnwindLLDB::RegisterSearchResult result;
474314564Sdim    result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister(
475314564Sdim        lldb_regnum, regloc);
476314564Sdim
477314564Sdim    // We descended down to the live register context aka stack frame 0 and are
478341825Sdim    // reading the value out of a live register.
479314564Sdim    if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound &&
480314564Sdim        regloc.type ==
481314564Sdim            UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext) {
482314564Sdim      return true;
483254721Semaste    }
484254721Semaste
485314564Sdim    // If we have unwind instructions saying that register N is saved in
486341825Sdim    // register M in the middle of the stack (and N can equal M here, meaning
487341825Sdim    // the register was not used in this function), then change the register
488341825Sdim    // number we're looking for to M and keep looking for a concrete  location
489314564Sdim    // down the stack, or an actual value from a live RegisterContext at frame
490314564Sdim    // 0.
491314564Sdim    if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound &&
492314564Sdim        regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister &&
493314564Sdim        frame_num > 0) {
494314564Sdim      result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
495314564Sdim      lldb_regnum = regloc.location.register_number;
496314564Sdim    }
497280031Sdim
498314564Sdim    if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
499314564Sdim      return true;
500314564Sdim    if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)
501314564Sdim      return false;
502314564Sdim    frame_num--;
503314564Sdim  }
504314564Sdim  return false;
505254721Semaste}
506