Blame sysdeps/s390/s390-64/backtrace.c

Packit 6c4009
/* Return backtrace of current program state.  64 bit S/390 version.
Packit 6c4009
   Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>.
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 <libc-lock.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <execinfo.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unwind.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This is a global variable set at program start time.  It marks the
Packit 6c4009
   highest used stack address.  */
Packit 6c4009
extern void *__libc_stack_end;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This is the stack layout we see for every non-leaf function.
Packit 6c4009
           size                    offset
Packit 6c4009
    %r15 ->    +------------------+
Packit 6c4009
             8 | back chain       |   0
Packit 6c4009
             8 | end of stack     |   8
Packit 6c4009
            32 | scratch          |  16
Packit 6c4009
            80 | save area r6-r15 |  48
Packit 6c4009
            16 | save area f4,f6  | 128
Packit 6c4009
            16 | empty            | 144
Packit 6c4009
               +------------------+
Packit 6c4009
   r14 in the save area holds the return address.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
struct layout
Packit 6c4009
{
Packit 6c4009
  long back_chain;
Packit 6c4009
  long end_of_stack;
Packit 6c4009
  long scratch[4];
Packit 6c4009
  long save_grps[10];
Packit 6c4009
  long save_fp[2];
Packit 6c4009
  long empty[2];
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
struct trace_arg
Packit 6c4009
{
Packit 6c4009
  void **array;
Packit 6c4009
  int cnt, size;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
Packit 6c4009
static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
init (void)
Packit 6c4009
{
Packit 6c4009
  void *handle = __libc_dlopen ("libgcc_s.so.1");
Packit 6c4009
Packit 6c4009
  if (handle == NULL)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  unwind_backtrace = __libc_dlsym (handle, "_Unwind_Backtrace");
Packit 6c4009
  unwind_getip = __libc_dlsym (handle, "_Unwind_GetIP");
Packit 6c4009
  if (unwind_getip == NULL)
Packit 6c4009
    unwind_backtrace = NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
__backchain_backtrace (void **array, int size)
Packit 6c4009
{
Packit 6c4009
  /* We assume that all the code is generated with frame pointers set.  */
Packit 6c4009
  struct layout *stack;
Packit 6c4009
  int cnt = 0;
Packit 6c4009
Packit 6c4009
  __asm__ ("LGR  %0,%%r15" : "=d" (stack) );
Packit 6c4009
  /* We skip the call to this function, it makes no sense to record it.  */
Packit 6c4009
  stack = (struct layout *) stack->back_chain;
Packit 6c4009
  while (cnt < size)
Packit 6c4009
    {
Packit 6c4009
      if (stack == NULL || (void *) stack > __libc_stack_end)
Packit 6c4009
	/* This means the address is out of range.  Note that for the
Packit 6c4009
	   toplevel we see a frame pointer with value NULL which clearly is
Packit 6c4009
	   out of range.  */
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      array[cnt++] = (void *) stack->save_grps[8];
Packit 6c4009
Packit 6c4009
      stack = (struct layout *) stack->back_chain;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return cnt;
Packit 6c4009
}
Packit 6c4009
#else
Packit 6c4009
# define unwind_backtrace _Unwind_Backtrace
Packit 6c4009
# define unwind_getip _Unwind_GetIP
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static _Unwind_Reason_Code
Packit 6c4009
backtrace_helper (struct _Unwind_Context *ctx, void *a)
Packit 6c4009
{
Packit 6c4009
  struct trace_arg *arg = a;
Packit 6c4009
Packit 6c4009
  /* We are first called with address in the __backtrace function.
Packit 6c4009
     Skip it.  */
Packit 6c4009
  if (arg->cnt != -1)
Packit 6c4009
    arg->array[arg->cnt] = (void *) unwind_getip (ctx);
Packit 6c4009
  if (++arg->cnt == arg->size)
Packit 6c4009
    return _URC_END_OF_STACK;
Packit 6c4009
  return _URC_NO_REASON;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__backtrace (void **array, int size)
Packit 6c4009
{
Packit 6c4009
  struct trace_arg arg = { .array = array, .size = size, .cnt = -1 };
Packit 6c4009
Packit 6c4009
  if (size <= 0)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
  __libc_once_define (static, once);
Packit 6c4009
Packit 6c4009
  __libc_once (once, init);
Packit 6c4009
Packit 6c4009
  if (unwind_backtrace == NULL)
Packit 6c4009
    return __backchain_backtrace (array, size);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  unwind_backtrace (backtrace_helper, &arg;;
Packit 6c4009
Packit 6c4009
  return arg.cnt != -1 ? arg.cnt : 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__backtrace, backtrace)
Packit 6c4009
libc_hidden_def (__backtrace)