forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gather demangled stack traces and report the same to console on crash…
…es. (flutter#16450) These should only be used on host binaries for more detailed crash reports. Installing the handler on targets (iOS/Android) may cause use to break existing crash reporting mechanisms users may have installed themselves in the process. This should work on Darwin & Linux for now. Doing something like int* a = nullptr; *a = 12; or abort or tripping an assertion should print something the following before program termination. We can tweak the report further if necessary. ``` [ERROR:flutter/fml/backtrace.cc(110)] Caught signal SIGSEGV during program execution. Frame 0: 0x10658342c void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) Frame 1: 0x106555070 void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) Frame 2: 0x106554f81 testing::Test::Run() Frame 3: 0x106555dc3 testing::TestInfo::Run() Frame 4: 0x1065570a1 testing::TestSuite::Run() Frame 5: 0x106562a55 testing::internal::UnitTestImpl::RunAllTests() Frame 6: 0x10658c22c bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) Frame 7: 0x1065625c3 bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) Frame 8: 0x106562445 testing::UnitTest::Run() Frame 9: 0x105c8dc33 RUN_ALL_TESTS() Frame 10: 0x105c8dbe6 main Frame 11: 0x7fff7c2dc3d5 start ``` Known issue: This routines that generate the stack trace are not signal safe. But since we only use the same before the process is terminating, this ought to be fine. I’ll work in a separate patch to convert all the internals to be signal safe. In the meantime, this will help us better identify the causes of flakes on our bots. Fixes flutter/flutter#50244
- Loading branch information
1 parent
17e07c5
commit 7c2d975
Showing
8 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/backtrace.h" | ||
|
||
#include <cxxabi.h> | ||
#include <sstream> | ||
|
||
#include <dlfcn.h> | ||
#include <execinfo.h> | ||
#include <signal.h> | ||
|
||
#include "flutter/fml/logging.h" | ||
|
||
namespace fml { | ||
|
||
static std::string kKUnknownFrameName = "Unknown"; | ||
|
||
static std::string DemangleSymbolName(const std::string& mangled) { | ||
if (mangled == kKUnknownFrameName) { | ||
return kKUnknownFrameName; | ||
} | ||
|
||
int status = 0; | ||
size_t length = 0; | ||
char* demangled = __cxxabiv1::__cxa_demangle( | ||
mangled.data(), // mangled name | ||
nullptr, // output buffer (malloc-ed if nullptr) | ||
&length, // demangled length | ||
&status); | ||
|
||
if (demangled == nullptr || status != 0) { | ||
return mangled; | ||
} | ||
|
||
auto demangled_string = std::string{demangled, length}; | ||
free(demangled); | ||
return demangled_string; | ||
} | ||
|
||
static std::string GetSymbolName(void* symbol) { | ||
Dl_info info = {}; | ||
|
||
if (::dladdr(symbol, &info) == 0) { | ||
return kKUnknownFrameName; | ||
} | ||
|
||
return DemangleSymbolName({info.dli_sname}); | ||
} | ||
|
||
std::string BacktraceHere(size_t offset) { | ||
constexpr size_t kMaxFrames = 256; | ||
void* symbols[kMaxFrames]; | ||
const auto available_frames = ::backtrace(symbols, kMaxFrames); | ||
if (available_frames <= 0) { | ||
return ""; | ||
} | ||
|
||
std::stringstream stream; | ||
for (int i = 1 + offset; i < available_frames; ++i) { | ||
stream << "Frame " << i - 1 - offset << ": " << symbols[i] << " " | ||
<< GetSymbolName(symbols[i]) << std::endl; | ||
} | ||
return stream.str(); | ||
} | ||
|
||
static size_t kKnownSignalHandlers[] = { | ||
SIGABRT, // abort program | ||
SIGFPE, // floating-point exception | ||
SIGBUS, // bus error | ||
SIGSEGV, // segmentation violation | ||
SIGSYS, // non-existent system call invoked | ||
SIGPIPE, // write on a pipe with no reader | ||
SIGALRM, // real-time timer expired | ||
SIGTERM, // software termination signal | ||
}; | ||
|
||
static std::string SignalNameToString(int signal) { | ||
switch (signal) { | ||
case SIGABRT: | ||
return "SIGABRT"; | ||
case SIGFPE: | ||
return "SIGFPE"; | ||
case SIGBUS: | ||
return "SIGBUS"; | ||
case SIGSEGV: | ||
return "SIGSEGV"; | ||
case SIGSYS: | ||
return "SIGSYS"; | ||
case SIGPIPE: | ||
return "SIGPIPE"; | ||
case SIGALRM: | ||
return "SIGALRM"; | ||
case SIGTERM: | ||
return "SIGTERM"; | ||
}; | ||
return std::to_string(signal); | ||
} | ||
|
||
static void ToggleSignalHandlers(bool set); | ||
|
||
static void SignalHandler(int signal) { | ||
// We are a crash signal handler. This can only happen once. Since we don't | ||
// want to catch crashes while we are generating the crash reports, disable | ||
// all set signal handlers to their default values before reporting the crash | ||
// and re-raising the signal. | ||
ToggleSignalHandlers(false); | ||
|
||
FML_LOG(ERROR) << "Caught signal " << SignalNameToString(signal) | ||
<< " during program execution." << std::endl | ||
<< BacktraceHere(3); | ||
|
||
::raise(signal); | ||
} | ||
|
||
static void ToggleSignalHandlers(bool set) { | ||
for (size_t i = 0; i < sizeof(kKnownSignalHandlers) / sizeof(size_t); ++i) { | ||
auto signal_name = kKnownSignalHandlers[i]; | ||
auto handler = set ? &SignalHandler : SIG_DFL; | ||
|
||
if (::signal(signal_name, handler) == SIG_ERR) { | ||
FML_LOG(ERROR) << "Could not attach signal handler for " << signal_name; | ||
} | ||
} | ||
} | ||
|
||
void InstallCrashHandler() { | ||
ToggleSignalHandlers(true); | ||
} | ||
|
||
bool IsCrashHandlingSupported() { | ||
return true; | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_BACKTRACE_H_ | ||
#define FLUTTER_FML_BACKTRACE_H_ | ||
|
||
#include <string> | ||
|
||
#include "flutter/fml/macros.h" | ||
|
||
namespace fml { | ||
|
||
std::string BacktraceHere(size_t offset = 0); | ||
|
||
void InstallCrashHandler(); | ||
|
||
bool IsCrashHandlingSupported(); | ||
|
||
} // namespace fml | ||
|
||
#endif // FLUTTER_FML_BACKTRACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/backtrace.h" | ||
|
||
namespace fml { | ||
|
||
static std::string kKUnknownFrameName = "Unknown"; | ||
|
||
std::string BacktraceHere(size_t offset) { | ||
return ""; | ||
} | ||
|
||
void InstallCrashHandler() { | ||
// Not supported. | ||
} | ||
|
||
bool IsCrashHandlingSupported() { | ||
return false; | ||
} | ||
|
||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "backtrace.h" | ||
#include "gtest/gtest.h" | ||
#include "logging.h" | ||
|
||
namespace fml { | ||
namespace testing { | ||
|
||
TEST(BacktraceTest, CanGatherBacktrace) { | ||
if (!IsCrashHandlingSupported()) { | ||
GTEST_SKIP(); | ||
return; | ||
} | ||
{ | ||
auto trace = BacktraceHere(0); | ||
ASSERT_GT(trace.size(), 0u); | ||
ASSERT_NE(trace.find("Frame 0"), std::string::npos); | ||
} | ||
|
||
{ | ||
auto trace = BacktraceHere(1); | ||
ASSERT_GT(trace.size(), 0u); | ||
ASSERT_NE(trace.find("Frame 0"), std::string::npos); | ||
} | ||
|
||
{ | ||
auto trace = BacktraceHere(2); | ||
ASSERT_GT(trace.size(), 0u); | ||
ASSERT_NE(trace.find("Frame 0"), std::string::npos); | ||
} | ||
} | ||
|
||
} // namespace testing | ||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters