1 //===-- StackFrameList.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 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/Target/StackFrameList.h" 15 #include "lldb/Breakpoint/BreakpointLocation.h" 16 #include "lldb/Breakpoint/Breakpoint.h" 17 #include "lldb/Core/Log.h" 18 #include "lldb/Core/StreamFile.h" 19 #include "lldb/Core/SourceManager.h" 20 #include "lldb/Symbol/Block.h" 21 #include "lldb/Symbol/Function.h" 22 #include "lldb/Symbol/Symbol.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/RegisterContext.h" 25 #include "lldb/Target/StackFrame.h" 26 #include "lldb/Target/StopInfo.h" 27 #include "lldb/Target/Target.h" 28 #include "lldb/Target/Thread.h" 29 #include "lldb/Target/Unwind.h" 30 31 //#define DEBUG_STACK_FRAMES 1 32 33 using namespace lldb; 34 using namespace lldb_private; 35 36 //---------------------------------------------------------------------- 37 // StackFrameList constructor 38 //---------------------------------------------------------------------- 39 StackFrameList::StackFrameList 40 ( 41 Thread &thread, 42 const lldb::StackFrameListSP &prev_frames_sp, 43 bool show_inline_frames 44 ) : 45 m_thread (thread), 46 m_prev_frames_sp (prev_frames_sp), 47 m_mutex (Mutex::eMutexTypeRecursive), 48 m_frames (), 49 m_selected_frame_idx (0), 50 m_concrete_frames_fetched (0), 51 m_current_inlined_depth (UINT32_MAX), 52 m_current_inlined_pc (LLDB_INVALID_ADDRESS), 53 m_show_inlined_frames (show_inline_frames) 54 { 55 if (prev_frames_sp) 56 { 57 m_current_inlined_depth = prev_frames_sp->m_current_inlined_depth; 58 m_current_inlined_pc = prev_frames_sp->m_current_inlined_pc; 59 } 60 } 61 62 StackFrameList::~StackFrameList() 63 { 64 // Call clear since this takes a lock and clears the stack frame list 65 // in case another thread is currently using this stack frame list 66 Clear(); 67 } 68 69 void 70 StackFrameList::CalculateCurrentInlinedDepth() 71 { 72 uint32_t cur_inlined_depth = GetCurrentInlinedDepth(); 73 if (cur_inlined_depth == UINT32_MAX) 74 { 75 ResetCurrentInlinedDepth(); 76 } 77 } 78 79 uint32_t 80 StackFrameList::GetCurrentInlinedDepth () 81 { 82 if (m_show_inlined_frames && m_current_inlined_pc != LLDB_INVALID_ADDRESS) 83 { 84 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC(); 85 if (cur_pc != m_current_inlined_pc) 86 { 87 m_current_inlined_pc = LLDB_INVALID_ADDRESS; 88 m_current_inlined_depth = UINT32_MAX; 89 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 90 if (log && log->GetVerbose()) 91 log->Printf ("GetCurrentInlinedDepth: invalidating current inlined depth.\n"); 92 } 93 return m_current_inlined_depth; 94 } 95 else 96 { 97 return UINT32_MAX; 98 } 99 } 100 101 void 102 StackFrameList::ResetCurrentInlinedDepth () 103 { 104 Mutex::Locker locker (m_mutex); 105 106 if (m_show_inlined_frames) 107 { 108 GetFramesUpTo(0); 109 if (m_frames.empty()) 110 return; 111 if (!m_frames[0]->IsInlined()) 112 { 113 m_current_inlined_depth = UINT32_MAX; 114 m_current_inlined_pc = LLDB_INVALID_ADDRESS; 115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 116 if (log && log->GetVerbose()) 117 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n"); 118 } 119 else 120 { 121 // We only need to do something special about inlined blocks when we 122 // are at the beginning of an inlined function: 123 // FIXME: We probably also have to do something special if the PC is at the END 124 // of an inlined function, which coincides with the end of either its containing 125 // function or another inlined function. 126 127 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); 128 Block *block_ptr = m_frames[0]->GetFrameBlock(); 129 if (block_ptr) 130 { 131 Address pc_as_address; 132 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget())); 133 AddressRange containing_range; 134 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range)) 135 { 136 if (pc_as_address == containing_range.GetBaseAddress()) 137 { 138 // If we got here because of a breakpoint hit, then set the inlined depth depending on where 139 // the breakpoint was set. 140 // If we got here because of a crash, then set the inlined depth to the deepest most block. 141 // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the 142 // containing frame of the whole set of nested inlines, so the user can then "virtually" 143 // step into the frames one by one, or next over the whole mess. 144 // Note: We don't have to handle being somewhere in the middle of the stack here, since 145 // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set. 146 StopInfoSP stop_info_sp = m_thread.GetStopInfo(); 147 if (stop_info_sp) 148 { 149 switch (stop_info_sp->GetStopReason()) 150 { 151 case eStopReasonWatchpoint: 152 case eStopReasonException: 153 case eStopReasonExec: 154 case eStopReasonSignal: 155 // In all these cases we want to stop in the deepest most frame. 156 m_current_inlined_pc = curr_pc; 157 m_current_inlined_depth = 0; 158 break; 159 case eStopReasonBreakpoint: 160 { 161 // FIXME: Figure out what this break point is doing, and set the inline depth 162 // appropriately. Be careful to take into account breakpoints that implement 163 // step over prologue, since that should do the default calculation. 164 // For now, if the breakpoints corresponding to this hit are all internal, 165 // I set the stop location to the top of the inlined stack, since that will make 166 // things like stepping over prologues work right. But if there are any non-internal 167 // breakpoints I do to the bottom of the stack, since that was the old behavior. 168 uint32_t bp_site_id = stop_info_sp->GetValue(); 169 BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id)); 170 bool all_internal = true; 171 if (bp_site_sp) 172 { 173 uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 174 for (uint32_t i = 0; i < num_owners; i++) 175 { 176 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 177 if (!bp_ref.IsInternal()) 178 { 179 all_internal = false; 180 } 181 } 182 } 183 if (!all_internal) 184 { 185 m_current_inlined_pc = curr_pc; 186 m_current_inlined_depth = 0; 187 break; 188 } 189 } 190 LLVM_FALLTHROUGH; 191 default: 192 { 193 // Otherwise, we should set ourselves at the container of the inlining, so that the 194 // user can descend into them. 195 // So first we check whether we have more than one inlined block sharing this PC: 196 int num_inlined_functions = 0; 197 198 for (Block *container_ptr = block_ptr->GetInlinedParent(); 199 container_ptr != nullptr; 200 container_ptr = container_ptr->GetInlinedParent()) 201 { 202 if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range)) 203 break; 204 if (pc_as_address != containing_range.GetBaseAddress()) 205 break; 206 207 num_inlined_functions++; 208 } 209 m_current_inlined_pc = curr_pc; 210 m_current_inlined_depth = num_inlined_functions + 1; 211 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 212 if (log && log->GetVerbose()) 213 log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc); 214 215 } 216 break; 217 } 218 } 219 } 220 } 221 } 222 } 223 } 224 } 225 226 bool 227 StackFrameList::DecrementCurrentInlinedDepth () 228 { 229 if (m_show_inlined_frames) 230 { 231 uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 232 if (current_inlined_depth != UINT32_MAX) 233 { 234 if (current_inlined_depth > 0) 235 { 236 m_current_inlined_depth--; 237 return true; 238 } 239 } 240 } 241 return false; 242 } 243 244 void 245 StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth) 246 { 247 m_current_inlined_depth = new_depth; 248 if (new_depth == UINT32_MAX) 249 m_current_inlined_pc = LLDB_INVALID_ADDRESS; 250 else 251 m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC(); 252 } 253 254 void 255 StackFrameList::GetFramesUpTo(uint32_t end_idx) 256 { 257 // this makes sure we do not fetch frames for an invalid thread 258 if (!m_thread.IsValid()) 259 return; 260 261 // We've already gotten more frames than asked for, or we've already finished unwinding, return. 262 if (m_frames.size() > end_idx || GetAllFramesFetched()) 263 return; 264 265 Unwind *unwinder = m_thread.GetUnwinder (); 266 267 if (m_show_inlined_frames) 268 { 269 #if defined (DEBUG_STACK_FRAMES) 270 StreamFile s(stdout, false); 271 #endif 272 // If we are hiding some frames from the outside world, we need to add those onto the total count of 273 // frames to fetch. However, we don't need to do that if end_idx is 0 since in that case we always 274 // get the first concrete frame and all the inlined frames below it... And of course, if end_idx is 275 // UINT32_MAX that means get all, so just do that... 276 277 uint32_t inlined_depth = 0; 278 if (end_idx > 0 && end_idx != UINT32_MAX) 279 { 280 inlined_depth = GetCurrentInlinedDepth(); 281 if (inlined_depth != UINT32_MAX) 282 { 283 if (end_idx > 0) 284 end_idx += inlined_depth; 285 } 286 } 287 288 StackFrameSP unwind_frame_sp; 289 do 290 { 291 uint32_t idx = m_concrete_frames_fetched++; 292 lldb::addr_t pc = LLDB_INVALID_ADDRESS; 293 lldb::addr_t cfa = LLDB_INVALID_ADDRESS; 294 if (idx == 0) 295 { 296 // We might have already created frame zero, only create it 297 // if we need to 298 if (m_frames.empty()) 299 { 300 RegisterContextSP reg_ctx_sp (m_thread.GetRegisterContext()); 301 302 if (reg_ctx_sp) 303 { 304 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 305 // There shouldn't be any way not to get the frame info for frame 0. 306 // But if the unwinder can't make one, lets make one by hand with the 307 // SP as the CFA and see if that gets any further. 308 if (!success) 309 { 310 cfa = reg_ctx_sp->GetSP(); 311 pc = reg_ctx_sp->GetPC(); 312 } 313 314 unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), 315 m_frames.size(), 316 idx, 317 reg_ctx_sp, 318 cfa, 319 pc, 320 nullptr)); 321 m_frames.push_back (unwind_frame_sp); 322 } 323 } 324 else 325 { 326 unwind_frame_sp = m_frames.front(); 327 cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 328 } 329 } 330 else 331 { 332 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 333 if (!success) 334 { 335 // We've gotten to the end of the stack. 336 SetAllFramesFetched(); 337 break; 338 } 339 const bool cfa_is_valid = true; 340 const bool stop_id_is_valid = false; 341 const bool is_history_frame = false; 342 unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid, pc, 343 0, stop_id_is_valid, is_history_frame, nullptr)); 344 m_frames.push_back (unwind_frame_sp); 345 } 346 347 assert(unwind_frame_sp); 348 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction); 349 Block *unwind_block = unwind_sc.block; 350 if (unwind_block) 351 { 352 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress()); 353 TargetSP target_sp = m_thread.CalculateTarget(); 354 // Be sure to adjust the frame address to match the address 355 // that was used to lookup the symbol context above. If we are 356 // in the first concrete frame, then we lookup using the current 357 // address, else we decrement the address by one to get the correct 358 // location. 359 if (idx > 0) 360 { 361 if (curr_frame_address.GetOffset() == 0) 362 { 363 // If curr_frame_address points to the first address in a section then after 364 // adjustment it will point to an other section. In that case resolve the 365 // address again to the correct section plus offset form. 366 addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(target_sp.get(), eAddressClassCode); 367 curr_frame_address.SetOpcodeLoadAddress(load_addr - 1, target_sp.get(), eAddressClassCode); 368 } 369 else 370 { 371 curr_frame_address.Slide(-1); 372 } 373 } 374 375 SymbolContext next_frame_sc; 376 Address next_frame_address; 377 378 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address)) 379 { 380 next_frame_sc.line_entry.ApplyFileMappings(target_sp); 381 StackFrameSP frame_sp(new StackFrame(m_thread.shared_from_this(), 382 m_frames.size(), 383 idx, 384 unwind_frame_sp->GetRegisterContextSP (), 385 cfa, 386 next_frame_address, 387 &next_frame_sc)); 388 389 m_frames.push_back (frame_sp); 390 unwind_sc = next_frame_sc; 391 curr_frame_address = next_frame_address; 392 } 393 } 394 } while (m_frames.size() - 1 < end_idx); 395 396 // Don't try to merge till you've calculated all the frames in this stack. 397 if (GetAllFramesFetched() && m_prev_frames_sp) 398 { 399 StackFrameList *prev_frames = m_prev_frames_sp.get(); 400 StackFrameList *curr_frames = this; 401 402 //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth; 403 //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc; 404 //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc); 405 406 #if defined (DEBUG_STACK_FRAMES) 407 s.PutCString("\nprev_frames:\n"); 408 prev_frames->Dump (&s); 409 s.PutCString("\ncurr_frames:\n"); 410 curr_frames->Dump (&s); 411 s.EOL(); 412 #endif 413 size_t curr_frame_num, prev_frame_num; 414 415 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size(); 416 curr_frame_num > 0 && prev_frame_num > 0; 417 --curr_frame_num, --prev_frame_num) 418 { 419 const size_t curr_frame_idx = curr_frame_num-1; 420 const size_t prev_frame_idx = prev_frame_num-1; 421 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]); 422 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]); 423 424 #if defined (DEBUG_STACK_FRAMES) 425 s.Printf("\n\nCurr frame #%u ", curr_frame_idx); 426 if (curr_frame_sp) 427 curr_frame_sp->Dump (&s, true, false); 428 else 429 s.PutCString("NULL"); 430 s.Printf("\nPrev frame #%u ", prev_frame_idx); 431 if (prev_frame_sp) 432 prev_frame_sp->Dump (&s, true, false); 433 else 434 s.PutCString("NULL"); 435 #endif 436 437 StackFrame *curr_frame = curr_frame_sp.get(); 438 StackFrame *prev_frame = prev_frame_sp.get(); 439 440 if (curr_frame == nullptr || prev_frame == nullptr) 441 break; 442 443 // Check the stack ID to make sure they are equal 444 if (curr_frame->GetStackID() != prev_frame->GetStackID()) 445 break; 446 447 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame); 448 // Now copy the fixed up previous frame into the current frames 449 // so the pointer doesn't change 450 m_frames[curr_frame_idx] = prev_frame_sp; 451 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame); 452 453 #if defined (DEBUG_STACK_FRAMES) 454 s.Printf("\n Copying previous frame to current frame"); 455 #endif 456 } 457 // We are done with the old stack frame list, we can release it now 458 m_prev_frames_sp.reset(); 459 } 460 461 #if defined (DEBUG_STACK_FRAMES) 462 s.PutCString("\n\nNew frames:\n"); 463 Dump (&s); 464 s.EOL(); 465 #endif 466 } 467 else 468 { 469 if (end_idx < m_concrete_frames_fetched) 470 return; 471 472 if (unwinder) 473 { 474 uint32_t num_frames = unwinder->GetFramesUpTo(end_idx); 475 if (num_frames <= end_idx + 1) 476 { 477 //Done unwinding. 478 m_concrete_frames_fetched = UINT32_MAX; 479 } 480 m_frames.resize(num_frames); 481 } 482 } 483 } 484 485 uint32_t 486 StackFrameList::GetNumFrames (bool can_create) 487 { 488 Mutex::Locker locker (m_mutex); 489 490 if (can_create) 491 GetFramesUpTo (UINT32_MAX); 492 493 uint32_t inlined_depth = GetCurrentInlinedDepth(); 494 if (inlined_depth == UINT32_MAX) 495 return m_frames.size(); 496 else 497 return m_frames.size() - inlined_depth; 498 } 499 500 void 501 StackFrameList::Dump (Stream *s) 502 { 503 if (s == nullptr) 504 return; 505 Mutex::Locker locker (m_mutex); 506 507 const_iterator pos, begin = m_frames.begin(), end = m_frames.end(); 508 for (pos = begin; pos != end; ++pos) 509 { 510 StackFrame *frame = (*pos).get(); 511 s->Printf("%p: ", static_cast<void*>(frame)); 512 if (frame) 513 { 514 frame->GetStackID().Dump (s); 515 frame->DumpUsingSettingsFormat (s); 516 } 517 else 518 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos)); 519 s->EOL(); 520 } 521 s->EOL(); 522 } 523 524 StackFrameSP 525 StackFrameList::GetFrameAtIndex (uint32_t idx) 526 { 527 StackFrameSP frame_sp; 528 Mutex::Locker locker (m_mutex); 529 uint32_t original_idx = idx; 530 531 uint32_t inlined_depth = GetCurrentInlinedDepth(); 532 if (inlined_depth != UINT32_MAX) 533 idx += inlined_depth; 534 535 if (idx < m_frames.size()) 536 frame_sp = m_frames[idx]; 537 538 if (frame_sp) 539 return frame_sp; 540 541 // GetFramesUpTo will fill m_frames with as many frames as you asked for, 542 // if there are that many. If there weren't then you asked for too many 543 // frames. 544 GetFramesUpTo (idx); 545 if (idx < m_frames.size()) 546 { 547 if (m_show_inlined_frames) 548 { 549 // When inline frames are enabled we actually create all the frames in GetFramesUpTo. 550 frame_sp = m_frames[idx]; 551 } 552 else 553 { 554 Unwind *unwinder = m_thread.GetUnwinder (); 555 if (unwinder) 556 { 557 addr_t pc, cfa; 558 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) 559 { 560 const bool cfa_is_valid = true; 561 const bool stop_id_is_valid = false; 562 const bool is_history_frame = false; 563 frame_sp.reset(new StackFrame(m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0, 564 stop_id_is_valid, is_history_frame, nullptr)); 565 566 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function; 567 if (function) 568 { 569 // When we aren't showing inline functions we always use 570 // the top most function block as the scope. 571 frame_sp->SetSymbolContextScope (&function->GetBlock(false)); 572 } 573 else 574 { 575 // Set the symbol scope from the symbol regardless if it is nullptr or valid. 576 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol); 577 } 578 SetFrameAtIndex(idx, frame_sp); 579 } 580 } 581 } 582 } 583 else if (original_idx == 0) 584 { 585 // There should ALWAYS be a frame at index 0. If something went wrong with the CurrentInlinedDepth such that 586 // there weren't as many frames as we thought taking that into account, then reset the current inlined depth 587 // and return the real zeroth frame. 588 if (m_frames.empty()) 589 { 590 // Why do we have a thread with zero frames, that should not ever happen... 591 if (m_thread.IsValid()) 592 assert ("A valid thread has no frames."); 593 } 594 else 595 { 596 ResetCurrentInlinedDepth(); 597 frame_sp = m_frames[original_idx]; 598 } 599 } 600 601 return frame_sp; 602 } 603 604 StackFrameSP 605 StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 606 { 607 // First try assuming the unwind index is the same as the frame index. The 608 // unwind index is always greater than or equal to the frame index, so it 609 // is a good place to start. If we have inlined frames we might have 5 610 // concrete frames (frame unwind indexes go from 0-4), but we might have 15 611 // frames after we make all the inlined frames. Most of the time the unwind 612 // frame index (or the concrete frame index) is the same as the frame index. 613 uint32_t frame_idx = unwind_idx; 614 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx)); 615 while (frame_sp) 616 { 617 if (frame_sp->GetFrameIndex() == unwind_idx) 618 break; 619 frame_sp = GetFrameAtIndex (++frame_idx); 620 } 621 return frame_sp; 622 } 623 624 static bool 625 CompareStackID (const StackFrameSP &stack_sp, const StackID &stack_id) 626 { 627 return stack_sp->GetStackID() < stack_id; 628 } 629 630 StackFrameSP 631 StackFrameList::GetFrameWithStackID (const StackID &stack_id) 632 { 633 StackFrameSP frame_sp; 634 635 if (stack_id.IsValid()) 636 { 637 Mutex::Locker locker (m_mutex); 638 uint32_t frame_idx = 0; 639 // Do a binary search in case the stack frame is already in our cache 640 collection::const_iterator begin = m_frames.begin(); 641 collection::const_iterator end = m_frames.end(); 642 if (begin != end) 643 { 644 collection::const_iterator pos = std::lower_bound (begin, end, stack_id, CompareStackID); 645 if (pos != end) 646 { 647 if ((*pos)->GetStackID() == stack_id) 648 return *pos; 649 } 650 651 // if (m_frames.back()->GetStackID() < stack_id) 652 // frame_idx = m_frames.size(); 653 } 654 do 655 { 656 frame_sp = GetFrameAtIndex (frame_idx); 657 if (frame_sp && frame_sp->GetStackID() == stack_id) 658 break; 659 frame_idx++; 660 } 661 while (frame_sp); 662 } 663 return frame_sp; 664 } 665 666 bool 667 StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp) 668 { 669 if (idx >= m_frames.size()) 670 m_frames.resize(idx + 1); 671 // Make sure allocation succeeded by checking bounds again 672 if (idx < m_frames.size()) 673 { 674 m_frames[idx] = frame_sp; 675 return true; 676 } 677 return false; // resize failed, out of memory? 678 } 679 680 uint32_t 681 StackFrameList::GetSelectedFrameIndex () const 682 { 683 Mutex::Locker locker (m_mutex); 684 return m_selected_frame_idx; 685 } 686 687 uint32_t 688 StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame) 689 { 690 Mutex::Locker locker (m_mutex); 691 const_iterator pos; 692 const_iterator begin = m_frames.begin(); 693 const_iterator end = m_frames.end(); 694 m_selected_frame_idx = 0; 695 for (pos = begin; pos != end; ++pos) 696 { 697 if (pos->get() == frame) 698 { 699 m_selected_frame_idx = std::distance (begin, pos); 700 uint32_t inlined_depth = GetCurrentInlinedDepth(); 701 if (inlined_depth != UINT32_MAX) 702 m_selected_frame_idx -= inlined_depth; 703 break; 704 } 705 } 706 SetDefaultFileAndLineToSelectedFrame(); 707 return m_selected_frame_idx; 708 } 709 710 // Mark a stack frame as the current frame using the frame index 711 bool 712 StackFrameList::SetSelectedFrameByIndex (uint32_t idx) 713 { 714 Mutex::Locker locker (m_mutex); 715 StackFrameSP frame_sp (GetFrameAtIndex (idx)); 716 if (frame_sp) 717 { 718 SetSelectedFrame(frame_sp.get()); 719 return true; 720 } 721 else 722 return false; 723 } 724 725 void 726 StackFrameList::SetDefaultFileAndLineToSelectedFrame() 727 { 728 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) 729 { 730 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex())); 731 if (frame_sp) 732 { 733 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 734 if (sc.line_entry.file) 735 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file, 736 sc.line_entry.line); 737 } 738 } 739 } 740 741 // The thread has been run, reset the number stack frames to zero so we can 742 // determine how many frames we have lazily. 743 void 744 StackFrameList::Clear () 745 { 746 Mutex::Locker locker (m_mutex); 747 m_frames.clear(); 748 m_concrete_frames_fetched = 0; 749 } 750 751 void 752 StackFrameList::InvalidateFrames (uint32_t start_idx) 753 { 754 Mutex::Locker locker (m_mutex); 755 if (m_show_inlined_frames) 756 { 757 Clear(); 758 } 759 else 760 { 761 const size_t num_frames = m_frames.size(); 762 while (start_idx < num_frames) 763 { 764 m_frames[start_idx].reset(); 765 ++start_idx; 766 } 767 } 768 } 769 770 void 771 StackFrameList::Merge (std::unique_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp) 772 { 773 Mutex::Locker curr_locker(curr_ap ? &curr_ap->m_mutex : nullptr); 774 Mutex::Locker prev_locker(prev_sp ? &prev_sp->m_mutex : nullptr); 775 776 #if defined (DEBUG_STACK_FRAMES) 777 StreamFile s(stdout, false); 778 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 779 if (prev_sp) 780 prev_sp->Dump (&s); 781 else 782 s.PutCString ("NULL"); 783 s.PutCString("\nCurr:\n"); 784 if (curr_ap) 785 curr_ap->Dump (&s); 786 else 787 s.PutCString ("NULL"); 788 s.EOL(); 789 #endif 790 791 if (!curr_ap || curr_ap->GetNumFrames(false) == 0) 792 { 793 #if defined (DEBUG_STACK_FRAMES) 794 s.PutCString("No current frames, leave previous frames alone...\n"); 795 #endif 796 curr_ap.release(); 797 return; 798 } 799 800 if (!prev_sp || prev_sp->GetNumFrames(false) == 0) 801 { 802 #if defined (DEBUG_STACK_FRAMES) 803 s.PutCString("No previous frames, so use current frames...\n"); 804 #endif 805 // We either don't have any previous frames, or since we have more than 806 // one current frames it means we have all the frames and can safely 807 // replace our previous frames. 808 prev_sp.reset (curr_ap.release()); 809 return; 810 } 811 812 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false); 813 814 if (num_curr_frames > 1) 815 { 816 #if defined (DEBUG_STACK_FRAMES) 817 s.PutCString("We have more than one current frame, so use current frames...\n"); 818 #endif 819 // We have more than one current frames it means we have all the frames 820 // and can safely replace our previous frames. 821 prev_sp.reset (curr_ap.release()); 822 823 #if defined (DEBUG_STACK_FRAMES) 824 s.PutCString("\nMerged:\n"); 825 prev_sp->Dump (&s); 826 #endif 827 return; 828 } 829 830 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0)); 831 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0)); 832 StackID curr_stack_id (curr_frame_zero_sp->GetStackID()); 833 StackID prev_stack_id (prev_frame_zero_sp->GetStackID()); 834 835 #if defined (DEBUG_STACK_FRAMES) 836 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false); 837 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 838 #endif 839 840 // We have only a single current frame 841 // Our previous stack frames only had a single frame as well... 842 if (curr_stack_id == prev_stack_id) 843 { 844 #if defined (DEBUG_STACK_FRAMES) 845 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n"); 846 #endif 847 848 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp); 849 // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp); 850 // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 851 } 852 else if (curr_stack_id < prev_stack_id) 853 { 854 #if defined (DEBUG_STACK_FRAMES) 855 s.Printf("\nCurrent frame #0 has a stack ID that is less than the previous frame #0, insert current frame zero in front of previous\n"); 856 #endif 857 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp); 858 } 859 860 curr_ap.release(); 861 862 #if defined (DEBUG_STACK_FRAMES) 863 s.PutCString("\nMerged:\n"); 864 prev_sp->Dump (&s); 865 #endif 866 } 867 868 lldb::StackFrameSP 869 StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 870 { 871 const_iterator pos; 872 const_iterator begin = m_frames.begin(); 873 const_iterator end = m_frames.end(); 874 lldb::StackFrameSP ret_sp; 875 876 for (pos = begin; pos != end; ++pos) 877 { 878 if (pos->get() == stack_frame_ptr) 879 { 880 ret_sp = (*pos); 881 break; 882 } 883 } 884 return ret_sp; 885 } 886 887 size_t 888 StackFrameList::GetStatus (Stream& strm, 889 uint32_t first_frame, 890 uint32_t num_frames, 891 bool show_frame_info, 892 uint32_t num_frames_with_source, 893 const char *selected_frame_marker) 894 { 895 size_t num_frames_displayed = 0; 896 897 if (num_frames == 0) 898 return 0; 899 900 StackFrameSP frame_sp; 901 uint32_t frame_idx = 0; 902 uint32_t last_frame; 903 904 // Don't let the last frame wrap around... 905 if (num_frames == UINT32_MAX) 906 last_frame = UINT32_MAX; 907 else 908 last_frame = first_frame + num_frames; 909 910 StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 911 const char *unselected_marker = nullptr; 912 std::string buffer; 913 if (selected_frame_marker) 914 { 915 size_t len = strlen(selected_frame_marker); 916 buffer.insert(buffer.begin(), len, ' '); 917 unselected_marker = buffer.c_str(); 918 } 919 const char *marker = nullptr; 920 921 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) 922 { 923 frame_sp = GetFrameAtIndex(frame_idx); 924 if (!frame_sp) 925 break; 926 927 if (selected_frame_marker != nullptr) 928 { 929 if (frame_sp == selected_frame_sp) 930 marker = selected_frame_marker; 931 else 932 marker = unselected_marker; 933 } 934 935 if (!frame_sp->GetStatus (strm, 936 show_frame_info, 937 num_frames_with_source > (first_frame - frame_idx), marker)) 938 break; 939 ++num_frames_displayed; 940 } 941 942 strm.IndentLess(); 943 return num_frames_displayed; 944 } 945