Delphi - 在OSX上获得完整堆栈跟踪

时间:2014-04-13 05:35:29

标签: macos delphi exception posix mach

我有一个可以记录堆栈跟踪的应用程序,以后可以用于调试。

在Windows上,我使用了JEDI项目提供的优秀JCLDebug单元。

现在我的应用程序在OSX上运行,我遇到了一些麻烦 - 我不知道如何在发生异常时获得正确的堆栈跟踪。

我已经掌握了基础知识 -

1)我可以使用' backtrace'来获得一个堆栈跟踪。 (在libSystem.dylib中找到)

2)使用Delphi的链接器提供的.map文件可以将生成的回溯转换为行号

我离开的问题是 - 我不知道从哪里调用回溯。我知道Delphi使用Mach异常(在一个单独的线程上),并且我不能使用posix信号,但这就是我设法解决的所有问题。

我可以在'尝试......除了'块,但不幸的是,到那时栈已经缩小了。

如何安装在异常发生后立即运行的正确异常记录器?

更新

根据Honza R的建议,我已经看了一下GetExceptionStackInfoProc'' GetExceptionStackInfoProc'过程

这个功能确实让我内心深处'异常处理过程,但不幸的是,我留下了一些与之前相同的问题。

首先 - 在桌面平台上,这个功能' GetExceptionStackInfoProc'只是一个函数指针,您可以使用自己的异常信息处理程序进行分配。因此,开箱即用,Delphi不提供任何堆栈信息提供商。

如果我将一个函数分配给' GetExceptionStackInfoProc'然后运行'回溯'在其中,我收到一个堆栈跟踪,但该跟踪是相对于异常处理程序,而不是导致异常的线程。

' GetExceptionStackInfoProc'确实包含一个指向“TExceptionRecord”的指针,但是这里的文档非常有限。

我可能会超越自己的深度,但是如何从正确的线程获得堆栈跟踪?我是否有可能注入自己的回溯'函数进入异常处理程序,然后从那里返回标准异常处理程序?

更新2

更多细节。有一点需要澄清 - 这个问题是关于MACH消息处理的异常,而不是完全在RTL中处理的软件异常。

Embarcadero已经提出了一些评论以及这些功能 -

    System.Internal.MachExceptions.pas -> catch_exception_raise_state_identity

    {
     Now we set up the thread state for the faulting thread so that when we
     return, control will be passed to the exception dispatcher on that thread,
     and this POSIX thread will continue watching for Mach exception messages.
     See the documentation at <code>DispatchMachException()</code> for more
     detail on the parameters loaded in EAX, EDX, and ECX.
    }

    System.Internal.ExcUtils.pas -> SignalConverter

    {
      Here's the tricky part.  We arrived here directly by virtue of our
      signal handler tweaking the execution context with our address.  That
      means there's no return address on the stack.  The unwinder needs to
      have a return address so that it can unwind past this function when
      we raise the Delphi exception.  We will use the faulting instruction
      pointer as a fake return address.  Because of the fencepost conditions
      in the Delphi unwinder, we need to have an address that is strictly
      greater than the actual faulting instruction, so we increment that
      address by one.  This may be in the middle of an instruction, but we
      don't care, because we will never be returning to that address.
      Finally, the way that we get this address onto the stack is important.
      The compiler will generate unwind information for SignalConverter that
      will attempt to undo any stack modifications that are made by this
      function when unwinding past it.  In this particular case, we don't want
      that to happen, so we use some assembly language tricks to get around
      the compiler noticing the stack modification.
    }

这似乎是我所遇到的问题的原因。

当我在此异常系统将控制权移交给RTL之后执行堆栈跟踪时,它看起来像这样 - (请记住,堆栈展开已经被回溯例程取代。回溯将控制权交给unwinder一旦完成)

0: MyExceptionBacktracer
1: initunwinder in System.pas
2: RaiseSignalException in System.Internal.ExcUtils.pas 

由于RaiseSignalException调用了SignalConverter,因此我认为libc提供的backtrace函数与对堆栈的修改不兼容。因此,它无法在该点之外读取堆栈,但堆栈仍然存在于下方。

有谁知道该怎么做(或者我的假设是否正确)?

更新3

我终于设法在OSX上获得了适当的堆栈跟踪。非常感谢Honza和Sebastian。通过结合他们的两种技术,我发现了一些有效的方法。

对于任何可以从中受益的人来说,这里是基本来源。请记住,我不确定它是否100%正确,如果你能提出改进建议,请继续。这个技术在Delphi在错误线程上展开堆栈之前就挂起了一个异常,并补偿了可能事先发生的任何堆栈帧损坏。

unit MyExceptionHandler;

interface

implementation

uses
  SysUtils;

var
  PrevRaiseException: function(Exc: Pointer): LongBool; cdecl;

function backtrace2(base : NativeUInt; buffer : PPointer; size : Integer) : Integer;
var SPMin   : NativeUInt;
begin
  SPMin:=base;
  Result:=0;
  while (size > 0) and (base >= SPMin) and (base <> 0) do begin

    buffer^:=PPointer(base + 4)^;
    base:=PNativeInt(base)^;

    //uncomment to test stacktrace
    //WriteLn(inttohex(NativeUInt(buffer^), 8));

    Inc(Result);
    Inc(buffer);
    Dec(size);

  end;
  if (size > 0) then buffer^:=nil;
end;

