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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | #include "ThreadGDBRemote.h" |
| 11 | |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 12 | #include "lldb/Breakpoint/Watchpoint.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ArchSpec.h" |
| 14 | #include "lldb/Core/DataExtractor.h" |
Jim Ingham | 0f16e73 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 15 | #include "lldb/Core/State.h" |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Platform.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Process.h" |
| 18 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 19 | #include "lldb/Target/StopInfo.h" |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 20 | #include "lldb/Target/SystemRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Zachary Turner | 93749ab | 2015-03-03 21:51:25 +0000 | [diff] [blame] | 22 | #include "lldb/Target/UnixSignals.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Unwind.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/StreamString.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; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 32 | using namespace lldb_private::process_gdb_remote; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | |
| 34 | //---------------------------------------------------------------------- |
| 35 | // Thread Registers |
| 36 | //---------------------------------------------------------------------- |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | ThreadGDBRemote::ThreadGDBRemote(Process &process, lldb::tid_t tid) |
| 39 | : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(), |
| 40 | m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS), |
| 41 | m_dispatch_queue_t(LLDB_INVALID_ADDRESS), m_queue_kind(eQueueKindUnknown), |
| 42 | m_queue_serial_number(LLDB_INVALID_QUEUE_ID), |
| 43 | m_associated_with_libdispatch_queue(eLazyBoolCalculate) { |
| 44 | ProcessGDBRemoteLog::LogIf( |
| 45 | GDBR_LOG_THREAD, |
| 46 | "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, |
| 47 | process.GetID(), GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | ThreadGDBRemote::~ThreadGDBRemote() { |
| 51 | ProcessSP process_sp(GetProcess()); |
| 52 | ProcessGDBRemoteLog::LogIf( |
| 53 | GDBR_LOG_THREAD, |
| 54 | "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)", this, |
| 55 | process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID, GetID()); |
| 56 | DestroyThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | const char *ThreadGDBRemote::GetName() { |
| 60 | if (m_thread_name.empty()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | return m_thread_name.c_str(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | void ThreadGDBRemote::ClearQueueInfo() { |
| 66 | m_dispatch_queue_name.clear(); |
| 67 | m_queue_kind = eQueueKindUnknown; |
| 68 | m_queue_serial_number = 0; |
| 69 | m_dispatch_queue_t = LLDB_INVALID_ADDRESS; |
| 70 | m_associated_with_libdispatch_queue = eLazyBoolCalculate; |
Jason Molenda | 77f8935 | 2016-01-12 07:09:16 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | void ThreadGDBRemote::SetQueueInfo(std::string &&queue_name, |
| 74 | QueueKind queue_kind, uint64_t queue_serial, |
| 75 | addr_t dispatch_queue_t, |
| 76 | LazyBool associated_with_libdispatch_queue) { |
| 77 | m_dispatch_queue_name = queue_name; |
| 78 | m_queue_kind = queue_kind; |
| 79 | m_queue_serial_number = queue_serial; |
| 80 | m_dispatch_queue_t = dispatch_queue_t; |
| 81 | m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; |
Jason Molenda | 3dc4f44 | 2013-10-18 05:55:24 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | const char *ThreadGDBRemote::GetQueueName() { |
| 85 | // If our cached queue info is valid, then someone called |
| 86 | // ThreadGDBRemote::SetQueueInfo(...) |
| 87 | // with valid information that was gleaned from the stop reply packet. In this |
| 88 | // case we trust |
| 89 | // that the info is valid in m_dispatch_queue_name without refetching it |
| 90 | if (CachedQueueInfoIsValid()) { |
| 91 | if (m_dispatch_queue_name.empty()) |
| 92 | return nullptr; |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 93 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | return m_dispatch_queue_name.c_str(); |
| 95 | } |
| 96 | // Always re-fetch the dispatch queue name since it can change |
| 97 | |
| 98 | if (m_associated_with_libdispatch_queue == eLazyBoolNo) |
| 99 | return nullptr; |
| 100 | |
| 101 | if (m_thread_dispatch_qaddr != 0 && |
| 102 | m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { |
| 103 | ProcessSP process_sp(GetProcess()); |
| 104 | if (process_sp) { |
| 105 | SystemRuntime *runtime = process_sp->GetSystemRuntime(); |
| 106 | if (runtime) |
| 107 | m_dispatch_queue_name = |
| 108 | runtime->GetQueueNameFromThreadQAddress(m_thread_dispatch_qaddr); |
| 109 | else |
| 110 | m_dispatch_queue_name.clear(); |
| 111 | |
| 112 | if (!m_dispatch_queue_name.empty()) |
| 113 | return m_dispatch_queue_name.c_str(); |
Greg Clayton | b3ae876 | 2013-04-12 20:07:46 +0000 | [diff] [blame] | 114 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | } |
| 116 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | QueueKind ThreadGDBRemote::GetQueueKind() { |
| 120 | // If our cached queue info is valid, then someone called |
| 121 | // ThreadGDBRemote::SetQueueInfo(...) |
| 122 | // with valid information that was gleaned from the stop reply packet. In this |
| 123 | // case we trust |
| 124 | // that the info is valid in m_dispatch_queue_name without refetching it |
| 125 | if (CachedQueueInfoIsValid()) { |
| 126 | return m_queue_kind; |
| 127 | } |
| 128 | |
| 129 | if (m_associated_with_libdispatch_queue == eLazyBoolNo) |
| 130 | return eQueueKindUnknown; |
| 131 | |
| 132 | if (m_thread_dispatch_qaddr != 0 && |
| 133 | m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { |
| 134 | ProcessSP process_sp(GetProcess()); |
| 135 | if (process_sp) { |
| 136 | SystemRuntime *runtime = process_sp->GetSystemRuntime(); |
| 137 | if (runtime) |
| 138 | m_queue_kind = runtime->GetQueueKind(m_thread_dispatch_qaddr); |
| 139 | return m_queue_kind; |
| 140 | } |
| 141 | } |
| 142 | return eQueueKindUnknown; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | queue_id_t ThreadGDBRemote::GetQueueID() { |
| 146 | // If our cached queue info is valid, then someone called |
| 147 | // ThreadGDBRemote::SetQueueInfo(...) |
| 148 | // with valid information that was gleaned from the stop reply packet. In this |
| 149 | // case we trust |
| 150 | // that the info is valid in m_dispatch_queue_name without refetching it |
| 151 | if (CachedQueueInfoIsValid()) |
| 152 | return m_queue_serial_number; |
| 153 | |
| 154 | if (m_associated_with_libdispatch_queue == eLazyBoolNo) |
| 155 | return LLDB_INVALID_QUEUE_ID; |
| 156 | |
| 157 | if (m_thread_dispatch_qaddr != 0 && |
| 158 | m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { |
| 159 | ProcessSP process_sp(GetProcess()); |
| 160 | if (process_sp) { |
| 161 | SystemRuntime *runtime = process_sp->GetSystemRuntime(); |
| 162 | if (runtime) { |
| 163 | return runtime->GetQueueIDFromThreadQAddress(m_thread_dispatch_qaddr); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | return LLDB_INVALID_QUEUE_ID; |
Jason Molenda | 545304d | 2015-12-18 00:45:35 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | QueueSP ThreadGDBRemote::GetQueue() { |
| 171 | queue_id_t queue_id = GetQueueID(); |
| 172 | QueueSP queue; |
| 173 | if (queue_id != LLDB_INVALID_QUEUE_ID) { |
| 174 | ProcessSP process_sp(GetProcess()); |
| 175 | if (process_sp) { |
| 176 | queue = process_sp->GetQueueList().FindQueueByID(queue_id); |
| 177 | } |
| 178 | } |
| 179 | return queue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | addr_t ThreadGDBRemote::GetQueueLibdispatchQueueAddress() { |
| 183 | if (m_dispatch_queue_t == LLDB_INVALID_ADDRESS) { |
| 184 | if (m_thread_dispatch_qaddr != 0 && |
| 185 | m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) { |
| 186 | ProcessSP process_sp(GetProcess()); |
| 187 | if (process_sp) { |
| 188 | SystemRuntime *runtime = process_sp->GetSystemRuntime(); |
| 189 | if (runtime) { |
| 190 | m_dispatch_queue_t = |
| 191 | runtime->GetLibdispatchQueueAddressFromThreadQAddress( |
| 192 | m_thread_dispatch_qaddr); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return m_dispatch_queue_t; |
| 198 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | void ThreadGDBRemote::SetQueueLibdispatchQueueAddress( |
| 201 | lldb::addr_t dispatch_queue_t) { |
| 202 | m_dispatch_queue_t = dispatch_queue_t; |
| 203 | } |
| 204 | |
| 205 | bool ThreadGDBRemote::ThreadHasQueueInformation() const { |
| 206 | if (m_thread_dispatch_qaddr != 0 && |
| 207 | m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS && |
| 208 | m_dispatch_queue_t != LLDB_INVALID_ADDRESS && |
| 209 | m_queue_kind != eQueueKindUnknown && m_queue_serial_number != 0) { |
| 210 | return true; |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | LazyBool ThreadGDBRemote::GetAssociatedWithLibdispatchQueue() { |
| 216 | return m_associated_with_libdispatch_queue; |
| 217 | } |
| 218 | |
| 219 | void ThreadGDBRemote::SetAssociatedWithLibdispatchQueue( |
| 220 | LazyBool associated_with_libdispatch_queue) { |
| 221 | m_associated_with_libdispatch_queue = associated_with_libdispatch_queue; |
| 222 | } |
| 223 | |
| 224 | StructuredData::ObjectSP ThreadGDBRemote::FetchThreadExtendedInfo() { |
| 225 | StructuredData::ObjectSP object_sp; |
| 226 | const lldb::user_id_t tid = GetProtocolID(); |
| 227 | Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); |
| 228 | if (log) |
| 229 | log->Printf("Fetching extended information for thread %4.4" PRIx64, tid); |
| 230 | ProcessSP process_sp(GetProcess()); |
| 231 | if (process_sp) { |
| 232 | ProcessGDBRemote *gdb_process = |
| 233 | static_cast<ProcessGDBRemote *>(process_sp.get()); |
| 234 | object_sp = gdb_process->GetExtendedInfoForThread(tid); |
| 235 | } |
| 236 | return object_sp; |
| 237 | } |
| 238 | |
| 239 | void ThreadGDBRemote::WillResume(StateType resume_state) { |
| 240 | int signo = GetResumeSignal(); |
| 241 | const lldb::user_id_t tid = GetProtocolID(); |
| 242 | Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_THREAD)); |
| 243 | if (log) |
| 244 | log->Printf("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, |
| 245 | StateAsCString(resume_state)); |
| 246 | |
| 247 | ProcessSP process_sp(GetProcess()); |
| 248 | if (process_sp) { |
| 249 | ProcessGDBRemote *gdb_process = |
| 250 | static_cast<ProcessGDBRemote *>(process_sp.get()); |
| 251 | switch (resume_state) { |
| 252 | case eStateSuspended: |
| 253 | case eStateStopped: |
| 254 | // Don't append anything for threads that should stay stopped. |
| 255 | break; |
| 256 | |
| 257 | case eStateRunning: |
| 258 | if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) |
| 259 | gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo)); |
| 260 | else |
| 261 | gdb_process->m_continue_c_tids.push_back(tid); |
| 262 | break; |
| 263 | |
| 264 | case eStateStepping: |
| 265 | if (gdb_process->GetUnixSignals()->SignalIsValid(signo)) |
| 266 | gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo)); |
| 267 | else |
| 268 | gdb_process->m_continue_s_tids.push_back(tid); |
| 269 | break; |
| 270 | |
| 271 | default: |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void ThreadGDBRemote::RefreshStateAfterStop() { |
| 278 | // Invalidate all registers in our register context. We don't set "force" to |
| 279 | // true because the stop reply packet might have had some register values |
| 280 | // that were expedited and these will already be copied into the register |
| 281 | // context by the time this function gets called. The GDBRemoteRegisterContext |
| 282 | // class has been made smart enough to detect when it needs to invalidate |
| 283 | // which registers are valid by putting hooks in the register read and |
| 284 | // register supply functions where they check the process stop ID and do |
| 285 | // the right thing. |
| 286 | const bool force = false; |
| 287 | GetRegisterContext()->InvalidateIfNeeded(force); |
| 288 | } |
| 289 | |
| 290 | bool ThreadGDBRemote::ThreadIDIsValid(lldb::tid_t thread) { |
| 291 | return thread != 0; |
| 292 | } |
| 293 | |
| 294 | void ThreadGDBRemote::Dump(Log *log, uint32_t index) {} |
| 295 | |
| 296 | bool ThreadGDBRemote::ShouldStop(bool &step_more) { return true; } |
| 297 | lldb::RegisterContextSP ThreadGDBRemote::GetRegisterContext() { |
| 298 | if (m_reg_context_sp.get() == NULL) |
| 299 | m_reg_context_sp = CreateRegisterContextForFrame(NULL); |
| 300 | return m_reg_context_sp; |
| 301 | } |
| 302 | |
| 303 | lldb::RegisterContextSP |
| 304 | ThreadGDBRemote::CreateRegisterContextForFrame(StackFrame *frame) { |
| 305 | lldb::RegisterContextSP reg_ctx_sp; |
| 306 | uint32_t concrete_frame_idx = 0; |
| 307 | |
| 308 | if (frame) |
| 309 | concrete_frame_idx = frame->GetConcreteFrameIndex(); |
| 310 | |
| 311 | if (concrete_frame_idx == 0) { |
| 312 | ProcessSP process_sp(GetProcess()); |
| 313 | if (process_sp) { |
| 314 | ProcessGDBRemote *gdb_process = |
| 315 | static_cast<ProcessGDBRemote *>(process_sp.get()); |
| 316 | // read_all_registers_at_once will be true if 'p' packet is not supported. |
| 317 | bool read_all_registers_at_once = |
| 318 | !gdb_process->GetGDBRemote().GetpPacketSupported(GetID()); |
| 319 | reg_ctx_sp.reset(new GDBRemoteRegisterContext( |
| 320 | *this, concrete_frame_idx, gdb_process->m_register_info, |
| 321 | read_all_registers_at_once)); |
| 322 | } |
| 323 | } else { |
| 324 | Unwind *unwinder = GetUnwinder(); |
| 325 | if (unwinder) |
| 326 | reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame); |
| 327 | } |
| 328 | return reg_ctx_sp; |
| 329 | } |
| 330 | |
| 331 | bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, |
| 332 | llvm::ArrayRef<uint8_t> data) { |
| 333 | GDBRemoteRegisterContext *gdb_reg_ctx = |
| 334 | static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); |
| 335 | assert(gdb_reg_ctx); |
| 336 | return gdb_reg_ctx->PrivateSetRegisterValue(reg, data); |
| 337 | } |
| 338 | |
| 339 | bool ThreadGDBRemote::PrivateSetRegisterValue(uint32_t reg, uint64_t regval) { |
| 340 | GDBRemoteRegisterContext *gdb_reg_ctx = |
| 341 | static_cast<GDBRemoteRegisterContext *>(GetRegisterContext().get()); |
| 342 | assert(gdb_reg_ctx); |
| 343 | return gdb_reg_ctx->PrivateSetRegisterValue(reg, regval); |
| 344 | } |
| 345 | |
| 346 | bool ThreadGDBRemote::CalculateStopInfo() { |
| 347 | ProcessSP process_sp(GetProcess()); |
| 348 | if (process_sp) |
| 349 | return static_cast<ProcessGDBRemote *>(process_sp.get()) |
| 350 | ->CalculateThreadStopInfo(this); |
| 351 | return false; |
| 352 | } |