blob: 69425a3901c346318bc4697f7d3847f32c97b5aa [file] [log] [blame]
Zack Rusinf6667d12011-03-30 11:03:37 -04001#include "apitrace.h"
2
Zack Rusinebf971e2011-09-06 17:44:43 -04003#include "traceloader.h"
Zack Rusind809a062011-04-17 23:30:58 -04004#include "saverthread.h"
Zack Rusinf6667d12011-03-30 11:03:37 -04005
Zack Rusin3176ebe2011-09-06 21:11:36 -04006#include <QDebug>
José Fonseca24bc5c32014-11-10 20:13:31 +00007#include <QFileInfo>
Zack Rusin63efea82011-04-17 17:10:45 -04008#include <QDir>
Zack Rusinebf971e2011-09-06 17:44:43 -04009#include <QThread>
Zack Rusin63efea82011-04-17 17:10:45 -040010
Zack Rusinf6667d12011-03-30 11:03:37 -040011ApiTrace::ApiTrace()
José Fonseca5cd8d992012-03-25 23:12:20 +010012 : m_needsSaving(false)
Zack Rusinf6667d12011-03-30 11:03:37 -040013{
Zack Rusinebf971e2011-09-06 17:44:43 -040014 m_loader = new TraceLoader();
Zack Rusinf682e192011-09-07 01:36:41 -040015
Zack Rusinebf971e2011-09-06 17:44:43 -040016 connect(this, SIGNAL(loadTrace(QString)),
17 m_loader, SLOT(loadTrace(QString)));
Zack Rusin3176ebe2011-09-06 21:11:36 -040018 connect(this, SIGNAL(requestFrame(ApiTraceFrame*)),
19 m_loader, SLOT(loadFrame(ApiTraceFrame*)));
Zack Rusinebf971e2011-09-06 17:44:43 -040020 connect(m_loader, SIGNAL(framesLoaded(const QList<ApiTraceFrame*>)),
Zack Rusinf6667d12011-03-30 11:03:37 -040021 this, SLOT(addFrames(const QList<ApiTraceFrame*>)));
Zack Rusin8f98c3a2011-09-11 18:21:29 -040022 connect(m_loader,
Zack Rusind9d9d222013-10-11 18:02:26 -040023 SIGNAL(frameContentsLoaded(ApiTraceFrame*,QVector<ApiTraceCall*>, QVector<ApiTraceCall*>,quint64)),
Zack Rusin8f98c3a2011-09-11 18:21:29 -040024 this,
Zack Rusind9d9d222013-10-11 18:02:26 -040025 SLOT(loaderFrameLoaded(ApiTraceFrame*,QVector<ApiTraceCall*>,QVector<ApiTraceCall*>,quint64)));
José Fonseca67964382012-03-27 23:54:30 +010026 connect(m_loader, SIGNAL(guessedApi(int)),
27 this, SLOT(guessedApi(int)));
Zack Rusinad513b32011-09-25 14:33:41 -040028 connect(this, SIGNAL(loaderSearch(ApiTrace::SearchRequest)),
29 m_loader, SLOT(search(ApiTrace::SearchRequest)));
Zack Rusin8f98c3a2011-09-11 18:21:29 -040030 connect(m_loader,
Zack Rusinad513b32011-09-25 14:33:41 -040031 SIGNAL(searchResult(ApiTrace::SearchRequest,ApiTrace::SearchResult,ApiTraceCall*)),
Zack Rusin8f98c3a2011-09-11 18:21:29 -040032 this,
Zack Rusinad513b32011-09-25 14:33:41 -040033 SLOT(loaderSearchResult(ApiTrace::SearchRequest,ApiTrace::SearchResult,ApiTraceCall*)));
Zack Rusin93e4d152011-09-13 02:23:39 -040034 connect(this, SIGNAL(loaderFindFrameStart(ApiTraceFrame*)),
35 m_loader, SLOT(findFrameStart(ApiTraceFrame*)));
36 connect(this, SIGNAL(loaderFindFrameEnd(ApiTraceFrame*)),
37 m_loader, SLOT(findFrameEnd(ApiTraceFrame*)));
38 connect(m_loader, SIGNAL(foundFrameStart(ApiTraceFrame*)),
39 this, SIGNAL(foundFrameStart(ApiTraceFrame*)));
40 connect(m_loader, SIGNAL(foundFrameEnd(ApiTraceFrame*)),
41 this, SIGNAL(foundFrameEnd(ApiTraceFrame*)));
Zack Rusinda7579b2011-09-13 17:33:05 -040042 connect(this, SIGNAL(loaderFindCallIndex(int)),
43 m_loader, SLOT(findCallIndex(int)));
44 connect(m_loader, SIGNAL(foundCallIndex(ApiTraceCall*)),
45 this, SIGNAL(foundCallIndex(ApiTraceCall*)));
Zack Rusin8f98c3a2011-09-11 18:21:29 -040046
Zack Rusinebf971e2011-09-06 17:44:43 -040047
Jose Fonseca8f0846c2015-11-10 19:53:55 +000048 connect(m_loader, SIGNAL(parseProblem(const QString&)),
49 this, SIGNAL(problemLoadingTrace(const QString&)));
Zack Rusinebf971e2011-09-06 17:44:43 -040050 connect(m_loader, SIGNAL(startedParsing()),
Zack Rusinde4ea412011-03-30 11:30:08 -040051 this, SIGNAL(startedLoadingTrace()));
Zack Rusinebf971e2011-09-06 17:44:43 -040052 connect(m_loader, SIGNAL(parsed(int)),
53 this, SIGNAL(loaded(int)));
54 connect(m_loader, SIGNAL(finishedParsing()),
Zack Rusinde4ea412011-03-30 11:30:08 -040055 this, SIGNAL(finishedLoadingTrace()));
Zack Rusind809a062011-04-17 23:30:58 -040056
Zack Rusinebf971e2011-09-06 17:44:43 -040057
Zack Rusind809a062011-04-17 23:30:58 -040058 m_saver = new SaverThread(this);
Zack Rusin9af5bff2011-04-18 01:05:50 -040059 connect(m_saver, SIGNAL(traceSaved()),
60 this, SLOT(slotSaved()));
61 connect(m_saver, SIGNAL(traceSaved()),
62 this, SIGNAL(saved()));
Zack Rusinebf971e2011-09-06 17:44:43 -040063
64 m_loaderThread = new QThread();
65 m_loader->moveToThread(m_loaderThread);
66 m_loaderThread->start();
Zack Rusinf6667d12011-03-30 11:03:37 -040067}
68
69ApiTrace::~ApiTrace()
70{
Zack Rusinebf971e2011-09-06 17:44:43 -040071 m_loaderThread->quit();
72 m_loaderThread->deleteLater();
Zack Rusinf6667d12011-03-30 11:03:37 -040073 qDeleteAll(m_frames);
74 delete m_loader;
Zack Rusind809a062011-04-17 23:30:58 -040075 delete m_saver;
Zack Rusinf6667d12011-03-30 11:03:37 -040076}
77
Zack Rusinf6667d12011-03-30 11:03:37 -040078bool ApiTrace::isEmpty() const
79{
Zack Rusinc1743432011-09-13 17:58:58 -040080 return m_frames.isEmpty();
Zack Rusinf6667d12011-03-30 11:03:37 -040081}
82
83QString ApiTrace::fileName() const
84{
Zack Rusinc5929572011-09-19 03:04:40 -040085 if (edited()) {
Zack Rusin63efea82011-04-17 17:10:45 -040086 return m_tempFileName;
Zack Rusinc5929572011-09-19 03:04:40 -040087 }
Zack Rusin63efea82011-04-17 17:10:45 -040088
Zack Rusinf6667d12011-03-30 11:03:37 -040089 return m_fileName;
90}
91
José Fonseca34500442012-03-24 07:56:45 +000092const QList<ApiTraceFrame*> & ApiTrace::frames() const
Zack Rusinf6667d12011-03-30 11:03:37 -040093{
94 return m_frames;
95}
96
97ApiTraceFrame * ApiTrace::frameAt(int idx) const
98{
99 return m_frames.value(idx);
100}
101
102int ApiTrace::numFrames() const
103{
104 return m_frames.count();
105}
106
107int ApiTrace::numCallsInFrame(int idx) const
108{
109 const ApiTraceFrame *frame = frameAt(idx);
Zack Rusinc5929572011-09-19 03:04:40 -0400110 if (frame) {
Zack Rusind9d9d222013-10-11 18:02:26 -0400111 return frame->numTotalCalls();
Zack Rusinc5929572011-09-19 03:04:40 -0400112 } else {
Zack Rusinf6667d12011-03-30 11:03:37 -0400113 return 0;
Zack Rusinc5929572011-09-19 03:04:40 -0400114 }
Zack Rusinf6667d12011-03-30 11:03:37 -0400115}
116
117void ApiTrace::setFileName(const QString &name)
118{
119 if (m_fileName != name) {
120 m_fileName = name;
Zack Rusindaf82af2011-09-24 13:42:53 -0400121 m_tempFileName = QString();
Zack Rusinf6667d12011-03-30 11:03:37 -0400122
Zack Rusinca164112011-04-11 02:23:09 -0400123 m_frames.clear();
Zack Rusin30069572011-04-20 18:21:11 -0400124 m_errors.clear();
125 m_editedCalls.clear();
Zack Rusindaf82af2011-09-24 13:42:53 -0400126 m_queuedErrors.clear();
Zack Rusin30069572011-04-20 18:21:11 -0400127 m_needsSaving = false;
Zack Rusinf6667d12011-03-30 11:03:37 -0400128 emit invalidated();
129
Zack Rusinebf971e2011-09-06 17:44:43 -0400130 emit loadTrace(m_fileName);
Zack Rusinf6667d12011-03-30 11:03:37 -0400131 }
132}
133
Zack Rusinf6667d12011-03-30 11:03:37 -0400134void ApiTrace::addFrames(const QList<ApiTraceFrame*> &frames)
135{
136 int currentFrames = m_frames.count();
137 int numNewFrames = frames.count();
Zack Rusinb56e03d2011-04-20 23:58:52 -0400138
139 emit beginAddingFrames(currentFrames, numNewFrames);
140
Zack Rusinf6667d12011-03-30 11:03:37 -0400141 m_frames += frames;
142
Zack Rusinf6667d12011-03-30 11:03:37 -0400143 foreach(ApiTraceFrame *frame, frames) {
Zack Rusinebf971e2011-09-06 17:44:43 -0400144 frame->setParentTrace(this);
Zack Rusinf6667d12011-03-30 11:03:37 -0400145 }
146
Zack Rusinb56e03d2011-04-20 23:58:52 -0400147 emit endAddingFrames();
Zack Rusinf6667d12011-03-30 11:03:37 -0400148}
149
Zack Rusinf04cf8a2011-04-12 23:21:52 -0400150ApiTraceCall * ApiTrace::callWithIndex(int idx) const
151{
Zack Rusinc1743432011-09-13 17:58:58 -0400152 for (int i = 0; i < m_frames.count(); ++i) {
153 ApiTraceCall *call = m_frames[i]->callWithIndex(idx);
Zack Rusinc5929572011-09-19 03:04:40 -0400154 if (call) {
Zack Rusinf04cf8a2011-04-12 23:21:52 -0400155 return call;
Zack Rusinc5929572011-09-19 03:04:40 -0400156 }
Zack Rusinf04cf8a2011-04-12 23:21:52 -0400157 }
158 return NULL;
159}
160
Zack Rusine2dfa2e2011-04-13 01:35:03 -0400161ApiTraceState ApiTrace::defaultState() const
162{
163 ApiTraceFrame *frame = frameAt(0);
Zack Rusinc5929572011-09-19 03:04:40 -0400164 if (!frame || !frame->isLoaded() || frame->isEmpty()) {
Zack Rusine2dfa2e2011-04-13 01:35:03 -0400165 return ApiTraceState();
Zack Rusinc5929572011-09-19 03:04:40 -0400166 }
Zack Rusine2dfa2e2011-04-13 01:35:03 -0400167
Zack Rusin851d0b02011-09-14 22:04:07 -0400168 ApiTraceCall *firstCall = frame->calls().first();
169 if (!firstCall->hasState()) {
170 return ApiTraceState();
171 }
172
173 return *firstCall->state();
Zack Rusine2dfa2e2011-04-13 01:35:03 -0400174}
175
Zack Rusin661842d2011-04-17 01:59:16 -0400176void ApiTrace::callEdited(ApiTraceCall *call)
177{
Zack Rusin63efea82011-04-17 17:10:45 -0400178 if (!m_editedCalls.contains(call)) {
José Fonseca24bc5c32014-11-10 20:13:31 +0000179 // Lets generate a temp filename
180 QFileInfo fileInfo(m_fileName);
Martin Schulze88148592016-03-29 16:47:06 +0200181 m_tempFileName = QDir::temp().filePath(fileInfo.fileName() +
182 QString::fromLatin1(".edited"));
Zack Rusin63efea82011-04-17 17:10:45 -0400183 }
Zack Rusin661842d2011-04-17 01:59:16 -0400184 m_editedCalls.insert(call);
Zack Rusin9af5bff2011-04-18 01:05:50 -0400185 m_needsSaving = true;
Zack Rusin63efea82011-04-17 17:10:45 -0400186
Zack Rusin661842d2011-04-17 01:59:16 -0400187 emit changed(call);
188}
189
190void ApiTrace::callReverted(ApiTraceCall *call)
191{
192 m_editedCalls.remove(call);
Zack Rusin63efea82011-04-17 17:10:45 -0400193
194 if (m_editedCalls.isEmpty()) {
195 m_needsSaving = false;
196 }
Zack Rusin661842d2011-04-17 01:59:16 -0400197 emit changed(call);
198}
199
Zack Rusin0ddd2502011-04-17 02:34:45 -0400200bool ApiTrace::edited() const
Zack Rusin661842d2011-04-17 01:59:16 -0400201{
202 return !m_editedCalls.isEmpty();
203}
204
Zack Rusin63efea82011-04-17 17:10:45 -0400205bool ApiTrace::needsSaving() const
206{
207 return m_needsSaving;
208}
209
210void ApiTrace::save()
211{
212 QFileInfo fi(m_tempFileName);
213 QDir dir;
Zack Rusin9af5bff2011-04-18 01:05:50 -0400214 emit startedSaving();
Zack Rusin63efea82011-04-17 17:10:45 -0400215 dir.mkpath(fi.absolutePath());
Zack Rusin9b31ffc2011-09-13 23:58:45 -0400216 m_saver->saveFile(m_tempFileName,
217 m_fileName,
218 m_editedCalls);
Zack Rusin63efea82011-04-17 17:10:45 -0400219}
220
Zack Rusin9af5bff2011-04-18 01:05:50 -0400221void ApiTrace::slotSaved()
222{
223 m_needsSaving = false;
224}
225
226bool ApiTrace::isSaving() const
227{
228 return m_saver->isRunning();
229}
230
Zack Rusincc0b4912011-04-19 01:59:20 -0400231bool ApiTrace::hasErrors() const
232{
James Benton6a319062012-08-09 16:22:50 +0100233 return !m_errors.isEmpty() || !m_queuedErrors.isEmpty();
Zack Rusincc0b4912011-04-19 01:59:20 -0400234}
235
Zack Rusin3176ebe2011-09-06 21:11:36 -0400236void ApiTrace::loadFrame(ApiTraceFrame *frame)
Zack Rusinac4dd9a2011-08-28 00:38:13 -0400237{
Zack Rusin18d094e2011-10-07 23:55:55 -0400238 if (!isFrameLoading(frame)) {
239 Q_ASSERT(!frame->isLoaded());
240 m_loadingFrames.insert(frame);
241 emit requestFrame(frame);
242 }
Zack Rusin35c27932011-08-28 21:16:22 -0400243}
244
José Fonseca67964382012-03-27 23:54:30 +0100245void ApiTrace::guessedApi(int api)
246{
247 m_api = static_cast<trace::API>(api);
248}
249
250trace::API ApiTrace::api() const
251{
252 return m_api;
253}
254
Zack Rusinf682e192011-09-07 01:36:41 -0400255void ApiTrace::finishedParsing()
256{
José Fonseca0b08bc62012-03-22 16:22:33 +0000257 if (!m_frames.isEmpty()) {
258 ApiTraceFrame *firstFrame = m_frames[0];
259 if (firstFrame && !firstFrame->isLoaded()) {
260 loadFrame(firstFrame);
261 }
Zack Rusinf682e192011-09-07 01:36:41 -0400262 }
263}
264
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400265void ApiTrace::loaderFrameLoaded(ApiTraceFrame *frame,
Zack Rusind9d9d222013-10-11 18:02:26 -0400266 const QVector<ApiTraceCall*> &topLevelItems,
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400267 const QVector<ApiTraceCall*> &calls,
268 quint64 binaryDataSize)
Zack Rusinf682e192011-09-07 01:36:41 -0400269{
José Fonsecacaa84282013-04-11 18:54:06 +0100270 Q_ASSERT(frame->numChildrenToLoad() >= calls.size());
Zack Rusin18d094e2011-10-07 23:55:55 -0400271
272 if (!frame->isLoaded()) {
273 emit beginLoadingFrame(frame, calls.size());
Zack Rusind9d9d222013-10-11 18:02:26 -0400274 frame->setCalls(topLevelItems, calls, binaryDataSize);
Zack Rusin18d094e2011-10-07 23:55:55 -0400275 emit endLoadingFrame(frame);
276 m_loadingFrames.remove(frame);
277 }
Zack Rusin10fd4772011-09-14 01:45:12 -0400278
279 if (!m_queuedErrors.isEmpty()) {
280 QList< QPair<ApiTraceFrame*, ApiTraceError> >::iterator itr;
Evgeny Startsev1b587ca2015-07-22 19:16:33 +0100281 itr = m_queuedErrors.begin();
282 while (itr != m_queuedErrors.end()) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400283 const ApiTraceError &error = (*itr).second;
284 if ((*itr).first == frame) {
285 ApiTraceCall *call = frame->callWithIndex(error.callIndex);
286
287 if (!call) {
Evgeny Startsev1b587ca2015-07-22 19:16:33 +0100288 ++itr;
Zack Rusin10fd4772011-09-14 01:45:12 -0400289 continue;
290 }
291
292 call->setError(error.message);
Evgeny Startsev1b587ca2015-07-22 19:16:33 +0100293 itr = m_queuedErrors.erase(itr);
Zack Rusin10fd4772011-09-14 01:45:12 -0400294
295 if (call->hasError()) {
296 m_errors.insert(call);
297 } else {
298 m_errors.remove(call);
299 }
300 emit changed(call);
Evgeny Startsev1b587ca2015-07-22 19:16:33 +0100301 } else {
302 ++itr;
Zack Rusin10fd4772011-09-14 01:45:12 -0400303 }
304 }
305 }
Zack Rusinf682e192011-09-07 01:36:41 -0400306}
307
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400308void ApiTrace::findNext(ApiTraceFrame *frame,
309 ApiTraceCall *from,
310 const QString &str,
311 Qt::CaseSensitivity sensitivity)
312{
313 ApiTraceCall *foundCall = 0;
314 int frameIdx = m_frames.indexOf(frame);
Zack Rusinad513b32011-09-25 14:33:41 -0400315 SearchRequest request(SearchRequest::Next,
316 frame, from, str, sensitivity);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400317
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400318 if (frame->isLoaded()) {
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400319 foundCall = frame->findNextCall(from, str, sensitivity);
320 if (foundCall) {
Zack Rusinad513b32011-09-25 14:33:41 -0400321 emit findResult(request, SearchResult_Found, foundCall);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400322 return;
323 }
324
325 //if the frame is loaded we already searched it above
326 // so skip it
327 frameIdx += 1;
328 }
329
Zack Rusinad513b32011-09-25 14:33:41 -0400330 //for the rest of the frames we search from beginning
331 request.from = 0;
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400332 for (int i = frameIdx; i < m_frames.count(); ++i) {
333 ApiTraceFrame *frame = m_frames[i];
Zack Rusinad513b32011-09-25 14:33:41 -0400334 request.frame = frame;
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400335 if (!frame->isLoaded()) {
Zack Rusinad513b32011-09-25 14:33:41 -0400336 emit loaderSearch(request);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400337 return;
338 } else {
339 ApiTraceCall *call = frame->findNextCall(0, str, sensitivity);
340 if (call) {
Zack Rusinad513b32011-09-25 14:33:41 -0400341 emit findResult(request, SearchResult_Found, call);
Zack Rusin121e3162011-09-13 01:35:12 -0400342 return;
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400343 }
344 }
345 }
Zack Rusinad513b32011-09-25 14:33:41 -0400346 emit findResult(request, SearchResult_Wrapped, 0);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400347}
348
349void ApiTrace::findPrev(ApiTraceFrame *frame,
350 ApiTraceCall *from,
351 const QString &str,
352 Qt::CaseSensitivity sensitivity)
353{
354 ApiTraceCall *foundCall = 0;
355 int frameIdx = m_frames.indexOf(frame);
Zack Rusinad513b32011-09-25 14:33:41 -0400356 SearchRequest request(SearchRequest::Prev,
357 frame, from, str, sensitivity);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400358
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400359 if (frame->isLoaded()) {
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400360 foundCall = frame->findPrevCall(from, str, sensitivity);
361 if (foundCall) {
Zack Rusinad513b32011-09-25 14:33:41 -0400362 emit findResult(request, SearchResult_Found, foundCall);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400363 return;
364 }
365
366 //if the frame is loaded we already searched it above
367 // so skip it
368 frameIdx -= 1;
369 }
370
Zack Rusinad513b32011-09-25 14:33:41 -0400371 request.from = 0;
Zack Rusin121e3162011-09-13 01:35:12 -0400372 for (int i = frameIdx; i >= 0; --i) {
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400373 ApiTraceFrame *frame = m_frames[i];
Zack Rusinad513b32011-09-25 14:33:41 -0400374 request.frame = frame;
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400375 if (!frame->isLoaded()) {
Zack Rusinad513b32011-09-25 14:33:41 -0400376 emit loaderSearch(request);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400377 return;
378 } else {
379 ApiTraceCall *call = frame->findPrevCall(0, str, sensitivity);
380 if (call) {
Zack Rusinad513b32011-09-25 14:33:41 -0400381 emit findResult(request, SearchResult_Found, call);
Zack Rusin121e3162011-09-13 01:35:12 -0400382 return;
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400383 }
384 }
385 }
Zack Rusinad513b32011-09-25 14:33:41 -0400386 emit findResult(request, SearchResult_Wrapped, 0);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400387}
388
Zack Rusinad513b32011-09-25 14:33:41 -0400389void ApiTrace::loaderSearchResult(const ApiTrace::SearchRequest &request,
390 ApiTrace::SearchResult result,
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400391 ApiTraceCall *call)
392{
393 //qDebug()<<"Search result = "<<result
394 // <<", call is = "<<call;
Zack Rusinad513b32011-09-25 14:33:41 -0400395 emit findResult(request, result, call);
Zack Rusin8f98c3a2011-09-11 18:21:29 -0400396}
397
Zack Rusin93e4d152011-09-13 02:23:39 -0400398void ApiTrace::findFrameStart(ApiTraceFrame *frame)
399{
Alexandr Akulich05b07162012-12-07 16:58:14 +0600400 if (!frame)
401 return;
402
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400403 if (frame->isLoaded()) {
Zack Rusin93e4d152011-09-13 02:23:39 -0400404 emit foundFrameStart(frame);
405 } else {
406 emit loaderFindFrameStart(frame);
407 }
408}
409
410void ApiTrace::findFrameEnd(ApiTraceFrame *frame)
411{
Alexandr Akulich05b07162012-12-07 16:58:14 +0600412 if (!frame)
413 return;
414
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400415 if (frame->isLoaded()) {
Zack Rusin93e4d152011-09-13 02:23:39 -0400416 emit foundFrameEnd(frame);
417 } else {
418 emit loaderFindFrameEnd(frame);
419 }
420}
421
Zack Rusinda7579b2011-09-13 17:33:05 -0400422void ApiTrace::findCallIndex(int index)
423{
424 int frameIdx = callInFrame(index);
425 ApiTraceFrame *frame = 0;
426
427 if (frameIdx < 0) {
428 emit foundCallIndex(0);
429 return;
430 }
431
432 frame = m_frames[frameIdx];
433
434 if (frame) {
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400435 if (frame->isLoaded()) {
Zack Rusinda7579b2011-09-13 17:33:05 -0400436 ApiTraceCall *call = frame->callWithIndex(index);
437 emit foundCallIndex(call);
438 } else {
439 emit loaderFindCallIndex(index);
440 }
441 }
442}
443
444int ApiTrace::callInFrame(int callIdx) const
445{
446 unsigned numCalls = 0;
447
Dan McCabe6cc6dac2012-03-22 12:18:55 +0000448 for (int frameIdx = 0; frameIdx < m_frames.size(); ++frameIdx) {
Zack Rusinda7579b2011-09-13 17:33:05 -0400449 const ApiTraceFrame *frame = m_frames[frameIdx];
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400450 unsigned numCallsInFrame = frame->isLoaded()
Zack Rusind9d9d222013-10-11 18:02:26 -0400451 ? frame->numTotalCalls()
Zack Rusinda7579b2011-09-13 17:33:05 -0400452 : frame->numChildrenToLoad();
453 unsigned firstCall = numCalls;
454 unsigned endCall = numCalls + numCallsInFrame;
455 if (firstCall <= callIdx && endCall > callIdx) {
456 return frameIdx;
457 }
458 numCalls = endCall;
459 }
460
461 return -1;
462}
463
Zack Rusin10fd4772011-09-14 01:45:12 -0400464void ApiTrace::setCallError(const ApiTraceError &error)
465{
466 int frameIdx = callInFrame(error.callIndex);
Zack Rusin10fd4772011-09-14 01:45:12 -0400467 if (frameIdx < 0) {
468 return;
469 }
Zack Rusin10fd4772011-09-14 01:45:12 -0400470
Jose Fonseca05baa462015-01-25 17:21:08 +0000471 ApiTraceFrame *frame = 0;
472 frame = m_frames[frameIdx];
Zack Rusin1a9f7af2011-09-18 19:40:47 -0400473 if (frame->isLoaded()) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400474 ApiTraceCall *call = frame->callWithIndex(error.callIndex);
Jose Fonseca05baa462015-01-25 17:21:08 +0000475 // call might be null if the error is in a filtered call
476 if (call) {
477 call->setError(error.message);
478 if (call->hasError()) {
479 m_errors.insert(call);
480 } else {
481 m_errors.remove(call);
482 }
483 emit changed(call);
Zack Rusin10fd4772011-09-14 01:45:12 -0400484 }
Zack Rusin10fd4772011-09-14 01:45:12 -0400485 } else {
Zack Rusin18d094e2011-10-07 23:55:55 -0400486 loadFrame(frame);
Zack Rusin10fd4772011-09-14 01:45:12 -0400487 m_queuedErrors.append(qMakePair(frame, error));
488 }
489}
490
Zack Rusin18d094e2011-10-07 23:55:55 -0400491bool ApiTrace::isFrameLoading(ApiTraceFrame *frame) const
492{
493 return m_loadingFrames.contains(frame);
494}
495
Dan McCabec6f924e2012-06-01 13:40:05 -0700496void ApiTrace::bindThumbnails(const ImageHash &thumbnails)
Dan McCabe10bd4242012-03-05 17:20:40 -0800497{
Dan McCabec6f924e2012-06-01 13:40:05 -0700498 QHashIterator<int, QImage> i(thumbnails);
Dan McCabe10bd4242012-03-05 17:20:40 -0800499
Dan McCabec6f924e2012-06-01 13:40:05 -0700500 while (i.hasNext()) {
501 i.next();
Dan McCabe10bd4242012-03-05 17:20:40 -0800502
Dan McCabec6f924e2012-06-01 13:40:05 -0700503 if (!m_thumbnails.contains(i.key())) {
504 int callIndex = i.key();
505 const QImage &thumbnail = i.value();
Dan McCabe10bd4242012-03-05 17:20:40 -0800506
Dan McCabec6f924e2012-06-01 13:40:05 -0700507 m_thumbnails.insert(callIndex, thumbnail);
Dan McCabe10bd4242012-03-05 17:20:40 -0800508
Dan McCabec6f924e2012-06-01 13:40:05 -0700509 // find the frame associated with the call index
510 int frameIndex = 0;
511 while (frameAt(frameIndex)->lastCallIndex() < callIndex) {
512 ++frameIndex;
513 }
514
515 ApiTraceFrame *frame = frameAt(frameIndex);
516
517 // if the call was actually for a frame, ...
518 if (callIndex == frame->lastCallIndex()) {
519 frame->setThumbnail(thumbnail);
520
521 emit changed(frame);
522 } else {
523 ApiTraceCall *call = frame->callWithIndex(callIndex);
524 if (call) {
525 call->setThumbnail(thumbnail);
526
527 emit changed(call);
528 }
529 }
Dan McCabe10bd4242012-03-05 17:20:40 -0800530 }
531 }
532}
533
Dan McCabed1395322012-06-01 13:40:03 -0700534void ApiTrace::missingThumbnail(ApiTraceFrame *frame)
535{
536 missingThumbnail(frame->lastCallIndex());
537}
538
539void ApiTrace::missingThumbnail(ApiTraceCall *call)
540{
541 missingThumbnail(call->index());
542}
543
544void ApiTrace::missingThumbnail(int callIdx)
545{
546 // technically, the contain() test is redundant, since this is a set;
547 // however, it enables debugging techniques to confirm correct behavior
548 if (!m_missingThumbnails.contains(callIdx)) {
549 //qDebug() << QLatin1String("debug: new missing thumbnail: ") << callIdx;
550 m_missingThumbnails.insert(callIdx);
551 }
552}
553
554bool ApiTrace::isMissingThumbnails() const
555{
556 return !m_missingThumbnails.isEmpty();
557}
558void ApiTrace::resetMissingThumbnails()
559{
560 m_missingThumbnails.clear();
561}
562
Dan McCabe88938852012-06-01 13:40:04 -0700563void ApiTrace::iterateMissingThumbnails(void *object, ThumbnailCallback cb)
564{
565 //qDebug() << QLatin1String("debug: count of missing thumbnail list") << m_missingThumbnails.count();
566 foreach (int thumbnailIndex, m_missingThumbnails) {
567 //qDebug() << QLatin1String("debug: iterate missing thumbnail list") << thumbnailIndex;
568 (*cb)(object, thumbnailIndex);
569 }
570}
571
Zack Rusinf6667d12011-03-30 11:03:37 -0400572#include "apitrace.moc"