|
Packit |
6c4009 |
/* Return backtrace of current program state.
|
|
Packit |
6c4009 |
Copyright (C) 1998-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <execinfo.h>
|
|
Packit |
6c4009 |
#include <stddef.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <signal.h>
|
|
Packit |
6c4009 |
#include <libc-vdso.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This is the stack layout we see with every stack frame.
|
|
Packit |
6c4009 |
Note that every routine is required by the ABI to lay out the stack
|
|
Packit |
6c4009 |
like this.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
+----------------+ +-----------------+
|
|
Packit |
6c4009 |
%r1 -> | %r1 last frame--------> | %r1 last frame--->... --> NULL
|
|
Packit |
6c4009 |
| | | |
|
|
Packit |
6c4009 |
| (unused) | | return address |
|
|
Packit |
6c4009 |
+----------------+ +-----------------+
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
struct layout
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct layout *next;
|
|
Packit |
6c4009 |
void *return_address;
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define SIGNAL_FRAMESIZE 64
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Since the signal handler is just like any other function it needs to
|
|
Packit |
6c4009 |
save/restore its LR and it will save it into callers stack frame.
|
|
Packit |
6c4009 |
Since a signal handler doesn't have a caller, the kernel creates a
|
|
Packit |
6c4009 |
dummy frame to make it look like it has a caller. */
|
|
Packit |
6c4009 |
struct signal_frame_32 {
|
|
Packit |
6c4009 |
char dummy[SIGNAL_FRAMESIZE];
|
|
Packit |
6c4009 |
struct sigcontext sctx;
|
|
Packit |
6c4009 |
mcontext_t mctx;
|
|
Packit |
6c4009 |
/* We don't care about the rest, since IP value is at 'mctx' field. */
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline int
|
|
Packit |
6c4009 |
is_sigtramp_address (void *nip)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
#ifdef SHARED
|
|
Packit |
6c4009 |
if (nip == VDSO_SYMBOL (sigtramp32))
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct rt_signal_frame_32 {
|
|
Packit |
6c4009 |
char dummy[SIGNAL_FRAMESIZE + 16];
|
|
Packit |
6c4009 |
siginfo_t info;
|
|
Packit |
6c4009 |
ucontext_t uc;
|
|
Packit |
6c4009 |
/* We don't care about the rest, since IP value is at 'uc' field. */
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline int
|
|
Packit |
6c4009 |
is_sigtramp_address_rt (void * nip)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
#ifdef SHARED
|
|
Packit |
6c4009 |
if (nip == VDSO_SYMBOL (sigtramp_rt32))
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__backtrace (void **array, int size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct layout *current;
|
|
Packit |
6c4009 |
int count;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Force gcc to spill LR. */
|
|
Packit |
6c4009 |
asm volatile ("" : "=l"(current));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the address on top-of-stack. */
|
|
Packit |
6c4009 |
asm volatile ("lwz %0,0(1)" : "=r"(current));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for ( count = 0;
|
|
Packit |
6c4009 |
current != NULL && count < size;
|
|
Packit |
6c4009 |
current = current->next, count++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
gregset_t *gregset = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
array[count] = current->return_address;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check if the symbol is the signal trampoline and get the interrupted
|
|
Packit |
6c4009 |
* symbol address from the trampoline saved area. */
|
|
Packit |
6c4009 |
if (is_sigtramp_address (current->return_address))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct signal_frame_32 *sigframe =
|
|
Packit |
6c4009 |
(struct signal_frame_32*) current;
|
|
Packit |
6c4009 |
gregset = &sigframe->mctx.gregs;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (is_sigtramp_address_rt (current->return_address))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct rt_signal_frame_32 *sigframe =
|
|
Packit |
6c4009 |
(struct rt_signal_frame_32*) current;
|
|
Packit |
6c4009 |
gregset = &sigframe->uc.uc_mcontext.uc_regs->gregs;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (gregset)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
array[++count] = (void*)((*gregset)[PT_NIP]);
|
|
Packit |
6c4009 |
current = (void*)((*gregset)[PT_R1]);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* It's possible the second-last stack frame can't return
|
|
Packit |
6c4009 |
(that is, it's __libc_start_main), in which case
|
|
Packit |
6c4009 |
the CRT startup code will have set its LR to 'NULL'. */
|
|
Packit |
6c4009 |
if (count > 0 && array[count-1] == NULL)
|
|
Packit |
6c4009 |
count--;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return count;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
weak_alias (__backtrace, backtrace)
|
|
Packit |
6c4009 |
libc_hidden_def (__backtrace)
|