OProfileJITEventListener.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===-- OProfileJITEventListener.cpp - Tell OProfile about JITted code ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines a JITEventListener object that uses OProfileWrapper to tell
  11. // oprofile about JITted functions, including source line information.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Config/config.h"
  15. #include "EventListenerCommon.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/ExecutionEngine/JITEventListener.h"
  18. #include "llvm/ExecutionEngine/OProfileWrapper.h"
  19. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  20. #include "llvm/IR/DebugInfo.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/Object/ObjectFile.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/Errno.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <dirent.h>
  27. #include <fcntl.h>
  28. using namespace llvm;
  29. using namespace llvm::jitprofiling;
  30. using namespace llvm::object;
  31. #define DEBUG_TYPE "oprofile-jit-event-listener"
  32. namespace {
  33. class OProfileJITEventListener : public JITEventListener {
  34. std::unique_ptr<OProfileWrapper> Wrapper;
  35. void initialize();
  36. std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
  37. public:
  38. OProfileJITEventListener(std::unique_ptr<OProfileWrapper> LibraryWrapper)
  39. : Wrapper(std::move(LibraryWrapper)) {
  40. initialize();
  41. }
  42. ~OProfileJITEventListener();
  43. void NotifyObjectEmitted(const ObjectFile &Obj,
  44. const RuntimeDyld::LoadedObjectInfo &L) override;
  45. void NotifyFreeingObject(const ObjectFile &Obj) override;
  46. };
  47. void OProfileJITEventListener::initialize() {
  48. if (!Wrapper->op_open_agent()) {
  49. const std::string err_str = sys::StrError();
  50. DEBUG(dbgs() << "Failed to connect to OProfile agent: " << err_str << "\n");
  51. } else {
  52. DEBUG(dbgs() << "Connected to OProfile agent.\n");
  53. }
  54. }
  55. OProfileJITEventListener::~OProfileJITEventListener() {
  56. if (Wrapper->isAgentAvailable()) {
  57. if (Wrapper->op_close_agent() == -1) {
  58. const std::string err_str = sys::StrError();
  59. DEBUG(dbgs() << "Failed to disconnect from OProfile agent: "
  60. << err_str << "\n");
  61. } else {
  62. DEBUG(dbgs() << "Disconnected from OProfile agent.\n");
  63. }
  64. }
  65. }
  66. void OProfileJITEventListener::NotifyObjectEmitted(
  67. const ObjectFile &Obj,
  68. const RuntimeDyld::LoadedObjectInfo &L) {
  69. if (!Wrapper->isAgentAvailable()) {
  70. return;
  71. }
  72. OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
  73. const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
  74. // Use symbol info to iterate functions in the object.
  75. for (symbol_iterator I = DebugObj.symbol_begin(), E = DebugObj.symbol_end();
  76. I != E; ++I) {
  77. SymbolRef::Type SymType;
  78. if (I->getType(SymType)) continue;
  79. if (SymType == SymbolRef::ST_Function) {
  80. StringRef Name;
  81. uint64_t Addr;
  82. uint64_t Size;
  83. if (I->getName(Name)) continue;
  84. if (I->getAddress(Addr)) continue;
  85. if (I->getSize(Size)) continue;
  86. if (Wrapper->op_write_native_code(Name.data(), Addr, (void*)Addr, Size)
  87. == -1) {
  88. DEBUG(dbgs() << "Failed to tell OProfile about native function "
  89. << Name << " at ["
  90. << (void*)Addr << "-" << ((char*)Addr + Size) << "]\n");
  91. continue;
  92. }
  93. // TODO: support line number info (similar to IntelJITEventListener.cpp)
  94. }
  95. }
  96. DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
  97. }
  98. void OProfileJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
  99. if (Wrapper->isAgentAvailable()) {
  100. // If there was no agent registered when the original object was loaded then
  101. // we won't have created a debug object for it, so bail out.
  102. if (DebugObjects.find(Obj.getData().data()) == DebugObjects.end())
  103. return;
  104. const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
  105. // Use symbol info to iterate functions in the object.
  106. for (symbol_iterator I = DebugObj.symbol_begin(),
  107. E = DebugObj.symbol_end();
  108. I != E; ++I) {
  109. SymbolRef::Type SymType;
  110. if (I->getType(SymType)) continue;
  111. if (SymType == SymbolRef::ST_Function) {
  112. uint64_t Addr;
  113. if (I->getAddress(Addr)) continue;
  114. if (Wrapper->op_unload_native_code(Addr) == -1) {
  115. DEBUG(dbgs()
  116. << "Failed to tell OProfile about unload of native function at "
  117. << (void*)Addr << "\n");
  118. continue;
  119. }
  120. }
  121. }
  122. }
  123. DebugObjects.erase(Obj.getData().data());
  124. }
  125. } // anonymous namespace.
  126. namespace llvm {
  127. JITEventListener *JITEventListener::createOProfileJITEventListener() {
  128. return new OProfileJITEventListener(llvm::make_unique<OProfileWrapper>());
  129. }
  130. } // namespace llvm