如何在EExternal异常期间获取EXCEPTION_POINTERS?

Ian*_*oyd 9 delphi minidump exception-handling dbghelp delphi-5

我如何得到EXCEPTION_POINTERS,即:

  • PEXCEPTION_RECORD
  • PCONTEXT

EExternal异常期间的数据?

背景

当Windows抛出异常时,它会传递一个PEXCEPTION_POINTERS; 指向异常信息的指针:

typedef struct _EXCEPTION_POINTERS {
   PEXCEPTION_RECORD ExceptionRecord;
   PCONTEXT          ContextRecord;
} EXCEPTION_POINTERS, *PEXCEPTION_POINTERS;
Run Code Online (Sandbox Code Playgroud)

Delphi抛出EExternal异常时,它只包含一半的信息,PEXCEPTION_RECORD唯一的:

EExternal = class(Exception)
public
  ExceptionRecord: PExceptionRecord;
end;
Run Code Online (Sandbox Code Playgroud)

EExternal例外情况下,如何同时获得两者?

示例用法

我正在尝试使用MiniDumpWriteDumpDelphi中的函数编写Minidump .

该函数有一些可选参数:

function MiniDumpWriteDump(
    hProcess: THandle; //A handle to the process for which the information is to be generated.
    ProcessID: DWORD; //The identifier of the process for which the information is to be generated.
    hFile: THandle; //A handle to the file in which the information is to be written.
    DumpType: MINIDUMP_TYPE; //The type of information to be generated.
    {in, optional}ExceptionParam: PMinidumpExceptionInformation; //A pointer to a MINIDUMP_EXCEPTION_INFORMATION structure describing the client exception that caused the minidump to be generated.
    {in, optional}UserStreamParam: PMinidumpUserStreamInformation;
    {in, optional}CallbackParam: PMinidumpCallbackInformation): Boolean;
Run Code Online (Sandbox Code Playgroud)

在基本级别,我可以省略三个可选参数:

MiniDumpWriteDump(
    GetCurrentProcess(), 
    GetCurrentProcessId(),
    hFileHandle,
    nil,  //PMinidumpExceptionInformation
    nil,
    nil);
Run Code Online (Sandbox Code Playgroud)

它成功了.缺点是minidump缺少异常信息.使用第4个miniExceptionInfo参数(可选)传递该信息:

TMinidumpExceptionInformation = record
    ThreadId: DWORD;
    ExceptionPointers: PExceptionPointers;
    ClientPointers: BOOL;
end;
PMinidumpExceptionInformation = ^TMinidumpExceptionInformation;
Run Code Online (Sandbox Code Playgroud)

这很好,除了我需要一种方法来获取EXCEPTION_POINTERSWindows在异常发生时提供的内容.

TExceptionPointers结构包含两个成员:

EXCEPTION_POINTERS = record
   ExceptionRecord : PExceptionRecord;
   ContextRecord : PContext;
end;
Run Code Online (Sandbox Code Playgroud)

我知道Delphi的EExternal例外是所有"Windows"异常的基础,它包含所需的PExceptionRecord:

EExternal = class(Exception)
public
  ExceptionRecord: PExceptionRecord;
end;
Run Code Online (Sandbox Code Playgroud)

但它不包含相关的ContextRecord.

是不是PEXCEPTION_RECORD不够好?

如果我试图传递EXCEPTION_POINTERSMiniDumpWriteDump,留下ContextRecord零:

procedure TDataModule1.ApplicationEvents1Exception(Sender: TObject; E: Exception);
var
   ei: TExceptionPointers;
begin
   if (E is EExternal) then
   begin
      ei.ExceptionRecord := EExternal(E).ExceptionRecord;
      ei.ContextRecord := nil;
      GenerateDump(@ei);
   end;

   ...
end;

function GenerateDump(exceptionInfo: PExceptionPointers): Boolean;
var
   miniEI: TMinidumpExceptionInformation;
begin
   ...

   miniEI.ThreadID := GetCurrentThreadID();
   miniEI.ExceptionPointers := exceptionInfo;
   miniEI.ClientPointers := True;

   MiniDumpWriteDump(
       GetCurrentProcess(), 
       GetCurrentProcessId(),
       hFileHandle,
       @miniEI,  //PMinidumpExceptionInformation
       nil,
       nil);
end;
Run Code Online (Sandbox Code Playgroud)

然后该函数失败并出现错误 0x8007021B

只完成了ReadProcessMemory或WriteProcessMemory请求的一部分

怎么样SetUnhandledExceptionFilter

你为什么不使用SetUnhandledExceptionFilter并得到你需要的指针?

SetUnhandledExceptionFilter(@DebugHelpExceptionFilter);

