================
@@ -7,17 +7,135 @@
 
//===----------------------------------------------------------------------===//
 
 #include "lldb/Host/Host.h"
+#include "lldb/Host/posix/Support.h"
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+#include "lldb/Utility/ProcessInfo.h"
 #include "lldb/Utility/Status.h"
+#include "llvm/BinaryFormat/XCOFF.h"
+#include <sys/proc.h>
+#include <sys/procfs.h>
 
+using namespace llvm;
+using namespace lldb;
 using namespace lldb_private;
 
+namespace {
+enum class ProcessState {
+  Unknown,
+  Dead,
+  DiskSleep,
+  Idle,
+  Paging,
+  Parked,
+  Running,
+  Sleeping,
+  TracedOrStopped,
+  Zombie,
+};
+}
+
+ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) {
+  ProcessInstanceInfo::timespec ts;
+  ts.tv_sec = t.tv_sec;
+  ts.tv_usec = t.tv_nsec / 1000; // nanos to micros
+  return ts;
+}
+
+static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo,
+                          ProcessState &State) {
+  struct pstatus pstatusData;
+  auto BufferOrError = getProcFile(pid, "status");
+  if (!BufferOrError)
+    return false;
+
+  std::unique_ptr<llvm::MemoryBuffer> StatusBuffer = std::move(*BufferOrError);
+  // Ensure there's enough data for psinfoData
+  if (StatusBuffer->getBufferSize() < sizeof(pstatusData))
+    return false;
+
+  std::memcpy(&pstatusData, StatusBuffer->getBufferStart(),
+              sizeof(pstatusData));
+  switch (pstatusData.pr_stat) {
+  case SIDL:
+    State = ProcessState::Idle;
+    break;
+  case SACTIVE:
+    State = ProcessState::Running;
+    break;
+  case SSTOP:
+    State = ProcessState::TracedOrStopped;
+    break;
+  case SZOMB:
+    State = ProcessState::Zombie;
+    break;
+  default:
+    State = ProcessState::Unknown;
+    break;
+  }
+  processInfo.SetIsZombie(State == ProcessState::Zombie);
+  processInfo.SetUserTime(convert(pstatusData.pr_utime));
+  processInfo.SetSystemTime(convert(pstatusData.pr_stime));
+  processInfo.SetCumulativeUserTime(convert(pstatusData.pr_cutime));
+  processInfo.SetCumulativeSystemTime(convert(pstatusData.pr_cstime));
+  return true;
+}
+
+static bool GetExePathAndIds(::pid_t pid, ProcessInstanceInfo &process_info) {
+  struct psinfo psinfoData;
+  auto BufferOrError = getProcFile(pid, "psinfo");
+  if (!BufferOrError)
+    return false;
+
+  std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = std::move(*BufferOrError);
+  // Ensure there's enough data for psinfoData
+  if (PsinfoBuffer->getBufferSize() < sizeof(psinfoData))
+    return false;
+
+  std::memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData));
+  llvm::StringRef PathRef(&(psinfoData.pr_psargs[0]));
----------------
labath wrote:

Is this guaranteed to be null-terminated (at least on linux, these kinds of 
fields do not get null-terminated when they're truncated). If it isn't, you may 
want to do something like `StringRef(psinfoData.pr_psargs, 
strnlen(psinfoData.pr_psargs, sizeof(psinfoData.pr_psargs))`

https://github.com/llvm/llvm-project/pull/134354
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to