V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
code-events.h
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_CODE_EVENTS_H_
6 #define V8_CODE_EVENTS_H_
7 
8 #include <unordered_set>
9 
10 #include "src/base/platform/mutex.h"
11 #include "src/globals.h"
12 #include "src/objects/code.h"
13 #include "src/objects/name.h"
14 #include "src/objects/string.h"
15 #include "src/vector.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 class AbstractCode;
21 class Name;
22 class SharedFunctionInfo;
23 class String;
24 
25 namespace wasm {
26 class WasmCode;
27 using WasmName = Vector<const char>;
28 } // namespace wasm
29 
30 #define LOG_EVENTS_LIST(V) \
31  V(CODE_CREATION_EVENT, code-creation) \
32  V(CODE_DISABLE_OPT_EVENT, code-disable-optimization) \
33  V(CODE_MOVE_EVENT, code-move) \
34  V(CODE_DELETE_EVENT, code-delete) \
35  V(CODE_MOVING_GC, code-moving-gc) \
36  V(SHARED_FUNC_MOVE_EVENT, sfi-move) \
37  V(SNAPSHOT_CODE_NAME_EVENT, snapshot-code-name) \
38  V(TICK_EVENT, tick)
39 
40 #define TAGS_LIST(V) \
41  V(BUILTIN_TAG, Builtin) \
42  V(CALLBACK_TAG, Callback) \
43  V(EVAL_TAG, Eval) \
44  V(FUNCTION_TAG, Function) \
45  V(INTERPRETED_FUNCTION_TAG, InterpretedFunction) \
46  V(HANDLER_TAG, Handler) \
47  V(BYTECODE_HANDLER_TAG, BytecodeHandler) \
48  V(LAZY_COMPILE_TAG, LazyCompile) \
49  V(REG_EXP_TAG, RegExp) \
50  V(SCRIPT_TAG, Script) \
51  V(STUB_TAG, Stub) \
52  V(NATIVE_FUNCTION_TAG, Function) \
53  V(NATIVE_LAZY_COMPILE_TAG, LazyCompile) \
54  V(NATIVE_SCRIPT_TAG, Script)
55 // Note that 'NATIVE_' cases for functions and scripts are mapped onto
56 // original tags when writing to the log.
57 
58 #define LOG_EVENTS_AND_TAGS_LIST(V) \
59  LOG_EVENTS_LIST(V) \
60  TAGS_LIST(V)
61 
62 #define PROFILE(the_isolate, Call) (the_isolate)->code_event_dispatcher()->Call;
63 
65  public:
66 #define DECLARE_ENUM(enum_item, _) enum_item,
67  enum LogEventsAndTags {
68  LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM) NUMBER_OF_LOG_EVENTS
69  };
70 #undef DECLARE_ENUM
71 
72  virtual ~CodeEventListener() = default;
73 
74  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
75  const char* comment) = 0;
76  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
77  Name name) = 0;
78  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
79  SharedFunctionInfo* shared, Name source) = 0;
80  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
81  SharedFunctionInfo* shared, Name source,
82  int line, int column) = 0;
83  virtual void CodeCreateEvent(LogEventsAndTags tag, const wasm::WasmCode* code,
84  wasm::WasmName name) = 0;
85  virtual void CallbackEvent(Name name, Address entry_point) = 0;
86  virtual void GetterCallbackEvent(Name name, Address entry_point) = 0;
87  virtual void SetterCallbackEvent(Name name, Address entry_point) = 0;
88  virtual void RegExpCodeCreateEvent(AbstractCode code, String source) = 0;
89  virtual void CodeMoveEvent(AbstractCode from, AbstractCode to) = 0;
90  virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
91  virtual void CodeMovingGCEvent() = 0;
92  virtual void CodeDisableOptEvent(AbstractCode code,
93  SharedFunctionInfo* shared) = 0;
94  virtual void CodeDeoptEvent(Code code, DeoptimizeKind kind, Address pc,
95  int fp_to_sp_delta) = 0;
96 
97  virtual bool is_listening_to_code_events() { return false; }
98 };
99 
101  public:
102  using LogEventsAndTags = CodeEventListener::LogEventsAndTags;
103 
104  CodeEventDispatcher() = default;
105 
106  bool AddListener(CodeEventListener* listener) {
107  base::MutexGuard guard(&mutex_);
108  return listeners_.insert(listener).second;
109  }
110  void RemoveListener(CodeEventListener* listener) {
111  base::MutexGuard guard(&mutex_);
112  listeners_.erase(listener);
113  }
114  bool IsListeningToCodeEvents() {
115  for (auto it : listeners_) {
116  if (it->is_listening_to_code_events()) {
117  return true;
118  }
119  }
120  return false;
121  }
122 
123 #define CODE_EVENT_DISPATCH(code) \
124  base::MutexGuard guard(&mutex_); \
125  for (auto it = listeners_.begin(); it != listeners_.end(); ++it) (*it)->code
126 
127  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
128  const char* comment) {
129  CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, comment));
130  }
131  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code, Name name) {
132  CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, name));
133  }
134  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
135  SharedFunctionInfo* shared, Name name) {
136  CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, shared, name));
137  }
138  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode code,
139  SharedFunctionInfo* shared, Name source, int line,
140  int column) {
141  CODE_EVENT_DISPATCH(
142  CodeCreateEvent(tag, code, shared, source, line, column));
143  }
144  void CodeCreateEvent(LogEventsAndTags tag, const wasm::WasmCode* code,
145  wasm::WasmName name) {
146  CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, name));
147  }
148  void CallbackEvent(Name name, Address entry_point) {
149  CODE_EVENT_DISPATCH(CallbackEvent(name, entry_point));
150  }
151  void GetterCallbackEvent(Name name, Address entry_point) {
152  CODE_EVENT_DISPATCH(GetterCallbackEvent(name, entry_point));
153  }
154  void SetterCallbackEvent(Name name, Address entry_point) {
155  CODE_EVENT_DISPATCH(SetterCallbackEvent(name, entry_point));
156  }
157  void RegExpCodeCreateEvent(AbstractCode code, String source) {
158  CODE_EVENT_DISPATCH(RegExpCodeCreateEvent(code, source));
159  }
160  void CodeMoveEvent(AbstractCode from, AbstractCode to) {
161  CODE_EVENT_DISPATCH(CodeMoveEvent(from, to));
162  }
163  void SharedFunctionInfoMoveEvent(Address from, Address to) {
164  CODE_EVENT_DISPATCH(SharedFunctionInfoMoveEvent(from, to));
165  }
166  void CodeMovingGCEvent() { CODE_EVENT_DISPATCH(CodeMovingGCEvent()); }
167  void CodeDisableOptEvent(AbstractCode code, SharedFunctionInfo* shared) {
168  CODE_EVENT_DISPATCH(CodeDisableOptEvent(code, shared));
169  }
170  void CodeDeoptEvent(Code code, DeoptimizeKind kind, Address pc,
171  int fp_to_sp_delta) {
172  CODE_EVENT_DISPATCH(CodeDeoptEvent(code, kind, pc, fp_to_sp_delta));
173  }
174 #undef CODE_EVENT_DISPATCH
175 
176  private:
177  std::unordered_set<CodeEventListener*> listeners_;
178  base::Mutex mutex_;
179 
180  DISALLOW_COPY_AND_ASSIGN(CodeEventDispatcher);
181 };
182 
183 } // namespace internal
184 } // namespace v8
185 
186 #endif // V8_CODE_EVENTS_H_
Definition: libplatform.h:13