Debian Codegraph @

src/libsysprof/tests/test-elf-loader.c

raw
1 /* test-elf-loader.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 <sysprof.h>
22
23 #include "sysprof-document-process-private.h"
24 #include "sysprof-elf-loader-private.h"
25
26 static const GOptionEntry entries[] = {
27 { 0 }
28 };
29
30 int
31 main (int argc,
32 char *argv[])
33 {
34 g_autoptr(GOptionContext) context = g_option_context_new ("- test loading of ELF files");
35 g_autoptr(GMainLoop) main_loop = g_main_loop_new (NULL, FALSE);
36 g_autoptr(SysprofDocumentLoader) loader = NULL;
37 g_autoptr(SysprofElfLoader) elf_loader = NULL;
38 g_autoptr(SysprofDocument) document = NULL;
39 g_autoptr(GListModel) processes = NULL;
40 g_autoptr(GError) error = NULL;
41 guint n_processes;
42
43 g_option_context_add_main_entries (context, entries, NULL);
44 if (!g_option_context_parse (context, &argc, &argv, &error))
45 g_error ("%s", error->message);
46
47 if (argc < 2)
48 g_error ("usage: %s CAPTURE_FILE", argv[0]);
49
50 loader = sysprof_document_loader_new (argv[1]);
51 sysprof_document_loader_set_symbolizer (loader, sysprof_no_symbolizer_get ());
52 if (!(document = sysprof_document_loader_load (loader, NULL, &error)))
53 g_error ("Failed to load document: %s", error->message);
54
55 processes = sysprof_document_list_processes (document);
56 n_processes = g_list_model_get_n_items (processes);
57
58 elf_loader = sysprof_elf_loader_new ();
59
60 for (guint i = 0; i < n_processes; i++)
61 {
62 g_autoptr(SysprofDocumentProcess) process = g_list_model_get_item (processes, i);
63 g_autoptr(GListModel) memory_maps = sysprof_document_process_list_memory_maps (process);
64 guint n_memory_maps = g_list_model_get_n_items (memory_maps);
65 SysprofProcessInfo *info = _sysprof_document_process_get_info (process);
66
67 for (guint j = 0; j < n_memory_maps; j++)
68 {
69 g_autoptr(SysprofDocumentMmap) memory_map = g_list_model_get_item (memory_maps, j);
70 const char *file = sysprof_document_mmap_get_file (memory_map);
71 const char *build_id = sysprof_document_mmap_get_build_id (memory_map);
72 guint64 file_inode = sysprof_document_mmap_get_file_inode (memory_map);
73 g_autoptr(SysprofElf) elf = sysprof_elf_loader_load (elf_loader, info->mount_namespace, file, build_id, file_inode, &error);
74
75 if (elf == NULL)
76 g_print ("%u: %s (build-id %s) (inode %"G_GINT64_FORMAT") => [unresolved]\n",
77 info->pid, file, build_id ? build_id : "none", file_inode);
78 else
79 g_print ("%u: %s => %s\n", info->pid, file, sysprof_elf_get_file (elf));
80
81 g_clear_error (&error);
82 }
83 }
84
85 return 0;
86 }