Rizin
unix-like reverse engineering framework and cli tools
xnu_threads.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015 Álvaro Felipe Melchor <alvaro.felipe91@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #ifndef _INCLUDE_XNU_THREADS_H_
5 #define _INCLUDE_XNU_THREADS_H_
6 
7 #include <rz_debug.h>
8 #include <mach/mach_vm.h>
9 
10 #if __POWERPC__
11 // TODO add better support for PPC
12 #define RZ_REG_T ppc_thread_state_t
13 #define RZ_REG_STATE_T PPC_THREAD_STATE
14 #define RZ_REG_STATE_SZ PPC_THREAD_STATE_SZ
15 
16 #elif __arm || __arm64 || __aarch64
17 #include <mach/arm/thread_status.h>
18 #ifndef ARM_THREAD_STATE
19 #define ARM_THREAD_STATE 1
20 #endif
21 #ifndef ARM_THREAD_STATE64
22 #define ARM_THREAD_STATE64 6
23 #endif
24 #define RZ_REG_T arm_unified_thread_state_t
25 #define RZ_REG_STATE_T MACHINE_THREAD_STATE
26 #define RZ_REG_STATE_SZ MACHINE_THREAD_STATE_COUNT
27 #elif __x86_64__ || __i386__
28 #define RZ_REG_T x86_thread_state_t
29 #define RZ_REG_STATE_T MACHINE_THREAD_STATE
30 #define RZ_REG_STATE_SZ MACHINE_THREAD_STATE_COUNT
31 #endif
32 
33 #define RETURN_ON_MACH_ERROR(msg, retval) \
34  if (kr != KERN_SUCCESS) { \
35  mach_error(msg, kr); \
36  return ((retval)); \
37  }
38 
39 typedef struct _exception_info {
40  exception_mask_t masks[EXC_TYPES_COUNT];
41  mach_port_t ports[EXC_TYPES_COUNT];
42  exception_behavior_t behaviors[EXC_TYPES_COUNT];
43  thread_state_flavor_t flavors[EXC_TYPES_COUNT];
44  mach_msg_type_number_t count;
45  pthread_t thread;
46  mach_port_t exception_port;
48 
49 // XXX use rizin types
50 typedef struct _xnu_thread {
51  thread_t port; // mach_port // XXX bad naming here
52  char *name; // name of thread
53  thread_basic_info_data_t basic_info; // need this?
54  ut8 stepping; // thread is stepping or not //TODO implement stepping
55  RZ_REG_T gpr; // type RZ_REG_T using unified API XXX bad naming
56  void *state;
58 #if __arm64 || __aarch64 || __arm64__ || __aarch64__
59  union {
60  arm_debug_state32_t drx32;
61  arm_debug_state64_t drx64;
62  } debug;
63 #elif __arm__ || __arm || __armv7__
64  union {
65  arm_debug_state_t drx;
66  } debug;
67 #elif __x86_64__ || __i386__
68  x86_debug_state_t drx;
69 #endif
73 
74 typedef struct _exc_msg {
75  mach_msg_header_t hdr;
76  /* start of the kernel processed data */
77  mach_msg_body_t msg_body;
78  mach_msg_port_descriptor_t thread;
79  mach_msg_port_descriptor_t task;
80  /* end of the kernel processed data */
81  NDR_record_t NDR;
82  exception_type_t exception;
83  mach_msg_type_number_t code_cnt;
84 
90  ut32 code[0x4];
91 
92  /* some times RCV_TO_LARGE probs */
93  char pad[512];
95 
96 typedef struct _rep_msg {
97  mach_msg_header_t hdr;
98  NDR_record_t NDR;
99  kern_return_t ret_code;
101 
109 
111 static inline bool xnu_set_trace_bit(RzDebug *dbg, xnu_thread_t *th) {
112  return xnu_modify_trace_bit(dbg, th, 1);
113 }
114 static inline bool xnu_clear_trace_bit(RzDebug *dbg, xnu_thread_t *th) {
115  return xnu_modify_trace_bit(dbg, th, 0);
116 }
117 
120 RZ_IPI RzDebugReasonType xnu_wait_for_exception(RzDebug *dbg, int pid, ut32 timeout_ms, bool quiet_signal);
121 
122 #endif
#define RZ_IPI
Definition: analysis_wasm.c:11
uint16_t ut16
uint32_t ut32
RzDebug * dbg
Definition: desil.c:30
uint8_t ut8
Definition: lh5801.h:11
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
RzDebugReasonType
Definition: rz_debug.h:89
mach_msg_port_descriptor_t task
Definition: xnu_threads.h:79
exception_type_t exception
Definition: xnu_threads.h:82
mach_msg_type_number_t code_cnt
Definition: xnu_threads.h:83
char pad[512]
Definition: xnu_threads.h:93
mach_msg_header_t hdr
Definition: xnu_threads.h:75
NDR_record_t NDR
Definition: xnu_threads.h:81
mach_msg_port_descriptor_t thread
Definition: xnu_threads.h:78
mach_msg_body_t msg_body
Definition: xnu_threads.h:77
mach_msg_type_number_t count
Definition: xnu_threads.h:44
mach_port_t exception_port
Definition: xnu_threads.h:46
mach_port_t ports[EXC_TYPES_COUNT]
Definition: xnu_threads.h:41
thread_state_flavor_t flavors[EXC_TYPES_COUNT]
Definition: xnu_threads.h:43
exception_mask_t masks[EXC_TYPES_COUNT]
Definition: xnu_threads.h:40
exception_behavior_t behaviors[EXC_TYPES_COUNT]
Definition: xnu_threads.h:42
pthread_t thread
Definition: xnu_threads.h:45
NDR_record_t NDR
Definition: xnu_threads.h:98
mach_msg_header_t hdr
Definition: xnu_threads.h:97
kern_return_t ret_code
Definition: xnu_threads.h:99
thread_basic_info_data_t basic_info
Definition: xnu_threads.h:53
thread_t port
Definition: xnu_threads.h:51
char * name
Definition: xnu_threads.h:52
ut32 state_size
Definition: xnu_threads.h:57
RZ_REG_T gpr
Definition: xnu_threads.h:55
void * state
Definition: xnu_threads.h:56
Definition: inftree9.h:24
static int debug
Definition: visual.c:21
RZ_IPI bool rz_xnu_thread_set_gpr(RzDebug *dbg, xnu_thread_t *thread)
Definition: xnu_threads.c:130
struct _exception_info xnu_exception_info
RZ_IPI int rz_xnu_update_thread_list(RzDebug *dbg)
Definition: xnu_threads.c:290
RZ_IPI xnu_thread_t * rz_xnu_get_thread(RzDebug *dbg, int tid)
Definition: xnu_threads.c:378
struct _rep_msg rep_msg
RZ_IPI thread_t rz_xnu_get_cur_thread(RzDebug *dbg)
Definition: xnu_threads.c:402
RZ_IPI bool rz_xnu_thread_set_drx(RzDebug *dbg, xnu_thread_t *thread)
Definition: xnu_threads.c:70
RZ_IPI bool rz_xnu_thread_get_gpr(RzDebug *dbg, xnu_thread_t *thread)
Definition: xnu_threads.c:184
RZ_IPI bool xnu_restore_exception_ports(int pid)
RZ_IPI RzDebugReasonType xnu_wait_for_exception(RzDebug *dbg, int pid, ut32 timeout_ms, bool quiet_signal)
struct _xnu_thread xnu_thread_t
RZ_IPI bool rz_xnu_thread_get_drx(RzDebug *dbg, xnu_thread_t *thread)
Definition: xnu_threads.c:26
struct _exc_msg exc_msg
RZ_IPI bool xnu_create_exception_thread(RzDebug *dbg)
static bool xnu_set_trace_bit(RzDebug *dbg, xnu_thread_t *th)
Definition: xnu_threads.h:111
static bool xnu_clear_trace_bit(RzDebug *dbg, xnu_thread_t *th)
Definition: xnu_threads.h:114
RZ_IPI bool xnu_modify_trace_bit(RzDebug *dbg, xnu_thread_t *th, int enable)