1

There are many posts on SO about getting the call stack for exception handling, and we have EurekaLog for handling that, but I really want to be able to get the stack at any point during runtime, just as you can if you put a breakpoint in the IDE.

An event somewhere in the legacy code is causing a function to execute that is doing something it's not supposed to do. While we can see from the debug output the name of the function, we can't tell what actually called it without the stack trace. It's not an exception and we don't want to raise an exception in that function just so EurekaLog can fire.

Is there a way to get the call stack without any exception?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SiBrit
  • 1,460
  • 12
  • 39

2 Answers2

7

You do not need to raise an exception to get a stack trace. Call EurekaLog's GetTracer() function to get a TEurekaBaseStackList object, and then call its Build() method to get the stack trace. Here is the example provided in EurekaLog's documentation:

var
  CallStack: TEurekaBaseStackList;
begin
  CallStack := GetTracer(TracerWindows);
  try
    // Build current call stack including current execution point
    CallStack.Build(CallStack.GetCurrentInstruction);
    // ... use CallStack somehow
  finally
    FreeAndNil(CallStack);
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

From help: Use GetCurrentCallStack function from ECallStack unit:

uses
  ECallStack; // for TEurekaBaseStackList and GetCurrentCallStack

procedure TForm1.Button1Click(Sender: TObject);
var
  CallStack: TEurekaBaseStackList;
begin
  CallStack := GetCurrentCallStack;
  // You can also use other functions from ECallStack unit
  try
    Memo1.Lines.Assign(CallStack);
    // You can also use: 
    // CallStack.ToString - to convert call stack to String
    // CallStackToString(s) routines to customize textual formatting
  finally
    FreeAndNil(CallStack);
  end;
end;

P.S. You may also want to consider using EurekaLog's logging routines.

Alex
  • 5,477
  • 2
  • 36
  • 56