forked from N1coc4colA/DA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaddonapplication.cpp
283 lines (243 loc) · 8.97 KB
/
daddonapplication.cpp
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
/*
* This file is part LDA, Library for Deepin Toolkit Addons.
* Some components from this project can include sources.
* If they claim a license, then it is specified as the sources
* can have been modified. They are shown at beginnig of files
* or inclusions (when include is replaced by its content).
*
* Copyright (C) 2020 Nicolas B. @n1coc4cola or N1coc4colA on Github.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "daddonapplication.h"
#include <execinfo.h> // for backtrace
#include <dlfcn.h> // for dladdr
#include <cxxabi.h> // for __cxa_demangle
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <signal.h>
#include <mcheck.h>
#include <QProcess>
#include <DApplication>
#include <QSettings>
#include <QDebug>
#include <QFile>
#include <QStandardPaths>
#include <QDateTime>
#include <DNotifySender>
#include <QStyle>
#include <QStyleHints>
using Sender = Dtk::Core::DUtil::DNotifySender;
LDA_BEGIN_NAMESPACE
const int MAX_STACK_FRAMES = 128;
const QString strPath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/dde-collapse.log";
const QString cfgPath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/dde-cfg.ini";
QString appBinPath = "None";
QString bin_name = "";
bool active_mtrace = false;
bool enableDebug = false;
std::string Backtrace(int skip = 1)
{
void *callstack[128];
const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
char buf[1024];
int nFrames = backtrace(callstack, nMaxFrames);
char **symbols = backtrace_symbols(callstack, nFrames);
std::ostringstream trace_buf;
for (int i = skip; i < nFrames; i++) {
Dl_info info;
if (dladdr(callstack[i], &info) && info.dli_sname) {
char *demangled = nullptr;
int status = -1;
if (info.dli_sname[0] == '_')
demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n",
i, int(2 + sizeof(void*) * 2), callstack[i],
status == 0 ? demangled :
info.dli_sname == nullptr ? symbols[i] : info.dli_sname,
(char *)callstack[i] - (char *)info.dli_saddr);
free(demangled);
} else {
snprintf(buf, sizeof(buf), "%-3d %*p %s\n",
i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]);
}
trace_buf << buf;
}
free(symbols);
if (nFrames == nMaxFrames)
trace_buf << "[truncated]\n";
return trace_buf.str();
}
void handleSignals [[ noreturn ]] (int sig)
{
QFile *file = new QFile(strPath);
if (!QFile::exists(cfgPath)) {
QFile file(cfgPath);
if (!file.open(QIODevice::WriteOnly))
exit(0);
file.close();
}
QSettings settings(cfgPath, QSettings::IniFormat);
settings.beginGroup(bin_name);
QDateTime lastDate = QDateTime::fromString(settings.value("lastDate").toString(), "yyyy-MM-dd hh:mm:ss:zzz");
int collapseNum = settings.value("collapse").toInt();
if (qAbs(lastDate.secsTo(QDateTime::currentDateTime())) < 10) {
settings.setValue("collapse", collapseNum + 1);
} else {
settings.setValue("collapse", 0);
}
settings.setValue("lastDate", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz"));
settings.endGroup();
settings.sync();
if (!file->open(QIODevice::Text | QIODevice::Append)) {
qDebug() << file->errorString();
exit(0);
}
if (file->size() >= 10 * 1024 * 1024) {
file->close();
if (file->open(QIODevice::Text | QIODevice::Truncate)) {
qDebug() << file->errorString();
exit(0);
}
}
try {
DWIDGET_USE_NAMESPACE
QString head = "\n#####" + qApp->applicationName() + "#####\n"
+ QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss:zzz]")
+ "[crash signal number:" + QString::number(sig) + "]\n";
file->write(head.toUtf8());
#ifdef Q_OS_LINUX
void *array[MAX_STACK_FRAMES];
size_t size = 0;
char **strings = nullptr;
size_t i;
signal(sig, SIG_DFL);
size = static_cast<size_t>(backtrace(array, MAX_STACK_FRAMES));
strings = backtrace_symbols(array, int(size));
for (i = 0; i < size; ++i) {
QString line = QString::number(i) + " " + QString::fromStdString(strings[i]) + "\n";
file->write(line.toUtf8());
std::string symbol(strings[i]);
QString strSymbol = QString::fromStdString(symbol);
int pos1 = strSymbol.indexOf("[");
int pos2 = strSymbol.lastIndexOf("]");
QString address = strSymbol.mid(pos1 + 1,pos2 - pos1 - 1);
QString cmd = "addr2line -C -f -e " + qApp->applicationName() + " " + address;
QProcess *p = new QProcess;
p->setReadChannel(QProcess::StandardOutput);
p->start(cmd);
p->waitForFinished();
p->waitForReadyRead();
const char* __data = p->readAllStandardOutput();
file->write(__data);
delete p;
p = nullptr;
}
std::cerr << Backtrace() << std::endl;
free(strings);
#endif // __linux
} catch (...) {
//
}
file->close();
QString sigVal;
switch (sig) {
case SIGTERM: sigVal = "SIGTERM"; break;
case SIGILL: sigVal = "SIGILL"; break;
case SIGSEGV: sigVal = "SIGSEGV"; break;
case SIGINT: sigVal = "SIGINT"; break;
case SIGABRT: sigVal = "SIGABRT"; break;
case SIGFPE: sigVal = "SIGFPE"; break;
}
DWIDGET_USE_NAMESPACE
Sender send("Application crashed");
send.timeOut(3000);
send.appIcon("preferences-system");
send.appName("Crash reporter");
send.appBody("Application: \"" + QString("##BEGIN##" + appBinPath).replace("##BEGIN##/home", "~") + "\" crashed: " + sigVal);
send.call();
send.~DNotifySender();
sigVal.~QString();
appBinPath.~QString();
if (active_mtrace) {
muntrace();
}
exit(sig);
}
DAddonApplication::DAddonApplication(int &argc, char **argv, bool enforce) : DApplication(argc, argv)
{
std::cout << "You are currently using LDA, Lib Dtk Addons, by @n1coc4cola, GNU Public License V3." << std::endl;
bin_name = QString(argv[0]);
int i = 0;
while (i < argc) {
if (strcmp(argv[i], "--enable-mtrace")) {
mtrace();
active_mtrace = true;
} else if (enforce == false && strcmp(argv[i], "--enable-cdbg")) {
std::cout << "Enabled Console Debugging (CDBG) for library internal messages." << std::endl;
enableDebug = true;
}
i++;
}
if (enforce) {
enableDebug = true;
}
signal(SIGTERM, handleSignals);
signal(SIGSEGV, handleSignals);
signal(SIGILL, handleSignals);
signal(SIGINT, handleSignals);
signal(SIGABRT, handleSignals);
signal(SIGFPE, handleSignals);
appBinPath = this->applicationFilePath();
}
DAddonApplication::~DAddonApplication()
{
//appBinPath.~QString();
if (active_mtrace) {
muntrace();
}
}
void DAddonApplication::handleQuitAction() { this->DApplication::handleQuitAction(); }
void DAddonApplication::handleHelpAction() { this->DApplication::handleHelpAction(); }
void DAddonApplication::handleAboutAction() { this->DApplication::handleAboutAction(); }
void DAddonApplication::setEnabledHoverEffects(bool enable)
{
this->styleHints()->setUseHoverEffects(enable);
}
void DAddonApplication::toggleHoverEffects()
{
this->styleHints()->setUseHoverEffects(!this->styleHints()->useHoverEffects());
}
bool DAddonApplication::areHoverEffectsEnabled()
{
return this->styleHints()->useHoverEffects();
}
bool DAddonApplication::is_mtrace_active()
{
return active_mtrace;
}
bool DAddonApplication::isConsoleDebug()
{
return enableDebug;
}
LDA_END_NAMESPACE