Valgrind on Mac OS X (10.9) Mavericks

On Mac OS X, the common way to write C code is to simply use the Xcode IDE.  Xcode comes with solid autocomplete functionality and has a built-in instruments app – a performance, analysis, and testing tool for dynamically tracing and profiling your program – which is absolutely critical for preserving your sanity and for revealing any mistakes you might have made that is causing memory leaks and various syntax errors in your non-trivial C applications.

The other alternative tool is, of course, the venerable valgrind.

I was curious to see if I could get valgrind working on my Mac laptop running Mavericks (10.9) as it is not yet officially supported.  Attempts to get valgrind installed via both the Macports and Homebrew package managers fail.

Fortunately, I discovered a patched branch by Frederic Germain here – https://github.com/fredericgermain/valgrind/ and and this patched branch seems to work great for Mavericks.

And this works beautifully.

# Make sure I have autoconf and automake both installed.
sudo port -v install automake
sudo port -v install autoconf
# Grab Frederic's patched valgrind on his "homebrew" branch
cd ~/work  # My usual project directory
git clone https://github.com/fredericgermain/valgrind/ -b homebrew
cd valgrind
# Because he placed VEX as a git submodule, we have to make sure we clone it too
git submodule init
git submodule update
# With VEX submodule now available, we can compile valgrind
./autogen.sh
./configure --prefix=/usr/local   # set the stage for sudo make install to place our compiled valgrind binary as /usr/local/bin/valgrind
make
sudo make install

And checking that I indeed have valgrind installed.

calvin % which valgrind
/usr/local/bin/valgrind

And now, to see valgrind in action, checking a simple C program just to verify that valgrind works as advertised.

cd ~/work/simplecprogram
make program1  # compiles my program1.c source file to program1 binary
valgrind ./program1

We should see the stdout read:-

==49132== Memcheck, a memory error detector
==49132== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==49132== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==49132== Command: ./program1
==49132==
==49132== WARNING: Support on MacOS 10.8/10.9 is experimental and mostly broken.
==49132== WARNING: Expect incorrect results, assertions and crashes.
==49132== WARNING: In particular, Memcheck on 32-bit programs will fail to
==49132== WARNING: detect any errors associated with heap-allocated data.
==49132==
Hello World.
==49132==
==49132== HEAP SUMMARY:
==49132== in use at exit: 29,917 bytes in 378 blocks
==49132== total heap usage: 456 allocs, 78 frees, 35,965 bytes allocated
==49132==
==49132== LEAK SUMMARY:
==49132== definitely lost: 0 bytes in 0 blocks
==49132== indirectly lost: 0 bytes in 0 blocks
==49132== possibly lost: 0 bytes in 0 blocks
==49132== still reachable: 4,096 bytes in 1 blocks
==49132== suppressed: 25,821 bytes in 377 blocks
==49132== Rerun with --leak-check=full to see details of leaked memory
==49132==
==49132== For counts of detected and suppressed errors, rerun with: -v
==49132== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 117 from 20)

And that’s it. Notice the warnings of course!  As I mentioned, valgrind is not offically supported beyond Mac OS X 10.7 yet.

WARNING: Support on MacOS 10.8/10.9 is experimental and mostly broken.
WARNING: Expect incorrect results, assertions and crashes.
WARNING: In particular, Memcheck on 32-bit programs will fail to
WARNING: detect any errors associated with heap-allocated data.

