ThreadGDBRemote.cpp revision 254721
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
13254721Semaste#include "lldb/Core/ArchSpec.h"
14254721Semaste#include "lldb/Core/DataExtractor.h"
15254721Semaste#include "lldb/Core/StreamString.h"
16254721Semaste#include "lldb/Core/State.h"
17254721Semaste#include "lldb/Target/Process.h"
18254721Semaste#include "lldb/Target/RegisterContext.h"
19254721Semaste#include "lldb/Target/StopInfo.h"
20254721Semaste#include "lldb/Target/Target.h"
21254721Semaste#include "lldb/Target/Unwind.h"
22254721Semaste#include "lldb/Breakpoint/Watchpoint.h"
23254721Semaste
24254721Semaste#include "ProcessGDBRemote.h"
25254721Semaste#include "ProcessGDBRemoteLog.h"
26254721Semaste#include "Utility/StringExtractorGDBRemote.h"
27254721Semaste
28254721Semasteusing namespace lldb;
29254721Semasteusing namespace lldb_private;
30254721Semaste
31254721Semaste//----------------------------------------------------------------------
32254721Semaste// Thread Registers
33254721Semaste//----------------------------------------------------------------------
34254721Semaste
35254721SemasteThreadGDBRemote::ThreadGDBRemote (Process &process, lldb::tid_t tid) :
36254721Semaste    Thread(process, tid),
37254721Semaste    m_thread_name (),
38254721Semaste    m_dispatch_queue_name (),
39254721Semaste    m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
40254721Semaste{
41254721Semaste    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
42254721Semaste                               this,
43254721Semaste                               process.GetID(),
44254721Semaste                               GetID());
45254721Semaste}
46254721Semaste
47254721SemasteThreadGDBRemote::~ThreadGDBRemote ()
48254721Semaste{
49254721Semaste    ProcessSP process_sp(GetProcess());
50254721Semaste    ProcessGDBRemoteLog::LogIf(GDBR_LOG_THREAD, "%p: ThreadGDBRemote::~ThreadGDBRemote (pid = %i, tid = 0x%4.4x)",
51254721Semaste                               this,
52254721Semaste                               process_sp ? process_sp->GetID() : LLDB_INVALID_PROCESS_ID,
53254721Semaste                               GetID());
54254721Semaste    DestroyThread();
55254721Semaste}
56254721Semaste
57254721Semasteconst char *
58254721SemasteThreadGDBRemote::GetName ()
59254721Semaste{
60254721Semaste    if (m_thread_name.empty())
61254721Semaste        return NULL;
62254721Semaste    return m_thread_name.c_str();
63254721Semaste}
64254721Semaste
65254721Semaste
66254721Semasteconst char *
67254721SemasteThreadGDBRemote::GetQueueName ()
68254721Semaste{
69254721Semaste    // Always re-fetch the dispatch queue name since it can change
70254721Semaste
71254721Semaste    if (m_thread_dispatch_qaddr != 0 || m_thread_dispatch_qaddr != LLDB_INVALID_ADDRESS)
72254721Semaste    {
73254721Semaste        ProcessSP process_sp (GetProcess());
74254721Semaste        if (process_sp)
75254721Semaste        {
76254721Semaste            ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
77254721Semaste            return gdb_process->GetDispatchQueueNameForThread (m_thread_dispatch_qaddr, m_dispatch_queue_name);
78254721Semaste        }
79254721Semaste    }
80254721Semaste    return NULL;
81254721Semaste}
82254721Semaste
83254721Semastevoid
84254721SemasteThreadGDBRemote::WillResume (StateType resume_state)
85254721Semaste{
86254721Semaste    int signo = GetResumeSignal();
87254721Semaste    const lldb::user_id_t tid = GetProtocolID();
88254721Semaste    Log *log(lldb_private::GetLogIfAnyCategoriesSet (GDBR_LOG_THREAD));
89254721Semaste    if (log)
90254721Semaste        log->Printf ("Resuming thread: %4.4" PRIx64 " with state: %s.", tid, StateAsCString(resume_state));
91254721Semaste
92254721Semaste    ProcessSP process_sp (GetProcess());
93254721Semaste    if (process_sp)
94254721Semaste    {
95254721Semaste        ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
96254721Semaste        switch (resume_state)
97254721Semaste        {
98254721Semaste        case eStateSuspended:
99254721Semaste        case eStateStopped:
100254721Semaste            // Don't append anything for threads that should stay stopped.
101254721Semaste            break;
102254721Semaste
103254721Semaste        case eStateRunning:
104254721Semaste            if (gdb_process->GetUnixSignals().SignalIsValid (signo))
105254721Semaste                gdb_process->m_continue_C_tids.push_back(std::make_pair(tid, signo));
106254721Semaste            else
107254721Semaste                gdb_process->m_continue_c_tids.push_back(tid);
108254721Semaste            break;
109254721Semaste
110254721Semaste        case eStateStepping:
111254721Semaste            if (gdb_process->GetUnixSignals().SignalIsValid (signo))
112254721Semaste                gdb_process->m_continue_S_tids.push_back(std::make_pair(tid, signo));
113254721Semaste            else
114254721Semaste                gdb_process->m_continue_s_tids.push_back(tid);
115254721Semaste            break;
116254721Semaste
117254721Semaste        default:
118254721Semaste            break;
119254721Semaste        }
120254721Semaste    }
121254721Semaste}
122254721Semaste
123254721Semastevoid
124254721SemasteThreadGDBRemote::RefreshStateAfterStop()
125254721Semaste{
126254721Semaste    // Invalidate all registers in our register context. We don't set "force" to
127254721Semaste    // true because the stop reply packet might have had some register values
128254721Semaste    // that were expedited and these will already be copied into the register
129254721Semaste    // context by the time this function gets called. The GDBRemoteRegisterContext
130254721Semaste    // class has been made smart enough to detect when it needs to invalidate
131254721Semaste    // which registers are valid by putting hooks in the register read and
132254721Semaste    // register supply functions where they check the process stop ID and do
133254721Semaste    // the right thing.
134254721Semaste    const bool force = false;
135254721Semaste    GetRegisterContext()->InvalidateIfNeeded (force);
136254721Semaste}
137254721Semaste
138254721Semastebool
139254721SemasteThreadGDBRemote::ThreadIDIsValid (lldb::tid_t thread)
140254721Semaste{
141254721Semaste    return thread != 0;
142254721Semaste}
143254721Semaste
144254721Semastevoid
145254721SemasteThreadGDBRemote::Dump(Log *log, uint32_t index)
146254721Semaste{
147254721Semaste}
148254721Semaste
149254721Semaste
150254721Semastebool
151254721SemasteThreadGDBRemote::ShouldStop (bool &step_more)
152254721Semaste{
153254721Semaste    return true;
154254721Semaste}
155254721Semastelldb::RegisterContextSP
156254721SemasteThreadGDBRemote::GetRegisterContext ()
157254721Semaste{
158254721Semaste    if (m_reg_context_sp.get() == NULL)
159254721Semaste        m_reg_context_sp = CreateRegisterContextForFrame (NULL);
160254721Semaste    return m_reg_context_sp;
161254721Semaste}
162254721Semaste
163254721Semastelldb::RegisterContextSP
164254721SemasteThreadGDBRemote::CreateRegisterContextForFrame (StackFrame *frame)
165254721Semaste{
166254721Semaste    lldb::RegisterContextSP reg_ctx_sp;
167254721Semaste    const bool read_all_registers_at_once = false;
168254721Semaste    uint32_t concrete_frame_idx = 0;
169254721Semaste
170254721Semaste    if (frame)
171254721Semaste        concrete_frame_idx = frame->GetConcreteFrameIndex ();
172254721Semaste
173254721Semaste
174254721Semaste    if (concrete_frame_idx == 0)
175254721Semaste    {
176254721Semaste        ProcessSP process_sp (GetProcess());
177254721Semaste        if (process_sp)
178254721Semaste        {
179254721Semaste            ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
180254721Semaste            reg_ctx_sp.reset (new GDBRemoteRegisterContext (*this, concrete_frame_idx, gdb_process->m_register_info, read_all_registers_at_once));
181254721Semaste        }
182254721Semaste    }
183254721Semaste    else
184254721Semaste    {
185254721Semaste        Unwind *unwinder = GetUnwinder ();
186254721Semaste        if (unwinder)
187254721Semaste            reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
188254721Semaste    }
189254721Semaste    return reg_ctx_sp;
190254721Semaste}
191254721Semaste
192254721Semastebool
193254721SemasteThreadGDBRemote::PrivateSetRegisterValue (uint32_t reg, StringExtractor &response)
194254721Semaste{
195254721Semaste    GDBRemoteRegisterContext *gdb_reg_ctx = static_cast<GDBRemoteRegisterContext *>(GetRegisterContext ().get());
196254721Semaste    assert (gdb_reg_ctx);
197254721Semaste    return gdb_reg_ctx->PrivateSetRegisterValue (reg, response);
198254721Semaste}
199254721Semaste
200254721Semastebool
201254721SemasteThreadGDBRemote::CalculateStopInfo ()
202254721Semaste{
203254721Semaste    ProcessSP process_sp (GetProcess());
204254721Semaste    if (process_sp)
205254721Semaste    {
206254721Semaste        StringExtractorGDBRemote stop_packet;
207254721Semaste        ProcessGDBRemote *gdb_process = static_cast<ProcessGDBRemote *>(process_sp.get());
208254721Semaste        if (gdb_process->GetGDBRemote().GetThreadStopInfo(GetProtocolID(), stop_packet))
209254721Semaste            return gdb_process->SetThreadStopInfo (stop_packet) == eStateStopped;
210254721Semaste    }
211254721Semaste    return false;
212254721Semaste}
213254721Semaste
214254721Semaste
215