forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrace.cc
144 lines (122 loc) · 3.68 KB
/
backtrace.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// 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 <csignal>
#include <sstream>
#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/paths.h"
#include "third_party/abseil-cpp/absl/debugging/symbolize.h"
#ifdef FML_OS_WIN
#include <Windows.h>
#include <crtdbg.h>
#include <debugapi.h>
#else // FML_OS_WIN
#include <execinfo.h>
#endif // FML_OS_WIN
namespace fml {
static std::string kKUnknownFrameName = "Unknown";
static std::string GetSymbolName(void* symbol) {
char name[1024];
if (!absl::Symbolize(symbol, name, sizeof(name))) {
return kKUnknownFrameName;
}
return name;
}
static int Backtrace(void** symbols, int size) {
#if FML_OS_WIN
return CaptureStackBackTrace(0, size, symbols, NULL);
#else
return ::backtrace(symbols, size);
#endif // FML_OS_WIN
}
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 "";
}
// Exclude here.
offset += 2;
std::stringstream stream;
for (int i = offset; i < available_frames; ++i) {
stream << "Frame " << i - offset << ": " << symbols[i] << " "
<< GetSymbolName(symbols[i]) << std::endl;
}
return stream.str();
}
static size_t kKnownSignalHandlers[] = {
SIGABRT, // abort program
SIGFPE, // floating-point exception
SIGTERM, // software termination signal
SIGSEGV, // segmentation violation
#if !FML_OS_WIN
SIGBUS, // bus error
SIGSYS, // non-existent system call invoked
SIGPIPE, // write on a pipe with no reader
SIGALRM, // real-time timer expired
#endif // !FML_OS_WIN
};
static std::string SignalNameToString(int signal) {
switch (signal) {
case SIGABRT:
return "SIGABRT";
case SIGFPE:
return "SIGFPE";
case SIGSEGV:
return "SIGSEGV";
case SIGTERM:
return "SIGTERM";
#if !FML_OS_WIN
case SIGBUS:
return "SIGBUS";
case SIGSYS:
return "SIGSYS";
case SIGPIPE:
return "SIGPIPE";
case SIGALRM:
return "SIGALRM";
#endif // !FML_OS_WIN
};
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() {
#if FML_OS_WIN
if (!IsDebuggerPresent()) {
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
}
#endif
auto exe_path = fml::paths::GetExecutablePath();
if (exe_path.first) {
absl::InitializeSymbolizer(exe_path.second.c_str());
}
ToggleSignalHandlers(true);
}
bool IsCrashHandlingSupported() {
return true;
}
} // namespace fml