Debian Codegraph @

src/preload/backtrace-helper.h

raw
1 /* backtrace-helper.h
2 *
3 * Copyright 2020 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 #pragma once
22
23 #define UNW_LOCAL_ONLY
24 #include <libunwind.h>
25
26 static void
27 backtrace_init (void)
28 {
29 unw_set_caching_policy (unw_local_addr_space, UNW_CACHE_PER_THREAD);
30 #ifdef HAVE_UNW_SET_CACHE_SIZE
31 unw_set_cache_size (unw_local_addr_space, 1024, 0);
32 #endif
33 }
34
35 static int
36 backtrace_func (SysprofCaptureAddress *addrs,
37 guint n_addrs,
38 G_GNUC_UNUSED gpointer user_data)
39 {
40 #if GLIB_SIZEOF_VOID_P == 8
41 SYSPROF_STATIC_ASSERT (offsetof (SysprofCaptureAllocation, addrs) >= GLIB_SIZEOF_VOID_P * 2,
42 "Require space for 2 discarded instruction pointers");
43 /* We know that collector will overwrite fields *AFTER* it
44 * has called the backtrace function allowing us to cheat
45 * and subtract an offset from addrs to avoid having to
46 * copy frame pointers around.
47 */
48 return unw_backtrace (((void **)addrs) - 2, n_addrs) - 2;
49 #else
50 static const int skip = 2;
51 void **stack = alloca (n_addrs * sizeof (gpointer));
52 int n = unw_backtrace (stack, n_addrs);
53 for (guint i = skip; i < n; i++)
54 addrs[i-skip] = GPOINTER_TO_SIZE (stack[i]);
55 return MAX (0, n - skip);
56 #endif
57 }
58