1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qqmldebug.h"
5#include "qqmldebugconnector_p.h"
6#include "qqmldebugserviceinterfaces_p.h"
7
8#include <private/qqmlengine_p.h>
9#include <private/qv4compileddata_p.h>
10
11#include <atomic>
12#include <cstdio>
13
14QT_REQUIRE_CONFIG(qml_debug);
15
16QT_BEGIN_NAMESPACE
17
18#if __cplusplus >= 202002L
19# define Q_ATOMIC_FLAG_INIT {}
20#else
21# define Q_ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT // deprecated in C++20
22#endif
23
24Q_CONSTINIT static std::atomic_flag s_printedWarning = Q_ATOMIC_FLAG_INIT;
25
26void QQmlDebuggingEnabler::enableDebugging(bool printWarning)
27{
28 if (printWarning && !s_printedWarning.test_and_set(m: std::memory_order_relaxed))
29 fprintf(stderr, format: "QML debugging is enabled. Only use this in a safe environment.\n");
30 QQmlEnginePrivate::qml_debugging_enabled.store(i: true, m: std::memory_order_relaxed);
31}
32
33#if QT_DEPRECATED_SINCE(6, 4)
34QQmlDebuggingEnabler::QQmlDebuggingEnabler(bool printWarning)
35{
36 enableDebugging(printWarning);
37};
38#endif // QT_DEPRECATED_SINCE(6, 4)
39
40/*!
41 * Retrieves the plugin keys of the debugger services provided by default. The debugger services
42 * enable a debug client to use a Qml/JavaScript debugger, in order to set breakpoints, pause
43 * execution, evaluate expressions and similar debugging tasks.
44 * \return List of plugin keys of default debugger services.
45 */
46QStringList QQmlDebuggingEnabler::debuggerServices()
47{
48 return {QV4DebugService::s_key, QQmlEngineDebugService::s_key, QDebugMessageService::s_key};
49}
50
51/*!
52 * Retrieves the plugin keys of the inspector services provided by default. The inspector services
53 * enable a debug client to use a visual inspector tool for Qt Quick.
54 * \return List of plugin keys of default inspector services.
55 */
56QStringList QQmlDebuggingEnabler::inspectorServices()
57{
58 return {QQmlInspectorService::s_key};
59}
60
61/*!
62 * Retrieves the names of the profiler services provided by default. The profiler services enable a
63 * debug client to use a profiler and track the time taken by various QML and JavaScript constructs,
64 * as well as the QtQuick SceneGraph.
65 * \return List of plugin keys of default profiler services.
66 */
67QStringList QQmlDebuggingEnabler::profilerServices()
68{
69 return {QQmlProfilerService::s_key, QQmlEngineControlService::s_key, QDebugMessageService::s_key};
70}
71
72/*!
73 * Retrieves the plugin keys of the debug services designed to be used with a native debugger. The
74 * native debugger will communicate with these services by directly reading and writing the
75 * application's memory.
76 * \return List of plugin keys of debug services designed to be used with a native debugger.
77 */
78QStringList QQmlDebuggingEnabler::nativeDebuggerServices()
79{
80 return {QQmlNativeDebugService::s_key};
81}
82
83/*!
84 * Restricts the services available from the debug connector. The connector will scan plugins in the
85 * "qmltooling" subdirectory of the default plugin path. If this function is not called before the
86 * debug connector is enabled, all services found that way will be available to any client. If this
87 * function is called, only the services with plugin keys given in \a services will be available.
88 *
89 * Use this method to disable debugger and inspector services when profiling to get better
90 * performance and more realistic profiles. The debugger service will put any JavaScript engine it
91 * connects to into interpreted mode, disabling the JIT compiler.
92 *
93 * \sa debuggerServices(), profilerServices(), inspectorServices()
94 */
95void QQmlDebuggingEnabler::setServices(const QStringList &services)
96{
97 QQmlDebugConnector::setServices(services);
98}
99
100/*!
101 * \enum QQmlDebuggingEnabler::StartMode
102 *
103 * Defines the debug connector's start behavior. You can interrupt QML engines starting while a
104 * debug client is connecting, in order to set breakpoints in or profile startup code.
105 *
106 * \value DoNotWaitForClient Run any QML engines as usual while the debug services are connecting.
107 * \value WaitForClient If a QML engine starts while the debug services are connecting,
108 * interrupt it until they are done.
109 */
110
111/*!
112 * Enables debugging for QML engines created after calling this function. The debug connector will
113 * listen on \a port at \a hostName and block the QML engine until it receives a connection if
114 * \a mode is \c WaitForClient. If \a mode is not specified it won't block and if \a hostName is not
115 * specified it will listen on all available interfaces. You can only start one debug connector at a
116 * time. A debug connector may have already been started if the -qmljsdebugger= command line
117 * argument was given. This method returns \c true if a new debug connector was successfully
118 * started, or \c false otherwise.
119 */
120bool QQmlDebuggingEnabler::startTcpDebugServer(int port, StartMode mode, const QString &hostName)
121{
122 QVariantHash configuration;
123 configuration[QLatin1String("portFrom")] = configuration[QLatin1String("portTo")] = port;
124 configuration[QLatin1String("block")] = (mode == WaitForClient);
125 configuration[QLatin1String("hostAddress")] = hostName;
126 return startDebugConnector(pluginName: QLatin1String("QQmlDebugServer"), configuration);
127}
128
129/*!
130 * \since 5.6
131 *
132 * Enables debugging for QML engines created after calling this function. The debug connector will
133 * connect to a debugger waiting on a local socket at the given \a socketFileName and block the QML
134 * engine until the connection is established if \a mode is \c WaitForClient. If \a mode is not
135 * specified it will not block. You can only start one debug connector at a time. A debug connector
136 * may have already been started if the -qmljsdebugger= command line argument was given. This method
137 * returns \c true if a new debug connector was successfully started, or \c false otherwise.
138 */
139bool QQmlDebuggingEnabler::connectToLocalDebugger(const QString &socketFileName, StartMode mode)
140{
141 QVariantHash configuration;
142 configuration[QLatin1String("fileName")] = socketFileName;
143 configuration[QLatin1String("block")] = (mode == WaitForClient);
144 return startDebugConnector(pluginName: QLatin1String("QQmlDebugServer"), configuration);
145}
146
147/*!
148 * \since 5.7
149 *
150 * Enables debugging for QML engines created after calling this function. A debug connector plugin
151 * specified by \a pluginName will be loaded and started using the given \a configuration. Supported
152 * configuration entries and their semantics depend on the plugin being loaded. You can only start
153 * one debug connector at a time. A debug connector may have already been started if the
154 * -qmljsdebugger= command line argument was given. This method returns \c true if a new debug
155 * connector was successfully started, or \c false otherwise.
156 */
157bool QQmlDebuggingEnabler::startDebugConnector(const QString &pluginName,
158 const QVariantHash &configuration)
159{
160 QQmlDebugConnector::setPluginKey(pluginName);
161 QQmlDebugConnector *connector = QQmlDebugConnector::instance();
162 return connector ? connector->open(configuration) : false;
163}
164
165enum { HookCount = 4 };
166
167// Only add to the end, and bump version if you do.
168quintptr Q_QML_EXPORT qtDeclarativeHookData[] = {
169 // Version of this Array. Bump if you add to end.
170 2,
171
172 // Number of entries in this array.
173 HookCount,
174
175 // TypeInformationVersion, an integral value, bumped whenever private
176 // object sizes or member offsets that are used in Qt Creator's
177 // data structure "pretty printing" change.
178 3,
179
180 // Version of the cache data.
181 QV4_DATA_STRUCTURE_VERSION
182};
183
184Q_STATIC_ASSERT(HookCount == sizeof(qtDeclarativeHookData) / sizeof(qtDeclarativeHookData[0]));
185
186QT_END_NAMESPACE
187

source code of qtdeclarative/src/qml/debugger/qqmldebug.cpp