Skip to content

[lldb] [Process/FreeBSDKernel] List threads in correct order#178306

Merged
DavidSpickett merged 1 commit intollvm:mainfrom
mchoo7:print
Feb 5, 2026
Merged

[lldb] [Process/FreeBSDKernel] List threads in correct order#178306
DavidSpickett merged 1 commit intollvm:mainfrom
mchoo7:print

Conversation

@mchoo7
Copy link
Contributor

@mchoo7 mchoo7 commented Jan 27, 2026

In FreeBSD, allproc is a prepend list and new processes are appeneded at head. This results in reverse pid order, so we first need to order pid incrementally then print threads according to the correct order.

Before:

Process 0 stopped
* thread #1: tid = 101866, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff8015882f780, flags=259) at sched_ule.c:2448:26, name = '(pid 12991) dtrace'
  thread #2: tid = 101915, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80158825780, flags=259) at sched_ule.c:2448:26, name = '(pid 11509) zsh'
  thread #3: tid = 101942, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80142599000, flags=259) at sched_ule.c:2448:26, name = '(pid 11504) ftcleanup'
  thread #4: tid = 101545, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80131898000, flags=259) at sched_ule.c:2448:26, name = '(pid 5599) zsh'
  thread #5: tid = 100905, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80131899000, flags=259) at sched_ule.c:2448:26, name = '(pid 5598) sshd-session'
  thread #6: tid = 101693, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff8015886e780, flags=259) at sched_ule.c:2448:26, name = '(pid 5595) sshd-session'
  thread #7: tid = 101626, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801588be000, flags=259) at sched_ule.c:2448:26, name = '(pid 5592) sh'
...

After:

(lldb) thread list
Process 0 stopped
* thread #1: tid = 100000, 0xffffffff80bf9322 kernel`sched_switch(td=0xffffffff81abe840, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel'
  thread #2: tid = 100035, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d9780, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_0'
  thread #3: tid = 100036, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d9000, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_1'
  thread #4: tid = 100037, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d8780, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_2'
  thread #5: tid = 100038, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d8000, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_3'
  thread #6: tid = 100039, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d7780, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_4'
  thread #7: tid = 100040, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d7000, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_5'
...

@mchoo7 mchoo7 requested a review from JDevlieghere as a code owner January 27, 2026 22:19
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the lldb label Jan 27, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 27, 2026

@llvm/pr-subscribers-lldb

Author: Minsoo Choo (mchoo7)

Changes

In FreeBSD, allproc is a prepend list and new processes are appeneded at head. This results in reverse pid order, so we first need to order pid incrementally then print threads according to the correct order.

Before:

Process 0 stopped
* thread #<!-- -->1: tid = 101866, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff8015882f780, flags=259) at sched_ule.c:2448:26, name = '(pid 12991) dtrace'
  thread #<!-- -->2: tid = 101915, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80158825780, flags=259) at sched_ule.c:2448:26, name = '(pid 11509) zsh'
  thread #<!-- -->3: tid = 101942, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80142599000, flags=259) at sched_ule.c:2448:26, name = '(pid 11504) ftcleanup'
  thread #<!-- -->4: tid = 101545, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80131898000, flags=259) at sched_ule.c:2448:26, name = '(pid 5599) zsh'
  thread #<!-- -->5: tid = 100905, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff80131899000, flags=259) at sched_ule.c:2448:26, name = '(pid 5598) sshd-session'
  thread #<!-- -->6: tid = 101693, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff8015886e780, flags=259) at sched_ule.c:2448:26, name = '(pid 5595) sshd-session'
  thread #<!-- -->7: tid = 101626, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801588be000, flags=259) at sched_ule.c:2448:26, name = '(pid 5592) sh'
...

After:

(lldb) thread list
Process 0 stopped
* thread #<!-- -->1: tid = 100000, 0xffffffff80bf9322 kernel`sched_switch(td=0xffffffff81abe840, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel'
  thread #<!-- -->2: tid = 100035, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d9780, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_0'
  thread #<!-- -->3: tid = 100036, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d9000, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_1'
  thread #<!-- -->4: tid = 100037, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d8780, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_2'
  thread #<!-- -->5: tid = 100038, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d8000, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_3'
  thread #<!-- -->6: tid = 100039, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d7780, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_4'
  thread #<!-- -->7: tid = 100040, 0xffffffff80bf9322 kernel`sched_switch(td=0xfffff801052d7000, flags=259) at sched_ule.c:2448:26, name = '(pid 0) kernel/softirq_5'
