1 /* Return backtrace of current program state.
2 Copyright (C) 2003-2023 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/>. */
22 #include <unwind-link.h>
27 struct unwind_link
*unwind_link
;
33 static _Unwind_Reason_Code
34 backtrace_helper (struct _Unwind_Context
*ctx
, void *a
)
36 struct trace_arg
*arg
= a
;
38 /* We are first called with address in the __backtrace function.
43 = (void *) UNWIND_LINK_PTR (arg
->unwind_link
, _Unwind_GetIP
) (ctx
);
46 = unwind_arch_adjustment (arg
->array
[arg
->cnt
- 1],
47 arg
->array
[arg
->cnt
]);
49 /* Check whether we make any progress. */
51 = UNWIND_LINK_PTR (arg
->unwind_link
, _Unwind_GetCFA
) (ctx
);
53 if (arg
->cnt
> 0 && arg
->array
[arg
->cnt
- 1] == arg
->array
[arg
->cnt
]
55 return _URC_END_OF_STACK
;
58 if (++arg
->cnt
== arg
->size
)
59 return _URC_END_OF_STACK
;
60 return _URC_NO_REASON
;
64 __backtrace (void **array
, int size
)
66 struct trace_arg arg
=
69 .unwind_link
= __libc_unwind_link_get (),
75 if (size
<= 0 || arg
.unwind_link
== NULL
)
78 UNWIND_LINK_PTR (arg
.unwind_link
, _Unwind_Backtrace
)
79 (backtrace_helper
, &arg
);
81 /* _Unwind_Backtrace seems to put NULL address above
82 _start. Fix it up here. */
83 if (arg
.cnt
> 1 && arg
.array
[arg
.cnt
- 1] == NULL
)
85 return arg
.cnt
!= -1 ? arg
.cnt
: 0;
87 weak_alias (__backtrace
, backtrace
)
88 libc_hidden_def (__backtrace
)