/usr/share/qt5/doc/qtdoc/debug.html is in qt5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- debug.qdoc -->
<title>Debugging Techniques | Qt 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td ><a href="index.html">Qt 5.9</a></td><td >Debugging Techniques</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#configuring-qt-for-debugging">Configuring Qt for Debugging</a></li>
<li class="level2"><a href="#debugging-in-macos-and-xcode">Debugging in macOS and Xcode</a></li>
<li class="level1"><a href="#command-line-options-recognized-by-qt">Command Line Options Recognized by Qt</a></li>
<li class="level1"><a href="#environment-variables-recognized-by-qt">Environment Variables Recognized by Qt</a></li>
<li class="level1"><a href="#warning-and-debugging-messages">Warning and Debugging Messages</a></li>
<li class="level1"><a href="#providing-support-for-the-qdebug-stream-operator">Providing Support for the qDebug() Stream Operator</a></li>
<li class="level1"><a href="#debugging-macros">Debugging Macros</a></li>
<li class="level1"><a href="#common-bugs">Common Bugs</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Debugging Techniques</h1>
<span class="subtitle"></span>
<!-- $$$debug.html-description -->
<div class="descr"> <a name="details"></a>
<p>Here we present some useful hints to help you with debugging your Qt-based software.</p>
<a name="configuring-qt-for-debugging"></a>
<h2 id="configuring-qt-for-debugging">Configuring Qt for Debugging</h2>
<p>When <a href="configure-options.html">configuring</a> Qt for installation, it is possible to ensure that it is built to include debug symbols that can make it easier to track bugs in applications and libraries. However, on some platforms, building Qt in debug mode will cause applications to be larger than desirable.</p>
<a name="debugging-in-macos-and-xcode"></a>
<h3 >Debugging in macOS and Xcode</h3>
<a name="debugging-with-without-frameworks"></a>
<h4 >Debugging With/Without Frameworks</h4>
<p>The basic stuff you need to know about debug libraries and frameworks is found at developer.apple.com in: <a href="http://developer.apple.com/technotes/tn2004/tn2124.html#SECDEBUGLIB">Apple Technical Note TN2124</a>.</p>
<p>When you build Qt, frameworks are built by default, and inside the framework you will find both a release and a debug version (e.g., <a href="../qtcore/qtcore-module.html">QtCore</a> and QtCore_debug). If you pass the <code>-no-framework</code> flag when you build Qt, two dylibs are built for each Qt library (e.g., libQtCore.4.dylib and libQtCore_debug.4.dylib).</p>
<p>What happens when you link depends on whether you use frameworks or not. We don't see a compelling reason to recommend one over the other.</p>
<a name="with-frameworks"></a>
<h5 >With Frameworks:</h5>
<p>Since the release and debug libraries are inside the framework, the app is simply linked against the framework. Then when you run in the debugger, you will get either the release version or the debug version, depending on whether you set <code>DYLD_IMAGE_SUFFIX</code>. If you don't set it, you get the release version by default (i.e., non _debug). If you set <code>DYLD_IMAGE_SUFFIX=_debug</code>, you get the debug version.</p>
<a name="without-frameworks"></a>
<h5 >Without Frameworks:</h5>
<p>When you tell <i>qmake</i> to generate a Makefile with the debug config, it will link against the _debug version of the libraries and generate debug symbols for the app. Running this program in GDB will then work like running GDB on other platforms, and you will be able to trace inside Qt.</p>
<a name="command-line-options-recognized-by-qt"></a>
<h2 id="command-line-options-recognized-by-qt">Command Line Options Recognized by Qt</h2>
<p>When you run a Qt application, you can specify several command-line options that can help with debugging. These are recognized by <a href="../qtwidgets/qapplication.html">QApplication</a>.</p>
<div class="table"><table class="generic">
<thead><tr class="qt-style"><th >Option</th><th >Description</th></tr></thead>
<tr valign="top" class="odd"><td ><code>-nograb</code></td><td >The application should never grab <a href="../qtwidgets/qwidget.html#grabMouse">the mouse</a> or <a href="../qtwidgets/qwidget.html#grabKeyboard">the keyboard</a>. This option is set by default when the program is running in the <code>gdb</code> debugger under Linux.</td></tr>
<tr valign="top" class="even"><td ><code>-dograb</code></td><td >Ignore any implicit or explicit <code>-nograb</code>. <code>-dograb</code> wins over <code>-nograb</code> even when <code>-nograb</code> is last on the command line.</td></tr>
</table></div>
<a name="environment-variables-recognized-by-qt"></a>
<h2 id="environment-variables-recognized-by-qt">Environment Variables Recognized by Qt</h2>
<p>At runtime, a Qt application recognizes many environment variables, some of which can be helpful for debugging:</p>
<div class="table"><table class="generic">
<thead><tr class="qt-style"><th >Variable</th><th >Description</th></tr></thead>
<tr valign="top" class="odd"><td ><code>QT_DEBUG_PLUGINS</code></td><td >Set to a non-zero value to make Qt print out diagnostic information about the each (C++) plugin it tries to load.</td></tr>
<tr valign="top" class="even"><td ><code>QML_IMPORT_TRACE</code></td><td >Set to a non-zero value to make QML print out diagnostic information from the import loading mechanism.</td></tr>
<tr valign="top" class="odd"><td ><code>QT_HASH_SEED</code></td><td >Set to an integer value to disable <a href="../qtcore/qhash.html#qhash">QHash</a> and <a href="../qtcore/qset.html">QSet</a> using a new random ordering for each application run, which in some cases might make testing and debugging difficult.</td></tr>
</table></div>
<a name="warning-and-debugging-messages"></a>
<h2 id="warning-and-debugging-messages">Warning and Debugging Messages</h2>
<p>Qt includes global macros for writing out warning and debug text. You can use them for the following purposes:</p>
<ul>
<li><a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() is used for writing custom debug output.</li>
<li><a href="../qtcore/qtglobal.html#qInfo">qInfo</a>() is used for informational messages.</li>
<li><a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() is used to report warnings and recoverable errors in your application.</li>
<li><a href="../qtcore/qtglobal.html#qCritical">qCritical</a>() is used for writing critical error messages and reporting system errors.</li>
<li><a href="../qtcore/qtglobal.html#qFatal">qFatal</a>() is used for writing fatal error messages shortly before exiting.</li>
</ul>
<p>If you include the <QtDebug> header file, the <code>qDebug()</code> macro can also be used as an output stream. For example:</p>
<pre class="cpp">
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Widget"</span> <span class="operator"><</span><span class="operator"><</span> widget <span class="operator"><</span><span class="operator"><</span> <span class="string">"at position"</span> <span class="operator"><</span><span class="operator"><</span> widget<span class="operator">-</span><span class="operator">></span>pos();
</pre>
<p>The Qt implementation of these macros prints to the <code>stderr</code> output under Unix/X11 and macOS. With Windows, if it is a console application, the text is sent to console; otherwise, it is sent to the debugger.</p>
<p>By default, only the message is printed. You can include additional information by setting the <code>QT_MESSAGE_PATTERN</code> environment variable. For example:</p>
<pre class="cpp">
QT_MESSAGE_PATTERN<span class="operator">=</span><span class="string">"[%{type}] %{appname} (%{file}:%{line}) - %{message}"</span>
</pre>
<p>The format is documented in <a href="../qtcore/qtglobal.html#qSetMessagePattern">qSetMessagePattern</a>(). You can also install your own message handler using <a href="../qtcore/qtglobal.html#qInstallMessageHandler">qInstallMessageHandler</a>().</p>
<p>If the <code>QT_FATAL_WARNINGS</code> environment variable is set, <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() exits after printing the warning message. This makes it easy to obtain a backtrace in the debugger.</p>
<p><a href="../qtcore/qtglobal.html#qDebug">qDebug</a>(), <a href="../qtcore/qtglobal.html#qInfo">qInfo</a>(), and <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() are debugging tools. They can be compiled away by defining <code>QT_NO_DEBUG_OUTPUT</code>, <code>QT_NO_INFO_OUTPUT</code>, or <code>QT_NO_WARNING_OUTPUT</code> during compilation.</p>
<p>The debugging functions QObject::dumpObjectTree() and QObject::dumpObjectInfo() are often useful when an application looks or acts strangely. More useful if you use <a href="../qtcore/qobject.html#objectName-prop">object names</a> than not, but often useful even without names.</p>
<a name="providing-support-for-the-qdebug-stream-operator"></a>
<h2 id="providing-support-for-the-qdebug-stream-operator">Providing Support for the qDebug() Stream Operator</h2>
<p>You can implement the stream operator used by <a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() to provide debugging support for your classes. The class that implements the stream is <code>QDebug</code>. Use <code>QDebugStateSaver</code> to temporarily save the formatting options of the stream. Use <a href="../qtcore/qdebug.html#nospace">nospace()</a> and <a href="../qtcore/qtextstream.html#qtextstream-manipulators">QTextStream manipulators</a> to further customize the formatting.</p>
<p>Here is an example for a class that represents a 2D coordinate.</p>
<pre class="cpp">
<span class="type"><a href="../qtcore/qdebug.html">QDebug</a></span> <span class="keyword">operator</span><span class="operator"><</span><span class="operator"><</span>(<span class="type"><a href="../qtcore/qdebug.html">QDebug</a></span> dbg<span class="operator">,</span> <span class="keyword">const</span> Coordinate <span class="operator">&</span>c)
{
<span class="type"><a href="../qtcore/qdebugstatesaver.html">QDebugStateSaver</a></span> saver(dbg);
dbg<span class="operator">.</span>nospace() <span class="operator"><</span><span class="operator"><</span> <span class="string">"("</span> <span class="operator"><</span><span class="operator"><</span> c<span class="operator">.</span>x() <span class="operator"><</span><span class="operator"><</span> <span class="string">", "</span> <span class="operator"><</span><span class="operator"><</span> c<span class="operator">.</span>y() <span class="operator"><</span><span class="operator"><</span> <span class="string">")"</span>;
<span class="keyword">return</span> dbg;
}
</pre>
<p>Integration of custom types with Qt's meta-object system is covered in more depth in the <a href="../qtcore/custom-types.html">Creating Custom Qt Types</a> document.</p>
<a name="debugging-macros"></a>
<h2 id="debugging-macros">Debugging Macros</h2>
<p>The header file <code><QtGlobal></code> contains some debugging macros and <code>#define</code>s.</p>
<p>Three important macros are:</p>
<ul>
<li><a href="../qtcore/qtglobal.html#Q_ASSERT">Q_ASSERT</a>(cond), where <code>cond</code> is a boolean expression, writes the warning "ASSERT: '<i>cond</i>' in file xyz.cpp, line 234" and exits if <code>cond</code> is false.</li>
<li><a href="../qtcore/qtglobal.html#Q_ASSERT_X">Q_ASSERT_X</a>(cond, where, what), where <code>cond</code> is a boolean expression, <code>where</code> a location, and <code>what</code> a message, writes the warning: "ASSERT failure in <code>where</code>: '<code>what</code>', file xyz.cpp, line 234" and exits if <code>cond</code> is false.</li>
<li><a href="../qtcore/qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>(ptr), where <code>ptr</code> is a pointer. Writes the warning "In file xyz.cpp, line 234: Out of memory" and exits if <code>ptr</code> is 0.</li>
</ul>
<p>These macros are useful for detecting program errors, e.g. like this:</p>
<pre class="cpp">
<span class="type">char</span> <span class="operator">*</span>alloc(<span class="type">int</span> size)
{
Q_ASSERT(size <span class="operator">></span> <span class="number">0</span>);
<span class="type">char</span> <span class="operator">*</span>ptr <span class="operator">=</span> <span class="keyword">new</span> <span class="type">char</span><span class="operator">[</span>size<span class="operator">]</span>;
Q_CHECK_PTR(ptr);
<span class="keyword">return</span> ptr;
}
</pre>
<p><a href="../qtcore/qtglobal.html#Q_ASSERT">Q_ASSERT</a>(), <a href="../qtcore/qtglobal.html#Q_ASSERT_X">Q_ASSERT_X</a>(), and <a href="../qtcore/qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>() expand to nothing if <code>QT_NO_DEBUG</code> is defined during compilation. For this reason, the arguments to these macro should not have any side-effects. Here is an incorrect usage of <a href="../qtcore/qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>():</p>
<pre class="cpp">
<span class="type">char</span> <span class="operator">*</span>alloc(<span class="type">int</span> size)
{
<span class="type">char</span> <span class="operator">*</span>ptr;
Q_CHECK_PTR(ptr <span class="operator">=</span> <span class="keyword">new</span> <span class="type">char</span><span class="operator">[</span>size<span class="operator">]</span>); <span class="comment">// WRONG</span>
<span class="keyword">return</span> ptr;
}
</pre>
<p>If this code is compiled with <code>QT_NO_DEBUG</code> defined, the code in the <a href="../qtcore/qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>() expression is not executed and <i>alloc</i> returns an uninitialized pointer.</p>
<p>The Qt library contains hundreds of internal checks that will print warning messages when a programming error is detected. We therefore recommend that you use a debug version of Qt when developing Qt-based software.</p>
<a name="common-bugs"></a>
<h2 id="common-bugs">Common Bugs</h2>
<p>There is one bug that is so common that it deserves mention here: If you include the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro in a class declaration and run <a href="moc.html">the meta-object compiler</a> (<code>moc</code>), but forget to link the <code>moc</code>-generated object code into your executable, you will get very confusing error messages. Any link error complaining about a lack of <code>vtbl</code>, <code>_vtbl</code>, <code>__vtbl</code> or similar is likely to be a result of this problem.</p>
</div>
<!-- @@@debug.html -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|