From dfe9a7943bf7a926e51b319a2c2f73e4b4b4cf43 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Wed, 4 Dec 2019 16:06:48 -0800 Subject: [PATCH] [lldb/Reproducers] Override capture with LLDB_CAPTURE_REPRODUCER env var Make it possible to override reproducer capture with the LLDB_CAPTURE_REPRODUCER environment variable. The goal of this change is twofold. (1) I want to be able to enable capturing reproducers during regular test runs, both locally and on the bots. To do so I need a way to force capture. I cannot do this through the Python API, because reproducer capture must be enabled *before* the debugger initialized, which happens automatically when doing `import lldb`. (2) I want to provide an escape hatch for when reproducers are enabled by default. Downstream we have reproducer capture enabled by default in the driver. This patch solves both problems by overriding the reproducer mode based on the environment variable. Acceptable values are 0/1 and ON/OFF. --- lldb/source/Utility/Reproducer.cpp | 10 ++++++++++ .../Reproducer/TestCaptureEnvOverride.test | 20 +++++++++++++++++++ lldb/test/Shell/Reproducer/lit.local.cfg | 1 + 3 files changed, 31 insertions(+) create mode 100644 lldb/test/Shell/Reproducer/TestCaptureEnvOverride.test diff --git a/lldb/source/Utility/Reproducer.cpp b/lldb/source/Utility/Reproducer.cpp index e0806f5f5981..8a28e9b13675 100644 --- a/lldb/source/Utility/Reproducer.cpp +++ b/lldb/source/Utility/Reproducer.cpp @@ -25,6 +25,16 @@ llvm::Error Reproducer::Initialize(ReproducerMode mode, lldbassert(!InstanceImpl() && "Already initialized."); InstanceImpl().emplace(); + // The environment can override the capture mode. + if (mode != ReproducerMode::Replay) { + std::string env = + llvm::StringRef(getenv("LLDB_CAPTURE_REPRODUCER")).lower(); + if (env == "0" || env == "off") + mode = ReproducerMode::Off; + else if (env == "1" || env == "on") + mode = ReproducerMode::Capture; + } + switch (mode) { case ReproducerMode::Capture: { if (!root) { diff --git a/lldb/test/Shell/Reproducer/TestCaptureEnvOverride.test b/lldb/test/Shell/Reproducer/TestCaptureEnvOverride.test new file mode 100644 index 000000000000..a8e7bdec250e --- /dev/null +++ b/lldb/test/Shell/Reproducer/TestCaptureEnvOverride.test @@ -0,0 +1,20 @@ +# UNSUPPORTED: system-windows +# This tests the LLDB_CAPTURE_REPRODUCER override. + +# RUN: %lldb -b -o 'reproducer status' --capture --capture-path %t.repro /bin/ls | FileCheck %s --check-prefix CAPTURE +# RUN: %lldb -b -o 'reproducer status' --capture | FileCheck %s --check-prefix CAPTURE + +# RUN: LLDB_CAPTURE_REPRODUCER=1 %lldb -b -o 'reproducer status' | FileCheck %s --check-prefix CAPTURE +# RUN: LLDB_CAPTURE_REPRODUCER=ON %lldb -b -o 'reproducer status' | FileCheck %s --check-prefix CAPTURE +# RUN: LLDB_CAPTURE_REPRODUCER=on %lldb -b -o 'reproducer status' | FileCheck %s --check-prefix CAPTURE + +# RUN: LLDB_CAPTURE_REPRODUCER=0 %lldb -b -o 'reproducer status' --capture --capture-path %t.repro | FileCheck %s --check-prefix OFF +# RUN: LLDB_CAPTURE_REPRODUCER=0 %lldb -b -o 'reproducer status' --capture | FileCheck %s --check-prefix OFF +# RUN: LLDB_CAPTURE_REPRODUCER=OFF %lldb -b -o 'reproducer status' --capture --capture-path %t.repro | FileCheck %s --check-prefix OFF +# RUN: LLDB_CAPTURE_REPRODUCER=off %lldb -b -o 'reproducer status' --capture | FileCheck %s --check-prefix OFF + +# RUN: LLDB_CAPTURE_REPRODUCER=bogus %lldb -b -o 'reproducer status' --capture | FileCheck %s --check-prefix CAPTURE +# RUN: LLDB_CAPTURE_REPRODUCER=bogus %lldb -b -o 'reproducer status' | FileCheck %s --check-prefix OFF + +# CAPTURE: Reproducer is in capture mode. +# OFF: Reproducer is off. diff --git a/lldb/test/Shell/Reproducer/lit.local.cfg b/lldb/test/Shell/Reproducer/lit.local.cfg index 5659f1baa06d..7386b8655d1e 100644 --- a/lldb/test/Shell/Reproducer/lit.local.cfg +++ b/lldb/test/Shell/Reproducer/lit.local.cfg @@ -1,2 +1,3 @@ # Enable crash reports for the reproducer tests. del config.environment['LLVM_DISABLE_CRASH_REPORT'] +del config.environment['LLDB_CAPTURE_REPRODUCER']