Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadGDBRemote.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 | |
| 11 | #include "ThreadGDBRemote.h" |
| 12 | |
| 13 | #include "lldb/Core/ArchSpec.h" |
| 14 | #include "lldb/Core/DataExtractor.h" |
| 15 | #include "lldb/Core/StreamString.h" |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 16 | #include "lldb/Core/State.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 19 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Target.h" |
| 21 | #include "lldb/Target/Unwind.h" |
| 22 | #include "lldb/Breakpoint/WatchpointLocation.h" |
| 23 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "ProcessGDBRemote.h" |
| 25 | #include "ProcessGDBRemoteLog.h" |
Greg Clayton | f29a08f | 2011-02-09 17:41:27 +0000 | [diff] [blame] | 26 | #include "Plugins/Process/Utility/UnwindLLDB.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 27 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 29 | #if defined(__APPLE__) |
Greg Clayton | 8d110ff | 2011-02-05 06:35:06 +0000 | [diff] [blame] | 30 | #include "UnwindMacOSXFrameBackchain.h" |
| 31 | #endif |
| 32 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | using namespace lldb; |
| 34 | using namespace lldb_private; |
| 35 | |
| 36 | //---------------------------------------------------------------------- |
| 37 | // Thread Registers |
| 38 | //---------------------------------------------------------------------- |
| 39 | |
| 40 | ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) : |
| 41 | Thread(process, tid), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | m_thread_name (), |
| 43 | m_dispatch_queue_name (), |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 44 | m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID()); |
| 47 | } |
| 48 | |
| 49 | ThreadGDBRemote::~ThreadGDBRemote () |
| 50 | { |
| 51 | ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID()); |
Jim Ingham | cdea236 | 2010-11-18 02:47:07 +0000 | [diff] [blame] | 52 | DestroyThread(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | const char * |
| 57 | ThreadGDBRemote::GetInfo () |
| 58 | { |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | const char * |
| 64 | ThreadGDBRemote::GetName () |
| 65 | { |
| 66 | if (m_thread_name.empty()) |
| 67 | return NULL; |
| 68 | return m_thread_name.c_str(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | const char * |
| 73 | ThreadGDBRemote::GetQueueName () |
| 74 | { |
| 75 | // Always re-fetch the dispatch queue name since it can change |
| 76 | if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) |
| 77 | return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | bool |
| 82 | ThreadGDBRemote::WillResume (StateType resume_state) |
| 83 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | ClearStackFrames(); |
Greg Clayton | 8f6be2a | 2010-10-09 01:40:57 +0000 | [diff] [blame] | 85 | // Call the Thread::WillResume first. If we stop at a signal, the stop info |
| 86 | // class for signal will set the resume signal that we need below. The signal |
| 87 | // stuff obeys the Process::UnixSignal defaults. |
| 88 | Thread::WillResume(resume_state); |
| 89 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | int signo = GetResumeSignal(); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 91 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP)); |
| 92 | if (log) |
| 93 | log->Printf ("Resuming thread: %4.4x with state: %s.", GetID(), StateAsCString(resume_state)); |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 94 | |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 95 | ProcessGDBRemote &process = GetGDBProcess(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | switch (resume_state) |
| 97 | { |
| 98 | case eStateSuspended: |
| 99 | case eStateStopped: |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 100 | // Don't append anything for threads that should stay stopped. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | break; |
| 102 | |
| 103 | case eStateRunning: |
| 104 | if (m_process.GetUnixSignals().SignalIsValid (signo)) |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 105 | process.m_continue_C_tids.push_back(std::make_pair(GetID(), signo)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | else |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 107 | process.m_continue_c_tids.push_back(GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | break; |
| 109 | |
| 110 | case eStateStepping: |
| 111 | if (m_process.GetUnixSignals().SignalIsValid (signo)) |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 112 | process.m_continue_S_tids.push_back(std::make_pair(GetID(), signo)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | else |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 114 | process.m_continue_s_tids.push_back(GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | break; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 116 | |
| 117 | default: |
| 118 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | ThreadGDBRemote::RefreshStateAfterStop() |
| 125 | { |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 126 | // Invalidate all registers in our register context. We don't set "force" to |
| 127 | // true because the stop reply packet might have had some register values |
| 128 | // that were expedited and these will already be copied into the register |
| 129 | // context by the time this function gets called. The GDBRemoteRegisterContext |
| 130 | // class has been made smart enough to detect when it needs to invalidate |
| 131 | // which registers are valid by putting hooks in the register read and |
| 132 | // register supply functions where they check the process stop ID and do |
| 133 | // the right thing. |
| 134 | const bool force = false; |
| 135 | GetRegisterContext()->InvalidateIfNeeded (force); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Unwind * |
| 139 | ThreadGDBRemote::GetUnwinder () |
| 140 | { |
| 141 | if (m_unwinder_ap.get() == NULL) |
| 142 | { |
| 143 | const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ()); |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 144 | const llvm::Triple::ArchType machine = target_arch.GetMachine(); |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 145 | switch (machine) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | { |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 147 | case llvm::Triple::x86_64: |
| 148 | case llvm::Triple::x86: |
Greg Clayton | 75906e4 | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 149 | case llvm::Triple::arm: |
| 150 | case llvm::Triple::thumb: |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 151 | m_unwinder_ap.reset (new UnwindLLDB (*this)); |
| 152 | break; |
| 153 | |
| 154 | default: |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 155 | #if defined(__APPLE__) |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 156 | m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this)); |
Greg Clayton | 8d110ff | 2011-02-05 06:35:06 +0000 | [diff] [blame] | 157 | #endif |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 158 | break; |
| 159 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | } |
| 161 | return m_unwinder_ap.get(); |
| 162 | } |
| 163 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | void |
| 165 | ThreadGDBRemote::ClearStackFrames () |
| 166 | { |
| 167 | Unwind *unwinder = GetUnwinder (); |
| 168 | if (unwinder) |
| 169 | unwinder->Clear(); |
| 170 | Thread::ClearStackFrames(); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | bool |
| 175 | ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) |
| 176 | { |
| 177 | return thread != 0; |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | ThreadGDBRemote::Dump(Log *log, uint32_t index) |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | |
| 186 | bool |
| 187 | ThreadGDBRemote::ShouldStop (bool &step_more) |
| 188 | { |
| 189 | return true; |
| 190 | } |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 191 | lldb::RegisterContextSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | ThreadGDBRemote::GetRegisterContext () |
| 193 | { |
| 194 | if (m_reg_context_sp.get() == NULL) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 195 | m_reg_context_sp = CreateRegisterContextForFrame (NULL); |
| 196 | return m_reg_context_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 199 | lldb::RegisterContextSP |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) |
| 201 | { |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 202 | lldb::RegisterContextSP reg_ctx_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 203 | const bool read_all_registers_at_once = false; |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 204 | uint32_t concrete_frame_idx = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | |
| 206 | if (frame) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 207 | concrete_frame_idx = frame->GetConcreteFrameIndex (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 209 | if (concrete_frame_idx == 0) |
| 210 | reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, GetGDBProcess().m_register_info, read_all_registers_at_once)); |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 211 | else if (m_unwinder_ap.get()) |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 212 | reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame); |
| 213 | return reg_ctx_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 216 | bool |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 217 | ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) |
| 218 | { |
| 219 | GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); |
| 220 | assert (gdb_reg_ctx); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 221 | return gdb_reg_ctx->PrivateSetRegisterValue (reg, response); |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 224 | lldb::StopInfoSP |
| 225 | ThreadGDBRemote::GetPrivateStopReason () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | { |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 227 | const uint32_t process_stop_id = GetProcess().GetStopID(); |
| 228 | if (m_thread_stop_reason_stop_id != process_stop_id || |
| 229 | (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | { |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 231 | // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason |
| 232 | // for this thread, then m_actual_stop_info_sp will not ever contain |
| 233 | // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false" |
| 234 | // check will never be able to tell us if we have the correct stop info |
| 235 | // for this thread and we will continually send qThreadStopInfo packets |
| 236 | // down to the remote GDB server, so we need to keep our own notion |
| 237 | // of the stop ID that m_actual_stop_info_sp is valid for (even if it |
| 238 | // contains nothing). We use m_thread_stop_reason_stop_id for this below. |
| 239 | m_thread_stop_reason_stop_id = process_stop_id; |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 240 | m_actual_stop_info_sp.reset(); |
| 241 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | StringExtractorGDBRemote stop_packet; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 243 | ProcessGDBRemote &gdb_process = GetGDBProcess(); |
| 244 | if (gdb_process.GetGDBRemote().GetThreadStopInfo(GetID(), stop_packet)) |
| 245 | gdb_process.SetThreadStopInfo (stop_packet); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 247 | return m_actual_stop_info_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |