Blame debug/backtrace.c

Packit 6c4009
/* Return backtrace of current program state.
Packit 6c4009
   Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 <gnu/lib-names.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
  _Unwind_Word cfa;
Packit 6c4009
  int cnt;
Packit 6c4009
  int 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
static _Unwind_Word (*unwind_getcfa) (struct _Unwind_Context *);
Packit 6c4009
static void *libgcc_handle;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Dummy version in case libgcc_s does not contain the real code.  */
Packit 6c4009
static _Unwind_Word
Packit 6c4009
dummy_getcfa (struct _Unwind_Context *ctx __attribute__ ((unused)))
Packit 6c4009
{
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
init (void)
Packit 6c4009
{
Packit 6c4009
  libgcc_handle = __libc_dlopen (LIBGCC_S_SO);
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
  if (unwind_getip == NULL)
Packit 6c4009
    unwind_backtrace = NULL;
Packit 6c4009
  unwind_getcfa = (__libc_dlsym (libgcc_handle, "_Unwind_GetCFA")
Packit 6c4009
		  ?: dummy_getcfa);
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
#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
    {
Packit 6c4009
      arg->array[arg->cnt] = (void *) unwind_getip (ctx);
Packit 6c4009
Packit 6c4009
      /* Check whether we make any progress.  */
Packit 6c4009
      _Unwind_Word cfa = unwind_getcfa (ctx);
Packit 6c4009
Packit 6c4009
      if (arg->cnt > 0 && arg->array[arg->cnt - 1] == arg->array[arg->cnt]
Packit 6c4009
	 && cfa == arg->cfa)
Packit 6c4009
       return _URC_END_OF_STACK;
Packit 6c4009
      arg->cfa = cfa;
Packit 6c4009
    }
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, .cfa = 0, .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
  /* _Unwind_Backtrace seems to put NULL address above
Packit 6c4009
     _start.  Fix it up here.  */
Packit 6c4009
  if (arg.cnt > 1 && arg.array[arg.cnt - 1] == NULL)
Packit 6c4009
    --arg.cnt;
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