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 <QByteArray>
13
14#include <atomic>
15#include <cstdio>
16
17#ifdef Q_OS_WIN
18#include <qt_windows.h>
19#endif
20
22
23QT_BEGIN_NAMESPACE
24
25#if __cplusplus >= 202002L
26# define Q_ATOMIC_FLAG_INIT {}
27#else
28# define Q_ATOMIC_FLAG_INIT ATOMIC_FLAG_INIT // deprecated in C++20
29#endif
30
31/*!
32 \class QQmlDebuggingEnabler
33 \inmodule QtQml
34 \brief The QQmlDebuggingEnabler class provides methods to enable debugging or profiling.
35
36 Usually QML debugging and profiling is enabled by passing
37 \c{QT_ENABLE_QML_DEBUG} via CMake or \c{CONFIG+=qml_debug} via qmake when
38 building your application. At run time, the application generally parses
39 any \c{-qmljsdebugger} command line arguments to actually start debugging
40 or profiling.
41
42 You can instead handle these tasks manually by using the methods in this
43 class.
44 */
45
46Q_CONSTINIT static std::atomic_flag s_printedWarning = Q_ATOMIC_FLAG_INIT;
47
48/*!
49 Enable debugging or profiling. If \a printWarning is \c{true}, print the
50 following warning to stderr:
51
52 \badcode
53 QML debugging is enabled. Only use this in a safe environment.
54 \endcode
55
56 If \a printWarning is \c{false}, the warning gets suppressed. Then, nothing
57 tells the user that debugging is enabled. Only suppress the warning if the
58 application informs the user by other means, or if it runs in an
59 environment where the exposure of the debug connectors is acceptable.
60
61 This method needs to be called one way or another before starting a debug
62 connector of any kind. Otherwise the connector will refuse to start.
63
64 This method is automatically called at startup (with warnings enabled) if
65 either:
66
67 \list
68 \li \c{QT_ENABLE_QML_DEBUG} is passed via CMake
69 \li \c{CONFIG+=qml_debug} is passed via qmake
70 \li \c{QT_QML_DEBUG} is passed to the compiler when it builds a file that
71 includes \c{qqmldebug.h} directly or indirectly. Most public headers
72 in QtQml include it.
73 \endlist
74
75 If \c{QT_QML_DEBUG_NO_WARNING} is passed to the compiler when it builds a
76 file that includes \c{qqmldebug.h} directly or indirectly, the method is
77 called with the warning suppressed instead.
78
79 Calling the method explicitly from application code has exactly the same
80 effect as passing any of the above defines, even if none of them were passed.
81 The absence of the defines does not guarantee the absence of a debug
82 connector.
83
84 To make sure that an application cannot be debugged, build Qt with
85 the \c{qml-debug} feature disabled or don't deploy the \c qmltooling plugins.
86
87 \warning Never enable QML debugging in applications you deploy to production
88 or run in an untrusted environment. The debug connectors do not authenticate
89 or encrypt. Any peer that can reach an active connector gains control over
90 the application. See \l {QML debugging infrastructure} for details.
91
92 \sa startTcpDebugServer(), connectToLocalDebugger(), startDebugConnector()
93 */
94void QQmlDebuggingEnabler::enableDebugging(bool printWarning)
95{
96 if (printWarning && !s_printedWarning.test_and_set(std::memory_order_relaxed)) {
97 QByteArray warning("QML debugging is enabled. Only use this in a safe environment.");
98 if (qEnvironmentVariableIsSet("CLICOLOR_FORCE") || qEnvironmentVariableIsSet("QTC_RUN")
99 || qEnvironmentVariableIsSet("VSCODE_CLI")) {
100 warning = QByteArray("\033[38;2;255;0;0m") + warning + "\033[0m";
101 }
102 warning += '\n';
103#ifdef Q_OS_WIN
104 // Debuggers on Windows (except vscode) do not display the stderr output
105 if (!qEnvironmentVariableIsSet("VSCODE_CLI"))
106 OutputDebugStringA(warning);
107#endif
108
109 fprintf(stderr, "%s", warning.constData());
110 fflush(stderr); // We really want to print this warning, even if stderr is buffered
111 }
112 QQmlEnginePrivate::qml_debugging_enabled.store(true, std::memory_order_relaxed);
113}
114
115#if QT_DEPRECATED_SINCE(6, 4)
116/*!
117 \internal
118 */
119QQmlDebuggingEnabler::QQmlDebuggingEnabler(bool printWarning)
120{
121 enableDebugging(printWarning);
122};
123#endif // QT_DEPRECATED_SINCE(6, 4)
124
125/*!
126 * Retrieves the plugin keys of the debugger services provided by default. The debugger services
127 * enable a debug client to use a Qml/JavaScript debugger, in order to set breakpoints, pause
128 * execution, evaluate expressions and similar debugging tasks.
129 * \return List of plugin keys of default debugger services.
130 */
131QStringList QQmlDebuggingEnabler::debuggerServices()
132{
133 return {QV4DebugService::s_key, QQmlEngineDebugService::s_key, QDebugMessageService::s_key};
134}
135
136/*!
137 * Retrieves the plugin keys of the inspector services provided by default. The inspector services
138 * enable a debug client to use a visual inspector tool for Qt Quick.
139 * \return List of plugin keys of default inspector services.
140 */
141QStringList QQmlDebuggingEnabler::inspectorServices()
142{
143 return {QQmlInspectorService::s_key};
144}
145
146/*!
147 * Retrieves the names of the profiler services provided by default. The profiler services enable a
148 * debug client to use a profiler and track the time taken by various QML and JavaScript constructs,
149 * as well as the QtQuick SceneGraph.
150 * \return List of plugin keys of default profiler services.
151 */
152QStringList QQmlDebuggingEnabler::profilerServices()
153{
154 return {QQmlProfilerService::s_key, QQmlEngineControlService::s_key, QDebugMessageService::s_key};
155}
156
157/*!
158 * Retrieves the plugin keys of the debug services designed to be used with a native debugger. The
159 * native debugger will communicate with these services by directly reading and writing the
160 * application's memory.
161 * \return List of plugin keys of debug services designed to be used with a native debugger.
162 */
163QStringList QQmlDebuggingEnabler::nativeDebuggerServices()
164{
165 return {QQmlNativeDebugService::s_key};
166}
167
168/*!
169 * Restricts the services available from the debug connector. The connector will scan plugins in the
170 * "qmltooling" subdirectory of the default plugin path. If this function is not called before the
171 * debug connector is enabled, all services found that way will be available to any client. If this
172 * function is called, only the services with plugin keys given in \a services will be available.
173 *
174 * Use this method to disable debugger and inspector services when profiling to get better
175 * performance and more realistic profiles. The debugger service will put any JavaScript engine it
176 * connects to into interpreted mode, disabling the JIT compiler.
177 *
178 * \warning A client connected to the debugger or inspector services can read and modify the state
179 * of the application, evaluate arbitrary JavaScript in it, and thereby invoke any \c Q_INVOKABLE
180 * method or slot, and change any property reachable from QML. Other services can leak data, replace
181 * the application's QML files, inject input events, or stall the application. By default all
182 * services found are loaded. Restricting the services reduces this exposure, but whoever can reach
183 * the debug connector can still use all services that are enabled. See
184 * \l {QML debugging infrastructure} for the exposure of each transport.
185 *
186 * \sa debuggerServices(), profilerServices(), inspectorServices()
187 */
188void QQmlDebuggingEnabler::setServices(const QStringList &services)
189{
190 QQmlDebugConnector::setServices(services);
191}
192
193/*!
194 * \enum QQmlDebuggingEnabler::StartMode
195 *
196 * Defines the debug connector's start behavior. You can interrupt QML engines starting while a
197 * debug client is connecting, in order to set breakpoints in or profile startup code.
198 *
199 * \value DoNotWaitForClient Run any QML engines as usual while the debug services are connecting.
200 * \value WaitForClient If a QML engine starts while the debug services are connecting,
201 * interrupt it until they are done.
202 */
203
204/*!
205 * Enables debugging for QML engines created after calling this function. The debug connector will
206 * listen on \a port at \a hostName and block the QML engine until it receives a connection if
207 * \a mode is \c WaitForClient. If \a mode is not specified it won't block and if \a hostName is not
208 * specified it will listen on all available interfaces. You can only start one debug connector at a
209 * time. A debug connector may have already been started if the -qmljsdebugger= command line
210 * argument was given. This method returns \c true if a new debug connector was successfully
211 * started, or \c false otherwise.
212 *
213 * \warning Any host that can reach \a port on the interface given by \a hostName can connect and
214 * use all enabled services, including evaluating JavaScript in the application. Listening on all
215 * interfaces, which is the default and also the fallback if \a hostName is not a valid IP address,
216 * exposes the application to the whole network. Pass a loopback address such as \c 127.0.0.1 if the
217 * debugger runs on the same machine, and see \l {QML debugging infrastructure}.
218 */
219bool QQmlDebuggingEnabler::startTcpDebugServer(int port, StartMode mode, const QString &hostName)
220{
221 QVariantHash configuration;
222 configuration[QLatin1String("portFrom")] = configuration[QLatin1String("portTo")] = port;
223 configuration[QLatin1String("block")] = (mode == WaitForClient);
224 configuration[QLatin1String("hostAddress")] = hostName;
225 return startDebugConnector(QLatin1String("QQmlDebugServer"), configuration);
226}
227
228/*!
229 * \since 5.6
230 *
231 * Enables debugging for QML engines created after calling this function. The debug connector will
232 * connect to a debugger waiting on a local socket at the given \a socketFileName and block the QML
233 * engine until the connection is established if \a mode is \c WaitForClient. If \a mode is not
234 * specified it will not block. You can only start one debug connector at a time. A debug connector
235 * may have already been started if the -qmljsdebugger= command line argument was given. This method
236 * returns \c true if a new debug connector was successfully started, or \c false otherwise.
237 *
238 * \warning The application connects out to whatever is listening on \a socketFileName and grants
239 * it control over all enabled services. Whoever can create or replace that socket, which depends
240 * on the permissions of the socket's directory, can take over the application. Use a location
241 * only the debugging user can write to. See \l {QML debugging infrastructure}.
242 */
243bool QQmlDebuggingEnabler::connectToLocalDebugger(const QString &socketFileName, StartMode mode)
244{
245 QVariantHash configuration;
246 configuration[QLatin1String("fileName")] = socketFileName;
247 configuration[QLatin1String("block")] = (mode == WaitForClient);
248 return startDebugConnector(QLatin1String("QQmlDebugServer"), configuration);
249}
250
251/*!
252 * \since 5.7
253 *
254 * Enables debugging for QML engines created after calling this function. A debug connector plugin
255 * specified by \a pluginName will be loaded and started using the given \a configuration. Supported
256 * configuration entries and their semantics depend on the plugin being loaded. You can only start
257 * one debug connector at a time. A debug connector may have already been started if the
258 * -qmljsdebugger= command line argument was given. This method returns \c true if a new debug
259 * connector was successfully started, or \c false otherwise.
260 *
261 * \warning Whether and how clients are authenticated, and who can reach the connector, is up to
262 * the plugin. Do not assume that a connector plugin restricts access. The common ones don't. See
263 * \l {QML debugging infrastructure}.
264 */
265bool QQmlDebuggingEnabler::startDebugConnector(const QString &pluginName,
266 const QVariantHash &configuration)
267{
268 QQmlDebugConnector::setPluginKey(pluginName);
269 QQmlDebugConnector *connector = QQmlDebugConnector::instance();
270 return connector ? connector->open(configuration) : false;
271}
272
273enum { HookCount = 4 };
274
275// Only add to the end, and bump version if you do.
276quintptr Q_QML_EXPORT qtDeclarativeHookData[] = {
277 // Version of this Array. Bump if you add to end.
278 2,
279
280 // Number of entries in this array.
281 HookCount,
282
283 // TypeInformationVersion, an integral value, bumped whenever private
284 // object sizes or member offsets that are used in Qt Creator's
285 // data structure "pretty printing" change.
286 4,
287
288 // Version of the cache data.
289 QV4_DATA_STRUCTURE_VERSION
290};
291
292Q_STATIC_ASSERT(HookCount == sizeof(qtDeclarativeHookData) / sizeof(qtDeclarativeHookData[0]));
293
294QT_END_NAMESPACE
@ HookCount
#define Q_ATOMIC_FLAG_INIT
Definition qqmldebug.cpp:28
static Q_CONSTINIT std::atomic_flag s_printedWarning
Definition qqmldebug.cpp:46
QT_REQUIRE_CONFIG(qml_debug)
QDebug Q_QML_EXPORT operator<<(QDebug debug, const QQmlError &error)
Q_STATIC_ASSERT(sizeof(SharedImageHeader) % 4==0)