summaryrefslogtreecommitdiff
path: root/gui/retracer.cpp
blob: 1d82f588fa1af33c55d9d0ed3c2007a695da68c7 (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
#include "retracer.h"

#include "apitracecall.h"

#include <QDebug>
#include <QVariant>

#include <qjson/parser.h>

Retracer::Retracer(QObject *parent)
    : QThread(parent),
      m_benchmarking(false),
      m_doubleBuffered(true),
      m_captureState(false),
      m_captureCall(0)
{
#ifdef Q_OS_WIN
    QString format = QLatin1String("%1;");
#else
    QString format = QLatin1String("%1:");
#endif
    QString buildPath = format.arg(BUILD_DIR);
    m_processEnvironment = QProcessEnvironment::systemEnvironment();
    m_processEnvironment.insert("PATH", buildPath +
                                m_processEnvironment.value("PATH"));

    qputenv("PATH",
            m_processEnvironment.value("PATH").toLatin1());
}

QString Retracer::fileName() const
{
    return m_fileName;
}

void Retracer::setFileName(const QString &name)
{
    m_fileName = name;
}

bool Retracer::isBenchmarking() const
{
    return m_benchmarking;
}

void Retracer::setBenchmarking(bool bench)
{
    m_benchmarking = bench;
}

bool Retracer::isDoubleBuffered() const
{
    return m_doubleBuffered;
}

void Retracer::setDoubleBuffered(bool db)
{
    m_doubleBuffered = db;
}

void Retracer::setCaptureAtCallNumber(qlonglong num)
{
    m_captureCall = num;
}

qlonglong Retracer::captureAtCallNumber() const
{
    return m_captureCall;
}

bool Retracer::captureState() const
{
    return m_captureState;
}

void Retracer::setCaptureState(bool enable)
{
    m_captureState = enable;
}


void Retracer::run()
{
    RetraceProcess *retrace = new RetraceProcess();
    retrace->process()->setProcessEnvironment(m_processEnvironment);

    retrace->setFileName(m_fileName);
    retrace->setBenchmarking(m_benchmarking);
    retrace->setDoubleBuffered(m_doubleBuffered);
    retrace->setCaptureState(m_captureState);
    retrace->setCaptureAtCallNumber(m_captureCall);

    connect(retrace, SIGNAL(finished(const QString&)),
            this, SLOT(cleanup()));
    connect(retrace, SIGNAL(error(const QString&)),
            this, SLOT(cleanup()));
    connect(retrace, SIGNAL(finished(const QString&)),
            this, SIGNAL(finished(const QString&)));
    connect(retrace, SIGNAL(error(const QString&)),
            this, SIGNAL(error(const QString&)));
    connect(retrace, SIGNAL(foundState(const ApiTraceState&)),
            this, SIGNAL(foundState(const ApiTraceState&)));
    connect(retrace, SIGNAL(retraceErrors(const QList<RetraceError>&)),
            this, SIGNAL(retraceErrors(const QList<RetraceError>&)));

    retrace->start();

    exec();

    /* means we need to kill the process */
    if (retrace->process()->state() != QProcess::NotRunning) {
        retrace->terminate();
    }

    delete retrace;
}


void RetraceProcess::start()
{
    QStringList arguments;

    if (m_doubleBuffered) {
        arguments << QLatin1String("-db");
    }

    if (m_captureState) {
        arguments << QLatin1String("-D");
        arguments << QString::number(m_captureCall);
    } else {
        if (m_benchmarking) {
            arguments << QLatin1String("-b");
        }
    }

    arguments << m_fileName;

    m_process->start(QLatin1String("glretrace"), arguments);
}


void RetraceProcess::replayFinished()
{
    QByteArray output = m_process->readAllStandardOutput();
    QString msg;
    QString errStr = m_process->readAllStandardError();

#if 0
    qDebug()<<"Process finished = ";
    qDebug()<<"\terr = "<<errStr;
    qDebug()<<"\tout = "<<output;
#endif
    if (m_captureState) {
        bool ok = false;
        QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
        ApiTraceState state(parsedJson);
        emit foundState(state);
        msg = tr("State fetched.");
    } else {
        msg = QString::fromUtf8(output);
    }

    QStringList errorLines = errStr.split('\n');
    QList<RetraceError> errors;
    QRegExp regexp("(^\\d+): +(\\b\\w+\\b): (.+$)");
    foreach(QString line, errorLines) {
        if (regexp.indexIn(line) != -1) {
            RetraceError error;
            error.callIndex = regexp.cap(1).toInt();
            error.type = regexp.cap(2);
            error.message = regexp.cap(3);
            errors.append(error);
        }
    }
    if (!errors.isEmpty()) {
        emit retraceErrors(errors);
    }
    emit finished(msg);
}

void RetraceProcess::replayError(QProcess::ProcessError err)
{
    qDebug()<<"Process error = "<<err;
    qDebug()<<"\terr = "<<m_process->readAllStandardError();
    qDebug()<<"\tout = "<<m_process->readAllStandardOutput();

    emit error(
        tr("Couldn't execute the replay file '%1'").arg(m_fileName));
}

Q_DECLARE_METATYPE(QList<RetraceError>);
RetraceProcess::RetraceProcess(QObject *parent)
    : QObject(parent)
{
    m_process = new QProcess(this);
    m_jsonParser = new QJson::Parser();

    qRegisterMetaType<QList<RetraceError> >();

    connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
            this, SLOT(replayFinished()));
    connect(m_process, SIGNAL(error(QProcess::ProcessError)),
            this, SLOT(replayError(QProcess::ProcessError)));
}

QProcess * RetraceProcess::process() const
{
    return m_process;
}

QString RetraceProcess::fileName() const
{
    return m_fileName;
}

void RetraceProcess::setFileName(const QString &name)
{
    m_fileName = name;
}

bool RetraceProcess::isBenchmarking() const
{
    return m_benchmarking;
}

void RetraceProcess::setBenchmarking(bool bench)
{
    m_benchmarking = bench;
}

bool RetraceProcess::isDoubleBuffered() const
{
    return m_doubleBuffered;
}

void RetraceProcess::setDoubleBuffered(bool db)
{
    m_doubleBuffered = db;
}

void RetraceProcess::setCaptureAtCallNumber(qlonglong num)
{
    m_captureCall = num;
}

qlonglong RetraceProcess::captureAtCallNumber() const
{
    return m_captureCall;
}

bool RetraceProcess::captureState() const
{
    return m_captureState;
}

void RetraceProcess::setCaptureState(bool enable)
{
    m_captureState = enable;
}

void RetraceProcess::terminate()
{
    if (m_process) {
        m_process->terminate();
        emit finished(tr("Process terminated."));
    }
}

void Retracer::cleanup()
{
    quit();
}

RetraceProcess::~RetraceProcess()
{
    delete m_jsonParser;
}

#include "retracer.moc"