1 | /* Return backtrace of current program state. |
2 | Copyright (C) 2013-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public License as |
7 | published by the Free Software Foundation; either version 2 of the |
8 | License, or (at your option) any later version. |
9 | |
10 | The GNU C Library 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 GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public |
16 | License along with the GNU C Library; see the file COPYING.LIB. If |
17 | not, see <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <execinfo.h> |
20 | #include <stddef.h> |
21 | #include <sysdep.h> |
22 | #include <sys/trap.h> |
23 | #include <backtrace.h> |
24 | #include <unwind-link.h> |
25 | |
26 | struct layout |
27 | { |
28 | unsigned long locals[8]; |
29 | unsigned long ins[6]; |
30 | unsigned long next; |
31 | void *return_address; |
32 | }; |
33 | |
34 | struct trace_arg |
35 | { |
36 | void **array; |
37 | struct unwind_link *unwind_link; |
38 | _Unwind_Word cfa; |
39 | int cnt; |
40 | int size; |
41 | }; |
42 | |
43 | static _Unwind_Reason_Code |
44 | backtrace_helper (struct _Unwind_Context *ctx, void *a) |
45 | { |
46 | struct trace_arg *arg = a; |
47 | _Unwind_Ptr ip; |
48 | |
49 | /* We are first called with address in the __backtrace function. |
50 | Skip it. */ |
51 | if (arg->cnt != -1) |
52 | { |
53 | ip = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetIP) (ctx); |
54 | arg->array[arg->cnt] = (void *) ip; |
55 | |
56 | /* Check whether we make any progress. */ |
57 | _Unwind_Word cfa |
58 | = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetCFA) (ctx); |
59 | |
60 | if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt] |
61 | && cfa == arg->cfa) |
62 | return _URC_END_OF_STACK; |
63 | arg->cfa = cfa; |
64 | } |
65 | if (++arg->cnt == arg->size) |
66 | return _URC_END_OF_STACK; |
67 | return _URC_NO_REASON; |
68 | } |
69 | |
70 | int |
71 | __backtrace (void **array, int size) |
72 | { |
73 | int count; |
74 | struct trace_arg arg = |
75 | { |
76 | .array = array, |
77 | .unwind_link = __libc_unwind_link_get (), |
78 | .size = size, |
79 | .cnt = -1, |
80 | }; |
81 | |
82 | if (size <= 0) |
83 | return 0; |
84 | |
85 | if (arg.unwind_link == NULL) |
86 | { |
87 | struct layout *current; |
88 | unsigned long fp, i7; |
89 | |
90 | asm volatile ("mov %%fp, %0" : "=r" (fp)); |
91 | asm volatile ("mov %%i7, %0" : "=r" (i7)); |
92 | current = (struct layout *) (fp + BACKTRACE_STACK_BIAS); |
93 | |
94 | array[0] = (void *) i7; |
95 | |
96 | if (size == 1) |
97 | return 1; |
98 | |
99 | backtrace_flush_register_windows(); |
100 | for (count = 1; count < size; count++) |
101 | { |
102 | array[count] = current->return_address; |
103 | if (!current->next) |
104 | break; |
105 | current = (struct layout *) (current->next + BACKTRACE_STACK_BIAS); |
106 | } |
107 | } |
108 | else |
109 | { |
110 | UNWIND_LINK_PTR (arg.unwind_link, _Unwind_Backtrace) |
111 | (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 | count = arg.cnt != -1 ? arg.cnt : 0; |
118 | } |
119 | return count; |
120 | } |
121 | weak_alias (__backtrace, backtrace) |
122 | libc_hidden_def (__backtrace) |
123 | |