Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 1 | #include "retracer.h" |
| 2 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 3 | #include "apitracecall.h" |
| 4 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 5 | #include <QDebug> |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 6 | #include <QVariant> |
| 7 | |
| 8 | #include <qjson/parser.h> |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 9 | |
| 10 | Retracer::Retracer(QObject *parent) |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 11 | : QThread(parent), |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 12 | m_benchmarking(true), |
| 13 | m_doubleBuffered(true), |
| 14 | m_captureState(false), |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 15 | m_captureCall(0) |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 16 | { |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 17 | #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 Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | QString Retracer::fileName() const |
| 32 | { |
| 33 | return m_fileName; |
| 34 | } |
| 35 | |
| 36 | void Retracer::setFileName(const QString &name) |
| 37 | { |
| 38 | m_fileName = name; |
| 39 | } |
| 40 | |
| 41 | bool Retracer::isBenchmarking() const |
| 42 | { |
| 43 | return m_benchmarking; |
| 44 | } |
| 45 | |
| 46 | void Retracer::setBenchmarking(bool bench) |
| 47 | { |
| 48 | m_benchmarking = bench; |
| 49 | } |
| 50 | |
| 51 | bool Retracer::isDoubleBuffered() const |
| 52 | { |
| 53 | return m_doubleBuffered; |
| 54 | } |
| 55 | |
| 56 | void Retracer::setDoubleBuffered(bool db) |
| 57 | { |
| 58 | m_doubleBuffered = db; |
| 59 | } |
| 60 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 61 | void Retracer::setCaptureAtCallNumber(qlonglong num) |
| 62 | { |
| 63 | m_captureCall = num; |
| 64 | } |
| 65 | |
| 66 | qlonglong Retracer::captureAtCallNumber() const |
| 67 | { |
| 68 | return m_captureCall; |
| 69 | } |
| 70 | |
| 71 | bool Retracer::captureState() const |
| 72 | { |
| 73 | return m_captureState; |
| 74 | } |
| 75 | |
| 76 | void Retracer::setCaptureState(bool enable) |
| 77 | { |
| 78 | m_captureState = enable; |
| 79 | } |
| 80 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 81 | |
| 82 | void 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 | |
| 117 | void RetraceProcess::start() |
| 118 | { |
| 119 | QStringList arguments; |
| 120 | if (m_captureState) { |
| 121 | arguments << QLatin1String("-D"); |
| 122 | arguments << QString::number(m_captureCall); |
| 123 | } else { |
| 124 | if (m_benchmarking) { |
| 125 | arguments << QLatin1String("-b"); |
| 126 | } |
| 127 | if (m_doubleBuffered) { |
| 128 | arguments << QLatin1String("-db"); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | arguments << m_fileName; |
| 133 | |
| 134 | m_process->start(QLatin1String("glretrace"), arguments); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void RetraceProcess::replayFinished() |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 139 | { |
| 140 | QByteArray output = m_process->readAllStandardOutput(); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 141 | QString msg; |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 142 | |
| 143 | #if 0 |
| 144 | qDebug()<<"Process finished = "; |
| 145 | qDebug()<<"\terr = "<<m_process->readAllStandardError(); |
| 146 | qDebug()<<"\tout = "<<output; |
| 147 | #endif |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 148 | if (m_captureState) { |
| 149 | bool ok = false; |
| 150 | QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap(); |
| 151 | ApiTraceState state(parsedJson); |
| 152 | emit foundState(state); |
| 153 | msg = tr("State fetched."); |
| 154 | } else { |
| 155 | msg = QString::fromUtf8(output); |
| 156 | } |
| 157 | |
| 158 | emit finished(msg); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 161 | void RetraceProcess::replayError(QProcess::ProcessError err) |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 162 | { |
| 163 | qDebug()<<"Process error = "<<err; |
| 164 | qDebug()<<"\terr = "<<m_process->readAllStandardError(); |
| 165 | qDebug()<<"\tout = "<<m_process->readAllStandardOutput(); |
| 166 | |
| 167 | emit error( |
| 168 | tr("Couldn't execute the replay file '%1'").arg(m_fileName)); |
| 169 | } |
| 170 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame^] | 171 | |
| 172 | RetraceProcess::RetraceProcess(QObject *parent) |
| 173 | : QObject(parent) |
| 174 | { |
| 175 | m_process = new QProcess(this); |
| 176 | m_jsonParser = new QJson::Parser(); |
| 177 | |
| 178 | connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), |
| 179 | this, SLOT(replayFinished())); |
| 180 | connect(m_process, SIGNAL(error(QProcess::ProcessError)), |
| 181 | this, SLOT(replayError(QProcess::ProcessError))); |
| 182 | } |
| 183 | |
| 184 | QProcess * RetraceProcess::process() const |
| 185 | { |
| 186 | return m_process; |
| 187 | } |
| 188 | |
| 189 | QString RetraceProcess::fileName() const |
| 190 | { |
| 191 | return m_fileName; |
| 192 | } |
| 193 | |
| 194 | void RetraceProcess::setFileName(const QString &name) |
| 195 | { |
| 196 | m_fileName = name; |
| 197 | } |
| 198 | |
| 199 | bool RetraceProcess::isBenchmarking() const |
| 200 | { |
| 201 | return m_benchmarking; |
| 202 | } |
| 203 | |
| 204 | void RetraceProcess::setBenchmarking(bool bench) |
| 205 | { |
| 206 | m_benchmarking = bench; |
| 207 | } |
| 208 | |
| 209 | bool RetraceProcess::isDoubleBuffered() const |
| 210 | { |
| 211 | return m_doubleBuffered; |
| 212 | } |
| 213 | |
| 214 | void RetraceProcess::setDoubleBuffered(bool db) |
| 215 | { |
| 216 | m_doubleBuffered = db; |
| 217 | } |
| 218 | |
| 219 | void RetraceProcess::setCaptureAtCallNumber(qlonglong num) |
| 220 | { |
| 221 | m_captureCall = num; |
| 222 | } |
| 223 | |
| 224 | qlonglong RetraceProcess::captureAtCallNumber() const |
| 225 | { |
| 226 | return m_captureCall; |
| 227 | } |
| 228 | |
| 229 | bool RetraceProcess::captureState() const |
| 230 | { |
| 231 | return m_captureState; |
| 232 | } |
| 233 | |
| 234 | void RetraceProcess::setCaptureState(bool enable) |
| 235 | { |
| 236 | m_captureState = enable; |
| 237 | } |
| 238 | |
| 239 | void RetraceProcess::terminate() |
| 240 | { |
| 241 | if (m_process) { |
| 242 | m_process->terminate(); |
| 243 | emit finished(tr("Process terminated.")); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void Retracer::cleanup() |
| 248 | { |
| 249 | quit(); |
| 250 | } |
| 251 | |
| 252 | RetraceProcess::~RetraceProcess() |
| 253 | { |
| 254 | delete m_jsonParser; |
| 255 | } |
| 256 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 257 | #include "retracer.moc" |