LLDB  mainline
Reproducer.h
Go to the documentation of this file.
1 //===-- Reproducer.h --------------------------------------------*- C++ -*-===//
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 #ifndef LLDB_UTILITY_REPRODUCER_H
10 #define LLDB_UTILITY_REPRODUCER_H
11 
12 #include "lldb/Utility/FileSpec.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/VirtualFileSystem.h"
17 #include "llvm/Support/YAMLTraits.h"
18 
19 #include <mutex>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 namespace lldb_private {
25 class UUID;
26 namespace repro {
27 
28 class Reproducer;
29 
30 enum class ReproducerMode {
31  Capture,
32  Off,
33 };
34 
35 /// The provider defines an interface for generating files needed for
36 /// reproducing.
37 ///
38 /// Different components will implement different providers.
39 class ProviderBase {
40 public:
41  virtual ~ProviderBase() = default;
42 
43  const FileSpec &GetRoot() const { return m_root; }
44 
45  /// The Keep method is called when it is decided that we need to keep the
46  /// data in order to provide a reproducer.
47  virtual void Keep(){};
48 
49  /// The Discard method is called when it is decided that we do not need to
50  /// keep any information and will not generate a reproducer.
51  virtual void Discard(){};
52 
53  // Returns the class ID for this type.
54  static const void *ClassID() { return &ID; }
55 
56  // Returns the class ID for the dynamic type of this Provider instance.
57  virtual const void *DynamicClassID() const = 0;
58 
59  virtual llvm::StringRef GetName() const = 0;
60  virtual llvm::StringRef GetFile() const = 0;
61 
62 protected:
63  ProviderBase(const FileSpec &root) : m_root(root) {}
64 
65 private:
66  /// Every provider knows where to dump its potential files.
68 
69  virtual void anchor();
70  static char ID;
71 };
72 
73 template <typename ThisProviderT> class Provider : public ProviderBase {
74 public:
75  static const void *ClassID() { return &ThisProviderT::ID; }
76 
77  const void *DynamicClassID() const override { return &ThisProviderT::ID; }
78 
79  llvm::StringRef GetName() const override { return ThisProviderT::Info::name; }
80  llvm::StringRef GetFile() const override { return ThisProviderT::Info::file; }
81 
82 protected:
83  using ProviderBase::ProviderBase; // Inherit constructor.
84 };
85 
86 /// The generator is responsible for the logic needed to generate a
87 /// reproducer. For doing so it relies on providers, who serialize data that
88 /// is necessary for reproducing a failure.
89 class Generator final {
90 
91 public:
92  Generator(FileSpec root);
93  ~Generator();
94 
95  /// Method to indicate we want to keep the reproducer. If reproducer
96  /// generation is disabled, this does nothing.
97  void Keep();
98 
99  /// Method to indicate we do not want to keep the reproducer. This is
100  /// unaffected by whether or not generation reproduction is enabled, as we
101  /// might need to clean up files already written to disk.
102  void Discard();
103 
104  /// Enable or disable auto generate.
105  void SetAutoGenerate(bool b);
106 
107  /// Return whether auto generate is enabled.
108  bool IsAutoGenerate() const;
109 
110  /// Create and register a new provider.
111  template <typename T> T *Create() {
112  std::unique_ptr<ProviderBase> provider = std::make_unique<T>(m_root);
113  return static_cast<T *>(Register(std::move(provider)));
114  }
115 
116  /// Get an existing provider.
117  template <typename T> T *Get() {
118  auto it = m_providers.find(T::ClassID());
119  if (it == m_providers.end())
120  return nullptr;
121  return static_cast<T *>(it->second.get());
122  }
123 
124  /// Get a provider if it exists, otherwise create it.
125  template <typename T> T &GetOrCreate() {
126  auto *provider = Get<T>();
127  if (provider)
128  return *provider;
129  return *Create<T>();
130  }
131 
132  const FileSpec &GetRoot() const;
133 
134 private:
135  friend Reproducer;
136 
137  ProviderBase *Register(std::unique_ptr<ProviderBase> provider);
138 
139  /// Builds and index with provider info.
140  void AddProvidersToIndex();
141 
142  /// Map of provider IDs to provider instances.
143  llvm::DenseMap<const void *, std::unique_ptr<ProviderBase>> m_providers;
144  std::mutex m_providers_mutex;
145 
146  /// The reproducer root directory.
148 
149  /// Flag to ensure that we never call both keep and discard.
150  bool m_done = false;
151 
152  /// Flag to auto generate a reproducer when it would otherwise be discarded.
153  bool m_auto_generate = false;
154 };
155 
156 class Loader final {
157 public:
158  Loader(FileSpec root, bool passive = false);
159 
160  template <typename T> FileSpec GetFile() {
161  if (!HasFile(T::file))
162  return {};
163 
164  return GetRoot().CopyByAppendingPathComponent(T::file);
165  }
166 
167  template <typename T> llvm::Expected<std::string> LoadBuffer() {
168  FileSpec file = GetFile<typename T::Info>();
169  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
170  llvm::vfs::getRealFileSystem()->getBufferForFile(file.GetPath());
171  if (!buffer)
172  return llvm::errorCodeToError(buffer.getError());
173  return (*buffer)->getBuffer().str();
174  }
175 
177 
178  const FileSpec &GetRoot() const { return m_root; }
179 
180 private:
181  bool HasFile(llvm::StringRef file);
182 
184  std::vector<std::string> m_files;
185  bool m_loaded;
186 };
187 
188 /// The reproducer enables clients to obtain access to the Generator and
189 /// Loader.
190 class Reproducer {
191 public:
192  static Reproducer &Instance();
194  llvm::Optional<FileSpec> root);
195  static void Initialize();
196  static bool Initialized();
197  static void Terminate();
198 
199  Reproducer() = default;
200 
202  Loader *GetLoader();
203 
204  const Generator *GetGenerator() const;
205  const Loader *GetLoader() const;
206 
207  FileSpec GetReproducerPath() const;
208 
209  bool IsCapturing() { return static_cast<bool>(m_generator); };
210 
211 protected:
212  llvm::Error SetCapture(llvm::Optional<FileSpec> root);
213 
214 private:
215  static llvm::Optional<Reproducer> &InstanceImpl();
216 
217  llvm::Optional<Generator> m_generator;
218  llvm::Optional<Loader> m_loader;
219 
220  mutable std::mutex m_mutex;
221 };
222 
224  bool verify = true;
225  bool check_version = true;
226 };
227 
228 } // namespace repro
229 } // namespace lldb_private
230 
231 #endif // LLDB_UTILITY_REPRODUCER_H
lldb_private::repro::Loader::LoadIndex
llvm::Error LoadIndex()
Definition: Reproducer.cpp:202
lldb_private::repro::ProviderBase::m_root
FileSpec m_root
Every provider knows where to dump its potential files.
Definition: Reproducer.h:67
lldb_private::repro::Generator::Discard
void Discard()
Method to indicate we do not want to keep the reproducer.
Definition: Reproducer.cpp:164
lldb_private::repro::Generator::Get
T * Get()
Get an existing provider.
Definition: Reproducer.h:117
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::m_generator
llvm::Optional< Generator > m_generator
Definition: Reproducer.h:217
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::repro::ProviderBase::GetName
virtual llvm::StringRef GetName() const =0
lldb_private::repro::ReplayOptions::check_version
bool check_version
Definition: Reproducer.h:225
lldb_private::repro::ReproducerMode::Off
@ Off
lldb_private::repro::Provider
Definition: Reproducer.h:73
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::ProviderBase::anchor
virtual void anchor()
Definition: ReproducerProvider.cpp:172
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::ProviderBase::GetFile
virtual llvm::StringRef GetFile() const =0
lldb_private::repro::Generator::AddProvidersToIndex
void AddProvidersToIndex()
Builds and index with provider info.
Definition: Reproducer.cpp:181
lldb_private::repro::Generator::Reproducer
friend Reproducer
Definition: Reproducer.h:135
lldb_private::repro::Provider::ClassID
static const void * ClassID()
Definition: Reproducer.h:75
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::repro::Generator::GetOrCreate
T & GetOrCreate()
Get a provider if it exists, otherwise create it.
Definition: Reproducer.h:125
lldb_private::repro::Reproducer::m_loader
llvm::Optional< Loader > m_loader
Definition: Reproducer.h:218
ID
static char ID
Definition: IRDynamicChecks.cpp:33
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
lldb_private::repro::ReplayOptions::verify
bool verify
Definition: Reproducer.h:224
lldb_private::repro::Loader::LoadBuffer
llvm::Expected< std::string > LoadBuffer()
Definition: Reproducer.h:167
lldb_private::repro::Reproducer::GetReproducerPath
FileSpec GetReproducerPath() const
Definition: Reproducer.cpp:115
lldb_private::repro::ProviderBase::Discard
virtual void Discard()
The Discard method is called when it is decided that we do not need to keep any information and will ...
Definition: Reproducer.h:51
lldb_private::repro::Reproducer::IsCapturing
bool IsCapturing()
Definition: Reproducer.h:209
lldb_private::repro::Generator::Generator
Generator(FileSpec root)
Definition: Reproducer.cpp:130
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::ProviderBase::Keep
virtual void Keep()
The Keep method is called when it is decided that we need to keep the data in order to provide a repr...
Definition: Reproducer.h:47
lldb_private::repro::Loader::m_root
FileSpec m_root
Definition: Reproducer.h:183
lldb_private::repro::ProviderBase::ProviderBase
ProviderBase(const FileSpec &root)
Definition: Reproducer.h:63
lldb_private::repro::Reproducer::Terminate
static void Terminate()
Definition: Reproducer.cpp:60
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::ProviderBase::DynamicClassID
virtual const void * DynamicClassID() const =0
lldb_private::repro::ProviderBase::ClassID
static const void * ClassID()
Definition: Reproducer.h:54
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::Provider::GetFile
llvm::StringRef GetFile() const override
Definition: Reproducer.h:80
lldb_private::repro::Generator::Create
T * Create()
Create and register a new provider.
Definition: Reproducer.h:111
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
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
FileSpec.h
lldb_private::repro::Reproducer::Reproducer
Reproducer()=default
lldb_private::repro::Loader::GetFile
FileSpec GetFile()
Definition: Reproducer.h:160
lldb_private::repro::ProviderBase::ID
static char ID
Definition: Reproducer.h:70
lldb_private::repro::Reproducer
The reproducer enables clients to obtain access to the Generator and Loader.
Definition: Reproducer.h:190
lldb_private::repro::ProviderBase::GetRoot
const FileSpec & GetRoot() const
Definition: Reproducer.h:43
lldb_private::repro::ReplayOptions
Definition: Reproducer.h:223
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::Loader::GetRoot
const FileSpec & GetRoot() const
Definition: Reproducer.h:178
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::ProviderBase::~ProviderBase
virtual ~ProviderBase()=default
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
lldb_private::repro::Provider::GetName
llvm::StringRef GetName() const override
Definition: Reproducer.h:79
lldb_private::repro::Reproducer::m_mutex
std::mutex m_mutex
Definition: Reproducer.h:220
lldb_private::repro::Provider::DynamicClassID
const void * DynamicClassID() const override
Definition: Reproducer.h:77