参考:http://software.intel.com/sites/landingpage/pintool/docs/62732/Pin/html/

http://blog.nruns.com/blog/2013/10/07/TracingExecutionWithPin-Carlos/

Pin is a tool for the instrumentation of programs. It supports the Android*, Linux*, OSX* and Windows* operating systems and executables for the IA-32, Intel(R) 64 and Intel(R) Many Integrated Core architectures.

Pin allows a tool to insert arbitrary code (written in C or C++) in arbitrary places in the executable. The code is added dynamically while the executable is running. This also makes it possible to attach Pin to an already running process.

Pin provides a rich API that abstracts away the underlying instruction set idiosyncracies and allows context information such as register contents to be passed to the injected code as parameters. Pin automatically saves and restores the registers that are overwritten by the injected code so the application continues to work. Limited access to symbol and debug information is available as well.

有点类似于Detours的意义,动态插桩。但是Detours的粒度是函数级别,而Pin的粒度是指令级别。

这也很好理解,Detours是Microsoft开发的,而Pin是Intel开发的。

1. 怎样在Windows下编译samples of Pintools

可以到http://software.intel.com/en-us/articles/pintool-downloads下载不同的Pin版本。

但是在Windows平台下,自版本56759以后,nmake被改成了make,但是内部仍然调用的cl.exe等工具进行生成,make问题不成功,没办法,只好退回49306版本。

cd .\source\tools\SimpleExamples

..\nmake TARGET=ia32 tools

然后,将

.\ia32\bin

.\source\tools\SimpleExamples\obj-ia32

目录下的可执行文件拷贝到目标机器上,即可以按照documentation指示的方法来执行pintools了。

2. 怎样编写自己的pintools

首先需要了解一下回调函数:

Pin提供不同粒度的回调函数,大体上可以分为下面层次:

IMG: Image Object

INS: Instruction Object

SEC: Section Object

RTN: Routine Object

REG: Register Object

TRACE: Single entrance, multiple exit sequence of instructions

BBL: Single entrance, single exit sequence of instructions

SYM: Symbol Object

我们首先编写一下IMG粒度的回调,用来捕获加载image的事件。

在\source\tools下面新建一个目录daniel

结构如下:

-- daniel

| 

 -- daniel.cpp

| 

 -- Nmakefile

|

 -- make.bat

1. daniel.cpp

#include "pin.H"

#include "portability.H"

#include <iostream>

#include <fstream>

 

using namespace std;

static std::ofstream* out = 0;

 

static INT32 Usage()

{

    cerr << "Daniel King's test module\n" << endl;

    return -1;

}

 

 

VOID ImageLoad(IMG img, void* v)

{

    *out << "loading image " << IMG_Name(img) << endl;

}

 

 

 

VOID Fini(int n, void *v)

{    

    *out << "## eof\n";

    out->close();

}

 

int main(int argc, char *argv[])

{

    

    if( PIN_Init(argc,argv) )

    {

        return Usage();

    }

    

        

    string filename =  "daniel.out";

    out = new std::ofstream(filename.c_str());

 

    IMG_AddInstrumentFunction(ImageLoad, 0);

    PIN_AddFiniFunction(Fini, 0);

 

    PIN_StartProgram();

    

    return 0;

}

2. Nmakefile

######################################################################################

# This is the NMAKE file for building and testing PIN toos contained in one of the

# subdirectories of the PinTool project or PIN kit. 

#

# For description of targets and options, see Nmakefile in the root directory.  

######################################################################################

 

!if "$(PIN_HOME)"==""

PIN_HOME=..

!endif

 

# Define tools to be buit and tested

######################################################################################

COMMON_TOOLS= daniel.dll

 

# Include building and testing rules from the root Nmakefile. 

######################################################################################

INCLUDE_SUB_RULES=1

!INCLUDE $(PIN_HOME)\Nmakefile 

3. make.bat

..\nmake target=ia32 tools

以后每次添加新的module时,在Nmakefile中添加生成目标,然后执行make.bat就会在obj-ia32目录下生成目标文件。

