================
@@ -684,34 +685,59 @@ static bool 
GetMacOSXProcessUserAndGroup(ProcessInstanceInfo &process_info) {
   return false;
 }
 
-uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
-                                 ProcessInstanceInfoList &process_infos) {
-  std::vector<struct kinfo_proc> kinfos;
-
+/// Fetches the list of all processes. Returns true on success.
+[[nodiscard]] static bool
+GetProcessList(std::vector<struct kinfo_proc> &kinfos) {
   int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
 
-  size_t pid_data_size = 0;
-  if (::sysctl(mib, 3, nullptr, &pid_data_size, nullptr, 0) != 0)
-    return 0;
+  // This is an inherently racy API. We have to first query the size for our
+  // buffer and then pass back to sysctl. If more processes spawn between the
+  // size query and the actual call to fetch, then sysctl returns ENOMEM.
+  // We keep retrying until we get a passing result.
+  for (int attempt = 1; attempt < 1024; ++attempt) {
+    // Fetch the buffer size sysctl would return.
+    size_t current_pid_size = 0;
+    if (::sysctl(mib, 3, nullptr, &current_pid_size, nullptr, 0) != 0)
+      return false;
 
-  // Add a few extra in case a few more show up
-  const size_t estimated_pid_count =
-      (pid_data_size / sizeof(struct kinfo_proc)) + 10;
+    // We expect some process growth. Start small and keep increasing the
+    // expected growth each attempt until we can get the sysctl call to
+    // succeed.
+    const size_t expected_growth = attempt * 128 * sizeof(struct kinfo_proc);
----------------
JDevlieghere wrote:

Same here, replace the magic 128 constant with something like 
`g_buffer_entries` or something. 
```suggestion
    const size_t expected_growth = attempt * g_buffer_entries * sizeof(struct 
kinfo_proc);
```

https://github.com/llvm/llvm-project/pull/204109
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to