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/System/Mutex.h"
34 // This must be kept in sync with gdb/gdb/jit.h .
37 // Debuggers puts a breakpoint in this function.
38 LLVM_ATTRIBUTE_NOINLINE
void __jit_debug_register_code() { }
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 };
49 /// JITDebugLock - Used to serialize all code registration events, since they
50 /// modify global variables.
51 sys::Mutex JITDebugLock
;
55 JITDebugRegisterer::JITDebugRegisterer(TargetMachine
&tm
) : TM(tm
), FnMap() { }
57 JITDebugRegisterer::~JITDebugRegisterer() {
58 // Free all ELF memory.
59 for (RegisteredFunctionsMap::iterator I
= FnMap
.begin(), E
= FnMap
.end();
61 // Call the private method that doesn't update the map so our iterator
63 UnregisterFunctionInternal(I
);
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.
73 Module
M("", Context
);
75 // Make a buffer for the ELF in memory.
77 raw_string_ostream
O(Buffer
);
79 EW
.doInitialization(M
);
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
);
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
,
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
);
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
,
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
);
113 EW
.doFinalization(M
);
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";
125 raw_fd_ostream
O3(Filename
.c_str(), Errors
);
133 void JITDebugRegisterer::RegisterFunction(const Function
*F
, DebugInfo
&I
) {
134 // TODO: Support non-ELF platforms.
135 if (!TM
.getELFWriterInfo())
138 std::string Buffer
= MakeELF(F
, I
);
140 jit_code_entry
*JITCodeEntry
= new jit_code_entry();
141 JITCodeEntry
->symfile_addr
= Buffer
.c_str();
142 JITCodeEntry
->symfile_size
= Buffer
.size();
144 // Add a mapping from F to the entry and buffer, so we can delete this
146 FnMap
[F
] = std::make_pair
<std::string
, jit_code_entry
*>(Buffer
, JITCodeEntry
);
148 // Acquire the lock and do the registration.
150 MutexGuard
locked(JITDebugLock
);
151 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
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
;
160 __jit_debug_descriptor
.first_entry
= JITCodeEntry
;
161 __jit_debug_descriptor
.relevant_entry
= JITCodeEntry
;
162 __jit_debug_register_code();
166 void JITDebugRegisterer::UnregisterFunctionInternal(
167 RegisteredFunctionsMap::iterator I
) {
168 jit_code_entry
*&JITCodeEntry
= I
->second
.second
;
170 // Acquire the lock and do the unregistration.
172 MutexGuard
locked(JITDebugLock
);
173 __jit_debug_descriptor
.action_flag
= JIT_UNREGISTER_FN
;
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
;
179 NextEntry
->prev_entry
= PrevEntry
;
182 PrevEntry
->next_entry
= NextEntry
;
184 assert(__jit_debug_descriptor
.first_entry
== JITCodeEntry
);
185 __jit_debug_descriptor
.first_entry
= NextEntry
;
188 // Tell GDB which entry we removed, and unregister the code.
189 __jit_debug_descriptor
.relevant_entry
= JITCodeEntry
;
190 __jit_debug_register_code();
196 // Free the ELF file in memory.
197 std::string
&Buffer
= I
->second
.first
;
201 void JITDebugRegisterer::UnregisterFunction(const Function
*F
) {
202 // TODO: Support non-ELF platforms.
203 if (!TM
.getELFWriterInfo())
206 RegisteredFunctionsMap::iterator I
= FnMap
.find(F
);
207 if (I
== FnMap
.end()) return;
208 UnregisterFunctionInternal(I
);
212 } // end namespace llvm