Debuginfod.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Debuginfod/Debuginfod.h - Debuginfod client --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file contains several declarations for the debuginfod client and
  16. /// server. The client functions are getDefaultDebuginfodUrls,
  17. /// getCachedOrDownloadArtifact, and several convenience functions for specific
  18. /// artifact types: getCachedOrDownloadSource, getCachedOrDownloadExecutable,
  19. /// and getCachedOrDownloadDebuginfo. For the server, this file declares the
  20. /// DebuginfodLogEntry and DebuginfodServer structs, as well as the
  21. /// DebuginfodLog, DebuginfodCollection classes.
  22. ///
  23. //===----------------------------------------------------------------------===//
  24. #ifndef LLVM_DEBUGINFOD_DEBUGINFOD_H
  25. #define LLVM_DEBUGINFOD_DEBUGINFOD_H
  26. #include "HTTPServer.h"
  27. #include "llvm/ADT/StringMap.h"
  28. #include "llvm/ADT/StringRef.h"
  29. #include "llvm/Object/BuildID.h"
  30. #include "llvm/Support/Error.h"
  31. #include "llvm/Support/MemoryBuffer.h"
  32. #include "llvm/Support/Mutex.h"
  33. #include "llvm/Support/RWMutex.h"
  34. #include "llvm/Support/Timer.h"
  35. #include <chrono>
  36. #include <condition_variable>
  37. #include <optional>
  38. #include <queue>
  39. namespace llvm {
  40. /// Returns false if a debuginfod lookup can be determined to have no chance of
  41. /// succeeding.
  42. bool canUseDebuginfod();
  43. /// Finds default array of Debuginfod server URLs by checking DEBUGINFOD_URLS
  44. /// environment variable.
  45. SmallVector<StringRef> getDefaultDebuginfodUrls();
  46. /// Finds a default local file caching directory for the debuginfod client,
  47. /// first checking DEBUGINFOD_CACHE_PATH.
  48. Expected<std::string> getDefaultDebuginfodCacheDirectory();
  49. /// Finds a default timeout for debuginfod HTTP requests. Checks
  50. /// DEBUGINFOD_TIMEOUT environment variable, default is 90 seconds (90000 ms).
  51. std::chrono::milliseconds getDefaultDebuginfodTimeout();
  52. /// Fetches a specified source file by searching the default local cache
  53. /// directory and server URLs.
  54. Expected<std::string> getCachedOrDownloadSource(object::BuildIDRef ID,
  55. StringRef SourceFilePath);
  56. /// Fetches an executable by searching the default local cache directory and
  57. /// server URLs.
  58. Expected<std::string> getCachedOrDownloadExecutable(object::BuildIDRef ID);
  59. /// Fetches a debug binary by searching the default local cache directory and
  60. /// server URLs.
  61. Expected<std::string> getCachedOrDownloadDebuginfo(object::BuildIDRef ID);
  62. /// Fetches any debuginfod artifact using the default local cache directory and
  63. /// server URLs.
  64. Expected<std::string> getCachedOrDownloadArtifact(StringRef UniqueKey,
  65. StringRef UrlPath);
  66. /// Fetches any debuginfod artifact using the specified local cache directory,
  67. /// server URLs, and request timeout (in milliseconds). If the artifact is
  68. /// found, uses the UniqueKey for the local cache file.
  69. Expected<std::string> getCachedOrDownloadArtifact(
  70. StringRef UniqueKey, StringRef UrlPath, StringRef CacheDirectoryPath,
  71. ArrayRef<StringRef> DebuginfodUrls, std::chrono::milliseconds Timeout);
  72. class ThreadPool;
  73. struct DebuginfodLogEntry {
  74. std::string Message;
  75. DebuginfodLogEntry() = default;
  76. DebuginfodLogEntry(const Twine &Message);
  77. };
  78. class DebuginfodLog {
  79. std::mutex QueueMutex;
  80. std::condition_variable QueueCondition;
  81. std::queue<DebuginfodLogEntry> LogEntryQueue;
  82. public:
  83. // Adds a log entry to end of the queue.
  84. void push(DebuginfodLogEntry Entry);
  85. // Adds a log entry to end of the queue.
  86. void push(const Twine &Message);
  87. // Blocks until there are log entries in the queue, then pops and returns the
  88. // first one.
  89. DebuginfodLogEntry pop();
  90. };
  91. /// Tracks a collection of debuginfod artifacts on the local filesystem.
  92. class DebuginfodCollection {
  93. SmallVector<std::string, 1> Paths;
  94. sys::RWMutex BinariesMutex;
  95. StringMap<std::string> Binaries;
  96. sys::RWMutex DebugBinariesMutex;
  97. StringMap<std::string> DebugBinaries;
  98. Error findBinaries(StringRef Path);
  99. Expected<std::optional<std::string>> getDebugBinaryPath(object::BuildIDRef);
  100. Expected<std::optional<std::string>> getBinaryPath(object::BuildIDRef);
  101. // If the collection has not been updated since MinInterval, call update() and
  102. // return true. Otherwise return false. If update returns an error, return the
  103. // error.
  104. Expected<bool> updateIfStale();
  105. DebuginfodLog &Log;
  106. ThreadPool &Pool;
  107. Timer UpdateTimer;
  108. sys::Mutex UpdateMutex;
  109. // Minimum update interval, in seconds, for on-demand updates triggered when a
  110. // build-id is not found.
  111. double MinInterval;
  112. public:
  113. DebuginfodCollection(ArrayRef<StringRef> Paths, DebuginfodLog &Log,
  114. ThreadPool &Pool, double MinInterval);
  115. Error update();
  116. Error updateForever(std::chrono::milliseconds Interval);
  117. Expected<std::string> findDebugBinaryPath(object::BuildIDRef);
  118. Expected<std::string> findBinaryPath(object::BuildIDRef);
  119. };
  120. struct DebuginfodServer {
  121. HTTPServer Server;
  122. DebuginfodLog &Log;
  123. DebuginfodCollection &Collection;
  124. DebuginfodServer(DebuginfodLog &Log, DebuginfodCollection &Collection);
  125. };
  126. } // end namespace llvm
  127. #endif
  128. #ifdef __GNUC__
  129. #pragma GCC diagnostic pop
  130. #endif