According to
GDB news pageJune 4th, 2017 - GDB 8.0: "Support for thread names on MS-Windows"
I believe the version of GDB that ships with Lazarus is old (2014?), so I don't think it supports MS thread naming.
Recompiling everything just to get SEH support to set thread names seems a bit too much. I decided to do it in assembly.
Someone already did something similar.
I used that code to do a quick test:
type
TThreadNameInfo =
record
RecType: DWord;
Name: pointer;//PAnsiChar;
ThreadID: DWord;
Flags: DWord;
end;
var
info: TThreadNameInfo;
procedure NameThreadForDebugging(const pName: pointer);
begin
info.RecType := $1000;
info.Name := pName;
info.ThreadID := $FFFFFFFF;
info.Flags := 0;
//try
Windows.RaiseException($406D1388, 0, SizeOf(info) div SizeOf(DWord), Align(PDWord(@info),8));
//except
//end;
end;
{$AsmMode intel}
function NameThread(vName:pointer):boolean;assembler;nostackframe;stdcall;
label
CLEANUP_label,next_instruction;
asm
PUSH EBP //set up function's stack frame
MOV EBP, ESP
CALL next_instruction //(next_instruction) //get EIP of next instruction (after 5-byte CALL) - MASM-syntax
next_instruction:
POP EAX //EAX now equals whatever memory location THIS instruction is loaded at
ADD EAX, 01Ch //apply relative offset so EAX now points to our exception-handler entry point
PUSH EAX //build EXCEPTION_REGISTRATION structure on stack, first with our handler
PUSH DWORD PTR FS:[0] // followed by previous handler in chain
MOV DWORD PTR FS:[0], ESP //install our exception_frame to be first in chain
MOV EAX, DWORD PTR SS:[EBP+8] //eax = pointer passed as argument #1
call NameThreadForDebugging
//MOV EAX, DWORD PTR DS:[EAX] //eax = *eax / dereference pointer - can we read DWORD memory?
XOR EAX, EAX //if we got here, no exception occurred, memory is readable, return zero (eax=0)
//JMP SHORT CLEANUP_label (0000000Fh) //skip past exception_handler code
JMP SHORT CLEANUP_label //skip past exception_handler code
XOR EAX, EAX //exception_handler entry point
INC EAX //return 1
MOV ESP, DWORD PTR FS:[0] // **** SWALLOW EXCEPTION BY ****
MOV ESP, DWORD PTR SS:[ESP] // **** RESTORING ESP ****
CLEANUP_label:
POP DWORD PTR FS:[0] //CLEANUP_label - restore previous exception handler
ADD ESP, 4 //remove our exception_handler from stack
POP EBP //restore caller's stack frame
RETN 4 //remove STDCALL argument and return from function
end;
I don't know if it works, as I don't have a debugger that supports it. Calling
NameThread(@s[1]); supposed to change the name of the current thread.
Edit:
OllyDbg 2.0 supports "Thread names (MS_VC_EXCEPTION)". Tested the code and it showed the name of the thread. Check attached image.