aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/valgrind/valgrindrunner.cpp
blob: c5fdc115c678265e9c54327b43f66448033023c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Author: Milian Wolff, KDAB (milian.wolff@kdab.com)
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** 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.
**
** 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.
**
****************************************************************************/

#include "valgrindrunner.h"

#include "xmlprotocol/threadedparser.h"

#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>

#include <QEventLoop>
#include <QTcpServer>
#include <QTcpSocket>

using namespace ProjectExplorer;
using namespace Utils;

namespace Valgrind {

class ValgrindRunner::Private : public QObject
{
public:
    Private(ValgrindRunner *owner) : q(owner) {}

    void run();

    void handleRemoteStderr(const QString &b);
    void handleRemoteStdout(const QString &b);

    void closed(bool success);
    void localProcessStarted();
    void remoteProcessStarted();
    void findPidOutputReceived(const QString &out);

    ValgrindRunner *q;
    Runnable m_debuggee;
    ApplicationLauncher m_valgrindProcess;
    IDevice::ConstPtr m_device;

    ApplicationLauncher m_findPID;

    QString m_valgrindExecutable;
    QStringList m_valgrindArguments;

    QHostAddress localServerAddress;
    QProcess::ProcessChannelMode channelMode = QProcess::SeparateChannels;
    bool m_finished = false;

    QTcpServer xmlServer;
    XmlProtocol::ThreadedParser parser;
    QTcpServer logServer;
    QTcpSocket *logSocket = nullptr;

