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

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: Addressed reviewer's comments. 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"
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"
24 #include "llvm/System/Path.h" 26 #include "llvm/System/Mutex.h"
25 #include <map>
26 #include <string> 27 #include <string>
27 #include <vector> 28 #include <vector>
28 29
29 namespace llvm { 30 namespace llvm {
30 31
31 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { 32 // This must be kept in sync with gdb/gdb/jit.h .
32 TmpDir = sys::Path::GetTemporaryDirectory(); 33 extern "C" {
Jeffrey Yasskin 2009/07/08 00:40:45 If you initialize TmpDir in the initializer list,
Reid Kleckner 2009/07/08 01:35:05 On 2009/07/08 00:40:45, Jeffrey Yasskin wrote: > I
33 } 34
35 // Debuggers puts a breakpoint in this function.
36 void __attribute__((noinline)) __jit_debug_register_code() { }
37
38 // We put information about the JITed function in this global, which the
39 // debugger reads. Make sure to specify the version statically, because the
40 // debugger checks the version before we can set it during runtime.
41 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
42
43 }
44
45 namespace {
46
47 /// JITDebugLock - Used to serialize all code registration events, since they
48 /// modify global variables.
49 sys::Mutex JITDebugLock;
50
51 }
52
53 JITDebugRegisterer::JITDebugRegisterer(TargetMachine &tm) : TM(tm), FnMap() { }
34 54
35 JITDebugRegisterer::~JITDebugRegisterer() { 55 JITDebugRegisterer::~JITDebugRegisterer() {
36 // Clean up all the temporary files. 56 // Free all ELF memory.
37 for (std::map<const Function*, sys::Path>::iterator I = FnMap.begin(), 57 for (RegisteredFunctionsMap::iterator I = FnMap.begin(), E = FnMap.end();
Jeffrey Yasskin 2009/07/08 00:40:45 Almost never use std::map. DenseMap is better.
Reid Kleckner 2009/07/08 01:35:05 On 2009/07/08 00:40:45, Jeffrey Yasskin wrote: > A
38 E = FnMap.end(); I != E; ++I) { 58 I != E; ++I) {
39 // 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
40 // doesn't break. 60 // doesn't break.
41 _UnregisterFunction(I->first); 61 UnregisterFunctionInternal(I);
Jeffrey Yasskin 2009/07/08 00:40:45 In LLVM, don't start names with '_'+Capital. It st
Reid Kleckner 2009/07/08 01:35:05 On 2009/07/08 00:40:45, Jeffrey Yasskin wrote: > I
42 } 62 }
43 FnMap.clear(); 63 FnMap.clear();
44 TmpDir.eraseFromDisk(); 64 }
45 } 65
46 66 std::string JITDebugRegisterer::MakeELF(const Function *F, DebugInfo &I) {
47 sys::Path JITDebugRegisterer::MakeFilename(const Function *F, 67 // Stack allocate an empty module with an empty LLVMContext for the ELFWriter
48 uint8_t *FnStart) { 68 // API. We don't use the real module because then the ELFWriter would write
49 std::string Filename; 69 // out unnecessary GlobalValues during finalization.
50 raw_string_ostream O(Filename); 70 LLVMContext Context;
51 // Address first so it's easier to find the right filename when trying to add 71 Module M("", Context);
52 // the symbol file interactively. 72
53 O << "llvm_function_" << FnStart << "_" << F->getNameStr() << ".o"; 73 // Make a buffer for the ELF in memory.
54 O.flush(); 74 std::string Buffer;
55 sys::Path TmpPath(TmpDir); // Copy path. 75 raw_string_ostream O(Buffer);
56 TmpPath.appendComponent(Filename);
57 return TmpPath;
58 }
59
60 void JITDebugRegisterer::RegisterFunction(const Function *F, DebugInfo &I) {
61 // Pick the filename for the function.
62 sys::Path Filename = MakeFilename(F, I.FnStart);
63
64 // Add a mapping from F to Filename, so we can delete this function later.
65 FnMap[F] = Filename;
66
67 // Stack allocate an empty module for the ELFWriter API. We don't use the
68 // real module because then the ELFWriter would write out unnecessary
69 // GlobalValues during finalization.
70 Module M("", getGlobalContext());
71
72 // Open the ELF file.
73 // TODO: Keep the ELF in memory.
74 std::string Error;
75 raw_fd_ostream O(Filename.c_str(), /*Binary=*/ true, Error);
76 if (Error != "") {
77 fprintf(stderr, "Error opening file '%s': %s",
78 Filename.c_str(), Error.c_str());
79 abort();
80 }
81 ELFWriter EW(O, TM); 76 ELFWriter EW(O, TM);
82 EW.doInitialization(M); 77 EW.doInitialization(M);
83 78
84 // 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
85 // useful to be able to disassemble the ELF by hand. 80 // useful to be able to disassemble the ELF by hand.
86 ELFSection &Text = EW.getTextSection(); 81 ELFSection &Text = EW.getTextSection((Function *)F);
87 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.
88 Text.getData().assign(I.FnStart, I.FnEnd); 85 Text.getData().assign(I.FnStart, I.FnEnd);
89 86
90 // Copy the exception handling call frame information into the .eh_frame 87 // Copy the exception handling call frame information into the .eh_frame
91 // 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
92 // 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
93 // into memory at runtime. 90 // into memory at runtime.
94 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS, 91 ELFSection &EH = EW.getSection(".eh_frame", ELFSection::SHT_PROGBITS,
95 ELFSection::SHF_ALLOC); 92 ELFSection::SHF_ALLOC);
96 // 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,
97 // which is stored here. 94 // which is stored here.
98 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.
99 EH.getData().assign(I.EhStart, I.EhEnd); 98 EH.getData().assign(I.EhStart, I.EhEnd);
100 99
101 // 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
102 // instead of '???'. 101 // name instead of '???'. We give the symbol default global visibility.
103 ELFSym FnSym(F); 102 ELFSym *FnSym = ELFSym::getGV(F,
104 FnSym.setBind(ELFSym::STB_GLOBAL); // Make it globally visible. 103 ELFSym::STB_GLOBAL,
105 FnSym.setType(ELFSym::STT_FUNC); 104 ELFSym::STT_FUNC,
106 FnSym.SectionIdx = Text.SectionIdx; 105 ELFSym::STV_DEFAULT);
107 FnSym.Size = I.FnEnd - I.FnStart; 106 FnSym->SectionIdx = Text.SectionIdx;
108 FnSym.Value = 0; // Offset from start of section. 107 FnSym->Size = I.FnEnd - I.FnStart;
108 FnSym->Value = 0; // Offset from start of section.
109 EW.SymbolList.push_back(FnSym); 109 EW.SymbolList.push_back(FnSym);
110 110
111 EW.doFinalization(M); 111 EW.doFinalization(M);
112 112 O.flush();
113 // TODO: Automatically register the ELF with GDB. 113
114 } 114 // When trying to debug this code, it's awfully helpful to write the object
115 115 // file to disk.
116 void JITDebugRegisterer::_UnregisterFunction(const Function *F) { 116 std::string Filename;
117 // It would be nice if we could actually unregister the debug info, but for 117 raw_string_ostream O2(Filename);
118 // now there is no way to undo an add-symbol-file. However, more the most 118 O2 << "/tmp/llvm_function_" << I.FnStart << "_" << F->getNameStr() << ".o";
Jeffrey Yasskin 2009/07/08 00:40:45 "However, more the most"?
Reid Kleckner 2009/07/08 01:35:05 On 2009/07/08 00:40:45, Jeffrey Yasskin wrote: > "
119 // recently loaded symbols take precedence, so if we reuse memory for new 119 O2.flush();
120 // machine functions everything should just work. 120 std::string Errors;
121 121 raw_fd_ostream O3(Filename.c_str(), Errors);
122 // Delete the ELF file. We don't catch any errors since deleting this temp 122 O3 << Buffer;
123 // file isn't critical. 123 O3.close();
124 sys::Path &P = FnMap[F]; 124
Jeffrey Yasskin 2009/07/08 00:40:45 Try to avoid using operator[] on maps, since it in
Reid Kleckner 2009/07/08 01:35:05 On 2009/07/08 00:40:45, Jeffrey Yasskin wrote: > T
125 P.eraseFromDisk(); 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();
126 } 191 }
127 192
128 void JITDebugRegisterer::UnregisterFunction(const Function *F) { 193 void JITDebugRegisterer::UnregisterFunction(const Function *F) {
129 _UnregisterFunction(F); 194 // TODO: Support non-ELF platforms.
130 FnMap.erase(F); 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);
131 } 202 }
132 203
133 } // end namespace llvm 204 } // end namespace llvm
LEFTRIGHT

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