procedure UnInstallExceptionHandler; forward;

var
  InRaiseException: Boolean;

function RaiseException(Exc: Pointer): LongBool; cdecl;
var b : NativeUInt;
    c : Integer;
    buff : array[0..7] of Pointer;
begin
  InRaiseException := True;

  asm
    mov b, ebp
  end;

  c:=backtrace2(b - $4 {this is the compiler dependent value}, @buff, Length(buff));
  //... do whatever you want to do with the stacktrace

  Result := PrevRaiseException(Exc);
  InRaiseException := False;
end;

procedure InstallExceptionHandler;
var
  U: TUnwinder;
begin
  GetUnwinder(U);
  Assert(Assigned(U.RaiseException));
  PrevRaiseException := U.RaiseException;
  U.RaiseException := RaiseException;
  SetUnwinder(U);
end;

procedure UnInstallExceptionHandler;
var
  U: TUnwinder;
begin
  GetUnwinder(U);
  U.RaiseException := PrevRaiseException;
  SetUnwinder(U);
end;

initialization
  InstallExceptionHandler;
end.

2 个答案:

答案 0 :(得分:7)

您可以在GetExceptionStackInfoProc中使用CleanUpStackInfoProcGetStackInfoStringProcException来保存GetExceptionStackInfoProc中的堆栈跟踪,然后使用{{1}检索它如果您使用GetStackInfoStringProc的{​​{1}}属性,将由RTL调用。也许你也可以看看https://bitbucket.org/shadow_cs/delphi-arm-backtrace,它在Android上演示了这一点。

要在Mac OS X上正确执行此操作,无法使用StackTrace Exception函数,因为Delphi会在从libc调用backtrace时损坏堆栈帧。必须使用自己的实现,它能够从不同的基址转移堆栈,可以手动纠正。

你的GetExceptionStackInfoProc看起来像这样(我在本例中使用了XE5,根据你使用的编译器,这个例子只在Mac OS X上测试,Windows实现可能或者可能没有区别):

Exception.RaisingException

GetExceptionStackInfoProc函数看起来像这样(请注意,实现中缺少停止条件和其他验证,以确保在堆栈遍历期间不会导致AV):

var b : NativeUInt;
    c : Integer;
    buff : array[0..7] of Pointer;
begin
  asm
    mov b, ebp
  end;
  c:=backtrace2(b - $14 {this is the compiler dependent value}, @buff, Length(buff));
  //... do whatever you want to do with the stacktrace
end;

答案 1 :(得分:1)

您可以将自己挂钩到Exception Unwinder中。然后你可以在异常发生的地方调用backtrace。这是一个例子。 SBMapFiles单元是我用来读取mapfiles的单位。不需要获取异常调用堆栈。

unit MyExceptionHandler;

interface

implementation

uses
  Posix.Base, SysUtils, SBMapFiles;

function backtrace(result: PNativeUInt; size: Integer): Integer; cdecl; external libc name '_backtrace';
function _NSGetExecutablePath(buf: PAnsiChar; BufSize: PCardinal): Integer; cdecl; external libc name '__NSGetExecutablePath';

var
  PrevRaiseException: function(Exc: Pointer): LongBool; cdecl;
  MapFile: TSBMapFile;

const
  MaxDepth = 20;
  SkipFrames = 3;

procedure ShowCurrentStack;
var
  StackLog: PNativeUInt; //array[0..10] of Pointer;
  Cnt: Integer;
  I: Integer;
begin
  {$POINTERMATH ON}
  GetMem(StackLog, SizeOf(Pointer) * MaxDepth);
  try
    Cnt := backtrace(StackLog, MaxDepth);

    for I := SkipFrames to Cnt - 1 do
    begin
      if StackLog[I] = $BE00EF00 then
      begin
        WriteLn('---');
        Break;
      end;
      WriteLn(IntToHex(StackLog[I], 8), ' ', MapFile.GetFunctionName(StackLog[I]));
    end;

   finally
    FreeMem(StackLog);
   end;
  {$POINTERMATH OFF}
end;

procedure InstallExceptionHandler; forward;
procedure UnInstallExceptionHandler; forward;

var
  InRaiseException: Boolean;

function RaiseException(Exc: Pointer): LongBool; cdecl;
begin
  InRaiseException := True;
  ShowCurrentStack;

  Result := PrevRaiseException(Exc);
  InRaiseException := False;
end;

procedure InstallExceptionHandler;
var
  U: TUnwinder;
begin
  GetUnwinder(U);
  Assert(Assigned(U.RaiseException));
  PrevRaiseException := U.RaiseException;
  U.RaiseException := RaiseException;
  SetUnwinder(U);
end;

procedure UnInstallExceptionHandler;
var
  U: TUnwinder;
begin
  GetUnwinder(U);
  U.RaiseException := PrevRaiseException;
  SetUnwinder(U);
end;

procedure LoadMapFile;
var
  FileName: array[0..255] of AnsiChar;
  Len: Integer;
begin
  if MapFile = nil then
  begin
    MapFile := TSBMapFile.Create;
    Len := Length(FileName);
    _NSGetExecutablePath(@FileName[0], @Len);
    if FileExists(ChangeFileExt(FileName, '.map')) then
      MapFile.LoadFromFile(ChangeFileExt(FileName, '.map'));
  end;
end;

initialization
  LoadMapFile;
  InstallExceptionHandler;
end.
相关问题