Wednesday, February 24, 2010

Compiling Valgrind on Snow Leopard

Checkout the valgrind sources:

svn co svn:// valgrind

Download Greg Parker's 10.6 patch
curl > 10.6.patch
mv 10.6.patch ./valgrind

Apply the patch

cd valgrind
patch -p0 < 10.6.patch

Compile valgrind (if you want to run it on 64 bit binaries, use "./configure --build=amd64-darwin", otherwise you'll build it for 32 bits)

Install it
sudo make install

Run it
cristi:tmp diciu$ cat /tmp/test.c
int main()
char * t = malloc(1024);
char * w = "Ana";

t = w;
cristi:tmp diciu$ gcc -arch i386 /tmp/test.c
cristi:tmp diciu$ valgrind --leak-check=full /tmp/a.out
==938== in use at exit: 1,344 bytes in 8 blocks
==938== total heap usage: 8 allocs, 0 frees, 1,344 bytes allocated
==938== 1,024 bytes in 1 blocks are definitely lost in loss record 4 of 4
==938== at 0xF666: malloc (vg_replace_malloc.c:195)
==938== by 0x1F66: main (in /tmp/a.out)


scztt said...

Thanks for the rundown. This worked perfectly the first time - a rarity for "Compile this non-OSX tool on OSX" instructions.

diciu said...

@scztt - well, Nicholas Nethercote and Greg Parker made it easy.

Now let's hope someone takes over and brings back 10.6 support in the valgrind trunk.

johnswan said...

Fantastic, thank you!

Everything worked first time, except for the test program at the end.

Here's my correction for the example:

cat >/tmp/test.c
int main()
char * t = malloc(1024);
char * w = "Ana";

t = w;
hit CTRL-D

gcc -arch i386 /tmp/test.c
valgrind --leak-check=full ./a.out

Many thanks again for doing this!

diciu said...

@johnswan you're right about the missing include but I'm lazy and, in this case, gcc lets me get away with a warning, compiling test.c in spite of the missing header.

John S. said...


I'm still not able to get Valgrind to work on my Macbook Pro 10.6.

I downloaded the latest Release 3.5.0 from Valgrind.

Then I navigated to the directory via command line and issued the command:

curl > 10.6.patch

Then I issues: patch -p0 < 10.6.patch
- This did work.

Then I ran ./configure, and got the same error as before:

configure: error: Valgrind works on Darwin 9.x (Mac OS X 10.5).

Having Valgrind on my mac would save my life in my Data Structures class, so any solution would be greatly appreciated.

Thank you.

diciu said...

@John S - I don't know why it did not work for you. Are you sure you followed the entire procedure (i.e. including running

John S. said...

I finally got it!

The problem was I sipped the SVN part, and downloaded the Release from the Valgrind site.

It's working from the command line now. Thank you very much.

I'm curious to know if it is now on my path? It woks from the command line, but I'm using the Eclipse Linux Tools plug-in, and when I try to profile my project with Valgrind it displays an error saying to ensure valgrind in insalled and on my path.

What do you think?

Thank you.

diciu said...

@John S: if you run "which valgrind" in the Terminal app and you get "/usr/local/bin/valgrind" as a response, the path is set up correctly.

On my 10.6, /usr/local/bin is already in path (it's set up in /etc/paths)

If the "which" commands does not yield a response, you can edit your path by editing ~/.bash_profile and adding a new line:

export PATH=$PATH:/usr/local/bin

John S. said...

Thank you.

When I: which valgrind I get - /usr/local/bin/valgrind

So it is for sure on my path. My problem is that for some reason the Eclipse IDE is not seeing it on the path.

I would to try to add the file itself to the .bash_profile, but when I: ls -a ~ The .bash_profile doesn't exist. It's not in my home directory.

How do I find and open it?

Thank you for the help.

Ivan said...

Beautiful work. Thanks.