为了从 by 提供的本机指令指针转换为ICorProfilerInfo2::DoStackSnapshot
中间语言方法偏移量,您必须采取两个步骤,因为DoStackSnapshot
将FunctionID
本机指令指针作为虚拟内存地址提供。
第 1 步,是将指令指针转换为本机代码方法偏移量。(从 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指导寻找解决方案。