1/* Return backtrace of current program state.
2 Copyright (C) 2003-2019 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 <http://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
27struct trace_arg
28{
29 void **array;
30 _Unwind_Word cfa;
31 int cnt;
32 int size;
33};
34
35#ifdef SHARED
36static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
37static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
38static _Unwind_Word (*unwind_getcfa) (struct _Unwind_Context *);
39static void *libgcc_handle;
40
41
42/* Dummy version in case libgcc_s does not contain the real code. */
43static _Unwind_Word
44dummy_getcfa (struct _Unwind_Context *ctx __attribute__ ((unused)))
45{
46 return 0;
47}
48
49
50static void
51init (void)
52{
53 libgcc_handle = __libc_dlopen (LIBGCC_S_SO);
54
55 if (libgcc_handle == NULL)
56 return;
57
58 unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
59 unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
60 if (unwind_getip == NULL)
61 unwind_backtrace = NULL;
62 unwind_getcfa = (__libc_dlsym (libgcc_handle, "_Unwind_GetCFA")
63 ?: dummy_getcfa);
64}
65#else
66# define unwind_backtrace _Unwind_Backtrace
67# define unwind_getip _Unwind_GetIP
68# define unwind_getcfa _Unwind_GetCFA
69#endif
70
71static _Unwind_Reason_Code
72backtrace_helper (struct _Unwind_Context *ctx, void *a)
73{
74 struct trace_arg *arg = a;
75
76 /* We are first called with address in the __backtrace function.
77 Skip it. */
78 if (arg->cnt != -1)
79 {
80 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
81
82 /* Check whether we make any progress. */
83 _Unwind_Word cfa = unwind_getcfa (ctx);
84
85 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
86 && cfa == arg->cfa)
87 return _URC_END_OF_STACK;
88 arg->cfa = cfa;
89 }
90 if (++arg->cnt == arg->size)
91 return _URC_END_OF_STACK;
92 return _URC_NO_REASON;
93}
94
95int
96__backtrace (void **array, int size)
97{
98 struct trace_arg arg = { .array = array, .cfa = 0, .size = size, .cnt = -1 };
99
100 if (size <= 0)
101 return 0;
102
103#ifdef SHARED
104 __libc_once_define (static, once);
105
106 __libc_once (once, init);
107 if (unwind_backtrace == NULL)
108 return 0;
109#endif
110
111 unwind_backtrace (backtrace_helper, &arg);
112
113 /* _Unwind_Backtrace seems to put NULL address above
114 _start. Fix it up here. */
115 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
116 --arg.cnt;
117 return arg.cnt != -1 ? arg.cnt : 0;
118}
119weak_alias (__backtrace, backtrace)
120libc_hidden_def (__backtrace)
121
122
123#ifdef SHARED
124/* Free all resources if necessary. */
125libc_freeres_fn (free_mem)
126{
127 unwind_backtrace = NULL;
128 if (libgcc_handle != NULL)
129 {
130 __libc_dlclose (libgcc_handle);
131 libgcc_handle = NULL;
132 }
133}
134#endif
135