Recent

Author Topic: Ubuntu: How to get pthread_t from tid [was: AV when using pthread_getname_np()]  (Read 4716 times)

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #15 on: June 27, 2022, 02:32:24 pm »
Well, is there really no way to get pthread_t from tid?

And what about pthread_self: https://www.delftstack.com/howto/c/pthread-get-thread-id-in-c/
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Uff, this makes no sense as i have to query the ThreadManager of the third party app.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #17 on: June 27, 2022, 03:20:53 pm »
Well, is there really no way to get pthread_t from tid?

And what about pthread_self: https://www.delftstack.com/howto/c/pthread-get-thread-id-in-c/
This works inside the thread. But as i am debugging another process i need a function which converts the tid to a pthread_t to make a call to pthread_getname_np().
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #18 on: June 27, 2022, 03:29:35 pm »
Well, is there really no way to get pthread_t from tid?

And what about pthread_self: https://www.delftstack.com/howto/c/pthread-get-thread-id-in-c/
This works inside the thread. But as i am debugging another process i need a function which converts the tid to a pthread_t to make a call to pthread_getname_np().

And you didn't read my answer:
Quote
Or should i use reading "/proc/[pid]/task/[tid]/comm"?
From the same link: https://linux.die.net/man/3/pthread_getname_np
Quote
Notes

pthread_setname_np() internally writes to the thread specific comm file under /proc filesystem: /proc/self/task/[tid]/comm.
pthread_getname_np() retrieves it from the same location.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #19 on: June 28, 2022, 09:21:14 am »
And you didn't read my answer:
Quote
Or should i use reading "/proc/[pid]/task/[tid]/comm"?
From the same link: https://linux.die.net/man/3/pthread_getname_np
Quote
Notes

pthread_setname_np() internally writes to the thread specific comm file under /proc filesystem: /proc/self/task/[tid]/comm.
pthread_getname_np() retrieves it from the same location.
I've read and understood it. But my original goal was to use pthread_getname_np instead of /proc/[tid]/comm.
It's implemented that way in trunk now as i wasn't able to find a solution to get a pthread_id from an tid.

Thanks for your efforts to help.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #20 on: June 28, 2022, 01:40:45 pm »
Yes, right, it's all about debugging here as mentioned in initial post.

Ehm, no this isn't mentioned in your initial post. While there is a TDbgLinuxThread type in the code example this does in no way explain what you're trying to do. It could after all simply be some thread in your own application that has this name for providing information about other threads in the same application. Nothing hints at you trying to extend FpDebug.

Well, is there really no way to get pthread_t from tid?

No, there isn't because pthread_t is a type provided by the C library, not the kernel and an application might not use the C library to create its threads (e.g. by using the clone syscall directly). Thus your safest bet is the comm file as that is provided by the kernel.

I am thinking of adding a lookup array to Threadmanager (rtl/unix/cthreads.pp) which is updated in CBeginThread.

Code: Pascal  [Select][+][-]
  1. ...
  2. AddThread(Do_SysCall(syscall_nr_gettid)), threadid);
  3. ...

That would be rejected.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #21 on: June 28, 2022, 03:02:59 pm »
Yes, right, it's all about debugging here as mentioned in initial post.

Ehm, no this isn't mentioned in your initial post. While there is a TDbgLinuxThread type in the code example this does in no way explain what you're trying to do. It could after all simply be some thread in your own application that has this name for providing information about other threads in the same application. Nothing hints at you trying to extend FpDebug.

Well, not that clear, you are right.

I am thinking of adding a lookup array to Threadmanager (rtl/unix/cthreads.pp) which is updated in CBeginThread.

Code: Pascal  [Select][+][-]
  1. ...
  2. AddThread(Do_SysCall(syscall_nr_gettid)), threadid);
  3. ...

That would be rejected.

Was rejected as the lookup array will only be available from the debugger process not the other process.
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

 

TinyPortal © 2005-2018