Debian Codegraph @

src/libsysprof/sysprof-document-symbols.c

raw
1 /* sysprof-document-symbols.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 <glib/gi18n.h>
24
25 #include "sysprof-address-layout-private.h"
26 #include "sysprof-document-private.h"
27 #include "sysprof-document-symbols-private.h"
28 #include "sysprof-document-traceable.h"
29 #include "sysprof-mount-namespace-private.h"
30 #include "sysprof-no-symbolizer.h"
31 #include "sysprof-symbol-private.h"
32 #include "sysprof-symbolizer-private.h"
33
34 G_DEFINE_FINAL_TYPE (SysprofDocumentSymbols, sysprof_document_symbols, G_TYPE_OBJECT)
35
36 static void
37 sysprof_document_symbols_finalize (GObject *object)
38 {
39 SysprofDocumentSymbols *self = (SysprofDocumentSymbols *)object;
40
41 for (guint i = 0; i < G_N_ELEMENTS (self->context_switches); i++)
42 g_clear_object (&self->context_switches[i]);
43
44 g_clear_object (&self->kernel_symbols);
45
46 G_OBJECT_CLASS (sysprof_document_symbols_parent_class)->finalize (object);
47 }
48
49 static void
50 sysprof_document_symbols_class_init (SysprofDocumentSymbolsClass *klass)
51 {
52 GObjectClass *object_class = G_OBJECT_CLASS (klass);
53
54 object_class->finalize = sysprof_document_symbols_finalize;
55 }
56
57 static void
58 sysprof_document_symbols_init (SysprofDocumentSymbols *self)
59 {
60 self->kernel_symbols = sysprof_symbol_cache_new ();
61 }
62
63 typedef struct _Symbolize
64 {
65 SysprofDocument *document;
66 SysprofSymbolizer *symbolizer;
67 SysprofDocumentSymbols *symbols;
68 SysprofStrings *strings;
69 GHashTable *pid_to_process_info;
70 ProgressFunc progress_func;
71 gpointer progress_data;
72 GDestroyNotify progress_data_destroy;
73 } Symbolize;
74
75 static void
76 symbolize_free (Symbolize *state)
77 {
78 if (state->progress_data_destroy)
79 state->progress_data_destroy (state->progress_data);
80 g_clear_object (&state->document);
81 g_clear_object (&state->symbolizer);
82 g_clear_object (&state->symbols);
83 g_clear_pointer (&state->strings, sysprof_strings_unref);
84 g_clear_pointer (&state->pid_to_process_info, g_hash_table_unref);
85 g_free (state);
86 }
87
88 static SysprofSymbol *
89 do_symbolize (SysprofSymbolizer *symbolizer,
90 SysprofStrings *strings,
91 SysprofProcessInfo *process_info,
92 SysprofAddressContext last_context,
93 SysprofAddress address)
94 {
95 SysprofDocumentMmap *map;
96 g_autofree char *name = NULL;
97 SysprofSymbol *ret;
98 const char *nick = NULL;
99 const char *path;
100 guint64 map_begin;
101 guint64 map_end;
102 guint64 relative_address;
103 guint64 file_offset;
104
105 if ((ret = _sysprof_symbolizer_symbolize (symbolizer, strings, process_info, last_context, address)))
106 return ret;
107
108 /* Fallback, we failed to locate the symbol within a file we can
109 * access, so tell the user about what file contained the symbol
110 * and where (relative to that file) the IP was.
111 */
112
113 if (process_info == NULL || process_info->address_layout == NULL)
114 return NULL;
115
116 if (!(map = sysprof_address_layout_lookup (process_info->address_layout, address)))
117 return NULL;
118
119 map_begin = sysprof_document_mmap_get_start_address (map);
120 map_end = sysprof_document_mmap_get_end_address (map);
121
122 g_assert (address >= map_begin);
123 g_assert (address < map_end);
124
125 file_offset = sysprof_document_mmap_get_file_offset (map);
126
127 relative_address = address;
128 relative_address -= map_begin;
129 relative_address += file_offset;
130
131 path = sysprof_document_mmap_get_file (map);
132
133 name = g_strdup_printf ("In File %s+0x%"G_GINT64_MODIFIER"x",
134 sysprof_document_mmap_get_file (map),
135 relative_address);
136
137 ret = _sysprof_symbol_new (sysprof_strings_get (strings, name),
138 sysprof_strings_get (strings, path),
139 sysprof_strings_get (strings, nick),
140 address, address + 1,
141 SYSPROF_SYMBOL_KIND_USER);
142 ret->is_fallback = TRUE;
143
144 return ret;
145 }
146
147 static void
148 add_traceable (SysprofDocumentSymbols *self,
149 SysprofStrings *strings,
150 SysprofProcessInfo *process_info,
151 SysprofDocumentTraceable *traceable,
152 SysprofSymbolizer *symbolizer)
153 {
154 SysprofAddressContext last_context;
155 guint64 *addresses;
156 guint n_addresses;
157
158 g_assert (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable));
159 g_assert (SYSPROF_IS_SYMBOLIZER (symbolizer));
160
161 n_addresses = sysprof_document_traceable_get_stack_depth (traceable);
162 addresses = g_alloca (sizeof (guint64) * n_addresses);
163 sysprof_document_traceable_get_stack_addresses (traceable, addresses, n_addresses);
164
165 last_context = SYSPROF_ADDRESS_CONTEXT_NONE;
166
167 for (guint i = 0; i < n_addresses; i++)
168 {
169 SysprofAddress address = addresses[i];
170 SysprofAddressContext context = SYSPROF_ADDRESS_CONTEXT_NONE;
171
172 if (sysprof_address_is_context_switch (address, &context))
173 {
174 last_context = context;
175 continue;
176 }
177
178 if (last_context == SYSPROF_ADDRESS_CONTEXT_KERNEL)
179 {
180 g_autoptr(SysprofSymbol) symbol = NULL;
181
182 if (sysprof_symbol_cache_lookup (self->kernel_symbols, address) != NULL)
183 continue;
184
185 if ((symbol = do_symbolize (symbolizer, strings, process_info, last_context, address)))
186 sysprof_symbol_cache_take (self->kernel_symbols, g_steal_pointer (&symbol));
187 }
188 else
189 {
190 g_autoptr(SysprofSymbol) symbol = NULL;
191
192 if (process_info != NULL &&
193 sysprof_symbol_cache_lookup (process_info->symbol_cache, address) != NULL)
194 continue;
195
196 if ((symbol = do_symbolize (symbolizer, strings, process_info, last_context, address)))
197 sysprof_symbol_cache_take (process_info->symbol_cache, g_steal_pointer (&symbol));
198 }
199 }
200 }
201
202 static void
203 sysprof_document_symbols_worker (GTask *task,
204 gpointer source_object,
205 gpointer task_data,
206 GCancellable *cancellable)
207 {
208 static const struct {
209 const char *name;
210 guint value;
211 } context_switches[] = {
212 { "- - Hypervisor - -", SYSPROF_ADDRESS_CONTEXT_HYPERVISOR },
213 { "- - Kernel - -", SYSPROF_ADDRESS_CONTEXT_KERNEL },
214 { "- - User - -", SYSPROF_ADDRESS_CONTEXT_USER },
215 { "- - Guest - -", SYSPROF_ADDRESS_CONTEXT_GUEST },
216 { "- - Guest Kernel - -", SYSPROF_ADDRESS_CONTEXT_GUEST_KERNEL },
217 { "- - Guest User - -", SYSPROF_ADDRESS_CONTEXT_GUEST_USER },
218 };
219 g_autoptr(GRefString) context_switch = g_ref_string_new_intern ("Context Switch");
220 Symbolize *state = task_data;
221 EggBitsetIter iter;
222 EggBitset *bitset;
223 GListModel *model;
224 guint count = 0;
225 guint i;
226
227 g_assert (source_object == NULL);
228 g_assert (G_IS_TASK (task));
229 g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
230 g_assert (state != NULL);
231 g_assert (SYSPROF_IS_DOCUMENT (state->document));
232 g_assert (SYSPROF_IS_SYMBOLIZER (state->symbolizer));
233 g_assert (SYSPROF_IS_DOCUMENT_SYMBOLS (state->symbols));
234
235 bitset = _sysprof_document_traceables (state->document);
236 model = G_LIST_MODEL (state->document);
237
238 /* Create static symbols for context switch use */
239 for (guint cs = 0; cs < G_N_ELEMENTS (context_switches); cs++)
240 {
241 g_autoptr(SysprofSymbol) symbol = _sysprof_symbol_new (g_ref_string_new_intern (context_switches[cs].name),
242 NULL,
243 g_ref_string_acquire (context_switch),
244 0, 0,
245 SYSPROF_SYMBOL_KIND_CONTEXT_SWITCH);
246
247 /* TODO: It would be nice if we had enough insight from the capture header
248 * as to the host system, so we can show "vmlinuz" and "Linux" respectively
249 * for binary-path and binary-nick when the capture came from Linux.
250 */
251
252 state->symbols->context_switches[context_switches[cs].value] = g_steal_pointer (&symbol);
253 }
254
255 /* Walk through the available traceables which need symbols extracted */
256 if (!SYSPROF_IS_NO_SYMBOLIZER (state->symbolizer) &&
257 egg_bitset_iter_init_first (&iter, bitset, &i))
258 {
259 guint n_items = egg_bitset_get_size (bitset);
260
261 do
262 {
263 g_autoptr(SysprofDocumentTraceable) traceable = g_list_model_get_item (model, i);
264 int pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable));
265 SysprofProcessInfo *process_info = g_hash_table_lookup (state->pid_to_process_info, GINT_TO_POINTER (pid));
266
267 add_traceable (state->symbols,
268 state->strings,
269 process_info,
270 traceable,
271 state->symbolizer);
272
273 count++;
274
275 if (state->progress_func != NULL && count % 100 == 0)
276 state->progress_func (count / (double)n_items, _("Symbolizing stack traces"), state->progress_data);
277 }
278 while (egg_bitset_iter_next (&iter, &i));
279 }
280
281 g_task_return_pointer (task,
282 g_object_ref (state->symbols),
283 g_object_unref);
284 }
285
286 void
287 _sysprof_document_symbols_new (SysprofDocument *document,
288 SysprofStrings *strings,
289 SysprofSymbolizer *symbolizer,
290 GHashTable *pid_to_process_info,
291 ProgressFunc progress_func,
292 gpointer progress_data,
293 GDestroyNotify progress_data_destroy,
294 GCancellable *cancellable,
295 GAsyncReadyCallback callback,
296 gpointer user_data)
297 {
298 g_autoptr(GTask) task = NULL;
299 Symbolize *state;
300
301 g_return_if_fail (SYSPROF_IS_DOCUMENT (document));
302 g_return_if_fail (SYSPROF_IS_SYMBOLIZER (symbolizer));
303
304 state = g_new0 (Symbolize, 1);
305 state->document = g_object_ref (document);
306 state->symbolizer = g_object_ref (symbolizer);
307 state->symbols = g_object_new (SYSPROF_TYPE_DOCUMENT_SYMBOLS, NULL);
308 state->strings = sysprof_strings_ref (strings);
309 state->pid_to_process_info = g_hash_table_ref (pid_to_process_info);
310 state->progress_func = progress_func;
311 state->progress_data = progress_data;
312 state->progress_data_destroy = progress_data_destroy;
313
314 task = g_task_new (NULL, cancellable, callback, user_data);
315 g_task_set_source_tag (task, _sysprof_document_symbols_new);
316 g_task_set_task_data (task, state, (GDestroyNotify)symbolize_free);
317 g_task_run_in_thread (task, sysprof_document_symbols_worker);
318 }
319
320 SysprofDocumentSymbols *
321 _sysprof_document_symbols_new_finish (GAsyncResult *result,
322 GError **error)
323 {
324 g_return_val_if_fail (G_IS_TASK (result), NULL);
325 g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
326 g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == _sysprof_document_symbols_new, NULL);
327
328 return g_task_propagate_pointer (G_TASK (result), error);
329 }
330
331 /**
332 * _sysprof_document_symbols_lookup:
333 * @self: a #SysprofDocumentSymbols
334 * @process_info: (nullable): the process info if necessary
335 * @context: the #SysprofAddressContext for the address
336 * @address: a #SysprofAddress to lookup the symbol for
337 *
338 * Locates the symbol that is found at @address within @context of @pid.
339 *
340 * Returns: (transfer none) (nullable): a #SysprofSymbol or %NULL
341 */
342 SysprofSymbol *
343 _sysprof_document_symbols_lookup (SysprofDocumentSymbols *self,
344 const SysprofProcessInfo *process_info,
345 SysprofAddressContext context,
346 SysprofAddress address)
347 {
348 SysprofAddressContext new_context;
349
350 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_SYMBOLS (self), NULL);
351 g_return_val_if_fail (context <= SYSPROF_ADDRESS_CONTEXT_GUEST_USER, NULL);
352
353 if (context == SYSPROF_ADDRESS_CONTEXT_NONE)
354 context = SYSPROF_ADDRESS_CONTEXT_USER;
355
356 if (sysprof_address_is_context_switch (address, &new_context))
357 return self->context_switches[context];
358
359 if (context == SYSPROF_ADDRESS_CONTEXT_KERNEL)
360 return sysprof_symbol_cache_lookup (self->kernel_symbols, address);
361
362 if (process_info != NULL)
363 return sysprof_symbol_cache_lookup (process_info->symbol_cache, address);
364
365 return NULL;
366 }