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