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