Skip to content

Commit 1bea469

Browse files
authored
Merge pull request #636 from JDevlieghere/🍒/lldb-repro-prereqs
🍒-pick lldb-repro prerequisites
2 parents 15e8953 + d52d3ad commit 1bea469

File tree

8 files changed

+76
-9
lines changed

8 files changed

+76
-9
lines changed

lldb/include/lldb/API/SBReproducer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class LLDB_API SBReproducer {
2222
static const char *Capture(const char *path);
2323
static const char *Replay(const char *path, bool skip_version_check = false);
2424
static const char *GetPath();
25+
static bool SetAutoGenerate(bool b);
2526
static bool Generate();
2627
};
2728

lldb/include/lldb/Utility/Reproducer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ class Generator final {
245245
/// might need to clean up files already written to disk.
246246
void Discard();
247247

248+
/// Enable or disable auto generate.
249+
void SetAutoGenerate(bool b);
250+
251+
/// Return whether auto generate is enabled.
252+
bool IsAutoGenerate() const;
253+
248254
/// Create and register a new provider.
249255
template <typename T> T *Create() {
250256
std::unique_ptr<ProviderBase> provider = llvm::make_unique<T>(m_root);
@@ -286,6 +292,9 @@ class Generator final {
286292

287293
/// Flag to ensure that we never call both keep and discard.
288294
bool m_done = false;
295+
296+
/// Flag to auto generate a reproducer when it would otherwise be discarded.
297+
bool m_auto_generate = false;
289298
};
290299

291300
class Loader final {

lldb/lit/Reproducer/TestDriverOptions.test

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
#
1111
# RUN: %lldb --capture --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix NO-WARNING --check-prefix STATUS-CAPTURE
1212
# RUN: %lldb --capture -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix NO-WARNING --check-prefix STATUS-CAPTURE
13-
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix WARNING --check-prefix STATUS-CAPTURE
13+
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix WARNING --check-prefix STATUS-CAPTURE --check-prefix NOAUTOGEN
14+
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' --reproducer-auto-generate 2>&1 | FileCheck %s --check-prefix WARNING2
1415
#
1516
# NO-WARNING-NOT: warning: -capture-path specified without -capture
1617
# WARNING: warning: -capture-path specified without -capture
18+
# WARNING2: warning: -reproducer-auto-generate specified without -capture
1719
# STATUS-CAPTURE: Reproducer is in capture mode.
20+
# NOAUTOGEN-NOT: Auto generate
21+
22+
# Check auto generate.
23+
# RUN: rm -rf %t.repro
24+
# 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
25+
# RUN: cat %t.repro/index.yaml
26+
# AUTOGEN: Auto generate: on

lldb/source/API/SBReproducer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ bool SBReproducer::Generate() {
173173
return false;
174174
}
175175

176+
bool SBReproducer::SetAutoGenerate(bool b) {
177+
auto &r = Reproducer::Instance();
178+
if (auto generator = r.GetGenerator()) {
179+
generator->SetAutoGenerate(b);
180+
return true;
181+
}
182+
return false;
183+
}
184+
176185
const char *SBReproducer::GetPath() {
177186
static std::string path;
178187
auto &r = Reproducer::Instance();

lldb/source/Commands/CommandObjectReproducer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ class CommandObjectReproducerStatus : public CommandObjectParsed {
259259
result.GetOutputStream() << "Reproducer is off.\n";
260260
}
261261

262+
if (r.IsCapturing() || r.IsReplaying()) {
263+
result.GetOutputStream()
264+
<< "Path: " << r.GetReproducerPath().GetPath() << '\n';
265+
}
266+
267+
// Auto generate is hidden unless enabled because this is mostly for
268+
// development and testing.
269+
if (Generator *g = r.GetGenerator()) {
270+
if (g->IsAutoGenerate())
271+
result.GetOutputStream() << "Auto generate: on\n";
272+
}
273+
262274
result.SetStatus(eReturnStatusSuccessFinishResult);
263275
return result.Succeeded();
264276
}

lldb/source/Utility/Reproducer.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ using namespace lldb_private::repro;
1818
using namespace llvm;
1919
using namespace llvm::yaml;
2020

21+
static llvm::Optional<bool> GetEnv(const char *var) {
22+
std::string val = llvm::StringRef(getenv(var)).lower();
23+
if (val == "0" || val == "off")
24+
return false;
25+
if (val == "1" || val == "on")
26+
return true;
27+
return {};
28+
}
29+
2130
Reproducer &Reproducer::Instance() { return *InstanceImpl(); }
2231

2332
llvm::Error Reproducer::Initialize(ReproducerMode mode,
@@ -27,12 +36,12 @@ llvm::Error Reproducer::Initialize(ReproducerMode mode,
2736

2837
// The environment can override the capture mode.
2938
if (mode != ReproducerMode::Replay) {
30-
std::string env =
31-
llvm::StringRef(getenv("LLDB_CAPTURE_REPRODUCER")).lower();
32-
if (env == "0" || env == "off")
33-
mode = ReproducerMode::Off;
34-
else if (env == "1" || env == "on")
35-
mode = ReproducerMode::Capture;
39+
if (llvm::Optional<bool> override = GetEnv("LLDB_CAPTURE_REPRODUCER")) {
40+
if (*override)
41+
mode = ReproducerMode::Capture;
42+
else
43+
mode = ReproducerMode::Off;
44+
}
3645
}
3746

3847
switch (mode) {
@@ -158,8 +167,12 @@ Generator::Generator(FileSpec root) : m_root(MakeAbsolute(std::move(root))) {
158167
}
159168

160169
Generator::~Generator() {
161-
if (!m_done)
162-
Discard();
170+
if (!m_done) {
171+
if (m_auto_generate)
172+
Keep();
173+
else
174+
Discard();
175+
}
163176
}
164177

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

206+
void Generator::SetAutoGenerate(bool b) { m_auto_generate = b; }
207+
208+
bool Generator::IsAutoGenerate() const { return m_auto_generate; }
209+
193210
const FileSpec &Generator::GetRoot() const { return m_root; }
194211

195212
void Generator::AddProvidersToIndex() {

lldb/tools/driver/Driver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,14 @@ llvm::Optional<int> InitializeReproducer(opt::InputArgList &input_args) {
807807
}
808808

809809
bool capture = input_args.hasArg(OPT_capture);
810+
bool auto_generate = input_args.hasArg(OPT_auto_generate);
810811
auto *capture_path = input_args.getLastArg(OPT_capture_path);
811812

813+
if (auto_generate && !capture) {
814+
WithColor::warning()
815+
<< "-reproducer-auto-generate specified without -capture\n";
816+
}
817+
812818
if (capture || capture_path) {
813819
if (capture_path) {
814820
if (!capture)
@@ -824,6 +830,8 @@ llvm::Optional<int> InitializeReproducer(opt::InputArgList &input_args) {
824830
return 1;
825831
}
826832
}
833+
if (auto_generate)
834+
SBReproducer::SetAutoGenerate(true);
827835
}
828836

829837
return llvm::None;

lldb/tools/driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,7 @@ def replay: Separate<["--", "-"], "replay">,
234234
HelpText<"Tells the debugger to replay a reproducer from <filename>.">;
235235
def skip_version_check: F<"reproducer-skip-version-check">,
236236
HelpText<"Skip the reproducer version check.">;
237+
def auto_generate: F<"reproducer-auto-generate">,
238+
HelpText<"Generate reproducer on exit.">;
237239

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

0 commit comments

Comments
 (0)