Qt Creator Manual

Using Debugging Helpers

Qt Creator is able to show complex data types in a customized, user-extensible manner. For this purpose, it takes advantage of two technologies, collectively referred to as Debugging Helpers.

Using the debugging helpers is not essential for debugging with Qt Creator, but they enhance the user's ability to quickly examine complex data significantly.

Debugging Helpers Based on C++

This is the first and original approach to display complex data types. While it has been superseded on most platforms by the more robust and more flexible second approch using Python scripting, it is the only feasible one on Windows/MSVC, Mac OS, and old Linux distributions. Moreover, this approach will automatically be chosen as fallback in case the Python based approach fails.

During debugging with the C++ based debugging helpers, Qt Creator dynamically loads a helper library in form of a DLL or a shared object into the debugged process. The Qt SDK package already contains a prebuilt debugging helper library. To create your own debugging helper library, select Tools > Options... > Qt4 > Qt Versions. As the internal data structures of Qt can change between versions, the debugging helper library is built for each Qt version.

Debugging Helpers Based on Python

Qt Creator uses GDB builds that enable Python scripting to display information in the Locals and Expressions view. When Python scripting is used, code (Debugging helpers) does not need to be injected into the debugged process to nicely display QStringList or std::map contents, for example.

The code injection caused problems and put an extra stress on the debugged process. You can now easily extend the debugging helpers to other types. No compilation is required, just adding a few lines of Python.

Python scripting vastly reduces the communication overhead compared with the previous solution. However, there are some obstacles:

  • There is no Python-enabled GDB for Mac OS. Mac OS continues injection with C++ based debugging helpers.
  • On the Symbian platform, an on-device debugging agent restricts the communication between GDB and the device. Therefore, extracting QObject properties, for example, is not possible.
  • There is no GDB to communicate with MSVC compiled applications on Windows. So information can be displayed nicely only in a limited fashion by using a cdb extension DLL.

Extending the Python Based Debugging Helpers

On platforms featuring a Python-enabled version of the GDB debugger, the data extraction is done by a Python script. This is more robust as the script execution is separated from the debugged process. It is also easier to extend as the script is less dependent on the actual Qt version and does not need compilation.

To extend the shipped Python based debugging helpers for custom types, define one Python function per user defined type in the GDB startup file. By default, the following startup file is used: ~/.gdbinit. To use another file, select Tools > Options... > Debugger > GDB and specify a filename in the GDB startup script field.

The function name has to be qdump__NS__Foo, where NS::Foo is the class or class template to be examined. Nested namespaces are possible.

The debugger plugin calls this function whenever you want to display an object of this type. The function is passed the following parameters:

  • d of type Dumper
  • item of type Item

The function has to feed the Dumper object with certain information which is used to build up the object and its children's display in the Locals and Expressions view.

Example:

 def qdump__QVector(d, item):
     d_ptr = item.value["d"]
     p_ptr = item.value["p"]
     alloc = d_ptr["alloc"]
     size = d_ptr["size"]

     check(0 <= size and size <= alloc and alloc <= 1000 * 1000 * 1000)
     check(d_ptr["ref"]["_q_value"] > 0)

     innerType = item.value.type.template_argument(0)
     d.putItemCount(size)
     d.putNumChild(size)
     if d.isExpanded(item):
         p = gdb.Value(p_ptr["array"]).cast(innerType.pointer())
         with Children(d, [size, 2000], innerType)
             for i in d.childRange():
                 d.putSubItem(Item(p.dereference(), item.iname, i))
                 p += 1

Item Class

The Item Python class is a thin wrapper around values corresponding to one line in the Locals and Expressions view. The Item members are as follows :

  • __init__(self, value, parentiname, iname, name = None) - A constructor. The object's internal name is created by concatenating parentiname and iname. If None is passed as name, a serial number is used.
  • value - An object of type gdb.Value representing the value to be displayed.
  • iname - The internal name of the object, constituting a dot-separated list of identifiers, corresponding to the position of the object's representation in the view.
  • name - An optional name. If given, is used in the name column of the view. If not, a simple number in brackets is used instead.

Dumper Class

For each line in the Locals and Expressions view, a string like the following needs to be created and channeled to the debugger plugin.

 "{iname='some internal name',
   addr='object address in memory',
   name='contents of the name column',
   value='contents of the value column',
   type='contents of the type column',
   numchild='number of children',        // zero/nonzero is sufficient
   childtype='default type of children', // optional
   childnumchild='default number of grandchildren', // optional
   children=[              // only needed if item is expanded in view
      {iname='internal name of first child',
       ... },
      {iname='internal name of second child',
       ... },
      ...
   ]}"

While in theory, you can build up the entire string above manually, it is easier to employ the Dumper Python class for that purpose. The Dumper Python class contains a complete framework to take care of the iname and addr fields, to handle children of simple types, references, pointers, enums, known and unknown structs as well as some convenience methods to handle common situations.

