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