1*d415bd75Srobert //===-- llvm-debuginfod-find.cpp - Simple CLI for libdebuginfod-client ----===//
2*d415bd75Srobert //
3*d415bd75Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*d415bd75Srobert // See https://llvm.org/LICENSE.txt for license information.
5*d415bd75Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*d415bd75Srobert //
7*d415bd75Srobert //===----------------------------------------------------------------------===//
8*d415bd75Srobert ///
9*d415bd75Srobert /// \file
10*d415bd75Srobert /// This file contains the llvm-debuginfod-find tool. This tool
11*d415bd75Srobert /// queries the debuginfod servers in the DEBUGINFOD_URLS environment
12*d415bd75Srobert /// variable (delimited by space (" ")) for the executable,
13*d415bd75Srobert /// debuginfo, or specified source file of the binary matching the
14*d415bd75Srobert /// given build-id.
15*d415bd75Srobert ///
16*d415bd75Srobert //===----------------------------------------------------------------------===//
17*d415bd75Srobert
18*d415bd75Srobert #include "llvm/Debuginfod/BuildIDFetcher.h"
19*d415bd75Srobert #include "llvm/Debuginfod/Debuginfod.h"
20*d415bd75Srobert #include "llvm/Debuginfod/HTTPClient.h"
21*d415bd75Srobert #include "llvm/Support/CommandLine.h"
22*d415bd75Srobert #include "llvm/Support/InitLLVM.h"
23*d415bd75Srobert
24*d415bd75Srobert using namespace llvm;
25*d415bd75Srobert
26*d415bd75Srobert cl::OptionCategory DebuginfodFindCategory("llvm-debuginfod-find Options");
27*d415bd75Srobert
28*d415bd75Srobert cl::opt<std::string> InputBuildID(cl::Positional, cl::Required,
29*d415bd75Srobert cl::desc("<input build_id>"), cl::init("-"),
30*d415bd75Srobert cl::cat(DebuginfodFindCategory));
31*d415bd75Srobert
32*d415bd75Srobert static cl::opt<bool>
33*d415bd75Srobert FetchExecutable("executable", cl::init(false),
34*d415bd75Srobert cl::desc("If set, fetch a binary file associated with this "
35*d415bd75Srobert "build id, containing the executable sections."),
36*d415bd75Srobert cl::cat(DebuginfodFindCategory));
37*d415bd75Srobert
38*d415bd75Srobert static cl::opt<bool>
39*d415bd75Srobert FetchDebuginfo("debuginfo", cl::init(false),
40*d415bd75Srobert cl::desc("If set, fetch a binary file associated with this "
41*d415bd75Srobert "build id, containing the debuginfo sections."),
42*d415bd75Srobert cl::cat(DebuginfodFindCategory));
43*d415bd75Srobert
44*d415bd75Srobert static cl::opt<std::string> FetchSource(
45*d415bd75Srobert "source", cl::init(""),
46*d415bd75Srobert cl::desc("Fetch a source file associated with this build id, which is at "
47*d415bd75Srobert "this relative path relative to the compilation directory."),
48*d415bd75Srobert cl::cat(DebuginfodFindCategory));
49*d415bd75Srobert
50*d415bd75Srobert static cl::opt<bool>
51*d415bd75Srobert DumpToStdout("dump", cl::init(false),
52*d415bd75Srobert cl::desc("If set, dumps the contents of the fetched artifact "
53*d415bd75Srobert "to standard output. Otherwise, dumps the absolute "
54*d415bd75Srobert "path to the cached artifact on disk."),
55*d415bd75Srobert cl::cat(DebuginfodFindCategory));
56*d415bd75Srobert
57*d415bd75Srobert static cl::list<std::string> DebugFileDirectory(
58*d415bd75Srobert "debug-file-directory",
59*d415bd75Srobert cl::desc("Path to directory where to look for debug files."),
60*d415bd75Srobert cl::cat(DebuginfodFindCategory));
61*d415bd75Srobert
helpExit()62*d415bd75Srobert [[noreturn]] static void helpExit() {
63*d415bd75Srobert errs() << "Must specify exactly one of --executable, "
64*d415bd75Srobert "--source=/path/to/file, or --debuginfo.";
65*d415bd75Srobert exit(1);
66*d415bd75Srobert }
67*d415bd75Srobert
68*d415bd75Srobert ExitOnError ExitOnErr;
69*d415bd75Srobert
70*d415bd75Srobert static std::string fetchDebugInfo(object::BuildIDRef BuildID);
71*d415bd75Srobert
main(int argc,char ** argv)72*d415bd75Srobert int main(int argc, char **argv) {
73*d415bd75Srobert InitLLVM X(argc, argv);
74*d415bd75Srobert HTTPClient::initialize();
75*d415bd75Srobert
76*d415bd75Srobert cl::HideUnrelatedOptions({&DebuginfodFindCategory});
77*d415bd75Srobert cl::ParseCommandLineOptions(
78*d415bd75Srobert argc, argv,
79*d415bd75Srobert "llvm-debuginfod-find: Fetch debuginfod artifacts\n\n"
80*d415bd75Srobert "This program is a frontend to the debuginfod client library. The cache "
81*d415bd75Srobert "directory, request timeout (in seconds), and debuginfod server urls are "
82*d415bd75Srobert "set by these environment variables:\n"
83*d415bd75Srobert "DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n"
84*d415bd75Srobert "DEBUGINFOD_TIMEOUT (defaults to 90s)\n"
85*d415bd75Srobert "DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)\n");
86*d415bd75Srobert
87*d415bd75Srobert if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1)
88*d415bd75Srobert helpExit();
89*d415bd75Srobert
90*d415bd75Srobert std::string IDString;
91*d415bd75Srobert if (!tryGetFromHex(InputBuildID, IDString)) {
92*d415bd75Srobert errs() << "Build ID " << InputBuildID << " is not a hex string.\n";
93*d415bd75Srobert exit(1);
94*d415bd75Srobert }
95*d415bd75Srobert object::BuildID ID(IDString.begin(), IDString.end());
96*d415bd75Srobert
97*d415bd75Srobert std::string Path;
98*d415bd75Srobert if (FetchSource != "")
99*d415bd75Srobert Path = ExitOnErr(getCachedOrDownloadSource(ID, FetchSource));
100*d415bd75Srobert else if (FetchExecutable)
101*d415bd75Srobert Path = ExitOnErr(getCachedOrDownloadExecutable(ID));
102*d415bd75Srobert else if (FetchDebuginfo)
103*d415bd75Srobert Path = fetchDebugInfo(ID);
104*d415bd75Srobert else
105*d415bd75Srobert llvm_unreachable("We have already checked that exactly one of the above "
106*d415bd75Srobert "conditions is true.");
107*d415bd75Srobert
108*d415bd75Srobert if (DumpToStdout) {
109*d415bd75Srobert // Print the contents of the artifact.
110*d415bd75Srobert ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
111*d415bd75Srobert Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
112*d415bd75Srobert ExitOnErr(errorCodeToError(Buf.getError()));
113*d415bd75Srobert outs() << Buf.get()->getBuffer();
114*d415bd75Srobert } else
115*d415bd75Srobert // Print the path to the cached artifact file.
116*d415bd75Srobert outs() << Path << "\n";
117*d415bd75Srobert }
118*d415bd75Srobert
119*d415bd75Srobert // Find a debug file in local build ID directories and via debuginfod.
fetchDebugInfo(object::BuildIDRef BuildID)120*d415bd75Srobert std::string fetchDebugInfo(object::BuildIDRef BuildID) {
121*d415bd75Srobert if (std::optional<std::string> Path =
122*d415bd75Srobert DebuginfodFetcher(DebugFileDirectory).fetch(BuildID))
123*d415bd75Srobert return *Path;
124*d415bd75Srobert errs() << "Build ID " << llvm::toHex(BuildID, /*Lowercase=*/true)
125*d415bd75Srobert << " could not be found.";
126*d415bd75Srobert exit(1);
127*d415bd75Srobert }
128