Skip to content

Commit 2998b69

Browse files
committed
resolve links for build-id-constructed paths
1 parent 06e3289 commit 2998b69

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

context.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,27 @@ Context::getImageIfLoaded(const Container &ctr, const typename Container::key_ty
159159
* Find an image from a name, caching in container, and using "paths" as potential prefixes for name
160160
*/
161161
std::shared_ptr<Elf::Object>
162-
Context::getImageInPath(const std::vector<std::filesystem::path> &paths, NameMap &container, const std::filesystem::path &name, bool isDebug) {
162+
Context::getImageInPath(const std::vector<std::filesystem::path> &paths, NameMap &container, const std::filesystem::path &name, bool isDebug, bool resolveLink) {
163163
std::optional<Elf::Object::sptr> cached = getImageIfLoaded(container, name, isDebug);
164164
if (cached)
165165
return *cached;
166166

167167
Elf::Object::sptr res;
168168
for (const auto &dir : paths) {
169+
auto path = dir/name;
170+
if (resolveLink) {
171+
char buf[PATH_MAX];
172+
char *p = realpath( path.c_str(), buf );
173+
if (p) {
174+
path = p;
175+
} else if (errno == ENOENT) {
176+
return nullptr;
177+
} else {
178+
*debug << "failed to resolve " << path << ": " << strerror(errno) << "\n";
179+
}
180+
}
169181
try {
170-
res = std::make_shared<Elf::Object>(*this, std::make_shared<MmapReader>(*this,dir / name) , isDebug);
182+
res = std::make_shared<Elf::Object>(*this, std::make_shared<MmapReader>(*this, path) , isDebug);
171183
break;
172184
}
173185
catch (const std::exception &ex) {
@@ -189,7 +201,7 @@ Context::getImageInPath(const std::vector<std::filesystem::path> &paths, NameMap
189201
*/
190202
std::shared_ptr<Elf::Object>
191203
Context::getImage(const std::filesystem::path &name) {
192-
return getImageInPath(exePrefixes, imageByName, name, false);
204+
return getImageInPath(exePrefixes, imageByName, name, false, false);
193205
}
194206

195207
/*
@@ -224,7 +236,7 @@ std::shared_ptr<Elf::Object> Context::getImageImpl( const Elf::BuildID &bid, boo
224236

225237
std::filesystem::path bidpath = std::filesystem::path( bucket.str() ) / std::filesystem::path( rest.str() );
226238

227-
Elf::Object::sptr res = getImageInPath(paths, nameContainer, bidpath, isDebug);
239+
Elf::Object::sptr res = getImageInPath(paths, nameContainer, bidpath, isDebug, true);
228240
#ifdef DEBUGINFOD
229241
if (!res && debuginfod()) {
230242
char *path;
@@ -255,7 +267,7 @@ std::shared_ptr<Elf::Object> Context::getImageImpl( const Elf::BuildID &bid, boo
255267

256268
std::shared_ptr<Elf::Object>
257269
Context::getDebugImage(const std::filesystem::path &name) {
258-
return getImageInPath(debugPrefixes, debugImageByName, name, true);
270+
return getImageInPath(debugPrefixes, debugImageByName, name, true, false);
259271
}
260272

261273
#ifndef DEBUGINFOD

libpstack/context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Context {
5252
int elfHits;
5353
} counters {};
5454
template <typename Container> std::optional<std::shared_ptr<Elf::Object>> getImageIfLoaded(const Container &ctr, const typename Container::key_type &key, bool isDebug);
55-
std::shared_ptr<Elf::Object> getImageInPath(const std::vector<std::filesystem::path> &paths, NameMap &container, const std::filesystem::path &name, bool isDebug);
55+
std::shared_ptr<Elf::Object> getImageInPath(const std::vector<std::filesystem::path> &paths, NameMap &container, const std::filesystem::path &name, bool isDebug, bool resolveLinks);
5656
std::shared_ptr<Elf::Object> getImageImpl( const Elf::BuildID &bid, bool isDebug);
5757

5858
struct DidClose { void operator() ( struct debuginfod_client *client ); };

0 commit comments

Comments
 (0)