blob: 36223db8186d544b3257ab77bc24d520e00a832e [file] [log] [blame]
Jason Molendaab4f1922010-10-25 11:12:07 +00001//===-- UnwindLLDB.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 Claytondc5eb692011-04-25 18:36:36 +000010#include "lldb/Core/Module.h"
11#include "lldb/Core/Log.h"
12#include "lldb/Symbol/FuncUnwinders.h"
13#include "lldb/Symbol/Function.h"
14#include "lldb/Symbol/UnwindPlan.h"
Jason Molendaab4f1922010-10-25 11:12:07 +000015#include "lldb/Target/Thread.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
Jason Molendaab4f1922010-10-25 11:12:07 +000019
Greg Claytone576ab22011-02-15 00:19:15 +000020#include "UnwindLLDB.h"
21#include "RegisterContextLLDB.h"
22
Jason Molendaab4f1922010-10-25 11:12:07 +000023using namespace lldb;
24using namespace lldb_private;
25
26UnwindLLDB::UnwindLLDB (Thread &thread) :
27 Unwind (thread),
Jim Inghamb0c72a52012-02-29 03:40:22 +000028 m_frames(),
29 m_unwind_complete(false)
Jason Molendaab4f1922010-10-25 11:12:07 +000030{
31}
32
33uint32_t
Jim Ingham8f077162011-10-21 01:49:48 +000034UnwindLLDB::DoGetFrameCount()
Jason Molendaab4f1922010-10-25 11:12:07 +000035{
Jim Inghamb0c72a52012-02-29 03:40:22 +000036 if (!m_unwind_complete)
Jason Molendaab4f1922010-10-25 11:12:07 +000037 {
Greg Clayton58be07b2011-01-07 06:08:19 +000038//#define DEBUG_FRAME_SPEED 1
39#if DEBUG_FRAME_SPEED
Greg Clayton3e06bd92011-01-09 21:07:35 +000040#define FRAME_COUNT 10000
Greg Clayton58be07b2011-01-07 06:08:19 +000041 TimeValue time_value (TimeValue::Now());
42#endif
Jason Molenda8fed2952010-11-09 02:31:21 +000043 if (!AddFirstFrame ())
Jason Molendaab4f1922010-10-25 11:12:07 +000044 return 0;
Greg Clayton9b72eb72011-05-24 23:06:02 +000045
Greg Clayton1ac04c32012-02-21 00:09:25 +000046 ProcessSP process_sp (m_thread.GetProcess());
47 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
Greg Clayton9b72eb72011-05-24 23:06:02 +000048
49 while (AddOneMoreFrame (abi))
Greg Clayton58be07b2011-01-07 06:08:19 +000050 {
51#if DEBUG_FRAME_SPEED
Greg Clayton3e06bd92011-01-09 21:07:35 +000052 if ((m_frames.size() % FRAME_COUNT) == 0)
Greg Clayton58be07b2011-01-07 06:08:19 +000053 {
54 TimeValue now(TimeValue::Now());
55 uint64_t delta_t = now - time_value;
Daniel Malead01b2952012-11-29 21:49:15 +000056 printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n",
Greg Clayton3e06bd92011-01-09 21:07:35 +000057 FRAME_COUNT,
Peter Collingbourneba23ca02011-06-18 23:52:14 +000058 delta_t / TimeValue::NanoSecPerSec,
59 delta_t % TimeValue::NanoSecPerSec,
60 (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec));
Greg Clayton58be07b2011-01-07 06:08:19 +000061 time_value = now;
62 }
63#endif
64 }
Jason Molendaab4f1922010-10-25 11:12:07 +000065 }
66 return m_frames.size ();
67}
68
69bool
Jason Molenda8fed2952010-11-09 02:31:21 +000070UnwindLLDB::AddFirstFrame ()
71{
Jim Inghamb0c72a52012-02-29 03:40:22 +000072 if (m_frames.size() > 0)
73 return true;
74
Jason Molenda8fed2952010-11-09 02:31:21 +000075 // First, set up the 0th (initial) frame
76 CursorSP first_cursor_sp(new Cursor ());
Greg Claytone1cd1be2012-01-29 20:56:30 +000077 RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread,
78 RegisterContextLLDBSP(),
79 first_cursor_sp->sctx,
80 0, *this));
Greg Clayton9b72eb72011-05-24 23:06:02 +000081 if (reg_ctx_sp.get() == NULL)
Jim Inghamb0c72a52012-02-29 03:40:22 +000082 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +000083
Greg Clayton9b72eb72011-05-24 23:06:02 +000084 if (!reg_ctx_sp->IsValid())
Jim Inghamb0c72a52012-02-29 03:40:22 +000085 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +000086
Greg Clayton9b72eb72011-05-24 23:06:02 +000087 if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
Jim Inghamb0c72a52012-02-29 03:40:22 +000088 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +000089
Greg Clayton9b72eb72011-05-24 23:06:02 +000090 if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
Jim Inghamb0c72a52012-02-29 03:40:22 +000091 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +000092
93 // Everything checks out, so release the auto pointer value and let the
94 // cursor own it in its shared pointer
Greg Claytone1cd1be2012-01-29 20:56:30 +000095 first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
Jason Molenda8fed2952010-11-09 02:31:21 +000096 m_frames.push_back (first_cursor_sp);
97 return true;
Jason Molenda3d219752013-12-20 01:05:11 +000098
Jim Inghamb0c72a52012-02-29 03:40:22 +000099unwind_done:
Jason Molenda3d219752013-12-20 01:05:11 +0000100 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
101 if (log)
102 {
103 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
104 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000105 m_unwind_complete = true;
106 return false;
Jason Molenda8fed2952010-11-09 02:31:21 +0000107}
108
109// For adding a non-zero stack frame to m_frames.
110bool
Greg Clayton9b72eb72011-05-24 23:06:02 +0000111UnwindLLDB::AddOneMoreFrame (ABI *abi)
Jason Molenda8fed2952010-11-09 02:31:21 +0000112{
Jim Inghamb0c72a52012-02-29 03:40:22 +0000113 // If we've already gotten to the end of the stack, don't bother to try again...
114 if (m_unwind_complete)
115 return false;
116
Greg Clayton5160ce52013-03-27 23:08:40 +0000117 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
Jason Molenda8fed2952010-11-09 02:31:21 +0000118 CursorSP cursor_sp(new Cursor ());
Jason Molenda8fed2952010-11-09 02:31:21 +0000119
120 // Frame zero is a little different
121 if (m_frames.size() == 0)
122 return false;
123
124 uint32_t cur_idx = m_frames.size ();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000125 RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread,
126 m_frames[cur_idx - 1]->reg_ctx_lldb_sp,
127 cursor_sp->sctx,
128 cur_idx,
129 *this));
Jason Molenda9dbe9e62013-05-03 04:48:41 +0000130
131 // We want to detect an unwind that cycles erronously and stop backtracing.
132 // Don't want this maximum unwind limit to be too low -- if you have a backtrace
133 // with an "infinitely recursing" bug, it will crash when the stack blows out
134 // and the first 35,000 frames are uninteresting - it's the top most 5 frames that
135 // you actually care about. So you can't just cap the unwind at 10,000 or something.
136 // Realistically anything over around 200,000 is going to blow out the stack space.
137 // If we're still unwinding at that point, we're probably never going to finish.
138 if (cur_idx > 300000)
139 {
140 if (log)
141 log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.",
142 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
143 goto unwind_done;
144 }
145
Greg Clayton9b72eb72011-05-24 23:06:02 +0000146 if (reg_ctx_sp.get() == NULL)
Jason Molenda3d219752013-12-20 01:05:11 +0000147 {
148 if (log)
149 log->Printf ("%*sFrame %d did not get a RegisterContext, stopping.",
150 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
Jim Inghamb0c72a52012-02-29 03:40:22 +0000151 goto unwind_done;
Jason Molenda3d219752013-12-20 01:05:11 +0000152 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000153
Greg Clayton9b72eb72011-05-24 23:06:02 +0000154 if (!reg_ctx_sp->IsValid())
Jason Molenda8fed2952010-11-09 02:31:21 +0000155 {
Jason Molenda8fed2952010-11-09 02:31:21 +0000156 if (log)
157 {
158 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
159 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
160 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000161 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000162 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000163 if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
Jason Molenda8fed2952010-11-09 02:31:21 +0000164 {
Jason Molenda8fed2952010-11-09 02:31:21 +0000165 if (log)
166 {
167 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
168 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
169 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000170 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000171 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000172 if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
Jason Molenda8fed2952010-11-09 02:31:21 +0000173 {
Jason Molenda4b792472014-01-03 22:06:25 +0000174 // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not have
175 // its (constructed) CFA aligned correctly -- don't do the abi alignment check for
176 // these.
Jason Molenda6223db272014-02-13 07:11:08 +0000177 if (reg_ctx_sp->IsTrapHandlerFrame() == false)
Jason Molenda8fed2952010-11-09 02:31:21 +0000178 {
Jason Molenda4b792472014-01-03 22:06:25 +0000179 if (log)
180 {
181 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
182 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
183 }
184 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000185 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000186 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000187 if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
Jason Molenda8fed2952010-11-09 02:31:21 +0000188 {
Jason Molenda8fed2952010-11-09 02:31:21 +0000189 if (log)
190 {
191 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
192 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
193 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000194 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000195 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000196 if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
197 {
198 if (log)
199 {
200 log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
201 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
202 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000203 goto unwind_done;
Greg Clayton9b72eb72011-05-24 23:06:02 +0000204 }
Jason Molenda3d219752013-12-20 01:05:11 +0000205 if (!m_frames.empty())
206 {
207 // Infinite loop where the current cursor is the same as the previous one...
208 if (m_frames.back()->start_pc == cursor_sp->start_pc && m_frames.back()->cfa == cursor_sp->cfa)
209 {
210 if (log)
211 log->Printf ("th%d pc of this frame is the same as the previous frame and CFAs for both frames are identical -- stopping unwind", m_thread.GetIndexID());
212 goto unwind_done;
213 }
214 }
Ashok Thirumurthi8b577302013-09-26 14:35:59 +0000215
Greg Claytone1cd1be2012-01-29 20:56:30 +0000216 cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
Jason Molenda8fed2952010-11-09 02:31:21 +0000217 m_frames.push_back (cursor_sp);
218 return true;
Jim Inghamb0c72a52012-02-29 03:40:22 +0000219
220unwind_done:
Jason Molenda3d219752013-12-20 01:05:11 +0000221 if (log)
222 {
223 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
224 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000225 m_unwind_complete = true;
226 return false;
Jason Molenda8fed2952010-11-09 02:31:21 +0000227}
228
229bool
Jim Ingham8f077162011-10-21 01:49:48 +0000230UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
Jason Molendaab4f1922010-10-25 11:12:07 +0000231{
Jason Molendaab4f1922010-10-25 11:12:07 +0000232 if (m_frames.size() == 0)
Jason Molenda8fed2952010-11-09 02:31:21 +0000233 {
234 if (!AddFirstFrame())
235 return false;
236 }
237
Greg Clayton1ac04c32012-02-21 00:09:25 +0000238 ProcessSP process_sp (m_thread.GetProcess());
239 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
Greg Clayton9b72eb72011-05-24 23:06:02 +0000240
241 while (idx >= m_frames.size() && AddOneMoreFrame (abi))
Jason Molenda8fed2952010-11-09 02:31:21 +0000242 ;
Jason Molendaab4f1922010-10-25 11:12:07 +0000243
244 if (idx < m_frames.size ())
245 {
Jason Molenda59762002010-11-04 00:53:20 +0000246 cfa = m_frames[idx]->cfa;
247 pc = m_frames[idx]->start_pc;
Jason Molendaab4f1922010-10-25 11:12:07 +0000248 return true;
249 }
250 return false;
251}
252
Greg Clayton5ccbd292011-01-06 22:15:06 +0000253lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000254UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame)
Jason Molendaab4f1922010-10-25 11:12:07 +0000255{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000256 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton671cabe2011-01-08 01:53:06 +0000257 uint32_t idx = frame->GetConcreteFrameIndex ();
Jason Molenda8fed2952010-11-09 02:31:21 +0000258
Jason Molendaab4f1922010-10-25 11:12:07 +0000259 if (idx == 0)
260 {
261 return m_thread.GetRegisterContext();
262 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000263
264 if (m_frames.size() == 0)
265 {
266 if (!AddFirstFrame())
Greg Clayton5ccbd292011-01-06 22:15:06 +0000267 return reg_ctx_sp;
Jason Molenda8fed2952010-11-09 02:31:21 +0000268 }
269
Greg Clayton1ac04c32012-02-21 00:09:25 +0000270 ProcessSP process_sp (m_thread.GetProcess());
271 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
Greg Clayton9b72eb72011-05-24 23:06:02 +0000272
Greg Claytone1cd1be2012-01-29 20:56:30 +0000273 while (idx >= m_frames.size())
274 {
275 if (!AddOneMoreFrame (abi))
276 break;
277 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000278
Greg Claytone1cd1be2012-01-29 20:56:30 +0000279 const uint32_t num_frames = m_frames.size();
280 if (idx < num_frames)
281 {
282 Cursor *frame_cursor = m_frames[idx].get();
Greg Claytone72dfb32012-02-24 01:59:29 +0000283 reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000284 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000285 return reg_ctx_sp;
Jason Molendaab4f1922010-10-25 11:12:07 +0000286}
Jason Molenda707fec42011-11-01 03:21:25 +0000287
Greg Claytone1cd1be2012-01-29 20:56:30 +0000288UnwindLLDB::RegisterContextLLDBSP
Jason Molenda707fec42011-11-01 03:21:25 +0000289UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num)
290{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000291 RegisterContextLLDBSP reg_ctx_sp;
292 if (frame_num < m_frames.size())
293 reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
Jason Molenda707fec42011-11-01 03:21:25 +0000294 return reg_ctx_sp;
295}
296
297bool
Jason Molenda23399d72013-06-05 00:12:50 +0000298UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc, uint32_t starting_frame_num, bool pc_reg)
Jason Molenda707fec42011-11-01 03:21:25 +0000299{
300 int64_t frame_num = starting_frame_num;
301 if (frame_num >= m_frames.size())
302 return false;
Jason Molenda60f0bd42012-10-26 06:08:58 +0000303
304 // Never interrogate more than one level while looking for the saved pc value. If the value
305 // isn't saved by frame_num, none of the frames lower on the stack will have a useful value.
Jason Molenda23399d72013-06-05 00:12:50 +0000306 if (pc_reg)
Jason Molenda60f0bd42012-10-26 06:08:58 +0000307 {
Jason Molendaaff2a262012-11-16 01:03:31 +0000308 UnwindLLDB::RegisterSearchResult result;
309 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
310 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
Jason Molenda60f0bd42012-10-26 06:08:58 +0000311 return true;
312 else
313 return false;
314 }
Jason Molenda707fec42011-11-01 03:21:25 +0000315 while (frame_num >= 0)
316 {
Jason Molendaaff2a262012-11-16 01:03:31 +0000317 UnwindLLDB::RegisterSearchResult result;
318 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
Jason Molenda4c781fd72013-01-19 03:53:42 +0000319
320 // If we have unwind instructions saying that register N is saved in register M in the middle of
321 // the stack (and N can equal M here, meaning the register was not used in this function), then
322 // change the register number we're looking for to M and keep looking for a concrete location
323 // down the stack, or an actual value from a live RegisterContext at frame 0.
324 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound
325 && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister
326 && frame_num > 0)
327 {
328 result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
329 lldb_regnum = regloc.location.register_number;
330 }
331
Jason Molendaaff2a262012-11-16 01:03:31 +0000332 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
Jason Molenda707fec42011-11-01 03:21:25 +0000333 return true;
Jason Molendaaff2a262012-11-16 01:03:31 +0000334 if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)
335 return false;
Jason Molenda707fec42011-11-01 03:21:25 +0000336 frame_num--;
337 }
338 return false;
339}