blob: 75f92bbef2834920ae83ed95da88249a2b78cc87 [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
38 TimeValue time_value (TimeValue::Now());
39#endif
Jason Molenda1da513b2010-11-09 02:31:21 +000040 if (!AddFirstFrame ())
Jason Molenda8280cbe2010-10-25 11:12:07 +000041 return 0;
Jason Molenda1da513b2010-11-09 02:31:21 +000042 while (AddOneMoreFrame ())
Greg Claytonfd119992011-01-07 06:08:19 +000043 {
44#if DEBUG_FRAME_SPEED
45 if ((m_frames.size() % 10000) == 0)
46 {
47 TimeValue now(TimeValue::Now());
48 uint64_t delta_t = now - time_value;
49 printf ("10000 frames in %llu.%09llu ms\n", delta_t / NSEC_PER_SEC, delta_t % NSEC_PER_SEC);
50 time_value = now;
51 }
52#endif
53 }
Jason Molenda8280cbe2010-10-25 11:12:07 +000054 }
55 return m_frames.size ();
56}
57
58bool
Jason Molenda1da513b2010-11-09 02:31:21 +000059UnwindLLDB::AddFirstFrame ()
60{
61 // First, set up the 0th (initial) frame
62 CursorSP first_cursor_sp(new Cursor ());
63 RegisterContextSP no_frame;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000064 std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB(m_thread, no_frame, first_cursor_sp->sctx, 0));
65 if (first_register_ctx_ap.get() == NULL)
Jason Molenda1da513b2010-11-09 02:31:21 +000066 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000067
68 if (!first_register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +000069 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000070
71 if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +000072 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000073
74 if (!first_register_ctx_ap->GetPC (first_cursor_sp->start_pc))
75 return false;
76
77 // Everything checks out, so release the auto pointer value and let the
78 // cursor own it in its shared pointer
79 first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +000080 m_frames.push_back (first_cursor_sp);
81 return true;
82}
83
84// For adding a non-zero stack frame to m_frames.
85bool
86UnwindLLDB::AddOneMoreFrame ()
87{
88 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
89 CursorSP cursor_sp(new Cursor ());
Jason Molenda1da513b2010-11-09 02:31:21 +000090
91 // Frame zero is a little different
92 if (m_frames.size() == 0)
93 return false;
94
95 uint32_t cur_idx = m_frames.size ();
Greg Clayton08d7d3a2011-01-06 22:15:06 +000096 std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread,
97 m_frames[cur_idx - 1]->reg_ctx,
98 cursor_sp->sctx,
99 cur_idx));
100 if (register_ctx_ap.get() == NULL)
101 return false;
Jason Molenda1da513b2010-11-09 02:31:21 +0000102
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000103 if (!register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +0000104 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000105 if (log)
106 {
107 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
108 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
109 }
110 return false;
111 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000112 if (!register_ctx_ap->GetCFA (cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +0000113 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000114 if (log)
115 {
116 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
117 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
118 }
119 return false;
120 }
121 if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0)
122 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000123 if (log)
124 {
125 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
126 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
127 }
128 return false;
129 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000130 if (!register_ctx_ap->GetPC (cursor_sp->start_pc))
Jason Molenda1da513b2010-11-09 02:31:21 +0000131 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000132 if (log)
133 {
134 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
135 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
136 }
137 return false;
138 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000139 RegisterContextSP register_ctx_sp(register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +0000140 cursor_sp->reg_ctx = register_ctx_sp;
141 m_frames.push_back (cursor_sp);
142 return true;
143}
144
145bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000146UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
147{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000148 if (m_frames.size() == 0)
Jason Molenda1da513b2010-11-09 02:31:21 +0000149 {
150 if (!AddFirstFrame())
151 return false;
152 }
153
154 while (idx >= m_frames.size() && AddOneMoreFrame ())
155 ;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000156
157 if (idx < m_frames.size ())
158 {
Jason Molenda800d11d2010-11-04 00:53:20 +0000159 cfa = m_frames[idx]->cfa;
160 pc = m_frames[idx]->start_pc;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000161 return true;
162 }
163 return false;
164}
165
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000166lldb::RegisterContextSP
Jason Molenda8280cbe2010-10-25 11:12:07 +0000167UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame)
168{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000169 lldb::RegisterContextSP reg_ctx_sp;
Greg Clayton869efc22011-01-08 01:53:06 +0000170 uint32_t idx = frame->GetConcreteFrameIndex ();
Jason Molenda1da513b2010-11-09 02:31:21 +0000171
Jason Molenda8280cbe2010-10-25 11:12:07 +0000172 if (idx == 0)
173 {
174 return m_thread.GetRegisterContext();
175 }
Jason Molenda1da513b2010-11-09 02:31:21 +0000176
177 if (m_frames.size() == 0)
178 {
179 if (!AddFirstFrame())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000180 return reg_ctx_sp;
Jason Molenda1da513b2010-11-09 02:31:21 +0000181 }
182
183 while (idx >= m_frames.size() && AddOneMoreFrame ())
184 ;
185
Jason Molenda8280cbe2010-10-25 11:12:07 +0000186 if (idx < m_frames.size ())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000187 reg_ctx_sp = m_frames[idx]->reg_ctx;
188 return reg_ctx_sp;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000189}