2021-12-14 17:29:17 +00:00
|
|
|
//===-- llvm-debuginfod-find.cpp - Simple CLI for libdebuginfod-client ----===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// This file contains the llvm-debuginfod-find tool. This tool
|
|
|
|
/// queries the debuginfod servers in the DEBUGINFOD_URLS environment
|
|
|
|
/// variable (delimited by space (" ")) for the executable,
|
|
|
|
/// debuginfo, or specified source file of the binary matching the
|
|
|
|
/// given build-id.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2023-06-17 13:18:23 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2024-09-10 23:04:05 -07:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2022-08-23 13:39:33 -07:00
|
|
|
#include "llvm/Debuginfod/BuildIDFetcher.h"
|
2021-12-14 17:29:17 +00:00
|
|
|
#include "llvm/Debuginfod/Debuginfod.h"
|
|
|
|
#include "llvm/Debuginfod/HTTPClient.h"
|
2024-09-10 23:04:05 -07:00
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2021-12-14 17:29:17 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2024-09-10 23:04:05 -07:00
|
|
|
#include "llvm/Support/LLVMDriver.h"
|
2021-12-14 17:29:17 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2024-09-10 23:04:05 -07:00
|
|
|
// Command-line option boilerplate.
|
|
|
|
namespace {
|
|
|
|
enum ID {
|
|
|
|
OPT_INVALID = 0, // This is not an option ID.
|
|
|
|
#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2024-12-11 15:44:44 -08:00
|
|
|
#define OPTTABLE_STR_TABLE_CODE
|
2024-09-10 23:04:05 -07:00
|
|
|
#include "Opts.inc"
|
2024-12-11 15:44:44 -08:00
|
|
|
#undef OPTTABLE_STR_TABLE_CODE
|
|
|
|
|
|
|
|
#define OPTTABLE_PREFIXES_TABLE_CODE
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTTABLE_PREFIXES_TABLE_CODE
|
2024-09-10 23:04:05 -07:00
|
|
|
|
|
|
|
using namespace llvm::opt;
|
|
|
|
static constexpr opt::OptTable::Info InfoTable[] = {
|
|
|
|
#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class DebuginfodFindOptTable : public opt::GenericOptTable {
|
|
|
|
public:
|
2024-12-11 15:44:44 -08:00
|
|
|
DebuginfodFindOptTable()
|
|
|
|
: GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {}
|
2024-09-10 23:04:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static std::string InputBuildID;
|
|
|
|
static bool FetchExecutable;
|
|
|
|
static bool FetchDebuginfo;
|
|
|
|
static std::string FetchSource;
|
|
|
|
static bool DumpToStdout;
|
|
|
|
static std::vector<std::string> DebugFileDirectory;
|
|
|
|
|
|
|
|
static void parseArgs(int argc, char **argv) {
|
|
|
|
DebuginfodFindOptTable Tbl;
|
|
|
|
llvm::BumpPtrAllocator A;
|
|
|
|
llvm::StringSaver Saver{A};
|
|
|
|
opt::InputArgList Args =
|
|
|
|
Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
|
|
|
|
llvm::errs() << Msg << '\n';
|
|
|
|
std::exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Args.hasArg(OPT_help)) {
|
2024-10-01 18:57:39 -07:00
|
|
|
Tbl.printHelp(
|
|
|
|
llvm::outs(), "llvm-debuginfod-find [options] <input build_id>",
|
|
|
|
"llvm-debuginfod-find: Fetch debuginfod artifacts\n\n"
|
|
|
|
"This program is a frontend to the debuginfod client library. The "
|
|
|
|
"cache directory, request timeout (in seconds), and debuginfod server "
|
|
|
|
"urls are set by these environment variables:\n"
|
|
|
|
"DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n"
|
|
|
|
"DEBUGINFOD_TIMEOUT (defaults to 90s)\n"
|
|
|
|
"DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)");
|
2024-09-10 23:04:05 -07:00
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputBuildID = Args.getLastArgValue(OPT_INPUT);
|
|
|
|
|
|
|
|
FetchExecutable = Args.hasArg(OPT_fetch_executable);
|
|
|
|
FetchDebuginfo = Args.hasArg(OPT_fetch_debuginfo);
|
|
|
|
DumpToStdout = Args.hasArg(OPT_dump_to_stdout);
|
|
|
|
FetchSource = Args.getLastArgValue(OPT_fetch_source, "");
|
|
|
|
DebugFileDirectory = Args.getAllArgValues(OPT_debug_file_directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[noreturn]] static void helpExit() {
|
|
|
|
errs() << "Must specify exactly one of --executable, "
|
|
|
|
"--source=/path/to/file, or --debuginfo.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitOnError ExitOnDebuginfodFindError;
|
2021-12-14 17:29:17 +00:00
|
|
|
|
2022-08-23 13:39:33 -07:00
|
|
|
static std::string fetchDebugInfo(object::BuildIDRef BuildID);
|
2022-05-17 14:29:27 +00:00
|
|
|
|
2024-09-10 23:04:05 -07:00
|
|
|
int llvm_debuginfod_find_main(int argc, char **argv,
|
|
|
|
const llvm::ToolContext &) {
|
|
|
|
// InitLLVM X(argc, argv);
|
2021-12-14 17:29:17 +00:00
|
|
|
HTTPClient::initialize();
|
2024-09-10 23:04:05 -07:00
|
|
|
parseArgs(argc, argv);
|
2021-12-14 17:29:17 +00:00
|
|
|
|
|
|
|
if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1)
|
|
|
|
helpExit();
|
|
|
|
|
|
|
|
std::string IDString;
|
|
|
|
if (!tryGetFromHex(InputBuildID, IDString)) {
|
|
|
|
errs() << "Build ID " << InputBuildID << " is not a hex string.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-08-23 13:39:33 -07:00
|
|
|
object::BuildID ID(IDString.begin(), IDString.end());
|
2021-12-14 17:29:17 +00:00
|
|
|
|
|
|
|
std::string Path;
|
|
|
|
if (FetchSource != "")
|
2024-09-10 23:04:05 -07:00
|
|
|
Path =
|
|
|
|
ExitOnDebuginfodFindError(getCachedOrDownloadSource(ID, FetchSource));
|
2021-12-14 17:29:17 +00:00
|
|
|
else if (FetchExecutable)
|
2024-09-10 23:04:05 -07:00
|
|
|
Path = ExitOnDebuginfodFindError(getCachedOrDownloadExecutable(ID));
|
2021-12-14 17:29:17 +00:00
|
|
|
else if (FetchDebuginfo)
|
2022-05-17 14:29:27 +00:00
|
|
|
Path = fetchDebugInfo(ID);
|
2021-12-14 17:29:17 +00:00
|
|
|
else
|
|
|
|
llvm_unreachable("We have already checked that exactly one of the above "
|
|
|
|
"conditions is true.");
|
|
|
|
|
|
|
|
if (DumpToStdout) {
|
|
|
|
// Print the contents of the artifact.
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
|
|
|
|
Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
|
2024-09-10 23:04:05 -07:00
|
|
|
ExitOnDebuginfodFindError(errorCodeToError(Buf.getError()));
|
2021-12-14 17:29:17 +00:00
|
|
|
outs() << Buf.get()->getBuffer();
|
|
|
|
} else
|
|
|
|
// Print the path to the cached artifact file.
|
|
|
|
outs() << Path << "\n";
|
2024-09-10 23:04:05 -07:00
|
|
|
|
|
|
|
return 0;
|
2021-12-14 17:29:17 +00:00
|
|
|
}
|
2022-05-17 14:29:27 +00:00
|
|
|
|
2022-08-23 13:39:33 -07:00
|
|
|
// Find a debug file in local build ID directories and via debuginfod.
|
|
|
|
std::string fetchDebugInfo(object::BuildIDRef BuildID) {
|
2022-12-04 09:11:11 +00:00
|
|
|
if (std::optional<std::string> Path =
|
2022-08-23 13:39:33 -07:00
|
|
|
DebuginfodFetcher(DebugFileDirectory).fetch(BuildID))
|
|
|
|
return *Path;
|
|
|
|
errs() << "Build ID " << llvm::toHex(BuildID, /*Lowercase=*/true)
|
2023-10-09 14:20:04 -07:00
|
|
|
<< " could not be found.\n";
|
2022-08-23 13:39:33 -07:00
|
|
|
exit(1);
|
2022-05-17 14:29:27 +00:00
|
|
|
}
|