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 
11 
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Threading.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 using namespace lldb_private;
17 using namespace lldb_private::repro;
18 using namespace llvm;
19 using namespace llvm::yaml;
20 
21 static llvm::Optional<bool> GetEnv(const char *var) {
22  std::string val = llvm::StringRef(getenv(var)).lower();
23  if (val == "0" || val == "off")
24  return false;
25  if (val == "1" || val == "on")
26  return true;
27  return {};
28 }
29 
30 Reproducer &Reproducer::Instance() { return *InstanceImpl(); }
31 
33  llvm::Optional<FileSpec> root) {
34  lldbassert(!InstanceImpl() && "Already initialized.");
35  InstanceImpl().emplace();
36 
37  // The environment can override the capture mode.
38  if (mode != ReproducerMode::Replay) {
39  if (llvm::Optional<bool> override = GetEnv("LLDB_CAPTURE_REPRODUCER")) {
40  if (*override)
42  else
43  mode = ReproducerMode::Off;
44  }
45  }
46 
47  switch (mode) {
49  if (!root) {
50  SmallString<128> repro_dir;
51  auto ec = sys::fs::createUniqueDirectory("reproducer", repro_dir);
52  if (ec)
53  return make_error<StringError>(
54  "unable to create unique reproducer directory", ec);
55  root.emplace(repro_dir);
56  } else {
57  auto ec = sys::fs::create_directory(root->GetPath());
58  if (ec)
59  return make_error<StringError>("unable to create reproducer directory",
60  ec);
61  }
62  return Instance().SetCapture(root);
63  } break;
65  return Instance().SetReplay(root, /*passive*/ false);
67  return Instance().SetReplay(root, /*passive*/ true);
69  break;
70  };
71 
72  return Error::success();
73 }
74 
75 bool Reproducer::Initialized() { return InstanceImpl().operator bool(); }
76 
78  lldbassert(InstanceImpl() && "Already terminated.");
79  InstanceImpl().reset();
80 }
81 
82 Optional<Reproducer> &Reproducer::InstanceImpl() {
83  static Optional<Reproducer> g_reproducer;
84  return g_reproducer;
85 }
86 
88  std::lock_guard<std::mutex> guard(m_mutex);
89  if (m_generator)
90  return &(*m_generator);
91  return nullptr;
92 }
93 
94 const Loader *Reproducer::GetLoader() const {
95  std::lock_guard<std::mutex> guard(m_mutex);
96  if (m_loader)
97  return &(*m_loader);
98  return nullptr;
99 }
100 
102  std::lock_guard<std::mutex> guard(m_mutex);
103  if (m_generator)
104  return &(*m_generator);
105  return nullptr;
106 }
107 
109  std::lock_guard<std::mutex> guard(m_mutex);
110  if (m_loader)
111  return &(*m_loader);
112  return nullptr;
113 }
114 
115 llvm::Error Reproducer::SetCapture(llvm::Optional<FileSpec> root) {
116  std::lock_guard<std::mutex> guard(m_mutex);
117 
118  if (root && m_loader)
119  return make_error<StringError>(
120  "cannot generate a reproducer when replay one",
121  inconvertibleErrorCode());
122 
123  if (!root) {
124  m_generator.reset();
125  return Error::success();
126  }
127 
128  m_generator.emplace(*root);
129  return Error::success();
130 }
131 
132 llvm::Error Reproducer::SetReplay(llvm::Optional<FileSpec> root, bool passive) {
133  std::lock_guard<std::mutex> guard(m_mutex);
134 
135  if (root && m_generator)
136  return make_error<StringError>(
137  "cannot replay a reproducer when generating one",
138  inconvertibleErrorCode());
139 
140  if (!root) {
141  m_loader.reset();
142  return Error::success();
143  }
144 
145  m_loader.emplace(*root, passive);
146  if (auto e = m_loader->LoadIndex())
147  return e;
148 
149  return Error::success();
150 }
151 
153  if (auto g = GetGenerator())
154  return g->GetRoot();
155  if (auto l = GetLoader())
156  return l->GetRoot();
157  return {};
158 }
159 
160 static FileSpec MakeAbsolute(FileSpec file_spec) {
161  SmallString<128> path;
162  file_spec.GetPath(path, false);
163  llvm::sys::fs::make_absolute(path);
164  return FileSpec(path, file_spec.GetPathStyle());
165 }
166 
167 Generator::Generator(FileSpec root) : m_root(MakeAbsolute(std::move(root))) {
168  GetOrCreate<repro::WorkingDirectoryProvider>();
169 }
170 
172  if (!m_done) {
173  if (m_auto_generate)
174  Keep();
175  else
176  Discard();
177  }
178 }
179 
180 ProviderBase *Generator::Register(std::unique_ptr<ProviderBase> provider) {
181  std::lock_guard<std::mutex> lock(m_providers_mutex);
182  std::pair<const void *, std::unique_ptr<ProviderBase>> key_value(
183  provider->DynamicClassID(), std::move(provider));
184  auto e = m_providers.insert(std::move(key_value));
185  return e.first->getSecond().get();
186 }
187 
189  assert(!m_done);
190  m_done = true;
191 
192  for (auto &provider : m_providers)
193  provider.second->Keep();
194 
196 }
197 
199  assert(!m_done);
200  m_done = true;
201 
202  for (auto &provider : m_providers)
203  provider.second->Discard();
204 
205  llvm::sys::fs::remove_directories(m_root.GetPath());
206 }
207 
209 
211 
212 const FileSpec &Generator::GetRoot() const { return m_root; }
213 
215  FileSpec index = m_root;
216  index.AppendPathComponent("index.yaml");
217 
218  std::error_code EC;
219  auto strm = std::make_unique<raw_fd_ostream>(index.GetPath(), EC,
220  sys::fs::OpenFlags::OF_None);
221  yaml::Output yout(*strm);
222 
223  std::vector<std::string> files;
224  files.reserve(m_providers.size());
225  for (auto &provider : m_providers) {
226  files.emplace_back(provider.second->GetFile());
227  }
228 
229  yout << files;
230 }
231 
232 Loader::Loader(FileSpec root, bool passive)
233  : m_root(MakeAbsolute(std::move(root))), m_loaded(false),
234  m_passive_replay(passive) {}
235 
237  if (m_loaded)
238  return llvm::Error::success();
239 
240  FileSpec index = m_root.CopyByAppendingPathComponent("index.yaml");
241 
242  auto error_or_file = MemoryBuffer::getFile(index.GetPath());
243  if (auto err = error_or_file.getError())
244  return make_error<StringError>("unable to load reproducer index", err);
245 
246  yaml::Input yin((*error_or_file)->getBuffer());
247  yin >> m_files;
248  if (auto err = yin.error())
249  return make_error<StringError>("unable to read reproducer index", err);
250 
251  // Sort files to speed up search.
252  llvm::sort(m_files);
253 
254  // Remember that we've loaded the index.
255  m_loaded = true;
256 
257  return llvm::Error::success();
258 }
259 
260 bool Loader::HasFile(StringRef file) {
261  assert(m_loaded);
262  auto it = std::lower_bound(m_files.begin(), m_files.end(), file.str());
263  return (it != m_files.end()) && (*it == file);
264 }
265 
266 llvm::Expected<std::unique_ptr<DataRecorder>>
267 DataRecorder::Create(const FileSpec &filename) {
268  std::error_code ec;
269  auto recorder = std::make_unique<DataRecorder>(std::move(filename), ec);
270  if (ec)
271  return llvm::errorCodeToError(ec);
272  return std::move(recorder);
273 }
274 
276  std::size_t i = m_data_recorders.size() + 1;
277  std::string filename = (llvm::Twine(Info::name) + llvm::Twine("-") +
278  llvm::Twine(i) + llvm::Twine(".yaml"))
279  .str();
280  auto recorder_or_error =
281  DataRecorder::Create(GetRoot().CopyByAppendingPathComponent(filename));
282  if (!recorder_or_error) {
283  llvm::consumeError(recorder_or_error.takeError());
284  return nullptr;
285  }
286 
287  m_data_recorders.push_back(std::move(*recorder_or_error));
288  return m_data_recorders.back().get();
289 }
290 
292  std::vector<std::string> files;
293  for (auto &recorder : m_data_recorders) {
294  recorder->Stop();
295  files.push_back(recorder->GetFilename().GetPath());
296  }
297 
298  FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
299  std::error_code ec;
300  llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
301  if (ec)
302  return;
303  yaml::Output yout(os);
304  yout << files;
305 }
306 
307 void CommandProvider::Discard() { m_data_recorders.clear(); }
308 
310  FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
311  std::error_code ec;
312  llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
313  if (ec)
314  return;
315  os << m_version << "\n";
316 }
317 
319  FileSpec file = GetRoot().CopyByAppendingPathComponent(Info::file);
320  std::error_code ec;
321  llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text);
322  if (ec)
323  return;
324  os << m_cwd << "\n";
325 }
326 
327 void FileProvider::recordInterestingDirectory(const llvm::Twine &dir) {
328  if (m_collector)
329  m_collector->addDirectory(dir);
330 }
331 
333 char CommandProvider::ID = 0;
334 char FileProvider::ID = 0;
335 char ProviderBase::ID = 0;
336 char VersionProvider::ID = 0;
338 const char *CommandProvider::Info::file = "command-interpreter.yaml";
339 const char *CommandProvider::Info::name = "command-interpreter";
340 const char *FileProvider::Info::file = "files.yaml";
341 const char *FileProvider::Info::name = "files";
342 const char *VersionProvider::Info::file = "version.txt";
343 const char *VersionProvider::Info::name = "version";
344 const char *WorkingDirectoryProvider::Info::file = "cwd.txt";
345 const char *WorkingDirectoryProvider::Info::name = "cwd";
void Keep() override
The Keep method is called when it is decided that we need to keep the data in order to provide a repr...
Definition: Reproducer.cpp:291
A class that represents a running process on the host machine.
Definition: Debugger.h:49
void Discard() override
The Discard method is called when it is decided that we do not need to keep any information and will ...
Definition: Reproducer.cpp:307
#define lldbassert(x)
Definition: LLDBAssert.h:15
void recordInterestingDirectory(const llvm::Twine &dir)
Definition: Reproducer.cpp:327
const FileSpec & GetRoot() const
Definition: Reproducer.h:314
llvm::Error Error
const FileSpec & GetRoot() const
Definition: Reproducer.cpp:212
void Discard()
Method to indicate we do not want to keep the reproducer.
Definition: Reproducer.cpp:198
A file utility class.
Definition: FileSpec.h:56
llvm::Error SetReplay(llvm::Optional< FileSpec > root, bool passive=false)
Definition: Reproducer.cpp:132
void SetAutoGenerate(bool b)
Enable or disable auto generate.
Definition: Reproducer.cpp:208
The reproducer enables clients to obtain access to the Generator and Loader.
Definition: Reproducer.h:329
std::vector< std::string > m_files
Definition: Reproducer.h:322
llvm::Error SetCapture(llvm::Optional< FileSpec > root)
Definition: Reproducer.cpp:115
FileSpec m_root
The reproducer root directory.
Definition: Reproducer.h:283
static llvm::Error Initialize(ReproducerMode mode, llvm::Optional< FileSpec > root)
Definition: Reproducer.cpp:32
bool m_done
Flag to ensure that we never call both keep and discard.
Definition: Reproducer.h:286
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
Definition: FileSpec.cpp:400
void AppendPathComponent(llvm::StringRef component)
Definition: FileSpec.cpp:435
static FileSpec MakeAbsolute(FileSpec file_spec)
Definition: Reproducer.cpp:160
static llvm::Optional< bool > GetEnv(const char *var)
Definition: Reproducer.cpp:21
void Keep() override
The Keep method is called when it is decided that we need to keep the data in order to provide a repr...
Definition: Reproducer.cpp:318
Loader(FileSpec root, bool passive=false)
Definition: Reproducer.cpp:232
void Keep()
Method to indicate we want to keep the reproducer.
Definition: Reproducer.cpp:188
ProviderBase * Register(std::unique_ptr< ProviderBase > provider)
Definition: Reproducer.cpp:180
void AddProvidersToIndex()
Builds and index with provider info.
Definition: Reproducer.cpp:214
static Reproducer & Instance()
Definition: Reproducer.cpp:30
The provider defines an interface for generating files needed for reproducing.
Definition: Reproducer.h:38
Style GetPathStyle() const
Definition: FileSpec.cpp:332
bool IsAutoGenerate() const
Return whether auto generate is enabled.
Definition: Reproducer.cpp:210
bool HasFile(llvm::StringRef file)
Definition: Reproducer.cpp:260
The generator is responsible for the logic needed to generate a reproducer.
Definition: Reproducer.h:225
static llvm::Expected< std::unique_ptr< DataRecorder > > Create(const FileSpec &filename)
Definition: Reproducer.cpp:267
bool m_auto_generate
Flag to auto generate a reproducer when it would otherwise be discarded.
Definition: Reproducer.h:289
llvm::DenseMap< const void *, std::unique_ptr< ProviderBase > > m_providers
Map of provider IDs to provider instances.
Definition: Reproducer.h:279
FileSpec GetReproducerPath() const
Definition: Reproducer.cpp:152
static llvm::Optional< Reproducer > & InstanceImpl()
Definition: Reproducer.cpp:82
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
void Keep() override
The Keep method is called when it is decided that we need to keep the data in order to provide a repr...
Definition: Reproducer.cpp:309