diff --git a/librz/debug/p/native/xnu/xnu_debug.c b/librz/debug/p/native/xnu/xnu_debug.c index 7787b64e39..4de2b7f77c 100644 --- a/librz/debug/p/native/xnu/xnu_debug.c +++ b/librz/debug/p/native/xnu/xnu_debug.c @@ -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; diff --git a/librz/debug/p/native/xnu/xnu_debug.h b/librz/debug/p/native/xnu/xnu_debug.h index 545a179e2b..ce21f7b2ee 100644 --- a/librz/debug/p/native/xnu/xnu_debug.h +++ b/librz/debug/p/native/xnu/xnu_debug.h @@ -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); diff --git a/test/db/archos/darwin-x64/dbg_di b/test/db/archos/darwin-x64/dbg_di new file mode 100644 index 0000000000..395c9b19f2 --- /dev/null +++ b/test/db/archos/darwin-x64/dbg_di @@ -0,0 +1,17 @@ +NAME=dbg.di +FILE=bins/mach0/hello-osx-x86_64 +ARGS=-d +CMDS=<