]>
Commit | Line | Data |
---|---|---|
409dfcea | 1 | /* Return backtrace of current program state. |
d614a753 | 2 | Copyright (C) 1998-2020 Free Software Foundation, Inc. |
409dfcea UD |
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 | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
409dfcea UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
409dfcea | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
409dfcea UD |
18 | |
19 | #include <execinfo.h> | |
c2a32973 | 20 | #include <stddef.h> |
d400dcac AZ |
21 | #include <string.h> |
22 | #include <signal.h> | |
7bf8fb10 | 23 | #include <libc-vdso.h> |
409dfcea UD |
24 | |
25 | /* This is the stack layout we see with every stack frame. | |
26 | Note that every routine is required by the ABI to lay out the stack | |
27 | like this. | |
28 | ||
29 | +----------------+ +-----------------+ | |
30 | %r1 -> | %r1 last frame--------> | %r1 last frame--->... --> NULL | |
31 | | | | | | |
32 | | (unused) | | return address | | |
33 | +----------------+ +-----------------+ | |
34 | */ | |
35 | struct layout | |
36 | { | |
70d9946a JM |
37 | struct layout *next; |
38 | void *return_address; | |
409dfcea UD |
39 | }; |
40 | ||
d400dcac AZ |
41 | #define SIGNAL_FRAMESIZE 64 |
42 | ||
43 | /* Since the signal handler is just like any other function it needs to | |
44 | save/restore its LR and it will save it into callers stack frame. | |
45 | Since a signal handler doesn't have a caller, the kernel creates a | |
46 | dummy frame to make it look like it has a caller. */ | |
47 | struct signal_frame_32 { | |
48 | char dummy[SIGNAL_FRAMESIZE]; | |
49 | struct sigcontext sctx; | |
50 | mcontext_t mctx; | |
51 | /* We don't care about the rest, since IP value is at 'mctx' field. */ | |
52 | }; | |
53 | ||
1bdda52f | 54 | static inline bool |
31073a53 | 55 | is_sigtramp_address (void *nip) |
d400dcac | 56 | { |
b8a7c7da | 57 | #ifdef HAVE_SIGTRAMP_RT32 |
1bdda52f AZ |
58 | if (nip == GLRO (dl_vdso_sigtramp_32)) |
59 | return true; | |
d400dcac | 60 | #endif |
1bdda52f | 61 | return false; |
d400dcac AZ |
62 | } |
63 | ||
64 | struct rt_signal_frame_32 { | |
65 | char dummy[SIGNAL_FRAMESIZE + 16]; | |
66 | siginfo_t info; | |
25128773 | 67 | ucontext_t uc; |
d400dcac AZ |
68 | /* We don't care about the rest, since IP value is at 'uc' field. */ |
69 | }; | |
70 | ||
1bdda52f | 71 | static inline bool |
31073a53 | 72 | is_sigtramp_address_rt (void * nip) |
d400dcac | 73 | { |
b8a7c7da | 74 | #ifdef HAVE_SIGTRAMP_32 |
1bdda52f AZ |
75 | if (nip == GLRO (dl_vdso_sigtramp_rt32)) |
76 | return true; | |
d400dcac | 77 | #endif |
1bdda52f | 78 | return false; |
d400dcac AZ |
79 | } |
80 | ||
409dfcea UD |
81 | int |
82 | __backtrace (void **array, int size) | |
83 | { | |
84 | struct layout *current; | |
85 | int count; | |
c2a32973 | 86 | |
409dfcea UD |
87 | /* Force gcc to spill LR. */ |
88 | asm volatile ("" : "=l"(current)); | |
89 | ||
90 | /* Get the address on top-of-stack. */ | |
91 | asm volatile ("lwz %0,0(1)" : "=r"(current)); | |
92 | ||
93 | for ( count = 0; | |
94 | current != NULL && count < size; | |
e782a927 | 95 | current = current->next, count++) |
d400dcac AZ |
96 | { |
97 | gregset_t *gregset = NULL; | |
98 | ||
99 | array[count] = current->return_address; | |
100 | ||
101 | /* Check if the symbol is the signal trampoline and get the interrupted | |
102 | * symbol address from the trampoline saved area. */ | |
31073a53 | 103 | if (is_sigtramp_address (current->return_address)) |
d400dcac AZ |
104 | { |
105 | struct signal_frame_32 *sigframe = | |
106 | (struct signal_frame_32*) current; | |
107 | gregset = &sigframe->mctx.gregs; | |
108 | } | |
31073a53 | 109 | else if (is_sigtramp_address_rt (current->return_address)) |
d400dcac AZ |
110 | { |
111 | struct rt_signal_frame_32 *sigframe = | |
112 | (struct rt_signal_frame_32*) current; | |
113 | gregset = &sigframe->uc.uc_mcontext.uc_regs->gregs; | |
114 | } | |
115 | if (gregset) | |
31073a53 AZ |
116 | { |
117 | array[++count] = (void*)((*gregset)[PT_NIP]); | |
118 | current = (void*)((*gregset)[PT_R1]); | |
119 | } | |
d400dcac | 120 | } |
409dfcea UD |
121 | |
122 | /* It's possible the second-last stack frame can't return | |
123 | (that is, it's __libc_start_main), in which case | |
124 | the CRT startup code will have set its LR to 'NULL'. */ | |
125 | if (count > 0 && array[count-1] == NULL) | |
126 | count--; | |
127 | ||
128 | return count; | |
129 | } | |
130 | weak_alias (__backtrace, backtrace) | |
0e66ade5 | 131 | libc_hidden_def (__backtrace) |