-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLLVMReactorDebugInfo.cpp
557 lines (480 loc) · 17.9 KB
/
LLVMReactorDebugInfo.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "LLVMReactorDebugInfo.hpp"
#ifdef ENABLE_RR_DEBUG_INFO
# include "LLVMReactor.hpp"
# include "Reactor.hpp"
# include "boost/stacktrace.hpp"
# include "llvm/Demangle/Demangle.h"
# include "llvm/ExecutionEngine/JITEventListener.h"
# include "llvm/IR/DIBuilder.h"
# include "llvm/IR/IRBuilder.h"
# include "llvm/IR/Intrinsics.h"
# include <cctype>
# include <fstream>
# include <mutex>
# include <regex>
# include <sstream>
# include <string>
# if 0
# define LOG(msg, ...) printf(msg "\n", ##__VA_ARGS__)
# else
# define LOG(msg, ...)
# endif
namespace {
std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path)
{
return llvm::StringRef(path).rsplit('/');
}
// Note: createGDBRegistrationListener() returns a pointer to a singleton.
// Nothing is actually created.
auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex
std::mutex jitEventListenerMutex;
} // namespace
namespace rr {
DebugInfo::DebugInfo(
llvm::IRBuilder<> *builder,
llvm::LLVMContext *context,
llvm::Module *module,
llvm::Function *function)
: builder(builder)
, context(context)
, module(module)
, function(function)
{
using namespace ::llvm;
auto location = getCallerLocation();
auto fileAndDir = splitPath(location.function.file.c_str());
diBuilder.reset(new llvm::DIBuilder(*module));
diCU = diBuilder->createCompileUnit(
llvm::dwarf::DW_LANG_C,
diBuilder->createFile(fileAndDir.first, fileAndDir.second),
"Reactor",
0, "", 0);
registerBasicTypes();
SmallVector<Metadata *, 8> EltTys;
auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(EltTys));
auto file = getOrCreateFile(location.function.file.c_str());
auto sp = diBuilder->createFunction(
file, // scope
"ReactorFunction", // function name
"ReactorFunction", // linkage
file, // file
location.line, // line
funcTy, // type
false, // internal linkage
true, // definition
location.line, // scope line
DINode::FlagPrototyped, // flags
false // is optimized
);
diSubprogram = sp;
function->setSubprogram(sp);
diRootLocation = DILocation::get(*context, location.line, 0, sp);
builder->SetCurrentDebugLocation(diRootLocation);
}
DebugInfo::~DebugInfo() = default;
void DebugInfo::Finalize()
{
while(diScope.size() > 0)
{
emitPending(diScope.back(), builder);
diScope.pop_back();
}
diBuilder->finalize();
}
void DebugInfo::EmitLocation()
{
auto const &backtrace = getCallerBacktrace();
syncScope(backtrace);
builder->SetCurrentDebugLocation(getLocation(backtrace, backtrace.size() - 1));
# ifdef ENABLE_RR_EMIT_PRINT_LOCATION
static Location lastLocation;
if(backtrace.size() == 0)
{
return;
}
Location currLocation = backtrace[backtrace.size() - 1];
if(currLocation != lastLocation)
{
rr::Print("rr> {0} [{1}:{2}]\n", currLocation.function.name.c_str(), currLocation.function.file.c_str(), currLocation.line);
lastLocation = std::move(currLocation);
}
# endif // ENABLE_RR_EMIT_PRINT_LOCATION
}
void DebugInfo::Flush()
{
emitPending(diScope.back(), builder);
}
void DebugInfo::syncScope(Backtrace const &backtrace)
{
auto shrink = [this](size_t newsize) {
while(diScope.size() > newsize)
{
auto &scope = diScope.back();
LOG("- STACK(%d): di: %p, location: %s:%d",
int(diScope.size() - 1), scope.di,
scope.location.function.file.c_str(),
int(scope.location.line));
emitPending(scope, builder);
diScope.pop_back();
}
};
if(backtrace.size() < diScope.size())
{
shrink(backtrace.size());
}
for(size_t i = 0; i < diScope.size(); i++)
{
auto &scope = diScope[i];
auto const &oldLocation = scope.location;
auto const &newLocation = backtrace[i];
if(oldLocation.function != newLocation.function)
{
LOG(" STACK(%d): Changed function %s -> %s", int(i),
oldLocation.function.name.c_str(), newLocation.function.name.c_str());
shrink(i);
break;
}
if(oldLocation.line > newLocation.line)
{
// Create a new di block to shadow all the variables in the loop.
auto file = getOrCreateFile(newLocation.function.file.c_str());
auto di = diBuilder->createLexicalBlock(scope.di, file, newLocation.line, 0);
LOG(" STACK(%d): Jumped backwards %d -> %d. di: %p -> %p", int(i),
oldLocation.line, newLocation.line, scope.di, di);
emitPending(scope, builder);
scope = { newLocation, di };
shrink(i + 1);
break;
}
scope.location = newLocation;
}
while(backtrace.size() > diScope.size())
{
auto i = diScope.size();
auto location = backtrace[i];
auto file = getOrCreateFile(location.function.file.c_str());
auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray({}));
char buf[1024];
size_t size = sizeof(buf);
int status = 0;
llvm::itaniumDemangle(location.function.name.c_str(), buf, &size, &status);
auto name = "jit!" + (status == 0 ? std::string(buf) : location.function.name);
auto func = diBuilder->createFunction(
file, // scope
name, // function name
"", // linkage
file, // file
location.line, // line
funcTy, // type
false, // internal linkage
true, // definition
location.line, // scope line
llvm::DINode::FlagPrototyped, // flags
false // is optimized
);
diScope.push_back({ location, func });
LOG("+ STACK(%d): di: %p, location: %s:%d", int(i), di,
location.function.file.c_str(), int(location.line));
}
}
llvm::DILocation *DebugInfo::getLocation(const Backtrace &backtrace, size_t i)
{
if(backtrace.size() == 0) { return nullptr; }
assert(backtrace.size() == diScope.size());
return llvm::DILocation::get(
*context,
backtrace[i].line,
0,
diScope[i].di,
i > 0 ? getLocation(backtrace, i - 1) : diRootLocation);
}
void DebugInfo::EmitVariable(Value *variable)
{
auto const &backtrace = getCallerBacktrace();
syncScope(backtrace);
for(int i = backtrace.size() - 1; i >= 0; i--)
{
auto const &location = backtrace[i];
auto tokens = getOrParseFileTokens(location.function.file.c_str());
auto tokIt = tokens->find(location.line);
if(tokIt == tokens->end())
{
break;
}
auto token = tokIt->second;
auto name = token.identifier;
if(token.kind == Token::Return)
{
// This is a:
//
// return <expr>;
//
// Emit this expression as two variables -
// Once as a synthetic 'return_value' variable at this scope.
// Again by bubbling the expression value up the callstack as
// Return Value Optimizations (RVOs) are likely to carry across
// the value to a local without calling a constructor in
// statements like:
//
// auto val = foo();
//
name = "return_value";
}
auto &scope = diScope[i];
if(scope.pending.location != location)
{
emitPending(scope, builder);
}
auto value = V(variable);
auto block = builder->GetInsertBlock();
auto insertAfter = block->size() > 0 ? &block->back() : nullptr;
while(insertAfter != nullptr && insertAfter->isTerminator())
{
insertAfter = insertAfter->getPrevNode();
}
scope.pending = Pending{};
scope.pending.name = name;
scope.pending.location = location;
scope.pending.diLocation = getLocation(backtrace, i);
scope.pending.value = value;
scope.pending.block = block;
scope.pending.insertAfter = insertAfter;
scope.pending.scope = scope.di;
if(token.kind == Token::Return)
{
// Insert a noop instruction so the debugger can inspect the
// return value before the function scope closes.
scope.pending.addNopOnNextLine = true;
}
else
{
break;
}
}
}
void DebugInfo::emitPending(Scope &scope, IRBuilder *builder)
{
auto const &pending = scope.pending;
if(pending.value == nullptr)
{
return;
}
if(!scope.symbols.emplace(pending.name).second)
{
return;
}
bool isAlloca = llvm::isa<llvm::AllocaInst>(pending.value);
LOG(" EMIT(%s): di: %p, location: %s:%d, isAlloca: %s", pending.name.c_str(), scope.di,
pending.location.function.file.c_str(), pending.location.line, isAlloca ? "true" : "false");
auto value = pending.value;
IRBuilder::InsertPointGuard guard(*builder);
if(pending.insertAfter != nullptr)
{
builder->SetInsertPoint(pending.block, ++pending.insertAfter->getIterator());
}
else
{
builder->SetInsertPoint(pending.block);
}
builder->SetCurrentDebugLocation(pending.diLocation);
if(!isAlloca)
{
// While insertDbgValueIntrinsic should be enough to declare a
// variable with no storage, variables of RValues can share the same
// llvm::Value, and only one can be named. Take for example:
//
// Int a = 42;
// RValue<Int> b = a;
// RValue<Int> c = b;
//
// To handle this, always promote named RValues to an alloca.
llvm::BasicBlock &entryBlock = function->getEntryBlock();
auto alloca = new llvm::AllocaInst(value->getType(), 0, pending.name);
entryBlock.getInstList().push_front(alloca);
builder->CreateStore(value, alloca);
value = alloca;
}
value->setName(pending.name);
auto diFile = getOrCreateFile(pending.location.function.file.c_str());
auto diType = getOrCreateType(value->getType()->getPointerElementType());
auto diVar = diBuilder->createAutoVariable(scope.di, pending.name, diFile, pending.location.line, diType);
auto di = diBuilder->insertDeclare(value, diVar, diBuilder->createExpression(), pending.diLocation, pending.block);
if(pending.insertAfter != nullptr) { di->moveAfter(pending.insertAfter); }
if(pending.addNopOnNextLine)
{
builder->SetCurrentDebugLocation(llvm::DILocation::get(
*context,
pending.diLocation->getLine() + 1,
0,
pending.diLocation->getScope(),
pending.diLocation->getInlinedAt()));
Nop();
}
scope.pending = Pending{};
}
void DebugInfo::NotifyObjectEmitted(const llvm::object::ObjectFile &Obj, const llvm::LoadedObjectInfo &L)
{
std::unique_lock<std::mutex> lock(jitEventListenerMutex);
jitEventListener->NotifyObjectEmitted(Obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo &>(L));
}
void DebugInfo::NotifyFreeingObject(const llvm::object::ObjectFile &Obj)
{
std::unique_lock<std::mutex> lock(jitEventListenerMutex);
jitEventListener->NotifyFreeingObject(Obj);
}
void DebugInfo::registerBasicTypes()
{
using namespace rr;
using namespace llvm;
auto vec4 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 4));
auto vec8 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 8));
auto vec16 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 16));
diTypes.emplace(T(Bool::getType()), diBuilder->createBasicType("Bool", sizeof(bool), dwarf::DW_ATE_boolean));
diTypes.emplace(T(Byte::getType()), diBuilder->createBasicType("Byte", 8, dwarf::DW_ATE_unsigned_char));
diTypes.emplace(T(SByte::getType()), diBuilder->createBasicType("SByte", 8, dwarf::DW_ATE_signed_char));
diTypes.emplace(T(Short::getType()), diBuilder->createBasicType("Short", 16, dwarf::DW_ATE_signed));
diTypes.emplace(T(UShort::getType()), diBuilder->createBasicType("UShort", 16, dwarf::DW_ATE_unsigned));
diTypes.emplace(T(Int::getType()), diBuilder->createBasicType("Int", 32, dwarf::DW_ATE_signed));
diTypes.emplace(T(UInt::getType()), diBuilder->createBasicType("UInt", 32, dwarf::DW_ATE_unsigned));
diTypes.emplace(T(Long::getType()), diBuilder->createBasicType("Long", 64, dwarf::DW_ATE_signed));
diTypes.emplace(T(Half::getType()), diBuilder->createBasicType("Half", 16, dwarf::DW_ATE_float));
diTypes.emplace(T(Float::getType()), diBuilder->createBasicType("Float", 32, dwarf::DW_ATE_float));
diTypes.emplace(T(Byte4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::getType())], { vec16 }));
diTypes.emplace(T(SByte4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::getType())], { vec16 }));
diTypes.emplace(T(Byte8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::getType())], { vec16 }));
diTypes.emplace(T(SByte8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::getType())], { vec16 }));
diTypes.emplace(T(Byte16::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::getType())], { vec16 }));
diTypes.emplace(T(SByte16::getType()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::getType())], { vec16 }));
diTypes.emplace(T(Short2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Short::getType())], { vec8 }));
diTypes.emplace(T(UShort2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::getType())], { vec8 }));
diTypes.emplace(T(Short4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Short::getType())], { vec8 }));
diTypes.emplace(T(UShort4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::getType())], { vec8 }));
diTypes.emplace(T(Short8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Short::getType())], { vec8 }));
diTypes.emplace(T(UShort8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::getType())], { vec8 }));
diTypes.emplace(T(Int2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Int::getType())], { vec4 }));
diTypes.emplace(T(UInt2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::getType())], { vec4 }));
diTypes.emplace(T(Int4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Int::getType())], { vec4 }));
diTypes.emplace(T(UInt4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::getType())], { vec4 }));
diTypes.emplace(T(Float2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Float::getType())], { vec4 }));
diTypes.emplace(T(Float4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Float::getType())], { vec4 }));
}
DebugInfo::Location DebugInfo::getCallerLocation() const
{
return getCallerBacktrace(1)[0];
}
DebugInfo::Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const
{
auto shouldSkipFile = [](llvm::StringRef fileSR) {
return fileSR.empty() ||
fileSR.endswith_lower("ReactorDebugInfo.cpp") ||
fileSR.endswith_lower("Reactor.cpp") ||
fileSR.endswith_lower("Reactor.hpp") ||
fileSR.endswith_lower("stacktrace.hpp");
};
std::vector<DebugInfo::Location> locations;
// Note that bs::stacktrace() effectively returns a vector of addresses; bs::frame construction is where
// the heavy lifting is done: resolving the function name, file and line number.
namespace bs = boost::stacktrace;
for(bs::frame frame : bs::stacktrace())
{
if(shouldSkipFile(frame.source_file()))
{
continue;
}
DebugInfo::Location location;
location.function.file = frame.source_file();
location.function.name = frame.name();
location.line = frame.source_line();
locations.push_back(location);
if(limit > 0 && locations.size() >= limit)
{
break;
}
}
std::reverse(locations.begin(), locations.end());
return locations;
}
llvm::DIType *DebugInfo::getOrCreateType(llvm::Type *type)
{
auto it = diTypes.find(type);
if(it != diTypes.end()) { return it->second; }
if(type->isPointerTy())
{
auto dbgTy = diBuilder->createPointerType(
getOrCreateType(type->getPointerElementType()),
sizeof(void *) * 8, alignof(void *) * 8);
diTypes.emplace(type, dbgTy);
return dbgTy;
}
llvm::errs() << "Unimplemented debug type: " << type << "\n";
assert(false);
return nullptr;
}
llvm::DIFile *DebugInfo::getOrCreateFile(const char *path)
{
auto it = diFiles.find(path);
if(it != diFiles.end()) { return it->second; }
auto dirAndName = splitPath(path);
auto file = diBuilder->createFile(dirAndName.second, dirAndName.first);
diFiles.emplace(path, file);
return file;
}
DebugInfo::LineTokens const *DebugInfo::getOrParseFileTokens(const char *path)
{
static std::regex reLocalDecl(
"^" // line start
"\\s*" // initial whitespace
"(?:For\\s*\\(\\s*)?" // optional 'For('
"((?:\\w+(?:<[^>]+>)?)(?:::\\w+(?:<[^>]+>)?)*)" // type (match group 1)
"\\s+" // whitespace between type and name
"(\\w+)" // identifier (match group 2)
"\\s*" // whitespace after identifier
"(\\[.*\\])?"); // optional array suffix (match group 3)
auto it = fileTokens.find(path);
if(it != fileTokens.end())
{
return it->second.get();
}
auto tokens = std::make_unique<LineTokens>();
std::ifstream file(path);
std::string line;
int lineCount = 0;
while(std::getline(file, line))
{
lineCount++;
std::smatch match;
if(std::regex_search(line, match, reLocalDecl) && match.size() > 3)
{
bool isArray = match.str(3) != "";
if(!isArray) // Cannot deal with C-arrays of values.
{
if(match.str(1) == "return")
{
(*tokens)[lineCount] = Token{ Token::Return };
}
else
{
(*tokens)[lineCount] = Token{ Token::Identifier, match.str(2) };
}
}
}
}
auto out = tokens.get();
fileTokens.emplace(path, std::move(tokens));
return out;
}
} // namespace rr
#endif // ENABLE_RR_DEBUG_INFO