LLVM API Documentation
00001 //===-- MacOSJITEventListener.cpp - Save symbol table for OSX perf tools --===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a JITEventListener object that records JITted functions to 00011 // a global __jitSymbolTable linked list. Apple's performance tools use this to 00012 // determine a symbol name and accurate code range for a PC value. Because 00013 // performance tools are generally asynchronous, the code below is written with 00014 // the hope that it could be interrupted at any time and have useful answers. 00015 // However, we don't go crazy with atomic operations, we just do a "reasonable 00016 // effort". 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #define DEBUG_TYPE "macos-jit-event-listener" 00021 #include "llvm/Function.h" 00022 #include "llvm/ExecutionEngine/JITEventListener.h" 00023 #include <stddef.h> 00024 using namespace llvm; 00025 00026 #ifdef __APPLE__ 00027 #define ENABLE_JIT_SYMBOL_TABLE 0 00028 #endif 00029 00030 #if ENABLE_JIT_SYMBOL_TABLE 00031 00032 namespace { 00033 00034 /// JITSymbolEntry - Each function that is JIT compiled results in one of these 00035 /// being added to an array of symbols. This indicates the name of the function 00036 /// as well as the address range it occupies. This allows the client to map 00037 /// from a PC value to the name of the function. 00038 struct JITSymbolEntry { 00039 const char *FnName; // FnName - a strdup'd string. 00040 void *FnStart; 00041 intptr_t FnSize; 00042 }; 00043 00044 00045 struct JITSymbolTable { 00046 /// NextPtr - This forms a linked list of JitSymbolTable entries. This 00047 /// pointer is not used right now, but might be used in the future. Consider 00048 /// it reserved for future use. 00049 JITSymbolTable *NextPtr; 00050 00051 /// Symbols - This is an array of JitSymbolEntry entries. Only the first 00052 /// 'NumSymbols' symbols are valid. 00053 JITSymbolEntry *Symbols; 00054 00055 /// NumSymbols - This indicates the number entries in the Symbols array that 00056 /// are valid. 00057 unsigned NumSymbols; 00058 00059 /// NumAllocated - This indicates the amount of space we have in the Symbols 00060 /// array. This is a private field that should not be read by external tools. 00061 unsigned NumAllocated; 00062 }; 00063 00064 class MacOSJITEventListener : public JITEventListener { 00065 public: 00066 virtual void NotifyFunctionEmitted(const Function &F, 00067 void *FnStart, size_t FnSize, 00068 const EmittedFunctionDetails &Details); 00069 virtual void NotifyFreeingMachineCode(const Function &F, void *OldPtr); 00070 }; 00071 00072 } // anonymous namespace. 00073 00074 // This is a public symbol so the performance tools can find it. 00075 JITSymbolTable *__jitSymbolTable; 00076 00077 namespace llvm { 00078 JITEventListener *createMacOSJITEventListener() { 00079 return new MacOSJITEventListener; 00080 } 00081 } 00082 00083 // Adds the just-emitted function to the symbol table. 00084 void MacOSJITEventListener::NotifyFunctionEmitted( 00085 const Function &F, void *FnStart, size_t FnSize, 00086 const EmittedFunctionDetails &) { 00087 assert(F.hasName() && FnStart != 0 && "Bad symbol to add"); 00088 JITSymbolTable **SymTabPtrPtr = 0; 00089 SymTabPtrPtr = &__jitSymbolTable; 00090 00091 // If this is the first entry in the symbol table, add the JITSymbolTable 00092 // index. 00093 if (*SymTabPtrPtr == 0) { 00094 JITSymbolTable *New = new JITSymbolTable(); 00095 New->NextPtr = 0; 00096 New->Symbols = 0; 00097 New->NumSymbols = 0; 00098 New->NumAllocated = 0; 00099 *SymTabPtrPtr = New; 00100 } 00101 00102 JITSymbolTable *SymTabPtr = *SymTabPtrPtr; 00103 00104 // If we have space in the table, reallocate the table. 00105 if (SymTabPtr->NumSymbols >= SymTabPtr->NumAllocated) { 00106 // If we don't have space, reallocate the table. 00107 unsigned NewSize = std::max(64U, SymTabPtr->NumAllocated*2); 00108 JITSymbolEntry *NewSymbols = new JITSymbolEntry[NewSize]; 00109 JITSymbolEntry *OldSymbols = SymTabPtr->Symbols; 00110 00111 // Copy the old entries over. 00112 memcpy(NewSymbols, OldSymbols, SymTabPtr->NumSymbols*sizeof(OldSymbols[0])); 00113 00114 // Swap the new symbols in, delete the old ones. 00115 SymTabPtr->Symbols = NewSymbols; 00116 SymTabPtr->NumAllocated = NewSize; 00117 delete [] OldSymbols; 00118 } 00119 00120 // Otherwise, we have enough space, just tack it onto the end of the array. 00121 JITSymbolEntry &Entry = SymTabPtr->Symbols[SymTabPtr->NumSymbols]; 00122 Entry.FnName = strdup(F.getName().data()); 00123 Entry.FnStart = FnStart; 00124 Entry.FnSize = FnSize; 00125 ++SymTabPtr->NumSymbols; 00126 } 00127 00128 // Removes the to-be-deleted function from the symbol table. 00129 void MacOSJITEventListener::NotifyFreeingMachineCode( 00130 const Function &, void *FnStart) { 00131 assert(FnStart && "Invalid function pointer"); 00132 JITSymbolTable **SymTabPtrPtr = 0; 00133 SymTabPtrPtr = &__jitSymbolTable; 00134 00135 JITSymbolTable *SymTabPtr = *SymTabPtrPtr; 00136 JITSymbolEntry *Symbols = SymTabPtr->Symbols; 00137 00138 // Scan the table to find its index. The table is not sorted, so do a linear 00139 // scan. 00140 unsigned Index; 00141 for (Index = 0; Symbols[Index].FnStart != FnStart; ++Index) 00142 assert(Index != SymTabPtr->NumSymbols && "Didn't find function!"); 00143 00144 // Once we have an index, we know to nuke this entry, overwrite it with the 00145 // entry at the end of the array, making the last entry redundant. 00146 const char *OldName = Symbols[Index].FnName; 00147 Symbols[Index] = Symbols[SymTabPtr->NumSymbols-1]; 00148 free((void*)OldName); 00149 00150 // Drop the number of symbols in the table. 00151 --SymTabPtr->NumSymbols; 00152 00153 // Finally, if we deleted the final symbol, deallocate the table itself. 00154 if (SymTabPtr->NumSymbols != 0) 00155 return; 00156 00157 *SymTabPtrPtr = 0; 00158 delete [] Symbols; 00159 delete SymTabPtr; 00160 } 00161 00162 #else // !ENABLE_JIT_SYMBOL_TABLE 00163 00164 namespace llvm { 00165 // By defining this to return NULL, we can let clients call it unconditionally, 00166 // even if they aren't on an Apple system. 00167 JITEventListener *createMacOSJITEventListener() { 00168 return NULL; 00169 } 00170 } // namespace llvm 00171 00172 #endif // ENABLE_JIT_SYMBOL_TABLE