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