在Delphi Win32应用程序中显示调用堆栈

Jos*_*ons 16 delphi error-handling

我想在Delphi 2007(Win32)的错误对话框中显示堆栈跟踪.

理想情况下,我喜欢这样的事情:

try
  //do something
except on e : exception do
  begin
    //rollback a transaction or whatever i need to do here       
    MessageDlg('An error has occurred!' + #13#10 +
                e.Message + #13#10 +
               'Here is the stack trace:' + #13#10 +
               e.StackTrace,mtError,[mbOK],0);
  end;  //except
end;  /try-except
Run Code Online (Sandbox Code Playgroud)

并且输出与IDE中的调用堆栈类似:

MYPROGRAM.SomeFunction
MYPROGRAM.SomeProcedure
MYPROGRAM.MYPROGRAM
:7c817067 kernel32.RegisterWaitForInputIdle + 0x49
Run Code Online (Sandbox Code Playgroud)

gab*_*abr 23

madExcept有一个方法StackTrace(在单元madStackTrace中)可以做到这一点.

JEDI代码库在JclDebug单元中提供类似的功能.

  • madExcept不能免费用于商业用途.JclDebug是免费和开源的.还有EurekaLog(http://www.eurekalog.com/) (5认同)
  • 为了逐步使用JEDI的JclDebug,我发现这很有用,http://robstechcorner.blogspot.com/2009/04/finding-hard-to-reproduce-errors.html [简单步骤部分] (2认同)

Mar*_*der 8

我们使用Exceptional Magic,它对我们非常有用.有了它你可以做这样的事情:

try
    raise Exception.Create('Something bad happened...');
except
    on e: Exception do begin
        CallStack := TStringList.Create;
        try
            ExceptionHook.LogException; // Logs call stack
            ExceptionHook.CallStack.Dump(CallStack);
            ShowMessage(CallStack.Text);
        finally
            CallStack.Free;
            end;
        end;
    end;
Run Code Online (Sandbox Code Playgroud)

这会产生一个非常详细的调用堆栈:

Exception 'Exception' in module BOAppTemplate.exe at 003F3C36
Something bad happened...

Module: BOAppUnit, Source: BOAppUnit.pas, Line 66
Procedure: MyProcedure

Call stack:
:007F4C36 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 66)
:7C812AFB [kernel32.dll]
:007F4C36 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 66)
:00404DF4 [BOAppTemplate.exe] System::__linkproc__ AfterConstruction
Recursive call (2 times):
:007F4C36 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 66)
:007F4CE6 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 79)
:007F4D22 [BOAppTemplate.exe] Boappunit::TBOAppForm::Button1Click (BOAppUnit.pas, line 82)
:004604C2 [BOAppTemplate.exe] Controls::TControl::Click
:004487FB [BOAppTemplate.exe] Stdctrls::TButton::Click
:004488F9 [BOAppTemplate.exe] Stdctrls::TButton::CNCommand
:0045FFBA [BOAppTemplate.exe] Controls::TControl::WndProc
Run Code Online (Sandbox Code Playgroud)

Exceptional Magic只有25美元没有来源,所以它相对便宜.希望有所帮助!