How can I find the value of a write instruction using the Intel Pin tool once writing has finished instantly in multithread programs?

29 Views Asked by At

I'm trying to extract the order of instructions from multithread C++ programs. I need to know the thread ID, the type of instruction (read or write), the memory address that the instruction affects, and the read or write value. There is no issue with the read instruction; however, the write instruction is differ from read.

Is there a way to read the value that will be written from the register or obtain the written value right away as the write instruction has completed?

Following is the code of mytool.

`#include <stdio.h>
#include "pin.H"
using namespace std;


// The running count of instructions is kept here
FILE *trace;
PIN_LOCK pinLock;

// This function is called before every read instruction is executed
VOID recordMemRead(void *ip, void *address, THREADID tid) 
{ 
PIN_GetLock(&pinLock, tid + 1);
//ADDRINT * addr_ptr = (ADDRINT*)address;
ADDRINT value;
PIN_SafeCopy(&value, address, sizeof(ADDRINT));
fprintf(trace, "Thread %d: R from %p %d\n", tid, address, value);
fflush(trace);
    PIN_ReleaseLock(&pinLock);
}

// This function is called before every write instruction is executed
VOID recordMemWrite(void *ip, void *address, THREADID tid) 
{ 
PIN_GetLock(&pinLock, tid + 1);
ADDRINT value;
PIN_SafeCopy(&value, address, sizeof(ADDRINT));
fprintf(trace, "Thread %d: W to %p %d\n", tid, address, value);
fflush(trace);
    PIN_ReleaseLock(&pinLock);
}


// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
if(INS_IsBranchOrCall(ins)) 
        return;
    if(INS_IsStackRead(ins))
        return;
    if(INS_IsStackWrite(ins))
        return;  
if (INS_IsMemoryRead(ins))
{
    INS_InsertPredicatedCall(
    ins,
    IPOINT_BEFORE,
    (AFUNPTR)recordMemRead,
    IARG_INST_PTR,
    IARG_MEMORYREAD_EA,
    IARG_THREAD_ID,
    IARG_END);
}
if (INS_IsMemoryWrite(ins))
{
    //REG reg = INS_OperandMemoryBaseReg(ins,0);
    INS_InsertPredicatedCall(
    ins,
    IPOINT_BEFORE,
    (AFUNPTR)recordMemWrite,
    IARG_INST_PTR,
    IARG_MEMORYWRITE_EA,
    IARG_THREAD_ID,
    //reg,
    IARG_END);
}
}
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
fclose(trace);
}
// argc, argv are the entire command line, including pin -t <toolname> -- ...
int main(int argc, char * argv[])
{
trace = fopen("my.out", "w");
// Initialize pin
PIN_Init(argc, argv);
// 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;
}`
0

There are 0 best solutions below