PERENNIAL FASCINATION WITH ALL THINGS TECH

Main menu

Post navigation

How To Install Valgrind On Mac OS X

As your code gets bigger, keeping track of memory becomes critical. These things often become the source of huge crashes and annoying bugs. Detecting them manually is a very tedious process. So we need a tool to manage all this for us. This is where Valgrind comes into picture. Valgrind is a programming tool for memory debugging, memory leak detection, and profiling. It can be used to keep track of all the memory allocated at all stages in your program. Although it was initially meant to be a memory debugger, it has grown to become a generic framework for building dynamic analysis tools. You can use Valgrind to build new tools as well. There are Valgrind tools like Memcheck that can automatically detect many memory management and threading bugs, and profile your programs in detail. Valgrind is widely used in the industry and it’s well respected within the coding community. Let’s see how we can get it up and running on Mac OS X.

Assuming you have svn installed (usually comes pre-installed with OS X), get the latest valgrind repo by executing the following command:

>> svn co svn://svn.valgrind.org/valgrind/trunk valgrind

This will create a directory called “valgrind”. Make sure you have Macports installed. If not, install from here. Once you install it, use it to install “automake”:

>> sudo port install automake

For OS X Mountain Lion, running the above command might sometimes throw an error saying that the Xcode you have is not the latest version (even though you have the newer version). It will look something like this:

Error: The installed version of Xcode (2.0 or lower) is too old to use on the installed OS version. Version 4.5.2 or later is recommended on Mac OS X 10.8.

This happens because it uses some default path. Use the following command to change it:

Here, “a.out” is the default executable generated after compiling the code. If you using a different name, replace it appropriately.

A quick note before ending this post. Valgrind is supposed to be buggy for OS X Mountain Lion. The above installation procedure will work just fine but Valgrind may not detect memory leaks in a few cases. So just be careful about that!

30 thoughts on “How To Install Valgrind On Mac OS X”

I didn’t think valgrind was available on Mac or rather that it’d be so easy to get it running on Mac OS X. Thanks for the post ! I’ve also had issues with the location of XCode in the past and xcode-select saved me. I wonder why this is required at all. But well… one doesn’t always have to know why, as long as there’s a how…

Yes, Valgrind can be run on the OS X 10.8 and 10.9. Not perfect but downloads and runs on Mavericks.I suspect Macports does not do it well but Homebrew is tolerant and able to install Valgrind provided there are no other instances of valgrind and other Installers e.g Macports & Fink.

I’m working on porting Valgrind to mavericks as well as for the iOS 7 simulator. It seems like there are a few issues: some intel instructions are missing and they got the bsdthread_register system call wrong, specifically the dispatchqueue_offset argument is hard-coded to 10.7 or 10.8’s value.

Hello Mark,
Do you have further context on where Valgrind is hard coding the dispatchqueue_offset argument to bsdthread_register syscall incorrectly. We (V devs) know that the bit rot in that part of the code is high at present.
Even better do you have patches?
Happy to review and submit if they pass muster.

while running this command i get this error:
command: >> ./autogen.sh
error:
coregrind/Makefile.am:90: but option ‘subdir-objects’ is disabled
coregrind/Makefile.am:90: warning: source file ‘m_mach/vm_mapUser.c’ is in a subdirectory,
coregrind/Makefile.am:90: but option ‘subdir-objects’ is disabled
coregrind/Makefile.am:371: warning: source file ‘m_replacemalloc/vg_replace_malloc.c’ is in a subdirectory,
coregrind/Makefile.am:371: but option ‘subdir-objects’ is disabled
coregrind/Makefile.am:378: warning: source file ‘m_replacemalloc/vg_replace_malloc.c’ is in a subdirectory,
coregrind/Makefile.am:378: but option ‘subdir-objects’ is disabled
error: while running ‘automake -a’
do you know how can i solve it?