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