测试时,将.\ia32\bin下的pin执行文件,以及刚刚生成的module文件一同拷贝到目标文件夹,编写run.bat:

pin.exe -t %1 -- %2

3. MyPinTool模板

MyPinTool是一个Windows下项目的模板,通过该模板,可以方便地创建自己的项目,并且支持调试。

4. PinTool调试

参照第三步的模板建立的工程文件中,都会设置调试程序。

具体如下:

 

Command: .\ia32\bin\pin.exe

Command Arguments: -t $(TargetPath) -- target.exe

Working Directory: $(TargetDir)

在按下F5后,会弹出如下的提示窗口:

在Visual Studio IDE中选择Debug->Attach to process菜单,按提示的pid选择目标程序,附加到调试器;再到终端窗口下按下Enter键,就可以开启调试了。

5. Pintool线程与Application线程

Pintool线程是各种Callback执行的线程,而Application线程是执行应用程序本身的指令,以及通过InsertXXX插入的Instrument指令的线程。

那么在Callback中的统计,与InsertXXX生成的Instrument函数中的统计有什么区别?

Callback针对同一位置的代码块只统计一次,而InsertXXX是调用了多少次,就统计多少次。

那么Callback是否能够统计完全整个应用程序中的所有代码块吗?估计不能,只是根据执行情况,动态地统计。

// Pin calls this function every time a new instruction is encountered

VOID Instruction(INS ins, VOID *v)

{

    RTN rtn = INS_Rtn(ins);

    if (!RTN_Valid(rtn))

    {

        ++insNoRtnDiscoveredCount;

        INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)InsNoRtnCount, IARG_END);

    }

    else if (RTN_IsDynamic(rtn))

    {

        ++insDynamicDiscoveredCount;

        INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)InsDynamicCount, IARG_END);

    }

    else

    {

        ++insNativeDiscoveredCount;

        INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)InsNativeCount, IARG_END);

    }

}

6. 粒度的理解

Trace instrumentation lets the Pintool inspect and instrument an executable one trace at a time. Traces usually begin at the target of a taken branch and end with an unconditional branch, including calls and returns. Pin guarantees that a trace is only entered at the top, but it may contain multiple exits. If a branch joins the middle of a trace, Pin constructs a new trace that begins with the branch target. Pin breaks the trace into basic blocks, BBLs. A BBL is a single entrance, single exit sequence of instructions. Branches to the middle of a bbl begin a new trace and hence a new BBL. It is often possible to insert a single analysis call for a BBL, instead of one analysis call for every instruction. Reducing the number of analysis calls makes instrumentation more efficient. Trace instrumentation utilizes the TRACE_AddInstrumentFunction API call.

trace是一个比函数小一级的代码块粒度。

trace有如下特征:

6.1 起始

trace由一个branch的目标代码处起始

6.2 结束

遇到非条件的jmp指令,call指令,以及ret指令结束。

因此trace可以包含jcc指令,代表着一个trace可以有多个出口,即trace中的代码可能会有多条执行路径,即手册中所说的“一个入口,多个出口”。

trace会在实际执行中不断地划分,如果trace中包含了子trace,即有其他代码跳转到trace中的某个位置,这时需要细分trace.

BBL是比trace再小一个级别的代码块,它符合“一个入口,一个出口”的特点。

因此,我们可以在实际使用中,将BBL作为代码块的最小单位,以减少插桩的次数。

在静态分析代码方面

代码段虽然在PE文件中有定义明确的范围,但是在这个范围内,仍然是一坨,可以通过export table entry,以及symbol table进行更细粒度的划分,但是许多release版本的可执行文件就不好更加详细地划分了。

怎样静态地解析出比单条指令更加粗粒度的代码块,比如BBL级别,是一个需要解决的问题。

