]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/backtrace.c
Update.
[thirdparty/glibc.git] / sysdeps / i386 / backtrace.c
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.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
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 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <execinfo.h>
22
23
24 /* This is a global variable set at program start time. It marks the
25 highest used stack address. */
26 extern void *__libc_stack_end;
27
28
29 /* This is the stack alyout we see with every stack frame.
30
31 +-----------------+ +-----------------+
32 %ebp -> | %ebp last frame--------> | %ebp last frame--->...
33 | | | |
34 | return address | | return address |
35 +-----------------+ +-----------------+
36 */
37 struct layout
38 {
39 struct layout *next;
40 void *return_address;
41 };
42
43 int
44 __backtrace (array, size)
45 void **array;
46 int size;
47 {
48 /* We assume that all the code is generated with frame pointers set. */
49 register void *ebp __asm__ ("ebp");
50 register void *esp __asm__ ("esp");
51 struct layout *current;
52 int cnt = 0;
53
54 /* We skip the call to this function, it makes no sense to record it. */
55 current = (struct layout *) ebp;
56 while (cnt < size)
57 {
58 if ((void *) current < esp || (void *) current > __libc_stack_end)
59 /* This means the address is out of range. Note that for the
60 toplevel we see a frame pointer with value NULL which clearly is
61 out of range. */
62 break;
63
64 array[cnt++] = current->return_address;
65
66 current = current->next;
67 }
68
69 return cnt;
70 }
71 weak_alias (__backtrace, backtrace)