Get the EurekaLog call stack of a specified thread at any time

123 Views Asked by At

Is it possible to get current call stack of a thread with specified thread id without any exception?

GetThreadCallStack (ThreadId)?

1

There are 1 best solutions below

3
Remy Lebeau On

Per EurekaLog's documentation, How to... get background thread call stack?, the TEurekaCallStack class has a BuildForThread() method which you can pass a Thread handle and Thread ID to, eg:

procedure TForm1.Button1Click(Sender: TObject);
var
  CallStack: TEurekaBaseStackList;
  Suspended: Boolean;
begin
  CallStack := EurekaCallStackClass.Create(nil);
  try
    CallStack.BuildForThread(YourThreadHandle, YourThreadId, 
      // Replace with your thread's handle and ID.
      // Thread handle must have:
      // THREAD_GET_CONTEXT, THREAD_QUERY_INFORMATION, and THREAD_SUSPEND_RESUME access rights
      // DO NOT PASS GetCurrentThread and GetCurrentThreadId!!!
      // There is also .BuildForThreadByID method, which does not require thread handle argument.

      nil, // first addr
      [ddUnit, ddProcedure, ddSourceCode], // CurrentEurekaLogOptions.csoDebugInfo
      True, // get debug info?
      False, // show begin calls?
      TracerRawEurekaLogV7Mask, // CurrentEurekaLogOptions.csoAllowedRenderMethods
      True, // running thread?
      @Suspended); // will return True if thread was suspended before call stack is taken
 
    Memo1.Lines.Assign(CallStack);
  finally
    FreeAndNil(CallStack);
  end;
end;

That same page also says:

Important Note: please note that background thread will continue to run. E.g. its actual call stack may be different from call stack that you have taken.

You may also be interested in RaiseFreezeException routine from EFreeze unit, which will create report for the specified individual thread.