Index: llvm/test/CMakeLists.txt =================================================================== --- llvm/test/CMakeLists.txt +++ llvm/test/CMakeLists.txt @@ -6,6 +6,7 @@ LLVM_ENABLE_DIA_SDK LLVM_ENABLE_FFI LLVM_ENABLE_THREADS + LLVM_ENABLE_CURL LLVM_ENABLE_ZLIB LLVM_ENABLE_LIBXML2 LLVM_INCLUDE_GO_TESTS Index: llvm/test/lit.cfg.py =================================================================== --- llvm/test/lit.cfg.py +++ llvm/test/lit.cfg.py @@ -158,9 +158,9 @@ tools.extend([ 'dsymutil', 'lli', 'lli-child-target', 'llvm-ar', 'llvm-as', 'llvm-addr2line', 'llvm-bcanalyzer', 'llvm-bitcode-strip', 'llvm-config', - 'llvm-cov', 'llvm-cxxdump', 'llvm-cvtres', 'llvm-diff', 'llvm-dis', - 'llvm-dwarfdump', 'llvm-dlltool', 'llvm-exegesis', 'llvm-extract', - 'llvm-isel-fuzzer', 'llvm-ifs', + 'llvm-cov', 'llvm-cxxdump', 'llvm-cvtres', 'llvm-debuginfod-find', + 'llvm-diff', 'llvm-dis', 'llvm-dwarfdump', 'llvm-dlltool', 'llvm-exegesis', + 'llvm-extract', 'llvm-isel-fuzzer', 'llvm-ifs', 'llvm-install-name-tool', 'llvm-jitlink', 'llvm-opt-fuzzer', 'llvm-lib', 'llvm-link', 'llvm-lto', 'llvm-lto2', 'llvm-mc', 'llvm-mca', 'llvm-modextract', 'llvm-nm', 'llvm-objcopy', 'llvm-objdump', 'llvm-otool', @@ -394,6 +394,9 @@ if config.have_libxml2: config.available_features.add('libxml2') +if config.have_curl: + config.available_features.add('curl') + if config.have_opt_viewer_modules: config.available_features.add('have_opt_viewer_modules') Index: llvm/test/lit.site.cfg.py.in =================================================================== --- llvm/test/lit.site.cfg.py.in +++ llvm/test/lit.site.cfg.py.in @@ -39,6 +39,7 @@ config.have_zlib = @LLVM_ENABLE_ZLIB@ config.have_libxar = @LLVM_HAVE_LIBXAR@ config.have_libxml2 = @LLVM_ENABLE_LIBXML2@ +config.have_curl = @LLVM_ENABLE_CURL@ config.have_dia_sdk = @LLVM_ENABLE_DIA_SDK@ config.enable_ffi = @LLVM_ENABLE_FFI@ config.build_examples = @LLVM_BUILD_EXAMPLES@ Index: llvm/test/tools/llvm-debuginfod-find/Inputs/buildid/abcdef/debuginfo =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod-find/Inputs/buildid/abcdef/debuginfo @@ -0,0 +1 @@ +fake_debuginfo Index: llvm/test/tools/llvm-debuginfod-find/Inputs/buildid/abcdef/executable =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod-find/Inputs/buildid/abcdef/executable @@ -0,0 +1 @@ +fake_executable Index: llvm/test/tools/llvm-debuginfod-find/Inputs/buildid/abcdef/source/directory/file.c =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod-find/Inputs/buildid/abcdef/source/directory/file.c @@ -0,0 +1 @@ +int foo = 0; Index: llvm/test/tools/llvm-debuginfod-find/debuginfod-find.py =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod-find/debuginfod-find.py @@ -0,0 +1,68 @@ +# REQUIRES: curl +# RUN: rm -rf %t && mkdir %t && python %S/debuginfod-find.py %S/Inputs \ +# RUN: llvm-debuginfod-find %t +import threading +import http.server +import functools +import subprocess +import os + + +test_assets = [ + (['--executable'], '1638848739525074778', 'fake_executable\n'), + (['--source=/directory/file.c'], '12251279412980855456', + 'int foo = 0;\n'), + (['--debuginfo'], '8810620761423389721', 'fake_debuginfo\n') +] + +def test_tool(inputs_path, tool_path, cache_directory) -> int: + httpd = http.server.ThreadingHTTPServer( + ('',0), functools.partial( + http.server.SimpleHTTPRequestHandler, + directory=inputs_path)) + port = httpd.server_port + thread = threading.Thread(target=httpd.serve_forever) + + try: + thread.start() + build_id = 'abcdef' + common_args = [tool_path, build_id] + for args, xxhash, contents in test_assets: + process = subprocess.Popen( + common_args + args, env={**os.environ, + 'DEBUGINFOD_URLS': f'http://localhost:{port}', + 'DEBUGINFOD_CACHE_PATH': cache_directory}) + if process.wait() != 0: + print('process returned nontrivial error code') + return 1 + fname = os.path.join(cache_directory, + f'llvmcache-{xxhash}') + if not os.path.exists(fname): + print(f'file {fname} doesnt exist') + return 1 + with open(fname) as infile: + if infile.read() != contents: + print('file contents not as expected') + return 1 + + finally: + httpd.shutdown() + thread.join() + + return 0 + + +def main(): + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('inputs_path') + parser.add_argument('tool_path') + parser.add_argument('cache_directory') + args = parser.parse_args() + result = test_tool(args.inputs_path, args.tool_path, + args.cache_directory) + os._exit(result) + + +if __name__ == '__main__': + main() Index: llvm/test/tools/llvm-debuginfod-find/lit.local.cfg =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod-find/lit.local.cfg @@ -0,0 +1 @@ +config.suffixes = ['.py'] Index: llvm/tools/llvm-debuginfod-find/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/tools/llvm-debuginfod-find/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_LINK_COMPONENTS + Debuginfod + Support + ) +add_llvm_tool(llvm-debuginfod-find + llvm-debuginfod-find.cpp + ) +if(LLVM_INSTALL_BINUTILS_SYMLINKS) + add_llvm_tool_symlink(debuginfod-find llvm-debuginfod-find) +endif() Index: llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp =================================================================== --- /dev/null +++ llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp @@ -0,0 +1,97 @@ +//===-- llvm-debuginfod-find.cpp - Simple CLI for libdebuginfod-client ----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This file contains the llvm-debuginfod-find tool. This tool +/// queries the debuginfod servers in the DEBUGINFOD_URLS environment +/// variable (delimited by space (" ")) for the executable, +/// debuginfo, or specified source file of the binary matching the +/// given build-id. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/StringRef.h" +#include "llvm/Config/config.h" +#include "llvm/Debuginfod/Debuginfod.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/COM.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/StringSaver.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include +#include + +using namespace llvm; + +cl::opt InputBuildID(cl::Positional, cl::Required, + cl::desc(""), cl::init("-")); + +static cl::opt + FetchExecutable("executable", cl::init(false), + cl::desc("fetch the associated executable")); + +static cl::opt FetchDebuginfo("debuginfo", cl::init(false), + cl::desc("fetch associated debuginfo")); + +static cl::opt FetchSource("source", cl::init(""), + cl::desc("/filename")); + +[[noreturn]] static void helpExit() { + errs() << "Must specify exactly one of --executable, " + "--source=/path/to/file, or --debuginfo."; + exit(1); +} + +ExitOnError ExitOnErr; + +int main(int argc, char **argv) { + InitLLVM X(argc, argv); + + cl::ParseCommandLineOptions( + argc, argv, + "llvm-debuginfod-find: Fetch debuginfod artifacts\n\n" + "This program is a frontend to the debuginfod client library. The cache " + "directory, request timeout (in seconds), and debuginfod server urls are " + "set by these environment variables:\n" + "DEBUGINFOD_CACHE_PATH (default set by sys::path::cache_directory)\n" + "DEBUGINFOD_TIMEOUT (defaults to 90s)\n" + "DEBUGINFOD_URLS=[comma separated URLs] (defaults to empty)\n"); + + if (FetchExecutable + FetchDebuginfo + (FetchSource != "") != 1) + helpExit(); + + std::string IDString; + if (!tryGetFromHex(InputBuildID, IDString)) { + errs() << "Build ID " << InputBuildID << " is not a hex string.\n"; + exit(1); + } + BuildID ID(IDString.begin(), IDString.end()); + + std::string Path; + if (FetchSource != "") + Path = ExitOnErr(getCachedOrDownloadSource(ID, FetchSource)); + else if (FetchExecutable) + Path = ExitOnErr(getCachedOrDownloadExecutable(ID)); + else if (FetchDebuginfo) + Path = ExitOnErr(getCachedOrDownloadDebuginfo(ID)); + else + llvm_unreachable("We have already checked that exactly one of the above " + "conditions is true."); + + // Print the path to the cached binary file on disk + outs() << Path << "\n"; +}