Download raw body.
extend ptrace(2) PT_GET_THREAD_* to include thread names
On Thu, Dec 11, 2025 at 12:37:52AM +0100, Jeremie Courreges-Anglas wrote:
> On Wed, Dec 10, 2025 at 04:41:27PM +0000, kurt@intricatesoftware.com wrote:
> > Instead of gdb using sysctl(2) to get thread names let's extend
> > ptrace(2) PT_GET_THREAD_* to include thread names. This allows
> > gdb to use ptrace for both thread names and thread is alive
> > detection.
> >
> > I'm using a new define larger then _MAXCOMLEN to avoid that
> > define and header from propagating in to ptrace.h as well.
> >
> > The diff for gdb to use this and remove sysctl use follows
> > below as well. This would be committed a few days after the
> > pthread change is committed.
> >
> > okay for both?
>
> Works for me and looks correct, ok jca@
>
> I got curious and looked at what it would take to support both the old
> and new structure sizes, and the diff below appears to work fine. The
> issue is more of a problem on slower architectures where kernel and
> packages can stay unsynchronized for a longer time. Thoughts?
The previous diff was unnecessarily complicated.
Index: kern/sys_process.c
===================================================================
RCS file: /home/cvs/src/sys/kern/sys_process.c,v
diff -u -p -r1.106 sys_process.c
--- kern/sys_process.c 17 Feb 2025 15:45:55 -0000 1.106
+++ kern/sys_process.c 11 Dec 2025 00:42:18 -0000
@@ -85,6 +85,10 @@ int ptrace_kstate(struct proc *, int, pi
int global_ptrace; /* [a] permit tracing of not children */
+/* Compat, delete after 7.8 */
+struct optrace_thread_state {
+ pid_t pts_pid;
+};
/*
* Process debugging system call.
@@ -104,6 +108,7 @@ sys_ptrace(struct proc *p, void *v, regi
void *kaddr = NULL; /* kernelspace */
int data = SCARG(uap, data);
union {
+ struct optrace_thread_state u_opts;
struct ptrace_thread_state u_pts;
struct ptrace_io_desc u_piod;
struct ptrace_event u_pe;
@@ -144,12 +149,18 @@ sys_ptrace(struct proc *p, void *v, regi
break;
case PT_GET_THREAD_FIRST:
mode = OUT;
- size = sizeof u.u_pts;
+ if (data == sizeof u.u_opts)
+ size = sizeof u.u_opts;
+ else
+ size = sizeof u.u_pts;
kstate = 1;
break;
case PT_GET_THREAD_NEXT:
mode = IN_OUT;
- size = sizeof u.u_pts;
+ if (data == sizeof u.u_opts)
+ size = sizeof u.u_opts;
+ else
+ size = sizeof u.u_pts;
kstate = 1;
break;
case PT_GET_EVENT_MASK:
@@ -605,8 +616,10 @@ ptrace_kstate(struct proc *p, int req, p
if (t == NULL)
pts->pts_tid = -1;
- else
+ else {
pts->pts_tid = t->p_tid + THREAD_PID_OFFSET;
+ strlcpy(pts->pts_name, t->p_name, sizeof(pts->pts_name));
+ }
return 0;
}
}
Index: sys/ptrace.h
===================================================================
RCS file: /home/cvs/src/sys/sys/ptrace.h,v
diff -u -p -r1.16 ptrace.h
--- sys/ptrace.h 16 Mar 2020 11:58:46 -0000 1.16
+++ sys/ptrace.h 10 Dec 2025 20:42:35 -0000
@@ -82,8 +82,11 @@ typedef struct ptrace_state {
#define PT_GET_THREAD_FIRST 15
#define PT_GET_THREAD_NEXT 16
+#define PT_PTS_NAMELEN 32 /* must be >= sizeof(p_name) in struct proc */
+
struct ptrace_thread_state {
pid_t pts_tid;
+ char pts_name[PT_PTS_NAMELEN];
};
#define PT_FIRSTMACH 32 /* for machine-specific requests */
--
jca
extend ptrace(2) PT_GET_THREAD_* to include thread names