c++ - 英特尔引脚 : How to generate objectdump-ish code?

标签 c++ intel-pin

我正在尝试使用 intel-pin 工具。我有一个简单的 Hello-world.c(它只打印“Hello world”)程序(比如 a.out)。如果我想从二进制文件生成程序集,我会执行 objdump -D a.out

我想在其中使用一些指令。

是否有可能在检测之前(这可以通过 objdump 轻松完成)和之后使用 pin 工具获取 objdump?

我创建了一个打印所有指令的工具。

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

FILE * trace;

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "pinatrace.out","A pin tool");

VOID Count(INS ins, void *v) {


        fprintf(trace,"\n%s",(INS_Disassemble(ins)).c_str());

}

VOID Fini(INT32 code, VOID *v)
{
        printf("count = %ld\n",(long)icount);
        fprintf(trace, "#eof\n");
        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[])
{
    if (PIN_Init(argc, argv)) return Usage();

    trace = fopen("pinatrace.out", "w");


    INS_AddInstrumentFunction(Count, 0);
    PIN_AddFiniFunction(Fini, 0);
    // Never returns
    PIN_StartProgram();

    return 0;
}

它打印了汇编指令,但我不确定它是否包含检测指令。

这是执行此操作的正确方法吗?你能帮帮我吗?

最佳答案

Is it possible to get objdump using pin tool, before (This can be easily done by objdump) and after instrumentation?

您需要一种方法来告诉 PIN 引擎您要对来自 objdump 的结果执行什么操作。例如,您可能想通过脚本链接它们。不过,这完全取决于您想做什么。

Is this the proper way to do this?

具体取决于你想做什么,但我想不是。

PIN 中的检测分析 之间有明显的区别。一旦你理解了它,剩下的就(相对)容易了。

从概念上讲,PIN 检测由两个部分组成:

  • 一种决定代码插入位置和插入内容的机制:工具
  • 在插入点执行的代码:分析

话虽这么说,但还有一点很重要:

  • 检测仅运行一次:当指令(或 BBL 或 TRACE)首次被发现时。
  • 每次执行指令(或 BBL、TRACE)时都会运行分析。

所以当你有:

// set up the **instrumentation**
INS_AddInstrumentFunction(Func_Instrumentation, 0);

您正在设置检测(然后只调用一次)。如果您需要在每次执行指令(或 BBL、TRACE)时调用回调,则需要设置一个分析例程,例如:

// this is the **analysis** routine.
// This function is called before every instruction is executed
VOID docount() { icount++; }

// The is the **instrumentation routine**, called by INS_AddInstrumentFunction().
// Pin calls this function each time a **new** instruction is encountered
// Note that is won't be called for the same instruction after the first time.
VOID Func_Instrumentation(INS ins, VOID *v)
{
    // Insert a call to docount before every instruction, no arguments are passed
    INS_InsertCall(
        ins,              // a representation of the instruction
        IPOINT_BEFORE,    // where to insert, relative to the instruction
        (AFUNPTR)docount, // the analysis routine
        IARG_END);        // no args to pass to the analysis routine
}

仔细检查可用的指令计数样本:

关于c++ - 英特尔引脚 : How to generate objectdump-ish code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54527580/

相关文章:

C++:如何将多个对象复制到剪贴板并在之后提取它们?

c++ - 使用 wxWebViewHandler 注册一个协议(protocol)

C++11 多重移动构造函数调用

c++ - 使用英特尔PIN修改寄存器

linux - 如何使用intel pin工具统计linux上执行的指令数?

c++ - 为什么我的结构中映射的构造函数不起作用?

c - C 中的非法汇编指令

c++ - 使用 IARG_MEMORYREAD_EA

c - 有一些我没用过的奇怪的 malloc 函数

c++ - Curiously Recurring Template Pattern 的实现是特定的吗?