Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / tools / llvm-debuginfod-find / llvm-debuginfod-find.cpp
blob425ee8d986a829f91ae663b775c232a37f0b5cb3
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 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Debuginfod/BuildIDFetcher.h"
20 #include "llvm/Debuginfod/Debuginfod.h"
21 #include "llvm/Debuginfod/HTTPClient.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/InitLLVM.h"
25 using namespace llvm;
27 cl::OptionCategory DebuginfodFindCategory("llvm-debuginfod-find Options");
29 cl::opt<std::string> InputBuildID(cl::Positional, cl::Required,
30 cl::desc("<input build_id>"), cl::init("-"),
31 cl::cat(DebuginfodFindCategory));
33 static cl::opt<bool>
34 FetchExecutable("executable", cl::init(false),
35 cl::desc("If set, fetch a binary file associated with this "
36 "build id, containing the executable sections."),
37 cl::cat(DebuginfodFindCategory));
39 static cl::opt<bool>
40 FetchDebuginfo("debuginfo", cl::init(false),
41 cl::desc("If set, fetch a binary file associated with this "
42 "build id, containing the debuginfo sections."),
43 cl::cat(DebuginfodFindCategory));
45 static cl::opt<std::string> FetchSource(
46 "source", cl::init(""),
47 cl::desc("Fetch a source file associated with this build id, which is at "
48 "this relative path relative to the compilation directory."),
49 cl::cat(DebuginfodFindCategory));
51 static cl::opt<bool>
52 DumpToStdout("dump", cl::init(false),
53 cl::desc("If set, dumps the contents of the fetched artifact "
54 "to standard output. Otherwise, dumps the absolute "
55 "path to the cached artifact on disk."),
56 cl::cat(DebuginfodFindCategory));
58 static cl::list<std::string> DebugFileDirectory(
59 "debug-file-directory",
60 cl::desc("Path to directory where to look for debug files."),
61 cl::cat(DebuginfodFindCategory));
63 [[noreturn]] static void helpExit() {
64 errs() << "Must specify exactly one of --executable, "
65 "--source=/path/to/file, or --debuginfo.";
66 exit(1);
69 ExitOnError ExitOnErr;
71 static std::string fetchDebugInfo(object::BuildIDRef BuildID);
73 int main(int argc, char **argv) {
74 InitLLVM X(argc, argv);
75 HTTPClient::initialize();
77 cl::HideUnrelatedOptions({&DebuginfodFindCategory});
78 cl::ParseCommandLineOptions(
79 argc, argv,
80 "llvm-debuginfod-find: Fetch debuginfod artifacts\n\n"
81 "This program is a frontend to the debuginfod client library. The cache "
82 "directory, request timeout (in seconds), and debuginfod server urls are "
83 "set by these environment variables:\n"
84 "DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n"
85 "DEBUGINFOD_TIMEOUT (defaults to 90s)\n"
86 "DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)\n");
88 if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1)
89 helpExit();
91 std::string IDString;
92 if (!tryGetFromHex(InputBuildID, IDString)) {
93 errs() << "Build ID " << InputBuildID << " is not a hex string.\n";
94 exit(1);
96 object::BuildID ID(IDString.begin(), IDString.end());
98 std::string Path;
99 if (FetchSource != "")
100 Path = ExitOnErr(getCachedOrDownloadSource(ID, FetchSource));
101 else if (FetchExecutable)
102 Path = ExitOnErr(getCachedOrDownloadExecutable(ID));
103 else if (FetchDebuginfo)
104 Path = fetchDebugInfo(ID);
105 else
106 llvm_unreachable("We have already checked that exactly one of the above "
107 "conditions is true.");
109 if (DumpToStdout) {
110 // Print the contents of the artifact.
111 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile(
112 Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
113 ExitOnErr(errorCodeToError(Buf.getError()));
114 outs() << Buf.get()->getBuffer();
115 } else
116 // Print the path to the cached artifact file.
117 outs() << Path << "\n";
120 // Find a debug file in local build ID directories and via debuginfod.
121 std::string fetchDebugInfo(object::BuildIDRef BuildID) {
122 if (std::optional<std::string> Path =
123 DebuginfodFetcher(DebugFileDirectory).fetch(BuildID))
124 return *Path;
125 errs() << "Build ID " << llvm::toHex(BuildID, /*Lowercase=*/true)
126 << " could not be found.\n";
127 exit(1);