Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.8 KiB

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