Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1 | // 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 Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 22 | #include "boost/stacktrace.hpp" |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 23 | |
| 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 Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 32 | #include <mutex> |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 33 | #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 Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 43 | namespace { |
| 44 | |
| 45 | std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char* path) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 46 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 47 | return llvm::StringRef(path).rsplit('/'); |
| 48 | } |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 49 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 50 | // Note: createGDBRegistrationListener() returns a pointer to a singleton. |
| 51 | // Nothing is actually created. |
| 52 | auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex |
| 53 | std::mutex jitEventListenerMutex; |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 54 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 55 | } // anonymous namespaces |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 56 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 57 | namespace rr { |
| 58 | |
| 59 | DebugInfo::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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 65 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 66 | using namespace ::llvm; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 67 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 68 | auto location = getCallerLocation(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 69 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 70 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 77 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 78 | registerBasicTypes(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 79 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 80 | SmallVector<Metadata *, 8> EltTys; |
| 81 | auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(EltTys)); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 82 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 83 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 103 | DebugInfo::~DebugInfo() = default; |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 104 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 105 | void DebugInfo::Finalize() |
| 106 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 107 | while(diScope.size() > 0) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 108 | { |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 109 | emitPending(diScope.back(), builder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 110 | diScope.pop_back(); |
| 111 | } |
| 112 | diBuilder->finalize(); |
| 113 | } |
| 114 | |
| 115 | void 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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 123 | if(backtrace.size() == 0) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 124 | { |
| 125 | return; |
| 126 | } |
| 127 | Location currLocation = backtrace[backtrace.size() - 1]; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 128 | if(currLocation != lastLocation) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 129 | { |
| 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 | |
| 136 | void DebugInfo::Flush() |
| 137 | { |
| 138 | emitPending(diScope.back(), builder); |
| 139 | } |
| 140 | |
| 141 | void DebugInfo::syncScope(Backtrace const& backtrace) |
| 142 | { |
| 143 | auto shrink = [this](size_t newsize) |
| 144 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 145 | while(diScope.size() > newsize) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 146 | { |
| 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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 157 | if(backtrace.size() < diScope.size()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 158 | { |
| 159 | shrink(backtrace.size()); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 162 | for(size_t i = 0; i < diScope.size(); i++) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 163 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 164 | auto &scope = diScope[i]; |
| 165 | auto const &oldLocation = scope.location; |
| 166 | auto const &newLocation = backtrace[i]; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 167 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 168 | if(oldLocation.function != newLocation.function) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 169 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 170 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 176 | if(oldLocation.line > newLocation.line) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 177 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 178 | // 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 189 | scope.location = newLocation; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 192 | while(backtrace.size() > diScope.size()) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 193 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 194 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 217 | ); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 218 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 221 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 222 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 223 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 224 | llvm::DILocation* DebugInfo::getLocation(const Backtrace &backtrace, size_t i) |
| 225 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 226 | if(backtrace.size() == 0) { return nullptr; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 227 | 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 | |
| 237 | void DebugInfo::EmitVariable(Value *variable) |
| 238 | { |
| 239 | auto const& backtrace = getCallerBacktrace(); |
| 240 | syncScope(backtrace); |
| 241 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 242 | for(int i = backtrace.size() - 1; i >= 0; i--) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 243 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 244 | auto const &location = backtrace[i]; |
| 245 | auto tokens = getOrParseFileTokens(location.function.file.c_str()); |
| 246 | auto tokIt = tokens->find(location.line); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 247 | if(tokIt == tokens->end()) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 248 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 249 | break; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 250 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 251 | auto token = tokIt->second; |
| 252 | auto name = token.identifier; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 253 | if(token.kind == Token::Return) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 254 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 255 | // This is a: |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 256 | // |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 257 | // return <expr>; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 258 | // |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 259 | // 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 271 | auto &scope = diScope[i]; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 272 | if(scope.pending.location != location) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 273 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 274 | 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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 281 | while(insertAfter != nullptr && insertAfter->isTerminator()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 282 | { |
| 283 | insertAfter = insertAfter->getPrevNode(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | scope.pending = Pending{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 287 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 294 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 295 | if(token.kind == Token::Return) |
Antonio Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 296 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 297 | // 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 307 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 308 | void DebugInfo::emitPending(Scope &scope, IRBuilder *builder) |
| 309 | { |
| 310 | auto const &pending = scope.pending; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 311 | if(pending.value == nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 312 | { |
| 313 | return; |
| 314 | } |
Antonio Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 315 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 316 | if(!scope.symbols.emplace(pending.name).second) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 317 | { |
| 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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 329 | if(pending.insertAfter != nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 330 | { |
| 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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 339 | if(!isAlloca) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 340 | { |
| 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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 365 | if(pending.insertAfter != nullptr) { di->moveAfter(pending.insertAfter); } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 366 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 367 | if(pending.addNopOnNextLine) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 368 | { |
| 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 | |
| 382 | void 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 | |
| 388 | void DebugInfo::NotifyFreeingObject(const llvm::object::ObjectFile &Obj) |
| 389 | { |
| 390 | std::unique_lock<std::mutex> lock(jitEventListenerMutex); |
| 391 | jitEventListener->NotifyFreeingObject(Obj); |
| 392 | } |
| 393 | |
| 394 | void 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 | |
| 434 | DebugInfo::Location DebugInfo::getCallerLocation() const |
| 435 | { |
| 436 | return getCallerBacktrace(1)[0]; |
| 437 | } |
| 438 | |
| 439 | DebugInfo::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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 454 | for(bs::frame frame : bs::stacktrace()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 455 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 456 | if(shouldSkipFile(frame.source_file())) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 457 | { |
| 458 | continue; |
Antonio Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 461 | 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 Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 466 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 467 | if(limit > 0 && locations.size() >= limit) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 468 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 469 | break; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 470 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 473 | std::reverse(locations.begin(), locations.end()); |
| 474 | |
| 475 | return locations; |
| 476 | } |
| 477 | |
| 478 | llvm::DIType *DebugInfo::getOrCreateType(llvm::Type* type) |
| 479 | { |
| 480 | auto it = diTypes.find(type); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 481 | if(it != diTypes.end()) { return it->second; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 482 | |
| 483 | if(type->isPointerTy()) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 484 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 485 | 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 | |
| 496 | llvm::DIFile *DebugInfo::getOrCreateFile(const char* path) |
| 497 | { |
| 498 | auto it = diFiles.find(path); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 499 | if(it != diFiles.end()) { return it->second; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 500 | auto dirAndName = splitPath(path); |
| 501 | auto file = diBuilder->createFile(dirAndName.second, dirAndName.first); |
| 502 | diFiles.emplace(path, file); |
| 503 | return file; |
| 504 | } |
| 505 | |
| 506 | DebugInfo::LineTokens const *DebugInfo::getOrParseFileTokens(const char* path) |
| 507 | { |
| 508 | static std::regex reLocalDecl( |
| 509 | "^" // line start |
| 510 | "\\s*" // initial whitespace |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 511 | "(?:For\\s*\\(\\s*)?" // optional 'For(' |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 512 | "((?:\\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 Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 519 | if(it != fileTokens.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | { |
| 521 | return it->second.get(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 524 | auto tokens = std::unique_ptr<LineTokens>(new LineTokens()); |
| 525 | |
| 526 | std::ifstream file(path); |
| 527 | std::string line; |
| 528 | int lineCount = 0; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 529 | while(std::getline(file, line)) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 530 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 531 | lineCount++; |
| 532 | std::smatch match; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 533 | if(std::regex_search(line, match, reLocalDecl) && match.size() > 3) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 534 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 535 | bool isArray = match.str(3) != ""; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 536 | if(!isArray) // Cannot deal with C-arrays of values. |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 537 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame^] | 538 | if(match.str(1) == "return") |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 539 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 540 | (*tokens)[lineCount] = Token{Token::Return}; |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | (*tokens)[lineCount] = Token{Token::Identifier, match.str(2)}; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 550 | auto out = tokens.get(); |
| 551 | fileTokens.emplace(path, std::move(tokens)); |
| 552 | return out; |
| 553 | } |
| 554 | |
| 555 | } // namespace rr |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 556 | |
| 557 | #endif // ENABLE_RR_DEBUG_INFO |