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/ABI.h"
16 #include "lldb/Target/Thread.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/RegisterContext.h"
21 #include "UnwindLLDB.h"
22 #include "RegisterContextLLDB.h"
25 using namespace lldb_private;
27 UnwindLLDB::UnwindLLDB (Thread &thread) :
30 m_unwind_complete(false),
31 m_user_supplied_trap_handler_functions()
33 ProcessSP process_sp(thread.GetProcess());
37 process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames (args);
38 size_t count = args.GetArgumentCount();
39 for (size_t i = 0; i < count; i++)
41 const char *func_name = args.GetArgumentAtIndex(i);
42 m_user_supplied_trap_handler_functions.push_back (ConstString (func_name));
48 UnwindLLDB::DoGetFrameCount()
50 if (!m_unwind_complete)
52 //#define DEBUG_FRAME_SPEED 1
54 #define FRAME_COUNT 10000
55 TimeValue time_value (TimeValue::Now());
57 if (!AddFirstFrame ())
60 ProcessSP process_sp (m_thread.GetProcess());
61 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
63 while (AddOneMoreFrame (abi))
66 if ((m_frames.size() % FRAME_COUNT) == 0)
68 TimeValue now(TimeValue::Now());
69 uint64_t delta_t = now - time_value;
70 printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n",
72 delta_t / TimeValue::NanoSecPerSec,
73 delta_t % TimeValue::NanoSecPerSec,
74 (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec));
80 return m_frames.size ();
84 UnwindLLDB::AddFirstFrame ()
86 if (m_frames.size() > 0)
89 // First, set up the 0th (initial) frame
90 CursorSP first_cursor_sp(new Cursor ());
91 RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread,
92 RegisterContextLLDBSP(),
93 first_cursor_sp->sctx,
95 if (reg_ctx_sp.get() == NULL)
98 if (!reg_ctx_sp->IsValid())
101 if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
104 if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
107 // Everything checks out, so release the auto pointer value and let the
108 // cursor own it in its shared pointer
109 first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
110 m_frames.push_back (first_cursor_sp);
114 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
117 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
119 m_unwind_complete = true;
123 // For adding a non-zero stack frame to m_frames.
125 UnwindLLDB::AddOneMoreFrame (ABI *abi)
127 // If we've already gotten to the end of the stack, don't bother to try again...
128 if (m_unwind_complete)
131 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
132 CursorSP cursor_sp(new Cursor ());
134 // Frame zero is a little different
135 if (m_frames.size() == 0)
138 uint32_t cur_idx = m_frames.size ();
139 RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread,
140 m_frames[cur_idx - 1]->reg_ctx_lldb_sp,
145 // We want to detect an unwind that cycles erronously and stop backtracing.
146 // Don't want this maximum unwind limit to be too low -- if you have a backtrace
147 // with an "infinitely recursing" bug, it will crash when the stack blows out
148 // and the first 35,000 frames are uninteresting - it's the top most 5 frames that
149 // you actually care about. So you can't just cap the unwind at 10,000 or something.
150 // Realistically anything over around 200,000 is going to blow out the stack space.
151 // If we're still unwinding at that point, we're probably never going to finish.
152 if (cur_idx > 300000)
155 log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.",
156 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
160 if (reg_ctx_sp.get() == NULL)
162 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
163 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
164 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
166 return AddOneMoreFrame (abi);
169 log->Printf ("%*sFrame %d did not get a RegisterContext, stopping.",
170 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
174 if (!reg_ctx_sp->IsValid())
176 // We failed to get a valid RegisterContext.
177 // See if the regctx below this on the stack has a fallback unwind plan it can use.
178 // Subsequent calls to TryFallbackUnwindPlan() will return false.
179 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
181 return AddOneMoreFrame (abi);
185 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
186 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
190 if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
192 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
193 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
194 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
196 return AddOneMoreFrame (abi);
200 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
201 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
205 if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
207 // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not have
208 // its (constructed) CFA aligned correctly -- don't do the abi alignment check for
210 if (reg_ctx_sp->IsTrapHandlerFrame() == false)
212 // See if we can find a fallback unwind plan for THIS frame. It may be
213 // that the UnwindPlan we're using for THIS frame was bad and gave us a
215 // If that's not it, then see if we can change the UnwindPlan for the frame
216 // below us ("NEXT") -- see if using that other UnwindPlan gets us a better
218 if (reg_ctx_sp->TryFallbackUnwindPlan() == false
219 || reg_ctx_sp->GetCFA (cursor_sp->cfa) == false
220 || abi->CallFrameAddressIsValid(cursor_sp->cfa) == false)
222 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
224 return AddOneMoreFrame (abi);
228 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
229 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
237 log->Printf("%*sFrame %d had a bad CFA value but we switched the UnwindPlan being used and got one that looks more realistic.",
238 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
243 if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
245 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
246 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
247 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
249 return AddOneMoreFrame (abi);
253 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
254 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
258 if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
260 // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return
261 // true. Subsequent calls to TryFallbackUnwindPlan() will return false.
262 if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan())
264 return AddOneMoreFrame (abi);
268 log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
269 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
273 if (!m_frames.empty())
275 // Infinite loop where the current cursor is the same as the previous one...
276 if (m_frames.back()->start_pc == cursor_sp->start_pc && m_frames.back()->cfa == cursor_sp->cfa)
279 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());
284 cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
285 m_frames.push_back (cursor_sp);
291 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
293 m_unwind_complete = true;
298 UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
300 if (m_frames.size() == 0)
302 if (!AddFirstFrame())
306 ProcessSP process_sp (m_thread.GetProcess());
307 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
309 while (idx >= m_frames.size() && AddOneMoreFrame (abi))
312 if (idx < m_frames.size ())
314 cfa = m_frames[idx]->cfa;
315 pc = m_frames[idx]->start_pc;
321 lldb::RegisterContextSP
322 UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame)
324 lldb::RegisterContextSP reg_ctx_sp;
325 uint32_t idx = frame->GetConcreteFrameIndex ();
329 return m_thread.GetRegisterContext();
332 if (m_frames.size() == 0)
334 if (!AddFirstFrame())
338 ProcessSP process_sp (m_thread.GetProcess());
339 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
341 while (idx >= m_frames.size())
343 if (!AddOneMoreFrame (abi))
347 const uint32_t num_frames = m_frames.size();
348 if (idx < num_frames)
350 Cursor *frame_cursor = m_frames[idx].get();
351 reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
356 UnwindLLDB::RegisterContextLLDBSP
357 UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num)
359 RegisterContextLLDBSP reg_ctx_sp;
360 if (frame_num < m_frames.size())
361 reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
366 UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc, uint32_t starting_frame_num, bool pc_reg)
368 int64_t frame_num = starting_frame_num;
369 if (static_cast<size_t>(frame_num) >= m_frames.size())
372 // Never interrogate more than one level while looking for the saved pc value. If the value
373 // isn't saved by frame_num, none of the frames lower on the stack will have a useful value.
376 UnwindLLDB::RegisterSearchResult result;
377 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
378 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
383 while (frame_num >= 0)
385 UnwindLLDB::RegisterSearchResult result;
386 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
388 // We descended down to the live register context aka stack frame 0 and are reading the value
389 // out of a live register.
390 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound
391 && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext)
396 // If we have unwind instructions saying that register N is saved in register M in the middle of
397 // the stack (and N can equal M here, meaning the register was not used in this function), then
398 // change the register number we're looking for to M and keep looking for a concrete location
399 // down the stack, or an actual value from a live RegisterContext at frame 0.
400 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound
401 && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister
404 result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
405 lldb_regnum = regloc.location.register_number;
408 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
410 if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)