Skip to content

How to use valgrind to detect QEMU memory leakage

chenc52 edited this page Nov 28, 2017 · 1 revision

Building and installing it

  1. Check out the code from SVN git clone git://sourceware.org/git/valgrind.git
  2. cd into the source directory.
  3. Run ./autogen.sh to setup the environment (you need the standard autoconf tools to do so).
  4. Run ./configure, with some options if you wish. The only interesting one is the usual --prefix=/where/you/want/it/installed.
  5. Run make.
  6. Run make install, possibly as root if the destination permissions require that.
  7. See if it works. Try valgrind ls -l. Either this works, or it bombs out with some complaint.

Use memcheck tool detect qemu memory leak

About memcheck

  1. Accessing memory you shouldn't, e.g. overrunning and underrunning heap blocks, overrunning the top of the stack, and accessing memory after it has been freed.
  2. Using undefined values, i.e. values that have not been initialised, or that have been derived from other undefined values.
  3. Incorrect freeing of heap memory, such as double-freeing heap blocks, or mismatched use of malloc/new/new[] versus free/delete/delete[]
  4. Overlapping src and dst pointers in memcpy and related functions.
  5. Passing a fishy (presumably negative) value to the size parameter of a memory allocation function.
  6. Memory leaks.

valgrind parameters

  1. --tool=memcheck: To use memcheck tool, you can specify --tool=memcheck on the Valgrind command line.
  2. --leak-check=full: Memcheck will give details for each definitely lost or possibly lost block, including where it was allocated.
  3. --log-file: Specifies that Valgrind should send all of its messages to the specified file. If the file name is empty, it causes an abort.

Detect qemu

sudo valgrind --tool=memcheck --leak-check=full --log-file=qemu.log <qemu-command>

full command example:

sudo valgrind --tool=memcheck --leak-check=full --log-file=qemu.log qemu-system-x86_64 -vnc :1 -name default-node -device sga --enable-kvm -smbios file=/home/infrasim/.infrasim/default/data/dell_r730_smbios.bin -boot order=ncd,splash=/usr/local/infrasim/data/boot_logo.jpg -machine q35,usb=off,vmport=off -chardev socket,path=/home/infrasim/.infrasim/default/.serial,id=serial0,reconnect=10 -device isa-serial,chardev=serial0 -uuid c55d539a-e4c1-43f2-b6ec-cc92340a4321 -cpu Haswell,+vmx -smp 2,sockets=2,cores=1,threads=1 -m 1024 -device ahci,id=sata0 -drive format=qcow2,cache=writeback,id=sata0-0-0-0,file=/home/infrasim/.infrasim/default/disk00.img,if=none -device ide-hd,bus=sata0.0,drive=sata0-0-0-0,id=dev-sata0-0-0-0 -netdev user,id=netdev0 -device e1000,netdev=netdev0,mac=00:60:16:9f:3a:f4 -chardev socket,host=127.0.0.1,port=9002,id=ipmi0,reconnect=10 -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0 -device isa-ipmi-kcs,bmc=bmc0 -chardev socket,host=127.0.0.1,port=2345,id=monitorchardev,server,nowait -mon chardev=monitorchardev,mode=readline

Reference