Skip to content

Commit c769e42

Browse files
Fix object path absolute paths (#75)
To support processes running in mount namespaces that aren't the root one we can access the files in their mount via procfs's root directory. Not only this was broken in terms of the paths being joined, but also the way `Path::join` works is that the joined path starts with a slash (so it's recognised as absolute) it will replace the whole path (!). This was reported upstream in rust-lang/rust#16507. Test Plan ========= Ran lightswitch for a little bit, and containerised workloads worked fine.
1 parent c989e3f commit c769e42

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/profiler.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,13 +1267,11 @@ impl Profiler<'_> {
12671267
}
12681268
match &map.pathname {
12691269
procfs::process::MMapPath::Path(path) => {
1270-
let mut abs_path = proc.exe()?;
1271-
abs_path.push("/root");
1272-
abs_path.push(path);
1270+
let abs_path = format!("/proc/{}/root/{}", pid, path.to_string_lossy());
12731271

12741272
// We've seen debug info executables that get deleted in Rust applications.
12751273
// There are probably other cases, but we'll handle them as we bump into them.
1276-
if abs_path.to_str().unwrap().contains("(deleted)") {
1274+
if abs_path.contains("(deleted)") {
12771275
continue;
12781276
}
12791277

@@ -1282,17 +1280,17 @@ impl Profiler<'_> {
12821280
let file = match fs::File::open(&abs_path) {
12831281
Ok(f) => f,
12841282
Err(e) => {
1285-
warn!("failed to open file {} due to {:?}", abs_path.display(), e);
1283+
warn!("failed to open file {} due to {:?}", abs_path, e);
12861284
// Rather than returning here, we prefer to be able to profile some
12871285
// parts of the binary
12881286
continue;
12891287
}
12901288
};
12911289

1292-
let object_file = match ObjectFile::new(&abs_path) {
1290+
let object_file = match ObjectFile::new(&PathBuf::from(abs_path.clone())) {
12931291
Ok(f) => f,
12941292
Err(e) => {
1295-
warn!("object_file {} failed with {:?}", abs_path.display(), e);
1293+
warn!("object_file {} failed with {:?}", abs_path, e);
12961294
// Rather than returning here, we prefer to be able to profile some
12971295
// parts of the binary
12981296
continue;
@@ -1311,7 +1309,7 @@ impl Profiler<'_> {
13111309
};
13121310

13131311
let Ok(executable_id) = object_file.id() else {
1314-
debug!("could not get id for object file: {:?}", abs_path);
1312+
debug!("could not get id for object file: {}", abs_path);
13151313
continue;
13161314
};
13171315

@@ -1343,7 +1341,7 @@ impl Profiler<'_> {
13431341
Entry::Vacant(entry) => match object_file.elf_load() {
13441342
Ok(elf_load) => {
13451343
entry.insert(ObjectFileInfo {
1346-
path: abs_path,
1344+
path: PathBuf::from(abs_path),
13471345
file,
13481346
load_offset: elf_load.offset,
13491347
load_vaddr: elf_load.vaddr,

0 commit comments

Comments
 (0)