From 70f3fa2dd2d8923fbd683dda9048938629ac5044 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sat, 12 Feb 2022 08:17:42 -0800 Subject: [PATCH] Kernel: Set new process name in `do_exec` before waiting for the tracer While investigating why gdb is failing when it calls `PT_CONTINUE` against Serenity I noticed that the names of the programs in the System Monitor didn't make sense. They were seemingly stale. After inspecting the kernel code, it became apparent that the sequence occurs as follows: 1. Debugger calls `fork()` 2. The forked child calls `PT_TRACE_ME` 3. The `PT_TRACE_ME` instructs the forked process to block in the kernel waiting for a signal from the tracer on the next call to `execve(..)`. 4. Debugger waits for forked child to spawn and stop, and then it calls `PT_ATTACH` followed by `PT_CONTINUE` on the child. 5. Currently the `PT_CONTINUE` fails because of some other yet to be found bug. 6. The process name is set immediately AFTER we are woken up by the `PT_CONTINUE` which never happens in the case I'm debugging. This chain of events leaves the process suspended, with the name of the original (forked) process instead of the name we inherit from the `execve(..)` call. To avoid such confusion in the future, we set the new name before we block waiting for the tracer. --- Kernel/Syscalls/execve.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 93d2e16dc3..8c27916837 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -564,6 +564,9 @@ ErrorOr Process::do_exec(NonnullRefPtr main_program_d // and we don't want to deal with faults after this point. auto new_userspace_sp = TRY(make_userspace_context_for_main_thread(new_main_thread->regs(), *load_result.stack_region.unsafe_ptr(), m_arguments, m_environment, move(auxv))); + m_name = move(new_process_name); + new_main_thread->set_name(move(new_main_thread_name)); + if (wait_for_tracer_at_next_execve()) { // Make sure we release the ptrace lock here or the tracer will block forever. ptrace_locker.unlock(); @@ -583,9 +586,6 @@ ErrorOr Process::do_exec(NonnullRefPtr main_program_d // NOTE: Be careful to not trigger any page faults below! - m_name = move(new_process_name); - new_main_thread->set_name(move(new_main_thread_name)); - { ProtectedDataMutationScope scope { *this }; m_protected_values.promises = m_protected_values.execpromises.load();