Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
qmldebugclient.cpp File Reference

QmlDebug: an in-process client of the QML engine debug service (design 0022, step 2a). More...

#include "qmldebugclient_p.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QDataStream>
#include <QtCore/QHash>
#include <QtCore/QLibraryInfo>
#include <QtCore/QPointer>
#include <QtCore/QRandomGenerator>
#include <QtCore/QTimer>
#include <QtCore/QVersionNumber>
#include <QtCore/QtEndian>
#include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket>
#include <QtQml/QQmlContext>
#include <QtQml/qqml.h>
#include <QtQml/QQmlDebuggingEnabler>
#include <memory>
#include <optional>
#include <utility>

Classes

class  Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::Client
 The client: the socket, the handshake, and the queries in flight. More...
struct  Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::Client::Pending
 A query waiting for its answer. More...
class  Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::Lookup
 Finds an object's debug id, then fetches it with its properties. More...

Namespaces

namespace  Sonde
 Embeddable Model Context Protocol server for Qt applications.

Functions

QString Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::serverId ()
 The control channel's name, client to server.
QString Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::clientId ()
 The control channel's name, server to client.
QString Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::serviceName ()
 The engine debug service's name.
void Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::readObjectData (QDataStream &ds, ServiceObject *o)
 Reads one object's own data (no children, no properties).
void Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::readObject (QDataStream &ds, ServiceObject *o, QList< ServiceObject > *children)
 Reads an object as FETCH_OBJECT_R sends it: its data, its children (only their data: fetches don't recurse) and its properties.
void Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::readContext (QDataStream &ds, QList< ServiceObject > *objects, int depth=0)
 Reads a context as LIST_OBJECTS_R sends it, adding every object in it, and in its child contexts, to objects.
QString Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::qmlId (const QObject *object)
 The QML id of object, or an empty string.
int Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::serviceChildIndex (const QObject *object)
 The position of object among its parent's children, as the service counts them (it leaves out QQmlContext children), or -1.
Client & Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::client ()
 The process-wide client, created on first use. Main thread only.
QHash< QObject *, std::pair< QPointer< QObject >, int > > & Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::knownIds ()
 Debug ids already found, by object: they stay the same while the object lives.
bool Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::isComponentRoot (const QObject *object)
 Whether object is the root of a component instance: those are the objects the service lists (LIST_OBJECTS), each the context object of its own context.
bool Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::sameValues (const ServiceObject &entry, const QObject *object)
 Whether the simple property values the service sent for entry are object's.
void Sonde::QmlDebug::start ()
 Turns on QML debugging, with only the engine debug service, connected to a local socket Sonde opened itself.
bool Sonde::QmlDebug::isAvailable ()
 Whether the engine debug service is connected and answering.
QString Sonde::QmlDebug::status ()
 What binding inspection through the service can do now, for app_info and qml_binding: "available", or why not.
void Sonde::QmlDebug::fetchProperties (QObject *object, QObject *context, const FetchCallback &done)
 Asks the service for object and its properties, each with whether it is bound now.

Variables

constexpr int Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::kConnectTimeoutMs = 5000
 How long the connector may take to connect.
constexpr int Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::kQueryTimeoutMs = 5000
 How long an answer may take.
constexpr qint32 Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::kProtocolVersion = 1
 The debug protocol's version.
constexpr qint32 Sonde::QmlDebug::anonymous_namespace{qmldebugclient.cpp}::kSignalProperty = 4
 The service's property type for signal handlers.

Detailed Description

QmlDebug: an in-process client of the QML engine debug service (design 0022, step 2a).

Qt's QML debugging runs services in the application, which a client talks to through a connector. Qt Creator's QML inspector is such a client; this is a smaller one, in the same process. It opens a local socket, asks Qt to connect its debug connector to it (QQmlDebuggingEnabler::connectToLocalDebugger()), and speaks the protocol itself, since Qt's client classes are private:

  • Packets are a little-endian qint32 total size (including itself), then a QDataStream.
  • The handshake: "QDeclarativeDebugServer" 0, protocol version 1, the services the client wants, its QDataStream version, and true (it reads several messages per packet). The server answers "QDeclarativeDebugClient" 0, its version, its services and their versions, and the QDataStream version both sides use from then on.
  • Then each packet is a service name and its messages (QByteArrays). The engine service's messages are a type ("LIST_ENGINES", ...), a query id, and the arguments; answers are the type with "_R" and the same id.

The protocol and the engine service's messages were the same in Qt 6.8.0 and 6.12.0 (see design 0022).

Function Documentation

◆ fetchProperties()

void Sonde::QmlDebug::fetchProperties ( QObject * object,
QObject * context,
const FetchCallback & done )

Asks the service for object and its properties, each with whether it is bound now.

Answers later, on the main thread, through done, unless context is destroyed first. The service names objects by ids of its own; object is matched to one by its objectName, QML id and parents, and by its position among its parent's children when siblings look the same.

◆ start()

void Sonde::QmlDebug::start ( )

Turns on QML debugging, with only the engine debug service, connected to a local socket Sonde opened itself.

Does nothing after the first call.

Must run before the first QQmlEngine is created: the service doesn't learn about engines created earlier. It is off when SONDE_QML_DEBUG is 0, when the application was started with -qmljsdebugger (Qt Creator's QML debugger, which takes the process's only debug connector), and when Qt was built without QML debugging. status() says which.