1 //===- JITEventListener.h - Exposes events from JIT compilation -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the JITEventListener interface, which lets users get
10 // callbacks when significant events happen during the JIT compilation process.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
15 #define LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
16 
17 #include "llvm-c/ExecutionEngine.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/IR/DebugLoc.h"
21 #include "llvm/Support/CBindingWrapping.h"
22 #include <cstdint>
23 
24 namespace llvm {
25 
26 class IntelJITEventsWrapper;
27 class MachineFunction;
28 class OProfileWrapper;
29 
30 namespace object {
31 
32 class ObjectFile;
33 
34 } // end namespace object
35 
36 /// JITEventListener - Abstract interface for use by the JIT to notify clients
37 /// about significant events during compilation. For example, to notify
38 /// profilers and debuggers that need to know where functions have been emitted.
39 ///
40 /// The default implementation of each method does nothing.
41 class JITEventListener {
42 public:
43   using ObjectKey = uint64_t;
44 
45   JITEventListener() = default;
46   virtual ~JITEventListener() = default;
47 
48   /// notifyObjectLoaded - Called after an object has had its sections allocated
49   /// and addresses assigned to all symbols. Note: Section memory will not have
50   /// been relocated yet. notifyFunctionLoaded will not be called for
51   /// individual functions in the object.
52   ///
53   /// ELF-specific information
54   /// The ObjectImage contains the generated object image
55   /// with section headers updated to reflect the address at which sections
56   /// were loaded and with relocations performed in-place on debug sections.
57   virtual void notifyObjectLoaded(ObjectKey K, const object::ObjectFile &Obj,
58                                   const RuntimeDyld::LoadedObjectInfo &L) {}
59 
60   /// notifyFreeingObject - Called just before the memory associated with
61   /// a previously emitted object is released.
62   virtual void notifyFreeingObject(ObjectKey K) {}
63 
64   // Get a pointe to the GDB debugger registration listener.
65   static JITEventListener *createGDBRegistrationListener();
66 
67 #if LLVM_USE_INTEL_JITEVENTS
68   // Construct an IntelJITEventListener
69   static JITEventListener *createIntelJITEventListener();
70 
71   // Construct an IntelJITEventListener with a test Intel JIT API implementation
72   static JITEventListener *createIntelJITEventListener(
73                                       IntelJITEventsWrapper* AlternativeImpl);
74 #else
75   static JITEventListener *createIntelJITEventListener() { return nullptr; }
76 
77   static JITEventListener *createIntelJITEventListener(
78                                       IntelJITEventsWrapper* AlternativeImpl) {
79     return nullptr;
80   }
81 #endif // USE_INTEL_JITEVENTS
82 
83 #if LLVM_USE_OPROFILE
84   // Construct an OProfileJITEventListener
85   static JITEventListener *createOProfileJITEventListener();
86 
87   // Construct an OProfileJITEventListener with a test opagent implementation
88   static JITEventListener *createOProfileJITEventListener(
89                                       OProfileWrapper* AlternativeImpl);
90 #else
91   static JITEventListener *createOProfileJITEventListener() { return nullptr; }
92 
93   static JITEventListener *createOProfileJITEventListener(
94                                       OProfileWrapper* AlternativeImpl) {
95     return nullptr;
96   }
97 #endif // USE_OPROFILE
98 
99 #if LLVM_USE_PERF
100   static JITEventListener *createPerfJITEventListener();
101 #else
102   static JITEventListener *createPerfJITEventListener()
103   {
104     return nullptr;
105   }
106 #endif // USE_PERF
107 
108 private:
109   virtual void anchor();
110 };
111 
112 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITEventListener, LLVMJITEventListenerRef)
113 
114 } // end namespace llvm
115 
116 #endif // LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
117