如何查看v8生成的机器码?
有谁知道我如何查看 v8 从 Javascript 生成的实际机器代码? 我已经到达 src/api.cc
中的 Script::Compile()
但我不知道从那里去哪里。
Does anybody know how I can see the actual machine code that v8 generates from Javascript? I've gotten as far as Script::Compile()
in src/api.cc
but I can't figure out where to go from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道如何从 C++ 代码中调用反汇编程序,但是有一种快速而肮脏的方法可以从 shell 中获取反汇编结果。
首先,使用反汇编器支持编译 v8:
现在您可以使用“--print_code”选项调用 shell:
这应该给您类似这样的结果:
当然,您的输出会有所不同。 以上来自为 Linux x64 编译的 v8 trunk。
I don't know how to invoke the disassembler from C++ code, but there is a quick-and-dirty way to get a disassembly from the shell.
First, compile v8 with disassembler support:
Now you can invoke the shell with the "--print_code" option:
Which should give you something like this:
Your output will vary, of course. The above is from the v8 trunk compiled for Linux x64.
尝试使用 NodeJS 或 Chrome:
-print-opt-code
:优化编译器生成的代码。-print-bytecode
:解释器生成的字节码。-trace-opt
和-trace-deopt
:哪些函数被优化(去)优化。查看@Franziska Hinkelmann 的这篇文章:
https://medium.com/dailyjs/understanding- v8s-bytecode-317d46c94775
此外,您还可以尝试
D8
:它将帮助您编译V8
并查看 JavaScript 生成的汇编代码。有关用法和详细信息:
http:// /www.mattzeunert.com/2015/08/19/viewing-assemble-code- generated-by-v8.html
Try with NodeJS or Chrome:
-print-opt-code
: Code generated by optimizing compiler.-print-bytecode
: Byte code generated by interpreter.-trace-opt
and-trace-deopt
: which functions are (de)optimized.Check this article by @Franziska Hinkelmann :
https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775
Additionally you can also try
D8
: It will help you compileV8
and view the assembly code generated from JavaScript.For usage and details:
http://www.mattzeunert.com/2015/08/19/viewing-assembly-code-generated-by-v8.html
您需要构建带有反汇编程序支持的 v8。
下载 v8 源代码。
使用反汇编程序支持进行构建。
根据您的需要,使用某些标志调用 d8 (v8 shell)。
供参考:
You need to build v8 with disassembler support.
Download v8 source code.
Build with disassembler support.
Call d8 (v8 shell) using certain flags, depending on what you want.
For reference:
我认为你走在正确的道路上。
看起来您需要从 Script::Compile 转到 Compiler::Compile,这将引导您到达代码生成器(codegen*.cc 和 .h)。
所有这些都是为了说明,查看 codegen-ia32.cc,如果您定义 ENABLE_DISASSEMBLER
我认为,当你构建时,你的反汇编应该被打印出来。
当然,所有这些都只是快速浏览我这里的旧源代码副本,所以 YMMV,但我认为这应该可行。
(再次查看您的帖子,我发现您正在寻找机器语言,而不是汇编程序 - 我不确定,但如果您想要汇编代码输出而不是反汇编,您可能必须修改逻辑)
You're on the right track, I think.
It looks like you need to get from Script::Compile to Compiler::Compile, which will lead you to the code generators (codegen*.cc and .h).
All of this to say that, looking at codegen-ia32.cc, if you define ENABLE_DISASSEMBLER
when you build, your disassembly should get printed, I think.
Of course, all of this is just from a quick browse of an old copy of the source I have here, so YMMV, but I think this should work.
(Looking at your post again, I see you're looking for the machine language, not the assembler -- I'm not sure, but you might have to modify the logic if you want the assembled code output rather than its disassembly)
看一下
v8_root/build/features.gypi
,您会发现与反汇编程序相关的以及许多其他 V8 编译时功能开关。Take a look at
v8_root/build/features.gypi
, and you will find disassembler related and many other compile time feature switches for V8.