I'm working on small Windows Exception handling engine trying to gather maximum information from the system, including C++ exceptions RTTI.
In a 32-bit VectoredExceptionHandler compiled by MSVS 2015, I successfully can obtain std::type_info pointer to RTTI of the type being thrown. It can easily be found in ((_ThrowInfo*) ExceptionPointers->ExceptionRecord->ExceptionInformation[2])->pCatchableTypeArray->arrayOfCatchableTypes[0]
(see classic article of Raymond Chen, some definitions from MS's ehdata.h
file and many others). This method is based on fetching pCatchableTypeArray
member of MSVC built-in _ThrowInfo
structure data which is built by the compiler.
But in 64-bit environment, _ThrowInfo
contains no direct RTTI: unfortunately, pCatchableTypeArray
is NULL. In disassembly window, I see that it is NULL even before a call to _CxxThrowException
, main MS throw-handler. I searched through a number of articles concerning new 64-bit exception-handling mechanism used in MSVC, but there is no information on RTTI. But maybe I missed something.
Are there any ways to obtain std::type_info (or simply type name) of the C++ exception being thrown in a vectored exception handler working in 64-bit MSVC environment?
Here's the output of dumping 32-bit and 64-bit exception info:
32-bit (RTTI success):
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-bit (RTTI failure)
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 })
The code used to get these dumps:
#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;
}
Build options:
// 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
Thank you in advance for the answers and advices.