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?

1
  • Did you look in the documentation? Jul 9, 2015 at 5:08

2 Answers 2

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;
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.