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

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: Added hooks for GDB to automatically add the symbol file. Created 4 months, 4 weeks 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"
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
30 namespace llvm {
31
32 // This must be kept in sync with gdb/gdb/jit.h .
29 extern "C" { 33 extern "C" {
30 34
31 // Keep in sync with gdb/gdb/jit.h . 35 // Debuggers puts a breakpoint in this function.
32 struct jit_code { 36 void __attribute__((noinline)) __jit_debug_register_code() { }
33 uint8_t *text_start; 37
34 char symbol_file[100]; 38 // We put information about the JITed function in this global, which the
35 }; 39 // debugger reads. Make sure to specify the version statically, because the
36 40 // debugger checks the version before we can set it during runtime.
37 // GDB puts a breakpoint in this function. 41 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
38 void __attribute__((noinline)) __gdb_jit_register_code() { } 42
39 43 }
40 // We put information about the JITed function in this global, which GDB 44
41 // reads. 45 namespace {
42 struct jit_code __gdb_jit_code; 46
43 47 /// JITDebugLock - Used to serialize all code registration events, since they
44 } 48 /// modify global variables.
45 49 sys::Mutex JITDebugLock;
46 namespace llvm { 50
47 51 }
48 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) 52
49 : TM(tm), FnMap(), TmpDir(sys::Path::GetTemporaryDirectory()) { } 53 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { }
50 54
51 JITDebugRegisterer::~JITDebugRegisterer() { 55 JITDebugRegisterer::~JITDebugRegisterer() {
52 // Clean up all the temporary files. 56 // Free all ELF memory.
53 for (DenseMap<const Function*, sys::Path>::iterator I = FnMap.begin(), 57 for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end();
54 E = FnMap.end(); I != E; ++I) { 58 I != E; ++I) {
55 // 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
56 // doesn't break. 60 // doesn't break.
57 UnregisterFunctionInternal(I); 61 UnregisterFunctionInternal(I);
58 } 62 }
59 FnMap.clear(); 63 FnMap.clear();
60 TmpDir.eraseFromDisk(); 64 }
61 } 65
62 66 std::string JITDebugRegisterer::MakeELF(const Function *F, DebugInfo &I) {
63 sys::Path JITDebugRegisterer::MakeFilename(const Function *F, 67 // Stack allocate an empty module with an empty LLVMContext for the ELFWriter
64 uint8_t *FnStart) { 68 // API. We don't use the real module because then the ELFWriter would write
65 std::string Filename; 69 // out unnecessary GlobalValues during finalization.
66 raw_string_ostream O(Filename); 70 LLVMContext Context;
67 // Address first so it's easier to find the right filename when trying to add 71 Module M("", Context);
68 // the symbol file interactively. 72
69 O << "llvm_function_" << FnStart << "_" << F->getNameStr() << ".o"; 73 // Make a buffer for the ELF in memory.
70 O.flush(); 74 std::string Buffer;
71 sys::Path TmpPath(TmpDir); // Copy path. 75 raw_string_ostream O(Buffer);
72 TmpPath.appendComponent(Filename);
73 return TmpPath;
74 }
75
76 void JITDebugRegisterer::RegisterFunction(const Function *F, DebugInfo &I) {
77 // Pick the filename for the function.
78 sys::Path Filename = MakeFilename(F, I.FnStart);
79
80 // Add a mapping from F to Filename, so we can delete this function later.
81 FnMap[F] = Filename;
82
83 // Stack allocate an empty module for the ELFWriter API. We don't use the
84 // real module because then the ELFWriter would write out unnecessary
85 // GlobalValues during finalization.
86 Module M("", getGlobalContext());
87
88 // Open the ELF file.
89 // TODO: Keep the ELF in memory.
90 std::string Error;
91 raw_fd_ostream O(Filename.c_str(), /*Binary=*/ true, Error);
92 if (Error != "") {
93 fprintf(stderr, "Error opening file '%s': %s",
94 Filename.c_str(), Error.c_str());
95 abort();
96 }
97 ELFWriter EW(O, TM); 76 ELFWriter EW(O, TM);
98 EW.doInitialization(M); 77 EW.doInitialization(M);
99 78
100 // 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
101 // useful to be able to disassemble the ELF by hand. 80 // useful to be able to disassemble the ELF by hand.
102 ELFSection &Text = EW.getTextSection(); 81 ELFSection &Text = EW.getTextSection((Function *)F);
103 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.
104 Text.getData().assign(I.FnStart, I.FnEnd); 85 Text.getData().assign(I.FnStart, I.FnEnd);
105 86
106 // Copy the exception handling call frame information into the .eh_frame 87 // Copy the exception handling call frame information into the .eh_frame
107 // 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
108 // 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
109 // into memory at runtime. 90 // into memory at runtime.
110 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS, 91 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS,
111 ELFSection::SHF_ALLOC); 92 ELFSection::SHF_ALLOC);
112 // 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,
113 // which is stored here. 94 // which is stored here.
114 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.
115 EH.getData().assign(I.EhStart, I.EhEnd); 98 EH.getData().assign(I.EhStart, I.EhEnd);
116 99
117 // 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
118 // instead of '???'. 101 // name instead of '???'. We give the symbol default global visibility.
119 ELFSym FnSym(F); 102 ELFSym *FnSym = ELFSym::getGV(F,
120 FnSym.setBind(ELFSym::STB_GLOBAL); // Make it globally visible. 103 ELFSym::STB_GLOBAL,
121 FnSym.setType(ELFSym::STT_FUNC); 104 ELFSym::STT_FUNC,
122 FnSym.SectionIdx = Text.SectionIdx; 105 ELFSym::STV_DEFAULT);
123 FnSym.Size = I.FnEnd - I.FnStart; 106 FnSym->SectionIdx = Text.SectionIdx;
124 FnSym.Value = 0; // Offset from start of section. 107 FnSym->Size = I.FnEnd - I.FnStart;
108 FnSym->Value = 0; // Offset from start of section.
125 EW.SymbolList.push_back(FnSym); 109 EW.SymbolList.push_back(FnSym);
126 110
127 EW.doFinalization(M); 111 EW.doFinalization(M);
128 O.close(); // This must happen before GDB can read the file. 112 O.flush();
129 113
130 // TODO: Make this thread safe by avoiding globals. 114 // When trying to debug this code, it's awfully helpful to write the object
131 __gdb_jit_code.text_start = I.FnStart; 115 // file to disk.
132 strncpy(__gdb_jit_code.symbol_file, Filename.c_str(), 100); 116 std::string Filename;
133 __gdb_jit_code.symbol_file[99] = 0; // Null-terminate for safety. 117 raw_string_ostream O2(Filename);
134 __gdb_jit_register_code(); 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 }
135 } 159 }
136 160
137 void JITDebugRegisterer::UnregisterFunctionInternal( 161 void JITDebugRegisterer::UnregisterFunctionInternal(
138 DenseMap<const Function*, sys::Path>::iterator I) { 162 RegisteredFunctionsMap::iterator I) {
139 // It would be nice if we could actually unregister the debug info, but for 163 jit_code_entry *JITCodeEntry = I->second.second;
140 // now there is no way to undo an add-symbol-file. However, the most 164
141 // recently loaded symbols take precedence, so if we reuse memory for new 165 // Acquire the lock and do the unregistration.
142 // machine functions everything should just work. 166 {
143 167 MutexGuard locked(JITDebugLock);
144 // Delete the ELF file. We don't catch any errors since deleting this temp 168 __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN;
145 // file isn't critical. 169
146 sys::Path &P = I->second; 170 // Remove the jit_code_entry from the linked list.
147 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();
148 } 191 }
149 192
150 void JITDebugRegisterer::UnregisterFunction(const Function *F) { 193 void JITDebugRegisterer::UnregisterFunction(const Function *F) {
151 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);
152 if (I == FnMap.end()) return; 199 if (I == FnMap.end()) return;
153 UnregisterFunctionInternal(I); 200 UnregisterFunctionInternal(I);
154 FnMap.erase(I); 201 FnMap.erase(I);
155 } 202 }
156 203
157 } // end namespace llvm 204 } // end namespace llvm
LEFTRIGHT

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