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 "lldb/API/SBReproducer.h" 10 #include "lldb/API/LLDB.h" 11 #include "lldb/API/SBAddress.h" 12 #include "lldb/API/SBAttachInfo.h" 13 #include "lldb/API/SBBlock.h" 14 #include "lldb/API/SBBreakpoint.h" 15 #include "lldb/API/SBCommandInterpreter.h" 16 #include "lldb/API/SBCommandInterpreterRunOptions.h" 17 #include "lldb/API/SBData.h" 18 #include "lldb/API/SBDebugger.h" 19 #include "lldb/API/SBDeclaration.h" 20 #include "lldb/API/SBError.h" 21 #include "lldb/API/SBFileSpec.h" 22 #include "lldb/API/SBHostOS.h" 23 #include "lldb/Host/FileSystem.h" 24 #include "lldb/Utility/Instrumentation.h" 25 #include "lldb/Utility/Reproducer.h" 26 #include "lldb/Utility/ReproducerProvider.h" 27 #include "lldb/Version/Version.h" 28 29 using namespace lldb; 30 using namespace lldb_private; 31 using namespace lldb_private::repro; 32 33 SBReplayOptions::SBReplayOptions() 34 : m_opaque_up(std::make_unique<ReplayOptions>()){} 35 36 SBReplayOptions::SBReplayOptions(const SBReplayOptions &rhs) 37 : m_opaque_up(std::make_unique<ReplayOptions>(*rhs.m_opaque_up)) {} 38 39 SBReplayOptions::~SBReplayOptions() = default; 40 41 SBReplayOptions &SBReplayOptions::operator=(const SBReplayOptions &rhs) { 42 LLDB_INSTRUMENT_VA(this, rhs) 43 if (this == &rhs) 44 return *this; 45 *m_opaque_up = *rhs.m_opaque_up; 46 return *this; 47 } 48 49 void SBReplayOptions::SetVerify(bool verify) { 50 LLDB_INSTRUMENT_VA(this, verify) m_opaque_up->verify = verify; 51 } 52 53 bool SBReplayOptions::GetVerify() const { 54 LLDB_INSTRUMENT_VA(this) return m_opaque_up->verify; 55 } 56 57 void SBReplayOptions::SetCheckVersion(bool check) { 58 LLDB_INSTRUMENT_VA(this, check) 59 m_opaque_up->check_version = check; 60 } 61 62 bool SBReplayOptions::GetCheckVersion() const { 63 LLDB_INSTRUMENT_VA(this) 64 return m_opaque_up->check_version; 65 } 66 67 const char *SBReproducer::Capture() { 68 LLDB_INSTRUMENT() 69 static std::string error; 70 if (auto e = Reproducer::Initialize(ReproducerMode::Capture, llvm::None)) { 71 error = llvm::toString(std::move(e)); 72 return error.c_str(); 73 } 74 75 return nullptr; 76 } 77 78 const char *SBReproducer::Capture(const char *path) { 79 LLDB_INSTRUMENT_VA(path) 80 static std::string error; 81 if (auto e = 82 Reproducer::Initialize(ReproducerMode::Capture, FileSpec(path))) { 83 error = llvm::toString(std::move(e)); 84 return error.c_str(); 85 } 86 87 return nullptr; 88 } 89 90 const char *SBReproducer::PassiveReplay(const char *path) { 91 LLDB_INSTRUMENT_VA(path) 92 return "Reproducer replay has been removed"; 93 } 94 95 const char *SBReproducer::Replay(const char *path) { 96 LLDB_INSTRUMENT_VA(path) 97 return "Reproducer replay has been removed"; 98 } 99 100 const char *SBReproducer::Replay(const char *path, bool skip_version_check) { 101 LLDB_INSTRUMENT_VA(path, skip_version_check) 102 return Replay(path); 103 } 104 105 const char *SBReproducer::Replay(const char *path, 106 const SBReplayOptions &options) { 107 LLDB_INSTRUMENT_VA(path, options) 108 return Replay(path); 109 } 110 111 const char *SBReproducer::Finalize(const char *path) { 112 LLDB_INSTRUMENT_VA(path) 113 static std::string error; 114 115 repro::Loader *loader = repro::Reproducer::Instance().GetLoader(); 116 if (!loader) { 117 error = "unable to get replay loader."; 118 return error.c_str(); 119 } 120 121 if (auto e = repro::Finalize(loader)) { 122 error = llvm::toString(std::move(e)); 123 return error.c_str(); 124 } 125 126 return nullptr; 127 } 128 129 bool SBReproducer::Generate() { 130 LLDB_INSTRUMENT() 131 auto &r = Reproducer::Instance(); 132 if (auto generator = r.GetGenerator()) { 133 generator->Keep(); 134 return true; 135 } 136 return false; 137 } 138 139 bool SBReproducer::SetAutoGenerate(bool b) { 140 LLDB_INSTRUMENT_VA(b) 141 auto &r = Reproducer::Instance(); 142 if (auto generator = r.GetGenerator()) { 143 generator->SetAutoGenerate(b); 144 return true; 145 } 146 return false; 147 } 148 149 const char *SBReproducer::GetPath() { 150 LLDB_INSTRUMENT() 151 ConstString path; 152 auto &r = Reproducer::Instance(); 153 if (FileSpec reproducer_path = Reproducer::Instance().GetReproducerPath()) 154 path = ConstString(r.GetReproducerPath().GetCString()); 155 return path.GetCString(); 156 } 157 158 void SBReproducer::SetWorkingDirectory(const char *path) { 159 LLDB_INSTRUMENT_VA(path) 160 if (auto *g = lldb_private::repro::Reproducer::Instance().GetGenerator()) { 161 auto &wp = g->GetOrCreate<repro::WorkingDirectoryProvider>(); 162 wp.SetDirectory(path); 163 auto &fp = g->GetOrCreate<repro::FileProvider>(); 164 fp.RecordInterestingDirectory(wp.GetDirectory()); 165 } 166 } 167