1 /* Return backtrace of current program state.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by David S. Miller <davem@davemloft.net>
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.
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.
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
18 not, see <http://www.gnu.org/licenses/>. */
26 #include <backtrace.h>
30 unsigned long locals
[8];
45 static _Unwind_Reason_Code (*unwind_backtrace
) (_Unwind_Trace_Fn
, void *);
46 static _Unwind_Ptr (*unwind_getip
) (struct _Unwind_Context
*);
47 static _Unwind_Word (*unwind_getcfa
) (struct _Unwind_Context
*);
48 static void *libgcc_handle
;
50 /* Dummy version in case libgcc_s does not contain the real code. */
52 dummy_getcfa (struct _Unwind_Context
*ctx
__attribute__ ((unused
)))
60 libgcc_handle
= __libc_dlopen ("libgcc_s.so.1");
62 if (libgcc_handle
== NULL
)
65 unwind_backtrace
= __libc_dlsym (libgcc_handle
, "_Unwind_Backtrace");
66 unwind_getip
= __libc_dlsym (libgcc_handle
, "_Unwind_GetIP");
67 if (unwind_getip
== NULL
)
68 unwind_backtrace
= NULL
;
69 unwind_getcfa
= (__libc_dlsym (libgcc_handle
, "_Unwind_GetCFA")
73 # define unwind_backtrace _Unwind_Backtrace
74 # define unwind_getip _Unwind_GetIP
75 # define unwind_getcfa _Unwind_GetCFA
78 static _Unwind_Reason_Code
79 backtrace_helper (struct _Unwind_Context
*ctx
, void *a
)
81 struct trace_arg
*arg
= a
;
84 /* We are first called with address in the __backtrace function.
88 ip
= unwind_getip (ctx
);
89 arg
->array
[arg
->cnt
] = (void *) ip
;
91 /* Check whether we make any progress. */
92 _Unwind_Word cfa
= unwind_getcfa (ctx
);
94 if (arg
->cnt
> 0 && arg
->array
[arg
->cnt
- 1] == arg
->array
[arg
->cnt
]
96 return _URC_END_OF_STACK
;
99 if (++arg
->cnt
== arg
->size
)
100 return _URC_END_OF_STACK
;
101 return _URC_NO_REASON
;
105 __backtrace (void **array
, int size
)
107 struct trace_arg arg
= { .array
= array
, .cfa
= 0, .size
= size
, .cnt
= -1 };
116 __libc_once_define (static, once
);
118 __libc_once (once
, init
);
119 if (unwind_backtrace
== NULL
)
120 use_unwinder
= false;
123 if (use_unwinder
== false)
125 struct layout
*current
;
126 unsigned long fp
, i7
;
128 asm volatile ("mov %%fp, %0" : "=r"(fp
));
129 asm volatile ("mov %%i7, %0" : "=r"(i7
));
130 current
= (struct layout
*) (fp
+ BACKTRACE_STACK_BIAS
);
132 array
[0] = (void *) i7
;
137 backtrace_flush_register_windows();
138 for (count
= 1; count
< size
; count
++)
140 array
[count
] = current
->return_address
;
143 current
= (struct layout
*) (current
->next
+ BACKTRACE_STACK_BIAS
);
148 unwind_backtrace (backtrace_helper
, &arg
);
150 /* _Unwind_Backtrace seems to put NULL address above
151 _start. Fix it up here. */
152 if (arg
.cnt
> 1 && arg
.array
[arg
.cnt
- 1] == NULL
)
154 count
= arg
.cnt
!= -1 ? arg
.cnt
: 0;
158 weak_alias (__backtrace
, backtrace
)
159 libc_hidden_def (__backtrace
)