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" |
| 18 | #include "UnwindLLDB.h" |
| 19 | #include "lldb/Symbol/UnwindPlan.h" |
| 20 | #include "lldb/Core/Log.h" |
| 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | UnwindLLDB::UnwindLLDB (Thread &thread) : |
| 26 | Unwind (thread), |
| 27 | m_frames() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | uint32_t |
| 32 | UnwindLLDB::GetFrameCount() |
| 33 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 34 | if (m_frames.empty()) |
| 35 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 36 | //#define DEBUG_FRAME_SPEED 1 |
| 37 | #if DEBUG_FRAME_SPEED |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 38 | #define FRAME_COUNT 10000 |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 39 | TimeValue time_value (TimeValue::Now()); |
| 40 | #endif |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 41 | if (!AddFirstFrame ()) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 42 | return 0; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 43 | while (AddOneMoreFrame ()) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 44 | { |
| 45 | #if DEBUG_FRAME_SPEED |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 46 | if ((m_frames.size() % FRAME_COUNT) == 0) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 47 | { |
| 48 | TimeValue now(TimeValue::Now()); |
| 49 | uint64_t delta_t = now - time_value; |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 50 | 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 Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 55 | time_value = now; |
| 56 | } |
| 57 | #endif |
| 58 | } |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 59 | } |
| 60 | return m_frames.size (); |
| 61 | } |
| 62 | |
| 63 | bool |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 64 | UnwindLLDB::AddFirstFrame () |
| 65 | { |
| 66 | // First, set up the 0th (initial) frame |
| 67 | CursorSP first_cursor_sp(new Cursor ()); |
| 68 | RegisterContextSP no_frame; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 69 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 71 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 72 | |
| 73 | if (!first_register_ctx_ap->IsValid()) |
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->GetCFA (first_cursor_sp->cfa)) |
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 | |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame^] | 79 | if (!first_register_ctx_ap->ReadPC (first_cursor_sp->start_pc)) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 80 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 85 | m_frames.push_back (first_cursor_sp); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // For adding a non-zero stack frame to m_frames. |
| 90 | bool |
| 91 | UnwindLLDB::AddOneMoreFrame () |
| 92 | { |
| 93 | LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
| 94 | CursorSP cursor_sp(new Cursor ()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 95 | |
| 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 Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 101 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 107 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 108 | if (!register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 109 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 110 | 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 Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 117 | if (!register_ctx_ap->GetCFA (cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 118 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 119 | 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 Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 128 | 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 Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame^] | 135 | if (!register_ctx_ap->ReadPC (cursor_sp->start_pc)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 136 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 137 | 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 Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame^] | 144 | 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 Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 153 | RegisterContextSP register_ctx_sp(register_ctx_ap.release()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 154 | cursor_sp->reg_ctx = register_ctx_sp; |
| 155 | m_frames.push_back (cursor_sp); |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 160 | UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
| 161 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 162 | if (m_frames.size() == 0) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 163 | { |
| 164 | if (!AddFirstFrame()) |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 169 | ; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 170 | |
| 171 | if (idx < m_frames.size ()) |
| 172 | { |
Jason Molenda | 800d11d | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 173 | cfa = m_frames[idx]->cfa; |
| 174 | pc = m_frames[idx]->start_pc; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 180 | lldb::RegisterContextSP |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 181 | UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame) |
| 182 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 183 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 869efc2 | 2011-01-08 01:53:06 +0000 | [diff] [blame] | 184 | uint32_t idx = frame->GetConcreteFrameIndex (); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 185 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 186 | if (idx == 0) |
| 187 | { |
| 188 | return m_thread.GetRegisterContext(); |
| 189 | } |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 190 | |
| 191 | if (m_frames.size() == 0) |
| 192 | { |
| 193 | if (!AddFirstFrame()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 194 | return reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 198 | ; |
| 199 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 200 | if (idx < m_frames.size ()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 201 | reg_ctx_sp = m_frames[idx]->reg_ctx; |
| 202 | return reg_ctx_sp; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 203 | } |