blob: 062d0a5f941a66d7bab27a816314f00d95e9cfad [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 {
Jason Molenda1da513b2010-11-09 02:31:21 +000036 if (!AddFirstFrame ())
Jason Molenda8280cbe2010-10-25 11:12:07 +000037 return 0;
Jason Molenda1da513b2010-11-09 02:31:21 +000038 while (AddOneMoreFrame ())
39 ;
Jason Molenda8280cbe2010-10-25 11:12:07 +000040 }
41 return m_frames.size ();
42}
43
44bool
Jason Molenda1da513b2010-11-09 02:31:21 +000045UnwindLLDB::AddFirstFrame ()
46{
47 // First, set up the 0th (initial) frame
48 CursorSP first_cursor_sp(new Cursor ());
49 RegisterContextSP no_frame;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000050 std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB(m_thread, no_frame, first_cursor_sp->sctx, 0));
51 if (first_register_ctx_ap.get() == NULL)
Jason Molenda1da513b2010-11-09 02:31:21 +000052 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000053
54 if (!first_register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +000055 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000056
57 if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +000058 return false;
Greg Clayton08d7d3a2011-01-06 22:15:06 +000059
60 if (!first_register_ctx_ap->GetPC (first_cursor_sp->start_pc))
61 return false;
62
63 // Everything checks out, so release the auto pointer value and let the
64 // cursor own it in its shared pointer
65 first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +000066 m_frames.push_back (first_cursor_sp);
67 return true;
68}
69
70// For adding a non-zero stack frame to m_frames.
71bool
72UnwindLLDB::AddOneMoreFrame ()
73{
74 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
75 CursorSP cursor_sp(new Cursor ());
Jason Molenda1da513b2010-11-09 02:31:21 +000076
77 // Frame zero is a little different
78 if (m_frames.size() == 0)
79 return false;
80
81 uint32_t cur_idx = m_frames.size ();
Greg Clayton08d7d3a2011-01-06 22:15:06 +000082 std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread,
83 m_frames[cur_idx - 1]->reg_ctx,
84 cursor_sp->sctx,
85 cur_idx));
86 if (register_ctx_ap.get() == NULL)
87 return false;
Jason Molenda1da513b2010-11-09 02:31:21 +000088
Greg Clayton08d7d3a2011-01-06 22:15:06 +000089 if (!register_ctx_ap->IsValid())
Jason Molenda1da513b2010-11-09 02:31:21 +000090 {
Jason Molenda1da513b2010-11-09 02:31:21 +000091 if (log)
92 {
93 log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
94 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
95 }
96 return false;
97 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +000098 if (!register_ctx_ap->GetCFA (cursor_sp->cfa))
Jason Molenda1da513b2010-11-09 02:31:21 +000099 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000100 if (log)
101 {
102 log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
103 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
104 }
105 return false;
106 }
107 if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0)
108 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000109 if (log)
110 {
111 log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
112 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
113 }
114 return false;
115 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000116 if (!register_ctx_ap->GetPC (cursor_sp->start_pc))
Jason Molenda1da513b2010-11-09 02:31:21 +0000117 {
Jason Molenda1da513b2010-11-09 02:31:21 +0000118 if (log)
119 {
120 log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
121 cur_idx < 100 ? cur_idx : 100, "", cur_idx);
122 }
123 return false;
124 }
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000125 RegisterContextSP register_ctx_sp(register_ctx_ap.release());
Jason Molenda1da513b2010-11-09 02:31:21 +0000126 cursor_sp->reg_ctx = register_ctx_sp;
127 m_frames.push_back (cursor_sp);
128 return true;
129}
130
131bool
Jason Molenda8280cbe2010-10-25 11:12:07 +0000132UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
133{
Jason Molenda8280cbe2010-10-25 11:12:07 +0000134 if (m_frames.size() == 0)
Jason Molenda1da513b2010-11-09 02:31:21 +0000135 {
136 if (!AddFirstFrame())
137 return false;
138 }
139
140 while (idx >= m_frames.size() && AddOneMoreFrame ())
141 ;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000142
143 if (idx < m_frames.size ())
144 {
Jason Molenda800d11d2010-11-04 00:53:20 +0000145 cfa = m_frames[idx]->cfa;
146 pc = m_frames[idx]->start_pc;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000147 return true;
148 }
149 return false;
150}
151
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000152lldb::RegisterContextSP
Jason Molenda8280cbe2010-10-25 11:12:07 +0000153UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame)
154{
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000155 lldb::RegisterContextSP reg_ctx_sp;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000156 uint32_t idx = frame->GetFrameIndex ();
Jason Molenda1da513b2010-11-09 02:31:21 +0000157
Jason Molenda8280cbe2010-10-25 11:12:07 +0000158 if (idx == 0)
159 {
160 return m_thread.GetRegisterContext();
161 }
Jason Molenda1da513b2010-11-09 02:31:21 +0000162
163 if (m_frames.size() == 0)
164 {
165 if (!AddFirstFrame())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000166 return reg_ctx_sp;
Jason Molenda1da513b2010-11-09 02:31:21 +0000167 }
168
169 while (idx >= m_frames.size() && AddOneMoreFrame ())
170 ;
171
Jason Molenda8280cbe2010-10-25 11:12:07 +0000172 if (idx < m_frames.size ())
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000173 reg_ctx_sp = m_frames[idx]->reg_ctx;
174 return reg_ctx_sp;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000175}