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/globals.h"
11
12namespace v8 {
13namespace internal {
14
15class AbstractCode;
16class Name;
17class SharedFunctionInfo;
18class String;
19
20#define LOG_EVENTS_AND_TAGS_LIST(V)                                      \
21  V(CODE_CREATION_EVENT, "code-creation")                                \
22  V(CODE_DISABLE_OPT_EVENT, "code-disable-optimization")                 \
23  V(CODE_MOVE_EVENT, "code-move")                                        \
24  V(CODE_DELETE_EVENT, "code-delete")                                    \
25  V(CODE_MOVING_GC, "code-moving-gc")                                    \
26  V(SHARED_FUNC_MOVE_EVENT, "sfi-move")                                  \
27  V(SNAPSHOT_CODE_NAME_EVENT, "snapshot-code-name")                      \
28  V(TICK_EVENT, "tick")                                                  \
29  V(REPEAT_META_EVENT, "repeat")                                         \
30  V(BUILTIN_TAG, "Builtin")                                              \
31  V(CALL_DEBUG_BREAK_TAG, "CallDebugBreak")                              \
32  V(CALL_DEBUG_PREPARE_STEP_IN_TAG, "CallDebugPrepareStepIn")            \
33  V(CALL_INITIALIZE_TAG, "CallInitialize")                               \
34  V(CALL_MEGAMORPHIC_TAG, "CallMegamorphic")                             \
35  V(CALL_MISS_TAG, "CallMiss")                                           \
36  V(CALL_NORMAL_TAG, "CallNormal")                                       \
37  V(LOAD_INITIALIZE_TAG, "LoadInitialize")                               \
38  V(LOAD_MEGAMORPHIC_TAG, "LoadMegamorphic")                             \
39  V(STORE_INITIALIZE_TAG, "StoreInitialize")                             \
40  V(STORE_GENERIC_TAG, "StoreGeneric")                                   \
41  V(STORE_MEGAMORPHIC_TAG, "StoreMegamorphic")                           \
42  V(KEYED_CALL_DEBUG_BREAK_TAG, "KeyedCallDebugBreak")                   \
43  V(KEYED_CALL_DEBUG_PREPARE_STEP_IN_TAG, "KeyedCallDebugPrepareStepIn") \
44  V(KEYED_CALL_INITIALIZE_TAG, "KeyedCallInitialize")                    \
45  V(KEYED_CALL_MEGAMORPHIC_TAG, "KeyedCallMegamorphic")                  \
46  V(KEYED_CALL_MISS_TAG, "KeyedCallMiss")                                \
47  V(KEYED_CALL_NORMAL_TAG, "KeyedCallNormal")                            \
48  V(CALLBACK_TAG, "Callback")                                            \
49  V(EVAL_TAG, "Eval")                                                    \
50  V(FUNCTION_TAG, "Function")                                            \
51  V(HANDLER_TAG, "Handler")                                              \
52  V(BYTECODE_HANDLER_TAG, "BytecodeHandler")                             \
53  V(KEYED_LOAD_IC_TAG, "KeyedLoadIC")                                    \
54  V(KEYED_LOAD_POLYMORPHIC_IC_TAG, "KeyedLoadPolymorphicIC")             \
55  V(KEYED_EXTERNAL_ARRAY_LOAD_IC_TAG, "KeyedExternalArrayLoadIC")        \
56  V(KEYED_STORE_IC_TAG, "KeyedStoreIC")                                  \
57  V(KEYED_STORE_POLYMORPHIC_IC_TAG, "KeyedStorePolymorphicIC")           \
58  V(KEYED_EXTERNAL_ARRAY_STORE_IC_TAG, "KeyedExternalArrayStoreIC")      \
59  V(LAZY_COMPILE_TAG, "LazyCompile")                                     \
60  V(CALL_IC_TAG, "CallIC")                                               \
61  V(LOAD_IC_TAG, "LoadIC")                                               \
62  V(LOAD_GLOBAL_IC_TAG, "LoadGlobalIC")                                  \
63  V(LOAD_POLYMORPHIC_IC_TAG, "LoadPolymorphicIC")                        \
64  V(REG_EXP_TAG, "RegExp")                                               \
65  V(SCRIPT_TAG, "Script")                                                \
66  V(STORE_IC_TAG, "StoreIC")                                             \
67  V(STORE_POLYMORPHIC_IC_TAG, "StorePolymorphicIC")                      \
68  V(STUB_TAG, "Stub")                                                    \
69  V(NATIVE_FUNCTION_TAG, "Function")                                     \
70  V(NATIVE_LAZY_COMPILE_TAG, "LazyCompile")                              \
71  V(NATIVE_SCRIPT_TAG, "Script")
72// Note that 'NATIVE_' cases for functions and scripts are mapped onto
73// original tags when writing to the log.
74
75#define PROFILE(the_isolate, Call) (the_isolate)->code_event_dispatcher()->Call;
76
77class CodeEventListener {
78 public:
79#define DECLARE_ENUM(enum_item, _) enum_item,
80  enum LogEventsAndTags {
81    LOG_EVENTS_AND_TAGS_LIST(DECLARE_ENUM) NUMBER_OF_LOG_EVENTS
82  };
83#undef DECLARE_ENUM
84
85  virtual ~CodeEventListener() {}
86
87  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
88                               const char* comment) = 0;
89  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
90                               Name* name) = 0;
91  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
92                               SharedFunctionInfo* shared, Name* name) = 0;
93  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
94                               SharedFunctionInfo* shared, Name* source,
95                               int line, int column) = 0;
96  virtual void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
97                               int args_count) = 0;
98  virtual void CallbackEvent(Name* name, Address entry_point) = 0;
99  virtual void GetterCallbackEvent(Name* name, Address entry_point) = 0;
100  virtual void SetterCallbackEvent(Name* name, Address entry_point) = 0;
101  virtual void RegExpCodeCreateEvent(AbstractCode* code, String* source) = 0;
102  virtual void CodeMoveEvent(AbstractCode* from, Address to) = 0;
103  virtual void SharedFunctionInfoMoveEvent(Address from, Address to) = 0;
104  virtual void CodeMovingGCEvent() = 0;
105  virtual void CodeDisableOptEvent(AbstractCode* code,
106                                   SharedFunctionInfo* shared) = 0;
107  virtual void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) = 0;
108};
109
110class CodeEventDispatcher {
111 public:
112  using LogEventsAndTags = CodeEventListener::LogEventsAndTags;
113
114  CodeEventDispatcher() {}
115
116  bool AddListener(CodeEventListener* listener) {
117    return listeners_.insert(listener).second;
118  }
119  void RemoveListener(CodeEventListener* listener) {
120    listeners_.erase(listener);
121  }
122
123#define CODE_EVENT_DISPATCH(code) \
124  for (auto it = listeners_.begin(); it != listeners_.end(); ++it) (*it)->code
125
126  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
127                       const char* comment) {
128    CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, comment));
129  }
130  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, Name* name) {
131    CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, name));
132  }
133  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
134                       SharedFunctionInfo* shared, Name* name) {
135    CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, shared, name));
136  }
137  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
138                       SharedFunctionInfo* shared, Name* source, int line,
139                       int column) {
140    CODE_EVENT_DISPATCH(
141        CodeCreateEvent(tag, code, shared, source, line, column));
142  }
143  void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
144                       int args_count) {
145    CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, args_count));
146  }
147  void CallbackEvent(Name* name, Address entry_point) {
148    CODE_EVENT_DISPATCH(CallbackEvent(name, entry_point));
149  }
150  void GetterCallbackEvent(Name* name, Address entry_point) {
151    CODE_EVENT_DISPATCH(GetterCallbackEvent(name, entry_point));
152  }
153  void SetterCallbackEvent(Name* name, Address entry_point) {
154    CODE_EVENT_DISPATCH(SetterCallbackEvent(name, entry_point));
155  }
156  void RegExpCodeCreateEvent(AbstractCode* code, String* source) {
157    CODE_EVENT_DISPATCH(RegExpCodeCreateEvent(code, source));
158  }
159  void CodeMoveEvent(AbstractCode* from, Address to) {
160    CODE_EVENT_DISPATCH(CodeMoveEvent(from, to));
161  }
162  void SharedFunctionInfoMoveEvent(Address from, Address to) {
163    CODE_EVENT_DISPATCH(SharedFunctionInfoMoveEvent(from, to));
164  }
165  void CodeMovingGCEvent() { CODE_EVENT_DISPATCH(CodeMovingGCEvent()); }
166  void CodeDisableOptEvent(AbstractCode* code, SharedFunctionInfo* shared) {
167    CODE_EVENT_DISPATCH(CodeDisableOptEvent(code, shared));
168  }
169  void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) {
170    CODE_EVENT_DISPATCH(CodeDeoptEvent(code, pc, fp_to_sp_delta));
171  }
172#undef CODE_EVENT_DISPATCH
173
174 private:
175  std::unordered_set<CodeEventListener*> listeners_;
176
177  DISALLOW_COPY_AND_ASSIGN(CodeEventDispatcher);
178};
179
180}  // namespace internal
181}  // namespace v8
182
183#endif  // V8_CODE_EVENTS_H_
184