Discussion:
How to get a thread ID?
Václav Haisman
2010-06-03 08:06:11 UTC
Permalink
Hi,
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does not
identify the thread well enough. GDB on FreeBSD seems to know about threads
and does not seem to use the same ID as is pthread_t.

--
VH
Dan Nelson
2010-06-03 14:44:52 UTC
Permalink
Post by Václav Haisman
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does
not identify the thread well enough. GDB on FreeBSD seems to know about
threads and does not seem to use the same ID as is pthread_t.
The return value of pthread_self() is a pointer to the (private) "struct
pthread" for the current thread, and should uniquely identify a thread. Do
you have a testcase that shows otherwise? GDB might just enumerate the
currently active threads starting from 1.
--
Dan Nelson
***@allantgroup.com
Kostik Belousov
2010-06-03 15:19:15 UTC
Permalink
Post by Dan Nelson
Post by Václav Haisman
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does
not identify the thread well enough. GDB on FreeBSD seems to know about
threads and does not seem to use the same ID as is pthread_t.
The return value of pthread_self() is a pointer to the (private) "struct
pthread" for the current thread, and should uniquely identify a thread. Do
you have a testcase that shows otherwise? GDB might just enumerate the
currently active threads starting from 1.
There is thr_self(2) undocumented syscall:
int thr_self(long *id);
Václav Haisman
2010-06-03 19:16:28 UTC
Permalink
Post by Kostik Belousov
Post by Dan Nelson
Post by Václav Haisman
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does
not identify the thread well enough. GDB on FreeBSD seems to know about
threads and does not seem to use the same ID as is pthread_t.
The return value of pthread_self() is a pointer to the (private) "struct
pthread" for the current thread, and should uniquely identify a thread. Do
you have a testcase that shows otherwise? GDB might just enumerate the
currently active threads starting from 1.
int thr_self(long *id);
Thanks, I'll try it. Is the returned ID the LWP ID that GDB shows?

--
VH
pluknet
2010-06-03 19:55:21 UTC
Permalink
Post by Václav Haisman
Post by Kostik Belousov
Post by Dan Nelson
Post by Václav Haisman
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()?  Something like gettid() on
Linux?  Apparently, on FreeBSD the pthread_t is a pointer type and does
not identify the thread well enough.  GDB on FreeBSD seems to know about
threads and does not seem to use the same ID as is pthread_t.
The return value of pthread_self() is a pointer to the (private) "struct
pthread" for the current thread, and should uniquely identify a thread.  Do
you have a testcase that shows otherwise?  GDB might just enumerate the
currently active threads starting from 1.
int thr_self(long *id);
Thanks, I'll try it. Is the returned ID the LWP ID that GDB shows?
thr_self() does its work as well as ddb and procstat do: using td->td_tid.
--
wbr,
pluknet
Daniel Eischen
2010-06-03 14:51:43 UTC
Permalink
Post by Václav Haisman
Hi,
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does not
identify the thread well enough. GDB on FreeBSD seems to know about threads
and does not seem to use the same ID as is pthread_t.
"identifies a thread" well enough for what? pthread_t is suppose
to be opaque. Whether it is an int, pointer, or whatever, it is
implementation-defined and not suppose provide any more information
than available through the standard pthread interfaces. There are
some non-portable interfaces in <pthread_np.h> though.
--
DE
Václav Haisman
2010-06-03 19:15:17 UTC
Permalink
Post by Daniel Eischen
Post by Václav Haisman
Hi,
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does not
identify the thread well enough. GDB on FreeBSD seems to know about threads
and does not seem to use the same ID as is pthread_t.
"identifies a thread" well enough for what? pthread_t is suppose
to be opaque. Whether it is an int, pointer, or whatever, it is
implementation-defined and not suppose provide any more information
than available through the standard pthread interfaces. There are
some non-portable interfaces in <pthread_np.h> though.
Exactly, pthread_t is opaque and it thus it is only by a chance that I can
print its value, it is a pointer on FreeBSD. It could as well be a struct and
then I could not possibly do anything to print the thread identity on screen.

* 4 Thread 28426ec0 (LWP 100172) 0x0804b9f6 in TestThread::run ()

As shown above, GDB can show some sort of ID, the LWP bit. How does it get
it? Is it possible to get the LWP ID from inside the process without
debugging it?
Daniel Eischen
2010-06-03 20:09:31 UTC
Permalink
Post by Václav Haisman
Post by Daniel Eischen
Post by Václav Haisman
Hi,
is it possible to obtain some sort of a thread ID that identifies a thread
within a process other than pthread_self()? Something like gettid() on
Linux? Apparently, on FreeBSD the pthread_t is a pointer type and does not
identify the thread well enough. GDB on FreeBSD seems to know about threads
and does not seem to use the same ID as is pthread_t.
"identifies a thread" well enough for what? pthread_t is suppose
to be opaque. Whether it is an int, pointer, or whatever, it is
implementation-defined and not suppose provide any more information
than available through the standard pthread interfaces. There are
some non-portable interfaces in <pthread_np.h> though.
Exactly, pthread_t is opaque and it thus it is only by a chance that I can
print its value, it is a pointer on FreeBSD. It could as well be a struct and
then I could not possibly do anything to print the thread identity on screen.
* 4 Thread 28426ec0 (LWP 100172) 0x0804b9f6 in TestThread::run ()
As shown above, GDB can show some sort of ID, the LWP bit. How does it get
it? Is it possible to get the LWP ID from inside the process without
debugging it?
I think the value that it is showing (28426ec0) is the hex
value of the pthread_t (pointer). You can also use
pthread_[gs]etname_np() from <pthread_np.h> if you want
to associate an string identifier with a thread.
--
DE
Loading...