Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:54

0001 //===-- OProfileWrapper.h - OProfile JIT API Wrapper ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 // This file defines a OProfileWrapper object that detects if the oprofile
0009 // daemon is running, and provides wrappers for opagent functions used to
0010 // communicate with the oprofile JIT interface. The dynamic library libopagent
0011 // does not need to be linked directly as this object lazily loads the library
0012 // when the first op_ function is called.
0013 //
0014 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
0015 // definition of the interface.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
0020 #define LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
0021 
0022 #include "llvm/Support/DataTypes.h"
0023 #include <opagent.h>
0024 
0025 namespace llvm {
0026 
0027 
0028 class OProfileWrapper {
0029   typedef  op_agent_t    (*op_open_agent_ptr_t)();
0030   typedef  int           (*op_close_agent_ptr_t)(op_agent_t);
0031   typedef  int           (*op_write_native_code_ptr_t)(op_agent_t,
0032                                                 const char*,
0033                                                 uint64_t,
0034                                                 void const*,
0035                                                 const unsigned int);
0036   typedef  int           (*op_write_debug_line_info_ptr_t)(op_agent_t,
0037                                                 void const*,
0038                                                 size_t,
0039                                                 struct debug_line_info const*);
0040   typedef  int           (*op_unload_native_code_ptr_t)(op_agent_t, uint64_t);
0041 
0042   // Also used for op_minor_version function which has the same signature
0043   typedef  int           (*op_major_version_ptr_t)();
0044 
0045   // This is not a part of the opagent API, but is useful nonetheless
0046   typedef  bool          (*IsOProfileRunningPtrT)();
0047 
0048 
0049   op_agent_t                      Agent;
0050   op_open_agent_ptr_t             OpenAgentFunc;
0051   op_close_agent_ptr_t            CloseAgentFunc;
0052   op_write_native_code_ptr_t      WriteNativeCodeFunc;
0053   op_write_debug_line_info_ptr_t  WriteDebugLineInfoFunc;
0054   op_unload_native_code_ptr_t     UnloadNativeCodeFunc;
0055   op_major_version_ptr_t          MajorVersionFunc;
0056   op_major_version_ptr_t          MinorVersionFunc;
0057   IsOProfileRunningPtrT           IsOProfileRunningFunc;
0058 
0059   bool Initialized;
0060 
0061 public:
0062   OProfileWrapper();
0063 
0064   // For testing with a mock opagent implementation, skips the dynamic load and
0065   // the function resolution.
0066   OProfileWrapper(op_open_agent_ptr_t OpenAgentImpl,
0067                   op_close_agent_ptr_t CloseAgentImpl,
0068                   op_write_native_code_ptr_t WriteNativeCodeImpl,
0069                   op_write_debug_line_info_ptr_t WriteDebugLineInfoImpl,
0070                   op_unload_native_code_ptr_t UnloadNativeCodeImpl,
0071                   op_major_version_ptr_t MajorVersionImpl,
0072                   op_major_version_ptr_t MinorVersionImpl,
0073                   IsOProfileRunningPtrT MockIsOProfileRunningImpl = 0)
0074   : OpenAgentFunc(OpenAgentImpl),
0075     CloseAgentFunc(CloseAgentImpl),
0076     WriteNativeCodeFunc(WriteNativeCodeImpl),
0077     WriteDebugLineInfoFunc(WriteDebugLineInfoImpl),
0078     UnloadNativeCodeFunc(UnloadNativeCodeImpl),
0079     MajorVersionFunc(MajorVersionImpl),
0080     MinorVersionFunc(MinorVersionImpl),
0081     IsOProfileRunningFunc(MockIsOProfileRunningImpl),
0082     Initialized(true)
0083   {
0084   }
0085 
0086   // Calls op_open_agent in the oprofile JIT library and saves the returned
0087   // op_agent_t handle internally so it can be used when calling all the other
0088   // op_* functions. Callers of this class do not need to keep track of
0089   // op_agent_t objects.
0090   bool op_open_agent();
0091 
0092   int op_close_agent();
0093   int op_write_native_code(const char* name,
0094                            uint64_t addr,
0095                            void const* code,
0096                            const unsigned int size);
0097   int op_write_debug_line_info(void const* code,
0098                                size_t num_entries,
0099                                struct debug_line_info const* info);
0100   int op_unload_native_code(uint64_t addr);
0101   int op_major_version();
0102   int op_minor_version();
0103 
0104   // Returns true if the oprofiled process is running, the opagent library is
0105   // loaded and a connection to the agent has been established, and false
0106   // otherwise.
0107   bool isAgentAvailable();
0108 
0109 private:
0110   // Loads the libopagent library and initializes this wrapper if the oprofile
0111   // daemon is running
0112   bool initialize();
0113 
0114   // Searches /proc for the oprofile daemon and returns true if the process is
0115   // found, or false otherwise.
0116   bool checkForOProfileProcEntry();
0117 
0118   bool isOProfileRunning();
0119 };
0120 
0121 } // namespace llvm
0122 
0123 #endif // LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H