4 This file is part of GammaRay, the Qt application inspection and
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Milian Wolff <milian.wolff@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "attachhelper.h"
30 #include <QCoreApplication>
36 AttachHelper::AttachHelper(const QString
&gammaray
, const QString
&injector
,
37 const QString
&debuggee
, const QStringList
&arguments
,
40 m_timer(new QTimer(this)),
41 m_proc(new QProcess(this)),
45 m_proc
->setProcessChannelMode(QProcess::ForwardedChannels
);
46 connect(m_proc
, SIGNAL(started()), this, SLOT(processStarted()));
47 connect(m_proc
, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
48 m_proc
->start(debuggee
, arguments
);
51 void AttachHelper::processStarted()
53 // attach randomly after 1-1500 ms
54 qsrand(QDateTime::currentMSecsSinceEpoch());
55 const int timeout
= qrand() % 1500 + 1;
56 qDebug() << "attaching gammaray in" << timeout
<< "ms";
57 m_timer
->setSingleShot(true);
58 connect(m_timer
, SIGNAL(timeout()), this, SLOT(attach()));
59 m_timer
->start(timeout
);
62 void AttachHelper::processFinished(int exitCode
)
67 void AttachHelper::attach()
69 if (m_proc
->state() != QProcess::Running
) {
72 qDebug() << "attaching gammaray";
76 args
<< "-i" << m_injector
<< "-p" << QString::number(m_proc
->pid()->dwProcessId
);
78 args
<< "-i" << m_injector
<< "-p" << QString::number(m_proc
->pid());
81 const int ret
= gammaray
.execute(m_gammaray
, args
);
84 qFatal("could not attach to debuggee");
88 int main(int argc
, char **argv
) {
89 QCoreApplication
app(argc
, argv
);
91 if (app
.arguments().size() < 4) {
92 qWarning() << "usage: " << app
.applicationName()
93 << " GAMMARAY INJECTOR DEBUGGEE [DEBUGGEE_ARGS]";
97 QStringList args
= app
.arguments();
98 // remove path to this bin
101 const QString gammaray
= args
.takeFirst();
103 const QString injector
= args
.takeFirst();
105 const QString debuggee
= args
.takeFirst();
107 AttachHelper
helper(gammaray
, injector
, debuggee
, args
);
112 #include "attachhelper.moc"