1 //===-- JITDebugRegisterer.cpp - Register debug symbols for JIT -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a JITDebugRegisterer object that is used by the JIT to
11 // register debug info with debuggers like GDB.
13 //===----------------------------------------------------------------------===//
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"
33 // This must be kept in sync with gdb/gdb/jit.h .
36 // Debuggers puts a breakpoint in this function.
37 LLVM_ATTRIBUTE_NOINLINE void __jit_debug_register_code() { }
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 };
48 /// JITDebugLock - Used to serialize all code registration events, since they
49 /// modify global variables.
50 sys::Mutex JITDebugLock;
54 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { }
56 JITDebugRegisterer::~JITDebugRegisterer() {
57 // Free all ELF memory.
58 for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end();
60 // Call the private method that doesn't update the map so our iterator
62 UnregisterFunctionInternal(I);
67 std::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.
72 Module M("", Context);
74 // Make a buffer for the ELF in memory.
76 raw_string_ostream O(Buffer);
78 EW.doInitialization(M);
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);
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,
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);
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,
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);
112 EW.doFinalization(M);
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";
124 raw_fd_ostream O3(Filename.c_str(), Errors);
132 void JITDebugRegisterer::RegisterFunction(const Function *F, DebugInfo &I) {
133 // TODO: Support non-ELF platforms.
134 if (!TM.getELFWriterInfo())
137 std::string Buffer = MakeELF(F, I);
139 jit_code_entry *JITCodeEntry = new jit_code_entry();
140 JITCodeEntry->symfile_addr = Buffer.c_str();
141 JITCodeEntry->symfile_size = Buffer.size();
143 // Add a mapping from F to the entry and buffer, so we can delete this
145 FnMap[F] = std::make_pair(Buffer, JITCodeEntry);
147 // Acquire the lock and do the registration.
149 MutexGuard locked(JITDebugLock);
150 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
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;
159 __jit_debug_descriptor.first_entry = JITCodeEntry;
160 __jit_debug_descriptor.relevant_entry = JITCodeEntry;
161 __jit_debug_register_code();
165 void JITDebugRegisterer::UnregisterFunctionInternal(
166 RegisteredFunctionsMap::iterator I) {
167 jit_code_entry *&JITCodeEntry = I->second.second;
169 // Acquire the lock and do the unregistration.
171 MutexGuard locked(JITDebugLock);
172 __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
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;
178 NextEntry->prev_entry = PrevEntry;
181 PrevEntry->next_entry = NextEntry;
183 assert(__jit_debug_descriptor.first_entry == JITCodeEntry);
184 __jit_debug_descriptor.first_entry = NextEntry;
187 // Tell GDB which entry we removed, and unregister the code.
188 __jit_debug_descriptor.relevant_entry = JITCodeEntry;
189 __jit_debug_register_code();
195 // Free the ELF file in memory.
196 std::string &Buffer = I->second.first;
200 void JITDebugRegisterer::UnregisterFunction(const Function *F) {
201 // TODO: Support non-ELF platforms.
202 if (!TM.getELFWriterInfo())
205 RegisteredFunctionsMap::iterator I = FnMap.find(F);
206 if (I == FnMap.end()) return;
207 UnregisterFunctionInternal(I);
211 } // end namespace llvm