Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7c84844

Browse files
authored
Add Windows support for //flutter/fml/backtrace.h (#36202)
1 parent fd4b90d commit 7c84844

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

fml/BUILD.gn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ source_set("fml") {
9696
"wakeable.h",
9797
]
9898

99-
if (is_mac || is_linux || (is_ios && is_debug)) {
99+
if (is_mac || is_linux || is_win || (is_ios && is_debug)) {
100100
sources += [ "backtrace.cc" ]
101101
} else {
102102
sources += [ "backtrace_stub.cc" ]
@@ -115,7 +115,7 @@ source_set("fml") {
115115
"//third_party/icu",
116116
]
117117

118-
if (is_mac || is_linux || (is_ios && is_debug)) {
118+
if (is_mac || is_linux || is_win || (is_ios && is_debug)) {
119119
# This abseil dependency is only used by backtrace.cc.
120120
deps += [ "//third_party/abseil-cpp/absl/debugging:symbolize" ]
121121
}

fml/backtrace.cc

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,56 @@
44

55
#include "flutter/fml/backtrace.h"
66

7-
#include <cxxabi.h>
8-
#include <dlfcn.h>
9-
#include <execinfo.h>
10-
117
#include <csignal>
128
#include <sstream>
139

14-
#if FML_OS_WIN
15-
#include <crtdbg.h>
16-
#include <debugapi.h>
17-
#endif
18-
10+
#include "flutter/fml/build_config.h"
1911
#include "flutter/fml/logging.h"
2012
#include "flutter/fml/paths.h"
2113
#include "third_party/abseil-cpp/absl/debugging/symbolize.h"
2214

15+
#ifdef FML_OS_WIN
16+
#include <Windows.h>
17+
#include <crtdbg.h>
18+
#include <debugapi.h>
19+
#else // FML_OS_WIN
20+
#include <execinfo.h>
21+
#endif // FML_OS_WIN
22+
2323
namespace fml {
2424

2525
static std::string kKUnknownFrameName = "Unknown";
2626

27-
static std::string DemangleSymbolName(const std::string& mangled) {
28-
if (mangled == kKUnknownFrameName) {
29-
return kKUnknownFrameName;
30-
}
31-
32-
int status = 0;
33-
size_t length = 0;
34-
char* demangled = __cxxabiv1::__cxa_demangle(
35-
mangled.data(), // mangled name
36-
nullptr, // output buffer (malloc-ed if nullptr)
37-
&length, // demangled length
38-
&status);
39-
40-
if (demangled == nullptr || status != 0) {
41-
return mangled;
42-
}
43-
44-
auto demangled_string = std::string{demangled, length};
45-
free(demangled);
46-
return demangled_string;
47-
}
48-
4927
static std::string GetSymbolName(void* symbol) {
5028
char name[1024];
5129
if (!absl::Symbolize(symbol, name, sizeof(name))) {
5230
return kKUnknownFrameName;
5331
}
32+
return name;
33+
}
5434

55-
return DemangleSymbolName({name});
35+
static int Backtrace(void** symbols, int size) {
36+
#if FML_OS_WIN
37+
return CaptureStackBackTrace(0, size, symbols, NULL);
38+
#else
39+
return ::backtrace(symbols, size);
40+
#endif // FML_OS_WIN
5641
}
5742

5843
std::string BacktraceHere(size_t offset) {
5944
constexpr size_t kMaxFrames = 256;
6045
void* symbols[kMaxFrames];
61-
const auto available_frames = ::backtrace(symbols, kMaxFrames);
46+
const auto available_frames = Backtrace(symbols, kMaxFrames);
6247
if (available_frames <= 0) {
6348
return "";
6449
}
6550

51+
// Exclude here.
52+
offset += 2;
53+
6654
std::stringstream stream;
67-
for (int i = 1 + offset; i < available_frames; ++i) {
68-
stream << "Frame " << i - 1 - offset << ": " << symbols[i] << " "
55+
for (int i = offset; i < available_frames; ++i) {
56+
stream << "Frame " << i - offset << ": " << symbols[i] << " "
6957
<< GetSymbolName(symbols[i]) << std::endl;
7058
}
7159
return stream.str();
@@ -74,12 +62,14 @@ std::string BacktraceHere(size_t offset) {
7462
static size_t kKnownSignalHandlers[] = {
7563
SIGABRT, // abort program
7664
SIGFPE, // floating-point exception
77-
SIGBUS, // bus error
65+
SIGTERM, // software termination signal
7866
SIGSEGV, // segmentation violation
67+
#if !FML_OS_WIN
68+
SIGBUS, // bus error
7969
SIGSYS, // non-existent system call invoked
8070
SIGPIPE, // write on a pipe with no reader
8171
SIGALRM, // real-time timer expired
82-
SIGTERM, // software termination signal
72+
#endif // !FML_OS_WIN
8373
};
8474

8575
static std::string SignalNameToString(int signal) {
@@ -88,18 +78,20 @@ static std::string SignalNameToString(int signal) {
8878
return "SIGABRT";
8979
case SIGFPE:
9080
return "SIGFPE";
91-
case SIGBUS:
92-
return "SIGBUS";
9381
case SIGSEGV:
9482
return "SIGSEGV";
83+
case SIGTERM:
84+
return "SIGTERM";
85+
#if !FML_OS_WIN
86+
case SIGBUS:
87+
return "SIGBUS";
9588
case SIGSYS:
9689
return "SIGSYS";
9790
case SIGPIPE:
9891
return "SIGPIPE";
9992
case SIGALRM:
10093
return "SIGALRM";
101-
case SIGTERM:
102-
return "SIGTERM";
94+
#endif // !FML_OS_WIN
10395
};
10496
return std::to_string(signal);
10597
}

fml/backtrace.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace fml {
1313

14+
// Retrieve the backtrace, for debugging.
15+
//
16+
// If the |offset| is 0, the backtrace is included caller function.
1417
std::string BacktraceHere(size_t offset = 0);
1518

1619
void InstallCrashHandler();

0 commit comments

Comments
 (0)