• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PROFILER_PROFILER_LISTENER_H_
6 #define V8_PROFILER_PROFILER_LISTENER_H_
7 
8 #include <memory>
9 
10 #include "include/v8-profiler.h"
11 #include "src/logging/code-events.h"
12 #include "src/profiler/profile-generator.h"
13 #include "src/profiler/weak-code-registry.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 class CodeEventsContainer;
19 class CodeDeoptEventRecord;
20 
21 class CodeEventObserver {
22  public:
23   virtual void CodeEventHandler(const CodeEventsContainer& evt_rec) = 0;
24   virtual ~CodeEventObserver() = default;
25 };
26 
27 class V8_EXPORT_PRIVATE ProfilerListener : public CodeEventListener,
28                                            public WeakCodeRegistry::Listener {
29  public:
30   ProfilerListener(Isolate*, CodeEventObserver*,
31                    CodeEntryStorage& code_entry_storage,
32                    WeakCodeRegistry& weak_code_registry,
33                    CpuProfilingNamingMode mode = kDebugNaming);
34   ~ProfilerListener() override;
35   ProfilerListener(const ProfilerListener&) = delete;
36   ProfilerListener& operator=(const ProfilerListener&) = delete;
37 
38   void CodeCreateEvent(LogEventsAndTags tag, Handle<AbstractCode> code,
39                        const char* name) override;
40   void CodeCreateEvent(LogEventsAndTags tag, Handle<AbstractCode> code,
41                        Handle<Name> name) override;
42   void CodeCreateEvent(LogEventsAndTags tag, Handle<AbstractCode> code,
43                        Handle<SharedFunctionInfo> shared,
44                        Handle<Name> script_name) override;
45   void CodeCreateEvent(LogEventsAndTags tag, Handle<AbstractCode> code,
46                        Handle<SharedFunctionInfo> shared,
47                        Handle<Name> script_name, int line, int column) override;
48 #if V8_ENABLE_WEBASSEMBLY
49   void CodeCreateEvent(LogEventsAndTags tag, const wasm::WasmCode* code,
50                        wasm::WasmName name, const char* source_url,
51                        int code_offset, int script_id) override;
52 #endif  // V8_ENABLE_WEBASSEMBLY
53 
54   void CallbackEvent(Handle<Name> name, Address entry_point) override;
55   void GetterCallbackEvent(Handle<Name> name, Address entry_point) override;
56   void SetterCallbackEvent(Handle<Name> name, Address entry_point) override;
57   void RegExpCodeCreateEvent(Handle<AbstractCode> code,
58                              Handle<String> source) override;
59   void CodeMoveEvent(AbstractCode from, AbstractCode to) override;
SharedFunctionInfoMoveEvent(Address from,Address to)60   void SharedFunctionInfoMoveEvent(Address from, Address to) override {}
61   void NativeContextMoveEvent(Address from, Address to) override;
CodeMovingGCEvent()62   void CodeMovingGCEvent() override {}
63   void CodeDisableOptEvent(Handle<AbstractCode> code,
64                            Handle<SharedFunctionInfo> shared) override;
65   void CodeDeoptEvent(Handle<Code> code, DeoptimizeKind kind, Address pc,
66                       int fp_to_sp_delta) override;
CodeDependencyChangeEvent(Handle<Code> code,Handle<SharedFunctionInfo> sfi,const char * reason)67   void CodeDependencyChangeEvent(Handle<Code> code,
68                                  Handle<SharedFunctionInfo> sfi,
69                                  const char* reason) override {}
70   void WeakCodeClearEvent() override;
71 
72   void OnHeapObjectDeletion(CodeEntry*) override;
73 
74   // Invoked after a mark-sweep cycle.
75   void CodeSweepEvent();
76 
GetName(Name name)77   const char* GetName(Name name) {
78     return code_entries_.strings().GetName(name);
79   }
GetName(int args_count)80   const char* GetName(int args_count) {
81     return code_entries_.strings().GetName(args_count);
82   }
GetName(const char * name)83   const char* GetName(const char* name) {
84     return code_entries_.strings().GetCopy(name);
85   }
86   const char* GetName(base::Vector<const char> name);
GetConsName(const char * prefix,Name name)87   const char* GetConsName(const char* prefix, Name name) {
88     return code_entries_.strings().GetConsName(prefix, name);
89   }
90 
set_observer(CodeEventObserver * observer)91   void set_observer(CodeEventObserver* observer) { observer_ = observer; }
92 
93  private:
94   const char* GetFunctionName(SharedFunctionInfo);
95 
96   void AttachDeoptInlinedFrames(Handle<Code> code, CodeDeoptEventRecord* rec);
97   Name InferScriptName(Name name, SharedFunctionInfo info);
DispatchCodeEvent(const CodeEventsContainer & evt_rec)98   V8_INLINE void DispatchCodeEvent(const CodeEventsContainer& evt_rec) {
99     observer_->CodeEventHandler(evt_rec);
100   }
101 
102   Isolate* isolate_;
103   CodeEventObserver* observer_;
104   CodeEntryStorage& code_entries_;
105   WeakCodeRegistry& weak_code_registry_;
106   const CpuProfilingNamingMode naming_mode_;
107 };
108 
109 }  // namespace internal
110 }  // namespace v8
111 
112 #endif  // V8_PROFILER_PROFILER_LISTENER_H_
113