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