Implement <unwind-link.h> for dynamically loading the libgcc_s unwinder
[platform/upstream/glibc.git] / debug / backtrace.c
1 /* Return backtrace of current program state.
2    Copyright (C) 2003-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library 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 GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <https://www.gnu.org/licenses/>.  */
19
20 #include <libc-lock.h>
21 #include <dlfcn.h>
22 #include <execinfo.h>
23 #include <gnu/lib-names.h>
24 #include <stdlib.h>
25 #include <unwind.h>
26 #include <unwind-link.h>
27
28 struct trace_arg
29 {
30   void **array;
31   _Unwind_Word cfa;
32   int cnt;
33   int size;
34 };
35
36 #ifdef SHARED
37 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
38 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
39 static _Unwind_Word (*unwind_getcfa) (struct _Unwind_Context *);
40 static void *libgcc_handle;
41
42
43 /* Dummy version in case libgcc_s does not contain the real code.  */
44 static _Unwind_Word
45 dummy_getcfa (struct _Unwind_Context *ctx __attribute__ ((unused)))
46 {
47   return 0;
48 }
49
50
51 static void
52 init (void)
53 {
54   libgcc_handle = __libc_dlopen (LIBGCC_S_SO);
55
56   if (libgcc_handle == NULL)
57     return;
58
59   unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
60   unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
61   if (unwind_getip == NULL)
62     unwind_backtrace = NULL;
63   unwind_getcfa = (__libc_dlsym (libgcc_handle, "_Unwind_GetCFA")
64                   ?: dummy_getcfa);
65 }
66 #else
67 # define unwind_backtrace _Unwind_Backtrace
68 # define unwind_getip _Unwind_GetIP
69 # define unwind_getcfa _Unwind_GetCFA
70 #endif
71
72 static _Unwind_Reason_Code
73 backtrace_helper (struct _Unwind_Context *ctx, void *a)
74 {
75   struct trace_arg *arg = a;
76
77   /* We are first called with address in the __backtrace function.
78      Skip it.  */
79   if (arg->cnt != -1)
80     {
81       arg->array[arg->cnt] = (void *) unwind_getip (ctx);
82       if (arg->cnt > 0)
83         arg->array[arg->cnt]
84           = unwind_arch_adjustment (arg->array[arg->cnt - 1],
85                                     arg->array[arg->cnt]);
86
87       /* Check whether we make any progress.  */
88       _Unwind_Word cfa = unwind_getcfa (ctx);
89
90       if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
91          && cfa == arg->cfa)
92        return _URC_END_OF_STACK;
93       arg->cfa = cfa;
94     }
95   if (++arg->cnt == arg->size)
96     return _URC_END_OF_STACK;
97   return _URC_NO_REASON;
98 }
99
100 int
101 __backtrace (void **array, int size)
102 {
103   struct trace_arg arg = { .array = array, .cfa = 0, .size = size, .cnt = -1 };
104
105   if (size <= 0)
106     return 0;
107
108 #ifdef SHARED
109   __libc_once_define (static, once);
110
111   __libc_once (once, init);
112   if (unwind_backtrace == NULL)
113     return 0;
114 #endif
115
116   unwind_backtrace (backtrace_helper, &arg);
117
118   /* _Unwind_Backtrace seems to put NULL address above
119      _start.  Fix it up here.  */
120   if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
121     --arg.cnt;
122   return arg.cnt != -1 ? arg.cnt : 0;
123 }
124 weak_alias (__backtrace, backtrace)
125 libc_hidden_def (__backtrace)
126
127
128 #ifdef SHARED
129 /* Free all resources if necessary.  */
130 libc_freeres_fn (free_mem)
131 {
132   unwind_backtrace = NULL;
133   if (libgcc_handle != NULL)
134     {
135       __libc_dlclose (libgcc_handle);
136       libgcc_handle = NULL;
137     }
138 }
139 #endif