d/control: Recommend gdb
[gammaray-debian.git] / tests / attachhelper.cpp
blobd7ea363427359c0019063867f8e31356baaf230d
1 /*
2 attachdialog.h
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
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"
26 #ifdef Q_OS_WIN32
27 #include <Windows.h>
28 #endif
30 #include <QCoreApplication>
31 #include <QTimer>
32 #include <QProcess>
33 #include <QDebug>
34 #include <QDateTime>
36 AttachHelper::AttachHelper(const QString &gammaray, const QString &injector,
37 const QString &debuggee, const QStringList &arguments,
38 QObject *parent)
39 : QObject(parent),
40 m_timer(new QTimer(this)),
41 m_proc(new QProcess(this)),
42 m_gammaray(gammaray),
43 m_injector(injector)
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)
64 qApp->exit(exitCode);
67 void AttachHelper::attach()
69 if (m_proc->state() != QProcess::Running) {
70 return;
72 qDebug() << "attaching gammaray";
73 QProcess gammaray;
74 QStringList args;
75 #ifdef Q_OS_WIN32
76 args << "-i" << m_injector << "-p" << QString::number(m_proc->pid()->dwProcessId);
77 #else
78 args << "-i" << m_injector << "-p" << QString::number(m_proc->pid());
79 #endif
80 args << "-nodialogs";
81 const int ret = gammaray.execute(m_gammaray, args);
82 if (ret != 0) {
83 m_proc->kill();
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]";
94 return 1;
97 QStringList args = app.arguments();
98 // remove path to this bin
99 args.removeFirst();
100 // gammaray
101 const QString gammaray = args.takeFirst();
102 // injector
103 const QString injector = args.takeFirst();
104 // app to run
105 const QString debuggee = args.takeFirst();
107 AttachHelper helper(gammaray, injector, debuggee, args);
109 return app.exec();
112 #include "attachhelper.moc"