blob: f620b07835e20fa6be302456bebf480273c408a7 [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"
15#include "lldb/Target/Thread.h"
16#include "lldb/Target/Process.h"
17#include "lldb/Target/Target.h"
18
19#include "RegisterContextMacOSXFrameBackchain.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24UnwindMacOSXFrameBackchain::UnwindMacOSXFrameBackchain (Thread &thread) :
25 Unwind (thread),
26 m_cursors()
27{
28}
29
30uint32_t
31UnwindMacOSXFrameBackchain::GetFrameCount()
32{
33 if (m_cursors.empty())
34 {
35 const ArchSpec target_arch (m_thread.GetProcess().GetTarget().GetArchitecture ());
36 // Frame zero should always be supplied by the thread...
37 StackFrameSP frame_sp (m_thread.GetStackFrameAtIndex (0));
38 if (target_arch == ArchSpec("x86_64"))
39 GetStackFrameData_x86_64 (frame_sp.get());
40 else if (target_arch == ArchSpec("i386"))
41 GetStackFrameData_i386 (frame_sp.get());
42
43 }
44 return m_cursors.size();
45}
46
47bool
48UnwindMacOSXFrameBackchain::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
49{
50 const uint32_t frame_count = GetFrameCount();
51 if (idx < frame_count)
52 {
53 if (m_cursors[idx].pc == LLDB_INVALID_ADDRESS)
54 return false;
55 if (m_cursors[idx].fp == LLDB_INVALID_ADDRESS)
56 return false;
57
58 pc = m_cursors[idx].pc;
59 cfa = m_cursors[idx].fp;
60
61 return true;
62 }
63 return false;
64}
65
66RegisterContext *
67UnwindMacOSXFrameBackchain::CreateRegisterContextForFrame (StackFrame *frame)
68{
Greg Clayton4fb08152010-08-30 18:11:35 +000069 uint32_t idx = frame->GetUnwindFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +000070 const uint32_t frame_count = GetFrameCount();
71 if (idx < frame_count)
72 return new RegisterContextMacOSXFrameBackchain (m_thread, frame, m_cursors[idx]);
73 return NULL;
74}
75
76size_t
77UnwindMacOSXFrameBackchain::GetStackFrameData_i386 (StackFrame *first_frame)
78{
79 m_cursors.clear();
80
81 std::pair<lldb::addr_t, lldb::addr_t> fp_pc_pair;
82
Greg Claytonc0418152010-07-07 17:07:17 +000083 struct Frame_i386
Chris Lattner24943d22010-06-08 16:52:24 +000084 {
85 uint32_t fp;
86 uint32_t pc;
87 };
88
89 RegisterContext *reg_ctx = m_thread.GetRegisterContext();
90 assert (reg_ctx);
91
92 Cursor cursor;
93 cursor.pc = reg_ctx->GetPC (LLDB_INVALID_ADDRESS);
94 cursor.fp = reg_ctx->GetFP (0);
95
96 Frame_i386 frame = { cursor.fp, cursor.pc };
97
98 m_cursors.push_back(cursor);
99
100 const size_t k_frame_size = sizeof(frame);
101 Error error;
102 while (frame.fp != 0 && frame.pc != 0 && ((frame.fp & 7) == 0))
103 {
104 // Read both the FP and PC (8 bytes)
105 if (m_thread.GetProcess().ReadMemory (frame.fp, &frame.fp, k_frame_size, error) != k_frame_size)
106 break;
107 if (frame.pc >= 0x1000)
108 {
109 cursor.pc = frame.pc;
110 cursor.fp = frame.fp;
111 m_cursors.push_back (cursor);
112 }
113 }
114 if (!m_cursors.empty())
115 {
116 lldb::addr_t first_frame_pc = m_cursors.front().pc;
117 if (first_frame_pc != LLDB_INVALID_ADDRESS)
118 {
119 const uint32_t resolve_scope = eSymbolContextModule |
120 eSymbolContextCompUnit |
121 eSymbolContextFunction |
122 eSymbolContextSymbol;
123
124 SymbolContext first_frame_sc (first_frame->GetSymbolContext(resolve_scope));
125 const AddressRange *addr_range_ptr = NULL;
126 if (first_frame_sc.function)
127 addr_range_ptr = &first_frame_sc.function->GetAddressRange();
128 else if (first_frame_sc.symbol)
129 addr_range_ptr = first_frame_sc.symbol->GetAddressRangePtr();
130
131 if (addr_range_ptr)
132 {
Greg Claytonb04e7a82010-08-24 21:05:24 +0000133 if (first_frame->GetFrameCodeAddress() == addr_range_ptr->GetBaseAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000134 {
135 // We are at the first instruction, so we can recover the
136 // previous PC by dereferencing the SP
137 lldb::addr_t first_frame_sp = reg_ctx->GetSP (0);
138 // Read the real second frame return address into frame.pc
139 if (first_frame_sp && m_thread.GetProcess().ReadMemory (first_frame_sp, &frame.pc, sizeof(frame.pc), error) == sizeof(frame.pc))
140 {
141 cursor.fp = m_cursors.front().fp;
142 cursor.pc = frame.pc; // Set the new second frame PC
143
144 // Insert the second frame
145 m_cursors.insert(m_cursors.begin()+1, cursor);
146
147 m_cursors.front().fp = first_frame_sp;
148 }
149 }
150 }
151 }
152 }
153// uint32_t i=0;
154// printf(" PC FP\n");
155// printf(" ------------------ ------------------ \n");
156// for (i=0; i<m_cursors.size(); ++i)
157// {
158// printf("[%3u] 0x%16.16llx 0x%16.16llx\n", i, m_cursors[i].pc, m_cursors[i].fp);
159// }
160 return m_cursors.size();
161}
162
163
164size_t
165UnwindMacOSXFrameBackchain::GetStackFrameData_x86_64 (StackFrame *first_frame)
166{
167 m_cursors.clear();
168
169 std::pair<lldb::addr_t, lldb::addr_t> fp_pc_pair;
170
Greg Claytonc0418152010-07-07 17:07:17 +0000171 struct Frame_x86_64
Chris Lattner24943d22010-06-08 16:52:24 +0000172 {
173 uint64_t fp;
174 uint64_t pc;
175 };
176
177 RegisterContext *reg_ctx = m_thread.GetRegisterContext();
178 assert (reg_ctx);
179
180 Cursor cursor;
181 cursor.pc = reg_ctx->GetPC (LLDB_INVALID_ADDRESS);
182 cursor.fp = reg_ctx->GetFP (0);
183
184 Frame_x86_64 frame = { cursor.fp, cursor.pc };
185
186 m_cursors.push_back(cursor);
187 Error error;
188 const size_t k_frame_size = sizeof(frame);
189 while (frame.fp != 0 && frame.pc != 0 && ((frame.fp & 7) == 0))
190 {
191 // Read both the FP and PC (16 bytes)
192 if (m_thread.GetProcess().ReadMemory (frame.fp, &frame.fp, k_frame_size, error) != k_frame_size)
193 break;
194
195 if (frame.pc >= 0x1000)
196 {
197 cursor.pc = frame.pc;
198 cursor.fp = frame.fp;
199 m_cursors.push_back (cursor);
200 }
201 }
202 if (!m_cursors.empty())
203 {
204 lldb::addr_t first_frame_pc = m_cursors.front().pc;
205 if (first_frame_pc != LLDB_INVALID_ADDRESS)
206 {
207 const uint32_t resolve_scope = eSymbolContextModule |
208 eSymbolContextCompUnit |
209 eSymbolContextFunction |
210 eSymbolContextSymbol;
211
212 SymbolContext first_frame_sc(first_frame->GetSymbolContext(resolve_scope));
213 const AddressRange *addr_range_ptr = NULL;
214 if (first_frame_sc.function)
215 addr_range_ptr = &first_frame_sc.function->GetAddressRange();
216 else if (first_frame_sc.symbol)
217 addr_range_ptr = first_frame_sc.symbol->GetAddressRangePtr();
218
219 if (addr_range_ptr)
220 {
Greg Claytonb04e7a82010-08-24 21:05:24 +0000221 if (first_frame->GetFrameCodeAddress() == addr_range_ptr->GetBaseAddress())
Chris Lattner24943d22010-06-08 16:52:24 +0000222 {
223 // We are at the first instruction, so we can recover the
224 // previous PC by dereferencing the SP
225 lldb::addr_t first_frame_sp = reg_ctx->GetSP (0);
226 // Read the real second frame return address into frame.pc
227 if (m_thread.GetProcess().ReadMemory (first_frame_sp, &frame.pc, sizeof(frame.pc), error) == sizeof(frame.pc))
228 {
229 cursor.fp = m_cursors.front().fp;
230 cursor.pc = frame.pc; // Set the new second frame PC
231
232 // Insert the second frame
233 m_cursors.insert(m_cursors.begin()+1, cursor);
234
235 m_cursors.front().fp = first_frame_sp;
236 }
237 }
238 }
239 }
240 }
241 return m_cursors.size();
242}
243