The Dumper members are the following:

  • __init__(self) - Initializes the output to an empty string and empties the child stack.
  • put(self, value) - Low level method to directly append to the output string.
  • putCommaIfNeeded(self) - Appends a comma if the current output ends in '}', '"' or ']' .
  • putField(self, name, value) - Appends a comma if needed, and a name='value' field.
  • beginItem(self, name) - Starts writing a field by writing name='.
  • endItem(self) - Ends writing a field by writing '.
  • endChildren(self) - Ends writing a list of children.
  • childRange(self) - Returns the range of children specified in the current Children scope.
  • putItemCount(self, count) - Appends a field value='<%d items' to the output.
  • putEllipsis(self) - Appends fields '{name="<incomplete>",value="",type="",numchild="0"}'. This is automatically done by endChildren if the number of children to print is smaller than the number of actual children.
  • putName(self, name) - Appends a name='...' field.
  • putType(self, type) - Appends a field type='...' unless the type coincides with the parent's default child type.
  • putNumChild(self, numchild) - Appends a field numchild='...' unless the numchild coincides with the parent's default child numchild value.
  • putValue(self, value, encoding = None) - Append a file value='...', optionally followed by a field valueencoding='...'. The value needs to be convertible to a string entirely consisting of alphanumerical values. The encoding parameter can be used to specify the encoding in case the real value had to be encoded in some way to meet the alphanumerical-only requirement. Currently the following encodings are supported:
    • 0: unencoded 8 bit data, interpreted as Latin1.
    • 1: base64 encoded 8 bit data, used for QByteArray, double quotes are added.
    • 2: base64 encoded 16 bit data, used for QString, double quotes are added.
    • 3: base64 encoded 32 bit data, double quotes are added.
    • 4: base64 encoded 16 bit data, without quotes (see 2)
    • 5: base64 encoded 8 bit data, without quotes (see 1)
    • 6: %02x encoded 8 bit data (as with QByteArray::toHex), double quotes are added.
    • 7: %04x encoded 16 bit data (as with QByteArray::toHex), double quotes are added.
  • putStringValue(self, value) - Encodes a QString and calls putValue with the correct encoding setting.
  • putByteArrayValue(self, value) - Encodes a QByteArray and calls putValue with the correct encoding setting.
  • isExpanded(self, item) - Checks whether the item with the internal name item.iname is expanded in the view.
  • isExpandedIName(self, iname) - Checks whether the item with the internal name iname is expanded in the view.
  • putIntItem(self, name, value) - Equivalent to:
         self.beginHash()
         self.putName(name)
         self.putValue(value)
         self.putType("int")
         self.putNumChild(0)
         self.endHash()
  • putBoolItem(self, name, value) - Equivalent to:
         self.beginHash()
         self.putName(name)
         self.putValue(value)
         self.putType("bool")
         self.putNumChild(0)
         self.endHash()
  • pushOutput(self) - Moves the output string to a safe location from with it is sent to the debugger plugin even if further operations raise an exception.
  • putCallItem(self, name, item, func) - Uses GDB to call the function func on the value specified by item.value and output the resulting item. This function is not available when debugging core dumps and it is not available on the Symbian platform due to restrictions imposed by the on-device debugging agent.
  • putItem(self, item) - The "master function", handling basic types, references, pointers and enums directly, iterates over base classes and class members of compound types and calls qdump__* functions whenever appropriate.
  • putSubItem(self, item) - Equivalent to:
         with SubItem(self):
             self.putItem(item)

    Exceptions raised by nested function calls are caught and all output produced by putItem is replaced by the output of:

             ...
         except RuntimeError:
             d.put('value="<invalid>",type="<unknown>",numchild="0",')

Children and SubItem Class

The attempt to create child items might lead to errors if data is uninitialized or corrupted. To gracefully recover in such situations, use Children and SubItem Context Managers to create the nested items.

The Children constructor __init__(self, dumper, numChild = 1, childType = None, childNumChild = None) uses one mandatory argument and three optional arguments. The mandatory argument refers to the current Dumper object. The optional arguments can be used to specify the number numChild of children, with type childType_ and childNumChild_ grandchildren each. If numChild_ is a list of two integers, the first one specifies the actual number of children and the second the maximum number of children to print.

Similarly, using the SubItem class helps to protect individual items.

Example:

 d.putNumChild(2)
 if d.isExpanded(item):
     with Children(d):
         with SubItem(d):
             d.putName("key")
             d.putItem(Item(key, item.iname, "key"))
         with SubItem(d):
             d.putName("value")
             d.putItem(Item(value, item.iname, "value"))

Debugging Helpers for QML

The debugging helpers for QML provide you with code completion for custom modules (qmldump) and debugging Qt Quick UI projects (qmlobserver).

You have to build the QML Inspector once for each Qt version that you want to debug with. Select Tools > Options... > Qt4 > Qt Versions.

Note: QML Inspector requires Qt 4.7.1 or later.

Enabling Debugging Helpers for Qt's Bootstrapped Applications

Qt's bootstrapped applications (such as moc and qmake) are built in a way that is incompatible with the default build of the debugging helpers. To work around this, add dumper.cpp to the compiled sources in the application Makefile.

Choose Tools > Options > Debugger > Debugging Helper > Use debugging helper from custom location, and specify an invalid location, such as /dev/null.

X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.

[0]; s.parentNode.insertBefore(ga, s); })();