본문 바로가기
pintool

Simple Instruction Count(Instruction Instrument)

by sonysame 2018. 9. 11.

Simple Instruction Count


The example below instruments a program to count the total number of instructions executed. It inserts a call to docount before every instruction. When the program exits, it saves the count in the file inscount.out



MAIN 함수


PIN_Init(argc, argv) : Initialize pin


OutFile.open(KnobOutputFile.Value().c_str()) : output 파일을 오픈한다.


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


PIN_AddFiniFunction(Fini, 0) : Register Fini to be called when the application exits


PIN_StartProgram() : Start the program, never returns




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



PIN_CALLBACK LEVEL_PINCLIENT::INS_AddInstrumentFunction(INS_INSTRUMENT_CALLBACK fun, VOID * val)

Add a function used to instrument at instruction granularity.

fun: Instrumentation function for instructions


VOID Instruction(INS ins, VOID *v)

{

INS_INSERTCALL(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);//insert a call to docount before every instruction, no arguments are passed.

}



VOID LEVEL_PINCLIENT::INS_InsertCall(INT ins, IPOINT action, AFUNPTR funptr,...)

Insert a call to funptr relative to instruction ins.

ins: Instruction to instrument

action: Specifies before, after, etc

IPOINT_BEFORE is always valid for all instructions

IPOINT_AFTER is valid only when a fall-through exists.

IPOINT_TAKEN_BRANCH is invalid for non-branches

funptr: Insert a call to funptr



static UNIT64 icount = 0;

VOID docount(){

icount++;

}


PIN_CALLBACK LEVEL_PINCLIENT::PIN_AddFiniFunction(FINI_CALLBACK fun, VOID * val)

Call func immediately before the application exits. The function is not an instrumentation function--it cannot insert instrumentation. There can be more than one Fini funcion.



VOID Fini(INT32 code, VOID *v) ->this function is called when the application exits.

{

OutFile.setf(ios::showbase); //출력에 진법 표시 접두어(0,0x)를 표시

OutFile << "Count " << icount << endl;

OutFile.close();

}




참고자료

http://kcoder.tistory.com/entry/C-cout-%EC%9D%98-%EB%AA%A8%EB%93%A0-%EA%B2%83-%EC%82%AC%EC%9A%A9%EB%B2%95-%EC%86%8C%EC%88%98%EC%A0%90-%EC%B6%9C%EB%A0%A5%EC%96%91%EC%8B%9D



#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;
}