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