pm: remove magic flag/event numbers to improve readability and understandability

This commit is contained in:
misson20000 2018-07-27 01:56:24 -07:00 committed by SciresM
parent 9d0d9d95e1
commit 6adcb483e5
3 changed files with 159 additions and 34 deletions

View file

@ -105,16 +105,16 @@ void Registration::HandleProcessLaunch() {
/* Setup process flags. */ /* Setup process flags. */
if (program_info.application_type & 1) { 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)) { 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)) { 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))) { 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. */ /* Add process to the list. */
@ -125,7 +125,7 @@ void Registration::HandleProcessLaunch() {
g_debug_title_event->signal_event(); g_debug_title_event->signal_event();
g_debug_on_launch_tid = 0; g_debug_on_launch_tid = 0;
rc = 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_application_event->signal_event();
g_debug_next_application = false; g_debug_next_application = false;
rc = 0; rc = 0;
@ -226,7 +226,7 @@ Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process
process->state = (ProcessState)tmp; process->state = (ProcessState)tmp;
if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) { if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) {
process->flags &= ~0x4; process->flags &= ~PROCESSFLAGS_CRASH_DEBUG;
} }
switch (process->state) { switch (process->state) {
case ProcessState_Created: case ProcessState_Created:
@ -234,37 +234,37 @@ Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process
case ProcessState_Exiting: case ProcessState_Exiting:
break; break;
case ProcessState_DebugDetached: case ProcessState_DebugDetached:
if (process->flags & 8) { if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) {
process->flags &= ~0x30; process->flags &= ~(PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED);
process->flags |= 0x10; process->flags |= PROCESSFLAGS_DEBUGEVENTPENDING;
g_process_event->signal_event(); g_process_event->signal_event();
} }
if (kernelAbove200() && process->flags & 0x80) { if (kernelAbove200() && process->flags & PROCESSFLAGS_NOTIFYDEBUGSPECIAL) {
process->flags &= ~0x180; process->flags &= ~(PROCESSFLAGS_NOTIFYDEBUGSPECIAL | PROCESSFLAGS_DEBUGDETACHED);
process->flags |= 0x100; process->flags |= PROCESSFLAGS_DEBUGDETACHED;
} }
break; break;
case ProcessState_Crashed: case ProcessState_Crashed:
process->flags |= 6; process->flags |= (PROCESSFLAGS_CRASHED | PROCESSFLAGS_CRASH_DEBUG);
g_process_event->signal_event(); g_process_event->signal_event();
break; break;
case ProcessState_Running: case ProcessState_Running:
if (process->flags & 8) { if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) {
process->flags &= ~0x30; process->flags &= ~(PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED);
process->flags |= 0x10; process->flags |= PROCESSFLAGS_DEBUGEVENTPENDING;
g_process_event->signal_event(); g_process_event->signal_event();
} }
break; break;
case ProcessState_Exited: case ProcessState_Exited:
if (process->flags & 1 && !kernelAbove500()) { if (process->flags & PROCESSFLAGS_NOTIFYWHENEXITED && !kernelAbove500()) {
g_process_event->signal_event(); g_process_event->signal_event();
} else { } else {
FinalizeExitedProcess(process); FinalizeExitedProcess(process);
} }
return 0xF601; return 0xF601;
case ProcessState_DebugSuspended: case ProcessState_DebugSuspended:
if (process->flags & 8) { if (process->flags & PROCESSFLAGS_NOTIFYDEBUGEVENTS) {
process->flags |= 0x30; process->flags |= (PROCESSFLAGS_DEBUGEVENTPENDING | PROCESSFLAGS_DEBUGSUSPENDED);
g_process_event->signal_event(); g_process_event->signal_event();
} }
break; break;
@ -274,7 +274,7 @@ Result Registration::HandleSignaledProcess(std::shared_ptr<Registration::Process
void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process> process) { void Registration::FinalizeExitedProcess(std::shared_ptr<Registration::Process> process) {
auto auto_lock = GetProcessListUniqueLock(); 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. */ /* Unregister with FS. */
if (R_FAILED(fsprUnregisterProgram(process->pid))) { if (R_FAILED(fsprUnregisterProgram(process->pid))) {
/* TODO: Panic. */ /* TODO: Panic. */
@ -344,7 +344,7 @@ bool Registration::HasApplicationProcess(std::shared_ptr<Registration::Process>
auto auto_lock = GetProcessListUniqueLock(); auto auto_lock = GetProcessListUniqueLock();
for (auto &process : g_process_list.processes) { for (auto &process : g_process_list.processes) {
if (process->flags & 0x40) { if (process->flags & PROCESSFLAGS_APPLICATION) {
if (out != nullptr) { if (out != nullptr) {
*out = process; *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) { 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; out_pids[num++] = process->pid;
} }
} }
@ -403,27 +403,33 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
auto auto_lock = GetProcessListUniqueLock(); auto auto_lock = GetProcessListUniqueLock();
for (auto &p : g_process_list.processes) { for (auto &p : g_process_list.processes) {
if (kernelAbove200() && p->state >= ProcessState_DebugDetached && p->flags & 0x100) { if (kernelAbove200() && p->state >= ProcessState_DebugDetached && p->flags & PROCESSFLAGS_DEBUGDETACHED) {
p->flags &= ~0x100; p->flags &= ~PROCESSFLAGS_DEBUGDETACHED;
*out_pid = p->pid; *out_pid = p->pid;
*out_type = kernelAbove500() ? 2 : 5; *out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_DEBUGDETACHED : PROCESSEVENTTYPE_DEBUGDETACHED;
return; return;
} }
if (p->flags & 0x10) { if (p->flags & PROCESSFLAGS_DEBUGEVENTPENDING) {
u64 old_flags = p->flags; u64 old_flags = p->flags;
p->flags &= ~0x10; p->flags &= ~PROCESSFLAGS_DEBUGEVENTPENDING;
*out_pid = p->pid; *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; return;
} }
if (p->flags & 2) { if (p->flags & PROCESSFLAGS_CRASHED) {
*out_pid = p->pid; *out_pid = p->pid;
*out_type = kernelAbove500() ? 3 : 1; *out_type = kernelAbove500() ? PROCESSEVENTTYPE_500_CRASH : PROCESSEVENTTYPE_CRASH;
return; 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_pid = p->pid;
*out_type = 2; *out_type = PROCESSEVENTTYPE_EXIT;
return; return;
} }
} }
@ -434,7 +440,7 @@ void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
std::shared_ptr<Registration::Process> process = g_dead_process_list.processes[0]; std::shared_ptr<Registration::Process> process = g_dead_process_list.processes[0];
g_dead_process_list.processes.erase(g_dead_process_list.processes.begin()); g_dead_process_list.processes.erase(g_dead_process_list.processes.begin());
*out_pid = process->pid; *out_pid = process->pid;
*out_type = 1; *out_type = PROCESSEVENTTYPE_500_EXIT;
return; return;
} }
} }

View file

@ -11,6 +11,125 @@
#define LAUNCHFLAGS_NOTIFYDEBUGEVENTS(flags) (flags & (kernelAbove500() ? 0x8 : 0x10)) #define LAUNCHFLAGS_NOTIFYDEBUGEVENTS(flags) (flags & (kernelAbove500() ? 0x8 : 0x10))
#define LAUNCHFLAGS_NOTIYDEBUGSPECIAL(flags) (flags & (kernelAbove500() ? 0x2 : (kernelAbove200() ? 0x20 : 0x0))) #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 { class Registration {
public: public:

View file

@ -140,7 +140,7 @@ std::tuple<Result> ShellService::clear_process_notification_flag(u64 pid) {
std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid); std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid);
if (proc != NULL) { if (proc != NULL) {
proc->flags &= ~2; proc->flags &= ~PROCESSFLAGS_CRASHED;
return {0x0}; return {0x0};
} else { } else {
return {0x20F}; return {0x20F};