blob: f28f479b9e3a4b0af9db4d6a9665f51fd8661dbf [file] [log] [blame]
Zack Rusin3acde362011-04-06 01:11:55 -04001#include "retracer.h"
2
Zack Rusinf389ae82011-04-10 19:27:28 -04003#include "apitracecall.h"
4
Zack Rusin3acde362011-04-06 01:11:55 -04005#include <QDebug>
Zack Rusinf389ae82011-04-10 19:27:28 -04006#include <QVariant>
7
8#include <qjson/parser.h>
Zack Rusin3acde362011-04-06 01:11:55 -04009
10Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -040011 : QThread(parent),
Zack Rusin3acde362011-04-06 01:11:55 -040012 m_benchmarking(true),
13 m_doubleBuffered(true),
14 m_captureState(false),
Zack Rusinf389ae82011-04-10 19:27:28 -040015 m_captureCall(0)
Zack Rusin3acde362011-04-06 01:11:55 -040016{
Zack Rusinf389ae82011-04-10 19:27:28 -040017#ifdef Q_OS_WIN
18 QString format = QLatin1String("%1;");
19#else
20 QString format = QLatin1String("%1:");
21#endif
22 QString buildPath = format.arg(BUILD_DIR);
23 m_processEnvironment = QProcessEnvironment::systemEnvironment();
24 m_processEnvironment.insert("PATH", buildPath +
25 m_processEnvironment.value("PATH"));
26
27 qputenv("PATH",
28 m_processEnvironment.value("PATH").toLatin1());
Zack Rusin3acde362011-04-06 01:11:55 -040029}
30
31QString Retracer::fileName() const
32{
33 return m_fileName;
34}
35
36void Retracer::setFileName(const QString &name)
37{
38 m_fileName = name;
39}
40
41bool Retracer::isBenchmarking() const
42{
43 return m_benchmarking;
44}
45
46void Retracer::setBenchmarking(bool bench)
47{
48 m_benchmarking = bench;
49}
50
51bool Retracer::isDoubleBuffered() const
52{
53 return m_doubleBuffered;
54}
55
56void Retracer::setDoubleBuffered(bool db)
57{
58 m_doubleBuffered = db;
59}
60
Zack Rusin3acde362011-04-06 01:11:55 -040061void Retracer::setCaptureAtCallNumber(qlonglong num)
62{
63 m_captureCall = num;
64}
65
66qlonglong Retracer::captureAtCallNumber() const
67{
68 return m_captureCall;
69}
70
71bool Retracer::captureState() const
72{
73 return m_captureState;
74}
75
76void Retracer::setCaptureState(bool enable)
77{
78 m_captureState = enable;
79}
80
Zack Rusinf389ae82011-04-10 19:27:28 -040081
82void Retracer::run()
83{
84 RetraceProcess *retrace = new RetraceProcess();
85 retrace->process()->setProcessEnvironment(m_processEnvironment);
86
87 retrace->setFileName(m_fileName);
88 retrace->setBenchmarking(m_benchmarking);
89 retrace->setDoubleBuffered(m_doubleBuffered);
90 retrace->setCaptureState(m_captureState);
91 retrace->setCaptureAtCallNumber(m_captureCall);
92
93 connect(retrace, SIGNAL(finished(const QString&)),
94 this, SLOT(cleanup()));
95 connect(retrace, SIGNAL(error(const QString&)),
96 this, SLOT(cleanup()));
97 connect(retrace, SIGNAL(finished(const QString&)),
98 this, SIGNAL(finished(const QString&)));
99 connect(retrace, SIGNAL(error(const QString&)),
100 this, SIGNAL(error(const QString&)));
101 connect(retrace, SIGNAL(foundState(const ApiTraceState&)),
102 this, SIGNAL(foundState(const ApiTraceState&)));
103
104 retrace->start();
105
106 exec();
107
108 /* means we need to kill the process */
109 if (retrace->process()->state() != QProcess::NotRunning) {
110 retrace->terminate();
111 }
112
113 delete retrace;
114}
115
116
117void RetraceProcess::start()
118{
119 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400120
121 if (m_doubleBuffered) {
122 arguments << QLatin1String("-db");
123 }
124
Zack Rusinf389ae82011-04-10 19:27:28 -0400125 if (m_captureState) {
126 arguments << QLatin1String("-D");
127 arguments << QString::number(m_captureCall);
128 } else {
129 if (m_benchmarking) {
130 arguments << QLatin1String("-b");
131 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400132 }
133
134 arguments << m_fileName;
135
136 m_process->start(QLatin1String("glretrace"), arguments);
137}
138
139
140void RetraceProcess::replayFinished()
Zack Rusin3acde362011-04-06 01:11:55 -0400141{
142 QByteArray output = m_process->readAllStandardOutput();
Zack Rusinf389ae82011-04-10 19:27:28 -0400143 QString msg;
Zack Rusin3acde362011-04-06 01:11:55 -0400144
145#if 0
146 qDebug()<<"Process finished = ";
147 qDebug()<<"\terr = "<<m_process->readAllStandardError();
148 qDebug()<<"\tout = "<<output;
149#endif
Zack Rusinf389ae82011-04-10 19:27:28 -0400150 if (m_captureState) {
151 bool ok = false;
152 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
153 ApiTraceState state(parsedJson);
154 emit foundState(state);
155 msg = tr("State fetched.");
156 } else {
157 msg = QString::fromUtf8(output);
158 }
159
160 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400161}
162
Zack Rusinf389ae82011-04-10 19:27:28 -0400163void RetraceProcess::replayError(QProcess::ProcessError err)
Zack Rusin3acde362011-04-06 01:11:55 -0400164{
165 qDebug()<<"Process error = "<<err;
166 qDebug()<<"\terr = "<<m_process->readAllStandardError();
167 qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
168
169 emit error(
170 tr("Couldn't execute the replay file '%1'").arg(m_fileName));
171}
172
Zack Rusinf389ae82011-04-10 19:27:28 -0400173
174RetraceProcess::RetraceProcess(QObject *parent)
175 : QObject(parent)
176{
177 m_process = new QProcess(this);
178 m_jsonParser = new QJson::Parser();
179
180 connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
181 this, SLOT(replayFinished()));
182 connect(m_process, SIGNAL(error(QProcess::ProcessError)),
183 this, SLOT(replayError(QProcess::ProcessError)));
184}
185
186QProcess * RetraceProcess::process() const
187{
188 return m_process;
189}
190
191QString RetraceProcess::fileName() const
192{
193 return m_fileName;
194}
195
196void RetraceProcess::setFileName(const QString &name)
197{
198 m_fileName = name;
199}
200
201bool RetraceProcess::isBenchmarking() const
202{
203 return m_benchmarking;
204}
205
206void RetraceProcess::setBenchmarking(bool bench)
207{
208 m_benchmarking = bench;
209}
210
211bool RetraceProcess::isDoubleBuffered() const
212{
213 return m_doubleBuffered;
214}
215
216void RetraceProcess::setDoubleBuffered(bool db)
217{
218 m_doubleBuffered = db;
219}
220
221void RetraceProcess::setCaptureAtCallNumber(qlonglong num)
222{
223 m_captureCall = num;
224}
225
226qlonglong RetraceProcess::captureAtCallNumber() const
227{
228 return m_captureCall;
229}
230
231bool RetraceProcess::captureState() const
232{
233 return m_captureState;
234}
235
236void RetraceProcess::setCaptureState(bool enable)
237{
238 m_captureState = enable;
239}
240
241void RetraceProcess::terminate()
242{
243 if (m_process) {
244 m_process->terminate();
245 emit finished(tr("Process terminated."));
246 }
247}
248
249void Retracer::cleanup()
250{
251 quit();
252}
253
254RetraceProcess::~RetraceProcess()
255{
256 delete m_jsonParser;
257}
258
Zack Rusin3acde362011-04-06 01:11:55 -0400259#include "retracer.moc"