1 //===-- SBReproducer.cpp ----------------------------------------*- C++ -*-===//
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::Replay(const char *path) {
128   return SBReproducer::Replay(path, false);
129 }
130 
131 const 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 
172 bool 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 
181 const 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 
188 char lldb_private::repro::SBProvider::ID = 0;
189 const char *SBProvider::Info::name = "sbapi";
190 const char *SBProvider::Info::file = "sbapi.bin";
191