Search code examples
intel-pin

How to stop the pintool artificially


I want to use the pin to track the address of the first 256*256 instructions, but I don't know how to stop the pin after the number is reached. I know there is a method PIN_ExitProcess(), but make will cause an error after adding it. Is there any other way?

#include <stdio.h>
#include "pin.H"

FILE * trace;

VOID printip(VOID *ip) { 
    fprintf(trace, "%p\n", ip); 
}

VOID Instruction(INS ins, VOID *v)
{
    if(0x70000000>INS_Address(ins))
        INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)printip, IARG_INST_PTR, IARG_END);
}

VOID Fini(INT32 code, VOID *v)
{
    fprintf(trace, "#eof\n");
    fclose(trace);
}

INT32 Usage()
{
    PIN_ERROR("This Pintool prints the IPs of every instruction executed\n" 
              + KNOB_BASE::StringKnobSummary() + "\n");
    return -1;
}

int main(int argc, char * argv[])
{
    trace = fopen("itrace.out", "w");

    if (PIN_Init(argc, argv)) return Usage();

    INS_AddInstrumentFunction(Instruction, 0);

    PIN_AddFiniFunction(Fini, 0);

    PIN_StartProgram();

    return 0;
}

Solution

  • You have two options:

    1. Simply count the number of traced instructions and stop fprintfing when you reach the threshold using an if statement.
    2. Call PIN_Detach() when the instruction threshold is reached. See more here: https://software.intel.com/sites/landingpage/pintool/docs/97503/Pin/html/group__PIN__CONTROL.html#ga6277d16bf33ede39685a26a92fc3cbef