简体   繁体   中英

How can I print V8 interpreter machine code?

Let's say I have a function and a function call like below:

function foo(i, j) {
  return i + j;
}

foo(1, 2);

Currently, I can print the bytecodes that the interpreter generates with a flag "--print-bytecode" like below.

$v8/out/x64.release/d8 --print-bytecode foo.js

What I'm really interested in the machine code that prints out at the CPU level (I'm not sure whether this is even possible for the interpreter level, so please let me know if it's not.) that I can get the instruction pointer information to do some sort of source level debugging and the register information.

Also, I read somewhere that I can activate the debugger like gdb with d8 (not the JIT gdb), I'm not really sure how to activate it. Does anyone have any suggestions for the above two problems?

Thank you in advance.

V8 developer here. I'm not entirely sure I understand correctly what you're asking for, but I think the answer is "it's not possible". The interpreter is not a compiler, so by design it does not generate machine code for your functions, only bytecode. The bytecode is "executed"/interpreted by so-called "bytecode handlers", but those are not specific to a function: the same bytecode is always handled by the same handler, which is part of the V8 binary. You can print the machine instructions of each handler, but that's not helpful for any "source level debugging".

That said, I'm not sure what you mean by "source level debugging". If you want to debug JavaScript programs, my recommendation is to use Chrome DevTools (or another browser's equivalent). Debugging V8 really is for finding bugs in V8 ; using low-level V8 insights to inspect JavaScript variables or set JavaScript breakpoints or whatnot may in theory be possible, but is orders of magnitude more difficult than using a browser's DevTools -- I've never done it, and would advise against trying because it's not the right tool for the job.

activate the debugger like gdb with d8

Just like you would run any other program in GDB: gdb -args out/x64.debug/d8 foo.js , but see above: for JavaScript debugging, this is not going to be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM