SBReproducer.cpp revision 360784
14Srgrimes//===-- SBReproducer.cpp ----------------------------------------*- C++ -*-===//
24Srgrimes//
34Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44Srgrimes// See https://llvm.org/LICENSE.txt for license information.
54Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64Srgrimes//
74Srgrimes//===----------------------------------------------------------------------===//
84Srgrimes
94Srgrimes#include "SBReproducerPrivate.h"
104Srgrimes
114Srgrimes#include "SBReproducerPrivate.h"
124Srgrimes#include "lldb/API/LLDB.h"
134Srgrimes#include "lldb/API/SBAddress.h"
144Srgrimes#include "lldb/API/SBAttachInfo.h"
154Srgrimes#include "lldb/API/SBBlock.h"
164Srgrimes#include "lldb/API/SBBreakpoint.h"
174Srgrimes#include "lldb/API/SBCommandInterpreter.h"
184Srgrimes#include "lldb/API/SBData.h"
194Srgrimes#include "lldb/API/SBDebugger.h"
204Srgrimes#include "lldb/API/SBDeclaration.h"
214Srgrimes#include "lldb/API/SBError.h"
224Srgrimes#include "lldb/API/SBFileSpec.h"
234Srgrimes#include "lldb/API/SBHostOS.h"
244Srgrimes#include "lldb/API/SBReproducer.h"
254Srgrimes#include "lldb/Host/FileSystem.h"
264Srgrimes#include "lldb/lldb-private.h"
274Srgrimes
284Srgrimesusing namespace lldb;
294Srgrimesusing namespace lldb_private;
304Srgrimesusing namespace lldb_private::repro;
314Srgrimes
32619SrgrimesSBRegistry::SBRegistry() {
3350477Speter  Registry &R = *this;
344Srgrimes
354Srgrimes  RegisterMethods<SBAddress>(R);
364Srgrimes  RegisterMethods<SBAttachInfo>(R);
374Srgrimes  RegisterMethods<SBBlock>(R);
384Srgrimes  RegisterMethods<SBBreakpoint>(R);
394Srgrimes  RegisterMethods<SBBreakpointList>(R);
404Srgrimes  RegisterMethods<SBBreakpointLocation>(R);
41204309Sattilio  RegisterMethods<SBBreakpointName>(R);
42204309Sattilio  RegisterMethods<SBBroadcaster>(R);
434Srgrimes  RegisterMethods<SBCommandInterpreterRunOptions>(R);
4493945Snyan  RegisterMethods<SBCommandReturnObject>(R);
4593945Snyan  RegisterMethods<SBCommunication>(R);
4693945Snyan  RegisterMethods<SBCompileUnit>(R);
4793945Snyan  RegisterMethods<SBData>(R);
4893945Snyan  RegisterMethods<SBInputReader>(R);
49121985Sjhb  RegisterMethods<SBDebugger>(R);
50233031Snyan  RegisterMethods<SBDeclaration>(R);
51233031Snyan  RegisterMethods<SBError>(R);
52233031Snyan  RegisterMethods<SBEvent>(R);
53233031Snyan  RegisterMethods<SBExecutionContext>(R);
54233031Snyan  RegisterMethods<SBExpressionOptions>(R);
55233031Snyan  RegisterMethods<SBFile>(R);
56233031Snyan  RegisterMethods<SBFileSpec>(R);
57233031Snyan  RegisterMethods<SBFileSpecList>(R);
58233031Snyan  RegisterMethods<SBFrame>(R);
59233031Snyan  RegisterMethods<SBFunction>(R);
60233031Snyan  RegisterMethods<SBHostOS>(R);
61233031Snyan  RegisterMethods<SBInstruction>(R);
62233031Snyan  RegisterMethods<SBInstructionList>(R);
63233031Snyan  RegisterMethods<SBLanguageRuntime>(R);
64233031Snyan  RegisterMethods<SBLaunchInfo>(R);
65233031Snyan  RegisterMethods<SBLineEntry>(R);
66233031Snyan  RegisterMethods<SBListener>(R);
67233031Snyan  RegisterMethods<SBMemoryRegionInfo>(R);
68233031Snyan  RegisterMethods<SBMemoryRegionInfoList>(R);
69233031Snyan  RegisterMethods<SBModule>(R);
70233031Snyan  RegisterMethods<SBModuleSpec>(R);
71233031Snyan  RegisterMethods<SBPlatformConnectOptions>(R);
72233031Snyan  RegisterMethods<SBPlatformShellCommand>(R);
73233031Snyan  RegisterMethods<SBPlatform>(R);
74233031Snyan  RegisterMethods<SBProcess>(R);
75233031Snyan  RegisterMethods<SBProcessInfo>(R);
76233031Snyan  RegisterMethods<SBQueue>(R);
77233031Snyan  RegisterMethods<SBQueueItem>(R);
78233031Snyan  RegisterMethods<SBSection>(R);
79233031Snyan  RegisterMethods<SBSourceManager>(R);
80233031Snyan  RegisterMethods<SBStream>(R);
81233031Snyan  RegisterMethods<SBStringList>(R);
82233031Snyan  RegisterMethods<SBStructuredData>(R);
83233031Snyan  RegisterMethods<SBSymbol>(R);
84233031Snyan  RegisterMethods<SBSymbolContext>(R);
85233031Snyan  RegisterMethods<SBSymbolContextList>(R);
86233031Snyan  RegisterMethods<SBTarget>(R);
87233031Snyan  RegisterMethods<SBThread>(R);
88233031Snyan  RegisterMethods<SBThreadCollection>(R);
89233031Snyan  RegisterMethods<SBThreadPlan>(R);
90233031Snyan  RegisterMethods<SBTrace>(R);
91233031Snyan  RegisterMethods<SBTraceOptions>(R);
92165302Skmacy  RegisterMethods<SBType>(R);
93121985Sjhb  RegisterMethods<SBTypeCategory>(R);
9425164Speter  RegisterMethods<SBTypeEnumMember>(R);
95204309Sattilio  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
108const 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
117const 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
127const char *SBReproducer::Replay(const char *path) {
128  return SBReproducer::Replay(path, false);
129}
130
131const char *SBReproducer::Replay(const char *path, bool skip_version_check) {
132  static std::string error;
133  if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) {
134    error = llvm::toString(std::move(e));
135    return error.c_str();
136  }
137
138  repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
139  if (!loader) {
140    error = "unable to get replay loader.";
141    return error.c_str();
142  }
143
144  if (!skip_version_check) {
145    llvm::Expected<std::string> version = loader->LoadBuffer<VersionProvider>();
146    if (!version) {
147      error = llvm::toString(version.takeError());
148      return error.c_str();
149    }
150    if (lldb_private::GetVersion() != llvm::StringRef(*version).rtrim()) {
151      error = "reproducer capture and replay version don't match:\n";
152      error.append("reproducer captured with:\n");
153      error.append(*version);
154      error.append("reproducer replayed with:\n");
155      error.append(lldb_private::GetVersion());
156      return error.c_str();
157    }
158  }
159
160  FileSpec file = loader->GetFile<SBProvider::Info>();
161  if (!file) {
162    error = "unable to get replay data from reproducer.";
163    return error.c_str();
164  }
165
166  SBRegistry registry;
167  registry.Replay(file);
168
169  return nullptr;
170}
171
172bool SBReproducer::Generate() {
173  auto &r = Reproducer::Instance();
174  if (auto generator = r.GetGenerator()) {
175    generator->Keep();
176    return true;
177  }
178  return false;
179}
180
181const char *SBReproducer::GetPath() {
182  static std::string path;
183  auto &r = Reproducer::Instance();
184  path = r.GetReproducerPath().GetCString();
185  return path.c_str();
186}
187
188char lldb_private::repro::SBProvider::ID = 0;
189const char *SBProvider::Info::name = "sbapi";
190const char *SBProvider::Info::file = "sbapi.bin";
191