Skip to content

Commit

Permalink
support stack unwind on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
huangqinjin authored Oct 8, 2021
1 parent acda903 commit 3965584
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 34 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/android-builds.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Android

on: [push, pull_request]

jobs:
build:
name: NDK-C++${{matrix.std}}-${{matrix.abi}}-${{matrix.build_type}}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
std: [98, 11, 14, 17, 20]
abi: [arm64-v8a, armeabi-v7a, x86_64, x86]
build_type: [Debug]

steps:
- uses: actions/checkout@v2

- name: Setup Ninja
uses: ashutoshvarma/setup-ninja@master
with:
version: 1.10.0

- name: Configure
shell: bash
run: |
cmake -S . -B ${{runner.workspace}}/build_CXX${{matrix.std}}-${{matrix.abi}} \
-G "Ninja Multi-Config" \
-DCMAKE_CXX_EXTENSIONS=OFF \
-DCMAKE_CXX_STANDARD=${{matrix.std}} \
-DCMAKE_CXX_STANDARD_REQUIRED=ON \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
-DANDROID_STL=c++_shared \
-DANDROID_NATIVE_API_LEVEL=28 \
-DANDROID_ABI=${{matrix.abi}} \
- name: Build
run: |
cmake --build ${{runner.workspace}}/build_CXX${{matrix.std}}-${{matrix.abi}} \
--config ${{matrix.build_type}}
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ find_package (Unwind)

if (Unwind_FOUND)
set (HAVE_LIB_UNWIND 1)
set (HAVE_UNWIND_H 1)
else (Unwind_FOUND)
check_include_file_cxx (unwind.h HAVE_UNWIND_H)
endif (Unwind_FOUND)

check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
Expand Down
2 changes: 1 addition & 1 deletion bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
"-DHAVE_CXX11_NULLPTR_T",
"-DHAVE_STDINT_H",
"-DHAVE_STRING_H",
"-DHAVE_UNWIND_H",
"-DGLOG_CUSTOM_PREFIX_SUPPORT",
"-I%s/glog_internal" % gendir,
] + (["-DHAVE_LIB_GFLAGS"] if with_gflags else [])
Expand All @@ -70,6 +69,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
"-DHAVE_SYS_UTSNAME_H",
# For src/utilities.cc.
"-DHAVE_SYS_TIME_H",
"-DHAVE_UNWIND_H",
# Enable dumping stacktrace upon sigaction.
"-DHAVE_SIGACTION",
# For logging.cc.
Expand Down
7 changes: 2 additions & 5 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
/* Define to 1 if you have the `pthread' library (-lpthread). */
#cmakedefine HAVE_LIBPTHREAD

/* Define to 1 if you have the <libunwind.h> header file. */
#cmakedefine HAVE_LIBUNWIND_H

/* define if you have google gflags library */
#cmakedefine HAVE_LIB_GFLAGS

Expand Down Expand Up @@ -115,8 +112,8 @@
/* Define to 1 if you have the <unistd.h> header file. */
#cmakedefine HAVE_UNISTD_H ${HAVE_UNISTD_H}

/* Define to 1 if you have the <unwind.h> header file. */
#cmakedefine HAVE_UNWIND_H ${HAVE_UNWIND_H}
/* Define if you have the <unwind.h> header file. */
#cmakedefine HAVE_UNWIND_H

/* define if the compiler supports using expression for operator */
#cmakedefine HAVE_USING_OPERATOR
Expand Down
34 changes: 10 additions & 24 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,11 @@ static void WriteToStderr(const char* message, size_t len) {
}

