Lazarus

Programming => Operating Systems => Topic started by: Pascal on June 26, 2022, 11:49:51 am

Title: Ubuntu: How to get pthread_t from tid [was: AV when using pthread_getname_np()]
Post by: Pascal on June 26, 2022, 11:49:51 am
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?
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: marcov 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.  
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: PascalDragon 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...
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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';
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: MarkMLl 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
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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?
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal on June 27, 2022, 11:42:38 am
Or should i use reading "/proc/[pid]/task/[tid]/comm"?
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Zvoni 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......
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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!
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: MarkMLl 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
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Zvoni 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?
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Zvoni 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. ยด
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: PascalDragon 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.
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: MarkMLl 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
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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. ...
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: sstvmaster 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/
Title: Re: Ubuntu: How to get pthread_t from tid [was: AV when using pthread_getname_np()]
Post by: Pascal on June 27, 2022, 02:43:24 pm
Uff, this makes no sense as i have to query the ThreadManager of the third party app.
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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().
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Zvoni 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.
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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.
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: PascalDragon 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.
Title: Re: Ubuntu: AV when using pthread_getname_np()
Post by: Pascal 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.
TinyPortal © 2005-2018