pin 的使用简介 ——环境设置 基本工具的使用 相关知识

来源:互联网 发布:adobe创意云 mac 编辑:程序博客网 时间:2024/06/17 17:50
  pin 是intel公司开发的用于程序测试的一款工具软件,支持32位、64位的Linux和Windows的可执行程序,可以检测程序运行过程中的命令、内存、地址等的详细信息。
下面我将介绍如何使用。
    设置pin的编译环境pin没有图形界面窗口,全部操作均需在dos界面下完成,要运行pin首先要安装对应版本visual studio作为编译工具,我用的是vs 2010,运行vs10的命令提示窗口,用cd命令将当前目录设置到source/tools,在窗口中运行nmake.bat,建立pin的运行环境,不好意思,在此之前还需要将pin加入环境变量,不然将无法识别pin指令将软件根目录pin-2.11-49306-msvc10-ia32_intel64-windows加入环境变量地址。在一个目录下建立所有的pin tool 示例:
                           cd source\tools\ManualExamples
                           ..\nmake tools,

以inscount0.dll为例介绍设置好之后

    运行pin:此时运行pin.exe或pin.bat会看到如下提示:Usage:pin [-t <Tool DLL> [Tool Args]] -- <App EXE> [App args],即可清楚知道pin的参数,tool-dll表示已编译的pintool,App EXE是要监测的程序,  例如监测软件根目录下的一个名为hello的程序:pin -t source\tools\ManualExamples\obj-ia32\inscount0.dll -- hello,在软件目录下可以看到输出结果inscount.out: Count 545071,我们还可以对输出文件进行设置:在工具名和短划线之间加入 -o <file_name>,可以改变输出文件的位置和格式。那一长串的文件名输着很麻烦,将工具拷贝到pin主目录下就可以直接引用了,条件是你已将主目录加入环境变量。

下面是Manual 给出的一些pin tool的用途:

imageload      跟踪程序上传和下载image
inscount0      记录程序运行指令总数
itrace         显示每个指令执行的内存地址
malloctrace    记录函数参数传递到函数的值或返回的值
pinatrace      检测指令读取和写入的内存地址
proccount      记录程序被调用的次数



  没有编译的工具:修改工具所在目录下的Nmakefile,COMMON_TOOLS=后面添加<tool_name>.dll,注意换行时需在行末加上"\",没有Nmakefile文件的可从其他目录拷贝一个过来进行修改。
  下面晒一下寻找过程,或许对你有启发:在使用pintool的过程中,我发现有些工具没有编译,既没有在相应目录下的obj-ia32文件夹中找到其对应的.dll文件,博主想或许这个工具需要专门进行编译,于是在窗口中输入..\nmake ***.dll,结果显示nmake不知道如何生成***.dll文件,这个方法失败了。
为什么同一目录下的有些编译了有些却没有,肯定是设置问题,想到manual中介绍tool编译一句话:Refer to source/tools/Nmakefile for a full list of supported targets and build configurations.
找到根目录下的Nmakefile文件,打开,发现时一大堆代码,如果这里是设置编译对象的地方,已经编译好的工具的名字一定会出现在代码里,Ctrl+F,结果没有找到,看来这个文件是通用的。再打开一个tool目录下的Nmakefile文件,看到COMMON_TOOLS=一行,眼前一亮:编译好的工具的名字出现了,再打开其他tool目录下的Nmakefile文件,发现只有这一行不同,于是放心大胆地修改命令行......

  Manual 文件至提供了为数不多的几个pin tool的说明,要弄清楚tool的用途,只能去看源代码


感想:仔细想想,从13号开始看pin的说明,到今天已经整整一周了,而上面所写的那点东西是一周的全部工作,效率颇低,顿感压力山大,其中还多亏了师兄的指导。今天看了一天的pin tool的cpp,还是没能弄明白:

pin -t inscount0.so -o inscount0.log -- /bin/ls
inscout.cpp代码如下:

#include <iostream>#include <fstream>#include "pin.H"ofstream OutFile;// The running count of instructions is kept here// make it static to help the compiler optimize docountstatic UINT64 icount = 0;// This function is called before every instruction is executedVOID docount() { icount++; }    // Pin calls this function every time a new instruction is encounteredVOID 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 exitsVOID Fini(INT32 code, VOID *v){    // Write to a file since cout and cerr maybe closed by the application    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: pin -t <toolname> -- ...    *//* ===================================================================== */int main(int argc, char * argv[]){    // Initialize pin    if (PIN_Init(argc, argv)) return Usage();    OutFile.open(KnobOutputFile.Value().c_str());    // 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;}

pin定义了很多自己的函数,看懂这些函数式关键

待续...

原创粉丝点击