Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NDK stack frame symbolication #1671

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "string.h"

#include <dlfcn.h>
#include <unwindstack/LocalUnwinder.h>
#include <unwindstack/Maps.h>
#include <unwindstack/MemoryLocal.h>
Expand Down Expand Up @@ -73,6 +74,23 @@ ssize_t bsg_unwind_crash_stack(bugsnag_stackframe stack[BUGSNAG_FRAMES_MAX],
sizeof(stack[frame_count].filename));
bsg_strncpy(stack[frame_count].method, frame.function_name.c_str(),
sizeof(stack[frame_count].method));

// if the filename or method name cannot be found - try fallback to dladdr
// to find them
static Dl_info info;
if ((frame.map_name.empty() || frame.function_name.empty()) &&
dladdr((void *)frame.pc, &info) != 0) {

if (info.dli_fname != nullptr) {
bsg_strncpy(stack[frame_count].filename, (char *)info.dli_fname,
sizeof(stack[frame_count].filename));
}
if (info.dli_sname != nullptr) {
bsg_strncpy(stack[frame_count].method, (char *)info.dli_sname,
sizeof(stack[frame_count].method));
}
}

frame_count++;
}
unwinding_crash_stack = false;
Expand All @@ -97,6 +115,22 @@ bsg_unwind_concurrent_stack(bugsnag_stackframe stack[BUGSNAG_FRAMES_MAX],
stack[frame_count].symbol_address = frame.pc - frame.map_info->offset();
bsg_strncpy(stack[frame_count].filename, frame.map_info->name().c_str(),
sizeof(stack[frame_count].filename));

// if the filename or method name cannot be found - try fallback to dladdr
// to find them
static Dl_info info;
if ((frame.map_info->name().empty() || frame.function_name.empty()) &&
dladdr((void *)frame.pc, &info) != 0) {

if (info.dli_fname != nullptr) {
bsg_strncpy(stack[frame_count].filename, (char *)info.dli_fname,
sizeof(stack[frame_count].filename));
}
if (info.dli_sname != nullptr) {
bsg_strncpy(stack[frame_count].method, (char *)info.dli_sname,
sizeof(stack[frame_count].method));
}
}
}
bsg_strncpy(stack[frame_count].method, frame.function_name.c_str(),
sizeof(stack[frame_count].method));
Expand Down
9 changes: 3 additions & 6 deletions features/fixtures/mazerunner/cxx-scenarios/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ add_library(cxx-scenarios SHARED
src/main/cpp/bugsnag-java-scenarios.cpp)

set_target_properties(cxx-scenarios PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES)
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES)

add_library(lib_monochrome SHARED IMPORTED)
set_target_properties(lib_monochrome PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libmonochrome.so)
target_link_libraries(cxx-scenarios lib_monochrome)
target_link_libraries(cxx-scenarios)

Empty file.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include <jni.h>
#include <cstdio>
#include <dlfcn.h>

extern "C" {
// defined in libs/[ABI]/libmonochrome.so
int something_innocuous(int input);
int (*something_innocuous)(int input);

JNIEXPORT int JNICALL
Java_com_bugsnag_android_mazerunner_scenarios_CXXExternalStackElementScenario_crash(
JNIEnv *env, jobject instance, jint counter) {
void *monochrome = dlopen("libmonochrome.so", RTLD_GLOBAL);
*(void**)(&something_innocuous) = dlsym(monochrome, "something_innocuous");

printf("Captain, why are we out here chasing comets?\n%d\n", counter);
int value = counter * 4;
if (counter > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
public class CXXExternalStackElementScenario extends Scenario {

static {
System.loadLibrary("monochrome");
System.loadLibrary("cxx-scenarios");
}

Expand Down
2 changes: 1 addition & 1 deletion features/full_tests/native_crash_handling.feature
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Feature: Native crash reporting
And the exception "type" equals "c"
And the first significant stack frames match:
| something_innocuous | libmonochrome.so | (ignore) |
| Java_com_bugsnag_android_mazerunner_scenarios_CXXExternalStackElementScenario_crash | CXXExternalStackElementScenario.cpp | 14 |
| Java_com_bugsnag_android_mazerunner_scenarios_CXXExternalStackElementScenario_crash | CXXExternalStackElementScenario.cpp | 18 |

Scenario: Call null function pointer
A null pointer should be the first element of a stack trace,
Expand Down