memcheckengine.cpp 6.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6
** Author: Nicolas Arnaud-Cormos, KDAB (nicolas.arnaud-cormos@kdab.com)
**
hjk's avatar
hjk committed
7
** This file is part of Qt Creator.
8
**
hjk's avatar
hjk committed
9 10 11 12
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
15
** use the contact form at http://www.qt.io/contact-us.
16 17
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
18
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
19 20 21 22 23 24
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
25
**
Eike Ziller's avatar
Eike Ziller committed
26 27
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
28 29
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
30
****************************************************************************/
31 32

#include "memcheckengine.h"
33
#include "memchecktool.h"
Orgad Shaneh's avatar
Orgad Shaneh committed
34
#include "valgrindprocess.h"
35
#include "valgrindsettings.h"
Orgad Shaneh's avatar
Orgad Shaneh committed
36 37 38 39 40 41
#include "xmlprotocol/error.h"
#include "xmlprotocol/status.h"

#include <debugger/debuggerkitinformation.h>
#include <debugger/debuggerstartparameters.h>
#include <debugger/debuggerruncontrol.h>
42

Orgad Shaneh's avatar
Orgad Shaneh committed
43 44 45
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/project.h>
46
#include <projectexplorer/projectexplorer.h>
Orgad Shaneh's avatar
Orgad Shaneh committed
47 48
#include <projectexplorer/target.h>
#include <projectexplorer/toolchain.h>
49 50 51

#include <utils/qtcassert.h>

52
using namespace Analyzer;
53
using namespace ProjectExplorer;
54 55
using namespace Valgrind::XmlProtocol;

56
namespace Valgrind {
hjk's avatar
hjk committed
57
namespace Internal {
58

59
MemcheckRunControl::MemcheckRunControl(const AnalyzerStartParameters &sp,
60 61
        RunConfiguration *runConfiguration, Core::Id runMode)
    : ValgrindRunControl(sp, runConfiguration, runMode)
62
{
Montel Laurent's avatar
Montel Laurent committed
63 64 65 66 67 68
    connect(&m_parser, &XmlProtocol::ThreadedParser::error,
            this, &MemcheckRunControl::parserError);
    connect(&m_parser, &XmlProtocol::ThreadedParser::suppressionCount,
            this, &MemcheckRunControl::suppressionCount);
    connect(&m_parser, &XmlProtocol::ThreadedParser::internalError,
            this, &MemcheckRunControl::internalParserError);
69 70
}

71
QString MemcheckRunControl::progressTitle() const
72 73 74 75
{
    return tr("Analyzing Memory");
}

Orgad Shaneh's avatar
Orgad Shaneh committed
76
ValgrindRunner *MemcheckRunControl::runner()
77 78 79 80
{
    return &m_runner;
}

81
bool MemcheckRunControl::startEngine()
82 83 84
{
    m_runner.setParser(&m_parser);

85
    appendMessage(tr("Analyzing memory of %1").arg(executable()) + QLatin1Char('\n'),
86
                        Utils::NormalMessageFormat);
87
    return ValgrindRunControl::startEngine();
88 89
}

90
void MemcheckRunControl::stopEngine()
91
{
92 93
    disconnect(&m_parser, &ThreadedParser::internalError,
               this, &MemcheckRunControl::internalParserError);
94
    ValgrindRunControl::stopEngine();
95 96
}

97
QStringList MemcheckRunControl::toolArguments() const
98 99 100 101
{
    QStringList arguments;
    arguments << QLatin1String("--gen-suppressions=all");

hjk's avatar
hjk committed
102
    QTC_ASSERT(m_settings, return arguments);
103

hjk's avatar
hjk committed
104
    if (m_settings->trackOrigins())
105 106
        arguments << QLatin1String("--track-origins=yes");

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    if (m_settings->showReachable())
        arguments << QLatin1String("--show-reachable=yes");

    QString leakCheckValue;
    switch (m_settings->leakCheckOnFinish()) {
    case ValgrindBaseSettings::LeakCheckOnFinishNo:
        leakCheckValue = QLatin1String("no");
        break;
    case ValgrindBaseSettings::LeakCheckOnFinishYes:
        leakCheckValue = QLatin1String("full");
        break;
    case ValgrindBaseSettings::LeakCheckOnFinishSummaryOnly:
    default:
        leakCheckValue = QLatin1String("summary");
        break;
    }
    arguments << QLatin1String("--leak-check=") + leakCheckValue;

hjk's avatar
hjk committed
125
    foreach (const QString &file, m_settings->suppressionFiles())
126
        arguments << QString::fromLatin1("--suppressions=%1").arg(file);
127

hjk's avatar
hjk committed
128
    arguments << QString::fromLatin1("--num-callers=%1").arg(m_settings->numCallers());
129 130 131
    return arguments;
}

132
QStringList MemcheckRunControl::suppressionFiles() const
133
{
hjk's avatar
hjk committed
134
    return m_settings->suppressionFiles();
135 136
}

Orgad Shaneh's avatar
Orgad Shaneh committed
137 138
MemcheckWithGdbRunControl::MemcheckWithGdbRunControl(const AnalyzerStartParameters &sp,
                                                     RunConfiguration *runConfiguration)
139
    : MemcheckRunControl(sp, runConfiguration, MEMCHECK_WITH_GDB_RUN_MODE)
Orgad Shaneh's avatar
Orgad Shaneh committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
    connect(&m_runner, &Memcheck::MemcheckRunner::started,
            this, &MemcheckWithGdbRunControl::startDebugger);
    connect(&m_runner, &Memcheck::MemcheckRunner::logMessageReceived,
            this, &MemcheckWithGdbRunControl::appendLog);
    disconnect(&m_parser, &ThreadedParser::internalError,
               this, &MemcheckRunControl::internalParserError);
    m_runner.disableXml();
}

QStringList MemcheckWithGdbRunControl::toolArguments() const
{
    return MemcheckRunControl::toolArguments()
            << QLatin1String("--vgdb=yes") << QLatin1String("--vgdb-error=0");
}

void MemcheckWithGdbRunControl::startDebugger()
{
    const qint64 valgrindPid = runner()->valgrindProcess()->pid();
    const AnalyzerStartParameters &mySp = startParameters();

161
    Debugger::DebuggerStartParameters sp;
Orgad Shaneh's avatar
Orgad Shaneh committed
162 163 164 165 166
    sp.executable = mySp.debuggee;
    sp.startMode = Debugger::AttachToRemoteServer;
    sp.displayName = QString::fromLatin1("VGdb %1").arg(valgrindPid);
    sp.remoteChannel = QString::fromLatin1("| vgdb --pid=%1").arg(valgrindPid);
    sp.useContinueInsteadOfRun = true;
167
    sp.expectedSignals.append("SIGTRAP");
Orgad Shaneh's avatar
Orgad Shaneh committed
168 169

    QString errorMessage;
170
    RunControl *gdbRunControl = Debugger::createDebuggerRunControl(sp, runConfiguration(), &errorMessage);
Orgad Shaneh's avatar
Orgad Shaneh committed
171 172 173 174 175 176 177 178 179 180 181
    QTC_ASSERT(gdbRunControl, return);
    connect(gdbRunControl, &RunControl::finished,
            gdbRunControl, &RunControl::deleteLater);
    gdbRunControl->start();
}

void MemcheckWithGdbRunControl::appendLog(const QByteArray &data)
{
    appendMessage(QString::fromUtf8(data), Utils::StdOutFormat);
}

hjk's avatar
hjk committed
182
} // namespace Internal
183
} // namespace Valgrind