From cc9c8bcfbcc105c0c5eb5ea59d69f736a379e1bb Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Tue, 14 May 2019 11:50:23 +0100 Subject: [PATCH] New tool: QmlStackTraceHelper --- README.md | 3 +- .../QmlStackTraceHelper.cpp | 55 +++++++++++++++++++ qt/qml/QmlStackTraceHelper/README.md | 26 +++++++++ qt/qml/QmlStackTraceHelper/example/.gitignore | 8 +++ .../QmlStackTraceHelper/example/example.pro | 25 +++++++++ qt/qml/QmlStackTraceHelper/example/main.cpp | 27 +++++++++ qt/qml/QmlStackTraceHelper/example/main.qml | 30 ++++++++++ qt/qml/QmlStackTraceHelper/example/qrc.qrc | 5 ++ 8 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 qt/qml/QmlStackTraceHelper/QmlStackTraceHelper.cpp create mode 100644 qt/qml/QmlStackTraceHelper/README.md create mode 100644 qt/qml/QmlStackTraceHelper/example/.gitignore create mode 100644 qt/qml/QmlStackTraceHelper/example/example.pro create mode 100644 qt/qml/QmlStackTraceHelper/example/main.cpp create mode 100644 qt/qml/QmlStackTraceHelper/example/main.qml create mode 100644 qt/qml/QmlStackTraceHelper/example/qrc.qrc diff --git a/README.md b/README.md index df3c891..cedc0b4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ Tools ================= - [Ui Watchdog](https://github.com/KDAB/KDToolBox/tree/master/qt/ui_watchdog) An _header-only_ tool to break the program when the main-thread event loop is blocked for more than 300ms. - +- [Qml Stacktrace Helper](https://github.com/KDAB/KDToolBox/tree/master/qt/qml/QmlStackTraceHelper) + A function for retrieving a QML backtrace with gdb. Licensing ========= diff --git a/qt/qml/QmlStackTraceHelper/QmlStackTraceHelper.cpp b/qt/qml/QmlStackTraceHelper/QmlStackTraceHelper.cpp new file mode 100644 index 0000000..3947286 --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/QmlStackTraceHelper.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** Copyright (C) 2019 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com. +** Author: sergio.martins@kdab.com +** All rights reserved. +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL.txt included. +** +** You may even contact us at info@kdab.com for different licensing options. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +extern "C" char *qt_v4StackTrace(void *executionContext); + +namespace KDAB { + +QString qmlStackTrace(QV4::ExecutionEngine *engine) +{ + return QString(qt_v4StackTrace(engine->currentContext())); +} + +void printQmlStackTraces() +{ + auto windows = qApp->topLevelWindows(); + for (QWindow *w : windows) { + if (auto qw = qobject_cast(w)) { + QQuickItem *item = qw->contentItem(); + QQmlContext* context = QQmlEngine::contextForObject(item); + if (!context) + continue; + QQmlEnginePrivate *enginePriv = QQmlEnginePrivate::get(context->engine()); + QV4::ExecutionEngine *v4engine = enginePriv->v4engine(); + qDebug() << "Stack trace for" << qw; + qDebug().noquote() << qmlStackTrace(v4engine); + qDebug() << "\n"; + } + } +} + +} diff --git a/qt/qml/QmlStackTraceHelper/README.md b/qt/qml/QmlStackTraceHelper/README.md new file mode 100644 index 0000000..308ef9a --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/README.md @@ -0,0 +1,26 @@ +Qml Stacktrace Helper +===================== + +An helper function so you can get a QML backtrace from gdb. + + +Example Usage +============== + +Build `QmlStackTraceHelper.cpp` with your project. + +``` +(gdb) call KDAB::printQmlStackTraces() +Stack trace for QQuickView(0x7fffffffd2e0 active exposed, visibility=QWindow::Windowed, flags=QFlags(Window), geometry=0,1290 500x500) + onSomeIndirection2Changed [qrc:/main.qml:28] + onSomeIndirectionChanged [qrc:/main.qml:22] + onClicked [qrc:/main.qml:17] +``` +Also useful when running under `valgrind --vgdb=yes`. +Just link `QmlStackTraceHelper.cpp` with your application. + + +Upstreaming to Qt +================= + +We're open to ideas on how to upstream this. diff --git a/qt/qml/QmlStackTraceHelper/example/.gitignore b/qt/qml/QmlStackTraceHelper/example/.gitignore new file mode 100644 index 0000000..7ff81af --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/example/.gitignore @@ -0,0 +1,8 @@ +qrc*o +*pro.user +Makefile +/example +*.o +main.moc +moc_predefs.h +qrc_qrc.cpp diff --git a/qt/qml/QmlStackTraceHelper/example/example.pro b/qt/qml/QmlStackTraceHelper/example/example.pro new file mode 100644 index 0000000..479b72c --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/example/example.pro @@ -0,0 +1,25 @@ +###################################################################### +# Automatically generated by qmake (3.1) Fri Mar 22 14:18:16 2019 +###################################################################### + +TEMPLATE = app +TARGET = example +INCLUDEPATH += . + +# The following define makes your compiler warn you if you use any +# feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +# Input +SOURCES += main.cpp ../QmlStackTraceHelper.cpp +QT += qml-private quick +CONFIG += debug + +RESOURCES += qrc.qrc diff --git a/qt/qml/QmlStackTraceHelper/example/main.cpp b/qt/qml/QmlStackTraceHelper/example/main.cpp new file mode 100644 index 0000000..19ce45e --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/example/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +class Controller : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE void crash() { + delete this; delete this; + } +}; + + +int main(int a, char **b) +{ + QGuiApplication app(a, b); + + QQuickView view; + view.rootContext()->setContextProperty("_controller", new Controller()); + view.setSource(QUrl("qrc:/main.qml")); + view.show(); + + return app.exec(); +} + +#include diff --git a/qt/qml/QmlStackTraceHelper/example/main.qml b/qt/qml/QmlStackTraceHelper/example/main.qml new file mode 100644 index 0000000..4c35262 --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/example/main.qml @@ -0,0 +1,30 @@ +import QtQuick 2.5 + +Rectangle { + color: "red" + width: 500 + height: 500 + + property int someIndirection: 0 + property int someIndirection2: 0 + + Text { + text: "click to crash" + } + MouseArea { + anchors.fill: parent + onClicked: { + someIndirection = 1; + } + } + + onSomeIndirectionChanged: { + if (someIndirection > 0) + someIndirection2 = 1; + } + + onSomeIndirection2Changed: { + if (someIndirection2 > 0) + _controller.crash(); + } +} diff --git a/qt/qml/QmlStackTraceHelper/example/qrc.qrc b/qt/qml/QmlStackTraceHelper/example/qrc.qrc new file mode 100644 index 0000000..5f6483a --- /dev/null +++ b/qt/qml/QmlStackTraceHelper/example/qrc.qrc @@ -0,0 +1,5 @@ + + + main.qml + +