본문 바로가기
pintool

Managed platforms support2

by sonysame 2018. 10. 2.
#include "pin.H"
#include <iostream>
#include <fstream>

// =====================================================================
// Global variables
// =====================================================================

std::ostream * out = &cerr;

// =====================================================================
// Command line switches
// =====================================================================

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

// =====================================================================
// Utilities
// =====================================================================

// Print out help message.
INT32 Usage()
{
    cerr << "This tool prints out the stack filtered by the dynamicaly created functions only" << endl;
    cerr << KNOB_BASE::StringKnobSummary() << endl;
    return -1;
}

// =====================================================================
// Analysis routines
// =====================================================================

VOID RtnCallPrint(CHAR * rtnName)
{
    *out << "Before run " << rtnName << endl;
}

// =====================================================================
// Instrumentation callbacks
// =====================================================================

// Pin calls this function every time a new rtn is executed
VOID Routine(RTN rtn, VOID *v)
{
    if (!RTN_IsDynamic(rtn))
    {
        return;
    }

    *out << "Just discovered " << RTN_Name(rtn) << endl;

    RTN_Open(rtn);

    // Insert a call at the entry point of a routine to increment the call count
    RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)RtnCallPrint, IARG_ADDRINT, RTN_Name(rtn).c_str(), IARG_END);

    RTN_Close(rtn);
}

// Print out analysis results.
// This function is called when the application exits.
// @param[in]   code            exit code of the application
// @param[in]   v               value specified by the tool in the
//                              PIN_AddFiniFunction function call
VOID Fini(INT32 code, VOID *v)
{
    const string fileName = KnobOutputFile.Value();
    if (!fileName.empty())
    {
        delete out;
    }
}

// The main procedure of the tool.
// This function is called when the application image is loaded but not yet started.
// @param[in]   argc            total number of elements in the argv array
// @param[in]   argv            array of command line arguments,
//                              including pin -t <toolname> -- ...
int main(int argc, char *argv[])
{
    // Initialize symbol processing
    PIN_InitSymbols();

    // Initialize PIN library. Print help message if -h(elp) is specified
    // in the command line or the command line is invalid
    if(PIN_Init(argc,argv))
    {
        return Usage();
    }

    const string fileName = KnobOutputFile.Value();

    if (!fileName.empty())
    {
        out = new std::ofstream(fileName.c_str());
    }

    // Register Routine to be called to instrument rtn
    RTN_AddInstrumentFunction(Routine, 0);

    // Register function to be called when the application exits
    PIN_AddFiniFunction(Fini, NULL);

    // Start the program, never returns
    PIN_StartProgram();

    return 0;
}

RTN_AddInstrumentFunction을 이용해서 just compiled된 function을 instrument할 수 있다.

본 예시는 Jitting과 dynamic function을 기록하는 프로그램을 instrument 하는 예시이다. 

'pintool' 카테고리의 다른 글

Find executable img, section, code  (0) 2018.10.12
Disassemble  (0) 2018.10.12
Managed platforms support1  (0) 2018.10.02
Instrumenting Before and After Forks  (0) 2018.10.02
Instrumenting Child Processes  (0) 2018.10.02