Recent

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

Pascal

  • Hero Member
  • *****
  • Posts: 932
I am trying to implement the ThreadNameForDebugging functionality but i do not get pthread_getname_np() working.
I always get an AV when using the funtion:

Code: Pascal  [Select][+][-]
  1. function pthread_getname_np(__thread:pthread_t; __name:PAnsiChar; __len:size_t):cint;cdecl;external 'c' name 'pthread_getname_np';
  2. ...
  3. function TDbgLinuxThread.GetName: String;
  4. var
  5.   n: AnsiString;
  6. begin
  7.   Result := '';
  8.   n := '                    ';
  9.   if pthread_getname_np(Handle, @n[1], 16) = 0 then begin
  10.     Result := n;
  11.   end;
  12.   if Result = '' then
  13.     Result := inherited GetName;
  14. end;

I am using Ubuntu 22.04 LTS (in WSL2 on Windows 10 21H2 64bit).

Any ideas?
« Last Edit: June 27, 2022, 02:18:03 pm by Pascal »
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11942
  • FPC developer.
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #1 on: June 26, 2022, 03:18:57 pm »
This is normal.  Ansistring are copy on write, so the assignment to the literal, is just letting the pointer point to the literal, which is subsequently overwritten.


Try
Code: Pascal  [Select][+][-]
  1.  
  2. uses strutils;
  3. ...
  4. ...
  5. ..
  6.  n:=DupeString(' ',16);
  7.  if pthread.....
  8.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #2 on: June 26, 2022, 11:11:20 pm »
Why don't you do SetLength(n, 16) (though it would be more correct to use SetLength(n, 31) as the man page for pthread_setname_np states that the maximum length is 31 Byte)? The function will even NUL-terminate the string for you, so it will have the right length after that...

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #3 on: June 27, 2022, 06:48:49 am »
Thanks for the hints,

but
Code: Pascal  [Select][+][-]
  1. function TDbgLinuxThread.GetName: String;
  2. var
  3.   n: AnsiString;
  4. begin
  5.   Result := '';
  6.   SetLength(n, 31);
  7.   if pthread_getname_np(Handle, @(n[1]), 16) <> 0 then begin
  8.     Result := n;
  9.   end;
  10.   if Result = '' then
  11.     Result := inherited GetName;
  12. end;etName;
  13. end;
didn't make a difference. Still AV.

Maybe i have a problem here:
Code: Pascal  [Select][+][-]
  1. function pthread_getname_np(__thread:pthread_t; __name:PAnsiChar; __len:size_t):cint;cdecl;external 'c' name 'pthread_getname_np';
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8032
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #4 on: June 27, 2022, 08:36:42 am »
Stepping back very slightly, are you sure that Handle contains what you expect... both in its being correctly initialised and being the right variable (i.e. usual scoping rules etc. when functions nest and/or objects inherit)?

There are so many things that have a handle in modern programming that that can be a real issue: I think it would be good practice to /always/ make that name more descriptive.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #5 on: June 27, 2022, 10:03:21 am »
That was also a point i just checked and realized that the Handle in TDbgLinuxThread is not the handle which was used in pthread_setname_np!

So the new question is: How to obtain the pthread_t handle from the process/thread number which is used in TDbgLinuxThread.Handle?
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 #6 on: June 27, 2022, 11:42:38 am »
Or should i use reading "/proc/[pid]/task/[tid]/comm"?
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #7 on: June 27, 2022, 11:56:17 am »
https://linux.die.net/man/3/pthread_getname_np
If i'm reading this right, the first argument of "pthread_getname_np" is the "Value" returned by "pthread_create" in its first argument (called "thread")
What you call Handle......
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 #8 on: June 27, 2022, 12:14:26 pm »
Yes, right. The problem i have now is that i only have pid (process_id) and tid (task_id) but i need pthread_id!
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8032
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #9 on: June 27, 2022, 12:22:38 pm »
Or should i use reading "/proc/[pid]/task/[tid]/comm"?

Try it, tell us what happens, but don't expect that to be an acceptable final solution.

I'm sure that you appreciate that stuff in /proc and /sys is used heavily when looking at a process "from the outside" during debugging etc., but what you need in this case is a way of recovering the tid at the point of creation... even if it needs the good offices of Sven or Marco to get it into the RTL.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #10 on: June 27, 2022, 12:53:31 pm »
Yes, right. The problem i have now is that i only have pid (process_id) and tid (task_id) but i need pthread_id!
Are you even calling "pthread_create" anywhere?
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

Zvoni

  • Hero Member
  • *****
  • Posts: 2741
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #11 on: June 27, 2022, 12:56:02 pm »
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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #12 on: June 27, 2022, 01:24:23 pm »
even if it needs the good offices of Sven or Marco to get it into the RTL.

There is nothing that needs to be implemented in the RTL here, because we're talking about debugging third party applications (Pascal didn't clarify this, but I remembered that they had asked about this functionality in FpDebug some months ago).

Yes, right. The problem i have now is that i only have pid (process_id) and tid (task_id) but i need pthread_id!
Are you even calling "pthread_create" anywhere?

No, because this is about debugging third party applications.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8032
Re: Ubuntu: AV when using pthread_getname_np()
« Reply #13 on: June 27, 2022, 02:09:32 pm »
There is nothing that needs to be implemented in the RTL here, because we're talking about debugging third party applications (Pascal didn't clarify this, but I remembered that they had asked about this functionality in FpDebug some months ago).

In that case use /proc etc. without embarrassment, it's what it's for and in the area of per-process data (i.e. as distinct from all the guff about filesystems etc.) is moderately standardised across unix variants.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Pascal

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

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

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. ...
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

 

TinyPortal © 2005-2018