* elf/dl-reloc.c (allocate_static_tls): Rename to...
[glibc.git] / sysdeps / i386 / backtrace.c
blob10ff44d11dd828d2e06ae859ca3fed418b3115f1
1 /* Return backtrace of current program state.
2 Copyright (C) 1998, 2000 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 <bp-checks.h>
25 /* This is a global variable set at program start time. It marks the
26 highest used stack address. */
27 extern void *__libc_stack_end;
30 /* This is the stack alyout we see with every stack frame.
32 +-----------------+ +-----------------+
33 %ebp -> | %ebp last frame--------> | %ebp last frame--->...
34 | | | |
35 | return address | | return address |
36 +-----------------+ +-----------------+
38 struct layout
40 struct layout *__unbounded next;
41 void *__unbounded return_address;
44 int
45 __backtrace (array, size)
46 void **array;
47 int size;
49 /* We assume that all the code is generated with frame pointers set. */
50 register void *ebp __asm__ ("ebp");
51 register void *esp __asm__ ("esp");
52 struct layout *current;
53 int cnt = 0;
55 /* We skip the call to this function, it makes no sense to record it. */
56 current = BOUNDED_1 ((struct layout *) ebp);
57 while (cnt < size)
59 if ((void *) current < esp || (void *) current > __libc_stack_end)
60 /* This means the address is out of range. Note that for the
61 toplevel we see a frame pointer with value NULL which clearly is
62 out of range. */
63 break;
65 array[cnt++] = current->return_address;
67 current = current->next;
70 return cnt;
72 weak_alias (__backtrace, backtrace)