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