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 together with the stack
127 trace of JavaScript execution at the point where it is called.
128\endtable
129
130Alternatively, a \l {QLoggingCategory}{logging category} can be passed as the
131first argument to any of these \c {console} functions. See
132\l [QML] LoggingCategory for more details.
133
134\section1 Debugging module imports
135
136Set the \c QML_IMPORT_TRACE environment variable to enable debug output from
137QML's import loading mechanisms.
138
139For example, for a simple QML file like this:
140
141\qml
142import QtQuick
143
144Rectangle { width: 100; height: 100 }
145\endqml
146
147If you set \c {QML_IMPORT_TRACE=1} before running the
148\l{qml_runtime_tool}{QML Runtime Tool} or your QML C++ application, you will see
149output similar to:
150
151\code
152QQmlImportDatabase::addImportPath "/qt-sdk/imports"
153QQmlImportDatabase::addImportPath "/qt-sdk/bin/QMLViewer.app/Contents/MacOS"
154QQmlImportDatabase::addToImport 0x106237370 "." -1.-1 File as ""
155QQmlImportDatabase::addToImport 0x106237370 "Qt" 4.7 Library as ""
156QQmlImportDatabase::resolveType "Rectangle" = "QDeclarativeRectangle"
157\endcode
158
159\section1 QML debugging infrastructure
160\keyword declarative_debug
161\keyword qml debug
162
163The \l{Qt Qml} module provides services for debugging, inspecting, and
164profiling applications via a TCP port or a local socket.
165
166\note The \c qmltooling plugins that are required for debugging and profiling
167QML applications on devices are automatically installed during Qt installation.
168They must be deployed to the devices for debugging and profiling to work.
169
170\section2 Enabling the infrastructure
171
172When you compile your application, you must explicitly enable the debugging
173infrastructure. If you use qmake, you can add the \c {CONFIG+=qml_debug}
174configuration parameters to the project \c{.pro} file.
175
176If you use another build system, you can pass the \c {QT_QML_DEBUG} define
177to the compiler:
178
179\note Enabling the debugging infrastructure may compromise the integrity of
180your application and system, and therefore, you should only enable it in a
181controlled environment. When the infrastructure is enabled, the application
182displays the following warning: \br \c {QML debugging is enabled. Only use this in a safe environment.}
183
184\section2 Starting applications
185
186To enable debugging -- from the start or to attach a debugger later on -- start
187the application with the following arguments:
188
189\c {-qmljsdebugger=port:<port_from>[,port_to][,host:<ip address>][,block][,file:<local socket>][,services:<comma-separated list of services to enable>]}
190
191Where:
192\list
193 \li the mandatory \c {port_from} specifies either the debugging port or the
194 start port of a range of ports when \c {port_to} is specified
195 \li the optional \c {ip address} specifies the IP address of the host where
196 the application is running
197 \li the optional \c block prevents the application from running until the
198 debug client connects to the server
199 \li the optional \c {file} specifies the local socket.
200 \li the optional \c {services} specifies the services to enable; the default
201 is all that are found. Note that the \c{v4 debug} service disables the
202 JIT.
203\endlist
204
205After the application has successfully started, it displays the following
206message:
207
208\c {QML Debugger: Waiting for connection on port <port_number>}
209
210or
211
212\c {QML Debugger: Connecting to socket at <file>}
213
214\section2 Connecting to applications
215
216When the application is running, an IDE or a tool that implements the binary
217protocol can connect to the open port.
218
219Qt provides a \c qmlprofiler command line tool to capture profiling data in a
220file. To run this tool, enter the following command:
221
222\c {qmlprofiler -p <port> -attach <ip address>}
223
224\section1 Debugging with \QC
225
226\QC uses the debugging infrastructure to debug, inspect, and profile QML
227applications on the desktop as well as on remote devices. \QC provides
228integrated clients for debugging JavaScript, inspecting the object tree, and
229profiling the activities of a QML engine. For more information, see
230\l{\QC: Debugging Qt Quick Projects}.
231
232*/