Skip to content

🍒-pick lldb-repro prerequisites #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lldb/include/lldb/API/SBReproducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LLDB_API SBReproducer {
static const char *Capture(const char *path);
static const char *Replay(const char *path, bool skip_version_check = false);
static const char *GetPath();
static bool SetAutoGenerate(bool b);
static bool Generate();
};

Expand Down
9 changes: 9 additions & 0 deletions lldb/include/lldb/Utility/Reproducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ class Generator final {
/// might need to clean up files already written to disk.
void Discard();

/// Enable or disable auto generate.
void SetAutoGenerate(bool b);

/// Return whether auto generate is enabled.
bool IsAutoGenerate() const;

/// Create and register a new provider.
template <typename T> T *Create() {
std::unique_ptr<ProviderBase> provider = llvm::make_unique<T>(m_root);
Expand Down Expand Up @@ -286,6 +292,9 @@ class Generator final {

/// Flag to ensure that we never call both keep and discard.
bool m_done = false;

/// Flag to auto generate a reproducer when it would otherwise be discarded.
bool m_auto_generate = false;
};

class Loader final {
Expand Down
11 changes: 10 additions & 1 deletion lldb/lit/Reproducer/TestDriverOptions.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@
#
# RUN: %lldb --capture --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix NO-WARNING --check-prefix STATUS-CAPTURE
# RUN: %lldb --capture -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix NO-WARNING --check-prefix STATUS-CAPTURE
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix WARNING --check-prefix STATUS-CAPTURE
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix WARNING --check-prefix STATUS-CAPTURE --check-prefix NOAUTOGEN
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' --reproducer-auto-generate 2>&1 | FileCheck %s --check-prefix WARNING2
#
# NO-WARNING-NOT: warning: -capture-path specified without -capture
# WARNING: warning: -capture-path specified without -capture
# WARNING2: warning: -reproducer-auto-generate specified without -capture
# STATUS-CAPTURE: Reproducer is in capture mode.
# NOAUTOGEN-NOT: Auto generate

# Check auto generate.
# RUN: rm -rf %t.repro
# RUN: %lldb --capture --capture-path %t.repro -b --reproducer-auto-generate -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix NO-WARNING --check-prefix AUTOGEN
# RUN: cat %t.repro/index.yaml
# AUTOGEN: Auto generate: on
9 changes: 9 additions & 0 deletions lldb/source/API/SBReproducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ bool SBReproducer::Generate() {
return false;
}

bool SBReproducer::SetAutoGenerate(bool b) {
auto &r = Reproducer::Instance();
if (auto generator = r.GetGenerator()) {
generator->SetAutoGenerate(b);
return true;
}
return false;
}

const char *SBReproducer::GetPath() {
static std::string path;
auto &r = Reproducer::Instance();
Expand Down
12 changes: 12 additions & 0 deletions lldb/source/Commands/CommandObjectReproducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ class CommandObjectReproducerStatus : public CommandObjectParsed {
result.GetOutputStream() << "Reproducer is off.\n";
}

if (r.IsCapturing() || r.IsReplaying()) {
result.GetOutputStream()
<< "Path: " << r.GetReproducerPath().GetPath() << '\n';
}

// Auto generate is hidden unless enabled because this is mostly for
// development and testing.
if (Generator *g = r.GetGenerator()) {
if (g->IsAutoGenerate())
result.GetOutputStream() << "Auto generate: on\n";
}

result.SetStatus(eReturnStatusSuccessFinishResult);
return result.Succeeded();
}
Expand Down
33 changes: 25 additions & 8 deletions lldb/source/Utility/Reproducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ using namespace lldb_private::repro;
using namespace llvm;
using namespace llvm::yaml;

static llvm::Optional<bool> GetEnv(const char *var) {
std::string val = llvm::StringRef(getenv(var)).lower();
if (val == "0" || val == "off")
return false;
if (val == "1" || val == "on")
return true;
return {};
}

Reproducer &Reproducer::Instance() { return *InstanceImpl(); }

llvm::Error Reproducer::Initialize(ReproducerMode mode,
Expand All @@ -27,12 +36,12 @@ llvm::Error Reproducer::Initialize(ReproducerMode mode,

// 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;
if (llvm::Optional<bool> override = GetEnv("LLDB_CAPTURE_REPRODUCER")) {
if (*override)
mode = ReproducerMode::Capture;
else
mode = ReproducerMode::Off;
}
}

switch (mode) {
Expand Down Expand Up @@ -158,8 +167,12 @@ Generator::Generator(FileSpec root) : m_root(MakeAbsolute(std::move(root))) {
}

Generator::~Generator() {
if (!m_done)
Discard();
if (!m_done) {
if (m_auto_generate)
Keep();
else
Discard();
}
}

ProviderBase *Generator::Register(std::unique_ptr<ProviderBase> provider) {
Expand Down Expand Up @@ -190,6 +203,10 @@ void Generator::Discard() {
llvm::sys::fs::remove_directories(m_root.GetPath());
}

void Generator::SetAutoGenerate(bool b) { m_auto_generate = b; }

bool Generator::IsAutoGenerate() const { return m_auto_generate; }

const FileSpec &Generator::GetRoot() const { return m_root; }

void Generator::AddProvidersToIndex() {
Expand Down
8 changes: 8 additions & 0 deletions lldb/tools/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,14 @@ llvm::Optional<int> InitializeReproducer(opt::InputArgList &input_args) {
}

bool capture = input_args.hasArg(OPT_capture);
bool auto_generate = input_args.hasArg(OPT_auto_generate);
auto *capture_path = input_args.getLastArg(OPT_capture_path);

if (auto_generate && !capture) {
WithColor::warning()
<< "-reproducer-auto-generate specified without -capture\n";
}

if (capture || capture_path) {
if (capture_path) {
if (!capture)
Expand All @@ -824,6 +830,8 @@ llvm::Optional<int> InitializeReproducer(opt::InputArgList &input_args) {
return 1;
}
}
if (auto_generate)
SBReproducer::SetAutoGenerate(true);
}

return llvm::None;
Expand Down
2 changes: 2 additions & 0 deletions lldb/tools/driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,7 @@ def replay: Separate<["--", "-"], "replay">,
HelpText<"Tells the debugger to replay a reproducer from <filename>.">;
def skip_version_check: F<"reproducer-skip-version-check">,
HelpText<"Skip the reproducer version check.">;
def auto_generate: F<"reproducer-auto-generate">,
HelpText<"Generate reproducer on exit.">;

def REM : R<["--"], "">;