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[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 != NULL;
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() == false)
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 ot 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;
292             lldb::addr_t cfa;
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                     m_thread.GetRegisterContext();
300                     assert (m_thread.m_reg_context_sp.get());
301 
302                     const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
303                     // There shouldn't be any way not to get the frame info for frame 0.
304                     // But if the unwinder can't make one, lets make one by hand with the
305                     // SP as the CFA and see if that gets any further.
306                     if (!success)
307                     {
308                         cfa = m_thread.GetRegisterContext()->GetSP();
309                         pc = m_thread.GetRegisterContext()->GetPC();
310                     }
311 
312                     unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(),
313                                                            m_frames.size(),
314                                                            idx,
315                                                            m_thread.m_reg_context_sp,
316                                                            cfa,
317                                                            pc,
318                                                            NULL));
319                     m_frames.push_back (unwind_frame_sp);
320                 }
321                 else
322                 {
323                     unwind_frame_sp = m_frames.front();
324                     cfa = unwind_frame_sp->m_id.GetCallFrameAddress();
325                 }
326             }
327             else
328             {
329                 const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc);
330                 if (!success)
331                 {
332                     // We've gotten to the end of the stack.
333                     SetAllFramesFetched();
334                     break;
335                 }
336                 unwind_frame_sp.reset (new StackFrame (m_thread.shared_from_this(), m_frames.size(), idx, cfa, pc, NULL));
337                 m_frames.push_back (unwind_frame_sp);
338             }
339 
340             SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction);
341             Block *unwind_block = unwind_sc.block;
342             if (unwind_block)
343             {
344                 Address curr_frame_address (unwind_frame_sp->GetFrameCodeAddress());
345                 // Be sure to adjust the frame address to match the address
346                 // that was used to lookup the symbol context above. If we are
347                 // in the first concrete frame, then we lookup using the current
348                 // address, else we decrement the address by one to get the correct
349                 // location.
350                 if (idx > 0)
351                     curr_frame_address.Slide(-1);
352 
353                 SymbolContext next_frame_sc;
354                 Address next_frame_address;
355 
356                 while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address))
357                 {
358                         StackFrameSP frame_sp(new StackFrame (m_thread.shared_from_this(),
359                                                               m_frames.size(),
360                                                               idx,
361                                                               unwind_frame_sp->GetRegisterContextSP (),
362                                                               cfa,
363                                                               next_frame_address,
364                                                               &next_frame_sc));
365 
366                         m_frames.push_back (frame_sp);
367                         unwind_sc = next_frame_sc;
368                         curr_frame_address = next_frame_address;
369                 }
370             }
371         } while (m_frames.size() - 1 < end_idx);
372 
373         // Don't try to merge till you've calculated all the frames in this stack.
374         if (GetAllFramesFetched() && m_prev_frames_sp)
375         {
376             StackFrameList *prev_frames = m_prev_frames_sp.get();
377             StackFrameList *curr_frames = this;
378 
379             //curr_frames->m_current_inlined_depth = prev_frames->m_current_inlined_depth;
380             //curr_frames->m_current_inlined_pc = prev_frames->m_current_inlined_pc;
381             //printf ("GetFramesUpTo: Copying current inlined depth: %d 0x%" PRIx64 ".\n", curr_frames->m_current_inlined_depth, curr_frames->m_current_inlined_pc);
382 
383 #if defined (DEBUG_STACK_FRAMES)
384             s.PutCString("\nprev_frames:\n");
385             prev_frames->Dump (&s);
386             s.PutCString("\ncurr_frames:\n");
387             curr_frames->Dump (&s);
388             s.EOL();
389 #endif
390             size_t curr_frame_num, prev_frame_num;
391 
392             for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size();
393                  curr_frame_num > 0 && prev_frame_num > 0;
394                  --curr_frame_num, --prev_frame_num)
395             {
396                 const size_t curr_frame_idx = curr_frame_num-1;
397                 const size_t prev_frame_idx = prev_frame_num-1;
398                 StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]);
399                 StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]);
400 
401 #if defined (DEBUG_STACK_FRAMES)
402                 s.Printf("\n\nCurr frame #%u ", curr_frame_idx);
403                 if (curr_frame_sp)
404                     curr_frame_sp->Dump (&s, true, false);
405                 else
406                     s.PutCString("NULL");
407                 s.Printf("\nPrev frame #%u ", prev_frame_idx);
408                 if (prev_frame_sp)
409                     prev_frame_sp->Dump (&s, true, false);
410                 else
411                     s.PutCString("NULL");
412 #endif
413 
414                 StackFrame *curr_frame = curr_frame_sp.get();
415                 StackFrame *prev_frame = prev_frame_sp.get();
416 
417                 if (curr_frame == NULL || prev_frame == NULL)
418                     break;
419 
420                 // Check the stack ID to make sure they are equal
421                 if (curr_frame->GetStackID() != prev_frame->GetStackID())
422                     break;
423 
424                 prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame);
425                 // Now copy the fixed up previous frame into the current frames
426                 // so the pointer doesn't change
427                 m_frames[curr_frame_idx] = prev_frame_sp;
428                 //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame);
429 
430 #if defined (DEBUG_STACK_FRAMES)
431                 s.Printf("\n    Copying previous frame to current frame");
432 #endif
433             }
434             // We are done with the old stack frame list, we can release it now
435             m_prev_frames_sp.reset();
436         }
437 
438 #if defined (DEBUG_STACK_FRAMES)
439             s.PutCString("\n\nNew frames:\n");
440             Dump (&s);
441             s.EOL();
442 #endif
443     }
444     else
445     {
446         if (end_idx < m_concrete_frames_fetched)
447             return;
448 
449         uint32_t num_frames = unwinder->GetFramesUpTo(end_idx);
450         if (num_frames <= end_idx + 1)
451         {
452             //Done unwinding.
453             m_concrete_frames_fetched = UINT32_MAX;
454         }
455         m_frames.resize(num_frames);
456     }
457 }
458 
459 uint32_t
460 StackFrameList::GetNumFrames (bool can_create)
461 {
462     Mutex::Locker locker (m_mutex);
463 
464     if (can_create)
465         GetFramesUpTo (UINT32_MAX);
466 
467     uint32_t inlined_depth = GetCurrentInlinedDepth();
468     if (inlined_depth == UINT32_MAX)
469         return m_frames.size();
470     else
471         return m_frames.size() - inlined_depth;
472 }
473 
474 void
475 StackFrameList::Dump (Stream *s)
476 {
477     if (s == NULL)
478         return;
479     Mutex::Locker locker (m_mutex);
480 
481     const_iterator pos, begin = m_frames.begin(), end = m_frames.end();
482     for (pos = begin; pos != end; ++pos)
483     {
484         StackFrame *frame = (*pos).get();
485         s->Printf("%p: ", frame);
486         if (frame)
487         {
488             frame->GetStackID().Dump (s);
489             frame->DumpUsingSettingsFormat (s);
490         }
491         else
492             s->Printf("frame #%u", (uint32_t)std::distance (begin, pos));
493         s->EOL();
494     }
495     s->EOL();
496 }
497 
498 StackFrameSP
499 StackFrameList::GetFrameAtIndex (uint32_t idx)
500 {
501     StackFrameSP frame_sp;
502     Mutex::Locker locker (m_mutex);
503     uint32_t original_idx = idx;
504 
505     uint32_t inlined_depth = GetCurrentInlinedDepth();
506     if (inlined_depth != UINT32_MAX)
507         idx += inlined_depth;
508 
509     if (idx < m_frames.size())
510         frame_sp = m_frames[idx];
511 
512     if (frame_sp)
513         return frame_sp;
514 
515     // GetFramesUpTo will fill m_frames with as many frames as you asked for,
516     // if there are that many.  If there weren't then you asked for too many
517     // frames.
518     GetFramesUpTo (idx);
519     if (idx < m_frames.size())
520     {
521         if (m_show_inlined_frames)
522         {
523             // When inline frames are enabled we actually create all the frames in GetFramesUpTo.
524             frame_sp = m_frames[idx];
525         }
526         else
527         {
528             Unwind *unwinder = m_thread.GetUnwinder ();
529             if (unwinder)
530             {
531                 addr_t pc, cfa;
532                 if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc))
533                 {
534                     frame_sp.reset (new StackFrame (m_thread.shared_from_this(), idx, idx, cfa, pc, NULL));
535 
536                     Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function;
537                     if (function)
538                     {
539                         // When we aren't showing inline functions we always use
540                         // the top most function block as the scope.
541                         frame_sp->SetSymbolContextScope (&function->GetBlock(false));
542                     }
543                     else
544                     {
545                         // Set the symbol scope from the symbol regardless if it is NULL or valid.
546                         frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
547                     }
548                     SetFrameAtIndex(idx, frame_sp);
549                 }
550             }
551         }
552     }
553     else if (original_idx == 0)
554     {
555         // There should ALWAYS be a frame at index 0.  If something went wrong with the CurrentInlinedDepth such that
556         // there weren't as many frames as we thought taking that into account, then reset the current inlined depth
557         // and return the real zeroth frame.
558         if (m_frames.size() > 0)
559         {
560             ResetCurrentInlinedDepth();
561             frame_sp = m_frames[original_idx];
562         }
563         else
564         {
565             // Why do we have a thread with zero frames, that should not ever happen...
566             if (m_thread.IsValid())
567                 assert ("A valid thread has no frames.");
568 
569         }
570     }
571 
572     return frame_sp;
573 }
574 
575 StackFrameSP
576 StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
577 {
578     // First try assuming the unwind index is the same as the frame index. The
579     // unwind index is always greater than or equal to the frame index, so it
580     // is a good place to start. If we have inlined frames we might have 5
581     // concrete frames (frame unwind indexes go from 0-4), but we might have 15
582     // frames after we make all the inlined frames. Most of the time the unwind
583     // frame index (or the concrete frame index) is the same as the frame index.
584     uint32_t frame_idx = unwind_idx;
585     StackFrameSP frame_sp (GetFrameAtIndex (frame_idx));
586     while (frame_sp)
587     {
588         if (frame_sp->GetFrameIndex() == unwind_idx)
589             break;
590         frame_sp = GetFrameAtIndex (++frame_idx);
591     }
592     return frame_sp;
593 }
594 
595 StackFrameSP
596 StackFrameList::GetFrameWithStackID (const StackID &stack_id)
597 {
598     uint32_t frame_idx = 0;
599     StackFrameSP frame_sp;
600     do
601     {
602         frame_sp = GetFrameAtIndex (frame_idx);
603         if (frame_sp && frame_sp->GetStackID() == stack_id)
604             break;
605         frame_idx++;
606     }
607     while (frame_sp);
608     return frame_sp;
609 }
610 
611 bool
612 StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp)
613 {
614     if (idx >= m_frames.size())
615         m_frames.resize(idx + 1);
616     // Make sure allocation succeeded by checking bounds again
617     if (idx < m_frames.size())
618     {
619         m_frames[idx] = frame_sp;
620         return true;
621     }
622     return false;   // resize failed, out of memory?
623 }
624 
625 uint32_t
626 StackFrameList::GetSelectedFrameIndex () const
627 {
628     Mutex::Locker locker (m_mutex);
629     return m_selected_frame_idx;
630 }
631 
632 
633 uint32_t
634 StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame)
635 {
636     Mutex::Locker locker (m_mutex);
637     const_iterator pos;
638     const_iterator begin = m_frames.begin();
639     const_iterator end = m_frames.end();
640     m_selected_frame_idx = 0;
641     for (pos = begin; pos != end; ++pos)
642     {
643         if (pos->get() == frame)
644         {
645             m_selected_frame_idx = std::distance (begin, pos);
646             uint32_t inlined_depth = GetCurrentInlinedDepth();
647             if (inlined_depth != UINT32_MAX)
648                 m_selected_frame_idx -= inlined_depth;
649             break;
650         }
651     }
652     SetDefaultFileAndLineToSelectedFrame();
653     return m_selected_frame_idx;
654 }
655 
656 // Mark a stack frame as the current frame using the frame index
657 bool
658 StackFrameList::SetSelectedFrameByIndex (uint32_t idx)
659 {
660     Mutex::Locker locker (m_mutex);
661     StackFrameSP frame_sp (GetFrameAtIndex (idx));
662     if (frame_sp)
663     {
664         SetSelectedFrame(frame_sp.get());
665         return true;
666     }
667     else
668         return false;
669 }
670 
671 void
672 StackFrameList::SetDefaultFileAndLineToSelectedFrame()
673 {
674     if (m_thread.GetID() == m_thread.GetProcess()->GetThreadList().GetSelectedThread()->GetID())
675     {
676         StackFrameSP frame_sp (GetFrameAtIndex (GetSelectedFrameIndex()));
677         if (frame_sp)
678         {
679             SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextLineEntry);
680             if (sc.line_entry.file)
681                 m_thread.CalculateTarget()->GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file,
682                                                                                             sc.line_entry.line);
683         }
684     }
685 }
686 
687 // The thread has been run, reset the number stack frames to zero so we can
688 // determine how many frames we have lazily.
689 void
690 StackFrameList::Clear ()
691 {
692     Mutex::Locker locker (m_mutex);
693     m_frames.clear();
694     m_concrete_frames_fetched = 0;
695 }
696 
697 void
698 StackFrameList::InvalidateFrames (uint32_t start_idx)
699 {
700     Mutex::Locker locker (m_mutex);
701     if (m_show_inlined_frames)
702     {
703         Clear();
704     }
705     else
706     {
707         const size_t num_frames = m_frames.size();
708         while (start_idx < num_frames)
709         {
710             m_frames[start_idx].reset();
711             ++start_idx;
712         }
713     }
714 }
715 
716 void
717 StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp)
718 {
719     Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL);
720     Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL);
721 
722 #if defined (DEBUG_STACK_FRAMES)
723     StreamFile s(stdout, false);
724     s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n");
725     if (prev_sp.get())
726         prev_sp->Dump (&s);
727     else
728         s.PutCString ("NULL");
729     s.PutCString("\nCurr:\n");
730     if (curr_ap.get())
731         curr_ap->Dump (&s);
732     else
733         s.PutCString ("NULL");
734     s.EOL();
735 #endif
736 
737     if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0)
738     {
739 #if defined (DEBUG_STACK_FRAMES)
740         s.PutCString("No current frames, leave previous frames alone...\n");
741 #endif
742         curr_ap.release();
743         return;
744     }
745 
746     if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0)
747     {
748 #if defined (DEBUG_STACK_FRAMES)
749         s.PutCString("No previous frames, so use current frames...\n");
750 #endif
751         // We either don't have any previous frames, or since we have more than
752         // one current frames it means we have all the frames and can safely
753         // replace our previous frames.
754         prev_sp.reset (curr_ap.release());
755         return;
756     }
757 
758     const uint32_t num_curr_frames = curr_ap->GetNumFrames (false);
759 
760     if (num_curr_frames > 1)
761     {
762 #if defined (DEBUG_STACK_FRAMES)
763         s.PutCString("We have more than one current frame, so use current frames...\n");
764 #endif
765         // We have more than one current frames it means we have all the frames
766         // and can safely replace our previous frames.
767         prev_sp.reset (curr_ap.release());
768 
769 #if defined (DEBUG_STACK_FRAMES)
770         s.PutCString("\nMerged:\n");
771         prev_sp->Dump (&s);
772 #endif
773         return;
774     }
775 
776     StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0));
777     StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0));
778     StackID curr_stack_id (curr_frame_zero_sp->GetStackID());
779     StackID prev_stack_id (prev_frame_zero_sp->GetStackID());
780 
781 #if defined (DEBUG_STACK_FRAMES)
782     const uint32_t num_prev_frames = prev_sp->GetNumFrames (false);
783     s.Printf("\n%u previous frames with one current frame\n", num_prev_frames);
784 #endif
785 
786     // We have only a single current frame
787     // Our previous stack frames only had a single frame as well...
788     if (curr_stack_id == prev_stack_id)
789     {
790 #if defined (DEBUG_STACK_FRAMES)
791         s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n");
792 #endif
793 
794         curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp);
795 //        prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp);
796 //        prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp);
797     }
798     else if (curr_stack_id < prev_stack_id)
799     {
800 #if defined (DEBUG_STACK_FRAMES)
801         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");
802 #endif
803         prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp);
804     }
805 
806     curr_ap.release();
807 
808 #if defined (DEBUG_STACK_FRAMES)
809     s.PutCString("\nMerged:\n");
810     prev_sp->Dump (&s);
811 #endif
812 
813 
814 }
815 
816 lldb::StackFrameSP
817 StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
818 {
819     const_iterator pos;
820     const_iterator begin = m_frames.begin();
821     const_iterator end = m_frames.end();
822     lldb::StackFrameSP ret_sp;
823 
824     for (pos = begin; pos != end; ++pos)
825     {
826         if (pos->get() == stack_frame_ptr)
827         {
828             ret_sp = (*pos);
829             break;
830         }
831     }
832     return ret_sp;
833 }
834 
835 size_t
836 StackFrameList::GetStatus (Stream& strm,
837                            uint32_t first_frame,
838                            uint32_t num_frames,
839                            bool show_frame_info,
840                            uint32_t num_frames_with_source)
841 {
842     size_t num_frames_displayed = 0;
843 
844     if (num_frames == 0)
845         return 0;
846 
847     StackFrameSP frame_sp;
848     uint32_t frame_idx = 0;
849     uint32_t last_frame;
850 
851     // Don't let the last frame wrap around...
852     if (num_frames == UINT32_MAX)
853         last_frame = UINT32_MAX;
854     else
855         last_frame = first_frame + num_frames;
856 
857     for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx)
858     {
859         frame_sp = GetFrameAtIndex(frame_idx);
860         if (frame_sp.get() == NULL)
861             break;
862 
863         if (!frame_sp->GetStatus (strm,
864                                   show_frame_info,
865                                   num_frames_with_source > (first_frame - frame_idx)))
866             break;
867         ++num_frames_displayed;
868     }
869 
870     strm.IndentLess();
871     return num_frames_displayed;
872 }
873 
874