Index: llvm/CMakeLists.txt =================================================================== --- llvm/CMakeLists.txt +++ llvm/CMakeLists.txt @@ -971,6 +971,10 @@ set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") include(AddLLVM) + +# make this boolean usable in lit config +llvm_canonicalize_cmake_booleans(LLVM_ENABLE_CURL) + include(TableGen) include(LLVMDistributionSupport) Index: llvm/include/llvm/Debuginfod/Debuginfod.h =================================================================== --- /dev/null +++ llvm/include/llvm/Debuginfod/Debuginfod.h @@ -0,0 +1,44 @@ +//===-- llvm/Support/Debuginfod.h - Debuginfod client library ---*- C++ -*-===// +// +// 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 declarations of the debuginfod::fetchInfo +/// function and the debuginfod::AssetType enum class +/// +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_DEBUGINFOD_H +#define LLVM_SUPPORT_DEBUGINFOD_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" + +namespace llvm { + +/// DebuginfodAsset - This enum class specifies the types of +/// debugging information that can be requested from a debuginfod +/// server. +enum class DebuginfodAssetType { Executable, Debuginfo, Source }; + +/// Fetch a debuginfod asset to a file in a local cache and return the cached +/// file path. First queries the local cache in the given directory path, +/// followed by the debuginfod servers at the given urls for the specified +/// type of information about the given build ID. Source files are specified +/// by their absolute path provided in the Description. A callback function +/// may optionally be provided for further processing of the fetched asset. +Expected> fetchDebuginfo( + StringRef CacheDirectoryPath, ArrayRef DebuginfodUrls, + StringRef BuildID, DebuginfodAssetType Type, StringRef Description = "", + std::function)> AddBuffer = + [](size_t Task, std::unique_ptr MB) {}); + +} // end namespace llvm + +#endif Index: llvm/include/llvm/Support/HTTPClient.h =================================================================== --- /dev/null +++ llvm/include/llvm/Support/HTTPClient.h @@ -0,0 +1,33 @@ +//===-- llvm/Support/HTTPClient.h - HTTP client library ---*- C++ -------*-===// +// +// 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 declarations of the HTTPResponse struct +/// and httpGet function. +/// +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_HTTP_CLIENT_H +#define LLVM_SUPPORT_HTTP_CLIENT_H + +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" + +namespace llvm { + +struct HTTPResponse { + long Code = 0; + std::unique_ptr Body; +}; + +Expected httpGet(const Twine &Url); + +} // end namespace llvm + +#endif // LLVM_SUPPORT_HTTP_CLIENT_H Index: llvm/lib/CMakeLists.txt =================================================================== --- llvm/lib/CMakeLists.txt +++ llvm/lib/CMakeLists.txt @@ -12,6 +12,7 @@ add_subdirectory(BinaryFormat) add_subdirectory(Bitcode) add_subdirectory(Bitstream) +add_subdirectory(Debuginfod) add_subdirectory(DWARFLinker) add_subdirectory(Extensions) add_subdirectory(Frontend) Index: llvm/lib/Debuginfod/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/lib/Debuginfod/CMakeLists.txt @@ -0,0 +1,11 @@ +if(LLVM_ENABLE_CURL) + add_llvm_component_library(LLVMDebuginfod + Debuginfod.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/Debuginfod + + LINK_COMPONENTS + Support + ) +endif() Index: llvm/lib/Debuginfod/Debuginfod.cpp =================================================================== --- /dev/null +++ llvm/lib/Debuginfod/Debuginfod.cpp @@ -0,0 +1,107 @@ +//===-- llvm/Support/Debuginfod.cpp - Debuginfod client library -----------===// +//-*-===// +// +// 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 defines the fetchInfo function, which retrieves +/// any of the three supported asset types: (executable, debuginfo, source file) +/// associated with a build-id from debuginfod servers. If a source file is to +/// be fetched, its absolute path must be specified in the Description argument +/// to fetchInfo. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/Debuginfod/Debuginfod.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/CachePruning.h" +#include "llvm/Support/Caching.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/HTTPClient.h" +#include "llvm/Support/xxhash.h" + +namespace llvm { + +#define DEBUG_TYPE "DEBUGINFOD" + +Expected> fetchDebuginfo( + StringRef CacheDirectoryPath, ArrayRef DebuginfodUrls, + StringRef BuildID, DebuginfodAssetType Type, StringRef Description, + std::function)> AddBuffer) { + LLVM_DEBUG(dbgs() << "fetching info, debuginfod urls size = " + << DebuginfodUrls.size() << "\n";); + std::string Suffix; + switch (Type) { + case DebuginfodAssetType::Executable: + Suffix = "executable"; + break; + case DebuginfodAssetType::Debuginfo: + Suffix = "debuginfo"; + break; + case DebuginfodAssetType::Source: + // Description is the absolute source path + Suffix = "source" + Description.str(); + break; + } + + std::string UniqueKey = utostr(xxHash64((BuildID + Suffix).str())); + + Expected CacheOrErr = localCache( + "Debuginfod-client", ".debuginfod-client", CacheDirectoryPath, AddBuffer); + if (Error Err = CacheOrErr.takeError()) + return Err; + + NativeObjectCache &Cache = *CacheOrErr; + + // We choose an arbitrary Task parameter as we do not make use of it. + unsigned Task = 0; + AddStreamFn CacheAddStream = Cache(Task, UniqueKey); + + SmallString<64> AbsCachedAssetPath; + sys::path::append(AbsCachedAssetPath, CacheDirectoryPath, + "llvmcache-" + UniqueKey); + + if (!CacheAddStream) { + LLVM_DEBUG(dbgs() << "cache hit\n";); + return AbsCachedAssetPath; + } + + LLVM_DEBUG(dbgs() << "cache miss, UniqueKey = " << UniqueKey << "\n";); + + // The asset was not found in the local cache, so we must query + // the debuginfod servers. + for (const StringRef &ServerUrl : DebuginfodUrls) { + SmallString<64> AssetUrl; + sys::path::append(AssetUrl, ServerUrl, "buildid", BuildID, Suffix); + + Expected ResponseOrErr = httpGet(AssetUrl); + + if (Error Err = ResponseOrErr.takeError()) + return Err; + + HTTPResponse &Resp = *ResponseOrErr; + if (Resp.Code != 200) + continue; + + // We have retrieved the asset from this server, + // and now add it to the file cache. + auto Stream = CacheAddStream(Task); + assert(Resp.Body && "Unallocated MemoryBuffer returned from httpGet."); + *Stream->OS << Resp.Body->getBuffer(); + + // Return the path to the asset on disk. + return AbsCachedAssetPath; + } + + return createStringError(errc::argument_out_of_domain, "build id not found"); +} + +#undef DEBUG_TYPE + +} // end namespace llvm Index: llvm/lib/Support/CMakeLists.txt =================================================================== --- llvm/lib/Support/CMakeLists.txt +++ llvm/lib/Support/CMakeLists.txt @@ -74,6 +74,11 @@ set(system_libs ${system_libs} ${Z3_LIBRARIES}) endif() +# Link LibCURL if the user wants it +if (LLVM_ENABLE_CURL) + set(system_libs ${system_libs} ${CURL_LIBRARIES}) +endif() + # Override the C runtime allocator on Windows and embed it into LLVM tools & libraries if(LLVM_INTEGRATED_CRT_ALLOC) if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$") @@ -155,6 +160,7 @@ GlobPattern.cpp GraphWriter.cpp Hashing.cpp + HTTPClient.cpp InitLLVM.cpp InstructionCost.cpp IntEqClasses.cpp Index: llvm/lib/Support/HTTPClient.cpp =================================================================== --- /dev/null +++ llvm/lib/Support/HTTPClient.cpp @@ -0,0 +1,120 @@ +//===-- llvm/Support/HTTPClient.cpp - HTTP client library -------*- C++ -*-===// +// +// 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 defines the httpGet function, implemented using libcurl. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/HTTPClient.h" + +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" + +#include + +namespace llvm { + +#define DEBUG_TYPE "HTTPClient" + +namespace { + +struct OffsetBuffer { + size_t Offset = 0; + std::unique_ptr Buffer; +}; + +size_t writeMemoryCallback(char *Contents, size_t Size, size_t NMemb, + OffsetBuffer *Buf) { + if (!Buf->Buffer) { + // No buffer has been allocated, so cause curl + // to return an error code. + return 0; + } + + size_t ChunkSize = Size * NMemb; + memcpy(Buf->Buffer->getBufferStart() + Buf->Offset, Contents, ChunkSize); + Buf->Offset += ChunkSize; + return ChunkSize; +} + +bool parseHTTPContentLength(StringRef LineRef, + unsigned long long &ContentLength) { + // Content-Length is a mandatory header, and the only one we handle. + if (LineRef.consume_front("Content-Length: ")) { + llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength); + return true; + } + return false; +} + +size_t allocateMemoryCallback(char *Contents, size_t Size, size_t NMemb, + OffsetBuffer *Buf) { + assert(Size == 1 && "The Size passed by libCURL to CURLOPT_HEADERFUNCTION " + "should always be 1."); + + if (Buf->Buffer) + return NMemb; + + StringRef Header(Contents, NMemb); + unsigned long long ContentLength; + if (!parseHTTPContentLength(Header, ContentLength)) + return NMemb; + + LLVM_DEBUG(dbgs() << "Received Content-Length header value " << ContentLength + << "\n";); + + Buf->Buffer = WritableMemoryBuffer::getNewUninitMemBuffer(ContentLength); + + return NMemb; +} + +} // end anonymous namespace + +Expected httpGet(const Twine &Url) { + LLVM_DEBUG(dbgs() << "getting Url " << Url << "\n";); + + CURL *Curl = curl_easy_init(); + CURLcode CurlRes; + if (!Curl) + return createStringError(errc::io_error, "http library error"); + + curl_easy_setopt(Curl, CURLOPT_URL, Url.str().c_str()); + curl_easy_setopt(Curl, CURLOPT_FOLLOWLOCATION, 1); + + OffsetBuffer Buf; + + curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, writeMemoryCallback); + curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &Buf); + + curl_easy_setopt(Curl, CURLOPT_HEADERFUNCTION, allocateMemoryCallback); + curl_easy_setopt(Curl, CURLOPT_HEADERDATA, &Buf); + + LLVM_DEBUG(dbgs() << "performing the curl\n";); + CurlRes = curl_easy_perform(Curl); + LLVM_DEBUG(dbgs() << "got the CurlRes\n";); + + curl_easy_cleanup(Curl); + + if (CurlRes != CURLE_OK) + return createStringError(errc::io_error, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(CurlRes)); + + HTTPResponse Resp; + Resp.Body = std::move(Buf.Buffer); + curl_easy_getinfo(Curl, CURLINFO_RESPONSE_CODE, &Resp.Code); + return Resp; +} + +#undef DEBUG_TYPE + +} // end namespace llvm 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', @@ -399,3 +399,6 @@ if config.expensive_checks: config.available_features.add('expensive_checks') + +if getattr(config, 'enable_debuginfod_client', False): + config.available_features.add('debuginfod_client') Index: llvm/test/lit.site.cfg.py.in =================================================================== --- llvm/test/lit.site.cfg.py.in +++ llvm/test/lit.site.cfg.py.in @@ -37,6 +37,7 @@ config.llvm_use_intel_jitevents = @LLVM_USE_INTEL_JITEVENTS@ config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@" config.have_zlib = @LLVM_ENABLE_ZLIB@ +config.enable_debuginfod_client = @LLVM_ENABLE_CURL@ config.have_libxar = @LLVM_HAVE_LIBXAR@ config.have_libxml2 = @LLVM_ENABLE_LIBXML2@ config.have_dia_sdk = @LLVM_ENABLE_DIA_SDK@ Index: llvm/test/tools/llvm-debuginfod/Inputs/buildid/fake_build_id/debuginfo =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod/Inputs/buildid/fake_build_id/debuginfo @@ -0,0 +1 @@ +fake_debuginfo Index: llvm/test/tools/llvm-debuginfod/Inputs/buildid/fake_build_id/executable =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod/Inputs/buildid/fake_build_id/executable @@ -0,0 +1 @@ +fake_executable Index: llvm/test/tools/llvm-debuginfod/Inputs/buildid/fake_build_id/source/directory/file.c =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod/Inputs/buildid/fake_build_id/source/directory/file.c @@ -0,0 +1 @@ +int foo = 0; Index: llvm/test/tools/llvm-debuginfod/client-server-test.py =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod/client-server-test.py @@ -0,0 +1,109 @@ +import argparse +import json +import os +import subprocess +import sys +import threading +import time + + +def run_test(clients, servers, client_delay, timeout) -> int: + timeout_failure = False + def kill_subprocess_and_fail_test(process): + nonlocal timeout_failure + process.kill() + timeout_failure = True + + groups = ['servers', 'clients'] + group_delays = {'servers': 0, 'clients': client_delay} + processes = {group: [] for group in groups} + watchdogs = {group: [] for group in groups} + specs = {'clients': clients, 'servers': servers} + for group in groups: + time.sleep(group_delays[group]) + print(group) + for process_spec in specs[group]: + print(process_spec) + process = subprocess.Popen( + process_spec['args'], + env={**os.environ, **process_spec.get('env', {})}) + processes[group].append(process) + + watchdog = threading.Timer( + timeout, kill_subprocess_and_fail_test, + args=[process]) + watchdog.start() + watchdogs[group].append(watchdog) + + all_clients_exited_normally = all(client.wait()==0 + for client in processes['clients']) + print(f'all_clients_exited_normally = {all_clients_exited_normally}') + test_passed = all_clients_exited_normally and not timeout_failure + + # kill watchdogs and processes + for group in groups: + for watchdog in watchdogs[group]: + watchdog.cancel() + for process in processes[group]: + process.kill() + + if test_passed: + return 0 + + return 1 + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Test client(s) and server(s) against each other. ' + 'Start servers first in the specified order, then ' + 'wait for client-delay before starting clients in ' + 'the specified order. Fails the test if any client' + ' has a nonzero exit code.') + + parser.add_argument( + '--clients', + type=str, + nargs='*', + default=[], + help='Specification of a client process.\n' + 'Must contain:\n' + ' `args`: [array of str]\n' + 'May optionally include:\n' + ' `env` : {str: str}\n' + ) + + parser.add_argument( + '--servers', + type=str, + nargs='*', + default=[], + help='Specification of a server process.\n' + 'Must contain:\n' + ' `args`: [array of str]\n' + 'May optionally include:\n' + ' `env` : {str: str}\n' + ) + + parser.add_argument( + '--client-delay', + type=float, + default=2, + help='How long to wait after starting the ' + 'server(s) before starting the client(s).' + ) + + parser.add_argument( + '--timeout', + type=float, + default=20, + help='How long to wait until forcefully exiting all ' + 'processes and failing the test.' + ) + + args = parser.parse_args() + result = run_test([json.loads(client) for client in args.clients], + [json.loads(server) for server in args.servers], + args.client_delay, args.timeout) + print(f'result = {result}') + os._exit(result) Index: llvm/test/tools/llvm-debuginfod/llvm-debuginfod-find.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-debuginfod/llvm-debuginfod-find.test @@ -0,0 +1,14 @@ +# REQUIRES: debuginfod_client +# RUN: python %S/client-server-test.py \ +# RUN: --servers '{"args": ["python", "-m", "http.server", \ +# RUN: "--directory", "%S/Inputs", "13576"]}' \ +# RUN: --clients '{"args": ["llvm-debuginfod-find", \ +# RUN: "fake_build_id", "--executable"],\ +# RUN: "env": {"DEBUGINFOD_URLS":"http://localhost:13576"}}'\ +# RUN: '{"args": ["llvm-debuginfod-find", \ +# RUN: "fake_build_id", "--debuginfo"],\ +# RUN: "env": {"DEBUGINFOD_URLS":"http://localhost:13576"}}'\ +# RUN: '{"args": ["llvm-debuginfod-find", \ +# RUN: "fake_build_id", "--source", "/directory/file.c"],\ +# RUN: "env": {"DEBUGINFOD_URLS":"http://localhost:13576"}}'\ +# RUN: --client-delay 2 --timeout 10 Index: llvm/tools/llvm-debuginfod/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/tools/llvm-debuginfod/CMakeLists.txt @@ -0,0 +1,13 @@ +if (LLVM_ENABLE_CURL) + set(LLVM_LINK_COMPONENTS + Debuginfod + Support + ) + add_llvm_tool(llvm-debuginfod-find + llvm-debuginfod-find.cpp + ) + set_property(TARGET llvm-debuginfod-find PROPERTY LLVM_SYSTEM_LIBS ${imported_libs}) + if(LLVM_INSTALL_BINUTILS_SYMLINKS) + add_llvm_tool_symlink(debuginfod-find llvm-debuginfod-find) + endif() +endif() Index: llvm/tools/llvm-debuginfod/llvm-debuginfod-find.cpp =================================================================== --- /dev/null +++ llvm/tools/llvm-debuginfod/llvm-debuginfod-find.cpp @@ -0,0 +1,110 @@ +//===-- 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 + +#define DEBUG_TYPE "llvm-debuginfod-find" + +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")); + +static cl::opt CacheDir("cache-dir", cl::init(""), + cl::desc("Cache Directory"), + cl::value_desc("directory")); + +ExitOnError ExitOnErr; + +int main(int argc, char **argv) { + InitLLVM X(argc, argv); + + cl::ParseCommandLineOptions(argc, argv); + + const char *DebuginfodUrlsEnv = std::getenv("DEBUGINFOD_URLS"); + if (DebuginfodUrlsEnv == NULL) { + errs() << "DEBUGINFOD_URLS not set\n"; + return 1; + } + + SmallVector DebuginfodUrls; + StringRef(DebuginfodUrlsEnv).split(DebuginfodUrls, " "); + + SmallString<64> CacheDirectoryPath = StringRef(CacheDir); + if (!CacheDirectoryPath.size() && + !sys::path::cache_directory(CacheDirectoryPath)) + CacheDirectoryPath = "."; + + assert(CacheDirectoryPath.size() && "CacheDirectoryPath should be nonempty"); + + DebuginfodAssetType Type; + StringRef Description; + if (FetchExecutable) { + Type = DebuginfodAssetType::Executable; + } else if (FetchDebuginfo) { + Type = DebuginfodAssetType::Debuginfo; + } else if (FetchSource != "") { + Type = DebuginfodAssetType::Source; + Description = FetchSource; + } else { + llvm_unreachable("invalid asset request"); + } + + if (Type == DebuginfodAssetType::Source) { + // Print the contents of the source file + ExitOnErr(fetchDebuginfo(CacheDirectoryPath, DebuginfodUrls, InputBuildID, + Type, Description, + [](size_t Task, std::unique_ptr MB) { + outs() << MB->getBuffer(); + })); + } else { + // Print the path to the cached binary file on disk + outs() << ExitOnErr(fetchDebuginfo(CacheDirectoryPath, DebuginfodUrls, + InputBuildID, Type, Description)) + << "\n"; + } +} + +#undef DEBUG_TYPE Index: llvm/unittests/CMakeLists.txt =================================================================== --- llvm/unittests/CMakeLists.txt +++ llvm/unittests/CMakeLists.txt @@ -22,6 +22,7 @@ add_subdirectory(Bitstream) add_subdirectory(CodeGen) add_subdirectory(DebugInfo) +add_subdirectory(Debuginfod) add_subdirectory(Demangle) add_subdirectory(ExecutionEngine) add_subdirectory(FileCheck) Index: llvm/unittests/Debuginfod/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/unittests/Debuginfod/CMakeLists.txt @@ -0,0 +1,9 @@ +if (LLVM_ENABLE_CURL) + set(LLVM_LINK_COMPONENTS + Debuginfod + ) + + add_llvm_unittest(DebuginfodTests + DebuginfodTests.cpp + ) +endif() Index: llvm/unittests/Debuginfod/DebuginfodTests.cpp =================================================================== --- /dev/null +++ llvm/unittests/Debuginfod/DebuginfodTests.cpp @@ -0,0 +1,19 @@ +//===-- llvm/unittest/Support/DebuginfodTests.cpp - unit tests --*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/Debuginfod/Debuginfod.h" + +#include "gtest/gtest.h" + +TEST(DebuginfodTests, noDebuginfodUrlsFetchInfoTest) { + auto InfoOrErr = fetchDebuginfo("./", {}, "fakeBuildId", + llvm::DebuginfodAssetType::Executable, ""); + handleAllErrors(InfoOrErr.takeError(), [](const llvm::StringError &SE) { + llvm::dbgs() << "got a StringError as expected\n"; + }); +} Index: llvm/unittests/Support/CMakeLists.txt =================================================================== --- llvm/unittests/Support/CMakeLists.txt +++ llvm/unittests/Support/CMakeLists.txt @@ -41,6 +41,7 @@ GlobPatternTest.cpp HashBuilderTest.cpp Host.cpp + HTTPClient.cpp IndexedAccessorTest.cpp InstructionCostTest.cpp ItaniumManglingCanonicalizerTest.cpp Index: llvm/unittests/Support/HTTPClient.cpp =================================================================== --- /dev/null +++ llvm/unittests/Support/HTTPClient.cpp @@ -0,0 +1,23 @@ +//===-- llvm/unittest/Support/HTTPClient.cpp - unit tests -------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifdef LLVM_WITH_CURL + +#include "llvm/Support/HTTPClient.h" + +#include "gtest/gtest.h" + +TEST(HTTPClientTests, invalidUrlTest) { + std::string invalidUrl = "llvm is fun"; + handleAllErrors(llvm::httpGet(invalidUrl).takeError(), + [](const llvm::StringError &SE) { + llvm::dbgs() << "got a StringError as expected\n"; + }); +} + +#endif