debug: make xnu attach idempotent for di (#6295)

This commit is contained in:
Riccardo Schirone 2026-05-02 08:43:53 +02:00 committed by GitHub
parent a99cc738c2
commit 056bb141a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 29 deletions

View file

@ -37,6 +37,7 @@ RZ_IPI bool rz_xnu_debug_init(RzDebug *dbg, void **user) {
return false;
}
ctx->old_pid = -1;
ctx->last_attached_pid = -1;
*user = ctx;
return true;
}
@ -152,6 +153,10 @@ int xnu_attach(RzDebug *dbg, int pid) {
RzXnuDebug *ctx = dbg->plugin_data;
dbg->pid = pid;
if (ctx->last_attached_pid == pid) {
RZ_LOG_INFO("Already attached to pid %d, skipping attach\n", pid);
return pid;
}
ctx->cpu = xnu_get_cpu_type(pid);
if (!ctx->cpu) {
@ -180,6 +185,7 @@ int xnu_attach(RzDebug *dbg, int pid) {
xnu_stop(dbg, pid);
}
ctx->last_attached_pid = pid;
return pid;
}
@ -452,21 +458,6 @@ RzDebugInfo *xnu_info(RzDebug *dbg, const char *arg) {
return rdi;
}
/*
static void xnu_free_threads_ports (RzDebugPid *p) {
kern_return_t kr;
if (!p) return;
free (p->path);
if (p->pid != old_pid) {
kr = mach_port_deallocate (old_pid, p->pid);
if (kr != KERN_SUCCESS) {
eprintf ("error mach_port_deallocate in "
"xnu_free_threads ports\n");
}
}
}
*/
RzList *xnu_thread_list(RzDebug *dbg, int pid, RzList *list) {
rz_return_val_if_fail(dbg && dbg->plugin_data, NULL);
RzXnuDebug *ctx = dbg->plugin_data;

View file

@ -275,11 +275,18 @@ typedef struct _exception_info {
mach_port_t exception_port;
} xnu_exception_info;
/**
* \brief XNU-specific debugger context.
*
* Holds XNU-specific state for a debug session and is used by the native XNU
* debugger implementation to track the debuggee's info
*/
typedef struct rz_xnu_debug_t {
cpu_type_t cpu; ///< CPU/Architecture of the debuggee, determined and set once after attach
task_t task_dbg;
int old_pid;
xnu_exception_info ex;
cpu_type_t cpu; ///< CPU/Architecture of the debuggee.
task_t task_dbg; ///< Mach task port for the process with pid `old_pid`. Cache for `pid_to_task`.
int old_pid; ///< Previously observed PID, related to `task_dbg`. Cache for `pid_to_task`.
int last_attached_pid; ///< Last PID the plugin attached to.
xnu_exception_info ex; ///< Exception handling state (ports, masks, flavors).
} RzXnuDebug;
RZ_IPI bool rz_xnu_debug_init(RzDebug *dbg, void **user);

View file

@ -0,0 +1,17 @@
NAME=dbg.di
FILE=bins/mach0/hello-osx-x86_64
ARGS=-d
CMDS=<<EOF
dcu main
di~baddr
ds
di~baddr
EOF
EXPECT=<<EOF
baddr=0x100000000
baddr=0x100000000
EOF
EXPECT_ERR=<<EOF
hit breakpoint at: 0x100000f0c
EOF
RUN

View file

@ -1,10 +0,0 @@
NAME=dbg.di
FILE=/bin/ls
ARGS=-d
BROKEN=1
CMDS=di~baddr
EXPECT=<<EOF
baddr=0x100000000
EOF
RUN