ThreadGDBRemote.cpp revision 258054
1254721Semaste//===-- ThreadGDBRemote.cpp -------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste
11254721Semaste#include "ThreadGDBRemote.h"
12254721Semaste
13258054Semaste#include "lldb/Breakpoint/Watchpoint.h"
14254721Semaste#include "lldb/Core/ArchSpec.h"
15254721Semaste#include "lldb/Core/DataExtractor.h"
16258054Semaste#include "lldb/Core/State.h"
17254721Semaste#include "lldb/Core/StreamString.h"
18258054Semaste#include "lldb/Target/Platform.h"
19254721Semaste#include "lldb/Target/Process.h"
20254721Semaste#include "lldb/Target/RegisterContext.h"
21254721Semaste#include "lldb/Target/StopInfo.h"
22254721Semaste#include "lldb/Target/Target.h"
23254721Semaste#include "lldb/Target/Unwind.h"
24254721Semaste
25254721Semaste#include "ProcessGDBRemote.h"
26254721Semaste#include "ProcessGDBRemoteLog.h"
27254721Semaste#include "Utility/StringExtractorGDBRemote.h"
28254721Semaste
29254721Semasteusing namespace lldb;
30254721Semasteusing namespace lldb_private;
31254721Semaste
32254721Semaste//----------------------------------------------------------------------
33254721Semaste// Thread Registers
34254721Semaste//----------------------------------------------------------------------
35254721Semaste
36254721SemasteThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
37254721Semaste    Thread(process, tid),
38254721Semaste    m_thread_name (),
39254721Semaste    m_dispatch_queue_name (),
40254721Semaste    m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
41254721Semaste{
42254721Semaste    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
43254721Semaste                               this,
44254721Semaste                               process.GetID(),
45254721Semaste                               GetID());
46254721Semaste}
47254721Semaste
48254721SemasteThreadGDBRemote::~ThreadGDBRemote ()
49254721Semaste{
50254721Semaste    ProcessSP process_sp(GetProcess());
51254721Semaste    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
52254721Semaste                               this,
53254721Semaste                               process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
54254721Semaste                               GetID());
55254721Semaste    DestroyThread();
56254721Semaste}
57254721Semaste
58254721Semasteconst char *
59254721SemasteThreadGDBRemote::GetName ()
60254721Semaste{
61254721Semaste    if (m_thread_name.empty())
62254721Semaste        return NULL;
63254721Semaste    return m_thread_name.c_str();
64254721Semaste}
65254721Semaste
66254721Semaste
67254721Semasteconst char *
68254721SemasteThreadGDBRemote::GetQueueName ()
69254721Semaste{
70254721Semaste    // Always re-fetch the dispatch queue name since it can change
71254721Semaste
72254721Semaste    if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
73254721Semaste    {
74254721Semaste        ProcessSP process_sp (GetProcess());
75254721Semaste        if (process_sp)
76254721Semaste        {
77258054Semaste            PlatformSP platform_sp (process_sp->GetTarget().GetPlatform());
78258054Semaste            if (platform_sp)
79258054Semaste            {
80258054Semaste                m_dispatch_queue_name = platform_sp->GetQueueNameForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr);
81258054Semaste            }
82258054Semaste            if (m_dispatch_queue_name.length() > 0)
83258054Semaste            {
84258054Semaste                return m_dispatch_queue_name.c_str();
85258054Semaste            }
86254721Semaste        }
87254721Semaste    }
88254721Semaste    return NULL;
89254721Semaste}
90254721Semaste
91258054Semastequeue_id_t
92258054SemasteThreadGDBRemote::GetQueueID ()
93258054Semaste{
94258054Semaste    if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
95258054Semaste    {
96258054Semaste        ProcessSP process_sp (GetProcess());
97258054Semaste        if (process_sp)
98258054Semaste        {
99258054Semaste            PlatformSP platform_sp (process_sp->GetTarget().GetPlatform());
100258054Semaste            if (platform_sp)
101258054Semaste            {
102258054Semaste                return platform_sp->GetQueueIDForThreadQAddress (process_sp.get(), m_thread_dispatch_qaddr);
103258054Semaste            }
104258054Semaste        }
105258054Semaste    }
106258054Semaste    return LLDB_INVALID_QUEUE_ID;
107258054Semaste}
108258054Semaste
109254721Semastevoid
110254721SemasteThreadGDBRemote::WillResume (StateType resume_state)
111254721Semaste{
112254721Semaste    int signo = GetResumeSignal();
113254721Semaste    const lldb::user_id_t tid = GetProtocolID();
114254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
115254721Semaste    if (log)
116254721Semaste        log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
117254721Semaste
118254721Semaste    ProcessSP process_sp (GetProcess());
119254721Semaste    if (process_sp)
120254721Semaste    {
121254721Semaste        ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
122254721Semaste        switch (resume_state)
123254721Semaste        {
124254721Semaste        case eStateSuspended:
125254721Semaste        case eStateStopped:
126254721Semaste            // Don't append anything for threads that should stay stopped.
127254721Semaste            break;
128254721Semaste
129254721Semaste        case eStateRunning:
130254721Semaste            if (gdb_process->GetUnixSignals().SignalIsValid (signo))
131254721Semaste                gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
132254721Semaste            else
133254721Semaste                gdb_process->m_continue_c_tids.push_back(tid);
134254721Semaste            break;
135254721Semaste
136254721Semaste        case eStateStepping:
137254721Semaste            if (gdb_process->GetUnixSignals().SignalIsValid (signo))
138254721Semaste                gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
139254721Semaste            else
140254721Semaste                gdb_process->m_continue_s_tids.push_back(tid);
141254721Semaste            break;
142254721Semaste
143254721Semaste        default:
144254721Semaste            break;
145254721Semaste        }
146254721Semaste    }
147254721Semaste}
148254721Semaste
149254721Semastevoid
150254721SemasteThreadGDBRemote::RefreshStateAfterStop()
151254721Semaste{
152254721Semaste    // Invalidate all registers in our register context. We don't set "force" to
153254721Semaste    // true because the stop reply packet might have had some register values
154254721Semaste    // that were expedited and these will already be copied into the register
155254721Semaste    // context by the time this function gets called. The GDBRemoteRegisterContext
156254721Semaste    // class has been made smart enough to detect when it needs to invalidate
157254721Semaste    // which registers are valid by putting hooks in the register read and
158254721Semaste    // register supply functions where they check the process stop ID and do
159254721Semaste    // the right thing.
160254721Semaste    const bool force = false;
161254721Semaste    GetRegisterContext()->InvalidateIfNeeded (force);
162254721Semaste}
163254721Semaste
164254721Semastebool
165254721SemasteThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
166254721Semaste{
167254721Semaste    return thread != 0;
168254721Semaste}
169254721Semaste
170254721Semastevoid
171254721SemasteThreadGDBRemote::Dump(Log *log, uint32_t index)
172254721Semaste{
173254721Semaste}
174254721Semaste
175254721Semaste
176254721Semastebool
177254721SemasteThreadGDBRemote::ShouldStop (bool &step_more)
178254721Semaste{
179254721Semaste    return true;
180254721Semaste}
181254721Semastelldb::RegisterContextSP
182254721SemasteThreadGDBRemote::GetRegisterContext ()
183254721Semaste{
184254721Semaste    if (m_reg_context_sp.get() == NULL)
185254721Semaste        m_reg_context_sp = CreateRegisterContextForFrame (NULL);
186254721Semaste    return m_reg_context_sp;
187254721Semaste}
188254721Semaste
189254721Semastelldb::RegisterContextSP
190254721SemasteThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
191254721Semaste{
192254721Semaste    lldb::RegisterContextSP reg_ctx_sp;
193254721Semaste    uint32_t concrete_frame_idx = 0;
194254721Semaste
195254721Semaste    if (frame)
196254721Semaste        concrete_frame_idx = frame->GetConcreteFrameIndex ();
197254721Semaste
198254721Semaste
199254721Semaste    if (concrete_frame_idx == 0)
200254721Semaste    {
201254721Semaste        ProcessSP process_sp (GetProcess());
202254721Semaste        if (process_sp)
203254721Semaste        {
204254721Semaste            ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
205258054Semaste            // read_all_registers_at_once will be true if 'p' packet is not supported.
206258054Semaste            bool read_all_registers_at_once = !gdb_process->GetGDBRemote().GetpPacketSupported (GetID());
207254721Semaste            reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
208254721Semaste        }
209254721Semaste    }
210254721Semaste    else
211254721Semaste    {
212254721Semaste        Unwind *unwinder = GetUnwinder ();
213254721Semaste        if (unwinder)
214254721Semaste            reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
215254721Semaste    }
216254721Semaste    return reg_ctx_sp;
217254721Semaste}
218254721Semaste
219254721Semastebool
220254721SemasteThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
221254721Semaste{
222254721Semaste    GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
223254721Semaste    assert (gdb_reg_ctx);
224254721Semaste    return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
225254721Semaste}
226254721Semaste
227254721Semastebool
228254721SemasteThreadGDBRemote::CalculateStopInfo ()
229254721Semaste{
230254721Semaste    ProcessSP process_sp (GetProcess());
231254721Semaste    if (process_sp)
232254721Semaste    {
233254721Semaste        StringExtractorGDBRemote stop_packet;
234254721Semaste        ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
235254721Semaste        if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
236254721Semaste            return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
237254721Semaste    }
238254721Semaste    return false;
239254721Semaste}
240254721Semaste
241254721Semaste
242