3 #include "apitracecall.h"
8 #include <qjson/parser.h>
10 Retracer::Retracer(QObject *parent)
12 m_benchmarking(false),
13 m_doubleBuffered(true),
14 m_captureState(false),
18 QString format = QLatin1String("%1;");
20 QString format = QLatin1String("%1:");
22 QString buildPath = format.arg(BUILD_DIR);
23 m_processEnvironment = QProcessEnvironment::systemEnvironment();
24 m_processEnvironment.insert("PATH", buildPath +
25 m_processEnvironment.value("PATH"));
28 m_processEnvironment.value("PATH").toLatin1());
31 QString Retracer::fileName() const
36 void Retracer::setFileName(const QString &name)
41 bool Retracer::isBenchmarking() const
43 return m_benchmarking;
46 void Retracer::setBenchmarking(bool bench)
48 m_benchmarking = bench;
51 bool Retracer::isDoubleBuffered() const
53 return m_doubleBuffered;
56 void Retracer::setDoubleBuffered(bool db)
58 m_doubleBuffered = db;
61 void Retracer::setCaptureAtCallNumber(qlonglong num)
66 qlonglong Retracer::captureAtCallNumber() const
71 bool Retracer::captureState() const
73 return m_captureState;
76 void Retracer::setCaptureState(bool enable)
78 m_captureState = enable;
84 RetraceProcess *retrace = new RetraceProcess();
85 retrace->process()->setProcessEnvironment(m_processEnvironment);
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);
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 connect(retrace, SIGNAL(retraceErrors(const QList<RetraceError>&)),
104 this, SIGNAL(retraceErrors(const QList<RetraceError>&)));
110 /* means we need to kill the process */
111 if (retrace->process()->state() != QProcess::NotRunning) {
112 retrace->terminate();
119 void RetraceProcess::start()
121 QStringList arguments;
123 if (m_doubleBuffered) {
124 arguments << QLatin1String("-db");
127 if (m_captureState) {
128 arguments << QLatin1String("-D");
129 arguments << QString::number(m_captureCall);
131 if (m_benchmarking) {
132 arguments << QLatin1String("-b");
136 arguments << m_fileName;
138 m_process->start(QLatin1String("glretrace"), arguments);
142 void RetraceProcess::replayFinished()
144 QByteArray output = m_process->readAllStandardOutput();
146 QString errStr = m_process->readAllStandardError();
149 qDebug()<<"Process finished = ";
150 qDebug()<<"\terr = "<<errStr;
151 qDebug()<<"\tout = "<<output;
153 if (m_captureState) {
155 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
156 ApiTraceState state(parsedJson);
157 emit foundState(state);
158 msg = tr("State fetched.");
160 msg = QString::fromUtf8(output);
163 QStringList errorLines = errStr.split('\n');
164 QList<RetraceError> errors;
165 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): (.+$)");
166 foreach(QString line, errorLines) {
167 if (regexp.indexIn(line) != -1) {
169 error.callIndex = regexp.cap(1).toInt();
170 error.type = regexp.cap(2);
171 error.message = regexp.cap(3);
172 errors.append(error);
175 if (!errors.isEmpty()) {
176 emit retraceErrors(errors);
181 void RetraceProcess::replayError(QProcess::ProcessError err)
183 qDebug()<<"Process error = "<<err;
184 qDebug()<<"\terr = "<<m_process->readAllStandardError();
185 qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
188 tr("Couldn't execute the replay file '%1'").arg(m_fileName));
191 Q_DECLARE_METATYPE(QList<RetraceError>);
192 RetraceProcess::RetraceProcess(QObject *parent)
195 m_process = new QProcess(this);
196 m_jsonParser = new QJson::Parser();
198 qRegisterMetaType<QList<RetraceError> >();
200 connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
201 this, SLOT(replayFinished()));
202 connect(m_process, SIGNAL(error(QProcess::ProcessError)),
203 this, SLOT(replayError(QProcess::ProcessError)));
206 QProcess * RetraceProcess::process() const
211 QString RetraceProcess::fileName() const
216 void RetraceProcess::setFileName(const QString &name)
221 bool RetraceProcess::isBenchmarking() const
223 return m_benchmarking;
226 void RetraceProcess::setBenchmarking(bool bench)
228 m_benchmarking = bench;
231 bool RetraceProcess::isDoubleBuffered() const
233 return m_doubleBuffered;
236 void RetraceProcess::setDoubleBuffered(bool db)
238 m_doubleBuffered = db;
241 void RetraceProcess::setCaptureAtCallNumber(qlonglong num)
246 qlonglong RetraceProcess::captureAtCallNumber() const
248 return m_captureCall;
251 bool RetraceProcess::captureState() const
253 return m_captureState;
256 void RetraceProcess::setCaptureState(bool enable)
258 m_captureState = enable;
261 void RetraceProcess::terminate()
264 m_process->terminate();
265 emit finished(tr("Process terminated."));
269 void Retracer::cleanup()
274 RetraceProcess::~RetraceProcess()
279 #include "retracer.moc"