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