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 (const ProcessSP &process_sp,
21                               tid_t tid,
22                               const ValueObjectSP &thread_info_valobj_sp) :
23     Thread (process_sp, 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         ProcessSP process_sp (GetProcess());
51         if (process_sp)
52         {
53             OperatingSystem *os = process_sp->GetOperatingSystem ();
54             if (os)
55                 m_reg_context_sp = os->CreateRegisterContextForThread (this);
56         }
57     }
58     return m_reg_context_sp;
59 }
60 
61 RegisterContextSP
62 ThreadMemory::CreateRegisterContextForFrame (StackFrame *frame)
63 {
64     RegisterContextSP reg_ctx_sp;
65     uint32_t concrete_frame_idx = 0;
66 
67     if (frame)
68         concrete_frame_idx = frame->GetConcreteFrameIndex ();
69 
70     if (concrete_frame_idx == 0)
71     {
72         reg_ctx_sp = GetRegisterContext ();
73     }
74     else if (m_unwinder_ap.get())
75     {
76         reg_ctx_sp = m_unwinder_ap->CreateRegisterContextForFrame (frame);
77     }
78     return reg_ctx_sp;
79 }
80 
81 lldb::StopInfoSP
82 ThreadMemory::GetPrivateStopReason ()
83 {
84     ProcessSP process_sp (GetProcess());
85 
86     if (process_sp)
87     {
88         const uint32_t process_stop_id = process_sp->GetStopID();
89         if (m_thread_stop_reason_stop_id != process_stop_id ||
90             (m_actual_stop_info_sp && !m_actual_stop_info_sp->IsValid()))
91         {
92             // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
93             // for this thread, then m_actual_stop_info_sp will not ever contain
94             // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
95             // check will never be able to tell us if we have the correct stop info
96             // for this thread and we will continually send qThreadStopInfo packets
97             // down to the remote GDB server, so we need to keep our own notion
98             // of the stop ID that m_actual_stop_info_sp is valid for (even if it
99             // contains nothing). We use m_thread_stop_reason_stop_id for this below.
100             m_thread_stop_reason_stop_id = process_stop_id;
101             m_actual_stop_info_sp.reset();
102 
103             OperatingSystem *os = process_sp->GetOperatingSystem ();
104             if (os)
105                 m_actual_stop_info_sp = os->CreateThreadStopReason (this);
106         }
107     }
108     return m_actual_stop_info_sp;
109 
110 }
111 
112 void
113 ThreadMemory::RefreshStateAfterStop()
114 {
115     RegisterContextSP reg_ctx_sp(GetRegisterContext());
116     if (reg_ctx_sp)
117     {
118         const bool force = true;
119         reg_ctx_sp->InvalidateIfNeeded (force);
120     }
121 }
122