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