1d00dff88SPavel Labath //===-- UnwindLLDB.cpp ----------------------------------------------------===//
2d00dff88SPavel Labath //
3d00dff88SPavel Labath // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d00dff88SPavel Labath // See https://llvm.org/LICENSE.txt for license information.
5d00dff88SPavel Labath // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d00dff88SPavel Labath //
7d00dff88SPavel Labath //===----------------------------------------------------------------------===//
8d00dff88SPavel Labath 
9d00dff88SPavel Labath #include "lldb/Target/UnwindLLDB.h"
10d00dff88SPavel Labath #include "lldb/Core/Module.h"
11d00dff88SPavel Labath #include "lldb/Symbol/FuncUnwinders.h"
12d00dff88SPavel Labath #include "lldb/Symbol/Function.h"
13d00dff88SPavel Labath #include "lldb/Symbol/UnwindPlan.h"
14d00dff88SPavel Labath #include "lldb/Target/ABI.h"
15d00dff88SPavel Labath #include "lldb/Target/Process.h"
16d00dff88SPavel Labath #include "lldb/Target/RegisterContext.h"
17d00dff88SPavel Labath #include "lldb/Target/RegisterContextUnwind.h"
18d00dff88SPavel Labath #include "lldb/Target/Target.h"
19d00dff88SPavel Labath #include "lldb/Target/Thread.h"
20*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
21d00dff88SPavel Labath #include "lldb/Utility/Log.h"
22d00dff88SPavel Labath 
23d00dff88SPavel Labath using namespace lldb;
24d00dff88SPavel Labath using namespace lldb_private;
25d00dff88SPavel Labath 
UnwindLLDB(Thread & thread)26d00dff88SPavel Labath UnwindLLDB::UnwindLLDB(Thread &thread)
27d00dff88SPavel Labath     : Unwind(thread), m_frames(), m_unwind_complete(false),
28d00dff88SPavel Labath       m_user_supplied_trap_handler_functions() {
29d00dff88SPavel Labath   ProcessSP process_sp(thread.GetProcess());
30d00dff88SPavel Labath   if (process_sp) {
31d00dff88SPavel Labath     Args args;
32d00dff88SPavel Labath     process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames(args);
33d00dff88SPavel Labath     size_t count = args.GetArgumentCount();
34d00dff88SPavel Labath     for (size_t i = 0; i < count; i++) {
35d00dff88SPavel Labath       const char *func_name = args.GetArgumentAtIndex(i);
36d00dff88SPavel Labath       m_user_supplied_trap_handler_functions.push_back(ConstString(func_name));
37d00dff88SPavel Labath     }
38d00dff88SPavel Labath   }
39d00dff88SPavel Labath }
40d00dff88SPavel Labath 
DoGetFrameCount()41d00dff88SPavel Labath uint32_t UnwindLLDB::DoGetFrameCount() {
42d00dff88SPavel Labath   if (!m_unwind_complete) {
43d00dff88SPavel Labath //#define DEBUG_FRAME_SPEED 1
44d00dff88SPavel Labath #if DEBUG_FRAME_SPEED
45d00dff88SPavel Labath #define FRAME_COUNT 10000
46d00dff88SPavel Labath     using namespace std::chrono;
47d00dff88SPavel Labath     auto time_value = steady_clock::now();
48d00dff88SPavel Labath #endif
49d00dff88SPavel Labath     if (!AddFirstFrame())
50d00dff88SPavel Labath       return 0;
51d00dff88SPavel Labath 
52d00dff88SPavel Labath     ProcessSP process_sp(m_thread.GetProcess());
53d00dff88SPavel Labath     ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
54d00dff88SPavel Labath 
55d00dff88SPavel Labath     while (AddOneMoreFrame(abi)) {
56d00dff88SPavel Labath #if DEBUG_FRAME_SPEED
57d00dff88SPavel Labath       if ((m_frames.size() % FRAME_COUNT) == 0) {
58d00dff88SPavel Labath         const auto now = steady_clock::now();
59d00dff88SPavel Labath         const auto delta_t = now - time_value;
60d00dff88SPavel Labath         printf("%u frames in %.9f ms (%g frames/sec)\n", FRAME_COUNT,
61d00dff88SPavel Labath                duration<double, std::milli>(delta_t).count(),
62d00dff88SPavel Labath                (float)FRAME_COUNT / duration<double>(delta_t).count());
63d00dff88SPavel Labath         time_value = now;
64d00dff88SPavel Labath       }
65d00dff88SPavel Labath #endif
66d00dff88SPavel Labath     }
67d00dff88SPavel Labath   }
68d00dff88SPavel Labath   return m_frames.size();
69d00dff88SPavel Labath }
70d00dff88SPavel Labath 
AddFirstFrame()71d00dff88SPavel Labath bool UnwindLLDB::AddFirstFrame() {
72d00dff88SPavel Labath   if (m_frames.size() > 0)
73d00dff88SPavel Labath     return true;
74d00dff88SPavel Labath 
75d00dff88SPavel Labath   ProcessSP process_sp(m_thread.GetProcess());
76d00dff88SPavel Labath   ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
77d00dff88SPavel Labath 
78d00dff88SPavel Labath   // First, set up the 0th (initial) frame
79d00dff88SPavel Labath   CursorSP first_cursor_sp(new Cursor());
80d00dff88SPavel Labath   RegisterContextLLDBSP reg_ctx_sp(new RegisterContextUnwind(
81d00dff88SPavel Labath       m_thread, RegisterContextLLDBSP(), first_cursor_sp->sctx, 0, *this));
82d00dff88SPavel Labath   if (reg_ctx_sp.get() == nullptr)
83d00dff88SPavel Labath     goto unwind_done;
84d00dff88SPavel Labath 
85d00dff88SPavel Labath   if (!reg_ctx_sp->IsValid())
86d00dff88SPavel Labath     goto unwind_done;
87d00dff88SPavel Labath 
88d00dff88SPavel Labath   if (!reg_ctx_sp->GetCFA(first_cursor_sp->cfa))
89d00dff88SPavel Labath     goto unwind_done;
90d00dff88SPavel Labath 
91d00dff88SPavel Labath   if (!reg_ctx_sp->ReadPC(first_cursor_sp->start_pc))
92d00dff88SPavel Labath     goto unwind_done;
93d00dff88SPavel Labath 
94d00dff88SPavel Labath   // Everything checks out, so release the auto pointer value and let the
95d00dff88SPavel Labath   // cursor own it in its shared pointer
96d00dff88SPavel Labath   first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
97d00dff88SPavel Labath   m_frames.push_back(first_cursor_sp);
98d00dff88SPavel Labath 
99d00dff88SPavel Labath   // Update the Full Unwind Plan for this frame if not valid
100d00dff88SPavel Labath   UpdateUnwindPlanForFirstFrameIfInvalid(abi);
101d00dff88SPavel Labath 
102d00dff88SPavel Labath   return true;
103d00dff88SPavel Labath 
104d00dff88SPavel Labath unwind_done:
105a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
106d00dff88SPavel Labath   if (log) {
107d00dff88SPavel Labath     LLDB_LOGF(log, "th%d Unwind of this thread is complete.",
108d00dff88SPavel Labath               m_thread.GetIndexID());
109d00dff88SPavel Labath   }
110d00dff88SPavel Labath   m_unwind_complete = true;
111d00dff88SPavel Labath   return false;
112d00dff88SPavel Labath }
113d00dff88SPavel Labath 
GetOneMoreFrame(ABI * abi)114d00dff88SPavel Labath UnwindLLDB::CursorSP UnwindLLDB::GetOneMoreFrame(ABI *abi) {
115d00dff88SPavel Labath   assert(m_frames.size() != 0 &&
116d00dff88SPavel Labath          "Get one more frame called with empty frame list");
117d00dff88SPavel Labath 
118d00dff88SPavel Labath   // If we've already gotten to the end of the stack, don't bother to try
119d00dff88SPavel Labath   // again...
120d00dff88SPavel Labath   if (m_unwind_complete)
121d00dff88SPavel Labath     return nullptr;
122d00dff88SPavel Labath 
123a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
124d00dff88SPavel Labath 
125d00dff88SPavel Labath   CursorSP prev_frame = m_frames.back();
126d00dff88SPavel Labath   uint32_t cur_idx = m_frames.size();
127d00dff88SPavel Labath 
128d00dff88SPavel Labath   CursorSP cursor_sp(new Cursor());
129d00dff88SPavel Labath   RegisterContextLLDBSP reg_ctx_sp(new RegisterContextUnwind(
130d00dff88SPavel Labath       m_thread, prev_frame->reg_ctx_lldb_sp, cursor_sp->sctx, cur_idx, *this));
131d00dff88SPavel Labath 
132d00dff88SPavel Labath   uint64_t max_stack_depth = m_thread.GetMaxBacktraceDepth();
133d00dff88SPavel Labath 
134d00dff88SPavel Labath   // We want to detect an unwind that cycles erroneously and stop backtracing.
135d00dff88SPavel Labath   // Don't want this maximum unwind limit to be too low -- if you have a
136d00dff88SPavel Labath   // backtrace with an "infinitely recursing" bug, it will crash when the stack
137d00dff88SPavel Labath   // blows out and the first 35,000 frames are uninteresting - it's the top
138d00dff88SPavel Labath   // most 5 frames that you actually care about.  So you can't just cap the
139d00dff88SPavel Labath   // unwind at 10,000 or something. Realistically anything over around 200,000
140d00dff88SPavel Labath   // is going to blow out the stack space. If we're still unwinding at that
141d00dff88SPavel Labath   // point, we're probably never going to finish.
142d00dff88SPavel Labath   if (cur_idx >= max_stack_depth) {
143d00dff88SPavel Labath     LLDB_LOGF(log,
144d00dff88SPavel Labath               "%*sFrame %d unwound too many frames, assuming unwind has "
145d00dff88SPavel Labath               "gone astray, stopping.",
146d00dff88SPavel Labath               cur_idx < 100 ? cur_idx : 100, "", cur_idx);
147d00dff88SPavel Labath     return nullptr;
148d00dff88SPavel Labath   }
149d00dff88SPavel Labath 
150d00dff88SPavel Labath   if (reg_ctx_sp.get() == nullptr) {
151d00dff88SPavel Labath     // If the RegisterContextUnwind has a fallback UnwindPlan, it will switch to
152d00dff88SPavel Labath     // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
153d00dff88SPavel Labath     // return false.
154d00dff88SPavel Labath     if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
155d00dff88SPavel Labath       // TryFallbackUnwindPlan for prev_frame succeeded and updated
156d00dff88SPavel Labath       // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
157d00dff88SPavel Labath       // still needs to be updated. Hence updating it.
158d00dff88SPavel Labath       if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
159d00dff88SPavel Labath         return nullptr;
160d00dff88SPavel Labath 
161d00dff88SPavel Labath       return GetOneMoreFrame(abi);
162d00dff88SPavel Labath     }
163d00dff88SPavel Labath 
164d00dff88SPavel Labath     LLDB_LOGF(log, "%*sFrame %d did not get a RegisterContext, stopping.",
165d00dff88SPavel Labath               cur_idx < 100 ? cur_idx : 100, "", cur_idx);
166d00dff88SPavel Labath     return nullptr;
167d00dff88SPavel Labath   }
168d00dff88SPavel Labath 
169d00dff88SPavel Labath   if (!reg_ctx_sp->IsValid()) {
170d00dff88SPavel Labath     // We failed to get a valid RegisterContext. See if the regctx below this
171d00dff88SPavel Labath     // on the stack has a fallback unwind plan it can use. Subsequent calls to
172d00dff88SPavel Labath     // TryFallbackUnwindPlan() will return false.
173d00dff88SPavel Labath     if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
174d00dff88SPavel Labath       // TryFallbackUnwindPlan for prev_frame succeeded and updated
175d00dff88SPavel Labath       // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
176d00dff88SPavel Labath       // still needs to be updated. Hence updating it.
177d00dff88SPavel Labath       if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
178d00dff88SPavel Labath         return nullptr;
179d00dff88SPavel Labath 
180d00dff88SPavel Labath       return GetOneMoreFrame(abi);
181d00dff88SPavel Labath     }
182d00dff88SPavel Labath 
183d00dff88SPavel Labath     LLDB_LOGF(log,
184d00dff88SPavel Labath               "%*sFrame %d invalid RegisterContext for this frame, "
185d00dff88SPavel Labath               "stopping stack walk",
186d00dff88SPavel Labath               cur_idx < 100 ? cur_idx : 100, "", cur_idx);
187d00dff88SPavel Labath     return nullptr;
188d00dff88SPavel Labath   }
189d00dff88SPavel Labath   if (!reg_ctx_sp->GetCFA(cursor_sp->cfa)) {
190d00dff88SPavel Labath     // If the RegisterContextUnwind has a fallback UnwindPlan, it will switch to
191d00dff88SPavel Labath     // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
192d00dff88SPavel Labath     // return false.
193d00dff88SPavel Labath     if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
194d00dff88SPavel Labath       // TryFallbackUnwindPlan for prev_frame succeeded and updated
195d00dff88SPavel Labath       // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
196d00dff88SPavel Labath       // still needs to be updated. Hence updating it.
197d00dff88SPavel Labath       if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
198d00dff88SPavel Labath         return nullptr;
199d00dff88SPavel Labath 
200d00dff88SPavel Labath       return GetOneMoreFrame(abi);
201d00dff88SPavel Labath     }
202d00dff88SPavel Labath 
203d00dff88SPavel Labath     LLDB_LOGF(log,
204d00dff88SPavel Labath               "%*sFrame %d did not get CFA for this frame, stopping stack walk",
205d00dff88SPavel Labath               cur_idx < 100 ? cur_idx : 100, "", cur_idx);
206d00dff88SPavel Labath     return nullptr;
207d00dff88SPavel Labath   }
208d00dff88SPavel Labath   if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa)) {
209d00dff88SPavel Labath     // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not
210d00dff88SPavel Labath     // have its (constructed) CFA aligned correctly -- don't do the abi
211d00dff88SPavel Labath     // alignment check for these.
212d00dff88SPavel Labath     if (!reg_ctx_sp->IsTrapHandlerFrame()) {
213d00dff88SPavel Labath       // See if we can find a fallback unwind plan for THIS frame.  It may be
214d00dff88SPavel Labath       // that the UnwindPlan we're using for THIS frame was bad and gave us a
215d00dff88SPavel Labath       // bad CFA. If that's not it, then see if we can change the UnwindPlan
216d00dff88SPavel Labath       // for the frame below us ("NEXT") -- see if using that other UnwindPlan
217d00dff88SPavel Labath       // gets us a better unwind state.
218d00dff88SPavel Labath       if (!reg_ctx_sp->TryFallbackUnwindPlan() ||
219d00dff88SPavel Labath           !reg_ctx_sp->GetCFA(cursor_sp->cfa) ||
220d00dff88SPavel Labath           !abi->CallFrameAddressIsValid(cursor_sp->cfa)) {
221d00dff88SPavel Labath         if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
222d00dff88SPavel Labath           // TryFallbackUnwindPlan for prev_frame succeeded and updated
223d00dff88SPavel Labath           // reg_ctx_lldb_sp field of prev_frame. However, cfa field of
224d00dff88SPavel Labath           // prev_frame still needs to be updated. Hence updating it.
225d00dff88SPavel Labath           if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
226d00dff88SPavel Labath             return nullptr;
227d00dff88SPavel Labath 
228d00dff88SPavel Labath           return GetOneMoreFrame(abi);
229d00dff88SPavel Labath         }
230d00dff88SPavel Labath 
231d00dff88SPavel Labath         LLDB_LOGF(log,
232d00dff88SPavel Labath                   "%*sFrame %d did not get a valid CFA for this frame, "
233d00dff88SPavel Labath                   "stopping stack walk",
234d00dff88SPavel Labath                   cur_idx < 100 ? cur_idx : 100, "", cur_idx);
235d00dff88SPavel Labath         return nullptr;
236d00dff88SPavel Labath       } else {
237d00dff88SPavel Labath         LLDB_LOGF(log,
238d00dff88SPavel Labath                   "%*sFrame %d had a bad CFA value but we switched the "
239d00dff88SPavel Labath                   "UnwindPlan being used and got one that looks more "
240d00dff88SPavel Labath                   "realistic.",
241d00dff88SPavel Labath                   cur_idx < 100 ? cur_idx : 100, "", cur_idx);
242d00dff88SPavel Labath       }
243d00dff88SPavel Labath     }
244d00dff88SPavel Labath   }
245d00dff88SPavel Labath   if (!reg_ctx_sp->ReadPC(cursor_sp->start_pc)) {
246d00dff88SPavel Labath     // If the RegisterContextUnwind has a fallback UnwindPlan, it will switch to
247d00dff88SPavel Labath     // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
248d00dff88SPavel Labath     // return false.
249d00dff88SPavel Labath     if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
250d00dff88SPavel Labath       // TryFallbackUnwindPlan for prev_frame succeeded and updated
251d00dff88SPavel Labath       // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
252d00dff88SPavel Labath       // still needs to be updated. Hence updating it.
253d00dff88SPavel Labath       if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
254d00dff88SPavel Labath         return nullptr;
255d00dff88SPavel Labath 
256d00dff88SPavel Labath       return GetOneMoreFrame(abi);
257d00dff88SPavel Labath     }
258d00dff88SPavel Labath 
259d00dff88SPavel Labath     LLDB_LOGF(log,
260d00dff88SPavel Labath               "%*sFrame %d did not get PC for this frame, stopping stack walk",
261d00dff88SPavel Labath               cur_idx < 100 ? cur_idx : 100, "", cur_idx);
262d00dff88SPavel Labath     return nullptr;
263d00dff88SPavel Labath   }
264d00dff88SPavel Labath   if (abi && !abi->CodeAddressIsValid(cursor_sp->start_pc)) {
265d00dff88SPavel Labath     // If the RegisterContextUnwind has a fallback UnwindPlan, it will switch to
266d00dff88SPavel Labath     // that and return true.  Subsequent calls to TryFallbackUnwindPlan() will
267d00dff88SPavel Labath     // return false.
268d00dff88SPavel Labath     if (prev_frame->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
269d00dff88SPavel Labath       // TryFallbackUnwindPlan for prev_frame succeeded and updated
270d00dff88SPavel Labath       // reg_ctx_lldb_sp field of prev_frame. However, cfa field of prev_frame
271d00dff88SPavel Labath       // still needs to be updated. Hence updating it.
272d00dff88SPavel Labath       if (!(prev_frame->reg_ctx_lldb_sp->GetCFA(prev_frame->cfa)))
273d00dff88SPavel Labath         return nullptr;
274d00dff88SPavel Labath 
275d00dff88SPavel Labath       return GetOneMoreFrame(abi);
276d00dff88SPavel Labath     }
277d00dff88SPavel Labath 
278d00dff88SPavel Labath     LLDB_LOGF(log, "%*sFrame %d did not get a valid PC, stopping stack walk",
279d00dff88SPavel Labath               cur_idx < 100 ? cur_idx : 100, "", cur_idx);
280d00dff88SPavel Labath     return nullptr;
281d00dff88SPavel Labath   }
282d00dff88SPavel Labath   // Infinite loop where the current cursor is the same as the previous one...
283d00dff88SPavel Labath   if (prev_frame->start_pc == cursor_sp->start_pc &&
284d00dff88SPavel Labath       prev_frame->cfa == cursor_sp->cfa) {
285d00dff88SPavel Labath     LLDB_LOGF(log,
286d00dff88SPavel Labath               "th%d pc of this frame is the same as the previous frame and "
287d00dff88SPavel Labath               "CFAs for both frames are identical -- stopping unwind",
288d00dff88SPavel Labath               m_thread.GetIndexID());
289d00dff88SPavel Labath     return nullptr;
290d00dff88SPavel Labath   }
291d00dff88SPavel Labath 
292d00dff88SPavel Labath   cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
293d00dff88SPavel Labath   return cursor_sp;
294d00dff88SPavel Labath }
295d00dff88SPavel Labath 
UpdateUnwindPlanForFirstFrameIfInvalid(ABI * abi)296d00dff88SPavel Labath void UnwindLLDB::UpdateUnwindPlanForFirstFrameIfInvalid(ABI *abi) {
297d00dff88SPavel Labath   // This function is called for First Frame only.
298d00dff88SPavel Labath   assert(m_frames.size() == 1 && "No. of cursor frames are not 1");
299d00dff88SPavel Labath 
300d00dff88SPavel Labath   bool old_m_unwind_complete = m_unwind_complete;
301d00dff88SPavel Labath   CursorSP old_m_candidate_frame = m_candidate_frame;
302d00dff88SPavel Labath 
303d00dff88SPavel Labath   // Try to unwind 2 more frames using the Unwinder. It uses Full UnwindPlan
304d00dff88SPavel Labath   // and if Full UnwindPlan fails, then uses FallBack UnwindPlan. Also update
305d00dff88SPavel Labath   // the cfa of Frame 0 (if required).
306d00dff88SPavel Labath   AddOneMoreFrame(abi);
307d00dff88SPavel Labath 
308d00dff88SPavel Labath   // Remove all the frames added by above function as the purpose of using
309d00dff88SPavel Labath   // above function was just to check whether Unwinder of Frame 0 works or not.
310d00dff88SPavel Labath   for (uint32_t i = 1; i < m_frames.size(); i++)
311d00dff88SPavel Labath     m_frames.pop_back();
312d00dff88SPavel Labath 
313d00dff88SPavel Labath   // Restore status after calling AddOneMoreFrame
314d00dff88SPavel Labath   m_unwind_complete = old_m_unwind_complete;
315d00dff88SPavel Labath   m_candidate_frame = old_m_candidate_frame;
316d00dff88SPavel Labath }
317d00dff88SPavel Labath 
AddOneMoreFrame(ABI * abi)318d00dff88SPavel Labath bool UnwindLLDB::AddOneMoreFrame(ABI *abi) {
319a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Unwind);
320d00dff88SPavel Labath 
321d00dff88SPavel Labath   // Frame zero is a little different
322d00dff88SPavel Labath   if (m_frames.empty())
323d00dff88SPavel Labath     return false;
324d00dff88SPavel Labath 
325d00dff88SPavel Labath   // If we've already gotten to the end of the stack, don't bother to try
326d00dff88SPavel Labath   // again...
327d00dff88SPavel Labath   if (m_unwind_complete)
328d00dff88SPavel Labath     return false;
329d00dff88SPavel Labath 
330d00dff88SPavel Labath   CursorSP new_frame = m_candidate_frame;
331d00dff88SPavel Labath   if (new_frame == nullptr)
332d00dff88SPavel Labath     new_frame = GetOneMoreFrame(abi);
333d00dff88SPavel Labath 
334d00dff88SPavel Labath   if (new_frame == nullptr) {
335d00dff88SPavel Labath     LLDB_LOGF(log, "th%d Unwind of this thread is complete.",
336d00dff88SPavel Labath               m_thread.GetIndexID());
337d00dff88SPavel Labath     m_unwind_complete = true;
338d00dff88SPavel Labath     return false;
339d00dff88SPavel Labath   }
340d00dff88SPavel Labath 
341d00dff88SPavel Labath   m_frames.push_back(new_frame);
342d00dff88SPavel Labath 
343d00dff88SPavel Labath   // If we can get one more frame further then accept that we get back a
344d00dff88SPavel Labath   // correct frame.
345d00dff88SPavel Labath   m_candidate_frame = GetOneMoreFrame(abi);
346d00dff88SPavel Labath   if (m_candidate_frame)
347d00dff88SPavel Labath     return true;
348d00dff88SPavel Labath 
349d00dff88SPavel Labath   // We can't go further from the frame returned by GetOneMore frame. Lets try
350d00dff88SPavel Labath   // to get a different frame with using the fallback unwind plan.
351d00dff88SPavel Labath   if (!m_frames[m_frames.size() - 2]
352d00dff88SPavel Labath            ->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) {
353d00dff88SPavel Labath     // We don't have a valid fallback unwind plan. Accept the frame as it is.
354d00dff88SPavel Labath     // This is a valid situation when we are at the bottom of the stack.
355d00dff88SPavel Labath     return true;
356d00dff88SPavel Labath   }
357d00dff88SPavel Labath 
358d00dff88SPavel Labath   // Remove the possibly incorrect frame from the frame list and try to add a
359d00dff88SPavel Labath   // different one with the newly selected fallback unwind plan.
360d00dff88SPavel Labath   m_frames.pop_back();
361d00dff88SPavel Labath   CursorSP new_frame_v2 = GetOneMoreFrame(abi);
362d00dff88SPavel Labath   if (new_frame_v2 == nullptr) {
363d00dff88SPavel Labath     // We haven't got a new frame from the fallback unwind plan. Accept the
364d00dff88SPavel Labath     // frame from the original unwind plan. This is a valid situation when we
365d00dff88SPavel Labath     // are at the bottom of the stack.
366d00dff88SPavel Labath     m_frames.push_back(new_frame);
367d00dff88SPavel Labath     return true;
368d00dff88SPavel Labath   }
369d00dff88SPavel Labath 
370d00dff88SPavel Labath   // Push the new frame to the list and try to continue from this frame. If we
371d00dff88SPavel Labath   // can get a new frame then accept it as the correct one.
372d00dff88SPavel Labath   m_frames.push_back(new_frame_v2);
373d00dff88SPavel Labath   m_candidate_frame = GetOneMoreFrame(abi);
374d00dff88SPavel Labath   if (m_candidate_frame) {
375d00dff88SPavel Labath     // If control reached here then TryFallbackUnwindPlan had succeeded for
376d00dff88SPavel Labath     // Cursor::m_frames[m_frames.size() - 2]. It also succeeded to Unwind next
377d00dff88SPavel Labath     // 2 frames i.e. m_frames[m_frames.size() - 1] and a frame after that. For
378d00dff88SPavel Labath     // Cursor::m_frames[m_frames.size() - 2], reg_ctx_lldb_sp field was already
379d00dff88SPavel Labath     // updated during TryFallbackUnwindPlan call above. However, cfa field
380d00dff88SPavel Labath     // still needs to be updated. Hence updating it here and then returning.
381d00dff88SPavel Labath     return m_frames[m_frames.size() - 2]->reg_ctx_lldb_sp->GetCFA(
382d00dff88SPavel Labath         m_frames[m_frames.size() - 2]->cfa);
383d00dff88SPavel Labath   }
384d00dff88SPavel Labath 
385d00dff88SPavel Labath   // The new frame hasn't helped in unwinding. Fall back to the original one as
386d00dff88SPavel Labath   // the default unwind plan is usually more reliable then the fallback one.
387d00dff88SPavel Labath   m_frames.pop_back();
388d00dff88SPavel Labath   m_frames.push_back(new_frame);
389d00dff88SPavel Labath   return true;
390d00dff88SPavel Labath }
391d00dff88SPavel Labath 
DoGetFrameInfoAtIndex(uint32_t idx,addr_t & cfa,addr_t & pc,bool & behaves_like_zeroth_frame)392d00dff88SPavel Labath bool UnwindLLDB::DoGetFrameInfoAtIndex(uint32_t idx, addr_t &cfa, addr_t &pc,
393d00dff88SPavel Labath                                        bool &behaves_like_zeroth_frame) {
394d00dff88SPavel Labath   if (m_frames.size() == 0) {
395d00dff88SPavel Labath     if (!AddFirstFrame())
396d00dff88SPavel Labath       return false;
397d00dff88SPavel Labath   }
398d00dff88SPavel Labath 
399d00dff88SPavel Labath   ProcessSP process_sp(m_thread.GetProcess());
400d00dff88SPavel Labath   ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
401d00dff88SPavel Labath 
402d00dff88SPavel Labath   while (idx >= m_frames.size() && AddOneMoreFrame(abi))
403d00dff88SPavel Labath     ;
404d00dff88SPavel Labath 
405d00dff88SPavel Labath   if (idx < m_frames.size()) {
406d00dff88SPavel Labath     cfa = m_frames[idx]->cfa;
407d00dff88SPavel Labath     pc = m_frames[idx]->start_pc;
408d00dff88SPavel Labath     if (idx == 0) {
409d00dff88SPavel Labath       // Frame zero always behaves like it.
410d00dff88SPavel Labath       behaves_like_zeroth_frame = true;
411d00dff88SPavel Labath     } else if (m_frames[idx - 1]->reg_ctx_lldb_sp->IsTrapHandlerFrame()) {
412d00dff88SPavel Labath       // This could be an asynchronous signal, thus the
413d00dff88SPavel Labath       // pc might point to the interrupted instruction rather
414d00dff88SPavel Labath       // than a post-call instruction
415d00dff88SPavel Labath       behaves_like_zeroth_frame = true;
416d00dff88SPavel Labath     } else if (m_frames[idx]->reg_ctx_lldb_sp->IsTrapHandlerFrame()) {
417d00dff88SPavel Labath       // This frame may result from signal processing installing
418d00dff88SPavel Labath       // a pointer to the first byte of a signal-return trampoline
419d00dff88SPavel Labath       // in the return address slot of the frame below, so this
420d00dff88SPavel Labath       // too behaves like the zeroth frame (i.e. the pc might not
421d00dff88SPavel Labath       // be pointing just past a call in it)
422d00dff88SPavel Labath       behaves_like_zeroth_frame = true;
423266bb78fSJason Molenda     } else if (m_frames[idx]->reg_ctx_lldb_sp->BehavesLikeZerothFrame()) {
424266bb78fSJason Molenda       behaves_like_zeroth_frame = true;
425d00dff88SPavel Labath     } else {
426d00dff88SPavel Labath       behaves_like_zeroth_frame = false;
427d00dff88SPavel Labath     }
428d00dff88SPavel Labath     return true;
429d00dff88SPavel Labath   }
430d00dff88SPavel Labath   return false;
431d00dff88SPavel Labath }
432d00dff88SPavel Labath 
433d00dff88SPavel Labath lldb::RegisterContextSP
DoCreateRegisterContextForFrame(StackFrame * frame)434d00dff88SPavel Labath UnwindLLDB::DoCreateRegisterContextForFrame(StackFrame *frame) {
435d00dff88SPavel Labath   lldb::RegisterContextSP reg_ctx_sp;
436d00dff88SPavel Labath   uint32_t idx = frame->GetConcreteFrameIndex();
437d00dff88SPavel Labath 
438d00dff88SPavel Labath   if (idx == 0) {
439d00dff88SPavel Labath     return m_thread.GetRegisterContext();
440d00dff88SPavel Labath   }
441d00dff88SPavel Labath 
442d00dff88SPavel Labath   if (m_frames.size() == 0) {
443d00dff88SPavel Labath     if (!AddFirstFrame())
444d00dff88SPavel Labath       return reg_ctx_sp;
445d00dff88SPavel Labath   }
446d00dff88SPavel Labath 
447d00dff88SPavel Labath   ProcessSP process_sp(m_thread.GetProcess());
448d00dff88SPavel Labath   ABI *abi = process_sp ? process_sp->GetABI().get() : nullptr;
449d00dff88SPavel Labath 
450d00dff88SPavel Labath   while (idx >= m_frames.size()) {
451d00dff88SPavel Labath     if (!AddOneMoreFrame(abi))
452d00dff88SPavel Labath       break;
453d00dff88SPavel Labath   }
454d00dff88SPavel Labath 
455d00dff88SPavel Labath   const uint32_t num_frames = m_frames.size();
456d00dff88SPavel Labath   if (idx < num_frames) {
457d00dff88SPavel Labath     Cursor *frame_cursor = m_frames[idx].get();
458d00dff88SPavel Labath     reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
459d00dff88SPavel Labath   }
460d00dff88SPavel Labath   return reg_ctx_sp;
461d00dff88SPavel Labath }
462d00dff88SPavel Labath 
463d00dff88SPavel Labath UnwindLLDB::RegisterContextLLDBSP
GetRegisterContextForFrameNum(uint32_t frame_num)464d00dff88SPavel Labath UnwindLLDB::GetRegisterContextForFrameNum(uint32_t frame_num) {
465d00dff88SPavel Labath   RegisterContextLLDBSP reg_ctx_sp;
466d00dff88SPavel Labath   if (frame_num < m_frames.size())
467d00dff88SPavel Labath     reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
468d00dff88SPavel Labath   return reg_ctx_sp;
469d00dff88SPavel Labath }
470d00dff88SPavel Labath 
SearchForSavedLocationForRegister(uint32_t lldb_regnum,lldb_private::UnwindLLDB::RegisterLocation & regloc,uint32_t starting_frame_num,bool pc_reg)471d00dff88SPavel Labath bool UnwindLLDB::SearchForSavedLocationForRegister(
472d00dff88SPavel Labath     uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc,
473d00dff88SPavel Labath     uint32_t starting_frame_num, bool pc_reg) {
474d00dff88SPavel Labath   int64_t frame_num = starting_frame_num;
475d00dff88SPavel Labath   if (static_cast<size_t>(frame_num) >= m_frames.size())
476d00dff88SPavel Labath     return false;
477d00dff88SPavel Labath 
478d00dff88SPavel Labath   // Never interrogate more than one level while looking for the saved pc
479d00dff88SPavel Labath   // value. If the value isn't saved by frame_num, none of the frames lower on
480d00dff88SPavel Labath   // the stack will have a useful value.
481d00dff88SPavel Labath   if (pc_reg) {
482d00dff88SPavel Labath     UnwindLLDB::RegisterSearchResult result;
483d00dff88SPavel Labath     result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister(
484d00dff88SPavel Labath         lldb_regnum, regloc);
485d00dff88SPavel Labath     return result == UnwindLLDB::RegisterSearchResult::eRegisterFound;
486d00dff88SPavel Labath   }
487d00dff88SPavel Labath   while (frame_num >= 0) {
488d00dff88SPavel Labath     UnwindLLDB::RegisterSearchResult result;
489d00dff88SPavel Labath     result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister(
490d00dff88SPavel Labath         lldb_regnum, regloc);
491d00dff88SPavel Labath 
492d00dff88SPavel Labath     // We descended down to the live register context aka stack frame 0 and are
493d00dff88SPavel Labath     // reading the value out of a live register.
494d00dff88SPavel Labath     if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound &&
495d00dff88SPavel Labath         regloc.type ==
496d00dff88SPavel Labath             UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext) {
497d00dff88SPavel Labath       return true;
498d00dff88SPavel Labath     }
499d00dff88SPavel Labath 
500d00dff88SPavel Labath     // If we have unwind instructions saying that register N is saved in
501d00dff88SPavel Labath     // register M in the middle of the stack (and N can equal M here, meaning
502d00dff88SPavel Labath     // the register was not used in this function), then change the register
503d00dff88SPavel Labath     // number we're looking for to M and keep looking for a concrete  location
504d00dff88SPavel Labath     // down the stack, or an actual value from a live RegisterContext at frame
505d00dff88SPavel Labath     // 0.
506d00dff88SPavel Labath     if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound &&
507d00dff88SPavel Labath         regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister &&
508d00dff88SPavel Labath         frame_num > 0) {
509d00dff88SPavel Labath       result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
510d00dff88SPavel Labath       lldb_regnum = regloc.location.register_number;
511d00dff88SPavel Labath     }
512d00dff88SPavel Labath 
513d00dff88SPavel Labath     if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
514d00dff88SPavel Labath       return true;
515d00dff88SPavel Labath     if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)
516d00dff88SPavel Labath       return false;
517d00dff88SPavel Labath     frame_num--;
518d00dff88SPavel Labath   }
519d00dff88SPavel Labath   return false;
520d00dff88SPavel Labath }
521