2020-05-21 17:57:53 +00:00
|
|
|
//===- Reproducer.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-05-21 18:19:57 +00:00
|
|
|
#include "Reproducer.h"
|
2020-05-21 18:41:20 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2023-10-10 22:42:39 +00:00
|
|
|
#include "llvm/Support/Process.h"
|
2020-05-21 17:57:53 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::dsymutil;
|
|
|
|
|
|
|
|
static std::string createReproducerDir(std::error_code &EC) {
|
|
|
|
SmallString<128> Root;
|
2023-10-10 22:42:39 +00:00
|
|
|
if (const char *Path = getenv("DSYMUTIL_REPRODUCER_PATH")) {
|
2020-05-21 17:57:53 +00:00
|
|
|
Root.assign(Path);
|
2023-10-10 22:42:39 +00:00
|
|
|
EC = sys::fs::create_directories(Root);
|
|
|
|
} else if (const char *Path = getenv("LLVM_DIAGNOSTIC_DIR")) {
|
|
|
|
Root.assign(Path);
|
|
|
|
llvm::sys::path::append(
|
|
|
|
Root, "dsymutil-" + llvm::Twine(llvm::sys::Process::getProcessId()));
|
|
|
|
EC = sys::fs::create_directories(Root);
|
2020-05-21 17:57:53 +00:00
|
|
|
} else {
|
|
|
|
EC = sys::fs::createUniqueDirectory("dsymutil", Root);
|
|
|
|
}
|
2022-11-11 19:50:45 +00:00
|
|
|
sys::fs::make_absolute(Root);
|
2020-05-21 17:57:53 +00:00
|
|
|
return EC ? "" : std::string(Root);
|
|
|
|
}
|
|
|
|
|
|
|
|
Reproducer::Reproducer() : VFS(vfs::getRealFileSystem()) {}
|
|
|
|
Reproducer::~Reproducer() = default;
|
|
|
|
|
2022-06-14 16:22:46 +00:00
|
|
|
ReproducerGenerate::ReproducerGenerate(std::error_code &EC, int Argc,
|
|
|
|
char **Argv, bool GenerateOnExit)
|
|
|
|
: Root(createReproducerDir(EC)), GenerateOnExit(GenerateOnExit) {
|
|
|
|
for (int I = 0; I < Argc; ++I)
|
|
|
|
Args.push_back(Argv[I]);
|
2020-05-21 17:57:53 +00:00
|
|
|
if (!Root.empty())
|
|
|
|
FC = std::make_shared<FileCollector>(Root, Root);
|
|
|
|
VFS = FileCollector::createCollectorVFS(vfs::getRealFileSystem(), FC);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReproducerGenerate::~ReproducerGenerate() {
|
2022-06-14 16:22:46 +00:00
|
|
|
if (GenerateOnExit && !Generated)
|
|
|
|
generate();
|
2023-04-04 04:43:36 +00:00
|
|
|
else if (!Generated && !Root.empty())
|
|
|
|
sys::fs::remove_directories(Root, /* IgnoreErrors */ true);
|
2022-06-14 16:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReproducerGenerate::generate() {
|
2020-05-21 17:57:53 +00:00
|
|
|
if (!FC)
|
|
|
|
return;
|
2022-06-14 16:22:46 +00:00
|
|
|
Generated = true;
|
2020-05-21 17:57:53 +00:00
|
|
|
FC->copyFiles(false);
|
|
|
|
SmallString<128> Mapping(Root);
|
|
|
|
sys::path::append(Mapping, "mapping.yaml");
|
|
|
|
FC->writeMapping(Mapping.str());
|
2022-06-14 16:22:46 +00:00
|
|
|
errs() << "********************\n";
|
|
|
|
errs() << "Reproducer written to " << Root << '\n';
|
|
|
|
errs() << "Please include the reproducer and the following invocation in "
|
|
|
|
"your bug report:\n";
|
|
|
|
for (llvm::StringRef Arg : Args)
|
|
|
|
errs() << Arg << ' ';
|
|
|
|
errs() << "--use-reproducer " << Root << '\n';
|
|
|
|
errs() << "********************\n";
|
2020-05-21 17:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReproducerUse::~ReproducerUse() = default;
|
|
|
|
|
|
|
|
ReproducerUse::ReproducerUse(StringRef Root, std::error_code &EC) {
|
|
|
|
SmallString<128> Mapping(Root);
|
|
|
|
sys::path::append(Mapping, "mapping.yaml");
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
|
|
|
|
vfs::getRealFileSystem()->getBufferForFile(Mapping.str());
|
|
|
|
|
|
|
|
if (!Buffer) {
|
|
|
|
EC = Buffer.getError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VFS = llvm::vfs::getVFSFromYAML(std::move(Buffer.get()), nullptr, Mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<std::unique_ptr<Reproducer>>
|
2022-06-14 16:22:46 +00:00
|
|
|
Reproducer::createReproducer(ReproducerMode Mode, StringRef Root, int Argc,
|
|
|
|
char **Argv) {
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
std::unique_ptr<Reproducer> Repro;
|
2020-05-21 17:57:53 +00:00
|
|
|
switch (Mode) {
|
2022-06-14 16:22:46 +00:00
|
|
|
case ReproducerMode::GenerateOnExit:
|
|
|
|
Repro = std::make_unique<ReproducerGenerate>(EC, Argc, Argv, true);
|
|
|
|
break;
|
|
|
|
case ReproducerMode::GenerateOnCrash:
|
|
|
|
Repro = std::make_unique<ReproducerGenerate>(EC, Argc, Argv, false);
|
|
|
|
break;
|
|
|
|
case ReproducerMode::Use:
|
|
|
|
Repro = std::make_unique<ReproducerUse>(Root, EC);
|
|
|
|
break;
|
2020-05-21 17:57:53 +00:00
|
|
|
case ReproducerMode::Off:
|
2022-06-14 16:22:46 +00:00
|
|
|
Repro = std::make_unique<Reproducer>();
|
|
|
|
break;
|
2020-05-21 17:57:53 +00:00
|
|
|
}
|
2022-06-14 16:22:46 +00:00
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
2022-06-14 23:09:39 +00:00
|
|
|
return {std::move(Repro)};
|
2020-05-21 17:57:53 +00:00
|
|
|
}
|