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(APITRACE_BINARY_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(ApiTraceState*)),
102 this, SIGNAL(foundState(ApiTraceState*)));
103 connect(retrace, SIGNAL(retraceErrors(const QList<ApiTraceError>&)),
104 this, SIGNAL(retraceErrors(const QList<ApiTraceError>&)));
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");
126 arguments << QLatin1String("-sb");
129 if (m_captureState) {
130 arguments << QLatin1String("-D");
131 arguments << QString::number(m_captureCall);
133 if (m_benchmarking) {
134 arguments << QLatin1String("-b");
138 arguments << m_fileName;
140 m_process->start(QLatin1String("glretrace"), arguments);
144 void RetraceProcess::replayFinished()
146 QByteArray output = m_process->readAllStandardOutput();
148 QString errStr = m_process->readAllStandardError();
151 qDebug()<<"Process finished = ";
152 qDebug()<<"\terr = "<<errStr;
153 qDebug()<<"\tout = "<<output;
155 if (m_captureState) {
157 QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
158 ApiTraceState *state = new ApiTraceState(parsedJson);
159 emit foundState(state);
160 msg = tr("State fetched.");
162 msg = QString::fromUtf8(output);
165 QStringList errorLines = errStr.split('\n');
166 QList<ApiTraceError> errors;
167 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): (.+$)");
168 foreach(QString line, errorLines) {
169 if (regexp.indexIn(line) != -1) {
171 error.callIndex = regexp.cap(1).toInt();
172 error.type = regexp.cap(2);
173 error.message = regexp.cap(3);
174 errors.append(error);
177 if (!errors.isEmpty()) {
178 emit retraceErrors(errors);
183 void RetraceProcess::replayError(QProcess::ProcessError err)
185 qDebug()<<"Process error = "<<err;
186 qDebug()<<"\terr = "<<m_process->readAllStandardError();
187 qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
190 tr("Couldn't execute the replay file '%1'").arg(m_fileName));
193 Q_DECLARE_METATYPE(QList<ApiTraceError>);
194 RetraceProcess::RetraceProcess(QObject *parent)
197 m_process = new QProcess(this);
198 m_jsonParser = new QJson::Parser();
200 qRegisterMetaType<QList<ApiTraceError> >();
202 connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
203 this, SLOT(replayFinished()));
204 connect(m_process, SIGNAL(error(QProcess::ProcessError)),
205 this, SLOT(replayError(QProcess::ProcessError)));
208 QProcess * RetraceProcess::process() const
213 QString RetraceProcess::fileName() const
218 void RetraceProcess::setFileName(const QString &name)
223 bool RetraceProcess::isBenchmarking() const
225 return m_benchmarking;
228 void RetraceProcess::setBenchmarking(bool bench)
230 m_benchmarking = bench;
233 bool RetraceProcess::isDoubleBuffered() const
235 return m_doubleBuffered;
238 void RetraceProcess::setDoubleBuffered(bool db)
240 m_doubleBuffered = db;
243 void RetraceProcess::setCaptureAtCallNumber(qlonglong num)
248 qlonglong RetraceProcess::captureAtCallNumber() const
250 return m_captureCall;
253 bool RetraceProcess::captureState() const
255 return m_captureState;
258 void RetraceProcess::setCaptureState(bool enable)
260 m_captureState = enable;
263 void RetraceProcess::terminate()
266 m_process->terminate();
267 emit finished(tr("Process terminated."));
271 void Retracer::cleanup()
276 RetraceProcess::~RetraceProcess()
281 #include "retracer.moc"