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 OProfileWrapper;
28 
29 namespace object {
30 
31 class ObjectFile;
32 
33 } // end namespace object
34 
35 /// JITEventListener - Abstract interface for use by the JIT to notify clients
36 /// about significant events during compilation. For example, to notify
37 /// profilers and debuggers that need to know where functions have been emitted.
38 ///
39 /// The default implementation of each method does nothing.
40 class JITEventListener {
41 public:
42   using ObjectKey = uint64_t;
43 
44   JITEventListener() = default;
45   virtual ~JITEventListener() = default;
46 
47   /// notifyObjectLoaded - Called after an object has had its sections allocated
48   /// and addresses assigned to all symbols. Note: Section memory will not have
49   /// been relocated yet. notifyFunctionLoaded will not be called for
50   /// individual functions in the object.
51   ///
52   /// ELF-specific information
53   /// The ObjectImage contains the generated object image
54   /// with section headers updated to reflect the address at which sections
55   /// were loaded and with relocations performed in-place on debug sections.
56   virtual void notifyObjectLoaded(ObjectKey K, const object::ObjectFile &Obj,
57                                   const RuntimeDyld::LoadedObjectInfo &L) {}
58 
59   /// notifyFreeingObject - Called just before the memory associated with
60   /// a previously emitted object is released.
61   virtual void notifyFreeingObject(ObjectKey K) {}
62 
63   // Get a pointe to the GDB debugger registration listener.
64   static JITEventListener *createGDBRegistrationListener();
65 
66 #if LLVM_USE_INTEL_JITEVENTS
67   // Construct an IntelJITEventListener
68   static JITEventListener *createIntelJITEventListener();
69 
70   // Construct an IntelJITEventListener with a test Intel JIT API implementation
71   static JITEventListener *createIntelJITEventListener(
72                                       IntelJITEventsWrapper* AlternativeImpl);
73 #else
74   static JITEventListener *createIntelJITEventListener() { return nullptr; }
75 
76   static JITEventListener *createIntelJITEventListener(
77                                       IntelJITEventsWrapper* AlternativeImpl) {
78     return nullptr;
79   }
80 #endif // USE_INTEL_JITEVENTS
81 
82 #if LLVM_USE_OPROFILE
83   // Construct an OProfileJITEventListener
84   static JITEventListener *createOProfileJITEventListener();
85 
86   // Construct an OProfileJITEventListener with a test opagent implementation
87   static JITEventListener *createOProfileJITEventListener(
88                                       OProfileWrapper* AlternativeImpl);
89 #else
90   static JITEventListener *createOProfileJITEventListener() { return nullptr; }
91 
92   static JITEventListener *createOProfileJITEventListener(
93                                       OProfileWrapper* AlternativeImpl) {
94     return nullptr;
95   }
96 #endif // USE_OPROFILE
97 
98 #if LLVM_USE_PERF
99   static JITEventListener *createPerfJITEventListener();
100 #else
101   static JITEventListener *createPerfJITEventListener()
102   {
103     return nullptr;
104   }
105 #endif // USE_PERF
106 
107 private:
108   virtual void anchor();
109 };
110 
111 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITEventListener, LLVMJITEventListenerRef)
112 
113 } // end namespace llvm
114 
115 #endif // LLVM_EXECUTIONENGINE_JITEVENTLISTENER_H
116