我正在学习英特尔引脚,我在我的pintool的主要功能中写下以下代码。
try
{
throw std::exception("test daniel");
}
catch (std::exception& e)
{
printf(e.what());
}
运行它(pin.exe -t Test.dll - calc.exe),但它刚刚崩溃,这肯定是由于未捕获的异常。 但我想知道为什么我的"赶上"代码失败。
任何人都知道原因,或者如何在pintool中捕获异常?
答案 0 :(得分:1)
假设您拥有所有必需的编译选项,以下是如何在pintool中捕获抛出的异常。应该注意的是,除了捕获引脚或工具(不是应用程序)引发的异常之外,这个简单的pintool不会做任何事情。
您将注意到异常处理函数的注册在PIN_StartProgram()函数之前发生,否则将忽略异常。
最后,尽管文档中没有提到,但我希望在调用PIN_AddInternalExceptionHandler()之后和PIN_StartProgram()之前抛出的异常不被处理程序捕获。我希望处理程序能够捕获在PIN_StartProgram()之后抛出的异常,但同样,文档中没有提到它。
//-------------------------------------main.cpp--------------------------------
#include "pin.h"
#include <iostream>
EXCEPT_HANDLING_RESULT ExceptionHandler(THREADID tid, EXCEPTION_INFO *pExceptInfo, PHYSICAL_CONTEXT *pPhysCtxt, VOID *v)
{
EXCEPTION_CODE c = PIN_GetExceptionCode(pExceptInfo);
EXCEPTION_CLASS cl = PIN_GetExceptionClass(c);
std::cout << "Exception class " << cl;
std::cout << PIN_ExceptionToString(pExceptInfo);
return EHR_UNHANDLED ;
}
VOID test(TRACE trace, VOID * v)
{
// throw your exception here
}
int main(int argc, char *argv[])
{
// Initialize PIN library. This was apparently missing in your tool ?
PIN_InitSymbols();
if( PIN_Init(argc,argv) )
{
return Usage();
}
// Register here your exception handler function
PIN_AddInternalExceptionHandler(ExceptionHandler,NULL);
//register your instrumentation function
TRACE_AddInstrumentFunction(test,null);
// Start the program, never returns...
PIN_StartProgram();
return 0;
}