(_ISwbit): Protext use of parameter with parentheses.
[glibc.git] / sysdeps / powerpc / backtrace.c
blob7d71fbd9a8b283234d8f3ffa783abd854597d73d
1 /* Return backtrace of current program state.
2 Copyright (C) 1998 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 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.
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.
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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <execinfo.h>
22 /* This is the stack layout we see with every stack frame.
23 Note that every routine is required by the ABI to lay out the stack
24 like this.
26 +----------------+ +-----------------+
27 %r1 -> | %r1 last frame--------> | %r1 last frame--->... --> NULL
28 | | | |
29 | (unused) | | return address |
30 +----------------+ +-----------------+
32 struct layout
34 struct layout *next;
35 void *return_address;
38 int
39 __backtrace (void **array, int size)
41 struct layout *current;
42 int count;
44 /* Force gcc to spill LR. */
45 asm volatile ("" : "=l"(current));
47 /* Get the address on top-of-stack. */
48 asm volatile ("lwz %0,0(1)" : "=r"(current));
50 for ( count = 0;
51 current != NULL && count < size;
52 current = current->next, count++)
53 array[count] = current->return_address;
55 /* It's possible the second-last stack frame can't return
56 (that is, it's __libc_start_main), in which case
57 the CRT startup code will have set its LR to 'NULL'. */
58 if (count > 0 && array[count-1] == NULL)
59 count--;
61 return count;
63 weak_alias (__backtrace, backtrace)