LLDB  mainline
SBReproducer.cpp
Go to the documentation of this file.
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"
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 
109 }
110 
111 const char *SBReproducer::Capture() {
112  static std::string error;
113  if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) {
114  error = llvm::toString(std::move(e));
115  return error.c_str();
116  }
117 
118  if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
119  auto &p = g->GetOrCreate<SBProvider>();
120  InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry());
121  }
122 
123  return nullptr;
124 }
125 
126 const char *SBReproducer::Capture(const char *path) {
127  static std::string error;
128  if (auto e =
129  Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) {
130  error = llvm::toString(std::move(e));
131  return error.c_str();
132  }
133 
134  if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
135  auto &p = g->GetOrCreate<SBProvider>();
136  InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry());
137  }
138 
139  return nullptr;
140 }
141 
142 const char *SBReproducer::PassiveReplay(const char *path) {
143  static std::string error;
144  if (auto e = Reproducer::Initialize(ReproducerMode::PassiveReplay,
145  FileSpec(path))) {
146  error = llvm::toString(std::move(e));
147  return error.c_str();
148  }
149 
150  if (auto *l = lldb_private::repro::Reproducer::Instance().GetLoader()) {
151  FileSpec file = l->GetFile<SBProvider::Info>();
152  auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath());
153  if (!error_or_file) {
154  error =
155  "unable to read SB API data: " + error_or_file.getError().message();
156  return error.c_str();
157  }
158  static ReplayData r(std::move(*error_or_file));
159  InstrumentationData::Initialize(r.GetDeserializer(), r.GetRegistry());
160  }
161 
162  return nullptr;
163 }
164 
165 const char *SBReproducer::Replay(const char *path) {
166  return SBReproducer::Replay(path, false);
167 }
168 
169 const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
170  static std::string error;
171  if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) {
172  error = llvm::toString(std::move(e));
173  return error.c_str();
174  }
175 
176  repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
177  if (!loader) {
178  error = "unable to get replay loader.";
179  return error.c_str();
180  }
181 
182  if (!skip_version_check) {
183  llvm::Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
184  if (!version) {
185  error = llvm::toString(version.takeError());
186  return error.c_str();
187  }
188  if (lldb_private::GetVersion() != llvm::StringRef(*version).rtrim()) {
189  error = "reproducer capture and replay version don't match:\n";
190  error.append("reproducer captured with:\n");
191  error.append(*version);
192  error.append("reproducer replayed with:\n");
193  error.append(lldb_private::GetVersion());
194  return error.c_str();
195  }
196  }
197 
198  FileSpec file = loader->GetFile<SBProvider::Info>();
199  if (!file) {
200  error = "unable to get replay data from reproducer.";
201  return error.c_str();
202  }
203 
204  SBRegistry registry;
205  registry.Replay(file);
206 
207  return nullptr;
208 }
209 
211  auto &r = Reproducer::Instance();
212  if (auto generator = r.GetGenerator()) {
213  generator->Keep();
214  return true;
215  }
216  return false;
217 }
218 
220  auto &r = Reproducer::Instance();
221  if (auto generator = r.GetGenerator()) {
222  generator->SetAutoGenerate(b);
223  return true;
224  }
225  return false;
226 }
227 
228 const char *SBReproducer::GetPath() {
229  static std::string path;
230  auto &r = Reproducer::Instance();
231  path = r.GetReproducerPath().GetCString();
232  return path.c_str();
233 }
234 
235 void SBReproducer::SetWorkingDirectory(const char *path) {
236  if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) {
237  g->GetOrCreate<WorkingDirectoryProvider>().Update(path);
238  }
239 }
240 
242 const char *SBProvider::Info::name = "sbapi";
243 const char *SBProvider::Info::file = "sbapi.bin";
void RegisterMethods< SBProcess >(Registry &R)
Definition: SBProcess.cpp:1315
void RegisterMethods< SBFrame >(Registry &R)
Definition: SBFrame.cpp:1294
The registry contains a unique mapping between functions and their ID.
A class that represents a running process on the host machine.
void RegisterMethods< SBCommandReturnObject >(Registry &R)
Provider for the LLDB version number.
Definition: Reproducer.h:122
void RegisterMethods< SBTypeFilter >(Registry &R)
void RegisterMethods< SBSymbolContext >(Registry &R)
static bool SetAutoGenerate(bool b)
llvm::Expected< std::string > LoadBuffer()
Definition: Reproducer.h:303
void RegisterMethods< SBTrace >(Registry &R)
Definition: SBTrace.cpp:135
void RegisterMethods< SBTypeCategory >(Registry &R)
void RegisterMethods< SBAddress >(Registry &R)
Definition: SBAddress.cpp:291
A file utility class.
Definition: FileSpec.h:56
bool Replay(const FileSpec &file)
Replay functions from a file.
void RegisterMethods< SBBreakpoint >(Registry &R)
void RegisterMethods< SBStream >(Registry &R)
Definition: SBStream.cpp:201
void RegisterMethods< SBBlock >(Registry &R)
Definition: SBBlock.cpp:353
void RegisterMethods< SBQueueItem >(Registry &R)
void RegisterMethods< SBListener >(Registry &R)
Definition: SBListener.cpp:330
void RegisterMethods< SBType >(Registry &R)
Definition: SBType.cpp:902
void RegisterMethods< SBCommandInterpreterRunOptions >(Registry &R)
void RegisterMethods< SBModuleSpec >(Registry &R)
static const char * GetPath()
void RegisterMethods< SBTypeEnumMember >(Registry &R)
void RegisterMethods< SBUnixSignals >(Registry &R)
void RegisterMethods< SBExpressionOptions >(Registry &R)
void RegisterMethods< SBBreakpointLocation >(Registry &R)
void RegisterMethods< SBTypeSummaryOptions >(Registry &R)
void RegisterMethods< SBDebugger >(Registry &R)
void RegisterMethods< SBPlatformShellCommand >(Registry &R)
Definition: SBPlatform.cpp:694
void RegisterMethods< SBLaunchInfo >(Registry &R)
void RegisterMethods< SBTypeFormat >(Registry &R)
void RegisterMethods< SBLineEntry >(Registry &R)
static const char * PassiveReplay(const char *path)
void RegisterMethods< SBEvent >(Registry &R)
Definition: SBEvent.cpp:214
void RegisterMethods< SBSection >(Registry &R)
Definition: SBSection.cpp:296
llvm::StringRef toString(Record::Kind K)
void RegisterMethods< SBDeclaration >(Registry &R)
static const char * Replay(const char *path)
void RegisterMethods< SBFileSpec >(Registry &R)
Definition: SBFileSpec.cpp:195
void RegisterMethods< SBData >(Registry &R)
Definition: SBData.cpp:654
void RegisterMethods< SBVariablesOptions >(Registry &R)
void RegisterMethods< SBThreadPlan >(Registry &R)
void RegisterMethods< SBPlatformConnectOptions >(Registry &R)
Definition: SBPlatform.cpp:672
void RegisterMethods< SBThread >(Registry &R)
Definition: SBThread.cpp:1354
void RegisterMethods< SBPlatform >(Registry &R)
Definition: SBPlatform.cpp:719
void RegisterMethods< SBFunction >(Registry &R)
Definition: SBFunction.cpp:245
void RegisterMethods< SBTypeSummary >(Registry &R)
void RegisterMethods< SBCompileUnit >(Registry &R)
void RegisterMethods< SBInputReader >(Registry &R)
void RegisterMethods< SBAttachInfo >(Registry &R)
static bool Generate()
void RegisterMethods< SBLanguageRuntime >(Registry &R)
void RegisterMethods< SBQueue >(Registry &R)
Definition: SBQueue.cpp:336
void RegisterMethods< SBTraceOptions >(Registry &R)
void RegisterMethods< SBSourceManager >(Registry &R)
void RegisterMethods< SBCommandInterpreter >(Registry &R)
void RegisterMethods< SBSymbolContextList >(Registry &R)
void RegisterMethods< SBExecutionContext >(Registry &R)
void RegisterMethods< SBEnvironment >(Registry &R)
void RegisterMethods< SBInstruction >(Registry &R)
static Reproducer & Instance()
Definition: Reproducer.cpp:30
void RegisterMethods< SBHostOS >(Registry &R)
Definition: SBHostOS.cpp:182
void RegisterMethods< SBStringList >(Registry &R)
static const char * Capture()
void RegisterMethods< SBBreakpointName >(Registry &R)
void RegisterMethods< SBInstructionList >(Registry &R)
Definition: SBAddress.h:15
void RegisterMethods< SBMemoryRegionInfo >(Registry &R)
void RegisterMethods< SBMemoryRegionInfoList >(Registry &R)
void RegisterMethods< SBSymbol >(Registry &R)
Definition: SBSymbol.cpp:209
void RegisterMethods< SBTypeSynthetic >(Registry &R)
static void SetWorkingDirectory(const char *path)
The working directory is set to the current working directory when the reproducers are initialized...
void RegisterMethods< SBFile >(Registry &R)
Definition: SBFile.cpp:139
void RegisterMethods< SBError >(Registry &R)
Definition: SBError.cpp:191
void RegisterMethods< SBProcessInfo >(Registry &R)
void RegisterMethods< SBBreakpointList >(Registry &R)
void RegisterMethods< SBFileSpecList >(Registry &R)
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
void RegisterMethods< SBValueList >(Registry &R)
void RegisterMethods< SBTarget >(Registry &R)
Definition: SBTarget.cpp:2406
void RegisterMethods< SBStructuredData >(Registry &R)
void RegisterMethods< SBValue >(Registry &R)
Definition: SBValue.cpp:1550
void RegisterMethods< SBTypeNameSpecifier >(Registry &R)
void RegisterMethods< SBModule >(Registry &R)
Definition: SBModule.cpp:690
void RegisterMethods< SBThreadCollection >(Registry &R)
void RegisterMethods< SBBroadcaster >(Registry &R)
void RegisterMethods< SBWatchpoint >(Registry &R)
Provider for the LLDB current working directory.
Definition: Reproducer.h:142
void RegisterMethods< SBCommunication >(Registry &R)