1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*
objectinspector.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "objectinspector.h"
#include "propertycontroller.h"
#include "methodsextension.h"
#include "classinfoextension.h"
#include "enumsextension.h"
#include "propertiesextension.h"
#include "connectionsextension.h"
#include "applicationattributeextension.h"
#include "bindingextension.h"
#include "stacktraceextension.h"
#include "inboundconnectionsmodel.h"
#include "outboundconnectionsmodel.h"
#include "objectdataprovider.h"
#include <common/objectbroker.h>
#include <common/objectmodel.h>
#include <core/bindingaggregator.h>
#include <core/problemcollector.h>
#include <core/util.h>
#include <remote/serverproxymodel.h>
#include <QCoreApplication>
#include <QItemSelectionModel>
#include <QMetaMethod>
#include <QMutexLocker>
#include <QThread>
using namespace GammaRay;
ObjectInspector::ObjectInspector(Probe *probe, QObject *parent)
: QObject(parent)
{
registerPCExtensions();
m_propertyController = new PropertyController(QStringLiteral(
"com.kdab.GammaRay.ObjectInspector"),
this);
auto proxy = new ServerProxyModel<QSortFilterProxyModel>(this);
proxy->setSourceModel(probe->objectTreeModel());
probe->registerModel(QStringLiteral("com.kdab.GammaRay.ObjectInspectorTree"), proxy);
m_selectionModel = ObjectBroker::selectionModel(proxy);
connect(m_selectionModel,
&QItemSelectionModel::selectionChanged,
this, &ObjectInspector::objectSelectionChanged);
connect(probe, &Probe::objectSelected,
this, &ObjectInspector::objectSelected);
ProblemCollector::registerProblemChecker(QStringLiteral("com.kdab.GammaRay.ObjectInspector.BindingLoopScan"),
QStringLiteral("Binding Loops"),
QStringLiteral("Scans all QObjects for binding loops"),
&BindingAggregator::scanForBindingLoops);
ProblemCollector::registerProblemChecker(QStringLiteral("com.kdab.GammaRay.ObjectInspector.ConnectionsCheck"),
QStringLiteral("Connection issues"),
QStringLiteral("Scans all QObjects for direct cross-thread and duplicate connections"),
&ObjectInspector::scanForConnectionIssues);
ProblemCollector::registerProblemChecker(QStringLiteral("com.kdab.GammaRay.ObjectInspector.ThreadAffinityCheck"),
QStringLiteral("Threading issues"),
QStringLiteral("Scans all QObjects for thread affinity issues"),
&ObjectInspector::scanForThreadAffinityIssues);
}
void ObjectInspector::objectSelectionChanged(const QItemSelection &selection)
{
if (selection.isEmpty())
modelIndexSelected(QModelIndex());
else
modelIndexSelected(selection.first().topLeft());
}
void ObjectInspector::modelIndexSelected(const QModelIndex &index)
{
if (index.isValid()) {
QObject *obj = index.data(ObjectModel::ObjectRole).value<QObject *>();
m_propertyController->setObject(obj);
} else {
m_propertyController->setObject(nullptr);
}
}
void ObjectInspector::objectSelected(QObject *object)
{
const QAbstractItemModel *model = m_selectionModel->model();
const QModelIndexList indexList = model->match(model->index(0, 0),
ObjectModel::ObjectRole,
QVariant::fromValue<QObject *>(object), 1,
Qt::MatchExactly | Qt::MatchRecursive | Qt::MatchWrap);
if (indexList.isEmpty())
return;
const QModelIndex index = indexList.first();
m_selectionModel->select(
index,
QItemSelectionModel::Select | QItemSelectionModel::Clear
| QItemSelectionModel::Rows | QItemSelectionModel::Current);
// TODO: move this to the client side!
// ui->objectTreeView->scrollTo(index);
modelIndexSelected(index);
}
void ObjectInspector::registerPCExtensions()
{
PropertyController::registerExtension<ClassInfoExtension>();
PropertyController::registerExtension<MethodsExtension>();
PropertyController::registerExtension<EnumsExtension>();
PropertyController::registerExtension<PropertiesExtension>();
PropertyController::registerExtension<ConnectionsExtension>();
PropertyController::registerExtension<ApplicationAttributeExtension>();
PropertyController::registerExtension<BindingExtension>();
PropertyController::registerExtension<StackTraceExtension>();
}
QVector<QByteArray> GammaRay::ObjectInspectorFactory::selectableTypes() const
{
return QVector<QByteArray>() << QObject::staticMetaObject.className();
}
void ObjectInspector::scanForConnectionIssues()
{
const QVector<QObject *> &allObjects = Probe::instance()->allQObjects();
QMutexLocker lock(Probe::objectLock());
for (QObject *obj : allObjects) {
if (!Probe::instance()->isValidObject(obj))
continue;
auto reportProblem = [obj](const AbstractConnectionsModel::Connection &connection, const QString &descriptionTemplate, const QString &problemType, bool isOutbound) {
QObject *sender = isOutbound ? obj : connection.endpoint.data();
QObject *receiver = isOutbound ? connection.endpoint.data() : obj;
if (!sender || !receiver) {
return;
}
QString signalName = sender->metaObject()->method(connection.signalIndex).name();
QString slotName = connection.slotIndex < 0 ? QStringLiteral("<slot object>") : receiver->metaObject()->method(connection.slotIndex).name();
QString senderName = Util::displayString(sender);
QString receiverName = Util::displayString(receiver);
Problem p;
p.severity = Problem::Warning;
p.description = descriptionTemplate.arg(receiverName, slotName, senderName, signalName);
p.object = ObjectId(receiver);
// p.location = bindingNode->sourceLocation(); //TODO can we get source locations of connect-statements?
p.problemId = QStringLiteral("com.kdab.GammaRay.ObjectInspector.ConnectionsCheck.%1:%2.%3-%4.%5")
.arg(problemType,
QString::number(reinterpret_cast<quintptr>(sender)),
QString::number(connection.signalIndex),
QString::number(reinterpret_cast<quintptr>(receiver)),
QString::number(connection.slotIndex));
p.findingCategory = Problem::Scan;
ProblemCollector::addProblem(p);
};
auto connections = InboundConnectionsModel::inboundConnectionsForObject(obj);
for (auto it = connections.begin(); it != connections.end(); ++it) {
auto &&connection = *it;
if (AbstractConnectionsModel::isDuplicate(connections, connection)) {
reportProblem(connection, QStringLiteral("The slot %1->%2 is connected to the signal %3->%4 multiple times."), QStringLiteral("Duplicate"), false);
}
if (AbstractConnectionsModel::isDirectCrossThreadConnection(obj, connection)) {
reportProblem(connection, QStringLiteral("The connection of slot %1->%2 to the signal %3->%4 is a direct cross-thread connection."), QStringLiteral("CrossTread"), false);
}
}
connections = OutboundConnectionsModel::outboundConnectionsForObject(obj);
for (auto it = connections.begin(); it != connections.end(); ++it) {
auto &&connection = *it;
if (AbstractConnectionsModel::isDuplicate(connections, connection)) {
reportProblem(connection, QStringLiteral("The slot %1->%2 is connected to the signal %3->%4 multiple times."), QStringLiteral("Duplicate"), true);
}
if (AbstractConnectionsModel::isDirectCrossThreadConnection(obj, connection)) {
reportProblem(connection, QStringLiteral("The connection of slot %1->%2 to the signal %3->%4 is a direct cross-thread connection."), QStringLiteral("CrossTread"), true);
}
}
}
}
void ObjectInspector::scanForThreadAffinityIssues()
{
const auto probe = Probe::instance();
const auto &objects = probe->allQObjects();
QMutexLocker lock(Probe::objectLock());
for (const auto object : objects) {
if (!probe->isValidObject(object)) {
continue;
}
const auto objectName = Util::displayString(object);
if (object == object->thread()) {
Problem problem;
problem.severity = Problem::Warning;
problem.description = QStringLiteral("The thread %1 has affinity with itself.").arg(objectName);
problem.object = ObjectId(object);
problem.locations.append(probe->objectCreationSourceLocation(object));
problem.problemId = QStringLiteral("com.kdab.GammaRay.ObjectInspector.ThreadAffinityCheck.Self.%1")
.arg(QString::number(reinterpret_cast<quintptr>(object)));
problem.findingCategory = Problem::Scan;
ProblemCollector::addProblem(problem);
}
const auto parent = object->parent();
if (parent == nullptr) {
continue;
}
const auto parentName = Util::displayString(parent);
if (object->thread() != parent->thread()) {
Problem problem;
problem.severity = Problem::Warning;
problem.description = QStringLiteral("The object %1 doesn't have the same thread affinity as its parent %2.").arg(objectName, parentName);
problem.object = ObjectId(object);
problem.locations.append(probe->objectCreationSourceLocation(object));
problem.problemId = QStringLiteral("com.kdab.GammaRay.ObjectInspector.ThreadAffinityCheck.%1:%2")
.arg(QString::number(reinterpret_cast<quintptr>(object)),
QString::number(reinterpret_cast<quintptr>(parent)));
problem.findingCategory = Problem::Scan;
ProblemCollector::addProblem(problem);
}
if (qobject_cast<QThread *>(parent) && object->thread() != object->parent()) {
Problem problem;
problem.severity = Problem::Warning;
problem.description = QStringLiteral("The object %1 has thread %2 as parent, but doesn't have affinity with it.").arg(objectName, parentName);
problem.object = ObjectId(object);
problem.locations.append(probe->objectCreationSourceLocation(object));
problem.problemId = QStringLiteral("com.kdab.GammaRay.ObjectInspector.ThreadAffinityCheck.Parent.%1")
.arg(QString::number(reinterpret_cast<quintptr>(object)),
QString::number(reinterpret_cast<quintptr>(parent)));
problem.findingCategory = Problem::Scan;
ProblemCollector::addProblem(problem);
}
}
}
|