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