blob: db970802b90cd67b6548af3a9b07d6a6c057f320 [file] [log] [blame]
Ben Claytonac07ed82019-03-26 14:17:41 +00001// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "LLVMReactorDebugInfo.hpp"
16
17#ifdef ENABLE_RR_DEBUG_INFO
18
19#include "Reactor.hpp"
20#include "LLVMReactor.hpp"
21
Antonio Maioranof448d8e2019-04-26 16:19:16 -040022#include "boost/stacktrace.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000023
24#include "llvm/Demangle/Demangle.h"
25#include "llvm/ExecutionEngine/JITEventListener.h"
26#include "llvm/IR/DIBuilder.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/IRBuilder.h"
29
30#include <cctype>
31#include <fstream>
Ben Clayton90cb2602019-05-23 14:42:32 +010032#include <mutex>
Ben Claytonac07ed82019-03-26 14:17:41 +000033#include <regex>
34#include <sstream>
35#include <string>
36
37#if 0
38#define LOG(msg, ...) printf(msg "\n", ##__VA_ARGS__)
39#else
40#define LOG(msg, ...)
41#endif
42
Nicolas Capens157ba262019-12-10 17:49:14 -050043namespace {
44
45std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char* path)
Ben Claytonac07ed82019-03-26 14:17:41 +000046{
Nicolas Capens157ba262019-12-10 17:49:14 -050047 return llvm::StringRef(path).rsplit('/');
48}
Ben Clayton90cb2602019-05-23 14:42:32 +010049
Nicolas Capens157ba262019-12-10 17:49:14 -050050// Note: createGDBRegistrationListener() returns a pointer to a singleton.
51// Nothing is actually created.
52auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex
53std::mutex jitEventListenerMutex;
Ben Clayton90cb2602019-05-23 14:42:32 +010054
Nicolas Capens157ba262019-12-10 17:49:14 -050055} // anonymous namespaces
Ben Claytonac07ed82019-03-26 14:17:41 +000056
Nicolas Capens157ba262019-12-10 17:49:14 -050057namespace rr {
58
59DebugInfo::DebugInfo(
60 llvm::IRBuilder<> *builder,
61 llvm::LLVMContext *context,
62 llvm::Module *module,
63 llvm::Function *function)
64 : builder(builder), context(context), module(module), function(function)
Ben Claytonac07ed82019-03-26 14:17:41 +000065{
Nicolas Capens157ba262019-12-10 17:49:14 -050066 using namespace ::llvm;
Ben Claytonac07ed82019-03-26 14:17:41 +000067
Nicolas Capens157ba262019-12-10 17:49:14 -050068 auto location = getCallerLocation();
Ben Claytonac07ed82019-03-26 14:17:41 +000069
Nicolas Capens157ba262019-12-10 17:49:14 -050070 auto fileAndDir = splitPath(location.function.file.c_str());
71 diBuilder.reset(new llvm::DIBuilder(*module));
72 diCU = diBuilder->createCompileUnit(
73 llvm::dwarf::DW_LANG_C,
74 diBuilder->createFile(fileAndDir.first, fileAndDir.second),
75 "Reactor",
76 0, "", 0);
Ben Claytonac07ed82019-03-26 14:17:41 +000077
Nicolas Capens157ba262019-12-10 17:49:14 -050078 registerBasicTypes();
Ben Claytonac07ed82019-03-26 14:17:41 +000079
Nicolas Capens157ba262019-12-10 17:49:14 -050080 SmallVector<Metadata *, 8> EltTys;
81 auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(EltTys));
Ben Claytonac07ed82019-03-26 14:17:41 +000082
Nicolas Capens157ba262019-12-10 17:49:14 -050083 auto file = getOrCreateFile(location.function.file.c_str());
84 auto sp = diBuilder->createFunction(
85 file, // scope
86 "ReactorFunction", // function name
87 "ReactorFunction", // linkage
88 file, // file
89 location.line, // line
90 funcTy, // type
91 false, // internal linkage
92 true, // definition
93 location.line, // scope line
94 DINode::FlagPrototyped, // flags
95 false // is optimized
96 );
97 diSubprogram = sp;
98 function->setSubprogram(sp);
99 diRootLocation = DILocation::get(*context, location.line, 0, sp);
100 builder->SetCurrentDebugLocation(diRootLocation);
101}
Ben Claytonac07ed82019-03-26 14:17:41 +0000102
Nicolas Capens157ba262019-12-10 17:49:14 -0500103DebugInfo::~DebugInfo() = default;
Ben Clayton90cb2602019-05-23 14:42:32 +0100104
Nicolas Capens157ba262019-12-10 17:49:14 -0500105void DebugInfo::Finalize()
106{
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500107 while(diScope.size() > 0)
Ben Claytonac07ed82019-03-26 14:17:41 +0000108 {
Ben Clayton90cb2602019-05-23 14:42:32 +0100109 emitPending(diScope.back(), builder);
Nicolas Capens157ba262019-12-10 17:49:14 -0500110 diScope.pop_back();
111 }
112 diBuilder->finalize();
113}
114
115void DebugInfo::EmitLocation()
116{
117 auto const& backtrace = getCallerBacktrace();
118 syncScope(backtrace);
119 builder->SetCurrentDebugLocation(getLocation(backtrace, backtrace.size() - 1));
120
121#ifdef ENABLE_RR_EMIT_PRINT_LOCATION
122 static Location lastLocation;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500123 if(backtrace.size() == 0)
Nicolas Capens157ba262019-12-10 17:49:14 -0500124 {
125 return;
126 }
127 Location currLocation = backtrace[backtrace.size() - 1];
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500128 if(currLocation != lastLocation)
Nicolas Capens157ba262019-12-10 17:49:14 -0500129 {
130 rr::Print("rr> {0} [{1}:{2}]\n", currLocation.function.name.c_str(), currLocation.function.file.c_str(), currLocation.line);
131 lastLocation = std::move(currLocation);
132 }
133#endif // ENABLE_RR_EMIT_PRINT_LOCATION
134}
135
136void DebugInfo::Flush()
137{
138 emitPending(diScope.back(), builder);
139}
140
141void DebugInfo::syncScope(Backtrace const& backtrace)
142{
143 auto shrink = [this](size_t newsize)
144 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500145 while(diScope.size() > newsize)
Nicolas Capens157ba262019-12-10 17:49:14 -0500146 {
147 auto &scope = diScope.back();
148 LOG("- STACK(%d): di: %p, location: %s:%d",
149 int(diScope.size() - 1), scope.di,
150 scope.location.function.file.c_str(),
151 int(scope.location.line));
152 emitPending(scope, builder);
153 diScope.pop_back();
154 }
155 };
156
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500157 if(backtrace.size() < diScope.size())
Nicolas Capens157ba262019-12-10 17:49:14 -0500158 {
159 shrink(backtrace.size());
Ben Claytonac07ed82019-03-26 14:17:41 +0000160 }
161
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500162 for(size_t i = 0; i < diScope.size(); i++)
Ben Claytonac07ed82019-03-26 14:17:41 +0000163 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500164 auto &scope = diScope[i];
165 auto const &oldLocation = scope.location;
166 auto const &newLocation = backtrace[i];
Ben Claytonac07ed82019-03-26 14:17:41 +0000167
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500168 if(oldLocation.function != newLocation.function)
Ben Claytonac07ed82019-03-26 14:17:41 +0000169 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500170 LOG(" STACK(%d): Changed function %s -> %s", int(i),
171 oldLocation.function.name.c_str(), newLocation.function.name.c_str());
172 shrink(i);
173 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000174 }
175
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500176 if(oldLocation.line > newLocation.line)
Ben Claytonac07ed82019-03-26 14:17:41 +0000177 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500178 // Create a new di block to shadow all the variables in the loop.
179 auto file = getOrCreateFile(newLocation.function.file.c_str());
180 auto di = diBuilder->createLexicalBlock(scope.di, file, newLocation.line, 0);
181 LOG(" STACK(%d): Jumped backwards %d -> %d. di: %p -> %p", int(i),
182 oldLocation.line, newLocation.line, scope.di, di);
183 emitPending(scope, builder);
184 scope = {newLocation, di};
185 shrink(i+1);
186 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000187 }
188
Nicolas Capens157ba262019-12-10 17:49:14 -0500189 scope.location = newLocation;
Ben Claytonac07ed82019-03-26 14:17:41 +0000190 }
191
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500192 while(backtrace.size() > diScope.size())
Ben Claytonac07ed82019-03-26 14:17:41 +0000193 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500194 auto i = diScope.size();
195 auto location = backtrace[i];
196 auto file = getOrCreateFile(location.function.file.c_str());
197 auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray({}));
198
199 char buf[1024];
200 size_t size = sizeof(buf);
201 int status = 0;
202 llvm::itaniumDemangle(location.function.name.c_str(), buf, &size, &status);
203 auto name = "jit!" + (status == 0 ? std::string(buf) : location.function.name);
204
205 auto func = diBuilder->createFunction(
206 file, // scope
207 name, // function name
208 "", // linkage
209 file, // file
210 location.line, // line
211 funcTy, // type
212 false, // internal linkage
213 true, // definition
214 location.line, // scope line
215 llvm::DINode::FlagPrototyped, // flags
216 false // is optimized
Ben Claytonac07ed82019-03-26 14:17:41 +0000217 );
Nicolas Capens157ba262019-12-10 17:49:14 -0500218 diScope.push_back({location, func});
219 LOG("+ STACK(%d): di: %p, location: %s:%d", int(i), di,
220 location.function.file.c_str(), int(location.line));
Ben Claytonac07ed82019-03-26 14:17:41 +0000221 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500222}
Ben Claytonac07ed82019-03-26 14:17:41 +0000223
Nicolas Capens157ba262019-12-10 17:49:14 -0500224llvm::DILocation* DebugInfo::getLocation(const Backtrace &backtrace, size_t i)
225{
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500226 if(backtrace.size() == 0) { return nullptr; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500227 assert(backtrace.size() == diScope.size());
228 return llvm::DILocation::get(
229 *context,
230 backtrace[i].line,
231 0,
232 diScope[i].di,
233 i > 0 ? getLocation(backtrace, i - 1) : diRootLocation
234 );
235}
236
237void DebugInfo::EmitVariable(Value *variable)
238{
239 auto const& backtrace = getCallerBacktrace();
240 syncScope(backtrace);
241
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500242 for(int i = backtrace.size() - 1; i >= 0; i--)
Ben Claytonac07ed82019-03-26 14:17:41 +0000243 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500244 auto const &location = backtrace[i];
245 auto tokens = getOrParseFileTokens(location.function.file.c_str());
246 auto tokIt = tokens->find(location.line);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500247 if(tokIt == tokens->end())
Ben Claytonac07ed82019-03-26 14:17:41 +0000248 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500249 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000250 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500251 auto token = tokIt->second;
252 auto name = token.identifier;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500253 if(token.kind == Token::Return)
Ben Claytonac07ed82019-03-26 14:17:41 +0000254 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500255 // This is a:
Ben Claytonac07ed82019-03-26 14:17:41 +0000256 //
Nicolas Capens157ba262019-12-10 17:49:14 -0500257 // return <expr>;
Ben Claytonac07ed82019-03-26 14:17:41 +0000258 //
Nicolas Capens157ba262019-12-10 17:49:14 -0500259 // Emit this expression as two variables -
260 // Once as a synthetic 'return_value' variable at this scope.
261 // Again by bubbling the expression value up the callstack as
262 // Return Value Optimizations (RVOs) are likely to carry across
263 // the value to a local without calling a constructor in
264 // statements like:
265 //
266 // auto val = foo();
267 //
268 name = "return_value";
Ben Claytonac07ed82019-03-26 14:17:41 +0000269 }
270
Nicolas Capens157ba262019-12-10 17:49:14 -0500271 auto &scope = diScope[i];
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500272 if(scope.pending.location != location)
Ben Claytonac07ed82019-03-26 14:17:41 +0000273 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500274 emitPending(scope, builder);
275 }
276
277 auto value = V(variable);
278 auto block = builder->GetInsertBlock();
279
280 auto insertAfter = block->size() > 0 ? &block->back() : nullptr;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500281 while(insertAfter != nullptr && insertAfter->isTerminator())
Nicolas Capens157ba262019-12-10 17:49:14 -0500282 {
283 insertAfter = insertAfter->getPrevNode();
Ben Claytonac07ed82019-03-26 14:17:41 +0000284 }
285
286 scope.pending = Pending{};
Nicolas Capens157ba262019-12-10 17:49:14 -0500287 scope.pending.name = name;
288 scope.pending.location = location;
289 scope.pending.diLocation = getLocation(backtrace, i);
290 scope.pending.value = value;
291 scope.pending.block = block;
292 scope.pending.insertAfter = insertAfter;
293 scope.pending.scope = scope.di;
Ben Claytonac07ed82019-03-26 14:17:41 +0000294
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500295 if(token.kind == Token::Return)
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400296 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500297 // Insert a noop instruction so the debugger can inspect the
298 // return value before the function scope closes.
299 scope.pending.addNopOnNextLine = true;
300 }
301 else
302 {
303 break;
304 }
305 }
306}
Ben Claytonac07ed82019-03-26 14:17:41 +0000307
Nicolas Capens157ba262019-12-10 17:49:14 -0500308void DebugInfo::emitPending(Scope &scope, IRBuilder *builder)
309{
310 auto const &pending = scope.pending;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500311 if(pending.value == nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -0500312 {
313 return;
314 }
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400315
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500316 if(!scope.symbols.emplace(pending.name).second)
Nicolas Capens157ba262019-12-10 17:49:14 -0500317 {
318 return;
319 }
320
321 bool isAlloca = llvm::isa<llvm::AllocaInst>(pending.value);
322
323 LOG(" EMIT(%s): di: %p, location: %s:%d, isAlloca: %s", pending.name.c_str(), scope.di,
324 pending.location.function.file.c_str(), pending.location.line, isAlloca ? "true" : "false");
325
326 auto value = pending.value;
327
328 IRBuilder::InsertPointGuard guard(*builder);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500329 if(pending.insertAfter != nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -0500330 {
331 builder->SetInsertPoint(pending.block, ++pending.insertAfter->getIterator());
332 }
333 else
334 {
335 builder->SetInsertPoint(pending.block);
336 }
337 builder->SetCurrentDebugLocation(pending.diLocation);
338
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500339 if(!isAlloca)
Nicolas Capens157ba262019-12-10 17:49:14 -0500340 {
341 // While insertDbgValueIntrinsic should be enough to declare a
342 // variable with no storage, variables of RValues can share the same
343 // llvm::Value, and only one can be named. Take for example:
344 //
345 // Int a = 42;
346 // RValue<Int> b = a;
347 // RValue<Int> c = b;
348 //
349 // To handle this, always promote named RValues to an alloca.
350
351 llvm::BasicBlock &entryBlock = function->getEntryBlock();
352 auto alloca = new llvm::AllocaInst(value->getType(), 0, pending.name);
353 entryBlock.getInstList().push_front(alloca);
354 builder->CreateStore(value, alloca);
355 value = alloca;
356 }
357
358 value->setName(pending.name);
359
360 auto diFile = getOrCreateFile(pending.location.function.file.c_str());
361 auto diType = getOrCreateType(value->getType()->getPointerElementType());
362 auto diVar = diBuilder->createAutoVariable(scope.di, pending.name, diFile, pending.location.line, diType);
363
364 auto di = diBuilder->insertDeclare(value, diVar, diBuilder->createExpression(), pending.diLocation, pending.block);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500365 if(pending.insertAfter != nullptr) { di->moveAfter(pending.insertAfter); }
Nicolas Capens157ba262019-12-10 17:49:14 -0500366
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500367 if(pending.addNopOnNextLine)
Nicolas Capens157ba262019-12-10 17:49:14 -0500368 {
369 builder->SetCurrentDebugLocation(llvm::DILocation::get(
370 *context,
371 pending.diLocation->getLine() + 1,
372 0,
373 pending.diLocation->getScope(),
374 pending.diLocation->getInlinedAt()
375 ));
376 Nop();
377 }
378
379 scope.pending = Pending{};
380}
381
382void DebugInfo::NotifyObjectEmitted(const llvm::object::ObjectFile &Obj, const llvm::LoadedObjectInfo &L)
383{
384 std::unique_lock<std::mutex> lock(jitEventListenerMutex);
385 jitEventListener->NotifyObjectEmitted(Obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo&>(L));
386}
387
388void DebugInfo::NotifyFreeingObject(const llvm::object::ObjectFile &Obj)
389{
390 std::unique_lock<std::mutex> lock(jitEventListenerMutex);
391 jitEventListener->NotifyFreeingObject(Obj);
392}
393
394void DebugInfo::registerBasicTypes()
395{
396 using namespace rr;
397 using namespace llvm;
398
399 auto vec4 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 4));
400 auto vec8 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 8));
401 auto vec16 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 16));
402
403 diTypes.emplace(T(Bool::getType()), diBuilder->createBasicType("Bool", sizeof(bool), dwarf::DW_ATE_boolean));
404 diTypes.emplace(T(Byte::getType()), diBuilder->createBasicType("Byte", 8, dwarf::DW_ATE_unsigned_char));
405 diTypes.emplace(T(SByte::getType()), diBuilder->createBasicType("SByte", 8, dwarf::DW_ATE_signed_char));
406 diTypes.emplace(T(Short::getType()), diBuilder->createBasicType("Short", 16, dwarf::DW_ATE_signed));
407 diTypes.emplace(T(UShort::getType()), diBuilder->createBasicType("UShort", 16, dwarf::DW_ATE_unsigned));
408 diTypes.emplace(T(Int::getType()), diBuilder->createBasicType("Int", 32, dwarf::DW_ATE_signed));
409 diTypes.emplace(T(UInt::getType()), diBuilder->createBasicType("UInt", 32, dwarf::DW_ATE_unsigned));
410 diTypes.emplace(T(Long::getType()), diBuilder->createBasicType("Long", 64, dwarf::DW_ATE_signed));
411 diTypes.emplace(T(Half::getType()), diBuilder->createBasicType("Half", 16, dwarf::DW_ATE_float));
412 diTypes.emplace(T(Float::getType()), diBuilder->createBasicType("Float", 32, dwarf::DW_ATE_float));
413
414 diTypes.emplace(T(Byte4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::getType())], {vec16}));
415 diTypes.emplace(T(SByte4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::getType())], {vec16}));
416 diTypes.emplace(T(Byte8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::getType())], {vec16}));
417 diTypes.emplace(T(SByte8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::getType())], {vec16}));
418 diTypes.emplace(T(Byte16::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::getType())], {vec16}));
419 diTypes.emplace(T(SByte16::getType()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::getType())], {vec16}));
420 diTypes.emplace(T(Short2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Short::getType())], {vec8}));
421 diTypes.emplace(T(UShort2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::getType())], {vec8}));
422 diTypes.emplace(T(Short4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Short::getType())], {vec8}));
423 diTypes.emplace(T(UShort4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::getType())], {vec8}));
424 diTypes.emplace(T(Short8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Short::getType())], {vec8}));
425 diTypes.emplace(T(UShort8::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::getType())], {vec8}));
426 diTypes.emplace(T(Int2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Int::getType())], {vec4}));
427 diTypes.emplace(T(UInt2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::getType())], {vec4}));
428 diTypes.emplace(T(Int4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Int::getType())], {vec4}));
429 diTypes.emplace(T(UInt4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::getType())], {vec4}));
430 diTypes.emplace(T(Float2::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Float::getType())], {vec4}));
431 diTypes.emplace(T(Float4::getType()), diBuilder->createVectorType(128, 128, diTypes[T(Float::getType())], {vec4}));
432}
433
434DebugInfo::Location DebugInfo::getCallerLocation() const
435{
436 return getCallerBacktrace(1)[0];
437}
438
439DebugInfo::Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const
440{
441 auto shouldSkipFile = [](llvm::StringRef fileSR) {
442 return fileSR.empty() ||
443 fileSR.endswith_lower("ReactorDebugInfo.cpp") ||
444 fileSR.endswith_lower("Reactor.cpp") ||
445 fileSR.endswith_lower("Reactor.hpp") ||
446 fileSR.endswith_lower("stacktrace.hpp");
447 };
448
449 std::vector<DebugInfo::Location> locations;
450
451 // Note that bs::stacktrace() effectively returns a vector of addresses; bs::frame construction is where
452 // the heavy lifting is done: resolving the function name, file and line number.
453 namespace bs = boost::stacktrace;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500454 for(bs::frame frame : bs::stacktrace())
Nicolas Capens157ba262019-12-10 17:49:14 -0500455 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500456 if(shouldSkipFile(frame.source_file()))
Nicolas Capens157ba262019-12-10 17:49:14 -0500457 {
458 continue;
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400459 }
460
Nicolas Capens157ba262019-12-10 17:49:14 -0500461 DebugInfo::Location location;
462 location.function.file = frame.source_file();
463 location.function.name = frame.name();
464 location.line = frame.source_line();
465 locations.push_back(location);
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400466
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500467 if(limit > 0 && locations.size() >= limit)
Ben Claytonac07ed82019-03-26 14:17:41 +0000468 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500469 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000470 }
Ben Claytonac07ed82019-03-26 14:17:41 +0000471 }
472
Nicolas Capens157ba262019-12-10 17:49:14 -0500473 std::reverse(locations.begin(), locations.end());
474
475 return locations;
476}
477
478llvm::DIType *DebugInfo::getOrCreateType(llvm::Type* type)
479{
480 auto it = diTypes.find(type);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500481 if(it != diTypes.end()) { return it->second; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500482
483 if(type->isPointerTy())
Ben Claytonac07ed82019-03-26 14:17:41 +0000484 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500485 auto dbgTy = diBuilder->createPointerType(
486 getOrCreateType(type->getPointerElementType()),
487 sizeof(void*)*8, alignof(void*)*8);
488 diTypes.emplace(type, dbgTy);
489 return dbgTy;
490 }
491 llvm::errs() << "Unimplemented debug type: " << type << "\n";
492 assert(false);
493 return nullptr;
494}
495
496llvm::DIFile *DebugInfo::getOrCreateFile(const char* path)
497{
498 auto it = diFiles.find(path);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500499 if(it != diFiles.end()) { return it->second; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500500 auto dirAndName = splitPath(path);
501 auto file = diBuilder->createFile(dirAndName.second, dirAndName.first);
502 diFiles.emplace(path, file);
503 return file;
504}
505
506DebugInfo::LineTokens const *DebugInfo::getOrParseFileTokens(const char* path)
507{
508 static std::regex reLocalDecl(
509 "^" // line start
510 "\\s*" // initial whitespace
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500511 "(?:For\\s*\\(\\s*)?" // optional 'For('
Nicolas Capens157ba262019-12-10 17:49:14 -0500512 "((?:\\w+(?:<[^>]+>)?)(?:::\\w+(?:<[^>]+>)?)*)" // type (match group 1)
513 "\\s+" // whitespace between type and name
514 "(\\w+)" // identifier (match group 2)
515 "\\s*" // whitespace after identifier
516 "(\\[.*\\])?"); // optional array suffix (match group 3)
517
518 auto it = fileTokens.find(path);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500519 if(it != fileTokens.end())
Nicolas Capens157ba262019-12-10 17:49:14 -0500520 {
521 return it->second.get();
Ben Claytonac07ed82019-03-26 14:17:41 +0000522 }
523
Nicolas Capens157ba262019-12-10 17:49:14 -0500524 auto tokens = std::unique_ptr<LineTokens>(new LineTokens());
525
526 std::ifstream file(path);
527 std::string line;
528 int lineCount = 0;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500529 while(std::getline(file, line))
Ben Claytonac07ed82019-03-26 14:17:41 +0000530 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500531 lineCount++;
532 std::smatch match;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500533 if(std::regex_search(line, match, reLocalDecl) && match.size() > 3)
Ben Claytonac07ed82019-03-26 14:17:41 +0000534 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500535 bool isArray = match.str(3) != "";
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500536 if(!isArray) // Cannot deal with C-arrays of values.
Ben Claytonac07ed82019-03-26 14:17:41 +0000537 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500538 if(match.str(1) == "return")
Ben Claytonac07ed82019-03-26 14:17:41 +0000539 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500540 (*tokens)[lineCount] = Token{Token::Return};
541 }
542 else
543 {
544 (*tokens)[lineCount] = Token{Token::Identifier, match.str(2)};
Ben Claytonac07ed82019-03-26 14:17:41 +0000545 }
546 }
547 }
Ben Claytonac07ed82019-03-26 14:17:41 +0000548 }
549
Nicolas Capens157ba262019-12-10 17:49:14 -0500550 auto out = tokens.get();
551 fileTokens.emplace(path, std::move(tokens));
552 return out;
553}
554
555} // namespace rr
Ben Claytonac07ed82019-03-26 14:17:41 +0000556
557#endif // ENABLE_RR_DEBUG_INFO