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

8
我有一个应用程序可以记录堆栈跟踪,以便后续进行调试。
在Windows上,我已经使用JEDI项目提供的优秀JCLDebug单元成功解决了问题。
现在我的应用程序在OSX上运行时,遇到了一些麻烦 - 我不知道如何在异常发生时获取正确的堆栈跟踪。
我已经掌握了基础知识 -
1)我可以使用“backtrace”(在libSystem.dylib中找到)获取堆栈跟踪
2)可以使用Delphi链接器提供的.map文件将结果转换为行号
我面临的问题是 - 我不知道从哪里调用backtrace。我知道Delphi使用Mach异常(在单独的线程上),而且我不能使用posix信号,但这就是我设法解决的全部问题。
我可以在'try...except'块中获取回溯,但不幸的是,在这一点上,堆栈已经缠绕下来了。
如何安装适当的异常记录器,以便在异常发生后立即运行?
更新:
根据'Honza R'的建议,我查看了'GetExceptionStackInfoProc'过程。
这个函数确实让我进入了异常处理过程中,但不幸的是,留下了之前遇到的一些问题。
首先 - 在桌面平台上,这个函数'GetExceptionStackInfoProc'只是一个函数指针,可以将其分配给自己的异常信息处理程序。因此,出于盒子外,Delphi不提供任何堆栈信息提供者。
如果我将一个函数分配给“GetExceptionStackInfoProc”,然后在其中运行“backtrace”,我会收到一个堆栈跟踪,但该跟踪相对于异常处理程序,而不是引发异常的线程。
'GetExceptionStackInfoProc'确实包含指向'TExceptionRecord'的指针,但文档非常有限。
我可能已经超出了我的能力范围,但我如何从正确的线程获取堆栈跟踪?我是否可以将自己的“backtrace”函数注入到异常处理程序中,然后从那里返回标准异常处理程序?
更新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。堆栈展开器已被后续的回溯例程所取代。回溯例程完成后,它将把控制权交还给展开器。

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

由于RaiseSignalExceptionSignalConverter调用,我认为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个回答

8
你可以在Exception类中使用GetExceptionStackInfoProcCleanUpStackInfoProcGetStackInfoStringProc函数用于保存堆栈跟踪信息,然后通过调用GetStackInfoStringProc函数检索它。如果你使用ExceptionStackTrace属性,RTL将会自动调用该函数。也许你还可以参考一下https://bitbucket.org/shadow_cs/delphi-arm-backtrace 教程来学习如何在 Android 上实现这个功能。
在 Mac OS X 上,不能使用libc库中的backtrace函数,因为 Delphi 在从 Exception.RaisingException 调用GetExceptionStackInfoProc时会破坏堆栈帧。必须使用自己的实现来遍历堆栈,该实现能够从不同的基地址开始,可以手动进行修正。
你的GetExceptionStackInfoProc看起来应该是这样的(我使用的是 XE5 进行示例,下面添加到 EBP 的值可能因使用的编译器而异,此示例仅在 Mac OS X 上测试,Windows 实现可能会略有不同):
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;

backtrace2 函数将如下所示(请注意,在实现过程中省略了停止条件和其他验证,以确保在堆栈行走期间不会引发AV):

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)^;
    Inc(Result);

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

太棒了。我有机会测试一下,等我把它搞定了就会勾选你的答案! - AudioGL
如果你试图使用这个来捕获 AVs 的话可能不起作用(我没有在安卓上测试过),但是由于这个使用了一个信号,很可能会终止你的 iOS 应用程序。但是我猜你应该可以使用“普通”的 Exceptions - Honza R
我已经尝试过这个,并更新了原始问题。这是一个开始,但我仍然不确定如何解决这个问题。 - AudioGL
关于GetExceptionStackInfoProc,存在更多问题,它包含指向TExceptionRecord的指针,但该记录的并非所有字段都被初始化(特别是在ARM上)。我也相信线程确实是同一个,但似乎存在一些堆栈损坏(不一致),导致backtrace函数无法正常工作,正如您所看到的,即使是Delphi调用堆栈也不完整。我在这里谈论的是x86上的Mac OS。 - Honza R
我花了一点时间摸索,但是我用这种技巧有些成功了。 - AudioGL
显示剩余5条评论

1
您可以将自己钩入异常解开器。然后您可以在异常发生时调用回溯。这里有一个例子。我使用的读取映射文件的单元是SBMapFiles。不需要获取异常调用堆栈。
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.

今晚我花了一些时间在这个问题上 - 尽管我很感激你的努力 - 但是我得到的结果和之前的解决方案一样。我会继续努力解决它,但似乎堆栈跟踪几乎与我之前的相同。 - AudioGL
另外,我进行了一些搜索,显然我们放置堆栈跟踪的取消程序是在一个.dylib文件中提供的,而不是在源代码中。因此无法查看其工作方式。真的希望那里会有一些线索。 - AudioGL
我有以下函数:procedure TForm1.Button3Click(Sender: TObject); begin raise EProgrammerNotFound.Create('Error Message'); end;然后我得到了以下堆栈跟踪:005A59EB Unit1.TForm1.Button3Click 00356F46 FMX.Controls.TControl.Click 00237D0D FMX.StdCtrls.TCustomButton.Click 0040383F FMX.Forms.TCommonCustomForm.MouseUp 00506DFA FMX.Platform.Mac.TPlatformCocoa.MouseEvent 00506FFD FMX.Platform.Mac.TPlatformCocoa.MouseEvent 0050D4BC FMX.Platform.Mac.TFMXViewBase.mouseUp 002D123A Macapi.ObjectiveC.DispatchT...你的真正问题可能是读取地图文件吗? - Sebastian Z
我可以问一下,“SBMapFiles”单元是从哪里来的吗?我找不到任何信息,但它听起来像是我可以使用的东西! - DNR
SBMapFiles是我一段时间前编写的一个单元。它用于读取地图文件并从地址获取函数名称。我还没有发布它。您可以在http://delphi.zierer.info/CrashReportMerger.zip中看到它的实际应用(抱歉,没有源代码)。该工具接受Delphi地图文件和OSX崩溃报告,并将其转换为更有用的内容。 - Sebastian Z
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接