Valgrind не требует повторной компиляции программы, хотя, как и все средства отладки, он имеет возможность компилировать программы с отладочной информацией.
$ valgrind ./broken
==30882== Memcheck, a.k.a. Valgrind, a Memory ERROR detector for x86-linux.
==30882== Copyright (C) 2002-2003, and GNU GPL'd, by Julian Seward.
==30882== Using valgrind-2.0.0, a program super vision framewok for x86-linux.
==30882== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
==30882== Estimated CPU clock rate is 1547 MHz
==30882== For more details, rerun with: -v
==30882==
==30882== Invalid write of size 1
==30882== Недопустимая запись размером 1
==30882== at 0xC030DB: strcpy (mac_replace_strmem.с:174)
==30882== by 0x8048409: broken (broken.с:15)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: libc_start_main (in /lib/libc-2.3.2.so)
==30882== Address 0x650F029 is 0 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.с:153)
==30882== by 0x80483F3: broken (broken.с:14)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Conditional jump or move depends on uninitialised value(s)
==30882== Условный переход или перемещение зависит от
неинициализироваиного значения(й)
==30882== at 0x863D8E: __GI_strlen (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf (in /lib/libc-2.3.2.so)
==30882== by 0x804841C: broken (broken.с:16)
==30882== by 0x804851F: main (broken.с:47)
1: 12345
==30882==
==30882== Invalid write of size 1
==30882== at 0xC030D0: strcpy (mac_replace_.с: 173)
==30882== by 0x804844D: broken (broken.с:21)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: _libc_start_main (in /lib/libc-2.3.2.so)
==30882== Address 0x650F061 is 0 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_ malloc.с:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid write of size 1
==30882== at 0xC030DB: strcpy (mac_replace_strmem.с:174)
==30882== by 0x804844D: broken (broken.с:21)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: libc_start_main (in /lib/libc-2.3.2.so)
==30882== Address 0x650F064 is 3 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.с:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid read of size 4
==30882== Недопустимое чтение размером 4
==30882== at 0x863D50: __GI_strlen (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf (in /lib/libc-2.3.2.so)
==30882== by 0x8048460: broken (broken.с:22)
==30882== by 0x804851F: main (broken.с:47)
==30882== Address 0x650F064 is 3 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.с:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid read of size 1
==30882== at 0x857A21: [email protected]@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==30882== by 0x835309: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf(in /lib/libc-2.3.2.so)
==30882== by 0x8048460: broken (broken.с:22)
==30882== Address 0x650F063 is 2 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.с:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.c:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid read of size 1
==30882== at 0x857910: [email protected]@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==30882== by 0x835309: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf (in /lib/libc-2.3.2.so)
==30882== by 0x8048460: broken (broken.с:22)
==30882== Address 0x650F061 is 0 bytes after a block of size 5'alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.с:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
2: 12345678
==30882==
==30882== Invalid write of size 1
==30882== at 0x8048468: broken (broken.с:25)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882== by 0x8048354: (within /usr/src/d/lad2/code/broken)
==30882== Address 0x650F05B is 1 bytes before a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.c:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid read of size 4
==30882== at 0x863D50: __GI_strlen (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf (in /lib/libc-2.3.2.so)
==30882== by 0x804847A: broken (broken.c:2 6)
==30882== by 0x804851F: main (broken.c:47)
==30882== Address 0x650F064 is 3 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.c:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.c:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid read of size 1
==30882== at 0x857A21: [email protected]@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==30882== by 0x835309: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf (in /lib/libc-2.3.2.so)
==30882== by 0x804847A: broken (broken.c:2 6)
==30882== Address 0x650F063 is 2 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.c:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882==
==30882== Invalid read of size 1
==30882== at 0x857910: [email protected]@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==30882== by 0x835309: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==30882== by 0x83BC31: _IO_printf (in /lib/libc-2.3.2.so) ==30882== by 0x804847A: broken (broken.c:2 6)
==30882== Address 0x650F061 is 0 bytes after a block of size 5 alloc'd
==30882== at 0xC0C28B: malloc (vg_replace_malloc.с:153)
==30882== by 0x8048437: broken (broken.с:20)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
3: 12345678
4: 12345
==30882==
==30882== Invalid write of size 1
==30882== at 0x80484A6; broken (broken.c:3 2)
==30882== by 0x804851F: main (broken.с:47)
==30882== by 0x802BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==30882== by 0x8048354: (within /usr/src/d/lad2/code/broken)
==30882== Address 0xBFF2D0FF is just below %esp. Possibly a bug in GCC/G++
==30882== v 2.96 or 3.0.X. To suppress, use: --workaround-gcc 296-bugs = yes
5: 12345
6: 12345
7: 12345
==30882==
==30882== ERROR SUMMARY: 22 ERRORS from 12 contexts (suppressed: 0 from 0)
==30882== malloc/free: in use at exit: 5 bytes in 1 blocks.
==30882== malloc/free: 2 allocs, 1 frees, 10 bytes allocated.
==30882== For a detailed leak analysis, rerun with: --leak-check=yes
==30882== For counts of detected ERRORS, rerun with: -v
==30882== ИТОГИ ПО ОШИБКАМ: 22 ошибки в 12 контекстах (подавлено: 0 из 0)
==30882== malloc/free: используются после завершения: 5 байт в 1 блоке.
==30882== malloc/free: 2 распределения, 1 освобождение, 10 байт распределено.
==30882== Для детального анализа утечек памяти запустите с: --leak-check=yes
==30882== Для подсчета обнаруженных ошибок запустите с: -v
Обратите внимание, что Valgrind нашел все, кроме глобального переполнения и недогрузки, и указал на ошибки более точно, нежели любое другое ранее описанное средство.
Имеется опция, позволяющая включить агрессивную проверку утечек памяти, при которой для каждого распределения находятся все доступные указатели, хранящие ссылку на эту память. Это более точный способ, поскольку часто в программе память распределяют, но в конце не освобождают, так как память в любом случае будет возвращена операционной системе после того, как программа завершится.
$ valgrind --leak-check=yes ./broken
...
==2292== searching for pointers to 1 not-freed blocks.
==2292== checked 5318724 bytes.
==2292== поиск указателей на 1 неосвобожденный блок.
==2292== проверено 5318724 байт.
==2292==
==2292== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2292== 5 байт в 1 блоке определенно потеряны в потерянной записи 1 из 1
==2292== at 0хЕС528В: malloc (vg_replace_malloc.с:153)
==2292== by 0x8048437: broken (broken.с:20)
==2292== by 0x804851F: main (broken.с:47)
==2292== by 0x126BAE: __libc_start_main (in /lib/libc-2.3.2.so)
==2292==
==2292== LEAK SUMMARY:
==2292== definitely lost: 5 bytes in 1 blocks.
==2292== possibly lost: 0 bytes in 0 blocks.
==2292== still reachable: 0 bytes in 0 blocks.
==2292== suppressed: 0 bytes in 0 blocks.
==2292== Reachable blocks (those to which a pointer was found) are not shown.
==2292== To see them, rerun with: --show-reachable=yes
==2292== ИТОГИ ПО УТЕЧКАМ:
==2292== определенно потеряно: 5 байт в 1 блоке.
==2292== возможно потеряно: 0 байт в 0 блоке.
==2292== пока достижимы: 0 байт в 0 блоке.
==2292== подавлено: 0 байт в 0 блоке.
==2292== Достижимые блоки (на которые найдены указатели) не показаны.
==2292== Чтобы увидеть их, запустите с: --show-reachable=yes
Инструмент Valgrind включает детализированную информацию по своим возможностям и поддерживает множество опций командной строки, которые модифицируют его поведение. Поскольку Valgrind использует эмулятор центрального процессора, программа в нем выполняется во много раз медленнее, чем обычно в системе. Точное замедление зависит от программы, однако Valgrind задумывался для интерактивных программ.