Add valgrind and memcheck remote running support.

Merge-request: 284
Reviewed-by: hjk <qtc-committer@nokia.com>
This commit is contained in:
Mike McQuaid
2011-04-04 14:39:27 +02:00
committed by hjk
parent 885bb6ad83
commit 472833ebbd
6 changed files with 407 additions and 32 deletions

View File

@@ -45,6 +45,12 @@
#include <QtNetwork/QTcpSocket> #include <QtNetwork/QTcpSocket>
#include <QtNetwork/QNetworkInterface> #include <QtNetwork/QNetworkInterface>
#include <QtCore/QEventLoop> #include <QtCore/QEventLoop>
#include <QtGui/QDialog>
#include <QtGui/QApplication>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QListWidget>
#include <QtGui/QDialogButtonBox>
using namespace Valgrind::Memcheck; using namespace Valgrind::Memcheck;
@@ -117,6 +123,91 @@ void MemcheckRunner::start()
ValgrindRunner::start(); ValgrindRunner::start();
} }
void MemcheckRunner::startRemotely(const Utils::SshConnectionParameters &sshParams)
{
QTC_ASSERT(d->parser, return);
QList<QHostAddress> possibleHostAddresses;
//NOTE: ::allAddresses does not seem to work for usb interfaces...
foreach(const QNetworkInterface &iface, QNetworkInterface::allInterfaces()) {
foreach(const QNetworkAddressEntry &entry, iface.addressEntries()) {
const QHostAddress addr = entry.ip();
if (addr.toString() != "127.0.0.1"
&& addr.toString() != "0:0:0:0:0:0:0:1")
{
possibleHostAddresses << addr;
break;
}
}
}
QHostAddress hostAddr;
if (possibleHostAddresses.isEmpty()) {
emit processErrorReceived(tr("No network interface found to use for remote analyzation."),
QProcess::FailedToStart);
return;
} else if (possibleHostAddresses.size() > 1) {
QDialog dlg;
dlg.setWindowTitle(tr("Select Network Interface"));
QVBoxLayout *layout = new QVBoxLayout;
QLabel *description = new QLabel;
description->setWordWrap(true);
description->setText(tr("More than one network interface was found on your machine. Please select which one you want to use for remote analyzation."));
layout->addWidget(description);
QListWidget *list = new QListWidget;
foreach(const QHostAddress &address, possibleHostAddresses)
list->addItem(address.toString());
list->setSelectionMode(QAbstractItemView::SingleSelection);
list->setCurrentRow(0);
layout->addWidget(list);
QDialogButtonBox *buttons = new QDialogButtonBox;
buttons->addButton(QDialogButtonBox::Ok);
buttons->addButton(QDialogButtonBox::Cancel);
connect(buttons, SIGNAL(accepted()),
&dlg, SLOT(accept()));
connect(buttons, SIGNAL(rejected()),
&dlg, SLOT(reject()));
layout->addWidget(buttons);
dlg.setLayout(layout);
if (dlg.exec() != QDialog::Accepted)
return;
QTC_ASSERT(list->currentRow() >= 0, return);
QTC_ASSERT(list->currentRow() < possibleHostAddresses.size(), return);
hostAddr = possibleHostAddresses.at(list->currentRow());
} else {
hostAddr = possibleHostAddresses.first();
}
QString ip = hostAddr.toString();
QTC_ASSERT(!ip.isEmpty(), return);
bool check = d->xmlServer.listen(hostAddr);
QTC_ASSERT(check, return);
d->xmlServer.setMaxPendingConnections(1);
const quint16 xmlPortNumber = d->xmlServer.serverPort();
connect(&d->xmlServer, SIGNAL(newConnection()), SLOT(xmlSocketConnected()));
check = d->logServer.listen(hostAddr);
QTC_ASSERT(check, return);
d->logServer.setMaxPendingConnections(1);
const quint16 logPortNumber = d->logServer.serverPort();
connect(&d->logServer, SIGNAL(newConnection()), SLOT(logSocketConnected()));
QStringList memcheckArguments;
memcheckArguments << QString("--xml=yes")
<< QString("--xml-socket=%1:%2").arg(ip, QString::number(xmlPortNumber))
<< QString("--child-silent-after-fork=yes")
<< QString("--log-socket=%1:%2").arg(ip, QString::number(logPortNumber));
setValgrindArguments(memcheckArguments);
ValgrindRunner::startRemotely(sshParams);
}
void MemcheckRunner::xmlSocketConnected() void MemcheckRunner::xmlSocketConnected()
{ {
QTcpSocket *socket = d->xmlServer.nextPendingConnection(); QTcpSocket *socket = d->xmlServer.nextPendingConnection();

View File

@@ -65,6 +65,7 @@ public:
void setParser(XmlProtocol::ThreadedParser *parser); void setParser(XmlProtocol::ThreadedParser *parser);
void start(); void start();
void startRemotely(const Utils::SshConnectionParameters &sshParams);
signals: signals:
void logMessageReceived(const QByteArray &); void logMessageReceived(const QByteArray &);

View File

@@ -40,7 +40,6 @@
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
namespace Valgrind { namespace Valgrind {
namespace Internal {
ValgrindProcess::ValgrindProcess(QObject *parent) ValgrindProcess::ValgrindProcess(QObject *parent)
: QObject(parent) : QObject(parent)
@@ -85,6 +84,11 @@ void LocalValgrindProcess::setWorkingDirectory(const QString &path)
m_process.setWorkingDirectory(path); m_process.setWorkingDirectory(path);
} }
QString LocalValgrindProcess::workingDirectory() const
{
return m_process.workingDirectory();
}
bool LocalValgrindProcess::isRunning() const bool LocalValgrindProcess::isRunning() const
{ {
return m_process.state() != QProcess::NotRunning; return m_process.state() != QProcess::NotRunning;
@@ -97,8 +101,7 @@ void LocalValgrindProcess::setEnvironment(const Utils::Environment &environment)
void LocalValgrindProcess::close() void LocalValgrindProcess::close()
{ {
m_process.close(); m_process.terminate();
m_process.waitForFinished(-1);
} }
void LocalValgrindProcess::run(const QString &valgrindExecutable, const QStringList &valgrindArguments, void LocalValgrindProcess::run(const QString &valgrindExecutable, const QStringList &valgrindArguments,
@@ -106,10 +109,6 @@ void LocalValgrindProcess::run(const QString &valgrindExecutable, const QStringL
{ {
QString arguments; QString arguments;
Utils::QtcProcess::addArgs(&arguments, valgrindArguments); Utils::QtcProcess::addArgs(&arguments, valgrindArguments);
#ifdef Q_OS_MAC
// May be slower to start but without it we get no filenames for symbols.
Utils::QtcProcess::addArg(&arguments, QLatin1String("--dsymutil=yes"));
#endif
Utils::QtcProcess::addArg(&arguments, debuggeeExecutable); Utils::QtcProcess::addArg(&arguments, debuggeeExecutable);
Utils::QtcProcess::addArgs(&arguments, debuggeeArguments); Utils::QtcProcess::addArgs(&arguments, debuggeeArguments);
@@ -117,6 +116,7 @@ void LocalValgrindProcess::run(const QString &valgrindExecutable, const QStringL
m_process.setCommand(valgrindExecutable, arguments); m_process.setCommand(valgrindExecutable, arguments);
m_process.start(); m_process.start();
m_process.waitForStarted(); m_process.waitForStarted();
m_pid = m_process.pid();
} }
QString LocalValgrindProcess::errorString() const QString LocalValgrindProcess::errorString() const
@@ -131,7 +131,7 @@ QProcess::ProcessError LocalValgrindProcess::error() const
Q_PID LocalValgrindProcess::pid() const Q_PID LocalValgrindProcess::pid() const
{ {
return m_process.pid(); return m_pid;
} }
void LocalValgrindProcess::readyReadStandardError() void LocalValgrindProcess::readyReadStandardError()
@@ -148,5 +148,218 @@ void LocalValgrindProcess::readyReadStandardOutput()
emit standardOutputReceived(b); emit standardOutputReceived(b);
} }
////////////////////////
RemoteValgrindProcess::RemoteValgrindProcess(const Utils::SshConnectionParameters &sshParams,
QObject *parent)
: ValgrindProcess(parent)
, m_params(sshParams)
, m_error(QProcess::UnknownError)
, m_pid(0)
{
} }
RemoteValgrindProcess::RemoteValgrindProcess(const Utils::SshConnection::Ptr &connection, QObject *parent)
: ValgrindProcess(parent)
, m_params(connection->connectionParameters())
, m_connection(connection)
, m_error(QProcess::UnknownError)
, m_pid(0)
{
}
RemoteValgrindProcess::~RemoteValgrindProcess()
{
}
bool RemoteValgrindProcess::isRunning() const
{
return m_process && m_process->isRunning();
}
void RemoteValgrindProcess::run(const QString &valgrindExecutable, const QStringList &valgrindArguments,
const QString &debuggeeExecutable, const QString &debuggeeArguments)
{
m_valgrindExe = valgrindExecutable;
m_debuggee = debuggeeExecutable;
m_debuggeeArgs = debuggeeArguments;
m_valgrindArgs = valgrindArguments;
// connect to host and wait for connection
if (!m_connection)
m_connection = Utils::SshConnection::create(m_params);
if (m_connection->state() != Utils::SshConnection::Connected) {
connect(m_connection.data(), SIGNAL(connected()),
this, SLOT(connected()));
connect(m_connection.data(), SIGNAL(error(Utils::SshError)),
this, SLOT(error(Utils::SshError)));
m_connection->connectToHost();
} else {
connected();
}
}
void RemoteValgrindProcess::connected()
{
// connected, run command
QString cmd;
if (!m_workingDir.isEmpty())
cmd += QString("cd '%1' && ").arg(m_workingDir);
QString arguments;
Utils::QtcProcess::addArgs(&arguments, m_valgrindArgs);
Utils::QtcProcess::addArg(&arguments, m_debuggee);
Utils::QtcProcess::addArgs(&arguments, m_debuggeeArgs);
cmd += m_valgrindExe + ' ' + arguments;
m_process = m_connection->createRemoteProcess(cmd.toUtf8());
connect(m_process.data(), SIGNAL(errorOutputAvailable(QByteArray)),
this, SIGNAL(standardErrorReceived(QByteArray)));
connect(m_process.data(), SIGNAL(outputAvailable(QByteArray)),
this, SIGNAL(standardOutputReceived(QByteArray)));
connect(m_process.data(), SIGNAL(closed(int)),
this, SLOT(closed(int)));
connect(m_process.data(), SIGNAL(started()),
this, SLOT(processStarted()));
m_process->start();
}
Utils::SshConnection::Ptr RemoteValgrindProcess::connection() const
{
return m_connection;
}
void RemoteValgrindProcess::processStarted()
{
// find out what PID our process has
// NOTE: valgrind cloaks its name,
// e.g.: valgrind --tool=memcheck foobar
// => ps aux, pidof will see valgrind.bin
// => pkill/killall/top... will see memcheck-amd64-linux or similar
// hence we need to do something more complex...
// plain path to exe, m_valgrindExe contains e.g. env vars etc. pp.
const QString proc = m_valgrindExe.split(QLatin1Char(' ')).last();
// sleep required since otherwise we might only match "bash -c..."
// and not the actual valgrind run
const QString cmd = QString("sleep 1; ps ax" // list all processes with aliased name
" | grep '\\b%1.*%2'" // find valgrind process
" | tail -n 1" // limit to single process
// we pick the last one, first would be "bash -c ..."
" | awk '{print $1;}'" // get pid
).arg(proc, QFileInfo(m_debuggee).fileName());
m_findPID = m_connection->createRemoteProcess(cmd.toUtf8());
connect(m_findPID.data(), SIGNAL(errorOutputAvailable(QByteArray)),
this, SIGNAL(standardErrorReceived(QByteArray)));
connect(m_findPID.data(), SIGNAL(outputAvailable(QByteArray)),
this, SLOT(findPIDOutputReceived(QByteArray)));
m_findPID->start();
}
void RemoteValgrindProcess::findPIDOutputReceived(const QByteArray &output)
{
bool ok;
m_pid = output.trimmed().toLongLong(&ok);
if (!ok) {
m_pid = 0;
m_errorString = tr("Could not determine remote PID.");
m_error = QProcess::FailedToStart;
emit ValgrindProcess::error(QProcess::FailedToStart);
close();
} else {
emit started();
}
}
void RemoteValgrindProcess::error(Utils::SshError error)
{
switch(error) {
case Utils::SshTimeoutError:
m_error = QProcess::Timedout;
break;
default:
m_error = QProcess::FailedToStart;
break;
}
m_errorString = m_connection->errorString();
emit ValgrindProcess::error(m_error);
}
void RemoteValgrindProcess::close()
{
if (m_process) {
m_process->closeChannel();
if (m_pid) {
const QString killTemplate = QString("kill -%2 %1" // kill
).arg(m_pid);
const QString niceKill = killTemplate.arg("SIGTERM");
const QString brutalKill = killTemplate.arg("SIGKILL");
const QString remoteCall = niceKill + QLatin1String("; sleep 1; ") + brutalKill;
m_cleanup = m_connection->createRemoteProcess(remoteCall.toUtf8());
m_cleanup->start();
}
}
}
void RemoteValgrindProcess::closed(int status)
{
m_errorString = m_process->errorString();
if (status == Utils::SshRemoteProcess::FailedToStart) {
m_error = QProcess::FailedToStart;
emit ValgrindProcess::error(QProcess::FailedToStart);
} else if (status == Utils::SshRemoteProcess::ExitedNormally) {
emit finished(m_process->exitCode(), QProcess::NormalExit);
} else if (status == Utils::SshRemoteProcess::KilledBySignal) {
m_error = QProcess::Crashed;
emit finished(m_process->exitCode(), QProcess::CrashExit);
}
}
QString RemoteValgrindProcess::errorString() const
{
return m_errorString;
}
QProcess::ProcessError RemoteValgrindProcess::error() const
{
return m_error;
}
void RemoteValgrindProcess::setEnvironment(const Utils::Environment &environment)
{
Q_UNUSED(environment);
///TODO: anything that should/could be done here?
}
void RemoteValgrindProcess::setProcessChannelMode(QProcess::ProcessChannelMode mode)
{
Q_UNUSED(mode);
///TODO: support this by handling the mode internally
}
void RemoteValgrindProcess::setWorkingDirectory(const QString &path)
{
m_workingDir = path;
}
QString RemoteValgrindProcess::workingDirectory() const
{
return m_workingDir;
}
Q_PID RemoteValgrindProcess::pid() const
{
return m_pid;
}
} }

View File

@@ -37,14 +37,17 @@
#define VALGRIND_RUNNER_P_H #define VALGRIND_RUNNER_P_H
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/ssh/sshremoteprocess.h>
#include <utils/ssh/sshconnection.h>
#include "valgrind_global.h"
namespace Valgrind { namespace Valgrind {
namespace Internal {
/** /**
* Abstract process that can be subclassed to supply local and remote valgrind runs * Abstract process that can be subclassed to supply local and remote valgrind runs
*/ */
class ValgrindProcess : public QObject class VALGRINDSHARED_EXPORT ValgrindProcess : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
@@ -62,8 +65,11 @@ public:
virtual void setProcessChannelMode(QProcess::ProcessChannelMode mode) = 0; virtual void setProcessChannelMode(QProcess::ProcessChannelMode mode) = 0;
virtual void setWorkingDirectory(const QString &path) = 0; virtual void setWorkingDirectory(const QString &path) = 0;
virtual QString workingDirectory() const = 0;
virtual void setEnvironment(const Utils::Environment &environment) = 0; virtual void setEnvironment(const Utils::Environment &environment) = 0;
virtual Q_PID pid() const = 0;
signals: signals:
void started(); void started();
void finished(int, QProcess::ExitStatus); void finished(int, QProcess::ExitStatus);
@@ -75,7 +81,7 @@ signals:
/** /**
* Run valgrind on the local machine * Run valgrind on the local machine
*/ */
class LocalValgrindProcess : public ValgrindProcess class VALGRINDSHARED_EXPORT LocalValgrindProcess : public ValgrindProcess
{ {
Q_OBJECT Q_OBJECT
@@ -94,9 +100,10 @@ public:
virtual void setProcessChannelMode(QProcess::ProcessChannelMode mode); virtual void setProcessChannelMode(QProcess::ProcessChannelMode mode);
virtual void setWorkingDirectory(const QString &path); virtual void setWorkingDirectory(const QString &path);
virtual QString workingDirectory() const;
virtual void setEnvironment(const Utils::Environment &environment); virtual void setEnvironment(const Utils::Environment &environment);
Q_PID pid() const; virtual Q_PID pid() const;
private slots: private slots:
void readyReadStandardError(); void readyReadStandardError();
@@ -104,11 +111,64 @@ private slots:
private: private:
Utils::QtcProcess m_process; Utils::QtcProcess m_process;
Q_PID m_pid;
}; };
// remote support will be supplied later /**
* Run valgrind on a remote machine via SSH
*/
class VALGRINDSHARED_EXPORT RemoteValgrindProcess : public ValgrindProcess
{
Q_OBJECT
public:
explicit RemoteValgrindProcess(const Utils::SshConnectionParameters &sshParams,
QObject *parent = 0);
explicit RemoteValgrindProcess(const Utils::SshConnection::Ptr &connection,
QObject *parent = 0);
virtual ~RemoteValgrindProcess();
virtual bool isRunning() const;
virtual void run(const QString &valgrindExecutable, const QStringList &valgrindArguments,
const QString &debuggeeExecutable, const QString &debuggeeArguments);
virtual void close();
virtual QString errorString() const;
QProcess::ProcessError error() const;
virtual void setProcessChannelMode(QProcess::ProcessChannelMode mode);
virtual void setWorkingDirectory(const QString &path);
virtual QString workingDirectory() const;
virtual void setEnvironment(const Utils::Environment &environment);
virtual Q_PID pid() const;
Utils::SshConnection::Ptr connection() const;
private slots:
void closed(int);
void connected();
void error(Utils::SshError error);
void processStarted();
void findPIDOutputReceived(const QByteArray &output);
private:
Utils::SshConnectionParameters m_params;
Utils::SshConnection::Ptr m_connection;
Utils::SshRemoteProcess::Ptr m_process;
Utils::SshRemoteProcess::Ptr m_cleanup;
QString m_workingDir;
QString m_valgrindExe;
QStringList m_valgrindArgs;
QString m_debuggee;
QString m_debuggeeArgs;
QString m_errorString;
QProcess::ProcessError m_error;
Q_PID m_pid;
Utils::SshRemoteProcess::Ptr m_findPID;
};
}
} }
#endif // VALGRIND_RUNNER_P_H #endif // VALGRIND_RUNNER_P_H

View File

@@ -39,6 +39,8 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/ssh/sshconnection.h>
#include <utils/ssh/sshremoteprocess.h>
#include <QtCore/QEventLoop> #include <QtCore/QEventLoop>
@@ -51,19 +53,17 @@ public:
: q(qq), : q(qq),
process(0), process(0),
channelMode(QProcess::SeparateChannels), channelMode(QProcess::SeparateChannels),
finished(false), finished(false)
lastPid(0)
{ {
} }
void run(Internal::ValgrindProcess *process); void run(ValgrindProcess *process);
ValgrindRunner *q; ValgrindRunner *q;
Internal::ValgrindProcess *process; ValgrindProcess *process;
Utils::Environment environment; Utils::Environment environment;
QProcess::ProcessChannelMode channelMode; QProcess::ProcessChannelMode channelMode;
bool finished; bool finished;
Q_PID lastPid;
QString valgrindExecutable; QString valgrindExecutable;
QStringList valgrindArguments; QStringList valgrindArguments;
QString debuggeeExecutable; QString debuggeeExecutable;
@@ -71,7 +71,7 @@ public:
QString workingdir; QString workingdir;
}; };
void ValgrindRunner::Private::run(Internal::ValgrindProcess *_process) void ValgrindRunner::Private::run(ValgrindProcess *_process)
{ {
if (process && process->isRunning()) { if (process && process->isRunning()) {
process->close(); process->close();
@@ -79,7 +79,6 @@ void ValgrindRunner::Private::run(Internal::ValgrindProcess *_process)
process->deleteLater(); process->deleteLater();
} }
lastPid = 0;
QTC_ASSERT(_process, return); QTC_ASSERT(_process, return);
process = _process; process = _process;
@@ -94,6 +93,11 @@ void ValgrindRunner::Private::run(Internal::ValgrindProcess *_process)
QStringList valgrindArgs = valgrindArguments; QStringList valgrindArgs = valgrindArguments;
valgrindArgs << QString("--tool=%1").arg(q->tool()); valgrindArgs << QString("--tool=%1").arg(q->tool());
#ifdef Q_OS_MAC
// May be slower to start but without it we get no filenames for symbols.
valgrindArgs << QLatin1String("--dsymutil=yes");
#endif
QObject::connect(process, SIGNAL(standardOutputReceived(QByteArray)), QObject::connect(process, SIGNAL(standardOutputReceived(QByteArray)),
q, SIGNAL(standardOutputReceived(QByteArray))); q, SIGNAL(standardOutputReceived(QByteArray)));
QObject::connect(process, SIGNAL(standardErrorReceived(QByteArray)), QObject::connect(process, SIGNAL(standardErrorReceived(QByteArray)),
@@ -196,7 +200,12 @@ void ValgrindRunner::waitForFinished() const
void ValgrindRunner::start() void ValgrindRunner::start()
{ {
d->run(new Internal::LocalValgrindProcess(this)); d->run(new LocalValgrindProcess(this));
}
void ValgrindRunner::startRemotely(const Utils::SshConnectionParameters &sshParams)
{
d->run(new RemoteValgrindProcess(sshParams, this));
} }
void ValgrindRunner::processError(QProcess::ProcessError e) void ValgrindRunner::processError(QProcess::ProcessError e)
@@ -227,17 +236,9 @@ void ValgrindRunner::processFinished(int ret, QProcess::ExitStatus status)
void ValgrindRunner::processStarted() void ValgrindRunner::processStarted()
{ {
if (Internal::LocalValgrindProcess *process = dynamic_cast<Internal::LocalValgrindProcess *>(d->process))
d->lastPid = process->pid();
emit started(); emit started();
} }
Q_PID ValgrindRunner::lastPid() const
{
return d->lastPid;
}
QString ValgrindRunner::errorString() const QString ValgrindRunner::errorString() const
{ {
if (d->process) if (d->process)
@@ -251,3 +252,8 @@ void ValgrindRunner::stop()
if (d->process) if (d->process)
d->process->close(); d->process->close();
} }
ValgrindProcess *ValgrindRunner::valgrindProcess() const
{
return d->process;
}

View File

@@ -46,10 +46,13 @@ QT_END_NAMESPACE
namespace Utils { namespace Utils {
class Environment; class Environment;
class SshConnectionParameters;
} }
namespace Valgrind { namespace Valgrind {
class ValgrindProcess;
class VALGRINDSHARED_EXPORT ValgrindRunner : public QObject class VALGRINDSHARED_EXPORT ValgrindRunner : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -77,14 +80,15 @@ public:
QString errorString() const; QString errorString() const;
virtual void start(); virtual void start();
virtual void startRemotely(const Utils::SshConnectionParameters &sshParams);
virtual void stop(); virtual void stop();
ValgrindProcess *valgrindProcess() const;
protected: protected:
virtual QString tool() const = 0; virtual QString tool() const = 0;
Q_PID lastPid() const;
signals: signals:
void standardOutputReceived(const QByteArray &); void standardOutputReceived(const QByteArray &);
void standardErrorReceived(const QByteArray &); void standardErrorReceived(const QByteArray &);