This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdebuginfo.cpp
251 lines (229 loc) · 11.1 KB
/
debuginfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// --- storing and accessing source location metadata ---
struct FuncInfo{
const Function* func;
size_t lengthAdr;
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
PRUNTIME_FUNCTION fnentry;
#endif
std::vector<JITEvent_EmittedFunctionDetails::LineStart> lines;
};
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
#include <dbghelp.h>
extern "C" EXCEPTION_DISPOSITION _seh_exception_handler(PEXCEPTION_RECORD ExceptionRecord,void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext);
extern "C" volatile int jl_in_stackwalk;
#endif
struct revcomp {
bool operator() (const size_t& lhs, const size_t& rhs) const
{return lhs>rhs;}
};
class JuliaJITEventListener: public JITEventListener
{
std::map<size_t, FuncInfo, revcomp> info;
public:
JuliaJITEventListener(){}
virtual ~JuliaJITEventListener() {}
virtual void NotifyFunctionEmitted(const Function &F, void *Code,
size_t Size, const EmittedFunctionDetails &Details)
{
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
assert(!jl_in_stackwalk);
jl_in_stackwalk = 1;
uintptr_t catchjmp = (uintptr_t)Code+Size;
*(uint8_t*)(catchjmp+0) = 0x48;
*(uint8_t*)(catchjmp+1) = 0xb8; // mov RAX, QWORD PTR [...]
*(uint64_t*)(catchjmp+2) = (uint64_t)&_seh_exception_handler;
*(uint8_t*)(catchjmp+10) = 0xff;
*(uint8_t*)(catchjmp+11) = 0xe0; // jmp RAX
PRUNTIME_FUNCTION tbl = (PRUNTIME_FUNCTION)((catchjmp+12+3)&~(uintptr_t)3);
uint8_t *UnwindData = (uint8_t*)((((uintptr_t)&tbl[1])+3)&~(uintptr_t)3);
RUNTIME_FUNCTION fn = {0,(DWORD)Size+13,(DWORD)(intptr_t)(UnwindData-(uint8_t*)Code)};
tbl[0] = fn;
UnwindData[0] = 0x09; // version info, UNW_FLAG_EHANDLER
UnwindData[1] = 4; // size of prolog (bytes)
UnwindData[2] = 2; // count of unwind codes (slots)
UnwindData[3] = 0x05; // frame register (rbp) = rsp
UnwindData[4] = 4; // second instruction
UnwindData[5] = 0x03; // mov RBP, RSP
UnwindData[6] = 1; // first instruction
UnwindData[7] = 0x50; // push RBP
*(DWORD*)&UnwindData[8] = (DWORD)(catchjmp-(intptr_t)Code);
DWORD mod_size = (DWORD)(size_t)(&UnwindData[8]-(uint8_t*)Code);
if (!SymLoadModuleEx(GetCurrentProcess(), NULL, NULL, NULL, (DWORD64)Code, mod_size, NULL, SLMFLAG_VIRTUAL)) {
JL_PRINTF(JL_STDERR, "WARNING: failed to insert function info for backtrace\n");
}
else {
if (!SymAddSymbol(GetCurrentProcess(), (ULONG64)Code, F.getName().data(), (DWORD64)Code, mod_size, 0)) {
JL_PRINTF(JL_STDERR, "WARNING: failed to insert function name into debug info\n");
}
if (!RtlAddFunctionTable(tbl,1,(DWORD64)Code)) {
JL_PRINTF(JL_STDERR, "WARNING: failed to insert function stack unwind info\n");
}
}
jl_in_stackwalk = 0;
FuncInfo tmp = {&F, Size, tbl, Details.LineStarts};
#else
FuncInfo tmp = {&F, Size, Details.LineStarts};
#endif
if (tmp.lines.size() != 0) info[(size_t)(Code)] = tmp;
}
std::map<size_t, FuncInfo, revcomp>& getMap()
{
return info;
}
};
JuliaJITEventListener *jl_jit_events;
extern "C" void jl_getFunctionInfo(const char **name, int *line, const char **filename,size_t pointer);
void jl_getFunctionInfo(const char **name, int *line, const char **filename, size_t pointer)
{
std::map<size_t, FuncInfo, revcomp> &info = jl_jit_events->getMap();
*name = NULL;
*line = -1;
*filename = "no file";
std::map<size_t, FuncInfo, revcomp>::iterator it = info.lower_bound(pointer);
if (it != info.end() && (size_t)(*it).first + (*it).second.lengthAdr >= pointer) {
// commenting these lines out skips functions that don't
// have explicit debug info. this is useful for hiding
// the jlcall wrapper functions we generate.
#if LLVM_VERSION_MAJOR == 3
#if LLVM_VERSION_MINOR == 0
//*name = &(*(*it).second.func).getNameStr()[0];
#elif LLVM_VERSION_MINOR >= 1
//*name = (((*(*it).second.func).getName()).data());
#endif
#endif
std::vector<JITEvent_EmittedFunctionDetails::LineStart>::iterator vit = (*it).second.lines.begin();
JITEvent_EmittedFunctionDetails::LineStart prev = *vit;
DISubprogram debugscope =
DISubprogram(prev.Loc.getScope((*it).second.func->getContext()));
*filename = debugscope.getFilename().data();
// the DISubprogram has the un-mangled name, so use that if
// available.
*name = debugscope.getName().data();
vit++;
while (vit != (*it).second.lines.end()) {
if (pointer <= (*vit).Address) {
*line = prev.Loc.getLine();
break;
}
prev = *vit;
vit++;
}
if (*line == -1) {
*line = prev.Loc.getLine();
}
}
}
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_)
extern "C" void* CALLBACK jl_getUnwindInfo(HANDLE hProcess, ULONG64 AddrBase, ULONG64 UserContext);
void* CALLBACK jl_getUnwindInfo(HANDLE hProcess, ULONG64 AddrBase, ULONG64 UserContext)
{
std::map<size_t, FuncInfo, revcomp> &info = jl_jit_events->getMap();
std::map<size_t, FuncInfo, revcomp>::iterator it = info.lower_bound(AddrBase);
if (it != info.end() && (size_t)(*it).first + (*it).second.lengthAdr >= AddrBase) {
return (void*)(*it).second.fnentry;
}
return NULL;
}
class JITMemoryManagerWin : public JITMemoryManager {
private:
JITMemoryManager *JMM;
public:
JITMemoryManagerWin() : JITMemoryManager() {
JMM = JITMemoryManager::CreateDefaultMemManager();
}
virtual void setMemoryWritable() { return JMM->setMemoryWritable(); }
virtual void setMemoryExecutable() { return JMM->setMemoryExecutable(); }
virtual void setPoisonMemory(bool poison) { return JMM->setPoisonMemory(poison); }
virtual void AllocateGOT() { JMM->AllocateGOT(); HasGOT = true; }
virtual uint8_t *getGOTBase() const { return JMM->getGOTBase(); }
virtual uint8_t *startFunctionBody(const Function *F,
uintptr_t &ActualSize) { ActualSize += 48; uint8_t *ret = JMM->startFunctionBody(F,ActualSize); ActualSize -= 48; return ret; }
virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
unsigned Alignment) { return JMM->allocateStub(F,StubSize,Alignment); }
virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
uint8_t *FunctionEnd) { return JMM->endFunctionBody(F,FunctionStart,FunctionEnd+48); }
virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { return JMM->allocateSpace(Size,Alignment); }
virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) { return JMM->allocateGlobal(Size,Alignment); }
virtual void deallocateFunctionBody(void *Body) { return JMM->deallocateFunctionBody(Body); }
virtual uint8_t* startExceptionTable(const Function* F,
uintptr_t &ActualSize) { return JMM->startExceptionTable(F,ActualSize); }
virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
uint8_t *TableEnd, uint8_t* FrameRegister) { return JMM->endExceptionTable(F,TableStart,TableEnd,FrameRegister); }
virtual void deallocateExceptionTable(void *ET) { return JMM->deallocateExceptionTable(ET); }
virtual bool CheckInvariants(std::string &str) { return JMM->CheckInvariants(str); }
virtual size_t GetDefaultCodeSlabSize() { return JMM->GetDefaultCodeSlabSize(); }
virtual size_t GetDefaultDataSlabSize() { return JMM->GetDefaultDataSlabSize(); }
virtual size_t GetDefaultStubSlabSize() { return JMM->GetDefaultStubSlabSize(); }
virtual unsigned GetNumCodeSlabs() { return JMM->GetNumCodeSlabs(); }
virtual unsigned GetNumDataSlabs() { return JMM->GetNumDataSlabs(); }
virtual unsigned GetNumStubSlabs() { return JMM->GetNumStubSlabs(); }
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID) { return JMM->allocateCodeSection(Size,Alignment,SectionID); }
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, bool IsReadOnly) { return JMM->allocateDataSection(Size,Alignment,SectionID,IsReadOnly); }
virtual void *getPointerToNamedFunction(const std::string &Name,
bool AbortOnFailure = true) { return JMM->getPointerToNamedFunction(Name,AbortOnFailure); }
virtual bool applyPermissions(std::string *ErrMsg = 0) { return JMM->applyPermissions(ErrMsg); }
virtual void registerEHFrames(StringRef SectionData) { return JMM->registerEHFrames(SectionData); }
};
#endif
// Code coverage
typedef std::map<std::string,std::vector<GlobalVariable*> > coveragedata_t;
static coveragedata_t coverageData;
static void coverageVisitLine(std::string filename, int line)
{
if (filename == "" || filename == "none" || filename == "no file")
return;
coveragedata_t::iterator it = coverageData.find(filename);
if (it == coverageData.end()) {
coverageData[filename] = std::vector<GlobalVariable*>(0);
}
std::vector<GlobalVariable*> &vec = coverageData[filename];
if (vec.size() <= (size_t)line)
vec.resize(line+1, NULL);
if (vec[line] == NULL)
vec[line] = new GlobalVariable(*jl_Module, T_int64, false, GlobalVariable::InternalLinkage,
ConstantInt::get(T_int64,0), "lcnt");
GlobalVariable *v = vec[line];
builder.CreateStore(builder.CreateAdd(builder.CreateLoad(v),
ConstantInt::get(T_int64,1)),
v);
}
extern "C" void jl_write_coverage_data(void)
{
coveragedata_t::iterator it = coverageData.begin();
for (; it != coverageData.end(); it++) {
std::string filename = (*it).first;
std::string outfile = filename + ".cov";
std::vector<GlobalVariable*> &counts = (*it).second;
if (counts.size() > 1) {
std::ifstream inf(filename.c_str());
if (inf.is_open()) {
std::ofstream outf(outfile.c_str(), std::ofstream::trunc | std::ofstream::out);
char line[1024];
int l = 1;
while (!inf.eof()) {
inf.getline(line, sizeof(line));
int count = -1;
if ((size_t)l < counts.size()) {
GlobalVariable *gv = counts[l];
if (gv) {
int *p = (int*)jl_ExecutionEngine->getPointerToGlobal(gv);
count = *p;
}
}
outf.width(9);
if (count == -1)
outf<<'-';
else
outf<<count;
outf.width(0);
outf<<" "<<line<<std::endl;
l++;
}
outf.close();
inf.close();
}
}
}
}