Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 1 | //===-- 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" |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/UnwindPlan.h" |
| 19 | #include "lldb/Core/Log.h" |
| 20 | |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame^] | 21 | #include "UnwindLLDB.h" |
| 22 | #include "RegisterContextLLDB.h" |
| 23 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | UnwindLLDB::UnwindLLDB (Thread &thread) : |
| 28 | Unwind (thread), |
| 29 | m_frames() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | uint32_t |
| 34 | UnwindLLDB::GetFrameCount() |
| 35 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 36 | if (m_frames.empty()) |
| 37 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 38 | //#define DEBUG_FRAME_SPEED 1 |
| 39 | #if DEBUG_FRAME_SPEED |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 40 | #define FRAME_COUNT 10000 |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 41 | TimeValue time_value (TimeValue::Now()); |
| 42 | #endif |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 43 | if (!AddFirstFrame ()) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 44 | return 0; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 45 | while (AddOneMoreFrame ()) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 46 | { |
| 47 | #if DEBUG_FRAME_SPEED |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 48 | if ((m_frames.size() % FRAME_COUNT) == 0) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 49 | { |
| 50 | TimeValue now(TimeValue::Now()); |
| 51 | uint64_t delta_t = now - time_value; |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 52 | 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 Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 57 | time_value = now; |
| 58 | } |
| 59 | #endif |
| 60 | } |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 61 | } |
| 62 | return m_frames.size (); |
| 63 | } |
| 64 | |
| 65 | bool |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 66 | UnwindLLDB::AddFirstFrame () |
| 67 | { |
| 68 | // First, set up the 0th (initial) frame |
| 69 | CursorSP first_cursor_sp(new Cursor ()); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame^] | 70 | std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB (m_thread, |
| 71 | RegisterContextLLDB::SharedPtr(), |
| 72 | first_cursor_sp->sctx, |
| 73 | 0)); |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 74 | if (first_register_ctx_ap.get() == NULL) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 75 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 76 | |
| 77 | if (!first_register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 78 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 79 | |
| 80 | if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 81 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 82 | |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 83 | if (!first_register_ctx_ap->ReadPC (first_cursor_sp->start_pc)) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 84 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 89 | m_frames.push_back (first_cursor_sp); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // For adding a non-zero stack frame to m_frames. |
| 94 | bool |
| 95 | UnwindLLDB::AddOneMoreFrame () |
| 96 | { |
| 97 | LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
| 98 | CursorSP cursor_sp(new Cursor ()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 99 | |
| 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 Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 105 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 111 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 112 | if (!register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 113 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 114 | 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 Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 121 | if (!register_ctx_ap->GetCFA (cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 122 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 123 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 132 | 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 Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 139 | if (!register_ctx_ap->ReadPC (cursor_sp->start_pc)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 140 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 141 | 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 Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 148 | 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 Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame^] | 157 | RegisterContextLLDB::SharedPtr reg_ctx_sp(register_ctx_ap.release()); |
| 158 | cursor_sp->reg_ctx = reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 159 | m_frames.push_back (cursor_sp); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 164 | UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
| 165 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 166 | if (m_frames.size() == 0) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 167 | { |
| 168 | if (!AddFirstFrame()) |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 173 | ; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 174 | |
| 175 | if (idx < m_frames.size ()) |
| 176 | { |
Jason Molenda | 800d11d | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 177 | cfa = m_frames[idx]->cfa; |
| 178 | pc = m_frames[idx]->start_pc; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 184 | lldb::RegisterContextSP |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 185 | UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame) |
| 186 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 187 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 869efc2 | 2011-01-08 01:53:06 +0000 | [diff] [blame] | 188 | uint32_t idx = frame->GetConcreteFrameIndex (); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 189 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 190 | if (idx == 0) |
| 191 | { |
| 192 | return m_thread.GetRegisterContext(); |
| 193 | } |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 194 | |
| 195 | if (m_frames.size() == 0) |
| 196 | { |
| 197 | if (!AddFirstFrame()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 198 | return reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 202 | ; |
| 203 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 204 | if (idx < m_frames.size ()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 205 | reg_ctx_sp = m_frames[idx]->reg_ctx; |
| 206 | return reg_ctx_sp; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 207 | } |