xref: /openbsd-src/gnu/llvm/lldb/tools/debugserver/source/MacOSX/MachThread.h (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 //===-- MachThread.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 //  Created by Greg Clayton on 6/19/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef __MachThread_h__
14 #define __MachThread_h__
15 
16 #include <string>
17 #include <vector>
18 
19 #include <libproc.h>
20 #include <mach/mach.h>
21 #include <pthread.h>
22 #include <sys/signal.h>
23 
24 #include "DNBArch.h"
25 #include "DNBRegisterInfo.h"
26 #include "MachException.h"
27 #include "PThreadCondition.h"
28 #include "PThreadMutex.h"
29 
30 #include "ThreadInfo.h"
31 
32 class DNBBreakpoint;
33 class MachProcess;
34 class MachThreadList;
35 
36 class MachThread {
37 public:
38   MachThread(MachProcess *process, bool is_64_bit,
39              uint64_t unique_thread_id = 0, thread_t mach_port_number = 0);
40   ~MachThread();
41 
42   MachProcess *Process() { return m_process; }
43   const MachProcess *Process() const { return m_process; }
44   nub_process_t ProcessID() const;
45   void Dump(uint32_t index);
46   uint64_t ThreadID() const { return m_unique_id; }
47   thread_t MachPortNumber() const { return m_mach_port_number; }
48   thread_t InferiorThreadID() const;
49 
50   uint32_t SequenceID() const { return m_seq_id; }
51   static bool ThreadIDIsValid(
52       uint64_t thread); // The 64-bit system-wide unique thread identifier
53   static bool MachPortNumberIsValid(thread_t thread); // The mach port # for
54                                                       // this thread in
55                                                       // debugserver namespace
56   void Resume(bool others_stopped);
57   void Suspend();
58   bool SetSuspendCountBeforeResume(bool others_stopped);
59   bool RestoreSuspendCountAfterStop();
60 
61   bool GetRegisterState(int flavor, bool force);
62   bool SetRegisterState(int flavor);
63   uint64_t
64   GetPC(uint64_t failValue = INVALID_NUB_ADDRESS); // Get program counter
65   bool SetPC(uint64_t value);                      // Set program counter
66   uint64_t GetSP(uint64_t failValue = INVALID_NUB_ADDRESS); // Get stack pointer
67 
68   DNBBreakpoint *CurrentBreakpoint();
69   uint32_t EnableHardwareBreakpoint(const DNBBreakpoint *breakpoint);
70   uint32_t EnableHardwareWatchpoint(const DNBBreakpoint *watchpoint,
71                                     bool also_set_on_task);
72   bool DisableHardwareBreakpoint(const DNBBreakpoint *breakpoint);
73   bool DisableHardwareWatchpoint(const DNBBreakpoint *watchpoint,
74                                  bool also_set_on_task);
75   uint32_t NumSupportedHardwareWatchpoints() const;
76   bool RollbackTransForHWP();
77   bool FinishTransForHWP();
78 
79   nub_state_t GetState();
80   void SetState(nub_state_t state);
81 
82   void ThreadWillResume(const DNBThreadResumeAction *thread_action,
83                         bool others_stopped = false);
84   bool ShouldStop(bool &step_more);
85   bool IsStepping();
86   bool ThreadDidStop();
87   bool NotifyException(MachException::Data &exc);
88   const MachException::Data &GetStopException() { return m_stop_exception; }
89 
90   nub_size_t GetNumRegistersInSet(nub_size_t regSet) const;
91   const char *GetRegisterSetName(nub_size_t regSet) const;
92   const DNBRegisterInfo *GetRegisterInfo(nub_size_t regSet,
93                                          nub_size_t regIndex) const;
94   void DumpRegisterState(nub_size_t regSet);
95   const DNBRegisterSetInfo *GetRegisterSetInfo(nub_size_t *num_reg_sets) const;
96   bool GetRegisterValue(uint32_t reg_set_idx, uint32_t reg_idx,
97                         DNBRegisterValue *reg_value);
98   bool SetRegisterValue(uint32_t reg_set_idx, uint32_t reg_idx,
99                         const DNBRegisterValue *reg_value);
100   nub_size_t GetRegisterContext(void *buf, nub_size_t buf_len);
101   nub_size_t SetRegisterContext(const void *buf, nub_size_t buf_len);
102   uint32_t SaveRegisterState();
103   bool RestoreRegisterState(uint32_t save_id);
104 
105   void NotifyBreakpointChanged(const DNBBreakpoint *bp) {}
106 
107   bool IsUserReady();
108   struct thread_basic_info *GetBasicInfo();
109   const char *GetBasicInfoAsString() const;
110   const char *GetName();
111 
112   DNBArchProtocol *GetArchProtocol() { return m_arch_up.get(); }
113 
114   ThreadInfo::QoS GetRequestedQoS(nub_addr_t tsd, uint64_t dti_qos_class_index);
115   nub_addr_t GetPThreadT();
116   nub_addr_t GetDispatchQueueT();
117   nub_addr_t
118   GetTSDAddressForThread(uint64_t plo_pthread_tsd_base_address_offset,
119                          uint64_t plo_pthread_tsd_base_offset,
120                          uint64_t plo_pthread_tsd_entry_size);
121 
122   static uint64_t GetGloballyUniqueThreadIDForMachPortID(thread_t mach_port_id);
123 
124 protected:
125   static bool GetBasicInfo(thread_t threadID,
126                            struct thread_basic_info *basic_info);
127 
128   bool GetIdentifierInfo();
129 
130   //    const char *
131   //    GetDispatchQueueName();
132   //
133   MachProcess *m_process; // The process that owns this thread
134   uint64_t m_unique_id; // The globally unique ID for this thread (nub_thread_t)
135   thread_t m_mach_port_number; // The mach port # for this thread in debugserver
136                                // namesp.
137   uint32_t m_seq_id;   // A Sequential ID that increments with each new thread
138   nub_state_t m_state; // The state of our process
139   PThreadMutex m_state_mutex;            // Multithreaded protection for m_state
140   struct thread_basic_info m_basic_info; // Basic information for a thread used
141                                          // to see if a thread is valid
142   int32_t m_suspend_count; // The current suspend count > 0 means we have
143                            // suspended m_suspendCount times,
144   //                           < 0 means we have resumed it m_suspendCount
145   //                           times.
146   MachException::Data m_stop_exception; // The best exception that describes why
147                                         // this thread is stopped
148   std::unique_ptr<DNBArchProtocol>
149       m_arch_up; // Arch specific information for register state and more
150   const DNBRegisterSetInfo
151       *m_reg_sets; // Register set information for this thread
152   nub_size_t m_num_reg_sets;
153   thread_identifier_info_data_t m_ident_info;
154   struct proc_threadinfo m_proc_threadinfo;
155   std::string m_dispatch_queue_name;
156   bool m_is_64_bit;
157 
158   // qos_class_t _pthread_qos_class_decode(pthread_priority_t priority, int *,
159   // unsigned long *);
160   unsigned int (*m_pthread_qos_class_decode)(unsigned long priority, int *,
161                                              unsigned long *);
162 
163 private:
164   friend class MachThreadList;
165 };
166 
167 typedef std::shared_ptr<MachThread> MachThreadSP;
168 
169 #endif
170