]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/x86_64/backtrace.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / x86_64 / backtrace.c
CommitLineData
e40bca1e 1/* Return backtrace of current program state.
b168057a 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
e40bca1e
UD
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
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
e40bca1e
UD
19
20#include <bits/libc-lock.h>
21#include <dlfcn.h>
22#include <execinfo.h>
23#include <stdlib.h>
24#include <unwind.h>
25
26struct trace_arg
27{
28 void **array;
29 _Unwind_Word cfa;
30 int cnt;
31 int size;
32};
33
34#ifdef SHARED
35static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
36static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
37static _Unwind_Word (*unwind_getcfa) (struct _Unwind_Context *);
38static void *libgcc_handle;
39
40
41/* Dummy version in case libgcc_s does not contain the real code. */
42static _Unwind_Word
43dummy_getcfa (struct _Unwind_Context *ctx __attribute__ ((unused)))
44{
45 return 0;
46}
47
48
49static void
50init (void)
51{
52 libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
53
54 if (libgcc_handle == NULL)
55 return;
56
57 unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
58 unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
59 if (unwind_getip == NULL)
60 unwind_backtrace = NULL;
61 unwind_getcfa = (__libc_dlsym (libgcc_handle, "_Unwind_GetCFA")
62 ?: dummy_getcfa);
63}
64#else
65# define unwind_backtrace _Unwind_Backtrace
66# define unwind_getip _Unwind_GetIP
67# define unwind_getcfa _Unwind_GetCFA
68#endif
69
70static _Unwind_Reason_Code
71backtrace_helper (struct _Unwind_Context *ctx, void *a)
72{
73 struct trace_arg *arg = a;
74
75 /* We are first called with address in the __backtrace function.
76 Skip it. */
77 if (arg->cnt != -1)
78 {
79 arg->array[arg->cnt] = (void *) unwind_getip (ctx);
80
81 /* Check whether we make any progress. */
82 _Unwind_Word cfa = unwind_getcfa (ctx);
83
84 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
85 && cfa == arg->cfa)
86 return _URC_END_OF_STACK;
87 arg->cfa = cfa;
88 }
89 if (++arg->cnt == arg->size)
90 return _URC_END_OF_STACK;
91 return _URC_NO_REASON;
92}
93
94int
95__backtrace (array, size)
96 void **array;
97 int size;
98{
99 struct trace_arg arg = { .array = array, .cfa = 0, .size = size, .cnt = -1 };
100#ifdef SHARED
101 __libc_once_define (static, once);
102
103 __libc_once (once, init);
104 if (unwind_backtrace == NULL)
105 return 0;
106#endif
107
108 if (size >= 1)
109 unwind_backtrace (backtrace_helper, &arg);
110
305502f6 111 /* _Unwind_Backtrace seems to put NULL address above
e40bca1e
UD
112 _start. Fix it up here. */
113 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
114 --arg.cnt;
115 return arg.cnt != -1 ? arg.cnt : 0;
116}
117weak_alias (__backtrace, backtrace)
118libc_hidden_def (__backtrace)
119
120
121#ifdef SHARED
122/* Free all resources if necessary. */
123libc_freeres_fn (free_mem)
124{
125 unwind_backtrace = NULL;
126 if (libgcc_handle != NULL)
127 {
128 __libc_dlclose (libgcc_handle);
129 libgcc_handle = NULL;
130 }
131}
132#endif