1 //===-- OProfileJITEventListener.cpp - Tell OProfile about JITted code ----===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a JITEventListener object that uses OProfileWrapper to tell
10 // oprofile about JITted functions, including source line information.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/ExecutionEngine.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
18 #include "llvm/ExecutionEngine/JITEventListener.h"
19 #include "llvm/ExecutionEngine/OProfileWrapper.h"
20 #include "llvm/ExecutionEngine/RuntimeDyld.h"
21 #include "llvm/IR/DebugInfo.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Object/SymbolSize.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Errno.h"
27 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm::object
;
34 #define DEBUG_TYPE "oprofile-jit-event-listener"
38 class OProfileJITEventListener
: public JITEventListener
{
39 std::unique_ptr
<OProfileWrapper
> Wrapper
;
42 std::map
<ObjectKey
, OwningBinary
<ObjectFile
>> DebugObjects
;
45 OProfileJITEventListener(std::unique_ptr
<OProfileWrapper
> LibraryWrapper
)
46 : Wrapper(std::move(LibraryWrapper
)) {
50 ~OProfileJITEventListener();
52 void notifyObjectLoaded(ObjectKey Key
, const ObjectFile
&Obj
,
53 const RuntimeDyld::LoadedObjectInfo
&L
) override
;
55 void notifyFreeingObject(ObjectKey Key
) override
;
58 void OProfileJITEventListener::initialize() {
59 if (!Wrapper
->op_open_agent()) {
60 const std::string err_str
= sys::StrError();
61 LLVM_DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str
64 LLVM_DEBUG(dbgs() << "Connected to OProfile agent.\n");
68 OProfileJITEventListener::~OProfileJITEventListener() {
69 if (Wrapper
->isAgentAvailable()) {
70 if (Wrapper
->op_close_agent() == -1) {
71 const std::string err_str
= sys::StrError();
72 LLVM_DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
75 LLVM_DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
80 void OProfileJITEventListener::notifyObjectLoaded(
81 ObjectKey Key
, const ObjectFile
&Obj
,
82 const RuntimeDyld::LoadedObjectInfo
&L
) {
83 if (!Wrapper
->isAgentAvailable()) {
87 OwningBinary
<ObjectFile
> DebugObjOwner
= L
.getObjectForDebug(Obj
);
88 const ObjectFile
&DebugObj
= *DebugObjOwner
.getBinary();
89 std::unique_ptr
<DIContext
> Context
= DWARFContext::create(DebugObj
);
91 // Use symbol info to iterate functions in the object.
92 for (const std::pair
<SymbolRef
, uint64_t> &P
: computeSymbolSizes(DebugObj
)) {
93 SymbolRef Sym
= P
.first
;
94 if (!Sym
.getType() || *Sym
.getType() != SymbolRef::ST_Function
)
97 Expected
<StringRef
> NameOrErr
= Sym
.getName();
100 StringRef Name
= *NameOrErr
;
101 Expected
<uint64_t> AddrOrErr
= Sym
.getAddress();
104 uint64_t Addr
= *AddrOrErr
;
105 uint64_t Size
= P
.second
;
107 if (Wrapper
->op_write_native_code(Name
.data(), Addr
, (void *)Addr
, Size
) ==
109 LLVM_DEBUG(dbgs() << "Failed to tell OProfile about native function "
110 << Name
<< " at [" << (void *)Addr
<< "-"
111 << ((char *)Addr
+ Size
) << "]\n");
115 DILineInfoTable Lines
= Context
->getLineInfoForAddressRange(Addr
, Size
);
117 size_t num_entries
= Lines
.size();
118 struct debug_line_info
*debug_line
;
119 debug_line
= (struct debug_line_info
*)calloc(
120 num_entries
, sizeof(struct debug_line_info
));
122 for (auto& It
: Lines
) {
123 debug_line
[i
].vma
= (unsigned long)It
.first
;
124 debug_line
[i
].lineno
= It
.second
.Line
;
125 debug_line
[i
].filename
=
126 const_cast<char *>(Lines
.front().second
.FileName
.c_str());
130 if (Wrapper
->op_write_debug_line_info((void *)Addr
, num_entries
,
132 LLVM_DEBUG(dbgs() << "Failed to tell OProfiler about debug object at ["
133 << (void *)Addr
<< "-" << ((char *)Addr
+ Size
)
139 DebugObjects
[Key
] = std::move(DebugObjOwner
);
142 void OProfileJITEventListener::notifyFreeingObject(ObjectKey Key
) {
143 if (Wrapper
->isAgentAvailable()) {
145 // If there was no agent registered when the original object was loaded then
146 // we won't have created a debug object for it, so bail out.
147 if (DebugObjects
.find(Key
) == DebugObjects
.end())
150 const ObjectFile
&DebugObj
= *DebugObjects
[Key
].getBinary();
152 // Use symbol info to iterate functions in the object.
153 for (symbol_iterator I
= DebugObj
.symbol_begin(),
154 E
= DebugObj
.symbol_end();
156 if (I
->getType() && *I
->getType() == SymbolRef::ST_Function
) {
157 Expected
<uint64_t> AddrOrErr
= I
->getAddress();
160 uint64_t Addr
= *AddrOrErr
;
162 if (Wrapper
->op_unload_native_code(Addr
) == -1) {
165 << "Failed to tell OProfile about unload of native function at "
166 << (void *)Addr
<< "\n");
173 DebugObjects
.erase(Key
);
176 } // anonymous namespace.
179 JITEventListener
*JITEventListener::createOProfileJITEventListener() {
180 return new OProfileJITEventListener(std::make_unique
<OProfileWrapper
>());
185 LLVMJITEventListenerRef
LLVMCreateOProfileJITEventListener(void)
187 return wrap(JITEventListener::createOProfileJITEventListener());