1//===-- JITDebugRegisterer.cpp - Register debug symbols for JIT -----------===// 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// This file defines a JITDebugRegisterer object that is used by the JIT to 11// register debug info with debuggers like GDB. 12// 13//===----------------------------------------------------------------------===// 14 15#include "JITDebugRegisterer.h" 16#include "../../CodeGen/ELF.h" 17#include "../../CodeGen/ELFWriter.h" 18#include "llvm/LLVMContext.h" 19#include "llvm/Function.h" 20#include "llvm/Module.h" 21#include "llvm/Target/TargetMachine.h" 22#include "llvm/Target/TargetOptions.h" 23#include "llvm/ADT/DenseMap.h" 24#include "llvm/ADT/OwningPtr.h" 25#include "llvm/Support/Compiler.h" 26#include "llvm/Support/MutexGuard.h" 27#include "llvm/Support/raw_ostream.h" 28#include "llvm/Support/Mutex.h" 29#include <string> 30 31namespace llvm { 32 33// This must be kept in sync with gdb/gdb/jit.h . 34extern "C" { 35 36 // Debuggers puts a breakpoint in this function. 37 LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() { } 38 39 // We put information about the JITed function in this global, which the 40 // debugger reads. Make sure to specify the version statically, because the 41 // debugger checks the version before we can set it during runtime. 42 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 }; 43 44} 45 46namespace { 47 48 /// JITDebugLock - Used to serialize all code registration events, since they 49 /// modify global variables. 50 sys::Mutex JITDebugLock; 51 52} 53 54JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { } 55 56JITDebugRegisterer::~JITDebugRegisterer() { 57 // Free all ELF memory. 58 for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end(); 59 I != E; ++I) { 60 // Call the private method that doesn't update the map so our iterator 61 // doesn't break. 62 UnregisterFunctionInternal(I); 63 } 64 FnMap.clear(); 65} 66 67std::string JITDebugRegisterer::MakeELF(const Function *F, DebugInfo &I) { 68 // Stack allocate an empty module with an empty LLVMContext for the ELFWriter 69 // API. We don't use the real module because then the ELFWriter would write 70 // out unnecessary GlobalValues during finalization. 71 LLVMContext Context; 72 Module M("", Context); 73 74 // Make a buffer for the ELF in memory. 75 std::string Buffer; 76 raw_string_ostream O(Buffer); 77 ELFWriter EW(O, TM); 78 EW.doInitialization(M); 79 80 // Copy the binary into the .text section. This isn't necessary, but it's 81 // useful to be able to disassemble the ELF by hand. 82 ELFSection &Text = EW.getTextSection(const_cast<Function *>(F)); 83 Text.Addr = (uint64_t)I.FnStart; 84 // TODO: We could eliminate this copy if we somehow used a pointer/size pair 85 // instead of a vector. 86 Text.getData().assign(I.FnStart, I.FnEnd); 87 88 // Copy the exception handling call frame information into the .eh_frame 89 // section. This allows GDB to get a good stack trace, particularly on 90 // linux x86_64. Mark this as a PROGBITS section that needs to be loaded 91 // into memory at runtime. 92 ELFSection &EH = EW.getSection(".eh_frame", ELF::SHT_PROGBITS, 93 ELF::SHF_ALLOC); 94 // Pointers in the DWARF EH info are all relative to the EH frame start, 95 // which is stored here. 96 EH.Addr = (uint64_t)I.EhStart; 97 // TODO: We could eliminate this copy if we somehow used a pointer/size pair 98 // instead of a vector. 99 EH.getData().assign(I.EhStart, I.EhEnd); 100 101 // Add this single function to the symbol table, so the debugger prints the 102 // name instead of '???'. We give the symbol default global visibility. 103 ELFSym *FnSym = ELFSym::getGV(F, 104 ELF::STB_GLOBAL, 105 ELF::STT_FUNC, 106 ELF::STV_DEFAULT); 107 FnSym->SectionIdx = Text.SectionIdx; 108 FnSym->Size = I.FnEnd - I.FnStart; 109 FnSym->Value = 0; // Offset from start of section. 110 EW.SymbolList.push_back(FnSym); 111 112 EW.doFinalization(M); 113 O.flush(); 114 115 // When trying to debug why GDB isn't getting the debug info right, it's 116 // awfully helpful to write the object file to disk so that it can be 117 // inspected with readelf and objdump. 118 if (JITEmitDebugInfoToDisk) { 119 std::string Filename; 120 raw_string_ostream O2(Filename); 121 O2 << "/tmp/llvm_function_" << I.FnStart << "_" << F->getNameStr() << ".o"; 122 O2.flush(); 123 std::string Errors; 124 raw_fd_ostream O3(Filename.c_str(), Errors); 125 O3 << Buffer; 126 O3.close(); 127 } 128 129 return Buffer; 130} 131 132void JITDebugRegisterer::RegisterFunction(const Function *F, DebugInfo &I) { 133 // TODO: Support non-ELF platforms. 134 if (!TM.getELFWriterInfo()) 135 return; 136 137 std::string Buffer = MakeELF(F, I); 138 139 jit_code_entry *JITCodeEntry = new jit_code_entry(); 140 JITCodeEntry->symfile_addr = Buffer.c_str(); 141 JITCodeEntry->symfile_size = Buffer.size(); 142 143 // Add a mapping from F to the entry and buffer, so we can delete this 144 // info later. 145 FnMap[F] = std::make_pair(Buffer, JITCodeEntry); 146 147 // Acquire the lock and do the registration. 148 { 149 MutexGuard locked(JITDebugLock); 150 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; 151 152 // Insert this entry at the head of the list. 153 JITCodeEntry->prev_entry = NULL; 154 jit_code_entry *NextEntry = __jit_debug_descriptor.first_entry; 155 JITCodeEntry->next_entry = NextEntry; 156 if (NextEntry != NULL) { 157 NextEntry->prev_entry = JITCodeEntry; 158 } 159 __jit_debug_descriptor.first_entry = JITCodeEntry; 160 __jit_debug_descriptor.relevant_entry = JITCodeEntry; 161 __jit_debug_register_code(); 162 } 163} 164 165void JITDebugRegisterer::UnregisterFunctionInternal( 166 RegisteredFunctionsMap::iterator I) { 167 jit_code_entry *&JITCodeEntry = I->second.second; 168 169 // Acquire the lock and do the unregistration. 170 { 171 MutexGuard locked(JITDebugLock); 172 __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN; 173 174 // Remove the jit_code_entry from the linked list. 175 jit_code_entry *PrevEntry = JITCodeEntry->prev_entry; 176 jit_code_entry *NextEntry = JITCodeEntry->next_entry; 177 if (NextEntry) { 178 NextEntry->prev_entry = PrevEntry; 179 } 180 if (PrevEntry) { 181 PrevEntry->next_entry = NextEntry; 182 } else { 183 assert(__jit_debug_descriptor.first_entry == JITCodeEntry); 184 __jit_debug_descriptor.first_entry = NextEntry; 185 } 186 187 // Tell GDB which entry we removed, and unregister the code. 188 __jit_debug_descriptor.relevant_entry = JITCodeEntry; 189 __jit_debug_register_code(); 190 } 191 192 delete JITCodeEntry; 193 JITCodeEntry = NULL; 194 195 // Free the ELF file in memory. 196 std::string &Buffer = I->second.first; 197 Buffer.clear(); 198} 199 200void JITDebugRegisterer::UnregisterFunction(const Function *F) { 201 // TODO: Support non-ELF platforms. 202 if (!TM.getELFWriterInfo()) 203 return; 204 205 RegisteredFunctionsMap::iterator I = FnMap.find(F); 206 if (I == FnMap.end()) return; 207 UnregisterFunctionInternal(I); 208 FnMap.erase(I); 209} 210 211} // end namespace llvm 212