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/Debuginfod/HTTPClient.h" |
22 | #include "llvm/Option/ArgList.h" |
23 | #include "llvm/Option/Option.h" |
24 | #include "llvm/Support/CommandLine.h" |
25 | #include "llvm/Support/LLVMDriver.h" |
26 | #include "llvm/Support/ThreadPool.h" |
27 | |
28 | using namespace llvm; |
29 | |
30 | // Command-line option boilerplate. |
31 | namespace { |
32 | enum 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 | |
39 | #define OPTTABLE_STR_TABLE_CODE |
40 | #include "Opts.inc" |
41 | #undef OPTTABLE_STR_TABLE_CODE |
42 | |
43 | #define OPTTABLE_PREFIXES_TABLE_CODE |
44 | #include "Opts.inc" |
45 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
46 | |
47 | using namespace llvm::opt; |
48 | static constexpr opt::OptTable::Info InfoTable[] = { |
49 | #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__), |
50 | #include "Opts.inc" |
51 | #undef OPTION |
52 | }; |
53 | |
54 | class DebuginfodOptTable : public opt::GenericOptTable { |
55 | public: |
56 | DebuginfodOptTable() |
57 | : GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {} |
58 | }; |
59 | } // end anonymous namespace |
60 | |
61 | // Options |
62 | static unsigned Port; |
63 | static std::string HostInterface; |
64 | static int ScanInterval; |
65 | static double MinInterval; |
66 | static size_t MaxConcurrency; |
67 | static bool VerboseLogging; |
68 | static std::vector<std::string> ScanPaths; |
69 | |
70 | ExitOnError ExitOnErr; |
71 | |
72 | template <typename T> |
73 | static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value, |
74 | T Default) { |
75 | if (const opt::Arg *A = Args.getLastArg(Ids: ID)) { |
76 | StringRef V(A->getValue()); |
77 | if (!llvm::to_integer(V, Value, 0)) { |
78 | errs() << A->getSpelling() + ": expected an integer, but got '" + V + "'" ; |
79 | exit(status: 1); |
80 | } |
81 | } else { |
82 | Value = Default; |
83 | } |
84 | } |
85 | |
86 | static void parseArgs(int argc, char **argv) { |
87 | DebuginfodOptTable Tbl; |
88 | llvm::StringRef ToolName = argv[0]; |
89 | llvm::BumpPtrAllocator A; |
90 | llvm::StringSaver Saver{A}; |
91 | opt::InputArgList Args = |
92 | Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) { |
93 | llvm::errs() << Msg << '\n'; |
94 | std::exit(status: 1); |
95 | }); |
96 | |
97 | if (Args.hasArg(Ids: OPT_help)) { |
98 | Tbl.printHelp(OS&: llvm::outs(), |
99 | Usage: "llvm-debuginfod [options] <Directories to scan>" , |
100 | Title: ToolName.str().c_str()); |
101 | std::exit(status: 0); |
102 | } |
103 | |
104 | VerboseLogging = Args.hasArg(Ids: OPT_verbose_logging); |
105 | ScanPaths = Args.getAllArgValues(Id: OPT_INPUT); |
106 | |
107 | parseIntArg(Args, ID: OPT_port, Value&: Port, Default: 0u); |
108 | parseIntArg(Args, ID: OPT_scan_interval, Value&: ScanInterval, Default: 300); |
109 | parseIntArg(Args, ID: OPT_max_concurrency, Value&: MaxConcurrency, Default: size_t(0)); |
110 | |
111 | if (const opt::Arg *A = Args.getLastArg(Ids: OPT_min_interval)) { |
112 | StringRef V(A->getValue()); |
113 | if (!llvm::to_float(T: V, Num&: MinInterval)) { |
114 | errs() << A->getSpelling() + ": expected a number, but got '" + V + "'" ; |
115 | exit(status: 1); |
116 | } |
117 | } else { |
118 | MinInterval = 10.0; |
119 | } |
120 | |
121 | HostInterface = Args.getLastArgValue(Id: OPT_host_interface, Default: "0.0.0.0" ); |
122 | } |
123 | |
124 | int llvm_debuginfod_main(int argc, char **argv, const llvm::ToolContext &) { |
125 | HTTPClient::initialize(); |
126 | parseArgs(argc, argv); |
127 | |
128 | SmallVector<StringRef, 1> Paths; |
129 | llvm::append_range(C&: Paths, R&: ScanPaths); |
130 | |
131 | DefaultThreadPool Pool(hardware_concurrency(ThreadCount: MaxConcurrency)); |
132 | DebuginfodLog Log; |
133 | DebuginfodCollection Collection(Paths, Log, Pool, MinInterval); |
134 | DebuginfodServer Server(Log, Collection); |
135 | |
136 | if (!Port) |
137 | Port = ExitOnErr(Server.Server.bind(HostInterface: HostInterface.c_str())); |
138 | else |
139 | ExitOnErr(Server.Server.bind(Port, HostInterface: HostInterface.c_str())); |
140 | |
141 | Log.push(Message: "Listening on port " + Twine(Port).str()); |
142 | |
143 | Pool.async(F: [&]() { ExitOnErr(Server.Server.listen()); }); |
144 | Pool.async(F: [&]() { |
145 | while (true) { |
146 | DebuginfodLogEntry Entry = Log.pop(); |
147 | if (VerboseLogging) { |
148 | outs() << Entry.Message << "\n" ; |
149 | outs().flush(); |
150 | } |
151 | } |
152 | }); |
153 | if (Paths.size()) |
154 | ExitOnErr(Collection.updateForever(Interval: std::chrono::seconds(ScanInterval))); |
155 | Pool.wait(); |
156 | llvm_unreachable("The ThreadPool should never finish running its tasks." ); |
157 | } |
158 | |