【发布时间】:2011-06-20 17:45:32
【问题描述】:
我想使用 JCLDebug 记录所有引发的异常,包括已处理的异常。
有可能吗?
【问题讨论】:
我想使用 JCLDebug 记录所有引发的异常,包括已处理的异常。
有可能吗?
【问题讨论】:
看看JclHookExcept单元中的JclAddExceptNotifier。
【讨论】:
它不是基于 JCL,但它是完全开源的,适用于 Delphi 5 到 XE。
这个logging mechanism 能够拦截任何异常。
事实上,从 Delphi 6 开始,您可以在 RtlUnwindProc 中定义一个全局过程,以便在引发任何异常时启动:
{$ifdef DELPHI5OROLDER}
procedure RtlUnwind; external kernel32 name 'RtlUnwind';
{$else}
var
oldUnWindProc: pointer;
{$endif}
procedure SynRtlUnwind(TargetFrame, TargetIp: pointer;
ExceptionRecord: PExceptionRecord; ReturnValue: Pointer); stdcall;
asm
pushad
cmp byte ptr SynLogExceptionEnabled,0
jz @oldproc
mov eax,TargetFrame
mov edx,ExceptionRecord
call LogExcept
@oldproc:
popad
pop ebp // hidden push ebp at asm level
{$ifdef DELPHI5OROLDER}
jmp RtlUnwind
{$else}
jmp oldUnWindProc
{$endif}
end;
oldUnWindProc := RTLUnwindProc;
RTLUnwindProc := @SynRtlUnwind;
此代码将启动以下功能:
type
PExceptionRecord = ^TExceptionRecord;
TExceptionRecord = record
ExceptionCode: DWord;
ExceptionFlags: DWord;
OuterException: PExceptionRecord;
ExceptionAddress: PtrUInt;
NumberParameters: Longint;
case {IsOsException:} Boolean of
True: (ExceptionInformation : array [0..14] of PtrUInt);
False: (ExceptAddr: PtrUInt; ExceptObject: Exception);
end;
GetExceptionClass = function(const P: TExceptionRecord): ExceptClass;
const
cDelphiExcept = $0EEDFAE0;
cDelphiException = $0EEDFADE;
procedure LogExcept(stack: PPtrUInt; const Exc: TExceptionRecord);
begin
LastError := GetLastError;
(...) intercept the exception
SetLastError(LastError); // code above could have changed this
end;
对于Delphi 5,我had to patch the VCL in-process,因为没有全局异常拦截器。
【讨论】: