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