IDE Debug Helpers: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 16: Line 16:


There is a new way to visualize native type, see
There is a new way to visualize native type, see
http://code.msdn.microsoft.com/Writing-type-visualizers-2eae77a2 and http://blogs.msdn.com/b/vcblog/archive/2013/06/28/using-visual-studio-2013-to-write-maintainable-native-visualizations-natvis.aspx for details.
[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.


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.
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.


So we can visualize QString and some other types using [https://code.qt.io/cgit/qt-labs/vstools.git/tree/src/qtvstools/qt5.natvis.xml qt5.natvis] file
So we can visualize QString and some other types using [https://code.qt.io/cgit/qt-labs/vstools.git/tree/src/qtvstools/qt5.natvis.xml qt5.natvis] file
(save to file: %USERPROFILE%\Documents\Visual Studio <version>\Visualizers\qt5.natvis)
(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)
<pre>
<pre>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>

Revision as of 19:02, 28 October 2020


Qt Creator

Qt Creator directly supports introspection of all Qt Containers and QObject derived classes for Qt 4 and Qt 5. 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 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)

<?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.