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 <execinfo.h>
21#include <stdlib.h>
22#include <unwind.h>
23#include <unwind-link.h>
24
25struct trace_arg
26{
27 void **array;
28 struct unwind_link *unwind_link;
29 _Unwind_Word cfa;
30 int cnt;
31 int size;
32};
33
34static _Unwind_Reason_Code
35backtrace_helper (struct _Unwind_Context *ctx, void *a)
36{
37 struct trace_arg *arg = a;
38
39 /* We are first called with address in the __backtrace function.
40 Skip it. */
41 if (arg->cnt != -1)
42 {
43 arg->array[arg->cnt]
44 = (void *) UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetIP) (ctx);
45 if (arg->cnt > 0)
46 arg->array[arg->cnt]
47 = unwind_arch_adjustment (arg->array[arg->cnt - 1],
48 arg->array[arg->cnt]);
49
50 /* Check whether we make any progress. */
51 _Unwind_Word cfa
52 = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetCFA) (ctx);
53
54 if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
55 && cfa == arg->cfa)
56 return _URC_END_OF_STACK;
57 arg->cfa = cfa;
58 }
59 if (++arg->cnt == arg->size)
60 return _URC_END_OF_STACK;
61 return _URC_NO_REASON;
62}
63
64int
65__backtrace (void **array, int size)
66{
67 struct trace_arg arg =
68 {
69 .array = array,
70 .unwind_link = __libc_unwind_link_get (),
71 .cfa = 0,
72 .size = size,
73 .cnt = -1
74 };
75
76 if (size <= 0 || arg.unwind_link == NULL)
77 return 0;
78
79 UNWIND_LINK_PTR (arg.unwind_link, _Unwind_Backtrace)
80 (backtrace_helper, &arg);
81
82 /* _Unwind_Backtrace seems to put NULL address above
83 _start. Fix it up here. */
84 if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
85 --arg.cnt;
86 return arg.cnt != -1 ? arg.cnt : 0;
87}
88weak_alias (__backtrace, backtrace)
89libc_hidden_def (__backtrace)
90