GDB Printers Use string instead of char array

Ralf Habacker ralf.habacker at freenet.de
Thu Jul 29 12:22:08 BST 2021


Am 29.07.21 um 12:49 schrieb Ralf Habacker via Kde-finance-apps:
> Am 28.07.21 um 15:48 schrieb Da Viper via Kde-finance-apps:
>> i am trying to return a string as one of the children in the pretty printer
>>
Compiling a test case for QStringRef having


   QString a("test)
   QStringRef aRef(&a);


with RelWidhDebInfo cmake build type on running gdb 10.1 with your qt
printers enabled I get the following error:

Traceback (most recent call last):
  File "/usr/share/gdb/python/gdb/printing.py", line 218, in __call__
    return printer.gen_printer(val)
  File "/home/ralf.habacker/src/gdb_printers/printers/qtprinters.py",
line 301, in __init__
    self.index = int(val['m_position'])
gdb.error: value has been optimized out


According to
https://github.com/qt/qtbase/blob/5.15/src/corelib/text/qstring.h#L1602
QStringRef is defined as

class Q_CORE_EXPORT QStringRef {
    const QString *m_string;
    int m_position;
    int m_size;


m_position is be accessable by using

(gdb) p ((int*)&aRef)[2]
$3 = 0

when the target has been compiled for x86_64 architecture and by

(gdb) p ((int*)&aRef)[1]
$3 = 0

with i586 architecture.


Accessing the referenced string can be done with

(gdb) p *(QString**)&aRef
$3 = (QString *) 0x7fffffffd6e8

and the content returned by

(gdb) p **(QString**)&bRef
$4 = "test"

Regards
Ralf


More information about the Kde-finance-apps mailing list