Orwell
Orwell

Reputation: 1518

Delphi obtain stack trace after exception

I'm trying to figure out how to obtain a stack trace after an exception is thrown in Delphi. However, when I try to read the stack in the Application.OnException event using the function below, the stack already seems to be flushed and replaced by the throwing procedures.

function GetStackReport: AnsiString;
var
    retaddr, walker: ^pointer;
begin

    // ...

    // History of stack, ignore esp frame
    asm
        mov walker, ebp
    end;

    // assume return address is present above ebp
    while Cardinal(walker^) <> 0 do begin
        retaddr := walker;
        Inc(retaddr);
        result := result + AddressInfo(Cardinal(retaddr^));
        walker := walker^;
    end;
end;

Here's what kind of results I'm getting:

001A63E3: TApplication.HandleException (Forms)
00129072: StdWndProc (Classes)
001A60B0: TApplication.ProcessMessage (Forms)

That's obviously not what I'm looking for, although it's correct. I'd like to retrieve the stack as it was just before the exception was thrown, or in other words the contents before (after would do too) the OnException call.

Is there any way to do that?

I am aware that I'm reinventing the wheel, because the folks over at madExcept/Eurekalog/jclDebug have already done this, but I'd like to know how it's done.

Upvotes: 13

Views: 16168

Answers (3)

Walter Verhoeven
Walter Verhoeven

Reputation: 4421

I agree with David, and I understand where you are coming from as dependencies on 3rd party are always risky "can't afford to lose it, can't afford to have it"

(I like to share code; I wrote this using Delphi 12, your implementation may differ)

There is nothing wrong with madExcept, but it's not the only one. Have a look at another Paid tool EurekaLog:

Here is the relevant documentation: https://www.eurekalog.com/help/eurekalog/index.php?how_to_convert_call_stack_to_text.php

Or a free version via GetIt Jedi, or get the code in GitHub. Here is how you manually get the call stack:

procedure LogException(const E: Exception);
var
  msg: string;
  StackTrace: string;
begin
   StackTrace := '';
  if not IsNullOrEmpty(E.StackTrace) then
    StackTrace := E.StackTrace
  else
  begin
    {$IFDEF EUREKALOG}
       {  Eurecalog is a bit of a puzzle but one needs following using statement
          for the code to compile:
          IsEurekaLogInstalled() -> ExceptionLog7 or EBase
          IsEurekaLogActive() -> ExceptionLog7 or EBase
          GetTracer(int) ->  ECallStack
          CallStackToString(statce,header) -> ECallStack
          TCompactStackFormatter -> ECallStack
          TracerFramesEurekaLogV7 -> const with the value of 6 in EStackTracing
          https://www.eurekalog.com/help/eurekalog/how_to_convert_call_stack_to_text.php
       }
      if IsEurekaLogInstalled() and  IsEurekaLogActive() and IsNullOrEmpty(StackTrace) then
      begin
        var callStack := GetTracer(TracerFramesEurekaLogV7);
        var formatter:= TCompactStackFormatter.Create;
        try
          CallStack.Build(CallStack.GetCurrentInstruction);
          //use the stack and covert it to a simple string
          StackTrace := CallStackToString(callStack,'Callastack:',formatter);

        finally
          FreeAndNil(callStack);
          FreeAndNil(formatter);
        end;
      end;
   {$ENDIF}
   {$IFDEF JclDebugEnabled }
     if IsNullOrEmpty(StackTrace) then
     begin
       var list := TStringList.Create;
       try
          JclLastExceptStackListToStrings(list,True, True, True, False);
          StackTrace := list.Text;
       finally
          FreeAndNil(list);
       end;
     end;
     {$ENDIF}

  end;

  msg := Format('%s: %s on %s', [E.ClassName, E.Message,StackTrace]);
  Log(msg, etException);//Quick Logger https://blogs.embarcadero.com/quick-logger-is-a-powerful-enterprise-grade-asynchronous-logger-for-delphi/
end;
 

I define if I use JEDI or EurekaLog using a compiler directive in the source of a .dpr file, you could do it in the project options UI as well:

{$DEFINE JclDebugEnabled}

The same goes for EurekaLog; however, the EurekaLog wizard does it for you: enter image description here

I did not write the code for re-use, but one could take the parts and make it more integration-friendly.

Here is a nice link to help those who like to understand jclDebug https://blog.dummzeuch.de/2014/03/08/using-jcldebug/ as jclDebug needs to be enabled; from the link:

initialization
  // Enable raw mode (default mode uses stack frames which aren't always generated by the compiler)
  Include(JclStackTrackingOptions, stRawMode);
  // Disable stack tracking in dynamically loaded modules (it makes stack tracking code a bit faster)
  Include(JclStackTrackingOptions, stStaticModuleList);
  // Initialize Exception tracking
  JclStartExceptionTracking;
finalization
  JclStopExceptionTracking;
end.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612934

I'd like to retrieve the stack as it was just before the exception was thrown, or in other words the contents before (after would do too) the OnException call.

Actually, you don't want the stack before the OnException call. That's what you've already got. You want the stack at the point at which the exception was raised. And that requires the stack tracing to happen ASAP after the raise. It's too late in the OnException call because the exception has propagated all the way to the top-level handler.

madExcept works by hooking all the RTL functions that handle exceptions. And it hooks the lowest level functions. This takes some serious effort to bring about. With these routines hooked the code can capture stack traces and so on. Note that the hooking is version specific and requires reverse engineering of the RTL.

What's more the stack walking is very much more advanced than your basic code. I don't mean that in a derogatory way, it's just that stack walking on x86 is a tricky business and the madExcept code is very well honed.

That's the basic idea. If you want to learn more then you can obtain the source code of JclDebug for free. Or buy madExcept and get its source.

Upvotes: 10

Remy Lebeau
Remy Lebeau

Reputation: 595971

It is not possible to manually obtain a viable stack trace from inside the OnException event. As you have already noticed, the stack at the time of the error is already gone by the time that event is triggered. What you are looking for requires obtaining the stack trace at the time the exception is raised. Third-party exception loggers, like MadExcept, EurekaLog, etc handle those details for you by hooking into key functions and core exception handlers inside of the RTL itself.

In recent Delphi versions, the SysUtils.Exception class does have public StackTrace and StackInfo properties now, which would be useful in the OnException event except for the fact that Embarcadero has chosen NOT to implement those properties natively for unknown reasons. It requires third-party exception loggers to assign handlers to various callbacks exposed by the Exception class to generate stack trace data for the properties. But if you have JclDebug installed, for instance, then you could provide your own callback handlers in your own code that use JCL's stack tracing functions to generate the stack data for the properties.

Upvotes: 22

Related Questions