    // Workaround for valgrind bug when running vgdb with xml output
    // https://bugs.kde.org/show_bug.cgi?id=343902
    bool disableXml = false;
};

void ValgrindRunner::Private::run()
{
    connect(&m_valgrindProcess, &ApplicationLauncher::processExited,
            this, &ValgrindRunner::Private::closed);
    connect(&m_valgrindProcess, &ApplicationLauncher::processStarted,
            this, &ValgrindRunner::Private::localProcessStarted);
    connect(&m_valgrindProcess, &ApplicationLauncher::error,
            q, &ValgrindRunner::processError);
    connect(&m_valgrindProcess, &ApplicationLauncher::appendMessage,
            q, &ValgrindRunner::processOutputReceived);
    connect(&m_valgrindProcess, &ApplicationLauncher::finished,
            q, &ValgrindRunner::finished);

    connect(&m_valgrindProcess, &ApplicationLauncher::remoteStderr,
            this, &ValgrindRunner::Private::handleRemoteStderr);
    connect(&m_valgrindProcess, &ApplicationLauncher::remoteStdout,
            this, &ValgrindRunner::Private::handleRemoteStdout);
    connect(&m_valgrindProcess, &ApplicationLauncher::remoteProcessStarted,
            this, &ValgrindRunner::Private::remoteProcessStarted);

    QStringList fullArgs = m_valgrindArguments;
    if (HostOsInfo::isMacHost())
        // May be slower to start but without it we get no filenames for symbols.
        fullArgs << "--dsymutil=yes";
    fullArgs << m_debuggee.executable;

    Runnable valgrind;
    valgrind.executable = m_valgrindExecutable;
    valgrind.workingDirectory = m_debuggee.workingDirectory;
    valgrind.environment = m_debuggee.environment;
    valgrind.device = m_device;
    valgrind.commandLineArguments = QtcProcess::joinArgs(fullArgs, m_device->osType());
    Utils::QtcProcess::addArgs(&valgrind.commandLineArguments, m_debuggee.commandLineArguments);
    emit q->valgrindExecuted(QtcProcess::quoteArg(valgrind.executable) + ' '
                             + valgrind.commandLineArguments);

    if (m_device->type() == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE)
        m_valgrindProcess.start(valgrind);
    else
        m_valgrindProcess.start(valgrind, m_device);
}

void ValgrindRunner::Private::handleRemoteStderr(const QString &b)
{
    if (!b.isEmpty())
        emit q->processOutputReceived(b, Utils::StdErrFormat);
}

void ValgrindRunner::Private::handleRemoteStdout(const QString &b)
{
    if (!b.isEmpty())
        emit q->processOutputReceived(b, Utils::StdOutFormat);
}

void ValgrindRunner::Private::localProcessStarted()
{
    qint64 pid = m_valgrindProcess.applicationPID().pid();
    emit q->valgrindStarted(pid);
}

void ValgrindRunner::Private::remoteProcessStarted()
{
    // find out what PID our process has

    // NOTE: valgrind cloaks its name,
    // e.g.: valgrind --tool=memcheck foobar
    // => ps aux, pidof will see valgrind.bin
    // => pkill/killall/top... will see memcheck-amd64-linux or similar
    // hence we need to do something more complex...

    // plain path to exe, m_valgrindExe contains e.g. env vars etc. pp.
    const QString proc = m_valgrindExecutable.split(' ').last();

    Runnable findPid;
    findPid.executable = "/bin/sh";
    // sleep required since otherwise we might only match "bash -c..."
    //  and not the actual valgrind run
    findPid.commandLineArguments = QString("-c \""
                                           "sleep 1; ps ax" // list all processes with aliased name
                                           " | grep '\\b%1.*%2'" // find valgrind process
                                           " | tail -n 1" // limit to single process
                                           // we pick the last one, first would be "bash -c ..."
                                           " | awk '{print $1;}'" // get pid
                                           "\""
                                           ).arg(proc, Utils::FileName::fromString(m_debuggee.executable).fileName());

//    m_remote.m_findPID = m_remote.m_connection->createRemoteProcess(cmd.toUtf8());
    connect(&m_findPID, &ApplicationLauncher::remoteStderr,
            this, &ValgrindRunner::Private::handleRemoteStderr);
    connect(&m_findPID, &ApplicationLauncher::remoteStdout,
            this, &ValgrindRunner::Private::findPidOutputReceived);
    m_findPID.start(findPid, m_device);
}

void ValgrindRunner::Private::findPidOutputReceived(const QString &out)
{
    if (out.isEmpty())
        return;
    bool ok;
    qint64 pid = out.trimmed().toLongLong(&ok);
    if (!ok) {
//        m_remote.m_errorString = tr("Could not determine remote PID.");
//        emit ValgrindRunner::Private::error(QProcess::FailedToStart);
//        close();
    } else {
        emit q->valgrindStarted(pid);
    }
}

void ValgrindRunner::Private::closed(bool success)
{
    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;
//        q->processError(QProcess::FailedToStart);
//    } else if (status == QSsh::SshRemoteProcess::NormalExit) {
//        q->processFinished(m_remote.m_process->exitCode(), QProcess::NormalExit);
//    } else if (status == QSsh::SshRemoteProcess::CrashExit) {
//        m_remote.m_error = QProcess::Crashed;
//        q->processFinished(m_remote.m_process->exitCode(), QProcess::CrashExit);
//    }
     q->processFinished(0, QProcess::NormalExit);
}


ValgrindRunner::ValgrindRunner(QObject *parent)
    : QObject(parent), d(new Private(this))
{
}

ValgrindRunner::~ValgrindRunner()
{
    if (d->m_valgrindProcess.isRunning()) {
        // make sure we don't delete the thread while it's still running
        waitForFinished();
    }
    if (d->parser.isRunning()) {
        // make sure we don't delete the thread while it's still running
        waitForFinished();
    }
    delete d;
    d = nullptr;
}

void ValgrindRunner::setValgrindExecutable(const QString &executable)
{
    d->m_valgrindExecutable = executable;
}

void ValgrindRunner::setValgrindArguments(const QStringList &toolArguments)
{
    d->m_valgrindArguments = toolArguments;
}

void ValgrindRunner::setDebuggee(const Runnable &debuggee)
{
    d->m_debuggee = debuggee;
}

void ValgrindRunner::setProcessChannelMode(QProcess::ProcessChannelMode mode)
{
    d->channelMode = mode;
}

void ValgrindRunner::setLocalServerAddress(const QHostAddress &localServerAddress)
{
    d->localServerAddress = localServerAddress;
}

void ValgrindRunner::setDevice(const IDevice::ConstPtr &device)
{
    d->m_device = device;
}

void ValgrindRunner::setUseTerminal(bool on)
{
    d->m_valgrindProcess.setUseTerminal(on);
}

void ValgrindRunner::waitForFinished() const
{
    if (d->m_finished)
        return;

    QEventLoop loop;
    connect(this, &ValgrindRunner::finished, &loop, &QEventLoop::quit);
    loop.exec();
}

static void handleSocketParameter(const QString &prefix, const QTcpServer &tcpServer,
                            bool *useXml, QStringList *arguments)
{
    QHostAddress serverAddress = tcpServer.serverAddress();
    if (serverAddress.protocol() != QAbstractSocket::IPv4Protocol) {
        // Report will end up in the Application Output pane, i.e. not have
        // clickable items, but that's better than nothing.
        qWarning("Need IPv4 for valgrind");
        *useXml = false;
    } else {
        *arguments << QString("%1=%2:%3").arg(prefix).arg(serverAddress.toString())
                                         .arg(tcpServer.serverPort());
    }
}

bool ValgrindRunner::start()
{
    if (!d->localServerAddress.isNull()) {
        if (!startServers())
            return false;

        bool enableXml = !d->disableXml;

        QStringList arguments = {"--child-silent-after-fork=yes"};

        handleSocketParameter("--xml-socket", d->xmlServer, &enableXml, &arguments);
        handleSocketParameter("--log-socket", d->logServer, &enableXml, &arguments);

        if (enableXml)
            arguments << "--xml=yes";

        d->m_valgrindArguments = arguments + d->m_valgrindArguments;
    }

    d->m_valgrindProcess.setProcessChannelMode(d->channelMode);
    // consider appending our options last so they override any interfering user-supplied options
    // -q as suggested by valgrind manual
    d->m_valgrindExecutable = d->m_valgrindExecutable;
    d->run();

    return true;
}

void ValgrindRunner::processError(QProcess::ProcessError e)
{
    if (d->m_finished)
        return;

    d->m_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)
{
    emit extraProcessFinished();

    if (d->m_finished)
        return;

    d->m_finished = true;

    // make sure we don't wait for the connection anymore
    emit finished();

    if (ret != 0 || status == QProcess::CrashExit)
        emit processErrorReceived(errorString(), d->m_valgrindProcess.processError());
}

QString ValgrindRunner::errorString() const
{
    return d->m_valgrindProcess.errorString();
}

void ValgrindRunner::stop()
{
    d->m_valgrindProcess.stop();
}

XmlProtocol::ThreadedParser *ValgrindRunner::parser() const
{
    return &d->parser;
}

void ValgrindRunner::xmlSocketConnected()
{
    QTcpSocket *socket = d->xmlServer.nextPendingConnection();
    QTC_ASSERT(socket, return);
    d->xmlServer.close();
    d->parser.parse(socket);
}

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()
{
    bool check = d->xmlServer.listen(d->localServerAddress);
    const QString ip = d->localServerAddress.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(d->localServerAddress);
    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;
}

} // namespace Valgrind