Skip to content

Commit 9b528ed

Browse files
GeorgeHuyuboGeorge Hu
andauthored
Debuginfod cache use index cache settings and include real file name (llvm#120814)
This PR include two changes: 1. Change debuginfod cache file name to include origin file name, the new file name would be something like: llvmcache-13267c5f5d2e3df472c133c8efa45fb3331ef1ea-liblzma.so.5.2.2.debuginfo.dwp So it will provide more information in image list instead of a plain llvmcache-123 2. Switch debuginfod cache to use lldb index cache settings. Currently we don't have proper settings for setting the cache path or the cache expiration time for debuginfod cache. We want to use the lldb index cache settings, as they make sense to be in the same place and have the same TTL. --------- Co-authored-by: George Hu <[email protected]>
1 parent 8af4d20 commit 9b528ed

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

lldb/source/Plugins/SymbolLocator/Debuginfod/SymbolLocatorDebuginfod.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "SymbolLocatorDebuginfod.h"
1010

11+
#include "lldb/Core/DataFileCache.h"
1112
#include "lldb/Core/PluginManager.h"
1213
#include "lldb/Interpreter/OptionValueString.h"
1314
#include "lldb/Utility/Args.h"
@@ -141,6 +142,24 @@ SymbolLocator *SymbolLocatorDebuginfod::CreateInstance() {
141142
return new SymbolLocatorDebuginfod();
142143
}
143144

145+
static llvm::StringRef getFileName(const ModuleSpec &module_spec,
146+
std::string url_path) {
147+
// Check if the URL path requests an executable file or a symbol file
148+
bool is_executable = url_path.find("debuginfo") == std::string::npos;
149+
if (is_executable)
150+
return module_spec.GetFileSpec().GetFilename().GetStringRef();
151+
llvm::StringRef symbol_file =
152+
module_spec.GetSymbolFileSpec().GetFilename().GetStringRef();
153+
// Remove llvmcache- prefix and hash, keep origin file name
154+
if (symbol_file.starts_with("llvmcache-")) {
155+
size_t pos = symbol_file.rfind('-');
156+
if (pos != llvm::StringRef::npos) {
157+
symbol_file = symbol_file.substr(pos + 1);
158+
}
159+
}
160+
return symbol_file;
161+
}
162+
144163
static std::optional<FileSpec>
145164
GetFileForModule(const ModuleSpec &module_spec,
146165
std::function<std::string(llvm::object::BuildID)> UrlBuilder) {
@@ -154,21 +173,28 @@ GetFileForModule(const ModuleSpec &module_spec,
154173
// Grab LLDB's Debuginfod overrides from the
155174
// plugin.symbol-locator.debuginfod.* settings.
156175
PluginProperties &plugin_props = GetGlobalPluginProperties();
157-
llvm::Expected<std::string> cache_path_or_err = plugin_props.GetCachePath();
158-
// A cache location is *required*.
159-
if (!cache_path_or_err)
160-
return {};
161-
std::string cache_path = *cache_path_or_err;
176+
// Grab the lldb index cache settings from the global module list properties.
177+
ModuleListProperties &properties =
178+
ModuleList::GetGlobalModuleListProperties();
179+
std::string cache_path = properties.GetLLDBIndexCachePath().GetPath();
180+
181+
llvm::CachePruningPolicy pruning_policy =
182+
DataFileCache::GetLLDBIndexCachePolicy();
183+
162184
llvm::SmallVector<llvm::StringRef> debuginfod_urls =
163185
llvm::getDefaultDebuginfodUrls();
164186
std::chrono::milliseconds timeout = plugin_props.GetTimeout();
165187

166188
// We're ready to ask the Debuginfod library to find our file.
167189
llvm::object::BuildID build_id(module_uuid.GetBytes());
168190
std::string url_path = UrlBuilder(build_id);
169-
std::string cache_key = llvm::getDebuginfodCacheKey(url_path);
191+
llvm::StringRef file_name = getFileName(module_spec, url_path);
192+
std::string cache_file_name = llvm::toHex(build_id, true);
193+
if (!file_name.empty())
194+
cache_file_name += "-" + file_name.str();
170195
llvm::Expected<std::string> result = llvm::getCachedOrDownloadArtifact(
171-
cache_key, url_path, cache_path, debuginfod_urls, timeout);
196+
cache_file_name, url_path, cache_path, debuginfod_urls, timeout,
197+
pruning_policy);
172198
if (result)
173199
return FileSpec(*result);
174200

llvm/include/llvm/Debuginfod/Debuginfod.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/StringMap.h"
2626
#include "llvm/ADT/StringRef.h"
2727
#include "llvm/Object/BuildID.h"
28+
#include "llvm/Support/CachePruning.h"
2829
#include "llvm/Support/Error.h"
2930
#include "llvm/Support/MemoryBuffer.h"
3031
#include "llvm/Support/Mutex.h"
@@ -95,7 +96,8 @@ Expected<std::string> getCachedOrDownloadArtifact(StringRef UniqueKey,
9596
/// found, uses the UniqueKey for the local cache file.
9697
Expected<std::string> getCachedOrDownloadArtifact(
9798
StringRef UniqueKey, StringRef UrlPath, StringRef CacheDirectoryPath,
98-
ArrayRef<StringRef> DebuginfodUrls, std::chrono::milliseconds Timeout);
99+
ArrayRef<StringRef> DebuginfodUrls, std::chrono::milliseconds Timeout,
100+
llvm::CachePruningPolicy policy);
99101

100102
class ThreadPoolInterface;
101103

llvm/lib/Debuginfod/Debuginfod.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ Expected<std::string> getDefaultDebuginfodCacheDirectory() {
106106
return std::string(CacheDirectory);
107107
}
108108

109+
Expected<llvm::CachePruningPolicy> getDefaultDebuginfodCachePruningPolicy() {
110+
Expected<CachePruningPolicy> PruningPolicyOrErr =
111+
parseCachePruningPolicy(std::getenv("DEBUGINFOD_CACHE_POLICY"));
112+
if (!PruningPolicyOrErr)
113+
return PruningPolicyOrErr.takeError();
114+
return *PruningPolicyOrErr;
115+
}
116+
109117
std::chrono::milliseconds getDefaultDebuginfodTimeout() {
110118
long Timeout;
111119
const char *DebuginfodTimeoutEnv = std::getenv("DEBUGINFOD_TIMEOUT");
@@ -169,9 +177,15 @@ Expected<std::string> getCachedOrDownloadArtifact(StringRef UniqueKey,
169177
return CacheDirOrErr.takeError();
170178
CacheDir = *CacheDirOrErr;
171179

172-
return getCachedOrDownloadArtifact(UniqueKey, UrlPath, CacheDir,
173-
getDefaultDebuginfodUrls(),
174-
getDefaultDebuginfodTimeout());
180+
Expected<llvm::CachePruningPolicy> PruningPolicyOrErr =
181+
getDefaultDebuginfodCachePruningPolicy();
182+
if (!PruningPolicyOrErr)
183+
return PruningPolicyOrErr.takeError();
184+
llvm::CachePruningPolicy PruningPolicy = *PruningPolicyOrErr;
185+
186+
return getCachedOrDownloadArtifact(
187+
UniqueKey, UrlPath, CacheDir, getDefaultDebuginfodUrls(),
188+
getDefaultDebuginfodTimeout(), PruningPolicy);
175189
}
176190

177191
namespace {
@@ -250,7 +264,8 @@ static SmallVector<std::string, 0> getHeaders() {
250264

251265
Expected<std::string> getCachedOrDownloadArtifact(
252266
StringRef UniqueKey, StringRef UrlPath, StringRef CacheDirectoryPath,
253-
ArrayRef<StringRef> DebuginfodUrls, std::chrono::milliseconds Timeout) {
267+
ArrayRef<StringRef> DebuginfodUrls, std::chrono::milliseconds Timeout,
268+
llvm::CachePruningPolicy policy) {
254269
SmallString<64> AbsCachedArtifactPath;
255270
sys::path::append(AbsCachedArtifactPath, CacheDirectoryPath,
256271
"llvmcache-" + UniqueKey);
@@ -304,11 +319,7 @@ Expected<std::string> getCachedOrDownloadArtifact(
304319
continue;
305320
}
306321

307-
Expected<CachePruningPolicy> PruningPolicyOrErr =
308-
parseCachePruningPolicy(std::getenv("DEBUGINFOD_CACHE_POLICY"));
309-
if (!PruningPolicyOrErr)
310-
return PruningPolicyOrErr.takeError();
311-
pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
322+
pruneCache(CacheDirectoryPath, policy);
312323

313324
// Return the path to the artifact on disk.
314325
return std::string(AbsCachedArtifactPath);

llvm/unittests/Debuginfod/DebuginfodTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ TEST(DebuginfodClient, CacheHit) {
3737
sys::fs::createTemporaryFile("llvmcache-key", "temp", FD, CachedFilePath);
3838
StringRef CacheDir = sys::path::parent_path(CachedFilePath);
3939
StringRef UniqueKey = sys::path::filename(CachedFilePath);
40+
llvm::CachePruningPolicy policy;
4041
EXPECT_TRUE(UniqueKey.consume_front("llvmcache-"));
4142
raw_fd_ostream OF(FD, true, /*unbuffered=*/true);
4243
OF << "contents\n";
4344
OF << CacheDir << "\n";
4445
OF.close();
4546
Expected<std::string> PathOrErr = getCachedOrDownloadArtifact(
4647
UniqueKey, /*UrlPath=*/"/null", CacheDir,
47-
/*DebuginfodUrls=*/{}, /*Timeout=*/std::chrono::milliseconds(1));
48+
/*DebuginfodUrls=*/{}, /*Timeout=*/std::chrono::milliseconds(1), policy);
4849
EXPECT_THAT_EXPECTED(PathOrErr, HasValue(CachedFilePath));
4950
}
5051

0 commit comments

Comments
 (0)