ThreadMemory.cpp revision 94a5d0de4433dce556db59758f3d6124eb0e1a2a
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_thread_info_valobj_sp (thread_info_valobj_sp),
25    m_name(),
26    m_queue()
27{
28}
29
30
31ThreadMemory::ThreadMemory (Process &process,
32                            lldb::tid_t tid,
33                            const char *name,
34                            const char *queue) :
35    Thread (process, tid),
36    m_thread_info_valobj_sp (),
37    m_name(),
38    m_queue()
39{
40    if (name)
41        m_name = name;
42    if (queue)
43        m_queue = queue;
44}
45
46
47ThreadMemory::~ThreadMemory()
48{
49    DestroyThread();
50}
51
52bool
53ThreadMemory::WillResume (StateType resume_state)
54{
55    ClearStackFrames();
56    // Call the Thread::WillResume first. If we stop at a signal, the stop info
57    // class for signal will set the resume signal that we need below. The signal
58    // stuff obeys the Process::UnixSignal defaults.
59    Thread::WillResume(resume_state);
60    return true;
61}
62
63RegisterContextSP
64ThreadMemory::GetRegisterContext ()
65{
66    if (!m_reg_context_sp)
67    {
68        ProcessSP process_sp (GetProcess());
69        if (process_sp)
70        {
71            OperatingSystem *os = process_sp->GetOperatingSystem ();
72            if (os)
73                m_reg_context_sp = os->CreateRegisterContextForThread (this);
74        }
75    }
76    return m_reg_context_sp;
77}
78
79RegisterContextSP
80ThreadMemory::CreateRegisterContextForFrame (StackFrame *frame)
81{
82    RegisterContextSP reg_ctx_sp;
83    uint32_t concrete_frame_idx = 0;
84
85    if (frame)
86        concrete_frame_idx = frame->GetConcreteFrameIndex ();
87
88    if (concrete_frame_idx == 0)
89    {
90        reg_ctx_sp = GetRegisterContext ();
91    }
92    else if (m_unwinder_ap.get())
93    {
94        reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
95    }
96    return reg_ctx_sp;
97}
98
99lldb::StopInfoSP
100ThreadMemory::GetPrivateStopReason ()
101{
102    ProcessSP process_sp (GetProcess());
103
104    if (process_sp)
105    {
106        const uint32_t process_stop_id = process_sp->GetStopID();
107        if (m_thread_stop_reason_stop_id != process_stop_id ||
108            (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
109        {
110            // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
111            // for this thread, then m_actual_stop_info_sp will not ever contain
112            // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
113            // check will never be able to tell us if we have the correct stop info
114            // for this thread and we will continually send qThreadStopInfo packets
115            // down to the remote GDB server, so we need to keep our own notion
116            // of the stop ID that m_actual_stop_info_sp is valid for (even if it
117            // contains nothing). We use m_thread_stop_reason_stop_id for this below.
118            m_thread_stop_reason_stop_id = process_stop_id;
119            m_actual_stop_info_sp.reset();
120
121            OperatingSystem *os = process_sp->GetOperatingSystem ();
122            if (os)
123                m_actual_stop_info_sp = os->CreateThreadStopReason (this);
124        }
125    }
126    return m_actual_stop_info_sp;
127
128}
129
130void
131ThreadMemory::RefreshStateAfterStop()
132{
133    RegisterContextSP reg_ctx_sp(GetRegisterContext());
134    if (reg_ctx_sp)
135    {
136        const bool force = true;
137        reg_ctx_sp->InvalidateIfNeeded (force);
138    }
139}
140