1//===-- llvm-debuginfod.cpp - federating debuginfod server ----------------===//
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 tool, which serves the debuginfod
11/// protocol over HTTP. The tool periodically scans zero or more filesystem
12/// directories for ELF binaries to serve, and federates requests for unknown
13/// build IDs to the debuginfod servers set in the DEBUGINFOD_URLS environment
14/// variable.
15///
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Debuginfod/Debuginfod.h"
21#include "llvm/HTTP/HTTPClient.h"
22#include "llvm/Option/ArgList.h"
23#include "llvm/Option/Option.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Driver.h"
26#include "llvm/Support/ThreadPool.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 DebuginfodOptTable : public opt::OptTable {
44public:
45 DebuginfodOptTable() : OptTable(optionTables()) {}
46};
47} // end anonymous namespace
48
49// Options
50static unsigned Port;
51static std::string HostInterface;
52static int ScanInterval;
53static double MinInterval;
54static size_t MaxConcurrency;
55static bool VerboseLogging;
56static std::vector<std::string> ScanPaths;
57
58ExitOnError ExitOnErr;
59
60template <typename T>
61static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value,
62 T Default) {
63 if (const opt::Arg *A = Args.getLastArg(Ids: ID)) {
64 StringRef V(A->getValue());
65 if (!llvm::to_integer(V, Value, 0)) {
66 errs() << A->getSpelling() + ": expected an integer, but got '" + V + "'";
67 exit(status: 1);
68 }
69 } else {
70 Value = Default;
71 }
72}
73
74static void parseArgs(int argc, char **argv) {
75 DebuginfodOptTable Tbl;
76 llvm::StringRef ToolName = argv[0];
77 llvm::BumpPtrAllocator A;
78 llvm::StringSaver Saver{A};
79 opt::InputArgList Args =
80 Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
81 llvm::errs() << Msg << '\n';
82 std::exit(status: 1);
83 });
84
85 if (Args.hasArg(Ids: OPT_help)) {
86 Tbl.printHelp(OS&: llvm::outs(),
87 Usage: "llvm-debuginfod [options] <Directories to scan>",
88 Title: ToolName.str().c_str());
89 std::exit(status: 0);
90 }
91
92 VerboseLogging = Args.hasArg(Ids: OPT_verbose_logging);
93 ScanPaths = Args.getAllArgValues(Id: OPT_INPUT);
94
95 parseIntArg(Args, ID: OPT_port, Value&: Port, Default: 0u);
96 parseIntArg(Args, ID: OPT_scan_interval, Value&: ScanInterval, Default: 300);
97 parseIntArg(Args, ID: OPT_max_concurrency, Value&: MaxConcurrency, Default: size_t(0));
98
99 if (const opt::Arg *A = Args.getLastArg(Ids: OPT_min_interval)) {
100 StringRef V(A->getValue());
101 if (!llvm::to_float(T: V, Num&: MinInterval)) {
102 errs() << A->getSpelling() + ": expected a number, but got '" + V + "'";
103 exit(status: 1);
104 }
105 } else {
106 MinInterval = 10.0;
107 }
108
109 HostInterface = Args.getLastArgValue(Id: OPT_host_interface, Default: "0.0.0.0");
110}
111
112int llvm_debuginfod_main(int argc, char **argv, const llvm::ToolContext &) {
113 HTTPClient::initialize();
114 parseArgs(argc, argv);
115
116 SmallVector<StringRef, 1> Paths;
117 llvm::append_range(C&: Paths, R&: ScanPaths);
118
119 DefaultThreadPool Pool(hardware_concurrency(ThreadCount: MaxConcurrency));
120 DebuginfodLog Log;
121 DebuginfodCollection Collection(Paths, Log, Pool, MinInterval);
122 DebuginfodServer Server(Log, Collection);
123
124 if (!Port)
125 Port = ExitOnErr(Server.Server.bind(HostInterface: HostInterface.c_str()));
126 else
127 ExitOnErr(Server.Server.bind(Port, HostInterface: HostInterface.c_str()));
128
129 Log.push(Message: "Listening on port " + Twine(Port));
130
131 Pool.async(F: [&]() { ExitOnErr(Server.Server.listen()); });
132 Pool.async(F: [&]() {
133 while (true) {
134 DebuginfodLogEntry Entry = Log.pop();
135 if (VerboseLogging) {
136 outs() << Entry.Message << "\n";
137 outs().flush();
138 }
139 }
140 });
141 if (Paths.size())
142 ExitOnErr(Collection.updateForever(Interval: std::chrono::seconds(ScanInterval)));
143 Pool.wait();
144 llvm_unreachable("The ThreadPool should never finish running its tasks.");
145}
146