136f01909SNoah Shutty //===-- llvm-debuginfod.cpp - federating debuginfod server ----------------===//
236f01909SNoah Shutty //
336f01909SNoah Shutty // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
436f01909SNoah Shutty // See https://llvm.org/LICENSE.txt for license information.
536f01909SNoah Shutty // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
636f01909SNoah Shutty //
736f01909SNoah Shutty //===----------------------------------------------------------------------===//
836f01909SNoah Shutty ///
936f01909SNoah Shutty /// \file
1036f01909SNoah Shutty /// This file contains the llvm-debuginfod tool, which serves the debuginfod
1136f01909SNoah Shutty /// protocol over HTTP. The tool periodically scans zero or more filesystem
1236f01909SNoah Shutty /// directories for ELF binaries to serve, and federates requests for unknown
1336f01909SNoah Shutty /// build IDs to the debuginfod servers set in the DEBUGINFOD_URLS environment
1436f01909SNoah Shutty /// variable.
1536f01909SNoah Shutty ///
1636f01909SNoah Shutty //===----------------------------------------------------------------------===//
1736f01909SNoah Shutty 
1836f01909SNoah Shutty #include "llvm/Debuginfod/Debuginfod.h"
1936f01909SNoah Shutty #include "llvm/Debuginfod/HTTPClient.h"
2036f01909SNoah Shutty #include "llvm/Support/CommandLine.h"
2136f01909SNoah Shutty #include "llvm/Support/InitLLVM.h"
2236f01909SNoah Shutty #include "llvm/Support/ThreadPool.h"
2336f01909SNoah Shutty 
2436f01909SNoah Shutty using namespace llvm;
2536f01909SNoah Shutty 
2636f01909SNoah Shutty cl::OptionCategory DebuginfodCategory("llvm-debuginfod Options");
2736f01909SNoah Shutty 
2836f01909SNoah Shutty static cl::list<std::string> ScanPaths(cl::Positional,
2936f01909SNoah Shutty                                        cl::desc("<Directories to scan>"),
3036f01909SNoah Shutty                                        cl::cat(DebuginfodCategory));
3136f01909SNoah Shutty 
3236f01909SNoah Shutty static cl::opt<unsigned>
3336f01909SNoah Shutty     Port("p", cl::init(0),
3436f01909SNoah Shutty          cl::desc("Port to listen on. Set to 0 to bind to any available port."),
3536f01909SNoah Shutty          cl::cat(DebuginfodCategory));
3636f01909SNoah Shutty 
3736f01909SNoah Shutty static cl::opt<std::string>
3836f01909SNoah Shutty     HostInterface("i", cl::init("0.0.0.0"),
3936f01909SNoah Shutty                   cl::desc("Host interface to bind to."),
4036f01909SNoah Shutty                   cl::cat(DebuginfodCategory));
4136f01909SNoah Shutty 
4236f01909SNoah Shutty static cl::opt<int>
4336f01909SNoah Shutty     ScanInterval("t", cl::init(300),
4436f01909SNoah Shutty                  cl::desc("Number of seconds to wait between subsequent "
4536f01909SNoah Shutty                           "automated scans of the filesystem."),
4636f01909SNoah Shutty                  cl::cat(DebuginfodCategory));
4736f01909SNoah Shutty 
4836f01909SNoah Shutty static cl::opt<double> MinInterval(
4936f01909SNoah Shutty     "m", cl::init(10),
5036f01909SNoah Shutty     cl::desc(
5136f01909SNoah Shutty         "Minimum number of seconds to wait before an on-demand update can be "
5236f01909SNoah Shutty         "triggered by a request for a buildid which is not in the collection."),
5336f01909SNoah Shutty     cl::cat(DebuginfodCategory));
5436f01909SNoah Shutty 
5536f01909SNoah Shutty static cl::opt<size_t>
5636f01909SNoah Shutty     MaxConcurrency("c", cl::init(0),
5736f01909SNoah Shutty                    cl::desc("Maximum number of files to scan concurrently. If "
5836f01909SNoah Shutty                             "0, use the hardware concurrency."),
5936f01909SNoah Shutty                    cl::cat(DebuginfodCategory));
6036f01909SNoah Shutty 
6136f01909SNoah Shutty static cl::opt<bool> VerboseLogging("v", cl::init(false),
6236f01909SNoah Shutty                                     cl::desc("Enable verbose logging."),
6336f01909SNoah Shutty                                     cl::cat(DebuginfodCategory));
6436f01909SNoah Shutty 
6536f01909SNoah Shutty ExitOnError ExitOnErr;
6636f01909SNoah Shutty 
main(int argc,char ** argv)6736f01909SNoah Shutty int main(int argc, char **argv) {
6836f01909SNoah Shutty   InitLLVM X(argc, argv);
6936f01909SNoah Shutty   HTTPClient::initialize();
7036f01909SNoah Shutty   cl::HideUnrelatedOptions({&DebuginfodCategory});
7136f01909SNoah Shutty   cl::ParseCommandLineOptions(argc, argv);
7236f01909SNoah Shutty 
7336f01909SNoah Shutty   SmallVector<StringRef, 1> Paths;
7436f01909SNoah Shutty   for (const std::string &Path : ScanPaths)
7536f01909SNoah Shutty     Paths.push_back(Path);
7636f01909SNoah Shutty 
7736f01909SNoah Shutty   ThreadPool Pool(hardware_concurrency(MaxConcurrency));
7836f01909SNoah Shutty   DebuginfodLog Log;
7936f01909SNoah Shutty   DebuginfodCollection Collection(Paths, Log, Pool, MinInterval);
8036f01909SNoah Shutty   DebuginfodServer Server(Log, Collection);
8136f01909SNoah Shutty 
8236f01909SNoah Shutty   if (!Port)
8336f01909SNoah Shutty     Port = ExitOnErr(Server.Server.bind(HostInterface.c_str()));
8436f01909SNoah Shutty   else
8536f01909SNoah Shutty     ExitOnErr(Server.Server.bind(Port, HostInterface.c_str()));
8636f01909SNoah Shutty 
8736f01909SNoah Shutty   Log.push("Listening on port " + Twine(Port).str());
8836f01909SNoah Shutty 
8936f01909SNoah Shutty   Pool.async([&]() { ExitOnErr(Server.Server.listen()); });
9036f01909SNoah Shutty   Pool.async([&]() {
91*7094ab4eSKazu Hirata     while (true) {
9236f01909SNoah Shutty       DebuginfodLogEntry Entry = Log.pop();
9336f01909SNoah Shutty       if (VerboseLogging) {
9436f01909SNoah Shutty         outs() << Entry.Message << "\n";
9536f01909SNoah Shutty         outs().flush();
9636f01909SNoah Shutty       }
9736f01909SNoah Shutty     }
9836f01909SNoah Shutty   });
9936f01909SNoah Shutty   if (Paths.size())
10036f01909SNoah Shutty     ExitOnErr(Collection.updateForever(std::chrono::seconds(ScanInterval)));
10136f01909SNoah Shutty   Pool.wait();
10236f01909SNoah Shutty   llvm_unreachable("The ThreadPool should never finish running its tasks.");
10336f01909SNoah Shutty }
104