This is an archive of the discontinued LLVM Phabricator instance.

[lldb/linux] Handle main thread exits
ClosedPublic

Authored by labath on Mar 30 2022, 3:41 AM.

Details

Summary

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).

Diff Detail

Event Timeline

labath created this revision.Mar 30 2022, 3:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 30 2022, 3:41 AM
Herald added a subscriber: krytarowski. · View Herald Transcript
labath requested review of this revision.Mar 30 2022, 3:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 30 2022, 3:41 AM
mgorny accepted this revision.Apr 4 2022, 1:36 PM

Makes sense.

This revision is now accepted and ready to land.Apr 4 2022, 1:36 PM
This revision was automatically updated to reflect the committed changes.