Pin学习笔记1

1.      类似于valgrind,pin也是一个用于编写动态分析程序的框架。Pin由Intel维护,因而貌似更加强大。Pin支持的Linux二进制,包括IA-32,Intel-64,Itanium等处理器;支持的windows二进制,包括IA-32和Intel64;支持的MacOS二进制,包括IA-32。Pin采取的思路是“运行时instrumentation”,而不是静态地重写二进制代码,这样,Pin可以处理一个正在运行的进程。

      Pin的特色是提供了大量地API,而且文档比较详细。


2. 一个完整的例子

#include <iostream>
#include <fstream>
#include "pin.H"

// The running count of instructions is kept here
// make it static to help the compiler optimize docount
static UINT64 icount = 0;

// This function is called before every instruction is executed
VOID docount() { icount++; }
    
// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
    // Insert a call to docount before every instruction, no arguments are passed
    INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);
}

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
    "o", "inscount.out", "specify output file name");

// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
    // Write to a file since cout and cerr maybe closed by the application
    ofstream OutFile;
    OutFile.open(KnobOutputFile.Value().c_str());
    OutFile.setf(ios::showbase);
    OutFile << "Count " << icount << endl;
    OutFile.close();
}

/* ===================================================================== */
/* Print Help Message                                                    */
/* ===================================================================== */

INT32 Usage()
{
    cerr << "This tool counts the number of dynamic instructions executed" << endl;
    cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
    return -1;
}

/* ===================================================================== */
/* Main                                                                  */
/* ===================================================================== */
/*   argc, argv are the entire command line: p in -t <toolname> -- ...    */
/* ===================================================================== */

int main(int argc, char * argv[])
{
    // Initialize pin
    if (PIN_Init(argc, argv)) return Usage();

    // Register Instruction to be called to instrument instructions
    INS_AddInstrumentFunction(Instruction, 0);

    // Register Fini to be called when the application exits
    PIN_AddFiniFunction(Fini, 0);
    
    // Start the program, never returns
    PIN_StartProgram();
    
    return 0;
}


3. 其它

3.1 例子的说明

      该例子实现了一个统计执行的指令数目的功能。可以看出,一个Pin工具主要包括4个部分:

  1. 初始化Pin环境:PIN_Init
  2. 设置用于Instrument的回调函数:INS_AddInstrumentFunction添加一个回调函数,该回调函数的执行时机是每执行一条指令
  3. 应用结束前工作,与PIN_Init对应:PIN_AddFiniFunction
  4. 启动Instrument过程

3.2 其它函数

函数名执行时机回调函数原型常用函数
INS_AddInstrumentFunction每执行一条新指令(INS, void *)INS_InsertCall
TRACE_AddInstrumentFunction每执行一个新trace(基本块)(TRACE, void *)BBL_InsertCall
RTN_AddInstrumentFunction每执行一个新函数(RTN, void *)RTN_InsertCall
IMG_AddInstrumentFunction每加载一个新映像(IMG, void *)
IMG_AddUnloadFunction每卸载一个映像


参考:

1. http://www.pintool.org

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值