gpt4 book ai didi

.net - 您如何在进程中将 native 映射到 IL 指令指针

转载 作者:行者123 更新时间:2023-12-04 02:18:11 26 4
gpt4 key购买 nike

在使用 .NET 框架的非托管 API 来分析 .NET 进程内进程时,是否可以查找与提供给 StackSnapshotCallback 函数的 native 指令指针相关的 IL 指令指针?

可能很明显,我正在拍摄当前堆栈的快照,并希望在堆栈转储中提供文件和行号信息。托管堆栈资源管理器通过查询 ISymUnmanagedMethod::GetSequencePoints 来执行此操作。 .这很好,但是序列点与偏移量相关联,到目前为止,我假设这些是从方法开始的偏移量(在中间语言中)。

在他的博客文章的后续评论中 Profiler stack walking: Basics and beyond , David Broman 表示可以使用 ICorDebugCode::GetILToNativeMapping 来实现这种映射。 .但是,这并不理想,因为获取此接口(interface)需要从另一个调试器进程附加到我的进程。

我想避免这一步,因为我想在拍摄这些快照时继续能够从 Visual Studio 调试器中运行我的应用程序。它可以更轻松地单击输出窗口中的行号并转到有问题的代码。

该功能是可能的......您可以在托管代码内部随意吐出一个行号堆栈跟踪,唯一的问题是它是否可访问。另外,我不想使用 System::Diagnostics::StackTraceSystem::Environment::StackTrace功能,因为出于性能原因,我需要延迟堆栈的实际转储......因此,希望为以后的方法名称和代码位置的解析节省成本......以及混合 native 和托管帧的能力.

最佳答案

为了从 ICorProfilerInfo2::DoStackSnapshot 提供的 native 指令指针进行转换到中间语言方法偏移,你必须采取两个步骤,因为 DoStackSnapshot提供FunctionID和本地指令指针作为虚拟内存地址。

第 1 步,是将指令指针转换为 native 代码方法偏移量。 (从 JITed 方法开始的偏移量)。这可以通过 ICorProfilerInfo2::GetCodeInfo2 来完成

ULONG32 pcIL(0xffffffff);
HRESULT hr(E_FAIL);
COR_PRF_CODE_INFO* codeInfo(NULL);
COR_DEBUG_IL_TO_NATIVE_MAP* map(NULL);
ULONG32 cItem(0);

UINT_PTR nativePCOffset(0xffffffff);
if (SUCCEEDED(hr = pInfo->GetCodeInfo2(functioId, 0, &cItem, NULL)) &&
(NULL != (codeInfo = new COR_PRF_CODE_INFO[cItem])))
{
if (SUCCEEDED(hr = pInfo->GetCodeInfo2(functionId, cItem, &cItem, codeInfo)))
{
COR_PRF_CODE_INFO *pCur(codeInfo), *pEnd(codeInfo + cItem);
nativePCOffset = 0;
for (; pCur < pEnd; pCur++)
{
// 'ip' is the UINT_PTR passed to the StackSnapshotCallback as named in
// the docs I am looking at
if ((ip >= pCur->startAddress) && (ip < (pCur->startAddress + pCur->size)))
{
nativePCOffset += (instructionPtr - pCur->startAddress);
break;
}
else
{
nativePCOffset += pCur->size;
}

}
}
delete[] codeInfo; codeInfo = NULL;
}

第 2 步。一旦你有一个从 natvie 代码方法开始的偏移量,你可以使用它来转换到使用 ICorProfilerInfo2::GetILToNativeMapping 的中间语言方法开始的偏移量。 .
if ((nativePCOffset != -1) &&
SUCCEEDED(hr = pInfo->GetILToNativeMapping(functionId, 0, &cItem, NULL)) &&
(NULL != (map = new COR_DEBUG_IL_TO_NATIVE_MAP[cItem])))
{
if (SUCCEEDED(pInfo->GetILToNativeMapping(functionId, cItem, &cItem, map)))
{
COR_DEBUG_IL_TO_NATIVE_MAP* mapCurrent = map + (cItem - 1);
for (;mapCurrent >= map; mapCurrent--)
{
if ((mapCurrent->nativeStartOffset <= nativePCOffset) &&
(mapCurrent->nativeEndOffset > nativePCOffset))
{
pcIL = mapCurrent->ilOffset;
break;
}
}
}
delete[] map; map = NULL;
}

然后可以使用符号 API 将代码位置映射到文件和行号

感谢 Mithun Shanbhag寻找解决方案的方向。

关于.net - 您如何在进程中将 native 映射到 IL 指令指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/198754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com