1//===-- llvm-debuginfod-find.cpp - Simple CLI for libdebuginfod-client ----===//
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/// \file
10/// This file contains the llvm-debuginfod-find tool. This tool
11/// queries the debuginfod servers in the DEBUGINFOD_URLS environment
12/// variable (delimited by space (" ")) for the executable,
13/// debuginfo, or specified source file of the binary matching the
14/// given build-id.
15///
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Debuginfod/BuildIDFetcher.h"
21#include "llvm/Debuginfod/Debuginfod.h"
22#include "llvm/HTTP/HTTPClient.h"
23#include "llvm/Option/ArgList.h"
24#include "llvm/Option/Option.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Driver.h"
27
28using namespace llvm;
29
30// Command-line option boilerplate.
31namespace {
32enum ID {
33 OPT_INVALID = 0, // This is not an option ID.
34#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
35#include "Opts.inc"
36#undef OPTION
37};
38
39using namespace llvm::opt;
40#define OPTTABLE_CODE
41#include "Opts.inc"
42
43class DebuginfodFindOptTable : public opt::OptTable {
44public:
45 DebuginfodFindOptTable() : OptTable(optionTables()) {}
46};
47
48} // end anonymous namespace
49
50static std::string InputBuildID;
51static bool FetchExecutable;
52static bool FetchDebuginfo;
53static std::string FetchSource;
54static bool DumpToStdout;
55static std::vector<std::string> DebugFileDirectory;
56
57static void parseArgs(int argc, char **argv) {
58 DebuginfodFindOptTable Tbl;
59 llvm::BumpPtrAllocator A;
60 llvm::StringSaver Saver{A};
61 opt::InputArgList Args =
62 Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
63 llvm::errs() << Msg << '\n';
64 std::exit(status: 1);
65 });
66
67 if (Args.hasArg(Ids: OPT_help)) {
68 Tbl.printHelp(
69 OS&: llvm::outs(), Usage: "llvm-debuginfod-find [options] <input build_id>",
70 Title: "llvm-debuginfod-find: Fetch debuginfod artifacts\n\n"
71 "This program is a frontend to the debuginfod client library. The "
72 "cache directory, request timeout (in seconds), and debuginfod server "
73 "urls are set by these environment variables:\n"
74 "DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n"
75 "DEBUGINFOD_TIMEOUT (defaults to 90s)\n"
76 "DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)");
77 std::exit(status: 0);
78 }
79
80 InputBuildID = Args.getLastArgValue(Id: OPT_INPUT);
81
82 FetchExecutable = Args.hasArg(Ids: OPT_fetch_executable);
83 FetchDebuginfo = Args.hasArg(Ids: OPT_fetch_debuginfo);
84 DumpToStdout = Args.hasArg(Ids: OPT_dump_to_stdout);
85 FetchSource = Args.getLastArgValue(Id: OPT_fetch_source, Default: "");
86 DebugFileDirectory = Args.getAllArgValues(Id: OPT_debug_file_directory);
87}
88
89[[noreturn]] static void helpExit() {
90 errs() << "Must specify exactly one of --executable, "
91 "--source=/path/to/file, or --debuginfo.\n";
92 exit(status: 1);
93}
94
95ExitOnError ExitOnDebuginfodFindError;
96
97static std::string fetchDebugInfo(object::BuildIDRef BuildID);
98
99int llvm_debuginfod_find_main(int argc, char **argv,
100 const llvm::ToolContext &) {
101 // InitLLVM X(argc, argv);
102 HTTPClient::initialize();
103 parseArgs(argc, argv);
104
105 if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1)
106 helpExit();
107
108 std::string IDString;
109 if (!tryGetFromHex(Input: InputBuildID, Output&: IDString)) {
110 errs() << "Build ID " << InputBuildID << " is not a hex string.\n";
111 exit(status: 1);
112 }
113 object::BuildID ID(IDString.begin(), IDString.end());
114
115 std::string Path;
116 if (FetchSource != "")
117 Path =
118 ExitOnDebuginfodFindError(getCachedOrDownloadSource(ID, SourceFilePath: FetchSource));
119 else if (FetchExecutable)
120 Path = ExitOnDebuginfodFindError(getCachedOrDownloadExecutable(ID));
121 else if (FetchDebuginfo)
122 Path = fetchDebugInfo(BuildID: ID);
123 else
124 llvm_unreachable("We have already checked that exactly one of the above "
125 "conditions is true.");
126
127 if (DumpToStdout) {
128 // Print the contents of the artifact.
129 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
130 Filename: Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
131 ExitOnDebuginfodFindError(errorCodeToError(EC: Buf.getError()));
132 outs() << Buf.get()->getBuffer();
133 } else
134 // Print the path to the cached artifact file.
135 outs() << Path << "\n";
136
137 return 0;
138}
139
140// Find a debug file in local build ID directories and via debuginfod.
141std::string fetchDebugInfo(object::BuildIDRef BuildID) {
142 Expected<std::string> Path =
143 DebuginfodFetcher(DebugFileDirectory).fetch(BuildID);
144 if (Path)
145 return *Path;
146 errs() << "Build ID " << llvm::toHex(Input: BuildID, /*Lowercase=*/LowerCase: true) << ": "
147 << toString(E: Path.takeError()) << "\n";
148 exit(status: 1);
149}
150