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