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 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 19 | # include "LLVMReactor.hpp" |
| 20 | # include "Reactor.hpp" |
Antonio Maiorano | 415d181 | 2020-02-11 16:22:55 -0500 | [diff] [blame] | 21 | # include "Print.hpp" |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 22 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 23 | # include "boost/stacktrace.hpp" |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 24 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 25 | // 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 Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 31 | # 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 36 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 37 | # ifdef _MSC_VER |
| 38 | __pragma(warning(pop)) |
| 39 | # endif |
| 40 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 41 | # include <cctype> |
| 42 | # include <fstream> |
| 43 | # include <mutex> |
| 44 | # include <regex> |
| 45 | # include <sstream> |
| 46 | # include <string> |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 47 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 48 | # if 0 |
| 49 | # define LOG(msg, ...) printf(msg "\n", ##__VA_ARGS__) |
| 50 | # else |
| 51 | # define LOG(msg, ...) |
| 52 | # endif |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 53 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 54 | namespace |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 55 | { |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 56 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 57 | std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path) |
| 58 | { |
| 59 | return llvm::StringRef(path).rsplit('/'); |
| 60 | } |
| 61 | |
| 62 | // Note: createGDBRegistrationListener() returns a pointer to a singleton. |
| 63 | // Nothing is actually created. |
| 64 | auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex |
| 65 | std::mutex jitEventListenerMutex; |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 66 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 67 | } // namespace |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 68 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 69 | namespace rr { |
| 70 | |
| 71 | DebugInfo::DebugInfo( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 72 | llvm::IRBuilder<> *builder, |
| 73 | llvm::LLVMContext *context, |
| 74 | llvm::Module *module, |
| 75 | llvm::Function *function) |
| 76 | : builder(builder) |
| 77 | , context(context) |
| 78 | , module(module) |
| 79 | , function(function) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 80 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 81 | using namespace ::llvm; |
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 location = getCallerLocation(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 84 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 85 | auto fileAndDir = splitPath(location.function.file.c_str()); |
| 86 | diBuilder.reset(new llvm::DIBuilder(*module)); |
| 87 | diCU = diBuilder->createCompileUnit( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 88 | llvm::dwarf::DW_LANG_C, |
| 89 | diBuilder->createFile(fileAndDir.first, fileAndDir.second), |
| 90 | "Reactor", |
| 91 | 0, "", 0); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 92 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 93 | registerBasicTypes(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 94 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 95 | SmallVector<Metadata *, 8> EltTys; |
| 96 | auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(EltTys)); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 97 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 98 | auto file = getOrCreateFile(location.function.file.c_str()); |
| 99 | auto sp = diBuilder->createFunction( |
Antonio Maiorano | 7d529ff | 2020-07-20 15:27:02 -0400 | [diff] [blame] | 100 | file, // scope |
| 101 | "ReactorFunction", // function name |
| 102 | "ReactorFunction", // linkage |
| 103 | file, // file |
| 104 | location.line, // line |
| 105 | funcTy, // type |
| 106 | location.line, // scope line |
| 107 | DINode::FlagPrototyped, // flags |
| 108 | DISubprogram::SPFlagDefinition // subprogram flags |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 109 | ); |
| 110 | diSubprogram = sp; |
| 111 | function->setSubprogram(sp); |
| 112 | diRootLocation = DILocation::get(*context, location.line, 0, sp); |
| 113 | builder->SetCurrentDebugLocation(diRootLocation); |
| 114 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 115 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 116 | DebugInfo::~DebugInfo() = default; |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 117 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 118 | void DebugInfo::Finalize() |
| 119 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 120 | while(diScope.size() > 0) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 121 | { |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 122 | emitPending(diScope.back(), builder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 123 | diScope.pop_back(); |
| 124 | } |
| 125 | diBuilder->finalize(); |
| 126 | } |
| 127 | |
| 128 | void DebugInfo::EmitLocation() |
| 129 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 130 | auto const &backtrace = getCallerBacktrace(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 131 | syncScope(backtrace); |
| 132 | builder->SetCurrentDebugLocation(getLocation(backtrace, backtrace.size() - 1)); |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 133 | emitPrintLocation(backtrace); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void DebugInfo::Flush() |
| 137 | { |
| 138 | emitPending(diScope.back(), builder); |
| 139 | } |
| 140 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 141 | void DebugInfo::syncScope(Backtrace const &backtrace) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 142 | { |
Antonio Maiorano | 7d529ff | 2020-07-20 15:27:02 -0400 | [diff] [blame] | 143 | using namespace ::llvm; |
| 144 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 145 | auto shrink = [this](size_t newsize) { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 146 | while(diScope.size() > newsize) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 147 | { |
| 148 | auto &scope = diScope.back(); |
| 149 | LOG("- STACK(%d): di: %p, location: %s:%d", |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 150 | int(diScope.size() - 1), scope.di, |
| 151 | scope.location.function.file.c_str(), |
| 152 | int(scope.location.line)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 153 | emitPending(scope, builder); |
| 154 | diScope.pop_back(); |
| 155 | } |
| 156 | }; |
| 157 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 158 | if(backtrace.size() < diScope.size()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 159 | { |
| 160 | shrink(backtrace.size()); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 163 | for(size_t i = 0; i < diScope.size(); i++) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 164 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 165 | auto &scope = diScope[i]; |
| 166 | auto const &oldLocation = scope.location; |
| 167 | auto const &newLocation = backtrace[i]; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 168 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 169 | if(oldLocation.function != newLocation.function) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 170 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 171 | LOG(" STACK(%d): Changed function %s -> %s", int(i), |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 172 | oldLocation.function.name.c_str(), newLocation.function.name.c_str()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 173 | shrink(i); |
| 174 | break; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 177 | if(oldLocation.line > newLocation.line) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 178 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 179 | // Create a new di block to shadow all the variables in the loop. |
| 180 | auto file = getOrCreateFile(newLocation.function.file.c_str()); |
| 181 | auto di = diBuilder->createLexicalBlock(scope.di, file, newLocation.line, 0); |
| 182 | LOG(" STACK(%d): Jumped backwards %d -> %d. di: %p -> %p", int(i), |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 183 | oldLocation.line, newLocation.line, scope.di, di); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 184 | emitPending(scope, builder); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 185 | scope = { newLocation, di }; |
| 186 | shrink(i + 1); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 187 | break; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 190 | scope.location = newLocation; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 193 | while(backtrace.size() > diScope.size()) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 194 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 195 | auto i = diScope.size(); |
| 196 | auto location = backtrace[i]; |
| 197 | auto file = getOrCreateFile(location.function.file.c_str()); |
| 198 | auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray({})); |
| 199 | |
| 200 | char buf[1024]; |
| 201 | size_t size = sizeof(buf); |
| 202 | int status = 0; |
| 203 | llvm::itaniumDemangle(location.function.name.c_str(), buf, &size, &status); |
| 204 | auto name = "jit!" + (status == 0 ? std::string(buf) : location.function.name); |
| 205 | |
| 206 | auto func = diBuilder->createFunction( |
Antonio Maiorano | 7d529ff | 2020-07-20 15:27:02 -0400 | [diff] [blame] | 207 | file, // scope |
| 208 | name, // function name |
| 209 | "", // linkage |
| 210 | file, // file |
| 211 | location.line, // line |
| 212 | funcTy, // type |
| 213 | location.line, // scope line |
| 214 | DINode::FlagPrototyped, // flags |
| 215 | DISubprogram::SPFlagDefinition // subprogram flags |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 216 | ); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 217 | diScope.push_back({ location, func }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 218 | LOG("+ STACK(%d): di: %p, location: %s:%d", int(i), di, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 219 | location.function.file.c_str(), int(location.line)); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 220 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 221 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 222 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 223 | llvm::DILocation *DebugInfo::getLocation(const Backtrace &backtrace, size_t i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 224 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 225 | if(backtrace.size() == 0) { return nullptr; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 226 | assert(backtrace.size() == diScope.size()); |
| 227 | return llvm::DILocation::get( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 228 | *context, |
| 229 | backtrace[i].line, |
| 230 | 0, |
| 231 | diScope[i].di, |
| 232 | i > 0 ? getLocation(backtrace, i - 1) : diRootLocation); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void DebugInfo::EmitVariable(Value *variable) |
| 236 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 237 | auto const &backtrace = getCallerBacktrace(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 238 | syncScope(backtrace); |
| 239 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 240 | for(int i = backtrace.size() - 1; i >= 0; i--) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 241 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 242 | auto const &location = backtrace[i]; |
| 243 | auto tokens = getOrParseFileTokens(location.function.file.c_str()); |
| 244 | auto tokIt = tokens->find(location.line); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 245 | if(tokIt == tokens->end()) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 246 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 247 | break; |
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 | auto token = tokIt->second; |
| 250 | auto name = token.identifier; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 251 | if(token.kind == Token::Return) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 252 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 253 | // This is a: |
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 | // return <expr>; |
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 | // Emit this expression as two variables - |
| 258 | // Once as a synthetic 'return_value' variable at this scope. |
| 259 | // Again by bubbling the expression value up the callstack as |
| 260 | // Return Value Optimizations (RVOs) are likely to carry across |
| 261 | // the value to a local without calling a constructor in |
| 262 | // statements like: |
| 263 | // |
| 264 | // auto val = foo(); |
| 265 | // |
| 266 | name = "return_value"; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 269 | auto &scope = diScope[i]; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 270 | if(scope.pending.location != location) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 271 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 272 | emitPending(scope, builder); |
| 273 | } |
| 274 | |
| 275 | auto value = V(variable); |
| 276 | auto block = builder->GetInsertBlock(); |
| 277 | |
| 278 | auto insertAfter = block->size() > 0 ? &block->back() : nullptr; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 279 | while(insertAfter != nullptr && insertAfter->isTerminator()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 280 | { |
| 281 | insertAfter = insertAfter->getPrevNode(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | scope.pending = Pending{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 285 | scope.pending.name = name; |
| 286 | scope.pending.location = location; |
| 287 | scope.pending.diLocation = getLocation(backtrace, i); |
| 288 | scope.pending.value = value; |
| 289 | scope.pending.block = block; |
| 290 | scope.pending.insertAfter = insertAfter; |
| 291 | scope.pending.scope = scope.di; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 292 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 293 | if(token.kind == Token::Return) |
Antonio Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 294 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 295 | // Insert a noop instruction so the debugger can inspect the |
| 296 | // return value before the function scope closes. |
| 297 | scope.pending.addNopOnNextLine = true; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 305 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 306 | void DebugInfo::emitPending(Scope &scope, IRBuilder *builder) |
| 307 | { |
| 308 | auto const &pending = scope.pending; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 309 | if(pending.value == nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 310 | { |
| 311 | return; |
| 312 | } |
Antonio Maiorano | f448d8e | 2019-04-26 16:19:16 -0400 | [diff] [blame] | 313 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 314 | if(!scope.symbols.emplace(pending.name).second) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 315 | { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | bool isAlloca = llvm::isa<llvm::AllocaInst>(pending.value); |
| 320 | |
| 321 | LOG(" EMIT(%s): di: %p, location: %s:%d, isAlloca: %s", pending.name.c_str(), scope.di, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 322 | pending.location.function.file.c_str(), pending.location.line, isAlloca ? "true" : "false"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 323 | |
| 324 | auto value = pending.value; |
| 325 | |
| 326 | IRBuilder::InsertPointGuard guard(*builder); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 327 | if(pending.insertAfter != nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 328 | { |
| 329 | builder->SetInsertPoint(pending.block, ++pending.insertAfter->getIterator()); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | builder->SetInsertPoint(pending.block); |
| 334 | } |
| 335 | builder->SetCurrentDebugLocation(pending.diLocation); |
| 336 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 337 | if(!isAlloca) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 338 | { |
| 339 | // While insertDbgValueIntrinsic should be enough to declare a |
| 340 | // variable with no storage, variables of RValues can share the same |
| 341 | // llvm::Value, and only one can be named. Take for example: |
| 342 | // |
| 343 | // Int a = 42; |
| 344 | // RValue<Int> b = a; |
| 345 | // RValue<Int> c = b; |
| 346 | // |
| 347 | // To handle this, always promote named RValues to an alloca. |
| 348 | |
| 349 | llvm::BasicBlock &entryBlock = function->getEntryBlock(); |
| 350 | auto alloca = new llvm::AllocaInst(value->getType(), 0, pending.name); |
| 351 | entryBlock.getInstList().push_front(alloca); |
| 352 | builder->CreateStore(value, alloca); |
| 353 | value = alloca; |
| 354 | } |
| 355 | |
| 356 | value->setName(pending.name); |
| 357 | |
| 358 | auto diFile = getOrCreateFile(pending.location.function.file.c_str()); |
| 359 | auto diType = getOrCreateType(value->getType()->getPointerElementType()); |
| 360 | auto diVar = diBuilder->createAutoVariable(scope.di, pending.name, diFile, pending.location.line, diType); |
| 361 | |
| 362 | auto di = diBuilder->insertDeclare(value, diVar, diBuilder->createExpression(), pending.diLocation, pending.block); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 363 | if(pending.insertAfter != nullptr) { di->moveAfter(pending.insertAfter); } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 364 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 365 | if(pending.addNopOnNextLine) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 366 | { |
| 367 | builder->SetCurrentDebugLocation(llvm::DILocation::get( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 368 | *context, |
| 369 | pending.diLocation->getLine() + 1, |
| 370 | 0, |
| 371 | pending.diLocation->getScope(), |
| 372 | pending.diLocation->getInlinedAt())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 373 | Nop(); |
| 374 | } |
| 375 | |
| 376 | scope.pending = Pending{}; |
| 377 | } |
| 378 | |
| 379 | void DebugInfo::NotifyObjectEmitted(const llvm::object::ObjectFile &Obj, const llvm::LoadedObjectInfo &L) |
| 380 | { |
| 381 | std::unique_lock<std::mutex> lock(jitEventListenerMutex); |
Antonio Maiorano | 7d529ff | 2020-07-20 15:27:02 -0400 | [diff] [blame] | 382 | auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj); |
| 383 | jitEventListener->notifyObjectLoaded(key, Obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo &>(L)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void DebugInfo::NotifyFreeingObject(const llvm::object::ObjectFile &Obj) |
| 387 | { |
| 388 | std::unique_lock<std::mutex> lock(jitEventListenerMutex); |
Antonio Maiorano | 7d529ff | 2020-07-20 15:27:02 -0400 | [diff] [blame] | 389 | auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj); |
| 390 | jitEventListener->notifyFreeingObject(key); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void DebugInfo::registerBasicTypes() |
| 394 | { |
| 395 | using namespace rr; |
| 396 | using namespace llvm; |
| 397 | |
| 398 | auto vec4 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 4)); |
| 399 | auto vec8 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 8)); |
| 400 | auto vec16 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 16)); |
| 401 | |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 402 | diTypes.emplace(T(Bool::type()), diBuilder->createBasicType("Bool", sizeof(bool), dwarf::DW_ATE_boolean)); |
| 403 | diTypes.emplace(T(Byte::type()), diBuilder->createBasicType("Byte", 8, dwarf::DW_ATE_unsigned_char)); |
| 404 | diTypes.emplace(T(SByte::type()), diBuilder->createBasicType("SByte", 8, dwarf::DW_ATE_signed_char)); |
| 405 | diTypes.emplace(T(Short::type()), diBuilder->createBasicType("Short", 16, dwarf::DW_ATE_signed)); |
| 406 | diTypes.emplace(T(UShort::type()), diBuilder->createBasicType("UShort", 16, dwarf::DW_ATE_unsigned)); |
| 407 | diTypes.emplace(T(Int::type()), diBuilder->createBasicType("Int", 32, dwarf::DW_ATE_signed)); |
| 408 | diTypes.emplace(T(UInt::type()), diBuilder->createBasicType("UInt", 32, dwarf::DW_ATE_unsigned)); |
| 409 | diTypes.emplace(T(Long::type()), diBuilder->createBasicType("Long", 64, dwarf::DW_ATE_signed)); |
| 410 | diTypes.emplace(T(Half::type()), diBuilder->createBasicType("Half", 16, dwarf::DW_ATE_float)); |
| 411 | diTypes.emplace(T(Float::type()), diBuilder->createBasicType("Float", 32, dwarf::DW_ATE_float)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 412 | |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 413 | diTypes.emplace(T(Byte4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 })); |
| 414 | diTypes.emplace(T(SByte4::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 })); |
| 415 | diTypes.emplace(T(Byte8::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 })); |
| 416 | diTypes.emplace(T(SByte8::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 })); |
| 417 | diTypes.emplace(T(Byte16::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 })); |
| 418 | diTypes.emplace(T(SByte16::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 })); |
| 419 | diTypes.emplace(T(Short2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 })); |
| 420 | diTypes.emplace(T(UShort2::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 })); |
| 421 | diTypes.emplace(T(Short4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 })); |
| 422 | diTypes.emplace(T(UShort4::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 })); |
| 423 | diTypes.emplace(T(Short8::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 })); |
| 424 | diTypes.emplace(T(UShort8::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 })); |
| 425 | diTypes.emplace(T(Int2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Int::type())], { vec4 })); |
| 426 | diTypes.emplace(T(UInt2::type()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::type())], { vec4 })); |
| 427 | diTypes.emplace(T(Int4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Int::type())], { vec4 })); |
| 428 | diTypes.emplace(T(UInt4::type()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::type())], { vec4 })); |
| 429 | diTypes.emplace(T(Float2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Float::type())], { vec4 })); |
| 430 | diTypes.emplace(T(Float4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Float::type())], { vec4 })); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 431 | } |
| 432 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 433 | Location DebugInfo::getCallerLocation() const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 434 | { |
| 435 | return getCallerBacktrace(1)[0]; |
| 436 | } |
| 437 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 438 | Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 439 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 440 | return rr::getCallerBacktrace(limit); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 443 | llvm::DIType *DebugInfo::getOrCreateType(llvm::Type *type) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 444 | { |
| 445 | auto it = diTypes.find(type); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 446 | if(it != diTypes.end()) { return it->second; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 447 | |
| 448 | if(type->isPointerTy()) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 449 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 450 | auto dbgTy = diBuilder->createPointerType( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 451 | getOrCreateType(type->getPointerElementType()), |
| 452 | sizeof(void *) * 8, alignof(void *) * 8); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 453 | diTypes.emplace(type, dbgTy); |
| 454 | return dbgTy; |
| 455 | } |
| 456 | llvm::errs() << "Unimplemented debug type: " << type << "\n"; |
| 457 | assert(false); |
| 458 | return nullptr; |
| 459 | } |
| 460 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 461 | llvm::DIFile *DebugInfo::getOrCreateFile(const char *path) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 462 | { |
| 463 | auto it = diFiles.find(path); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 464 | if(it != diFiles.end()) { return it->second; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 465 | auto dirAndName = splitPath(path); |
| 466 | auto file = diBuilder->createFile(dirAndName.second, dirAndName.first); |
| 467 | diFiles.emplace(path, file); |
| 468 | return file; |
| 469 | } |
| 470 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 471 | DebugInfo::LineTokens const *DebugInfo::getOrParseFileTokens(const char *path) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 472 | { |
| 473 | static std::regex reLocalDecl( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 474 | "^" // line start |
| 475 | "\\s*" // initial whitespace |
| 476 | "(?:For\\s*\\(\\s*)?" // optional 'For(' |
| 477 | "((?:\\w+(?:<[^>]+>)?)(?:::\\w+(?:<[^>]+>)?)*)" // type (match group 1) |
| 478 | "\\s+" // whitespace between type and name |
| 479 | "(\\w+)" // identifier (match group 2) |
| 480 | "\\s*" // whitespace after identifier |
| 481 | "(\\[.*\\])?"); // optional array suffix (match group 3) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 482 | |
| 483 | auto it = fileTokens.find(path); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 484 | if(it != fileTokens.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 485 | { |
| 486 | return it->second.get(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Ben Clayton | 368d39c | 2020-01-08 23:10:47 +0000 | [diff] [blame] | 489 | auto tokens = std::make_unique<LineTokens>(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 490 | |
| 491 | std::ifstream file(path); |
| 492 | std::string line; |
| 493 | int lineCount = 0; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 494 | while(std::getline(file, line)) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 495 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 496 | lineCount++; |
| 497 | std::smatch match; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 498 | if(std::regex_search(line, match, reLocalDecl) && match.size() > 3) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 499 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 500 | bool isArray = match.str(3) != ""; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 501 | if(!isArray) // Cannot deal with C-arrays of values. |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 502 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 503 | if(match.str(1) == "return") |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 504 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 505 | (*tokens)[lineCount] = Token{ Token::Return }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 506 | } |
| 507 | else |
| 508 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 509 | (*tokens)[lineCount] = Token{ Token::Identifier, match.str(2) }; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 515 | auto out = tokens.get(); |
| 516 | fileTokens.emplace(path, std::move(tokens)); |
| 517 | return out; |
| 518 | } |
| 519 | |
| 520 | } // namespace rr |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 521 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 522 | #endif // ENABLE_RR_DEBUG_INFO |