Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmldebug.cpp
Go to the documentation of this file.
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// Qt-Security score:critical reason:enables-debug-framework
4
5#include "qqmldebug.h"
8
9#include <private/qqmlengine_p.h>
10#include <private/qv4compileddata_p.h>
11
12#include <atomic>
13#include <cstdio>
14
16
17QT_BEGIN_NAMESPACE
18
19#if __cplusplus >= 202002L
20# define Q_ATOMIC_FLAG_INIT {}
21#else
22# define Q_ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT // deprecated in C++20
23#endif
24
25/*!
26 \class QQmlDebuggingEnabler
27 \inmodule QtQml
28 \brief The QQmlDebuggingEnabler class provides methods to enable debugging or profiling.
29
30 Usually QML debugging and profiling is enabled by passing
31 \c{QT_ENABLE_QML_DEBUG} via CMake or \c{CONFIG+=qml_debug} via qmake when
32 building your application. At run time, the application generally parses
33 any \c{-qmljsdebugger} command line arguments to actually start debugging
34 or profiling.
35
36 You can instead handle these tasks manually by using the methods in this
37 class.
38 */
39
40Q_CONSTINIT static std::atomic_flag s_printedWarning = Q_ATOMIC_FLAG_INIT;
41
42/*!
43 Enable debugging or profiling. If \a printWarning is \c{true}, print the
44 following warning to stderr:
45
46 \badcode
47 QML debugging is enabled. Only use this in a safe environment.
48 \endcode
49
50 This method is automatically called at startup if \c{QT_ENABLE_QML_DEBUG}
51 or \c{CONFIG+=qml_debug} is passed at build time.
52
53 This method needs to be called one way or another before starting a debug
54 connector of any kind. Otherwise the connector will refuse to start.
55
56 \sa startTcpDebugServer(), connectToLocalDebugger(), startDebugConnector()
57 */
58void QQmlDebuggingEnabler::enableDebugging(bool printWarning)
59{
60 if (printWarning && !s_printedWarning.test_and_set(std::memory_order_relaxed)) {
61 fprintf(stderr, "QML debugging is enabled. Only use this in a safe environment.\n");
62 fflush(stderr); // We really want to print this warning, even if stderr is buffered
63 }
64 QQmlEnginePrivate::qml_debugging_enabled.store(true, std::memory_order_relaxed);
65}
66
67#if QT_DEPRECATED_SINCE(6, 4)
68/*!
69 \internal
70 */
71QQmlDebuggingEnabler::QQmlDebuggingEnabler(bool printWarning)
72{
73 enableDebugging(printWarning);
74};
75#endif // QT_DEPRECATED_SINCE(6, 4)
76
77/*!
78 * Retrieves the plugin keys of the debugger services provided by default. The debugger services
79 * enable a debug client to use a Qml/JavaScript debugger, in order to set breakpoints, pause
80 * execution, evaluate expressions and similar debugging tasks.
81 * \return List of plugin keys of default debugger services.
82 */
83QStringList QQmlDebuggingEnabler::debuggerServices()
84{
85 return {QV4DebugService::s_key, QQmlEngineDebugService::s_key, QDebugMessageService::s_key};
86}
87
88/*!
89 * Retrieves the plugin keys of the inspector services provided by default. The inspector services
90 * enable a debug client to use a visual inspector tool for Qt Quick.
91 * \return List of plugin keys of default inspector services.
92 */
93QStringList QQmlDebuggingEnabler::inspectorServices()
94{
95 return {QQmlInspectorService::s_key};
96}
97
98/*!
99 * Retrieves the names of the profiler services provided by default. The profiler services enable a
100 * debug client to use a profiler and track the time taken by various QML and JavaScript constructs,
101 * as well as the QtQuick SceneGraph.
102 * \return List of plugin keys of default profiler services.
103 */
104QStringList QQmlDebuggingEnabler::profilerServices()
105{
106 return {QQmlProfilerService::s_key, QQmlEngineControlService::s_key, QDebugMessageService::s_key};
107}
108
109/*!
110 * Retrieves the plugin keys of the debug services designed to be used with a native debugger. The
111 * native debugger will communicate with these services by directly reading and writing the
112 * application's memory.
113 * \return List of plugin keys of debug services designed to be used with a native debugger.
114 */
115QStringList QQmlDebuggingEnabler::nativeDebuggerServices()
116{
117 return {QQmlNativeDebugService::s_key};
118}
119
120/*!
121 * Restricts the services available from the debug connector. The connector will scan plugins in the
122 * "qmltooling" subdirectory of the default plugin path. If this function is not called before the
123 * debug connector is enabled, all services found that way will be available to any client. If this
124 * function is called, only the services with plugin keys given in \a services will be available.
125 *
126 * Use this method to disable debugger and inspector services when profiling to get better
127 * performance and more realistic profiles. The debugger service will put any JavaScript engine it
128 * connects to into interpreted mode, disabling the JIT compiler.
129 *
130 * \sa debuggerServices(), profilerServices(), inspectorServices()
131 */
132void QQmlDebuggingEnabler::setServices(const QStringList &services)
133{
134 QQmlDebugConnector::setServices(services);
135}
136
137/*!
138 * \enum QQmlDebuggingEnabler::StartMode
139 *
140 * Defines the debug connector's start behavior. You can interrupt QML engines starting while a
141 * debug client is connecting, in order to set breakpoints in or profile startup code.
142 *
143 * \value DoNotWaitForClient Run any QML engines as usual while the debug services are connecting.
144 * \value WaitForClient If a QML engine starts while the debug services are connecting,
145 * interrupt it until they are done.
146 */
147
148/*!
149 * Enables debugging for QML engines created after calling this function. The debug connector will
150 * listen on \a port at \a hostName and block the QML engine until it receives a connection if
151 * \a mode is \c WaitForClient. If \a mode is not specified it won't block and if \a hostName is not
152 * specified it will listen on all available interfaces. You can only start one debug connector at a
153 * time. A debug connector may have already been started if the -qmljsdebugger= command line
154 * argument was given. This method returns \c true if a new debug connector was successfully
155 * started, or \c false otherwise.
156 */
157bool QQmlDebuggingEnabler::startTcpDebugServer(int port, StartMode mode, const QString &hostName)
158{
159 QVariantHash configuration;
160 configuration[QLatin1String("portFrom")] = configuration[QLatin1String("portTo")] = port;
161 configuration[QLatin1String("block")] = (mode == WaitForClient);
162 configuration[QLatin1String("hostAddress")] = hostName;
163 return startDebugConnector(QLatin1String("QQmlDebugServer"), configuration);
164}
165
166/*!
167 * \since 5.6
168 *
169 * Enables debugging for QML engines created after calling this function. The debug connector will
170 * connect to a debugger waiting on a local socket at the given \a socketFileName and block the QML
171 * engine until the connection is established if \a mode is \c WaitForClient. If \a mode is not
172 * specified it will not block. You can only start one debug connector at a time. A debug connector
173 * may have already been started if the -qmljsdebugger= command line argument was given. This method
174 * returns \c true if a new debug connector was successfully started, or \c false otherwise.
175 */
176bool QQmlDebuggingEnabler::connectToLocalDebugger(const QString &socketFileName, StartMode mode)
177{
178 QVariantHash configuration;
179 configuration[QLatin1String("fileName")] = socketFileName;
180 configuration[QLatin1String("block")] = (mode == WaitForClient);
181 return startDebugConnector(QLatin1String("QQmlDebugServer"), configuration);
182}
183
184/*!
185 * \since 5.7
186 *
187 * Enables debugging for QML engines created after calling this function. A debug connector plugin
188 * specified by \a pluginName will be loaded and started using the given \a configuration. Supported
189 * configuration entries and their semantics depend on the plugin being loaded. You can only start
190 * one debug connector at a time. A debug connector may have already been started if the
191 * -qmljsdebugger= command line argument was given. This method returns \c true if a new debug
192 * connector was successfully started, or \c false otherwise.
193 */
194bool QQmlDebuggingEnabler::startDebugConnector(const QString &pluginName,
195 const QVariantHash &configuration)
196{
197 QQmlDebugConnector::setPluginKey(pluginName);
198 QQmlDebugConnector *connector = QQmlDebugConnector::instance();
199 return connector ? connector->open(configuration) : false;
200}
201
202enum { HookCount = 4 };
203
204// Only add to the end, and bump version if you do.
205quintptr Q_QML_EXPORT qtDeclarativeHookData[] = {
206 // Version of this Array. Bump if you add to end.
207 2,
208
209 // Number of entries in this array.
210 HookCount,
211
212 // TypeInformationVersion, an integral value, bumped whenever private
213 // object sizes or member offsets that are used in Qt Creator's
214 // data structure "pretty printing" change.
215 4,
216
217 // Version of the cache data.
218 QV4_DATA_STRUCTURE_VERSION
219};
220
221Q_STATIC_ASSERT(HookCount == sizeof(qtDeclarativeHookData) / sizeof(qtDeclarativeHookData[0]));
222
223QT_END_NAMESPACE
@ HookCount
#define Q_ATOMIC_FLAG_INIT
Definition qqmldebug.cpp:22
static Q_CONSTINIT std::atomic_flag s_printedWarning
Definition qqmldebug.cpp:40
QT_REQUIRE_CONFIG(qml_debug)
QDebug Q_QML_EXPORT operator<<(QDebug debug, const QQmlError &error)
Q_STATIC_ASSERT(sizeof(SharedImageHeader) % 4==0)