Update.
[glibc.git] / sysdeps / generic / backtrace.c
blobf27f890bc44a82d3651aaa7e89970d3a12adc8b1
1 /* Return backtrace of current program state. Generic version.
2 Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <execinfo.h>
22 #include <signal.h>
23 #include <frame.h>
24 #include <sigcontextinfo.h>
25 #include <bp-checks.h>
27 /* This is a global variable set at program start time. It marks the
28 highest used stack address. */
29 extern void *__libc_stack_end;
32 /* This implementation assumes a stack layout that matches the defaults
33 used by gcc's `__builtin_frame_address' and `__builtin_return_address'
34 (FP is the frame pointer register):
36 +-----------------+ +-----------------+
37 FP -> | previous FP --------> | previous FP ------>...
38 | | | |
39 | return address | | return address |
40 +-----------------+ +-----------------+
44 /* Get some notion of the current stack. Need not be exactly the top
45 of the stack, just something somewhere in the current frame. */
46 #ifndef CURRENT_STACK_FRAME
47 # define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
48 #endif
50 /* By default we assume that the stack grows downward. */
51 #ifndef INNER_THAN
52 # define INNER_THAN <
53 #endif
55 /* By default assume the `next' pointer in struct layout points to the
56 next struct layout. */
57 #ifndef ADVANCE_STACK_FRAME
58 # define ADVANCE_STACK_FRAME(next) BOUNDED_1 ((struct layout *) (next))
59 #endif
61 /* By default, the frame pointer is just what we get from gcc. */
62 #ifndef FIRST_FRAME_POINTER
63 # define FIRST_FRAME_POINTER __builtin_frame_address (0)
64 #endif
66 int
67 __backtrace (array, size)
68 void **array;
69 int size;
71 struct layout *current;
72 void *__unbounded top_frame;
73 void *__unbounded top_stack;
74 int cnt = 0;
76 top_frame = FIRST_FRAME_POINTER;
77 top_stack = CURRENT_STACK_FRAME;
79 /* We skip the call to this function, it makes no sense to record it. */
80 current = BOUNDED_1 ((struct layout *) top_frame);
81 while (cnt < size)
83 if ((void *) current INNER_THAN top_stack
84 || !((void *) current INNER_THAN __libc_stack_end))
85 /* This means the address is out of range. Note that for the
86 toplevel we see a frame pointer with value NULL which clearly is
87 out of range. */
88 break;
90 array[cnt++] = current->return_address;
92 current = ADVANCE_STACK_FRAME (current->next);
95 return cnt;
97 weak_alias (__backtrace, backtrace)