[LWN Logo]
[LWN.net]
From:	 Julian Seward <Julian_Seward@muraroa.demon.co.uk>
To:	 lwn@lwn.net
Subject: Valgrind, an open-source memory debugger for x86-linux
Date:	 Wed, 13 Mar 2002 16:05:11 +0000


Valgrind is a memory debugger for x86 GNU/Linux programs, which
is significantly more powerful than the open-source community
has had up to now.  I attach some explainatory notes at the end
of this message.  The main site for it is

  http://developer.kde.org/~sewardj

Various folks in the KDE development community have been using
Valgrind to debug the KDE 3 core libraries and apps for the past
month.  David Faure, of Mandrake, mentioned using it in a
recent OSNews.com interview 
(http://www.osnews.com/story.php?news_id=704), and feedback from
others who have taken snapshots has been very positive.

Thanks,

Julian Seward (jseward@acm.org)

----------------------------------------------------------------

(copied from http://developer.kde.org/~sewardj)

Valgrind is a GPL'd tool to help you find memory-management problems 
in your programs.  When a program is run under Valgrind's supervision,
all reads and writes of memory are checked, and calls to 
malloc/new/free/delete are intercepted.  As a result, Valgrind can 
detect problems such as: 

      Use of uninitialised memory 
      Reading/writing memory after it has been free'd 
      Reading/writing off the end of malloc'd blocks 
      Reading/writing inappropriate areas on the stack 
      Memory leaks -- where pointers to malloc'd blocks are lost forever 
      Passing of uninitialised and/or unaddressible memory to system calls 
      Mismatched use of malloc/new/new [] vs free/delete/delete [] 

Valgrind tracks each byte of memory in the original program with nine 
status bits, one of which tracks addressibility of that byte, while the 
other eight track the validity of the byte. As a result, it can detect 
the use of single uninitialised bits, and does not report spurious errors 
on bitfield operations. 

You can use it to debug more or less any dynamically-linked ELF Linux x86 
executable, without modification, recompilation, or anything.  If you want, 
Valgrind can start GDB and attach it to your program at the point(s) where 
errors are detected, so that you can poke around and figure out what was 
going on at the time. 

Valgrind works well enough to debug KDE3 applications. It is still a work 
in progress, and has some teething difficulties, which I am working through 
as fast as I can. I can certainly start and run kate, konqueror, etc, from 
KDE in CVS.  I used to be able to see them them doing bad stuff like reading 
and writing freed memory, but nowadays they are much better behaved :). 
They run stably and exit cleanly, and most of the errors reported are genuine,
as far as I can tell.  Update as of 21 Feb: after much invaluable feedback 
and bug-fixing from the kde-core-devel crew, Valgrind can run large parts of 
KDE3 without serious problems. 

Full documentation is supplied, up to date as of 10 March 02. The overly-curious 
may want to read detailed tech docs about how Valgrind is implemented. 

To use: you need an x86 machine running Linux 2.4.X, glibc 2.2.X and XFree86 4.X. 
This covers most contemporary Linux distributions. I develop it on RedHat 7.2, 
so that platform, at least, should work. It has worked in the past on RedHat 6.2 
and 7.1 too. 

There are some limitations, the most significant of which are: 

      No support for threaded programs. Valgrind will abort if your program 
         does a clone() system call. 

      No support for MMX, SSE, SSE2 insns. When queried with a CPUID insn, 
         Valgrind claims to be a pre-MMX Pentium-133 (P54C), which sometimes 
         succeeds in fooling glibc and other libraries into not using MMX insns. 

      Support for non-POSIX signal stuff is flaky. If you stick to POSIX signals 
         you should be ok.