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/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 
32 SBRegistry::SBRegistry() {
33   Registry &R = *this;
34 
35   RegisterMethods<SBAddress>(R);
36   RegisterMethods<SBAttachInfo>(R);
37   RegisterMethods<SBBlock>(R);
38   RegisterMethods<SBBreakpoint>(R);
39   RegisterMethods<SBBreakpointList>(R);
40   RegisterMethods<SBBreakpointLocation>(R);
41   RegisterMethods<SBBreakpointName>(R);
42   RegisterMethods<SBBroadcaster>(R);
43   RegisterMethods<SBCommandInterpreterRunOptions>(R);
44   RegisterMethods<SBCommandReturnObject>(R);
45   RegisterMethods<SBCommunication>(R);
46   RegisterMethods<SBCompileUnit>(R);
47   RegisterMethods<SBData>(R);
48   RegisterMethods<SBInputReader>(R);
49   RegisterMethods<SBDebugger>(R);
50   RegisterMethods<SBDeclaration>(R);
51   RegisterMethods<SBError>(R);
52   RegisterMethods<SBEnvironment>(R);
53   RegisterMethods<SBEvent>(R);
54   RegisterMethods<SBExecutionContext>(R);
55   RegisterMethods<SBExpressionOptions>(R);
56   RegisterMethods<SBFile>(R);
57   RegisterMethods<SBFileSpec>(R);
58   RegisterMethods<SBFileSpecList>(R);
59   RegisterMethods<SBFrame>(R);
60   RegisterMethods<SBFunction>(R);
61   RegisterMethods<SBHostOS>(R);
62   RegisterMethods<SBInstruction>(R);
63   RegisterMethods<SBInstructionList>(R);
64   RegisterMethods<SBLanguageRuntime>(R);
65   RegisterMethods<SBLaunchInfo>(R);
66   RegisterMethods<SBLineEntry>(R);
67   RegisterMethods<SBListener>(R);
68   RegisterMethods<SBMemoryRegionInfo>(R);
69   RegisterMethods<SBMemoryRegionInfoList>(R);
70   RegisterMethods<SBModule>(R);
71   RegisterMethods<SBModuleSpec>(R);
72   RegisterMethods<SBPlatformConnectOptions>(R);
73   RegisterMethods<SBPlatformShellCommand>(R);
74   RegisterMethods<SBPlatform>(R);
75   RegisterMethods<SBProcess>(R);
76   RegisterMethods<SBProcessInfo>(R);
77   RegisterMethods<SBQueue>(R);
78   RegisterMethods<SBQueueItem>(R);
79   RegisterMethods<SBSection>(R);
80   RegisterMethods<SBSourceManager>(R);
81   RegisterMethods<SBStream>(R);
82   RegisterMethods<SBStringList>(R);
83   RegisterMethods<SBStructuredData>(R);
84   RegisterMethods<SBSymbol>(R);
85   RegisterMethods<SBSymbolContext>(R);
86   RegisterMethods<SBSymbolContextList>(R);
87   RegisterMethods<SBTarget>(R);
88   RegisterMethods<SBThread>(R);
89   RegisterMethods<SBThreadCollection>(R);
90   RegisterMethods<SBThreadPlan>(R);
91   RegisterMethods<SBTrace>(R);
92   RegisterMethods<SBTraceOptions>(R);
93   RegisterMethods<SBType>(R);
94   RegisterMethods<SBTypeCategory>(R);
95   RegisterMethods<SBTypeEnumMember>(R);
96   RegisterMethods<SBTypeFilter>(R);
97   RegisterMethods<SBTypeFormat>(R);
98   RegisterMethods<SBTypeNameSpecifier>(R);
99   RegisterMethods<SBTypeSummaryOptions>(R);
100   RegisterMethods<SBTypeSummary>(R);
101   RegisterMethods<SBTypeSynthetic>(R);
102   RegisterMethods<SBUnixSignals>(R);
103   RegisterMethods<SBValue>(R);
104   RegisterMethods<SBValueList>(R);
105   RegisterMethods<SBVariablesOptions>(R);
106   RegisterMethods<SBWatchpoint>(R);
107 }
108 
109 const char *SBReproducer::Capture() {
110   static std::string error;
111   if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) {
112     error = llvm::toString(std::move(e));
113     return error.c_str();
114   }
115   return nullptr;
116 }
117 
118 const char *SBReproducer::Capture(const char *path) {
119   static std::string error;
120   if (auto e =
121           Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) {
122     error = llvm::toString(std::move(e));
123     return error.c_str();
124   }
125   return nullptr;
126 }
127 
128 const char *SBReproducer::Replay(const char *path) {
129   return SBReproducer::Replay(path, false);
130 }
131 
132 const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
133   static std::string error;
134   if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) {
135     error = llvm::toString(std::move(e));
136     return error.c_str();
137   }
138 
139   repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
140   if (!loader) {
141     error = "unable to get replay loader.";
142     return error.c_str();
143   }
144 
145   if (!skip_version_check) {
146     llvm::Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
147     if (!version) {
148       error = llvm::toString(version.takeError());
149       return error.c_str();
150     }
151     if (lldb_private::GetVersion() != llvm::StringRef(*version).rtrim()) {
152       error = "reproducer capture and replay version don't match:\n";
153       error.append("reproducer captured with:\n");
154       error.append(*version);
155       error.append("reproducer replayed with:\n");
156       error.append(lldb_private::GetVersion());
157       return error.c_str();
158     }
159   }
160 
161   FileSpec file = loader->GetFile<SBProvider::Info>();
162   if (!file) {
163     error = "unable to get replay data from reproducer.";
164     return error.c_str();
165   }
166 
167   SBRegistry registry;
168   registry.Replay(file);
169 
170   return nullptr;
171 }
172 
173 bool SBReproducer::Generate() {
174   auto &r = Reproducer::Instance();
175   if (auto generator = r.GetGenerator()) {
176     generator->Keep();
177     return true;
178   }
179   return false;
180 }
181 
182 bool SBReproducer::SetAutoGenerate(bool b) {
183   auto &r = Reproducer::Instance();
184   if (auto generator = r.GetGenerator()) {
185     generator->SetAutoGenerate(b);
186     return true;
187   }
188   return false;
189 }
190 
191 const char *SBReproducer::GetPath() {
192   static std::string path;
193   auto &r = Reproducer::Instance();
194   path = r.GetReproducerPath().GetCString();
195   return path.c_str();
196 }
197 
198 char lldb_private::repro::SBProvider::ID = 0;
199 const char *SBProvider::Info::name = "sbapi";
200 const char *SBProvider::Info::file = "sbapi.bin";
201