xref: /llvm-project/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp (revision 5d88a068eed8625b9668db9f6b254ae5d2a0e187)
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     m_name(),
26     m_queue()
27 {
28 }
29 
30 
31 ThreadMemory::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 
47 ThreadMemory::~ThreadMemory()
48 {
49     DestroyThread();
50 }
51 
52 bool
53 ThreadMemory::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 
63 RegisterContextSP
64 ThreadMemory::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 
79 RegisterContextSP
80 ThreadMemory::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 
99 lldb::StopInfoSP
100 ThreadMemory::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 (IsStillAtLastBreakpointHit())
111                 return m_actual_stop_info_sp;
112 
113             // If GetGDBProcess().SetThreadStopInfo() doesn't find a stop reason
114             // for this thread, then m_actual_stop_info_sp will not ever contain
115             // a valid stop reason and the "m_actual_stop_info_sp->IsValid() == false"
116             // check will never be able to tell us if we have the correct stop info
117             // for this thread and we will continually send qThreadStopInfo packets
118             // down to the remote GDB server, so we need to keep our own notion
119             // of the stop ID that m_actual_stop_info_sp is valid for (even if it
120             // contains nothing). We use m_thread_stop_reason_stop_id for this below.
121             m_thread_stop_reason_stop_id = process_stop_id;
122             m_actual_stop_info_sp.reset();
123 
124             OperatingSystem *os = process_sp->GetOperatingSystem ();
125             if (os)
126                 m_actual_stop_info_sp = os->CreateThreadStopReason (this);
127         }
128     }
129     return m_actual_stop_info_sp;
130 
131 }
132 
133 void
134 ThreadMemory::RefreshStateAfterStop()
135 {
136     RegisterContextSP reg_ctx_sp(GetRegisterContext());
137     if (reg_ctx_sp)
138     {
139         const bool force = true;
140         reg_ctx_sp->InvalidateIfNeeded (force);
141     }
142 }
143