180814287SRaphael Isemann //===-- SBReproducer.cpp --------------------------------------------------===//
258947cf8SJonas Devlieghere //
3023f9998SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4023f9998SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5023f9998SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
658947cf8SJonas Devlieghere //
758947cf8SJonas Devlieghere //===----------------------------------------------------------------------===//
858947cf8SJonas Devlieghere 
91755f5b1SJonas Devlieghere #include "lldb/API/SBReproducer.h"
1058947cf8SJonas Devlieghere #include "lldb/API/LLDB.h"
1158947cf8SJonas Devlieghere #include "lldb/API/SBAddress.h"
1258947cf8SJonas Devlieghere #include "lldb/API/SBAttachInfo.h"
1358947cf8SJonas Devlieghere #include "lldb/API/SBBlock.h"
1458947cf8SJonas Devlieghere #include "lldb/API/SBBreakpoint.h"
1558947cf8SJonas Devlieghere #include "lldb/API/SBCommandInterpreter.h"
164b354039SJonas Devlieghere #include "lldb/API/SBCommandInterpreterRunOptions.h"
1758947cf8SJonas Devlieghere #include "lldb/API/SBData.h"
1858947cf8SJonas Devlieghere #include "lldb/API/SBDebugger.h"
1958947cf8SJonas Devlieghere #include "lldb/API/SBDeclaration.h"
2058947cf8SJonas Devlieghere #include "lldb/API/SBError.h"
2158947cf8SJonas Devlieghere #include "lldb/API/SBFileSpec.h"
2258947cf8SJonas Devlieghere #include "lldb/API/SBHostOS.h"
2358947cf8SJonas Devlieghere #include "lldb/Host/FileSystem.h"
241755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
25d51402acSJonas Devlieghere #include "lldb/Utility/Reproducer.h"
26d51402acSJonas Devlieghere #include "lldb/Utility/ReproducerProvider.h"
271755f5b1SJonas Devlieghere #include "lldb/Version/Version.h"
2858947cf8SJonas Devlieghere 
2958947cf8SJonas Devlieghere using namespace lldb;
3058947cf8SJonas Devlieghere using namespace lldb_private;
3158947cf8SJonas Devlieghere using namespace lldb_private::repro;
3258947cf8SJonas Devlieghere 
SBReplayOptions()3337469061SJonas Devlieghere SBReplayOptions::SBReplayOptions()
3413cde673SMartin Storsjö     : m_opaque_up(std::make_unique<ReplayOptions>()){}
3537469061SJonas Devlieghere 
SBReplayOptions(const SBReplayOptions & rhs)3637469061SJonas Devlieghere SBReplayOptions::SBReplayOptions(const SBReplayOptions &rhs)
3737469061SJonas Devlieghere     : m_opaque_up(std::make_unique<ReplayOptions>(*rhs.m_opaque_up)) {}
3837469061SJonas Devlieghere 
3937469061SJonas Devlieghere SBReplayOptions::~SBReplayOptions() = default;
4037469061SJonas Devlieghere 
operator =(const SBReplayOptions & rhs)4137469061SJonas Devlieghere SBReplayOptions &SBReplayOptions::operator=(const SBReplayOptions &rhs) {
421755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs)
4337469061SJonas Devlieghere   if (this == &rhs)
4437469061SJonas Devlieghere     return *this;
4537469061SJonas Devlieghere   *m_opaque_up = *rhs.m_opaque_up;
4637469061SJonas Devlieghere   return *this;
4737469061SJonas Devlieghere }
4837469061SJonas Devlieghere 
SetVerify(bool verify)491755f5b1SJonas Devlieghere void SBReplayOptions::SetVerify(bool verify) {
501755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, verify) m_opaque_up->verify = verify;
511755f5b1SJonas Devlieghere }
5237469061SJonas Devlieghere 
GetVerify() const531755f5b1SJonas Devlieghere bool SBReplayOptions::GetVerify() const {
541755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this) return m_opaque_up->verify;
551755f5b1SJonas Devlieghere }
5637469061SJonas Devlieghere 
SetCheckVersion(bool check)5737469061SJonas Devlieghere void SBReplayOptions::SetCheckVersion(bool check) {
581755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, check)
5937469061SJonas Devlieghere   m_opaque_up->check_version = check;
6037469061SJonas Devlieghere }
6137469061SJonas Devlieghere 
GetCheckVersion() const6237469061SJonas Devlieghere bool SBReplayOptions::GetCheckVersion() const {
631755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this)
6437469061SJonas Devlieghere   return m_opaque_up->check_version;
6537469061SJonas Devlieghere }
6637469061SJonas Devlieghere 
Capture()6713ecae2fSJonas Devlieghere const char *SBReproducer::Capture() {
681755f5b1SJonas Devlieghere   LLDB_INSTRUMENT()
6913ecae2fSJonas Devlieghere   static std::string error;
7013ecae2fSJonas Devlieghere   if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) {
7113ecae2fSJonas Devlieghere     error = llvm::toString(std::move(e));
7213ecae2fSJonas Devlieghere     return error.c_str();
7313ecae2fSJonas Devlieghere   }
74950a8aa1SJonas Devlieghere 
7513ecae2fSJonas Devlieghere   return nullptr;
7613ecae2fSJonas Devlieghere }
7713ecae2fSJonas Devlieghere 
Capture(const char * path)78936c6242SJonas Devlieghere const char *SBReproducer::Capture(const char *path) {
791755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path)
80936c6242SJonas Devlieghere   static std::string error;
81936c6242SJonas Devlieghere   if (auto e =
82936c6242SJonas Devlieghere           Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) {
83936c6242SJonas Devlieghere     error = llvm::toString(std::move(e));
84936c6242SJonas Devlieghere     return error.c_str();
85936c6242SJonas Devlieghere   }
86950a8aa1SJonas Devlieghere 
87936c6242SJonas Devlieghere   return nullptr;
88936c6242SJonas Devlieghere }
8958947cf8SJonas Devlieghere 
PassiveReplay(const char * path)90fc410138SJonas Devlieghere const char *SBReproducer::PassiveReplay(const char *path) {
911755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path)
92fa126069SJonas Devlieghere   return "Reproducer replay has been removed";
93fc410138SJonas Devlieghere }
94fc410138SJonas Devlieghere 
Replay(const char * path)95d3ba1e02SJonas Devlieghere const char *SBReproducer::Replay(const char *path) {
961755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path)
97fa126069SJonas Devlieghere   return "Reproducer replay has been removed";
98d3ba1e02SJonas Devlieghere }
99d3ba1e02SJonas Devlieghere 
Replay(const char * path,bool skip_version_check)10062827737SJonas Devlieghere const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
1011755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path, skip_version_check)
102fa126069SJonas Devlieghere   return Replay(path);
10337469061SJonas Devlieghere }
10437469061SJonas Devlieghere 
Replay(const char * path,const SBReplayOptions & options)10537469061SJonas Devlieghere const char *SBReproducer::Replay(const char *path,
10637469061SJonas Devlieghere                                  const SBReplayOptions &options) {
1071755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path, options)
108fa126069SJonas Devlieghere   return Replay(path);
109c8dfe907SJonas Devlieghere }
110c8dfe907SJonas Devlieghere 
Finalize(const char * path)11173811d32SJonas Devlieghere const char *SBReproducer::Finalize(const char *path) {
1121755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path)
1138b3b66eaSJonas Devlieghere   return "Reproducer finalize has been removed";
11473811d32SJonas Devlieghere }
11573811d32SJonas Devlieghere 
Generate()116c8dfe907SJonas Devlieghere bool SBReproducer::Generate() {
1171755f5b1SJonas Devlieghere   LLDB_INSTRUMENT()
118c8dfe907SJonas Devlieghere   auto &r = Reproducer::Instance();
119c8dfe907SJonas Devlieghere   if (auto generator = r.GetGenerator()) {
120c8dfe907SJonas Devlieghere     generator->Keep();
121c8dfe907SJonas Devlieghere     return true;
122c8dfe907SJonas Devlieghere   }
123c8dfe907SJonas Devlieghere   return false;
124c8dfe907SJonas Devlieghere }
125c8dfe907SJonas Devlieghere 
SetAutoGenerate(bool b)126066e817bSJonas Devlieghere bool SBReproducer::SetAutoGenerate(bool b) {
1271755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(b)
128066e817bSJonas Devlieghere   auto &r = Reproducer::Instance();
129066e817bSJonas Devlieghere   if (auto generator = r.GetGenerator()) {
130066e817bSJonas Devlieghere     generator->SetAutoGenerate(b);
131066e817bSJonas Devlieghere     return true;
132066e817bSJonas Devlieghere   }
133066e817bSJonas Devlieghere   return false;
134066e817bSJonas Devlieghere }
135066e817bSJonas Devlieghere 
GetPath()136c8dfe907SJonas Devlieghere const char *SBReproducer::GetPath() {
1371755f5b1SJonas Devlieghere   LLDB_INSTRUMENT()
13873811d32SJonas Devlieghere   ConstString path;
139*1b4b12a3SNico Weber   auto &r = Reproducer::Instance();
14073811d32SJonas Devlieghere   if (FileSpec reproducer_path = Reproducer::Instance().GetReproducerPath())
141*1b4b12a3SNico Weber     path = ConstString(r.GetReproducerPath().GetCString());
14273811d32SJonas Devlieghere   return path.GetCString();
14358947cf8SJonas Devlieghere }
14458947cf8SJonas Devlieghere 
SetWorkingDirectory(const char * path)1456671a81bSJonas Devlieghere void SBReproducer::SetWorkingDirectory(const char *path) {
1461755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(path)
1476671a81bSJonas Devlieghere   if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
148f8df2e1aSJonas Devlieghere     auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>();
149c90ca0c8SJonas Devlieghere     wp.SetDirectory(path);
150f8df2e1aSJonas Devlieghere     auto &fp = g->GetOrCreate<repro::FileProvider>();
151c90ca0c8SJonas Devlieghere     fp.RecordInterestingDirectory(wp.GetDirectory());
1526671a81bSJonas Devlieghere   }
1536671a81bSJonas Devlieghere }
154