-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccelerator_local.cpp
More file actions
318 lines (259 loc) · 9.56 KB
/
accelerator_local.cpp
File metadata and controls
318 lines (259 loc) · 9.56 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
/**
* ======================================================
* Accelerator local
* Written by Phoenix (˙·٠●Феникс●٠·˙) 2023-2025, Asher Baker (asherkin) 2011.
* ======================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*/
#include "accelerator_local.h"
#include "CMiniDumpComment.hpp"
#include "client/linux/handler/exception_handler.h"
#include "common/linux/linux_libc_support.h"
#include "third_party/lss/linux_syscall_support.h"
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <dirent.h>
#include <unistd.h>
#include <limits>
#include "common/path_helper.h"
#include "common/using_std_string.h"
#include "google_breakpad/processor/basic_source_line_resolver.h"
#include "google_breakpad/processor/minidump_processor.h"
#include "google_breakpad/processor/process_state.h"
#include "processor/simple_symbol_supplier.h"
#include "processor/stackwalk_common.h"
#include <google_breakpad/processor/call_stack.h>
#include <google_breakpad/processor/stack_frame.h>
#include <processor/pathname_stripper.h>
AcceleratorLocal g_AcceleratorLocal;
PLUGIN_EXPOSE(AcceleratorLocal, g_AcceleratorLocal);
char crashMap[256];
char crashGamePath[512];
char crashCommandLine[1024];
char dumpStoragePath[512];
google_breakpad::ExceptionHandler* exceptionHandler = nullptr;
CMiniDumpComment g_MiniDumpComment(95000);
void (*SignalHandler)(int, siginfo_t*, void*);
const int kExceptionSignals[] = {SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS};
const int kNumHandledSignals = std::size(kExceptionSignals);
class GameSessionConfiguration_t { };
SH_DECL_HOOK3_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool, bool, bool);
SH_DECL_HOOK3_void(INetworkServerService, StartupServer, SH_NOATTRIB, 0, const GameSessionConfiguration_t&, ISource2WorldSession*, const char*);
static bool dumpCallback(const google_breakpad::MinidumpDescriptor& descriptor, void* context, bool succeeded)
{
if (succeeded)
sys_write(STDOUT_FILENO, "Wrote minidump to: ", 19);
else
sys_write(STDOUT_FILENO, "Failed to write minidump to: ", 29);
sys_write(STDOUT_FILENO, descriptor.path(), my_strlen(descriptor.path()));
sys_write(STDOUT_FILENO, "\n", 1);
if (!succeeded)
return succeeded;
my_strlcpy(dumpStoragePath, descriptor.path(), sizeof(dumpStoragePath));
my_strlcat(dumpStoragePath, ".txt", sizeof(dumpStoragePath));
int extra = sys_open(dumpStoragePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (extra == -1)
{
sys_write(STDOUT_FILENO, "Failed to open metadata file!\n", 30);
return succeeded;
}
sys_write(extra, "-------- CONFIG BEGIN --------", 30);
sys_write(extra, "\nMap=", 5);
sys_write(extra, crashMap, my_strlen(crashMap));
sys_write(extra, "\nGamePath=", 10);
sys_write(extra, crashGamePath, my_strlen(crashGamePath));
sys_write(extra, "\nCommandLine=", 13);
sys_write(extra, crashCommandLine, my_strlen(crashCommandLine));
sys_write(extra, "\n-------- CONFIG END --------\n", 30);
sys_write(extra, "\n", 1);
LoggingSystem_GetLogCapture(&g_MiniDumpComment, true);
const char* pszConsoleHistory = g_MiniDumpComment.GetStartPointer();
if (pszConsoleHistory[0])
{
sys_write(extra, "-------- CONSOLE HISTORY BEGIN --------\n", 40);
sys_write(extra, pszConsoleHistory, my_strlen(pszConsoleHistory));
sys_write(extra, "-------- CONSOLE HISTORY END --------\n", 38);
sys_write(extra, "\n", 1);
}
google_breakpad::scoped_ptr<google_breakpad::SimpleSymbolSupplier> symbolSupplier;
google_breakpad::BasicSourceLineResolver resolver;
google_breakpad::MinidumpProcessor minidump_processor(symbolSupplier.get(), &resolver);
// Increase the maximum number of threads and regions.
google_breakpad::MinidumpThreadList::set_max_threads(std::numeric_limits<uint32_t>::max());
google_breakpad::MinidumpMemoryList::set_max_regions(std::numeric_limits<uint32_t>::max());
// Process the minidump.
google_breakpad::Minidump miniDump(descriptor.path());
if (!miniDump.Read())
{
sys_write(STDOUT_FILENO, "Failed to read minidump\n", 24);
}
else
{
google_breakpad::ProcessState processState;
if (minidump_processor.Process(&miniDump, &processState) != google_breakpad::PROCESS_OK)
{
sys_write(STDOUT_FILENO, "MinidumpProcessor::Process failed\n", 34);
}
else
{
int requestingThread = processState.requesting_thread();
if (requestingThread == -1)
requestingThread = 0;
const google_breakpad::CallStack* stack = processState.threads()->at(requestingThread);
size_t frameCount = MIN(stack->frames()->size(), 15);
auto signal_safe_hex_print = [](uint64_t num)
{
char buffer[18];
char* ptr = buffer + sizeof(buffer);
if (num == 0)
*(--ptr) = '0';
else
{
while (num > 0)
{
*(--ptr) = "0123456789abcdef"[num % 16];
num /= 16;
}
}
*(--ptr) = 'x';
*(--ptr) = '0';
size_t length = buffer + sizeof(buffer) - ptr;
sys_write(STDOUT_FILENO, ptr, length);
};
sys_write(STDOUT_FILENO, "\n", 1);
for (size_t frameIndex = 0; frameIndex < frameCount; ++frameIndex)
{
const google_breakpad::StackFrame* frame = stack->frames()->at(frameIndex);
uint64_t moduleOffset = frame->ReturnAddress();
if (frame->module)
{
const std::string moduleFile = google_breakpad::PathnameStripper::File(frame->module->code_file());
moduleOffset -= frame->module->base_address();
sys_write(STDOUT_FILENO, moduleFile.c_str(), moduleFile.size());
sys_write(STDOUT_FILENO, " + ", 3);
signal_safe_hex_print(moduleOffset);
sys_write(STDOUT_FILENO, "\n", 1);
}
else
{
sys_write(STDOUT_FILENO, "unknown + ", 10);
signal_safe_hex_print(moduleOffset);
sys_write(STDOUT_FILENO, "\n", 1);
}
}
freopen(dumpStoragePath, "a", stdout);
PrintProcessState(processState, true, false, &resolver);
fflush(stdout);
}
}
sys_close(extra);
return succeeded;
}
bool AcceleratorLocal::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();
GET_V_IFACE_CURRENT(GetServerFactory, g_pSource2Server, ISource2Server, SOURCE2SERVER_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, g_pNetworkServerService, INetworkServerService, NETWORKSERVERSERVICE_INTERFACE_VERSION);
strncpy(crashGamePath, ismm->GetBaseDir(), sizeof(crashGamePath) - 1);
ismm->Format(dumpStoragePath, sizeof(dumpStoragePath), "%s/addons/accelerator_local/dumps", ismm->GetBaseDir());
struct stat st = {0};
if (stat(dumpStoragePath, &st) == -1)
{
if(mkdir(dumpStoragePath, 0777) == -1)
{
ismm->Format(error, maxlen, "%s didn't exist and we couldn't create it :(", dumpStoragePath);
return false;
}
}
else
chmod(dumpStoragePath, 0777);
google_breakpad::MinidumpDescriptor descriptor(dumpStoragePath);
exceptionHandler = new google_breakpad::ExceptionHandler(descriptor, NULL, dumpCallback, NULL, true, -1);
struct sigaction oact;
sigaction(SIGSEGV, NULL, &oact);
SignalHandler = oact.sa_sigaction;
SH_ADD_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &AcceleratorLocal::GameFrame), true);
SH_ADD_HOOK(INetworkServerService, StartupServer, g_pNetworkServerService, SH_MEMBER(this, &AcceleratorLocal::StartupServer), true);
strncpy(crashCommandLine, CommandLine()->GetCmdLine(), sizeof(crashCommandLine) - 1);
if (late)
StartupServer({}, nullptr, g_pNetworkServerService->GetIGameServer()->GetMapName());
return true;
}
bool AcceleratorLocal::Unload(char* error, size_t maxlen)
{
SH_REMOVE_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &AcceleratorLocal::GameFrame), true);
SH_REMOVE_HOOK(INetworkServerService, StartupServer, g_pNetworkServerService, SH_MEMBER(this, &AcceleratorLocal::StartupServer), true);
delete exceptionHandler;
return true;
}
void AcceleratorLocal::GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
{
bool weHaveBeenFuckedOver = false;
struct sigaction oact;
for (int i = 0; i < kNumHandledSignals; ++i)
{
sigaction(kExceptionSignals[i], NULL, &oact);
if (oact.sa_sigaction != SignalHandler)
{
weHaveBeenFuckedOver = true;
break;
}
}
if (!weHaveBeenFuckedOver)
return;
struct sigaction act;
memset(&act, 0, sizeof(act));
sigemptyset(&act.sa_mask);
for (int i = 0; i < kNumHandledSignals; ++i)
sigaddset(&act.sa_mask, kExceptionSignals[i]);
act.sa_sigaction = SignalHandler;
act.sa_flags = SA_ONSTACK | SA_SIGINFO;
for (int i = 0; i < kNumHandledSignals; ++i)
sigaction(kExceptionSignals[i], &act, NULL);
}
void AcceleratorLocal::StartupServer(const GameSessionConfiguration_t& config, ISource2WorldSession*, const char* pszMapName)
{
strncpy(crashMap, pszMapName, sizeof(crashMap) - 1);
}
///////////////////////////////////////
const char* AcceleratorLocal::GetLicense()
{
return "GPL";
}
const char* AcceleratorLocal::GetVersion()
{
return "1.0.4";
}
const char* AcceleratorLocal::GetDate()
{
return __DATE__;
}
const char *AcceleratorLocal::GetLogTag()
{
return "AcceleratorLocal";
}
const char* AcceleratorLocal::GetAuthor()
{
return "Phoenix (˙·٠●Феникс●٠·˙), Asher Baker (asherkin)";
}
const char* AcceleratorLocal::GetDescription()
{
return "Crash Handler";
}
const char* AcceleratorLocal::GetName()
{
return "Accelerator local";
}
const char* AcceleratorLocal::GetURL()
{
return "https://github.com/komashchenko/AcceleratorLocal";
}