1 /* Return backtrace of current program state.
2 Copyright (C) 1998, 2000, 2002, 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 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
17 not, see <http://www.gnu.org/licenses/>. */
21 #include <bp-checks.h>
23 /* This is the stack layout we see with every stack frame.
24 Note that every routine is required by the ABI to lay out the stack
27 +----------------+ +-----------------+
28 %r1 -> | %r1 last frame--------> | %r1 last frame--->... --> NULL
30 | cr save | | cr save |
32 | (unused) | | return address |
33 +----------------+ +-----------------+
37 struct layout
*__unbounded next
;
38 long condition_register
;
39 void *__unbounded return_address
;
43 __backtrace (void **array
, int size
)
45 struct layout
*current
;
48 /* Force gcc to spill LR. */
49 asm volatile ("" : "=l"(current
));
51 /* Get the address on top-of-stack. */
52 asm volatile ("ld %0,0(1)" : "=r"(current
));
53 current
= BOUNDED_1 (current
);
56 current
!= NULL
&& count
< size
;
57 current
= BOUNDED_1 (current
->next
), count
++)
58 array
[count
] = current
->return_address
;
60 /* It's possible the second-last stack frame can't return
61 (that is, it's __libc_start_main), in which case
62 the CRT startup code will have set its LR to 'NULL'. */
63 if (count
> 0 && array
[count
-1] == NULL
)
68 weak_alias (__backtrace
, backtrace
)
69 libc_hidden_def (__backtrace
)