In any case, it is completely possible to invoke instruments from the command line as well – if you insist on writing C programs without the help of Xcode. And is a much safer bet when you are working on your production C programs.  But… that shall be a topic for another day. :-)

  • Pingback: StreamComputing | Valgrind suppression file for AMD64 on Linux - StreamComputing()

  • dez82

    Hi Calvin,

    I tried doing this in Mavericks. Got the automake and autoconf installed using this

    http://www.jattcode.com/installing-autoconf-automake-libtool-on-mac-osx-mountain-lion/

    However, when I run ‘sudo make install’, I get the below output.

    I had a look through install-sh and can find nothing that makes sense(my makefile knowledge is very basic and I’m learning C) so I’m a little lost.

    ______

    /Applications/Xcode.app/Contents/Developer/usr/bin/make install-recursive

    Making install in include

    make[3]: Nothing to be done for `install-exec-am’.

    .././install-sh -c -d ‘/usr/local/include/valgrind’

    .././install-sh -c -d ‘/usr/local/include/valgrind/vki’

    /usr/bin/install -c -m 644 vki/vki-linux.h vki/vki-darwin.h vki/vki-posixtypes-amd64-linux.h vki/vki-posixtypes-arm64-linux.h vki/vki-posixtypes-ppc32-linux.h vki/vki-posixtypes-ppc64-linux.h vki/vki-posixtypes-x86-linux.h vki/vki-posixtypes-arm-linux.h vki/vki-posixtypes-s390x-linux.h vki/vki-posixtypes-mips32-linux.h vki/vki-posixtypes-mips64-linux.h vki/vki-amd64-linux.h vki/vki-arm64-linux.h vki/vki-ppc32-linux.h vki/vki-ppc64-linux.h vki/vki-x86-linux.h vki/vki-arm-linux.h vki/vki-s390x-linux.h vki/vki-mips32-linux.h vki/vki-mips64-linux.h vki/vki-scnums-amd64-linux.h vki/vki-scnums-arm64-linux.h vki/vki-scnums-ppc32-linux.h vki/vki-scnums-ppc64-linux.h vki/vki-scnums-x86-linux.h vki/vki-scnums-arm-linux.h vki/vki-scnums-s390x-linux.h vki/vki-scnums-mips32-linux.h vki/vki-scnums-mips64-linux.h vki/vki-scnums-darwin.h vki/vki-xen.h vki/vki-xen-domctl.h vki/vki-xen-evtchn.h vki/vki-xen-gnttab.h vki/vki-xen-hvm.h vki/vki-xen-memory.h vki/vki-xen-mmuext.h vki/vki-xen-sysctl.h vki/vki-xen-tmem.h vki/vki-xen-version.h ‘/usr/local/include/valgrind/vki’

    .././install-sh -c -d ‘/usr/local/include/valgrind/vki’

    /usr/bin/install -c -m 644 vki/vki-xen-x86.h vki/vki-linux-drm.h ‘/usr/local/include/valgrind/vki’

    /usr/bin/install -c -m 644 pub_tool_basics.h pub_tool_basics_asm.h pub_tool_aspacehl.h pub_tool_aspacemgr.h pub_tool_clientstate.h pub_tool_clreq.h pub_tool_debuginfo.h pub_tool_errormgr.h pub_tool_execontext.h pub_tool_gdbserver.h pub_tool_poolalloc.h pub_tool_hashtable.h pub_tool_libcbase.h pub_tool_libcassert.h pub_tool_libcfile.h pub_tool_libcprint.h pub_tool_libcproc.h pub_tool_libcsetjmp.h pub_tool_libcsignal.h pub_tool_machine.h pub_tool_mallocfree.h pub_tool_options.h pub_tool_oset.h pub_tool_rangemap.h pub_tool_redir.h pub_tool_replacemalloc.h pub_tool_seqmatch.h pub_tool_signals.h pub_tool_sparsewa.h pub_tool_stacktrace.h pub_tool_threadstate.h pub_tool_tooliface.h pub_tool_vki.h pub_tool_vkiscnums.h pub_tool_vkiscnums_asm.h pub_tool_wordfm.h pub_tool_xarray.h valgrind.h ‘/usr/local/include/valgrind/.’

    Making install in VEX

    /Applications/Xcode.app/Contents/Developer/usr/bin/make install-am

    .././install-sh -c -d ‘/usr/local/lib/valgrind’

    /usr/bin/install -c -m 644 libvex-amd64-darwin.a libvex-x86-darwin.a ‘/usr/local/lib/valgrind’

    ( cd ‘/usr/local/lib/valgrind’ && ranlib libvex-amd64-darwin.a )

    ( cd ‘/usr/local/lib/valgrind’ && ranlib libvex-x86-darwin.a )

    .././install-sh -c -d ‘/usr/local/include/valgrind’

    /usr/bin/install -c -m 644 pub/libvex.h pub/libvex_basictypes.h pub/libvex_emnote.h pub/libvex_guest_offsets.h pub/libvex_guest_x86.h pub/libvex_guest_amd64.h pub/libvex_guest_ppc32.h pub/libvex_guest_ppc64.h pub/libvex_guest_arm.h pub/libvex_guest_arm64.h pub/libvex_guest_s390x.h pub/libvex_guest_mips32.h pub/libvex_guest_mips64.h pub/libvex_s390x_common.h pub/libvex_ir.h pub/libvex_trc_values.h ‘/usr/local/include/valgrind’

    Making install in coregrind

    make[2]: *** No rule to make target `/usr/include/mach/mach_vm.defs’, needed by `m_mach/mach_vmUser.c’. Stop.

    make[1]: *** [install-recursive] Error 1

    make: *** [install] Error 2

    _______

    The parts that seem like the errors are at the end

    make[2]: *** No rule to make target `/usr/include/mach/mach_vm.defs’, needed by `m_mach/mach_vmUser.c’. Stop.

    make[1]: *** [install-recursive] Error 1

    make: *** [install] Error 2

    But I’m not sure if I’m missing something sooner.
    ___
    If you know what’s causing this, let me know.

    • Valgrind and Darwin (Mac OS X) are both moving targets and Valgrind build often fails from release-to-release and from (svn) revision-to-revision for a specific Mac OS X version.

      I have updated a work set-up for Mac OS X 10.10 (Darwin 14.3.0) now; and including a build script that fixes the exact version of Valgrind we will grab from Valgrind’s svn repository (r14960). See https://calvinx.com/2015/04/10/valgrind-on-mac-os-x-10-10-mavericks/ , give it a go when you can and let me know if this works out.

      Cheers.

  • Oli

    Hi,

    I’ve been following thouroughly your steps, but when running make, I got the exact same error as @dez82:disqus …
    Any idea why that is?

  • Narsimham Chelluri

    Hello,

    Thanks for the article. I have tried installing Valgrind a few different ways: From the SVN repo’s trunk, using Homebrew, and now your post.

    If it makes a difference I’m using autoconf and automake installed from port, not brew, which is slightly different from your instructions.

    Unfortunately I keep running into the same issue:


    valgrind (homebrew *=) $ ./vg-in-place ~/dev/c/testing/valgrind-test

    valgrind: m_libcfile.c:73 (Int vgPlain_safe_fd(Int)): Assertion 'newfd >= VG_(fd_hard_limit)' failed.
    ==21430== at 0x238048F6B: ???
    ==21430== by 0x238048F14: ???
    ==21430== by 0x238049331: ???
    ==21430== by 0x23804F84B: ???
    ==21430== by 0x23804EE95: ???
    ==21430== by 0x23804EDD3: ???

    sched status:
    running_tid=0

    Note: see also the FAQ in the source distribution.
    It contains workarounds to several common problems.
    In particular, if Valgrind aborted or crashed after
    identifying problems in your program, there's a good chance
    that fixing those problems will prevent Valgrind aborting or
    crashing, especially if it happened in m_mallocfree.c.

    If that doesn't help, please report this bug to: www.valgrind.org

    In the bug report, send all the above text, the valgrind
    version, and what OS and version you are using. Thanks.

    valgrind (homebrew *=) $

    Just wondering if anyone has any solutions to the “Assertion ‘newfd >= VG_(fd_hard_limit)’ failed.” message.

    • Danix Defcon 5

      I stumbled upon the same error. However, if I run the same command line as root, it works. Maybe valgrind is requesting something that requires root privs?

  • I can’t seem to `make` it successfully either, even after following your guide of commands to execute. On Mac OSX 10.9 here too.

    The following error message is displayed:

    gcc -DHAVE_CONFIG_H -I. -I.. -I.. -I../include -I../VEX/pub -DVGA_amd64=1 -DVGO_darwin=1 -DVGP_amd64_darwin=1 -DVGPV_amd64_darwin_vanilla=1 -I../coregrind -DVG_LIBDIR=””/usr/local/lib/valgrind”” -DVG_PLATFORM=””amd64-darwin”” -arch x86_64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wno-format-zero-length -fno-strict-aliasing -fno-builtin -mmacosx-version-min=10.5 -fno-stack-protector -Wno-long-long -Wwrite-strings -fno-stack-protector -MT libcoregrind_amd64_darwin_a-m_syscall.o -MD -MP -MF .deps/libcoregrind_amd64_darwin_a-m_syscall.Tpo -c -o libcoregrind_amd64_darwin_a-m_syscall.o `test -f ‘m_syscall.c’ || echo ‘./’`m_syscall.c

    m_syscall.c:574:1: error: unknown type name ‘__private_extern__’

    __private_extern__ UWord

    ^

    m_syscall.c:575:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘do_syscall_unix_WRK’

    do_syscall_unix_WRK ( UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */

    ^

    m_syscall.c:598:1: error: unknown type name ‘__private_extern__’

    __private_extern__ UWord

    ^

    m_syscall.c:599:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘do_syscall_mach_WRK’

    do_syscall_mach_WRK ( UWord a1, UWord a2, UWord a3, /* rdi, rsi, rdx */

    ^

    m_syscall.c: In function ‘vgPlain_do_syscall’:

    m_syscall.c:775:10: warning: implicit declaration of function ‘do_syscall_unix_WRK’ [-Wimplicit-function-declaration]

    wLO = do_syscall_unix_WRK(a1,a2,a3,a4,a5,a6,a7,a8,

    ^

    m_syscall.c:780:10: warning: implicit declaration of function ‘do_syscall_mach_WRK’ [-Wimplicit-function-declaration]

    wLO = do_syscall_mach_WRK(a1,a2,a3,a4,a5,a6,a7,a8,

    ^

    make[3]: *** [libcoregrind_amd64_darwin_a-m_syscall.o] Error 1

    make[2]: *** [all] Error 2

    make[1]: *** [all-recursive] Error 1

    make: *** [all] Error 2

  • Dan Tenenbaum

    You can install automake and autoconf from source as well, you don’t need to install them using port. Just follow the instructions here: http://www.jattcode.com/installing-autoconf-automake-libtool-on-mac-osx-mountain-lion/ but don’t try to build/install libtool because it already exists on Mavericks, at least it did on my system.

  • Rob

    HI Calvin,

    Thanks for posting this, it looks good to me, though getting stuck with:
    autogen.sh

    I get the error:

    ./autogen.sh: line 6: aclocal: command not found
    error: while running ‘aclocal’

    What am I missing here, i am running Mavericks also. Any help on this would be very much appreciated, really struggling to get valgrind at the moment.

    Kind regards
    Rob

  • Danix Defcon 5

    For those of you having issues with Mavericks, it’s easier to download the latest tarball here:

    http://www.valgrind.org/downloads/current.html

    10.9 (Mavericks) is now officially supported. Just run

    ./configure

    make

    as you would with any other tarball package. As another notice: the *official* source code repo is in SVN:

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

  • Pingback: Calvin's | valgrind on Mac OS X (10.10) Mavericks()

  • Hello, I have updated a working set-up for Mac OS X 10.10 (Darwin 14.3.0) – https://calvinx.com/2015/04/10/valgrind-on-mac-os-x-10-10-mavericks/

    Also, for those going further and using it for more C programming work, I have a working Valgrind suppression file for your reference – https://gist.github.com/calvinchengx/0b1d45f67be9fdca205b

    And a response to a StackOverflow question relating to system level memory leaks detected by Valgrind on Mac OS X – http://stackoverflow.com/questions/27842968/valgrind-reports-errors-for-a-very-simple-c-program/29576131#29576131