Blame sysdeps/m68k/backtrace.c

Packit 6c4009
/* Return backtrace of current program state.
Packit 6c4009
   Copyright (C) 2013-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 <libc-lock.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <execinfo.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unwind.h>
Packit 6c4009
Packit 6c4009
struct trace_arg
Packit 6c4009
{
Packit 6c4009
  void **array;
Packit 6c4009
  int cnt, size;
Packit 6c4009
  void *lastfp, *lastsp;
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
static _Unwind_Ptr (*unwind_getcfa) (struct _Unwind_Context *);
Packit 6c4009
static _Unwind_Ptr (*unwind_getgr) (struct _Unwind_Context *, int);
Packit 6c4009
static void *libgcc_handle;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
init (void)
Packit 6c4009
{
Packit 6c4009
  libgcc_handle = __libc_dlopen ("libgcc_s.so.2");
Packit 6c4009
Packit 6c4009
  if (libgcc_handle == NULL)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
Packit 6c4009
  unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
Packit 6c4009
  unwind_getcfa = __libc_dlsym (libgcc_handle, "_Unwind_GetCFA");
Packit 6c4009
  unwind_getgr = __libc_dlsym (libgcc_handle, "_Unwind_GetGR");
Packit 6c4009
  if (unwind_getip == NULL || unwind_getgr == NULL || unwind_getcfa == NULL)
Packit 6c4009
    {
Packit 6c4009
      unwind_backtrace = NULL;
Packit 6c4009
      __libc_dlclose (libgcc_handle);
Packit 6c4009
      libgcc_handle = NULL;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#else
Packit 6c4009
# define unwind_backtrace _Unwind_Backtrace
Packit 6c4009
# define unwind_getip _Unwind_GetIP
Packit 6c4009
# define unwind_getcfa _Unwind_GetCFA
Packit 6c4009
# define unwind_getgr _Unwind_GetGR
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
Packit 6c4009
  /* %fp is DWARF2 register 14 on M68K.  */
Packit 6c4009
  arg->lastfp = (void *) unwind_getgr (ctx, 14);
Packit 6c4009
  arg->lastsp = (void *) unwind_getcfa (ctx);
Packit 6c4009
  return _URC_NO_REASON;
Packit 6c4009
}
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 with every stack frame
Packit 6c4009
   if not compiled without frame pointer.
Packit 6c4009
Packit 6c4009
           +-----------------+       +-----------------+
Packit 6c4009
    %fp -> | %fp last frame--------> | %fp last frame--->...
Packit 6c4009
           |                 |       |                 |
Packit 6c4009
           | return address  |       | return address  |
Packit 6c4009
           +-----------------+       +-----------------+
Packit 6c4009
Packit 6c4009
   First try as far to get as far as possible using
Packit 6c4009
   _Unwind_Backtrace which handles -fomit-frame-pointer
Packit 6c4009
   as well, but requires .eh_frame info.  Then fall back to
Packit 6c4009
   walking the stack manually.  */
Packit 6c4009
Packit 6c4009
struct layout
Packit 6c4009
{
Packit 6c4009
  struct layout *fp;
Packit 6c4009
  void *ret;
Packit 6c4009
};
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
  if (unwind_backtrace == NULL)
Packit 6c4009
    return 0;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  unwind_backtrace (backtrace_helper, &arg;;
Packit 6c4009
Packit 6c4009
  if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
Packit 6c4009
    --arg.cnt;
Packit 6c4009
  else if (arg.cnt < size)
Packit 6c4009
    {
Packit 6c4009
      struct layout *fp = (struct layout *) arg.lastfp;
Packit 6c4009
Packit 6c4009
      while (arg.cnt < size)
Packit 6c4009
	{
Packit 6c4009
	  /* Check for out of range.  */
Packit 6c4009
	  if ((void *) fp < arg.lastsp || (void *) fp > __libc_stack_end
Packit 6c4009
	      || ((long) fp & 1))
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  array[arg.cnt++] = fp->ret;
Packit 6c4009
	  fp = fp->fp;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return arg.cnt != -1 ? arg.cnt : 0;
Packit 6c4009
}
Packit 6c4009
weak_alias (__backtrace, backtrace)
Packit 6c4009
libc_hidden_def (__backtrace)
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
/* Free all resources if necessary.  */
Packit 6c4009
libc_freeres_fn (free_mem)
Packit 6c4009
{
Packit 6c4009
  unwind_backtrace = NULL;
Packit 6c4009
  if (libgcc_handle != NULL)
Packit 6c4009
    {
Packit 6c4009
      __libc_dlclose (libgcc_handle);
Packit 6c4009
      libgcc_handle = NULL;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#endif