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 115 if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) { 116 auto &p = g->GetOrCreate<SBProvider>(); 117 InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry()); 118 } 119 120 return nullptr; 121 } 122 123 const char *SBReproducer::Capture(const char *path) { 124 static std::string error; 125 if (auto e = 126 Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) { 127 error = llvm::toString(std::move(e)); 128 return error.c_str(); 129 } 130 131 if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) { 132 auto &p = g->GetOrCreate<SBProvider>(); 133 InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry()); 134 } 135 136 return nullptr; 137 } 138 139 const char *SBReproducer::PassiveReplay(const char *path) { 140 static std::string error; 141 if (auto e = Reproducer::Initialize(ReproducerMode::PassiveReplay, 142 FileSpec(path))) { 143 error = llvm::toString(std::move(e)); 144 return error.c_str(); 145 } 146 147 if (auto *l = lldb_private::repro::Reproducer::Instance().GetLoader()) { 148 FileSpec file = l->GetFile<SBProvider::Info>(); 149 auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath()); 150 if (!error_or_file) { 151 error = 152 "unable to read SB API data: " + error_or_file.getError().message(); 153 return error.c_str(); 154 } 155 static ReplayData r(std::move(*error_or_file)); 156 InstrumentationData::Initialize(r.GetDeserializer(), r.GetRegistry()); 157 } 158 159 return nullptr; 160 } 161 162 const char *SBReproducer::Replay(const char *path) { 163 return SBReproducer::Replay(path, false); 164 } 165 166 const char *SBReproducer::Replay(const char *path, bool skip_version_check) { 167 static std::string error; 168 if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) { 169 error = llvm::toString(std::move(e)); 170 return error.c_str(); 171 } 172 173 repro::Loader *loader = repro::Reproducer::Instance().GetLoader(); 174 if (!loader) { 175 error = "unable to get replay loader."; 176 return error.c_str(); 177 } 178 179 if (!skip_version_check) { 180 llvm::Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); 181 if (!version) { 182 error = llvm::toString(version.takeError()); 183 return error.c_str(); 184 } 185 if (lldb_private::GetVersion() != llvm::StringRef(*version).rtrim()) { 186 error = "reproducer capture and replay version don't match:\n"; 187 error.append("reproducer captured with:\n"); 188 error.append(*version); 189 error.append("reproducer replayed with:\n"); 190 error.append(lldb_private::GetVersion()); 191 return error.c_str(); 192 } 193 } 194 195 FileSpec file = loader->GetFile<SBProvider::Info>(); 196 if (!file) { 197 error = "unable to get replay data from reproducer."; 198 return error.c_str(); 199 } 200 201 SBRegistry registry; 202 registry.Replay(file); 203 204 return nullptr; 205 } 206 207 bool SBReproducer::Generate() { 208 auto &r = Reproducer::Instance(); 209 if (auto generator = r.GetGenerator()) { 210 generator->Keep(); 211 return true; 212 } 213 return false; 214 } 215 216 bool SBReproducer::SetAutoGenerate(bool b) { 217 auto &r = Reproducer::Instance(); 218 if (auto generator = r.GetGenerator()) { 219 generator->SetAutoGenerate(b); 220 return true; 221 } 222 return false; 223 } 224 225 const char *SBReproducer::GetPath() { 226 static std::string path; 227 auto &r = Reproducer::Instance(); 228 path = r.GetReproducerPath().GetCString(); 229 return path.c_str(); 230 } 231 232 char lldb_private::repro::SBProvider::ID = 0; 233 const char *SBProvider::Info::name = "sbapi"; 234 const char *SBProvider::Info::file = "sbapi.bin"; 235