Add pretty-printer for wait(2) statuses and modernize the code handling them

Summary:
A number of places were trying to decode the result of wait(). Add a simple
utility function that does that and a struct that encapsulates the
decoded result. Then also provide a pretty-printer for that class.

Reviewers: zturner, krytarowski, eugene

Subscribers: lldb-commits, mgorny

Differential Revision: https://reviews.llvm.org/D33998

llvm-svn: 305689
diff --git a/lldb/source/Plugins/Process/Darwin/NativeProcessDarwin.cpp b/lldb/source/Plugins/Process/Darwin/NativeProcessDarwin.cpp
index f6c8c78..518f0d2 100644
--- a/lldb/source/Plugins/Process/Darwin/NativeProcessDarwin.cpp
+++ b/lldb/source/Plugins/Process/Darwin/NativeProcessDarwin.cpp
@@ -1085,8 +1085,7 @@
                   "waitpid exiting pid from the pipe.  Will notify "
                   "as if parent process died with exit status -1.",
                   __FUNCTION__);
-    SetExitStatus(eExitTypeInvalid, -1, "failed to receive waitpid result",
-                  notify_status);
+    SetExitStatus(WaitStatus(WaitStatus::Exit, -1), notify_status);
     return error;
   }
 
@@ -1099,8 +1098,7 @@
                   "waitpid exit status from the pipe.  Will notify "
                   "as if parent process died with exit status -1.",
                   __FUNCTION__);
-    SetExitStatus(eExitTypeInvalid, -1, "failed to receive waitpid result",
-                  notify_status);
+    SetExitStatus(WaitStatus(WaitStatus::Exit, -1), notify_status);
     return error;
   }
 
@@ -1111,18 +1109,7 @@
                 __FUNCTION__, pid,
                 (pid == m_pid) ? "the inferior" : "not the inferior", status);
 
-  ExitType exit_type = eExitTypeInvalid;
-  int exit_status = -1;
-
-  if (WIFEXITED(status)) {
-    exit_type = eExitTypeExit;
-    exit_status = WEXITSTATUS(status);
-  } else if (WIFSIGNALED(status)) {
-    exit_type = eExitTypeSignal;
-    exit_status = WTERMSIG(status);
-  }
-
-  SetExitStatus(exit_type, exit_status, nullptr, notify_status);
+  SetExitStatus(WaitStatus::Decode(status), notify_status);
   return error;
 }
 
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
index a130472..e39a178 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
@@ -503,35 +503,9 @@
   return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts);
 }
 
-static ExitType convert_pid_status_to_exit_type(int status) {
-  if (WIFEXITED(status))
-    return ExitType::eExitTypeExit;
-  else if (WIFSIGNALED(status))
-    return ExitType::eExitTypeSignal;
-  else if (WIFSTOPPED(status))
-    return ExitType::eExitTypeStop;
-  else {
-    // We don't know what this is.
-    return ExitType::eExitTypeInvalid;
-  }
-}
-
-static int convert_pid_status_to_return_code(int status) {
-  if (WIFEXITED(status))
-    return WEXITSTATUS(status);
-  else if (WIFSIGNALED(status))
-    return WTERMSIG(status);
-  else if (WIFSTOPPED(status))
-    return WSTOPSIG(status);
-  else {
-    // We don't know what this is.
-    return ExitType::eExitTypeInvalid;
-  }
-}
-
 // Handles all waitpid events from the inferior process.
 void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited,
-                                         int signal, int status) {
+                                         WaitStatus status) {
   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
 
   // Certain activities differ based on whether the pid is the tid of the main
@@ -564,8 +538,7 @@
                          : "thread metadata not found",
             GetState());
         // The main thread exited.  We're done monitoring.  Report to delegate.
-        SetExitStatus(convert_pid_status_to_exit_type(status),
-                      convert_pid_status_to_return_code(status), nullptr, true);
+        SetExitStatus(status, true);
 
         // Notify delegate that our process has exited.
         SetState(StateType::eStateExited, true);
@@ -658,8 +631,7 @@
         // Notify the delegate - our process is not available but appears to
         // have been killed outside
         // our control.  Is eStateExited the right exit state in this case?
