blob: 98078195c4503e39af301e0fe31ae868b02a1b92 [file] [log] [blame]
Jason Molenda8280cbe2010-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 Clayton97eecb12011-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"
15#include "lldb/Target/ArchDefaultUnwindPlan.h"
Jason Molenda8280cbe2010-10-25 11:12:07 +000016#include "lldb/Target/Thread.h"
17#include "lldb/Target/Target.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/RegisterContext.h"
Jason Molenda8280cbe2010-10-25 11:12:07 +000020
Greg Claytonc3c46612011-02-15 00:19:15 +000021#include "UnwindLLDB.h"
22#include "RegisterContextLLDB.h"
23
Jason Molenda8280cbe2010-10-25 11:12:07 +000024using namespace lldb;
25using namespace lldb_private;
26
27UnwindLLDB::UnwindLLDB (Thread &thread) :
28 Unwind (thread),
29 m_frames()
30{
31}
32
33uint32_t
34UnwindLLDB::GetFrameCount()
35{
Jason Molenda8280cbe2010-10-25 11:12:07 +000036 if (m_frames.empty())
37 {
Greg Claytonfd119992011-01-07 06:08:19 +000038//#define DEBUG_FRAME_SPEED 1
39#if DEBUG_FRAME_SPEED
Greg Claytona875b642011-01-09 21:07:35 +000040#define FRAME_COUNT 10000
Greg Claytonfd119992011-01-07 06:08:19 +000041 TimeValue time_value (TimeValue::Now());
42#endif
Jason Molenda1da513b2010-11-09 02:31:21 +000043 if (!AddFirstFrame ())
Jason Molenda8280cbe2010-10-25 11:12:07 +000044 return 0;
Jason Molenda1da513b2010-11-09 02:31:21 +000045 while (AddOneMoreFrame ())
Greg Claytonfd119992011-01-07 06:08:19 +000046 {
47#if DEBUG_FRAME_SPEED
Greg Claytona875b642011-01-09 21:07:35 +000048 if ((m_frames.size() % FRAME_COUNT) == 0)
Greg Claytonfd119992011-01-07 06:08:19 +000049 {
50 TimeValue now(TimeValue::Now());
51 uint64_t delta_t = now - time_value;
Greg Claytona875b642011-01-09 21:07:35 +000052 printf ("%u frames in %llu.%09llu ms (%g frames/sec)\n",
53 FRAME_COUNT,
54 delta_t / NSEC_PER_SEC,
55 delta_t % NSEC_PER_SEC,
56 (float)FRAME_COUNT / ((float)delta_t / (float)NSEC_PER_SEC));
Greg Claytonfd119992011-01-07 06:08:19 +000057 time_value = now;
58 }
59#endif
60 }
Jason Molenda8280cbe2010-10-25 11:12:07 +000061 }
62 return m_frames.size ();
63}
64
65bool
Jason Molenda1da513b2010-11-09 02:31:21 +000066UnwindLLDB::AddFirstFrame ()
67{
68 // First, set up the 0th (initial) frame
69 CursorSP first_cursor_sp(new Cursor ());
Greg Claytonc3c46612011-02-15 00:19:15 +000070 std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB (m_thread,
71 RegisterContextLLDB::SharedPtr(),
72 first_cursor_sp->sctx,
73 0));
Greg Clayton08d7d3a2011-01-06 22:15:06 +000074 if (first_register_ctx_ap.get() == NULL)
Jason Molenda1da513b2010-11-09 02:31:21 +000075 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000076
77 if (!first_register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +000078 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000079
80 if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +000081 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000082
Greg Clayton1724dec2011-01-17 21:03:33 +000083 if (!first_register_ctx_ap->ReadPC (first_cursor_sp->start_pc))
Greg Clayton08d7d3a2011-01-06 22:15:06 +000084 return false;
85
86 // Everything checks out, so release the auto pointer value and let the
87 // cursor own it in its shared pointer
88 first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +000089 m_frames.push_back (first_cursor_sp);
90 return true;
91}
92
93// For adding a non-zero stack frame to m_frames.
94bool
95UnwindLLDB::AddOneMoreFrame ()
96{
97 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
98 CursorSP cursor_sp(new Cursor ());
Jason Molenda1da513b2010-11-09 02:31:21 +000099
100 // Frame zero is a little different
101 if (m_frames.size() == 0)
102 return false;
103
104 uint32_t cur_idx = m_frames.size ();
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000105 std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread,
106 m_frames[cur_idx - 1]->reg_ctx,
107 cursor_sp->sctx,
108 cur_idx));
109 if (register_ctx_ap.get() == NULL)
110 return false;
Jason Molenda1da513b2010-11-09 02:31:21 +0000111
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000112 if (!register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +0000113 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000114 if (log)
115 {
116 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
117 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
118 }
119 return false;
120 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000121 if (!register_ctx_ap->GetCFA (cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +0000122 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000123 if (log)
124 {
125 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
126 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
127 }
128 return false;
129 }
130 if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0)
131 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000132 if (log)
133 {
134 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
135 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
136 }
137 return false;
138 }
Greg Clayton1724dec2011-01-17 21:03:33 +0000139 if (!register_ctx_ap->ReadPC (cursor_sp->start_pc))
Jason Molenda1da513b2010-11-09 02:31:21 +0000140 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000141 if (log)
142 {
143 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
144 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
145 }
146 return false;
147 }
Greg Clayton1724dec2011-01-17 21:03:33 +0000148 if (!m_frames.empty())
149 {
150 if ((m_frames.back()->start_pc == cursor_sp->start_pc) &&
151 (m_frames.back()->cfa == cursor_sp->cfa))
152 {
153 // Infinite loop where the current cursor is the same as the previous one...
154 return false;
155 }
156 }
Greg Claytonc3c46612011-02-15 00:19:15 +0000157 RegisterContextLLDB::SharedPtr reg_ctx_sp(register_ctx_ap.release());
158 cursor_sp->reg_ctx = reg_ctx_sp;
Jason Molenda1da513b2010-11-09 02:31:21 +0000159 m_frames.push_back (cursor_sp);
160 return true;
161}
162
163bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000164UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
165{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000166 if (m_frames.size() == 0)
Jason Molenda1da513b2010-11-09 02:31:21 +0000167 {
168 if (!AddFirstFrame())
169 return false;
170 }
171
172 while (idx >= m_frames.size() && AddOneMoreFrame ())
173 ;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000174
175 if (idx < m_frames.size ())
176 {
Jason Molenda800d11d2010-11-04 00:53:20 +0000177 cfa = m_frames[idx]->cfa;
178 pc = m_frames[idx]->start_pc;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000179 return true;
180 }
181 return false;
182}
183
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000184lldb::RegisterContextSP
Jason Molenda8280cbe2010-10-25 11:12:07 +0000185UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame)
186{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000187 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton869efc22011-01-08 01:53:06 +0000188 uint32_t idx = frame->GetConcreteFrameIndex ();
Jason Molenda1da513b2010-11-09 02:31:21 +0000189
Jason Molenda8280cbe2010-10-25 11:12:07 +0000190 if (idx == 0)
191 {
192 return m_thread.GetRegisterContext();
193 }
Jason Molenda1da513b2010-11-09 02:31:21 +0000194
195 if (m_frames.size() == 0)
196 {
197 if (!AddFirstFrame())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000198 return reg_ctx_sp;
Jason Molenda1da513b2010-11-09 02:31:21 +0000199 }
200
201 while (idx >= m_frames.size() && AddOneMoreFrame ())
202 ;
203
Jason Molenda8280cbe2010-10-25 11:12:07 +0000204 if (idx < m_frames.size ())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000205 reg_ctx_sp = m_frames[idx]->reg_ctx;
206 return reg_ctx_sp;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000207}