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