|
13 | 13 | #include "llvm/ADT/DenseMap.h"
|
14 | 14 | #include "llvm/ADT/StringRef.h"
|
15 | 15 | #include "llvm/Support/Error.h"
|
16 |
| -#include "llvm/Support/FileCollector.h" |
| 16 | +#include "llvm/Support/VirtualFileSystem.h" |
17 | 17 | #include "llvm/Support/YAMLTraits.h"
|
18 | 18 |
|
19 | 19 | #include <mutex>
|
@@ -84,250 +84,6 @@ template <typename ThisProviderT> class Provider : public ProviderBase {
|
84 | 84 | using ProviderBase::ProviderBase; // Inherit constructor.
|
85 | 85 | };
|
86 | 86 |
|
87 |
| -class FileProvider : public Provider<FileProvider> { |
88 |
| -public: |
89 |
| - struct Info { |
90 |
| - static const char *name; |
91 |
| - static const char *file; |
92 |
| - }; |
93 |
| - |
94 |
| - FileProvider(const FileSpec &directory) |
95 |
| - : Provider(directory), |
96 |
| - m_collector(std::make_shared<llvm::FileCollector>( |
97 |
| - directory.CopyByAppendingPathComponent("root").GetPath(), |
98 |
| - directory.GetPath())) {} |
99 |
| - |
100 |
| - std::shared_ptr<llvm::FileCollector> GetFileCollector() { |
101 |
| - return m_collector; |
102 |
| - } |
103 |
| - |
104 |
| - void RecordInterestingDirectory(const llvm::Twine &dir); |
105 |
| - void RecordInterestingDirectoryRecursive(const llvm::Twine &dir); |
106 |
| - |
107 |
| - void Keep() override { |
108 |
| - auto mapping = GetRoot().CopyByAppendingPathComponent(Info::file); |
109 |
| - // Temporary files that are removed during execution can cause copy errors. |
110 |
| - if (auto ec = m_collector->copyFiles(/*stop_on_error=*/false)) |
111 |
| - return; |
112 |
| - m_collector->writeMapping(mapping.GetPath()); |
113 |
| - } |
114 |
| - |
115 |
| - static char ID; |
116 |
| - |
117 |
| -private: |
118 |
| - std::shared_ptr<llvm::FileCollector> m_collector; |
119 |
| -}; |
120 |
| - |
121 |
| -/// Provider for the LLDB version number. |
122 |
| -/// |
123 |
| -/// When the reproducer is kept, it writes the lldb version to a file named |
124 |
| -/// version.txt in the reproducer root. |
125 |
| -class VersionProvider : public Provider<VersionProvider> { |
126 |
| -public: |
127 |
| - VersionProvider(const FileSpec &directory) : Provider(directory) {} |
128 |
| - struct Info { |
129 |
| - static const char *name; |
130 |
| - static const char *file; |
131 |
| - }; |
132 |
| - void SetVersion(std::string version) { |
133 |
| - assert(m_version.empty()); |
134 |
| - m_version = std::move(version); |
135 |
| - } |
136 |
| - void Keep() override; |
137 |
| - std::string m_version; |
138 |
| - static char ID; |
139 |
| -}; |
140 |
| - |
141 |
| -/// Abstract provider to storing directory paths. |
142 |
| -template <typename T> class DirectoryProvider : public repro::Provider<T> { |
143 |
| -public: |
144 |
| - DirectoryProvider(const FileSpec &root) : Provider<T>(root) {} |
145 |
| - void SetDirectory(std::string directory) { |
146 |
| - m_directory = std::move(directory); |
147 |
| - } |
148 |
| - llvm::StringRef GetDirectory() { return m_directory; } |
149 |
| - |
150 |
| - void Keep() override { |
151 |
| - FileSpec file = this->GetRoot().CopyByAppendingPathComponent(T::Info::file); |
152 |
| - std::error_code ec; |
153 |
| - llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text); |
154 |
| - if (ec) |
155 |
| - return; |
156 |
| - os << m_directory << "\n"; |
157 |
| - } |
158 |
| - |
159 |
| -protected: |
160 |
| - std::string m_directory; |
161 |
| -}; |
162 |
| - |
163 |
| -/// Provider for the current working directory. |
164 |
| -/// |
165 |
| -/// When the reproducer is kept, it writes lldb's current working directory to |
166 |
| -/// a file named cwd.txt in the reproducer root. |
167 |
| -class WorkingDirectoryProvider |
168 |
| - : public DirectoryProvider<WorkingDirectoryProvider> { |
169 |
| -public: |
170 |
| - WorkingDirectoryProvider(const FileSpec &directory) |
171 |
| - : DirectoryProvider(directory) { |
172 |
| - llvm::SmallString<128> cwd; |
173 |
| - if (std::error_code EC = llvm::sys::fs::current_path(cwd)) |
174 |
| - return; |
175 |
| - SetDirectory(std::string(cwd)); |
176 |
| - } |
177 |
| - struct Info { |
178 |
| - static const char *name; |
179 |
| - static const char *file; |
180 |
| - }; |
181 |
| - static char ID; |
182 |
| -}; |
183 |
| - |
184 |
| -/// Provider for the home directory. |
185 |
| -/// |
186 |
| -/// When the reproducer is kept, it writes the user's home directory to a file |
187 |
| -/// a file named home.txt in the reproducer root. |
188 |
| -class HomeDirectoryProvider : public DirectoryProvider<HomeDirectoryProvider> { |
189 |
| -public: |
190 |
| - HomeDirectoryProvider(const FileSpec &directory) |
191 |
| - : DirectoryProvider(directory) { |
192 |
| - llvm::SmallString<128> home_dir; |
193 |
| - llvm::sys::path::home_directory(home_dir); |
194 |
| - SetDirectory(std::string(home_dir)); |
195 |
| - } |
196 |
| - struct Info { |
197 |
| - static const char *name; |
198 |
| - static const char *file; |
199 |
| - }; |
200 |
| - static char ID; |
201 |
| -}; |
202 |
| - |
203 |
| -/// The recorder is a small object handed out by a provider to record data. It |
204 |
| -/// is commonly used in combination with a MultiProvider which is meant to |
205 |
| -/// record information for multiple instances of the same source of data. |
206 |
| -class AbstractRecorder { |
207 |
| -protected: |
208 |
| - AbstractRecorder(const FileSpec &filename, std::error_code &ec) |
209 |
| - : m_filename(filename.GetFilename().GetStringRef()), |
210 |
| - m_os(filename.GetPath(), ec, llvm::sys::fs::OF_Text), m_record(true) {} |
211 |
| - |
212 |
| -public: |
213 |
| - const FileSpec &GetFilename() { return m_filename; } |
214 |
| - |
215 |
| - void Stop() { |
216 |
| - assert(m_record); |
217 |
| - m_record = false; |
218 |
| - } |
219 |
| - |
220 |
| -private: |
221 |
| - FileSpec m_filename; |
222 |
| - |
223 |
| -protected: |
224 |
| - llvm::raw_fd_ostream m_os; |
225 |
| - bool m_record; |
226 |
| -}; |
227 |
| - |
228 |
| -/// Recorder that records its data as text to a file. |
229 |
| -class DataRecorder : public AbstractRecorder { |
230 |
| -public: |
231 |
| - DataRecorder(const FileSpec &filename, std::error_code &ec) |
232 |
| - : AbstractRecorder(filename, ec) {} |
233 |
| - |
234 |
| - static llvm::Expected<std::unique_ptr<DataRecorder>> |
235 |
| - Create(const FileSpec &filename); |
236 |
| - |
237 |
| - template <typename T> void Record(const T &t, bool newline = false) { |
238 |
| - if (!m_record) |
239 |
| - return; |
240 |
| - m_os << t; |
241 |
| - if (newline) |
242 |
| - m_os << '\n'; |
243 |
| - m_os.flush(); |
244 |
| - } |
245 |
| -}; |
246 |
| - |
247 |
| -/// Recorder that records its data as YAML to a file. |
248 |
| -class YamlRecorder : public AbstractRecorder { |
249 |
| -public: |
250 |
| - YamlRecorder(const FileSpec &filename, std::error_code &ec) |
251 |
| - : AbstractRecorder(filename, ec) {} |
252 |
| - |
253 |
| - static llvm::Expected<std::unique_ptr<YamlRecorder>> |
254 |
| - Create(const FileSpec &filename); |
255 |
| - |
256 |
| - template <typename T> void Record(const T &t) { |
257 |
| - if (!m_record) |
258 |
| - return; |
259 |
| - llvm::yaml::Output yout(m_os); |
260 |
| - // The YAML traits are defined as non-const because they are used for |
261 |
| - // serialization and deserialization. The cast is safe because |
262 |
| - // serialization doesn't modify the object. |
263 |
| - yout << const_cast<T &>(t); |
264 |
| - m_os.flush(); |
265 |
| - } |
266 |
| -}; |
267 |
| - |
268 |
| -/// The MultiProvider is a provider that hands out recorder which can be used |
269 |
| -/// to capture data for different instances of the same object. The recorders |
270 |
| -/// can be passed around or stored as an instance member. |
271 |
| -/// |
272 |
| -/// The Info::file for the MultiProvider contains an index of files for every |
273 |
| -/// recorder. Use the MultiLoader to read the index and get the individual |
274 |
| -/// files. |
275 |
| -template <typename T, typename V> |
276 |
| -class MultiProvider : public repro::Provider<V> { |
277 |
| -public: |
278 |
| - MultiProvider(const FileSpec &directory) : Provider<V>(directory) {} |
279 |
| - |
280 |
| - T *GetNewRecorder() { |
281 |
| - std::size_t i = m_recorders.size() + 1; |
282 |
| - std::string filename = (llvm::Twine(V::Info::name) + llvm::Twine("-") + |
283 |
| - llvm::Twine(i) + llvm::Twine(".yaml")) |
284 |
| - .str(); |
285 |
| - auto recorder_or_error = |
286 |
| - T::Create(this->GetRoot().CopyByAppendingPathComponent(filename)); |
287 |
| - if (!recorder_or_error) { |
288 |
| - llvm::consumeError(recorder_or_error.takeError()); |
289 |
| - return nullptr; |
290 |
| - } |
291 |
| - |
292 |
| - m_recorders.push_back(std::move(*recorder_or_error)); |
293 |
| - return m_recorders.back().get(); |
294 |
| - } |
295 |
| - |
296 |
| - void Keep() override { |
297 |
| - std::vector<std::string> files; |
298 |
| - for (auto &recorder : m_recorders) { |
299 |
| - recorder->Stop(); |
300 |
| - files.push_back(recorder->GetFilename().GetPath()); |
301 |
| - } |
302 |
| - |
303 |
| - FileSpec file = this->GetRoot().CopyByAppendingPathComponent(V::Info::file); |
304 |
| - std::error_code ec; |
305 |
| - llvm::raw_fd_ostream os(file.GetPath(), ec, llvm::sys::fs::OF_Text); |
306 |
| - if (ec) |
307 |
| - return; |
308 |
| - llvm::yaml::Output yout(os); |
309 |
| - yout << files; |
310 |
| - } |
311 |
| - |
312 |
| - void Discard() override { m_recorders.clear(); } |
313 |
| - |
314 |
| -private: |
315 |
| - std::vector<std::unique_ptr<T>> m_recorders; |
316 |
| -}; |
317 |
| - |
318 |
| -class CommandProvider : public MultiProvider<DataRecorder, CommandProvider> { |
319 |
| -public: |
320 |
| - struct Info { |
321 |
| - static const char *name; |
322 |
| - static const char *file; |
323 |
| - }; |
324 |
| - |
325 |
| - CommandProvider(const FileSpec &directory) |
326 |
| - : MultiProvider<DataRecorder, CommandProvider>(directory) {} |
327 |
| - |
328 |
| - static char ID; |
329 |
| -}; |
330 |
| - |
331 | 87 | /// The generator is responsible for the logic needed to generate a
|
332 | 88 | /// reproducer. For doing so it relies on providers, who serialize data that
|
333 | 89 | /// is necessary for reproducing a failure.
|
@@ -469,60 +225,6 @@ class Reproducer {
|
469 | 225 | mutable std::mutex m_mutex;
|
470 | 226 | };
|
471 | 227 |
|
472 |
| -/// Loader for data captured with the MultiProvider. It will read the index and |
473 |
| -/// return the path to the files in the index. |
474 |
| -template <typename T> class MultiLoader { |
475 |
| -public: |
476 |
| - MultiLoader(std::vector<std::string> files) : m_files(std::move(files)) {} |
477 |
| - |
478 |
| - static std::unique_ptr<MultiLoader> Create(Loader *loader) { |
479 |
| - if (!loader) |
480 |
| - return {}; |
481 |
| - |
482 |
| - FileSpec file = loader->GetFile<typename T::Info>(); |
483 |
| - if (!file) |
484 |
| - return {}; |
485 |
| - |
486 |
| - auto error_or_file = llvm::MemoryBuffer::getFile(file.GetPath()); |
487 |
| - if (auto err = error_or_file.getError()) |
488 |
| - return {}; |
489 |
| - |
490 |
| - std::vector<std::string> files; |
491 |
| - llvm::yaml::Input yin((*error_or_file)->getBuffer()); |
492 |
| - yin >> files; |
493 |
| - |
494 |
| - if (auto err = yin.error()) |
495 |
| - return {}; |
496 |
| - |
497 |
| - for (auto &file : files) { |
498 |
| - FileSpec absolute_path = |
499 |
| - loader->GetRoot().CopyByAppendingPathComponent(file); |
500 |
| - file = absolute_path.GetPath(); |
501 |
| - } |
502 |
| - |
503 |
| - return std::make_unique<MultiLoader<T>>(std::move(files)); |
504 |
| - } |
505 |
| - |
506 |
| - llvm::Optional<std::string> GetNextFile() { |
507 |
| - if (m_index >= m_files.size()) |
508 |
| - return {}; |
509 |
| - return m_files[m_index++]; |
510 |
| - } |
511 |
| - |
512 |
| -private: |
513 |
| - std::vector<std::string> m_files; |
514 |
| - unsigned m_index = 0; |
515 |
| -}; |
516 |
| - |
517 |
| -/// Helper to read directories written by the DirectoryProvider. |
518 |
| -template <typename T> |
519 |
| -llvm::Expected<std::string> GetDirectoryFrom(repro::Loader *loader) { |
520 |
| - llvm::Expected<std::string> dir = loader->LoadBuffer<T>(); |
521 |
| - if (!dir) |
522 |
| - return dir.takeError(); |
523 |
| - return std::string(llvm::StringRef(*dir).rtrim()); |
524 |
| -} |
525 |
| - |
526 | 228 | } // namespace repro
|
527 | 229 | } // namespace lldb_private
|
528 | 230 |
|
|
0 commit comments