-        SetExitStatus(convert_pid_status_to_exit_type(status),
-                      convert_pid_status_to_return_code(status), nullptr, true);
+        SetExitStatus(status, true);
         SetState(StateType::eStateExited, true);
       } else {
         // This thread was pulled out from underneath us.  Anything to do here?
@@ -830,10 +802,8 @@
              data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(),
              is_main_thread);
 
-    if (is_main_thread) {
-      SetExitStatus(convert_pid_status_to_exit_type(data),
-                    convert_pid_status_to_return_code(data), nullptr, true);
-    }
+    if (is_main_thread)
+      SetExitStatus(WaitStatus::Decode(data), true);
 
     StateType state = thread.GetState();
     if (!StateIsRunningState(state)) {
@@ -2384,33 +2354,17 @@
       break;
     }
 
-    bool exited = false;
-    int signal = 0;
-    int exit_status = 0;
-    const char *status_cstr = nullptr;
-    if (WIFSTOPPED(status)) {
-      signal = WSTOPSIG(status);
-      status_cstr = "STOPPED";
-    } else if (WIFEXITED(status)) {
-      exit_status = WEXITSTATUS(status);
-      status_cstr = "EXITED";
-      exited = true;
-    } else if (WIFSIGNALED(status)) {
-      signal = WTERMSIG(status);
-      status_cstr = "SIGNALED";
-      if (wait_pid == static_cast<::pid_t>(GetID())) {
-        exited = true;
-        exit_status = -1;
-      }
-    } else
-      status_cstr = "(\?\?\?)";
+    WaitStatus wait_status = WaitStatus::Decode(status);
+    bool exited = wait_status.type == WaitStatus::Exit ||
+                  (wait_status.type == WaitStatus::Signal &&
+                   wait_pid == static_cast<::pid_t>(GetID()));
 
-    LLDB_LOG(log,
-             "waitpid (-1, &status, _) => pid = {0}, status = {1:x} "
-             "({2}), signal = {3}, exit_state = {4}",
-             wait_pid, status, status_cstr, signal, exit_status);
+    LLDB_LOG(
+        log,
+        "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}",
+        wait_pid, wait_status, exited);
 
-    MonitorCallback(wait_pid, exited, signal, exit_status);
+    MonitorCallback(wait_pid, exited, wait_status);
   }
 }
 
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
index 98fc88b..aa6fe44 100644
--- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
+++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
@@ -153,7 +153,7 @@
 
   static void *MonitorThread(void *baton);
 
-  void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
+  void MonitorCallback(lldb::pid_t pid, bool exited, WaitStatus status);
 
   void WaitForNewThread(::pid_t tid);
 
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
index efb19fc..cc8e337 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
+++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp
@@ -40,32 +40,6 @@
 using namespace lldb_private::process_netbsd;
 using namespace llvm;
 
-static ExitType convert_pid_status_to_exit_type(int status) {
-  if (WIFEXITED(status))
-    return ExitType::eExitTypeExit;
-  else if (WIFSIGNALED(status))
-    return ExitType::eExitTypeSignal;
-  else if (WIFSTOPPED(status))
-    return ExitType::eExitTypeStop;
-  else {
-    // We don't know what this is.
-    return ExitType::eExitTypeInvalid;
-  }
-}
-
-static int convert_pid_status_to_return_code(int status) {
-  if (WIFEXITED(status))
-    return WEXITSTATUS(status);
-  else if (WIFSIGNALED(status))
-    return WTERMSIG(status);
-  else if (WIFSTOPPED(status))
-    return WSTOPSIG(status);
-  else {
-    // We don't know what this is.
-    return ExitType::eExitTypeInvalid;
-  }
-}
-
 // Simple helper function to ensure flags are enabled on the given file
 // descriptor.
 static Status EnsureFDFlags(int fd, int flags) {
@@ -177,17 +151,15 @@
   }
 }
 
-void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, int signal,
-                                        int status) {
+void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) {
   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
 
-  LLDB_LOG(log, "got exit signal({0}) , pid = {1}", signal, pid);
+  LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid);
 
   /* Stop Tracking All Threads attached to Process */
   m_threads.clear();
 
-  SetExitStatus(convert_pid_status_to_exit_type(status),
-                convert_pid_status_to_return_code(status), nullptr, true);
+  SetExitStatus(status, true);
 
   // Notify delegate that our process has exited.
   SetState(StateType::eStateExited, true);
@@ -861,36 +833,21 @@
     LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error);
   }
 
