Index | Thread | Search

From:
Kurt Miller <kurt@intricatesoftware.com>
Subject:
[devel/gdb] Add support for displaying thread names
To:
jca@wxcvbn.org, pascal@stumpf.co, ports@openbsd.org
Date:
Thu, 4 Dec 2025 00:16:30 +0000

Download raw body.

Thread
  • Kurt Miller:

    [devel/gdb] Add support for displaying thread names

Add support for displaying thread names with info threads for running processes.
Modeled after the NetBSD implementation of the same function.

Example below of a java process. okay?

(gdb) info threads
  Id   Target Id                                                Frame 
* 1    thread 400422 of process 71831 "GC Thread#8"             futex () at /tmp/-:3
  2    thread 448414 of process 71831 ""                        futex () at /tmp/-:3
  3    thread 202535 of process 71831 ""                        futex () at /tmp/-:3
  4    thread 591164 of process 71831 "GC Thread#0"             futex () at /tmp/-:3
  5    thread 363279 of process 71831 "G1 Main Marker"          futex () at /tmp/-:3
  6    thread 203968 of process 71831 "G1 Conc#0"               futex () at /tmp/-:3
  7    thread 466655 of process 71831 "G1 Refine Control"       futex () at /tmp/-:3
  8    thread 573472 of process 71831 "G1 Refinement Workers#0" futex () at /tmp/-:3
  9    thread 172118 of process 71831 "G1 Service"              futex () at /tmp/-:3
  10   thread 492718 of process 71831 "VM Periodic Task Thread" futex () at /tmp/-:3
  11   thread 173634 of process 71831 "VM Thread"               futex () at /tmp/-:3
  12   thread 126483 of process 71831 "Reference Handler"       futex () at /tmp/-:3
  13   thread 448290 of process 71831 "Finalizer"               futex () at /tmp/-:3
  14   thread 590005 of process 71831 "Signal Dispatcher"       futex () at /tmp/-:3
  15   thread 546031 of process 71831 "Service Thread"          futex () at /tmp/-:3
  16   thread 155745 of process 71831 "Monitor Deflation Threa" futex () at /tmp/-:3
  17   thread 225195 of process 71831 "C2 CompilerThread0"      futex () at /tmp/-:3
  18   thread 341622 of process 71831 "C1 CompilerThread0"      futex () at /tmp/-:3
  19   thread 594614 of process 71831 "Notification Thread"     futex () at /tmp/-:3
  20   thread 148839 of process 71831 "Common-Cleaner"          futex () at /tmp/-:3
  21   thread 154447 of process 71831 "MainThread"              futex () at /tmp/-:3
  22   thread 575277 of process 71831 "VirtualThread-unblocker" futex () at /tmp/-:3
  23   thread 457994 of process 71831 "GC Thread#1"             futex () at /tmp/-:3
  24   thread 255483 of process 71831 "GC Thread#2"             futex () at /tmp/-:3
  25   thread 380901 of process 71831 "GC Thread#3"             futex () at /tmp/-:3
  26   thread 579163 of process 71831 "G1 Conc#1"               futex () at /tmp/-:3
  27   thread 104453 of process 71831 "GC Thread#4"             futex () at /tmp/-:3
  28   thread 214043 of process 71831 "GC Thread#5"             futex () at /tmp/-:3
  29   thread 288448 of process 71831 "GC Thread#6"             futex () at /tmp/-:3
  30   thread 585144 of process 71831 "GC Thread#7"             futex () at /tmp/-:3
(gdb)

Index: Makefile
===================================================================
RCS file: /cvs/ports/devel/gdb/Makefile,v
diff -u -p -u -r1.97 Makefile
--- Makefile	14 Jun 2025 11:25:54 -0000	1.97
+++ Makefile	3 Dec 2025 23:56:57 -0000
@@ -2,6 +2,7 @@ COMMENT=	GNU debugger
 CATEGORIES=	devel
 
 DISTNAME=	gdb-16.3
+REVISION=	0
 
 HOMEPAGE=	https://www.gnu.org/software/gdb/
 
