| LEFT | RIGHT |
| 1 //===-- JITDebugRegisterer.cpp - Register debug symbols for JIT -----------===// | 1 //===-- JITDebugRegisterer.cpp - Register debug symbols for JIT -----------===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file defines a JITDebugRegisterer object that is used by the JIT to | 10 // This file defines a JITDebugRegisterer object that is used by the JIT to |
| 11 // register debug info with debuggers like GDB. | 11 // register debug info with debuggers like GDB. |
| 12 // | 12 // |
| 13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 14 | 14 |
| 15 #include "JITDebugRegisterer.h" | 15 #include "JITDebugRegisterer.h" |
| 16 #include "../../CodeGen/ELF.h" | 16 #include "../../CodeGen/ELF.h" |
| 17 #include "../../CodeGen/ELFWriter.h" | 17 #include "../../CodeGen/ELFWriter.h" |
| 18 #include "llvm/LLVMContext.h" | 18 #include "llvm/LLVMContext.h" |
| 19 #include "llvm/Function.h" | 19 #include "llvm/Function.h" |
| 20 #include "llvm/Module.h" | 20 #include "llvm/Module.h" |
| 21 #include "llvm/Target/TargetMachine.h" | 21 #include "llvm/Target/TargetMachine.h" |
| 22 #include "llvm/ADT/DenseMap.h" | 22 #include "llvm/ADT/DenseMap.h" |
| 23 #include "llvm/ADT/OwningPtr.h" | 23 #include "llvm/ADT/OwningPtr.h" |
| 24 #include "llvm/Support/MutexGuard.h" |
| 24 #include "llvm/Support/raw_ostream.h" | 25 #include "llvm/Support/raw_ostream.h" |
| 25 #include "llvm/System/Path.h" | 26 #include "llvm/System/Mutex.h" |
| 26 #include <string> | 27 #include <string> |
| 27 #include <vector> | 28 #include <vector> |
| 28 | 29 |
| 29 namespace llvm { | 30 namespace llvm { |
| 30 | 31 |
| 31 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) | 32 // This must be kept in sync with gdb/gdb/jit.h . |
| 32 : TM(tm), FnMap(), TmpDir(sys::Path::GetTemporaryDirectory()) { } | 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() { } |
| 33 | 54 |
| 34 JITDebugRegisterer::~JITDebugRegisterer() { | 55 JITDebugRegisterer::~JITDebugRegisterer() { |
| 35 // Clean up all the temporary files. | 56 // Free all ELF memory. |
| 36 for (DenseMap<const Function*, sys::Path>::iterator I = FnMap.begin(), | 57 for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end(); |
| 37 E = FnMap.end(); I != E; ++I) { | 58 I != E; ++I) { |
| 38 // Call the private method that doesn't update the map so our iterator | 59 // Call the private method that doesn't update the map so our iterator |
| 39 // doesn't break. | 60 // doesn't break. |
| 40 UnregisterFunctionInternal(I); | 61 UnregisterFunctionInternal(I); |
| 41 } | 62 } |
| 42 FnMap.clear(); | 63 FnMap.clear(); |
| 43 TmpDir.eraseFromDisk(); | 64 } |
| 44 } | 65 |
| 45 | 66 std::string JITDebugRegisterer::MakeELF(const Function *F, DebugInfo &I) { |
| 46 sys::Path JITDebugRegisterer::MakeFilename(const Function *F, | 67 // Stack allocate an empty module with an empty LLVMContext for the ELFWriter |
| 47 uint8_t *FnStart) { | 68 // API. We don't use the real module because then the ELFWriter would write |
| 48 std::string Filename; | 69 // out unnecessary GlobalValues during finalization. |
| 49 raw_string_ostream O(Filename); | 70 LLVMContext Context; |
| 50 // Address first so it's easier to find the right filename when trying to add | 71 Module M("", Context); |
| 51 // the symbol file interactively. | 72 |
| 52 O << "llvm_function_" << FnStart << "_" << F->getNameStr() << ".o"; | 73 // Make a buffer for the ELF in memory. |
| 53 O.flush(); | 74 std::string Buffer; |
| 54 sys::Path TmpPath(TmpDir); // Copy path. | 75 raw_string_ostream O(Buffer); |
| 55 TmpPath.appendComponent(Filename); | |
| 56 return TmpPath; | |
| 57 } | |
| 58 | |
| 59 void JITDebugRegisterer::RegisterFunction(const Function *F, DebugInfo &I) { | |
| 60 // Pick the filename for the function. | |
| 61 sys::Path Filename = MakeFilename(F, I.FnStart); | |
| 62 | |
| 63 // Add a mapping from F to Filename, so we can delete this function later. | |
| 64 FnMap[F] = Filename; | |
| 65 | |
| 66 // Stack allocate an empty module for the ELFWriter API. We don't use the | |
| 67 // real module because then the ELFWriter would write out unnecessary | |
| 68 // GlobalValues during finalization. | |
| 69 Module M("", getGlobalContext()); | |
| 70 | |
| 71 // Open the ELF file. | |
| 72 // TODO: Keep the ELF in memory. | |
| 73 std::string Error; | |
| 74 raw_fd_ostream O(Filename.c_str(), /*Binary=*/ true, Error); | |
| 75 if (Error != "") { | |
| 76 fprintf(stderr, "Error opening file '%s': %s", | |
| 77 Filename.c_str(), Error.c_str()); | |
| 78 abort(); | |
| 79 } | |
| 80 ELFWriter EW(O, TM); | 76 ELFWriter EW(O, TM); |
| 81 EW.doInitialization(M); | 77 EW.doInitialization(M); |
| 82 | 78 |
| 83 // Copy the binary into the .text section. This isn't necessary, but it's | 79 // Copy the binary into the .text section. This isn't necessary, but it's |
| 84 // useful to be able to disassemble the ELF by hand. | 80 // useful to be able to disassemble the ELF by hand. |
| 85 ELFSection &Text = EW.getTextSection(); | 81 ELFSection &Text = EW.getTextSection((Function *)F); |
| 86 Text.Addr = (uint64_t)I.FnStart; | 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. |
| 87 Text.getData().assign(I.FnStart, I.FnEnd); | 85 Text.getData().assign(I.FnStart, I.FnEnd); |
| 88 | 86 |
| 89 // Copy the exception handling call frame information into the .eh_frame | 87 // Copy the exception handling call frame information into the .eh_frame |
| 90 // section. This allows GDB to get a good stack trace, particularly on | 88 // section. This allows GDB to get a good stack trace, particularly on |
| 91 // linux x86_64. Mark this as a PROGBITS section that needs to be loaded | 89 // linux x86_64. Mark this as a PROGBITS section that needs to be loaded |
| 92 // into memory at runtime. | 90 // into memory at runtime. |
| 93 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS, | 91 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS, |
| 94 ELFSection::SHF_ALLOC); | 92 ELFSection::SHF_ALLOC); |
| 95 // Pointers in the DWARF EH info are all relative to the EH frame start, | 93 // Pointers in the DWARF EH info are all relative to the EH frame start, |
| 96 // which is stored here. | 94 // which is stored here. |
| 97 EH.Addr = (uint64_t)I.EhStart; | 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); | 98 EH.getData().assign(I.EhStart, I.EhEnd); |
| 99 | 99 |
| 100 // Add this single function to the symbol table, so GDB prints the name | 100 // Add this single function to the symbol table, so the debugger prints the |
| 101 // instead of '???'. | 101 // name instead of '???'. We give the symbol default global visibility. |
| 102 ELFSym FnSym(F); | 102 ELFSym *FnSym = ELFSym::getGV(F, |
| 103 FnSym.setBind(ELFSym::STB_GLOBAL); // Make it globally visible. | 103 ELFSym::STB_GLOBAL, |
| 104 FnSym.setType(ELFSym::STT_FUNC); | 104 ELFSym::STT_FUNC, |
| 105 FnSym.SectionIdx = Text.SectionIdx; | 105 ELFSym::STV_DEFAULT); |
| 106 FnSym.Size = I.FnEnd - I.FnStart; | 106 FnSym->SectionIdx = Text.SectionIdx; |
| 107 FnSym.Value = 0; // Offset from start of section. | 107 FnSym->Size = I.FnEnd - I.FnStart; |
| 108 FnSym->Value = 0; // Offset from start of section. |
| 108 EW.SymbolList.push_back(FnSym); | 109 EW.SymbolList.push_back(FnSym); |
| 109 | 110 |
| 110 EW.doFinalization(M); | 111 EW.doFinalization(M); |
| 111 | 112 O.flush(); |
| 112 // TODO: Automatically register the ELF with GDB. | 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 } |
| 113 } | 159 } |
| 114 | 160 |
| 115 void JITDebugRegisterer::UnregisterFunctionInternal( | 161 void JITDebugRegisterer::UnregisterFunctionInternal( |
| 116 DenseMap<const Function*, sys::Path>::iterator I) { | 162 RegisteredFunctionsMap::iterator I) { |
| 117 // It would be nice if we could actually unregister the debug info, but for | 163 jit_code_entry *JITCodeEntry = I->second.second; |
| 118 // now there is no way to undo an add-symbol-file. However, the most | 164 |
| 119 // recently loaded symbols take precedence, so if we reuse memory for new | 165 // Acquire the lock and do the unregistration. |
| 120 // machine functions everything should just work. | 166 { |
| 121 | 167 MutexGuard locked(JITDebugLock); |
| 122 // Delete the ELF file. We don't catch any errors since deleting this temp | 168 __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN; |
| 123 // file isn't critical. | 169 |
| 124 sys::Path &P = I->second; | 170 // Remove the jit_code_entry from the linked list. |
| 125 P.eraseFromDisk(); | 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(); |
| 126 } | 191 } |
| 127 | 192 |
| 128 void JITDebugRegisterer::UnregisterFunction(const Function *F) { | 193 void JITDebugRegisterer::UnregisterFunction(const Function *F) { |
| 129 DenseMap<const Function*, sys::Path>::iterator I = FnMap.find(F); | 194 // TODO: Support non-ELF platforms. |
| 195 if (!TM.getELFWriterInfo()) |
| 196 return; |
| 197 |
| 198 RegisteredFunctionsMap::iterator I = FnMap.find(F); |
| 130 if (I == FnMap.end()) return; | 199 if (I == FnMap.end()) return; |
| 131 UnregisterFunctionInternal(I); | 200 UnregisterFunctionInternal(I); |
| 132 FnMap.erase(I); | 201 FnMap.erase(I); |
| 133 } | 202 } |
| 134 | 203 |
| 135 } // end namespace llvm | 204 } // end namespace llvm |
| LEFT | RIGHT |