xref: /freebsd-src/contrib/llvm-project/lldb/source/API/SBReproducer.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
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 #include "SBReproducerPrivate.h"
10 
11 #include "lldb/API/LLDB.h"
12 #include "lldb/API/SBAddress.h"
13 #include "lldb/API/SBAttachInfo.h"
14 #include "lldb/API/SBBlock.h"
15 #include "lldb/API/SBBreakpoint.h"
16 #include "lldb/API/SBCommandInterpreter.h"
17 #include "lldb/API/SBCommandInterpreterRunOptions.h"
18 #include "lldb/API/SBData.h"
19 #include "lldb/API/SBDebugger.h"
20 #include "lldb/API/SBDeclaration.h"
21 #include "lldb/API/SBError.h"
22 #include "lldb/API/SBFileSpec.h"
23 #include "lldb/API/SBHostOS.h"
24 #include "lldb/API/SBReproducer.h"
25 #include "lldb/Host/FileSystem.h"
26 #include "lldb/Version/Version.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 using namespace lldb_private::repro;
31 
32 SBReplayOptions::SBReplayOptions()
33     : m_opaque_up(std::make_unique<ReplayOptions>()){}
34 
35 SBReplayOptions::SBReplayOptions(const SBReplayOptions &rhs)
36     : m_opaque_up(std::make_unique<ReplayOptions>(*rhs.m_opaque_up)) {}
37 
38 SBReplayOptions::~SBReplayOptions() = default;
39 
40 SBReplayOptions &SBReplayOptions::operator=(const SBReplayOptions &rhs) {
41   if (this == &rhs)
42     return *this;
43   *m_opaque_up = *rhs.m_opaque_up;
44   return *this;
45 }
46 
47 void SBReplayOptions::SetVerify(bool verify) { m_opaque_up->verify = verify; }
48 
49 bool SBReplayOptions::GetVerify() const { return m_opaque_up->verify; }
50 
51 void SBReplayOptions::SetCheckVersion(bool check) {
52   m_opaque_up->check_version = check;
53 }
54 
55 bool SBReplayOptions::GetCheckVersion() const {
56   return m_opaque_up->check_version;
57 }
58 
59 SBRegistry::SBRegistry() {
60   Registry &R = *this;
61 
62   RegisterMethods<SBAddress>(R);
63   RegisterMethods<SBAttachInfo>(R);
64   RegisterMethods<SBBlock>(R);
65   RegisterMethods<SBBreakpoint>(R);
66   RegisterMethods<SBBreakpointList>(R);
67   RegisterMethods<SBBreakpointLocation>(R);
68   RegisterMethods<SBBreakpointName>(R);
69   RegisterMethods<SBBroadcaster>(R);
70   RegisterMethods<SBCommandInterpreter>(R);
71   RegisterMethods<SBCommandInterpreterRunOptions>(R);
72   RegisterMethods<SBCommandReturnObject>(R);
73   RegisterMethods<SBCommunication>(R);
74   RegisterMethods<SBCompileUnit>(R);
75   RegisterMethods<SBData>(R);
76   RegisterMethods<SBDebugger>(R);
77   RegisterMethods<SBDeclaration>(R);
78   RegisterMethods<SBEnvironment>(R);
79   RegisterMethods<SBError>(R);
80   RegisterMethods<SBEvent>(R);
81   RegisterMethods<SBExecutionContext>(R);
82   RegisterMethods<SBExpressionOptions>(R);
83   RegisterMethods<SBFile>(R);
84   RegisterMethods<SBFileSpec>(R);
85   RegisterMethods<SBFileSpecList>(R);
86   RegisterMethods<SBFrame>(R);
87   RegisterMethods<SBFunction>(R);
88   RegisterMethods<SBHostOS>(R);
89   RegisterMethods<SBInputReader>(R);
90   RegisterMethods<SBInstruction>(R);
91   RegisterMethods<SBInstructionList>(R);
92   RegisterMethods<SBLanguageRuntime>(R);
93   RegisterMethods<SBLaunchInfo>(R);
94   RegisterMethods<SBLineEntry>(R);
95   RegisterMethods<SBListener>(R);
96   RegisterMethods<SBMemoryRegionInfo>(R);
97   RegisterMethods<SBMemoryRegionInfoList>(R);
98   RegisterMethods<SBModule>(R);
99   RegisterMethods<SBModuleSpec>(R);
100   RegisterMethods<SBPlatform>(R);
101   RegisterMethods<SBPlatformConnectOptions>(R);
102   RegisterMethods<SBPlatformShellCommand>(R);
103   RegisterMethods<SBProcess>(R);
104   RegisterMethods<SBProcessInfo>(R);
105   RegisterMethods<SBQueue>(R);
106   RegisterMethods<SBQueueItem>(R);
107   RegisterMethods<SBSection>(R);
108   RegisterMethods<SBSourceManager>(R);
109   RegisterMethods<SBStream>(R);
110   RegisterMethods<SBStringList>(R);
111   RegisterMethods<SBStructuredData>(R);
112   RegisterMethods<SBSymbol>(R);
113   RegisterMethods<SBSymbolContext>(R);
114   RegisterMethods<SBSymbolContextList>(R);
115   RegisterMethods<SBTarget>(R);
116   RegisterMethods<SBThread>(R);
117   RegisterMethods<SBThreadCollection>(R);
118   RegisterMethods<SBThreadPlan>(R);
119   RegisterMethods<SBTrace>(R);
120   RegisterMethods<SBType>(R);
121   RegisterMethods<SBTypeCategory>(R);
122   RegisterMethods<SBTypeEnumMember>(R);
123   RegisterMethods<SBTypeFilter>(R);
124   RegisterMethods<SBTypeFormat>(R);
125   RegisterMethods<SBTypeNameSpecifier>(R);
126   RegisterMethods<SBTypeSummary>(R);
127   RegisterMethods<SBTypeSummaryOptions>(R);
128   RegisterMethods<SBTypeSynthetic>(R);
129   RegisterMethods<SBUnixSignals>(R);
130   RegisterMethods<SBValue>(R);
131   RegisterMethods<SBValueList>(R);
132   RegisterMethods<SBVariablesOptions>(R);
133   RegisterMethods<SBWatchpoint>(R);
134 }
135 
136 const char *SBReproducer::Capture() {
137   static std::string error;
138   if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) {
139     error = llvm::toString(std::move(e));
140     return error.c_str();
141   }
142 
143   if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
144     auto &p = g->GetOrCreate<SBProvider>();
145     InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry());
146   }
147 
148   return nullptr;
149 }
150 
151 const char *SBReproducer::Capture(const char *path) {
152   static std::string error;
153   if (auto e =
154           Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) {
155     error = llvm::toString(std::move(e));
156     return error.c_str();
157   }
158 
159   if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
160     auto &p = g->GetOrCreate<SBProvider>();
161     InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry());
162   }
163 
164   return nullptr;
165 }
166 
167 const char *SBReproducer::PassiveReplay(const char *path) {
168   return "Reproducer replay has been removed";
169 }
170 
171 const char *SBReproducer::Replay(const char *path) {
172   return "Reproducer replay has been removed";
173 }
174 
175 const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
176   return Replay(path);
177 }
178 
179 const char *SBReproducer::Replay(const char *path,
180                                  const SBReplayOptions &options) {
181   return Replay(path);
182 }
183 
184 const char *SBReproducer::Finalize(const char *path) {
185   static std::string error;
186 
187   repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
188   if (!loader) {
189     error = "unable to get replay loader.";
190     return error.c_str();
191   }
192 
193   if (auto e = repro::Finalize(loader)) {
194     error = llvm::toString(std::move(e));
195     return error.c_str();
196   }
197 
198   return nullptr;
199 }
200 
201 bool SBReproducer::Generate() {
202   auto &r = Reproducer::Instance();
203   if (auto generator = r.GetGenerator()) {
204     generator->Keep();
205     return true;
206   }
207   return false;
208 }
209 
210 bool SBReproducer::SetAutoGenerate(bool b) {
211   auto &r = Reproducer::Instance();
212   if (auto generator = r.GetGenerator()) {
213     generator->SetAutoGenerate(b);
214     return true;
215   }
216   return false;
217 }
218 
219 const char *SBReproducer::GetPath() {
220   ConstString path;
221   auto &r = Reproducer::Instance();
222   if (FileSpec reproducer_path = Reproducer::Instance().GetReproducerPath())
223     path = ConstString(r.GetReproducerPath().GetCString());
224   return path.GetCString();
225 }
226 
227 void SBReproducer::SetWorkingDirectory(const char *path) {
228   if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
229     auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>();
230     wp.SetDirectory(path);
231     auto &fp = g->GetOrCreate<repro::FileProvider>();
232     fp.RecordInterestingDirectory(wp.GetDirectory());
233   }
234 }
235 
236 char lldb_private::repro::SBProvider::ID = 0;
237 const char *SBProvider::Info::name = "sbapi";
238 const char *SBProvider::Info::file = "sbapi.bin";
239