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

Commit 450e5f2

Browse files
committed
Add Windows support for //flutter/fml/backtrace.h
1 parent dcdb130 commit 450e5f2

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
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: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,32 @@
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 <cxxabi.h>
21+
#include <dlfcn.h>
22+
#include <execinfo.h>
23+
#endif // FML_OS_WIN
24+
2325
namespace fml {
2426

2527
static std::string kKUnknownFrameName = "Unknown";
2628

2729
static std::string DemangleSymbolName(const std::string& mangled) {
30+
#if FML_OS_WIN
31+
return mangled;
32+
#else
2833
if (mangled == kKUnknownFrameName) {
2934
return kKUnknownFrameName;
3035
}
@@ -44,6 +49,7 @@ static std::string DemangleSymbolName(const std::string& mangled) {
4449
auto demangled_string = std::string{demangled, length};
4550
free(demangled);
4651
return demangled_string;
52+
#endif // FML_OS_WIN
4753
}
4854

4955
static std::string GetSymbolName(void* symbol) {
@@ -55,10 +61,18 @@ static std::string GetSymbolName(void* symbol) {
5561
return DemangleSymbolName({name});
5662
}
5763

64+
static int Backtrace(void** buffer, int size) {
65+
#if FML_OS_WIN
66+
return CaptureStackBackTrace(0, size, buffer, NULL);
67+
#else
68+
return ::backtrace(symbols, kMaxFrames);
69+
#endif // FML_OS_WIN
70+
}
71+
5872
std::string BacktraceHere(size_t offset) {
5973
constexpr size_t kMaxFrames = 256;
6074
void* symbols[kMaxFrames];
61-
const auto available_frames = ::backtrace(symbols, kMaxFrames);
75+
const auto available_frames = Backtrace(symbols, kMaxFrames);
6276
if (available_frames <= 0) {
6377
return "";
6478
}
@@ -74,12 +88,15 @@ std::string BacktraceHere(size_t offset) {
7488
static size_t kKnownSignalHandlers[] = {
7589
SIGABRT, // abort program
7690
SIGFPE, // floating-point exception
77-
SIGBUS, // bus error
91+
SIGTERM, // software termination signal
7892
SIGSEGV, // segmentation violation
93+
#if !FML_OS_WIN
94+
SIGBUS, // bus error
7995
SIGSYS, // non-existent system call invoked
8096
SIGPIPE, // write on a pipe with no reader
8197
SIGALRM, // real-time timer expired
82-
SIGTERM, // software termination signal
98+
99+
#endif // !FML_OS_WIN
83100
};
84101

85102
static std::string SignalNameToString(int signal) {
@@ -88,18 +105,20 @@ static std::string SignalNameToString(int signal) {
88105
return "SIGABRT";
89106
case SIGFPE:
90107
return "SIGFPE";
91-
case SIGBUS:
92-
return "SIGBUS";
93108
case SIGSEGV:
94109
return "SIGSEGV";
110+
case SIGTERM:
111+
return "SIGTERM";
112+
#if !FML_OS_WIN
113+
case SIGBUS:
114+
return "SIGBUS";
95115
case SIGSYS:
96116
return "SIGSYS";
97117
case SIGPIPE:
98118
return "SIGPIPE";
99119
case SIGALRM:
100120
return "SIGALRM";
101-
case SIGTERM:
102-
return "SIGTERM";
121+
#endif // !FML_OS_WIN
103122
};
104123
return std::to_string(signal);
105124
}

fml/backtrace_unittests.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ TEST(BacktraceTest, CanGatherBacktrace) {
1919
auto trace = BacktraceHere(0);
2020
ASSERT_GT(trace.size(), 0u);
2121
ASSERT_NE(trace.find("Frame 0"), std::string::npos);
22+
std::cout << trace << std::endl;
2223
}
2324

2425
{
2526
auto trace = BacktraceHere(1);
2627
ASSERT_GT(trace.size(), 0u);
2728
ASSERT_NE(trace.find("Frame 0"), std::string::npos);
29+
std::cout << trace << std::endl;
2830
}
2931

3032
{
3133
auto trace = BacktraceHere(2);
3234
ASSERT_GT(trace.size(), 0u);
3335
ASSERT_NE(trace.find("Frame 0"), std::string::npos);
36+
std::cout << trace << std::endl;
3437
}
3538
}
3639

0 commit comments

Comments
 (0)