]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 2005-2018 Free Software Foundation, Inc. |
7756ba9d DH |
2 | |
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 Lesser General Public License as | |
7 | published by the Free Software Foundation; either version 2.1 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 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <stdio.h> | |
20 | #include <string.h> | |
21 | #include <sysdep.h> | |
22 | #include <signal.h> | |
23 | #include <execinfo.h> | |
24 | ||
25 | extern int | |
26 | _identify_sighandler (unsigned long fp, unsigned long pc, | |
27 | unsigned long *pprev_fp, unsigned long *pprev_pc, | |
28 | unsigned long *retaddr); | |
29 | ||
2b18fe78 | 30 | static inline long |
7756ba9d DH |
31 | get_frame_size (unsigned long instr) |
32 | { | |
33 | return abs ((short signed) (instr & 0xFFFF)); | |
34 | } | |
35 | ||
36 | static unsigned long * | |
37 | find_frame_creation (unsigned long *pc) | |
38 | { | |
39 | int i; | |
40 | ||
41 | /* NOTE: Distance to search is arbitrary. | |
42 | 250 works well for most things, | |
43 | 750 picks up things like tcp_recvmsg, | |
44 | 1000 needed for fat_fill_super. */ | |
45 | for (i = 0; i < 1000; i++, pc--) | |
46 | { | |
47 | unsigned long instr; | |
48 | unsigned long frame_size; | |
49 | ||
50 | instr = *pc; | |
51 | ||
52 | /* Is the instruction of the form | |
53 | addik r1, r1, foo ? */ | |
54 | if ((instr & 0xFFFF0000) != 0x30210000) | |
55 | continue; | |
56 | ||
57 | frame_size = get_frame_size (instr); | |
58 | ||
59 | if ((frame_size < 8) || (frame_size & 3)) | |
60 | return NULL; | |
61 | ||
62 | return pc; | |
63 | } | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | static int | |
68 | lookup_prev_stack_frame (unsigned long fp, unsigned long pc, | |
69 | unsigned long *pprev_fp, unsigned long *pprev_pc, | |
70 | unsigned long *retaddr) | |
71 | { | |
72 | unsigned long *prologue = NULL; | |
73 | ||
74 | int is_signalhandler = _identify_sighandler (fp, pc, pprev_fp, | |
75 | pprev_pc, retaddr); | |
76 | ||
77 | if (!is_signalhandler) | |
78 | { | |
79 | prologue = find_frame_creation ((unsigned long *) pc); | |
80 | ||
81 | if (prologue) | |
82 | { | |
83 | long frame_size = get_frame_size (*prologue); | |
84 | *pprev_fp = fp + frame_size; | |
85 | if (*retaddr != 0) | |
86 | *pprev_pc = *retaddr; | |
87 | else | |
88 | *pprev_pc = *(unsigned long *) fp; | |
89 | ||
90 | *retaddr = 0; | |
91 | if (!*pprev_pc || (*pprev_pc & 3)) | |
92 | prologue=0; | |
93 | } | |
94 | else | |
95 | { | |
96 | *pprev_pc = 0; | |
97 | *pprev_fp = fp; | |
98 | *retaddr = 0; | |
99 | } | |
100 | } | |
101 | return (!*pprev_pc || (*pprev_pc & 3)) ? -1 : 0; | |
102 | } | |
103 | ||
104 | int | |
105 | __backtrace (void **array, int size) | |
106 | { | |
107 | unsigned long pc, fp; | |
108 | unsigned long ppc, pfp; | |
109 | /* Return address(r15) is required in the signal handler case, since the | |
110 | return address of the function which causes the signal may not be | |
111 | recorded in the stack. */ | |
112 | unsigned long retaddr; | |
113 | ||
114 | int count; | |
115 | int rc = 0; | |
116 | ||
d5dff793 PP |
117 | if (size <= 0) |
118 | return 0; | |
119 | ||
7756ba9d DH |
120 | __asm__ __volatile__ ("mfs %0, rpc" |
121 | : "=r"(pc)); | |
122 | ||
123 | __asm__ __volatile__ ("add %0, r1, r0" | |
124 | : "=r"(fp)); | |
125 | ||
126 | array[0] = (void *) pc; | |
127 | retaddr = 0; | |
128 | for (count = 1; count < size; count++) | |
129 | { | |
130 | rc = lookup_prev_stack_frame (fp, pc, &pfp, &ppc, &retaddr); | |
131 | ||
132 | fp = pfp; | |
133 | pc = ppc; | |
134 | array[count] = (void *) pc; | |
135 | if (rc) | |
136 | return count; | |
137 | } | |
138 | return count; | |
139 | } | |
140 | ||
141 | weak_alias (__backtrace, backtrace) | |
142 | libc_hidden_def (__backtrace) |