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