【问题标题】:Win64 exception stack walking not displaying entriesWin64异常堆栈行走不显示条目
【发布时间】:2013-01-06 22:25:13
【问题描述】:

在阅读 Win64 结构化异常跟踪(来自 Programming against the x64 exception handling support, part 7: Putting it all together, or building a stack walk routine)时,我转换了代码 StackWalk64.cpp

procedure DumpExceptionStack();
var
  LContext : CONTEXT;
  LUnwindHistoryTable : _UNWIND_HISTORY_TABLE;
  LRuntimeFunction : Pointer;
  LImageBase : ULONGLONG;
    HandlerData : Pointer;
    EstablisherFrame : ULONG64;
    NvContext : KNONVOLATILE_CONTEXT_POINTERS;

  LLineNumber                    : integer;
  LModuleName                    : UnicodeString;
  LPublicAddr                    : pointer;
  LPublicName                    : UnicodeString;
  LUnitName                      : UnicodeString;
begin
    //
    // First, we'll get the caller's context.
    //
  RtlCaptureContext(LContext);

    //
    // Initialize the (optional) unwind history table.
    //
  LUnwindHistoryTable := Default(_UNWIND_HISTORY_TABLE);

  // LUnwindHistoryTable.Unwind := True;

    //
    // This unwind loop intentionally skips the first call frame, as it shall
    // correspond to the call to StackTrace64, which we aren't interested in.
    //
  repeat
        //
        // Try to look up unwind metadata for the current function.
        //
        LRuntimeFunction := RtlLookupFunctionEntry(LContext.Rip,
                                               LImageBase,
                                               LUnwindHistoryTable);

    NvContext := Default(KNONVOLATILE_CONTEXT_POINTERS);

    if not Assigned(LRuntimeFunction) then
    begin
            //
            // If we don't have a RUNTIME_FUNCTION, then we've encountered
            // a leaf function.  Adjust the stack approprately.
            //

      //LContext.Rip  := (ULONG64)(*(PULONG64)Context.Rsp);
      LContext.Rip  := ULONG64(Pointer(LContext.Rsp)^);
            LContext.Rsp := LContext.Rsp + 8;
    end
    else
    begin
            //
            // Otherwise, call upon RtlVirtualUnwind to execute the unwind for
            // us.
            //
            RtlVirtualUnwind(UNW_FLAG_NHANDLER,
                       LImageBase,
                       LContext.Rip,
                       LRuntimeFunction,
                       LContext,
                       HandlerData,
                       EstablisherFrame,
                       NvContext);
    end;

        //
        // If we reach an RIP of zero, this means that we've walked off the end
        // of the call stack and are done.
        //
    if LContext.Rip = 0 then
      Break;

        //
        // Display the context.  Note that we don't bother showing the XMM
        // context, although we have the nonvolatile portion of it.
        //
    if madMapFile.GetMapFileInfos(Pointer(LContext.Rip),
                                  LModuleName,
                                  LUnitName,
                                  LPublicName,
                                  LPublicAddr,
                                  LLineNumber) then
    begin
      Writeln(Format('%p %s.%s %d', [Pointer(LContext.Rip), LUnitName, LPublicName, LLineNumber{, LSEHType}]));
    end;
  until LContext.Rip = 0;
end;

然后我用以下方式调用它:

procedure Main();
begin
  try
    try
      try
        try
          DumpExceptionStack();
        finally
          //
        end;
      except
        on E : Exception do
         raise
      end;
    except
      on E : Exception do
       raise
    end;
  except
    on E : Exception do
     raise
  end;
end;

当我运行应用程序(只是一个控制台应用程序)时,我只得到一个 Main 条目,但我预计会有四个(三个嵌套异常,最后一个)。

可能是我误解了,DumpExceptionStack 只会在抛出异常时给出我感兴趣的结果?如果是这样,获得所有异常堆栈(如果可能的话)所需的更改是什么 - 即。 Main 有四个输出?

【问题讨论】:

  • 出于好奇,这里要构建什么?
  • 使用C++代码不是更方便吗?

标签: windows delphi winapi


【解决方案1】:

与基于堆栈的 x86 模型相比,x64 异常模型是基于表的。这意味着异常堆栈不存在。无论如何,我从未见过尝试包含异常和 finally 块的 stalk walk 例程。这个没什么不同。它遍历函数调用堆栈。

单个函数中的异常流由作用域​​表控制。在您的函数中,如果您的代码在调用 DumpExceptionStack 时引发异常,则多个范围表条目与异常位置匹配。异常由最里面的匹配范围处理。范围的开始地址和结束地址之间的距离可以用来推断哪个范围是最里面的。如果该最内层范围不处理异常或重新引发异常,则要求下一个最内层范围处理它。以此类推,直到函数的所有匹配范围都用完为止。

【讨论】:

  • 我了解 x64 异常模型与 x86 模型不同。把问题放在头上,操作系统如何知道如果当前的 try-except 子句不处理当前的异常,调用哪个异常处理程序......或者这应该是一个不同的问题?
  • 我认为这可能是一个不同的问题,但我还是添加了一个段落来解决它。
  • @Nicholas 我回答问题了吗?
  • 从我的角度来看,不,但从问题来看,是的。所以我会勾选这个并提出另一个。
  • @Nicholas 您是否难以理解异常模型基于表的含义。也许您可以观察到 try 的代码在 x64 中没有任何作用。与 x86 中的信息被压入堆栈不同。
猜你喜欢
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2010-10-16
相关资源
最近更新 更多