Teléfono y Whatsapp: 675 18 68 80

Depuración de programas en GNU/Linux con Valgrind

Ando estos días trasteando bastante con C++ QML y QT5 porque estoy desarrollando un programa que interactúa con las conexiones de red. La cuestión es que este programa compilaba bien, pero al ejecutarlo me devolvía error de malloc() o simplemente terminaba su ejecución sin dar más aviso.

Aunque QTCreator (el IDE con el que desarrollo) tiene un debugger, valgrind es bastante más potente.

instalar valngrind

sudo apt-get install valgrind

debug con valgrind

valgrind --tool=memcheck --leak-check=yes ./programa-a-ejecutar

lo cual nos devolverá una traza que podremos debuguear facilmente:

==4856== Memcheck, a memory error detector
==4856== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==4856== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==4856== Command: ./programa-a-ejecutar
==4856== 
==4856== Invalid write of size 4
==4856==    at 0x51B4483: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856==  Address 0xaa1a3e8 is 8 bytes after a block of size 0 alloc'd
==4856==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4856==    by 0x5857068: QHashData::allocateNode(int) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==4856==    by 0x51B4479: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856== 
==4856== Invalid write of size 8
==4856==    at 0x51B4486: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856==  Address 0xaa1a3e0 is 0 bytes after a block of size 0 alloc'd
==4856==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4856==    by 0x5857068: QHashData::allocateNode(int) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==4856==    by 0x51B4479: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856== 
==4856== Invalid write of size 8
==4856==    at 0x51B4490: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856==  Address 0xaa1a3f0 is 16 bytes after a block of size 0 alloc'd
==4856==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4856==    by 0x5857068: QHashData::allocateNode(int) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==4856==    by 0x51B4479: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856== 
==4856== Invalid write of size 8
==4856==    at 0x51B44AB: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856==  Address 0xaa1a3f8 is not stack'd, malloc'd or (recently) free'd
==4856== 
==4856== Invalid write of size 4
==4856==    at 0x51B44C3: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
==4856==  Address 0xaa1a400 is not stack'd, malloc'd or (recently) free'd
==4856== 
 
valgrind: m_mallocfree.c:294 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 1, hi = 178365056.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata.  If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away.  Please try that before reporting this as a bug.
 
==4856==    at 0x380581EF: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==4856==    by 0x38058332: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==4856==    by 0x3806422C: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==4856==    by 0x3802B33C: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==4856==    by 0x3802B4C2: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==4856==    by 0x3809D58D: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
==4856==    by 0x380AC14C: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)
 
sched status:
  running_tid=1
 
Thread 1: status = VgTs_Runnable
==4856==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4856==    by 0x5803A03: QArrayData::allocate(unsigned long, unsigned long, unsigned long, QFlags<QArrayData::AllocationOption>) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==4856==    by 0x580553F: QByteArray::QByteArray(char const*, int) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==4856==    by 0x51B41EF: ??? (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7A97: QDBusConnection::connectToBus(QDBusConnection::BusType, QString const&) (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x51A7DB7: QDBusConnection::systemBus() (in /home/walo/Qt/5.2.1/gcc_64/lib/libQt5DBus.so.5.2.1)
==4856==    by 0x4E9CB56: NetworkManager::NetworkManagerPrivate::NetworkManagerPrivate() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9CD1B: ??? (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x4E9E848: NetworkManager::networkInterfaces() (in /usr/lib/libNetworkManagerQt.so.0.9.0.1)
==4856==    by 0x404064: main (main.cpp:28)
 
 
Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.
 
If that doesn't help, please report this bug to: www.valgrind.org
 
In the bug report, send all the above text, the valgrind
version, and what OS and version you are using.  Thanks.

y con esto ya tendremos más información a cerca de lo que está sucediendo.