2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-03-04 12:15:18 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
2011-03-04 12:15:18 +01:00
|
|
|
** Author: Milian Wolff, KDAB (milian.wolff@kdab.com)
|
2016-01-15 14:57:40 +01:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-03-04 12:15:18 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-03-04 12:15:18 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-03-04 12:15:18 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-03-04 12:15:18 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-03-04 12:15:18 +01:00
|
|
|
|
|
|
|
|
#include "valgrindrunner.h"
|
|
|
|
|
#include "valgrindprocess.h"
|
|
|
|
|
|
2017-06-21 08:08:43 +02:00
|
|
|
#include "xmlprotocol/threadedparser.h"
|
|
|
|
|
|
2016-01-25 17:15:54 +01:00
|
|
|
#include <projectexplorer/runnables.h>
|
|
|
|
|
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/environment.h>
|
|
|
|
|
#include <utils/hostosinfo.h>
|
2011-03-04 12:15:18 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
#include <ssh/sshconnection.h>
|
|
|
|
|
#include <ssh/sshremoteprocess.h>
|
2011-03-04 12:15:18 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QEventLoop>
|
2017-06-21 08:08:43 +02:00
|
|
|
#include <QTcpServer>
|
|
|
|
|
#include <QTcpSocket>
|
2011-03-04 12:15:18 +01:00
|
|
|
|
2016-01-25 17:15:54 +01:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
2011-05-18 17:05:49 +02:00
|
|
|
namespace Valgrind {
|
2011-03-04 12:15:18 +01:00
|
|
|
|
|
|
|
|
class ValgrindRunner::Private
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-06-21 08:08:43 +02:00
|
|
|
ValgrindProcess *process = nullptr;
|
2016-01-25 17:15:54 +01:00
|
|
|
QProcess::ProcessChannelMode channelMode = QProcess::SeparateChannels;
|
|
|
|
|
bool finished = false;
|
2011-03-04 12:15:18 +01:00
|
|
|
QString valgrindExecutable;
|
|
|
|
|
QStringList valgrindArguments;
|
2016-01-25 17:15:54 +01:00
|
|
|
StandardRunnable debuggee;
|
2016-01-30 01:38:58 +01:00
|
|
|
IDevice::ConstPtr device;
|
2017-06-20 18:01:25 +02:00
|
|
|
QString tool;
|
2017-06-21 08:08:43 +02:00
|
|
|
|
|
|
|
|
QTcpServer xmlServer;
|
2017-06-21 09:01:48 +02:00
|
|
|
XmlProtocol::ThreadedParser parser;
|
2017-06-21 08:08:43 +02:00
|
|
|
QTcpServer logServer;
|
|
|
|
|
QTcpSocket *logSocket = nullptr;
|
|
|
|
|
bool disableXml = false;
|
2011-03-04 12:15:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ValgrindRunner::ValgrindRunner(QObject *parent)
|
2016-02-02 16:09:40 +01:00
|
|
|
: QObject(parent), d(new Private)
|
2011-03-04 12:15:18 +01:00
|
|
|
{
|
2017-06-21 08:08:43 +02:00
|
|
|
setToolName("memcheck");
|
2011-03-04 12:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ValgrindRunner::~ValgrindRunner()
|
|
|
|
|
{
|
|
|
|
|
if (d->process && d->process->isRunning()) {
|
|
|
|
|
// make sure we don't delete the thread while it's still running
|
|
|
|
|
waitForFinished();
|
|
|
|
|
}
|
2017-06-21 09:01:48 +02:00
|
|
|
if (d->parser.isRunning()) {
|
2017-06-21 08:08:43 +02:00
|
|
|
// make sure we don't delete the thread while it's still running
|
|
|
|
|
waitForFinished();
|
|
|
|
|
}
|
2011-03-04 12:15:18 +01:00
|
|
|
delete d;
|
|
|
|
|
d = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::setValgrindExecutable(const QString &executable)
|
|
|
|
|
{
|
|
|
|
|
d->valgrindExecutable = executable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ValgrindRunner::valgrindExecutable() const
|
|
|
|
|
{
|
|
|
|
|
return d->valgrindExecutable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::setValgrindArguments(const QStringList &toolArguments)
|
|
|
|
|
{
|
|
|
|
|
d->valgrindArguments = toolArguments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList ValgrindRunner::valgrindArguments() const
|
|
|
|
|
{
|
|
|
|
|
return d->valgrindArguments;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 12:19:48 +02:00
|
|
|
QStringList ValgrindRunner::fullValgrindArguments() const
|
|
|
|
|
{
|
|
|
|
|
QStringList fullArgs = valgrindArguments();
|
2017-06-20 18:01:25 +02:00
|
|
|
fullArgs << QString("--tool=%1").arg(d->tool);
|
2014-04-25 12:19:48 +02:00
|
|
|
if (Utils::HostOsInfo::isMacHost())
|
|
|
|
|
// May be slower to start but without it we get no filenames for symbols.
|
|
|
|
|
fullArgs << QLatin1String("--dsymutil=yes");
|
|
|
|
|
return fullArgs;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 17:15:54 +01:00
|
|
|
void ValgrindRunner::setDebuggee(const StandardRunnable &debuggee)
|
2011-03-04 12:15:18 +01:00
|
|
|
{
|
2016-01-25 17:15:54 +01:00
|
|
|
d->debuggee = debuggee;
|
2014-05-06 00:08:12 +03:00
|
|
|
}
|
|
|
|
|
|
2011-03-04 12:15:18 +01:00
|
|
|
void ValgrindRunner::setProcessChannelMode(QProcess::ProcessChannelMode mode)
|
|
|
|
|
{
|
|
|
|
|
d->channelMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-30 01:38:58 +01:00
|
|
|
void ValgrindRunner::setDevice(const IDevice::ConstPtr &device)
|
2015-06-16 11:48:45 +02:00
|
|
|
{
|
2016-01-30 01:38:58 +01:00
|
|
|
d->device = device;
|
2015-06-16 11:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-30 01:38:58 +01:00
|
|
|
IDevice::ConstPtr ValgrindRunner::device() const
|
2015-06-16 11:48:45 +02:00
|
|
|
{
|
2016-01-30 01:38:58 +01:00
|
|
|
return d->device;
|
2015-06-16 11:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
2011-03-04 12:15:18 +01:00
|
|
|
void ValgrindRunner::waitForFinished() const
|
|
|
|
|
{
|
|
|
|
|
if (d->finished || !d->process)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QEventLoop loop;
|
2015-02-06 12:39:07 +02:00
|
|
|
connect(this, &ValgrindRunner::finished, &loop, &QEventLoop::quit);
|
2011-03-04 12:15:18 +01:00
|
|
|
loop.exec();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:01:25 +02:00
|
|
|
void ValgrindRunner::setToolName(const QString &toolName)
|
|
|
|
|
{
|
|
|
|
|
d->tool = toolName;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 14:19:44 +01:00
|
|
|
bool ValgrindRunner::start()
|
2011-03-04 12:15:18 +01:00
|
|
|
{
|
2017-06-21 08:08:43 +02:00
|
|
|
// FIXME: Remove hack.
|
|
|
|
|
if (d->tool == "memcheck"
|
|
|
|
|
&& device()->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) {
|
|
|
|
|
if (!startServers(QHostAddress::LocalHost))
|
|
|
|
|
return false;
|
|
|
|
|
setValgrindArguments(memcheckLogArguments() + valgrindArguments());
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 16:09:40 +01:00
|
|
|
d->process = new ValgrindProcess(d->device, this);
|
|
|
|
|
d->process->setProcessChannelMode(d->channelMode);
|
|
|
|
|
// consider appending our options last so they override any interfering user-supplied options
|
|
|
|
|
// -q as suggested by valgrind manual
|
|
|
|
|
d->process->setValgrindExecutable(d->valgrindExecutable);
|
|
|
|
|
d->process->setValgrindArguments(fullValgrindArguments());
|
|
|
|
|
d->process->setDebuggee(d->debuggee);
|
|
|
|
|
|
|
|
|
|
QObject::connect(d->process, &ValgrindProcess::processOutput,
|
|
|
|
|
this, &ValgrindRunner::processOutputReceived);
|
|
|
|
|
QObject::connect(d->process, &ValgrindProcess::started,
|
|
|
|
|
this, &ValgrindRunner::started);
|
|
|
|
|
QObject::connect(d->process, &ValgrindProcess::finished,
|
|
|
|
|
this, &ValgrindRunner::processFinished);
|
|
|
|
|
QObject::connect(d->process, &ValgrindProcess::error,
|
|
|
|
|
this, &ValgrindRunner::processError);
|
2016-04-26 14:59:23 +02:00
|
|
|
QObject::connect(d->process, &ValgrindProcess::localHostAddressRetrieved,
|
|
|
|
|
this, &ValgrindRunner::localHostAddressRetrieved);
|
2016-02-02 16:09:40 +01:00
|
|
|
|
2016-03-10 22:38:24 +02:00
|
|
|
d->process->run(d->debuggee.runMode);
|
2017-06-20 18:01:25 +02:00
|
|
|
|
|
|
|
|
emit extraStart();
|
|
|
|
|
|
2012-01-20 14:19:44 +01:00
|
|
|
return true;
|
2011-03-04 12:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::processError(QProcess::ProcessError e)
|
|
|
|
|
{
|
|
|
|
|
if (d->finished)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->finished = true;
|
|
|
|
|
|
|
|
|
|
// make sure we don't wait for the connection anymore
|
|
|
|
|
emit processErrorReceived(errorString(), e);
|
|
|
|
|
emit finished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::processFinished(int ret, QProcess::ExitStatus status)
|
|
|
|
|
{
|
2017-06-20 18:01:25 +02:00
|
|
|
emit extraProcessFinished();
|
|
|
|
|
|
2011-03-04 12:15:18 +01:00
|
|
|
if (d->finished)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->finished = true;
|
|
|
|
|
|
|
|
|
|
// make sure we don't wait for the connection anymore
|
|
|
|
|
emit finished();
|
|
|
|
|
|
|
|
|
|
if (ret != 0 || status == QProcess::CrashExit)
|
2015-02-06 11:41:57 +02:00
|
|
|
emit processErrorReceived(errorString(), d->process->processError());
|
2011-03-04 12:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-26 14:59:23 +02:00
|
|
|
void ValgrindRunner::localHostAddressRetrieved(const QHostAddress &localHostAddress)
|
|
|
|
|
{
|
2017-06-21 08:08:43 +02:00
|
|
|
if (startServers(localHostAddress)) {
|
|
|
|
|
setValgrindArguments(memcheckLogArguments() + valgrindArguments());
|
|
|
|
|
valgrindProcess()->setValgrindArguments(fullValgrindArguments());
|
|
|
|
|
}
|
2016-04-26 14:59:23 +02:00
|
|
|
}
|
|
|
|
|
|
2011-03-04 12:15:18 +01:00
|
|
|
QString ValgrindRunner::errorString() const
|
|
|
|
|
{
|
2017-06-21 08:08:43 +02:00
|
|
|
return d->process ? d->process->errorString() : QString();
|
2011-03-04 12:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::stop()
|
|
|
|
|
{
|
2016-03-02 13:57:37 +01:00
|
|
|
QTC_ASSERT(d->process, finished(); return);
|
|
|
|
|
d->process->close();
|
2011-03-08 13:56:52 +01:00
|
|
|
}
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
ValgrindProcess *ValgrindRunner::valgrindProcess() const
|
|
|
|
|
{
|
|
|
|
|
return d->process;
|
|
|
|
|
}
|
2011-05-18 17:05:49 +02:00
|
|
|
|
2017-06-21 09:01:48 +02:00
|
|
|
XmlProtocol::ThreadedParser *ValgrindRunner::parser() const
|
2017-06-21 08:08:43 +02:00
|
|
|
{
|
2017-06-21 09:01:48 +02:00
|
|
|
return &d->parser;
|
2017-06-21 08:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Workaround for valgrind bug when running vgdb with xml output
|
|
|
|
|
// https://bugs.kde.org/show_bug.cgi?id=343902
|
|
|
|
|
void ValgrindRunner::disableXml()
|
|
|
|
|
{
|
|
|
|
|
d->disableXml = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::xmlSocketConnected()
|
|
|
|
|
{
|
|
|
|
|
QTcpSocket *socket = d->xmlServer.nextPendingConnection();
|
|
|
|
|
QTC_ASSERT(socket, return);
|
|
|
|
|
d->xmlServer.close();
|
2017-06-21 09:01:48 +02:00
|
|
|
d->parser.parse(socket);
|
2017-06-21 08:08:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::logSocketConnected()
|
|
|
|
|
{
|
|
|
|
|
d->logSocket = d->logServer.nextPendingConnection();
|
|
|
|
|
QTC_ASSERT(d->logSocket, return);
|
|
|
|
|
connect(d->logSocket, &QIODevice::readyRead,
|
|
|
|
|
this, &ValgrindRunner::readLogSocket);
|
|
|
|
|
d->logServer.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ValgrindRunner::readLogSocket()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->logSocket, return);
|
|
|
|
|
emit logMessageReceived(d->logSocket->readAll());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ValgrindRunner::startServers(const QHostAddress &localHostAddress)
|
|
|
|
|
{
|
|
|
|
|
bool check = d->xmlServer.listen(localHostAddress);
|
|
|
|
|
const QString ip = localHostAddress.toString();
|
|
|
|
|
if (!check) {
|
|
|
|
|
emit processErrorReceived(tr("XmlServer on %1:").arg(ip) + ' '
|
|
|
|
|
+ d->xmlServer.errorString(), QProcess::FailedToStart );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
d->xmlServer.setMaxPendingConnections(1);
|
|
|
|
|
connect(&d->xmlServer, &QTcpServer::newConnection,
|
|
|
|
|
this, &ValgrindRunner::xmlSocketConnected);
|
|
|
|
|
check = d->logServer.listen(localHostAddress);
|
|
|
|
|
if (!check) {
|
|
|
|
|
emit processErrorReceived(tr("LogServer on %1:").arg(ip) + ' '
|
|
|
|
|
+ d->logServer.errorString(), QProcess::FailedToStart );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
d->logServer.setMaxPendingConnections(1);
|
|
|
|
|
connect(&d->logServer, &QTcpServer::newConnection,
|
|
|
|
|
this, &ValgrindRunner::logSocketConnected);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList ValgrindRunner::memcheckLogArguments() const
|
|
|
|
|
{
|
|
|
|
|
QStringList arguments;
|
|
|
|
|
if (!d->disableXml)
|
|
|
|
|
arguments << QLatin1String("--xml=yes");
|
|
|
|
|
arguments << QString::fromLatin1("--xml-socket=%1:%2")
|
|
|
|
|
.arg(d->xmlServer.serverAddress().toString()).arg(d->xmlServer.serverPort())
|
|
|
|
|
<< QLatin1String("--child-silent-after-fork=yes")
|
|
|
|
|
<< QString::fromLatin1("--log-socket=%1:%2")
|
|
|
|
|
.arg(d->logServer.serverAddress().toString()).arg(d->logServer.serverPort());
|
|
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:05:49 +02:00
|
|
|
} // namespace Valgrind
|