docs/runtime/stack-walking.md
Stack walking is the process of traversing the call stack to inspect active frames. V8 uses stack walking for several critical features:
Error.stack.V8 has a rich set of frame types, as execution can transition between interpreted JavaScript, optimized JavaScript, WebAssembly, and C++ host code.
Key frame types (defined in src/execution/frames.h):
ENTRY: The entry point from C++ into JavaScript.EXIT: The exit point from JavaScript back into C++ (e.g., calling a C++ runtime function).INTERPRETED: A frame for code running in the Ignition interpreter.BASELINE: A frame for code compiled by Sparkplug.MAGLEV: A frame for code compiled by Maglev.TURBOFAN: A frame for code compiled by TurboFan.BUILTIN: A frame for a builtin function (e.g., written in Torque or CSA).WASM: Frames for WebAssembly execution.The primary mechanism for stack walking in C++ is the StackFrameIterator class (and its variants like DebuggableStackFrameIterator).
for (StackFrameIterator it(isolate); !it.done(); it.Advance()) {
StackFrame* frame = it.frame();
// Inspect frame...
}
Most V8 frames follow a standard layout (though optimized frames may vary to save space):
+---------------------------+
| Caller's Frame Pointer | <- Higher addresses
+---------------------------+
| Caller's Return Address |
+---------------------------+
| Context / Marker |
+---------------------------+
| Function / Code Object | <- Current FP points here
+---------------------------+
| ... Locals / Stack ... | <- SP points to the top
+---------------------------+
During Garbage Collection, V8 must scan the stack to find all root pointers to heap objects. This process relies on the stack walking infrastructure.
V8 primarily uses Precise Stack Scanning. It knows exactly which slots on the stack contain tagged pointers and which contain raw data (like integers or floats).
Arguments to functions can be passed on the stack or in registers, depending on the calling convention and execution tier.
CommonFrameWithJSLinkage provides methods like GetParameter(index) to access arguments. The location is calculated relative to the frame pointer.GetActualArgumentCount()). This ensures that even with variadic arguments or parameter mismatch, the GC scans exactly the valid range of arguments on the stack.Stack walking must be done safely, especially when handling signals for the CPU profiler or during GC when the stack might be in an inconsistent state.
SafeStackFrameIterator) that performs sanity checks on pointers before dereferencing them to avoid crashes.