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 
26 #include "lldb/Host/FileSystem.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   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 
134   repro::Loader *loader = repro::Reproducer::Instance().GetLoader();
135   if (!loader) {
136     error = "unable to get replay loader.";
137     return error.c_str();
138   }
139 
140   FileSpec file = loader->GetFile<SBProvider::Info>();
141   if (!file) {
142     error = "unable to get replay data from reproducer.";
143     return error.c_str();
144   }
145 
146   SBRegistry registry;
147   registry.Replay(file);
148 
149   return nullptr;
150 }
151 
152 bool SBReproducer::Generate() {
153   auto &r = Reproducer::Instance();
154   if (auto generator = r.GetGenerator()) {
155     generator->Keep();
156     return true;
157   }
158   return false;
159 }
160 
161 const char *SBReproducer::GetPath() {
162   static std::string path;
163   auto &r = Reproducer::Instance();
164   path = r.GetReproducerPath().GetCString();
165   return path.c_str();
166 }
167 
168 char lldb_private::repro::SBProvider::ID = 0;
169 const char *SBProvider::Info::name = "sbapi";
170 const char *SBProvider::Info::file = "sbapi.bin";
171