【问题标题】:Intel pin tool cannot catch thrown exceptions英特尔 pin 工具无法捕获抛出的异常
【发布时间】:2014-06-21 08:38:01
【问题描述】:

我现在正在学习 Intel pin,我在 pintool 的 main 函数中编写了以下代码。

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

运行它(pin.exe -t Test.dll -- calc.exe),但它只是崩溃了,这肯定是由于未捕获的异常。 但我想知道为什么我的“catch”代码失败了。

谁知道原因,或者如何在 pintool 中捕获异常?

【问题讨论】:

  • 目前还没有直接的答案,但我发现 PIN_AddInternalExceptionHandler 设置的全局未捕获异常处理程序有效。就像SetUnhandledExceptionFilter 所做的一样。
  • 您实际插入这段代码的位置对于理解您的问题很重要。您这样描述它的事实:“在我的 pintool 的主要功能中”表明您还没有了解 pintools 的工作原理,尤其是分析和检测时间是什么。因此,这不能真正被视为一个问题。换句话说,这个问题不够精确,无法解决。你应该更详细地解释你的问题。
  • @Heyji 显然这是在main中的分析时间,它无法捕捉到抛出的异常,代码如此短且可重复,为什么它不够精确?
  • 我建议您编辑您的问题并显示您的工具的整个代码,以便我们更好地了解您想要实现的目标。您是否正在尝试捕获正在分析的应用程序(在您的情况下为 calc.exe)或您的工具(Test.dll)引发的异常?

标签: exception intel-pin


【解决方案1】:

假设您拥有所有必需的编译选项,下面是如何在 pintool 中捕获抛出的异常。应该注意的是,这个简单的 pintool 除了捕获 pin 或工具(而不是应用程序)抛出的异常之外,并没有做任何事情。

您会注意到异常处理函数的注册发生在 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;
}

【讨论】:

  • 不错的代码,但它似乎仍然是一个全局未捕获的异常处理程序,它不负责特定的一段代码,但是对于全局代码(即使没有你提到的那么全局),这不是什么为 try/catch 设计的,try/catch 只有在嵌套时才强大。
猜你喜欢
  • 1970-01-01
  • 2019-03-03
  • 2021-09-02
  • 2011-04-05
  • 2015-09-23
  • 2013-08-02
  • 2018-05-30
  • 2014-11-09
  • 1970-01-01
相关资源
最近更新 更多