Debian Codegraph @

src/libsysprof/sysprof-kallsyms-symbolizer.c

raw
1 /* sysprof-kallsyms-symbolizer.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 <errno.h>
24
25 #include "timsort/gtktimsortprivate.h"
26
27 #include "rust-demangle.h"
28
29 #include "sysprof-kallsyms-symbolizer.h"
30 #include "sysprof-document-private.h"
31 #include "sysprof-strings-private.h"
32 #include "sysprof-symbolizer-private.h"
33 #include "sysprof-symbol-private.h"
34
35 #define LAST_SYMBOL_LEN 0xffff
36
37 typedef struct _KernelSymbol
38 {
39 guint64 address;
40 GRefString *name;
41 } KernelSymbol;
42
43 struct _SysprofKallsymsSymbolizer
44 {
45 SysprofSymbolizer parent_instance;
46 GInputStream *stream;
47 GArray *kallsyms;
48 guint64 low;
49 guint64 high;
50 };
51
52 struct _SysprofKallsymsSymbolizerClass
53 {
54 SysprofSymbolizerClass parent_class;
55 };
56
57 G_DEFINE_FINAL_TYPE (SysprofKallsymsSymbolizer, sysprof_kallsyms_symbolizer, SYSPROF_TYPE_SYMBOLIZER)
58
59 static SysprofStrings *kallsym_strings;
60 static GRefString *linux_string;
61
62 static void
63 kernel_symbol_clear (gpointer data)
64 {
65 KernelSymbol *symbol = data;
66
67 g_clear_pointer (&symbol->name, g_ref_string_release);
68 }
69
70 static inline void
71 sysprof_kallsyms_symbolizer_add (SysprofKallsymsSymbolizer *self,
72 guint64 address,
73 guint8 type,
74 const char *name)
75 {
76 const KernelSymbol s = { address, sysprof_strings_get (kallsym_strings, name) };
77 g_array_append_val (self->kallsyms, s);
78 }
79
80 static inline int
81 sort_by_address (gconstpointer a,
82 gconstpointer b)
83 {
84 const KernelSymbol *sym_a = a;
85 const KernelSymbol *sym_b = b;
86
87 if (sym_a->address < sym_b->address)
88 return -1;
89 else if (sym_a->address > sym_b->address)
90 return 1;
91 else
92 return 0;
93 }
94
95 static void
96 sysprof_kallsyms_symbolizer_prepare_worker (GTask *task,
97 gpointer source_object,
98 gpointer task_data,
99 GCancellable *cancellable)
100 {
101 SysprofKallsymsSymbolizer *self = source_object;
102 GDataInputStream *input = task_data;
103 g_autoptr(GError) error = NULL;
104 guint64 last_address = 0;
105 char *line;
106 gsize len;
107
108 g_assert (G_IS_TASK (task));
109 g_assert (SYSPROF_IS_KALLSYMS_SYMBOLIZER (self));
110 g_assert (G_IS_DATA_INPUT_STREAM (input));
111
112 while ((line = g_data_input_stream_read_line_utf8 (input, &len, cancellable, &error)))
113 {
114 const char *endptr = &line[len];
115 const char *name;
116 guint64 address;
117 char *iter = line;
118 guint8 type;
119
120 address = g_ascii_strtoull (iter, &iter, 16);
121
122 if G_UNLIKELY ((address == 0 && errno == EINVAL) ||
123 (address == G_MAXUINT64 && errno == ERANGE))
124 goto failure;
125
126 if G_UNLIKELY (iter[0] != ' ')
127 goto failure;
128
129 /* Swallow space */
130 iter++;
131 if (iter >= endptr)
132 goto failure;
133
134 /* Get type 'ABDRTVWabdrtw' */
135 type = iter[0];
136
137 /* Move past type and space */
138 iter++;
139 iter++;
140 if (iter >= endptr)
141 goto failure;
142
143 /* Name starts here */
144 name = iter;
145
146 /* Walk ahead to first space or \0 */
147 while (iter < endptr && !g_ascii_isspace (*iter))
148 iter++;
149 if (iter > endptr)
150 goto failure;
151
152 /* Make @name usable as C string */
153 *iter = 0;
154
155 /* Sometimes we get duplicates in kallsyms right after one another.
156 * Rather than try to deduplicate those all after they're in the
157 * array just detect the simple case and skip them now.
158 */
159 if (address != last_address)
160 {
161 g_autofree char *demangle = NULL;
162
163 /* If we got a Rust kernel symbol, demangle it */
164 if (name[0] == '_' && name[1] == 'R' && name[2] == 'N')
165 name = demangle = sysprof_rust_demangle (name, 0);
166
167 sysprof_kallsyms_symbolizer_add (self, address, type, name);
168 }
169
170 last_address = address;
171
172 failure:
173 g_free (line);
174 }
175
176 /* We cannot rely on sorting of kallsyms up-front from Linux in all
177 * cases so we must sort the resulting array now.
178 */
179 gtk_tim_sort (self->kallsyms->data,
180 self->kallsyms->len,
181 sizeof (KernelSymbol),
182 (GCompareDataFunc)sort_by_address,
183 NULL);
184
185 /* Store a "best guess" at an lower/upper bound for the max address so that
186 * we can avoid searching for anything unreasonably past the end of the last
187 * kernel symbol.
188 */
189 if (self->kallsyms->len > 0)
190 {
191 const KernelSymbol *head = &g_array_index (self->kallsyms, KernelSymbol, 0);
192 const KernelSymbol *tail = &g_array_index (self->kallsyms, KernelSymbol, self->kallsyms->len-1);
193
194 self->low = head->address;
195 self->high = tail->address + LAST_SYMBOL_LEN;
196 }
197
198 g_task_return_boolean (task, TRUE);
199 }
200
201 static void
202 sysprof_kallsyms_symbolizer_prepare_async (SysprofSymbolizer *symbolizer,
203 SysprofDocument *document,
204 GCancellable *cancellable,
205 GAsyncReadyCallback callback,
206 gpointer user_data)
207 {
208 SysprofKallsymsSymbolizer *self = (SysprofKallsymsSymbolizer *)symbolizer;
209 g_autoptr(SysprofDocumentFile) file = NULL;
210 g_autoptr(GInputStream) input = NULL;
211 g_autoptr(GTask) task = NULL;
212 GInputStream *base_stream;
213
214 g_assert (SYSPROF_IS_KALLSYMS_SYMBOLIZER (self));
215 g_assert (SYSPROF_IS_DOCUMENT (document));
216 g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
217
218 task = g_task_new (self, cancellable, callback, user_data);
219 g_task_set_source_tag (task, sysprof_kallsyms_symbolizer_prepare_async);
220
221 base_stream = self->stream;
222
223 if (base_stream == NULL)
224 {
225 if (!(file = sysprof_document_lookup_file (document, "/proc/kallsyms")))
226 {
227 g_task_return_new_error (task,
228 G_IO_ERROR,
229 G_IO_ERROR_NOT_SUPPORTED,
230 "No kallsyms found to decode");
231 return;
232 }
233
234 base_stream = input = sysprof_document_file_read (file);
235 }
236
237 g_assert (base_stream != NULL);
238 g_assert (G_IS_INPUT_STREAM (base_stream));
239
240 g_task_set_task_data (task,
241 g_data_input_stream_new (base_stream),
242 g_object_unref);
243
244 g_task_run_in_thread (task, sysprof_kallsyms_symbolizer_prepare_worker);
245 }
246
247 static gboolean
248 sysprof_kallsyms_symbolizer_prepare_finish (SysprofSymbolizer *symbolizer,
249 GAsyncResult *result,
250 GError **error)
251 {
252 g_assert (SYSPROF_IS_KALLSYMS_SYMBOLIZER (symbolizer));
253 g_assert (G_IS_TASK (result));
254 g_assert (g_task_is_valid (result, symbolizer));
255
256 return g_task_propagate_boolean (G_TASK (result), error);
257 }
258
259 static SysprofSymbol *
260 sysprof_kallsyms_symbolizer_symbolize (SysprofSymbolizer *symbolizer,
261 SysprofStrings *strings,
262 const SysprofProcessInfo *process_info,
263 SysprofAddressContext context,
264 SysprofAddress address)
265 {
266 SysprofKallsymsSymbolizer *self = (SysprofKallsymsSymbolizer *)symbolizer;
267 const KernelSymbol *symbols;
268 guint n_symbols;
269 guint left;
270 guint right;
271 guint mid;
272
273 if (context != SYSPROF_ADDRESS_CONTEXT_KERNEL)
274 return NULL;
275
276 if (address < self->low || address >= self->high)
277 goto failure;
278
279 symbols = &g_array_index (self->kallsyms, KernelSymbol, 0);
280 n_symbols = self->kallsyms->len;
281
282 left = 0;
283 right = n_symbols;
284 mid = n_symbols / 2;
285
286 while (left <= right)
287 {
288 const KernelSymbol *ksym = &symbols[mid];
289 const KernelSymbol *next = &symbols[mid+1];
290
291 if (address >= ksym->address &&
292 (address < next->address || next->address == 0))
293 return _sysprof_symbol_new (g_ref_string_acquire (ksym->name),
294 NULL,
295 g_ref_string_acquire (linux_string),
296 ksym->address,
297 next->address ? next->address : ksym->address + LAST_SYMBOL_LEN,
298 SYSPROF_SYMBOL_KIND_KERNEL);
299
300 if (address < ksym->address)
301 right = mid;
302 else
303 left = mid + 1;
304
305 mid = left + ((right-left) / 2);
306 }
307
308 failure:
309 {
310 SysprofSymbol *ret;
311 char name[64];
312
313 g_snprintf (name, sizeof name, "In Kernel+0x%"G_GINT64_MODIFIER"x", address);
314 ret = _sysprof_symbol_new (sysprof_strings_get (strings, name),
315 NULL,
316 g_ref_string_acquire (linux_string),
317 address,
318 address + 1,
319 SYSPROF_SYMBOL_KIND_KERNEL);
320 ret->is_fallback = TRUE;
321
322 return ret;
323 }
324 }
325
326 static void
327 sysprof_kallsyms_symbolizer_finalize (GObject *object)
328 {
329 SysprofKallsymsSymbolizer *self = (SysprofKallsymsSymbolizer *)object;
330
331 g_clear_object (&self->stream);
332 g_clear_pointer (&self->kallsyms, g_array_unref);
333
334 G_OBJECT_CLASS (sysprof_kallsyms_symbolizer_parent_class)->finalize (object);
335 }
336
337 static void
338 sysprof_kallsyms_symbolizer_class_init (SysprofKallsymsSymbolizerClass *klass)
339 {
340 GObjectClass *object_class = G_OBJECT_CLASS (klass);
341 SysprofSymbolizerClass *symbolizer_class = SYSPROF_SYMBOLIZER_CLASS (klass);
342
343 object_class->finalize = sysprof_kallsyms_symbolizer_finalize;
344
345 symbolizer_class->prepare_async = sysprof_kallsyms_symbolizer_prepare_async;
346 symbolizer_class->prepare_finish = sysprof_kallsyms_symbolizer_prepare_finish;
347 symbolizer_class->symbolize = sysprof_kallsyms_symbolizer_symbolize;
348
349 kallsym_strings = sysprof_strings_new ();
350 linux_string = g_ref_string_new_intern ("Linux");
351 }
352
353 static void
354 sysprof_kallsyms_symbolizer_init (SysprofKallsymsSymbolizer *self)
355 {
356 self->kallsyms = g_array_new (TRUE, FALSE, sizeof (KernelSymbol));
357 g_array_set_clear_func (self->kallsyms, kernel_symbol_clear);
358 }
359
360 SysprofSymbolizer *
361 sysprof_kallsyms_symbolizer_new (void)
362 {
363 return g_object_new (SYSPROF_TYPE_KALLSYMS_SYMBOLIZER, NULL);
364 }
365
366 /**
367 * sysprof_kallsyms_symbolizer_new_for_symbols:
368 * @symbols: (transfer full): a #GInputStream
369 *
370 * Creates a symbolizer using the contents of @symbols as the contents
371 * of `/proc/kallsyms` for decoding. This is useful if you need to unwind
372 * symbols from a machine that is different than where the capture was
373 * recorded and have not embedded `/proc/kallsyms.gz` within the capture
374 * file for use by #SysprofKallsymsSymbolizer.
375 *
376 * Returns: (transfer full): a #SysprofKallsymsSymbolizer
377 */
378 SysprofSymbolizer *
379 sysprof_kallsyms_symbolizer_new_for_symbols (GInputStream *symbols)
380 {
381 SysprofKallsymsSymbolizer *self;
382
383 g_return_val_if_fail (G_IS_INPUT_STREAM (symbols), NULL);
384
385 self = g_object_new (SYSPROF_TYPE_KALLSYMS_SYMBOLIZER, NULL);
386 self->stream = symbols;
387
388 return SYSPROF_SYMBOLIZER (self);
389 }