libsysprof: truncate common prefix using debug dir

If we have a path like /app/bin/gnome-builder and the debug prefix is
/app/lib/debug then we don't want to end up with /app/lib/debug/app/bin
as the real data directory is /app/lib/debug/bin.

This often works with /usr because /usr/lib/debug/usr can link back to it's
parent. But we should try to do the right thing instead of relying on that
anyway.
This commit is contained in:
Christian Hergert
2023-08-29 13:41:49 -07:00
parent 6ed1317012
commit 2dc6b1b7a5

View File

@ -289,6 +289,31 @@ try_load_build_id (SysprofElfLoader *self,
return FALSE;
}
static const char *
skip_common_prefix (const char *path,
const char *other)
{
const char *committed = path;
/* If @path is "/app/bin/foo" and @other is "/app/lib/debug", we want
* to return "bin/foo", otherwise "/app/bin/foo".
*/
while (*path && *other)
{
if (*path != *other)
break;
if (*path == '/')
committed = path;
path++;
other++;
}
return committed;
}
static void
sysprof_elf_loader_annotate (SysprofElfLoader *self,
SysprofMountNamespace *mount_namespace,
@ -309,11 +334,13 @@ sysprof_elf_loader_annotate (SysprofElfLoader *self,
g_autofree char *directory_name = NULL;
g_autofree char *debug_path = NULL;
g_autofree char *container_path = NULL;
const char *short_directory_name;
const char *debug_dir = self->debug_dirs[i];
const char *build_id;
directory_name = g_path_get_dirname (orig_file);
debug_path = g_build_filename (debug_dir, directory_name, debug_link, NULL);
short_directory_name = skip_common_prefix (directory_name, debug_dir);
debug_path = g_build_filename (debug_dir, short_directory_name, debug_link, NULL);
build_id = sysprof_elf_get_build_id (elf);
if (try_load_build_id (self, mount_namespace, elf, build_id, debug_dir))