|
Packit |
6c4009 |
/* Return backtrace of current program state.
|
|
Packit |
6c4009 |
Copyright (C) 2008-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Kazu Hirata <kazu@codesourcery.com>, 2008.
|
|
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 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef SHARED
|
|
Packit |
6c4009 |
static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
|
|
Packit |
6c4009 |
static _Unwind_VRS_Result (*unwind_vrs_get) (_Unwind_Context *,
|
|
Packit |
6c4009 |
_Unwind_VRS_RegClass,
|
|
Packit |
6c4009 |
_uw,
|
|
Packit |
6c4009 |
_Unwind_VRS_DataRepresentation,
|
|
Packit |
6c4009 |
void *);
|
|
Packit |
6c4009 |
|
|
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.1");
|
|
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_vrs_get = __libc_dlsym (libgcc_handle, "_Unwind_VRS_Get");
|
|
Packit |
6c4009 |
if (unwind_vrs_get == NULL)
|
|
Packit |
6c4009 |
unwind_backtrace = NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This function is identical to "_Unwind_GetGR", except that it uses
|
|
Packit |
6c4009 |
"unwind_vrs_get" instead of "_Unwind_VRS_Get". */
|
|
Packit |
6c4009 |
static inline _Unwind_Word
|
|
Packit |
6c4009 |
unwind_getgr (_Unwind_Context *context, int regno)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
_uw val;
|
|
Packit |
6c4009 |
unwind_vrs_get (context, _UVRSC_CORE, regno, _UVRSD_UINT32, &val;;
|
|
Packit |
6c4009 |
return val;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This macro is identical to the _Unwind_GetIP macro, except that it
|
|
Packit |
6c4009 |
uses "unwind_getgr" instead of "_Unwind_GetGR". */
|
|
Packit |
6c4009 |
# define unwind_getip(context) \
|
|
Packit |
6c4009 |
(unwind_getgr (context, 15) & ~(_Unwind_Word)1)
|
|
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 |
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 |
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
|