| 1 |
/* sysprof-debuginfod-symbolizer.c |
| 2 |
* |
| 3 |
* Copyright 2024 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
|
| 25 |
#include <glib/gstdio.h> |
| 26 |
|
| 27 |
#include "sysprof-symbolizer-private.h" |
| 28 |
#include "sysprof-debuginfod-symbolizer.h" |
| 29 |
#include "sysprof-elf-loader-private.h" |
| 30 |
#include "sysprof-symbol-private.h" |
| 31 |
|
| 32 |
#if HAVE_DEBUGINFOD |
| 33 |
# include <elfutils/debuginfod.h> |
| 34 |
# include "sysprof-debuginfod-task-private.h" |
| 35 |
#else |
| 36 |
typedef struct _debuginfod_client debuginfod_client; |
| 37 |
#endif |
| 38 |
|
| 39 |
struct _SysprofDebuginfodSymbolizer |
| 40 |
{ |
| 41 |
SysprofSymbolizer parent_instance; |
| 42 |
|
| 43 |
GWeakRef loader_wr; |
| 44 |
|
| 45 |
debuginfod_client *client; |
| 46 |
SysprofElfLoader *loader; |
| 47 |
GHashTable *cache; |
| 48 |
GHashTable *failed; |
| 49 |
}; |
| 50 |
|
| 51 |
struct _SysprofDebuginfodSymbolizerClass |
| 52 |
{ |
| 53 |
SysprofSymbolizerClass parent_class; |
| 54 |
}; |
| 55 |
|
| 56 |
static gboolean |
| 57 |
sysprof_debuginfod_symbolizer_initable_init (GInitable *initable, |
| 58 |
GCancellable *cancellable, |
| 59 |
GError **error) |
| 60 |
{ |
| 61 |
#if HAVE_DEBUGINFOD |
| 62 |
SysprofDebuginfodSymbolizer *self = SYSPROF_DEBUGINFOD_SYMBOLIZER (initable); |
| 63 |
debuginfod_client *client; |
| 64 |
|
| 65 |
/* Don't even allow creating a SysprofDebuginfodSymbolizer instance unless we |
| 66 |
* can create a new debuginfod_client. This ensures that even if an application |
| 67 |
* does `g_initable_new(SYSPROF_TYPE_DEBUGINFOD_SYMBOLIZER, NULL, error, NULL)` |
| 68 |
* they will get `NULL` back instead of a misconfigured instance. |
| 69 |
*/ |
| 70 |
if (!(client = debuginfod_begin ())) |
| 71 |
{ |
| 72 |
g_set_error_literal (error, |
| 73 |
G_IO_ERROR, |
| 74 |
g_io_error_from_errno (ENOTSUP), |
| 75 |
g_strerror (ENOTSUP)); |
| 76 |
return FALSE; |
| 77 |
} |
| 78 |
|
| 79 |
self->client = client; |
| 80 |
|
| 81 |
return TRUE; |
| 82 |
#else |
| 83 |
g_set_error_literal (error, |
| 84 |
G_IO_ERROR, |
| 85 |
g_io_error_from_errno (ENOTSUP), |
| 86 |
g_strerror (ENOTSUP)); |
| 87 |
return FALSE; |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
initable_init (GInitableIface *iface) |
| 93 |
{ |
| 94 |
iface->init = sysprof_debuginfod_symbolizer_initable_init; |
| 95 |
} |
| 96 |
|
| 97 |
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDebuginfodSymbolizer, sysprof_debuginfod_symbolizer, SYSPROF_TYPE_SYMBOLIZER, |
| 98 |
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_init)) |
| 99 |
|
| 100 |
static SysprofSymbol * |
| 101 |
sysprof_debuginfod_symbolizer_symbolize (SysprofSymbolizer *symbolizer, |
| 102 |
SysprofStrings *strings, |
| 103 |
const SysprofProcessInfo *process_info, |
| 104 |
SysprofAddressContext context, |
| 105 |
SysprofAddress address) |
| 106 |
{ |
| 107 |
#if HAVE_DEBUGINFOD |
| 108 |
SysprofDebuginfodSymbolizer *self = SYSPROF_DEBUGINFOD_SYMBOLIZER (symbolizer); |
| 109 |
g_autoptr(SysprofElf) elf = NULL; |
| 110 |
g_autofree char *name = NULL; |
| 111 |
SysprofSymbol *sym = NULL; |
| 112 |
SysprofDocumentMmap *map; |
| 113 |
const char *build_id; |
| 114 |
const char *path; |
| 115 |
guint64 relative_address; |
| 116 |
guint64 begin_address; |
| 117 |
guint64 end_address; |
| 118 |
guint64 file_offset; |
| 119 |
guint64 map_begin; |
| 120 |
guint64 map_end; |
| 121 |
|
| 122 |
if (process_info == NULL || |
| 123 |
process_info->address_layout == NULL || |
| 124 |
process_info->mount_namespace == NULL || |
| 125 |
(context != SYSPROF_ADDRESS_CONTEXT_NONE && context != SYSPROF_ADDRESS_CONTEXT_USER) || |
| 126 |
!(map = sysprof_address_layout_lookup (process_info->address_layout, address))) |
| 127 |
return NULL; |
| 128 |
|
| 129 |
map_begin = sysprof_document_mmap_get_start_address (map); |
| 130 |
map_end = sysprof_document_mmap_get_end_address (map); |
| 131 |
|
| 132 |
g_assert (address < map_end); |
| 133 |
g_assert (address >= map_begin); |
| 134 |
|
| 135 |
file_offset = sysprof_document_mmap_get_file_offset (map); |
| 136 |
path = sysprof_document_mmap_get_file (map); |
| 137 |
|
| 138 |
if (g_hash_table_contains (self->failed, path)) |
| 139 |
return NULL; |
| 140 |
|
| 141 |
elf = sysprof_elf_loader_load (self->loader, |
| 142 |
process_info->mount_namespace, |
| 143 |
path, |
| 144 |
sysprof_document_mmap_get_build_id (map), |
| 145 |
sysprof_document_mmap_get_file_inode (map), |
| 146 |
NULL); |
| 147 |
if (elf == NULL) |
| 148 |
return NULL; |
| 149 |
|
| 150 |
if (!(build_id = sysprof_elf_get_build_id (elf))) |
| 151 |
return NULL; |
| 152 |
|
| 153 |
if (!g_hash_table_contains (self->cache, elf)) |
| 154 |
{ |
| 155 |
g_autoptr(SysprofDebuginfodTask) task = sysprof_debuginfod_task_new (); |
| 156 |
g_autoptr(SysprofDocumentLoader) loader = g_weak_ref_get (&self->loader_wr); |
| 157 |
g_autoptr(SysprofDocumentTaskScope) scope = _sysprof_document_task_register (SYSPROF_DOCUMENT_TASK (task), loader); |
| 158 |
g_autoptr(SysprofElf) debuginfo_elf = NULL; |
| 159 |
|
| 160 |
if (!(debuginfo_elf = sysprof_debuginfod_task_find_debuginfo (task, self->client, path, build_id, NULL))) |
| 161 |
{ |
| 162 |
g_hash_table_insert (self->failed, g_strdup (path), NULL); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
sysprof_elf_set_debug_link_elf (elf, debuginfo_elf); |
| 167 |
|
| 168 |
g_hash_table_insert (self->cache, g_object_ref (elf), NULL); |
| 169 |
} |
| 170 |
|
| 171 |
relative_address = address; |
| 172 |
relative_address -= map_begin; |
| 173 |
relative_address += file_offset; |
| 174 |
|
| 175 |
name = sysprof_elf_get_symbol_at_address (elf, |
| 176 |
relative_address, |
| 177 |
&begin_address, |
| 178 |
&end_address); |
| 179 |
if (!name) |
| 180 |
return NULL; |
| 181 |
|
| 182 |
begin_address = CLAMP (begin_address, file_offset, file_offset + (map_end - map_begin)); |
| 183 |
end_address = CLAMP (end_address, file_offset, file_offset + (map_end - map_begin)); |
| 184 |
if (end_address == begin_address) |
| 185 |
end_address++; |
| 186 |
|
| 187 |
sym = _sysprof_symbol_new (sysprof_strings_get (strings, name), |
| 188 |
sysprof_strings_get (strings, path), |
| 189 |
sysprof_strings_get (strings, sysprof_elf_get_nick (elf)), |
| 190 |
map_begin + (begin_address - file_offset), |
| 191 |
map_begin + (end_address - file_offset), |
| 192 |
SYSPROF_SYMBOL_KIND_USER); |
| 193 |
|
| 194 |
return sym; |
| 195 |
#else |
| 196 |
return NULL; |
| 197 |
#endif |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
sysprof_debuginfod_symbolizer_setup (SysprofSymbolizer *symbolizer, |
| 202 |
SysprofDocumentLoader *loader) |
| 203 |
{ |
| 204 |
SysprofDebuginfodSymbolizer *self = SYSPROF_DEBUGINFOD_SYMBOLIZER (symbolizer); |
| 205 |
|
| 206 |
g_weak_ref_set (&self->loader_wr, loader); |
| 207 |
} |
| 208 |
|
| 209 |
static void |
| 210 |
sysprof_debuginfod_symbolizer_dispose (GObject *object) |
| 211 |
{ |
| 212 |
SysprofDebuginfodSymbolizer *self = SYSPROF_DEBUGINFOD_SYMBOLIZER (object); |
| 213 |
|
| 214 |
g_hash_table_remove_all (self->cache); |
| 215 |
|
| 216 |
g_weak_ref_set (&self->loader_wr, NULL); |
| 217 |
|
| 218 |
G_OBJECT_CLASS (sysprof_debuginfod_symbolizer_parent_class)->dispose (object); |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
sysprof_debuginfod_symbolizer_finalize (GObject *object) |
| 223 |
{ |
| 224 |
SysprofDebuginfodSymbolizer *self = SYSPROF_DEBUGINFOD_SYMBOLIZER (object); |
| 225 |
|
| 226 |
g_clear_object (&self->loader); |
| 227 |
|
| 228 |
g_clear_pointer (&self->cache, g_hash_table_unref); |
| 229 |
g_clear_pointer (&self->failed, g_hash_table_unref); |
| 230 |
|
| 231 |
#if HAVE_DEBUGINFOD |
| 232 |
g_clear_pointer (&self->client, debuginfod_end); |
| 233 |
#endif |
| 234 |
|
| 235 |
g_weak_ref_clear (&self->loader_wr); |
| 236 |
|
| 237 |
G_OBJECT_CLASS (sysprof_debuginfod_symbolizer_parent_class)->finalize (object); |
| 238 |
} |
| 239 |
|
| 240 |
static void |
| 241 |
sysprof_debuginfod_symbolizer_class_init (SysprofDebuginfodSymbolizerClass *klass) |
| 242 |
{ |
| 243 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 244 |
SysprofSymbolizerClass *symbolizer_class = SYSPROF_SYMBOLIZER_CLASS (klass); |
| 245 |
|
| 246 |
object_class->dispose = sysprof_debuginfod_symbolizer_dispose; |
| 247 |
object_class->finalize = sysprof_debuginfod_symbolizer_finalize; |
| 248 |
|
| 249 |
symbolizer_class->setup = sysprof_debuginfod_symbolizer_setup; |
| 250 |
symbolizer_class->symbolize = sysprof_debuginfod_symbolizer_symbolize; |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
sysprof_debuginfod_symbolizer_init (SysprofDebuginfodSymbolizer *self) |
| 255 |
{ |
| 256 |
g_weak_ref_init (&self->loader_wr, NULL); |
| 257 |
|
| 258 |
self->loader = sysprof_elf_loader_new (); |
| 259 |
self->cache = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL); |
| 260 |
self->failed = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 261 |
} |
| 262 |
|
| 263 |
SysprofSymbolizer * |
| 264 |
sysprof_debuginfod_symbolizer_new (GError **error) |
| 265 |
{ |
| 266 |
SysprofSymbolizer *self; |
| 267 |
|
| 268 |
if (!(self = g_initable_new (SYSPROF_TYPE_DEBUGINFOD_SYMBOLIZER, NULL, error, NULL))) |
| 269 |
return NULL; |
| 270 |
|
| 271 |
return self; |
| 272 |
} |