implemented stacktrace capturing via libgcc's C++ ABI function

Particularly _Unwind_Backtrace which seems to be gcc extension.

This is what glibc's backtrace is commonly is using.

Using _Unwind_Backtrace directly is better than glibc's backtrace, since
it doesn't call into dlopen. While glibc does dlopen when it is built as
shared library apparently to avoid link-time dependency on libgcc_s.so
This commit is contained in:
Aliaksey Kandratsenka 2016-02-20 20:33:27 -08:00
parent 728cbe1021
commit 9095ed0840
4 changed files with 145 additions and 5 deletions

View File

@ -350,6 +350,7 @@ if WITH_STACK_TRACE
### The header files we use. We divide into categories based on directory
S_STACKTRACE_INCLUDES = src/stacktrace_impl_setup-inl.h \
src/stacktrace_generic-inl.h \
src/stacktrace_libgcc-inl.h \
src/stacktrace_libunwind-inl.h \
src/stacktrace_arm-inl.h \
src/stacktrace_powerpc-inl.h \

View File

@ -380,6 +380,18 @@ AS_IF([test "x$enable_sized_delete" = xyes -o "x$enable_dyn_sized_delete" = xyes
AM_CONDITIONAL(HAVE_SIZED_DEALLOCATION,
test "$perftools_cv_sized_deallocation_result" = yes)
AC_CACHE_CHECK([if target has _Unwind_Backtrace],
[perftools_cv_have_unwind_backtrace],
[AC_LANG_PUSH(C++)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
[[#include <unwind.h>]],
[[&_Unwind_Backtrace]])],
[perftools_cv_have_unwind_backtrace=yes],
[perftools_cv_have_unwind_backtrace=no])
AC_LANG_POP(C++)])
AS_IF([test "x$perftools_cv_have_unwind_backtrace" = xyes],
[AC_DEFINE(HAVE_UNWIND_BACKTRACE, 1, [Whether <unwind.h> contains _Unwind_Backtrace])])
# Defines PRIuS
AC_COMPILER_CHARACTERISTICS
@ -595,6 +607,8 @@ AC_CONFIG_FILES([Makefile
AC_OUTPUT
AS_IF([test "$x86_no_fp_by_default" = yes && test "x$enable_frame_pointers" != xyes && test "x$UNWIND_LIBS" = x && test "x$enable_minimal" != xyes],
[AS_IF([test "x$enable_backtrace" = xyes],
[AC_MSG_WARN([No frame pointers and no libunwind. Expect backtrace capturing and unittests to fail])],
[AC_MSG_FAILURE([No frame pointers and no libunwind. The compilation will fail])])])
[AS_IF([test "x$perftools_cv_have_unwind_backtrace" = xyes],
[AC_MSG_WARN([No frame pointers and no libunwind. Using experimental backtrace capturing via libgcc])],
[AS_IF([test "x$enable_backtrace" = xyes],
[AC_MSG_WARN([No frame pointers and no libunwind. Expect backtrace capturing and unittests to fail])],
[AC_MSG_FAILURE([No frame pointers and no libunwind. The compilation will fail])])])])

View File

@ -90,6 +90,15 @@ struct GetStackImplementation {
#define HAVE_GST_generic
#endif
#ifdef HAVE_UNWIND_BACKTRACE
#define STACKTRACE_INL_HEADER "stacktrace_libgcc-inl.h"
#define GST_SUFFIX libgcc
#include "stacktrace_impl_setup-inl.h"
#undef GST_SUFFIX
#undef STACKTRACE_INL_HEADER
#define HAVE_GST_libgcc
#endif
// libunwind uses __thread so we check for both libunwind.h and
// __thread support
#if defined(HAVE_LIBUNWIND_H) && defined(HAVE_TLS)
@ -153,6 +162,9 @@ struct GetStackImplementation {
#endif
static GetStackImplementation *all_impls[] = {
#ifdef HAVE_GST_libgcc
&impl__libgcc,
#endif
#ifdef HAVE_GST_generic
&impl__generic,
#endif
@ -195,10 +207,12 @@ static GetStackImplementation *get_stack_impl = &impl__x86;
static GetStackImplementation *get_stack_impl = &impl__ppc;
#elif defined(HAVE_GST_libunwind)
static GetStackImplementation *get_stack_impl = &impl__libunwind;
#elif defined(HAVE_GST_arm)
static GetStackImplementation *get_stack_impl = &impl__arm;
#elif defined(HAVE_GST_libgcc)
static GetStackImplementation *get_stack_impl = &impl__libgcc;
#elif defined(HAVE_GST_generic)
static GetStackImplementation *get_stack_impl = &impl__generic;
#elif defined(HAVE_GST_arm)
static GetStackImplementation *get_stack_impl = &impl__arm;
#elif 0
// This is for the benefit of code analysis tools that may have
// trouble with the computed #include above.

111
src/stacktrace_libgcc-inl.h Normal file
View File

@ -0,0 +1,111 @@
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
// Copyright (c) 2016, gperftools Contributors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This file implements backtrace capturing via libgcc's
// _Unwind_Backtrace. This generally works almost always. It will fail
// sometimes when we're trying to capture backtrace from signal
// handler (i.e. in cpu profiler) while some C++ code is throwing
// exception.
#ifndef BASE_STACKTRACE_LIBGCC_INL_H_
#define BASE_STACKTRACE_LIBGCC_INL_H_
// Note: this file is included into stacktrace.cc more than once.
// Anything that should only be defined once should be here:
extern "C" {
#include <assert.h>
#include <string.h> // for memset()
}
#include <unwind.h>
#include "gperftools/stacktrace.h"
struct libgcc_backtrace_data {
void **array;
int skip;
int pos;
int limit;
};
static _Unwind_Reason_Code libgcc_backtrace_helper(struct _Unwind_Context *ctx,
void *_data) {
libgcc_backtrace_data *data =
reinterpret_cast<libgcc_backtrace_data *>(_data);
if (data->skip > 0) {
data->skip--;
return _URC_NO_REASON;
}
if (data->pos < data->limit) {
void *ip = reinterpret_cast<void *>(_Unwind_GetIP(ctx));;
data->array[data->pos++] = ip;
}
return _URC_NO_REASON;
}
#endif // BASE_STACKTRACE_LIBGCC_INL_H_
// Note: this part of the file is included several times.
// Do not put globals below.
// The following 4 functions are generated from the code below:
// GetStack{Trace,Frames}()
// GetStack{Trace,Frames}WithContext()
//
// These functions take the following args:
// void** result: the stack-trace, as an array
// int* sizes: the size of each stack frame, as an array
// (GetStackFrames* only)
// int max_depth: the size of the result (and sizes) array(s)
// int skip_count: how many stack pointers to skip before storing in result
// void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only)
static int GET_STACK_TRACE_OR_FRAMES {
libgcc_backtrace_data data;
data.array = result;
// we're also skipping current and parent's frame
data.skip = skip_count + 2;
data.pos = 0;
data.limit = max_depth;
_Unwind_Backtrace(libgcc_backtrace_helper, &data);
if (data.pos > 1 && data.array[data.pos - 1] == NULL)
--data.pos;
#if IS_STACK_FRAMES
// No implementation for finding out the stack frame sizes.
memset(sizes, 0, sizeof(*sizes) * data.pos);
#endif
return data.pos;
}