Intel Pin基础的更多相关文章

  1. Intel pin 2.14/CentOS 6 X86-64/安装

    环境:Intel Pin 2.14 CentOS 6 X86-64 --linux.tar.gz 进入 ./source/tools/ManualExamples make all TARGET=in ...

  2. ASM:《X86汇编语言-从实模式到保护模式》第16章:Intel处理器的分页机制和动态页面分配

    第16章讲的是分页机制和动态页面分配的问题,说实话这个一开始接触是会把人绕晕的,但是这个的确太重要了,有了分页机制内存管理就变得很简单,而且能直接实现平坦模式. ★PART1:Intel X86基础分 ...

  3. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  4. Tracing Memory access of an oracle process : Intel PinTools

    https://mahmoudhatem.wordpress.com/2016/11/07/tracing-memory-access-of-an-oracle-process-intel-pinto ...

  5. 【转帖】龙芯3A3000处理器深度评测:和Intel、AMD差距巨大

    龙芯3A3000处理器深度评测:和Intel.AMD差距巨大 https://www.eefocus.com/mcu-dsp/424623/r0 作者非计算机科班毕业 让我汗颜. 我计算机毕业都不知道 ...

  6. Vuzzer自动漏洞挖掘工具简单分析附使用介绍

    Vuzzer 是由计算机科学机构  Vrije Universiteit Amsterdam.Amsterdam Department of Informatics 以及 International ...

  7. linux-2.6.18源码分析笔记---中断

    一.中断初始化 中断的一些硬件机制不做过多的描述,只介绍一些和linux实现比较贴近的机制,便于理解代码. 1.1 关于intel和linux几种门的简介 intel提供了4种门:系统门,中断门,陷阱 ...

  8. pin-a-binary-instrumentation-tool

    https://software.intel.com/en-us/articles/pin-a-binary-instrumentation-tool-downloads Introduction t ...

  9. [Cnbeta]龙芯处理器性能怎么样

    龙芯处理器性能怎么样?下一代CPU同频性能可达英特尔90% 在高性能处理器领域,英特尔是天花顶一般的存在(先不算地位特殊的IBM公司),国内发展CPU处理器的公司很多,绝大多数实力跟英特尔相比都差很远 ...

随机推荐

  1. 架构师技能树skill-map

    # 架构师技能树 ## 系统架构能力 ### 基本理论- 扩展性设计- 可用性设计- 可靠性设计- 一致性设计- 负载均衡设计- 过载保护设计 ### 协议设计- 二进制协议- 文本协议 ### 接入 ...

  2. 没有找到MSVCR110.dll,因此这个应用程序未能启动.重新安装应用程序可能会修复此问题

    问题: 在win7下用vs2012编译了一个exe放到xp上运行,弹出错误框"没有找到MSVCR110.dll,因此这个应用程序未能启动.重新安装应用程序可能会修复此问题" 解决办 ...

  3. LeetCode N皇后 & N皇后 II

    题目链接:https://leetcode-cn.com/problems/n-queens/ 题目链接:https://leetcode-cn.com/problems/n-queens-ii/ 题 ...

  4. flex属性flex-grow、flex-shrink、flex-basis

    tip: 1)这些属性写在子元素中,作用于子元素(父元素中应设置display:flex) 2)作用是子元素如何分配父元素的空间 3) flex-grow 是扩展比率,当子元素宽度总和小于父元素宽度时 ...

  5. java 重新学习 (四)

    一.内部类成员可以直接访问外部类的私有数据,因为内部类被当成其外部类成员,同一个类中的成员之间可以相互访问.但外部类不能访问内部类的实现细节,例如内部类的成员变量.匿名内部类适合用于创建仅需要一次使用 ...

  6. Django Model里的__str__以及Meta

    举个栗子,注释已经比较详细了 name = models.CharField(max_length=30,verbose_name='标签名称') #max_length=30里的30在mysql以前 ...

  7. UVA 10806 Cheerleaders

    Cheerleaders Description   C Cheerleaders In most professional sporting events, cheerleaders play a ...

  8. python之命名元组的好处

    collections.namedtuple() 命名元组的一个主要用途是将你的代码从下标操作中解脱出来举例使用 # 使用 from collections import namedtuple Sub ...

  9. 博客中引入了gitment评论系统

    官方github地址:https://github.com/imsun/gitment 官方中文说明地址:https://imsun.net/posts/gitment-introduction/ 官 ...

  10. PHP-删除排序数组中的重复项

    给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...