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