Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(56)

Delta Between Two Patch Sets: lib/ExecutionEngine/JIT/JITDebugRegisterer.cpp

Issue 91042: Implement LLVM JIT side of GDB JIT debugging interface (Closed) SVN Base: http://llvm.org/svn/llvm-project/llvm/trunk/
Left Patch Set: Created 5 months ago
Right Patch Set: Synced with TOT. Created 2 months, 2 weeks ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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"
Jeffrey Yasskin 2009/07/03 22:01:52 Nice.
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/OwningPtr.h" 23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/Support/MutexGuard.h"
23 #include "llvm/Support/raw_ostream.h" 25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/System/Mutex.h"
27 #include <string>
28 #include <vector>
24 29
25 namespace llvm { 30 namespace llvm {
26 31
27 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm) { 32 // This must be kept in sync with gdb/gdb/jit.h .
28 } 33 extern "C" {
29 34
30 std::string JITDebugRegisterer::MakeFilename(const Function *F, 35 // Debuggers puts a breakpoint in this function.
31 uint8_t *FnStart) { 36 void __attribute__((noinline)) __jit_debug_register_code() { }
32 std::string Filename; 37
33 raw_string_ostream O(Filename); 38 // We put information about the JITed function in this global, which the
34 O << "/tmp/llvm_function_" << FnStart << "_" << F->getNameStr() << ".o"; 39 // debugger reads. Make sure to specify the version statically, because the
Jeffrey Yasskin 2009/07/03 22:01:52 "/tmp" is probably wrong in general. llvm/System/P
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > "
35 O.flush(); 40 // debugger checks the version before we can set it during runtime.
36 return Filename; 41 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
37 } 42
38 43 }
39 void JITDebugRegisterer::MemCpySection(ELFSection &ES, 44
Jeffrey Yasskin 2009/07/03 22:01:52 I think this should be MemCpyToSection. Or maybe F
40 uint8_t* Start, uint8_t* End) { 45 namespace {
nlewycky 2009/07/04 01:27:07 Please be consistent with whether you put '* ' or
41 uintptr_t Size = (uintptr_t)End - (uintptr_t)Start; 46
Jeffrey Yasskin 2009/07/03 22:01:52 For future reference, End-Start would be of type p
42 std::vector<uint8_t> &Data = ES.getData(); 47 /// JITDebugLock - Used to serialize all code registration events, since they
43 Data.resize(Size); 48 /// modify global variables.
44 unsigned char *BuffBegin = &Data[0]; 49 sys::Mutex JITDebugLock;
Jeffrey Yasskin 2009/07/03 22:01:52 This would be undefined if Start==End.
nlewycky 2009/07/04 01:27:07 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > T
45 memcpy(BuffBegin, Start, Size); 50
Jeffrey Yasskin 2009/07/03 22:01:52 Instead of this dance, use ES.getData().assign(Sta
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > I
46 } 51 }
47 52
48 void JITDebugRegisterer::RegisterDebugInfo(const Function *F, 53 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { }
49 uint8_t* FnStart, uint8_t* FnEnd, 54
nlewycky 2009/07/04 01:27:07 'uint8_t *' again.
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/04 01:27:07, nlewycky wrote: > 'uint8_t
50 uint8_t* EhStart, uint8_t* EhEnd) { 55 JITDebugRegisterer::~JITDebugRegisterer() {
Jeffrey Yasskin 2009/07/03 22:01:52 This interface isn't as extensible as we'll eventu
51 // Pick the filename for the function. 56 // Free all ELF memory.
52 std::string Filename = MakeFilename(F, FnStart); 57 for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end();
53 58 I != E; ++I) {
54 // Stack allocate an empty module for the ELFWriter API. 59 // Call the private method that doesn't update the map so our iterator
Jeffrey Yasskin 2009/07/03 22:01:52 Please explain here why you can't just use the Fun
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > P
55 Module M("", getGlobalContext()); 60 // doesn't break.
56 61 UnregisterFunctionInternal(I);
57 // Open the ELF file. 62 }
58 std::string Error; 63 FnMap.clear();
59 raw_fd_ostream O(Filename.c_str(), true, Error); 64 }
Jeffrey Yasskin 2009/07/03 22:01:52 Please comment what "true" means. ", true /* Binar
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > P
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);
60 ELFWriter EW(O, TM); 76 ELFWriter EW(O, TM);
Jeffrey Yasskin 2009/07/03 22:01:52 Move this below the Error if.
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > M
61 if (Error != "") {
62 fprintf(stderr, "Error opening file '%s': %s",
63 Filename.c_str(), Error.c_str());
64 }
65 EW.doInitialization(M); 77 EW.doInitialization(M);
66 78
67 // Copy the binary into the .text section. This probably isn't critical, but 79 // Copy the binary into the .text section. This isn't necessary, but it's
Jeffrey Yasskin 2009/07/03 22:01:52 If it's not critical, I'd rather not do it, since
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > I
Jeffrey Yasskin 2009/07/08 00:40:45 On 2009/07/06 21:07:01, Reid Kleckner wrote: > On
68 // we put it there for completeness. 80 // useful to be able to disassemble the ELF by hand.
69 ELFSection &Text = EW.getTextSection(); 81 ELFSection &Text = EW.getTextSection((Function *)F);
70 Text.Addr = (uint64_t)FnStart; 82 Text.Addr = (uint64_t)I.FnStart;
71 MemCpySection(Text, FnStart, FnEnd); 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);
72 86
73 // Copy the exception handling call frame information into the .eh_frame 87 // Copy the exception handling call frame information into the .eh_frame
74 // section. This allows GDB to get a good stack trace. 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.
75 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS, 91 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS,
Jeffrey Yasskin 2009/07/03 22:01:52 Could you mention where you got these flags for ge
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/03 22:01:52, Jeffrey Yasskin wrote: > C
76 ELFSection::SHF_ALLOC); 92 ELFSection::SHF_ALLOC);
77 // Pointers in the DWARF call frame info are all relative to the EH frame 93 // Pointers in the DWARF EH info are all relative to the EH frame start,
78 // start, so the next line is actually critical for GDB. 94 // which is stored here.
79 EH.Addr = (uint64_t)EhStart; 95 EH.Addr = (uint64_t)I.EhStart;
80 MemCpySection(EH, EhStart, EhEnd); 96 // TODO: We could eliminate this copy if we somehow used a pointer/size pair
81 97 // instead of a vector.
82 // Add this single function to the symbol table, so GDB prints the name 98 EH.getData().assign(I.EhStart, I.EhEnd);
83 // instead of '???'. 99
84 ELFSym FnSym(F); 100 // Add this single function to the symbol table, so the debugger prints the
85 FnSym.setBind(ELFSym::STB_GLOBAL); // Just guessing about visibility. 101 // name instead of '???'. We give the symbol default global visibility.
nlewycky 2009/07/04 01:27:07 Global should always be fine here except on exotic
Reid Kleckner 2009/07/06 21:07:01 On 2009/07/04 01:27:07, nlewycky wrote: > Global s
86 FnSym.setType(ELFSym::STT_FUNC); 102 ELFSym *FnSym = ELFSym::getGV(F,
87 FnSym.SectionIdx = Text.SectionIdx; 103 ELFSym::STB_GLOBAL,
88 FnSym.Size = FnEnd - FnStart; 104 ELFSym::STT_FUNC,
89 FnSym.Value = 0; // Offset from start of section. 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.
90 EW.SymbolList.push_back(FnSym); 109 EW.SymbolList.push_back(FnSym);
91 110
92 EW.doFinalization(M); 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);
93 } 202 }
94 203
95 } // end namespace llvm 204 } // end namespace llvm
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497