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 |
| 38 | TimeValue time_value (TimeValue::Now()); |
| 39 | #endif |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 40 | if (!AddFirstFrame ()) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 41 | return 0; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 42 | while (AddOneMoreFrame ()) |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 43 | { |
| 44 | #if DEBUG_FRAME_SPEED |
| 45 | if ((m_frames.size() % 10000) == 0) |
| 46 | { |
| 47 | TimeValue now(TimeValue::Now()); |
| 48 | uint64_t delta_t = now - time_value; |
| 49 | printf ("10000 frames in %llu.%09llu ms\n", delta_t / NSEC_PER_SEC, delta_t % NSEC_PER_SEC); |
| 50 | time_value = now; |
| 51 | } |
| 52 | #endif |
| 53 | } |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 54 | } |
| 55 | return m_frames.size (); |
| 56 | } |
| 57 | |
| 58 | bool |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 59 | UnwindLLDB::AddFirstFrame () |
| 60 | { |
| 61 | // First, set up the 0th (initial) frame |
| 62 | CursorSP first_cursor_sp(new Cursor ()); |
| 63 | RegisterContextSP no_frame; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 64 | std::auto_ptr<RegisterContextLLDB> first_register_ctx_ap (new RegisterContextLLDB(m_thread, no_frame, first_cursor_sp->sctx, 0)); |
| 65 | if (first_register_ctx_ap.get() == NULL) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 66 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 67 | |
| 68 | if (!first_register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 69 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 70 | |
| 71 | if (!first_register_ctx_ap->GetCFA (first_cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 72 | return false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 73 | |
| 74 | if (!first_register_ctx_ap->GetPC (first_cursor_sp->start_pc)) |
| 75 | return false; |
| 76 | |
| 77 | // Everything checks out, so release the auto pointer value and let the |
| 78 | // cursor own it in its shared pointer |
| 79 | first_cursor_sp->reg_ctx.reset(first_register_ctx_ap.release()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 80 | m_frames.push_back (first_cursor_sp); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | // For adding a non-zero stack frame to m_frames. |
| 85 | bool |
| 86 | UnwindLLDB::AddOneMoreFrame () |
| 87 | { |
| 88 | LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
| 89 | CursorSP cursor_sp(new Cursor ()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 90 | |
| 91 | // Frame zero is a little different |
| 92 | if (m_frames.size() == 0) |
| 93 | return false; |
| 94 | |
| 95 | uint32_t cur_idx = m_frames.size (); |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 96 | std::auto_ptr<RegisterContextLLDB> register_ctx_ap(new RegisterContextLLDB (m_thread, |
| 97 | m_frames[cur_idx - 1]->reg_ctx, |
| 98 | cursor_sp->sctx, |
| 99 | cur_idx)); |
| 100 | if (register_ctx_ap.get() == NULL) |
| 101 | return false; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 103 | if (!register_ctx_ap->IsValid()) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 104 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 105 | if (log) |
| 106 | { |
| 107 | log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk", |
| 108 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 109 | } |
| 110 | return false; |
| 111 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 112 | if (!register_ctx_ap->GetCFA (cursor_sp->cfa)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 113 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 114 | if (log) |
| 115 | { |
| 116 | log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk", |
| 117 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | if (cursor_sp->cfa == (addr_t) -1 || cursor_sp->cfa == 1 || cursor_sp->cfa == 0) |
| 122 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 123 | if (log) |
| 124 | { |
| 125 | log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk", |
| 126 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 127 | } |
| 128 | return false; |
| 129 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 130 | if (!register_ctx_ap->GetPC (cursor_sp->start_pc)) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 131 | { |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 132 | if (log) |
| 133 | { |
| 134 | log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk", |
| 135 | cur_idx < 100 ? cur_idx : 100, "", cur_idx); |
| 136 | } |
| 137 | return false; |
| 138 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 139 | RegisterContextSP register_ctx_sp(register_ctx_ap.release()); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 140 | cursor_sp->reg_ctx = register_ctx_sp; |
| 141 | m_frames.push_back (cursor_sp); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 146 | UnwindLLDB::GetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc) |
| 147 | { |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 148 | if (m_frames.size() == 0) |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 149 | { |
| 150 | if (!AddFirstFrame()) |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 155 | ; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 156 | |
| 157 | if (idx < m_frames.size ()) |
| 158 | { |
Jason Molenda | 800d11d | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 159 | cfa = m_frames[idx]->cfa; |
| 160 | pc = m_frames[idx]->start_pc; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 166 | lldb::RegisterContextSP |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 167 | UnwindLLDB::CreateRegisterContextForFrame (StackFrame *frame) |
| 168 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 169 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 869efc2 | 2011-01-08 01:53:06 +0000 | [diff] [blame^] | 170 | uint32_t idx = frame->GetConcreteFrameIndex (); |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 171 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 172 | if (idx == 0) |
| 173 | { |
| 174 | return m_thread.GetRegisterContext(); |
| 175 | } |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 176 | |
| 177 | if (m_frames.size() == 0) |
| 178 | { |
| 179 | if (!AddFirstFrame()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 180 | return reg_ctx_sp; |
Jason Molenda | 1da513b | 2010-11-09 02:31:21 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | while (idx >= m_frames.size() && AddOneMoreFrame ()) |
| 184 | ; |
| 185 | |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 186 | if (idx < m_frames.size ()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 187 | reg_ctx_sp = m_frames[idx]->reg_ctx; |
| 188 | return reg_ctx_sp; |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 189 | } |