[Lldb-commits] [lldb] [lldb][Process/FreeBSDKernelCore] Fix thread ordering (PR #187976)

Minsoo Choo via lldb-commits lldb-commits at lists.llvm.org
Fri Apr 10 05:01:02 PDT 2026


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 <minsoochoo0122 at proton.me>
Date: Mon, 23 Mar 2026 13:47:48 +0800
Subject: [PATCH 1/4] [lldb][Process/FreeBSDKernelCore] Fix thread ordering

Signed-off-by: Minsoo Choo <minsoochoo0122 at proton.me>
---
 .../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 b2a670bb1701bb0c4b569873782deab25eac7c56 Mon Sep 17 00:00:00 2001
From: Minsoo Choo <minsoochoo0122 at proton.me>
Date: Tue, 24 Mar 2026 22:57:27 +0900
Subject: [PATCH 2/4] fixup! [lldb][Process/FreeBSDKernelCore] Fix thread
 ordering

Signed-off-by: Minsoo Choo <minsoochoo0122 at proton.me>
---
 .../ProcessFreeBSDKernelCore.cpp              | 33 +++++++++++--------
 1 file changed, 19 insertions(+), 14 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..92889b1771123 100644
--- a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
@@ -248,28 +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) {
-      int32_t pid =
-          ReadSignedIntegerFromMemory(proc + offset_p_pid, 4, -1, error);
+    for (auto [proc, pid] : process_addrs) {
       // process' command-line string
       char comm[fbsd_maxcomlen + 1];
       ReadCStringFromMemory(proc + offset_p_comm, comm, sizeof(comm), error);

>From 16a29d576d51abcd7c4ebb2d3f65dbbb440395b2 Mon Sep 17 00:00:00 2001
From: Minsoo Choo <minsoochoo0122 at proton.me>
Date: Wed, 25 Mar 2026 10:13:38 +0900
Subject: [PATCH 3/4] fixup! [lldb][Process/FreeBSDKernelCore] Fix thread
 ordering

Signed-off-by: Minsoo Choo <minsoochoo0122 at proton.me>
---
 .../FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp       | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
index 92889b1771123..a1dee722fe6ee 100644
--- a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
@@ -255,7 +255,7 @@ bool ProcessFreeBSDKernelCore::DoUpdateThreadList(ThreadList &old_thread_list,
     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();
+           error.Success() && proc != 0 && proc != LLDB_INVALID_ADDRESS;
            proc = ReadPointerFromMemory(proc + offset_p_list, error)) {
         int32_t pid =
             ReadSignedIntegerFromMemory(proc + offset_p_pid, 4, -1, error);
@@ -263,10 +263,9 @@ bool ProcessFreeBSDKernelCore::DoUpdateThreadList(ThreadList &old_thread_list,
           return false;
         process_addrs.emplace_back(proc, pid);
       }
-    }
-
-    if (error.Fail())
+    } else {
       return false;
+    }
 
     std::sort(process_addrs.begin(), process_addrs.end(),
               [](const std::pair<lldb::addr_t, int32_t> &a,

>From 136a875bf5bfb499df9c3ee354f62fcedec5a87a Mon Sep 17 00:00:00 2001
From: Minsoo Choo <minsoochoo0122 at proton.me>
Date: Fri, 10 Apr 2026 20:57:49 +0900
Subject: [PATCH 4/4] fixup! [lldb][Process/FreeBSDKernelCore] Fix thread
 ordering

Signed-off-by: Minsoo Choo <minsoochoo0122 at proton.me>
---
 .../Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
index a1dee722fe6ee..639e68f4ef072 100644
--- a/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD-Kernel-Core/ProcessFreeBSDKernelCore.cpp
@@ -268,10 +268,7 @@ bool ProcessFreeBSDKernelCore::DoUpdateThreadList(ThreadList &old_thread_list,
     }
 
     std::sort(process_addrs.begin(), process_addrs.end(),
-              [](const std::pair<lldb::addr_t, int32_t> &a,
-                 const std::pair<lldb::addr_t, int32_t> &b) {
-                return a.second < b.second;
-              });
+              [](const auto &a, const auto &b) { return a.second < b.second; });
 
     for (auto [proc, pid] : process_addrs) {
       // process' command-line string



More information about the lldb-commits mailing list