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 | |
| 23 | #include "LibUnwindRegisterContext.h" |
| 24 | #include "ProcessGDBRemote.h" |
| 25 | #include "ProcessGDBRemoteLog.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 26 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "UnwindLibUnwind.h" |
| 28 | #include "UnwindMacOSXFrameBackchain.h" |
Jason Molenda | 8132f2d | 2010-11-04 09:51:29 +0000 | [diff] [blame] | 29 | #include "UnwindLLDB.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | //---------------------------------------------------------------------- |
| 35 | // Thread Registers |
| 36 | //---------------------------------------------------------------------- |
| 37 | |
| 38 | ThreadGDBRemote::ThreadGDBRemote (ProcessGDBRemote &process, lldb::tid_t tid) : |
| 39 | Thread(process, tid), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | m_thread_name (), |
| 41 | m_dispatch_queue_name (), |
Jim Ingham | 7121908 | 2010-08-12 02:14:28 +0000 | [diff] [blame] | 42 | m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | { |
| 44 | // ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD | GDBR_LOG_VERBOSE, "ThreadGDBRemote::ThreadGDBRemote ( pid = %i, tid = 0x%4.4x, )", m_process.GetID(), GetID()); |
| 45 | ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID()); |
| 46 | } |
| 47 | |
| 48 | ThreadGDBRemote::~ThreadGDBRemote () |
| 49 | { |
| 50 | ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, m_process.GetID(), GetID()); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | const char * |
| 55 | ThreadGDBRemote::GetInfo () |
| 56 | { |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | const char * |
| 62 | ThreadGDBRemote::GetName () |
| 63 | { |
| 64 | if (m_thread_name.empty()) |
| 65 | return NULL; |
| 66 | return m_thread_name.c_str(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | const char * |
| 71 | ThreadGDBRemote::GetQueueName () |
| 72 | { |
| 73 | // Always re-fetch the dispatch queue name since it can change |
| 74 | if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) |
| 75 | return GetGDBProcess().GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | ThreadGDBRemote::WillResume (StateType resume_state) |
| 81 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | ClearStackFrames(); |
Greg Clayton | 8f6be2a | 2010-10-09 01:40:57 +0000 | [diff] [blame] | 83 | // Call the Thread::WillResume first. If we stop at a signal, the stop info |
| 84 | // class for signal will set the resume signal that we need below. The signal |
| 85 | // stuff obeys the Process::UnixSignal defaults. |
| 86 | Thread::WillResume(resume_state); |
| 87 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | int signo = GetResumeSignal(); |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | switch (resume_state) |
| 91 | { |
| 92 | case eStateSuspended: |
| 93 | case eStateStopped: |
| 94 | // Don't append anything for threads that should stay stopped. |
| 95 | break; |
| 96 | |
| 97 | case eStateRunning: |
| 98 | if (m_process.GetUnixSignals().SignalIsValid (signo)) |
| 99 | GetGDBProcess().m_continue_packet.Printf(";C%2.2x:%4.4x", signo, GetID()); |
| 100 | else |
| 101 | GetGDBProcess().m_continue_packet.Printf(";c:%4.4x", GetID()); |
| 102 | break; |
| 103 | |
| 104 | case eStateStepping: |
| 105 | if (m_process.GetUnixSignals().SignalIsValid (signo)) |
| 106 | GetGDBProcess().m_continue_packet.Printf(";S%2.2x:%4.4x", signo, GetID()); |
| 107 | else |
| 108 | GetGDBProcess().m_continue_packet.Printf(";s:%4.4x", GetID()); |
| 109 | break; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 110 | |
| 111 | default: |
| 112 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | ThreadGDBRemote::RefreshStateAfterStop() |
| 119 | { |
| 120 | // Invalidate all registers in our register context |
| 121 | GetRegisterContext()->Invalidate(); |
| 122 | } |
| 123 | |
Jason Molenda | 8132f2d | 2010-11-04 09:51:29 +0000 | [diff] [blame] | 124 | // Whether to use the new native unwinder (UnwindLLDB) or the libunwind-remote based unwinder for |
| 125 | // stack walks on i386/x86_64 |
| 126 | #undef USE_NATIVE_UNWINDER |
| 127 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | Unwind * |
| 129 | ThreadGDBRemote::GetUnwinder () |
| 130 | { |
| 131 | if (m_unwinder_ap.get() == NULL) |
| 132 | { |
| 133 | const ArchSpec target_arch (GetProcess().GetTarget().GetArchitecture ()); |
| 134 | if (target_arch == ArchSpec("x86_64") || target_arch == ArchSpec("i386")) |
| 135 | { |
Jason Molenda | 8132f2d | 2010-11-04 09:51:29 +0000 | [diff] [blame] | 136 | #if defined (USE_NATIVE_UNWINDER) |
| 137 | m_unwinder_ap.reset (new UnwindLLDB (*this)); |
| 138 | #else |
Jason Molenda | 96829fb | 2010-11-04 09:46:43 +0000 | [diff] [blame] | 139 | m_unwinder_ap.reset (new UnwindLibUnwind (*this, GetGDBProcess().GetLibUnwindAddressSpace())); |
Jason Molenda | 8132f2d | 2010-11-04 09:51:29 +0000 | [diff] [blame] | 140 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
| 144 | m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this)); |
| 145 | } |
| 146 | } |
| 147 | return m_unwinder_ap.get(); |
| 148 | } |
| 149 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | void |
| 151 | ThreadGDBRemote::ClearStackFrames () |
| 152 | { |
| 153 | Unwind *unwinder = GetUnwinder (); |
| 154 | if (unwinder) |
| 155 | unwinder->Clear(); |
| 156 | Thread::ClearStackFrames(); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | bool |
| 161 | ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) |
| 162 | { |
| 163 | return thread != 0; |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | ThreadGDBRemote::Dump(Log *log, uint32_t index) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | |
| 172 | bool |
| 173 | ThreadGDBRemote::ShouldStop (bool &step_more) |
| 174 | { |
| 175 | return true; |
| 176 | } |
| 177 | RegisterContext * |
| 178 | ThreadGDBRemote::GetRegisterContext () |
| 179 | { |
| 180 | if (m_reg_context_sp.get() == NULL) |
| 181 | m_reg_context_sp.reset (CreateRegisterContextForFrame (NULL)); |
| 182 | return m_reg_context_sp.get(); |
| 183 | } |
| 184 | |
| 185 | RegisterContext * |
| 186 | ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) |
| 187 | { |
| 188 | const bool read_all_registers_at_once = false; |
| 189 | uint32_t frame_idx = 0; |
| 190 | |
| 191 | if (frame) |
Greg Clayton | 33ed170 | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 192 | frame_idx = frame->GetFrameIndex (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | |
| 194 | if (frame_idx == 0) |
| 195 | return new GDBRemoteRegisterContext (*this, frame, 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()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | return m_unwinder_ap->CreateRegisterContextForFrame (frame); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | bool |
| 202 | ThreadGDBRemote::SaveFrameZeroState (RegisterCheckpoint &checkpoint) |
| 203 | { |
| 204 | lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); |
| 205 | if (frame_sp) |
| 206 | { |
| 207 | checkpoint.SetStackID(frame_sp->GetStackID()); |
| 208 | return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData()); |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | bool |
| 214 | ThreadGDBRemote::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) |
| 215 | { |
| 216 | lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); |
| 217 | if (frame_sp) |
| 218 | { |
| 219 | bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData()); |
| 220 | frame_sp->GetRegisterContext()->Invalidate(); |
| 221 | ClearStackFrames(); |
| 222 | return ret; |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 227 | lldb::StopInfoSP |
| 228 | ThreadGDBRemote::GetPrivateStopReason () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 230 | if (m_actual_stop_info_sp.get() == NULL || m_actual_stop_info_sp->IsValid() == false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 232 | m_actual_stop_info_sp.reset(); |
| 233 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 234 | char packet[256]; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 235 | ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | StringExtractorGDBRemote stop_packet; |
| 237 | if (GetGDBProcess().GetGDBRemote().SendPacketAndWaitForResponse(packet, stop_packet, 1, false)) |
| 238 | { |
| 239 | std::string copy(stop_packet.GetStringRef()); |
| 240 | GetGDBProcess().SetThreadStopInfo (stop_packet); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 243 | return m_actual_stop_info_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |