我正在處理小型Windows異常處理引擎,試圖從系統收集最大的信息,包括C++異常RTTI。Windows 64位VectoredExceptionHandler,MS Visual Studio 2015 C++ RTTI
在由MSVS 2015編譯的32位VectoredExceptionHandler中,我成功可以獲取std :: type_info指向正在引發的類型的RTTI的指針。它可以很容易地在((_ThrowInfo*) ExceptionPointers->ExceptionRecord->ExceptionInformation[2])->pCatchableTypeArray->arrayOfCatchableTypes[0]
(見classic article of Raymond Chen,來自MS的ehdata.h
文件和許多其他定義)中找到。此方法基於取得編譯器構建的MSVC內置_ThrowInfo
結構數據的pCatchableTypeArray
成員。
但在64位環境中,_ThrowInfo
不包含直接RTTI:不幸的是,pCatchableTypeArray
爲NULL。在反彙編窗口中,即使在調用_CxxThrowException
主MS丟擲處理程序之前,我也發現它是NULL。我搜索了許多關於MSVC中使用的新的64位異常處理機制的文章,但沒有關於RTTI的信息。但也許我錯過了一些東西。
有什麼方法可以獲得在64位MSVC環境中工作的向量化異常處理程序中拋出的C++異常的std :: type_info(或簡單鍵入名稱)?
這裏的傾倒32位和64位的異常信息的輸出:
32位(RTTI成功):
VectoredExceptionHandler(): Start
exc->ExceptionCode = 0xE06D7363
exc->ExceptionAddress = 0x74E2C54F
exc->NumberParameters = 3
exc->ExceptionInformation[0] = 0x19930520 (sig)
exc->ExceptionInformation[1] = 0x004FFD9C (object)
exc->ExceptionInformation[2] = 0x003AD85C (throwInfo)
exc->ExceptionInformation[3] = 0x005B18F8 (module)
throwInfo->attributes = 0x00000000
throwInfo->pmfnUnwind = 0x00000000
throwInfo->pForwardCompat = 0x00000000
throwInfo->pCatchableTypeArray = 0x003AD870
object = 0x004FFD9C
throwInfo = 0x003AD85C
module = 0x00000000
throwInfo->pCatchableTypeArray = 0x003AD870
cArray = 0x003AD870
cArray->arrayOfCatchableTypes[0] = 0x003AD878
cType = 0x003AD878
cType->pType = 0x003AFA70
type = 0x003AFA70
type->name() = "struct `int __cdecl main(void)'::`2'::meow_exception"
cType->sizeOrOffset = 4
VectoredExceptionHandler(): End
main(): catch (meow_exception { 3 })
64位(RTTI失敗)
VectoredExceptionHandler(): Start
exc->ExceptionCode = 0xE06D7363
exc->ExceptionAddress = 0x000007FEFCE0A06D
exc->NumberParameters = 4
exc->ExceptionInformation[0] = 0x0000000019930520 (sig)
exc->ExceptionInformation[1] = 0x000000000025FBE0 (object)
exc->ExceptionInformation[2] = 0x000000013FC52AB0 (throwInfo)
exc->ExceptionInformation[3] = 0x000000013FBE0000 (module)
module = 0x000000013FBE0000
throwInfo->attributes = 0x00000000
throwInfo->pmfnUnwind = 0x0000000000000000
throwInfo->pForwardCompat = 0x0000000000072AD0
throwInfo->pCatchableTypeArray = 0x0000000000000000
VectoredExceptionHandler(): End
main(): catch (meow_exception { 3 })
用於獲取這些轉儲的代碼:
#include <stdio.h>
#include <typeinfo>
#include <windows.h>
//--------------------------------------------------------------------------------------------------
const unsigned EXCEPTION_CPP_MICROSOFT = 0xE06D7363, // '?msc'
EXCEPTION_CPP_MICROSOFT_EH_MAGIC_NUMBER1 = 0x19930520, // '?msc' version magic, see ehdata.h
EXCEPTION_OUTPUT_DEBUG_STRING = 0x40010006, // OutputDebugString() call
EXCEPTION_THREAD_NAME = 0x406D1388; // Passing name of thread to the debugger
void OutputDebugPrintf (const char* format, ...);
//--------------------------------------------------------------------------------------------------
long WINAPI VectoredExceptionHandler (EXCEPTION_POINTERS* pointers)
{
const EXCEPTION_RECORD* exc = pointers->ExceptionRecord;
if (exc->ExceptionCode == EXCEPTION_OUTPUT_DEBUG_STRING ||
exc->ExceptionCode == EXCEPTION_THREAD_NAME)
return EXCEPTION_CONTINUE_SEARCH;
OutputDebugPrintf ("\n%s(): Start\n\n", __func__);
OutputDebugPrintf ("exc->ExceptionCode = 0x%X\n", exc->ExceptionCode);
OutputDebugPrintf ("exc->ExceptionAddress = 0x%p\n", exc->ExceptionAddress);
if (exc->ExceptionInformation[0] == EXCEPTION_CPP_MICROSOFT_EH_MAGIC_NUMBER1 &&
exc->NumberParameters >= 3)
{
OutputDebugPrintf ("exc->NumberParameters = %u\n", exc->NumberParameters);
OutputDebugPrintf ("exc->ExceptionInformation[0] = 0x%p (sig)\n", (void*) exc->ExceptionInformation[0]);
OutputDebugPrintf ("exc->ExceptionInformation[1] = 0x%p (object)\n", (void*) exc->ExceptionInformation[1]);
OutputDebugPrintf ("exc->ExceptionInformation[2] = 0x%p (throwInfo)\n", (void*) exc->ExceptionInformation[2]);
OutputDebugPrintf ("exc->ExceptionInformation[3] = 0x%p (module)\n", (void*) exc->ExceptionInformation[3]);
OutputDebugPrintf ("\n");
HMODULE module = (exc->NumberParameters >= 4)? (HMODULE) exc->ExceptionInformation[3] : NULL;
if (module)
{
OutputDebugPrintf ("module = 0x%p\n", module);
OutputDebugPrintf ("\n");
}
const _ThrowInfo* throwInfo = (const _ThrowInfo*) exc->ExceptionInformation[2];
if (throwInfo)
{
OutputDebugPrintf ("throwInfo->attributes = 0x%08X\n", throwInfo->attributes);
OutputDebugPrintf ("throwInfo->pmfnUnwind = 0x%p\n", throwInfo->pmfnUnwind);
OutputDebugPrintf ("throwInfo->pForwardCompat = 0x%p\n", throwInfo->pForwardCompat);
OutputDebugPrintf ("throwInfo->pCatchableTypeArray = 0x%p\n", throwInfo->pCatchableTypeArray);
OutputDebugPrintf ("\n");
}
if (throwInfo && throwInfo->pCatchableTypeArray)
{
#define RVA_TO_VA_(type, addr) ((type) ((uintptr_t) module + (uintptr_t) (addr)))
OutputDebugPrintf ("object = 0x%p\n", (void*) exc->ExceptionInformation[1]);
OutputDebugPrintf ("throwInfo = 0x%p\n", (void*) throwInfo);
OutputDebugPrintf ("module = 0x%p\n", (void*) module);
OutputDebugPrintf ("\n");
const _CatchableTypeArray* cArray = RVA_TO_VA_(const _CatchableTypeArray*, throwInfo->pCatchableTypeArray);
OutputDebugPrintf ("throwInfo->pCatchableTypeArray = 0x%p\n", (void*) throwInfo->pCatchableTypeArray);
OutputDebugPrintf ("cArray = 0x%p\n\n", (void*) cArray);
const _CatchableType* cType = RVA_TO_VA_(const _CatchableType*, cArray->arrayOfCatchableTypes[0]);
OutputDebugPrintf ("cArray->arrayOfCatchableTypes[0] = 0x%p\n", (void*) cArray->arrayOfCatchableTypes[0]);
OutputDebugPrintf ("cType = 0x%p\n\n", (void*) cType);
const std::type_info* type = RVA_TO_VA_(const std::type_info*, cType->pType);
OutputDebugPrintf ("cType->pType = 0x%p\n", (void*) cType->pType);
OutputDebugPrintf ("type = 0x%p\n\n", (void*) type);
OutputDebugPrintf ("type->name() = \"%s\"\n", type->name());
OutputDebugPrintf ("cType->sizeOrOffset = %zu\n\n", (size_t) cType->sizeOrOffset);
#undef RVA_TO_VA_
}
}
OutputDebugPrintf ("%s(): End\n", __func__);
return EXCEPTION_CONTINUE_SEARCH;
}
//--------------------------------------------------------------------------------------------------
void OutputDebugPrintf (const char* format, ...)
{
static char buf [1024] = "";
va_list arg; va_start (arg, format);
_vsnprintf_s (buf, sizeof (buf) - 1, _TRUNCATE, format, arg);
va_end (arg);
OutputDebugString (buf);
printf ("%s", buf);
}
//--------------------------------------------------------------------------------------------------
int main()
{
OutputDebugPrintf ("\n%s(): Start\n", __func__);
AddVectoredExceptionHandler (1, VectoredExceptionHandler);
struct meow_exception { int code = 3; };
try
{
throw meow_exception();
}
catch (const meow_exception& e)
{
OutputDebugPrintf ("\n%s(): catch (meow_exception { %d })\n", __func__, e.code);
}
catch (...)
{
OutputDebugPrintf ("\n%s(): catch (...)\n", __func__);
}
OutputDebugPrintf ("\n%s(): End\n", __func__);
return 0;
}
編譯選項:
// Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24213.1 (part of VS 2015 SP3)
cl /c code.cpp /EHsc /W4
link code.obj kernel32.lib /machine:x86 /subsystem:console /debug
預先感謝您的答案和建議。