New tool: QmlStackTraceHelper

This commit is contained in:
Sergio Martins
2019-05-14 11:50:23 +01:00
parent 2a21c8cede
commit cc9c8bcfbc
8 changed files with 178 additions and 1 deletions

View File

@@ -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
=========

View File

@@ -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 <QString>
#include <QVector>
#include <QGuiApplication>
#include <QQuickWindow>
#include <QQmlContext>
#include <QQuickItem>
#include <QtQml/private/qqmlengine_p.h>
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<QQuickWindow*>(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";
}
}
}
}

View File

@@ -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<Qt::WindowType>(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.

View File

@@ -0,0 +1,8 @@
qrc*o
*pro.user
Makefile
/example
*.o
main.moc
moc_predefs.h
qrc_qrc.cpp

View File

@@ -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

View File

@@ -0,0 +1,27 @@
#include <QGuiApplication>
#include <QQuickView>
#include <QQmlContext>
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 <main.moc>

View File

@@ -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();
}
}

View File

@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>