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 "lldb/API/LLDB.h" 12 #include "lldb/API/SBAddress.h" 13 #include "lldb/API/SBAttachInfo.h" 14 #include "lldb/API/SBBlock.h" 15 #include "lldb/API/SBBreakpoint.h" 16 #include "lldb/API/SBCommandInterpreter.h" 17 #include "lldb/API/SBCommandInterpreterRunOptions.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 SBReplayOptions::SBReplayOptions() 33 : m_opaque_up(std::make_unique<ReplayOptions>()){} 34 35 SBReplayOptions::SBReplayOptions(const SBReplayOptions &rhs) 36 : m_opaque_up(std::make_unique<ReplayOptions>(*rhs.m_opaque_up)) {} 37 38 SBReplayOptions::~SBReplayOptions() = default; 39 40 SBReplayOptions &SBReplayOptions::operator=(const SBReplayOptions &rhs) { 41 if (this == &rhs) 42 return *this; 43 *m_opaque_up = *rhs.m_opaque_up; 44 return *this; 45 } 46 47 void SBReplayOptions::SetVerify(bool verify) { m_opaque_up->verify = verify; } 48 49 bool SBReplayOptions::GetVerify() const { return m_opaque_up->verify; } 50 51 void SBReplayOptions::SetCheckVersion(bool check) { 52 m_opaque_up->check_version = check; 53 } 54 55 bool SBReplayOptions::GetCheckVersion() const { 56 return m_opaque_up->check_version; 57 } 58 59 SBRegistry::SBRegistry() { 60 Registry &R = *this; 61 62 RegisterMethods<SBAddress>(R); 63 RegisterMethods<SBAttachInfo>(R); 64 RegisterMethods<SBBlock>(R); 65 RegisterMethods<SBBreakpoint>(R); 66 RegisterMethods<SBBreakpointList>(R); 67 RegisterMethods<SBBreakpointLocation>(R); 68 RegisterMethods<SBBreakpointName>(R); 69 RegisterMethods<SBBroadcaster>(R); 70 RegisterMethods<SBCommandInterpreter>(R); 71 RegisterMethods<SBCommandInterpreterRunOptions>(R); 72 RegisterMethods<SBCommandReturnObject>(R); 73 RegisterMethods<SBCommunication>(R); 74 RegisterMethods<SBCompileUnit>(R); 75 RegisterMethods<SBData>(R); 76 RegisterMethods<SBDebugger>(R); 77 RegisterMethods<SBDeclaration>(R); 78 RegisterMethods<SBEnvironment>(R); 79 RegisterMethods<SBError>(R); 80 RegisterMethods<SBEvent>(R); 81 RegisterMethods<SBExecutionContext>(R); 82 RegisterMethods<SBExpressionOptions>(R); 83 RegisterMethods<SBFile>(R); 84 RegisterMethods<SBFileSpec>(R); 85 RegisterMethods<SBFileSpecList>(R); 86 RegisterMethods<SBFrame>(R); 87 RegisterMethods<SBFunction>(R); 88 RegisterMethods<SBHostOS>(R); 89 RegisterMethods<SBInputReader>(R); 90 RegisterMethods<SBInstruction>(R); 91 RegisterMethods<SBInstructionList>(R); 92 RegisterMethods<SBLanguageRuntime>(R); 93 RegisterMethods<SBLaunchInfo>(R); 94 RegisterMethods<SBLineEntry>(R); 95 RegisterMethods<SBListener>(R); 96 RegisterMethods<SBMemoryRegionInfo>(R); 97 RegisterMethods<SBMemoryRegionInfoList>(R); 98 RegisterMethods<SBModule>(R); 99 RegisterMethods<SBModuleSpec>(R); 100 RegisterMethods<SBPlatform>(R); 101 RegisterMethods<SBPlatformConnectOptions>(R); 102 RegisterMethods<SBPlatformShellCommand>(R); 103 RegisterMethods<SBProcess>(R); 104 RegisterMethods<SBProcessInfo>(R); 105 RegisterMethods<SBQueue>(R); 106 RegisterMethods<SBQueueItem>(R); 107 RegisterMethods<SBSection>(R); 108 RegisterMethods<SBSourceManager>(R); 109 RegisterMethods<SBStream>(R); 110 RegisterMethods<SBStringList>(R); 111 RegisterMethods<SBStructuredData>(R); 112 RegisterMethods<SBSymbol>(R); 113 RegisterMethods<SBSymbolContext>(R); 114 RegisterMethods<SBSymbolContextList>(R); 115 RegisterMethods<SBTarget>(R); 116 RegisterMethods<SBThread>(R); 117 RegisterMethods<SBThreadCollection>(R); 118 RegisterMethods<SBThreadPlan>(R); 119 RegisterMethods<SBTrace>(R); 120 RegisterMethods<SBTraceOptions>(R); 121 RegisterMethods<SBType>(R); 122 RegisterMethods<SBTypeCategory>(R); 123 RegisterMethods<SBTypeEnumMember>(R); 124 RegisterMethods<SBTypeFilter>(R); 125 RegisterMethods<SBTypeFormat>(R); 126 RegisterMethods<SBTypeNameSpecifier>(R); 127 RegisterMethods<SBTypeSummary>(R); 128 RegisterMethods<SBTypeSummaryOptions>(R); 129 RegisterMethods<SBTypeSynthetic>(R); 130 RegisterMethods<SBUnixSignals>(R); 131 RegisterMethods<SBValue>(R); 132 RegisterMethods<SBValueList>(R); 133 RegisterMethods<SBVariablesOptions>(R); 134 RegisterMethods<SBWatchpoint>(R); 135 } 136 137 const char *SBReproducer::Capture() { 138 static std::string error; 139 if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) { 140 error = llvm::toString(std::move(e)); 141 return error.c_str(); 142 } 143 144 if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) { 145 auto &p = g->GetOrCreate<SBProvider>(); 146 InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry()); 147 } 148 149 return nullptr; 150 } 151 152 const char *SBReproducer::Capture(const char *path) { 153 static std::string error; 154 if (auto e = 155 Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) { 156 error = llvm::toString(std::move(e)); 157 return error.c_str(); 158 } 159 160 if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) { 161 auto &p = g->GetOrCreate<SBProvider>(); 162 InstrumentationData::Initialize(p.GetSerializer(), p.GetRegistry()); 163 } 164 165 return nullptr; 166 } 167 168 const char *SBReproducer::PassiveReplay(const char *path) { 169 static std::string error; 170 if (auto e = Reproducer::Initialize(ReproducerMode::PassiveReplay, 171 FileSpec(path))) { 172 error = llvm::toString(std::move(e)); 173 return error.c_str(); 174 } 175 176 if (auto *l = lldb_private::repro::Reproducer::Instance().GetLoader()) { 177 FileSpec file = l->GetFile<SBProvider::Info>(); 178 auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath()); 179 if (!error_or_file) { 180 error = 181 "unable to read SB API data: " + error_or_file.getError().message(); 182 return error.c_str(); 183 } 184 static ReplayData r(std::move(*error_or_file)); 185 InstrumentationData::Initialize(r.GetDeserializer(), r.GetRegistry()); 186 } 187 188 return nullptr; 189 } 190 191 const char *SBReproducer::Replay(const char *path) { 192 SBReplayOptions options; 193 return SBReproducer::Replay(path, options); 194 } 195 196 const char *SBReproducer::Replay(const char *path, bool skip_version_check) { 197 SBReplayOptions options; 198 options.SetCheckVersion(!skip_version_check); 199 return SBReproducer::Replay(path, options); 200 } 201 202 const char *SBReproducer::Replay(const char *path, 203 const SBReplayOptions &options) { 204 static std::string error; 205 if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) { 206 error = llvm::toString(std::move(e)); 207 return error.c_str(); 208 } 209 210 repro::Loader *loader = repro::Reproducer::Instance().GetLoader(); 211 if (!loader) { 212 error = "unable to get replay loader."; 213 return error.c_str(); 214 } 215 216 if (options.GetCheckVersion()) { 217 llvm::Expected<std::string> version = loader->LoadBuffer<VersionProvider>(); 218 if (!version) { 219 error = llvm::toString(version.takeError()); 220 return error.c_str(); 221 } 222 if (lldb_private::GetVersion() != llvm::StringRef(*version).rtrim()) { 223 error = "reproducer capture and replay version don't match:\n"; 224 error.append("reproducer captured with:\n"); 225 error.append(*version); 226 error.append("reproducer replayed with:\n"); 227 error.append(lldb_private::GetVersion()); 228 return error.c_str(); 229 } 230 } 231 232 if (options.GetVerify()) { 233 bool verification_failed = false; 234 llvm::raw_string_ostream os(error); 235 auto error_callback = [&](llvm::StringRef error) { 236 verification_failed = true; 237 os << "\nerror: " << error; 238 }; 239 240 auto warning_callback = [&](llvm::StringRef warning) { 241 verification_failed = true; 242 os << "\nwarning: " << warning; 243 }; 244 245 auto note_callback = [&](llvm::StringRef warning) {}; 246 247 Verifier verifier(loader); 248 verifier.Verify(error_callback, warning_callback, note_callback); 249 250 if (verification_failed) { 251 os.flush(); 252 return error.c_str(); 253 } 254 } 255 256 FileSpec file = loader->GetFile<SBProvider::Info>(); 257 if (!file) { 258 error = "unable to get replay data from reproducer."; 259 return error.c_str(); 260 } 261 262 SBRegistry registry; 263 registry.Replay(file); 264 265 return nullptr; 266 } 267 268 const char *SBReproducer::Finalize(const char *path) { 269 static std::string error; 270 if (auto e = Reproducer::Initialize(ReproducerMode::Replay, FileSpec(path))) { 271 error = llvm::toString(std::move(e)); 272 return error.c_str(); 273 } 274 275 repro::Loader *loader = repro::Reproducer::Instance().GetLoader(); 276 if (!loader) { 277 error = "unable to get replay loader."; 278 return error.c_str(); 279 } 280 281 if (auto e = repro::Finalize(loader)) { 282 error = llvm::toString(std::move(e)); 283 return error.c_str(); 284 } 285 286 return nullptr; 287 } 288 289 bool SBReproducer::Generate() { 290 auto &r = Reproducer::Instance(); 291 if (auto generator = r.GetGenerator()) { 292 generator->Keep(); 293 return true; 294 } 295 return false; 296 } 297 298 bool SBReproducer::SetAutoGenerate(bool b) { 299 auto &r = Reproducer::Instance(); 300 if (auto generator = r.GetGenerator()) { 301 generator->SetAutoGenerate(b); 302 return true; 303 } 304 return false; 305 } 306 307 const char *SBReproducer::GetPath() { 308 ConstString path; 309 auto &r = Reproducer::Instance(); 310 if (FileSpec reproducer_path = Reproducer::Instance().GetReproducerPath()) 311 path = ConstString(r.GetReproducerPath().GetCString()); 312 return path.GetCString(); 313 } 314 315 void SBReproducer::SetWorkingDirectory(const char *path) { 316 if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) { 317 auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>(); 318 wp.SetDirectory(path); 319 auto &fp = g->GetOrCreate<repro::FileProvider>(); 320 fp.RecordInterestingDirectory(wp.GetDirectory()); 321 } 322 } 323 324 char lldb_private::repro::SBProvider::ID = 0; 325 const char *SBProvider::Info::name = "sbapi"; 326 const char *SBProvider::Info::file = "sbapi.bin"; 327