5

我是用于动态二进制检测的pin工具的新手。我试图使用 pin 工具 API 编写一些简单的客户端程序。一个这样的简单客户端正在计算二进制文件的执行指令的数量,这是作为 pin 的示例之一给出的。
我用 C 写了一个非常基本的程序,

int main(){return 0;}

并使用 gcc 编译器编译。当我使用 pin 工具计算用于上述 C 程序二进制文件的指令时,它给了我答案96072
当我使用valgrind执行相同的任务时,它给了我97487的答案,几乎等于前一个。但是当我使用perf时,答案是421,256 各种工具之间存在这种差异的原因是什么?
为了找到更多细节,我将 C 程序编译成 x86 程序集,它包含大约 20-30 行汇编指令,但是当我使用objdump要反汇编二进制文件,会产生 200-300 行汇编指令。我也无法弄清楚这种差异的原因。我正在运行带有 Linux 内核版本 3.8.0-39 的 64 位 Ubuntu 12.04。提前致谢。

4

1 回答 1

7

当我使用 valgrind 执行相同的任务时,它给了我 97487 的答案,几乎等于前一个。但是当我使用 perf 时,答案是 421,256。各种工具之间存在这种差异的原因是什么?

我的猜测是,它perf为您提供了用户模式内核模式指令(这是默认设置)。请试试

    perf stat -e instructions:u your_executable

这应该只计算在用户模式下执行的指令。性能教程中的更多详细信息。

To find more details I have compiled the C program into a x86 assembly and it consists about 20-30 lines of assembly instructions, But when I used objdump to disassemble the binary it was result in a 200-300 lines of assembly instructions. I was not able to figure out the reason for this difference too.

In the first case you only get the assembly instructions exclusively for your code. In the second case you get all the instructions contained in the executable. Please compile

    int main() { }

and run objdump -d name_of_the_executable. As you will see, many things happen before the main() is executed; and after main() has finished, a clean-up is executed.

Linux x86 Program Start Up or - How the heck do we get to main()? seems like a nice tutorial.

于 2014-04-25T10:38:09.217 回答