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
debugging.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick-debugging.html
6\ingroup qtquick-tools
7\title Debugging QML Applications
8\brief Provides information on how to use QML's debugging tools.
9
10When you develop an application with QML, there are many ways to debug possible
11issues that you may face. The sections below describe the debugging tools
12available and how to use them.
13
14\section1 Console API
15
16\table
17 \header
18 \li Feature
19 \li Description
20 \row
21 \keyword console log
22 \li Log
23 \li Use \c{console.log}, \c{console.debug}, \c{console.info},
24 \c{console.warn}, or \c{console.error} to print debugging
25 information to the console.
26
27 For example:
28
29 \code
30 function f(a, b) {
31 console.log("a is ", a, "b is ", b);
32 }
33 \endcode
34
35 The output is generated using the qCDebug, qCWarning, or qCCritical
36 methods in C++, with a category of \c qml or \c js, depending on the
37 type of file doing the logging.
38
39 See also \l {Debugging Techniques}.
40 \row
41 \keyword console assert
42 \li Assert
43 \li \c console.assert tests that an expression is true. If not, it
44 writes an optional message to the console and prints the stack trace.
45
46 For example:
47
48 \code
49 function f() {
50 var x = 12
51 console.assert(x == 12, "This will pass");
52 console.assert(x > 12, "This will fail");
53 }
54 \endcode
55 \row
56 \keyword console time
57 \keyword console timeEnd
58 \li Timer
59 \li \c{console.time} and \c{console.timeEnd} log the time (in
60 milliseconds) that was spent between the calls. Both take a string
61 argument that identifies the measurement.
62
63 For example:
64
65 \code
66 function f() {
67 console.time("wholeFunction");
68 console.time("firstPart");
69 // first part
70 console.timeEnd("firstPart");
71 // second part
72 console.timeEnd("wholeFunction");
73 }
74 \endcode
75 \row
76 \keyword console trace
77 \li Trace
78 \li \c console.trace prints the stack trace of the JavaScript execution
79 at the point where it was called. This stack trace information
80 contains the function name, file name, line number, and column
81 number. The stack trace is limited to last 10 stack frames.
82 \row
83 \keyword console count
84 \li Count
85 \li \c console.count prints the current number of times a particular
86 piece of code has run, along with a message.
87
88 For example:
89
90 \code
91 function f() {
92 console.count("f called");
93 }
94 \endcode
95
96 The code sample above prints \c{f called: 1}, \c{f called: 2} ...
97 whenever \c{f()} is run.
98 \row
99 \keyword console profile
100 \li Profile
101 \li \c console.profile turns on the QML and JavaScript profilers. Nested
102 calls are not supported and prints a warning to the console.
103 \row
104 \keyword console profileEnd
105 \li ProfileEnd
106 \li \c console.profileEnd turns off the QML and JavaScript profilers.
107 Calling this function without a previous call to \c console.profile
108 prints a warning to the console. A profiling client needs to be
109 attached before this call to receive and store the profiling data.
110
111 For example:
112
113 \code
114 function f() {
115 console.profile();
116 //Call some function that needs to be profiled.
117 //Ensure that a client is attached before ending
118 //the profiling session.
119 console.profileEnd();
120 }
121 \endcode
122
123 \row
124 \keyword console exception
125 \li Exception
126 \li \c console.exception prints an error message. It works like
127 \c console.error but requires at least one argument and
128 additionally prints a stack trace of the JavaScript execution at
129 the point where it is called.
130\endtable
131
132Alternatively, a \l {QLoggingCategory}{logging category} can be passed as the
133first argument to any of these \c {console} functions. See
134\l [QML] LoggingCategory for more details.
135
136\section1 Debugging module imports
137
138Set the \c QML_IMPORT_TRACE environment variable to enable debug output from
139QML's import loading mechanisms.
140
141For example, for a simple QML file like this:
142
143\qml
144import QtQuick
145
146Rectangle { width: 100; height: 100 }
147\endqml
148
149If you set \c {QML_IMPORT_TRACE=1} before running the
150\l{qml_runtime_tool}{QML Runtime Tool} or your QML C++ application, you will see
151output similar to:
152
153\code
154QQmlImportDatabase::addImportPath "/qt-sdk/imports"
155QQmlImportDatabase::addImportPath "/qt-sdk/bin/QMLViewer.app/Contents/MacOS"
156QQmlImportDatabase::addToImport 0x106237370 "." -1.-1 File as ""
157QQmlImportDatabase::addToImport 0x106237370 "Qt" 4.7 Library as ""
158QQmlImportDatabase::resolveType "Rectangle" = "QDeclarativeRectangle"
159\endcode
160
161\section1 QML debugging infrastructure
162\keyword declarative_debug
163\keyword qml debug
164
165The \l{Qt Qml} module provides services for debugging, inspecting, and
166profiling applications via a TCP port or a local socket.
167
168\note The \c qmltooling plugins that are required for debugging and profiling
169QML applications on devices are automatically installed during Qt installation.
170They must be deployed to the devices for debugging and profiling to work.
171
172\section2 Enabling the infrastructure
173
174When you compile your application, you must explicitly enable the debugging
175infrastructure.
176
177If you use CMake, you can pass \c{-DCMAKE_CXX_FLAGS_INIT=-DQT_QML_DEBUG} as an
178argument to \c cmake on the command line to enable it only for a specific build. You can also permanently enable
179it in your CMakeLists.txt file by adding the following snippet:
180
181\code [cmake]
182 qt_add_executable(MyApp
183 ...
184 )
185
186 target_compile_definitions(MyApp PRIVATE QT_QML_DEBUG)
187\endcode
188
189If you use qmake, you can add the \c {CONFIG+=qml_debug}
190configuration parameters to the project \c{.pro} file or pass it as argument on the command line.
191
192If you use another build system, you need to pass the \c {QT_QML_DEBUG} define
193to the compiler via the build system's means.
194
195\section2 Security considerations
196\keyword qml debugging security
197
198Enabling the debugging infrastructure may compromise the integrity of
199your application and system, and therefore, you should only enable it in a
200controlled environment. When the infrastructure is enabled, the application
201usually displays the following warning:
202
203\c {QML debugging is enabled. Only use this in a safe environment.}
204
205The warning can be suppressed, though.
206
207\warning Never enable QML debugging in production builds or in applications that
208run in an untrusted environment. This includes enabling it at run time via
209\l{QQmlDebuggingEnabler::enableDebugging()}, \c{QT_ENABLE_QML_DEBUG},
210\c{QT_QML_DEBUG}, \c{QT_QML_DEBUG_NO_WARNING}, or \c{CONFIG+=qml_debug}.
211
212Any peer that can reach an active debug connector gets control of the
213application, with the privileges of the application's process. All services that
214were found are loaded unless you restrict them. The following services are
215shipped with Qt. The infrastructure is extensible and further services can be
216added by deploying them in the \c qmltooling plugin directory of the Qt
217installation.
218
219\table
220 \header
221 \li Service (plugin key)
222 \li What a client can do
223 \row
224 \li \c{QmlDebugger}
225 \li Walk the object tree, read and write properties, replace bindings and
226 method bodies, and evaluate arbitrary JavaScript expressions in the
227 context of any object. This can call any \c Q_INVOKABLE method or slot.
228 \row
229 \li \c{V8Debugger}
230 \li Set breakpoints, pause and step the JavaScript engine, inspect variables,
231 and evaluate expressions.
232 \row
233 \li \c{QmlInspector}
234 \li Select, inspect and modify items, and change the application's UI at
235 will. This is a way to manipulate what the user sees and does.
236 \row
237 \li \c{QmlPreview}
238 \li Upload QML and other files and make the application load them instead of
239 its own. This replaces the application's code and resources with the
240 client's, so it is code execution.
241 \row
242 \li \c{DebugTranslation}
243 \li Replace translations in the running application. This changes what the
244 user sees in the respective text fields.
245 \row
246 \li \c{EventReplay}
247 \li Inject synthetic mouse and key events into the application's windows,
248 as if the user had operated it.
249 \row
250 \li \c{DebugMessages}
251 \li Receive all log output, which can contain sensitive data, including
252 from other libraries.
253 \row
254 \li \c{CanvasFrameRate}
255 \li Receive detailed profiling data, which discloses file names, source code
256 locations, and what the application is doing.
257 \row
258 \li \c{EngineControl}
259 \li Hold QML engines back while they start or stop. This can be used to
260 stall the application.
261 \row
262 \li \c{NativeQmlDebugger}
263 \li Provides state that is designed to be read and written by a native
264 debugger directly in the application's memory. Anyone who can attach such
265 a debugger to the process can already manipulate the application, though.
266\endtable
267
268The exposure depends on the transport. The following connector plugins are
269shipped with Qt. The infrastructure is extensible and further ones can be added
270by deploying them to Qt's \c qmltooling plugin directory.
271
272\table
273 \header
274 \li Transport
275 \li Exposure
276 \row
277 \li TCP (\c{port:}, QQmlDebuggingEnabler::startTcpDebugServer())
278 \li The application listens for connections. Unless you give a \c{host:}
279 address, it listens on all network interfaces. If the address given
280 with \c{host:} cannot be parsed, it also listens on all network
281 interfaces. Any machine that can reach the port can connect. If you
282 must use the TCP transport, prefer a loopback address, and tunnel
283 the connection (for example with SSH port forwarding) when debugging
284 remote devices.
285 \row
286 \li Local socket (\c{file:}, QQmlDebuggingEnabler::connectToLocalDebugger())
287 \li The application connects to a socket that a debugger has created.
288 Only users who can create or replace that socket can take control of
289 the application. Make sure the socket's directory is writable only by
290 the user running the debugger.
291 \row
292 \li Native debugger (\c{native})
293 \li The application expects a native debugger to be already attached and
294 uses it as transport. A native debugger can already manipulate the
295 application at will.
296 \row
297 \li Connector plugins (\c{connector:}, QQmlDebuggingEnabler::startDebugConnector())
298 \li The plugin determines who can connect and whether any authentication
299 takes place. This is the extension mechanism.
300\endtable
301
302Use QQmlDebuggingEnabler::setServices() or the \c services: argument to enable
303only the services you need. For example, profiling does not need the debugger and
304inspector services. By default, all services found in the \c qmltooling plugins
305are loaded. Restricting the services reduces what a client can do, but it does
306not protect against untrusted clients: all enabled services are available to
307every client that connects.
308
309\section2 Disabling QML debugging for release builds
310
311Several independent layers control whether an application can be debugged.
312Only the first two remove the capability:
313
314\list 1
315 \li The \c qml-debug Qt feature, which is on by default. Configure Qt with
316 \c{-no-qml-debug} to build without the debugging infrastructure.
317 \li The \c qmltooling plugins, which are installed with Qt. Deployment tools may
318 deploy them with your application. Do not ship them in release builds. Without
319 them, no connector or service can be loaded.
320 \li Enabling the infrastructure. \c QT_QML_DEBUG, \c QT_QML_DEBUG_NO_WARNING,
321 \c{CONFIG+=qml_debug} and \c QT_ENABLE_QML_DEBUG merely call
322 \l{QQmlDebuggingEnabler::enableDebugging()} from a static initializer. Any code
323 in the application, including third-party libraries, can call it as well. An
324 application built without the define accepts \c -qmljsdebugger exactly like
325 a debug build if this happens.
326 \li Starting a connector, via \c -qmljsdebugger, or
327 QQmlDebuggingEnabler::startTcpDebugServer(),
328 QQmlDebuggingEnabler::connectToLocalDebugger() or
329 QQmlDebuggingEnabler::startDebugConnector().
330\endlist
331
332Enabling debugging prints a warning, unless this is suppressed by passing
333\c false to QQmlDebuggingEnabler::enableDebugging() or by defining
334\c QT_QML_DEBUG_NO_WARNING. This warning is the only notice given that debugging is
335enabled.
336
337\section2 Starting applications
338
339To enable debugging -- from the start or to attach a debugger later on -- start
340the application with the following arguments:
341
342\c {-qmljsdebugger=port:<port_from>[,port_to][,host:<ip address>][,block][,file:<local socket>][,services:<comma-separated list of services to enable>]}
343
344Where:
345\list
346 \li the mandatory \c {port_from} specifies either the debugging port or the
347 start port of a range of ports when \c {port_to} is specified
348 \li the optional \c {ip address} specifies the IP address of the host where
349 the application is running
350 \li the optional \c block prevents the application from running until the
351 debug client connects to the server
352 \li the optional \c {file} specifies the local socket.
353 \li the optional \c {services} specifies the services to enable; the default
354 is all that are found. Note that the \c{v4 debug} service disables the
355 JIT.
356\endlist
357
358After the application has successfully started, it displays the following
359message:
360
361\c {QML Debugger: Waiting for connection on port <port_number>}
362
363or
364
365\c {QML Debugger: Connecting to socket at <file>}
366
367\section2 Connecting to applications
368
369When the application is running, an IDE or a tool that implements the binary
370protocol can connect to the open port.
371
372\section1 Debugging with \QC
373
374\QC uses the debugging infrastructure to debug, inspect, and profile QML
375applications on the desktop as well as on remote devices. \QC provides
376integrated clients for debugging JavaScript, inspecting the object tree, and
377profiling the activities of a QML engine. For more information, see
378\l{\QC: Debugging Qt Quick Projects}.
379
380\section1 Debugging with \QVSC
381
382\QVSC provides integrated clients for debugging JavaScript and
383profiling QML engine activity. For more information, see
384\l{\QVSC: Debug Qt Quick applications}.
385
386*/