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