]>
Commit | Line | Data |
---|---|---|
b6ab06ce | 1 | /* Return backtrace of current program state. Generic version. |
b168057a | 2 | Copyright (C) 1998-2015 Free Software Foundation, Inc. |
b6ab06ce UD |
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 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. | |
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 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
b6ab06ce UD |
19 | |
20 | #include <execinfo.h> | |
21 | #include <signal.h> | |
22 | #include <frame.h> | |
23 | #include <sigcontextinfo.h> | |
b6ab06ce UD |
24 | #include <ldsodefs.h> |
25 | ||
26 | /* This implementation assumes a stack layout that matches the defaults | |
27 | used by gcc's `__builtin_frame_address' and `__builtin_return_address' | |
28 | (FP is the frame pointer register): | |
29 | ||
30 | +-----------------+ +-----------------+ | |
31 | FP -> | previous FP --------> | previous FP ------>... | |
32 | | | | | | |
33 | | return address | | return address | | |
34 | +-----------------+ +-----------------+ | |
35 | ||
36 | */ | |
37 | ||
38 | /* Get some notion of the current stack. Need not be exactly the top | |
39 | of the stack, just something somewhere in the current frame. */ | |
40 | #ifndef CURRENT_STACK_FRAME | |
41 | # define CURRENT_STACK_FRAME ({ char __csf; &__csf; }) | |
42 | #endif | |
43 | ||
44 | /* By default we assume that the stack grows downward. */ | |
45 | #ifndef INNER_THAN | |
46 | # define INNER_THAN < | |
47 | #endif | |
48 | ||
49 | /* By default assume the `next' pointer in struct layout points to the | |
50 | next struct layout. */ | |
51 | #ifndef ADVANCE_STACK_FRAME | |
e782a927 | 52 | # define ADVANCE_STACK_FRAME(next) ((struct layout *) (next)) |
b6ab06ce UD |
53 | #endif |
54 | ||
55 | /* By default, the frame pointer is just what we get from gcc. */ | |
56 | #ifndef FIRST_FRAME_POINTER | |
57 | # define FIRST_FRAME_POINTER __builtin_frame_address (0) | |
58 | #endif | |
59 | ||
60 | int | |
9d46370c | 61 | __backtrace (void **array, int size) |
b6ab06ce UD |
62 | { |
63 | struct layout *current; | |
70d9946a JM |
64 | void *top_frame; |
65 | void *top_stack; | |
b6ab06ce UD |
66 | int cnt = 0; |
67 | ||
68 | top_frame = FIRST_FRAME_POINTER; | |
69 | top_stack = CURRENT_STACK_FRAME; | |
70 | ||
71 | /* We skip the call to this function, it makes no sense to record it. */ | |
e782a927 | 72 | current = ((struct layout *) top_frame); |
b6ab06ce UD |
73 | while (cnt < size) |
74 | { | |
75 | if ((void *) current INNER_THAN top_stack | |
76 | || !((void *) current INNER_THAN __libc_stack_end)) | |
77 | /* This means the address is out of range. Note that for the | |
78 | toplevel we see a frame pointer with value NULL which clearly is | |
79 | out of range. */ | |
80 | break; | |
81 | ||
82 | array[cnt++] = current->return_address; | |
83 | ||
84 | current = ADVANCE_STACK_FRAME (current->next); | |
85 | } | |
86 | ||
87 | return cnt; | |
88 | } | |
89 | weak_alias (__backtrace, backtrace) | |
90 | libc_hidden_def (__backtrace) |