-  bool exited = false;
-  int signal = 0;
-  int exit_status = 0;
-  const char *status_cstr = nullptr;
-  if (WIFSTOPPED(status)) {
-    signal = WSTOPSIG(status);
-    status_cstr = "STOPPED";
-  } else if (WIFEXITED(status)) {
-    exit_status = WEXITSTATUS(status);
-    status_cstr = "EXITED";
-    exited = true;
-  } else if (WIFSIGNALED(status)) {
-    signal = WTERMSIG(status);
-    status_cstr = "SIGNALED";
-    if (wait_pid == static_cast<::pid_t>(GetID())) {
-      exited = true;
-      exit_status = -1;
-    }
-  } else
-    status_cstr = "(\?\?\?)";
+  WaitStatus wait_status = WaitStatus::Decode(status);
+  bool exited = wait_status.type == WaitStatus::Exit ||
+                (wait_status.type == WaitStatus::Signal &&
+                 wait_pid == static_cast<::pid_t>(GetID()));
 
   LLDB_LOG(log,
-           "waitpid ({0}, &status, _) => pid = {1}, status = {2:x} "
-           "({3}), signal = {4}, exit_state = {5}",
-           GetID(), wait_pid, status, status_cstr, signal, exit_status);
+           "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}",
+           GetID(), wait_pid, status, exited);
 
   if (exited)
-    MonitorExited(wait_pid, signal, exit_status);
-  else
-    MonitorCallback(wait_pid, signal);
+    MonitorExited(wait_pid, wait_status);
+  else {
+    assert(wait_status == WaitStatus::Stop);
+    MonitorCallback(wait_pid, wait_status.status);
+  }
 }
 
 bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) {
diff --git a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
index 758956e..7a1303f 100644
--- a/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
+++ b/lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
@@ -123,7 +123,7 @@
   void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error);
 
   void MonitorCallback(lldb::pid_t pid, int signal);
-  void MonitorExited(lldb::pid_t pid, int signal, int status);
+  void MonitorExited(lldb::pid_t pid, WaitStatus status);
   void MonitorSIGSTOP(lldb::pid_t pid);
   void MonitorSIGTRAP(lldb::pid_t pid);
   void MonitorSignal(lldb::pid_t pid, int signal);
diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
index d318c35..d34a794 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
@@ -370,53 +370,23 @@
   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
 
   // send W notification
-  ExitType exit_type = ExitType::eExitTypeInvalid;
-  int return_code = 0;
-  std::string exit_description;
-
-  const bool got_exit_info =
-      process->GetExitStatus(&exit_type, &return_code, exit_description);
-  if (!got_exit_info) {
-    if (log)
-      log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
-                  ", failed to retrieve process exit status",
-                  __FUNCTION__, process->GetID());
+  auto wait_status = process->GetExitStatus();
+  if (!wait_status) {
+    LLDB_LOG(log, "pid = {0}, failed to retrieve process exit status",
+             process->GetID());
 
     StreamGDBRemote response;
     response.PutChar('E');
     response.PutHex8(GDBRemoteServerError::eErrorExitStatus);
     return SendPacketNoLock(response.GetString());
-  } else {
-    if (log)
-      log->Printf("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
-                  ", returning exit type %d, return code %d [%s]",
-                  __FUNCTION__, process->GetID(), exit_type, return_code,
-                  exit_description.c_str());
-
-    StreamGDBRemote response;
-
-    char return_type_code;
-    switch (exit_type) {
-    case ExitType::eExitTypeExit:
-      return_type_code = 'W';
-      break;
-    case ExitType::eExitTypeSignal:
-      return_type_code = 'X';
-      break;
-    case ExitType::eExitTypeStop:
-      return_type_code = 'S';
-      break;
-    case ExitType::eExitTypeInvalid:
-      return_type_code = 'E';
-      break;
-    }
-    response.PutChar(return_type_code);
-
-    // POSIX exit status limited to unsigned 8 bits.
-    response.PutHex8(return_code);
-
-    return SendPacketNoLock(response.GetString());
   }
+
+  LLDB_LOG(log, "pid = {0}, returning exit type {1}", process->GetID(),
+           *wait_status);
+
+  StreamGDBRemote response;
+  response.Format("{0:g}", *wait_status);
+  return SendPacketNoLock(response.GetString());
 }
 
 static void AppendHexValue(StreamString &response, const uint8_t *buf,