pin 的使用簡介 ——環境設置 基本工具的使用 相關知識

  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 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
    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定義了很多自己的函數,看懂這些函數式關鍵

待續...


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章