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 |
Jim Ingham | 591cf15 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 33 | UnwindLLDB::DoGetFrameCount() |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 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, |
Peter Collingbourne | 20fe30c | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 56 | delta_t / TimeValue::NanoSecPerSec, |
| 57 | delta_t % TimeValue::NanoSecPerSec, |
| 58 | (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec)); |
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 | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 72 | RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread, |
| 73 | RegisterContextLLDBSP(), |
| 74 | first_cursor_sp->sctx, |
| 75 | 0, *this)); |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 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 | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 90 | first_cursor_sp->reg_ctx_lldb_sp = 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 | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 107 | RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread, |
| 108 | m_frames[cur_idx - 1]->reg_ctx_lldb_sp, |
| 109 | cursor_sp->sctx, |
| 110 | cur_idx, |
| 111 | *this)); |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 112 | if (reg_ctx_sp.get() == NULL) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 113 | return false; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 114 | |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 115 | if (!reg_ctx_sp->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 116 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 117 | if (log) |
| 118 | { |
| 119 | log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk", |
| 120 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 121 | } |
| 122 | return false; |
| 123 | } |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 124 | if (!reg_ctx_sp->GetCFA (cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 125 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 126 | if (log) |
| 127 | { |
| 128 | log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk", |
| 129 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 130 | } |
| 131 | return false; |
| 132 | } |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 133 | if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 134 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 135 | if (log) |
| 136 | { |
| 137 | log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk", |
| 138 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 139 | } |
| 140 | return false; |
| 141 | } |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 142 | if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 143 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 144 | if (log) |
| 145 | { |
| 146 | log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk", |
| 147 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 148 | } |
| 149 | return false; |
| 150 | } |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 151 | if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc)) |
| 152 | { |
| 153 | if (log) |
| 154 | { |
| 155 | log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk", |
| 156 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 157 | } |
| 158 | return false; |
| 159 | } |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 160 | if (!m_frames.empty()) |
| 161 | { |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 162 | if (m_frames.back()->start_pc == cursor_sp->start_pc) |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 163 | { |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 164 | if (m_frames.back()->cfa == cursor_sp->cfa) |
| 165 | return false; // Infinite loop where the current cursor is the same as the previous one... |
| 166 | else if (abi->StackUsesFrames()) |
| 167 | { |
Greg Clayton | 19552cb | 2011-05-25 17:56:20 +0000 | [diff] [blame] | 168 | // We might have a CFA that is not using the frame pointer and |
| 169 | // we want to validate that the frame pointer is valid. |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 170 | if (reg_ctx_sp->GetFP() == 0) |
| 171 | return false; |
| 172 | } |
Greg Clayton | 1724dec | 2011-01-17 21:03:33 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 175 | cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 176 | m_frames.push_back (cursor_sp); |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool |
Jim Ingham | 591cf15 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 181 | UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 182 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 183 | if (m_frames.size() == 0) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 184 | { |
| 185 | if (!AddFirstFrame()) |
| 186 | return false; |
| 187 | } |
| 188 | |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 189 | ABI *abi = m_thread.GetProcess().GetABI().get(); |
| 190 | |
| 191 | while (idx >= m_frames.size() && AddOneMoreFrame (abi)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 192 | ; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 193 | |
| 194 | if (idx < m_frames.size ()) |
| 195 | { |
Jason Molenda | 800d11d | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 196 | cfa = m_frames[idx]->cfa; |
| 197 | pc = m_frames[idx]->start_pc; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 203 | lldb::RegisterContextSP |
Jim Ingham | 591cf15 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 204 | UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 205 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 206 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 869efc2 | 2011-01-08 01:53:06 +0000 | [diff] [blame] | 207 | uint32_t idx = frame->GetConcreteFrameIndex (); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 208 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 209 | if (idx == 0) |
| 210 | { |
| 211 | return m_thread.GetRegisterContext(); |
| 212 | } |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 213 | |
| 214 | if (m_frames.size() == 0) |
| 215 | { |
| 216 | if (!AddFirstFrame()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 217 | return reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Greg Clayton | 54b3841 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 220 | ABI *abi = m_thread.GetProcess().GetABI().get(); |
| 221 | |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 222 | while (idx >= m_frames.size()) |
| 223 | { |
| 224 | if (!AddOneMoreFrame (abi)) |
| 225 | break; |
| 226 | } |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 227 | |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 228 | const uint32_t num_frames = m_frames.size(); |
| 229 | if (idx < num_frames) |
| 230 | { |
| 231 | Cursor *frame_cursor = m_frames[idx].get(); |
| 232 | reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp->shared_from_this(); |
| 233 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 234 | return reg_ctx_sp; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 235 | } |
Jason Molenda | abe2d36 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 236 | |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 237 | UnwindLLDB::RegisterContextLLDBSP |
Jason Molenda | abe2d36 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 238 | UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num) |
| 239 | { |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 240 | RegisterContextLLDBSP reg_ctx_sp; |
| 241 | if (frame_num < m_frames.size()) |
| 242 | reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp; |
Jason Molenda | abe2d36 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 243 | return reg_ctx_sp; |
| 244 | } |
| 245 | |
| 246 | bool |
| 247 | UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc, uint32_t starting_frame_num) |
| 248 | { |
| 249 | int64_t frame_num = starting_frame_num; |
| 250 | if (frame_num >= m_frames.size()) |
| 251 | return false; |
| 252 | while (frame_num >= 0) |
| 253 | { |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 254 | if (m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc, false)) |
Jason Molenda | abe2d36 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 255 | return true; |
| 256 | frame_num--; |
| 257 | } |
| 258 | return false; |
| 259 | } |