Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / microblaze / backtrace.c
blob24e827ac39805f3c41fbf7f33e18afce70c1aeb4
1 /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <stdio.h>
20 #include <string.h>
21 #include <sysdep.h>
22 #include <signal.h>
23 #include <execinfo.h>
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);
30 inline long
31 get_frame_size (unsigned long instr)
33 return abs ((short signed) (instr & 0xFFFF));
36 static unsigned long *
37 find_frame_creation (unsigned long *pc)
39 int i;
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--)
47 unsigned long instr;
48 unsigned long frame_size;
50 instr = *pc;
52 /* Is the instruction of the form
53 addik r1, r1, foo ? */
54 if ((instr & 0xFFFF0000) != 0x30210000)
55 continue;
57 frame_size = get_frame_size (instr);
59 if ((frame_size < 8) || (frame_size & 3))
60 return NULL;
62 return pc;
64 return NULL;
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)
72 unsigned long *prologue = NULL;
74 int is_signalhandler = _identify_sighandler (fp, pc, pprev_fp,
75 pprev_pc, retaddr);
77 if (!is_signalhandler)
79 prologue = find_frame_creation ((unsigned long *) pc);
81 if (prologue)
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;
90 *retaddr = 0;
91 if (!*pprev_pc || (*pprev_pc & 3))
92 prologue=0;
94 else
96 *pprev_pc = 0;
97 *pprev_fp = fp;
98 *retaddr = 0;
101 return (!*pprev_pc || (*pprev_pc & 3)) ? -1 : 0;
105 __backtrace (void **array, int size)
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;
114 int count;
115 int rc = 0;
117 __asm__ __volatile__ ("mfs %0, rpc"
118 : "=r"(pc));
120 __asm__ __volatile__ ("add %0, r1, r0"
121 : "=r"(fp));
123 array[0] = (void *) pc;
124 retaddr = 0;
125 for (count = 1; count < size; count++)
127 rc = lookup_prev_stack_frame (fp, pc, &pfp, &ppc, &retaddr);
129 fp = pfp;
130 pc = ppc;
131 array[count] = (void *) pc;
132 if (rc)
133 return count;
135 return count;
138 weak_alias (__backtrace, backtrace)
139 libc_hidden_def (__backtrace)