1 //===-- MachThread.h --------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Created by Greg Clayton on 6/19/07.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTHREAD_H
14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_MACHTHREAD_H
20 #include <mach/mach.h>
22 #include <sys/signal.h>
25 #include "DNBRegisterInfo.h"
26 #include "MachException.h"
27 #include "PThreadCondition.h"
28 #include "PThreadMutex.h"
30 #include "ThreadInfo.h"
38 MachThread(MachProcess
*process
, bool is_64_bit
,
39 uint64_t unique_thread_id
= 0, thread_t mach_port_number
= 0);
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;
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
55 // debugserver namespace
56 void Resume(bool others_stopped
);
58 bool SetSuspendCountBeforeResume(bool others_stopped
);
59 bool RestoreSuspendCountAfterStop();
61 bool GetRegisterState(int flavor
, bool force
);
62 bool SetRegisterState(int flavor
);
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
68 DNBBreakpoint
*CurrentBreakpoint();
69 uint32_t EnableHardwareBreakpoint(const DNBBreakpoint
*breakpoint
,
70 bool also_set_on_task
);
71 uint32_t EnableHardwareWatchpoint(const DNBBreakpoint
*watchpoint
,
72 bool also_set_on_task
);
73 bool DisableHardwareBreakpoint(const DNBBreakpoint
*breakpoint
,
74 bool also_set_on_task
);
75 bool DisableHardwareWatchpoint(const DNBBreakpoint
*watchpoint
,
76 bool also_set_on_task
);
77 uint32_t NumSupportedHardwareWatchpoints() const;
78 bool RollbackTransForHWP();
79 bool FinishTransForHWP();
81 nub_state_t
GetState();
82 void SetState(nub_state_t state
);
84 void ThreadWillResume(const DNBThreadResumeAction
*thread_action
,
85 bool others_stopped
= false);
86 bool ShouldStop(bool &step_more
);
89 bool NotifyException(MachException::Data
&exc
);
90 const MachException::Data
&GetStopException() { return m_stop_exception
; }
92 nub_size_t
GetNumRegistersInSet(nub_size_t regSet
) const;
93 const char *GetRegisterSetName(nub_size_t regSet
) const;
94 const DNBRegisterInfo
*GetRegisterInfo(nub_size_t regSet
,
95 nub_size_t regIndex
) const;
96 void DumpRegisterState(nub_size_t regSet
);
97 const DNBRegisterSetInfo
*GetRegisterSetInfo(nub_size_t
*num_reg_sets
) const;
98 bool GetRegisterValue(uint32_t reg_set_idx
, uint32_t reg_idx
,
99 DNBRegisterValue
*reg_value
);
100 bool SetRegisterValue(uint32_t reg_set_idx
, uint32_t reg_idx
,
101 const DNBRegisterValue
*reg_value
);
102 nub_size_t
GetRegisterContext(void *buf
, nub_size_t buf_len
);
103 nub_size_t
SetRegisterContext(const void *buf
, nub_size_t buf_len
);
104 uint32_t SaveRegisterState();
105 bool RestoreRegisterState(uint32_t save_id
);
107 void NotifyBreakpointChanged(const DNBBreakpoint
*bp
) {}
110 struct thread_basic_info
*GetBasicInfo();
111 const char *GetBasicInfoAsString() const;
112 const char *GetName();
114 DNBArchProtocol
*GetArchProtocol() { return m_arch_up
.get(); }
116 ThreadInfo::QoS
GetRequestedQoS(nub_addr_t tsd
, uint64_t dti_qos_class_index
);
117 nub_addr_t
GetPThreadT();
118 nub_addr_t
GetDispatchQueueT();
120 GetTSDAddressForThread(uint64_t plo_pthread_tsd_base_address_offset
,
121 uint64_t plo_pthread_tsd_base_offset
,
122 uint64_t plo_pthread_tsd_entry_size
);
124 static uint64_t GetGloballyUniqueThreadIDForMachPortID(thread_t mach_port_id
);
127 static bool GetBasicInfo(thread_t threadID
,
128 struct thread_basic_info
*basic_info
);
130 bool GetIdentifierInfo();
133 // GetDispatchQueueName();
135 MachProcess
*m_process
; // The process that owns this thread
136 uint64_t m_unique_id
; // The globally unique ID for this thread (nub_thread_t)
137 thread_t m_mach_port_number
; // The mach port # for this thread in debugserver
139 uint32_t m_seq_id
; // A Sequential ID that increments with each new thread
140 nub_state_t m_state
; // The state of our process
141 PThreadMutex m_state_mutex
; // Multithreaded protection for m_state
142 struct thread_basic_info m_basic_info
; // Basic information for a thread used
143 // to see if a thread is valid
144 int32_t m_suspend_count
; // The current suspend count > 0 means we have
145 // suspended m_suspendCount times,
146 // < 0 means we have resumed it m_suspendCount
148 MachException::Data m_stop_exception
; // The best exception that describes why
149 // this thread is stopped
150 std::unique_ptr
<DNBArchProtocol
>
151 m_arch_up
; // Arch specific information for register state and more
152 const DNBRegisterSetInfo
153 *m_reg_sets
; // Register set information for this thread
154 nub_size_t m_num_reg_sets
;
155 thread_identifier_info_data_t m_ident_info
;
156 struct proc_threadinfo m_proc_threadinfo
;
157 std::string m_dispatch_queue_name
;
160 // qos_class_t _pthread_qos_class_decode(pthread_priority_t priority, int *,
162 unsigned int (*m_pthread_qos_class_decode
)(unsigned long priority
, int *,
166 friend class MachThreadList
;
169 typedef std::shared_ptr
<MachThread
> MachThreadSP
;