1 /* Return backtrace of current program state.
2 Copyright (C) 2013-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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
23 #include <backtrace.h>
24 #include <unwind-link.h>
28 unsigned long locals
[8];
37 struct unwind_link
*unwind_link
;
43 static _Unwind_Reason_Code
44 backtrace_helper (struct _Unwind_Context
*ctx
, void *a
)
46 struct trace_arg
*arg
= a
;
49 /* We are first called with address in the __backtrace function.
53 ip
= UNWIND_LINK_PTR (arg
->unwind_link
, _Unwind_GetIP
) (ctx
);
54 arg
->array
[arg
->cnt
] = (void *) ip
;
56 /* Check whether we make any progress. */
58 = UNWIND_LINK_PTR (arg
->unwind_link
, _Unwind_GetCFA
) (ctx
);
60 if (arg
->cnt
> 0 && arg
->array
[arg
->cnt
- 1] == arg
->array
[arg
->cnt
]
62 return _URC_END_OF_STACK
;
65 if (++arg
->cnt
== arg
->size
)
66 return _URC_END_OF_STACK
;
67 return _URC_NO_REASON
;
71 __backtrace (void **array
, int size
)
74 struct trace_arg arg
=
77 .unwind_link
= __libc_unwind_link_get (),
85 if (arg
.unwind_link
== NULL
)
87 struct layout
*current
;
90 asm volatile ("mov %%fp, %0" : "=r"(fp
));
91 asm volatile ("mov %%i7, %0" : "=r"(i7
));
92 current
= (struct layout
*) (fp
+ BACKTRACE_STACK_BIAS
);
94 array
[0] = (void *) i7
;
99 backtrace_flush_register_windows();
100 for (count
= 1; count
< size
; count
++)
102 array
[count
] = current
->return_address
;
105 current
= (struct layout
*) (current
->next
+ BACKTRACE_STACK_BIAS
);
110 UNWIND_LINK_PTR (arg
.unwind_link
, _Unwind_Backtrace
)
111 (backtrace_helper
, &arg
);
113 /* _Unwind_Backtrace seems to put NULL address above
114 _start. Fix it up here. */
115 if (arg
.cnt
> 1 && arg
.array
[arg
.cnt
- 1] == NULL
)
117 count
= arg
.cnt
!= -1 ? arg
.cnt
: 0;
121 weak_alias (__backtrace
, backtrace
)
122 libc_hidden_def (__backtrace
)