Debug V8 in Node.js core with GDB

Or in other projects that embed V8

Franziska Hinkelmann
Fhinkel
4 min readJan 17, 2017

--

If you contribute to Node core, you will eventually need to debug C++ code in /node/src/. If you’ve done this before, you might have noticed that GDB’s print command is not helpful when working with V8 code.

We'd really like to see "ContextifyScript" mentioned in the output, not a memory address.

Node core uses V8 as its JavaScript engine. A lot of variables in V8 are either v8::Values wrapped in v8::Local handles, or v8::internal::HeapObjects wrapped in v8::internal::Handle handles. Handles are needed for the garbage collector. You’ll find v8::Locals all over the Node core sources. However, if you want to debug them, you need to do something a little more involved than just typing print.

V8 provides a gdbinit file with user-defined commands for inspecting V8 heap objects. They allow us to easily debug V8 objects. Let’s go through it step-by-step how to use these commands.

Get the gdbinit file from the V8 repo and save it as .gdbinit in your Node directory or home directory. You can also pass the file to GDB with -x. If you save it in your Node folder, you probably want to add it to .git/info/exclude.

Next, you need a debug build of Node.

Say we are working on the vm module in node_contextify.cc and need to figure out what’s going on there. We call node on a test case (or any other JavaScript file for that matter) that uses the code that we want to debug.

Let’s set a few break points in the functions that we are interested in.

Now the debugger has stopped at Breakpoint 1. The code is using the V8 API and we have several v8::Local handles. Let’s see what their values are.

If we use jlh instead of print on our first example, we see the underlying string (probably what we’re interested in) and not a memory address.

To sum up, if you want to print the content of a v8::Local handle, use jlh.

If you wonder what jlh stands for, it stands for “job local handle”. job is V8’s user-defined GDB command for printing v8::internal::HeapObjects, which are the very common objects in V8.

Not V8 specific, but here are a few more helpful GDB commands. Happy debugging!

Thanks to Yang Guo for adding his jlh command to V8’s gdbinit and thanks to Andreas Haas for proofreading and valuable corrections.

--

--

Franziska Hinkelmann
Fhinkel

Principal Engineering Manager at Microsoft. Node.js Monkey Patcher.