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" |
José Fonseca | 3f45640 | 2012-03-25 20:59:24 +0100 | [diff] [blame] | 4 | #include "thumbnail.h" |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 5 | |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 6 | #include "image.hpp" |
| 7 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 8 | #include "trace_profiler.hpp" |
| 9 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 10 | #include <QDebug> |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 11 | #include <QVariant> |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 12 | #include <QList> |
| 13 | #include <QImage> |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 14 | |
| 15 | #include <qjson/parser.h> |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 16 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 17 | /** |
| 18 | * Wrapper around a QProcess which enforces IO to block . |
| 19 | * |
| 20 | * Several QIODevice users (notably QJSON) expect blocking semantics, e.g., |
| 21 | * they expect that QIODevice::read() will blocked until the requested ammount |
| 22 | * of bytes is read or end of file is reached. But by default QProcess, does |
| 23 | * not block. And passing QIODevice::Unbuffered mitigates but does not fully |
| 24 | * address the problem either. |
| 25 | * |
| 26 | * This class wraps around QProcess, providing QIODevice interface, while |
| 27 | * ensuring that all reads block. |
| 28 | * |
| 29 | * This class also works around a bug in QProcess::atEnd() implementation. |
| 30 | * |
| 31 | * See also: |
| 32 | * - http://qt-project.org/wiki/Simple_Crypt_IO_Device |
| 33 | * - http://qt-project.org/wiki/Custom_IO_Device |
| 34 | */ |
| 35 | class BlockingIODevice : public QIODevice |
| 36 | { |
| 37 | /* We don't use the Q_OBJECT in this class given we don't declare any |
| 38 | * signals and slots or use any other services provided by Qt's meta-object |
| 39 | * system. */ |
| 40 | public: |
| 41 | BlockingIODevice(QProcess * io); |
| 42 | bool isSequential() const; |
| 43 | bool atEnd() const; |
| 44 | bool waitForReadyRead(int msecs = -1); |
| 45 | |
| 46 | protected: |
| 47 | qint64 readData(char * data, qint64 maxSize); |
| 48 | qint64 writeData(const char * data, qint64 maxSize); |
| 49 | |
| 50 | private: |
| 51 | QProcess *m_device; |
| 52 | }; |
| 53 | |
| 54 | BlockingIODevice::BlockingIODevice(QProcess * io) : |
| 55 | m_device(io) |
| 56 | { |
| 57 | /* |
| 58 | * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do |
| 59 | * its own buffering on top of the overridden readData() method. |
| 60 | * |
| 61 | * The only buffering used will be to satisfy QIODevice::peek() and |
| 62 | * QIODevice::ungetChar(). |
| 63 | */ |
| 64 | setOpenMode(ReadOnly | Unbuffered); |
| 65 | } |
| 66 | |
| 67 | bool BlockingIODevice::isSequential() const |
| 68 | { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool BlockingIODevice::atEnd() const |
| 73 | { |
| 74 | /* |
| 75 | * XXX: QProcess::atEnd() documentation is wrong -- it will return true |
| 76 | * even when the process is running --, so we try to workaround that here. |
| 77 | */ |
| 78 | if (m_device->atEnd()) { |
| 79 | if (m_device->state() == QProcess::Running) { |
| 80 | if (!m_device->waitForReadyRead(-1)) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool BlockingIODevice::waitForReadyRead(int msecs) |
| 89 | { |
| 90 | Q_UNUSED(msecs); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | qint64 BlockingIODevice::readData(char * data, qint64 maxSize) |
| 95 | { |
| 96 | qint64 bytesToRead = maxSize; |
| 97 | qint64 readSoFar = 0; |
| 98 | do { |
| 99 | qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead); |
| 100 | if (chunkSize < 0) { |
| 101 | if (readSoFar) { |
| 102 | return readSoFar; |
| 103 | } else { |
| 104 | return chunkSize; |
| 105 | } |
| 106 | } |
| 107 | Q_ASSERT(chunkSize <= bytesToRead); |
| 108 | bytesToRead -= chunkSize; |
| 109 | readSoFar += chunkSize; |
| 110 | if (bytesToRead) { |
| 111 | if (!m_device->waitForReadyRead(-1)) { |
| 112 | qDebug() << "waitForReadyRead failed\n"; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } while(bytesToRead); |
| 117 | |
| 118 | return readSoFar; |
| 119 | } |
| 120 | |
| 121 | qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize) |
| 122 | { |
| 123 | Q_ASSERT(false); |
| 124 | return -1; |
| 125 | } |
| 126 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 127 | Q_DECLARE_METATYPE(QList<ApiTraceError>); |
| 128 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 129 | Retracer::Retracer(QObject *parent) |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 130 | : QThread(parent), |
Zack Rusin | 404a1ef | 2011-04-19 23:49:56 -0400 | [diff] [blame] | 131 | m_benchmarking(false), |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 132 | m_doubleBuffered(true), |
| 133 | m_captureState(false), |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 134 | m_captureCall(0), |
| 135 | m_profileGpu(false), |
| 136 | m_profileCpu(false), |
| 137 | m_profilePixels(false) |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 138 | { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 139 | qRegisterMetaType<QList<ApiTraceError> >(); |
| 140 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 141 | #ifdef Q_OS_WIN |
| 142 | QString format = QLatin1String("%1;"); |
| 143 | #else |
| 144 | QString format = QLatin1String("%1:"); |
| 145 | #endif |
José Fonseca | 2744092 | 2011-11-01 08:27:12 +0000 | [diff] [blame] | 146 | QString buildPath = format.arg(APITRACE_BINARY_DIR); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 147 | m_processEnvironment = QProcessEnvironment::systemEnvironment(); |
| 148 | m_processEnvironment.insert("PATH", buildPath + |
| 149 | m_processEnvironment.value("PATH")); |
| 150 | |
| 151 | qputenv("PATH", |
| 152 | m_processEnvironment.value("PATH").toLatin1()); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | QString Retracer::fileName() const |
| 156 | { |
| 157 | return m_fileName; |
| 158 | } |
| 159 | |
| 160 | void Retracer::setFileName(const QString &name) |
| 161 | { |
| 162 | m_fileName = name; |
| 163 | } |
| 164 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 165 | void Retracer::setAPI(trace::API api) |
| 166 | { |
| 167 | m_api = api; |
| 168 | } |
| 169 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 170 | bool Retracer::isBenchmarking() const |
| 171 | { |
| 172 | return m_benchmarking; |
| 173 | } |
| 174 | |
| 175 | void Retracer::setBenchmarking(bool bench) |
| 176 | { |
| 177 | m_benchmarking = bench; |
| 178 | } |
| 179 | |
| 180 | bool Retracer::isDoubleBuffered() const |
| 181 | { |
| 182 | return m_doubleBuffered; |
| 183 | } |
| 184 | |
| 185 | void Retracer::setDoubleBuffered(bool db) |
| 186 | { |
| 187 | m_doubleBuffered = db; |
| 188 | } |
| 189 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 190 | bool Retracer::isProfilingGpu() const |
| 191 | { |
| 192 | return m_profileGpu; |
| 193 | } |
| 194 | |
| 195 | bool Retracer::isProfilingCpu() const |
| 196 | { |
| 197 | return m_profileCpu; |
| 198 | } |
| 199 | |
| 200 | bool Retracer::isProfilingPixels() const |
| 201 | { |
| 202 | return m_profilePixels; |
| 203 | } |
| 204 | |
| 205 | bool Retracer::isProfiling() const |
| 206 | { |
| 207 | return m_profileGpu || m_profileCpu || m_profilePixels; |
| 208 | } |
| 209 | |
| 210 | void Retracer::setProfiling(bool gpu, bool cpu, bool pixels) |
| 211 | { |
| 212 | m_profileGpu = gpu; |
| 213 | m_profileCpu = cpu; |
| 214 | m_profilePixels = pixels; |
| 215 | } |
| 216 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 217 | void Retracer::setCaptureAtCallNumber(qlonglong num) |
| 218 | { |
| 219 | m_captureCall = num; |
| 220 | } |
| 221 | |
| 222 | qlonglong Retracer::captureAtCallNumber() const |
| 223 | { |
| 224 | return m_captureCall; |
| 225 | } |
| 226 | |
| 227 | bool Retracer::captureState() const |
| 228 | { |
| 229 | return m_captureState; |
| 230 | } |
| 231 | |
| 232 | void Retracer::setCaptureState(bool enable) |
| 233 | { |
| 234 | m_captureState = enable; |
| 235 | } |
| 236 | |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 237 | bool Retracer::captureThumbnails() const |
| 238 | { |
| 239 | return m_captureThumbnails; |
| 240 | } |
| 241 | |
| 242 | void Retracer::setCaptureThumbnails(bool enable) |
| 243 | { |
| 244 | m_captureThumbnails = enable; |
| 245 | } |
| 246 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 247 | /** |
| 248 | * Starting point for the retracing thread. |
| 249 | * |
| 250 | * Overrides QThread::run(). |
| 251 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 252 | void Retracer::run() |
| 253 | { |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 254 | QString msg = QLatin1String("Replay finished!"); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 255 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 256 | /* |
| 257 | * Construct command line |
| 258 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 259 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 260 | QString prog; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 261 | QStringList arguments; |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 262 | |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 263 | switch (m_api) { |
| 264 | case trace::API_GL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 265 | prog = QLatin1String("glretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 266 | break; |
| 267 | case trace::API_EGL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 268 | prog = QLatin1String("eglretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 269 | break; |
| 270 | case trace::API_DX: |
| 271 | case trace::API_D3D7: |
| 272 | case trace::API_D3D8: |
| 273 | case trace::API_D3D9: |
| 274 | case trace::API_D3D10: |
| 275 | case trace::API_D3D10_1: |
| 276 | case trace::API_D3D11: |
| 277 | #ifdef Q_OS_WIN |
| 278 | prog = QLatin1String("d3dretrace"); |
| 279 | #else |
| 280 | prog = QLatin1String("wine"); |
| 281 | arguments << QLatin1String("d3dretrace.exe"); |
| 282 | #endif |
| 283 | break; |
| 284 | default: |
José Fonseca | 6796438 | 2012-03-27 23:54:30 +0100 | [diff] [blame] | 285 | emit finished(QLatin1String("Unsupported API")); |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
| 288 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 289 | if (m_captureState) { |
| 290 | arguments << QLatin1String("-D"); |
| 291 | arguments << QString::number(m_captureCall); |
| 292 | } else if (m_captureThumbnails) { |
| 293 | arguments << QLatin1String("-s"); // emit snapshots |
| 294 | arguments << QLatin1String("-"); // emit to stdout |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 295 | } else if (isProfiling()) { |
| 296 | if (m_profileGpu) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 297 | arguments << QLatin1String("--pgpu"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (m_profileCpu) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 301 | arguments << QLatin1String("--pcpu"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | if (m_profilePixels) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 305 | arguments << QLatin1String("--ppd"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 306 | } |
| 307 | } else { |
| 308 | if (m_doubleBuffered) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 309 | arguments << QLatin1String("--db"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 310 | } else { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 311 | arguments << QLatin1String("--sb"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (m_benchmarking) { |
| 315 | arguments << QLatin1String("-b"); |
| 316 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | arguments << m_fileName; |
| 320 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 321 | /* |
| 322 | * Start the process. |
| 323 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 324 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 325 | QProcess process; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 326 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 327 | process.start(prog, arguments, QIODevice::ReadOnly); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 328 | if (!process.waitForStarted(-1)) { |
| 329 | emit finished(QLatin1String("Could not start process")); |
| 330 | return; |
| 331 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 332 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 333 | /* |
| 334 | * Process standard output |
| 335 | */ |
| 336 | |
| 337 | QList<QImage> thumbnails; |
| 338 | QVariantMap parsedJson; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 339 | trace::Profile* profile = NULL; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 340 | |
| 341 | process.setReadChannel(QProcess::StandardOutput); |
| 342 | if (process.waitForReadyRead(-1)) { |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 343 | BlockingIODevice io(&process); |
| 344 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 345 | if (m_captureState) { |
| 346 | /* |
| 347 | * Parse JSON from the output. |
| 348 | * |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 349 | * XXX: QJSON's scanner is inneficient as it abuses single |
| 350 | * character QIODevice::peek (not cheap), instead of maintaining a |
| 351 | * lookahead character on its own. |
| 352 | */ |
| 353 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 354 | bool ok = false; |
| 355 | QJson::Parser jsonParser; |
José Fonseca | 74936bb | 2012-10-27 10:13:58 +0100 | [diff] [blame] | 356 | |
| 357 | // Allow Nan/Infinity |
| 358 | jsonParser.allowSpecialNumbers(true); |
José Fonseca | 95b4056 | 2012-04-05 20:06:42 +0100 | [diff] [blame] | 359 | #if 0 |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 360 | parsedJson = jsonParser.parse(&io, &ok).toMap(); |
José Fonseca | 95b4056 | 2012-04-05 20:06:42 +0100 | [diff] [blame] | 361 | #else |
| 362 | /* |
| 363 | * XXX: QJSON expects blocking IO, and it looks like |
| 364 | * BlockingIODevice does not work reliably in all cases. |
| 365 | */ |
| 366 | process.waitForFinished(-1); |
| 367 | parsedJson = jsonParser.parse(&process, &ok).toMap(); |
| 368 | #endif |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 369 | if (!ok) { |
| 370 | msg = QLatin1String("failed to parse JSON"); |
| 371 | } |
| 372 | } else if (m_captureThumbnails) { |
| 373 | /* |
| 374 | * Parse concatenated PNM images from output. |
| 375 | */ |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 376 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 377 | while (!io.atEnd()) { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 378 | unsigned channels = 0; |
| 379 | unsigned width = 0; |
| 380 | unsigned height = 0; |
| 381 | |
| 382 | char header[512]; |
| 383 | qint64 headerSize = 0; |
| 384 | int headerLines = 3; // assume no optional comment line |
| 385 | |
| 386 | for (int headerLine = 0; headerLine < headerLines; ++headerLine) { |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 387 | qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 388 | |
| 389 | // if header actually contains optional comment line, ... |
| 390 | if (headerLine == 1 && header[headerSize] == '#') { |
| 391 | ++headerLines; |
| 392 | } |
| 393 | |
| 394 | headerSize += headerRead; |
| 395 | } |
| 396 | |
| 397 | const char *headerEnd = image::readPNMHeader(header, headerSize, &channels, &width, &height); |
| 398 | |
| 399 | // if invalid PNM header was encountered, ... |
| 400 | if (header == headerEnd) { |
| 401 | qDebug() << "error: invalid snapshot stream encountered"; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height"; |
| 406 | |
| 407 | QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888); |
| 408 | |
| 409 | int rowBytes = channels * width; |
| 410 | for (int y = 0; y < height; ++y) { |
| 411 | unsigned char *scanLine = snapshot.scanLine(y); |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 412 | qint64 readBytes = io.read((char *) scanLine, rowBytes); |
| 413 | Q_ASSERT(readBytes == rowBytes); |
José Fonseca | a2bf287 | 2012-11-15 13:35:22 +0000 | [diff] [blame^] | 414 | (void)readBytes; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 415 | } |
| 416 | |
José Fonseca | dc9e9c6 | 2012-03-26 10:29:32 +0100 | [diff] [blame] | 417 | QImage thumb = thumbnail(snapshot); |
| 418 | thumbnails.append(thumb); |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 419 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 420 | |
| 421 | Q_ASSERT(process.state() != QProcess::Running); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 422 | } else if (isProfiling()) { |
| 423 | profile = new trace::Profile(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 424 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 425 | while (!io.atEnd()) { |
| 426 | char line[256]; |
| 427 | qint64 lineLength; |
| 428 | |
| 429 | lineLength = io.readLine(line, 256); |
| 430 | |
| 431 | if (lineLength == -1) |
| 432 | break; |
| 433 | |
| 434 | trace::Profiler::parseLine(line, profile); |
| 435 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 436 | } else { |
José Fonseca | 676cd17 | 2012-03-24 09:46:24 +0000 | [diff] [blame] | 437 | QByteArray output; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 438 | output = process.readAllStandardOutput(); |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 439 | if (output.length() < 80) { |
| 440 | msg = QString::fromUtf8(output); |
| 441 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 442 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 443 | } |
| 444 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 445 | /* |
| 446 | * Wait for process termination |
| 447 | */ |
| 448 | |
| 449 | process.waitForFinished(-1); |
| 450 | |
| 451 | if (process.exitStatus() != QProcess::NormalExit) { |
| 452 | msg = QLatin1String("Process crashed"); |
| 453 | } else if (process.exitCode() != 0) { |
| 454 | msg = QLatin1String("Process exited with non zero exit code"); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Parse errors. |
| 459 | */ |
| 460 | |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 461 | QList<ApiTraceError> errors; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 462 | process.setReadChannel(QProcess::StandardError); |
José Fonseca | 8fdf56c | 2012-03-24 10:06:56 +0000 | [diff] [blame] | 463 | QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$"); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 464 | while (!process.atEnd()) { |
| 465 | QString line = process.readLine(); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 466 | if (regexp.indexIn(line) != -1) { |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 467 | ApiTraceError error; |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 468 | error.callIndex = regexp.cap(1).toInt(); |
| 469 | error.type = regexp.cap(2); |
| 470 | error.message = regexp.cap(3); |
| 471 | errors.append(error); |
gregory | f2329b6 | 2012-07-06 21:48:59 +0200 | [diff] [blame] | 472 | } else if (!errors.isEmpty()) { |
| 473 | // Probably a multiligne message |
| 474 | ApiTraceError &previous = errors.last(); |
| 475 | if (line.endsWith("\n")) { |
| 476 | line.chop(1); |
| 477 | } |
| 478 | previous.message.append('\n'); |
| 479 | previous.message.append(line); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 480 | } |
| 481 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 482 | |
| 483 | /* |
| 484 | * Emit signals |
| 485 | */ |
| 486 | |
| 487 | if (m_captureState) { |
| 488 | ApiTraceState *state = new ApiTraceState(parsedJson); |
| 489 | emit foundState(state); |
| 490 | msg = QLatin1String("State fetched."); |
| 491 | } |
| 492 | |
| 493 | if (m_captureThumbnails && !thumbnails.isEmpty()) { |
| 494 | emit foundThumbnails(thumbnails); |
| 495 | } |
| 496 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 497 | if (isProfiling() && profile) { |
| 498 | emit foundProfile(profile); |
| 499 | } |
| 500 | |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 501 | if (!errors.isEmpty()) { |
| 502 | emit retraceErrors(errors); |
| 503 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 504 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 505 | emit finished(msg); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 508 | #include "retracer.moc" |