-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatformhelper.cpp
More file actions
176 lines (152 loc) · 5.2 KB
/
Copy pathplatformhelper.cpp
File metadata and controls
176 lines (152 loc) · 5.2 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
#include "platformhelper.h"
#include <QBuffer>
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QProcess>
#include <QSharedPointer>
namespace ValWell {
#if !defined (Q_OS_LINUX) && !(defined (Q_OS_WIN32) && defined (Q_CC_MSVC))
class PlatformHelperPrivate
{
public:
PlatformHelperPrivate() = default;
~PlatformHelperPrivate() = default;
};
PlatformHelper::PlatformHelper(const QSharedPointer<InfoManager>& info)
: infoManager(info)
, _private(new PlatformHelperPrivate())
, outputPath(QString())
, dumpSymsPath(QString())
, minidumpStackwalkPath(QString())
, crashReportFilePath(QString())
{}
PlatformHelper::~PlatformHelper()
{}
void PlatformHelper::initCrashHandler()
{}
#endif
QSharedPointer<InfoManager> PlatformHelper::getInfoManager()
{
return infoManager;
}
void PlatformHelper::infoFinished()
{
infoManager->finished();
}
void PlatformHelper::updateBreakpadPath()
{
outputPath += QString("/%1_%2").arg(QCoreApplication::applicationName()).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss"));
}
QString PlatformHelper::breakpadPath() const
{
return outputPath;
}
void PlatformHelper::setCrashReportFilePath(const QString &filePath)
{
crashReportFilePath = filePath;
}
QString PlatformHelper::getCrashReportFilePath() const
{
return crashReportFilePath;
}
void PlatformHelper::prepareReportFilePath()
{
if (crashReportFilePath.isEmpty()) {
setCrashReportFilePath(breakpadPath() + QLatin1String("/crash_report.bug"));
}
}
QStringList PlatformHelper::prepareSharedLib(const QString &dirPath, const QString suffix)
{
QDir sharedLibDir(dirPath);
QStringList filenameList;
QFileInfoList fileinfoList = sharedLibDir.entryInfoList(QDir::Files);
for (auto& fileinfo : fileinfoList) {
if (fileinfo.completeSuffix().contains(suffix) && !fileinfo.fileName().contains("Qt5")
&& !fileinfo.fileName().contains("api-ms-win", Qt::CaseInsensitive)) {
filenameList.append(fileinfo.fileName());
}
}
return filenameList;
}
QString PlatformHelper::archiveDumpFile(const QString& oldDumpFilePath)
{
int lastSlashIdx = oldDumpFilePath.lastIndexOf("/");
QString dumpFileName = oldDumpFilePath.right(oldDumpFilePath.size() - lastSlashIdx - 1);
QString newDumpFilePath = breakpadPath() + "/" + dumpFileName;
infoManager->pushDumpPath(newDumpFilePath);
QDir reportDir(breakpadPath());
if (!reportDir.exists()) {
if (!reportDir.mkpath(breakpadPath())) {
infoManager->pushInfo(QObject::tr("Can not make dir %1!").arg(breakpadPath()));
return QString();
}
}
if (!QFile::exists(oldDumpFilePath)) {
infoManager->pushInfo(QObject::tr("Not found dmp file!"));
return QString();
}
if (!QFile::copy(oldDumpFilePath, newDumpFilePath)) {
infoManager->pushInfo(QObject::tr("Move %1 to %2 failed!").arg(oldDumpFilePath).arg(newDumpFilePath));
return QString();
}
QFile::remove(oldDumpFilePath);
return newDumpFilePath;
}
bool PlatformHelper::reportCrash(const QString &dumpFilePath)
{
QSharedPointer<QProcess> minidumpProcess(new QProcess());
QSharedPointer<QByteArray> readData(new QByteArray());
QString symbolsDirPath(breakpadPath() + QLatin1String("/symbols"));
// 将输出写入到 crashReportFilePath
QObject::connect(minidumpProcess.data(), &QProcess::readyReadStandardOutput, [=](){
readData->append(minidumpProcess->readAllStandardOutput());
});
// 类似命令:minidump_stackwalk ./xxxx.dump ./symbols
minidumpProcess->start(minidumpStackwalkPath, {dumpFilePath, symbolsDirPath});
minidumpProcess->waitForFinished();
minidumpProcess->close();
infoManager->pushReportPath(crashReportFilePath);
if (minidumpProcess->exitCode() != 0) {
infoManager->pushInfo(QObject::tr("%1 execute failed! Exit code=%2").arg(minidumpStackwalkPath).arg(minidumpProcess->exitCode()));
return false;
}
if (readData->isEmpty()) {
infoManager->pushInfo(QObject::tr("minidump_stackwalk output crash report failed!"));
return false;
}
QFile reportFile(crashReportFilePath);
if (!reportFile.open(QFile::WriteOnly)) {
infoManager->pushInfo(QObject::tr("Can not open %1! %2").arg(crashReportFilePath).arg(reportFile.errorString()));
return false;
}
if (reportFile.write(readData->data()) <= 0) {
infoManager->pushInfo(QObject::tr("Write crash report data to %1 failed!").arg(crashReportFilePath));
reportFile.flush();
reportFile.close();
return false;
}
reportFile.flush();
reportFile.close();
return true;
}
bool PlatformHelper::removeSymbolDir()
{
QDir dir(breakpadPath() + QLatin1String("/symbols"));
return dir.removeRecursively();
}
QString PlatformHelper::readFirstLineId(const QSharedPointer<QByteArray> &bufferData)
{
QBuffer buffer(bufferData.data());
if (!buffer.open(QBuffer::ReadOnly)) {
return QString();
}
QList<QByteArray> splitList = buffer.readLine().split(' ');
buffer.close();
if (splitList.size() < 4) {
return QString();
}
return splitList.at(3);
}
} // namespace ValWell