Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 10 | #include "lldb/Target/StackFrameList.h" |
| 11 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 16 | #include "lldb/Core/StreamFile.h" |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 17 | #include "lldb/Core/SourceManager.h" |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/Block.h" |
| 19 | #include "lldb/Symbol/Function.h" |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/Symbol.h" |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Process.h" |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 22 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Target/StackFrame.h" |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Target.h" |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Thread.h" |
| 26 | #include "lldb/Target/Unwind.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 28 | //#define DEBUG_STACK_FRAMES 1 |
| 29 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | //---------------------------------------------------------------------- |
| 34 | // StackFrameList constructor |
| 35 | //---------------------------------------------------------------------- |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 36 | StackFrameList::StackFrameList |
| 37 | ( |
| 38 | Thread &thread, |
| 39 | const lldb::StackFrameListSP &prev_frames_sp, |
| 40 | bool show_inline_frames |
| 41 | ) : |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 42 | m_thread (thread), |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 43 | m_prev_frames_sp (prev_frames_sp), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | m_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 45 | m_frames (), |
Stephen Wilson | dbeb3e1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 46 | m_selected_frame_idx (0), |
| 47 | m_show_inlined_frames (show_inline_frames) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | //---------------------------------------------------------------------- |
| 52 | // Destructor |
| 53 | //---------------------------------------------------------------------- |
| 54 | StackFrameList::~StackFrameList() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | |
| 59 | uint32_t |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 60 | StackFrameList::GetNumFrames (bool can_create) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | { |
| 62 | Mutex::Locker locker (m_mutex); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 63 | |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 64 | if (can_create && m_frames.size() <= 1) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 65 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 66 | if (m_show_inlined_frames) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 67 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 68 | #if defined (DEBUG_STACK_FRAMES) |
Johnny Chen | 0a77f90 | 2011-08-25 17:27:02 +0000 | [diff] [blame] | 69 | StreamFile s(stdout, false); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 70 | #endif |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 71 | Unwind *unwinder = m_thread.GetUnwinder (); |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 72 | addr_t pc = LLDB_INVALID_ADDRESS; |
| 73 | addr_t cfa = LLDB_INVALID_ADDRESS; |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 74 | |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 75 | // If we are going to show inlined stack frames as actual frames, |
| 76 | // we need to calculate all concrete frames first, then iterate |
| 77 | // through all of them and count up how many inlined functions are |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 78 | // in each frame. |
| 79 | const uint32_t unwind_frame_count = unwinder->GetFrameCount(); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 80 | |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 81 | StackFrameSP unwind_frame_sp; |
| 82 | for (uint32_t idx=0; idx<unwind_frame_count; ++idx) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 83 | { |
| 84 | if (idx == 0) |
| 85 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 86 | // We might have already created frame zero, only create it |
| 87 | // if we need to |
| 88 | if (m_frames.empty()) |
| 89 | { |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 90 | cfa = m_thread.m_reg_context_sp->GetSP(); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 91 | m_thread.GetRegisterContext(); |
| 92 | unwind_frame_sp.reset (new StackFrame (m_frames.size(), |
| 93 | idx, |
| 94 | m_thread, |
| 95 | m_thread.m_reg_context_sp, |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 96 | cfa, |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 97 | m_thread.m_reg_context_sp->GetPC(), |
| 98 | NULL)); |
| 99 | m_frames.push_back (unwind_frame_sp); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | unwind_frame_sp = m_frames.front(); |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 104 | cfa = unwind_frame_sp->m_id.GetCallFrameAddress(); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 105 | } |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | const bool success = unwinder->GetFrameInfoAtIndex(idx, cfa, pc); |
| 110 | assert (success); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 111 | unwind_frame_sp.reset (new StackFrame (m_frames.size(), idx, m_thread, cfa, pc, NULL)); |
| 112 | m_frames.push_back (unwind_frame_sp); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 113 | } |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 114 | |
Greg Clayton | 2f57db0 | 2011-10-01 00:45:15 +0000 | [diff] [blame^] | 115 | SymbolContext unwind_sc = unwind_frame_sp->GetSymbolContext (eSymbolContextBlock | eSymbolContextFunction); |
| 116 | Block *unwind_block = unwind_sc.block; |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 117 | if (unwind_block) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 118 | { |
Greg Clayton | 2f57db0 | 2011-10-01 00:45:15 +0000 | [diff] [blame^] | 119 | Address curr_frame_address = unwind_frame_sp->GetFrameCodeAddress(); |
| 120 | SymbolContext next_frame_sc; |
| 121 | Address next_frame_address; |
| 122 | |
| 123 | while (unwind_sc.GetParentOfInlinedScope(curr_frame_address, next_frame_sc, next_frame_address)) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 124 | { |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 125 | StackFrameSP frame_sp(new StackFrame (m_frames.size(), |
| 126 | idx, |
| 127 | m_thread, |
| 128 | unwind_frame_sp->GetRegisterContextSP (), |
| 129 | cfa, |
Greg Clayton | 2f57db0 | 2011-10-01 00:45:15 +0000 | [diff] [blame^] | 130 | next_frame_address, |
| 131 | &next_frame_sc)); |
| 132 | |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 133 | m_frames.push_back (frame_sp); |
Greg Clayton | 2f57db0 | 2011-10-01 00:45:15 +0000 | [diff] [blame^] | 134 | unwind_sc = next_frame_sc; |
| 135 | curr_frame_address = next_frame_address; |
| 136 | |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 140 | |
| 141 | if (m_prev_frames_sp) |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 142 | { |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 143 | StackFrameList *prev_frames = m_prev_frames_sp.get(); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 144 | StackFrameList *curr_frames = this; |
| 145 | |
| 146 | #if defined (DEBUG_STACK_FRAMES) |
Greg Clayton | 870a1cd | 2010-08-27 21:47:54 +0000 | [diff] [blame] | 147 | s.PutCString("\nprev_frames:\n"); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 148 | prev_frames->Dump (&s); |
Greg Clayton | 870a1cd | 2010-08-27 21:47:54 +0000 | [diff] [blame] | 149 | s.PutCString("\ncurr_frames:\n"); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 150 | curr_frames->Dump (&s); |
| 151 | s.EOL(); |
| 152 | #endif |
| 153 | size_t curr_frame_num, prev_frame_num; |
| 154 | |
| 155 | for (curr_frame_num = curr_frames->m_frames.size(), prev_frame_num = prev_frames->m_frames.size(); |
| 156 | curr_frame_num > 0 && prev_frame_num > 0; |
| 157 | --curr_frame_num, --prev_frame_num) |
| 158 | { |
| 159 | const size_t curr_frame_idx = curr_frame_num-1; |
| 160 | const size_t prev_frame_idx = prev_frame_num-1; |
| 161 | StackFrameSP curr_frame_sp (curr_frames->m_frames[curr_frame_idx]); |
| 162 | StackFrameSP prev_frame_sp (prev_frames->m_frames[prev_frame_idx]); |
| 163 | |
| 164 | #if defined (DEBUG_STACK_FRAMES) |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 165 | s.Printf("\n\nCurr frame #%u ", curr_frame_idx); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 166 | if (curr_frame_sp) |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 167 | curr_frame_sp->Dump (&s, true, false); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 168 | else |
| 169 | s.PutCString("NULL"); |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 170 | s.Printf("\nPrev frame #%u ", prev_frame_idx); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 171 | if (prev_frame_sp) |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 172 | prev_frame_sp->Dump (&s, true, false); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 173 | else |
| 174 | s.PutCString("NULL"); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 175 | #endif |
| 176 | |
| 177 | StackFrame *curr_frame = curr_frame_sp.get(); |
| 178 | StackFrame *prev_frame = prev_frame_sp.get(); |
| 179 | |
| 180 | if (curr_frame == NULL || prev_frame == NULL) |
| 181 | break; |
| 182 | |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 183 | // Check the stack ID to make sure they are equal |
| 184 | if (curr_frame->GetStackID() != prev_frame->GetStackID()) |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 185 | break; |
| 186 | |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 187 | prev_frame->UpdatePreviousFrameFromCurrentFrame (*curr_frame); |
| 188 | // Now copy the fixed up previous frame into the current frames |
| 189 | // so the pointer doesn't change |
| 190 | m_frames[curr_frame_idx] = prev_frame_sp; |
| 191 | //curr_frame->UpdateCurrentFrameFromPreviousFrame (*prev_frame); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 192 | |
| 193 | #if defined (DEBUG_STACK_FRAMES) |
Greg Clayton | 870a1cd | 2010-08-27 21:47:54 +0000 | [diff] [blame] | 194 | s.Printf("\n Copying previous frame to current frame"); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 195 | #endif |
| 196 | } |
| 197 | // We are done with the old stack frame list, we can release it now |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 198 | m_prev_frames_sp.reset(); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 199 | } |
Greg Clayton | 870a1cd | 2010-08-27 21:47:54 +0000 | [diff] [blame] | 200 | |
| 201 | #if defined (DEBUG_STACK_FRAMES) |
| 202 | s.PutCString("\n\nNew frames:\n"); |
| 203 | Dump (&s); |
| 204 | s.EOL(); |
| 205 | #endif |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 206 | } |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 207 | else |
| 208 | { |
| 209 | m_frames.resize(m_thread.GetUnwinder()->GetFrameCount()); |
| 210 | } |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 211 | } |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 212 | return m_frames.size(); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | StackFrameList::Dump (Stream *s) |
| 217 | { |
| 218 | if (s == NULL) |
| 219 | return; |
| 220 | Mutex::Locker locker (m_mutex); |
| 221 | |
| 222 | const_iterator pos, begin = m_frames.begin(), end = m_frames.end(); |
| 223 | for (pos = begin; pos != end; ++pos) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 224 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 225 | StackFrame *frame = (*pos).get(); |
| 226 | s->Printf("%p: ", frame); |
| 227 | if (frame) |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 228 | { |
| 229 | frame->GetStackID().Dump (s); |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 230 | frame->DumpUsingSettingsFormat (s); |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 231 | } |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 232 | else |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 233 | s->Printf("frame #%ld", std::distance (begin, pos)); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 234 | s->EOL(); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 235 | } |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 236 | s->EOL(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | StackFrameSP |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 240 | StackFrameList::GetFrameAtIndex (uint32_t idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | { |
| 242 | StackFrameSP frame_sp; |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 243 | Mutex::Locker locker (m_mutex); |
| 244 | if (idx < m_frames.size()) |
| 245 | frame_sp = m_frames[idx]; |
| 246 | |
| 247 | if (frame_sp) |
| 248 | return frame_sp; |
| 249 | |
| 250 | // Special case the first frame (idx == 0) so that we don't need to |
| 251 | // know how many stack frames there are to get it. If we need any other |
| 252 | // frames, then we do need to know if "idx" is a valid index. |
| 253 | if (idx == 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 255 | // If this is the first frame, we want to share the thread register |
| 256 | // context with the stack frame at index zero. |
| 257 | m_thread.GetRegisterContext(); |
| 258 | assert (m_thread.m_reg_context_sp.get()); |
| 259 | frame_sp.reset (new StackFrame (0, |
| 260 | 0, |
| 261 | m_thread, |
| 262 | m_thread.m_reg_context_sp, |
| 263 | m_thread.m_reg_context_sp->GetSP(), |
| 264 | m_thread.m_reg_context_sp->GetPC(), |
| 265 | NULL)); |
Greg Clayton | f40e308 | 2010-08-26 02:28:22 +0000 | [diff] [blame] | 266 | |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 267 | SetFrameAtIndex(idx, frame_sp); |
| 268 | } |
| 269 | else if (idx < GetNumFrames()) |
| 270 | { |
| 271 | if (m_show_inlined_frames) |
| 272 | { |
| 273 | // When inline frames are enabled we cache up all frames in GetNumFrames() |
| 274 | frame_sp = m_frames[idx]; |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 275 | } |
| 276 | else |
| 277 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 278 | Unwind *unwinder = m_thread.GetUnwinder (); |
| 279 | if (unwinder) |
| 280 | { |
| 281 | addr_t pc, cfa; |
| 282 | if (unwinder->GetFrameInfoAtIndex(idx, cfa, pc)) |
| 283 | { |
| 284 | frame_sp.reset (new StackFrame (idx, idx, m_thread, cfa, pc, NULL)); |
Greg Clayton | 4fb0815 | 2010-08-30 18:11:35 +0000 | [diff] [blame] | 285 | |
| 286 | Function *function = frame_sp->GetSymbolContext (eSymbolContextFunction).function; |
| 287 | if (function) |
| 288 | { |
| 289 | // When we aren't showing inline functions we always use |
| 290 | // the top most function block as the scope. |
| 291 | frame_sp->SetSymbolContextScope (&function->GetBlock(false)); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | // Set the symbol scope from the symbol regardless if it is NULL or valid. |
| 296 | frame_sp->SetSymbolContextScope (frame_sp->GetSymbolContext (eSymbolContextSymbol).symbol); |
| 297 | } |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 298 | SetFrameAtIndex(idx, frame_sp); |
| 299 | } |
| 300 | } |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | } |
| 303 | return frame_sp; |
| 304 | } |
| 305 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 306 | StackFrameSP |
| 307 | StackFrameList::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) |
| 308 | { |
| 309 | // First try assuming the unwind index is the same as the frame index. The |
| 310 | // unwind index is always greater than or equal to the frame index, so it |
| 311 | // is a good place to start. If we have inlined frames we might have 5 |
| 312 | // concrete frames (frame unwind indexes go from 0-4), but we might have 15 |
| 313 | // frames after we make all the inlined frames. Most of the time the unwind |
| 314 | // frame index (or the concrete frame index) is the same as the frame index. |
| 315 | uint32_t frame_idx = unwind_idx; |
| 316 | StackFrameSP frame_sp (GetFrameAtIndex (frame_idx)); |
| 317 | while (frame_sp) |
| 318 | { |
| 319 | if (frame_sp->GetFrameIndex() == unwind_idx) |
| 320 | break; |
| 321 | frame_sp = GetFrameAtIndex (++frame_idx); |
| 322 | } |
| 323 | return frame_sp; |
| 324 | } |
| 325 | |
Jim Ingham | 5c4b160 | 2011-03-31 00:15:49 +0000 | [diff] [blame] | 326 | StackFrameSP |
| 327 | StackFrameList::GetFrameWithStackID (StackID &stack_id) |
| 328 | { |
| 329 | uint32_t frame_idx = 0; |
| 330 | StackFrameSP frame_sp; |
| 331 | do |
| 332 | { |
| 333 | frame_sp = GetFrameAtIndex (frame_idx); |
| 334 | if (frame_sp && frame_sp->GetStackID() == stack_id) |
| 335 | break; |
| 336 | frame_idx++; |
| 337 | } |
| 338 | while (frame_sp); |
| 339 | return frame_sp; |
| 340 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 341 | |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 342 | bool |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 343 | StackFrameList::SetFrameAtIndex (uint32_t idx, StackFrameSP &frame_sp) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 344 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 345 | if (idx >= m_frames.size()) |
| 346 | m_frames.resize(idx + 1); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 347 | // Make sure allocation succeeded by checking bounds again |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 348 | if (idx < m_frames.size()) |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 349 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 350 | m_frames[idx] = frame_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | return true; |
| 352 | } |
| 353 | return false; // resize failed, out of memory? |
| 354 | } |
| 355 | |
| 356 | uint32_t |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 357 | StackFrameList::GetSelectedFrameIndex () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | { |
| 359 | Mutex::Locker locker (m_mutex); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 360 | return m_selected_frame_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | uint32_t |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 365 | StackFrameList::SetSelectedFrame (lldb_private::StackFrame *frame) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | { |
| 367 | Mutex::Locker locker (m_mutex); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 368 | const_iterator pos; |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 369 | const_iterator begin = m_frames.begin(); |
| 370 | const_iterator end = m_frames.end(); |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 371 | m_selected_frame_idx = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | for (pos = begin; pos != end; ++pos) |
| 373 | { |
| 374 | if (pos->get() == frame) |
| 375 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 376 | m_selected_frame_idx = std::distance (begin, pos); |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 377 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 380 | SetDefaultFileAndLineToSelectedFrame(); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 381 | return m_selected_frame_idx; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Mark a stack frame as the current frame using the frame index |
| 385 | void |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 386 | StackFrameList::SetSelectedFrameByIndex (uint32_t idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 387 | { |
| 388 | Mutex::Locker locker (m_mutex); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 389 | m_selected_frame_idx = idx; |
Jim Ingham | fdf24ef | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 390 | SetDefaultFileAndLineToSelectedFrame(); |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | StackFrameList::SetDefaultFileAndLineToSelectedFrame() |
| 395 | { |
| 396 | if (m_thread.GetID() == m_thread.GetProcess().GetThreadList().GetSelectedThread()->GetID()) |
| 397 | { |
| 398 | StackFrameSP frame_sp = m_frames[m_selected_frame_idx]; |
| 399 | if (frame_sp) |
| 400 | { |
| 401 | SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextEverything); |
| 402 | if (sc.line_entry.file) |
| 403 | m_thread.GetProcess().GetTarget().GetSourceManager().SetDefaultFileAndLine (sc.line_entry.file, sc.line_entry.line); |
| 404 | } |
| 405 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // The thread has been run, reset the number stack frames to zero so we can |
| 409 | // determine how many frames we have lazily. |
| 410 | void |
| 411 | StackFrameList::Clear () |
| 412 | { |
| 413 | Mutex::Locker locker (m_mutex); |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 414 | m_frames.clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void |
| 418 | StackFrameList::InvalidateFrames (uint32_t start_idx) |
| 419 | { |
| 420 | Mutex::Locker locker (m_mutex); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 421 | if (m_show_inlined_frames) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 422 | { |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 423 | Clear(); |
| 424 | } |
| 425 | else |
| 426 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 427 | const size_t num_frames = m_frames.size(); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 428 | while (start_idx < num_frames) |
| 429 | { |
Greg Clayton | 1d66ef5 | 2010-08-27 18:24:16 +0000 | [diff] [blame] | 430 | m_frames[start_idx].reset(); |
Greg Clayton | 782b9cc | 2010-08-25 00:35:26 +0000 | [diff] [blame] | 431 | ++start_idx; |
| 432 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 435 | |
| 436 | void |
| 437 | StackFrameList::Merge (std::auto_ptr<StackFrameList>& curr_ap, lldb::StackFrameListSP& prev_sp) |
| 438 | { |
| 439 | Mutex::Locker curr_locker (curr_ap.get() ? curr_ap->m_mutex.GetMutex() : NULL); |
| 440 | Mutex::Locker prev_locker (prev_sp.get() ? prev_sp->m_mutex.GetMutex() : NULL); |
| 441 | |
| 442 | #if defined (DEBUG_STACK_FRAMES) |
Johnny Chen | 0a77f90 | 2011-08-25 17:27:02 +0000 | [diff] [blame] | 443 | StreamFile s(stdout, false); |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 444 | s.PutCString("\n\nStackFrameList::Merge():\nPrev:\n"); |
| 445 | if (prev_sp.get()) |
| 446 | prev_sp->Dump (&s); |
| 447 | else |
| 448 | s.PutCString ("NULL"); |
| 449 | s.PutCString("\nCurr:\n"); |
| 450 | if (curr_ap.get()) |
| 451 | curr_ap->Dump (&s); |
| 452 | else |
| 453 | s.PutCString ("NULL"); |
| 454 | s.EOL(); |
| 455 | #endif |
| 456 | |
| 457 | if (curr_ap.get() == NULL || curr_ap->GetNumFrames (false) == 0) |
| 458 | { |
| 459 | #if defined (DEBUG_STACK_FRAMES) |
| 460 | s.PutCString("No current frames, leave previous frames alone...\n"); |
| 461 | #endif |
| 462 | curr_ap.release(); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | if (prev_sp.get() == NULL || prev_sp->GetNumFrames (false) == 0) |
| 467 | { |
| 468 | #if defined (DEBUG_STACK_FRAMES) |
| 469 | s.PutCString("No previous frames, so use current frames...\n"); |
| 470 | #endif |
| 471 | // We either don't have any previous frames, or since we have more than |
| 472 | // one current frames it means we have all the frames and can safely |
| 473 | // replace our previous frames. |
| 474 | prev_sp.reset (curr_ap.release()); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | const uint32_t num_curr_frames = curr_ap->GetNumFrames (false); |
| 479 | |
| 480 | if (num_curr_frames > 1) |
| 481 | { |
| 482 | #if defined (DEBUG_STACK_FRAMES) |
| 483 | s.PutCString("We have more than one current frame, so use current frames...\n"); |
| 484 | #endif |
| 485 | // We have more than one current frames it means we have all the frames |
| 486 | // and can safely replace our previous frames. |
| 487 | prev_sp.reset (curr_ap.release()); |
| 488 | |
| 489 | #if defined (DEBUG_STACK_FRAMES) |
| 490 | s.PutCString("\nMerged:\n"); |
| 491 | prev_sp->Dump (&s); |
| 492 | #endif |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | StackFrameSP prev_frame_zero_sp(prev_sp->GetFrameAtIndex (0)); |
| 497 | StackFrameSP curr_frame_zero_sp(curr_ap->GetFrameAtIndex (0)); |
| 498 | StackID curr_stack_id (curr_frame_zero_sp->GetStackID()); |
| 499 | StackID prev_stack_id (prev_frame_zero_sp->GetStackID()); |
| 500 | |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 501 | #if defined (DEBUG_STACK_FRAMES) |
Johnny Chen | 0a77f90 | 2011-08-25 17:27:02 +0000 | [diff] [blame] | 502 | const uint32_t num_prev_frames = prev_sp->GetNumFrames (false); |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 503 | s.Printf("\n%u previous frames with one current frame\n", num_prev_frames); |
| 504 | #endif |
| 505 | |
| 506 | // We have only a single current frame |
| 507 | // Our previous stack frames only had a single frame as well... |
| 508 | if (curr_stack_id == prev_stack_id) |
| 509 | { |
| 510 | #if defined (DEBUG_STACK_FRAMES) |
| 511 | s.Printf("\nPrevious frame #0 is same as current frame #0, merge the cached data\n"); |
| 512 | #endif |
| 513 | |
| 514 | curr_frame_zero_sp->UpdateCurrentFrameFromPreviousFrame (*prev_frame_zero_sp); |
| 515 | // prev_frame_zero_sp->UpdatePreviousFrameFromCurrentFrame (*curr_frame_zero_sp); |
| 516 | // prev_sp->SetFrameAtIndex (0, prev_frame_zero_sp); |
| 517 | } |
| 518 | else if (curr_stack_id < prev_stack_id) |
| 519 | { |
| 520 | #if defined (DEBUG_STACK_FRAMES) |
| 521 | 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"); |
| 522 | #endif |
| 523 | prev_sp->m_frames.insert (prev_sp->m_frames.begin(), curr_frame_zero_sp); |
| 524 | } |
| 525 | |
| 526 | curr_ap.release(); |
| 527 | |
| 528 | #if defined (DEBUG_STACK_FRAMES) |
| 529 | s.PutCString("\nMerged:\n"); |
| 530 | prev_sp->Dump (&s); |
| 531 | #endif |
| 532 | |
| 533 | |
| 534 | } |
Jim Ingham | ccd584d | 2010-09-23 17:40:12 +0000 | [diff] [blame] | 535 | |
| 536 | lldb::StackFrameSP |
| 537 | StackFrameList::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) |
| 538 | { |
| 539 | const_iterator pos; |
| 540 | const_iterator begin = m_frames.begin(); |
| 541 | const_iterator end = m_frames.end(); |
| 542 | lldb::StackFrameSP ret_sp; |
| 543 | |
| 544 | for (pos = begin; pos != end; ++pos) |
| 545 | { |
| 546 | if (pos->get() == stack_frame_ptr) |
| 547 | { |
| 548 | ret_sp = (*pos); |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | return ret_sp; |
| 553 | } |
| 554 | |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 555 | size_t |
| 556 | StackFrameList::GetStatus (Stream& strm, |
| 557 | uint32_t first_frame, |
| 558 | uint32_t num_frames, |
| 559 | bool show_frame_info, |
| 560 | uint32_t num_frames_with_source, |
| 561 | uint32_t source_lines_before, |
| 562 | uint32_t source_lines_after) |
| 563 | { |
| 564 | size_t num_frames_displayed = 0; |
| 565 | |
| 566 | if (num_frames == 0) |
| 567 | return 0; |
| 568 | |
| 569 | StackFrameSP frame_sp; |
| 570 | uint32_t frame_idx = 0; |
| 571 | uint32_t last_frame; |
| 572 | |
| 573 | // Don't let the last frame wrap around... |
| 574 | if (num_frames == UINT32_MAX) |
| 575 | last_frame = UINT32_MAX; |
| 576 | else |
| 577 | last_frame = first_frame + num_frames; |
| 578 | |
| 579 | for (frame_idx = first_frame; frame_idx < last_frame; ++frame_idx) |
| 580 | { |
| 581 | frame_sp = GetFrameAtIndex(frame_idx); |
| 582 | if (frame_sp.get() == NULL) |
| 583 | break; |
| 584 | |
| 585 | if (!frame_sp->GetStatus (strm, |
| 586 | show_frame_info, |
| 587 | num_frames_with_source > first_frame - frame_idx, |
| 588 | source_lines_before, |
| 589 | source_lines_after)) |
| 590 | break; |
| 591 | ++num_frames_displayed; |
| 592 | } |
| 593 | |
| 594 | strm.IndentLess(); |
| 595 | return num_frames_displayed; |
| 596 | } |
| 597 | |