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