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/Debuginfod/Debuginfod.h"
19 #include "llvm/Debuginfod/HTTPClient.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/InitLLVM.h"
22 
23 using namespace llvm;
24 
25 cl::OptionCategory DebuginfodFindCategory("llvm-debuginfod-find Options");
26 
27 cl::opt<std::string> InputBuildID(cl::Positional, cl::Required,
28                                   cl::desc("<input build_id>"), cl::init("-"),
29                                   cl::cat(DebuginfodFindCategory));
30 
31 static cl::opt<bool>
32     FetchExecutable("executable", cl::init(false),
33                     cl::desc("If set, fetch a binary file associated with this "
34                              "build id, containing the executable sections."),
35                     cl::cat(DebuginfodFindCategory));
36 
37 static cl::opt<bool>
38     FetchDebuginfo("debuginfo", cl::init(false),
39                    cl::desc("If set, fetch a binary file associated with this "
40                             "build id, containing the debuginfo sections."),
41                    cl::cat(DebuginfodFindCategory));
42 
43 static cl::opt<std::string> FetchSource(
44     "source", cl::init(""),
45     cl::desc("Fetch a source file associated with this build id, which is at "
46              "this relative path relative to the compilation directory."),
47     cl::cat(DebuginfodFindCategory));
48 
49 static cl::opt<bool>
50     DumpToStdout("dump", cl::init(false),
51                  cl::desc("If set, dumps the contents of the fetched artifact "
52                           "to standard output. Otherwise, dumps the absolute "
53                           "path to the cached artifact on disk."),
54                  cl::cat(DebuginfodFindCategory));
55 
56 [[noreturn]] static void helpExit() {
57   errs() << "Must specify exactly one of --executable, "
58             "--source=/path/to/file, or --debuginfo.";
59   exit(1);
60 }
61 
62 ExitOnError ExitOnErr;
63 
64 int main(int argc, char **argv) {
65   InitLLVM X(argc, argv);
66   HTTPClient::initialize();
67 
68   cl::HideUnrelatedOptions({&DebuginfodFindCategory});
69   cl::ParseCommandLineOptions(
70       argc, argv,
71       "llvm-debuginfod-find: Fetch debuginfod artifacts\n\n"
72       "This program is a frontend to the debuginfod client library. The cache "
73       "directory, request timeout (in seconds), and debuginfod server urls are "
74       "set by these environment variables:\n"
75       "DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n"
76       "DEBUGINFOD_TIMEOUT (defaults to 90s)\n"
77       "DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)\n");
78 
79   if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1)
80     helpExit();
81 
82   std::string IDString;
83   if (!tryGetFromHex(InputBuildID, IDString)) {
84     errs() << "Build ID " << InputBuildID << " is not a hex string.\n";
85     exit(1);
86   }
87   BuildID ID(IDString.begin(), IDString.end());
88 
89   std::string Path;
90   if (FetchSource != "")
91     Path = ExitOnErr(getCachedOrDownloadSource(ID, FetchSource));
92   else if (FetchExecutable)
93     Path = ExitOnErr(getCachedOrDownloadExecutable(ID));
94   else if (FetchDebuginfo)
95     Path = ExitOnErr(getCachedOrDownloadDebuginfo(ID));
96   else
97     llvm_unreachable("We have already checked that exactly one of the above "
98                      "conditions is true.");
99 
100   if (DumpToStdout) {
101     // Print the contents of the artifact.
102     ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
103         Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
104     ExitOnErr(errorCodeToError(Buf.getError()));
105     outs() << Buf.get()->getBuffer();
106   } else
107     // Print the path to the cached artifact file.
108     outs() << Path << "\n";
109 }
110