Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QtCreator debugger does not display values of std::string

I tried to debug my small little lexer and ran into this problem: the QtCreator-Debugger does not display any content of my std::string-variable. I tried to debug it in console and I got the same result, just plain structure information.

The version of QtCreator I used a few days ago did display the content of the strings. All other STL-Elements like std::vector, std::map, std::multimap, etc. display the correct data, it's just the std::string class which does not do the right.

After several hours of googling I found a lot of web pages which describe the creation of pretty printers, my really noobish approaches to fix this didn't help. Any ideas how I can get rid of this bug?

Note: The 'content' of the string-variables will always be displayed as 'not accessible'. I use QtCreator 2.6 (QT5) for 64bit Linux OS.

Edit(1): I reinstalled everything from OS to Compilers and IDE... The strange is, when I build my project with optimization level 3 (-O3) QT can display std::strings.

The command line is the following: clang++ -std=c++11 -O3 -g -c foo.cpp

When I remove -O3 std::strings are < not accessible >. Any ideas?

like image 582
bash0r Avatar asked Dec 21 '12 09:12

bash0r


3 Answers

You can try to fix "/usr/share/qtcreator/debugger/stdtypes.py". As they use "hard-code assumption on member position" it seems like not working everywhere. In my case - Linux x64, gcc 9.1 it works exactly like you described: string not accessible

So find function def qdumpHelper_std__string(d, value, charType, format):

And change (size, alloc, refcount) = d.split("ppp", data - 3 * d.ptrSize()) to (size, alloc, refcount) = d.split("ppp", value.address() + d.ptrSize())

Also comment d.check(0 <= size and size <= alloc and alloc <= 100*1000*1000) or change it to something like

if size > 1000:
   size = 1000

On my system std::string has next structure

pointer 8 byte
size    8 byte
union   16 byte

And this union field can change its meaning depends on string size. So we need to comment that size < alloc check. value.address() - the address of the string object, so value.address() + d.ptrSize()will point to size, and value.address() + 2 * d.ptrSize() point to that union, which contain alloc size value, from time to time.

Just look at your std::string class declaration so you will get structure on your system. And after the fix: fixed debuger view

Works both - when "system GDB pretty printers" checked and clear

like image 78
AstoBasto Avatar answered Nov 15 '22 20:11

AstoBasto


Try this, it worked for me.
In Qt Creator menu bar:

Tools -> Options -> Debugger
Uncheck the option (Load system GDB pretty printers)
like image 42
Kahn Avatar answered Nov 15 '22 20:11

Kahn


To clarify and sum up AstoBasto post: In file: /usr/share/qtcreator/debugger/stdtypes.py replace this function:

def qdumpHelper_std__string(d, value, charType, format):
[...]

With this:

def qdumpHelper_std__string(d, value, charType, format):
if d.isQnxTarget():
    qdumpHelper__std__string__QNX(d, value, charType, format)
    return
if d.isMsvcTarget():
    qdumpHelper__std__string__MSVC(d, value, charType, format)
    return

data = value.extractPointer()
# We can't lookup the std::string::_Rep type without crashing LLDB,
# so hard-code assumption on member position
# struct { size_type _M_length, size_type _M_capacity, int _M_refcount; }

(size, alloc, refcount) = d.split("ppp", value.address() + d.ptrSize())
refcount = refcount & 0xffffffff
d.check(refcount >= -1) # Can be -1 according to docs.

if size > 4002:
    size = 4002
d.putCharArrayHelper(data, size, charType, format)

And this works (at least on Kubuntu 19.10).

like image 45
Szyk Cech Avatar answered Nov 15 '22 21:11

Szyk Cech