1f0ca8d24SNoah Shutty //===-- llvm-debuginfod-find.cpp - Simple CLI for libdebuginfod-client ----===// 2f0ca8d24SNoah Shutty // 3f0ca8d24SNoah Shutty // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4f0ca8d24SNoah Shutty // See https://llvm.org/LICENSE.txt for license information. 5f0ca8d24SNoah Shutty // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6f0ca8d24SNoah Shutty // 7f0ca8d24SNoah Shutty //===----------------------------------------------------------------------===// 8f0ca8d24SNoah Shutty /// 9f0ca8d24SNoah Shutty /// \file 10f0ca8d24SNoah Shutty /// This file contains the llvm-debuginfod-find tool. This tool 11f0ca8d24SNoah Shutty /// queries the debuginfod servers in the DEBUGINFOD_URLS environment 12f0ca8d24SNoah Shutty /// variable (delimited by space (" ")) for the executable, 13f0ca8d24SNoah Shutty /// debuginfo, or specified source file of the binary matching the 14f0ca8d24SNoah Shutty /// given build-id. 15f0ca8d24SNoah Shutty /// 16f0ca8d24SNoah Shutty //===----------------------------------------------------------------------===// 17f0ca8d24SNoah Shutty 18b0abd489SElliot Goodrich #include "llvm/ADT/StringExtras.h" 19bc152fbfSPrabhuk #include "llvm/ADT/StringRef.h" 20e61d89efSDaniel Thornburgh #include "llvm/Debuginfod/BuildIDFetcher.h" 21f0ca8d24SNoah Shutty #include "llvm/Debuginfod/Debuginfod.h" 22f0ca8d24SNoah Shutty #include "llvm/Debuginfod/HTTPClient.h" 23bc152fbfSPrabhuk #include "llvm/Option/ArgList.h" 24bc152fbfSPrabhuk #include "llvm/Option/Option.h" 25f0ca8d24SNoah Shutty #include "llvm/Support/CommandLine.h" 26f0ca8d24SNoah Shutty #include "llvm/Support/InitLLVM.h" 27bc152fbfSPrabhuk #include "llvm/Support/LLVMDriver.h" 28f0ca8d24SNoah Shutty 29f0ca8d24SNoah Shutty using namespace llvm; 30f0ca8d24SNoah Shutty 31bc152fbfSPrabhuk // Command-line option boilerplate. 32bc152fbfSPrabhuk namespace { 33bc152fbfSPrabhuk enum ID { 34bc152fbfSPrabhuk OPT_INVALID = 0, // This is not an option ID. 35bc152fbfSPrabhuk #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__), 36bc152fbfSPrabhuk #include "Opts.inc" 37bc152fbfSPrabhuk #undef OPTION 38bc152fbfSPrabhuk }; 39bc152fbfSPrabhuk 40*dd647e3eSChandler Carruth #define OPTTABLE_STR_TABLE_CODE 41bc152fbfSPrabhuk #include "Opts.inc" 42*dd647e3eSChandler Carruth #undef OPTTABLE_STR_TABLE_CODE 43*dd647e3eSChandler Carruth 44*dd647e3eSChandler Carruth #define OPTTABLE_PREFIXES_TABLE_CODE 45*dd647e3eSChandler Carruth #include "Opts.inc" 46*dd647e3eSChandler Carruth #undef OPTTABLE_PREFIXES_TABLE_CODE 47bc152fbfSPrabhuk 48bc152fbfSPrabhuk using namespace llvm::opt; 49bc152fbfSPrabhuk static constexpr opt::OptTable::Info InfoTable[] = { 50bc152fbfSPrabhuk #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__), 51bc152fbfSPrabhuk #include "Opts.inc" 52bc152fbfSPrabhuk #undef OPTION 53bc152fbfSPrabhuk }; 54bc152fbfSPrabhuk 55bc152fbfSPrabhuk class DebuginfodFindOptTable : public opt::GenericOptTable { 56bc152fbfSPrabhuk public: 57*dd647e3eSChandler Carruth DebuginfodFindOptTable() 58*dd647e3eSChandler Carruth : GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {} 59bc152fbfSPrabhuk }; 60bc152fbfSPrabhuk 61bc152fbfSPrabhuk } // end anonymous namespace 62bc152fbfSPrabhuk 63bc152fbfSPrabhuk static std::string InputBuildID; 64bc152fbfSPrabhuk static bool FetchExecutable; 65bc152fbfSPrabhuk static bool FetchDebuginfo; 66bc152fbfSPrabhuk static std::string FetchSource; 67bc152fbfSPrabhuk static bool DumpToStdout; 68bc152fbfSPrabhuk static std::vector<std::string> DebugFileDirectory; 69bc152fbfSPrabhuk 70bc152fbfSPrabhuk static void parseArgs(int argc, char **argv) { 71bc152fbfSPrabhuk DebuginfodFindOptTable Tbl; 72bc152fbfSPrabhuk llvm::BumpPtrAllocator A; 73bc152fbfSPrabhuk llvm::StringSaver Saver{A}; 74bc152fbfSPrabhuk opt::InputArgList Args = 75bc152fbfSPrabhuk Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) { 76bc152fbfSPrabhuk llvm::errs() << Msg << '\n'; 77bc152fbfSPrabhuk std::exit(1); 78bc152fbfSPrabhuk }); 79bc152fbfSPrabhuk 80bc152fbfSPrabhuk if (Args.hasArg(OPT_help)) { 81ab83d31bSPrabhuk Tbl.printHelp( 82ab83d31bSPrabhuk llvm::outs(), "llvm-debuginfod-find [options] <input build_id>", 83ab83d31bSPrabhuk "llvm-debuginfod-find: Fetch debuginfod artifacts\n\n" 84ab83d31bSPrabhuk "This program is a frontend to the debuginfod client library. The " 85ab83d31bSPrabhuk "cache directory, request timeout (in seconds), and debuginfod server " 86ab83d31bSPrabhuk "urls are set by these environment variables:\n" 87ab83d31bSPrabhuk "DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n" 88ab83d31bSPrabhuk "DEBUGINFOD_TIMEOUT (defaults to 90s)\n" 89ab83d31bSPrabhuk "DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)"); 90bc152fbfSPrabhuk std::exit(0); 91bc152fbfSPrabhuk } 92bc152fbfSPrabhuk 93bc152fbfSPrabhuk InputBuildID = Args.getLastArgValue(OPT_INPUT); 94bc152fbfSPrabhuk 95bc152fbfSPrabhuk FetchExecutable = Args.hasArg(OPT_fetch_executable); 96bc152fbfSPrabhuk FetchDebuginfo = Args.hasArg(OPT_fetch_debuginfo); 97bc152fbfSPrabhuk DumpToStdout = Args.hasArg(OPT_dump_to_stdout); 98bc152fbfSPrabhuk FetchSource = Args.getLastArgValue(OPT_fetch_source, ""); 99bc152fbfSPrabhuk DebugFileDirectory = Args.getAllArgValues(OPT_debug_file_directory); 100bc152fbfSPrabhuk } 101bc152fbfSPrabhuk 102bc152fbfSPrabhuk [[noreturn]] static void helpExit() { 103bc152fbfSPrabhuk errs() << "Must specify exactly one of --executable, " 104bc152fbfSPrabhuk "--source=/path/to/file, or --debuginfo.\n"; 105bc152fbfSPrabhuk exit(1); 106bc152fbfSPrabhuk } 107bc152fbfSPrabhuk 108bc152fbfSPrabhuk ExitOnError ExitOnDebuginfodFindError; 109f0ca8d24SNoah Shutty 110e61d89efSDaniel Thornburgh static std::string fetchDebugInfo(object::BuildIDRef BuildID); 111eafa0530SDaniel Thornburgh 112bc152fbfSPrabhuk int llvm_debuginfod_find_main(int argc, char **argv, 113bc152fbfSPrabhuk const llvm::ToolContext &) { 114bc152fbfSPrabhuk // InitLLVM X(argc, argv); 115f0ca8d24SNoah Shutty HTTPClient::initialize(); 116bc152fbfSPrabhuk parseArgs(argc, argv); 117f0ca8d24SNoah Shutty 118f0ca8d24SNoah Shutty if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1) 119f0ca8d24SNoah Shutty helpExit(); 120f0ca8d24SNoah Shutty 121f0ca8d24SNoah Shutty std::string IDString; 122f0ca8d24SNoah Shutty if (!tryGetFromHex(InputBuildID, IDString)) { 123f0ca8d24SNoah Shutty errs() << "Build ID " << InputBuildID << " is not a hex string.\n"; 124f0ca8d24SNoah Shutty exit(1); 125f0ca8d24SNoah Shutty } 126e61d89efSDaniel Thornburgh object::BuildID ID(IDString.begin(), IDString.end()); 127f0ca8d24SNoah Shutty 128f0ca8d24SNoah Shutty std::string Path; 129f0ca8d24SNoah Shutty if (FetchSource != "") 130bc152fbfSPrabhuk Path = 131bc152fbfSPrabhuk ExitOnDebuginfodFindError(getCachedOrDownloadSource(ID, FetchSource)); 132f0ca8d24SNoah Shutty else if (FetchExecutable) 133bc152fbfSPrabhuk Path = ExitOnDebuginfodFindError(getCachedOrDownloadExecutable(ID)); 134f0ca8d24SNoah Shutty else if (FetchDebuginfo) 135eafa0530SDaniel Thornburgh Path = fetchDebugInfo(ID); 136f0ca8d24SNoah Shutty else 137f0ca8d24SNoah Shutty llvm_unreachable("We have already checked that exactly one of the above " 138f0ca8d24SNoah Shutty "conditions is true."); 139f0ca8d24SNoah Shutty 140f0ca8d24SNoah Shutty if (DumpToStdout) { 141f0ca8d24SNoah Shutty // Print the contents of the artifact. 142f0ca8d24SNoah Shutty ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = MemoryBuffer::getFile( 143f0ca8d24SNoah Shutty Path, /*IsText=*/false, /*RequiresNullTerminator=*/false); 144bc152fbfSPrabhuk ExitOnDebuginfodFindError(errorCodeToError(Buf.getError())); 145f0ca8d24SNoah Shutty outs() << Buf.get()->getBuffer(); 146f0ca8d24SNoah Shutty } else 147f0ca8d24SNoah Shutty // Print the path to the cached artifact file. 148f0ca8d24SNoah Shutty outs() << Path << "\n"; 149bc152fbfSPrabhuk 150bc152fbfSPrabhuk return 0; 151f0ca8d24SNoah Shutty } 152eafa0530SDaniel Thornburgh 153e61d89efSDaniel Thornburgh // Find a debug file in local build ID directories and via debuginfod. 154e61d89efSDaniel Thornburgh std::string fetchDebugInfo(object::BuildIDRef BuildID) { 155c302fb5cSFangrui Song if (std::optional<std::string> Path = 156e61d89efSDaniel Thornburgh DebuginfodFetcher(DebugFileDirectory).fetch(BuildID)) 157e61d89efSDaniel Thornburgh return *Path; 158e61d89efSDaniel Thornburgh errs() << "Build ID " << llvm::toHex(BuildID, /*Lowercase=*/true) 1599827467dSDaniel Thornburgh << " could not be found.\n"; 160e61d89efSDaniel Thornburgh exit(1); 161eafa0530SDaniel Thornburgh } 162