Skip to content

Commit fd2418a

Browse files
committed
[lldb/Reproducers] Add a flag to always generating a reproducer
Add a flag which always generates a reproducer when normally it would be discarded. This is meant for testing purposes to capture a debugger session without modification the session itself. (cherry picked from commit 066e817)
1 parent d49f335 commit fd2418a

File tree

7 files changed

+41
-2
lines changed

7 files changed

+41
-2
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ 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+
248251
/// Create and register a new provider.
249252
template <typename T> T *Create() {
250253
std::unique_ptr<ProviderBase> provider = llvm::make_unique<T>(m_root);
@@ -286,6 +289,9 @@ class Generator final {
286289

287290
/// Flag to ensure that we never call both keep and discard.
288291
bool m_done = false;
292+
293+
/// Flag to auto generate a reproducer when it would otherwise be discarded.
294+
bool m_auto_generate = false;
289295
};
290296

291297
class Loader final {

lldb/lit/Reproducer/TestDriverOptions.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
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
1313
# RUN: %lldb --capture-path %t.repro -b -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix WARNING --check-prefix STATUS-CAPTURE
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+
21+
# Check auto generate.
22+
# RUN: rm -rf %t.repro
23+
# RUN: %lldb --capture --capture-path %t.repro -b --reproducer-auto-generate -o 'reproducer status' 2>&1 | FileCheck %s --check-prefix NO-WARNING
24+
# RUN: cat %t.repro/index.yaml

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/Utility/Reproducer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ Generator::Generator(FileSpec root) : m_root(MakeAbsolute(std::move(root))) {
167167
}
168168

169169
Generator::~Generator() {
170-
if (!m_done)
171-
Discard();
170+
if (!m_done) {
171+
if (m_auto_generate)
172+
Keep();
173+
else
174+
Discard();
175+
}
172176
}
173177

174178
ProviderBase *Generator::Register(std::unique_ptr<ProviderBase> provider) {
@@ -199,6 +203,8 @@ void Generator::Discard() {
199203
llvm::sys::fs::remove_directories(m_root.GetPath());
200204
}
201205

206+
void Generator::SetAutoGenerate(bool b) { m_auto_generate = b; }
207+
202208
const FileSpec &Generator::GetRoot() const { return m_root; }
203209

204210
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)