Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- UnwindMacOSXFrameBackchain.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/Core/ArchSpec.h" |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 15 | #include "lldb/Target/ExecutionContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Process.h" |
| 17 | #include "lldb/Target/Target.h" |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 18 | #include "lldb/Target/Thread.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
| 20 | #include "RegisterContextMacOSXFrameBackchain.h" |
| 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | UnwindMacOSXFrameBackchain::UnwindMacOSXFrameBackchain (Thread &thread) : |
| 26 | Unwind (thread), |
| 27 | m_cursors() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | uint32_t |
Jim Ingham | 591cf15 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 32 | UnwindMacOSXFrameBackchain::DoGetFrameCount() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | { |
| 34 | if (m_cursors.empty()) |
| 35 | { |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 36 | ExecutionContext exe_ctx (m_thread.shared_from_this()); |
| 37 | Target *target = exe_ctx.GetTargetPtr(); |
| 38 | if (target) |
| 39 | { |
| 40 | const ArchSpec& target_arch = target->GetArchitecture (); |
| 41 | // Frame zero should always be supplied by the thread... |
| 42 | exe_ctx.SetFrameSP (m_thread.GetStackFrameAtIndex (0)); |
| 43 | |
| 44 | if (target_arch.GetAddressByteSize() == 8) |
| 45 | GetStackFrameData_x86_64 (exe_ctx); |
| 46 | else |
| 47 | GetStackFrameData_i386 (exe_ctx); |
| 48 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | } |
| 50 | return m_cursors.size(); |
| 51 | } |
| 52 | |
| 53 | bool |
Jim Ingham | 591cf15 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 54 | UnwindMacOSXFrameBackchain::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | { |
| 56 | const uint32_t frame_count = GetFrameCount(); |
| 57 | if (idx < frame_count) |
| 58 | { |
| 59 | if (m_cursors[idx].pc == LLDB_INVALID_ADDRESS) |
| 60 | return false; |
| 61 | if (m_cursors[idx].fp == LLDB_INVALID_ADDRESS) |
| 62 | return false; |
| 63 | |
| 64 | pc = m_cursors[idx].pc; |
| 65 | cfa = m_cursors[idx].fp; |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 72 | lldb::RegisterContextSP |
Jim Ingham | 591cf15 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 73 | UnwindMacOSXFrameBackchain::DoCreateRegisterContextForFrame (StackFrame *frame) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 75 | lldb::RegisterContextSP reg_ctx_sp; |
| 76 | uint32_t concrete_idx = frame->GetConcreteFrameIndex (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | const uint32_t frame_count = GetFrameCount(); |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 78 | if (concrete_idx < frame_count) |
| 79 | reg_ctx_sp.reset (new RegisterContextMacOSXFrameBackchain (m_thread, concrete_idx, m_cursors[concrete_idx])); |
| 80 | return reg_ctx_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | size_t |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 84 | UnwindMacOSXFrameBackchain::GetStackFrameData_i386 (const ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | { |
| 86 | m_cursors.clear(); |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 87 | |
| 88 | StackFrame *first_frame = exe_ctx.GetFramePtr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 90 | Process *process = exe_ctx.GetProcessPtr(); |
| 91 | if (process == NULL) |
| 92 | return 0; |
| 93 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | std::pair<lldb::addr_t, lldb::addr_t> fp_pc_pair; |
| 95 | |
Greg Clayton | c041815 | 2010-07-07 17:07:17 +0000 | [diff] [blame] | 96 | struct Frame_i386 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | { |
| 98 | uint32_t fp; |
| 99 | uint32_t pc; |
| 100 | }; |
| 101 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 102 | RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | assert (reg_ctx); |
| 104 | |
| 105 | Cursor cursor; |
| 106 | cursor.pc = reg_ctx->GetPC (LLDB_INVALID_ADDRESS); |
| 107 | cursor.fp = reg_ctx->GetFP (0); |
| 108 | |
| 109 | Frame_i386 frame = { cursor.fp, cursor.pc }; |
| 110 | |
| 111 | m_cursors.push_back(cursor); |
| 112 | |
| 113 | const size_t k_frame_size = sizeof(frame); |
| 114 | Error error; |
| 115 | while (frame.fp != 0 && frame.pc != 0 && ((frame.fp & 7) == 0)) |
| 116 | { |
| 117 | // Read both the FP and PC (8 bytes) |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 118 | if (process->ReadMemory (frame.fp, &frame.fp, k_frame_size, error) != k_frame_size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | break; |
| 120 | if (frame.pc >= 0x1000) |
| 121 | { |
| 122 | cursor.pc = frame.pc; |
| 123 | cursor.fp = frame.fp; |
| 124 | m_cursors.push_back (cursor); |
| 125 | } |
| 126 | } |
| 127 | if (!m_cursors.empty()) |
| 128 | { |
| 129 | lldb::addr_t first_frame_pc = m_cursors.front().pc; |
| 130 | if (first_frame_pc != LLDB_INVALID_ADDRESS) |
| 131 | { |
| 132 | const uint32_t resolve_scope = eSymbolContextModule | |
| 133 | eSymbolContextCompUnit | |
| 134 | eSymbolContextFunction | |
| 135 | eSymbolContextSymbol; |
| 136 | |
| 137 | SymbolContext first_frame_sc (first_frame->GetSymbolContext(resolve_scope)); |
| 138 | const AddressRange *addr_range_ptr = NULL; |
| 139 | if (first_frame_sc.function) |
| 140 | addr_range_ptr = &first_frame_sc.function->GetAddressRange(); |
| 141 | else if (first_frame_sc.symbol) |
| 142 | addr_range_ptr = first_frame_sc.symbol->GetAddressRangePtr(); |
| 143 | |
| 144 | if (addr_range_ptr) |
| 145 | { |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 146 | if (first_frame->GetFrameCodeAddress() == addr_range_ptr->GetBaseAddress()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | { |
| 148 | // We are at the first instruction, so we can recover the |
| 149 | // previous PC by dereferencing the SP |
| 150 | lldb::addr_t first_frame_sp = reg_ctx->GetSP (0); |
| 151 | // Read the real second frame return address into frame.pc |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 152 | if (first_frame_sp && process->ReadMemory (first_frame_sp, &frame.pc, sizeof(frame.pc), error) == sizeof(frame.pc)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | { |
| 154 | cursor.fp = m_cursors.front().fp; |
| 155 | cursor.pc = frame.pc; // Set the new second frame PC |
| 156 | |
| 157 | // Insert the second frame |
| 158 | m_cursors.insert(m_cursors.begin()+1, cursor); |
| 159 | |
| 160 | m_cursors.front().fp = first_frame_sp; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | // uint32_t i=0; |
| 167 | // printf(" PC FP\n"); |
| 168 | // printf(" ------------------ ------------------ \n"); |
| 169 | // for (i=0; i<m_cursors.size(); ++i) |
| 170 | // { |
| 171 | // printf("[%3u] 0x%16.16llx 0x%16.16llx\n", i, m_cursors[i].pc, m_cursors[i].fp); |
| 172 | // } |
| 173 | return m_cursors.size(); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | size_t |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 178 | UnwindMacOSXFrameBackchain::GetStackFrameData_x86_64 (const ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | { |
| 180 | m_cursors.clear(); |
| 181 | |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 182 | Process *process = exe_ctx.GetProcessPtr(); |
| 183 | if (process == NULL) |
| 184 | return 0; |
| 185 | |
| 186 | StackFrame *first_frame = exe_ctx.GetFramePtr(); |
| 187 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | std::pair<lldb::addr_t, lldb::addr_t> fp_pc_pair; |
| 189 | |
Greg Clayton | c041815 | 2010-07-07 17:07:17 +0000 | [diff] [blame] | 190 | struct Frame_x86_64 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 191 | { |
| 192 | uint64_t fp; |
| 193 | uint64_t pc; |
| 194 | }; |
| 195 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 196 | RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | assert (reg_ctx); |
| 198 | |
| 199 | Cursor cursor; |
| 200 | cursor.pc = reg_ctx->GetPC (LLDB_INVALID_ADDRESS); |
| 201 | cursor.fp = reg_ctx->GetFP (0); |
| 202 | |
| 203 | Frame_x86_64 frame = { cursor.fp, cursor.pc }; |
| 204 | |
| 205 | m_cursors.push_back(cursor); |
| 206 | Error error; |
| 207 | const size_t k_frame_size = sizeof(frame); |
| 208 | while (frame.fp != 0 && frame.pc != 0 && ((frame.fp & 7) == 0)) |
| 209 | { |
| 210 | // Read both the FP and PC (16 bytes) |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 211 | if (process->ReadMemory (frame.fp, &frame.fp, k_frame_size, error) != k_frame_size) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | break; |
| 213 | |
| 214 | if (frame.pc >= 0x1000) |
| 215 | { |
| 216 | cursor.pc = frame.pc; |
| 217 | cursor.fp = frame.fp; |
| 218 | m_cursors.push_back (cursor); |
| 219 | } |
| 220 | } |
| 221 | if (!m_cursors.empty()) |
| 222 | { |
| 223 | lldb::addr_t first_frame_pc = m_cursors.front().pc; |
| 224 | if (first_frame_pc != LLDB_INVALID_ADDRESS) |
| 225 | { |
| 226 | const uint32_t resolve_scope = eSymbolContextModule | |
| 227 | eSymbolContextCompUnit | |
| 228 | eSymbolContextFunction | |
| 229 | eSymbolContextSymbol; |
| 230 | |
| 231 | SymbolContext first_frame_sc(first_frame->GetSymbolContext(resolve_scope)); |
| 232 | const AddressRange *addr_range_ptr = NULL; |
| 233 | if (first_frame_sc.function) |
| 234 | addr_range_ptr = &first_frame_sc.function->GetAddressRange(); |
| 235 | else if (first_frame_sc.symbol) |
| 236 | addr_range_ptr = first_frame_sc.symbol->GetAddressRangePtr(); |
| 237 | |
| 238 | if (addr_range_ptr) |
| 239 | { |
Greg Clayton | b04e7a8 | 2010-08-24 21:05:24 +0000 | [diff] [blame] | 240 | if (first_frame->GetFrameCodeAddress() == addr_range_ptr->GetBaseAddress()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | { |
| 242 | // We are at the first instruction, so we can recover the |
| 243 | // previous PC by dereferencing the SP |
| 244 | lldb::addr_t first_frame_sp = reg_ctx->GetSP (0); |
| 245 | // Read the real second frame return address into frame.pc |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame^] | 246 | if (process->ReadMemory (first_frame_sp, &frame.pc, sizeof(frame.pc), error) == sizeof(frame.pc)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | { |
| 248 | cursor.fp = m_cursors.front().fp; |
| 249 | cursor.pc = frame.pc; // Set the new second frame PC |
| 250 | |
| 251 | // Insert the second frame |
| 252 | m_cursors.insert(m_cursors.begin()+1, cursor); |
| 253 | |
| 254 | m_cursors.front().fp = first_frame_sp; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | return m_cursors.size(); |
| 261 | } |
| 262 | |