UnwindLLDB.cpp revision 258054
1//===-- UnwindLLDB.cpp -------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 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" 19 20#include "UnwindLLDB.h" 21#include "RegisterContextLLDB.h" 22 23using namespace lldb; 24using namespace lldb_private; 25 26UnwindLLDB::UnwindLLDB (Thread &thread) : 27 Unwind (thread), 28 m_frames(), 29 m_unwind_complete(false) 30{ 31} 32 33uint32_t 34UnwindLLDB::DoGetFrameCount() 35{ 36 if (!m_unwind_complete) 37 { 38//#define DEBUG_FRAME_SPEED 1 39#if DEBUG_FRAME_SPEED 40#define FRAME_COUNT 10000 41 TimeValue time_value (TimeValue::Now()); 42#endif 43 if (!AddFirstFrame ()) 44 return 0; 45 46 ProcessSP process_sp (m_thread.GetProcess()); 47 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; 48 49 while (AddOneMoreFrame (abi)) 50 { 51#if DEBUG_FRAME_SPEED 52 if ((m_frames.size() % FRAME_COUNT) == 0) 53 { 54 TimeValue now(TimeValue::Now()); 55 uint64_t delta_t = now - time_value; 56 printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n", 57 FRAME_COUNT, 58 delta_t / TimeValue::NanoSecPerSec, 59 delta_t % TimeValue::NanoSecPerSec, 60 (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec)); 61 time_value = now; 62 } 63#endif 64 } 65 } 66 return m_frames.size (); 67} 68 69bool 70UnwindLLDB::AddFirstFrame () 71{ 72 if (m_frames.size() > 0) 73 return true; 74 75 // First, set up the 0th (initial) frame 76 CursorSP first_cursor_sp(new Cursor ()); 77 RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread, 78 RegisterContextLLDBSP(), 79 first_cursor_sp->sctx, 80 0, *this)); 81 if (reg_ctx_sp.get() == NULL) 82 goto unwind_done; 83 84 if (!reg_ctx_sp->IsValid()) 85 goto unwind_done; 86 87 if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa)) 88 goto unwind_done; 89 90 if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc)) 91 goto unwind_done; 92 93 // Everything checks out, so release the auto pointer value and let the 94 // cursor own it in its shared pointer 95 first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp; 96 m_frames.push_back (first_cursor_sp); 97 return true; 98unwind_done: 99 m_unwind_complete = true; 100 return false; 101} 102 103// For adding a non-zero stack frame to m_frames. 104bool 105UnwindLLDB::AddOneMoreFrame (ABI *abi) 106{ 107 // If we've already gotten to the end of the stack, don't bother to try again... 108 if (m_unwind_complete) 109 return false; 110 111 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 112 CursorSP cursor_sp(new Cursor ()); 113 114 // Frame zero is a little different 115 if (m_frames.size() == 0) 116 return false; 117 118 uint32_t cur_idx = m_frames.size (); 119 RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread, 120 m_frames[cur_idx - 1]->reg_ctx_lldb_sp, 121 cursor_sp->sctx, 122 cur_idx, 123 *this)); 124 125 // We want to detect an unwind that cycles erronously and stop backtracing. 126 // Don't want this maximum unwind limit to be too low -- if you have a backtrace 127 // with an "infinitely recursing" bug, it will crash when the stack blows out 128 // and the first 35,000 frames are uninteresting - it's the top most 5 frames that 129 // you actually care about. So you can't just cap the unwind at 10,000 or something. 130 // Realistically anything over around 200,000 is going to blow out the stack space. 131 // If we're still unwinding at that point, we're probably never going to finish. 132 if (cur_idx > 300000) 133 { 134 if (log) 135 log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.", 136 cur_idx < 100 ? cur_idx : 100, "", cur_idx); 137 goto unwind_done; 138 } 139 140 if (reg_ctx_sp.get() == NULL) 141 goto unwind_done; 142 143 if (!reg_ctx_sp->IsValid()) 144 { 145 if (log) 146 { 147 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk", 148 cur_idx < 100 ? cur_idx : 100, "", cur_idx); 149 } 150 goto unwind_done; 151 } 152 if (!reg_ctx_sp->GetCFA (cursor_sp->cfa)) 153 { 154 if (log) 155 { 156 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk", 157 cur_idx < 100 ? cur_idx : 100, "", cur_idx); 158 } 159 goto unwind_done; 160 } 161 if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa)) 162 { 163 if (log) 164 { 165 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk", 166 cur_idx < 100 ? cur_idx : 100, "", cur_idx); 167 } 168 goto unwind_done; 169 } 170 if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc)) 171 { 172 if (log) 173 { 174 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk", 175 cur_idx < 100 ? cur_idx : 100, "", cur_idx); 176 } 177 goto unwind_done; 178 } 179 if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc)) 180 { 181 if (log) 182 { 183 log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk", 184 cur_idx < 100 ? cur_idx : 100, "", cur_idx); 185 } 186 goto unwind_done; 187 } 188 189 cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp; 190 m_frames.push_back (cursor_sp); 191 return true; 192 193unwind_done: 194 m_unwind_complete = true; 195 return false; 196} 197 198bool 199UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) 200{ 201 if (m_frames.size() == 0) 202 { 203 if (!AddFirstFrame()) 204 return false; 205 } 206 207 ProcessSP process_sp (m_thread.GetProcess()); 208 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; 209 210 while (idx >= m_frames.size() && AddOneMoreFrame (abi)) 211 ; 212 213 if (idx < m_frames.size ()) 214 { 215 cfa = m_frames[idx]->cfa; 216 pc = m_frames[idx]->start_pc; 217 return true; 218 } 219 return false; 220} 221 222lldb::RegisterContextSP 223UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame) 224{ 225 lldb::RegisterContextSP reg_ctx_sp; 226 uint32_t idx = frame->GetConcreteFrameIndex (); 227 228 if (idx == 0) 229 { 230 return m_thread.GetRegisterContext(); 231 } 232 233 if (m_frames.size() == 0) 234 { 235 if (!AddFirstFrame()) 236 return reg_ctx_sp; 237 } 238 239 ProcessSP process_sp (m_thread.GetProcess()); 240 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; 241 242 while (idx >= m_frames.size()) 243 { 244 if (!AddOneMoreFrame (abi)) 245 break; 246 } 247 248 const uint32_t num_frames = m_frames.size(); 249 if (idx < num_frames) 250 { 251 Cursor *frame_cursor = m_frames[idx].get(); 252 reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp; 253 } 254 return reg_ctx_sp; 255} 256 257UnwindLLDB::RegisterContextLLDBSP 258UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num) 259{ 260 RegisterContextLLDBSP reg_ctx_sp; 261 if (frame_num < m_frames.size()) 262 reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp; 263 return reg_ctx_sp; 264} 265 266bool 267UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc, uint32_t starting_frame_num, bool pc_reg) 268{ 269 int64_t frame_num = starting_frame_num; 270 if (frame_num >= m_frames.size()) 271 return false; 272 273 // Never interrogate more than one level while looking for the saved pc value. If the value 274 // isn't saved by frame_num, none of the frames lower on the stack will have a useful value. 275 if (pc_reg) 276 { 277 UnwindLLDB::RegisterSearchResult result; 278 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc); 279 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound) 280 return true; 281 else 282 return false; 283 } 284 while (frame_num >= 0) 285 { 286 UnwindLLDB::RegisterSearchResult result; 287 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc); 288 289 // If we have unwind instructions saying that register N is saved in register M in the middle of 290 // the stack (and N can equal M here, meaning the register was not used in this function), then 291 // change the register number we're looking for to M and keep looking for a concrete location 292 // down the stack, or an actual value from a live RegisterContext at frame 0. 293 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound 294 && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister 295 && frame_num > 0) 296 { 297 result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 298 lldb_regnum = regloc.location.register_number; 299 } 300 301 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound) 302 return true; 303 if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile) 304 return false; 305 frame_num--; 306 } 307 return false; 308} 309