From 6adcb483e5dab08e8aab9467bfbd5c4c894e4c2f Mon Sep 17 00:00:00 2001 From: misson20000 Date: Fri, 27 Jul 2018 01:56:24 -0700 Subject: [PATCH] pm: remove magic flag/event numbers to improve readability and understandability --- stratosphere/pm/source/pm_registration.cpp | 72 +++++++------ stratosphere/pm/source/pm_registration.hpp | 119 +++++++++++++++++++++ stratosphere/pm/source/pm_shell.cpp | 2 +- 3 files changed, 159 insertions(+), 34 deletions(-) diff --git a/stratosphere/pm/source/pm_registration.cpp b/stratosphere/pm/source/pm_registration.cpp index 856e5dfec..e61b81a3b 100644 --- a/stratosphere/pm/source/pm_registration.cpp +++ b/stratosphere/pm/source/pm_registration.cpp @@ -105,16 +105,16 @@ void Registration::HandleProcessLaunch() { /* Setup process flags. */ if (program_info.application_type & 1) { - new_process.flags |= 0x40; + new_process.flags |= PROCESSFLAGS_APPLICATION; } if (kernelAbove200() && LAUNCHFLAGS_NOTIYDEBUGSPECIAL(launch_flags) && (program_info.application_type & 4)) { - new_process.flags |= 0x80; + new_process.flags |= PROCESSFLAGS_NOTIFYDEBUGSPECIAL; } if (LAUNCHFLAGS_NOTIFYWHENEXITED(launch_flags)) { - new_process.flags |= 1; + new_process.flags |= PROCESSFLAGS_NOTIFYWHENEXITED; } if (LAUNCHFLAGS_NOTIFYDEBUGEVENTS(launch_flags) && (!kernelAbove200() || (program_info.application_type & 4))) { - new_process.flags |= 0x8; + new_process.flags |= PROCESSFLAGS_NOTIFYDEBUGEVENTS; } /* Add process to the list. */ @@ -125,7 +125,7 @@ void Registration::HandleProcessLaunch() { g_debug_title_event->signal_event(); g_debug_on_launch_tid = 0; rc = 0; - } else if ((new_process.flags & 0x40) && g_debug_next_application.load()) { + } else if ((new_process.flags & PROCESSFLAGS_APPLICATION) && g_debug_next_application.load()) { g_debug_application_event->signal_event(); g_debug_next_application = false; rc = 0; @@ -226,7 +226,7 @@ Result Registration::HandleSignaledProcess(std::shared_ptrstate = (ProcessState)tmp; if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) { - process->flags &= ~0x4; + process->flags &= ~PROCESSFLAGS_CRASH_DEBUG; } switch (process->state) { case ProcessState_Created: @@ -234,37 +234,37 @@ Result Registration::HandleSignaledProcess(std::shared_ptrflags & 8) { - process->flags &= ~0x30; - process->flags |= 0x10; + if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) { + process->flags &= ~(PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED); + process->flags |= PROCESSFLAGS_DEBUGEVENTPENDING; g_process_event->signal_event(); } - if (kernelAbove200() && process->flags & 0x80) { - process->flags &= ~0x180; - process->flags |= 0x100; + if (kernelAbove200() && process->flags & PROCESSFLAGS_NOTIFYDEBUGSPECIAL) { + process->flags &= ~(PROCESSFLAGS_NOTIFYDEBUGSPECIAL | PROCESSFLAGS_DEBUGDETACHED); + process->flags |= PROCESSFLAGS_DEBUGDETACHED; } break; case ProcessState_Crashed: - process->flags |= 6; + process->flags |= (PROCESSFLAGS_CRASHED | PROCESSFLAGS_CRASH_DEBUG); g_process_event->signal_event(); break; case ProcessState_Running: - if (process->flags & 8) { - process->flags &= ~0x30; - process->flags |= 0x10; + if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) { + process->flags &= ~(PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED); + process->flags |= PROCESSFLAGS_DEBUGEVENTPENDING; g_process_event->signal_event(); } break; case ProcessState_Exited: - if (process->flags & 1 && !kernelAbove500()) { + if (process->flags & PROCESSFLAGS_NOTIFYWHENEXITED && !kernelAbove500()) { g_process_event->signal_event(); } else { FinalizeExitedProcess(process); } return 0xF601; case ProcessState_DebugSuspended: - if (process->flags & 8) { - process->flags |= 0x30; + if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) { + process->flags |= (PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED); g_process_event->signal_event(); } break; @@ -274,7 +274,7 @@ Result Registration::HandleSignaledProcess(std::shared_ptr process) { auto auto_lock = GetProcessListUniqueLock(); - bool signal_debug_process_5x = kernelAbove500() && process->flags & 1; + bool signal_debug_process_5x = kernelAbove500() && process->flags & PROCESSFLAGS_NOTIFYWHENEXITED; /* Unregister with FS. */ if (R_FAILED(fsprUnregisterProgram(process->pid))) { /* TODO: Panic. */ @@ -344,7 +344,7 @@ bool Registration::HasApplicationProcess(std::shared_ptr auto auto_lock = GetProcessListUniqueLock(); for (auto &process : g_process_list.processes) { - if (process->flags & 0x40) { + if (process->flags & PROCESSFLAGS_APPLICATION) { if (out != nullptr) { *out = process; } @@ -386,7 +386,7 @@ Result Registration::GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out for (auto &process : g_process_list.processes) { - if (process->flags & 4 && num < max_out) { + if (process->flags & PROCESSFLAGS_CRASH_DEBUG && num < max_out) { out_pids[num++] = process->pid; } } @@ -403,27 +403,33 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) { auto auto_lock = GetProcessListUniqueLock(); for (auto &p : g_process_list.processes) { - if (kernelAbove200() && p->state >= ProcessState_DebugDetached && p->flags & 0x100) { - p->flags &= ~0x100; + if (kernelAbove200() && p->state >= ProcessState_DebugDetached && p->flags & PROCESSFLAGS_DEBUGDETACHED) { + p->flags &= ~PROCESSFLAGS_DEBUGDETACHED; *out_pid = p->pid; - *out_type = kernelAbove500() ? 2 : 5; + *out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED; return; } - if (p->flags & 0x10) { + if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) { u64 old_flags = p->flags; - p->flags &= ~0x10; + p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING; *out_pid = p->pid; - *out_type = kernelAbove500() ? (((old_flags >> 5) & 1) | 4) : (((old_flags >> 5) & 1) + 3); + *out_type = kernelAbove500() ? + ((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ? + PROCESSEVENTTYPE_500_SUSPENDED : + PROCESSEVENTTYPE_500_RUNNING) : + ((old_flags & PROCESSFLAGS_DEBUGSUSPENDED) ? + PROCESSEVENTTYPE_SUSPENDED : + PROCESSEVENTTYPE_RUNNING); return; } - if (p->flags & 2) { + if (p->flags & PROCESSFLAGS_CRASHED) { *out_pid = p->pid; - *out_type = kernelAbove500() ? 3 : 1; + *out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH; return; } - if (!kernelAbove500() && p->flags & 1 && p->state == ProcessState_Exited) { + if (!kernelAbove500() && p->flags & PROCESSFLAGS_NOTIFYWHENEXITED && p->state == ProcessState_Exited) { *out_pid = p->pid; - *out_type = 2; + *out_type = PROCESSEVENTTYPE_EXIT; return; } } @@ -434,7 +440,7 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) { std::shared_ptr process = g_dead_process_list.processes[0]; g_dead_process_list.processes.erase(g_dead_process_list.processes.begin()); *out_pid = process->pid; - *out_type = 1; + *out_type = PROCESSEVENTTYPE_500_EXIT; return; } } diff --git a/stratosphere/pm/source/pm_registration.hpp b/stratosphere/pm/source/pm_registration.hpp index ce22b8573..89dc1d929 100644 --- a/stratosphere/pm/source/pm_registration.hpp +++ b/stratosphere/pm/source/pm_registration.hpp @@ -11,7 +11,126 @@ #define LAUNCHFLAGS_NOTIFYDEBUGEVENTS(flags) (flags & (kernelAbove500() ? 0x8 : 0x10)) #define LAUNCHFLAGS_NOTIYDEBUGSPECIAL(flags) (flags & (kernelAbove500() ? 0x2 : (kernelAbove200() ? 0x20 : 0x0))) +// none of these names are official or authoritative in any way +enum { + /* + set in HandleProcessLaunch when + - launch_flags has LAUNCHFLAGS_NOTIFYWHENEXITED set + signals g_process_event in HandleSignaledProcess when + - process enters Exited state + - we're below 5.0.0 + (finalizes dead process when not set) + adds to dead process list in FinalizeExitedProcess when + - we're 5.0.0+ + (also signals g_process_event) + [5.0.0+] causes ProcessEventType 2 + */ + PROCESSFLAGS_NOTIFYWHENEXITED = 0x001, + /* + set in HandleSignaledProcess when + - process crashes + causes ProcessEventType 1 ([5.0.0+] 3) (persistent) + */ + PROCESSFLAGS_CRASHED = 0x002, + + /* + cleared in HandleSignaledProcess when + - process goes from CRASHED to any other state + set in HandleSignaledProcess when + - process crashes + adds process to GetDebugProcessIds + */ + PROCESSFLAGS_CRASH_DEBUG = 0x004, + + /* + set in HandleProcessLaunch when + - launch_flags has LAUNCHFLAGS_NOTIFYDEBUGEVENTS set + - and (we're 1.0.0 or program_info.application_type & 4) (is this because 1.0.0 doesn't check?) + signals g_process_event in HandleSignaledProcess when + - process enters DebugDetached state + signals g_process_event in HandleSignaledProcess when + - process enters Running state + */ + PROCESSFLAGS_NOTIFYDEBUGEVENTS = 0x008, + + /* + set in HandleSignaledProcess when + - process enters DebugDetached state + - and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set + set in HandleSignaledProcess when + - process enters Running state + - and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set + set in HandleSignaledProcess when + - process enters DebugSuspended state + - and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set + causes some complicated ProcessEvent (one-shot) + */ + PROCESSFLAGS_DEBUGEVENTPENDING = 0x010, + + /* + cleared in HandleSignaledProcess when + - process enters DebugDetached state + - and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set + cleared in HandleSignaledProcess when + - process enters Running state + - and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set + set in HandleSignaledProcess when + - process enters DebugSuspended state + - and PROCESSFLAGS_NOTIFYDEBUGEVENTS is set + */ + PROCESSFLAGS_DEBUGSUSPENDED = 0x020, + + /* + set in HandleProcessLaunch when + - program_info.application_type & 1 + signals g_debug_application_event in HandleProcessLaunch when + - g_debug_next_application is set (unsets g_debug_next_application) + causes HasApplicationProcess to return true + meaning? + application + */ + PROCESSFLAGS_APPLICATION = 0x040, + + /* + set in HandleProcessLaunch when + - we're above 2.0.0 (creport related?) + - and launch_flags has LAUNCHFLAGS_NOTIFYDEBUGSPECIAL set + - and program_info.application_type & 4 + tested in HandleSignaledProcess when + - process enters DebugDetached state + - and we're above 2.0.0 + causes + - clearing of PROCESSFLAGS_NOTIFYDEBUGSPECIAL (one-shot?) + - setting of PROCESSFLAGS_DEBUGDETACHED + */ + PROCESSFLAGS_NOTIFYDEBUGSPECIAL = 0x080, + + /* + set in HandleSignaledProcess when + - process enters DebugDetached state + - and we're above 2.0.0 + - and PROCESSFLAGS_NOTIFYDEBUGSPECIAL was set + causes ProcessEventType 5 ([5.0.0+] 2) (one-shot) + */ + PROCESSFLAGS_DEBUGDETACHED = 0x100, +}; + +enum { + PROCESSEVENTTYPE_CRASH = 1, + PROCESSEVENTTYPE_EXIT = 2, // only fired once, when process enters DebugDetached state (likely creport related) + PROCESSEVENTTYPE_RUNNING = 3, // debug detached or running + PROCESSEVENTTYPE_SUSPENDED = 4, // debug suspended + PROCESSEVENTTYPE_DEBUGDETACHED = 5, + + + PROCESSEVENTTYPE_500_EXIT = 1, + PROCESSEVENTTYPE_500_DEBUGDETACHED = 2, // only fired once, when process enters DebugDetached state (likely creport related) + PROCESSEVENTTYPE_500_CRASH = 3, + PROCESSEVENTTYPE_500_RUNNING = 4, // debug detached or running + PROCESSEVENTTYPE_500_SUSPENDED = 5, // debug suspended +}; + class Registration { public: struct TidSid { diff --git a/stratosphere/pm/source/pm_shell.cpp b/stratosphere/pm/source/pm_shell.cpp index d09807eb8..ffdab1bd6 100644 --- a/stratosphere/pm/source/pm_shell.cpp +++ b/stratosphere/pm/source/pm_shell.cpp @@ -140,7 +140,7 @@ std::tuple ShellService::clear_process_notification_flag(u64 pid) { std::shared_ptr proc = Registration::GetProcess(pid); if (proc != NULL) { - proc->flags &= ~2; + proc->flags &= ~PROCESSFLAGS_CRASHED; return {0x0}; } else { return {0x20F};