blob: b4c0688c5bfb426f338abae93fe33a1718e3615d [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
Ben Clayton713b8d32019-12-17 20:37:56 +000019# include "LLVMReactor.hpp"
20# include "Reactor.hpp"
Antonio Maiorano415d1812020-02-11 16:22:55 -050021# include "Print.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000022
Ben Clayton713b8d32019-12-17 20:37:56 +000023# include "boost/stacktrace.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000024
Nicolas Capens41a73022020-01-30 00:30:14 -050025// TODO(b/143539525): Eliminate when warning has been fixed.
26# ifdef _MSC_VER
27__pragma(warning(push))
28 __pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned
29# endif
30
Ben Clayton713b8d32019-12-17 20:37:56 +000031# include "llvm/Demangle/Demangle.h"
32# include "llvm/ExecutionEngine/JITEventListener.h"
33# include "llvm/IR/DIBuilder.h"
34# include "llvm/IR/IRBuilder.h"
35# include "llvm/IR/Intrinsics.h"
Ben Claytonac07ed82019-03-26 14:17:41 +000036
Nicolas Capens41a73022020-01-30 00:30:14 -050037# ifdef _MSC_VER
38 __pragma(warning(pop))
39# endif
40
Ben Clayton713b8d32019-12-17 20:37:56 +000041# include <cctype>
42# include <fstream>
43# include <mutex>
44# include <regex>
45# include <sstream>
46# include <string>
Ben Claytonac07ed82019-03-26 14:17:41 +000047
Ben Clayton713b8d32019-12-17 20:37:56 +000048# if 0
49# define LOG(msg, ...) printf(msg "\n", ##__VA_ARGS__)
50# else
51# define LOG(msg, ...)
52# endif
Ben Claytonac07ed82019-03-26 14:17:41 +000053
Nicolas Capens41a73022020-01-30 00:30:14 -050054 namespace
Ben Claytonac07ed82019-03-26 14:17:41 +000055{
Ben Clayton90cb2602019-05-23 14:42:32 +010056
Nicolas Capens41a73022020-01-30 00:30:14 -050057 std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path)
58 {
Antonio Maioranoaf4a3a92021-01-14 14:29:45 -050059# ifdef _WIN32
60 auto dirAndFile = llvm::StringRef(path).rsplit('\\');
61# else
62 auto dirAndFile = llvm::StringRef(path).rsplit('/');
63# endif
Ben Claytonee18f392020-10-19 16:54:21 -040064 if(dirAndFile.second == "")
65 {
66 dirAndFile.second = "<unknown>";
67 }
68 return dirAndFile;
Nicolas Capens41a73022020-01-30 00:30:14 -050069 }
70
71 // Note: createGDBRegistrationListener() returns a pointer to a singleton.
72 // Nothing is actually created.
73 auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex
74 std::mutex jitEventListenerMutex;
Ben Clayton90cb2602019-05-23 14:42:32 +010075
Ben Clayton713b8d32019-12-17 20:37:56 +000076} // namespace
Ben Claytonac07ed82019-03-26 14:17:41 +000077
Nicolas Capens157ba262019-12-10 17:49:14 -050078namespace rr {
79
80DebugInfo::DebugInfo(
Ben Clayton713b8d32019-12-17 20:37:56 +000081 llvm::IRBuilder<> *builder,
82 llvm::LLVMContext *context,
83 llvm::Module *module,
84 llvm::Function *function)
85 : builder(builder)
86 , context(context)
87 , module(module)
88 , function(function)
Ben Claytonac07ed82019-03-26 14:17:41 +000089{
Nicolas Capens157ba262019-12-10 17:49:14 -050090 using namespace ::llvm;
Ben Claytonac07ed82019-03-26 14:17:41 +000091
Nicolas Capens157ba262019-12-10 17:49:14 -050092 auto location = getCallerLocation();
Ben Claytonac07ed82019-03-26 14:17:41 +000093
Ben Claytonee18f392020-10-19 16:54:21 -040094 auto dirAndFile = splitPath(location.function.file.c_str());
Nicolas Capens157ba262019-12-10 17:49:14 -050095 diBuilder.reset(new llvm::DIBuilder(*module));
96 diCU = diBuilder->createCompileUnit(
Ben Clayton713b8d32019-12-17 20:37:56 +000097 llvm::dwarf::DW_LANG_C,
Ben Claytonee18f392020-10-19 16:54:21 -040098 diBuilder->createFile(dirAndFile.second, dirAndFile.first),
Ben Clayton713b8d32019-12-17 20:37:56 +000099 "Reactor",
100 0, "", 0);
Ben Claytonac07ed82019-03-26 14:17:41 +0000101
Nicolas Capens157ba262019-12-10 17:49:14 -0500102 registerBasicTypes();
Ben Claytonac07ed82019-03-26 14:17:41 +0000103
Nicolas Capens157ba262019-12-10 17:49:14 -0500104 SmallVector<Metadata *, 8> EltTys;
105 auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(EltTys));
Ben Claytonac07ed82019-03-26 14:17:41 +0000106
Nicolas Capens157ba262019-12-10 17:49:14 -0500107 auto file = getOrCreateFile(location.function.file.c_str());
108 auto sp = diBuilder->createFunction(
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400109 file, // scope
110 "ReactorFunction", // function name
111 "ReactorFunction", // linkage
112 file, // file
113 location.line, // line
114 funcTy, // type
115 location.line, // scope line
116 DINode::FlagPrototyped, // flags
117 DISubprogram::SPFlagDefinition // subprogram flags
Nicolas Capens157ba262019-12-10 17:49:14 -0500118 );
119 diSubprogram = sp;
120 function->setSubprogram(sp);
121 diRootLocation = DILocation::get(*context, location.line, 0, sp);
122 builder->SetCurrentDebugLocation(diRootLocation);
123}
Ben Claytonac07ed82019-03-26 14:17:41 +0000124
Nicolas Capens157ba262019-12-10 17:49:14 -0500125DebugInfo::~DebugInfo() = default;
Ben Clayton90cb2602019-05-23 14:42:32 +0100126
Nicolas Capens157ba262019-12-10 17:49:14 -0500127void DebugInfo::Finalize()
128{
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500129 while(diScope.size() > 0)
Ben Claytonac07ed82019-03-26 14:17:41 +0000130 {
Ben Clayton90cb2602019-05-23 14:42:32 +0100131 emitPending(diScope.back(), builder);
Nicolas Capens157ba262019-12-10 17:49:14 -0500132 diScope.pop_back();
133 }
134 diBuilder->finalize();
135}
136
137void DebugInfo::EmitLocation()
138{
Shahbaz Youssefi4dbbcd02022-09-13 22:23:30 -0400139 const auto &backtrace = getCallerBacktrace();
Nicolas Capens157ba262019-12-10 17:49:14 -0500140 syncScope(backtrace);
141 builder->SetCurrentDebugLocation(getLocation(backtrace, backtrace.size() - 1));
Antonio Maioranoaae33732020-02-14 14:52:34 -0500142 emitPrintLocation(backtrace);
Nicolas Capens157ba262019-12-10 17:49:14 -0500143}
144
145void DebugInfo::Flush()
146{
Ben Claytonee18f392020-10-19 16:54:21 -0400147 if(!diScope.empty())
148 {
149 emitPending(diScope.back(), builder);
150 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500151}
152
Nicolas Capens7e1d67a2022-10-04 10:36:33 -0400153void DebugInfo::syncScope(const Backtrace &backtrace)
Nicolas Capens157ba262019-12-10 17:49:14 -0500154{
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400155 using namespace ::llvm;
156
Ben Clayton713b8d32019-12-17 20:37:56 +0000157 auto shrink = [this](size_t newsize) {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500158 while(diScope.size() > newsize)
Nicolas Capens157ba262019-12-10 17:49:14 -0500159 {
160 auto &scope = diScope.back();
161 LOG("- STACK(%d): di: %p, location: %s:%d",
Ben Clayton713b8d32019-12-17 20:37:56 +0000162 int(diScope.size() - 1), scope.di,
163 scope.location.function.file.c_str(),
164 int(scope.location.line));
Nicolas Capens157ba262019-12-10 17:49:14 -0500165 emitPending(scope, builder);
166 diScope.pop_back();
167 }
168 };
169
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500170 if(backtrace.size() < diScope.size())
Nicolas Capens157ba262019-12-10 17:49:14 -0500171 {
172 shrink(backtrace.size());
Ben Claytonac07ed82019-03-26 14:17:41 +0000173 }
174
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500175 for(size_t i = 0; i < diScope.size(); i++)
Ben Claytonac07ed82019-03-26 14:17:41 +0000176 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500177 auto &scope = diScope[i];
Shahbaz Youssefi4dbbcd02022-09-13 22:23:30 -0400178 const auto &oldLocation = scope.location;
179 const auto &newLocation = backtrace[i];
Ben Claytonac07ed82019-03-26 14:17:41 +0000180
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500181 if(oldLocation.function != newLocation.function)
Ben Claytonac07ed82019-03-26 14:17:41 +0000182 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500183 LOG(" STACK(%d): Changed function %s -> %s", int(i),
Ben Clayton713b8d32019-12-17 20:37:56 +0000184 oldLocation.function.name.c_str(), newLocation.function.name.c_str());
Nicolas Capens157ba262019-12-10 17:49:14 -0500185 shrink(i);
186 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000187 }
188
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500189 if(oldLocation.line > newLocation.line)
Ben Claytonac07ed82019-03-26 14:17:41 +0000190 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500191 // Create a new di block to shadow all the variables in the loop.
192 auto file = getOrCreateFile(newLocation.function.file.c_str());
193 auto di = diBuilder->createLexicalBlock(scope.di, file, newLocation.line, 0);
194 LOG(" STACK(%d): Jumped backwards %d -> %d. di: %p -> %p", int(i),
Ben Clayton713b8d32019-12-17 20:37:56 +0000195 oldLocation.line, newLocation.line, scope.di, di);
Nicolas Capens157ba262019-12-10 17:49:14 -0500196 emitPending(scope, builder);
Nicolas Capens2e89a702021-01-07 22:25:16 -0500197 scope = { newLocation, di, {}, {} };
Ben Clayton713b8d32019-12-17 20:37:56 +0000198 shrink(i + 1);
Nicolas Capens157ba262019-12-10 17:49:14 -0500199 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000200 }
201
Nicolas Capens157ba262019-12-10 17:49:14 -0500202 scope.location = newLocation;
Ben Claytonac07ed82019-03-26 14:17:41 +0000203 }
204
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500205 while(backtrace.size() > diScope.size())
Ben Claytonac07ed82019-03-26 14:17:41 +0000206 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500207 auto i = diScope.size();
208 auto location = backtrace[i];
209 auto file = getOrCreateFile(location.function.file.c_str());
210 auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray({}));
211
212 char buf[1024];
213 size_t size = sizeof(buf);
214 int status = 0;
215 llvm::itaniumDemangle(location.function.name.c_str(), buf, &size, &status);
216 auto name = "jit!" + (status == 0 ? std::string(buf) : location.function.name);
217
218 auto func = diBuilder->createFunction(
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400219 file, // scope
220 name, // function name
221 "", // linkage
222 file, // file
223 location.line, // line
224 funcTy, // type
225 location.line, // scope line
226 DINode::FlagPrototyped, // flags
227 DISubprogram::SPFlagDefinition // subprogram flags
Ben Claytonac07ed82019-03-26 14:17:41 +0000228 );
Nicolas Capens2e89a702021-01-07 22:25:16 -0500229 diScope.push_back({ location, func, {}, {} });
Nicolas Capens157ba262019-12-10 17:49:14 -0500230 LOG("+ STACK(%d): di: %p, location: %s:%d", int(i), di,
Ben Clayton713b8d32019-12-17 20:37:56 +0000231 location.function.file.c_str(), int(location.line));
Ben Claytonac07ed82019-03-26 14:17:41 +0000232 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500233}
Ben Claytonac07ed82019-03-26 14:17:41 +0000234
Ben Clayton713b8d32019-12-17 20:37:56 +0000235llvm::DILocation *DebugInfo::getLocation(const Backtrace &backtrace, size_t i)
Nicolas Capens157ba262019-12-10 17:49:14 -0500236{
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500237 if(backtrace.size() == 0) { return nullptr; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500238 assert(backtrace.size() == diScope.size());
239 return llvm::DILocation::get(
Ben Clayton713b8d32019-12-17 20:37:56 +0000240 *context,
241 backtrace[i].line,
242 0,
243 diScope[i].di,
244 i > 0 ? getLocation(backtrace, i - 1) : diRootLocation);
Nicolas Capens157ba262019-12-10 17:49:14 -0500245}
246
247void DebugInfo::EmitVariable(Value *variable)
248{
Shahbaz Youssefi4dbbcd02022-09-13 22:23:30 -0400249 const auto &backtrace = getCallerBacktrace();
Nicolas Capens157ba262019-12-10 17:49:14 -0500250 syncScope(backtrace);
251
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500252 for(int i = backtrace.size() - 1; i >= 0; i--)
Ben Claytonac07ed82019-03-26 14:17:41 +0000253 {
Shahbaz Youssefi4dbbcd02022-09-13 22:23:30 -0400254 const auto &location = backtrace[i];
Nicolas Capens157ba262019-12-10 17:49:14 -0500255 auto tokens = getOrParseFileTokens(location.function.file.c_str());
256 auto tokIt = tokens->find(location.line);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500257 if(tokIt == tokens->end())
Ben Claytonac07ed82019-03-26 14:17:41 +0000258 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500259 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000260 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500261 auto token = tokIt->second;
262 auto name = token.identifier;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500263 if(token.kind == Token::Return)
Ben Claytonac07ed82019-03-26 14:17:41 +0000264 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500265 // This is a:
Ben Claytonac07ed82019-03-26 14:17:41 +0000266 //
Nicolas Capens157ba262019-12-10 17:49:14 -0500267 // return <expr>;
Ben Claytonac07ed82019-03-26 14:17:41 +0000268 //
Nicolas Capens157ba262019-12-10 17:49:14 -0500269 // Emit this expression as two variables -
270 // Once as a synthetic 'return_value' variable at this scope.
271 // Again by bubbling the expression value up the callstack as
272 // Return Value Optimizations (RVOs) are likely to carry across
273 // the value to a local without calling a constructor in
274 // statements like:
275 //
276 // auto val = foo();
277 //
278 name = "return_value";
Ben Claytonac07ed82019-03-26 14:17:41 +0000279 }
280
Nicolas Capens157ba262019-12-10 17:49:14 -0500281 auto &scope = diScope[i];
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500282 if(scope.pending.location != location)
Ben Claytonac07ed82019-03-26 14:17:41 +0000283 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500284 emitPending(scope, builder);
285 }
286
287 auto value = V(variable);
288 auto block = builder->GetInsertBlock();
289
290 auto insertAfter = block->size() > 0 ? &block->back() : nullptr;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500291 while(insertAfter != nullptr && insertAfter->isTerminator())
Nicolas Capens157ba262019-12-10 17:49:14 -0500292 {
293 insertAfter = insertAfter->getPrevNode();
Ben Claytonac07ed82019-03-26 14:17:41 +0000294 }
295
296 scope.pending = Pending{};
Nicolas Capens157ba262019-12-10 17:49:14 -0500297 scope.pending.name = name;
298 scope.pending.location = location;
299 scope.pending.diLocation = getLocation(backtrace, i);
300 scope.pending.value = value;
301 scope.pending.block = block;
302 scope.pending.insertAfter = insertAfter;
303 scope.pending.scope = scope.di;
Ben Claytonac07ed82019-03-26 14:17:41 +0000304
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500305 if(token.kind == Token::Return)
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400306 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500307 // Insert a noop instruction so the debugger can inspect the
308 // return value before the function scope closes.
309 scope.pending.addNopOnNextLine = true;
310 }
311 else
312 {
313 break;
314 }
315 }
316}
Ben Claytonac07ed82019-03-26 14:17:41 +0000317
Nicolas Capens157ba262019-12-10 17:49:14 -0500318void DebugInfo::emitPending(Scope &scope, IRBuilder *builder)
319{
Shahbaz Youssefi4dbbcd02022-09-13 22:23:30 -0400320 const auto &pending = scope.pending;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500321 if(pending.value == nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -0500322 {
323 return;
324 }
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400325
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500326 if(!scope.symbols.emplace(pending.name).second)
Nicolas Capens157ba262019-12-10 17:49:14 -0500327 {
328 return;
329 }
330
331 bool isAlloca = llvm::isa<llvm::AllocaInst>(pending.value);
332
333 LOG(" EMIT(%s): di: %p, location: %s:%d, isAlloca: %s", pending.name.c_str(), scope.di,
Ben Clayton713b8d32019-12-17 20:37:56 +0000334 pending.location.function.file.c_str(), pending.location.line, isAlloca ? "true" : "false");
Nicolas Capens157ba262019-12-10 17:49:14 -0500335
336 auto value = pending.value;
337
338 IRBuilder::InsertPointGuard guard(*builder);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500339 if(pending.insertAfter != nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -0500340 {
341 builder->SetInsertPoint(pending.block, ++pending.insertAfter->getIterator());
342 }
343 else
344 {
345 builder->SetInsertPoint(pending.block);
346 }
347 builder->SetCurrentDebugLocation(pending.diLocation);
348
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500349 if(!isAlloca)
Nicolas Capens157ba262019-12-10 17:49:14 -0500350 {
351 // While insertDbgValueIntrinsic should be enough to declare a
352 // variable with no storage, variables of RValues can share the same
353 // llvm::Value, and only one can be named. Take for example:
354 //
355 // Int a = 42;
356 // RValue<Int> b = a;
357 // RValue<Int> c = b;
358 //
359 // To handle this, always promote named RValues to an alloca.
360
361 llvm::BasicBlock &entryBlock = function->getEntryBlock();
Daniele Vettorele1930f02022-03-04 15:44:31 +0000362 llvm::AllocaInst *alloca = nullptr;
363 if(entryBlock.getInstList().size() > 0)
364 {
365 alloca = new llvm::AllocaInst(value->getType(), 0, pending.name, &entryBlock.getInstList().front());
366 }
367 else
368 {
369 alloca = new llvm::AllocaInst(value->getType(), 0, pending.name, &entryBlock);
370 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500371 builder->CreateStore(value, alloca);
372 value = alloca;
373 }
374
375 value->setName(pending.name);
376
377 auto diFile = getOrCreateFile(pending.location.function.file.c_str());
378 auto diType = getOrCreateType(value->getType()->getPointerElementType());
379 auto diVar = diBuilder->createAutoVariable(scope.di, pending.name, diFile, pending.location.line, diType);
380
381 auto di = diBuilder->insertDeclare(value, diVar, diBuilder->createExpression(), pending.diLocation, pending.block);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500382 if(pending.insertAfter != nullptr) { di->moveAfter(pending.insertAfter); }
Nicolas Capens157ba262019-12-10 17:49:14 -0500383
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500384 if(pending.addNopOnNextLine)
Nicolas Capens157ba262019-12-10 17:49:14 -0500385 {
386 builder->SetCurrentDebugLocation(llvm::DILocation::get(
Ben Clayton713b8d32019-12-17 20:37:56 +0000387 *context,
388 pending.diLocation->getLine() + 1,
389 0,
390 pending.diLocation->getScope(),
391 pending.diLocation->getInlinedAt()));
Nicolas Capens157ba262019-12-10 17:49:14 -0500392 Nop();
393 }
394
395 scope.pending = Pending{};
396}
397
Ben Claytonee18f392020-10-19 16:54:21 -0400398void DebugInfo::NotifyObjectEmitted(uint64_t key, const llvm::object::ObjectFile &obj, const llvm::LoadedObjectInfo &l)
Nicolas Capens157ba262019-12-10 17:49:14 -0500399{
400 std::unique_lock<std::mutex> lock(jitEventListenerMutex);
Ben Claytonee18f392020-10-19 16:54:21 -0400401 jitEventListener->notifyObjectLoaded(key, obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo &>(l));
Nicolas Capens157ba262019-12-10 17:49:14 -0500402}
403
Ben Claytonee18f392020-10-19 16:54:21 -0400404void DebugInfo::NotifyFreeingObject(uint64_t key)
Nicolas Capens157ba262019-12-10 17:49:14 -0500405{
406 std::unique_lock<std::mutex> lock(jitEventListenerMutex);
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400407 jitEventListener->notifyFreeingObject(key);
Nicolas Capens157ba262019-12-10 17:49:14 -0500408}
409
410void DebugInfo::registerBasicTypes()
411{
412 using namespace rr;
413 using namespace llvm;
414
415 auto vec4 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 4));
416 auto vec8 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 8));
417 auto vec16 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 16));
418
Antonio Maiorano4b777772020-06-22 14:55:37 -0400419 diTypes.emplace(T(Bool::type()), diBuilder->createBasicType("Bool", sizeof(bool), dwarf::DW_ATE_boolean));
420 diTypes.emplace(T(Byte::type()), diBuilder->createBasicType("Byte", 8, dwarf::DW_ATE_unsigned_char));
421 diTypes.emplace(T(SByte::type()), diBuilder->createBasicType("SByte", 8, dwarf::DW_ATE_signed_char));
422 diTypes.emplace(T(Short::type()), diBuilder->createBasicType("Short", 16, dwarf::DW_ATE_signed));
423 diTypes.emplace(T(UShort::type()), diBuilder->createBasicType("UShort", 16, dwarf::DW_ATE_unsigned));
424 diTypes.emplace(T(Int::type()), diBuilder->createBasicType("Int", 32, dwarf::DW_ATE_signed));
425 diTypes.emplace(T(UInt::type()), diBuilder->createBasicType("UInt", 32, dwarf::DW_ATE_unsigned));
426 diTypes.emplace(T(Long::type()), diBuilder->createBasicType("Long", 64, dwarf::DW_ATE_signed));
427 diTypes.emplace(T(Half::type()), diBuilder->createBasicType("Half", 16, dwarf::DW_ATE_float));
428 diTypes.emplace(T(Float::type()), diBuilder->createBasicType("Float", 32, dwarf::DW_ATE_float));
Nicolas Capens157ba262019-12-10 17:49:14 -0500429
Antonio Maiorano4b777772020-06-22 14:55:37 -0400430 diTypes.emplace(T(Byte4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 }));
431 diTypes.emplace(T(SByte4::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 }));
432 diTypes.emplace(T(Byte8::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 }));
433 diTypes.emplace(T(SByte8::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 }));
434 diTypes.emplace(T(Byte16::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 }));
435 diTypes.emplace(T(SByte16::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 }));
436 diTypes.emplace(T(Short2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 }));
437 diTypes.emplace(T(UShort2::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 }));
438 diTypes.emplace(T(Short4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 }));
439 diTypes.emplace(T(UShort4::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 }));
440 diTypes.emplace(T(Short8::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 }));
441 diTypes.emplace(T(UShort8::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 }));
442 diTypes.emplace(T(Int2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Int::type())], { vec4 }));
443 diTypes.emplace(T(UInt2::type()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::type())], { vec4 }));
444 diTypes.emplace(T(Int4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Int::type())], { vec4 }));
445 diTypes.emplace(T(UInt4::type()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::type())], { vec4 }));
446 diTypes.emplace(T(Float2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Float::type())], { vec4 }));
447 diTypes.emplace(T(Float4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Float::type())], { vec4 }));
Nicolas Capens157ba262019-12-10 17:49:14 -0500448}
449
Antonio Maioranoaae33732020-02-14 14:52:34 -0500450Location DebugInfo::getCallerLocation() const
Nicolas Capens157ba262019-12-10 17:49:14 -0500451{
Ben Claytonee18f392020-10-19 16:54:21 -0400452 auto backtrace = getCallerBacktrace(1);
453 return backtrace.empty() ? Location{} : backtrace[0];
Nicolas Capens157ba262019-12-10 17:49:14 -0500454}
455
Antonio Maioranoaae33732020-02-14 14:52:34 -0500456Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const
Nicolas Capens157ba262019-12-10 17:49:14 -0500457{
Antonio Maioranoaae33732020-02-14 14:52:34 -0500458 return rr::getCallerBacktrace(limit);
Nicolas Capens157ba262019-12-10 17:49:14 -0500459}
460
Ben Clayton713b8d32019-12-17 20:37:56 +0000461llvm::DIType *DebugInfo::getOrCreateType(llvm::Type *type)
Nicolas Capens157ba262019-12-10 17:49:14 -0500462{
463 auto it = diTypes.find(type);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500464 if(it != diTypes.end()) { return it->second; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500465
466 if(type->isPointerTy())
Ben Claytonac07ed82019-03-26 14:17:41 +0000467 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500468 auto dbgTy = diBuilder->createPointerType(
Ben Clayton713b8d32019-12-17 20:37:56 +0000469 getOrCreateType(type->getPointerElementType()),
470 sizeof(void *) * 8, alignof(void *) * 8);
Nicolas Capens157ba262019-12-10 17:49:14 -0500471 diTypes.emplace(type, dbgTy);
472 return dbgTy;
473 }
474 llvm::errs() << "Unimplemented debug type: " << type << "\n";
475 assert(false);
476 return nullptr;
477}
478
Ben Clayton713b8d32019-12-17 20:37:56 +0000479llvm::DIFile *DebugInfo::getOrCreateFile(const char *path)
Nicolas Capens157ba262019-12-10 17:49:14 -0500480{
481 auto it = diFiles.find(path);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500482 if(it != diFiles.end()) { return it->second; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500483 auto dirAndName = splitPath(path);
484 auto file = diBuilder->createFile(dirAndName.second, dirAndName.first);
485 diFiles.emplace(path, file);
486 return file;
487}
488
Nicolas Capens3d7faaa2022-10-04 14:48:57 -0400489const DebugInfo::LineTokens *DebugInfo::getOrParseFileTokens(const char *path)
Nicolas Capens157ba262019-12-10 17:49:14 -0500490{
491 static std::regex reLocalDecl(
Ben Clayton713b8d32019-12-17 20:37:56 +0000492 "^" // line start
493 "\\s*" // initial whitespace
494 "(?:For\\s*\\(\\s*)?" // optional 'For('
495 "((?:\\w+(?:<[^>]+>)?)(?:::\\w+(?:<[^>]+>)?)*)" // type (match group 1)
496 "\\s+" // whitespace between type and name
497 "(\\w+)" // identifier (match group 2)
498 "\\s*" // whitespace after identifier
499 "(\\[.*\\])?"); // optional array suffix (match group 3)
Nicolas Capens157ba262019-12-10 17:49:14 -0500500
501 auto it = fileTokens.find(path);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500502 if(it != fileTokens.end())
Nicolas Capens157ba262019-12-10 17:49:14 -0500503 {
504 return it->second.get();
Ben Claytonac07ed82019-03-26 14:17:41 +0000505 }
506
Ben Clayton368d39c2020-01-08 23:10:47 +0000507 auto tokens = std::make_unique<LineTokens>();
Nicolas Capens157ba262019-12-10 17:49:14 -0500508
509 std::ifstream file(path);
510 std::string line;
511 int lineCount = 0;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500512 while(std::getline(file, line))
Ben Claytonac07ed82019-03-26 14:17:41 +0000513 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500514 lineCount++;
515 std::smatch match;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500516 if(std::regex_search(line, match, reLocalDecl) && match.size() > 3)
Ben Claytonac07ed82019-03-26 14:17:41 +0000517 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500518 bool isArray = match.str(3) != "";
Ben Clayton713b8d32019-12-17 20:37:56 +0000519 if(!isArray) // Cannot deal with C-arrays of values.
Ben Claytonac07ed82019-03-26 14:17:41 +0000520 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500521 if(match.str(1) == "return")
Ben Claytonac07ed82019-03-26 14:17:41 +0000522 {
Nicolas Capens2e89a702021-01-07 22:25:16 -0500523 (*tokens)[lineCount] = Token{ Token::Return, "" };
Nicolas Capens157ba262019-12-10 17:49:14 -0500524 }
525 else
526 {
Ben Clayton713b8d32019-12-17 20:37:56 +0000527 (*tokens)[lineCount] = Token{ Token::Identifier, match.str(2) };
Ben Claytonac07ed82019-03-26 14:17:41 +0000528 }
529 }
530 }
Ben Claytonac07ed82019-03-26 14:17:41 +0000531 }
532
Nicolas Capens157ba262019-12-10 17:49:14 -0500533 auto out = tokens.get();
534 fileTokens.emplace(path, std::move(tokens));
535 return out;
536}
537
538} // namespace rr
Ben Claytonac07ed82019-03-26 14:17:41 +0000539
Ben Clayton713b8d32019-12-17 20:37:56 +0000540#endif // ENABLE_RR_DEBUG_INFO