https://github.com/mchoo7 updated 
https://github.com/llvm/llvm-project/pull/187976

>From 15b145f1899d9f7fdcb1c01b05ada5ce1627b2a4 Mon Sep 17 00:00:00 2001
From: Minsoo Choo <[email protected]>
Date: Mon, 23 Mar 2026 13:47:48 +0800
Subject: [PATCH 1/2] [lldb][Process/FreeBSDKernelCore] Fix thread ordering

Signed-off-by: Minsoo Choo <[email protected]>
---
 .../ProcessFreeBSDKernelCore.cpp              | 23 +++++++++++--------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
index d1a4a1ebc47d7..ac94cfe0de096 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
@@ -247,10 +247,8 @@ bool 
ProcessFreeBSDKernelCore::DoUpdateThreadList(ThreadList &old_thread_list,
     // https://cgit.freebsd.org/src/tree/sys/sys/param.h
     constexpr size_t fbsd_maxcomlen = 19;
 
-    // Iterate through a linked list of all processes. New processes are added
-    // to the head of this list. Which means that earlier PIDs are actually at
-    // the end of the list, so we have to walk it backwards. First collect all
-    // the processes in the list order.
+    // Iterate through a linked list of all processes then order incrementally
+    // by pid.
     std::vector<lldb::addr_t> process_addrs;
     if (lldb::addr_t allproc_addr = FindSymbol("allproc");
         allproc_addr != LLDB_INVALID_ADDRESS) {
@@ -259,12 +257,17 @@ bool 
ProcessFreeBSDKernelCore::DoUpdateThreadList(ThreadList &old_thread_list,
            proc = ReadPointerFromMemory(proc + offset_p_list, error))
         process_addrs.push_back(proc);
     }
-
-    // Processes are in the linked list in descending PID order, so we must 
walk
-    // them in reverse to get ascending PID order.
-    for (auto proc_it = process_addrs.rbegin(); proc_it != 
process_addrs.rend();
-         ++proc_it) {
-      lldb::addr_t proc = *proc_it;
+    std::sort(process_addrs.begin(), process_addrs.end(),
+              [&](lldb::addr_t a, lldb::addr_t b) {
+                Status err;
+                int32_t pid_a =
+                    ReadSignedIntegerFromMemory(a + offset_p_pid, 4, -1, err);
+                int32_t pid_b =
+                    ReadSignedIntegerFromMemory(b + offset_p_pid, 4, -1, err);
+                return pid_a < pid_b;
+              });
+
+    for (lldb::addr_t proc : process_addrs) {
       int32_t pid =
           ReadSignedIntegerFromMemory(proc + offset_p_pid, 4, -1, error);
       // process' command-line string

>From 598d637cc5b06885429ad28cb88e020ab3c41f65 Mon Sep 17 00:00:00 2001
From: Minsoo Choo <[email protected]>
Date: Tue, 24 Mar 2026 22:57:27 +0900
Subject: [PATCH 2/2] fixup! [lldb][Process/FreeBSDKernelCore] Fix thread
 ordering

Signed-off-by: Minsoo Choo <[email protected]>
---
 .../ProcessFreeBSDKernelCore.cpp              | 31 ++++++++++++-------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
index ac94cfe0de096..dd746f394bde6 100644
--- 
a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
+++ 
b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
@@ -248,26 +248,33 @@ bool 
ProcessFreeBSDKernelCore::DoUpdateThreadList(ThreadList &old_thread_list,
     constexpr size_t fbsd_maxcomlen = 19;
 
     // Iterate through a linked list of all processes then order incrementally
-    // by pid.
-    std::vector<lldb::addr_t> process_addrs;
+    // by pid. Though new processes are added to the head of this list, process
+    // ids may be reused as well. So we cannot rely on it being in a particular
+    // order.
+    std::vector<std::pair<lldb::addr_t, int32_t>> process_addrs;
     if (lldb::addr_t allproc_addr = FindSymbol("allproc");
         allproc_addr != LLDB_INVALID_ADDRESS) {
       for (lldb::addr_t proc = ReadPointerFromMemory(allproc_addr, error);
            proc != 0 && proc != LLDB_INVALID_ADDRESS && error.Success();
-           proc = ReadPointerFromMemory(proc + offset_p_list, error))
-        process_addrs.push_back(proc);
+           proc = ReadPointerFromMemory(proc + offset_p_list, error)) {
+        int32_t pid =
+            ReadSignedIntegerFromMemory(proc + offset_p_pid, 4, -1, error);
+        if (error.Fail())
+          return false;
+        process_addrs.emplace_back(proc, pid);
+      }
     }
+
+    if (error.Fail())
+      return false;
+
     std::sort(process_addrs.begin(), process_addrs.end(),
-              [&](lldb::addr_t a, lldb::addr_t b) {
-                Status err;
-                int32_t pid_a =
-                    ReadSignedIntegerFromMemory(a + offset_p_pid, 4, -1, err);
-                int32_t pid_b =
-                    ReadSignedIntegerFromMemory(b + offset_p_pid, 4, -1, err);
-                return pid_a < pid_b;
+              [](const std::pair<lldb::addr_t, int32_t> &a,
+                 const std::pair<lldb::addr_t, int32_t> &b) {
+                return a.second < b.second;
               });
 
-    for (lldb::addr_t proc : process_addrs) {
+    for (auto [proc, pid] : process_addrs) {
       int32_t pid =
           ReadSignedIntegerFromMemory(proc + offset_p_pid, 4, -1, error);
       // process' command-line string

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

Reply via email to