blob: f1cb91535eb6afb1ab26b90d791b186937a8884f [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Claytonf4124de2012-02-21 00:09:25 +000015#include "lldb/Target/ExecutionContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Target/Process.h"
17#include "lldb/Target/Target.h"
Greg Claytonf4124de2012-02-21 00:09:25 +000018#include "lldb/Target/Thread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019
20#include "RegisterContextMacOSXFrameBackchain.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25UnwindMacOSXFrameBackchain::UnwindMacOSXFrameBackchain (Thread &thread) :
26 Unwind (thread),
27 m_cursors()
28{
29}
30
31uint32_t
Jim Ingham591cf152011-10-21 01:49:48 +000032UnwindMacOSXFrameBackchain::DoGetFrameCount()
Chris Lattner24943d22010-06-08 16:52:24 +000033{
34 if (m_cursors.empty())
35 {
Greg Claytonf4124de2012-02-21 00:09:25 +000036 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 Lattner24943d22010-06-08 16:52:24 +000049 }
50 return m_cursors.size();
51}
52
53bool
Jim Ingham591cf152011-10-21 01:49:48 +000054UnwindMacOSXFrameBackchain::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
Chris Lattner24943d22010-06-08 16:52:24 +000055{
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 Clayton08d7d3a2011-01-06 22:15:06 +000072lldb::RegisterContextSP
Jim Ingham591cf152011-10-21 01:49:48 +000073UnwindMacOSXFrameBackchain::DoCreateRegisterContextForFrame (StackFrame *frame)
Chris Lattner24943d22010-06-08 16:52:24 +000074{
Greg Clayton08d7d3a2011-01-06 22:15:06 +000075 lldb::RegisterContextSP reg_ctx_sp;
76 uint32_t concrete_idx = frame->GetConcreteFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +000077 const uint32_t frame_count = GetFrameCount();
Greg Clayton08d7d3a2011-01-06 22:15:06 +000078 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 Lattner24943d22010-06-08 16:52:24 +000081}
82
83size_t
Greg Claytonf4124de2012-02-21 00:09:25 +000084UnwindMacOSXFrameBackchain::GetStackFrameData_i386 (const ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +000085{
86 m_cursors.clear();
Greg Claytonf4124de2012-02-21 00:09:25 +000087
88 StackFrame *first_frame = exe_ctx.GetFramePtr();
Chris Lattner24943d22010-06-08 16:52:24 +000089
Greg Claytonf4124de2012-02-21 00:09:25 +000090 Process *process = exe_ctx.GetProcessPtr();
91 if (process == NULL)
92 return 0;
93
Chris Lattner24943d22010-06-08 16:52:24 +000094 std::pair<lldb::addr_t, lldb::addr_t> fp_pc_pair;
95
Greg Claytonc0418152010-07-07 17:07:17 +000096 struct Frame_i386
Chris Lattner24943d22010-06-08 16:52:24 +000097 {
98 uint32_t fp;
99 uint32_t pc;
100 };
101
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000102 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000103 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 Claytonf4124de2012-02-21 00:09:25 +0000118 if (process->ReadMemory (frame.fp, &frame.fp, k_frame_size, error) != k_frame_size)
Chris Lattner24943d22010-06-08 16:52:24 +0000119 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 Claytonb04e7a82010-08-24 21:05:24 +0000146 if (first_frame->GetFrameCodeAddress() == addr_range_ptr->GetBaseAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000147 {
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 Claytonf4124de2012-02-21 00:09:25 +0000152 if (first_frame_sp && process->ReadMemory (first_frame_sp, &frame.pc, sizeof(frame.pc), error) == sizeof(frame.pc))
Chris Lattner24943d22010-06-08 16:52:24 +0000153 {
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
177size_t
Greg Claytonf4124de2012-02-21 00:09:25 +0000178UnwindMacOSXFrameBackchain::GetStackFrameData_x86_64 (const ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +0000179{
180 m_cursors.clear();
181
Greg Claytonf4124de2012-02-21 00:09:25 +0000182 Process *process = exe_ctx.GetProcessPtr();
183 if (process == NULL)
184 return 0;
185
186 StackFrame *first_frame = exe_ctx.GetFramePtr();
187
Chris Lattner24943d22010-06-08 16:52:24 +0000188 std::pair<lldb::addr_t, lldb::addr_t> fp_pc_pair;
189
Greg Claytonc0418152010-07-07 17:07:17 +0000190 struct Frame_x86_64
Chris Lattner24943d22010-06-08 16:52:24 +0000191 {
192 uint64_t fp;
193 uint64_t pc;
194 };
195
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000196 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000197 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 Claytonf4124de2012-02-21 00:09:25 +0000211 if (process->ReadMemory (frame.fp, &frame.fp, k_frame_size, error) != k_frame_size)
Chris Lattner24943d22010-06-08 16:52:24 +0000212 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 Claytonb04e7a82010-08-24 21:05:24 +0000240 if (first_frame->GetFrameCodeAddress() == addr_range_ptr->GetBaseAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000241 {
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 Claytonf4124de2012-02-21 00:09:25 +0000246 if (process->ReadMemory (first_frame_sp, &frame.pc, sizeof(frame.pc), error) == sizeof(frame.pc))
Chris Lattner24943d22010-06-08 16:52:24 +0000247 {
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