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 if (m_show_inlined_frames) 109 { 110 GetFramesUpTo(0); 111 if (m_frames.size() == 0) 112 return; 113 if (!m_frames[0]->IsInlined()) 114 { 115 m_current_inlined_depth = UINT32_MAX; 116 m_current_inlined_pc = LLDB_INVALID_ADDRESS; 117 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 118 if (log && log->GetVerbose()) 119 log->Printf ("ResetCurrentInlinedDepth: Invalidating current inlined depth.\n"); 120 } 121 else 122 { 123 // We only need to do something special about inlined blocks when we 124 // are at the beginning of an inlined function: 125 // FIXME: We probably also have to do something special if the PC is at the END 126 // of an inlined function, which coincides with the end of either its containing 127 // function or another inlined function. 128 129 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC(); 130 Block *block_ptr = m_frames[0]->GetFrameBlock(); 131 if (block_ptr) 132 { 133 Address pc_as_address; 134 pc_as_address.SetLoadAddress(curr_pc, &(m_thread.GetProcess()->GetTarget())); 135 AddressRange containing_range; 136 if (block_ptr->GetRangeContainingAddress(pc_as_address, containing_range)) 137 { 138 if (pc_as_address == containing_range.GetBaseAddress()) 139 { 140 // If we got here because of a breakpoint hit, then set the inlined depth depending on where 141 // the breakpoint was set. 142 // If we got here because of a crash, then set the inlined depth to the deepest most block. 143 // Otherwise, we stopped here naturally as the result of a step, so set ourselves in the 144 // containing frame of the whole set of nested inlines, so the user can then "virtually" 145 // step into the frames one by one, or next over the whole mess. 146 // Note: We don't have to handle being somewhere in the middle of the stack here, since 147 // ResetCurrentInlinedDepth doesn't get called if there is a valid inlined depth set. 148 StopInfoSP stop_info_sp = m_thread.GetStopInfo(); 149 if (stop_info_sp) 150 { 151 switch (stop_info_sp->GetStopReason()) 152 { 153 case eStopReasonWatchpoint: 154 case eStopReasonException: 155 case eStopReasonExec: 156 case eStopReasonSignal: 157 // In all these cases we want to stop in the deepest most frame. 158 m_current_inlined_pc = curr_pc; 159 m_current_inlined_depth = 0; 160 break; 161 case eStopReasonBreakpoint: 162 { 163 // FIXME: Figure out what this break point is doing, and set the inline depth 164 // appropriately. Be careful to take into account breakpoints that implement 165 // step over prologue, since that should do the default calculation. 166 // For now, if the breakpoints corresponding to this hit are all internal, 167 // I set the stop location to the top of the inlined stack, since that will make 168 // things like stepping over prologues work right. But if there are any non-internal 169 // breakpoints I do to the bottom of the stack, since that was the old behavior. 170 uint32_t bp_site_id = stop_info_sp->GetValue(); 171 BreakpointSiteSP bp_site_sp(m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id)); 172 bool all_internal = true; 173 if (bp_site_sp) 174 { 175 uint32_t num_owners = bp_site_sp->GetNumberOfOwners(); 176 for (uint32_t i = 0; i < num_owners; i++) 177 { 178 Breakpoint &bp_ref = bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint(); 179 if (!bp_ref.IsInternal()) 180 { 181 all_internal = false; 182 } 183 } 184 } 185 if (!all_internal) 186 { 187 m_current_inlined_pc = curr_pc; 188 m_current_inlined_depth = 0; 189 break; 190 } 191 } 192 default: 193 { 194 // Otherwise, we should set ourselves at the container of the inlining, so that the 195 // user can descend into them. 196 // So first we check whether we have more than one inlined block sharing this PC: 197 int num_inlined_functions = 0; 198 199 for (Block *container_ptr = block_ptr->GetInlinedParent(); 200 container_ptr != NULL; 201 container_ptr = container_ptr->GetInlinedParent()) 202 { 203 if (!container_ptr->GetRangeContainingAddress(pc_as_address, containing_range)) 204 break; 205 if (pc_as_address != containing_range.GetBaseAddress()) 206 break; 207 208 num_inlined_functions++; 209 } 210 m_current_inlined_pc = curr_pc; 211 m_current_inlined_depth = num_inlined_functions + 1; 212 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 213 if (log && log->GetVerbose()) 214 log->Printf ("ResetCurrentInlinedDepth: setting inlined depth: %d 0x%" PRIx64 ".\n", m_current_inlined_depth, curr_pc); 215 216 } 217 break; 218 } 219 } 220 } 221 } 222 } 223 } 224 } 225 } 226 227 bool 228 StackFrameList::DecrementCurrentInlinedDepth () 229 { 230 if (m_show_inlined_frames) 231 { 232 uint32_t current_inlined_depth = GetCurrentInlinedDepth(); 233 if (current_inlined_depth != UINT32_MAX) 234 { 235 if (current_inlined_depth > 0) 236 { 237 m_current_inlined_depth--; 238 return true; 239 } 240 } 241 } 242 return false; 243 } 244 245 void 246 StackFrameList::SetCurrentInlinedDepth (uint32_t new_depth) 247 { 248 m_current_inlined_depth = new_depth; 249 if (new_depth == UINT32_MAX) 250 m_current_inlined_pc = LLDB_INVALID_ADDRESS; 251 else 252 m_current_inlined_pc = m_thread.GetRegisterContext()->GetPC(); 253 } 254 255 void 256 StackFrameList::GetFramesUpTo(uint32_t end_idx) 257 { 258 // this makes sure we do not fetch frames for an invalid thread 259 if (m_thread.IsValid() == false) 260 return; 261 262 // We've already gotten more frames than asked for, or we've already finished unwinding, return. 263 if (m_frames.size() > end_idx || GetAllFramesFetched()) 264 return; 265 266 Unwind *unwinder = m_thread.GetUnwinder (); 267 268 if (m_show_inlined_frames) 269 { 270 #if defined (DEBUG_STACK_FRAMES) 271 StreamFile s(stdout, false); 272 #endif 273 // If we are hiding some frames from the outside world, we need to add those onto the total count of 274 // frames to fetch. However, we don't need to do that if end_idx is 0 since in that case we always 275 // get the first concrete frame and all the inlined frames below it... And of course, if end_idx is 276 // UINT32_MAX that means get all, so just do that... 277 278 uint32_t inlined_depth = 0; 279 if (end_idx > 0 && end_idx != UINT32_MAX) 280 { 281 inlined_depth = GetCurrentInlinedDepth(); 282 if (inlined_depth != UINT32_MAX) 283 { 284 if (end_idx > 0) 285 end_idx += inlined_depth; 286 } 287 } 288 289 StackFrameSP unwind_frame_sp; 290 do 291 { 292 uint32_t idx = m_concrete_frames_fetched++; 293 lldb::addr_t pc = LLDB_INVALID_ADDRESS; 294 lldb::addr_t cfa = LLDB_INVALID_ADDRESS; 295 if (idx == 0) 296 { 297 // We might have already created frame zero, only create it 298 // if we need to 299 if (m_frames.empty()) 300 { 301 RegisterContextSP reg_ctx_sp (m_thread.GetRegisterContext()); 302 303 if (reg_ctx_sp) 304 { 305 306 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 307 // There shouldn't be any way not to get the frame info for frame 0. 308 // But if the unwinder can't make one, lets make one by hand with the 309 // SP as the CFA and see if that gets any further. 310 if (!success) 311 { 312 cfa = reg_ctx_sp->GetSP(); 313 pc = reg_ctx_sp->GetPC(); 314 } 315 316 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), 317 m_frames.size(), 318 idx, 319 reg_ctx_sp, 320 cfa, 321 pc, 322 NULL)); 323 m_frames.push_back (unwind_frame_sp); 324 } 325 } 326 else 327 { 328 unwind_frame_sp = m_frames.front(); 329 cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); 330 } 331 } 332 else 333 { 334 const bool success = unwinder && unwinder->GetFrameInfoAtIndex(idx, cfa, pc); 335 if (!success) 336 { 337 // We've gotten to the end of the stack. 338 SetAllFramesFetched(); 339 break; 340 } 341 const bool cfa_is_valid = true; 342 const bool stop_id_is_valid = false; 343 const bool is_history_frame = false; 344 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)); 345 m_frames.push_back (unwind_frame_sp); 346 } 347 348 assert(unwind_frame_sp); 349 SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction); 350 Block *unwind_block = unwind_sc.block; 351 if (unwind_block) 352 { 353 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress()); 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 TargetSP target = m_thread.CalculateTarget(); 367 addr_t load_addr = curr_frame_address.GetOpcodeLoadAddress(target.get(), eAddressClassCode); 368 curr_frame_address.SetOpcodeLoadAddress(load_addr - 1, target.get(), eAddressClassCode); 369 } 370 else 371 { 372 curr_frame_address.Slide(-1); 373 } 374 } 375 376 SymbolContext next_frame_sc; 377 Address next_frame_address; 378 379 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address)) 380 { 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 == NULL || prev_frame == NULL) 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 == NULL) 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, stop_id_is_valid, is_history_frame, NULL)); 564 565 Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function; 566 if (function) 567 { 568 // When we aren't showing inline functions we always use 569 // the top most function block as the scope. 570 frame_sp->SetSymbolContextScope (&function->GetBlock(false)); 571 } 572 else 573 { 574 // Set the symbol scope from the symbol regardless if it is NULL or valid. 575 frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol); 576 } 577 SetFrameAtIndex(idx, frame_sp); 578 } 579 } 580 } 581 } 582 else if (original_idx == 0) 583 { 584 // There should ALWAYS be a frame at index 0. If something went wrong with the CurrentInlinedDepth such that 585 // there weren't as many frames as we thought taking that into account, then reset the current inlined depth 586 // and return the real zeroth frame. 587 if (m_frames.size() > 0) 588 { 589 ResetCurrentInlinedDepth(); 590 frame_sp = m_frames[original_idx]; 591 } 592 else 593 { 594 // Why do we have a thread with zero frames, that should not ever happen... 595 if (m_thread.IsValid()) 596 assert ("A valid thread has no frames."); 597 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 688 uint32_t 689 StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame) 690 { 691 Mutex::Locker locker (m_mutex); 692 const_iterator pos; 693 const_iterator begin = m_frames.begin(); 694 const_iterator end = m_frames.end(); 695 m_selected_frame_idx = 0; 696 for (pos = begin; pos != end; ++pos) 697 { 698 if (pos->get() == frame) 699 { 700 m_selected_frame_idx = std::distance (begin, pos); 701 uint32_t inlined_depth = GetCurrentInlinedDepth(); 702 if (inlined_depth != UINT32_MAX) 703 m_selected_frame_idx -= inlined_depth; 704 break; 705 } 706 } 707 SetDefaultFileAndLineToSelectedFrame(); 708 return m_selected_frame_idx; 709 } 710 711 // Mark a stack frame as the current frame using the frame index 712 bool 713 StackFrameList::SetSelectedFrameByIndex (uint32_t idx) 714 { 715 Mutex::Locker locker (m_mutex); 716 StackFrameSP frame_sp (GetFrameAtIndex (idx)); 717 if (frame_sp) 718 { 719 SetSelectedFrame(frame_sp.get()); 720 return true; 721 } 722 else 723 return false; 724 } 725 726 void 727 StackFrameList::SetDefaultFileAndLineToSelectedFrame() 728 { 729 if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID()) 730 { 731 StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex())); 732 if (frame_sp) 733 { 734 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry); 735 if (sc.line_entry.file) 736 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file, 737 sc.line_entry.line); 738 } 739 } 740 } 741 742 // The thread has been run, reset the number stack frames to zero so we can 743 // determine how many frames we have lazily. 744 void 745 StackFrameList::Clear () 746 { 747 Mutex::Locker locker (m_mutex); 748 m_frames.clear(); 749 m_concrete_frames_fetched = 0; 750 } 751 752 void 753 StackFrameList::InvalidateFrames (uint32_t start_idx) 754 { 755 Mutex::Locker locker (m_mutex); 756 if (m_show_inlined_frames) 757 { 758 Clear(); 759 } 760 else 761 { 762 const size_t num_frames = m_frames.size(); 763 while (start_idx < num_frames) 764 { 765 m_frames[start_idx].reset(); 766 ++start_idx; 767 } 768 } 769 } 770 771 void 772 StackFrameList::Merge (std::unique_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp) 773 { 774 Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL); 775 Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL); 776 777 #if defined (DEBUG_STACK_FRAMES) 778 StreamFile s(stdout, false); 779 s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); 780 if (prev_sp.get()) 781 prev_sp->Dump (&s); 782 else 783 s.PutCString ("NULL"); 784 s.PutCString("\nCurr:\n"); 785 if (curr_ap.get()) 786 curr_ap->Dump (&s); 787 else 788 s.PutCString ("NULL"); 789 s.EOL(); 790 #endif 791 792 if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0) 793 { 794 #if defined (DEBUG_STACK_FRAMES) 795 s.PutCString("No current frames, leave previous frames alone...\n"); 796 #endif 797 curr_ap.release(); 798 return; 799 } 800 801 if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0) 802 { 803 #if defined (DEBUG_STACK_FRAMES) 804 s.PutCString("No previous frames, so use current frames...\n"); 805 #endif 806 // We either don't have any previous frames, or since we have more than 807 // one current frames it means we have all the frames and can safely 808 // replace our previous frames. 809 prev_sp.reset (curr_ap.release()); 810 return; 811 } 812 813 const uint32_t num_curr_frames = curr_ap->GetNumFrames (false); 814 815 if (num_curr_frames > 1) 816 { 817 #if defined (DEBUG_STACK_FRAMES) 818 s.PutCString("We have more than one current frame, so use current frames...\n"); 819 #endif 820 // We have more than one current frames it means we have all the frames 821 // and can safely replace our previous frames. 822 prev_sp.reset (curr_ap.release()); 823 824 #if defined (DEBUG_STACK_FRAMES) 825 s.PutCString("\nMerged:\n"); 826 prev_sp->Dump (&s); 827 #endif 828 return; 829 } 830 831 StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0)); 832 StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0)); 833 StackID curr_stack_id (curr_frame_zero_sp->GetStackID()); 834 StackID prev_stack_id (prev_frame_zero_sp->GetStackID()); 835 836 #if defined (DEBUG_STACK_FRAMES) 837 const uint32_t num_prev_frames = prev_sp->GetNumFrames (false); 838 s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); 839 #endif 840 841 // We have only a single current frame 842 // Our previous stack frames only had a single frame as well... 843 if (curr_stack_id == prev_stack_id) 844 { 845 #if defined (DEBUG_STACK_FRAMES) 846 s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n"); 847 #endif 848 849 curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp); 850 // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp); 851 // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); 852 } 853 else if (curr_stack_id < prev_stack_id) 854 { 855 #if defined (DEBUG_STACK_FRAMES) 856 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"); 857 #endif 858 prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp); 859 } 860 861 curr_ap.release(); 862 863 #if defined (DEBUG_STACK_FRAMES) 864 s.PutCString("\nMerged:\n"); 865 prev_sp->Dump (&s); 866 #endif 867 868 869 } 870 871 lldb::StackFrameSP 872 StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 873 { 874 const_iterator pos; 875 const_iterator begin = m_frames.begin(); 876 const_iterator end = m_frames.end(); 877 lldb::StackFrameSP ret_sp; 878 879 for (pos = begin; pos != end; ++pos) 880 { 881 if (pos->get() == stack_frame_ptr) 882 { 883 ret_sp = (*pos); 884 break; 885 } 886 } 887 return ret_sp; 888 } 889 890 size_t 891 StackFrameList::GetStatus (Stream& strm, 892 uint32_t first_frame, 893 uint32_t num_frames, 894 bool show_frame_info, 895 uint32_t num_frames_with_source, 896 const char *selected_frame_marker) 897 { 898 size_t num_frames_displayed = 0; 899 900 if (num_frames == 0) 901 return 0; 902 903 StackFrameSP frame_sp; 904 uint32_t frame_idx = 0; 905 uint32_t last_frame; 906 907 // Don't let the last frame wrap around... 908 if (num_frames == UINT32_MAX) 909 last_frame = UINT32_MAX; 910 else 911 last_frame = first_frame + num_frames; 912 913 StackFrameSP selected_frame_sp = m_thread.GetSelectedFrame(); 914 const char *unselected_marker = NULL; 915 std::string buffer; 916 if (selected_frame_marker) 917 { 918 size_t len = strlen(selected_frame_marker); 919 buffer.insert(buffer.begin(), len, ' '); 920 unselected_marker = buffer.c_str(); 921 } 922 const char *marker = NULL; 923 924 for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) 925 { 926 frame_sp = GetFrameAtIndex(frame_idx); 927 if (frame_sp.get() == NULL) 928 break; 929 930 if (selected_frame_marker != NULL) 931 { 932 if (frame_sp == selected_frame_sp) 933 marker = selected_frame_marker; 934 else 935 marker = unselected_marker; 936 } 937 938 if (!frame_sp->GetStatus (strm, 939 show_frame_info, 940 num_frames_with_source > (first_frame - frame_idx), marker)) 941 break; 942 ++num_frames_displayed; 943 } 944 945 strm.IndentLess(); 946 return num_frames_displayed; 947 } 948 949