IDE Debug Helpers: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Update paths to natvis file)
 
(19 intermediate revisions by 10 users not shown)
Line 1: Line 1:
[[Category:Tools::QtCreator]]<br />[[Category:Developing_Qt]]
[[Category:Tools::QtCreator]]
 
[[Category:Developing_Qt]]
= IDE debug helpers =


== Qt Creator ==
== Qt Creator ==


Qt Creator directly supports introspection of all Qt Containers and QObject derived classes for Qt 4 and Qt 5.<br />User defined types can be supported in addition, see [http://doc.qt.io/qtcreator-2.8/creator-debugging-helpers.html the Qt Creator documentation] for details.
Qt Creator directly supports introspection of all Qt Containers and QObject derived classes for Qt 4 and up
 
User defined types can be supported in addition, see [http://doc.qt.io/qtcreator/creator-debugging-helpers.html the Qt Creator documentation] for details.
== MS visual studio QString &amp; QByteArray expansions ==
 
The new layout of QString in Qt 5 is hard to inspect using the debugger.<br />The following code can be added to autoexp.dat (c:files(x86)visual studio 9.0\common7\packages\debugger\autoexp.dat)<br />You should add it to the &lt;nowiki&amp;gt;[Visualizer]&lt;/nowiki&amp;gt; section, before the STL/ATL containers.


<code><br />; Qt types<br />QStringData{<br /> preview ([(unsigned short*)$e.d + 2 + $e.offset,su])<br /> stringview ([(unsigned short*)$e.d + 2 + $e.offset,sub])<br />}<br />QString{<br /> preview ([$e.d])<br />}<br />QByteArrayData{<br /> preview ([(unsigned char*)$e.d + 4 + $e.offset,s])<br /> stringview ([(unsigned char*)$e.d + 4 + $e.offset,sb])<br />}<br />QByteArray{<br /> preview ([$e.d])<br />}<br /></code>
== LLDB ==


Unfortunately, sizeof() cannot be used. That is why there is a constant 2 or 4 for the offset pointer.<br />For an x64 build, the sizes most likely need to be doubled.
There is an effort to introspect Qt types using LLDB at https://bitbucket.org/lukeworth/lldb-qt-formatters.


== MS Visual Studio 2012 ==
KDevelop ships formatter scripts in its LLDB plugin for Qt types that can be used directly in plain LLDB. https://unlimitedcodeworks.xyz/blog/2016/08/20/gsoc-kdevelop-lldb-final-report/#using-data-formatter-scripts-outside-kdevelop


There is a new way to visualize native type, see<br />&quot;http://code.msdn.microsoft.com/Writing-type-visualizers-2eae77a2&amp;quot;:http://code.msdn.microsoft.com/Writing-type-visualizers-2eae77a2 for details.
== MS Visual Studio 2012 and up ==


Ready-made .natvis files are included in the &quot;Visual Studio add-in&amp;quot;:http://wiki.qt.io/QtVSAddin, and can be grabbed directly from &quot;gitorious&amp;quot;:https://qt.gitorious.org/qt-labs/vstools/source/tools/Qt4EEAddin/qt5.natvis.
There is a new way to visualize native type, see
[https://docs.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects?view=vs-2019 Create custom views of C++ objects in the debugger using the Natvis framework] and [http://blogs.msdn.com/b/vcblog/archive/2013/06/28/using-visual-studio-2013-to-write-maintainable-native-visualizations-natvis.aspx Using Visual Studio 2013 to write maintainable native visualizations (natvis)] for details.


So we can visualize QString and some other types using .natvis file<br />&amp;#40;save to file: USERPROFILEDocuments\Visual Studio 2012\Visualizers\Qt5.natvis&amp;amp;#41;<br /><code><br />&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;utf-8&amp;quot;?&amp;gt;<br />&lt;AutoVisualizer &gt;
The qt5.natvis file is also included in the [http://wiki.qt.io/QtVSAddin Visual Studio add-in] which automatically installs it in the correct directory.


&lt;Type Name=&quot;QString&amp;quot;&gt;<br /> &lt;DisplayString&amp;gt;{(char*)d + d-&amp;gt;offset,su}&lt;/DisplayString&amp;gt;<br /> &lt;/Type&amp;gt;
So we can visualize QString and some other types using [https://code.qt.io/cgit/qt-labs/vstools.git/tree/QtMSBuild/QtMsBuild/qt5.natvis.xml qt5.natvis] (for Qt5) file
(save to file: %USERPROFILE%\Documents\Visual Studio <version>\Visualizers\qt5.natvis, remove all occourences of '##NAMESPACE##::' - it's replaced by the a Qt namespace by the installer but the default Qt installation has no namespace).


&lt;Type Name=&quot;QtPrivate::RefCount&amp;quot;&gt;<br /> &lt;DisplayString&amp;gt;{atomic}&lt;/DisplayString&amp;gt;<br /> &lt;/Type&amp;gt;
For Qt6 the natvis file can be found [https://code.qt.io/cgit/qt-labs/vstools.git/tree/QtMSBuild/QtMsBuild/qt6.natvis.xml here].
<pre>
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer >


&lt;Type Name=&quot;QBasicAtomicInteger&amp;amp;amp;lt;int&amp;amp;amp;gt;&quot;&gt;<br /> &lt;DisplayString&amp;gt;{_q_value}&lt;/DisplayString&amp;gt;<br /> &lt;/Type&amp;gt;
<Type Name="QString">
    <DisplayString>{((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub}</DisplayString>
    <StringView>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),sub</StringView>
    <Expand>
        <Item Name="[size]">d-&gt;size</Item>
        <Item Name="[referenced]">d-&gt;ref.atomic._q_value</Item>
        <ArrayItems>
            <Size>d-&gt;size</Size>
            <ValuePointer>((reinterpret_cast&lt;unsigned short*&gt;(d)) + d->offset / 2),c</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>


&lt;Type Name=&quot;QTypedArrayData&amp;amp;amp;lt;'''&amp;amp;gt;&quot;&gt;<br /> &lt;DisplayString&amp;gt;{{Count = {size}}}&lt;/DisplayString&amp;gt;<br /> &lt;Expand&amp;gt;<br /> &lt;Item Name=&quot;[size]&quot;&gt;size&amp;lt;/Item&amp;gt;<br /> &lt;ArrayItems&amp;gt;<br /> &lt;Size&amp;gt;size&amp;lt;/Size&amp;gt;<br /> &lt;ValuePointer&amp;gt;(iterator) ((char''')this + offset)&lt;/ValuePointer&amp;gt;<br /> &lt;/ArrayItems&amp;gt;<br /> &lt;/Expand&amp;gt;<br /> &lt;/Type&amp;gt;
<Type Name="QByteArray">
    <DisplayString>{((reinterpret_cast&lt;char*&gt;(d)) + d-&gt;offset),sb}</DisplayString>
    <StringView>((reinterpret_cast&lt;char*&gt;(d)) + d-&gt;offset),sb</StringView>
    <Expand>
        <Item Name="[size]">d-&gt;size</Item>
        <Item Name="[referenced]">d-&gt;ref.atomic._q_value</Item>
        <ArrayItems>
            <Size>d-&gt;size</Size>
            <ValuePointer>((reinterpret_cast&lt;char*&gt;(d)) + d-&gt;offset),c</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>


&lt;Type Name=&quot;QByteArray&amp;quot;&gt;<br /> &lt;DisplayString&amp;gt;{*d}&lt;/DisplayString&amp;gt;<br /> &lt;/Type&amp;gt;
<!— More Qt5 types… —>


&amp;lt;!— More Qt5 types… —&amp;gt;
</AutoVisualizer>
</pre>


&lt;/AutoVisualizer&amp;gt;<br /></code>
== MS Visual Studio before 2012 - QString & QByteArray expansions ==


== MS Visual Studio 2013 ==
The new layout of QString in Qt 5 is hard to inspect using the debugger.
The following code can be added to autoexp.dat (c:\program files(x86)\visual studio 9.0\common7\packages\debugger\autoexp.dat)
You should add it to the <nowiki>[Visualizer]</nowiki> section, before the STL/ATL containers.


The &quot;.natvis&amp;quot; files introduced in MSVS2012 received some additional attention in MSVS2013:
<pre>
; Qt types
QStringData{
preview ([(unsigned short*)$e.d + $e.offset,su])
stringview ([(unsigned short*)$e.d + $e.offset,sub])
}
QString{
preview ([$e.d])
}
QByteArrayData{
preview ([(unsigned char*)$e.d + $e.offset,s])
stringview ([(unsigned char*)$e.d + $e.offset,sb])
}
QByteArray{
preview ([$e.d])
}
</pre>


http://blogs.msdn.com/b/vcblog/archive/2013/06/28/using-visual-studio-2013-to-write-maintainable-native-visualizations-natvis.aspx
If all else fails you can always just add a watcher for
  (char*)str.d + str.d->offset,su
in the debugger, to see the contents of str.

Latest revision as of 20:12, 30 September 2022


Qt Creator

Qt Creator directly supports introspection of all Qt Containers and QObject derived classes for Qt 4 and up User defined types can be supported in addition, see the Qt Creator documentation for details.

LLDB

There is an effort to introspect Qt types using LLDB at https://bitbucket.org/lukeworth/lldb-qt-formatters.

KDevelop ships formatter scripts in its LLDB plugin for Qt types that can be used directly in plain LLDB. https://unlimitedcodeworks.xyz/blog/2016/08/20/gsoc-kdevelop-lldb-final-report/#using-data-formatter-scripts-outside-kdevelop

MS Visual Studio 2012 and up

There is a new way to visualize native type, see Create custom views of C++ objects in the debugger using the Natvis framework and Using Visual Studio 2013 to write maintainable native visualizations (natvis) for details.

The qt5.natvis file is also included in the Visual Studio add-in which automatically installs it in the correct directory.

So we can visualize QString and some other types using qt5.natvis (for Qt5) file (save to file: %USERPROFILE%\Documents\Visual Studio <version>\Visualizers\qt5.natvis, remove all occourences of '##NAMESPACE##::' - it's replaced by the a Qt namespace by the installer but the default Qt installation has no namespace).

For Qt6 the natvis file can be found here.

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer >

<Type Name="QString">
    <DisplayString>{((reinterpret_cast<unsigned short*>(d)) + d->offset / 2),sub}</DisplayString>
    <StringView>((reinterpret_cast<unsigned short*>(d)) + d->offset / 2),sub</StringView>
    <Expand>
        <Item Name="[size]">d->size</Item>
        <Item Name="[referenced]">d->ref.atomic._q_value</Item>
        <ArrayItems>
            <Size>d->size</Size>
            <ValuePointer>((reinterpret_cast<unsigned short*>(d)) + d->offset / 2),c</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>

<Type Name="QByteArray">
    <DisplayString>{((reinterpret_cast<char*>(d)) + d->offset),sb}</DisplayString>
    <StringView>((reinterpret_cast<char*>(d)) + d->offset),sb</StringView>
    <Expand>
        <Item Name="[size]">d->size</Item>
        <Item Name="[referenced]">d->ref.atomic._q_value</Item>
        <ArrayItems>
            <Size>d->size</Size>
            <ValuePointer>((reinterpret_cast<char*>(d)) + d->offset),c</ValuePointer>
        </ArrayItems>
    </Expand>
</Type>

<!— More Qt5 types… —>

</AutoVisualizer>

MS Visual Studio before 2012 - QString & QByteArray expansions

The new layout of QString in Qt 5 is hard to inspect using the debugger. The following code can be added to autoexp.dat (c:\program files(x86)\visual studio 9.0\common7\packages\debugger\autoexp.dat) You should add it to the [Visualizer] section, before the STL/ATL containers.

; Qt types
QStringData{
 preview ([(unsigned short*)$e.d + $e.offset,su])
 stringview ([(unsigned short*)$e.d + $e.offset,sub])
}
QString{
 preview ([$e.d])
}
QByteArrayData{
 preview ([(unsigned char*)$e.d + $e.offset,s])
 stringview ([(unsigned char*)$e.d + $e.offset,sb])
}
QByteArray{
 preview ([$e.d])
}

If all else fails you can always just add a watcher for

  (char*)str.d + str.d->offset,su

in the debugger, to see the contents of str.