* We need to handle 4 different cases in here: * (1) our thread, from ctor: * - wait until next event-loop re-entry of our thread * - emit objectCreated if object still valid * (2) our thread, after ctor: * - emit objectCreated right away * (3) other thread, from ctor: * - wait until next event-loop re-entry in other thread (FIXME: we do not currently do this!!) * - post information to our
| 500 | * Pre-conditions: lock may or may not be held already, arbitrary thread |
| 501 | */ |
| 502 | void Probe::objectAdded(QObject *obj, bool fromCtor) |
| 503 | { |
| 504 | if (obj == nullptr) |
| 505 | return; |
| 506 | QMutexLocker lock(s_lock()); |
| 507 | |
| 508 | // attempt to ignore objects created by GammaRay itself, especially short-lived ones |
| 509 | if (fromCtor && ProbeGuard::insideProbe() && obj->thread() == QThread::currentThread()) |
| 510 | return; |
| 511 | |
| 512 | // ignore objects created when global statics are already getting destroyed (on exit) |
| 513 | if (s_listener.isDestroyed()) |
| 514 | return; |
| 515 | |
| 516 | |
| 517 | if (Execution::hasFastStackTrace() && fromCtor) { |
| 518 | s_listener()->constructionBacktracesForObjects.insert(obj, Execution::stackTrace(32, 2)); // skip 2: this and the hook function calling us |
| 519 | } |
| 520 | |
| 521 | if (!isInitialized()) { |
| 522 | IF_DEBUG(cout |
| 523 | << "objectAdded Before: " |
| 524 | << hex << obj |
| 525 | << (fromCtor ? " (from ctor)" : "") << endl;) |
| 526 | s_listener()->addedBeforeProbeInstance << obj; |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | if (instance()->filterObject(obj)) { |
| 531 | IF_DEBUG(cout |
| 532 | << "objectAdded Filter: " |
| 533 | << hex << obj |
| 534 | << (fromCtor ? " (from ctor)" : "") << endl;) |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | if (instance()->m_validObjects.contains(obj)) { |
| 539 | // this happens when we get a child event before the objectAdded call from the ctor |
| 540 | // or when we add an item from addedBeforeProbeInstance who got added already |
| 541 | // due to the add-parent-before-child logic |
| 542 | IF_DEBUG(cout |
| 543 | << "objectAdded Known: " |
| 544 | << hex << obj |
| 545 | << (fromCtor ? " (from ctor)" : "") << endl;) |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | // make sure we already know the parent |
| 550 | if (obj->parent() && !instance()->m_validObjects.contains(obj->parent())) |
| 551 | objectAdded(obj->parent(), fromCtor); |
| 552 | Q_ASSERT(!obj->parent() || instance()->m_validObjects.contains(obj->parent())); |
| 553 | |
| 554 | instance()->m_validObjects << obj; |
| 555 | |
| 556 | if (!fromCtor && obj->parent() && instance()->isObjectCreationQueued(obj->parent())) { |
| 557 | // when a child event triggers a call to objectAdded while inside the ctor |
| 558 | // the parent is already tracked but it's call to objectFullyConstructed |
| 559 | // was delayed. hence we must do the same for the child for integrity |