v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-unwinder.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_UNWINDER_H_
6#define INCLUDE_V8_UNWINDER_H_
7
8#include <memory>
9
10#include "v8-embedder-state-scope.h" // NOLINT(build/include_directory)
11#include "v8config.h" // NOLINT(build/include_directory)
12
13namespace v8 {
14// Holds the callee saved registers needed for the stack unwinder. It is the
15// empty struct if no registers are required. Implemented in
16// include/v8-unwinder-state.h.
17struct CalleeSavedRegisters;
18
19// A RegisterState represents the current state of registers used
20// by the sampling profiler API.
26
27 void* pc; // Instruction pointer.
28 void* sp; // Stack pointer.
29 void* fp; // Frame pointer.
30 void* lr; // Link register (or nullptr on platforms without a link register).
31 // Callee saved registers (or null if no callee saved registers were stored)
33};
34
35// A StateTag represents a possible state of the VM.
36// This enum is append-only to preserve compatibility with historical logs.
37// Add new states only at the end and do not reorder or remove existing values.
38// LINT.IfChange
51};
52// LINT.ThenChange(../tools/profile.mjs, ../tools/tickprocessor.mjs)
53
54constexpr bool IsExternal(StateTag state) {
55 return state == EXTERNAL || state == IDLE_EXTERNAL;
56}
57
58constexpr bool IsIdle(StateTag state) {
59 return state == IDLE || state == IDLE_EXTERNAL;
60}
61
62// The output structure filled up by GetStackSample API function.
63struct SampleInfo {
64 size_t frames_count; // Number of frames collected.
65 void* external_callback_entry; // External callback address if VM is
66 // executing an external callback.
67 void* context; // Incumbent native context address.
68 void* embedder_context; // Native context address for embedder state
69 StateTag vm_state; // Current VM state.
70 EmbedderStateTag embedder_state; // Current Embedder state
71};
72
74 const void* start = nullptr;
76};
77
80};
81
86};
87
88/**
89 * Various helpers for skipping over V8 frames in a given stack.
90 *
91 * The unwinder API is only supported on the x64, ARM64 and ARM32 architectures.
92 */
94 public:
95 /**
96 * Attempt to unwind the stack to the most recent C++ frame. This function is
97 * signal-safe and does not access any V8 state and thus doesn't require an
98 * Isolate.
99 *
100 * The unwinder needs to know the location of the JS Entry Stub (a piece of
101 * code that is run when C++ code calls into generated JS code). This is used
102 * for edge cases where the current frame is being constructed or torn down
103 * when the stack sample occurs.
104 *
105 * The unwinder also needs the virtual memory range of all possible V8 code
106 * objects. There are two ranges required - the heap code range and the range
107 * for code embedded in the binary.
108 *
109 * Available on x64, ARM64 and ARM32.
110 *
111 * \param code_pages A list of all of the ranges in which V8 has allocated
112 * executable code. The caller should obtain this list by calling
113 * Isolate::CopyCodePages() during the same interrupt/thread suspension that
114 * captures the stack.
115 * \param register_state The current registers. This is an in-out param that
116 * will be overwritten with the register values after unwinding, on success.
117 * \param stack_base The resulting stack pointer and frame pointer values are
118 * bounds-checked against the stack_base and the original stack pointer value
119 * to ensure that they are valid locations in the given stack. If these values
120 * or any intermediate frame pointer values used during unwinding are ever out
121 * of these bounds, unwinding will fail.
122 *
123 * \return True on success.
124 */
125 static bool TryUnwindV8Frames(const JSEntryStubs& entry_stubs,
126 size_t code_pages_length,
127 const MemoryRange* code_pages,
128 RegisterState* register_state,
129 const void* stack_base);
130
131 /**
132 * Whether the PC is within the V8 code range represented by code_pages.
133 *
134 * If this returns false, then calling UnwindV8Frames() with the same PC
135 * and unwind_state will always fail. If it returns true, then unwinding may
136 * (but not necessarily) be successful.
137 *
138 * Available on x64, ARM64 and ARM32
139 */
140 static bool PCIsInV8(size_t code_pages_length, const MemoryRange* code_pages,
141 void* pc);
142};
143
144} // namespace v8
145
146#endif // INCLUDE_V8_UNWINDER_H_
static bool PCIsInV8(size_t code_pages_length, const MemoryRange *code_pages, void *pc)
static bool TryUnwindV8Frames(const JSEntryStubs &entry_stubs, size_t code_pages_length, const MemoryRange *code_pages, RegisterState *register_state, const void *stack_base)
constexpr bool IsIdle(StateTag state)
Definition v8-unwinder.h:58
StateTag
Definition v8-unwinder.h:39
@ PARSER
Definition v8-unwinder.h:42
@ COMPILER
Definition v8-unwinder.h:44
@ EXTERNAL
Definition v8-unwinder.h:46
@ IDLE_EXTERNAL
Definition v8-unwinder.h:50
@ JS
Definition v8-unwinder.h:40
@ LOGGING
Definition v8-unwinder.h:49
@ BYTECODE_COMPILER
Definition v8-unwinder.h:43
@ OTHER
Definition v8-unwinder.h:45
@ GC
Definition v8-unwinder.h:41
@ ATOMICS_WAIT
Definition v8-unwinder.h:47
@ IDLE
Definition v8-unwinder.h:48
constexpr bool IsExternal(StateTag state)
Definition v8-unwinder.h:54
Definition v8-unwinder.h:78
MemoryRange code
Definition v8-unwinder.h:79
Definition v8-unwinder.h:82
JSEntryStub js_construct_entry_stub
Definition v8-unwinder.h:84
JSEntryStub js_entry_stub
Definition v8-unwinder.h:83
JSEntryStub js_run_microtasks_entry_stub
Definition v8-unwinder.h:85
const void * start
Definition v8-unwinder.h:74
size_t length_in_bytes
Definition v8-unwinder.h:75
std::unique_ptr< CalleeSavedRegisters > callee_saved
Definition v8-unwinder.h:32
RegisterState(const RegisterState &other)
RegisterState & operator=(const RegisterState &other)
EmbedderStateTag embedder_state
Definition v8-unwinder.h:70
void * embedder_context
Definition v8-unwinder.h:68
StateTag vm_state
Definition v8-unwinder.h:69
void * external_callback_entry
Definition v8-unwinder.h:65
size_t frames_count
Definition v8-unwinder.h:64
#define V8_EXPORT
Definition v8config.h:860