]>
Commit | Line | Data |
---|---|---|
7c1e01aa | 1 | /* Return backtrace of current program state. |
581c785b | 2 | Copyright (C) 2013-2022 Free Software Foundation, Inc. |
7c1e01aa | 3 | This file is part of the GNU C Library. |
7c1e01aa DM |
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 | |
5a82c748 | 17 | not, see <https://www.gnu.org/licenses/>. */ |
7c1e01aa DM |
18 | |
19 | #include <execinfo.h> | |
20 | #include <stddef.h> | |
7c1e01aa DM |
21 | #include <sysdep.h> |
22 | #include <sys/trap.h> | |
7c1e01aa | 23 | #include <backtrace.h> |
600fe89c | 24 | #include <unwind-link.h> |
7c1e01aa DM |
25 | |
26 | struct layout | |
27 | { | |
28 | unsigned long locals[8]; | |
29 | unsigned long ins[6]; | |
30 | unsigned long next; | |
70d9946a | 31 | void *return_address; |
7c1e01aa DM |
32 | }; |
33 | ||
34 | struct trace_arg | |
35 | { | |
36 | void **array; | |
600fe89c | 37 | struct unwind_link *unwind_link; |
7c1e01aa DM |
38 | _Unwind_Word cfa; |
39 | int cnt; | |
40 | int size; | |
41 | }; | |
42 | ||
7c1e01aa DM |
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 | { | |
600fe89c | 53 | ip = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetIP) (ctx); |
7c1e01aa DM |
54 | arg->array[arg->cnt] = (void *) ip; |
55 | ||
56 | /* Check whether we make any progress. */ | |
600fe89c FW |
57 | _Unwind_Word cfa |
58 | = UNWIND_LINK_PTR (arg->unwind_link, _Unwind_GetCFA) (ctx); | |
7c1e01aa DM |
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 | { | |
7c1e01aa | 73 | int count; |
600fe89c FW |
74 | struct trace_arg arg = |
75 | { | |
76 | .array = array, | |
77 | .unwind_link = __libc_unwind_link_get (), | |
78 | .size = size, | |
79 | .cnt = -1, | |
80 | }; | |
7c1e01aa | 81 | |
d5dff793 | 82 | if (size <= 0) |
7c1e01aa DM |
83 | return 0; |
84 | ||
600fe89c | 85 | if (arg.unwind_link == NULL) |
7c1e01aa DM |
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)); | |
70d9946a | 92 | current = (struct layout *) (fp + BACKTRACE_STACK_BIAS); |
7c1e01aa | 93 | |
70d9946a | 94 | array[0] = (void *) i7; |
7c1e01aa DM |
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; | |
70d9946a | 105 | current = (struct layout *) (current->next + BACKTRACE_STACK_BIAS); |
7c1e01aa DM |
106 | } |
107 | } | |
108 | else | |
109 | { | |
600fe89c FW |
110 | UNWIND_LINK_PTR (arg.unwind_link, _Unwind_Backtrace) |
111 | (backtrace_helper, &arg); | |
7c1e01aa DM |
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) |