This patch handles the situation where the main thread exits (through
the SYS_exit syscall). In this case, the process as a whole continues
running until all of the other threads exit, or one of them issues an
exit_group syscall.
The patch consists of two changes:
- a moderate redesign of the handling of thread exit (WIFEXITED) events. Previously, we were removing (forgetting) a thread once we received the WIFEXITED (or WIFSIGNALED) event. This was problematic for the main thread, since the main thread WIFEXITED event (which is better thought of as a process-wide event) gets reported only after the entire process exits. This resulted in deadlocks, where we were waiting for the process to stop (because we still considered the main thread "live").
This patch changes the logic such that the main thread is removed as soon as its PTRACE_EVENT_EXIT (the pre-exit) event is received. At this point we can consider the thread gone (for most purposes). As a corrolary, I needed to add special logic to catch process-wide exit events in the cases where we don't have the main thread around.
- The second part of the patch is the removal of the assumptions that the main thread is always available. This generally meant replacing the uses of GetThreadByID(process_id) with GetCurrentThread() in various process-wide operations (such as memory reads).