Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get LWP PID from NPTL thread_id

1,076 views
Skip to first unread message

Christian Hammers

unread,
Jan 10, 2008, 7:09:21 AM1/10/08
to
Hello

I try to debug a program that uses NPTL threads and would like to output the
threads "pid" like it is visible with "ps ax -L" under Linux to be able to
use "strace" and "gdb" with this threads.

The function pthread_self() seems to return a system created unique identifier
which does not equal to the shorter PID which seems to be reffered to as
"LWP PID" in some sources I googled.

There was a reference to "syscall(253);" or so but this did not look very
portable and moreover at the point where I can modify the program I only
have just a list of thread ids but am not in one of those threads myself.

Any idea where I can find a "pid_t get_lwpid_from_thread_id(pthread_t id)"?

bye,

-christian-

Marcin ‘Qrczak’ Kowalczyk

unread,
Jan 10, 2008, 8:06:42 AM1/10/08
to

Dnia 10-01-2008, Cz o godzinie 13:09 +0100, Christian Hammers pisze:

> Any idea where I can find a "pid_t get_lwpid_from_thread_id(pthread_t id)"?

There is Linux-specific gettid() for the current thread.

I don't know any official (even Linux-specific) way to obtain tid from
pthread_t. glibc-2.6.1/nptl/descr.h contains:

struct pthread
{
union
{
#if !TLS_DTV_AT_TP
/* This overlaps the TCB as used for TLS without threads (see tls.h). */
tcbhead_t header;
#else
struct
{
int multiple_threads;
int gscope_flag;
} header;
#endif

/* This extra padding has no special purpose, and this structure layout
is private and subject to change without affecting the official ABI.
We just have it here in case it might be convenient for some
implementation-specific instrumentation hack or suchlike. */
void *__padding[16];
};

/* This descriptor's link on the `stack_used' or `__stack_user' list. */
list_t list;

/* Thread ID - which is also a 'is this thread descriptor (and
therefore stack) used' flag. */
pid_t tid;

[...]

I think that pthread_t is a pointer to this structure. This means that
tid has offset 18 times pointer size (list_t contains two pointers),
and that this fact is not an official property of the ABI and may change
in other glibc versions.

--
__("< Marcin Kowalczyk
\__/ qrc...@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/

David Schwartz

unread,
Jan 12, 2008, 5:36:10 AM1/12/08
to
On Jan 10, 4:09 am, Christian Hammers <chamm...@netcologne.de> wrote:

> There was a reference to "syscall(253);" or so but this did not look very
> portable

Huh? That a thread has an identifier you can list in "ps" is not
portable. So why would you care that the way to get that identifier
isn't portable?

> and moreover at the point where I can modify the program I only
> have just a list of thread ids but am not in one of those threads myself.

Unfortunately, it's very complicated. You need to keep a signal
unblocked in all threads and set to go to your own signal handler in
the main thread before other threads are created. Then you can send
that signal to the thread whose identifier you want to get. Have the
signal handler do syscall(253) and stash the response where you can
get it. If you need synchronization, you have to use a semphore.

Perhaps a better solution is to wrap pthread_create and wrap the
thread's start function. The thread's start function can acquire a
lock and add the thread's ID and pthread_t to a linked list of
identifiers. You can use a TID destructor to remove it should the
thread go away.

DS

0 new messages