Forum > Operating Systems

Ubuntu: How to get pthread_t from tid [was: AV when using pthread_getname_np()]

(1/5) > >>

Pascal:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- uses strutils;........ n:=DupeString(' ',16); if pthread..... 

PascalDragon:
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:
Thanks for the hints,

but

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function pthread_getname_np(__thread:pthread_t; __name:PAnsiChar; __len:size_t):cint;cdecl;external 'c' name 'pthread_getname_np';

MarkMLl:
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

Navigation

[0] Message Index

[#] Next page

Go to full version