1 /* Return backtrace of current program state.
2 Copyright (C) 2013-2020 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <libc-lock.h>
29 void *lastfp
, *lastsp
;
33 static _Unwind_Reason_Code (*unwind_backtrace
) (_Unwind_Trace_Fn
, void *);
34 static _Unwind_Ptr (*unwind_getip
) (struct _Unwind_Context
*);
35 static _Unwind_Ptr (*unwind_getcfa
) (struct _Unwind_Context
*);
36 static _Unwind_Ptr (*unwind_getgr
) (struct _Unwind_Context
*, int);
37 static void *libgcc_handle
;
42 libgcc_handle
= __libc_dlopen ("libgcc_s.so.2");
44 if (libgcc_handle
== NULL
)
47 unwind_backtrace
= __libc_dlsym (libgcc_handle
, "_Unwind_Backtrace");
48 unwind_getip
= __libc_dlsym (libgcc_handle
, "_Unwind_GetIP");
49 unwind_getcfa
= __libc_dlsym (libgcc_handle
, "_Unwind_GetCFA");
50 unwind_getgr
= __libc_dlsym (libgcc_handle
, "_Unwind_GetGR");
51 if (unwind_getip
== NULL
|| unwind_getgr
== NULL
|| unwind_getcfa
== NULL
)
53 unwind_backtrace
= NULL
;
54 __libc_dlclose (libgcc_handle
);
59 # define unwind_backtrace _Unwind_Backtrace
60 # define unwind_getip _Unwind_GetIP
61 # define unwind_getcfa _Unwind_GetCFA
62 # define unwind_getgr _Unwind_GetGR
65 static _Unwind_Reason_Code
66 backtrace_helper (struct _Unwind_Context
*ctx
, void *a
)
68 struct trace_arg
*arg
= a
;
70 /* We are first called with address in the __backtrace function.
73 arg
->array
[arg
->cnt
] = (void *) unwind_getip (ctx
);
74 if (++arg
->cnt
== arg
->size
)
75 return _URC_END_OF_STACK
;
77 /* %fp is DWARF2 register 14 on M68K. */
78 arg
->lastfp
= (void *) unwind_getgr (ctx
, 14);
79 arg
->lastsp
= (void *) unwind_getcfa (ctx
);
80 return _URC_NO_REASON
;
84 /* This is a global variable set at program start time. It marks the
85 highest used stack address. */
86 extern void *__libc_stack_end
;
89 /* This is the stack layout we see with every stack frame
90 if not compiled without frame pointer.
92 +-----------------+ +-----------------+
93 %fp -> | %fp last frame--------> | %fp last frame--->...
95 | return address | | return address |
96 +-----------------+ +-----------------+
98 First try as far to get as far as possible using
99 _Unwind_Backtrace which handles -fomit-frame-pointer
100 as well, but requires .eh_frame info. Then fall back to
101 walking the stack manually. */
111 __backtrace (void **array
, int size
)
113 struct trace_arg arg
= { .array
= array
, .size
= size
, .cnt
= -1 };
119 __libc_once_define (static, once
);
121 __libc_once (once
, init
);
122 if (unwind_backtrace
== NULL
)
126 unwind_backtrace (backtrace_helper
, &arg
);
128 if (arg
.cnt
> 1 && arg
.array
[arg
.cnt
- 1] == NULL
)
130 else if (arg
.cnt
< size
)
132 struct layout
*fp
= (struct layout
*) arg
.lastfp
;
134 while (arg
.cnt
< size
)
136 /* Check for out of range. */
137 if ((void *) fp
< arg
.lastsp
|| (void *) fp
> __libc_stack_end
141 array
[arg
.cnt
++] = fp
->ret
;
145 return arg
.cnt
!= -1 ? arg
.cnt
: 0;
147 weak_alias (__backtrace
, backtrace
)
148 libc_hidden_def (__backtrace
)
152 /* Free all resources if necessary. */
153 libc_freeres_fn (free_mem
)
155 unwind_backtrace
= NULL
;
156 if (libgcc_handle
!= NULL
)
158 __libc_dlclose (libgcc_handle
);
159 libgcc_handle
= NULL
;