ptrace and threads (libthr)

Kostik Belousov kostikbel at
Fri Jun 17 21:10:18 UTC 2011

On Thu, Jun 16, 2011 at 08:30:19PM -0500, John Schumacher wrote:
> Does anybody know if FreeBSD allow for ptracing to be done on threads ex.
Yes, there are people who know. Then answer is contained in the
file sys/kern/sys_process.c.

> I recall this being impossible before,
> but maybe this has changed now. Is there a viable alternative to getting
> around this?
>   if (ptrace (PT_ATTACH, ikipp->ki_tid, 0, 0) != 0)
>   {
>       fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", tid,strerror
> (errno), errno);
>       fflush (stderr);
>   }
>  if (ptrace (PT_SUSPEND, ikipp->ki_tid, 0, 0) != 0)
>  {
>       fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n",tid,strerror
> (errno), errno);
>       fflush (stderr);
>  }
> both yield:
> Cannot suspend lwp 100167: Device busy (16)
> Cannot attach to lwp 100167: Device busy (16)
> In my understanding how ptrace works, the thread or process to be traced
> must be 'stopped'. Is there a way to pause threads to allow an attach to
> occur(SIGSTOP)? Is thread the correct term in this case? Or is anything the
> kernel can manipulate via signals and pt_methods technically a
> light-weight-process or kernel thread 'mapped' to that user-thread
> (pthread)? This is FreeBSD 8.1 with the libthr library.

The ptrace(2) requires the debugger to be attached to debuggee. The
attachment can only happen at the process level, you cannot attach to

After the attachment is done, operations like PT_SUSPEND, PT_CONTINUE,
PT_STEP etc operate on the lwps, and not on the process. This is somewhat
obscured by two issues.

First is the mention of the "pid" in the man page. In fact, pid may be
process identifier, in which case the kernel will select some random
thread in the target process, or it may be an lwp id, in the later case
the exact lwp is acted upon.

The namespace of the pid and lwpid do not cross. You can obtain the
list of the debugee lwps with PT_GETLWPLIST.

Second issue is that for the singlethreaded process, the only random
thread the kernel can select is the only process thread.

So the man page needs an update to be exact on the scope of the action,
either the process or lwp.  Anybody volunteer to do this ?

> Thanks !
> -- 
> John Schumacher
> University of Minnesota (TC)
> Electrical Engineering
> _______________________________________________
> freebsd-threads at mailing list
> To unsubscribe, send any mail to "freebsd-threads-unsubscribe at"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url :

More information about the freebsd-threads mailing list