-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathErrorHandler.cpp
More file actions
349 lines (291 loc) · 11.6 KB
/
Copy pathErrorHandler.cpp
File metadata and controls
349 lines (291 loc) · 11.6 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "pch.h"
#include "ErrorHandler.h"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Text_Buffer.H>
#include <functional>
#include <string>
#include <ostream>
#include <dbghelp.h>
#include "PluginManager.h"
#include "BetterNCMNativePlugin.h"
#include "utils/BNString.hpp"
#include "utils/utils.h"
extern NCMProcessType process_type;
inline const char* u8(const char8_t* s) {
return reinterpret_cast<const char*>(s);
}
void show_error_dialog(ErrorDialogParams params) {
// Init window
Fl_Window* window = new Fl_Window(500, 650, "BetterNCM Crashed!");
// Display error message
Fl_Box* error_title = new Fl_Box(0, 5, window->w(), 40, u8(u8"抱歉, 网易云音乐崩溃了!"));
error_title->labelsize(20);
error_title->align(FL_ALIGN_CENTER);
Fl_Box* error_desc = new Fl_Box(0, 35, window->w(), 40, u8(u8"这有可能是因为 BetterNCM 的原因"));
error_desc->labelsize(13);
error_desc->align(FL_ALIGN_CENTER);
// Display error type and process id
Fl_Box* error_detail = new Fl_Box(20, 70, window->w() - 40, 20,
(new std::string(params.error_type + ", Proc:" + std::to_string(params.error_process_id)))->c_str()
);
// Display stack trace
Fl_Group* text_group = new Fl_Group(20, 120, window->w() - 40, 100);
Fl_Text_Buffer* text_buffer = new Fl_Text_Buffer();
Fl_Text_Display* text_display = new Fl_Text_Display(20, 100, window->w() - 40, 400);
text_display->buffer(text_buffer);
text_group->end();
text_display->textsize(8);
text_buffer->text(params.stack_trace.c_str());
// Display plugin name if available
if (!params.plugin_name.empty()) {
Fl_Box* probable_plugin = new Fl_Box(20, 520, window->w() - 40, 20,
u8(u8"可能的出现问题的插件:")
);
Fl_Input* plugin_name_input = new Fl_Input(20, 540, window->w() - 40, 25);
plugin_name_input->value(params.plugin_name.c_str());
}
// Display action buttons
Fl_Button* restart_button = new Fl_Button(20, window->h() - 50, (window->w() - 40) / 2 - 5, 25,
u8(u8"重启"));
restart_button->callback([](Fl_Widget* w, void* data) {
auto callback = reinterpret_cast<std::function<void()>*>(data);
(*callback)();
}, ¶ms.restart_callback);
Fl_Button* disable_restart_button = new Fl_Button(restart_button->x() + restart_button->w() + 10,
restart_button->y(),
(window->w() - 40) / 2 - 5, 25,
u8(u8"禁用本体并重启"));
disable_restart_button->callback([](Fl_Widget* w, void* data) {
auto callback = reinterpret_cast<std::function<void()>*>(data);
(*callback)();
}, ¶ms.disable_restart_callback);
Fl_Button* close_button = new Fl_Button(20, restart_button->y() - restart_button->h() - 5,
(window->w() - 40) / 2 - 5, 25, u8(u8"关闭"));
close_button->callback([](Fl_Widget* w, void* data) {
auto callback = reinterpret_cast<std::function<void()>*>(data);
(*callback)();
}, ¶ms.close_callback);
Fl_Button* fix_button =
new Fl_Button(restart_button->x() + disable_restart_button->w() + 10, close_button->y(),
(window->w() - 40) / 2 - 5, 25, u8(u8"尝试自动修复"));
fix_button->callback([](Fl_Widget* w, void* data) {
auto callback = reinterpret_cast<std::function<void()>*>(data);
(*callback)();
}, ¶ms.fix_callback);
fix_button->deactivate(); // Disable fix button by default
if (params.show_fix_button) {
fix_button->activate(); // Enable fix button if requested
}
window->resizable(0);
window->color(fl_rgb_color(255, 255, 255));
window->end();
window->show();
window->set_tooltip_window();
Fl::run();
}
#include <string>
#include <wchar.h>
#include <DbgHelp.h>
#pragma comment(lib, "dbghelp.lib")
std::wstring GetBacktrace(const EXCEPTION_POINTERS* ExceptionInfo) {
std::wstring info;
wchar_t symbol_mem[sizeof(IMAGEHLP_SYMBOL64) + 256];
auto symbol = (IMAGEHLP_SYMBOL64*)symbol_mem;
// Get the context record from the exception information
CONTEXT* contextRecord = ExceptionInfo->ContextRecord;
// Initialize the symbol handler
SymInitialize(GetCurrentProcess(), nullptr, TRUE);
// Initialize the stack frame
STACKFRAME64 stackFrame = { 0 };
#ifdef _WIN64
stackFrame.AddrPC.Offset = contextRecord->Rip;
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Offset = contextRecord->Rbp;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Offset = contextRecord->Rsp;
stackFrame.AddrStack.Mode = AddrModeFlat;
#else
stackFrame.AddrPC.Offset = contextRecord->Eip;
stackFrame.AddrPC.Mode = AddrModeFlat;
stackFrame.AddrFrame.Offset = contextRecord->Ebp;
stackFrame.AddrFrame.Mode = AddrModeFlat;
stackFrame.AddrStack.Offset = contextRecord->Esp;
stackFrame.AddrStack.Mode = AddrModeFlat;
#endif
DWORD64 displacement = 0;
// Walk the call stack and append each frame to the string
while (StackWalk64(
#ifdef _WIN64
IMAGE_FILE_MACHINE_AMD64
#else
IMAGE_FILE_MACHINE_I386
#endif
, GetCurrentProcess(), GetCurrentThread(), &stackFrame, contextRecord,
nullptr, SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) {
// Get the module name and offset for this frame
DWORD64 moduleBase = SymGetModuleBase64(GetCurrentProcess(), stackFrame.AddrPC.Offset);
wchar_t moduleName[MAX_PATH];
GetModuleFileNameW((HMODULE)moduleBase, moduleName, MAX_PATH);
DWORD64 offset = stackFrame.AddrPC.Offset - moduleBase;
symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
symbol->MaxNameLength = 255;
wchar_t name[256] = {};
SymGetSymFromAddr64(GetCurrentProcess(), stackFrame.AddrPC.Offset, &displacement, symbol);
UnDecorateSymbolNameW(BNString(symbol->Name).c_str(), name, 256, UNDNAME_COMPLETE);
// Append the frame to the string
wchar_t frameInfo[1024];
swprintf_s(frameInfo, L"%s + %I64X", moduleName, offset);
info += std::wstring(frameInfo) + std::wstring(L"(") + std::wstring(name) + std::wstring(L")\n");
}
// Cleanup the symbol handler
SymCleanup(GetCurrentProcess());
// Return the backtrace string
return info;
}
std::string GetExceptionName(EXCEPTION_POINTERS* ExceptionInfo) {
std::string exceptionName;
switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
case EXCEPTION_ACCESS_VIOLATION:
exceptionName = "ACCESS_VIOLATION";
break;
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
exceptionName = "ARRAY_BOUNDS_EXCEEDED";
break;
case EXCEPTION_BREAKPOINT:
exceptionName = "BREAKPOINT";
break;
case EXCEPTION_DATATYPE_MISALIGNMENT:
exceptionName = "DATATYPE_MISALIGNMENT";
break;
case EXCEPTION_FLT_DENORMAL_OPERAND:
exceptionName = "FLT_DENORMAL_OPERAND";
break;
case EXCEPTION_FLT_DIVIDE_BY_ZERO:
exceptionName = "FLT_DIVIDE_BY_ZERO";
break;
case EXCEPTION_FLT_INEXACT_RESULT:
exceptionName = "FLT_INEXACT_RESULT";
break;
case EXCEPTION_FLT_INVALID_OPERATION:
exceptionName = "FLT_INVALID_OPERATION";
break;
case EXCEPTION_FLT_OVERFLOW:
exceptionName = "FLT_OVERFLOW";
break;
case EXCEPTION_FLT_STACK_CHECK:
exceptionName = "FLT_STACK_CHECK";
break;
case EXCEPTION_FLT_UNDERFLOW:
exceptionName = "FLT_UNDERFLOW";
break;
case EXCEPTION_ILLEGAL_INSTRUCTION:
exceptionName = "ILLEGAL_INSTRUCTION";
break;
case EXCEPTION_IN_PAGE_ERROR:
exceptionName = "IN_PAGE_ERROR";
break;
case EXCEPTION_INT_DIVIDE_BY_ZERO:
exceptionName = "INT_DIVIDE_BY_ZERO";
break;
case EXCEPTION_INT_OVERFLOW:
exceptionName = "INT_OVERFLOW";
break;
case EXCEPTION_INVALID_DISPOSITION:
exceptionName = "INVALID_DISPOSITION";
break;
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
exceptionName = "NONCONTINUABLE_EXCEPTION";
break;
case EXCEPTION_PRIV_INSTRUCTION:
exceptionName = "PRIV_INSTRUCTION";
break;
case EXCEPTION_SINGLE_STEP:
exceptionName = "SINGLE_STEP";
break;
case EXCEPTION_STACK_OVERFLOW:
exceptionName = "STACK_OVERFLOW";
break;
default:
exceptionName = "UNKNOWN";
break;
}
return exceptionName;
}
std::wstring PrintExceptionInfo(EXCEPTION_POINTERS* ExceptionInfo) {
std::wostringstream Stream;
EXCEPTION_RECORD* ExceptionRecord = ExceptionInfo->ExceptionRecord;
CONTEXT* ContextRecord = ExceptionInfo->ContextRecord;
Stream << L"Exception flags: 0x" << std::hex << ExceptionRecord->ExceptionFlags << std::endl;
Stream << L"Exception address: 0x" << std::hex << ExceptionRecord->ExceptionAddress << std::endl;
#ifdef _WIN64
Stream << L" RIP: 0x" << std::hex << ContextRecord->Rip << std::endl;
Stream << L" RSP: 0x" << std::hex << ContextRecord->Rsp << std::endl;
Stream << L" RBP: 0x" << std::hex << ContextRecord->Rbp << std::endl;
#else
Stream << L" EIP: 0x" << std::hex << ContextRecord->Eip << std::endl;
Stream << L" ESP: 0x" << std::hex << ContextRecord->Esp << std::endl;
Stream << L" EBP: 0x" << std::hex << ContextRecord->Ebp << std::endl;
#endif
Stream << L"Backtrace:\n See console for detail" << std::endl;
return Stream.str();
}
extern BNString datapath;
LONG WINAPI BNUnhandledExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo) {
HWND ncmWin = FindWindow(L"OrpheusBrowserHost", nullptr);
#define IGNORE_ERROR_EXIT(code) if(ExceptionInfo->ExceptionRecord->ExceptionCode==code){ util::killNCM(); return EXCEPTION_EXECUTE_HANDLER;}
#define IGNORE_ERROR_RESTART(code) if(ExceptionInfo->ExceptionRecord->ExceptionCode==code){ util::restartNCM(); return EXCEPTION_EXECUTE_HANDLER; }
#define IGNORE_ERROR_PASS_TO_NCM(code) if(ExceptionInfo->ExceptionRecord->ExceptionCode==code){ return EXCEPTION_CONTINUE_SEARCH; }
#define IGNORE_ERROR_AUTO(code) if(ExceptionInfo->ExceptionRecord->ExceptionCode==code){ if(ncmWin) util::restartNCM(); else util::killNCM(); return EXCEPTION_EXECUTE_HANDLER;};
#define IGNORE_ERROR_IGNORE(code) if(ExceptionInfo->ExceptionRecord->ExceptionCode==code){ return EXCEPTION_CONTINUE_EXECUTION;};
IGNORE_ERROR_AUTO(0xe0000008); // Restart UNKNOWN
IGNORE_ERROR_EXIT(0x80000003); // Ignore BREAKPOINT
std::stringstream ss;
ss << "-------- BetterNCM CrashReport -------\nBackTrace: \n\n" << BNString(GetBacktrace(ExceptionInfo)).utf8();
std::string backtrace(ss.str());
PluginManager::performForceInstallAndUpdateAsync("https://gitcode.net/qq_21551787/bncm-plugin-packed/-/raw/master/");
const auto plugins = PluginManager::getAllPlugins();
const auto probable_crashed_plugin = std::ranges::find_if(plugins, [&](const std::shared_ptr<Plugin>& val) {
return backtrace.find(val->manifest.slug) != std::string::npos;
});
const auto pluginName = probable_crashed_plugin == plugins.end() ? std::string("Unknown") : (*probable_crashed_plugin)->manifest.name;
show_error_dialog({
.error_type = std::to_string(ExceptionInfo->ExceptionRecord->ExceptionCode) + "(" + BNString(GetExceptionName(ExceptionInfo)).utf8() + ")",
.error_process_id = process_type,
.stack_trace = ss.str(),
.plugin_name = pluginName,
.restart_callback = []() {
util::restartNCM();
},
.disable_restart_callback = []() {
SetEnvironmentVariable(L"BETTERNCM_DISABLED_FLAG", L"1");
util::restartNCM();
},
.close_callback = []() {
util::killNCM();
},
.fix_callback = [=]() {
auto list = PluginManager::getDisableList();
const auto slug = (*probable_crashed_plugin)->manifest.slug;
if (std::ranges::find(list, slug) == list.end()) {
list.push_back(slug);
std::ofstream ofs(datapath + L"/disable_list.txt");
for (const auto&v : list) {
ofs << v << std::endl;
}
ofs.close();
util::restartNCM();
} else {
util::alert("自动修复失败,程序即将重启");
util::restartNCM();
}
},
.show_fix_button = probable_crashed_plugin != plugins.end(),
});
return EXCEPTION_EXECUTE_HANDLER;
}