Intel Pin:如何生成 objectdump-ish 代码?
Intel Pin: How to generate objectdump-ish code?
我正在尝试使用 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 的结果执行什么操作。例如,您可能希望通过脚本 link 两者。这完全取决于你想做什么。
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
}
仔细检查可用的指令计数示例:
- 在
source/tools/ManualExamples/inscount0.cpp
- In the PIN Manual
我正在尝试使用 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 的结果执行什么操作。例如,您可能希望通过脚本 link 两者。这完全取决于你想做什么。
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
}
仔细检查可用的指令计数示例:
- 在
source/tools/ManualExamples/inscount0.cpp
- In the PIN Manual