Index: patches/patch-gdb_obsd-nat_c
===================================================================
RCS file: patches/patch-gdb_obsd-nat_c
diff -N patches/patch-gdb_obsd-nat_c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-gdb_obsd-nat_c	3 Dec 2025 23:56:57 -0000
@@ -0,0 +1,89 @@
+Add support for thread_name.
+
+Index: gdb/obsd-nat.c
+--- gdb/obsd-nat.c.orig
++++ gdb/obsd-nat.c
+@@ -23,11 +23,13 @@
+ 
+ #include <sys/types.h>
+ #include <sys/ptrace.h>
++#include <sys/sysctl.h>
+ #include "gdbsupport/gdb_wait.h"
+ 
+ #include "inf-ptrace.h"
+ #include "obsd-nat.h"
+ #include "gdbsupport/eintr.h"
++#include "gdbsupport/function-view.h"
+ 
+ /* OpenBSD 5.2 and later include rthreads which uses a thread model
+    that maps userland threads directly onto kernel threads in a 1:1
+@@ -183,4 +185,69 @@ int
+ obsd_nat_target::remove_fork_catchpoint (int pid)
+ {
+   return 0;
++}
++
++/* Generic thread lister within a specified PID.  The CALLBACK
++   parameters is a C++ function that is called for each detected thread.
++   When the CALLBACK function returns true, the iteration is interrupted.
++
++   This function assumes internally that the queried process is stopped
++   and the number of threads does not change between two sysctl () calls.  */
++
++static bool
++obsd_thread_lister (const pid_t pid,
++		      gdb::function_view<bool (const struct kinfo_proc *)>
++		      callback)
++{
++  int mib[6] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_SHOW_THREADS,
++      pid, sizeof(struct kinfo_proc), 0};
++  size_t size;
++
++  if (sysctl (mib, ARRAY_SIZE (mib), NULL, &size, NULL, 0) == -1 || size == 0)
++    perror_with_name (("sysctl"));
++
++  mib[5] = size / sizeof (struct kinfo_proc);
++
++  gdb::unique_xmalloc_ptr<struct kinfo_proc[]> ki
++    ((struct kinfo_proc *) xcalloc (mib[5], sizeof (struct kinfo_proc)));
++
++  if (sysctl (mib, ARRAY_SIZE (mib), ki.get (), &size, NULL, 0) == -1
++      || size == 0)
++    perror_with_name (("sysctl"));
++
++  for (size_t i = 0; i < size / sizeof (struct kinfo_proc); i++)
++    {
++      struct kinfo_proc *l = &ki[i];
++      if (callback (l))
++	return true;
++    }
++
++  return false;
++}
++
++/* See obsd-nat.h.  */
++
++const char *
++obsd_nat_target::thread_name (struct thread_info *thr)
++{
++  pid_t pid = thr->ptid.pid ();
++  ptid_t::lwp_type tid = thr->ptid.lwp ();
++
++  static char buf[KI_MAXCOMLEN] = {};
++
++  auto fn
++    = [=] (const struct kinfo_proc *ki)
++      {
++	if (ki->p_tid == tid)
++	  {
++	    xsnprintf (buf, sizeof buf, "%s", ki->p_name);
++	    return true;
++	  }
++	return false;
++      };
++
++  if (obsd_thread_lister (pid, fn))
++    return buf;
++  else
++    return NULL;
+ }
Index: patches/patch-gdb_obsd-nat_h
===================================================================
RCS file: patches/patch-gdb_obsd-nat_h
diff -N patches/patch-gdb_obsd-nat_h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ patches/patch-gdb_obsd-nat_h	3 Dec 2025 23:56:57 -0000
@@ -0,0 +1,13 @@
+Add support for thread_name.
+
+Index: gdb/obsd-nat.h
+--- gdb/obsd-nat.h.orig
++++ gdb/obsd-nat.h
+@@ -27,6 +27,7 @@ class obsd_nat_target : public inf_ptrace_target
+   /* Override some methods to support threads.  */
+   std::string pid_to_str (ptid_t) override;
+   void update_thread_list () override;
++  const char *thread_name (struct thread_info *thr) override;
+   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
+ 
+   void follow_fork (inferior *inf, ptid_t, target_waitkind, bool, bool) override;