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/lldb-private.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 using namespace lldb_private::repro;
31 
SBReplayOptions()32 SBReplayOptions::SBReplayOptions()
33     : m_opaque_up(std::make_unique<ReplayOptions>()){}
34 
SBReplayOptions(const SBReplayOptions & rhs)35 SBReplayOptions::SBReplayOptions(const SBReplayOptions &rhs)
36     : m_opaque_up(std::make_unique<ReplayOptions>(*rhs.m_opaque_up)) {}
37 
38 SBReplayOptions::~SBReplayOptions() = default;
39 
operator =(const SBReplayOptions & rhs)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 
SetVerify(bool verify)47 void SBReplayOptions::SetVerify(bool verify) { m_opaque_up->verify = verify; }
48 
GetVerify() const49 bool SBReplayOptions::GetVerify() const { return m_opaque_up->verify; }
50 
SetCheckVersion(bool check)51 void SBReplayOptions::SetCheckVersion(bool check) {
52   m_opaque_up->check_version = check;
53 }
54 
GetCheckVersion() const55 bool SBReplayOptions::GetCheckVersion() const {
56   return m_opaque_up->check_version;
57 }
58 
SBRegistry()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 
Capture()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 
Capture(const char * path)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 
PassiveReplay(const char * path)167 const char *SBReproducer::PassiveReplay(const char *path) {
168   static std::string error;
169   if (auto e = Reproducer::Initialize(ReproducerMode::PassiveReplay,
170                                       FileSpec(path))) {
171     error = llvm::toString(std::move(e));
172     return error.c_str();
173   }
174 
175   if (auto *l = lldb_private::repro::Reproducer::Instance().GetLoader()) {
176     FileSpec file = l->GetFile<SBProvider::Info>();
177     auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath());
178     if (!error_or_file) {
179       error =
180           "unable to read SB API data: " + error_or_file.getError().message();
181       return error.c_str();
182     }
183     static ReplayData r(std::move(*error_or_file));
184     InstrumentationData::Initialize(r.GetDeserializer(), r.GetRegistry());
185   }
186 
187   return nullptr;
188 }
189 
Replay(const char * path)190 const char *SBReproducer::Replay(const char *path) {
191   SBReplayOptions options;
192   return SBReproducer::Replay(path, options);
193 }
194 
Replay(const char * path,bool skip_version_check)195 const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
196   SBReplayOptions options;
197   options.SetCheckVersion(!skip_version_check);
198   return SBReproducer::Replay(path, options);
199 }
200 
Replay(const char * path,const SBReplayOptions & options)201 const char *SBReproducer::Replay(const char *path,
202                                  const SBReplayOptions &options) {
203   static std::string error;
204   if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) {
205     error = llvm::toString(std::move(e));
206     return error.c_str();
207   }
208 
209   repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
210   if (!loader) {
211     error = "unable to get replay loader.";
212     return error.c_str();
213   }
214 
215   if (options.GetCheckVersion()) {
216     llvm::Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
217     if (!version) {
218       error = llvm::toString(version.takeError());
219       return error.c_str();
220     }
221     if (lldb_private::GetVersion() != llvm::StringRef(*version).rtrim()) {
222       error = "reproducer capture and replay version don't match:\n";
223       error.append("reproducer captured with:\n");
224       error.append(*version);
225       error.append("reproducer replayed with:\n");
226       error.append(lldb_private::GetVersion());
227       return error.c_str();
228     }
229   }
230 
231   if (options.GetVerify()) {
232     bool verification_failed = false;
233     llvm::raw_string_ostream os(error);
234     auto error_callback = [&](llvm::StringRef error) {
235       verification_failed = true;
236       os << "\nerror: " << error;
237     };
238 
239     auto warning_callback = [&](llvm::StringRef warning) {
240       verification_failed = true;
241       os << "\nwarning: " << warning;
242     };
243 
244     auto note_callback = [&](llvm::StringRef warning) {};
245 
246     Verifier verifier(loader);
247     verifier.Verify(error_callback, warning_callback, note_callback);
248 
249     if (verification_failed) {
250       os.flush();
251       return error.c_str();
252     }
253   }
254 
255   FileSpec file = loader->GetFile<SBProvider::Info>();
256   if (!file) {
257     error = "unable to get replay data from reproducer.";
258     return error.c_str();
259   }
260 
261   SBRegistry registry;
262   registry.Replay(file);
263 
264   return nullptr;
265 }
266 
Finalize(const char * path)267 const char *SBReproducer::Finalize(const char *path) {
268   static std::string error;
269   if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) {
270     error = llvm::toString(std::move(e));
271     return error.c_str();
272   }
273 
274   repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
275   if (!loader) {
276     error = "unable to get replay loader.";
277     return error.c_str();
278   }
279 
280   if (auto e = repro::Finalize(loader)) {
281     error = llvm::toString(std::move(e));
282     return error.c_str();
283   }
284 
285   return nullptr;
286 }
287 
Generate()288 bool SBReproducer::Generate() {
289   auto &r = Reproducer::Instance();
290   if (auto generator = r.GetGenerator()) {
291     generator->Keep();
292     return true;
293   }
294   return false;
295 }
296 
SetAutoGenerate(bool b)297 bool SBReproducer::SetAutoGenerate(bool b) {
298   auto &r = Reproducer::Instance();
299   if (auto generator = r.GetGenerator()) {
300     generator->SetAutoGenerate(b);
301     return true;
302   }
303   return false;
304 }
305 
GetPath()306 const char *SBReproducer::GetPath() {
307   ConstString path;
308   auto &r = Reproducer::Instance();
309   if (FileSpec reproducer_path = Reproducer::Instance().GetReproducerPath())
310     path = ConstString(r.GetReproducerPath().GetCString());
311   return path.GetCString();
312 }
313 
SetWorkingDirectory(const char * path)314 void SBReproducer::SetWorkingDirectory(const char *path) {
315   if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
316     auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>();
317     wp.SetDirectory(path);
318     auto &fp = g->GetOrCreate<repro::FileProvider>();
319     fp.RecordInterestingDirectory(wp.GetDirectory());
320   }
321 }
322 
323 char lldb_private::repro::SBProvider::ID = 0;
324 const char *SBProvider::Info::name = "sbapi";
325 const char *SBProvider::Info::file = "sbapi.bin";
326