Pin使用小结
1.Pintools四大类
- 指令级插桩(instruction instrumentatio),通过函数INS_AddInstrumentFunctio实现。
- 轨迹级插装(trace instrumentation),通过函数TRACE_AddInstrumentFunction实现。(貌似就是基本块插装)
- 镜像级插装(image instrumentation),使用IMG_AddInstrumentFunction函数,由于其依赖于符号信息去确定函数边界,因此必须在调用PIN_Init之前调用PIN_InitSymbols。
- 函数级的插装(routine instrumentation),使用RTN_AddInstrumentFunction函数。函数级插装比镜像级插装更有效,因为只有镜像中的一小部分函数被执行。
主要分为上述的四种插装粒度。其中,IMG_AddInstrumentFunction和RTN_AddInstrumentFunction需要先调用PIN_InitSymbols(),来分析出符号。在无符号的程序中,IMG_AddInstrumentFunction和RTN_AddInstrumentFunction无法分析出相应的需要插装的块。
在各种粒度的插装函数调用时,可以添加自己的处理函数在代码中,程序被加载后,在被插装的代码运行时,自己添加的函数会被调用。
INS_AddInstrumentFunctio、TRACE_AddInstrumentFunction、IMG_AddInstrumentFunction、RTN_AddInstrumentFunction指定的回调函数只有在相应的代码被分析到时才会被调用,即分析到一次只被调用一次,但程序运行过程中一般不再被调用,但INS_InsertCall之类的程序添加的函数,是在相应的代码位置添加函数,根据程序运行的情况,会被多次调用。
在INS_AddInstrumentFunctio指令级插装的代码中,只有在INS_AddInstrumentFunctio指定的函数被调用时INS指令才有效,在INS_InsertCall函数中,INS无效。
2. Pin的使用
一个简单的例子。使用inscount.so来对一个程序执行的指令数进行计数
进入ManulExample该目录 …/…/…/pin –t obj-ia32/inscount0.so –o inscount0.log – /bin/ls 最后生成的log就在该目录里面
…/…/…/pin –t ./obj-ia32/inscount0.so –o inscount0.log – /bin/ls也可以
执行后查看结果 head xxx.out