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