How To Install Valgrind On Mac OS X

mainAs 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:

>> sudo xcode-select -switch /Applications/Xcode46-DP3.app/Contents/Developer

Change the name from “Xcode46-DP3” to whatever version you have. Xcode46-DP3 refers to Xcode 4.6 Developer Version 3.

Now run the automake install command again and it will work.

We have to install another dependency called “autoconf” before proceeding further. Execute the following command to do it:

>> sudo port install autoconf

Now we are all set to install valgrind. Run the following commands to install it:

>> cd valgrind
>> ./autogen.sh
>> ./configure 
>> make
>> sudo make install

And you are done! We are all set to use valgrind now. Execute the following command to use valgrind on your code.

>> g++ test.cpp
>> valgrind ./a.out

This will display minimal information on the command line. To see full details, execute the following command:

>> valgrind --tool=memcheck --leak-check=yes --leak-check=full ./a.out arg1 arg2

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

  1. Pingback: Valgrind Qt Application on Mac OSX - reporting incompatible processor | BlogoSfera

  2. 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…

  3. I get an error when i try to make

    m_syswrap/syswrap-darwin.c: At top level:
    m_syswrap/syswrap-darwin.c:8328: error: ‘vgSysWrap_darwin_iopolicysys_before’ undeclared here (not in a function)
    m_syswrap/syswrap-darwin.c:8328: error: ‘vgSysWrap_darwin_iopolicysys_after’ undeclared here (not in a function)
    make[3]: *** [m_syswrap/libcoregrind_amd64_darwin_a-syswrap-darwin.o] Error 1

    Anyone seen this ? Any pointers ?

  4. 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.

  5. 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.

  6. 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?

  7. anyone?
    checking for a supported version of gcc… Configured with: –prefix=/Library/Developer/CommandLineTools/usr –with-gxx-include-dir=/usr/include/c++/4.2.1
    ok (4.2.1)
    checking build system type… x86_64-apple-darwin14.1.0
    checking host system type… x86_64-apple-darwin14.1.0
    checking for a supported CPU… ok (x86_64)
    checking for a 64-bit only build… no
    checking for a 32-bit only build… no
    checking for a supported OS… ok (darwin14.1.0)
    checking for the kernel version… unsupported (14.1.0)
    configure: error: Valgrind works on Darwin 10.x, 11.x, 12.x and 13.x (Mac OS X 10.6/7/8/9)
    bash-3.2$ make
    make: *** No targets specified and no makefile found. Stop.
    bash-3.2$ sudo make install
    make: *** No rule to make target `install’. Stop.
    bash-3.2$

  8. i got these lines at the end of the ./configure step.

    checking for the kernel version… unsupported (14.1.0)
    configure: error: Valgrind works on Darwin 10.x, 11.x, 12.x and 13.x (Mac OS X 10.6/7/8/9)

    any solution?

    • You will need to run ‘svn up’ within the folder you used to check out Valgrind. Following this, best to run ‘make clean’, then ‘./autogen.sh’, ‘./configure –disable-tls’ and ‘make’.

      Presently, there is no official Valgrind release that supports OS X 10.10 Yosemite. However (partial) support is being worked on within the latest SVN code base.

      • As of the Valgrind 3.11.0 release (22 September 2015), you should no longer need to track the latest development SVN code base for preliminary OS X 10.10+ support.

        Of course, you can continue doing so if you’d like to track the latest features and contribute to development.

  9. Hey I tried to install as you suggested until you run the commands to install Valgrind..The command make during execution shows “No such file in directory”..hence I cannot use it

    Please help me out.

  10. Hi, Installed as you said, create ne a.out file, but when I try to launch Valgrind terminal says that “valgrind: command not found”. I’m running it on El Captain

    • oh this means the Valgrind is not installed properlly , you can try to reinstall it, did you get any error message when installing?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s