2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-04-04 14:39:27 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-04 14:39:27 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-04-04 14:39:27 +02: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-04-04 14:39:27 +02: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-05-06 15:05:37 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
#include "callgrindcontroller.h"
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
#include <ssh/sftpchannel.h>
|
|
|
|
|
#include <ssh/sshconnectionmanager.h>
|
2011-04-04 14:39:27 +02:00
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
#include <utils/fileutils.h>
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2011-04-04 14:39:27 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2017-06-28 18:45:57 +02:00
|
|
|
#include <utils/qtcprocess.h>
|
2017-01-19 16:44:22 +01:00
|
|
|
#include <utils/temporaryfile.h>
|
2017-06-28 18:45:57 +02:00
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QEventLoop>
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
#define CALLGRIND_CONTROL_DEBUG 0
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace Utils;
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
namespace Valgrind {
|
|
|
|
|
namespace Callgrind {
|
|
|
|
|
|
2018-12-01 20:56:21 +02:00
|
|
|
const char CALLGRIND_CONTROL_BINARY[] = "callgrind_control";
|
2017-06-28 18:45:57 +02:00
|
|
|
|
|
|
|
|
CallgrindController::CallgrindController()
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CallgrindController::~CallgrindController()
|
|
|
|
|
{
|
|
|
|
|
cleanupTempFile();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
static QString toOptionString(CallgrindController::Option option)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
2014-06-15 22:37:06 +03:00
|
|
|
/* callgrind_control help from v3.9.0
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-h --help Show this help text
|
|
|
|
|
--version Show version
|
|
|
|
|
-s --stat Show statistics
|
|
|
|
|
-b --back Show stack/back trace
|
|
|
|
|
-e [<A>,...] Show event counters for <A>,... (default: all)
|
|
|
|
|
--dump[=<s>] Request a dump optionally using <s> as description
|
|
|
|
|
-z --zero Zero all event counters
|
|
|
|
|
-k --kill Kill
|
|
|
|
|
--instr=<on|off> Switch instrumentation state on/off
|
|
|
|
|
*/
|
|
|
|
|
|
2011-05-11 16:26:34 +02:00
|
|
|
switch (option) {
|
2011-04-04 14:39:27 +02:00
|
|
|
case CallgrindController::Dump:
|
2012-11-26 15:02:17 +02:00
|
|
|
return QLatin1String("--dump");
|
2011-04-04 14:39:27 +02:00
|
|
|
case CallgrindController::ResetEventCounters:
|
2012-11-26 15:02:17 +02:00
|
|
|
return QLatin1String("--zero");
|
2011-04-04 14:39:27 +02:00
|
|
|
case CallgrindController::Pause:
|
2012-11-26 15:02:17 +02:00
|
|
|
return QLatin1String("--instr=off");
|
2011-04-04 14:39:27 +02:00
|
|
|
case CallgrindController::UnPause:
|
2012-11-26 15:02:17 +02:00
|
|
|
return QLatin1String("--instr=on");
|
2011-04-04 14:39:27 +02:00
|
|
|
default:
|
2012-11-26 15:02:17 +02:00
|
|
|
return QString(); // never reached
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindController::run(Option option)
|
|
|
|
|
{
|
2017-06-28 18:45:57 +02:00
|
|
|
if (m_controllerProcess) {
|
2011-04-04 14:39:27 +02:00
|
|
|
emit statusMessage(tr("Previous command has not yet finished."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// save back current running operation
|
|
|
|
|
m_lastOption = option;
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
m_controllerProcess = new ApplicationLauncher;
|
2011-04-04 14:39:27 +02:00
|
|
|
|
2011-05-11 16:26:34 +02:00
|
|
|
switch (option) {
|
2011-04-04 14:39:27 +02:00
|
|
|
case CallgrindController::Dump:
|
|
|
|
|
emit statusMessage(tr("Dumping profile data..."));
|
|
|
|
|
break;
|
|
|
|
|
case CallgrindController::ResetEventCounters:
|
|
|
|
|
emit statusMessage(tr("Resetting event counters..."));
|
|
|
|
|
break;
|
|
|
|
|
case CallgrindController::Pause:
|
|
|
|
|
emit statusMessage(tr("Pausing instrumentation..."));
|
|
|
|
|
break;
|
|
|
|
|
case CallgrindController::UnPause:
|
|
|
|
|
emit statusMessage(tr("Unpausing instrumentation..."));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if CALLGRIND_CONTROL_DEBUG
|
2017-06-28 18:45:57 +02:00
|
|
|
m_controllerProcess->setProcessChannelMode(QProcess::ForwardedChannels);
|
2011-07-05 10:40:37 +02:00
|
|
|
#endif
|
2017-06-28 18:45:57 +02:00
|
|
|
connect(m_controllerProcess, &ApplicationLauncher::processExited,
|
|
|
|
|
this, &CallgrindController::controllerProcessFinished);
|
|
|
|
|
connect(m_controllerProcess, &ApplicationLauncher::error,
|
|
|
|
|
this, &CallgrindController::handleControllerProcessError);
|
|
|
|
|
connect(m_controllerProcess, &ApplicationLauncher::finished,
|
|
|
|
|
this, &CallgrindController::controllerProcessClosed);
|
|
|
|
|
|
2018-05-16 15:42:03 +02:00
|
|
|
Runnable controller = m_valgrindRunnable;
|
2017-06-28 18:45:57 +02:00
|
|
|
controller.executable = CALLGRIND_CONTROL_BINARY;
|
|
|
|
|
controller.commandLineArguments = QString("%1 %2").arg(toOptionString(option)).arg(m_pid);
|
|
|
|
|
|
|
|
|
|
if (!m_valgrindRunnable.device
|
|
|
|
|
|| m_valgrindRunnable.device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE)
|
|
|
|
|
m_controllerProcess->start(controller);
|
|
|
|
|
else
|
|
|
|
|
m_controllerProcess->start(controller, m_valgrindRunnable.device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindController::setValgrindPid(qint64 pid)
|
|
|
|
|
{
|
|
|
|
|
m_pid = pid;
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
void CallgrindController::handleControllerProcessError(QProcess::ProcessError)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
2017-06-28 18:45:57 +02:00
|
|
|
QTC_ASSERT(m_controllerProcess, return);
|
|
|
|
|
const QString error = m_controllerProcess->errorString();
|
2012-11-26 15:02:17 +02:00
|
|
|
emit statusMessage(tr("An error occurred while trying to run %1: %2").arg(CALLGRIND_CONTROL_BINARY).arg(error));
|
2011-04-04 14:39:27 +02:00
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
m_controllerProcess->deleteLater();
|
|
|
|
|
m_controllerProcess = nullptr;
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
void CallgrindController::controllerProcessFinished(int rc, QProcess::ExitStatus status)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
2017-06-28 18:45:57 +02:00
|
|
|
QTC_ASSERT(m_controllerProcess, return);
|
|
|
|
|
const QString error = m_controllerProcess->errorString();
|
2011-04-04 14:39:27 +02:00
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
m_controllerProcess->deleteLater(); // Called directly from finished() signal in m_process
|
|
|
|
|
m_controllerProcess = nullptr;
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
if (rc != 0 || status != QProcess::NormalExit) {
|
|
|
|
|
qWarning() << "Controller exited abnormally:" << error;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this call went fine, we might run another task after this
|
2011-05-11 16:26:34 +02:00
|
|
|
switch (m_lastOption) {
|
2011-04-04 14:39:27 +02:00
|
|
|
case ResetEventCounters:
|
2011-05-10 15:19:38 +02:00
|
|
|
// lets dump the new reset profiling info
|
2011-04-04 14:39:27 +02:00
|
|
|
run(Dump);
|
|
|
|
|
return;
|
|
|
|
|
case Pause:
|
2014-11-02 17:56:43 +02:00
|
|
|
break;
|
2011-04-04 14:39:27 +02:00
|
|
|
case Dump:
|
|
|
|
|
emit statusMessage(tr("Callgrind dumped profiling info"));
|
|
|
|
|
break;
|
|
|
|
|
case UnPause:
|
|
|
|
|
emit statusMessage(tr("Callgrind unpaused."));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit finished(m_lastOption);
|
|
|
|
|
m_lastOption = Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
void CallgrindController::controllerProcessClosed(bool success)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
2017-06-28 18:45:57 +02:00
|
|
|
Q_UNUSED(success);
|
|
|
|
|
// QTC_ASSERT(m_remote.m_process, return);
|
|
|
|
|
|
|
|
|
|
// m_remote.m_errorString = m_remote.m_process->errorString();
|
|
|
|
|
// if (status == QSsh::SshRemoteProcess::FailedToStart) {
|
|
|
|
|
// m_remote.m_error = QProcess::FailedToStart;
|
|
|
|
|
// emit ValgrindProcessX::error(QProcess::FailedToStart);
|
|
|
|
|
// } else if (status == QSsh::SshRemoteProcess::NormalExit) {
|
|
|
|
|
// emit finished(m_remote.m_process->exitCode(), QProcess::NormalExit);
|
|
|
|
|
// } else if (status == QSsh::SshRemoteProcess::CrashExit) {
|
|
|
|
|
// m_remote.m_error = QProcess::Crashed;
|
|
|
|
|
// emit finished(m_remote.m_process->exitCode(), QProcess::CrashExit);
|
|
|
|
|
// }
|
|
|
|
|
controllerProcessFinished(0, QProcess::NormalExit);
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindController::getLocalDataFile()
|
|
|
|
|
{
|
|
|
|
|
// we look for callgrind.out.PID, but there may be updated ones called ~.PID.NUM
|
2017-06-28 18:45:57 +02:00
|
|
|
const QString baseFileName = QString("callgrind.out.%1").arg(m_pid);
|
|
|
|
|
const QString workingDir = m_valgrindRunnable.workingDirectory;
|
2011-04-04 14:39:27 +02:00
|
|
|
// first, set the to-be-parsed file to callgrind.out.PID
|
2017-06-28 18:45:57 +02:00
|
|
|
QString fileName = workingDir.isEmpty() ? baseFileName : (workingDir + '/' + baseFileName);
|
2011-04-04 14:39:27 +02:00
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
if (m_valgrindRunnable.device
|
|
|
|
|
&& m_valgrindRunnable.device->type() != ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE) {
|
2017-06-28 14:53:21 +02:00
|
|
|
// ///TODO: error handling
|
|
|
|
|
// emit statusMessage(tr("Downloading remote profile data..."));
|
|
|
|
|
// m_ssh = m_valgrindProc->connection();
|
|
|
|
|
// // if there are files like callgrind.out.PID.NUM, set it to the most recent one of those
|
|
|
|
|
// QString cmd = QString::fromLatin1("ls -t %1* | head -n 1").arg(fileName);
|
|
|
|
|
// m_findRemoteFile = m_ssh->createRemoteProcess(cmd.toUtf8());
|
|
|
|
|
// connect(m_findRemoteFile.data(), &QSsh::SshRemoteProcess::readyReadStandardOutput,
|
|
|
|
|
// this, &CallgrindController::foundRemoteFile);
|
|
|
|
|
// m_findRemoteFile->start();
|
2011-04-04 14:39:27 +02:00
|
|
|
} else {
|
2018-11-07 23:46:34 +02:00
|
|
|
const QDir dir(workingDir, QString::fromLatin1("%1.*").arg(baseFileName), QDir::Time);
|
|
|
|
|
const QStringList outputFiles = dir.entryList();
|
2011-04-04 14:39:27 +02:00
|
|
|
// if there are files like callgrind.out.PID.NUM, set it to the most recent one of those
|
|
|
|
|
if (!outputFiles.isEmpty())
|
2018-12-01 20:56:21 +02:00
|
|
|
fileName = workingDir + '/' + outputFiles.first();
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
emit localParseDataAvailable(fileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 17:23:51 +01:00
|
|
|
void CallgrindController::foundRemoteFile()
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
2011-11-14 17:23:51 +01:00
|
|
|
m_remoteFile = m_findRemoteFile->readAllStandardOutput().trimmed();
|
2011-04-04 14:39:27 +02:00
|
|
|
|
|
|
|
|
m_sftp = m_ssh->createSftpChannel();
|
2015-02-06 12:39:07 +02:00
|
|
|
connect(m_sftp.data(), &QSsh::SftpChannel::finished,
|
|
|
|
|
this, &CallgrindController::sftpJobFinished);
|
|
|
|
|
connect(m_sftp.data(), &QSsh::SftpChannel::initialized,
|
|
|
|
|
this, &CallgrindController::sftpInitialized);
|
2011-04-04 14:39:27 +02:00
|
|
|
m_sftp->initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindController::sftpInitialized()
|
|
|
|
|
{
|
|
|
|
|
cleanupTempFile();
|
2017-01-19 16:44:22 +01:00
|
|
|
Utils::TemporaryFile dataFile("callgrind.out.");
|
2011-04-04 14:39:27 +02:00
|
|
|
QTC_ASSERT(dataFile.open(), return);
|
|
|
|
|
m_tempDataFile = dataFile.fileName();
|
|
|
|
|
dataFile.setAutoRemove(false);
|
|
|
|
|
dataFile.close();
|
|
|
|
|
|
2014-05-09 12:01:31 +02:00
|
|
|
m_downloadJob = m_sftp->downloadFile(QString::fromUtf8(m_remoteFile), m_tempDataFile, QSsh::SftpOverwriteExisting);
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
void CallgrindController::sftpJobFinished(QSsh::SftpJobId job, const QString &error)
|
2011-04-04 14:39:27 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(job == m_downloadJob, return);
|
|
|
|
|
|
|
|
|
|
m_sftp->closeChannel();
|
|
|
|
|
|
|
|
|
|
if (error.isEmpty())
|
|
|
|
|
emit localParseDataAvailable(m_tempDataFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindController::cleanupTempFile()
|
|
|
|
|
{
|
|
|
|
|
if (!m_tempDataFile.isEmpty() && QFile::exists(m_tempDataFile))
|
|
|
|
|
QFile::remove(m_tempDataFile);
|
|
|
|
|
|
2011-05-10 15:19:38 +02:00
|
|
|
m_tempDataFile.clear();
|
2011-04-04 14:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
void CallgrindController::setValgrindRunnable(const Runnable &runnable)
|
|
|
|
|
{
|
2018-05-16 15:42:03 +02:00
|
|
|
m_valgrindRunnable = runnable;
|
2017-06-28 18:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-18 17:05:49 +02:00
|
|
|
} // namespace Callgrind
|
|
|
|
|
} // namespace Valgrind
|