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