Jason Molenda | ab4f192 | 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 | dc5eb69 | 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 | ab4f192 | 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 | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 19 | |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 20 | #include "UnwindLLDB.h" |
| 21 | #include "RegisterContextLLDB.h" |
| 22 | |
Jason Molenda | ab4f192 | 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), |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 28 | m_frames(), |
Jason Molenda | a4bea72 | 2014-02-14 05:06:49 +0000 | [diff] [blame] | 29 | m_unwind_complete(false), |
| 30 | m_user_supplied_trap_handler_functions() |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 31 | { |
Jason Molenda | a4bea72 | 2014-02-14 05:06:49 +0000 | [diff] [blame] | 32 | ProcessSP process_sp(thread.GetProcess()); |
| 33 | if (process_sp) |
| 34 | { |
| 35 | Args args; |
| 36 | process_sp->GetTarget().GetUserSpecifiedTrapHandlerNames (args); |
| 37 | size_t count = args.GetArgumentCount(); |
| 38 | for (size_t i = 0; i < count; i++) |
| 39 | { |
| 40 | const char *func_name = args.GetArgumentAtIndex(i); |
| 41 | m_user_supplied_trap_handler_functions.push_back (ConstString (func_name)); |
| 42 | } |
| 43 | } |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | uint32_t |
Jim Ingham | 8f07716 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 47 | UnwindLLDB::DoGetFrameCount() |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 48 | { |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 49 | if (!m_unwind_complete) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 50 | { |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 51 | //#define DEBUG_FRAME_SPEED 1 |
| 52 | #if DEBUG_FRAME_SPEED |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 53 | #define FRAME_COUNT 10000 |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 54 | TimeValue time_value (TimeValue::Now()); |
| 55 | #endif |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 56 | if (!AddFirstFrame ()) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 57 | return 0; |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 58 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 59 | ProcessSP process_sp (m_thread.GetProcess()); |
| 60 | ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 61 | |
| 62 | while (AddOneMoreFrame (abi)) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 63 | { |
| 64 | #if DEBUG_FRAME_SPEED |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 65 | if ((m_frames.size() % FRAME_COUNT) == 0) |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 66 | { |
| 67 | TimeValue now(TimeValue::Now()); |
| 68 | uint64_t delta_t = now - time_value; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 69 | printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n", |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 70 | FRAME_COUNT, |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 71 | delta_t / TimeValue::NanoSecPerSec, |
| 72 | delta_t % TimeValue::NanoSecPerSec, |
| 73 | (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec)); |
Greg Clayton | 58be07b | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 74 | time_value = now; |
| 75 | } |
| 76 | #endif |
| 77 | } |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 78 | } |
| 79 | return m_frames.size (); |
| 80 | } |
| 81 | |
| 82 | bool |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 83 | UnwindLLDB::AddFirstFrame () |
| 84 | { |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 85 | if (m_frames.size() > 0) |
| 86 | return true; |
| 87 | |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 88 | // First, set up the 0th (initial) frame |
| 89 | CursorSP first_cursor_sp(new Cursor ()); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 90 | RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread, |
| 91 | RegisterContextLLDBSP(), |
| 92 | first_cursor_sp->sctx, |
| 93 | 0, *this)); |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 94 | if (reg_ctx_sp.get() == NULL) |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 95 | goto unwind_done; |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 96 | |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 97 | if (!reg_ctx_sp->IsValid()) |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 98 | goto unwind_done; |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 99 | |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 100 | if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa)) |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 101 | goto unwind_done; |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 103 | if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc)) |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 104 | goto unwind_done; |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 105 | |
| 106 | // Everything checks out, so release the auto pointer value and let the |
| 107 | // cursor own it in its shared pointer |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 108 | first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 109 | m_frames.push_back (first_cursor_sp); |
| 110 | return true; |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 111 | |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 112 | unwind_done: |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 113 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
| 114 | if (log) |
| 115 | { |
| 116 | log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID()); |
| 117 | } |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 118 | m_unwind_complete = true; |
| 119 | return false; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // For adding a non-zero stack frame to m_frames. |
| 123 | bool |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 124 | UnwindLLDB::AddOneMoreFrame (ABI *abi) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 125 | { |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 126 | // If we've already gotten to the end of the stack, don't bother to try again... |
| 127 | if (m_unwind_complete) |
| 128 | return false; |
| 129 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 130 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 131 | CursorSP cursor_sp(new Cursor ()); |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 132 | |
| 133 | // Frame zero is a little different |
| 134 | if (m_frames.size() == 0) |
| 135 | return false; |
| 136 | |
| 137 | uint32_t cur_idx = m_frames.size (); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 138 | RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread, |
| 139 | m_frames[cur_idx - 1]->reg_ctx_lldb_sp, |
| 140 | cursor_sp->sctx, |
| 141 | cur_idx, |
| 142 | *this)); |
Jason Molenda | 9dbe9e6 | 2013-05-03 04:48:41 +0000 | [diff] [blame] | 143 | |
| 144 | // We want to detect an unwind that cycles erronously and stop backtracing. |
| 145 | // Don't want this maximum unwind limit to be too low -- if you have a backtrace |
| 146 | // with an "infinitely recursing" bug, it will crash when the stack blows out |
| 147 | // and the first 35,000 frames are uninteresting - it's the top most 5 frames that |
| 148 | // you actually care about. So you can't just cap the unwind at 10,000 or something. |
| 149 | // Realistically anything over around 200,000 is going to blow out the stack space. |
| 150 | // If we're still unwinding at that point, we're probably never going to finish. |
| 151 | if (cur_idx > 300000) |
| 152 | { |
| 153 | if (log) |
| 154 | log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.", |
| 155 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 156 | goto unwind_done; |
| 157 | } |
| 158 | |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 159 | if (reg_ctx_sp.get() == NULL) |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 160 | { |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 161 | // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return |
| 162 | // true. Subsequent calls to TryFallbackUnwindPlan() will return false. |
| 163 | if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) |
| 164 | { |
| 165 | return AddOneMoreFrame (abi); |
| 166 | } |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 167 | if (log) |
| 168 | log->Printf ("%*sFrame %d did not get a RegisterContext, stopping.", |
| 169 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 170 | goto unwind_done; |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 171 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 172 | |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 173 | if (!reg_ctx_sp->IsValid()) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 174 | { |
Jason Molenda | 22975a2 | 2014-11-13 07:31:45 +0000 | [diff] [blame] | 175 | // We failed to get a valid RegisterContext. |
| 176 | // See if the regctx below this on the stack has a fallback unwind plan it can use. |
| 177 | // Subsequent calls to TryFallbackUnwindPlan() will return false. |
Jason Molenda | 4b00893 | 2014-11-04 05:28:40 +0000 | [diff] [blame] | 178 | if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 179 | { |
| 180 | return AddOneMoreFrame (abi); |
| 181 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 182 | if (log) |
| 183 | { |
| 184 | log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk", |
| 185 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 186 | } |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 187 | goto unwind_done; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 188 | } |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 189 | if (!reg_ctx_sp->GetCFA (cursor_sp->cfa)) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 190 | { |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 191 | // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return |
| 192 | // true. Subsequent calls to TryFallbackUnwindPlan() will return false. |
| 193 | if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) |
| 194 | { |
| 195 | return AddOneMoreFrame (abi); |
| 196 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 197 | if (log) |
| 198 | { |
| 199 | log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk", |
| 200 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 201 | } |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 202 | goto unwind_done; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 203 | } |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 204 | if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa)) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 205 | { |
Jason Molenda | 4b79247 | 2014-01-03 22:06:25 +0000 | [diff] [blame] | 206 | // On Mac OS X, the _sigtramp asynchronous signal trampoline frame may not have |
| 207 | // its (constructed) CFA aligned correctly -- don't do the abi alignment check for |
| 208 | // these. |
Jason Molenda | 6223db27 | 2014-02-13 07:11:08 +0000 | [diff] [blame] | 209 | if (reg_ctx_sp->IsTrapHandlerFrame() == false) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 210 | { |
Jason Molenda | cea6d63 | 2014-11-22 01:52:03 +0000 | [diff] [blame] | 211 | // See if we can find a fallback unwind plan for THIS frame. It may be |
| 212 | // that the UnwindPlan we're using for THIS frame was bad and gave us a |
| 213 | // bad CFA. |
| 214 | // If that's not it, then see if we can change the UnwindPlan for the frame |
| 215 | // below us ("NEXT") -- see if using that other UnwindPlan gets us a better |
| 216 | // unwind state. |
| 217 | if (reg_ctx_sp->TryFallbackUnwindPlan() == false |
| 218 | || reg_ctx_sp->GetCFA (cursor_sp->cfa) == false |
| 219 | || abi->CallFrameAddressIsValid(cursor_sp->cfa) == false) |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 220 | { |
Jason Molenda | cea6d63 | 2014-11-22 01:52:03 +0000 | [diff] [blame] | 221 | if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) |
| 222 | { |
| 223 | return AddOneMoreFrame (abi); |
| 224 | } |
| 225 | if (log) |
| 226 | { |
| 227 | log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk", |
| 228 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 229 | } |
| 230 | goto unwind_done; |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 231 | } |
Jason Molenda | cea6d63 | 2014-11-22 01:52:03 +0000 | [diff] [blame] | 232 | else |
Jason Molenda | 4b79247 | 2014-01-03 22:06:25 +0000 | [diff] [blame] | 233 | { |
Jason Molenda | cea6d63 | 2014-11-22 01:52:03 +0000 | [diff] [blame] | 234 | if (log) |
| 235 | { |
| 236 | log->Printf("%*sFrame %d had a bad CFA value but we switched the UnwindPlan being used and got one that looks more realistic.", |
| 237 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 238 | } |
Jason Molenda | 4b79247 | 2014-01-03 22:06:25 +0000 | [diff] [blame] | 239 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 240 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 241 | } |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 242 | if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc)) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 243 | { |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 244 | // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return |
| 245 | // true. Subsequent calls to TryFallbackUnwindPlan() will return false. |
| 246 | if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) |
| 247 | { |
| 248 | return AddOneMoreFrame (abi); |
| 249 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 250 | if (log) |
| 251 | { |
| 252 | log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk", |
| 253 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 254 | } |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 255 | goto unwind_done; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 256 | } |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 257 | if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc)) |
| 258 | { |
Jason Molenda | 31d7ad4 | 2014-02-21 05:20:25 +0000 | [diff] [blame] | 259 | // If the RegisterContextLLDB has a fallback UnwindPlan, it will switch to that and return |
| 260 | // true. Subsequent calls to TryFallbackUnwindPlan() will return false. |
| 261 | if (m_frames[cur_idx - 1]->reg_ctx_lldb_sp->TryFallbackUnwindPlan()) |
| 262 | { |
| 263 | return AddOneMoreFrame (abi); |
| 264 | } |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 265 | if (log) |
| 266 | { |
| 267 | log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk", |
| 268 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 269 | } |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 270 | goto unwind_done; |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 271 | } |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 272 | if (!m_frames.empty()) |
| 273 | { |
| 274 | // Infinite loop where the current cursor is the same as the previous one... |
| 275 | if (m_frames.back()->start_pc == cursor_sp->start_pc && m_frames.back()->cfa == cursor_sp->cfa) |
| 276 | { |
| 277 | if (log) |
| 278 | log->Printf ("th%d pc of this frame is the same as the previous frame and CFAs for both frames are identical -- stopping unwind", m_thread.GetIndexID()); |
| 279 | goto unwind_done; |
| 280 | } |
| 281 | } |
Ashok Thirumurthi | 8b57730 | 2013-09-26 14:35:59 +0000 | [diff] [blame] | 282 | |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 283 | cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 284 | m_frames.push_back (cursor_sp); |
| 285 | return true; |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 286 | |
| 287 | unwind_done: |
Jason Molenda | 3d21975 | 2013-12-20 01:05:11 +0000 | [diff] [blame] | 288 | if (log) |
| 289 | { |
| 290 | log->Printf ("th%d Unwind of this thread is complete.", m_thread.GetIndexID()); |
| 291 | } |
Jim Ingham | b0c72a5 | 2012-02-29 03:40:22 +0000 | [diff] [blame] | 292 | m_unwind_complete = true; |
| 293 | return false; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | bool |
Jim Ingham | 8f07716 | 2011-10-21 01:49:48 +0000 | [diff] [blame] | 297 | UnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 298 | { |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 299 | if (m_frames.size() == 0) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 300 | { |
| 301 | if (!AddFirstFrame()) |
| 302 | return false; |
| 303 | } |
| 304 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 305 | ProcessSP process_sp (m_thread.GetProcess()); |
| 306 | ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 307 | |
| 308 | while (idx >= m_frames.size() && AddOneMoreFrame (abi)) |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 309 | ; |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 310 | |
| 311 | if (idx < m_frames.size ()) |
| 312 | { |
Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 313 | cfa = m_frames[idx]->cfa; |
| 314 | pc = m_frames[idx]->start_pc; |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 315 | return true; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 320 | lldb::RegisterContextSP |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 321 | UnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 322 | { |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 323 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 671cabe | 2011-01-08 01:53:06 +0000 | [diff] [blame] | 324 | uint32_t idx = frame->GetConcreteFrameIndex (); |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 325 | |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 326 | if (idx == 0) |
| 327 | { |
| 328 | return m_thread.GetRegisterContext(); |
| 329 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 330 | |
| 331 | if (m_frames.size() == 0) |
| 332 | { |
| 333 | if (!AddFirstFrame()) |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 334 | return reg_ctx_sp; |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 337 | ProcessSP process_sp (m_thread.GetProcess()); |
| 338 | ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; |
Greg Clayton | 9b72eb7 | 2011-05-24 23:06:02 +0000 | [diff] [blame] | 339 | |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 340 | while (idx >= m_frames.size()) |
| 341 | { |
| 342 | if (!AddOneMoreFrame (abi)) |
| 343 | break; |
| 344 | } |
Jason Molenda | 8fed295 | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 345 | |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 346 | const uint32_t num_frames = m_frames.size(); |
| 347 | if (idx < num_frames) |
| 348 | { |
| 349 | Cursor *frame_cursor = m_frames[idx].get(); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 350 | reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 351 | } |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 352 | return reg_ctx_sp; |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 353 | } |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 354 | |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 355 | UnwindLLDB::RegisterContextLLDBSP |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 356 | UnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num) |
| 357 | { |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 358 | RegisterContextLLDBSP reg_ctx_sp; |
| 359 | if (frame_num < m_frames.size()) |
| 360 | reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp; |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 361 | return reg_ctx_sp; |
| 362 | } |
| 363 | |
| 364 | bool |
Jason Molenda | 23399d7 | 2013-06-05 00:12:50 +0000 | [diff] [blame] | 365 | UnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc, uint32_t starting_frame_num, bool pc_reg) |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 366 | { |
| 367 | int64_t frame_num = starting_frame_num; |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame] | 368 | if (static_cast<size_t>(frame_num) >= m_frames.size()) |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 369 | return false; |
Jason Molenda | 60f0bd4 | 2012-10-26 06:08:58 +0000 | [diff] [blame] | 370 | |
| 371 | // Never interrogate more than one level while looking for the saved pc value. If the value |
| 372 | // isn't saved by frame_num, none of the frames lower on the stack will have a useful value. |
Jason Molenda | 23399d7 | 2013-06-05 00:12:50 +0000 | [diff] [blame] | 373 | if (pc_reg) |
Jason Molenda | 60f0bd4 | 2012-10-26 06:08:58 +0000 | [diff] [blame] | 374 | { |
Jason Molenda | aff2a26 | 2012-11-16 01:03:31 +0000 | [diff] [blame] | 375 | UnwindLLDB::RegisterSearchResult result; |
| 376 | result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc); |
| 377 | if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound) |
Jason Molenda | 60f0bd4 | 2012-10-26 06:08:58 +0000 | [diff] [blame] | 378 | return true; |
| 379 | else |
| 380 | return false; |
| 381 | } |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 382 | while (frame_num >= 0) |
| 383 | { |
Jason Molenda | aff2a26 | 2012-11-16 01:03:31 +0000 | [diff] [blame] | 384 | UnwindLLDB::RegisterSearchResult result; |
| 385 | result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc); |
Jason Molenda | 4c781fd7 | 2013-01-19 03:53:42 +0000 | [diff] [blame] | 386 | |
Jason Molenda | ce19fe3 | 2014-12-09 22:28:10 +0000 | [diff] [blame] | 387 | // We descended down to the live register context aka stack frame 0 and are reading the value |
| 388 | // out of a live register. |
| 389 | if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound |
| 390 | && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext) |
| 391 | { |
| 392 | return true; |
| 393 | } |
| 394 | |
Jason Molenda | 4c781fd7 | 2013-01-19 03:53:42 +0000 | [diff] [blame] | 395 | // If we have unwind instructions saying that register N is saved in register M in the middle of |
| 396 | // the stack (and N can equal M here, meaning the register was not used in this function), then |
| 397 | // change the register number we're looking for to M and keep looking for a concrete location |
| 398 | // down the stack, or an actual value from a live RegisterContext at frame 0. |
| 399 | if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound |
| 400 | && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister |
| 401 | && frame_num > 0) |
| 402 | { |
| 403 | result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound; |
| 404 | lldb_regnum = regloc.location.register_number; |
| 405 | } |
| 406 | |
Jason Molenda | aff2a26 | 2012-11-16 01:03:31 +0000 | [diff] [blame] | 407 | if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound) |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 408 | return true; |
Jason Molenda | aff2a26 | 2012-11-16 01:03:31 +0000 | [diff] [blame] | 409 | if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile) |
| 410 | return false; |
Jason Molenda | 707fec4 | 2011-11-01 03:21:25 +0000 | [diff] [blame] | 411 | frame_num--; |
| 412 | } |
| 413 | return false; |
| 414 | } |