OProfileWrapper.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- OProfileWrapper.h - OProfile JIT API Wrapper ------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. // This file defines a OProfileWrapper object that detects if the oprofile
  14. // daemon is running, and provides wrappers for opagent functions used to
  15. // communicate with the oprofile JIT interface. The dynamic library libopagent
  16. // does not need to be linked directly as this object lazily loads the library
  17. // when the first op_ function is called.
  18. //
  19. // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
  20. // definition of the interface.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
  24. #define LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
  25. #include "llvm/Support/DataTypes.h"
  26. #include <opagent.h>
  27. namespace llvm {
  28. class OProfileWrapper {
  29. typedef op_agent_t (*op_open_agent_ptr_t)();
  30. typedef int (*op_close_agent_ptr_t)(op_agent_t);
  31. typedef int (*op_write_native_code_ptr_t)(op_agent_t,
  32. const char*,
  33. uint64_t,
  34. void const*,
  35. const unsigned int);
  36. typedef int (*op_write_debug_line_info_ptr_t)(op_agent_t,
  37. void const*,
  38. size_t,
  39. struct debug_line_info const*);
  40. typedef int (*op_unload_native_code_ptr_t)(op_agent_t, uint64_t);
  41. // Also used for op_minor_version function which has the same signature
  42. typedef int (*op_major_version_ptr_t)();
  43. // This is not a part of the opagent API, but is useful nonetheless
  44. typedef bool (*IsOProfileRunningPtrT)();
  45. op_agent_t Agent;
  46. op_open_agent_ptr_t OpenAgentFunc;
  47. op_close_agent_ptr_t CloseAgentFunc;
  48. op_write_native_code_ptr_t WriteNativeCodeFunc;
  49. op_write_debug_line_info_ptr_t WriteDebugLineInfoFunc;
  50. op_unload_native_code_ptr_t UnloadNativeCodeFunc;
  51. op_major_version_ptr_t MajorVersionFunc;
  52. op_major_version_ptr_t MinorVersionFunc;
  53. IsOProfileRunningPtrT IsOProfileRunningFunc;
  54. bool Initialized;
  55. public:
  56. OProfileWrapper();
  57. // For testing with a mock opagent implementation, skips the dynamic load and
  58. // the function resolution.
  59. OProfileWrapper(op_open_agent_ptr_t OpenAgentImpl,
  60. op_close_agent_ptr_t CloseAgentImpl,
  61. op_write_native_code_ptr_t WriteNativeCodeImpl,
  62. op_write_debug_line_info_ptr_t WriteDebugLineInfoImpl,
  63. op_unload_native_code_ptr_t UnloadNativeCodeImpl,
  64. op_major_version_ptr_t MajorVersionImpl,
  65. op_major_version_ptr_t MinorVersionImpl,
  66. IsOProfileRunningPtrT MockIsOProfileRunningImpl = 0)
  67. : OpenAgentFunc(OpenAgentImpl),
  68. CloseAgentFunc(CloseAgentImpl),
  69. WriteNativeCodeFunc(WriteNativeCodeImpl),
  70. WriteDebugLineInfoFunc(WriteDebugLineInfoImpl),
  71. UnloadNativeCodeFunc(UnloadNativeCodeImpl),
  72. MajorVersionFunc(MajorVersionImpl),
  73. MinorVersionFunc(MinorVersionImpl),
  74. IsOProfileRunningFunc(MockIsOProfileRunningImpl),
  75. Initialized(true)
  76. {
  77. }
  78. // Calls op_open_agent in the oprofile JIT library and saves the returned
  79. // op_agent_t handle internally so it can be used when calling all the other
  80. // op_* functions. Callers of this class do not need to keep track of
  81. // op_agent_t objects.
  82. bool op_open_agent();
  83. int op_close_agent();
  84. int op_write_native_code(const char* name,
  85. uint64_t addr,
  86. void const* code,
  87. const unsigned int size);
  88. int op_write_debug_line_info(void const* code,
  89. size_t num_entries,
  90. struct debug_line_info const* info);
  91. int op_unload_native_code(uint64_t addr);
  92. int op_major_version();
  93. int op_minor_version();
  94. // Returns true if the oprofiled process is running, the opagent library is
  95. // loaded and a connection to the agent has been established, and false
  96. // otherwise.
  97. bool isAgentAvailable();
  98. private:
  99. // Loads the libopagent library and initializes this wrapper if the oprofile
  100. // daemon is running
  101. bool initialize();
  102. // Searches /proc for the oprofile daemon and returns true if the process if
  103. // found, or false otherwise.
  104. bool checkForOProfileProcEntry();
  105. bool isOProfileRunning();
  106. };
  107. } // namespace llvm
  108. #endif // LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
  109. #ifdef __GNUC__
  110. #pragma GCC diagnostic pop
  111. #endif