Blame sysdeps/powerpc/powerpc64/backtrace.c

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 Library General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2 of the
Packit 6c4009
   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
   Library General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Library General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
#include <execinfo.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
            | cr save        |        | cr save	  |
Packit 6c4009
            |                |        |                 |
Packit 6c4009
            | (unused)       |        | return address  |
Packit 6c4009
            +----------------+        +-----------------+
Packit 6c4009
*/
Packit 6c4009
struct layout
Packit 6c4009
{
Packit 6c4009
  struct layout *next;
Packit 6c4009
  long int condition_register;
Packit 6c4009
  void *return_address;
Packit 6c4009
};
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_64 {
Packit 6c4009
#define SIGNAL_FRAMESIZE 128
Packit 6c4009
  char dummy[SIGNAL_FRAMESIZE];
Packit 6c4009
  ucontext_t uc;
Packit 6c4009
  /* We don't care about the rest, since the IP value is at 'uc' 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 (sigtramp_rt64))
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 ("ld %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
      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_64 *sigframe = (struct signal_frame_64*) current;
Packit Service c487ed
	  if (count + 1 == size)
Packit Service c487ed
	    break;
Packit 6c4009
          array[++count] = (void*) sigframe->uc.uc_mcontext.gp_regs[PT_NIP];
Packit 6c4009
	  current = (void*) sigframe->uc.uc_mcontext.gp_regs[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)