0

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?

Daniel King
  • 407
  • 4
  • 11
  • No direct answer yet, but I found out the global uncaught exception handler set by PIN_AddInternalExceptionHandler works. just like what [SetUnhandledExceptionFilter](http://msdn.microsoft.com/en-us/library/windows/desktop/ms680634(v=vs.85).aspx) does. – Daniel King May 08 '14 at 06:23
  • Where you actually inserted this piece of code matters to understand your issue. The fact that you describe it this way : "in main function of my pintool" suggest that you have not understood how pintools work, especially what analysis and instrumentation time are. As such, this cannot really be considered as a question. Said differently, this question is not precise enough to be addressed. you should explain your issue in more details. – Heyji Sep 14 '15 at 21:27
  • @Heyji Apparently this is in analysis time in main, it fails to catch the thrown exception, the code is so short and repeatable, Why is it not precise enough? – Daniel King Oct 29 '15 at 02:59
  • 1
    I suggest that you edit your question and show the whole code of your tool, so that we understand better what you are trying to achieve. Are you trying to catch exception raised by the application being analysed (calc.exe in your case) or raised by your tool (Test.dll) ? – Heyji May 10 '16 at 20:28

1 Answers1

1

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;
}
Heyji
  • 1,113
  • 8
  • 26
  • good codes, but it still seems a global uncaught exception handler, which is not responsible for a specific piece of codes, but for global codes(even not so global as you mentioned), this is not what the try/catch designed for, try/catch only powerful when nested. – Daniel King Feb 17 '16 at 15:31