Home | History | Annotate | Download | only in JIT
      1 //===- OProfileJITEventListenerTest.cpp - Unit tests for OProfileJITEventsListener --------===//
      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 #include "llvm/ExecutionEngine/OProfileWrapper.h"
     11 #include "JITEventListenerTestCommon.h"
     12 #include "llvm/ExecutionEngine/JITEventListener.h"
     13 #include <list>
     14 #include <map>
     15 
     16 using namespace llvm;
     17 
     18 namespace {
     19 
     20 struct OprofileNativeFunction {
     21   const char* Name;
     22   uint64_t Addr;
     23   const void* CodePtr;
     24   unsigned int CodeSize;
     25 
     26   OprofileNativeFunction(const char* name,
     27                          uint64_t addr,
     28                          const void* code,
     29                          unsigned int size)
     30   : Name(name)
     31   , Addr(addr)
     32   , CodePtr(code)
     33   , CodeSize(size) {
     34   }
     35 };
     36 
     37 typedef std::list<OprofileNativeFunction> NativeFunctionList;
     38 typedef std::list<debug_line_info> NativeDebugList;
     39 NativeFunctionList NativeFunctions;
     40 
     41 NativeCodeMap ReportedDebugFuncs;
     42 
     43 } // namespace
     44 
     45 /// Mock implementaion of opagent library
     46 namespace test_opagent {
     47 
     48 op_agent_t globalAgent = reinterpret_cast<op_agent_t>(42);
     49 
     50 op_agent_t open_agent()
     51 {
     52   // return non-null op_agent_t
     53   return globalAgent;
     54 }
     55 
     56 int close_agent(op_agent_t agent)
     57 {
     58   EXPECT_EQ(globalAgent, agent);
     59   return 0;
     60 }
     61 
     62 int write_native_code(op_agent_t agent,
     63                       const char* name,
     64                       uint64_t addr,
     65                       void const* code,
     66                       unsigned int size)
     67 {
     68   EXPECT_EQ(globalAgent, agent);
     69   OprofileNativeFunction func(name, addr, code, size);
     70   NativeFunctions.push_back(func);
     71 
     72   // Verify no other registration has take place for the same address
     73   EXPECT_TRUE(ReportedDebugFuncs.find(addr) == ReportedDebugFuncs.end());
     74 
     75   ReportedDebugFuncs[addr];
     76   return 0;
     77 }
     78 
     79 int write_debug_line_info(op_agent_t agent,
     80                           void const* code,
     81                           size_t num_entries,
     82                           struct debug_line_info const* info)
     83 {
     84   EXPECT_EQ(globalAgent, agent);
     85 
     86   //verify code has been loaded first
     87   uint64_t addr = reinterpret_cast<uint64_t>(code);
     88   NativeCodeMap::iterator i = ReportedDebugFuncs.find(addr);
     89   EXPECT_TRUE(i != ReportedDebugFuncs.end());
     90 
     91   NativeDebugList NativeInfo(info, info + num_entries);
     92 
     93   SourceLocations locs;
     94   for(NativeDebugList::iterator i = NativeInfo.begin();
     95       i != NativeInfo.end();
     96       ++i) {
     97     locs.push_back(std::make_pair(std::string(i->filename), i->lineno));
     98   }
     99   ReportedDebugFuncs[addr] = locs;
    100 
    101   return 0;
    102 }
    103 
    104 int unload_native_code(op_agent_t agent, uint64_t addr) {
    105   EXPECT_EQ(globalAgent, agent);
    106 
    107   //verify that something for the given JIT addr has been loaded first
    108   NativeCodeMap::iterator i = ReportedDebugFuncs.find(addr);
    109   EXPECT_TRUE(i != ReportedDebugFuncs.end());
    110   ReportedDebugFuncs.erase(i);
    111   return 0;
    112 }
    113 
    114 int version() {
    115   return 1;
    116 }
    117 
    118 bool is_oprofile_running() {
    119   return true;
    120 }
    121 
    122 } //namespace test_opagent
    123 
    124 class OProfileJITEventListenerTest
    125 : public JITEventListenerTestBase<OProfileWrapper>
    126 {
    127 public:
    128   OProfileJITEventListenerTest()
    129   : JITEventListenerTestBase<OProfileWrapper>(
    130     new OProfileWrapper(test_opagent::open_agent,
    131       test_opagent::close_agent,
    132       test_opagent::write_native_code,
    133       test_opagent::write_debug_line_info,
    134       test_opagent::unload_native_code,
    135       test_opagent::version,
    136       test_opagent::version,
    137       test_opagent::is_oprofile_running))
    138   {
    139     EXPECT_TRUE(0 != MockWrapper);
    140 
    141     Listener.reset(JITEventListener::createOProfileJITEventListener(
    142       MockWrapper.get()));
    143     EXPECT_TRUE(0 != Listener);
    144     EE->RegisterJITEventListener(Listener.get());
    145   }
    146 };
    147 
    148 TEST_F(OProfileJITEventListenerTest, NoDebugInfo) {
    149   TestNoDebugInfo(ReportedDebugFuncs);
    150 }
    151 
    152 TEST_F(OProfileJITEventListenerTest, SingleLine) {
    153   TestSingleLine(ReportedDebugFuncs);
    154 }
    155 
    156 TEST_F(OProfileJITEventListenerTest, MultipleLines) {
    157   TestMultipleLines(ReportedDebugFuncs);
    158 }
    159 
    160 TEST_F(OProfileJITEventListenerTest, MultipleFiles) {
    161   TestMultipleFiles(ReportedDebugFuncs);
    162 }
    163 
    164 testing::Environment* const jit_env =
    165   testing::AddGlobalTestEnvironment(new JITEnvironment);
    166