1 //===-- ThreadMemory.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_THREADMEMORY_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_THREADMEMORY_H
11 
12 #include <string>
13 
14 #include "lldb/Target/Thread.h"
15 
16 class ThreadMemory : public lldb_private::Thread {
17 public:
18   ThreadMemory(lldb_private::Process &process, lldb::tid_t tid,
19                const lldb::ValueObjectSP &thread_info_valobj_sp);
20 
21   ThreadMemory(lldb_private::Process &process, lldb::tid_t tid,
22                llvm::StringRef name, llvm::StringRef queue,
23                lldb::addr_t register_data_addr);
24 
25   ~ThreadMemory() override;
26 
27   lldb::RegisterContextSP GetRegisterContext() override;
28 
29   lldb::RegisterContextSP
30   CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override;
31 
32   bool CalculateStopInfo() override;
33 
34   const char *GetInfo() override {
35     if (m_backing_thread_sp)
36       m_backing_thread_sp->GetInfo();
37     return nullptr;
38   }
39 
40   const char *GetName() override {
41     if (!m_name.empty())
42       return m_name.c_str();
43     if (m_backing_thread_sp)
44       m_backing_thread_sp->GetName();
45     return nullptr;
46   }
47 
48   const char *GetQueueName() override {
49     if (!m_queue.empty())
50       return m_queue.c_str();
51     if (m_backing_thread_sp)
52       m_backing_thread_sp->GetQueueName();
53     return nullptr;
54   }
55 
56   void WillResume(lldb::StateType resume_state) override;
57 
58   void DidResume() override {
59     if (m_backing_thread_sp)
60       m_backing_thread_sp->DidResume();
61   }
62 
63   lldb::user_id_t GetProtocolID() const override {
64     if (m_backing_thread_sp)
65       return m_backing_thread_sp->GetProtocolID();
66     return Thread::GetProtocolID();
67   }
68 
69   void RefreshStateAfterStop() override;
70 
71   lldb::ValueObjectSP &GetValueObject() { return m_thread_info_valobj_sp; }
72 
73   void ClearStackFrames() override;
74 
75   void ClearBackingThread() override { m_backing_thread_sp.reset(); }
76 
77   bool SetBackingThread(const lldb::ThreadSP &thread_sp) override {
78     // printf ("Thread 0x%llx is being backed by thread 0x%llx\n", GetID(),
79     // thread_sp->GetID());
80     m_backing_thread_sp = thread_sp;
81     return (bool)thread_sp;
82   }
83 
84   lldb::ThreadSP GetBackingThread() const override {
85     return m_backing_thread_sp;
86   }
87 
88 protected:
89   bool IsOperatingSystemPluginThread() const override { return true; }
90 
91   // If this memory thread is actually represented by a thread from the
92   // lldb_private::Process subclass, then fill in the thread here and
93   // all APIs will be routed through this thread object. If m_backing_thread_sp
94   // is empty, then this thread is simply in memory with no representation
95   // through the process plug-in.
96   lldb::ThreadSP m_backing_thread_sp;
97   lldb::ValueObjectSP m_thread_info_valobj_sp;
98   std::string m_name;
99   std::string m_queue;
100   lldb::addr_t m_register_data_addr;
101 
102 private:
103   ThreadMemory(const ThreadMemory &) = delete;
104   const ThreadMemory &operator=(const ThreadMemory &) = delete;
105 };
106 
107 #endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_THREADMEMORY_H
108