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

Macos Delphi-在OSX上获取完整堆栈跟踪,macos,delphi,exception,posix,mach,Macos,Delphi,Exception,Posix,Mach,我有一个可以记录stacktrace的应用程序,以后可以用于调试 在Windows上,我通过使用绝地计划提供的优秀JCLDebug单元获得了成功 现在,我的应用程序正在OSX上运行,我遇到了一个小问题——我不知道在发生异常时如何获得正确的stacktrace 我已经记下了基础知识- 1) 我可以使用“backtrace”(在libSystem.dylib中找到)获取堆栈跟踪 2) 可以使用Delphi的链接器提供的.map文件将结果回溯转换为行号 留给我的问题是——我不知道从哪里打电话回溯。我知

我有一个可以记录stacktrace的应用程序,以后可以用于调试

在Windows上,我通过使用绝地计划提供的优秀JCLDebug单元获得了成功

现在,我的应用程序正在OSX上运行,我遇到了一个小问题——我不知道在发生异常时如何获得正确的stacktrace

我已经记下了基础知识-

1) 我可以使用“backtrace”(在libSystem.dylib中找到)获取堆栈跟踪

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

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

我可以在“try…except”块中得到回溯,但不幸的是,此时堆栈已经结束

如何安装在异常发生后立即运行的适当异常记录器

更新:

根据Honza R的建议,我已经查看了“GetExceptionStackInfoProc”过程

这个函数确实让我“进入”了异常处理过程,但不幸的是,它给我留下了一些与以前相同的问题

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

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

“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后执行stacktrace时,看起来是这样的-(请记住,堆栈放卷机已被回溯例程取代。一旦完成,回溯将控制权移交给放卷机)

由于
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.
unitmyExceptionHandler;
接口
实施
使用
SysUtils;
变量
PrevRaiseException:函数(Exc:指针):LongBool;cdecl;
函数backtrace2(base:NativeUInt;buffer:PPointer;size:Integer):整数;
var-SPMin:NativeUInt;
开始
SPMin:=基准;
结果:=0;
而(大小>0)和(基数>=SPMin)和(基数0)则开始
缓冲区^:=PPointer(base+4)^;
基数:=PNativeInt(base)^;
//取消对测试stacktrace的注释
//WriteLn(inttohex(NativeUInt(buffer^),8));
公司(结果);
公司(缓冲区);
Dec(大小);
结束;
如果(大小>0),则缓冲区^:=nil;
结束;
程序卸载异常处理程序;向前地
变量
InRaiseException:布尔值;
函数RaiseException(Exc:Pointer):LongBool;cdecl;
变量b:本地单元;
c:整数;
buff:指针的数组[0..7];
开始
INRAISEEException:=真;
asm
mov b,ebp
结束;
c:=backtrace2(b-$4{这是依赖于编译器的值},@buff,Length(buff));
//... 用stacktrace做任何你想做的事
结果:=PrevRaiseException(Exc);
INRAISEEException:=假;
结束;
过程InstallExceptionHandler;
变量
U:TUnwinder;
开始
开卷机(U);
断言(赋值(U.RaiseException));
PrevRaiseException:=U.RaiseException;
U.RaiseException:=RaiseException;
放卷机(U);
结束;
程序卸载异常处理程序;
变量
U:TUnwinder;
开始
开卷机(U);
U.RaiseException:=PrevRaiseException;
放卷机(U);
结束;
初始化
InstallExceptionHandler;
结束。

您可以使用
GetExceptionStackInfoProc
cleanupstackinfostringproc
GetStackInfoStringProc
Exception
类中,您需要将堆栈跟踪保存在
getexceptionstackinfostringproc
中,然后使用
GetStackInfoStringProc
检索堆栈跟踪,如果使用
Exception
StackTrace
属性,将被RTL调用。也许你也可以看看Android上的哪个演示

要在Mac OS X上正确执行此操作,无法使用
libc
backtrace
函数,因为调用
GetExceptionStackInfoProcunit 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.
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;
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;
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.