blob: a3a7002ea099ca69a0445034ede7715bbf873e8a [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(),
Jason Molendaa4bea722014-02-14 05:06:49 +000029 m_unwind_complete(false),
30 m_user_supplied_trap_handler_functions()
Jason Molendaab4f1922010-10-25 11:12:07 +000031{
Jason Molendaa4bea722014-02-14 05:06:49 +000032 ProcessSP process_sp(thread.GetProcess());
33 if (process_sp)
34 {
35 Args args;
36 process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames (args);
37 size_t count = args.GetArgumentCount();
38 for (size_t i = 0; i < count; i++)
39 {
40 const char *func_name = args.GetArgumentAtIndex(i);
41 m_user_supplied_trap_handler_functions.push_back (ConstString (func_name));
42 }
43 }
Jason Molendaab4f1922010-10-25 11:12:07 +000044}
45
46uint32_t
Jim Ingham8f077162011-10-21 01:49:48 +000047UnwindLLDB::DoGetFrameCount()
Jason Molendaab4f1922010-10-25 11:12:07 +000048{
Jim Inghamb0c72a52012-02-29 03:40:22 +000049 if (!m_unwind_complete)
Jason Molendaab4f1922010-10-25 11:12:07 +000050 {
Greg Clayton58be07b2011-01-07 06:08:19 +000051//#define DEBUG_FRAME_SPEED 1
52#if DEBUG_FRAME_SPEED
Greg Clayton3e06bd92011-01-09 21:07:35 +000053#define FRAME_COUNT 10000
Greg Clayton58be07b2011-01-07 06:08:19 +000054 TimeValue time_value (TimeValue::Now());
55#endif
Jason Molenda8fed2952010-11-09 02:31:21 +000056 if (!AddFirstFrame ())
Jason Molendaab4f1922010-10-25 11:12:07 +000057 return 0;
Greg Clayton9b72eb72011-05-24 23:06:02 +000058
Greg Clayton1ac04c32012-02-21 00:09:25 +000059 ProcessSP process_sp (m_thread.GetProcess());
60 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
Greg Clayton9b72eb72011-05-24 23:06:02 +000061
62 while (AddOneMoreFrame (abi))
Greg Clayton58be07b2011-01-07 06:08:19 +000063 {
64#if DEBUG_FRAME_SPEED
Greg Clayton3e06bd92011-01-09 21:07:35 +000065 if ((m_frames.size() % FRAME_COUNT) == 0)
Greg Clayton58be07b2011-01-07 06:08:19 +000066 {
67 TimeValue now(TimeValue::Now());
68 uint64_t delta_t = now - time_value;
Daniel Malead01b2952012-11-29 21:49:15 +000069 printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n",
Greg Clayton3e06bd92011-01-09 21:07:35 +000070 FRAME_COUNT,
Peter Collingbourneba23ca02011-06-18 23:52:14 +000071 delta_t / TimeValue::NanoSecPerSec,
72 delta_t % TimeValue::NanoSecPerSec,
73 (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec));
Greg Clayton58be07b2011-01-07 06:08:19 +000074 time_value = now;
75 }
76#endif
77 }
Jason Molendaab4f1922010-10-25 11:12:07 +000078 }
79 return m_frames.size ();
80}
81
82bool
Jason Molenda8fed2952010-11-09 02:31:21 +000083UnwindLLDB::AddFirstFrame ()
84{
Jim Inghamb0c72a52012-02-29 03:40:22 +000085 if (m_frames.size() > 0)
86 return true;
87
Jason Molenda8fed2952010-11-09 02:31:21 +000088 // First, set up the 0th (initial) frame
89 CursorSP first_cursor_sp(new Cursor ());
Greg Claytone1cd1be2012-01-29 20:56:30 +000090 RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread,
91 RegisterContextLLDBSP(),
92 first_cursor_sp->sctx,
93 0, *this));
Greg Clayton9b72eb72011-05-24 23:06:02 +000094 if (reg_ctx_sp.get() == NULL)
Jim Inghamb0c72a52012-02-29 03:40:22 +000095 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +000096
Greg Clayton9b72eb72011-05-24 23:06:02 +000097 if (!reg_ctx_sp->IsValid())
Jim Inghamb0c72a52012-02-29 03:40:22 +000098 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +000099
Greg Clayton9b72eb72011-05-24 23:06:02 +0000100 if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
Jim Inghamb0c72a52012-02-29 03:40:22 +0000101 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000102
Greg Clayton9b72eb72011-05-24 23:06:02 +0000103 if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
Jim Inghamb0c72a52012-02-29 03:40:22 +0000104 goto unwind_done;
Greg Clayton5ccbd292011-01-06 22:15:06 +0000105
106 // Everything checks out, so release the auto pointer value and let the
107 // cursor own it in its shared pointer
Greg Claytone1cd1be2012-01-29 20:56:30 +0000108 first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
Jason Molenda8fed2952010-11-09 02:31:21 +0000109 m_frames.push_back (first_cursor_sp);
110 return true;
Jason Molenda3d219752013-12-20 01:05:11 +0000111
Jim Inghamb0c72a52012-02-29 03:40:22 +0000112unwind_done:
Jason Molenda3d219752013-12-20 01:05:11 +0000113 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
114 if (log)
115 {
116 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
117 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000118 m_unwind_complete = true;
119 return false;
Jason Molenda8fed2952010-11-09 02:31:21 +0000120}
121
122// For adding a non-zero stack frame to m_frames.
123bool
Greg Clayton9b72eb72011-05-24 23:06:02 +0000124UnwindLLDB::AddOneMoreFrame (ABI *abi)
Jason Molenda8fed2952010-11-09 02:31:21 +0000125{
Jim Inghamb0c72a52012-02-29 03:40:22 +0000126 // If we've already gotten to the end of the stack, don't bother to try again...
127 if (m_unwind_complete)
128 return false;
129
Greg Clayton5160ce52013-03-27 23:08:40 +0000130 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
Jason Molenda8fed2952010-11-09 02:31:21 +0000131 CursorSP cursor_sp(new Cursor ());
Jason Molenda8fed2952010-11-09 02:31:21 +0000132
133 // Frame zero is a little different
134 if (m_frames.size() == 0)
135 return false;
136
137 uint32_t cur_idx = m_frames.size ();
Greg Claytone1cd1be2012-01-29 20:56:30 +0000138 RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread,
139 m_frames[cur_idx - 1]->reg_ctx_lldb_sp,
140 cursor_sp->sctx,
141 cur_idx,
142 *this));
Jason Molenda9dbe9e62013-05-03 04:48:41 +0000143
144 // We want to detect an unwind that cycles erronously and stop backtracing.
145 // Don't want this maximum unwind limit to be too low -- if you have a backtrace
146 // with an "infinitely recursing" bug, it will crash when the stack blows out
147 // and the first 35,000 frames are uninteresting - it's the top most 5 frames that
148 // you actually care about. So you can't just cap the unwind at 10,000 or something.
149 // Realistically anything over around 200,000 is going to blow out the stack space.
150 // If we're still unwinding at that point, we're probably never going to finish.
151 if (cur_idx > 300000)
152 {
153 if (log)
154 log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.",
155 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
156 goto unwind_done;
157 }
158
Greg Clayton9b72eb72011-05-24 23:06:02 +0000159 if (reg_ctx_sp.get() == NULL)
Jason Molenda3d219752013-12-20 01:05:11 +0000160 {
161 if (log)
162 log->Printf ("%*sFrame %d did not get a RegisterContext, stopping.",
163 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
Jim Inghamb0c72a52012-02-29 03:40:22 +0000164 goto unwind_done;
Jason Molenda3d219752013-12-20 01:05:11 +0000165 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000166
Greg Clayton9b72eb72011-05-24 23:06:02 +0000167 if (!reg_ctx_sp->IsValid())
Jason Molenda8fed2952010-11-09 02:31:21 +0000168 {
Jason Molenda8fed2952010-11-09 02:31:21 +0000169 if (log)
170 {
171 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
172 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
173 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000174 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000175 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000176 if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
Jason Molenda8fed2952010-11-09 02:31:21 +0000177 {
Jason Molenda8fed2952010-11-09 02:31:21 +0000178 if (log)
179 {
180 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
181 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
182 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000183 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000184 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000185 if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
Jason Molenda8fed2952010-11-09 02:31:21 +0000186 {
Jason Molenda4b792472014-01-03 22:06:25 +0000187 // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not have
188 // its (constructed) CFA aligned correctly -- don't do the abi alignment check for
189 // these.
Jason Molenda6223db272014-02-13 07:11:08 +0000190 if (reg_ctx_sp->IsTrapHandlerFrame() == false)
Jason Molenda8fed2952010-11-09 02:31:21 +0000191 {
Jason Molenda4b792472014-01-03 22:06:25 +0000192 if (log)
193 {
194 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
195 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
196 }
197 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000198 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000199 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000200 if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
Jason Molenda8fed2952010-11-09 02:31:21 +0000201 {
Jason Molenda8fed2952010-11-09 02:31:21 +0000202 if (log)
203 {
204 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
205 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
206 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000207 goto unwind_done;
Jason Molenda8fed2952010-11-09 02:31:21 +0000208 }
Greg Clayton9b72eb72011-05-24 23:06:02 +0000209 if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
210 {
211 if (log)
212 {
213 log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
214 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
215 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000216 goto unwind_done;
Greg Clayton9b72eb72011-05-24 23:06:02 +0000217 }
Jason Molenda3d219752013-12-20 01:05:11 +0000218 if (!m_frames.empty())
219 {
220 // Infinite loop where the current cursor is the same as the previous one...
221 if (m_frames.back()->start_pc == cursor_sp->start_pc && m_frames.back()->cfa == cursor_sp->cfa)
222 {
223 if (log)
224 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());
225 goto unwind_done;
226 }
227 }
Ashok Thirumurthi8b577302013-09-26 14:35:59 +0000228
Greg Claytone1cd1be2012-01-29 20:56:30 +0000229 cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
Jason Molenda8fed2952010-11-09 02:31:21 +0000230 m_frames.push_back (cursor_sp);
231 return true;
Jim Inghamb0c72a52012-02-29 03:40:22 +0000232
233unwind_done:
Jason Molenda3d219752013-12-20 01:05:11 +0000234 if (log)
235 {
236 log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID());
237 }
Jim Inghamb0c72a52012-02-29 03:40:22 +0000238 m_unwind_complete = true;
239 return false;
Jason Molenda8fed2952010-11-09 02:31:21 +0000240}
241
242bool
Jim Ingham8f077162011-10-21 01:49:48 +0000243UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
Jason Molendaab4f1922010-10-25 11:12:07 +0000244{
Jason Molendaab4f1922010-10-25 11:12:07 +0000245 if (m_frames.size() == 0)
Jason Molenda8fed2952010-11-09 02:31:21 +0000246 {
247 if (!AddFirstFrame())
248 return false;
249 }
250
Greg Clayton1ac04c32012-02-21 00:09:25 +0000251 ProcessSP process_sp (m_thread.GetProcess());
252 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
Greg Clayton9b72eb72011-05-24 23:06:02 +0000253
254 while (idx >= m_frames.size() && AddOneMoreFrame (abi))
Jason Molenda8fed2952010-11-09 02:31:21 +0000255 ;
Jason Molendaab4f1922010-10-25 11:12:07 +0000256
257 if (idx < m_frames.size ())
258 {
Jason Molenda59762002010-11-04 00:53:20 +0000259 cfa = m_frames[idx]->cfa;
260 pc = m_frames[idx]->start_pc;
Jason Molendaab4f1922010-10-25 11:12:07 +0000261 return true;
262 }
263 return false;
264}
265
Greg Clayton5ccbd292011-01-06 22:15:06 +0000266lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000267UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame)
Jason Molendaab4f1922010-10-25 11:12:07 +0000268{
Greg Clayton5ccbd292011-01-06 22:15:06 +0000269 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton671cabe2011-01-08 01:53:06 +0000270 uint32_t idx = frame->GetConcreteFrameIndex ();
Jason Molenda8fed2952010-11-09 02:31:21 +0000271
Jason Molendaab4f1922010-10-25 11:12:07 +0000272 if (idx == 0)
273 {
274 return m_thread.GetRegisterContext();
275 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000276
277 if (m_frames.size() == 0)
278 {
279 if (!AddFirstFrame())
Greg Clayton5ccbd292011-01-06 22:15:06 +0000280 return reg_ctx_sp;
Jason Molenda8fed2952010-11-09 02:31:21 +0000281 }
282
Greg Clayton1ac04c32012-02-21 00:09:25 +0000283 ProcessSP process_sp (m_thread.GetProcess());
284 ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
Greg Clayton9b72eb72011-05-24 23:06:02 +0000285
Greg Claytone1cd1be2012-01-29 20:56:30 +0000286 while (idx >= m_frames.size())
287 {
288 if (!AddOneMoreFrame (abi))
289 break;
290 }
Jason Molenda8fed2952010-11-09 02:31:21 +0000291
Greg Claytone1cd1be2012-01-29 20:56:30 +0000292 const uint32_t num_frames = m_frames.size();
293 if (idx < num_frames)
294 {
295 Cursor *frame_cursor = m_frames[idx].get();
Greg Claytone72dfb32012-02-24 01:59:29 +0000296 reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000297 }
Greg Clayton5ccbd292011-01-06 22:15:06 +0000298 return reg_ctx_sp;
Jason Molendaab4f1922010-10-25 11:12:07 +0000299}
Jason Molenda707fec42011-11-01 03:21:25 +0000300
Greg Claytone1cd1be2012-01-29 20:56:30 +0000301UnwindLLDB::RegisterContextLLDBSP
Jason Molenda707fec42011-11-01 03:21:25 +0000302UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num)
303{
Greg Claytone1cd1be2012-01-29 20:56:30 +0000304 RegisterContextLLDBSP reg_ctx_sp;
305 if (frame_num < m_frames.size())
306 reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
Jason Molenda707fec42011-11-01 03:21:25 +0000307 return reg_ctx_sp;
308}
309
310bool
Jason Molenda23399d72013-06-05 00:12:50 +0000311UnwindLLDB::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 +0000312{
313 int64_t frame_num = starting_frame_num;
314 if (frame_num >= m_frames.size())
315 return false;
Jason Molenda60f0bd42012-10-26 06:08:58 +0000316
317 // Never interrogate more than one level while looking for the saved pc value. If the value
318 // 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 +0000319 if (pc_reg)
Jason Molenda60f0bd42012-10-26 06:08:58 +0000320 {
Jason Molendaaff2a262012-11-16 01:03:31 +0000321 UnwindLLDB::RegisterSearchResult result;
322 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
323 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
Jason Molenda60f0bd42012-10-26 06:08:58 +0000324 return true;
325 else
326 return false;
327 }
Jason Molenda707fec42011-11-01 03:21:25 +0000328 while (frame_num >= 0)
329 {
Jason Molendaaff2a262012-11-16 01:03:31 +0000330 UnwindLLDB::RegisterSearchResult result;
331 result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
Jason Molenda4c781fd72013-01-19 03:53:42 +0000332
333 // If we have unwind instructions saying that register N is saved in register M in the middle of
334 // the stack (and N can equal M here, meaning the register was not used in this function), then
335 // change the register number we're looking for to M and keep looking for a concrete location
336 // down the stack, or an actual value from a live RegisterContext at frame 0.
337 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound
338 && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister
339 && frame_num > 0)
340 {
341 result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
342 lldb_regnum = regloc.location.register_number;
343 }
344
Jason Molendaaff2a262012-11-16 01:03:31 +0000345 if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
Jason Molenda707fec42011-11-01 03:21:25 +0000346 return true;
Jason Molendaaff2a262012-11-16 01:03:31 +0000347 if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)
348 return false;
Jason Molenda707fec42011-11-01 03:21:25 +0000349 frame_num--;
350 }
351 return false;
352}