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 | |
Greg Clayton | 97eecb1 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 10 | #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" |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 15 | #include "lldb/Target/Thread.h" |
| 16 | #include "lldb/Target/Target.h" |
| 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Target/RegisterContext.h" |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 19 | |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 20 | #include "UnwindLLDB.h" |
| 21 | #include "RegisterContextLLDB.h" |
| 22 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | UnwindLLDB::UnwindLLDB (Thread &thread) : |
| 27 | Unwind (thread), |
| 28 | m_frames() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | uint32_t |
| 33 | UnwindLLDB::GetFrameCount() |
| 34 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 35 | if (m_frames.empty()) |
| 36 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 37 | //#define DEBUG_FRAME_SPEED 1 |
| 38 | #if DEBUG_FRAME_SPEED |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 39 | #define FRAME_COUNT 10000 |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 40 | TimeValue time_value (TimeValue::Now()); |
| 41 | #endif |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 42 | if (!AddFirstFrame ()) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 43 | return 0; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 44 | while (AddOneMoreFrame ()) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 45 | { |
| 46 | #if DEBUG_FRAME_SPEED |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 47 | if ((m_frames.size() % FRAME_COUNT) == 0) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 48 | { |
| 49 | TimeValue now(TimeValue::Now()); |
| 50 | uint64_t delta_t = now - time_value; |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 51 | printf ("%u frames in %llu.%09llu ms (%g frames/sec)\n", |
| 52 | FRAME_COUNT, |
| 53 | delta_t / NSEC_PER_SEC, |
| 54 | delta_t % NSEC_PER_SEC, |
| 55 | (float)FRAME_COUNT / ((float)delta_t / (float)NSEC_PER_SEC)); |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 56 | time_value = now; |
| 57 | } |
| 58 | #endif |
| 59 | } |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 60 | } |
| 61 | return m_frames.size (); |
| 62 | } |
| 63 | |
| 64 | bool |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 65 | UnwindLLDB::AddFirstFrame () |
| 66 | { |
| 67 | // First, set up the 0th (initial) frame |
| 68 | CursorSP first_cursor_sp(new Cursor ()); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 69 | std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB (m_thread, |
| 70 | RegisterContextLLDB::SharedPtr(), |
| 71 | first_cursor_sp->sctx, |
| 72 | 0)); |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 73 | if (first_register_ctx_ap.get() == NULL) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 74 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 75 | |
| 76 | if (!first_register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 77 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 78 | |
| 79 | if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 80 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 81 | |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 82 | if (!first_register_ctx_ap->ReadPC (first_cursor_sp->start_pc)) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 83 | return false; |
| 84 | |
| 85 | // Everything checks out, so release the auto pointer value and let the |
| 86 | // cursor own it in its shared pointer |
| 87 | first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 88 | m_frames.push_back (first_cursor_sp); |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | // For adding a non-zero stack frame to m_frames. |
| 93 | bool |
| 94 | UnwindLLDB::AddOneMoreFrame () |
| 95 | { |
| 96 | LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
| 97 | CursorSP cursor_sp(new Cursor ()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 98 | |
| 99 | // Frame zero is a little different |
| 100 | if (m_frames.size() == 0) |
| 101 | return false; |
| 102 | |
| 103 | uint32_t cur_idx = m_frames.size (); |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 104 | std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread, |
| 105 | m_frames[cur_idx - 1]->reg_ctx, |
| 106 | cursor_sp->sctx, |
| 107 | cur_idx)); |
| 108 | if (register_ctx_ap.get() == NULL) |
| 109 | return false; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 110 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 111 | if (!register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 112 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 113 | if (log) |
| 114 | { |
| 115 | log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk", |
| 116 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 117 | } |
| 118 | return false; |
| 119 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 120 | if (!register_ctx_ap->GetCFA (cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 121 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 122 | if (log) |
| 123 | { |
| 124 | log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk", |
| 125 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0) |
| 130 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 131 | if (log) |
| 132 | { |
| 133 | log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk", |
| 134 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 135 | } |
| 136 | return false; |
| 137 | } |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 138 | if (!register_ctx_ap->ReadPC (cursor_sp->start_pc)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 139 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 140 | if (log) |
| 141 | { |
| 142 | log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk", |
| 143 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 144 | } |
| 145 | return false; |
| 146 | } |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 147 | if (!m_frames.empty()) |
| 148 | { |
| 149 | if ((m_frames.back()->start_pc == cursor_sp->start_pc) && |
| 150 | (m_frames.back()->cfa == cursor_sp->cfa)) |
| 151 | { |
| 152 | // Infinite loop where the current cursor is the same as the previous one... |
| 153 | return false; |
| 154 | } |
| 155 | } |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 156 | RegisterContextLLDB::SharedPtr reg_ctx_sp(register_ctx_ap.release()); |
| 157 | cursor_sp->reg_ctx = reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 158 | m_frames.push_back (cursor_sp); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | bool |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 163 | UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
| 164 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 165 | if (m_frames.size() == 0) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 166 | { |
| 167 | if (!AddFirstFrame()) |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 172 | ; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 173 | |
| 174 | if (idx < m_frames.size ()) |
| 175 | { |
Jason Molenda | 800d11d | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 176 | cfa = m_frames[idx]->cfa; |
| 177 | pc = m_frames[idx]->start_pc; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 183 | lldb::RegisterContextSP |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 184 | UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame) |
| 185 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 186 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 869efc2 | 2011-01-08 01:53:06 +0000 | [diff] [blame] | 187 | uint32_t idx = frame->GetConcreteFrameIndex (); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 188 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 189 | if (idx == 0) |
| 190 | { |
| 191 | return m_thread.GetRegisterContext(); |
| 192 | } |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 193 | |
| 194 | if (m_frames.size() == 0) |
| 195 | { |
| 196 | if (!AddFirstFrame()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 197 | return reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 201 | ; |
| 202 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 203 | if (idx < m_frames.size ()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 204 | reg_ctx_sp = m_frames[idx]->reg_ctx; |
| 205 | return reg_ctx_sp; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 206 | } |