Commit 032e7dc4 authored by ck's avatar ck
Browse files

Maemo: Factor SSH operations out of run control classes.

Preparation for removing MaemoDebugRunControl.

Reviewed-by: kh1
parent 01fb9cdb
...@@ -57,6 +57,15 @@ struct CORE_EXPORT SshConnectionParameters ...@@ -57,6 +57,15 @@ struct CORE_EXPORT SshConnectionParameters
enum AuthType { AuthByPwd, AuthByKey } authType; enum AuthType { AuthByPwd, AuthByKey } authType;
quint16 port; quint16 port;
}; };
CORE_EXPORT inline bool operator==(const SshConnectionParameters &p1,
const SshConnectionParameters &p2)
{
return p1.host == p2.host && p1.uname == p2.uname
&& p1.authType == p2.authType
&& (p1.authType == SshConnectionParameters::AuthByPwd ?
p1.pwd == p2.pwd : p1.privateKeyFile == p2.privateKeyFile)
&& p1.timeout == p2.timeout && p1.port == p2.port;
}
/* /*
......
...@@ -85,8 +85,7 @@ QString RemotePlainGdbAdapter::fromLocalEncoding(const QByteArray &b) const ...@@ -85,8 +85,7 @@ QString RemotePlainGdbAdapter::fromLocalEncoding(const QByteArray &b) const
void RemotePlainGdbAdapter::handleApplicationOutput(const QByteArray &output) void RemotePlainGdbAdapter::handleApplicationOutput(const QByteArray &output)
{ {
// FIXME: Remote encoding? showMessage(QString::fromUtf8(output), AppOutput);
showMessage(QString::fromLatin1(output), AppOutput);
} }
} // namespace Internal } // namespace Internal
......
...@@ -29,9 +29,9 @@ ...@@ -29,9 +29,9 @@
#include "maemorunconfiguration.h" #include "maemorunconfiguration.h"
#include "maemodeployables.h"
#include "maemodeploystep.h" #include "maemodeploystep.h"
#include "maemoglobal.h" #include "maemoglobal.h"
#include "maemopackagecreationstep.h"
#include "maemorunconfigurationwidget.h" #include "maemorunconfigurationwidget.h"
#include "maemotoolchain.h" #include "maemotoolchain.h"
#include "qemuruntimemanager.h" #include "qemuruntimemanager.h"
...@@ -217,7 +217,7 @@ const QString MaemoRunConfiguration::dumperLib() const ...@@ -217,7 +217,7 @@ const QString MaemoRunConfiguration::dumperLib() const
return qt4bc->qtVersion()->debuggingHelperLibrary(); return qt4bc->qtVersion()->debuggingHelperLibrary();
} }
QString MaemoRunConfiguration::executable() const QString MaemoRunConfiguration::localExecutableFilePath() const
{ {
TargetInformation ti = qt4Target()->qt4Project()->rootProjectNode() TargetInformation ti = qt4Target()->qt4Project()->rootProjectNode()
->targetInformation(m_proFilePath); ->targetInformation(m_proFilePath);
...@@ -227,6 +227,12 @@ QString MaemoRunConfiguration::executable() const ...@@ -227,6 +227,12 @@ QString MaemoRunConfiguration::executable() const
return QDir::cleanPath(ti.workingDir + QLatin1Char('/') + ti.target); return QDir::cleanPath(ti.workingDir + QLatin1Char('/') + ti.target);
} }
QString MaemoRunConfiguration::remoteExecutableFilePath() const
{
return deployStep()->deployables()
->remoteExecutableFilePath(localExecutableFilePath());
}
QString MaemoRunConfiguration::runtimeGdbServerPort() const QString MaemoRunConfiguration::runtimeGdbServerPort() const
{ {
if (Qt4BuildConfiguration *qt4bc = activeQt4BuildConfiguration()) { if (Qt4BuildConfiguration *qt4bc = activeQt4BuildConfiguration()) {
......
...@@ -75,7 +75,8 @@ public: ...@@ -75,7 +75,8 @@ public:
MaemoDeployStep *deployStep() const; MaemoDeployStep *deployStep() const;
QString maddeRoot() const; QString maddeRoot() const;
QString executable() const; QString localExecutableFilePath() const;
QString remoteExecutableFilePath() const;
const QString sysRoot() const; const QString sysRoot() const;
const QString targetRoot() const; const QString targetRoot() const;
const QStringList arguments() const; const QStringList arguments() const;
......
...@@ -83,7 +83,7 @@ MaemoRunConfigurationWidget::MaemoRunConfigurationWidget( ...@@ -83,7 +83,7 @@ MaemoRunConfigurationWidget::MaemoRunConfigurationWidget(
devConfLayout->addWidget(debuggerConfLabel); devConfLayout->addWidget(debuggerConfLabel);
mainLayout->addRow(new QLabel(tr("Device configuration:")), devConfWidget); mainLayout->addRow(new QLabel(tr("Device configuration:")), devConfWidget);
m_executableLabel = new QLabel(m_runConfiguration->executable()); m_executableLabel = new QLabel(m_runConfiguration->localExecutableFilePath());
mainLayout->addRow(tr("Executable:"), m_executableLabel); mainLayout->addRow(tr("Executable:"), m_executableLabel);
m_argsLineEdit = new QLineEdit(m_runConfiguration->arguments().join(" ")); m_argsLineEdit = new QLineEdit(m_runConfiguration->arguments().join(" "));
mainLayout->addRow(tr("Arguments:"), m_argsLineEdit); mainLayout->addRow(tr("Arguments:"), m_argsLineEdit);
...@@ -118,7 +118,7 @@ void MaemoRunConfigurationWidget::argumentsEdited(const QString &text) ...@@ -118,7 +118,7 @@ void MaemoRunConfigurationWidget::argumentsEdited(const QString &text)
void MaemoRunConfigurationWidget::updateTargetInformation() void MaemoRunConfigurationWidget::updateTargetInformation()
{ {
m_executableLabel->setText(m_runConfiguration->executable()); m_executableLabel->setText(m_runConfiguration->localExecutableFilePath());
} }
void MaemoRunConfigurationWidget::deviceConfigurationChanged(const QString &name) void MaemoRunConfigurationWidget::deviceConfigurationChanged(const QString &name)
......
...@@ -34,15 +34,14 @@ ...@@ -34,15 +34,14 @@
#include "maemoruncontrol.h" #include "maemoruncontrol.h"
#include "maemodeployables.h"
#include "maemodeploystep.h" #include "maemodeploystep.h"
#include "maemoglobal.h" #include "maemoglobal.h"
#include "maemorunconfiguration.h" #include "maemorunconfiguration.h"
#include "maemosshrunner.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/ssh/sftpchannel.h> #include <coreplugin/ssh/sftpchannel.h>
#include <coreplugin/ssh/sshconnection.h> #include <coreplugin/ssh/sshconnection.h>
#include <coreplugin/ssh/sshremoteprocess.h>
#include <debugger/debuggerengine.h> #include <debugger/debuggerengine.h>
#include <debugger/debuggerplugin.h> #include <debugger/debuggerplugin.h>
#include <debugger/debuggerrunner.h> #include <debugger/debuggerrunner.h>
...@@ -73,6 +72,8 @@ AbstractMaemoRunControl::AbstractMaemoRunControl(RunConfiguration *rc, QString m ...@@ -73,6 +72,8 @@ AbstractMaemoRunControl::AbstractMaemoRunControl(RunConfiguration *rc, QString m
: RunControl(rc, mode) : RunControl(rc, mode)
, m_runConfig(qobject_cast<MaemoRunConfiguration *>(rc)) , m_runConfig(qobject_cast<MaemoRunConfiguration *>(rc))
, m_devConfig(m_runConfig ? m_runConfig->deviceConfig() : MaemoDeviceConfig()) , m_devConfig(m_runConfig ? m_runConfig->deviceConfig() : MaemoDeviceConfig())
, m_runner(new MaemoSshRunner(this, m_runConfig))
, m_running(false)
{ {
} }
...@@ -80,115 +81,62 @@ AbstractMaemoRunControl::~AbstractMaemoRunControl() ...@@ -80,115 +81,62 @@ AbstractMaemoRunControl::~AbstractMaemoRunControl()
{ {
} }
// TODO: We can cache the connection. We'd have to check if the connection
// is active and its parameters are the same as m_devConfig. If yes,
// skip the connection step and jump right to handleConnected()
void AbstractMaemoRunControl::start() void AbstractMaemoRunControl::start()
{ {
if (!m_devConfig.isValid()) { if (!m_devConfig.isValid()) {
handleError(tr("No device configuration set for run configuration.")); handleError(tr("No device configuration set for run configuration."));
} else { } else {
emit appendMessage(this, tr("Preparing remote side ..."), false);
m_running = true;
m_stopped = false; m_stopped = false;
emit started(); emit started();
m_connection = SshConnection::create(); connect(m_runner, SIGNAL(error(QString)), this,
connect(m_connection.data(), SIGNAL(connected()), this, SLOT(handleSshError(QString)));
SLOT(handleConnected())); connect(m_runner, SIGNAL(readyForExecution()), this,
connect(m_connection.data(), SIGNAL(error(SshError)), this, SLOT(startExecution()));
SLOT(handleConnectionFailure())); connect(m_runner, SIGNAL(remoteErrorOutput(QByteArray)), this,
m_connection->connectToHost(m_devConfig.server); SLOT(handleRemoteErrorOutput(QByteArray)));
connect(m_runner, SIGNAL(remoteOutput(QByteArray)), this,
SLOT(handleRemoteOutput(QByteArray)));
connect(m_runner, SIGNAL(remoteProcessStarted()), this,
SLOT(handleRemoteProcessStarted()));
connect(m_runner, SIGNAL(remoteProcessFinished(int)), this,
SLOT(handleRemoteProcessFinished(int)));
m_runner->start();
} }
} }
void AbstractMaemoRunControl::handleConnected()
{
if (m_stopped)
return;
emit appendMessage(this, tr("Cleaning up remote leftovers first ..."), false);
const QStringList appsToKill = QStringList() << executableFileName()
#ifdef USE_GDBSERVER
<< QLatin1String("gdbserver");
#else
<< QLatin1String("gdb");
#endif
killRemoteProcesses(appsToKill, true);
}
void AbstractMaemoRunControl::handleConnectionFailure()
{
if (m_stopped)
return;
handleError(tr("Could not connect to host: %1")
.arg(m_connection->errorString()));
emit finished();
}
void AbstractMaemoRunControl::stop() void AbstractMaemoRunControl::stop()
{ {
m_stopped = true; m_stopped = true;
if (m_connection && m_connection->state() == SshConnection::Connecting) { if (m_runner)
disconnect(m_connection.data(), 0, this, 0); m_runner->stop();
m_connection->disconnectFromHost(); stopInternal();
emit finished();
} else if (m_initialCleaner && m_initialCleaner->isRunning()) {
disconnect(m_initialCleaner.data(), 0, this, 0);
emit finished();
} else {
stopInternal();
}
}
QString AbstractMaemoRunControl::executableFilePathOnTarget() const
{
const MaemoDeployables * const deployables
= m_runConfig->deployStep()->deployables();
return deployables->remoteExecutableFilePath(m_runConfig->executable());
} }
void AbstractMaemoRunControl::startExecutionIfPossible() void AbstractMaemoRunControl::handleSshError(const QString &error)
{ {
if (executableFilePathOnTarget().isEmpty()) { handleError(error);
handleError(tr("Cannot run: No remote executable set.")); setFinished();
emit finished();
} else {
startExecution();
}
} }
void AbstractMaemoRunControl::startExecution() void AbstractMaemoRunControl::startExecution()
{ {
m_runner = m_connection->createRemoteProcess(remoteCall().toUtf8()); emit appendMessage(this, tr("Starting remote process ..."), false);
connect(m_runner.data(), SIGNAL(started()), this, m_runner->startExecution(QString::fromLocal8Bit("%1 %2 %3")
SLOT(handleRemoteProcessStarted())); .arg(targetCmdLinePrefix()).arg(m_runConfig->remoteExecutableFilePath())
connect(m_runner.data(), SIGNAL(closed(int)), this, .arg(arguments()).toUtf8());
SLOT(handleRemoteProcessFinished(int)));
connect(m_runner.data(), SIGNAL(outputAvailable(QByteArray)), this,
SLOT(handleRemoteOutput(QByteArray)));
connect(m_runner.data(), SIGNAL(errorOutputAvailable(QByteArray)), this,
SLOT(handleRemoteErrorOutput(QByteArray)));
emit appendMessage(this, tr("Starting remote application."), false);
m_runner->start();
} }
void AbstractMaemoRunControl::handleRemoteProcessFinished(int exitStatus) void AbstractMaemoRunControl::handleRemoteProcessFinished(int exitCode)
{ {
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally);
if (m_stopped) if (m_stopped)
return; return;
if (exitStatus == SshRemoteProcess::ExitedNormally) { emit appendMessage(this,
emit appendMessage(this, tr("Finished running remote process. Exit code was %1.").arg(exitCode),
tr("Finished running remote process. Exit code was %1.") false);
.arg(m_runner->exitCode()), false); setFinished();
emit finished();
} else {
handleError(tr("Error running remote process: %1")
.arg(m_runner->errorString()));
}
} }
void AbstractMaemoRunControl::handleRemoteOutput(const QByteArray &output) void AbstractMaemoRunControl::handleRemoteOutput(const QByteArray &output)
...@@ -203,84 +151,17 @@ void AbstractMaemoRunControl::handleRemoteErrorOutput(const QByteArray &output) ...@@ -203,84 +151,17 @@ void AbstractMaemoRunControl::handleRemoteErrorOutput(const QByteArray &output)
bool AbstractMaemoRunControl::isRunning() const bool AbstractMaemoRunControl::isRunning() const
{ {
return m_runner && m_runner->isRunning(); return m_running;
}
void AbstractMaemoRunControl::stopRunning(bool forDebugging)
{
if (m_runner && m_runner->isRunning()) {
disconnect(m_runner.data(), 0, this, 0);
QStringList apps(executableFileName());
if (forDebugging)
apps << QLatin1String("gdbserver");
killRemoteProcesses(apps, false);
emit finished();
}
}
void AbstractMaemoRunControl::killRemoteProcesses(const QStringList &apps,
bool initialCleanup)
{
QString niceKill;
QString brutalKill;
foreach (const QString &app, apps) {
niceKill += QString::fromLocal8Bit("pkill -x %1;").arg(app);
brutalKill += QString::fromLocal8Bit("pkill -x -9 %1;").arg(app);
}
QString remoteCall = niceKill + QLatin1String("sleep 1; ") + brutalKill;
remoteCall.remove(remoteCall.count() - 1, 1); // Get rid of trailing semicolon.
SshRemoteProcess::Ptr proc
= m_connection->createRemoteProcess(remoteCall.toUtf8());
if (initialCleanup) {
m_initialCleaner = proc;
connect(m_initialCleaner.data(), SIGNAL(closed(int)), this,
SLOT(handleInitialCleanupFinished(int)));
} else {
m_stopper = proc;
}
proc->start();
}
void AbstractMaemoRunControl::handleInitialCleanupFinished(int exitStatus)
{
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally);
if (m_stopped)
return;
if (exitStatus != SshRemoteProcess::ExitedNormally) {
handleError(tr("Initial cleanup failed: %1")
.arg(m_initialCleaner->errorString()));
} else {
emit appendMessage(this, tr("Initial cleanup done."), false);
startExecutionIfPossible();
}
}
const QString AbstractMaemoRunControl::executableOnHost() const
{
return m_runConfig->executable();
}
const QString AbstractMaemoRunControl::executableFileName() const
{
return QFileInfo(executableOnHost()).fileName();
}
const QString AbstractMaemoRunControl::uploadDir() const
{
return MaemoGlobal::homeDirOnDevice(m_devConfig.server.uname);
} }
const QString AbstractMaemoRunControl::targetCmdLinePrefix() const const QString AbstractMaemoRunControl::targetCmdLinePrefix() const
{ {
return QString::fromLocal8Bit("%1 chmod a+x %2 && source /etc/profile && DISPLAY=:0.0 ") return QString::fromLocal8Bit("%1 chmod a+x %2 && source /etc/profile && DISPLAY=:0.0 ")
.arg(MaemoGlobal::remoteSudo()).arg(executableFilePathOnTarget()); .arg(MaemoGlobal::remoteSudo())
.arg(m_runConfig->remoteExecutableFilePath());
} }
QString AbstractMaemoRunControl::targetCmdLineSuffix() const QString AbstractMaemoRunControl::arguments() const
{ {
return m_runConfig->arguments().join(" "); return m_runConfig->arguments().join(" ");
} }
...@@ -292,12 +173,10 @@ void AbstractMaemoRunControl::handleError(const QString &errString) ...@@ -292,12 +173,10 @@ void AbstractMaemoRunControl::handleError(const QString &errString)
stop(); stop();
} }
template<typename SshChannel> void AbstractMaemoRunControl::closeSshChannel(SshChannel &channel) void AbstractMaemoRunControl::setFinished()
{ {
if (channel) { m_running = false;
disconnect(channel.data(), 0, this, 0); emit finished();
// channel->closeChannel();
}
} }
...@@ -311,15 +190,9 @@ MaemoRunControl::~MaemoRunControl() ...@@ -311,15 +190,9 @@ MaemoRunControl::~MaemoRunControl()
stop(); stop();
} }
QString MaemoRunControl::remoteCall() const
{
return QString::fromLocal8Bit("%1 %2 %3").arg(targetCmdLinePrefix())
.arg(executableFilePathOnTarget()).arg(targetCmdLineSuffix());
}
void MaemoRunControl::stopInternal() void MaemoRunControl::stopInternal()
{ {
AbstractMaemoRunControl::stopRunning(false); setFinished();
} }
...@@ -331,17 +204,19 @@ MaemoDebugRunControl::MaemoDebugRunControl(RunConfiguration *runConfiguration) ...@@ -331,17 +204,19 @@ MaemoDebugRunControl::MaemoDebugRunControl(RunConfiguration *runConfiguration)
{ {
#ifdef USE_GDBSERVER #ifdef USE_GDBSERVER
m_startParams->startMode = AttachToRemote; m_startParams->startMode = AttachToRemote;
m_startParams->executable = executableOnHost(); m_startParams->executable = m_runConfig->localExecutableFilePath();
m_startParams->debuggerCommand = m_runConfig->gdbCmd(); m_startParams->debuggerCommand = m_runConfig->gdbCmd();
m_startParams->remoteChannel m_startParams->remoteChannel
= m_devConfig.server.host % QLatin1Char(':') % gdbServerPort(); = m_devConfig.server.host % QLatin1Char(':') % gdbServerPort();
m_startParams->remoteArchitecture = QLatin1String("arm"); m_startParams->remoteArchitecture = QLatin1String("arm");
m_runner->addProcsToKill(QStringList() << QLatin1String("gdbserver"));
#else #else
m_startParams->startMode = StartRemoteGdb; m_startParams->startMode = StartRemoteGdb;
m_startParams->executable = executableFilePathOnTarget(); m_startParams->executable = m_runConfig->remoteExecutableFilePath();
m_startParams->debuggerCommand = targetCmdLinePrefix() m_startParams->debuggerCommand = targetCmdLinePrefix()
+ QLatin1String(" /usr/bin/gdb"); + QLatin1String(" /usr/bin/gdb");
m_startParams->connParams = m_devConfig.server; m_startParams->connParams = m_devConfig.server;
m_runner->addProcsToKill(QStringList() << QLatin1String("gdb"));
#endif #endif
m_startParams->processArgs = m_runConfig->arguments(); m_startParams->processArgs = m_runConfig->arguments();
m_startParams->sysRoot = m_runConfig->sysRoot(); m_startParams->sysRoot = m_runConfig->sysRoot();
...@@ -362,20 +237,13 @@ MaemoDebugRunControl::~MaemoDebugRunControl() ...@@ -362,20 +237,13 @@ MaemoDebugRunControl::~MaemoDebugRunControl()
stop(); stop();
} }
QString MaemoDebugRunControl::remoteCall() const
{
return QString::fromLocal8Bit("%1 gdbserver :%2 %3 %4")
.arg(targetCmdLinePrefix()).arg(gdbServerPort())
.arg(executableFilePathOnTarget()).arg(targetCmdLineSuffix());
}
void MaemoDebugRunControl::startExecution() void MaemoDebugRunControl::startExecution()
{ {
const QString &dumperLib = m_runConfig->dumperLib(); const QString &dumperLib = m_runConfig->dumperLib();
if (!dumperLib.isEmpty() if (!dumperLib.isEmpty()
&& m_runConfig->deployStep()->currentlyNeedsDeployment(m_devConfig.server.host, && m_runConfig->deployStep()->currentlyNeedsDeployment(m_devConfig.server.host,
MaemoDeployable(dumperLib, uploadDir()))) { MaemoDeployable(dumperLib, uploadDir()))) {
m_uploader = m_connection->createSftpChannel(); m_uploader = m_runner->connection()->createSftpChannel();
connect(m_uploader.data(), SIGNAL(initialized()), this, connect(m_uploader.data(), SIGNAL(initialized()), this,
SLOT(handleSftpChannelInitialized())); SLOT(handleSftpChannelInitialized()));
connect(m_uploader.data(), SIGNAL(initializationFailed(QString)), this, connect(m_uploader.data(), SIGNAL(initializationFailed(QString)), this,
...@@ -438,7 +306,10 @@ void MaemoDebugRunControl::handleSftpJobFinished(Core::SftpJobId job, ...@@ -438,7 +306,10 @@ void MaemoDebugRunControl::handleSftpJobFinished(Core::SftpJobId job,
void MaemoDebugRunControl::startDebugging() void MaemoDebugRunControl::startDebugging()
{ {
#ifdef USE_GDBSERVER #ifdef USE_GDBSERVER
AbstractMaemoRunControl::startExecution(); m_runner->startExecution(QString::fromLocal8Bit("%1 gdbserver :%2 %3 %4")
.arg(targetCmdLinePrefix()).arg(gdbServerPort())
.arg(m_runConfig->remoteExecutableFilePath())
.arg(arguments()).toUtf8());
#else #else
DebuggerPlugin::startDebugger(m_debuggerRunControl); DebuggerPlugin::startDebugger(m_debuggerRunControl);
#endif #endif
...@@ -455,27 +326,20 @@ void MaemoDebugRunControl::stopInternal() ...@@ -455,27 +326,20 @@ void MaemoDebugRunControl::stopInternal()
disconnect(m_uploader.data(), 0, this, 0); disconnect(m_uploader.data(), 0, this, 0);
m_uploader->closeChannel(); m_uploader->closeChannel();
m_uploadJob = SftpInvalidJob; m_uploadJob = SftpInvalidJob;
emit finished(); setFinished();
} else if (m_debuggerRunControl && m_debuggerRunControl->engine()) { } else if (m_debuggerRunControl && m_debuggerRunControl->engine()) {
m_debuggerRunControl->engine()->quitDebugger(); m_debuggerRunControl->engine()->quitDebugger();
} else { } else {
emit finished(); setFinished();
} }
} }
bool MaemoDebugRunControl::isRunning() const
{
return isDeploying() || AbstractMaemoRunControl::isRunning()
|| m_debuggerRunControl->state() != DebuggerNotReady;
}
void MaemoDebugRunControl::debuggingFinished()