0

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

GetThreadCallStack (ThreadId)?

Eirik A.
  • 447
  • 1
  • 5
  • 11
  • 2
    Potential duplicate of [Get the call stack from EurekaLog at any time](https://stackoverflow.com/questions/31305666/get-the-call-stack-from-eurekalog-at-any-time) – AmigoJack Mar 05 '23 at 13:44
  • I've viewed topic 'Get the call stack from EurekaLog at any time', but could not find solution for mine needs: get current codepoint + call stack of the thread by ID – Dmitry Belkeivch Mar 05 '23 at 17:43

1 Answers1

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    "This argument is used to build call stack for another (external) thread" is a bad sentense. The real work is described in "Mark call stack items as belonging to the specified thread". E.g. the only thing this argument does is marking items as belonging to a thread, it does not actually perform thread's context capturing - that is what BuildForThread is for. – Alex Mar 06 '23 at 08:40
  • @Alex thanks. I have removed it – Remy Lebeau Mar 06 '23 at 15:46
  • Thank you very much! CallStack.BuildForThreadByID is what I need. – Dmitry Belkeivch Mar 06 '23 at 17:22