Skip to content

Commit c7ff23c

Browse files
committed
Analyze binary if start fails
In case the binary does not start successfully some checks are done to find out what may be wrong. Change-Id: I01e466482b2847068227d9469b9d99dc33f7f9eb Reviewed-by: Rainer Keller <[email protected]>
1 parent 0e15c68 commit c7ff23c

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

process.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/socket.h>
88
#include <signal.h>
99
#include <fcntl.h>
10+
#include <QFileInfo>
1011

1112
static int pipefd[2];
1213

@@ -15,6 +16,53 @@ static void signalhandler(int)
1516
write(pipefd[1], " ", 1);
1617
}
1718

19+
static bool analyzeBinary(const QString &binary)
20+
{
21+
QFileInfo fi(binary);
22+
if (!fi.exists()) {
23+
printf("Binary does not exist.\n");
24+
return false;
25+
}
26+
if (!fi.isFile()) {
27+
printf("Binary is not a file.\n");
28+
return false;
29+
}
30+
if (!fi.isReadable()) {
31+
printf("Binary is not readable.\n");
32+
return false;
33+
}
34+
if (!fi.isExecutable()) {
35+
printf("Binary is not executable.\n");
36+
return false;
37+
}
38+
39+
if (fi.size() < 4) {
40+
printf("Binary is smaller than 4 bytes.\n");
41+
return false;
42+
}
43+
44+
QFile f(binary);
45+
if (!f.open(QFile::ReadOnly)) {
46+
printf("Could not open binary to analyze.\n");
47+
return false;
48+
}
49+
50+
QByteArray elfHeader = f.read(4);
51+
f.close();
52+
53+
if (elfHeader.size() < 4) {
54+
printf("Failed to read ELF header.\n");
55+
return false;
56+
}
57+
58+
if (elfHeader != QByteArray::fromHex("7f454C46")) { // 0x7f ELF
59+
printf("Binary is not an ELF file.\n");
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
1866
Process::Process()
1967
: QObject(0)
2068
, mProcess(new QProcess(this))
@@ -81,6 +129,7 @@ void Process::error(QProcess::ProcessError error)
81129
switch (error) {
82130
case QProcess::FailedToStart:
83131
printf("Failed to start\n");
132+
analyzeBinary(mBinary);
84133
break;
85134
case QProcess::Crashed:
86135
printf("Crashed\n");
@@ -125,10 +174,10 @@ void Process::startup(QStringList args)
125174
args.append(mConfig.args);
126175

127176
mProcess->setProcessEnvironment(pe);
128-
QString binary = args.first();
177+
mBinary = args.first();
129178
args.removeFirst();
130-
qDebug() << binary << args;
131-
mProcess->start(binary, args);
179+
qDebug() << mBinary << args;
180+
mProcess->start(mBinary, args);
132181
}
133182

134183
void Process::start(const QStringList &args)

process.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ private slots:
4949
int mDebuggee;
5050
bool mDebug;
5151
Config mConfig;
52+
QString mBinary;
5253
};

0 commit comments

Comments
 (0)