如何使用 PIN 工具获取特定函数调用的内存读写跟踪

问题描述

我正在尝试为我的应用程序中的特定函数调用转储内存 rd/wr 跟踪,经过一番研究后,我找到了一个解决方案。

但由于我对 PIN 的使用非常陌生,我不确定如何将例程名称(请参阅 Routine(RTN rtn,VOID *v))从应用程序传递到 pin 工具,以便触发正确的回调函数。有人可以帮忙吗?

截至目前,如果我运行给定的 pin 工具,我的 trace.out 是空的,因为“!isROI”始终设置为 false。

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

const CHAR * ROI_BEGIN = "__parsec_roi_begin";
const CHAR * ROI_END = "__parsec_roi_end";

FILE * trace;
bool isROI = false;

// Print a memory read record
VOID RecordMemRead(VOID * ip,VOID * addr,CHAR * rtn)
{
    // Return if not in ROI
    if(!isROI)
    {
        return;
    }

    // Log memory access in CSV
    fprintf(trace,"%p,R,%p,%s\n",ip,addr,rtn);
}

// Print a memory write record
VOID RecordMemWrite(VOID * ip,W,rtn);
}

// Set ROI flag
VOID StartROI()
{
    isROI = true;
}

// Set ROI flag
VOID StopROI()
{
    isROI = false;
}

// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins,VOID *v)
{
    // Instruments memory accesses using a predicated call,i.e.
    // the instrumentation is called iff the instruction will actually be executed.
    //
    // On the IA-32 and Intel(R) 64 architectures conditional moves and REP 
    // prefixed instructions appear as predicated instructions in Pin.
    UINT32 memOperands = INS_MemoryOperandCount(ins);

    // Iterate over each memory operand of the instruction.
    for (UINT32 memOp = 0; memOp < memOperands; memOp++)
    {
        // Get routine name if valid
        const CHAR * name = "invalid";
        if(RTN_Valid(INS_Rtn(ins))) 
        {
            name = RTN_Name(INS_Rtn(ins)).c_str();
        }

        if (INS_MemoryOperandisRead(ins,memOp))
        {
            INS_InsertPredicatedCall(
                ins,IPOINT_BEFORE,(AFUNPTR)RecordMemRead,IARG_INST_PTR,IARG_MEMORYOP_EA,memOp,IARG_ADDRINT,name,IARG_END);
        }
        // Note that in some architectures a single memory operand can be 
        // both read and written (for instance incl (%eax) on IA-32)
        // In that case we instrument it once for read and once for write.
        if (INS_MemoryOperandisWritten(ins,(AFUNPTR)RecordMemWrite,IARG_END);
        }
    }
}

// Pin calls this function every time a new rtn is executed
VOID Routine(RTN rtn,VOID *v)
{
    // Get routine name
    const CHAR * name = RTN_Name(rtn).c_str();

    if(strcmp(name,ROI_BEGIN) == 0) {
        // Start tracing after ROI begin exec
        RTN_Open(rtn);
        RTN_InsertCall(rtn,IPOINT_AFTER,(AFUNPTR)StartROI,IARG_END);
        RTN_Close(rtn);
    } else if (strcmp(name,ROI_END) == 0) {
        // Stop tracing before ROI end exec
        RTN_Open(rtn);
        RTN_InsertCall(rtn,(AFUNPTR)StopROI,IARG_END);
        RTN_Close(rtn);
    }
}

// Pin calls this function at the end
VOID Fini(INT32 code,VOID *v)
{
    fclose(trace);
}

/* ===================================================================== */
/* Print Help Message                                                    */
/* ===================================================================== */

INT32 Usage()
{
    PIN_ERROR( "This Pintool prints a trace of memory addresses\n" 
              + KNOB_BASE::StringKnobSummary() + "\n");
    return -1;
}

/* ===================================================================== */
/* Main                                                                  */
/* ===================================================================== */

int main(int argc,char *argv[])
{
    // Initialize symbol table code,needed for rtn instrumentation
    PIN_InitSymbols();

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

    // Open trace file and write header
    trace = fopen("roitrace.csv","w");
    fprintf(trace,"pc,rw,rtn\n");

    // Add instrument functions
    RTN_AddInstrumentFunction(Routine,0);
    INS_AddInstrumentFunction(Instruction,0);
    PIN_AddFiniFunction(Fini,0);

    // Never returns
    PIN_StartProgram();

    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)