blob: 533c21c6e2418d150e840434e077d2c9a3d4bdfe [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
10#include "lldb/Target/Thread.h"
11#include "lldb/Target/Target.h"
12#include "lldb/Target/Process.h"
13#include "lldb/Target/RegisterContext.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Symbol/FuncUnwinders.h"
16#include "lldb/Symbol/Function.h"
17#include "lldb/Utility/ArchDefaultUnwindPlan.h"
18#include "UnwindLLDB.h"
19#include "lldb/Symbol/UnwindPlan.h"
20#include "lldb/Core/Log.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25UnwindLLDB::UnwindLLDB (Thread &thread) :
26 Unwind (thread),
27 m_frames()
28{
29}
30
31uint32_t
32UnwindLLDB::GetFrameCount()
33{
Jason Molenda8280cbe2010-10-25 11:12:07 +000034 if (m_frames.empty())
35 {
Greg Claytonfd119992011-01-07 06:08:19 +000036//#define DEBUG_FRAME_SPEED 1
37#if DEBUG_FRAME_SPEED
Greg Claytona875b642011-01-09 21:07:35 +000038#define FRAME_COUNT 10000
Greg Claytonfd119992011-01-07 06:08:19 +000039 TimeValue time_value (TimeValue::Now());
40#endif
Jason Molenda1da513b2010-11-09 02:31:21 +000041 if (!AddFirstFrame ())
Jason Molenda8280cbe2010-10-25 11:12:07 +000042 return 0;
Jason Molenda1da513b2010-11-09 02:31:21 +000043 while (AddOneMoreFrame ())
Greg Claytonfd119992011-01-07 06:08:19 +000044 {
45#if DEBUG_FRAME_SPEED
Greg Claytona875b642011-01-09 21:07:35 +000046 if ((m_frames.size() % FRAME_COUNT) == 0)
Greg Claytonfd119992011-01-07 06:08:19 +000047 {
48 TimeValue now(TimeValue::Now());
49 uint64_t delta_t = now - time_value;
Greg Claytona875b642011-01-09 21:07:35 +000050 printf ("%u frames in %llu.%09llu ms (%g frames/sec)\n",
51 FRAME_COUNT,
52 delta_t / NSEC_PER_SEC,
53 delta_t % NSEC_PER_SEC,
54 (float)FRAME_COUNT / ((float)delta_t / (float)NSEC_PER_SEC));
Greg Claytonfd119992011-01-07 06:08:19 +000055 time_value = now;
56 }
57#endif
58 }
Jason Molenda8280cbe2010-10-25 11:12:07 +000059 }
60 return m_frames.size ();
61}
62
63bool
Jason Molenda1da513b2010-11-09 02:31:21 +000064UnwindLLDB::AddFirstFrame ()
65{
66 // First, set up the 0th (initial) frame
67 CursorSP first_cursor_sp(new Cursor ());
68 RegisterContextSP no_frame;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000069 std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB(m_thread, no_frame, first_cursor_sp->sctx, 0));
70 if (first_register_ctx_ap.get() == NULL)
Jason Molenda1da513b2010-11-09 02:31:21 +000071 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000072
73 if (!first_register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +000074 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000075
76 if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +000077 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000078
Greg Clayton1724dec2011-01-17 21:03:33 +000079 if (!first_register_ctx_ap->ReadPC (first_cursor_sp->start_pc))
Greg Clayton08d7d3a2011-01-06 22:15:06 +000080 return false;
81
82 // Everything checks out, so release the auto pointer value and let the
83 // cursor own it in its shared pointer
84 first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +000085 m_frames.push_back (first_cursor_sp);
86 return true;
87}
88
89// For adding a non-zero stack frame to m_frames.
90bool
91UnwindLLDB::AddOneMoreFrame ()
92{
93 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
94 CursorSP cursor_sp(new Cursor ());
Jason Molenda1da513b2010-11-09 02:31:21 +000095
96 // Frame zero is a little different
97 if (m_frames.size() == 0)
98 return false;
99
100 uint32_t cur_idx = m_frames.size ();
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000101 std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread,
102 m_frames[cur_idx - 1]->reg_ctx,
103 cursor_sp->sctx,
104 cur_idx));
105 if (register_ctx_ap.get() == NULL)
106 return false;
Jason Molenda1da513b2010-11-09 02:31:21 +0000107
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000108 if (!register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +0000109 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000110 if (log)
111 {
112 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
113 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
114 }
115 return false;
116 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000117 if (!register_ctx_ap->GetCFA (cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +0000118 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000119 if (log)
120 {
121 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
122 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
123 }
124 return false;
125 }
126 if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0)
127 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000128 if (log)
129 {
130 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
131 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
132 }
133 return false;
134 }
Greg Clayton1724dec2011-01-17 21:03:33 +0000135 if (!register_ctx_ap->ReadPC (cursor_sp->start_pc))
Jason Molenda1da513b2010-11-09 02:31:21 +0000136 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000137 if (log)
138 {
139 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
140 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
141 }
142 return false;
143 }
Greg Clayton1724dec2011-01-17 21:03:33 +0000144 if (!m_frames.empty())
145 {
146 if ((m_frames.back()->start_pc == cursor_sp->start_pc) &&
147 (m_frames.back()->cfa == cursor_sp->cfa))
148 {
149 // Infinite loop where the current cursor is the same as the previous one...
150 return false;
151 }
152 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000153 RegisterContextSP register_ctx_sp(register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +0000154 cursor_sp->reg_ctx = register_ctx_sp;
155 m_frames.push_back (cursor_sp);
156 return true;
157}
158
159bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000160UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
161{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000162 if (m_frames.size() == 0)
Jason Molenda1da513b2010-11-09 02:31:21 +0000163 {
164 if (!AddFirstFrame())
165 return false;
166 }
167
168 while (idx >= m_frames.size() && AddOneMoreFrame ())
169 ;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000170
171 if (idx < m_frames.size ())
172 {
Jason Molenda800d11d2010-11-04 00:53:20 +0000173 cfa = m_frames[idx]->cfa;
174 pc = m_frames[idx]->start_pc;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000175 return true;
176 }
177 return false;
178}
179
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000180lldb::RegisterContextSP
Jason Molenda8280cbe2010-10-25 11:12:07 +0000181UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame)
182{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000183 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton869efc22011-01-08 01:53:06 +0000184 uint32_t idx = frame->GetConcreteFrameIndex ();
Jason Molenda1da513b2010-11-09 02:31:21 +0000185
Jason Molenda8280cbe2010-10-25 11:12:07 +0000186 if (idx == 0)
187 {
188 return m_thread.GetRegisterContext();
189 }
Jason Molenda1da513b2010-11-09 02:31:21 +0000190
191 if (m_frames.size() == 0)
192 {
193 if (!AddFirstFrame())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000194 return reg_ctx_sp;
Jason Molenda1da513b2010-11-09 02:31:21 +0000195 }
196
197 while (idx >= m_frames.size() && AddOneMoreFrame ())
198 ;
199
Jason Molenda8280cbe2010-10-25 11:12:07 +0000200 if (idx < m_frames.size ())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000201 reg_ctx_sp = m_frames[idx]->reg_ctx;
202 return reg_ctx_sp;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000203}