inline void LogDestination::MaybeLogToStderr(LogSeverity severity,
const char* message, size_t message_len, size_t /*prefix_len*/) {
const char* message, size_t message_len, size_t prefix_len) {
if ((severity >= FLAGS_stderrthreshold) || FLAGS_alsologtostderr) {
ColoredWriteToStderr(severity, message, message_len);
#ifdef GLOG_OS_WINDOWS
(void) prefix_len;
// On Windows, also output to the debugger
::OutputDebugStringA(message);
#elif defined(__ANDROID__)
Expand All @@ -787,6 +788,8 @@ inline void LogDestination::MaybeLogToStderr(LogSeverity severity,
__android_log_write(android_log_levels[severity],
glog_internal_namespace_::ProgramInvocationShortName(),
message + prefix_len);
#else
(void) prefix_len;
#endif
}
}
Expand Down Expand Up @@ -1672,23 +1675,6 @@ ostream& LogMessage::stream() {
return data_->stream_;
}

namespace {
#if defined(__ANDROID__)
int AndroidLogLevel(const int severity) {
switch (severity) {
case 3:
return ANDROID_LOG_FATAL;
case 2:
return ANDROID_LOG_ERROR;
case 1:
return ANDROID_LOG_WARN;
default:
return ANDROID_LOG_INFO;
}
}
#endif // defined(__ANDROID__)
} // namespace

// Flush buffered message, called by the destructor, or any other function
// that needs to synchronize the log.
void LogMessage::Flush() {
Expand Down Expand Up @@ -1724,12 +1710,6 @@ void LogMessage::Flush() {
}
LogDestination::WaitForSinks(data_);

#if defined(__ANDROID__)
const int level = AndroidLogLevel((int)data_->severity_);
const std::string text = std::string(data_->message_text_);
__android_log_write(level, "native", text.substr(0,data_->num_chars_to_log_).c_str());
#endif // defined(__ANDROID__)

if (append_newline) {
// Fix the ostrstream back how it was before we screwed with it.
// It's 99.44% certain that we don't need to worry about doing this.
Expand Down Expand Up @@ -1859,6 +1839,12 @@ void LogMessage::SendToLog() EXCLUSIVE_LOCKS_REQUIRED(log_mutex) {
if (write(STDERR_FILENO, message, strlen(message)) < 0) {
// Ignore errors.
}
#if defined(__ANDROID__)
// ANDROID_LOG_FATAL as this message is of FATAL severity.
__android_log_write(ANDROID_LOG_FATAL,
glog_internal_namespace_::ProgramInvocationShortName(),
message);
#endif
Fail();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/stacktrace_x86_64-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef struct {


// Workaround for the malloc() in _Unwind_Backtrace() issue.
static _Unwind_Reason_Code nop_backtrace(struct _Unwind_Context *uc, void *opq) {
static _Unwind_Reason_Code nop_backtrace(struct _Unwind_Context */*uc*/, void */*opq*/) {
return _URC_NO_REASON;
}

Expand Down
9 changes: 9 additions & 0 deletions src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef __ANDROID__
#include <android/log.h>
#endif

#include "base/googleinit.h"

Expand Down Expand Up @@ -92,6 +95,12 @@ static void DebugWriteToStderr(const char* data, void *) {
if (write(STDERR_FILENO, data, strlen(data)) < 0) {
// Ignore errors.
}
#if defined(__ANDROID__)
// ANDROID_LOG_FATAL as fatal error occurred and now is dumping call stack.
__android_log_write(ANDROID_LOG_FATAL,
glog_internal_namespace_::ProgramInvocationShortName(),
data);
#endif
}

static void DebugWriteToString(const char* data, void *arg) {
Expand Down
4 changes: 2 additions & 2 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@

#if defined(HAVE_LIB_UNWIND)
# define STACKTRACE_H "stacktrace_libunwind-inl.h"
#elif defined(HAVE_UNWIND_H)
# define STACKTRACE_H "stacktrace_x86_64-inl.h"
#elif !defined(NO_FRAME_POINTER)
# if defined(__i386__) && __GNUC__ >= 2
# define STACKTRACE_H "stacktrace_x86-inl.h"
# elif defined(__x86_64__) && __GNUC__ >= 2 && HAVE_UNWIND_H
# define STACKTRACE_H "stacktrace_x86_64-inl.h"
# elif (defined(__ppc__) || defined(__PPC__)) && __GNUC__ >= 2
# define STACKTRACE_H "stacktrace_powerpc-inl.h"
# elif defined(GLOG_OS_WINDOWS)
Expand Down

0 comments on commit 3965584

Please sign in to comment.