17 #define LOG_MACH_ERROR(name, rc) \
19 const char *str = mach_error_string(rc); \
20 RZ_LOG_ERROR("%s/%s: %s\n", __FUNCTION__, name, str ? str : "(unknown)"); \
38 #define PT_THUPDATE 13
39 #define PT_ATTACHEXC 14
40 #define PT_FORCEQUOTA 30
41 #define PT_DENY_ATTACH 31
42 #define PT_FIRSTMACH 32
45 #include <sys/ptrace.h>
47 #include <sys/proc_info.h>
52 #include <sys/types.h>
54 #include <mach/exception_types.h>
55 #include <mach/mach_init.h>
56 #include <mach/mach_port.h>
57 #include <mach/mach_interface.h>
58 #include <mach/mach_traps.h>
59 #include <mach/mach_types.h>
61 #include <mach/mach_error.h>
62 #include <mach/task.h>
63 #include <mach/task_info.h>
64 #include <mach/thread_act.h>
65 #include <mach/thread_info.h>
66 #include <mach/vm_map.h>
67 #include <mach-o/loader.h>
68 #include <mach-o/nlist.h>
71 #include <sys/sysctl.h>
72 #include <sys/fcntl.h>
77 #include <sys/ptrace.h>
78 #include <sys/types.h>
80 #include <mach/ppc/_types.h>
81 #include <mach/ppc/thread_status.h>
84 #include <mach/aarch64/thread_status.h>
87 #include <mach/arm/thread_status.h>
89 #include <mach/arm/thread_status.h>
93 #include <mach/i386/thread_status.h>
94 #include <sys/ucontext.h>
95 #include <mach/i386/_structs.h>
115 #define HEADER_SIZE 0x1000
116 #define IMAGE_OFFSET 0x2000
117 #define KERNEL_LOWER 0xffffff8000000000
120 #define HEADER_SIZE 0x1000
121 #define IMAGE_OFFSET 0x201000
122 #define KERNEL_LOWER 0x80000000
128 #define REG_PC ((dbg->bits == RZ_SYS_BITS_64) ? 16 : 10)
129 #define REG_FL ((dbg->bits == RZ_SYS_BITS_64) ? 17 : 9)
152 #define BCR_M_IMVA_MATCH ((uint32_t)(0u << 21))
153 #define BCR_M_CONTEXT_ID_MATCH ((uint32_t)(1u << 21))
154 #define BCR_M_IMVA_MISMATCH ((uint32_t)(2u << 21))
155 #define BCR_M_RESERVED ((uint32_t)(3u << 21))
158 #define E_ENABLE_LINKING ((uint32_t)(1u << 20))
161 #define BAS_IMVA_PLUS_0 ((uint32_t)(1u << 5))
162 #define BAS_IMVA_PLUS_1 ((uint32_t)(1u << 6))
163 #define BAS_IMVA_PLUS_2 ((uint32_t)(1u << 7))
164 #define BAS_IMVA_PLUS_3 ((uint32_t)(1u << 8))
165 #define BAS_IMVA_0_1 ((uint32_t)(3u << 5))
166 #define BAS_IMVA_2_3 ((uint32_t)(3u << 7))
167 #define BAS_IMVA_ALL ((uint32_t)(0xfu << 5))
170 #define S_RSVD ((uint32_t)(0u << 1))
171 #define S_PRIV ((uint32_t)(1u << 1))
172 #define S_USER ((uint32_t)(2u << 1))
173 #define S_PRIV_USER ((S_PRIV) | (S_USER))
175 #define BCR_ENABLE ((uint32_t)(1u))
176 #define WCR_ENABLE ((uint32_t)(1u))
179 #define WCR_LOAD ((uint32_t)(1u << 3))
180 #define WCR_STORE ((uint32_t)(1u << 4))
192 thread_flavor_array[] = {
193 { PPC_THREAD_STATE, PPC_THREAD_STATE_COUNT },
194 { PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT },
195 { PPC_EXCEPTION_STATE, PPC_EXCEPTION_STATE_COUNT },
196 { PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT },
199 static int coredump_nflavors = 4;
201 #elif defined(__ppc64__)
204 thread_flavor_array[] = {
205 { PPC_THREAD_STATE64, PPC_THREAD_STATE64_COUNT },
206 { PPC_FLOAT_STATE, PPC_FLOAT_STATE_COUNT },
207 { PPC_EXCEPTION_STATE64, PPC_EXCEPTION_STATE64_COUNT },
208 { PPC_VECTOR_STATE, PPC_VECTOR_STATE_COUNT },
211 static int coredump_nflavors = 4;
213 #elif defined(__i386__)
216 thread_flavor_array[] = {
222 static int coredump_nflavors = 3;
224 #elif defined(__x86_64__)
227 thread_flavor_array[] = {
233 static int coredump_nflavors = 3;
235 #elif defined(__aarch64__) || defined(__arm64__)
238 thread_flavor_array[] = {
239 { ARM_UNIFIED_THREAD_STATE, ARM_UNIFIED_THREAD_STATE_COUNT }
242 static int coredump_nflavors = 1;
244 #elif defined(__arm__)
247 thread_flavor_array[] = {
248 { ARM_THREAD_STATE64, ARM_THREAD_STATE64_COUNT }
251 static int coredump_nflavors = 1;
255 #warning Unsupported architecture
259 #define MAX_TSTATE_FLAVORS 10
260 #define DEFAULT_COREFILE_DEST "core.%u"
261 #define RZ_DEBUG_REASON_MACH_RCV_INTERRUPTED -2
static void list(RzEgg *egg)
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
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid ptrace
#define x86_EXCEPTION_STATE64_COUNT
#define x86_FLOAT_STATE64_COUNT
#define x86_THREAD_STATE64_COUNT
mach_msg_type_number_t count
coredump_thread_state_flavor_t * flavors
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
char * xnu_reg_profile(RzDebug *dbg)
bool xnu_step(RzDebug *dbg)
int xnu_map_dealloc(RzDebug *dbg, ut64 addr, int size)
int xnu_continue(RzDebug *dbg, int pid, int tid, int sig)
RzDebugMap * xnu_map_alloc(RzDebug *dbg, ut64 addr, int size)
int xnu_reg_write(RzDebug *dgb, int type, const ut8 *buf, int size)
bool xnu_generate_corefile(RzDebug *dbg, RzBuffer *dest)
int xnu_map_protect(RzDebug *dbg, ut64 addr, int size, int perms)
RzList * xnu_dbg_maps(RzDebug *dbg, int only_modules)
RzDebugPid * xnu_get_pid(int pid)
char * xnu_corefile_default_location(void)
int xnu_get_vmmap_entries_for_pid(pid_t pid)
int xnu_detach(RzDebug *dbg, int pid)
int xnu_stop(RzDebug *dbg, int pid)
int xnu_reg_read(RzDebug *dbg, int type, ut8 *buf, int size)
int xnu_wait(RzDebug *dbg, int pid)
int xnu_attach(RzDebug *dbg, int pid)
RzList * xnu_thread_list(RzDebug *dbg, int pid, RzList *list)
task_t pid_to_task(int pid)
RzDebugInfo * xnu_info(RzDebug *dbg, const char *arg)