v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
code-events.h
Go to the documentation of this file.
1// Copyright 2016 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 V8_LOGGING_CODE_EVENTS_H_
6#define V8_LOGGING_CODE_EVENTS_H_
7
8#include <vector>
9
11#include "src/base/vector.h"
12#include "src/common/globals.h"
14#include "src/objects/code.h"
16#include "src/objects/name.h"
18#include "src/objects/string.h"
19
20namespace v8 {
21namespace internal {
22
23class AbstractCode;
24class Name;
25class SharedFunctionInfo;
26class String;
27
28namespace wasm {
29class WasmCode;
31} // namespace wasm
32
33#define LOG_EVENT_LIST(V) \
34 V(kCodeCreation, "code-creation") \
35 V(kCodeDisableOpt, "code-disable-optimization") \
36 V(kCodeMove, "code-move") \
37 V(kCodeDeopt, "code-deopt") \
38 V(kCodeDelete, "code-delete") \
39 V(kCodeMovingGC, "code-moving-gc") \
40 V(kSharedFuncMove, "sfi-move") \
41 V(kSnapshotCodeName, "snapshot-code-name") \
42 V(kTick, "tick")
43
44#define CODE_TYPE_LIST(V) \
45 V(kBuiltin, Builtin) \
46 V(kCallback, Callback) \
47 V(kEval, Eval) \
48 V(kFunction, JS) \
49 V(kHandler, Handler) \
50 V(kBytecodeHandler, BytecodeHandler) \
51 V(kRegExp, RegExp) \
52 V(kScript, Script) \
53 V(kStub, Stub) \
54 V(kNativeFunction, JS) \
55 V(kNativeScript, Script)
56// Note that 'Native' cases for functions and scripts are mapped onto
57// original tags when writing to the log.
58
59#define PROFILE(the_isolate, Call) (the_isolate)->logger()->Call;
60
62 public:
63#define DECLARE_ENUM(enum_item, _) enum_item,
64 enum class Event : uint8_t { LOG_EVENT_LIST(DECLARE_ENUM) kLength };
65 enum class CodeTag : uint8_t { CODE_TYPE_LIST(DECLARE_ENUM) kLength };
66#undef DECLARE_ENUM
67
68 virtual ~LogEventListener() = default;
69
71 const char* name) = 0;
73 DirectHandle<Name> name) = 0;
76 DirectHandle<Name> script_name) = 0;
79 DirectHandle<Name> script_name, int line,
80 int column) = 0;
81#if V8_ENABLE_WEBASSEMBLY
82 virtual void CodeCreateEvent(CodeTag tag, const wasm::WasmCode* code,
83 wasm::WasmName name, const char* source_url,
84 int code_offset, int script_id) = 0;
85#endif // V8_ENABLE_WEBASSEMBLY
86
87 virtual void CallbackEvent(DirectHandle<Name> name, Address entry_point) = 0;
89 Address entry_point) = 0;
91 Address entry_point) = 0;
94 RegExpFlags flags) = 0;
95 // Not handlified as this happens during GC. No allocation allowed.
100 virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
101 virtual void NativeContextMoveEvent(Address from, Address to) = 0;
102 virtual void CodeMovingGCEvent() = 0;
106 Address pc, int fp_to_sp_delta) = 0;
107 // These events can happen when 1. an assumption made by optimized code fails
108 // or 2. a weakly embedded object dies.
111 const char* reason) = 0;
112 // Called during GC shortly after any weak references to code objects are
113 // cleared.
114 virtual void WeakCodeClearEvent() = 0;
115
116 virtual bool is_listening_to_code_events() { return false; }
117 virtual bool allows_code_compaction() { return true; }
118};
119
120// Dispatches events to a set of registered listeners.
121class Logger {
122 public:
125
126 Logger() = default;
127 Logger(const Logger&) = delete;
128 Logger& operator=(const Logger&) = delete;
129
132 auto position = std::find(listeners_.begin(), listeners_.end(), listener);
133 if (position != listeners_.end()) return false;
134 // Add the listener to the end and update the element
135 listeners_.push_back(listener);
136 return true;
137 }
138
141 auto position = std::find(listeners_.begin(), listeners_.end(), listener);
142 if (position == listeners_.end()) return false;
143 listeners_.erase(position);
144 return true;
145 }
146
149 for (auto listener : listeners_) {
150 if (listener->is_listening_to_code_events()) return true;
151 }
152 return false;
153 }
154
157 for (auto listener : listeners_) {
158 if (!listener->allows_code_compaction()) return false;
159 }
160 return true;
161 }
162
164 const char* comment) {
166 for (auto listener : listeners_) {
167 listener->CodeCreateEvent(tag, code, comment);
168 }
169 }
170
172 DirectHandle<Name> name) {
174 for (auto listener : listeners_) {
175 listener->CodeCreateEvent(tag, code, name);
176 }
177 }
178
181 DirectHandle<Name> name) {
183 for (auto listener : listeners_) {
184 listener->CodeCreateEvent(tag, code, shared, name);
185 }
186 }
187
190 DirectHandle<Name> source, int line, int column) {
192 for (auto listener : listeners_) {
193 listener->CodeCreateEvent(tag, code, shared, source, line, column);
194 }
195 }
196
197#if V8_ENABLE_WEBASSEMBLY
198 void CodeCreateEvent(CodeTag tag, const wasm::WasmCode* code,
199 wasm::WasmName name, const char* source_url,
200 int code_offset, int script_id) {
202 for (auto listener : listeners_) {
203 listener->CodeCreateEvent(tag, code, name, source_url, code_offset,
204 script_id);
205 }
206 }
207#endif // V8_ENABLE_WEBASSEMBLY
208
209 void CallbackEvent(DirectHandle<Name> name, Address entry_point) {
211 for (auto listener : listeners_) {
212 listener->CallbackEvent(name, entry_point);
213 }
214 }
215
218 for (auto listener : listeners_) {
219 listener->GetterCallbackEvent(name, entry_point);
220 }
221 }
222
225 for (auto listener : listeners_) {
226 listener->SetterCallbackEvent(name, entry_point);
227 }
228 }
229
231 DirectHandle<String> source, RegExpFlags flags) {
233 for (auto listener : listeners_) {
234 listener->RegExpCodeCreateEvent(code, source, flags);
235 }
236 }
237
241 for (auto listener : listeners_) {
242 listener->CodeMoveEvent(from, to);
243 }
244 }
245
248 for (auto listener : listeners_) {
249 listener->BytecodeMoveEvent(from, to);
250 }
251 }
252
255 for (auto listener : listeners_) {
256 listener->SharedFunctionInfoMoveEvent(from, to);
257 }
258 }
259
262 for (auto listener : listeners_) {
263 listener->NativeContextMoveEvent(from, to);
264 }
265 }
266
269 for (auto listener : listeners_) {
270 listener->CodeMovingGCEvent();
271 }
272 }
273
277 for (auto listener : listeners_) {
278 listener->CodeDisableOptEvent(code, shared);
279 }
280 }
281
283 int fp_to_sp_delta) {
285 for (auto listener : listeners_) {
286 listener->CodeDeoptEvent(code, kind, pc, fp_to_sp_delta);
287 }
288 }
289
292 const char* reason) {
294 for (auto listener : listeners_) {
295 listener->CodeDependencyChangeEvent(code, sfi, reason);
296 }
297 }
298
301 for (auto listener : listeners_) {
302 listener->WeakCodeClearEvent();
303 }
304 }
305
306 private:
307 std::vector<LogEventListener*> listeners_;
309};
310
311} // namespace internal
312} // namespace v8
313
314#endif // V8_LOGGING_CODE_EVENTS_H_
Builtins::Kind kind
Definition builtins.cc:40
virtual void BytecodeMoveEvent(Tagged< BytecodeArray > from, Tagged< BytecodeArray > to)=0
virtual void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, DirectHandle< Name > name)=0
virtual void CodeDisableOptEvent(DirectHandle< AbstractCode > code, DirectHandle< SharedFunctionInfo > shared)=0
virtual ~LogEventListener()=default
virtual void NativeContextMoveEvent(Address from, Address to)=0
virtual bool allows_code_compaction()
virtual bool is_listening_to_code_events()
virtual void SetterCallbackEvent(DirectHandle< Name > name, Address entry_point)=0
virtual void CodeMoveEvent(Tagged< InstructionStream > from, Tagged< InstructionStream > to)=0
virtual void CodeDependencyChangeEvent(DirectHandle< Code > code, DirectHandle< SharedFunctionInfo > shared, const char *reason)=0
virtual void CodeDeoptEvent(DirectHandle< Code > code, DeoptimizeKind kind, Address pc, int fp_to_sp_delta)=0
virtual void CodeMovingGCEvent()=0
virtual void RegExpCodeCreateEvent(DirectHandle< AbstractCode > code, DirectHandle< String > source, RegExpFlags flags)=0
virtual void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, DirectHandle< SharedFunctionInfo > shared, DirectHandle< Name > script_name)=0
virtual void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, const char *name)=0
virtual void SharedFunctionInfoMoveEvent(Address from, Address to)=0
virtual void CallbackEvent(DirectHandle< Name > name, Address entry_point)=0
virtual void WeakCodeClearEvent()=0
virtual void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, DirectHandle< SharedFunctionInfo > shared, DirectHandle< Name > script_name, int line, int column)=0
virtual void GetterCallbackEvent(DirectHandle< Name > name, Address entry_point)=0
void CodeDisableOptEvent(DirectHandle< AbstractCode > code, DirectHandle< SharedFunctionInfo > shared)
void CodeMoveEvent(Tagged< InstructionStream > from, Tagged< InstructionStream > to)
Logger & operator=(const Logger &)=delete
void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, DirectHandle< SharedFunctionInfo > shared, DirectHandle< Name > source, int line, int column)
void CodeDeoptEvent(DirectHandle< Code > code, DeoptimizeKind kind, Address pc, int fp_to_sp_delta)
bool is_listening_to_code_events()
void SharedFunctionInfoMoveEvent(Address from, Address to)
void BytecodeMoveEvent(Tagged< BytecodeArray > from, Tagged< BytecodeArray > to)
void CodeDependencyChangeEvent(DirectHandle< Code > code, DirectHandle< SharedFunctionInfo > sfi, const char *reason)
void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, DirectHandle< Name > name)
bool AddListener(LogEventListener *listener)
bool RemoveListener(LogEventListener *listener)
Logger(const Logger &)=delete
void NativeContextMoveEvent(Address from, Address to)
void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, const char *comment)
LogEventListener::CodeTag CodeTag
void RegExpCodeCreateEvent(DirectHandle< AbstractCode > code, DirectHandle< String > source, RegExpFlags flags)
base::RecursiveMutex mutex_
void GetterCallbackEvent(DirectHandle< Name > name, Address entry_point)
void CallbackEvent(DirectHandle< Name > name, Address entry_point)
void SetterCallbackEvent(DirectHandle< Name > name, Address entry_point)
void CodeCreateEvent(CodeTag tag, DirectHandle< AbstractCode > code, DirectHandle< SharedFunctionInfo > shared, DirectHandle< Name > name)
std::vector< LogEventListener * > listeners_
int position
Definition liveedit.cc:290
DECLARE_ENUM(enum_item, ignore)
v8_inspector::String16 String
Definition string-util.h:26
Definition c-api.cc:87