1 //===-- SBReproducer.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 
10 #include "lldb/API/LLDB.h"
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBAttachInfo.h"
13 #include "lldb/API/SBBlock.h"
14 #include "lldb/API/SBBreakpoint.h"
15 #include "lldb/API/SBCommandInterpreter.h"
16 #include "lldb/API/SBCommandInterpreterRunOptions.h"
17 #include "lldb/API/SBData.h"
18 #include "lldb/API/SBDebugger.h"
19 #include "lldb/API/SBDeclaration.h"
20 #include "lldb/API/SBError.h"
21 #include "lldb/API/SBFileSpec.h"
22 #include "lldb/API/SBHostOS.h"
23 #include "lldb/API/SBReproducer.h"
24 #include "lldb/Host/FileSystem.h"
25 #include "lldb/Version/Version.h"
26 #include "lldb/Utility/ReproducerInstrumentation.h"
27 #include "lldb/Utility/Reproducer.h"
28 #include "lldb/Utility/ReproducerProvider.h"
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 using namespace lldb_private::repro;
33 
34 SBReplayOptions::SBReplayOptions()
35     : m_opaque_up(std::make_unique<ReplayOptions>()){}
36 
37 SBReplayOptions::SBReplayOptions(const SBReplayOptions &rhs)
38     : m_opaque_up(std::make_unique<ReplayOptions>(*rhs.m_opaque_up)) {}
39 
40 SBReplayOptions::~SBReplayOptions() = default;
41 
42 SBReplayOptions &SBReplayOptions::operator=(const SBReplayOptions &rhs) {
43   if (this == &rhs)
44     return *this;
45   *m_opaque_up = *rhs.m_opaque_up;
46   return *this;
47 }
48 
49 void SBReplayOptions::SetVerify(bool verify) { m_opaque_up->verify = verify; }
50 
51 bool SBReplayOptions::GetVerify() const { return m_opaque_up->verify; }
52 
53 void SBReplayOptions::SetCheckVersion(bool check) {
54   m_opaque_up->check_version = check;
55 }
56 
57 bool SBReplayOptions::GetCheckVersion() const {
58   return m_opaque_up->check_version;
59 }
60 
61 const char *SBReproducer::Capture() {
62   static std::string error;
63   if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) {
64     error = llvm::toString(std::move(e));
65     return error.c_str();
66   }
67 
68   return nullptr;
69 }
70 
71 const char *SBReproducer::Capture(const char *path) {
72   static std::string error;
73   if (auto e =
74           Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) {
75     error = llvm::toString(std::move(e));
76     return error.c_str();
77   }
78 
79   return nullptr;
80 }
81 
82 const char *SBReproducer::PassiveReplay(const char *path) {
83   return "Reproducer replay has been removed";
84 }
85 
86 const char *SBReproducer::Replay(const char *path) {
87   return "Reproducer replay has been removed";
88 }
89 
90 const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
91   return Replay(path);
92 }
93 
94 const char *SBReproducer::Replay(const char *path,
95                                  const SBReplayOptions &options) {
96   return Replay(path);
97 }
98 
99 const char *SBReproducer::Finalize(const char *path) {
100   static std::string error;
101 
102   repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
103   if (!loader) {
104     error = "unable to get replay loader.";
105     return error.c_str();
106   }
107 
108   if (auto e = repro::Finalize(loader)) {
109     error = llvm::toString(std::move(e));
110     return error.c_str();
111   }
112 
113   return nullptr;
114 }
115 
116 bool SBReproducer::Generate() {
117   auto &r = Reproducer::Instance();
118   if (auto generator = r.GetGenerator()) {
119     generator->Keep();
120     return true;
121   }
122   return false;
123 }
124 
125 bool SBReproducer::SetAutoGenerate(bool b) {
126   auto &r = Reproducer::Instance();
127   if (auto generator = r.GetGenerator()) {
128     generator->SetAutoGenerate(b);
129     return true;
130   }
131   return false;
132 }
133 
134 const char *SBReproducer::GetPath() {
135   ConstString path;
136   auto &r = Reproducer::Instance();
137   if (FileSpec reproducer_path = Reproducer::Instance().GetReproducerPath())
138     path = ConstString(r.GetReproducerPath().GetCString());
139   return path.GetCString();
140 }
141 
142 void SBReproducer::SetWorkingDirectory(const char *path) {
143   if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
144     auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>();
145     wp.SetDirectory(path);
146     auto &fp = g->GetOrCreate<repro::FileProvider>();
147     fp.RecordInterestingDirectory(wp.GetDirectory());
148   }
149 }
150