Chris Lattner | 30fdc8d | 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 | |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 13 | #include "lldb/Breakpoint/Watchpoint.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | #include "lldb/Core/ArchSpec.h" |
| 15 | #include "lldb/Core/DataExtractor.h" |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 16 | #include "lldb/Core/State.h" |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamString.h" |
| 18 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Target/Process.h" |
| 20 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 21 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
| 23 | #include "lldb/Target/Unwind.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "ProcessGDBRemote.h" |
| 26 | #include "ProcessGDBRemoteLog.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 27 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 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 | |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 36 | ThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) : |
| 37 | Thread(process, tid), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | m_thread_name (), |
| 39 | m_dispatch_queue_name (), |
Jim Ingham | 7778703 | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 40 | m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | { |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 42 | ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", |
| 43 | this, |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 44 | process.GetID(), |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 45 | GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ThreadGDBRemote::~ThreadGDBRemote () |
| 49 | { |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 50 | ProcessSP process_sp(GetProcess()); |
| 51 | ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", |
| 52 | this, |
| 53 | process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, |
| 54 | GetID()); |
Jim Ingham | 773d981 | 2010-11-18 02:47:07 +0000 | [diff] [blame] | 55 | DestroyThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | const char * |
| 59 | ThreadGDBRemote::GetName () |
| 60 | { |
| 61 | if (m_thread_name.empty()) |
| 62 | return NULL; |
| 63 | return m_thread_name.c_str(); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | const char * |
| 68 | ThreadGDBRemote::GetQueueName () |
| 69 | { |
| 70 | // Always re-fetch the dispatch queue name since it can change |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 73 | { |
| 74 | ProcessSP process_sp (GetProcess()); |
| 75 | if (process_sp) |
| 76 | { |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 77 | PlatformSP platform_sp (process_sp->GetTarget().GetPlatform()); |
| 78 | if (platform_sp) |
| 79 | { |
| 80 | m_dispatch_queue_name = platform_sp->GetQueueNameForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr); |
| 81 | } |
| 82 | if (m_dispatch_queue_name.length() > 0) |
| 83 | { |
| 84 | return m_dispatch_queue_name.c_str(); |
| 85 | } |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
| 90 | |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 91 | queue_id_t |
| 92 | ThreadGDBRemote::GetQueueID () |
| 93 | { |
| 94 | if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) |
| 95 | { |
| 96 | ProcessSP process_sp (GetProcess()); |
| 97 | if (process_sp) |
| 98 | { |
| 99 | PlatformSP platform_sp (process_sp->GetTarget().GetPlatform()); |
| 100 | if (platform_sp) |
| 101 | { |
| 102 | return platform_sp->GetQueueIDForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return LLDB_INVALID_QUEUE_ID; |
| 107 | } |
| 108 | |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 109 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | ThreadGDBRemote::WillResume (StateType resume_state) |
| 111 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | int signo = GetResumeSignal(); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 113 | const lldb::user_id_t tid = GetProtocolID(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 114 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD)); |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 115 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 116 | log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state)); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 117 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 118 | ProcessSP process_sp (GetProcess()); |
| 119 | if (process_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | { |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 121 | ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); |
| 122 | switch (resume_state) |
| 123 | { |
| 124 | case eStateSuspended: |
| 125 | case eStateStopped: |
| 126 | // Don't append anything for threads that should stay stopped. |
| 127 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 129 | case eStateRunning: |
| 130 | if (gdb_process->GetUnixSignals().SignalIsValid (signo)) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 131 | gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 132 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 133 | gdb_process->m_continue_c_tids.push_back(tid); |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 134 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 136 | case eStateStepping: |
| 137 | if (gdb_process->GetUnixSignals().SignalIsValid (signo)) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 138 | gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 139 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 140 | gdb_process->m_continue_s_tids.push_back(tid); |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 141 | break; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 142 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 143 | default: |
| 144 | break; |
| 145 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void |
| 150 | ThreadGDBRemote::RefreshStateAfterStop() |
| 151 | { |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 152 | // Invalidate all registers in our register context. We don't set "force" to |
| 153 | // true because the stop reply packet might have had some register values |
| 154 | // that were expedited and these will already be copied into the register |
| 155 | // context by the time this function gets called. The GDBRemoteRegisterContext |
| 156 | // class has been made smart enough to detect when it needs to invalidate |
| 157 | // which registers are valid by putting hooks in the register read and |
| 158 | // register supply functions where they check the process stop ID and do |
| 159 | // the right thing. |
| 160 | const bool force = false; |
| 161 | GetRegisterContext()->InvalidateIfNeeded (force); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | bool |
| 165 | ThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread) |
| 166 | { |
| 167 | return thread != 0; |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | ThreadGDBRemote::Dump(Log *log, uint32_t index) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | |
| 176 | bool |
| 177 | ThreadGDBRemote::ShouldStop (bool &step_more) |
| 178 | { |
| 179 | return true; |
| 180 | } |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 181 | lldb::RegisterContextSP |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | ThreadGDBRemote::GetRegisterContext () |
| 183 | { |
| 184 | if (m_reg_context_sp.get() == NULL) |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 185 | m_reg_context_sp = CreateRegisterContextForFrame (NULL); |
| 186 | return m_reg_context_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 189 | lldb::RegisterContextSP |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame^] | 190 | ThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 191 | { |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 192 | lldb::RegisterContextSP reg_ctx_sp; |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 193 | uint32_t concrete_frame_idx = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | |
| 195 | if (frame) |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 196 | concrete_frame_idx = frame->GetConcreteFrameIndex (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 198 | |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 199 | if (concrete_frame_idx == 0) |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 200 | { |
| 201 | ProcessSP process_sp (GetProcess()); |
| 202 | if (process_sp) |
| 203 | { |
| 204 | ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); |
Hafiz Abid Qadeer | 9a78cdf | 2013-08-29 09:09:45 +0000 | [diff] [blame] | 205 | // read_all_registers_at_once will be true if 'p' packet is not supported. |
Sean Callanan | b1de114 | 2013-09-04 23:24:15 +0000 | [diff] [blame] | 206 | bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID()); |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 207 | reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once)); |
| 208 | } |
| 209 | } |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 210 | else |
| 211 | { |
| 212 | Unwind *unwinder = GetUnwinder (); |
| 213 | if (unwinder) |
| 214 | reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); |
| 215 | } |
Greg Clayton | 5ccbd29 | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 216 | return reg_ctx_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 219 | bool |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 220 | ThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response) |
| 221 | { |
| 222 | GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get()); |
| 223 | assert (gdb_reg_ctx); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 224 | return gdb_reg_ctx->PrivateSetRegisterValue (reg, response); |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 227 | bool |
| 228 | ThreadGDBRemote::CalculateStopInfo () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | { |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 230 | ProcessSP process_sp (GetProcess()); |
| 231 | if (process_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | { |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 233 | StringExtractorGDBRemote stop_packet; |
| 234 | ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get()); |
| 235 | if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet)) |
| 236 | return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | } |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 238 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |