ThreadMemory.cpp revision 9acf3699d2bea583b45c762f4cd82b2a4af6131b
1//===-- ThreadMemory.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#include "Plugins/Process/Utility/ThreadMemory.h"
11#include "lldb/Target/OperatingSystem.h"
12#include "lldb/Target/RegisterContext.h"
13#include "lldb/Target/Process.h"
14#include "lldb/Target/StopInfo.h"
15#include "lldb/Target/Unwind.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20ThreadMemory::ThreadMemory (Process &process,
21                            tid_t tid,
22                            const ValueObjectSP &thread_info_valobj_sp) :
23    Thread (process, tid),
24    m_backing_thread_sp (),
25    m_thread_info_valobj_sp (thread_info_valobj_sp),
26    m_name(),
27    m_queue()
28{
29}
30
31
32ThreadMemory::ThreadMemory (Process &process,
33                            lldb::tid_t tid,
34                            const char *name,
35                            const char *queue,
36                            lldb::addr_t register_data_addr) :
37    Thread (process, tid),
38    m_backing_thread_sp (),
39    m_thread_info_valobj_sp (),
40    m_name(),
41    m_queue(),
42    m_register_data_addr (register_data_addr)
43{
44    if (name)
45        m_name = name;
46    if (queue)
47        m_queue = queue;
48}
49
50
51ThreadMemory::~ThreadMemory()
52{
53    DestroyThread();
54}
55
56bool
57ThreadMemory::WillResume (StateType resume_state)
58{
59    ClearStackFrames();
60    // Call the Thread::WillResume first. If we stop at a signal, the stop info
61    // class for signal will set the resume signal that we need below. The signal
62    // stuff obeys the Process::UnixSignal defaults.
63    Thread::WillResume(resume_state);
64    return true;
65}
66
67RegisterContextSP
68ThreadMemory::GetRegisterContext ()
69{
70    if (m_backing_thread_sp)
71        return m_backing_thread_sp->GetRegisterContext();
72
73    if (!m_reg_context_sp)
74    {
75        ProcessSP process_sp (GetProcess());
76        if (process_sp)
77        {
78            OperatingSystem *os = process_sp->GetOperatingSystem ();
79            if (os)
80                m_reg_context_sp = os->CreateRegisterContextForThread (this, m_register_data_addr);
81        }
82    }
83    return m_reg_context_sp;
84}
85
86RegisterContextSP
87ThreadMemory::CreateRegisterContextForFrame (StackFrame *frame)
88{
89    if (m_backing_thread_sp)
90        return m_backing_thread_sp->CreateRegisterContextForFrame(frame);
91
92    RegisterContextSP reg_ctx_sp;
93    uint32_t concrete_frame_idx = 0;
94
95    if (frame)
96        concrete_frame_idx = frame->GetConcreteFrameIndex ();
97
98    if (concrete_frame_idx == 0)
99    {
100        reg_ctx_sp = GetRegisterContext ();
101    }
102    else
103    {
104        Unwind *unwinder = GetUnwinder ();
105        if (unwinder)
106            reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
107    }
108    return reg_ctx_sp;
109}
110
111lldb::StopInfoSP
112ThreadMemory::GetPrivateStopReason ()
113{
114    if (m_backing_thread_sp)
115        return m_backing_thread_sp->GetPrivateStopReason();
116
117    ProcessSP process_sp (GetProcess());
118
119    if (process_sp)
120    {
121        const uint32_t process_stop_id = process_sp->GetStopID();
122        if (m_thread_stop_reason_stop_id != process_stop_id ||
123            (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
124        {
125            if (IsStillAtLastBreakpointHit())
126                return m_actual_stop_info_sp;
127
128            // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
129            // for this thread, then m_actual_stop_info_sp will not ever contain
130            // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
131            // check will never be able to tell us if we have the correct stop info
132            // for this thread and we will continually send qThreadStopInfo packets
133            // down to the remote GDB server, so we need to keep our own notion
134            // of the stop ID that m_actual_stop_info_sp is valid for (even if it
135            // contains nothing). We use m_thread_stop_reason_stop_id for this below.
136            m_thread_stop_reason_stop_id = process_stop_id;
137            m_actual_stop_info_sp.reset();
138
139            OperatingSystem *os = process_sp->GetOperatingSystem ();
140            if (os)
141                m_actual_stop_info_sp = os->CreateThreadStopReason (this);
142        }
143    }
144    return m_actual_stop_info_sp;
145
146}
147
148void
149ThreadMemory::RefreshStateAfterStop()
150{
151    if (m_backing_thread_sp)
152        return m_backing_thread_sp->RefreshStateAfterStop();
153
154
155    // Don't fetch the registers by calling Thread::GetRegisterContext() below.
156    // We might not have fetched any registers yet and we don't want to fetch
157    // the registers just to call invalidate on them...
158    RegisterContextSP reg_ctx_sp(m_reg_context_sp);
159    if (reg_ctx_sp)
160    {
161        const bool force = true;
162        reg_ctx_sp->InvalidateIfNeeded (force);
163    }
164}
165