From 159f8d384b453d310a39c189d22196eea2267b22 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 11 Oct 2023 17:51:27 -0400 Subject: [PATCH] dmnt.gen2: enable attach to arbitrary program id --- .../include/stratosphere/pm/pm_dmnt_api.hpp | 1 + .../libstratosphere/source/pm/pm_dmnt_api.cpp | 7 +++++++ .../source/dmnt2_gdb_server_impl.cpp | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/libraries/libstratosphere/include/stratosphere/pm/pm_dmnt_api.hpp b/libraries/libstratosphere/include/stratosphere/pm/pm_dmnt_api.hpp index 7a896ae66..4a5d53fa0 100644 --- a/libraries/libstratosphere/include/stratosphere/pm/pm_dmnt_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/pm/pm_dmnt_api.hpp @@ -28,6 +28,7 @@ namespace ams::pm::dmnt { Result GetProcessId(os::ProcessId *out_process_id, const ncm::ProgramId program_id); Result GetApplicationProcessId(os::ProcessId *out_process_id); Result HookToCreateApplicationProcess(os::NativeHandle *out_handle); + Result HookToCreateProcess(os::NativeHandle *out_handle, const ncm::ProgramId program_id); Result AtmosphereGetProcessInfo(os::NativeHandle *out_handle, ncm::ProgramLocation *out_loc, cfg::OverrideStatus *out_status, os::ProcessId process_id); #if defined(ATMOSPHERE_OS_HORIZON) diff --git a/libraries/libstratosphere/source/pm/pm_dmnt_api.cpp b/libraries/libstratosphere/source/pm/pm_dmnt_api.cpp index 2eed37653..559e21475 100644 --- a/libraries/libstratosphere/source/pm/pm_dmnt_api.cpp +++ b/libraries/libstratosphere/source/pm/pm_dmnt_api.cpp @@ -44,6 +44,13 @@ namespace ams::pm::dmnt { R_SUCCEED(); } + Result HookToCreateProcess(os::NativeHandle *out_handle, const ncm::ProgramId program_id) { + Event evt; + R_TRY(pmdmntHookToCreateProcess(std::addressof(evt), static_cast(program_id))); + *out_handle = evt.revent; + R_SUCCEED(); + } + Result AtmosphereGetProcessInfo(os::NativeHandle *out_handle, ncm::ProgramLocation *out_loc, cfg::OverrideStatus *out_status, os::ProcessId process_id) { *out_handle = os::InvalidNativeHandle; *out_loc = {}; diff --git a/stratosphere/dmnt.gen2/source/dmnt2_gdb_server_impl.cpp b/stratosphere/dmnt.gen2/source/dmnt2_gdb_server_impl.cpp index 379687bde..ddb0d8fbc 100644 --- a/stratosphere/dmnt.gen2/source/dmnt2_gdb_server_impl.cpp +++ b/stratosphere/dmnt.gen2/source/dmnt2_gdb_server_impl.cpp @@ -2105,9 +2105,24 @@ namespace ams::dmnt { ParsePrefix(command, "0x"); /* Decode program id. */ - const u64 program_id = DecodeHex(command); + const ncm::ProgramId program_id(DecodeHex(command)); - AppendReplyFormat(reply_cur, reply_end, "[TODO] wait for program id 0x%lx\n", program_id); + /* Wait for the process to be created. */ + { + /* Get hook to creation of process with program id. */ + os::NativeHandle h; + R_ABORT_UNLESS(pm::dmnt::HookToCreateProcess(std::addressof(h), program_id)); + + /* Wait for event. */ + os::SystemEvent hook_event(h, true, os::InvalidNativeHandle, false, os::EventClearMode_AutoClear); + hook_event.Wait(); + } + + /* Get process id. */ + R_ABORT_UNLESS(pm::dmnt::GetProcessId(std::addressof(m_wait_process_id), program_id)); + + /* Note that we're attaching. */ + AppendReplyFormat(reply_cur, reply_end, "Send `attach 0x%lx` to attach.\n", m_wait_process_id.value); } else { std::memcpy(m_reply_cur, command, std::strlen(command) + 1); AppendReplyFormat(reply_cur, reply_end, "Unknown command `%s`\n", m_reply_cur);