2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2011-04-04 14:39:29 +02:00
|
|
|
|
|
|
|
|
#include "callgrindengine.h"
|
|
|
|
|
|
2011-07-12 16:47:32 +02:00
|
|
|
#include "valgrindsettings.h"
|
2011-04-04 14:39:29 +02:00
|
|
|
|
|
|
|
|
#include <valgrind/callgrind/callgrindparser.h>
|
2017-06-28 18:45:57 +02:00
|
|
|
#include <valgrind/valgrindrunner.h>
|
2022-07-08 16:08:27 +02:00
|
|
|
#include <valgrind/valgrindtr.h>
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2016-02-24 14:42:52 +01:00
|
|
|
#include <debugger/analyzer/analyzermanager.h>
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2021-08-26 06:58:50 +02:00
|
|
|
#include <utils/filepath.h>
|
2011-04-04 14:39:29 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2021-02-17 11:00:36 +01:00
|
|
|
#include <utils/qtcprocess.h>
|
2022-05-31 12:59:27 +02:00
|
|
|
#include <utils/temporaryfile.h>
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
#define CALLGRIND_CONTROL_DEBUG 0
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
using namespace ProjectExplorer;
|
2017-06-20 18:01:25 +02:00
|
|
|
using namespace Valgrind::Callgrind;
|
2021-08-26 06:58:50 +02:00
|
|
|
using namespace Utils;
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
namespace Valgrind {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2022-05-31 12:59:27 +02:00
|
|
|
const char CALLGRIND_CONTROL_BINARY[] = "callgrind_control";
|
|
|
|
|
|
2018-05-29 13:54:07 +02:00
|
|
|
void setupCallgrindRunner(CallgrindToolRunner *);
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
CallgrindToolRunner::CallgrindToolRunner(RunControl *runControl)
|
2017-04-27 17:23:28 +02:00
|
|
|
: ValgrindToolRunner(runControl)
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
2018-08-21 08:28:27 +02:00
|
|
|
setId("CallgrindToolRunner");
|
2017-06-20 18:01:25 +02:00
|
|
|
|
2022-05-31 13:43:14 +02:00
|
|
|
connect(&m_runner, &ValgrindRunner::valgrindStarted, this, [this](qint64 pid) {
|
|
|
|
|
m_pid = pid;
|
|
|
|
|
});
|
2022-06-17 23:31:54 +02:00
|
|
|
connect(&m_runner, &ValgrindRunner::finished, this, [this] {
|
2017-06-20 18:01:25 +02:00
|
|
|
triggerParse();
|
2022-06-17 23:31:54 +02:00
|
|
|
emit parserDataReady(this);
|
|
|
|
|
});
|
|
|
|
|
connect(&m_parser, &Callgrind::Parser::parserDataReady, this, [this] {
|
|
|
|
|
emit parserDataReady(this);
|
2017-06-20 18:01:25 +02:00
|
|
|
});
|
2017-06-28 18:45:57 +02:00
|
|
|
|
2022-05-31 13:43:14 +02:00
|
|
|
m_valgrindRunnable = runControl->runnable();
|
2018-05-29 13:54:07 +02:00
|
|
|
|
2021-08-26 06:58:50 +02:00
|
|
|
static int fileCount = 100;
|
2022-05-30 17:18:15 +02:00
|
|
|
m_valgrindOutputFile = runControl->workingDirectory() / QString("callgrind.out.f%1").arg(++fileCount);
|
2021-08-26 06:58:50 +02:00
|
|
|
|
2018-05-29 13:54:07 +02:00
|
|
|
setupCallgrindRunner(this);
|
2011-06-30 13:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 12:59:27 +02:00
|
|
|
CallgrindToolRunner::~CallgrindToolRunner()
|
|
|
|
|
{
|
|
|
|
|
cleanupTempFile();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:23:28 +02:00
|
|
|
QStringList CallgrindToolRunner::toolArguments() const
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
2017-06-29 15:38:43 +02:00
|
|
|
QStringList arguments = {"--tool=callgrind"};
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2021-02-18 14:18:34 +01:00
|
|
|
if (m_settings.enableCacheSim.value())
|
2018-12-01 20:56:21 +02:00
|
|
|
arguments << "--cache-sim=yes";
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2021-02-18 14:18:34 +01:00
|
|
|
if (m_settings.enableBranchSim.value())
|
2018-12-01 20:56:21 +02:00
|
|
|
arguments << "--branch-sim=yes";
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2021-02-18 14:18:34 +01:00
|
|
|
if (m_settings.collectBusEvents.value())
|
2018-12-01 20:56:21 +02:00
|
|
|
arguments << "--collect-bus=yes";
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2021-02-18 14:18:34 +01:00
|
|
|
if (m_settings.collectSystime.value())
|
2018-12-01 20:56:21 +02:00
|
|
|
arguments << "--collect-systime=yes";
|
2011-04-04 14:39:29 +02:00
|
|
|
|
|
|
|
|
if (m_markAsPaused)
|
2018-12-01 20:56:21 +02:00
|
|
|
arguments << "--instr-atstart=no";
|
2011-04-04 14:39:29 +02:00
|
|
|
|
|
|
|
|
// add extra arguments
|
2012-02-02 20:26:12 +01:00
|
|
|
if (!m_argumentForToggleCollect.isEmpty())
|
|
|
|
|
arguments << m_argumentForToggleCollect;
|
2011-04-04 14:39:29 +02:00
|
|
|
|
2021-08-26 06:58:50 +02:00
|
|
|
arguments << "--callgrind-out-file=" + m_valgrindOutputFile.path();
|
|
|
|
|
|
2022-05-31 09:17:50 +02:00
|
|
|
arguments << ProcessArgs::splitArgs(m_settings.callgrindArguments.value());
|
2021-02-17 11:00:36 +01:00
|
|
|
|
2011-04-04 14:39:29 +02:00
|
|
|
return arguments;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:23:28 +02:00
|
|
|
QString CallgrindToolRunner::progressTitle() const
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
2022-07-08 16:08:27 +02:00
|
|
|
return Tr::tr("Profiling");
|
2011-04-04 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:23:28 +02:00
|
|
|
void CallgrindToolRunner::start()
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
2022-05-31 09:17:50 +02:00
|
|
|
const FilePath executable = runControl()->commandLine().executable();
|
2022-07-08 16:08:27 +02:00
|
|
|
appendMessage(Tr::tr("Profiling %1").arg(executable.toUserOutput()), NormalMessageFormat);
|
2017-04-27 17:23:28 +02:00
|
|
|
return ValgrindToolRunner::start();
|
2011-04-04 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:23:28 +02:00
|
|
|
void CallgrindToolRunner::setPaused(bool paused)
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
|
|
|
|
if (m_markAsPaused == paused)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_markAsPaused = paused;
|
|
|
|
|
|
2011-05-11 16:26:34 +02:00
|
|
|
// call controller only if it is attached to a valgrind process
|
2017-06-28 18:45:57 +02:00
|
|
|
if (paused)
|
|
|
|
|
pause();
|
|
|
|
|
else
|
|
|
|
|
unpause();
|
2011-04-04 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:23:28 +02:00
|
|
|
void CallgrindToolRunner::setToggleCollectFunction(const QString &toggleCollectFunction)
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
|
|
|
|
if (toggleCollectFunction.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2018-12-01 20:56:21 +02:00
|
|
|
m_argumentForToggleCollect = "--toggle-collect=" + toggleCollectFunction;
|
2011-04-04 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:23:28 +02:00
|
|
|
Callgrind::ParseData *CallgrindToolRunner::takeParserData()
|
2011-04-04 14:39:29 +02:00
|
|
|
{
|
2017-06-20 18:01:25 +02:00
|
|
|
return m_parser.takeData();
|
2011-04-04 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 18:01:25 +02:00
|
|
|
void CallgrindToolRunner::showStatusMessage(const QString &message)
|
|
|
|
|
{
|
|
|
|
|
Debugger::showPermanentStatusMessage(message);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 12:59:27 +02:00
|
|
|
static QString toOptionString(CallgrindToolRunner::Option option)
|
2017-06-20 18:01:25 +02:00
|
|
|
{
|
2022-05-31 12:59:27 +02:00
|
|
|
/* callgrind_control help from v3.9.0
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
|
case CallgrindToolRunner::Dump:
|
|
|
|
|
return QLatin1String("--dump");
|
|
|
|
|
case CallgrindToolRunner::ResetEventCounters:
|
|
|
|
|
return QLatin1String("--zero");
|
|
|
|
|
case CallgrindToolRunner::Pause:
|
|
|
|
|
return QLatin1String("--instr=off");
|
|
|
|
|
case CallgrindToolRunner::UnPause:
|
|
|
|
|
return QLatin1String("--instr=on");
|
|
|
|
|
default:
|
|
|
|
|
return QString(); // never reached
|
|
|
|
|
}
|
2017-06-20 18:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-31 12:59:27 +02:00
|
|
|
void CallgrindToolRunner::run(Option option)
|
2017-06-20 18:01:25 +02:00
|
|
|
{
|
2022-05-31 12:59:27 +02:00
|
|
|
if (m_controllerProcess) {
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Previous command has not yet finished."));
|
2022-05-31 12:59:27 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// save back current running operation
|
|
|
|
|
m_lastOption = option;
|
|
|
|
|
|
|
|
|
|
m_controllerProcess.reset(new QtcProcess);
|
|
|
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
|
case CallgrindToolRunner::Dump:
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Dumping profile data..."));
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
case CallgrindToolRunner::ResetEventCounters:
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Resetting event counters..."));
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
case CallgrindToolRunner::Pause:
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Pausing instrumentation..."));
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
case CallgrindToolRunner::UnPause:
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Unpausing instrumentation..."));
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if CALLGRIND_CONTROL_DEBUG
|
|
|
|
|
m_controllerProcess->setProcessChannelMode(QProcess::ForwardedChannels);
|
|
|
|
|
#endif
|
2022-06-20 15:03:39 +02:00
|
|
|
connect(m_controllerProcess.get(), &QtcProcess::done,
|
2022-05-31 12:59:27 +02:00
|
|
|
this, &CallgrindToolRunner::controllerProcessDone);
|
|
|
|
|
|
|
|
|
|
const FilePath control =
|
|
|
|
|
FilePath(CALLGRIND_CONTROL_BINARY).onDevice(m_valgrindRunnable.command.executable());
|
|
|
|
|
m_controllerProcess->setCommand({control, {toOptionString(option), QString::number(m_pid)}});
|
|
|
|
|
m_controllerProcess->setWorkingDirectory(m_valgrindRunnable.workingDirectory);
|
|
|
|
|
m_controllerProcess->setEnvironment(m_valgrindRunnable.environment);
|
|
|
|
|
m_controllerProcess->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindToolRunner::controllerProcessDone()
|
|
|
|
|
{
|
|
|
|
|
const QString error = m_controllerProcess->errorString();
|
|
|
|
|
const ProcessResult result = m_controllerProcess->result();
|
|
|
|
|
|
|
|
|
|
m_controllerProcess.release()->deleteLater();
|
|
|
|
|
|
|
|
|
|
if (result != ProcessResult::FinishedWithSuccess) {
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("An error occurred while trying to run %1: %2").arg(CALLGRIND_CONTROL_BINARY).arg(error));
|
2022-05-31 12:59:27 +02:00
|
|
|
qWarning() << "Controller exited abnormally:" << error;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this call went fine, we might run another task after this
|
|
|
|
|
switch (m_lastOption) {
|
|
|
|
|
case ResetEventCounters:
|
|
|
|
|
// lets dump the new reset profiling info
|
|
|
|
|
run(Dump);
|
|
|
|
|
return;
|
|
|
|
|
case Pause:
|
2022-05-31 13:43:14 +02:00
|
|
|
m_paused = true;
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
case Dump:
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Callgrind dumped profiling info"));
|
2022-05-31 13:43:14 +02:00
|
|
|
triggerParse();
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
case UnPause:
|
2022-05-31 13:43:14 +02:00
|
|
|
m_paused = false;
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Callgrind unpaused."));
|
2022-05-31 12:59:27 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_lastOption = Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 13:43:14 +02:00
|
|
|
void CallgrindToolRunner::triggerParse()
|
2022-05-31 12:59:27 +02:00
|
|
|
{
|
|
|
|
|
cleanupTempFile();
|
|
|
|
|
{
|
|
|
|
|
TemporaryFile dataFile("callgrind.out");
|
2022-06-08 13:14:27 +03:00
|
|
|
if (!dataFile.open()) {
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Failed opening temp file..."));
|
2022-06-08 13:14:27 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2022-05-31 12:59:27 +02:00
|
|
|
m_hostOutputFile = FilePath::fromString(dataFile.fileName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto afterCopy = [this](bool res) {
|
|
|
|
|
QTC_CHECK(res);
|
|
|
|
|
QTC_ASSERT(m_hostOutputFile.exists(), return);
|
2022-07-08 16:08:27 +02:00
|
|
|
showStatusMessage(Tr::tr("Parsing Profile Data..."));
|
2022-05-31 12:59:27 +02:00
|
|
|
m_parser.parse(m_hostOutputFile);
|
|
|
|
|
};
|
|
|
|
|
m_valgrindOutputFile.asyncCopyFile(afterCopy, m_hostOutputFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CallgrindToolRunner::cleanupTempFile()
|
|
|
|
|
{
|
|
|
|
|
if (!m_hostOutputFile.isEmpty() && m_hostOutputFile.exists())
|
|
|
|
|
m_hostOutputFile.removeFile();
|
|
|
|
|
|
|
|
|
|
m_hostOutputFile.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:45:57 +02:00
|
|
|
} // Internal
|
|
|
|
|
} // Valgrind
|