File indexing completed on 2024-04-28 04:38:54

0001 /*
0002     SPDX-FileCopyrightText: 2017 Anton Anikin <anton.anikin@htower.ru>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "job.h"
0008 
0009 #include "debug.h"
0010 #include "globalsettings.h"
0011 #include "utils.h"
0012 
0013 #include <execute/iexecuteplugin.h>
0014 #include <interfaces/icore.h>
0015 #include <interfaces/iuicontroller.h>
0016 #include <util/environmentprofilelist.h>
0017 #include <util/path.h>
0018 
0019 #include <KLocalizedString>
0020 
0021 #include <QFileInfo>
0022 #include <QRegularExpression>
0023 
0024 namespace Heaptrack
0025 {
0026 
0027 Job::Job(KDevelop::ILaunchConfiguration* launchConfig, IExecutePlugin* executePlugin)
0028     : m_pid(-1)
0029 {
0030     Q_ASSERT(launchConfig);
0031     Q_ASSERT(executePlugin);
0032 
0033     QString envProfile = executePlugin->environmentProfileName(launchConfig);
0034     if (envProfile.isEmpty()) {
0035         envProfile = KDevelop::EnvironmentProfileList(KSharedConfig::openConfig()).defaultProfileName();
0036     }
0037     setEnvironmentProfile(envProfile);
0038 
0039     QString errorString;
0040 
0041     m_analyzedExecutable = executePlugin->executable(launchConfig, errorString).toLocalFile();
0042     if (!errorString.isEmpty()) {
0043         setError(-1);
0044         setErrorText(errorString);
0045     }
0046 
0047     QStringList analyzedExecutableArguments = executePlugin->arguments(launchConfig, errorString);
0048     if (!errorString.isEmpty()) {
0049         setError(-1);
0050         setErrorText(errorString);
0051     }
0052 
0053     QUrl workDir = executePlugin->workingDirectory(launchConfig);
0054     if (workDir.isEmpty() || !workDir.isValid()) {
0055         workDir = QUrl::fromLocalFile(QFileInfo(m_analyzedExecutable).absolutePath());
0056     }
0057     setWorkingDirectory(workDir);
0058 
0059     *this << KDevelop::Path(GlobalSettings::heaptrackExecutable()).toLocalFile();
0060     *this << m_analyzedExecutable;
0061     *this << analyzedExecutableArguments;
0062 
0063     setup();
0064 }
0065 
0066 Job::Job(long int pid)
0067     : m_pid(pid)
0068 {
0069     *this << KDevelop::Path(GlobalSettings::heaptrackExecutable()).toLocalFile();
0070     *this << QStringLiteral("-p");
0071     *this << QString::number(m_pid);
0072 
0073     setup();
0074 }
0075 
0076 void Job::setup()
0077 {
0078     setProperties(DisplayStdout);
0079     setProperties(DisplayStderr);
0080     setProperties(PostProcessOutput);
0081 
0082     setCapabilities(Killable);
0083     setStandardToolView(KDevelop::IOutputView::TestView);
0084     setBehaviours(KDevelop::IOutputView::AutoScroll);
0085 
0086     KDevelop::ICore::self()->uiController()->registerStatus(this);
0087     connect(this, &Job::finished, this, [this]() {
0088         emit hideProgress(this);
0089     });
0090 }
0091 
0092 Job::~Job()
0093 {
0094 }
0095 
0096 QString Job::statusName() const
0097 {
0098     QString target = m_pid < 0 ? QFileInfo(m_analyzedExecutable).fileName()
0099                                : QStringLiteral("PID: %1").arg(m_pid);
0100     return i18n("Heaptrack Analysis (%1)", target);
0101 }
0102 
0103 QString Job::resultsFile() const
0104 {
0105     return m_resultsFile;
0106 }
0107 
0108 void Job::start()
0109 {
0110     emit showProgress(this, 0, 0, 0);
0111     OutputExecuteJob::start();
0112 }
0113 
0114 void Job::postProcessStdout(const QStringList& lines)
0115 {
0116     static const auto resultRegex =
0117         QRegularExpression(QStringLiteral("heaptrack output will be written to \\\"(.+)\\\""));
0118 
0119     if (m_resultsFile.isEmpty()) {
0120         QRegularExpressionMatch match;
0121         for (const QString & line : lines) {
0122             match = resultRegex.match(line);
0123             if (match.hasMatch()) {
0124                 m_resultsFile = match.captured(1);
0125                 break;
0126             }
0127         }
0128     }
0129 
0130     OutputExecuteJob::postProcessStdout(lines);
0131 }
0132 
0133 }
0134 
0135 #include "moc_job.cpp"