Ubuntu: How to get pthread_t from tid [was: AV when using pthread_getname_np()] lazarus / 23-Operating Systems ---------------------------------------------------------------------- Pascal · 2022-06-26 09:49 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function pthread_getname_np(__thread:pthread_t; __name:PAnsiChar; __len:size_t):cint;cdecl;external 'c' name 'pthread_getname_np';...function TDbgLinuxThread.GetName: String;var  n: AnsiString;begin  Result := '';  n := '                    ';  if pthread_getname_np(Handle, @n[1], 16) = 0 then begin    Result := n;  end;  if Result = '' then    Result := inherited GetName;end; I am using Ubuntu 22.04 LTS (in WSL2 on Windows 10 21H2 64bit). Any ideas? ---------------------------------------------------------------------- marcov · 2022-06-26 13:18 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  uses strutils;........ n:=DupeString(' ',16); if pthread..... ---------------------------------------------------------------------- PascalDragon · 2022-06-26 21:11 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 · 2022-06-27 04:48 Thanks for the hints, but Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TDbgLinuxThread.GetName: String;var  n: AnsiString;begin  Result := '';  SetLength(n, 31);  if pthread_getname_np(Handle, @(n[1]), 16) <> 0 then begin    Result := n;  end;  if Result = '' then    Result := inherited GetName;end;etName;end; didn't make a difference. Still AV. Maybe i have a problem here: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function pthread_getname_np(__thread:pthread_t; __name:PAnsiChar; __len:size_t):cint;cdecl;external 'c' name 'pthread_getname_np'; ---------------------------------------------------------------------- MarkMLl · 2022-06-27 06:36 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 ---------------------------------------------------------------------- Pascal · 2022-06-27 08:03 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? ---------------------------------------------------------------------- Pascal · 2022-06-27 09:42 Or should i use reading "/proc/[pid]/task/[tid]/comm"? ---------------------------------------------------------------------- Zvoni · 2022-06-27 09:56 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...... ---------------------------------------------------------------------- Pascal · 2022-06-27 10:14 Yes, right. The problem i have now is that i only have pid (process_id) and tid (task_id) but i need pthread_id! ---------------------------------------------------------------------- MarkMLl · 2022-06-27 10:22 Quote from: Pascal on June 27, 2022, 11:42:38 amOr 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 ---------------------------------------------------------------------- Zvoni · 2022-06-27 10:53 Quote from: Pascal on June 27, 2022, 12:14:26 pmYes, 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? ---------------------------------------------------------------------- Zvoni · 2022-06-27 10:56 Quote from: Pascal on June 27, 2022, 11:42:38 amOr should i use reading "/proc/[pid]/task/[tid]/comm"? From the same link: https://linux.die.net/man/3/pthread_getname_np QuoteNotes 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. ´ ---------------------------------------------------------------------- PascalDragon · 2022-06-27 11:24 Quote from: MarkMLl on June 27, 2022, 12:22:38 pmeven 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). Quote from: Zvoni on June 27, 2022, 12:53:31 pmQuote from: Pascal on June 27, 2022, 12:14:26 pmYes, 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 · 2022-06-27 12:09 Quote from: PascalDragon on June 27, 2022, 01:24:23 pmThere 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 ---------------------------------------------------------------------- Pascal · 2022-06-27 12:16 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...AddThread(Do_SysCall(syscall_nr_gettid)), threadid);... ---------------------------------------------------------------------- sstvmaster · 2022-06-27 12:32 Quote from: Pascal on June 27, 2022, 02:16:40 pmWell, 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/ ---------------------------------------------------------------------- Pascal · 2022-06-27 12:43 Uff, this makes no sense as i have to query the ThreadManager of the third party app. ---------------------------------------------------------------------- Pascal · 2022-06-27 13:20 Quote from: sstvmaster on June 27, 2022, 02:32:24 pmQuote from: Pascal on June 27, 2022, 02:16:40 pmWell, 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(). ---------------------------------------------------------------------- Zvoni · 2022-06-27 13:29 Quote from: Pascal on June 27, 2022, 03:20:53 pmQuote from: sstvmaster on June 27, 2022, 02:32:24 pmQuote from: Pascal on June 27, 2022, 02:16:40 pmWell, 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: QuoteQuote from: Pascal on June 27, 2022, 11:42:38 amOr should i use reading "/proc/[pid]/task/[tid]/comm"? From the same link: https://linux.die.net/man/3/pthread_getname_np QuoteNotes 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. ---------------------------------------------------------------------- Pascal · 2022-06-28 07:21 Quote from: Zvoni on June 27, 2022, 03:29:35 pmAnd you didn't read my answer: QuoteQuote from: Pascal on June 27, 2022, 11:42:38 amOr should i use reading "/proc/[pid]/task/[tid]/comm"? From the same link: https://linux.die.net/man/3/pthread_getname_np QuoteNotes 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. ---------------------------------------------------------------------- PascalDragon · 2022-06-28 11:40 Quote from: Pascal on June 27, 2022, 02:16:40 pmYes, 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. Quote from: Pascal on June 27, 2022, 02:16:40 pmWell, 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. Quote from: Pascal on June 27, 2022, 02:16:40 pmI am thinking of adding a lookup array to Threadmanager (rtl/unix/cthreads.pp) which is updated in CBeginThread. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...AddThread(Do_SysCall(syscall_nr_gettid)), threadid);... That would be rejected. ---------------------------------------------------------------------- Pascal · 2022-06-28 13:02 Quote from: PascalDragon on June 28, 2022, 01:40:45 pmQuote from: Pascal on June 27, 2022, 02:16:40 pmYes, 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. Quote from: PascalDragon on June 28, 2022, 01:40:45 pmQuote from: Pascal on June 27, 2022, 02:16:40 pmI am thinking of adding a lookup array to Threadmanager (rtl/unix/cthreads.pp) which is updated in CBeginThread. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...AddThread(Do_SysCall(syscall_nr_gettid)), threadid);... That would be rejected. Was rejected as the lookup array will only be available from the debugger process not the other process.