Index: llvm/include/llvm/Passes/StandardInstrumentations.h =================================================================== --- llvm/include/llvm/Passes/StandardInstrumentations.h +++ llvm/include/llvm/Passes/StandardInstrumentations.h @@ -15,6 +15,7 @@ #ifndef LLVM_PASSES_STANDARDINSTRUMENTATIONS_H #define LLVM_PASSES_STANDARDINSTRUMENTATIONS_H +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" @@ -409,6 +410,25 @@ void registerCallbacks(PassInstrumentationCallbacks &PIC); }; +// Print IR on crash. +class PrintCrashIRInstrumentation { +public: + PrintCrashIRInstrumentation() + : SavedIR("*** Dump of IR Before Last Pass Unknown ***") {} + ~PrintCrashIRInstrumentation(); + void registerCallbacks(PassInstrumentationCallbacks &PIC); + void reportCrashIR(); + +protected: + std::string SavedIR; + +private: + // All of the crash reporters that will report on a crash. + static DenseSet *CrashReporters; + // Crash handler registered when print-on-crash is specified. + static void SignalHandler(void *); +}; + /// This class provides an interface to register all the standard pass /// instrumentations and manages their state (if any). class StandardInstrumentations { @@ -421,6 +441,7 @@ IRChangedPrinter PrintChangedIR; PseudoProbeVerifier PseudoProbeVerification; InLineChangePrinter PrintChangedDiff; + PrintCrashIRInstrumentation PrintCrashIR; VerifyInstrumentation Verify; bool VerifyEach; Index: llvm/lib/Passes/PassBuilder.cpp =================================================================== --- llvm/lib/Passes/PassBuilder.cpp +++ llvm/lib/Passes/PassBuilder.cpp @@ -442,6 +442,14 @@ return !printBeforePasses().empty() || !printAfterPasses().empty(); } +// A pass for testing -print-on-crash. +// DO NOT USE THIS EXCEPT FOR TESTING! +class TriggerCrashPass : public PassInfoMixin { +public: + struct Result {}; + PreservedAnalyses run(Module &, ModuleAnalysisManager &) { assert(false); } +}; + } // namespace PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, Index: llvm/lib/Passes/PassRegistry.def =================================================================== --- llvm/lib/Passes/PassRegistry.def +++ llvm/lib/Passes/PassRegistry.def @@ -110,6 +110,7 @@ MODULE_PASS("strip-nondebug", StripNonDebugSymbolsPass()) MODULE_PASS("strip-nonlinetable-debuginfo", StripNonLineTableDebugInfoPass()) MODULE_PASS("synthetic-counts-propagation", SyntheticCountsPropagation()) +MODULE_PASS("trigger-crash", TriggerCrashPass()) MODULE_PASS("verify", VerifierPass()) MODULE_PASS("wholeprogramdevirt", WholeProgramDevirtPass()) MODULE_PASS("dfsan", DataFlowSanitizerPass()) Index: llvm/lib/Passes/StandardInstrumentations.cpp =================================================================== --- llvm/lib/Passes/StandardInstrumentations.cpp +++ llvm/lib/Passes/StandardInstrumentations.cpp @@ -15,6 +15,7 @@ #include "llvm/Passes/StandardInstrumentations.h" #include "llvm/ADT/Any.h" #include "llvm/ADT/Optional.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include "llvm/Analysis/CallGraphSCCPass.h" #include "llvm/Analysis/LazyCallGraph.h" @@ -27,10 +28,12 @@ #include "llvm/IR/PrintPasses.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/CrashRecoveryContext.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Program.h" +#include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -119,6 +122,12 @@ DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"), cl::desc("system diff used by change reporters")); +// An option to print the IR that was being processed when a pass crashes. +static cl::opt + PrintCrashIR("print-on-crash", + cl::desc("Print the last form of the IR before crash"), + cl::init(false), cl::Hidden); + namespace { // Perform a system based diff between \p Before and \p After, using @@ -1224,6 +1233,60 @@ PrintChanged == ChangePrinter::PrintChangedColourDiffQuiet), Verify(DebugLogging), VerifyEach(VerifyEach) {} +DenseSet + *PrintCrashIRInstrumentation::CrashReporters = nullptr; + +void PrintCrashIRInstrumentation::reportCrashIR() { dbgs() << SavedIR; } + +void PrintCrashIRInstrumentation::SignalHandler(void *) { + // Called by signal handlers so do not lock here + // Are any of PrintCrashIRInstrumentation objects still alive? + if (!CrashReporters) + return; + + assert(PrintCrashIR && "Did not expect to get here without option set."); + for (auto I : *CrashReporters) + I->reportCrashIR(); +} + +PrintCrashIRInstrumentation::~PrintCrashIRInstrumentation() { + if (!PrintCrashIR) + return; + + assert(CrashReporters && "Expected CrashReporters to be set"); + + // Was this registered? + DenseSet::iterator I = + CrashReporters->find(this); + if (I == CrashReporters->end()) + return; + CrashReporters->erase(I); + if (!CrashReporters->empty()) + return; + delete CrashReporters; + CrashReporters = nullptr; +} + +void PrintCrashIRInstrumentation::registerCallbacks( + PassInstrumentationCallbacks &PIC) { + if (!PrintCrashIR) + return; + + if (!CrashReporters) { + CrashReporters = new DenseSet(); + sys::AddSignalHandler(SignalHandler, nullptr); + } + CrashReporters->insert(this); + + PIC.registerBeforeNonSkippedPassCallback([this](StringRef PassID, Any IR) { + SavedIR.clear(); + raw_string_ostream OS(SavedIR); + OS << formatv("*** Dump of {0}IR Before Last Pass {1} Started ***", + llvm::forcePrintModuleIR() ? "Module " : "", PassID); + unwrapAndPrint(OS, IR); + }); +} + void StandardInstrumentations::registerCallbacks( PassInstrumentationCallbacks &PIC, FunctionAnalysisManager *FAM) { PrintIR.registerCallbacks(PIC); @@ -1238,6 +1301,7 @@ if (VerifyEach) Verify.registerCallbacks(PIC); PrintChangedDiff.registerCallbacks(PIC); + PrintCrashIR.registerCallbacks(PIC); } namespace llvm { Index: llvm/test/Other/print-on-crash.ll =================================================================== --- /dev/null +++ llvm/test/Other/print-on-crash.ll @@ -0,0 +1,27 @@ +; A test that the hidden option -print-on-crash properly sets a signal handler +; which gets called when a pass crashes. The trigger-crash pass asserts. + +; RUN: not --crash opt -print-on-crash -passes=trigger-crash < %s 2>&1 | FileCheck %s --check-prefix=CHECK_SIMPLE + +; REQUIRES: asserts + +; A test that the signal handler set by the hidden option -print-on-crash +; is not called when no pass crashes. + +; RUN: opt -print-on-crash -passes="default" < %s 2>&1 | FileCheck %s --check-prefix=CHECK_NO_CRASH + +; RUN: not --crash opt -print-on-crash -print-module-scope -passes=trigger-crash < %s 2>&1 | FileCheck %s --check-prefix=CHECK_MODULE + +; CHECK_SIMPLE: *** Dump of IR Before Last Pass {{.*}} Started *** +; CHECK_SIMPLE: @main +; CHECK_SIMPLE: entry: +; CHECK_NO_CRASH-NOT: *** Dump of IR +; CHECK_MODULE: *** Dump of Module IR Before Last Pass {{.*}} Started *** +; CHECK_MODULE: ; ModuleID = {{.*}} + +define i32 @main() { +entry: + %retval = alloca i32, align 4 + store i32 0, i32* %retval, align 4 + ret i32 0 +}