1 //===---------- DebugUtils.cpp - Utilities for debugging ORC JITs ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ExecutionEngine/Orc/DebugUtils.h" 10 11 #include "llvm/ExecutionEngine/Orc/Core.h" 12 #include "llvm/Support/CommandLine.h" 13 #include "llvm/Support/Debug.h" 14 #include "llvm/Support/FileSystem.h" 15 #include "llvm/Support/Format.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "llvm/Support/Path.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 #define DEBUG_TYPE "orc" 21 22 using namespace llvm; 23 24 namespace { 25 26 #ifndef NDEBUG 27 28 cl::opt<bool> PrintHidden("debug-orc-print-hidden", cl::init(true), 29 cl::desc("debug print hidden symbols defined by " 30 "materialization units"), 31 cl::Hidden); 32 33 cl::opt<bool> PrintCallable("debug-orc-print-callable", cl::init(true), 34 cl::desc("debug print callable symbols defined by " 35 "materialization units"), 36 cl::Hidden); 37 38 cl::opt<bool> PrintData("debug-orc-print-data", cl::init(true), 39 cl::desc("debug print data symbols defined by " 40 "materialization units"), 41 cl::Hidden); 42 43 #endif // NDEBUG 44 45 // SetPrinter predicate that prints every element. 46 template <typename T> struct PrintAll { 47 bool operator()(const T &E) { return true; } 48 }; 49 50 bool anyPrintSymbolOptionSet() { 51 #ifndef NDEBUG 52 return PrintHidden || PrintCallable || PrintData; 53 #else 54 return false; 55 #endif // NDEBUG 56 } 57 58 bool flagsMatchCLOpts(const JITSymbolFlags &Flags) { 59 #ifndef NDEBUG 60 // Bail out early if this is a hidden symbol and we're not printing hiddens. 61 if (!PrintHidden && !Flags.isExported()) 62 return false; 63 64 // Return true if this is callable and we're printing callables. 65 if (PrintCallable && Flags.isCallable()) 66 return true; 67 68 // Return true if this is data and we're printing data. 69 if (PrintData && !Flags.isCallable()) 70 return true; 71 72 // otherwise return false. 73 return false; 74 #else 75 return false; 76 #endif // NDEBUG 77 } 78 79 // Prints a sequence of items, filtered by an user-supplied predicate. 80 template <typename Sequence, 81 typename Pred = PrintAll<typename Sequence::value_type>> 82 class SequencePrinter { 83 public: 84 SequencePrinter(const Sequence &S, char OpenSeq, char CloseSeq, 85 Pred ShouldPrint = Pred()) 86 : S(S), OpenSeq(OpenSeq), CloseSeq(CloseSeq), 87 ShouldPrint(std::move(ShouldPrint)) {} 88 89 void printTo(llvm::raw_ostream &OS) const { 90 bool PrintComma = false; 91 OS << OpenSeq; 92 for (auto &E : S) { 93 if (ShouldPrint(E)) { 94 if (PrintComma) 95 OS << ','; 96 OS << ' ' << E; 97 PrintComma = true; 98 } 99 } 100 OS << ' ' << CloseSeq; 101 } 102 103 private: 104 const Sequence &S; 105 char OpenSeq; 106 char CloseSeq; 107 mutable Pred ShouldPrint; 108 }; 109 110 template <typename Sequence, typename Pred> 111 SequencePrinter<Sequence, Pred> printSequence(const Sequence &S, char OpenSeq, 112 char CloseSeq, Pred P = Pred()) { 113 return SequencePrinter<Sequence, Pred>(S, OpenSeq, CloseSeq, std::move(P)); 114 } 115 116 // Render a SequencePrinter by delegating to its printTo method. 117 template <typename Sequence, typename Pred> 118 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, 119 const SequencePrinter<Sequence, Pred> &Printer) { 120 Printer.printTo(OS); 121 return OS; 122 } 123 124 struct PrintSymbolFlagsMapElemsMatchingCLOpts { 125 bool operator()(const orc::SymbolFlagsMap::value_type &KV) { 126 return flagsMatchCLOpts(KV.second); 127 } 128 }; 129 130 struct PrintSymbolMapElemsMatchingCLOpts { 131 bool operator()(const orc::SymbolMap::value_type &KV) { 132 return flagsMatchCLOpts(KV.second.getFlags()); 133 } 134 }; 135 136 } // end anonymous namespace 137 138 namespace llvm { 139 namespace orc { 140 141 raw_ostream &operator<<(raw_ostream &OS, const SymbolStringPtr &Sym) { 142 return OS << *Sym; 143 } 144 145 raw_ostream &operator<<(raw_ostream &OS, const SymbolNameSet &Symbols) { 146 return OS << printSequence(Symbols, '{', '}', PrintAll<SymbolStringPtr>()); 147 } 148 149 raw_ostream &operator<<(raw_ostream &OS, const SymbolNameVector &Symbols) { 150 return OS << printSequence(Symbols, '[', ']', PrintAll<SymbolStringPtr>()); 151 } 152 153 raw_ostream &operator<<(raw_ostream &OS, ArrayRef<SymbolStringPtr> Symbols) { 154 return OS << printSequence(Symbols, '[', ']', PrintAll<SymbolStringPtr>()); 155 } 156 157 raw_ostream &operator<<(raw_ostream &OS, const JITSymbolFlags &Flags) { 158 if (Flags.hasError()) 159 OS << "[*ERROR*]"; 160 if (Flags.isCallable()) 161 OS << "[Callable]"; 162 else 163 OS << "[Data]"; 164 if (Flags.isWeak()) 165 OS << "[Weak]"; 166 else if (Flags.isCommon()) 167 OS << "[Common]"; 168 169 if (!Flags.isExported()) 170 OS << "[Hidden]"; 171 172 return OS; 173 } 174 175 raw_ostream &operator<<(raw_ostream &OS, const JITEvaluatedSymbol &Sym) { 176 return OS << format("0x%016" PRIx64, Sym.getAddress()) << " " 177 << Sym.getFlags(); 178 } 179 180 raw_ostream &operator<<(raw_ostream &OS, const SymbolFlagsMap::value_type &KV) { 181 return OS << "(\"" << KV.first << "\", " << KV.second << ")"; 182 } 183 184 raw_ostream &operator<<(raw_ostream &OS, const SymbolMap::value_type &KV) { 185 return OS << "(\"" << KV.first << "\": " << KV.second << ")"; 186 } 187 188 raw_ostream &operator<<(raw_ostream &OS, const SymbolFlagsMap &SymbolFlags) { 189 return OS << printSequence(SymbolFlags, '{', '}', 190 PrintSymbolFlagsMapElemsMatchingCLOpts()); 191 } 192 193 raw_ostream &operator<<(raw_ostream &OS, const SymbolMap &Symbols) { 194 return OS << printSequence(Symbols, '{', '}', 195 PrintSymbolMapElemsMatchingCLOpts()); 196 } 197 198 raw_ostream &operator<<(raw_ostream &OS, 199 const SymbolDependenceMap::value_type &KV) { 200 return OS << "(" << KV.first->getName() << ", " << KV.second << ")"; 201 } 202 203 raw_ostream &operator<<(raw_ostream &OS, const SymbolDependenceMap &Deps) { 204 return OS << printSequence(Deps, '{', '}', 205 PrintAll<SymbolDependenceMap::value_type>()); 206 } 207 208 raw_ostream &operator<<(raw_ostream &OS, const MaterializationUnit &MU) { 209 OS << "MU@" << &MU << " (\"" << MU.getName() << "\""; 210 if (anyPrintSymbolOptionSet()) 211 OS << ", " << MU.getSymbols(); 212 return OS << ")"; 213 } 214 215 raw_ostream &operator<<(raw_ostream &OS, const LookupKind &K) { 216 switch (K) { 217 case LookupKind::Static: 218 return OS << "Static"; 219 case LookupKind::DLSym: 220 return OS << "DLSym"; 221 } 222 llvm_unreachable("Invalid lookup kind"); 223 } 224 225 raw_ostream &operator<<(raw_ostream &OS, 226 const JITDylibLookupFlags &JDLookupFlags) { 227 switch (JDLookupFlags) { 228 case JITDylibLookupFlags::MatchExportedSymbolsOnly: 229 return OS << "MatchExportedSymbolsOnly"; 230 case JITDylibLookupFlags::MatchAllSymbols: 231 return OS << "MatchAllSymbols"; 232 } 233 llvm_unreachable("Invalid JITDylib lookup flags"); 234 } 235 236 raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupFlags &LookupFlags) { 237 switch (LookupFlags) { 238 case SymbolLookupFlags::RequiredSymbol: 239 return OS << "RequiredSymbol"; 240 case SymbolLookupFlags::WeaklyReferencedSymbol: 241 return OS << "WeaklyReferencedSymbol"; 242 } 243 llvm_unreachable("Invalid symbol lookup flags"); 244 } 245 246 raw_ostream &operator<<(raw_ostream &OS, 247 const SymbolLookupSet::value_type &KV) { 248 return OS << "(" << KV.first << ", " << KV.second << ")"; 249 } 250 251 raw_ostream &operator<<(raw_ostream &OS, const SymbolLookupSet &LookupSet) { 252 return OS << printSequence(LookupSet, '{', '}', 253 PrintAll<SymbolLookupSet::value_type>()); 254 } 255 256 raw_ostream &operator<<(raw_ostream &OS, 257 const JITDylibSearchOrder &SearchOrder) { 258 OS << "["; 259 if (!SearchOrder.empty()) { 260 assert(SearchOrder.front().first && 261 "JITDylibList entries must not be null"); 262 OS << " (\"" << SearchOrder.front().first->getName() << "\", " 263 << SearchOrder.begin()->second << ")"; 264 for (auto &KV : 265 make_range(std::next(SearchOrder.begin(), 1), SearchOrder.end())) { 266 assert(KV.first && "JITDylibList entries must not be null"); 267 OS << ", (\"" << KV.first->getName() << "\", " << KV.second << ")"; 268 } 269 } 270 OS << " ]"; 271 return OS; 272 } 273 274 raw_ostream &operator<<(raw_ostream &OS, const SymbolAliasMap &Aliases) { 275 OS << "{"; 276 for (auto &KV : Aliases) 277 OS << " " << *KV.first << ": " << KV.second.Aliasee << " " 278 << KV.second.AliasFlags; 279 OS << " }"; 280 return OS; 281 } 282 283 raw_ostream &operator<<(raw_ostream &OS, const SymbolState &S) { 284 switch (S) { 285 case SymbolState::Invalid: 286 return OS << "Invalid"; 287 case SymbolState::NeverSearched: 288 return OS << "Never-Searched"; 289 case SymbolState::Materializing: 290 return OS << "Materializing"; 291 case SymbolState::Resolved: 292 return OS << "Resolved"; 293 case SymbolState::Emitted: 294 return OS << "Emitted"; 295 case SymbolState::Ready: 296 return OS << "Ready"; 297 } 298 llvm_unreachable("Invalid state"); 299 } 300 301 DumpObjects::DumpObjects(std::string DumpDir, std::string IdentifierOverride) 302 : DumpDir(std::move(DumpDir)), 303 IdentifierOverride(std::move(IdentifierOverride)) { 304 305 /// Discard any trailing separators. 306 while (!this->DumpDir.empty() && 307 sys::path::is_separator(this->DumpDir.back())) 308 this->DumpDir.pop_back(); 309 } 310 311 Expected<std::unique_ptr<MemoryBuffer>> 312 DumpObjects::operator()(std::unique_ptr<MemoryBuffer> Obj) { 313 size_t Idx = 1; 314 315 std::string DumpPathStem; 316 raw_string_ostream(DumpPathStem) 317 << DumpDir << (DumpDir.empty() ? "" : "/") << getBufferIdentifier(*Obj); 318 319 std::string DumpPath = DumpPathStem + ".o"; 320 while (sys::fs::exists(DumpPath)) { 321 DumpPath.clear(); 322 raw_string_ostream(DumpPath) << DumpPathStem << "." << (++Idx) << ".o"; 323 } 324 325 LLVM_DEBUG({ 326 dbgs() << "Dumping object buffer [ " << (const void *)Obj->getBufferStart() 327 << " -- " << (const void *)(Obj->getBufferEnd() - 1) << " ] to " 328 << DumpPath << "\n"; 329 }); 330 331 std::error_code EC; 332 raw_fd_ostream DumpStream(DumpPath, EC); 333 if (EC) 334 return errorCodeToError(EC); 335 DumpStream.write(Obj->getBufferStart(), Obj->getBufferSize()); 336 337 return std::move(Obj); 338 } 339 340 StringRef DumpObjects::getBufferIdentifier(MemoryBuffer &B) { 341 if (!IdentifierOverride.empty()) 342 return IdentifierOverride; 343 StringRef Identifier = B.getBufferIdentifier(); 344 Identifier.consume_back(".o"); 345 return Identifier; 346 } 347 348 } // End namespace orc. 349 } // End namespace llvm. 350