我有一个可以记录堆栈跟踪的应用程序,以后可以用于调试。
在 Windows 上,我使用了 JEDI 项目提供的出色的 JCLDebug 单元。
现在我的应用程序在 OSX 上运行,我遇到了一些麻烦——我不知道在发生异常时如何获取正确的堆栈跟踪。
我已经掌握了基础知识-
1)我可以使用'backtrace'(在libSystem.dylib中找到)获得堆栈跟踪
2) 可以使用 Delphi 的链接器提供的 .map 文件将生成的回溯转换为行号
我剩下的问题是 - 我不知道从哪里调用回溯。我知道 Delphi 使用 Mach 异常(在单独的线程上),并且我不能使用 posix 信号,但这就是我设法解决的所有问题。
我可以在“try...except”块中获得回溯,但不幸的是,到那时堆栈已经结束了。
如何安装正确的异常记录器,它将在异常发生后立即运行?
更新:
根据“Honza R”的建议,我查看了“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 之后执行堆栈跟踪时,它看起来像这样 - (请记住,堆栈展开器已被回溯例程取代。回溯将控制权移交给展开器完全的)
0: MyExceptionBacktracer
1: initunwinder in System.pas
2: RaiseSignalException in System.Internal.ExcUtils.pas
由于RaiseSignalException
is 调用SignalConverter
,我被引导相信backtrace
libc 提供的函数与对堆栈所做的修改不兼容。因此,它无法读取超出该点的堆栈,但堆栈仍然存在于下方。
有谁知道该怎么做(或者我的假设是否正确)?
更新 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.