function DebugHelpExceptionFilter(const ExceptionInfo: TExceptionPointers): Longint; stdcall;
begin
   GenerateDump(@ExceptionInfo);
   Result := 1;  //1 = EXCEPTION_EXECUTE_HANDLER
end;
Run Code Online (Sandbox Code Playgroud)

问题在于,如果异常未经过滤,则未经过滤的异常处理程序才会启动.因为这是Delphi,因为我处理异常:

procedure DataModule1.ApplicationEvents1Exception(Sender: TObject; E: Exception);
var
    ei: TExceptionPointers;
begin
    if (E is EExternal) then
    begin
       //If it's EXCEPTION_IN_PAGE_ERROR then we have to terminate *now*
       if EExternal(E).ExceptionRecord.ExceptionCode = EXCEPTION_IN_PAGE_ERROR then
       begin
           ExitProcess(1);
           Exit;
       end;

       //Write minidump
       ...
    end;

    {$IFDEF SaveExceptionsToDatabase}
    SaveExceptionToDatabase(Sender, E);
    {$ENDIF}

    {$IFDEF ShowExceptionForm}
    ShowExceptionForm(Sender, E);
    {$ENDIF}
end;
Run Code Online (Sandbox Code Playgroud)

应用程序不会,也不希望它,以WER故障终止.

我要如何在EXCEPTION_POINTERS过程中EExternal

注意:您可以忽略背景中的所有内容.这是不必要的填充设计,让我看起来更聪明.

先发制人的Snarky Heffernan评论:你应该停止使用Delphi 5.

奖金阅读

500*_*ror 7

由于Delphi RTL没有直接暴露上下文指针但只提取异常指针并且在System的内容中这样做,因此解决方案将特定于您正在使用的Delphi版本.

我已经安装了Delphi 5已经有一段时间了,但我确实有Delphi 2007,我相信Delphi 5和Delphi 2007之间的概念基本保持不变.

考虑到这一点,这是一个如何为Delphi 2007完成它的例子:

program Sample;

{$APPTYPE CONSOLE}

uses
  Windows,
  SysUtils;


var
  SaveGetExceptionObject : function(P: PExceptionRecord):Exception;

// we show just the content of the general purpose registers in this example
procedure DumpContext(Context: PContext);
begin
  writeln('eip:', IntToHex(Context.Eip, 8));
  writeln('eax:', IntToHex(Context.Eax, 8));
  writeln('ebx:', IntToHex(Context.Ebx, 8));
  writeln('ecx:', IntToHex(Context.Ecx, 8));
  writeln('edx:', IntToHex(Context.Edx, 8));
  writeln('esi:', IntToHex(Context.Esi, 8));
  writeln('edi:', IntToHex(Context.Edi, 8));
  writeln('ebp:', IntToHex(Context.Ebp, 8));
  writeln('esp:', IntToHex(Context.Esp, 8));
end;

// Below, we redirect the ExceptObjProc ptr to point to here
// When control reaches here we locate the context ptr on
// stack, call the dump procedure, and then call the original ptr
function HookGetExceptionObject(P: PExceptionRecord):Exception;
var
  Context: PContext;
begin
  asm
    // This +44 value is likely to differ on a Delphi 5 setup, but probably
    // not by a lot. To figure out what value you should use, set a
    // break-point here, then look in the stack in the CPU window for the
    // P argument value on stack, and the Context pointer should be 8 bytes
    // (2 entries) above that on stack.
    // Note also that the 44 is sensitive to compiler switches, calling
    // conventions, and so on.
    mov eax, [esp+44]
    mov Context, eax
  end;
  DumpContext(Context);
  Result := SaveGetExceptionObject(P);
end;

var
  dvd, dvs, res: double; // used to force a div-by-zero error
begin
  dvd := 1; dvs := 0;
  SaveGetExceptionObject := ExceptObjProc;
  ExceptObjProc := @HookGetExceptionObject;
  try
    asm
      // this is just for register context verification
      // - don't do this in production
      mov esi, $BADF00D5;
    end;
    // cause a crash
    res := dvd / dvs;
    writeln(res);
  except
    on E:Exception do begin
      Writeln(E.Classname, ': ', E.Message);
      Readln;
    end;
  end;
end.
Run Code Online (Sandbox Code Playgroud)

  • +1干得好.我没有意识到挂钩ExceptObjProc是多么容易. (2认同)
  • @IanBoyd必须在引发异常的时刻捕获上下文.操作系统为您做到了这一点.并传递给你.然后RTL忽略它.所以我认为这种挂钩方法是唯一可行的解​​决方案.我认为更难的原因是我做的不仅仅是这个.它挂钩了`ExceptObjProc`机制,并允许用户仍然使用该机制.相当漂亮. (2认同)