LLDB  mainline
Reproducer.cpp
Go to the documentation of this file.
1 //===-- Reproducer.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 
12 #include "lldb/Utility/Timer.h"
13 
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Threading.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 using namespace lldb_private;
19 using namespace lldb_private::repro;
20 using namespace llvm;
21 using namespace llvm::yaml;
22 
23 Reproducer &Reproducer::Instance() { return *InstanceImpl(); }
24 
26  llvm::Optional<FileSpec> root) {
27  lldbassert(!InstanceImpl() && "Already initialized.");
28  InstanceImpl().emplace();
29 
30  switch (mode) {
32  if (!root) {
33  SmallString<128> repro_dir;
34  auto ec = sys::fs::createUniqueDirectory("reproducer", repro_dir);
35  if (ec)
36  return make_error<StringError>(
37  "unable to create unique reproducer directory", ec);
38  root.emplace(repro_dir);
39  } else {
40  auto ec = sys::fs::create_directory(root->GetPath());
41  if (ec)
42  return make_error<StringError>("unable to create reproducer directory",
43  ec);
44  }
45  return Instance().SetCapture(root);
46  } break;
48  break;
49  };
50 
51  return Error::success();
52 }
53 
55  llvm::cantFail(Initialize(repro::ReproducerMode::Off, llvm::None));
56 }
57 
58 bool Reproducer::Initialized() { return InstanceImpl().operator bool(); }
59 
61  lldbassert(InstanceImpl() && "Already terminated.");
62  InstanceImpl().reset();
63 }
64 
65 Optional<Reproducer> &Reproducer::InstanceImpl() {
66  static Optional<Reproducer> g_reproducer;
67  return g_reproducer;
68 }
69 
71  std::lock_guard<std::mutex> guard(m_mutex);
72  if (m_generator)
73  return &(*m_generator);
74  return nullptr;
75 }
76 
77 const Loader *Reproducer::GetLoader() const {
78  std::lock_guard<std::mutex> guard(m_mutex);
79  if (m_loader)
80  return &(*m_loader);
81  return nullptr;
82 }
83 
85  std::lock_guard<std::mutex> guard(m_mutex);
86  if (m_generator)
87  return &(*m_generator);
88  return nullptr;
89 }
90 
92  std::lock_guard<std::mutex> guard(m_mutex);
93  if (m_loader)
94  return &(*m_loader);
95  return nullptr;
96 }
97 
98 llvm::Error Reproducer::SetCapture(llvm::Optional<FileSpec> root) {
99  std::lock_guard<std::mutex> guard(m_mutex);
100 
101  if (root && m_loader)
102  return make_error<StringError>(
103  "cannot generate a reproducer when replay one",
104  inconvertibleErrorCode());
105 
106  if (!root) {
107  m_generator.reset();
108  return Error::success();
109  }
110 
111  m_generator.emplace(*root);
112  return Error::success();
113 }
114 
116  if (auto g = GetGenerator())
117  return g->GetRoot();
118  if (auto l = GetLoader())
119  return l->GetRoot();
120  return {};
121 }
122 
123 static FileSpec MakeAbsolute(const FileSpec &file_spec) {
124  SmallString<128> path;
125  file_spec.GetPath(path, false);
126  llvm::sys::fs::make_absolute(path);
127  return FileSpec(path, file_spec.GetPathStyle());
128 }
129 
130 Generator::Generator(FileSpec root) : m_root(MakeAbsolute(std::move(root))) {
131  GetOrCreate<repro::WorkingDirectoryProvider>();
132  GetOrCreate<repro::HomeDirectoryProvider>();
133 }
134 
136  if (!m_done) {
137  if (m_auto_generate) {
138  Keep();
139  } else {
140  Discard();
141  }
142  }
143 }
144 
145 ProviderBase *Generator::Register(std::unique_ptr<ProviderBase> provider) {
146  std::lock_guard<std::mutex> lock(m_providers_mutex);
147  std::pair<const void *, std::unique_ptr<ProviderBase>> key_value(
148  provider->DynamicClassID(), std::move(provider));
149  auto e = m_providers.insert(std::move(key_value));
150  return e.first->getSecond().get();
151 }
152 
155  assert(!m_done);
156  m_done = true;
157 
158  for (auto &provider : m_providers)
159  provider.second->Keep();
160 
162 }
163 
166  assert(!m_done);
167  m_done = true;
168 
169  for (auto &provider : m_providers)
170  provider.second->Discard();
171 
172  llvm::sys::fs::remove_directories(m_root.GetPath());
173 }
174 
176 
178 
179 const FileSpec &Generator::GetRoot() const { return m_root; }
180 
182  FileSpec index = m_root;
183  index.AppendPathComponent("index.yaml");
184 
185  std::error_code EC;
186  auto strm = std::make_unique<raw_fd_ostream>(index.GetPath(), EC,
187  sys::fs::OpenFlags::OF_None);
188  yaml::Output yout(*strm);
189 
190  std::vector<std::string> files;
191  files.reserve(m_providers.size());
192  for (auto &provider : m_providers) {
193  files.emplace_back(provider.second->GetFile());
194  }
195 
196  yout << files;
197 }
198 
199 Loader::Loader(FileSpec root, bool passive)
200  : m_root(MakeAbsolute(std::move(root))), m_loaded(false) {}
201 
203  if (m_loaded)
204  return llvm::Error::success();
205 
206  FileSpec index = m_root.CopyByAppendingPathComponent("index.yaml");
207 
208  auto error_or_file = MemoryBuffer::getFile(index.GetPath());
209  if (auto err = error_or_file.getError())
210  return make_error<StringError>("unable to load reproducer index", err);
211 
212  yaml::Input yin((*error_or_file)->getBuffer());
213  yin >> m_files;
214  if (auto err = yin.error())
215  return make_error<StringError>("unable to read reproducer index", err);
216 
217  // Sort files to speed up search.
218  llvm::sort(m_files);
219 
220  // Remember that we've loaded the index.
221  m_loaded = true;
222 
223  return llvm::Error::success();
224 }
225 
226 bool Loader::HasFile(StringRef file) {
227  assert(m_loaded);
228  auto it = std::lower_bound(m_files.begin(), m_files.end(), file.str());
229  return (it != m_files.end()) && (*it == file);
230 }
lldb_private::repro::Loader::LoadIndex
llvm::Error LoadIndex()
Definition: Reproducer.cpp:202
lldb_private::repro::Generator::Discard
void Discard()
Method to indicate we do not want to keep the reproducer.
Definition: Reproducer.cpp:164
llvm
Definition: Debugger.h:50
lldb_private::repro::Loader::m_files
std::vector< std::string > m_files
Definition: Reproducer.h:184
lldb_private::repro::Reproducer::InstanceImpl
static llvm::Optional< Reproducer > & InstanceImpl()
Definition: Reproducer.cpp:65
lldb_private::repro::Reproducer::Initialize
static void Initialize()
Definition: Reproducer.cpp:54
lldb_private::repro::Generator::m_providers
llvm::DenseMap< const void *, std::unique_ptr< ProviderBase > > m_providers
Map of provider IDs to provider instances.
Definition: Reproducer.h:143
lldb_private::repro::Loader::Loader
Loader(FileSpec root, bool passive=false)
Definition: Reproducer.cpp:199
lldb_private::repro::Loader::m_loaded
bool m_loaded
Definition: Reproducer.h:185
lldb_private::FileSpec::GetPathStyle
Style GetPathStyle() const
Definition: FileSpec.cpp:331
Reproducer.h
lldb_private::repro::ReproducerMode::Off
@ Off
lldb_private::repro::Generator::SetAutoGenerate
void SetAutoGenerate(bool b)
Enable or disable auto generate.
Definition: Reproducer.cpp:175
lldb_private::repro::Reproducer::GetLoader
Loader * GetLoader()
Definition: Reproducer.cpp:77
lldb_private::repro::Loader
Definition: Reproducer.h:156
lldb_private::repro::Generator::m_done
bool m_done
Flag to ensure that we never call both keep and discard.
Definition: Reproducer.h:150
lldb_private::repro::Generator::AddProvidersToIndex
void AddProvidersToIndex()
Builds and index with provider info.
Definition: Reproducer.cpp:181
ReproducerProvider.h
lldb_private::FileSpec
Definition: FileSpec.h:56
LLDBAssert.h
lldb_private::repro::Generator::m_auto_generate
bool m_auto_generate
Flag to auto generate a reproducer when it would otherwise be discarded.
Definition: Reproducer.h:153
MakeAbsolute
static FileSpec MakeAbsolute(const FileSpec &file_spec)
Definition: Reproducer.cpp:123
lldb_private::repro::Reproducer::GetReproducerPath
FileSpec GetReproducerPath() const
Definition: Reproducer.cpp:115
Timer.h
lldb_private::repro::Generator::Generator
Generator(FileSpec root)
Definition: Reproducer.cpp:130
LLDB_SCOPED_TIMER
#define LLDB_SCOPED_TIMER()
Definition: Timer.h:83
lldb_private::repro::ReproducerMode
ReproducerMode
Definition: Reproducer.h:30
lldb_private::repro::Generator::m_root
FileSpec m_root
The reproducer root directory.
Definition: Reproducer.h:147
lldb_private::repro::Generator::m_providers_mutex
std::mutex m_providers_mutex
Definition: Reproducer.h:144
lldb_private::FileSpec::CopyByAppendingPathComponent
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
Definition: FileSpec.cpp:399
lldb_private::repro::Loader::m_root
FileSpec m_root
Definition: Reproducer.h:183
lldb_private::repro::Reproducer::Terminate
static void Terminate()
Definition: Reproducer.cpp:60
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::repro::Reproducer::SetCapture
llvm::Error SetCapture(llvm::Optional< FileSpec > root)
Definition: Reproducer.cpp:98
lldb_private::repro::Generator::GetRoot
const FileSpec & GetRoot() const
Definition: Reproducer.cpp:179
lldb_private::repro::Reproducer::Initialized
static bool Initialized()
Definition: Reproducer.cpp:58
lldb_private::repro::Reproducer::GetGenerator
Generator * GetGenerator()
Definition: Reproducer.cpp:70
lldb_private::repro::Generator::IsAutoGenerate
bool IsAutoGenerate() const
Return whether auto generate is enabled.
Definition: Reproducer.cpp:177
lldb_private::repro::Loader::HasFile
bool HasFile(llvm::StringRef file)
Definition: Reproducer.cpp:226
lldb_private::repro
Definition: SBReproducer.h:15
lldb_private::repro::Generator::Keep
void Keep()
Method to indicate we want to keep the reproducer.
Definition: Reproducer.cpp:153
lldb_private::repro::ReproducerMode::Capture
@ Capture
llvm::yaml
Definition: ArchSpec.h:567
lldb_private::FileSpec::AppendPathComponent
void AppendPathComponent(llvm::StringRef component)
Definition: FileSpec.cpp:434
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:30
lldb_private::repro::ProviderBase
The provider defines an interface for generating files needed for reproducing.
Definition: Reproducer.h:39
lldb_private::repro::Reproducer
The reproducer enables clients to obtain access to the Generator and Loader.
Definition: Reproducer.h:190
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:347
lldb_private::repro::Reproducer::Instance
static Reproducer & Instance()
Definition: Reproducer.cpp:23
lldb_private::repro::Generator
The generator is responsible for the logic needed to generate a reproducer.
Definition: Reproducer.h:89
lldb_private::repro::Generator::Register
ProviderBase * Register(std::unique_ptr< ProviderBase > provider)
Definition: Reproducer.cpp:145
lldb_private::repro::Generator::~Generator
~Generator()
Definition: Reproducer.cpp:135