...

Full diff: https://github.com/llvm/llvm-project/pull/178306.diff

1 Files Affected:

  • (modified) lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp (+11-3)
diff --git a/lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp b/lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp
index d209e5b5384f3..43a875f5c541e 100644
--- a/lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp
+++ b/lldb/source/Plugins/Process/FreeBSDKernel/ProcessFreeBSDKernel.cpp
@@ -183,13 +183,21 @@ bool ProcessFreeBSDKernel::DoUpdateThreadList(ThreadList &old_thread_list,
     // from FreeBSD sys/param.h
     constexpr size_t fbsd_maxcomlen = 19;
 
-    // iterate through a linked list of all processes
-    // allproc is a pointer to the first list element, p_list field
-    // (found at offset_p_list) specifies the next element
+    // pid is in reverse order. Order it incrementally
+    std::vector<lldb::addr_t> process_addrs;
     for (lldb::addr_t proc =
              ReadPointerFromMemory(FindSymbol("allproc"), error);
          proc != 0 && proc != LLDB_INVALID_ADDRESS;
          proc = ReadPointerFromMemory(proc + offset_p_list, error)) {
+      process_addrs.push_back(proc);
+    }
+
+    // iterate through a linked list of all processes
+    // allproc is a pointer to the first list element, p_list field
+    // (found at offset_p_list) specifies the next element
+    for (auto proc_it = process_addrs.rbegin(); proc_it != process_addrs.rend();
+         ++proc_it) {
+      lldb::addr_t proc = *proc_it;
       int32_t pid =
           ReadSignedIntegerFromMemory(proc + offset_p_pid, 4, -1, error);
       // process' command-line string

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's more logical to list them in ascending order. Code is fine, just some comments on the comments.

As with the other PR, please get the existing tests running and see what the results are. I think they either do check this detail, or should, and should be extended to do so.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more nits since you're touching these lines :-)

In FreeBSD, allproc is a prepend list and new processes are appeneded at
head. This results in reverse pid order, so we first need to order pid
incrementally then print threads according to the correct order.

Signed-off-by: Minsoo Choo <minsoochoo0122@proton.me>
@aokblast
Copy link
Contributor

Please use fixup instead of force push. See: https://llvm.org/docs/GitHub.html#updating-pull-requests

@mchoo7
Copy link
Contributor Author

mchoo7 commented Jan 31, 2026

Please use fixup instead of force push. See: https://llvm.org/docs/GitHub.html#updating-pull-requests

Sorry... I already lost previous commits (I run git gc prune regularly). I'll do that next time.

@DavidSpickett
Copy link
Collaborator

On testing: we discussed this on another PR and the situation is that because the coredumps are so large, they were generated by patching LLDB to make them smaller. The minidump format tests don't need that, I don't think. They are also only run when you've got specific libraries installed, which none of our usual CI machines do. So if the results drift, it's not going to impact anyone but FreeBSD users.

So I personally am ok with @mchoo7 doing these changes and then updating the tests later, and maybe we can figure out a better way for re-generating that doesn't involve patching lldb.

I hope others agree with that.

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM assuming no one objects to the test plan, or lack of, for the time being.

@mchoo7
Copy link
Contributor Author

mchoo7 commented Feb 2, 2026

ping @emaste

@emaste
Copy link
Member

emaste commented Feb 4, 2026

So I personally am ok with @mchoo7 doing these changes and then updating the tests later, and maybe we can figure out a better way for re-generating that doesn't involve patching lldb.

Yeah, this situation is a bit unfortunate, but I agree this patch improves LLDB on FreeBSD and shouldn't affect the test results (as run in and reported by CI). Hopefully we (FreeBSD LLVM community) can work on improving CI.

@DavidSpickett DavidSpickett merged commit 5349c66 into llvm:main Feb 5, 2026
10 checks passed
@github-actions
Copy link

github-actions bot commented Feb 5, 2026

@mchoo7 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants