1 //===-- UnwindLLDB.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Module.h"
11 #include "lldb/Core/Log.h"
12 #include "lldb/Symbol/FuncUnwinders.h"
13 #include "lldb/Symbol/Function.h"
14 #include "lldb/Symbol/UnwindPlan.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/RegisterContext.h"
20 #include "UnwindLLDB.h"
21 #include "RegisterContextLLDB.h"
24 using namespace lldb_private;
26 UnwindLLDB::UnwindLLDB (Thread &thread) :
29 m_unwind_complete(false),
30 m_user_supplied_trap_handler_functions()
32 ProcessSP process_sp(thread.GetProcess());
36 process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames (args);
37 size_t count = args.GetArgumentCount();
38 for (size_t i = 0; i < count; i++)
40 const char *func_name = args.GetArgumentAtIndex(i);
41 m_user_supplied_trap_handler_functions.push_back (ConstString (func_name));
47 UnwindLLDB::DoGetFrameCount()
49 if (!m_unwind_complete)
51 //#define DEBUG_FRAME_SPEED 1
53 #define FRAME_COUNT 10000
54 TimeValue time_value (TimeValue::Now());
56 if (!AddFirstFrame ())
59 ProcessSP process_sp (m_thread.GetProcess());
60 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
62 while (AddOneMoreFrame (abi))
65 if ((m_frames.size() % FRAME_COUNT) == 0)
67 TimeValue now(TimeValue::Now());
68 uint64_t delta_t = now - time_value;
69 printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n",
71 delta_t / TimeValue::NanoSecPerSec,
72 delta_t % TimeValue::NanoSecPerSec,
73 (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec));
79 return m_frames.size ();
83 UnwindLLDB::AddFirstFrame ()
85 if (m_frames.size() > 0)
88 // First, set up the 0th (initial) frame
89 CursorSP first_cursor_sp(new Cursor ());
90 RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread,
91 RegisterContextLLDBSP(),
92 first_cursor_sp->sctx,
94 if (reg_ctx_sp.get() == NULL)
97 if (!reg_ctx_sp->IsValid())
100 if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
103 if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
106 // Everything checks out, so release the auto pointer value and let the
107 // cursor own it in its shared pointer
108 first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
109 m_frames.push_back (first_cursor_sp);
113 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
116 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
118 m_unwind_complete = true;
122 // For adding a non-zero stack frame to m_frames.
124 UnwindLLDB::AddOneMoreFrame (ABI *abi)
126 // If we've already gotten to the end of the stack, don't bother to try again...
127 if (m_unwind_complete)
130 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
131 CursorSP cursor_sp(new Cursor ());
133 // Frame zero is a little different
134 if (m_frames.size() == 0)
137 uint32_t cur_idx = m_frames.size ();
138 RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread,
139 m_frames[cur_idx - 1]->reg_ctx_lldb_sp,
144 // We want to detect an unwind that cycles erronously and stop backtracing.
145 // Don't want this maximum unwind limit to be too low -- if you have a backtrace
146 // with an "infinitely recursing" bug, it will crash when the stack blows out
147 // and the first 35,000 frames are uninteresting - it's the top most 5 frames that
148 // you actually care about. So you can't just cap the unwind at 10,000 or something.
149 // Realistically anything over around 200,000 is going to blow out the stack space.
150 // If we're still unwinding at that point, we're probably never going to finish.
151 if (cur_idx > 300000)
154 log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.",
155 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
159 if (reg_ctx_sp.get() == NULL)
161 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
162 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
163 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
165 return AddOneMoreFrame (abi);
168 log->Printf ("%*sFrame %d did not get a RegisterContext, stopping.",
169 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
173 if (!reg_ctx_sp->IsValid())
175 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
176 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
177 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
179 return AddOneMoreFrame (abi);
183 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
184 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
188 if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
190 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
191 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
192 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
194 return AddOneMoreFrame (abi);
198 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
199 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
203 if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
205 // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not have
206 // its (constructed) CFA aligned correctly -- don't do the abi alignment check for
208 if (reg_ctx_sp->IsTrapHandlerFrame() == false)
210 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
211 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
212 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
214 return AddOneMoreFrame (abi);
218 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
219 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
224 if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
226 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
227 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
228 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
230 return AddOneMoreFrame (abi);
234 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
235 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
239 if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
241 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
242 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
243 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
245 return AddOneMoreFrame (abi);
249 log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
250 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
254 if (!m_frames.empty())
256 // Infinite loop where the current cursor is the same as the previous one...
257 if (m_frames.back()->start_pc == cursor_sp->start_pc && m_frames.back()->cfa == cursor_sp->cfa)
260 log->Printf ("th%d pc of this frame is the same as the previous frame and CFAs for both frames are identical -- stopping unwind", m_thread.GetIndexID());
265 cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
266 m_frames.push_back (cursor_sp);
272 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
274 m_unwind_complete = true;
279 UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
281 if (m_frames.size() == 0)
283 if (!AddFirstFrame())
287 ProcessSP process_sp (m_thread.GetProcess());
288 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
290 while (idx >= m_frames.size() && AddOneMoreFrame (abi))
293 if (idx < m_frames.size ())
295 cfa = m_frames[idx]->cfa;
296 pc = m_frames[idx]->start_pc;
302 lldb::RegisterContextSP
303 UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame)
305 lldb::RegisterContextSP reg_ctx_sp;
306 uint32_t idx = frame->GetConcreteFrameIndex ();
310 return m_thread.GetRegisterContext();
313 if (m_frames.size() == 0)
315 if (!AddFirstFrame())
319 ProcessSP process_sp (m_thread.GetProcess());
320 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
322 while (idx >= m_frames.size())
324 if (!AddOneMoreFrame (abi))
328 const uint32_t num_frames = m_frames.size();
329 if (idx < num_frames)
331 Cursor *frame_cursor = m_frames[idx].get();
332 reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
337 UnwindLLDB::RegisterContextLLDBSP
338 UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num)
340 RegisterContextLLDBSP reg_ctx_sp;
341 if (frame_num < m_frames.size())
342 reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
347 UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc, uint32_t starting_frame_num, bool pc_reg)
349 int64_t frame_num = starting_frame_num;
350 if (frame_num >= m_frames.size())
353 // Never interrogate more than one level while looking for the saved pc value. If the value
354 // isn't saved by frame_num, none of the frames lower on the stack will have a useful value.
357 UnwindLLDB::RegisterSearchResult result;
358 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
359 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
364 while (frame_num >= 0)
366 UnwindLLDB::RegisterSearchResult result;
367 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
369 // If we have unwind instructions saying that register N is saved in register M in the middle of
370 // the stack (and N can equal M here, meaning the register was not used in this function), then
371 // change the register number we're looking for to M and keep looking for a concrete location
372 // down the stack, or an actual value from a live RegisterContext at frame 0.
373 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound
374 && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister
377 result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
378 lldb_regnum = regloc.location.register_number;
381 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
383 if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)