Debian Codegraph @

src/libsysprof/sysprof-elf.c

raw
1 /* sysprof-elf.c
2 *
3 * Copyright 2023 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 "elfparser.h"
24
25 #include "sysprof-elf-private.h"
26
27 struct _SysprofElf
28 {
29 GObject parent_instance;
30 const char *nick;
31 char *build_id;
32 char *file;
33 SysprofElf *debug_link_elf;
34 ElfParser *parser;
35 guint64 file_inode;
36 gulong text_offset;
37 };
38
39 enum {
40 PROP_0,
41 PROP_BUILD_ID,
42 PROP_DEBUG_LINK,
43 PROP_DEBUG_LINK_ELF,
44 PROP_FILE,
45 N_PROPS
46 };
47
48 G_DEFINE_FINAL_TYPE (SysprofElf, sysprof_elf, G_TYPE_OBJECT)
49
50 static GParamSpec *properties [N_PROPS];
51 static GHashTable *nicks;
52 static const struct {
53 const char *library;
54 const char *nick;
55 } nick_table[] = {
56 /* Self Tracking Sysprof */
57 { "libsysprof-memory-6.so", "Sysprof" },
58 { "libsysprof-tracer-6.so", "Sysprof" },
59 { "libsysprof-6.so", "Sysprof" },
60
61 /* Platform */
62 { "libc.so", "libc" },
63 { "libffi.so", "libffi" },
64 { "ld-linux-x86-64.so", "glibc" },
65 { "libnss_sss.so", "NSS" },
66 { "libsss_debug.so", "SSSD" },
67 { "libsss_util.so", "SSSD" },
68 { "libnss_systemd.so", "NSS" },
69 { "libpcre2-8.so", "PCRE" },
70 { "libselinux.so", "SELinux" },
71 { "libssl3.so", "NSS" },
72 { "libstdc++.so", "libc" },
73 { "libsystemd.so", "systemd" },
74 { "libudev.so", "udev" },
75 { "libxul.so", "XUL" },
76 { "libz.so", "Zlib" },
77 { "libzstd.so", "Zstd" },
78
79 /* GObject */
80 { "libglib-2.0.so", "GLib" },
81 { "libgobject-2.0.so", "GObject" },
82 { "libgio-2.0.so", "Gio" },
83 { "libgirepository-1.0.so", "Introspection" },
84
85 /* Cairo/Pixman */
86 { "libcairo-gobject.so", "Cairo" },
87 { "libcairo.so", "Cairo" },
88 { "libpixman-1.so", "Pixman" },
89
90 /* Javascript */
91 { "libgjs.so", "JS" },
92 { "libmozjs-102.so", "JS" },
93 { "libmozjs-115.so", "JS" },
94 { "libjavascriptcoregtk-4.0.so", "JS" },
95 { "libjavascriptcoregtk-4.1.so", "JS" },
96 { "libjavascriptcoregtk-6.0.so", "JS" },
97
98 /* WebKit */
99 { "libwebkit2gtk-4.0.so", "WebKit" },
100 { "libwebkit2gtk-4.1.so", "WebKit" },
101 { "libwebkitgtk-6.0.so", "WebKit" },
102
103 /* Various GNOME Platform Libraries */
104 { "libdex-1.so", "Dex" },
105 { "libgstreamer-1-0.so", "GStreamer" },
106 { "libgudev-1.0.so", "udev" },
107 { "libibus-1.0.so", "IBus" },
108 { "libjson-glib-1.0.so", "JSON-GLib" },
109 { "libjsonrpc-glib-1.0.so", "JSONRPC-GLib" },
110 { "libpolkit-agent-1.so", "PolicyKit" },
111 { "libpolkit-gobject-1.so", "PolicyKit" },
112 { "libvte-2.91-gtk4.so", "VTE" },
113 { "libvte-2.91.so", "VTE" },
114
115 /* Pango and Harfbuzz */
116 { "libfribidi.so", "Fribidi" },
117 { "libpango-1.0.so", "Pango" },
118 { "libpangocairo-1.0.so", "Pango" },
119 { "libpangoft2-1.0.so", "Pango" },
120 { "libharfbuzz-cairo.so", "Harfbuzz" },
121 { "libharfbuzz-gobject.so", "Harfbuzz" },
122 { "libharfbuzz-icu.so", "Harfbuzz" },
123 { "libharfbuzz-subset.so", "Harfbuzz" },
124 { "libharfbuzz.so", "Harfbuzz" },
125 { "libfontconfig.so", "FontConfig" },
126
127 /* GTK */
128 { "libgtk-3.so", "GTK 3" },
129 { "libgdk-3.so", "GTK 3" },
130 { "libgtk-4.so", "GTK 4" },
131 { "libadwaita-1.so", "Adwaita" },
132 { "libgraphene-1.0.so", "Graphene" },
133 { "libgdk_pixbuf-2.0.so", "GdkPixbuf" },
134 { "librsvg-2.so", "rsvg" },
135
136 /* Xorg/X11 */
137 { "libX11-xcb.so", "X11" },
138 { "libX11.so", "X11" },
139 { "libxcb.so", "X11" },
140 { "libxkbcommon.so", "XKB" },
141
142 /* Wayland */
143 { "libwayland-client.so", "Wayland Client" },
144 { "libwayland-cursor.so", "Wayland Cursor" },
145 { "libwayland-egl.so", "Wayland EGL" },
146 { "libwayland-server.so", "Wayland Server" },
147
148 /* Mutter/Clutter/Shell */
149 { "libclutter-1.0.so", "Clutter" },
150 { "libclutter-glx-1.0.so", "Clutter" },
151 { "libinput.so", "libinput" },
152 { "libmutter-12.so", "Mutter" },
153 { "libmutter-13.so", "Mutter" },
154 { "libmutter-cogl-12.so", "Mutter" },
155 { "libmutter-cogl-13.so", "Mutter" },
156 { "libmutter-clutter-12.so", "Mutter" },
157 { "libmutter-clutter-13.so", "Mutter" },
158 { "libst-12.so", "GNOME Shell" },
159 { "libst-13.so", "GNOME Shell" },
160
161 /* GtkSourceView */
162 { "libgtksourceview-3.0.so", "GtkSourceView" },
163 { "libgtksourceview-4.so", "GtkSourceView" },
164 { "libgtksourceview-5.so", "GtkSourceView" },
165
166 /* Pipewire and Legacy Audio modules */
167 { "libasound.so", "ALSA" },
168 { "libpipewire-0.3.so", "Pipewire" },
169 { "libpipewire-module-client-node.so", "Pipewire" },
170 { "libpipewire-module-protocol-pulse.so", "Pipewire" },
171 { "libpulse.so", "PulseAudio" },
172 { "libpulsecommon-16.1.so", "PulseAudio" },
173 { "libspa-alsa.so", "Pipewire" },
174 { "libspa-audioconvert.so", "Pipewire" },
175 { "libspa-support.so", "Pipewire" },
176
177 /* OpenGL base libraries */
178 { "libEGL.so", "EGL" },
179 { "libGL.so", "GL" },
180
181 /* Mesa and DRI Drivers */
182 { "asahi_dri.so", "Mesa" },
183 { "apple_dri.so", "Mesa" },
184 { "crocus_dri.so", "Mesa" },
185 { "i915_dri.so", "Mesa" },
186 { "i965_drv_video.so", "Mesa" },
187 { "iHD_drv_video.so", "Mesa" },
188 { "iris_dri.so", "Mesa" },
189 { "kms_swrast_dri.so", "Mesa" },
190 { "libEGL_mesa.so", "Mesa" },
191 { "libGLX_mesa.so", "Mesa" },
192 { "libdrm.so", "Mesa" },
193 { "nouveau_dri.so", "Mesa" },
194 { "r300_dri.so", "Mesa" },
195 { "r600_dri.so", "Mesa" },
196 { "radeonsi_dri.so", "Mesa" },
197 { "swrast_dri.so", "Mesa" },
198 { "virtio_gpu_dri.so", "Mesa" },
199 { "vmwgfx_dri.so", "Mesa" },
200 { "zink_dri.so", "Mesa" },
201
202 /* Various cryptography libraries */
203 { "libcrypto.so", "Cryptography" },
204 { "libssl.so", "Cryptography" },
205 { "libssl3.so", "Cryptography" },
206 { "libgnutls.so", "Cryptography" },
207 { "libgnutlsxx.so", "Cryptography" },
208 { "libgnutls-dane.so", "Cryptography" },
209
210 /* App Store */
211 { "libgnomesoftware.so", "App Store" },
212 { "libappstream.so", "App Store" },
213 { "libappstream-glib.so", "App Store" },
214 };
215
216 static void
217 sysprof_elf_finalize (GObject *object)
218 {
219 SysprofElf *self = (SysprofElf *)object;
220
221 g_clear_pointer (&self->file, g_free);
222 g_clear_pointer (&self->parser, elf_parser_free);
223 g_clear_object (&self->debug_link_elf);
224
225 G_OBJECT_CLASS (sysprof_elf_parent_class)->finalize (object);
226 }
227
228 static void
229 sysprof_elf_get_property (GObject *object,
230 guint prop_id,
231 GValue *value,
232 GParamSpec *pspec)
233 {
234 SysprofElf *self = SYSPROF_ELF (object);
235
236 switch (prop_id)
237 {
238 case PROP_BUILD_ID:
239 g_value_set_string (value, sysprof_elf_get_build_id (self));
240 break;
241
242 case PROP_DEBUG_LINK:
243 g_value_set_string (value, sysprof_elf_get_debug_link (self));
244 break;
245
246 case PROP_DEBUG_LINK_ELF:
247 g_value_set_object (value, sysprof_elf_get_debug_link_elf (self));
248 break;
249
250 case PROP_FILE:
251 g_value_set_string (value, sysprof_elf_get_file (self));
252 break;
253
254 default:
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 }
257 }
258
259 static void
260 sysprof_elf_set_property (GObject *object,
261 guint prop_id,
262 const GValue *value,
263 GParamSpec *pspec)
264 {
265 SysprofElf *self = SYSPROF_ELF (object);
266
267 switch (prop_id)
268 {
269 case PROP_DEBUG_LINK_ELF:
270 sysprof_elf_set_debug_link_elf (self, g_value_get_object (value));
271 break;
272
273 default:
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275 }
276 }
277
278 static void
279 sysprof_elf_class_init (SysprofElfClass *klass)
280 {
281 GObjectClass *object_class = G_OBJECT_CLASS (klass);
282
283 object_class->finalize = sysprof_elf_finalize;
284 object_class->get_property = sysprof_elf_get_property;
285 object_class->set_property = sysprof_elf_set_property;
286
287 properties [PROP_BUILD_ID] =
288 g_param_spec_string ("build-id", NULL, NULL,
289 NULL,
290 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
291
292 properties [PROP_DEBUG_LINK] =
293 g_param_spec_string ("debug-link", NULL, NULL,
294 NULL,
295 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
296
297 properties [PROP_DEBUG_LINK_ELF] =
298 g_param_spec_object ("debug-link-elf", NULL, NULL,
299 SYSPROF_TYPE_ELF,
300 (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
301
302 properties [PROP_FILE] =
303 g_param_spec_string ("file", NULL, NULL,
304 NULL,
305 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
306
307 g_object_class_install_properties (object_class, N_PROPS, properties);
308
309 nicks = g_hash_table_new (g_str_hash, g_str_equal);
310 for (guint i = 0; i < G_N_ELEMENTS (nick_table); i++)
311 {
312 g_assert (g_str_has_suffix (nick_table[i].library, ".so"));
313 g_hash_table_insert (nicks,
314 (char *)nick_table[i].library,
315 (char *)nick_table[i].nick);
316 }
317 }
318
319 static void
320 sysprof_elf_init (SysprofElf *self)
321 {
322 }
323
324 static void
325 guess_nick (SysprofElf *self,
326 const char *name,
327 const char *endptr)
328 {
329 char key[32];
330
331 if (endptr <= name)
332 return;
333
334 if (endptr - name >= sizeof key)
335 return;
336
337 memcpy (key, name, endptr-name);
338 key[endptr-name] = 0;
339
340 self->nick = g_hash_table_lookup (nicks, key);
341 }
342
343 SysprofElf *
344 sysprof_elf_new (const char *filename,
345 GMappedFile *mapped_file,
346 guint64 file_inode,
347 GError **error)
348 {
349 SysprofElf *self;
350 ElfParser *parser;
351
352 g_return_val_if_fail (mapped_file != NULL, NULL);
353
354 if (!(parser = elf_parser_new_from_mmap (g_steal_pointer (&mapped_file), error)))
355 return NULL;
356
357 self = g_object_new (SYSPROF_TYPE_ELF, NULL);
358 self->file = g_strdup (filename);
359 self->parser = g_steal_pointer (&parser);
360 self->file_inode = file_inode;
361 self->text_offset = elf_parser_get_text_offset (self->parser);
362
363 if (filename != NULL)
364 {
365 const char *base;
366 const char *endptr;
367
368 if ((base = strrchr (filename, '/')))
369 {
370 endptr = strstr (++base, ".so");
371
372 if (endptr != NULL && (endptr[3] == 0 || endptr[3] == '.'))
373 guess_nick (self, base, &endptr[3]);
374 }
375 }
376
377 return self;
378 }
379
380 const char *
381 sysprof_elf_get_file (SysprofElf *self)
382 {
383 g_return_val_if_fail (SYSPROF_IS_ELF (self), NULL);
384
385 return self->file;
386 }
387
388 const char *
389 sysprof_elf_get_build_id (SysprofElf *self)
390 {
391 g_return_val_if_fail (SYSPROF_IS_ELF (self), NULL);
392
393 return elf_parser_get_build_id (self->parser);
394 }
395
396 const char *
397 sysprof_elf_get_debug_link (SysprofElf *self)
398 {
399 guint crc32;
400
401 g_return_val_if_fail (SYSPROF_IS_ELF (self), NULL);
402
403 return elf_parser_get_debug_link (self->parser, &crc32);
404 }
405
406 static char *
407 sysprof_elf_get_symbol_at_address_internal (SysprofElf *self,
408 const char *filename,
409 guint64 address,
410 guint64 *begin_address,
411 guint64 *end_address,
412 guint64 text_offset)
413 {
414 const ElfSym *symbol;
415 char *ret = NULL;
416 gulong begin = 0;
417 gulong end = 0;
418
419 g_return_val_if_fail (SYSPROF_IS_ELF (self), NULL);
420
421 if (self->debug_link_elf != NULL)
422 {
423 ret = sysprof_elf_get_symbol_at_address_internal (self->debug_link_elf, filename, address, begin_address, end_address, text_offset);
424
425 if (ret != NULL)
426 return ret;
427 }
428
429 if ((symbol = elf_parser_lookup_symbol (self->parser, address - text_offset)))
430 {
431 const char *name;
432
433 if (begin_address || end_address)
434 {
435 elf_parser_get_sym_address_range (self->parser, symbol, &begin, &end);
436 begin += text_offset;
437 end += text_offset;
438 }
439
440 name = elf_parser_get_sym_name (self->parser, symbol);
441
442 if (name != NULL && name[0] == '_' && ((name[1] == 'Z') || (name[1] == 'R')))
443 ret = elf_demangle (name);
444 else
445 ret = g_strdup (name);
446 }
447 else
448 {
449 return NULL;
450 }
451
452 if (begin_address)
453 *begin_address = begin;
454
455 if (end_address)
456 *end_address = end;
457
458 return ret;
459 }
460
461 char *
462 sysprof_elf_get_symbol_at_address (SysprofElf *self,
463 guint64 address,
464 guint64 *begin_address,
465 guint64 *end_address)
466 {
467 return sysprof_elf_get_symbol_at_address_internal (self,
468 self->file,
469 address,
470 begin_address,
471 end_address,
472 self->text_offset);
473 }
474
475 /**
476 * sysprof_elf_get_debug_link_elf:
477 * @self: a #SysprofElf
478 *
479 * Gets a #SysprofElf that was resolved from the `.gnu_debuglink`
480 * ELF section header.
481 *
482 * Returns: (transfer none) (nullable): a #SysprofElf or %NULL
483 */
484 SysprofElf *
485 sysprof_elf_get_debug_link_elf (SysprofElf *self)
486 {
487 g_return_val_if_fail (SYSPROF_IS_ELF (self), NULL);
488
489 return self->debug_link_elf;
490 }
491
492 void
493 sysprof_elf_set_debug_link_elf (SysprofElf *self,
494 SysprofElf *debug_link_elf)
495 {
496 g_return_if_fail (SYSPROF_IS_ELF (self));
497 g_return_if_fail (!debug_link_elf || SYSPROF_IS_ELF (debug_link_elf));
498 g_return_if_fail (debug_link_elf != self);
499
500 if (g_set_object (&self->debug_link_elf, debug_link_elf))
501 g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_DEBUG_LINK_ELF]);
502 }
503
504 gboolean
505 sysprof_elf_matches (SysprofElf *self,
506 guint64 file_inode,
507 const char *build_id)
508 {
509 g_return_val_if_fail (SYSPROF_IS_ELF (self), FALSE);
510
511 if (build_id != NULL)
512 {
513 const char *elf_build_id = elf_parser_get_build_id (self->parser);
514
515 /* Not matching build-id, you definitely don't want this ELF */
516 if (elf_build_id != NULL && !g_str_equal (build_id, elf_build_id))
517 return FALSE;
518 }
519
520 if (file_inode && self->file_inode && file_inode != self->file_inode)
521 return FALSE;
522
523 return TRUE;
524 }
525
526 const char *
527 sysprof_elf_get_nick (SysprofElf *self)
528 {
529 g_return_val_if_fail (SYSPROF_IS_ELF (self), NULL);
530
531 return self->nick;
532 }