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