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
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "valgrindprocess.h"
#include "valgrindtr.h"
#include "xmlprotocol/parser.h"
#include <solutions/tasking/barrier.h>
#include <solutions/tasking/conditional.h>
#include <solutions/tasking/tasktreerunner.h>
#include <utils/qtcprocess.h>
#include <utils/processinterface.h>
#include <utils/qtcassert.h>
#include <QEventLoop>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
using namespace Tasking;
using namespace Utils;
using namespace Valgrind::XmlProtocol;
namespace Valgrind::Internal {
static CommandLine valgrindCommand(const CommandLine &command,
const QTcpServer &xmlServer,
const QTcpServer &logServer)
{
CommandLine cmd = command;
cmd.addArg("--child-silent-after-fork=yes");
// Workaround for valgrind bug when running vgdb with xml output
// https://bugs.kde.org/show_bug.cgi?id=343902
bool enableXml = true;
auto handleSocketParameter = [&enableXml, &cmd](const QString &prefix,
const QTcpServer &tcpServer)
{
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");
enableXml = false;
} else {
cmd.addArg(QString("%1=%2:%3").arg(prefix).arg(serverAddress.toString())
.arg(tcpServer.serverPort()));
}
};
handleSocketParameter("--xml-socket", xmlServer);
handleSocketParameter("--log-socket", logServer);
if (enableXml)
cmd.addArg("--xml=yes");
return cmd;
}
class ValgrindProcessPrivate : public QObject
{
Q_OBJECT
public:
ValgrindProcessPrivate(ValgrindProcess *owner)
: q(owner)
{
connect(&m_taskTreeRunner, &TaskTreeRunner::done, this, [this](DoneWith result) {
emit q->done(toDoneResult(result == DoneWith::Success));
});
}
Group runRecipe() const;
bool run();
ValgrindProcess *q = nullptr;
CommandLine m_valgrindCommand;
ProcessRunData m_debuggee;
QProcess::ProcessChannelMode m_channelMode = QProcess::SeparateChannels;
QHostAddress m_localServerAddress;
bool m_useTerminal = false;
TaskTreeRunner m_taskTreeRunner;
signals:
void stopRequested();
};
Group ValgrindProcessPrivate::runRecipe() const
{
struct ValgrindStorage {
CommandLine m_valgrindCommand;
std::unique_ptr<QTcpServer> m_xmlServer;
std::unique_ptr<QTcpServer> m_logServer;
std::unique_ptr<QTcpSocket> m_xmlSocket;
};
Storage<ValgrindStorage> storage;
SingleBarrier xmlBarrier;
const auto isSetupValid = [this, storage, xmlBarrier] {
ValgrindStorage *storagePtr = storage.activeStorage();
storagePtr->m_valgrindCommand.setExecutable(m_valgrindCommand.executable());
if (!m_localServerAddress.isNull()) {
Barrier *barrier = xmlBarrier->barrier();
const QString ip = m_localServerAddress.toString();
QTcpServer *xmlServer = new QTcpServer;
storagePtr->m_xmlServer.reset(xmlServer);
connect(xmlServer, &QTcpServer::newConnection, this, [xmlServer, storagePtr, barrier] {
QTcpSocket *socket = xmlServer->nextPendingConnection();
QTC_ASSERT(socket, return);
xmlServer->close();
storagePtr->m_xmlSocket.reset(socket);
barrier->advance(); // Release Parser task
});
if (!xmlServer->listen(m_localServerAddress)) {
emit q->processErrorReceived(Tr::tr("XmlServer on %1:").arg(ip) + ' '
+ xmlServer->errorString(), ProcessResult::StartFailed);
return false;
}
xmlServer->setMaxPendingConnections(1);
QTcpServer *logServer = new QTcpServer;
storagePtr->m_logServer.reset(logServer);
connect(logServer, &QTcpServer::newConnection, this, [this, logServer] {
QTcpSocket *socket = logServer->nextPendingConnection();
QTC_ASSERT(socket, return);
connect(socket, &QIODevice::readyRead, this, [this, socket] {
emit q->logMessageReceived(socket->readAll());
});
logServer->close();
});
if (!logServer->listen(m_localServerAddress)) {
emit q->processErrorReceived(Tr::tr("LogServer on %1:").arg(ip) + ' '
+ logServer->errorString(), ProcessResult::StartFailed);
return false;
}
logServer->setMaxPendingConnections(1);
storagePtr->m_valgrindCommand = valgrindCommand(storagePtr->m_valgrindCommand,
*xmlServer, *logServer);
}
return true;
};
const auto onProcessSetup = [this, storage](Process &process) {
CommandLine cmd = storage->m_valgrindCommand;
cmd.addArgs(m_valgrindCommand.arguments(), CommandLine::Raw);
// consider appending our options last so they override any interfering user-supplied
// options -q as suggested by valgrind manual
if (cmd.executable().osType() == OsTypeMac) {
// May be slower to start but without it we get no filenames for symbols.
cmd.addArg("--dsymutil=yes");
}
cmd.addCommandLineAsArgs(m_debuggee.command);
emit q->appendMessage(cmd.toUserOutput(), NormalMessageFormat);
process.setCommand(cmd);
process.setWorkingDirectory(m_debuggee.workingDirectory);
process.setEnvironment(m_debuggee.environment);
process.setProcessChannelMode(m_channelMode);
process.setTerminalMode(m_useTerminal ? TerminalMode::Run : TerminalMode::Off);
Process *processPtr = &process;
connect(processPtr, &Process::started, this, [this, processPtr] {
emit q->valgrindStarted(processPtr->processId());
});
connect(processPtr, &Process::readyReadStandardOutput, this, [this, processPtr] {
emit q->appendMessage(processPtr->readAllStandardOutput(), StdOutFormat);
});
connect(processPtr, &Process::readyReadStandardError, this, [this, processPtr] {
emit q->appendMessage(processPtr->readAllStandardError(), StdErrFormat);
});
connect(this, &ValgrindProcessPrivate::stopRequested, processPtr, &Process::stop);
};
const auto onProcessDone = [this, storage](const Process &process) {
emit q->processErrorReceived(process.errorString(), process.result());
};
const auto isAddressValid = [this] { return !m_localServerAddress.isNull(); };
const auto onParserSetup = [this, storage](Parser &parser) {
connect(&parser, &Parser::status, q, &ValgrindProcess::status);
connect(&parser, &Parser::error, q, &ValgrindProcess::error);
parser.setSocket(storage->m_xmlSocket.release());
};
const auto onParserDone = [this](const Parser &parser) {
emit q->internalError(parser.errorString());
};
const Group root {
storage,
xmlBarrier,
If (isSetupValid) >> Then {
parallel,
ProcessTask(onProcessSetup, onProcessDone, CallDoneIf::Error),
If (isAddressValid) >> Then {
waitForBarrierTask(xmlBarrier),
ParserTask(onParserSetup, onParserDone, CallDoneIf::Error)
}
} >> Else {
errorItem
}
};
return root;
}
bool ValgrindProcessPrivate::run()
{
m_taskTreeRunner.start(runRecipe());
return m_taskTreeRunner.isRunning();
}
ValgrindProcess::ValgrindProcess(QObject *parent)
: QObject(parent)
, d(new ValgrindProcessPrivate(this))
{}
ValgrindProcess::~ValgrindProcess() = default;
void ValgrindProcess::setValgrindCommand(const CommandLine &command)
{
d->m_valgrindCommand = command;
}
void ValgrindProcess::setDebuggee(const ProcessRunData &debuggee)
{
d->m_debuggee = debuggee;
}
void ValgrindProcess::setProcessChannelMode(QProcess::ProcessChannelMode mode)
{
d->m_channelMode = mode;
}
void ValgrindProcess::setLocalServerAddress(const QHostAddress &localServerAddress)
{
d->m_localServerAddress = localServerAddress;
}
void ValgrindProcess::setUseTerminal(bool on)
{
d->m_useTerminal = on;
}
bool ValgrindProcess::start()
{
return d->run();
}
void ValgrindProcess::stop()
{
emit d->stopRequested();
}
bool ValgrindProcess::runBlocking()
{
bool ok = false;
QEventLoop loop;
const auto finalize = [&loop, &ok](DoneResult result) {
ok = result == DoneResult::Success;
// Refer to the QObject::deleteLater() docs.
QMetaObject::invokeMethod(&loop, [&loop] { loop.quit(); }, Qt::QueuedConnection);
};
connect(this, &ValgrindProcess::done, &loop, finalize);
QTimer::singleShot(0, this, &ValgrindProcess::start);
loop.exec(QEventLoop::ExcludeUserInputEvents);
return ok;
}
} // namespace Valgrind::Internal
#include "valgrindprocess.moc"
|