Debian Codegraph @

src/libsysprof/sysprof-debuginfod-task.c

raw
1 /*
2 * sysprof-debuginfod-task.c
3 *
4 * Copyright 2024 Christian Hergert <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GPL-3.0-or-later
20 */
21
22 #include "config.h"
23
24 #include <glib/gstdio.h>
25 #include <glib/gi18n.h>
26
27 #include <gio/gio.h>
28
29 #include "sysprof-debuginfod-task-private.h"
30
31 struct _SysprofDebuginfodTask
32 {
33 SysprofDocumentTask parent_instance;
34 };
35
36 G_DEFINE_FINAL_TYPE (SysprofDebuginfodTask, sysprof_debuginfod_task, SYSPROF_TYPE_DOCUMENT_TASK)
37
38 static void
39 sysprof_debuginfod_task_class_init (SysprofDebuginfodTaskClass *klass)
40 {
41 }
42
43 static void
44 sysprof_debuginfod_task_init (SysprofDebuginfodTask *self)
45 {
46 }
47
48 SysprofDebuginfodTask *
49 sysprof_debuginfod_task_new (void)
50 {
51 return g_object_new (SYSPROF_TYPE_DEBUGINFOD_TASK, NULL);
52 }
53
54 static int
55 sysprof_debuginfod_task_progress_cb (debuginfod_client *client,
56 long a,
57 long b)
58 {
59 SysprofDocumentTask *task = debuginfod_get_user_data (client);
60 double progress;
61
62 g_assert (client != NULL);
63 g_assert (SYSPROF_IS_DEBUGINFOD_TASK (task));
64
65 if (b > 0)
66 progress = (double)a / (double)b;
67 else
68 progress = 0;
69
70 _sysprof_document_task_set_progress (task, progress);
71
72 if (sysprof_document_task_is_cancelled (task))
73 return -1;
74
75 return 0;
76 }
77
78 SysprofElf *
79 sysprof_debuginfod_task_find_debuginfo (SysprofDebuginfodTask *self,
80 debuginfod_client *client,
81 const char *path,
82 const char *build_id_string,
83 GError **error)
84 {
85 g_autoptr(GMappedFile) mapped_file = NULL;
86 g_autoptr(SysprofElf) debuginfo_elf = NULL;
87 g_autofd int fd = -1;
88 char *debuginfo_path = NULL;
89
90 g_return_val_if_fail (SYSPROF_IS_DEBUGINFOD_TASK (self), NULL);
91 g_return_val_if_fail (client != NULL, NULL);
92 g_return_val_if_fail (build_id_string != NULL, NULL);
93
94 debuginfod_set_user_data (client, self);
95 debuginfod_set_progressfn (client, sysprof_debuginfod_task_progress_cb);
96
97 _sysprof_document_task_set_title (SYSPROF_DOCUMENT_TASK (self), _("Downloading Symbols…"));
98 _sysprof_document_task_take_message (SYSPROF_DOCUMENT_TASK (self), g_strdup (path));
99
100 fd = debuginfod_find_debuginfo (client,
101 (const unsigned char *)build_id_string, 0,
102 &debuginfo_path);
103
104 if (fd < 0)
105 {
106 if (error != NULL)
107 {
108 int errsv = errno;
109 g_set_error_literal (error,
110 G_IO_ERROR,
111 g_io_error_from_errno (errsv),
112 g_strerror (errsv));
113 }
114
115 goto failure;
116 }
117
118 if (!(mapped_file = g_mapped_file_new_from_fd (fd, FALSE, error)))
119 goto failure;
120
121 if (!(debuginfo_elf = sysprof_elf_new (debuginfo_path, g_steal_pointer (&mapped_file), 0, error)))
122 goto failure;
123
124 failure:
125 free (debuginfo_path);
126
127 debuginfod_set_user_data (client, NULL);
128 debuginfod_set_progressfn (client, NULL);
129
130 return g_steal_pointer (&debuginfo_elf);
131 }