2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc32 / backtrace.c
blobe7e12544c53e1b3fe6739e3f501da5e18bfd4369
1 /* Return backtrace of current program state.
2 Copyright (C) 1998, 2000, 2005 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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <execinfo.h>
21 #include <stddef.h>
22 #include <bp-checks.h>
24 /* This is the stack layout we see with every stack frame.
25 Note that every routine is required by the ABI to lay out the stack
26 like this.
28 +----------------+ +-----------------+
29 %r1 -> | %r1 last frame--------> | %r1 last frame--->... --> NULL
30 | | | |
31 | (unused) | | return address |
32 +----------------+ +-----------------+
34 struct layout
36 struct layout *__unbounded next;
37 void *__unbounded return_address;
40 int
41 __backtrace (void **array, int size)
43 struct layout *current;
44 int count;
46 /* Force gcc to spill LR. */
47 asm volatile ("" : "=l"(current));
49 /* Get the address on top-of-stack. */
50 asm volatile ("lwz %0,0(1)" : "=r"(current));
51 current = BOUNDED_1 (current);
53 for ( count = 0;
54 current != NULL && count < size;
55 current = BOUNDED_1 (current->next), count++)
56 array[count] = current->return_address;
58 /* It's possible the second-last stack frame can't return
59 (that is, it's __libc_start_main), in which case
60 the CRT startup code will have set its LR to 'NULL'. */
61 if (count > 0 && array[count-1] == NULL)
62 count--;
64 return count;
66 weak_alias (__backtrace, backtrace)
67 libc_hidden_def (__backtrace)