Daniel King
Daniel King

Reputation: 405

Intel pin tool cannot catch thrown exceptions

I am now learning Intel pin, I write the following codes in main function of my pintool.

try
{
    throw std::exception("test daniel");
}
catch (std::exception& e)
{
    printf(e.what());
}

Run it( pin.exe -t Test.dll -- calc.exe), but it just crashed, this is definitely due to an uncaught exception. But I wonder why my "catch" code failed.

Anyone know the reason, or how to catch exception in pintool?

Upvotes: 0

Views: 1207

Answers (1)

Heyji
Heyji

Reputation: 1211

Here is how thrown exceptions should be catched in a pintool, assuming you have all the mandatory compile options. It should be noted that this simple pintool does not do anything beside catching exceptions thrown by pin or the tool (not the application).

You will note that the registration of the exception handler function occures before the PIN_StartProgram() function, otherwise exceptions will be ignored.

Finally, although it is not mentioned in the documentation, I would expect that exceptions thrown after the call to PIN_AddInternalExceptionHandler() and before PIN_StartProgram() be not catched by the handler. I would instead expect the handler to catch exceptions thrown after PIN_StartProgram(), but again, it is not mentioned in the documentation.

//-------------------------------------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;
}

Upvotes: 1

Related Questions