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 default: 191 { 192 // Otherwise, we should set ourselves at the container of the inlining, so that the 193 // user can descend into them. 194 // So first we check whether we have more than one inlined block sharing this PC: 195 int num_inlined_functions = 0; 196 197 for (Block *container_ptr = block_ptr->GetInlinedParent(); 198 container_ptr != nullptr; 199 container_ptr = container_ptr->GetInlinedParent()) 200 { 201 if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range)) 202 break; 203 if (pc_as_address != containing_range.GetBaseAddress()) 204 break; 205 206 num_inlined_functions++; 207 } 208 m_current_inlined_pc = curr_pc; 209 m_current_inlined_depth = num_inlined_functions + 1; 210 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 211 if (log && log->GetVerbose()) 212 log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc); 213 214 } 215 break; 216 } 217 } 218 } 219 } 220 } 221 } 222 } 223 } 224 225 bool 226 StackFrameList::DecrementCurrentInlinedDepth () 227 { 228 if (m_show_inlined_frames) 229 { 230 uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 231 if (current_inlined_depth != UINT32_MAX) 232 { 233 if (current_inlined_depth > 0) 234 { 235 m_current_inlined_depth--; 236 return true; 237 } 238 } 239 } 240 return false; 241 } 242 243 void 244 StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth) 245 { 246 m_current_inlined_depth = new_depth; 247 if (new_depth == UINT32_MAX) 248 m_current_inlined_pc = LLDB_INVALID_ADDRESS; 249 else 250 m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC(); 251 } 252 253 void 254 StackFrameList::GetFramesUpTo(uint32_t end_idx) 255 { 256 // this makes sure we do not fetch frames for an invalid thread 257 if (!m_thread.IsValid()) 258 return; 259 260 // We've already gotten more frames than asked for, or we've already finished unwinding, return. 261 if (m_frames.size() > end_idx || GetAllFramesFetched()) 262 return; 263 264 Unwind *unwinder = m_thread.GetUnwinder (); 265 266 if (m_show_inlined_frames) 267 { 268 #if defined (DEBUG_STACK_FRAMES) 269 StreamFile s(stdout, false); 270 #endif 271 // If we are hiding some frames from the outside world, we need to add those onto the total count of 272 // frames to fetch. However, we don't need to do that if end_idx is 0 since in that case we always 273 // get the first concrete frame and all the inlined frames below it... And of course, if end_idx is 274 // UINT32_MAX that means get all, so just do that... 275 276 uint32_t inlined_depth = 0; 277 if (end_idx > 0 && end_idx != UINT32_MAX) 278 { 279 inlined_depth = GetCurrentInlinedDepth(); 280 if (inlined_depth != UINT32_MAX) 281 { 282 if (end_idx > 0) 283 end_idx += inlined_depth; 284 } 285 } 286 287 StackFrameSP unwind_frame_sp; 288 do 289 { 290 uint32_t idx = m_concrete_frames_fetched++; 291 lldb::addr_t pc = LLDB_INVALID_ADDRESS; 292 lldb::addr_t cfa = LLDB_INVALID_ADDRESS; 293 if (idx == 0) 294 { 295 // We might have already created frame zero, only create it 296 // if we need to 297 if (m_frames.empty()) 298 { 299 RegisterContextSP reg_ctx_sp (m_thread.GetRegisterContext()); 300 301 if (reg_ctx_sp) 302 { 303 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 304 // There shouldn't be any way not to get the frame info for frame 0. 305 // But if the unwinder can't make one, lets make one by hand with the 306 // SP as the CFA and see if that gets any further. 307 if (!success) 308 { 309 cfa = reg_ctx_sp->GetSP(); 310 pc = reg_ctx_sp->GetPC(); 311 } 312 313 unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), 314 m_frames.size(), 315 idx, 316 reg_ctx_sp, 317 cfa, 318 pc, 319 nullptr)); 320 m_frames.push_back (unwind_frame_sp); 321 } 322 } 323 else 324 { 325 unwind_frame_sp = m_frames.front(); 326 cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 327 } 328 } 329 else 330 { 331 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 332 if (!success) 333 { 334 // We've gotten to the end of the stack. 335 SetAllFramesFetched(); 336 break; 337 } 338 const bool cfa_is_valid = true; 339 const bool stop_id_is_valid = false; 340 const bool is_history_frame = false; 341 unwind_frame_sp.reset(new StackFrame(m_thread.shared_from_this(), m_frames.size(), idx, cfa, cfa_is_valid, pc, 342 0, stop_id_is_valid, is_history_frame, nullptr)); 343 m_frames.push_back (unwind_frame_sp); 344 } 345 346 assert(unwind_frame_sp); 347 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction); 348 Block *unwind_block = unwind_sc.block; 349 if (unwind_block) 350 { 351 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress()); 352 // Be sure to adjust the frame address to match the address 353 // that was used to lookup the symbol context above. If we are 354 // in the first concrete frame, then we lookup using the current 355 // address, else we decrement the address by one to get the correct 356 // location. 357 if (idx > 0) 358 { 359 if (curr_frame_address.GetOffset() == 0) 360 { 361 // If curr_frame_address points to the first address in a section then after 362 // adjustment it will point to an other section. In that case resolve the 363 // address again to the correct section plus offset form. 364 TargetSP target = m_thread.CalculateTarget(); 365 addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(target.get(), eAddressClassCode); 366 curr_frame_address.SetOpcodeLoadAddress(load_addr - 1, target.get(), eAddressClassCode); 367 } 368 else 369 { 370 curr_frame_address.Slide(-1); 371 } 372 } 373 374 SymbolContext next_frame_sc; 375 Address next_frame_address; 376 377 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address)) 378 { 379 StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(), 380 m_frames.size(), 381 idx, 382 unwind_frame_sp->GetRegisterContextSP (), 383 cfa, 384 next_frame_address, 385 &next_frame_sc)); 386 387 m_frames.push_back (frame_sp); 388 unwind_sc = next_frame_sc; 389 curr_frame_address = next_frame_address; 390 } 391 } 392 } while (m_frames.size() - 1 < end_idx); 393 394 // Don't try to merge till you've calculated all the frames in this stack. 395 if (GetAllFramesFetched() && m_prev_frames_sp) 396 { 397 StackFrameList *prev_frames = m_prev_frames_sp.get(); 398 StackFrameList *curr_frames = this; 399 400 //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth; 401 //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc; 402 //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc); 403 404 #if defined (DEBUG_STACK_FRAMES) 405 s.PutCString("\nprev_frames:\n"); 406 prev_frames->Dump (&s); 407 s.PutCString("\ncurr_frames:\n"); 408 curr_frames->Dump (&s); 409 s.EOL(); 410 #endif 411 size_t curr_frame_num, prev_frame_num; 412 413 for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size(); 414 curr_frame_num > 0 && prev_frame_num > 0; 415 --curr_frame_num, --prev_frame_num) 416 { 417 const size_t curr_frame_idx = curr_frame_num-1; 418 const size_t prev_frame_idx = prev_frame_num-1; 419 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]); 420 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]); 421 422 #if defined (DEBUG_STACK_FRAMES) 423 s.Printf("\n\nCurr frame #%u ", curr_frame_idx); 424 if (curr_frame_sp) 425 curr_frame_sp->Dump (&s, true, false); 426 else 427 s.PutCString("NULL"); 428 s.Printf("\nPrev frame #%u ", prev_frame_idx); 429 if (prev_frame_sp) 430 prev_frame_sp->Dump (&s, true, false); 431 else 432 s.PutCString("NULL"); 433 #endif 434 435 StackFrame *curr_frame = curr_frame_sp.get(); 436 StackFrame *prev_frame = prev_frame_sp.get(); 437 438 if (curr_frame == nullptr || prev_frame == nullptr) 439 break; 440 441 // Check the stack ID to make sure they are equal 442 if (curr_frame->GetStackID() != prev_frame->GetStackID()) 443 break; 444 445 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame); 446 // Now copy the fixed up previous frame into the current frames 447 // so the pointer doesn't change 448 m_frames[curr_frame_idx] = prev_frame_sp; 449 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame); 450 451 #if defined (DEBUG_STACK_FRAMES) 452 s.Printf("\n Copying previous frame to current frame"); 453 #endif 454 } 455 // We are done with the old stack frame list, we can release it now 456 m_prev_frames_sp.reset(); 457 } 458 459 #if defined (DEBUG_STACK_FRAMES) 460 s.PutCString("\n\nNew frames:\n"); 461 Dump (&s); 462 s.EOL(); 463 #endif 464 } 465 else 466 { 467 if (end_idx < m_concrete_frames_fetched) 468 return; 469 470 if (unwinder) 471 { 472 uint32_t num_frames = unwinder->GetFramesUpTo(end_idx); 473 if (num_frames <= end_idx + 1) 474 { 475 //Done unwinding. 476 m_concrete_frames_fetched = UINT32_MAX; 477 } 478 m_frames.resize(num_frames); 479 } 480 } 481 } 482 483 uint32_t 484 StackFrameList::GetNumFrames (bool can_create) 485 { 486 Mutex::Locker locker (m_mutex); 487 488 if (can_create) 489 GetFramesUpTo (UINT32_MAX); 490 491 uint32_t inlined_depth = GetCurrentInlinedDepth(); 492 if (inlined_depth == UINT32_MAX) 493 return m_frames.size(); 494 else 495 return m_frames.size() - inlined_depth; 496 } 497 498 void 499 StackFrameList::Dump (Stream *s) 500 { 501 if (s == nullptr) 502 return; 503 Mutex::Locker locker (m_mutex); 504 505 const_iterator pos, begin = m_frames.begin(), end = m_frames.end(); 506 for (pos = begin; pos != end; ++pos) 507 { 508 StackFrame *frame = (*pos).get(); 509 s->Printf("%p: ", static_cast<void*>(frame)); 510 if (frame) 511 { 512 frame->GetStackID().Dump (s); 513 frame->DumpUsingSettingsFormat (s); 514 } 515 else 516 s->Printf("frame #%u", (uint32_t)std::distance (begin, pos)); 517 s->EOL(); 518 } 519 s->EOL(); 520 } 521 522 StackFrameSP 523 StackFrameList::GetFrameAtIndex (uint32_t idx) 524 { 525 StackFrameSP frame_sp; 526 Mutex::Locker locker (m_mutex); 527 uint32_t original_idx = idx; 528 529 uint32_t inlined_depth = GetCurrentInlinedDepth(); 530 if (inlined_depth != UINT32_MAX) 531 idx += inlined_depth; 532 533 if (idx < m_frames.size()) 534 frame_sp = m_frames[idx]; 535 536 if (frame_sp) 537 return frame_sp; 538 539 // GetFramesUpTo will fill m_frames with as many frames as you asked for, 540 // if there are that many. If there weren't then you asked for too many 541 // frames. 542 GetFramesUpTo (idx); 543 if (idx < m_frames.size()) 544 { 545 if (m_show_inlined_frames) 546 { 547 // When inline frames are enabled we actually create all the frames in GetFramesUpTo. 548 frame_sp = m_frames[idx]; 549 } 550 else 551 { 552 Unwind *unwinder = m_thread.GetUnwinder (); 553 if (unwinder) 554 { 555 addr_t pc, cfa; 556 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) 557 { 558 const bool cfa_is_valid = true; 559 const bool stop_id_is_valid = false; 560 const bool is_history_frame = false; 561 frame_sp.reset(new StackFrame(m_thread.shared_from_this(), idx, idx, cfa, cfa_is_valid, pc, 0, 562 stop_id_is_valid, is_history_frame, nullptr)); 563 564 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function; 565 if (function) 566 { 567 // When we aren't showing inline functions we always use 568 // the top most function block as the scope. 569 frame_sp->SetSymbolContextScope (&function->GetBlock(false)); 570 } 571 else 572 { 573 // Set the symbol scope from the symbol regardless if it is nullptr or valid. 574 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol); 575 } 576 SetFrameAtIndex(idx, frame_sp); 577 } 578 } 579 } 580 } 581 else if (original_idx == 0) 582 { 583 // There should ALWAYS be a frame at index 0. If something went wrong with the CurrentInlinedDepth such that 584 // there weren't as many frames as we thought taking that into account, then reset the current inlined depth 585 // and return the real zeroth frame. 586 if (m_frames.empty()) 587 { 588 // Why do we have a thread with zero frames, that should not ever happen... 589 if (m_thread.IsValid()) 590 assert ("A valid thread has no frames."); 591 } 592 else 593 { 594 ResetCurrentInlinedDepth(); 595 frame_sp = m_frames[original_idx]; 596 } 597 } 598 599 return frame_sp; 600 } 601 602 StackFrameSP 603 StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 604 { 605 // First try assuming the unwind index is the same as the frame index. The 606 // unwind index is always greater than or equal to the frame index, so it 607 // is a good place to start. If we have inlined frames we might have 5 608 // concrete frames (frame unwind indexes go from 0-4), but we might have 15 609 // frames after we make all the inlined frames. Most of the time the unwind 610 // frame index (or the concrete frame index) is the same as the frame index. 611 uint32_t frame_idx = unwind_idx; 612 StackFrameSP frame_sp (GetFrameAtIndex (frame_idx)); 613 while (frame_sp) 614 { 615 if (frame_sp->GetFrameIndex() == unwind_idx) 616 break; 617 frame_sp = GetFrameAtIndex (++frame_idx); 618 } 619 return frame_sp; 620 } 621 622 static bool 623 CompareStackID (const StackFrameSP &stack_sp, const StackID &stack_id) 624 { 625 return stack_sp->GetStackID() < stack_id; 626 } 627 628 StackFrameSP 629 StackFrameList::GetFrameWithStackID (const StackID &stack_id) 630 { 631 StackFrameSP frame_sp; 632 633 if (stack_id.IsValid()) 634 { 635 Mutex::Locker locker (m_mutex); 636 uint32_t frame_idx = 0; 637 // Do a binary search in case the stack frame is already in our cache 638 collection::const_iterator begin = m_frames.begin(); 639 collection::const_iterator end = m_frames.end(); 640 if (begin != end) 641 { 642 collection::const_iterator pos = std::lower_bound (begin, end, stack_id, CompareStackID); 643 if (pos != end) 644 { 645 if ((*pos)->GetStackID() == stack_id) 646 return *pos; 647 } 648 649 // if (m_frames.back()->GetStackID() < stack_id) 650 // frame_idx = m_frames.size(); 651 } 652 do 653 { 654 frame_sp = GetFrameAtIndex (frame_idx); 655 if (frame_sp && frame_sp->GetStackID() == stack_id) 656 break; 657 frame_idx++; 658 } 659 while (frame_sp); 660 } 661 return frame_sp; 662 } 663 664 bool 665 StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp) 666 { 667 if (idx >= m_frames.size()) 668 m_frames.resize(idx + 1); 669 // Make sure allocation succeeded by checking bounds again 670 if (idx < m_frames.size()) 671 { 672 m_frames[idx] = frame_sp; 673 return true; 674 } 675 return false; // resize failed, out of memory? 676 } 677 678 uint32_t 679 StackFrameList::GetSelectedFrameIndex () const 680 { 681 Mutex::Locker locker (m_mutex); 682 return m_selected_frame_idx; 683 } 684 685 uint32_t 686 StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame) 687 { 688 Mutex::Locker locker (m_mutex); 689 const_iterator pos; 690 const_iterator begin = m_frames.begin(); 691 const_iterator end = m_frames.end(); 692 m_selected_frame_idx = 0; 693 for (pos = begin; pos != end; ++pos) 694 { 695 if (pos->get() == frame) 696 { 697 m_selected_frame_idx = std::distance (begin, pos); 698 uint32_t inlined_depth = GetCurrentInlinedDepth(); 699 if (inlined_depth != UINT32_MAX) 700 m_selected_frame_idx -= inlined_depth; 701 break; 702 } 703 } 704 SetDefaultFileAndLineToSelectedFrame(); 705 return m_selected_frame_idx; 706 } 707 708 // Mark a stack frame as the current frame using the frame index 709 bool 710 StackFrameList::SetSelectedFrameByIndex (uint32_t idx) 711 { 712 Mutex::Locker locker (m_mutex); 713 StackFrameSP frame_sp (GetFrameAtIndex (idx)); 714 if (frame_sp) 715 { 716 SetSelectedFrame(frame_sp.get()); 717 return true; 718 } 719 else 720 return false; 721 } 722 723 void 724 StackFrameList::SetDefaultFileAndLineToSelectedFrame() 725 { 726 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) 727 { 728 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex())); 729 if (frame_sp) 730 { 731 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 732 if (sc.line_entry.file) 733 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file, 734 sc.line_entry.line); 735 } 736 } 737 } 738 739 // The thread has been run, reset the number stack frames to zero so we can 740 // determine how many frames we have lazily. 741 void 742 StackFrameList::Clear () 743 { 744 Mutex::Locker locker (m_mutex); 745 m_frames.clear(); 746 m_concrete_frames_fetched = 0; 747 } 748 749 void 750 StackFrameList::InvalidateFrames (uint32_t start_idx) 751 { 752 Mutex::Locker locker (m_mutex); 753 if (m_show_inlined_frames) 754 { 755 Clear(); 756 } 757 else 758 { 759 const size_t num_frames = m_frames.size(); 760 while (start_idx < num_frames) 761 { 762 m_frames[start_idx].reset(); 763 ++start_idx; 764 } 765 } 766 } 767 768 void 769 StackFrameList::Merge (std::unique_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp) 770 { 771 Mutex::Locker curr_locker(curr_ap ? &curr_ap->m_mutex : nullptr); 772 Mutex::Locker prev_locker(prev_sp ? &prev_sp->m_mutex : nullptr); 773 774 #if defined (DEBUG_STACK_FRAMES) 775 StreamFile s(stdout, false); 776 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 777 if (prev_sp) 778 prev_sp->Dump (&s); 779 else 780 s.PutCString ("NULL"); 781 s.PutCString("\nCurr:\n"); 782 if (curr_ap) 783 curr_ap->Dump (&s); 784 else 785 s.PutCString ("NULL"); 786 s.EOL(); 787 #endif 788 789 if (!curr_ap || curr_ap->GetNumFrames(false) == 0) 790 { 791 #if defined (DEBUG_STACK_FRAMES) 792 s.PutCString("No current frames, leave previous frames alone...\n"); 793 #endif 794 curr_ap.release(); 795 return; 796 } 797 798 if (!prev_sp || prev_sp->GetNumFrames(false) == 0) 799 { 800 #if defined (DEBUG_STACK_FRAMES) 801 s.PutCString("No previous frames, so use current frames...\n"); 802 #endif 803 // We either don't have any previous frames, or since we have more than 804 // one current frames it means we have all the frames and can safely 805 // replace our previous frames. 806 prev_sp.reset (curr_ap.release()); 807 return; 808 } 809 810 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false); 811 812 if (num_curr_frames > 1) 813 { 814 #if defined (DEBUG_STACK_FRAMES) 815 s.PutCString("We have more than one current frame, so use current frames...\n"); 816 #endif 817 // We have more than one current frames it means we have all the frames 818 // and can safely replace our previous frames. 819 prev_sp.reset (curr_ap.release()); 820 821 #if defined (DEBUG_STACK_FRAMES) 822 s.PutCString("\nMerged:\n"); 823 prev_sp->Dump (&s); 824 #endif 825 return; 826 } 827 828 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0)); 829 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0)); 830 StackID curr_stack_id (curr_frame_zero_sp->GetStackID()); 831 StackID prev_stack_id (prev_frame_zero_sp->GetStackID()); 832 833 #if defined (DEBUG_STACK_FRAMES) 834 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false); 835 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 836 #endif 837 838 // We have only a single current frame 839 // Our previous stack frames only had a single frame as well... 840 if (curr_stack_id == prev_stack_id) 841 { 842 #if defined (DEBUG_STACK_FRAMES) 843 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n"); 844 #endif 845 846 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp); 847 // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp); 848 // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 849 } 850 else if (curr_stack_id < prev_stack_id) 851 { 852 #if defined (DEBUG_STACK_FRAMES) 853 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"); 854 #endif 855 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp); 856 } 857 858 curr_ap.release(); 859 860 #if defined (DEBUG_STACK_FRAMES) 861 s.PutCString("\nMerged:\n"); 862 prev_sp->Dump (&s); 863 #endif 864 } 865 866 lldb::StackFrameSP 867 StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 868 { 869 const_iterator pos; 870 const_iterator begin = m_frames.begin(); 871 const_iterator end = m_frames.end(); 872 lldb::StackFrameSP ret_sp; 873 874 for (pos = begin; pos != end; ++pos) 875 { 876 if (pos->get() == stack_frame_ptr) 877 { 878 ret_sp = (*pos); 879 break; 880 } 881 } 882 return ret_sp; 883 } 884 885 size_t 886 StackFrameList::GetStatus (Stream& strm, 887 uint32_t first_frame, 888 uint32_t num_frames, 889 bool show_frame_info, 890 uint32_t num_frames_with_source, 891 const char *selected_frame_marker) 892 { 893 size_t num_frames_displayed = 0; 894 895 if (num_frames == 0) 896 return 0; 897 898 StackFrameSP frame_sp; 899 uint32_t frame_idx = 0; 900 uint32_t last_frame; 901 902 // Don't let the last frame wrap around... 903 if (num_frames == UINT32_MAX) 904 last_frame = UINT32_MAX; 905 else 906 last_frame = first_frame + num_frames; 907 908 StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 909 const char *unselected_marker = nullptr; 910 std::string buffer; 911 if (selected_frame_marker) 912 { 913 size_t len = strlen(selected_frame_marker); 914 buffer.insert(buffer.begin(), len, ' '); 915 unselected_marker = buffer.c_str(); 916 } 917 const char *marker = nullptr; 918 919 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) 920 { 921 frame_sp = GetFrameAtIndex(frame_idx); 922 if (!frame_sp) 923 break; 924 925 if (selected_frame_marker != nullptr) 926 { 927 if (frame_sp == selected_frame_sp) 928 marker = selected_frame_marker; 929 else 930 marker = unselected_marker; 931 } 932 933 if (!frame_sp->GetStatus (strm, 934 show_frame_info, 935 num_frames_with_source > (first_frame - frame_idx), marker)) 936 break; 937 ++num_frames_displayed; 938 } 939 940 strm.IndentLess(); 941 return num_frames_displayed; 942 } 943