How to install Valgrind on macOS Mojave(10.14) with Homebrew?

MacosHomebrewValgrindMemory Leak-Detector

Macos Problem Overview


I tried to install Valgrind with brew install Valgrind and got :

valgrind: This formula either does not compile or function as expected
on macOS versions newer than Sierra due to an upstream
incompatibility. 
Error: An unsatisfied requirement failed this build.

I tried brew install --HEAD Valgrind.

Instead, after successfully installing the dependencies autoconf, automake and libtool, when it tries to install valgrind, I get a configure error:

Valgrind works on Darwin 10.x, 11.x, 12.x, 13.x, 14.x, 15.x, 16.x and
17.x (Mac OS X 10.6/7/8/9/10/11 and macOS 10.12/13)

My OS is macOS Mojave(10.14), so does it mean that I can't install a functioning Valgrind with Homebrew presently?

Macos Solutions


Solution 1 - Macos

A (rather painful) install from source workaround based on this patch, this post and this answer.

$ git clone https://github.com/Echelon9/valgrind.git
$ cd valgrind
$ git checkout feature/v3.14/macos-mojave-support-v2
$ ./autogen.sh
$ ./configure --prefix=/where/you/want/it/installed --enable-only64bit
$ make

If you get the following error: No rule to make target '/usr/include/mach/mach_vm.defs’, you will need to run xcode-select --install. You might need to install Xcode from the app store if you don't already have it. Once that's done, you will need to edit the coregrind/Makefile:

Search for:

am__append_19 = \
	/usr/include/mach/mach_vm.defs \
        /usr/include/mach/task.defs \
        /usr/include/mach/thread_act.defs \
        /usr/include/mach/vm_map.defs

After double checking the below folder exists, prefix every line with:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk

End result should be:

am__append_19 = \
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/mach/mach_vm.defs \
        /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/mach/task.defs \
        /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/mach/thread_act.defs \
        /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/mach/vm_map.defs

Now run make again and the includes should be found. But that doesn't necessarily mean it will compile. I got the following error:

vg_preloaded.c:136:19: error: expected ';' before 'const'
 __private_extern__ const char *__crashreporter_info__ = "Instrumented by Valgrind " VERSION;

A fix for this is to add the following line:

#define __private_extern__ extern

to the following files:

  • coregrind/m_syscall.c
  • coregrind/m_syswrap/syswrap-darwin.c
  • coregrind/vg_preloaded.c

Finally, you need to cross your fingers hoping no other errors show up:

$ make
$ make install

Solution 2 - Macos

You may use Experimental Version of Valgrind for macOS 10.14.5 Mojave at:

https://github.com/sowson/valgrind

The command to use it is:

brew install --HEAD https://raw.githubusercontent.com/sowson/valgrind/master/valgrind.rb

It is still experimental and needs some work but for simple projects works already... Enjoy!

Solution 3 - Macos

addition: I found this one worked for me on my OSX 10.14

brew install --HEAD https://raw.githubusercontent.com/LouisBrunner/valgrind-macos/master/valgrind.rb

A branch that is working to get OSX correct. something to tide us over until we get a real valgrind version fixed.

Solution 4 - Macos

I am having the same issue. It seems like valgrind is not compatible with the newest macOS(10.14 Mojave). Tried installing it the High Sierra way (https://www.gungorbudak.com/blog/2018/04/28/how-to-install-valgrind-on-macos-high-sierra/) and got the same output you described. The only solution I can offer you right now is either working on virtual machine (https://www.virtualbox.org/) or using Docker (https://www.gungorbudak.com/blog/2018/06/13/memory-leak-testing-with-valgrind-on-macos-using-docker-containers/).

Solution 5 - Macos

As others have mentioned, Louis Brunner maintains a working version at https://github.com/LouisBrunner/valgrind-macos.

brew tap LouisBrunner/valgrind
brew install --HEAD LouisBrunner/valgrind/valgrind

This worked for me for MacOS 10.15.7 Catalina.

Solution 6 - Macos

I have just found a working solution to use VALGRIND on my Mac (Mojave 10.14.6). Just run this command :

brew install --HEAD https://raw.githubusercontent.com/LouisBrunner/valgrind-macos/master/valgrind.rb

(From https://github.com/LouisBrunner/valgrind-macos)

Hope it will work for you.

Solution 7 - Macos

Not a proper solution for macOs, but for the time being, I created a docker image. After installing docker for macOS, this is how to start valgrind:

cd </path/to/source/directory/where/you/want/run/valgrind/with>
curl -O https://raw.githubusercontent.com/biocyberman/ValgrindDocker/master/startValgrind
./startValgrind # this will takes time for the first time, because it needs to fetch docker valgrind image
# you will get a root command prompt inside the docker image. 
# do what ever you want
# type 'exit' to quit

Solution 8 - Macos

You can follow alex.m's answer to get valgrind, but if you'r using it on a int main() { return 0; } program, you'll get many weird errors, and non-existing allocs / free.

To 'hide' these annoying errors, you can follow theses steps (it's more a workaround than a real fix) (based on this wiki page and some research in Valgrind's source code):

  • First, create and compile a int main() { return 0; } program.
  • Execute the following command (to create file containing error suppression):

valgrind --leak-check=full --show-reachable=yes --error-limit=no --gen-supressions=all --log-file=$YOUR_LOG$ $YOUR_BINARY$

cat ./$YOUR_LOG$ | ./$YOUR_SCRIPT_FILE$ > minimal.supp

  • Copy and Past minimal.supp content at the end of $YOUR_VALGRIND_INSTALLATION_PATH$/lib/valgrind/default.supp file

And you are done! Weird and nonexistent errors will be ignored. If you also want's to remove the non-existing allocs, frees etc, you can directly edit Valgrind's source code. Or just use heapusage for leak-tracking

Solution 9 - Macos

As of 2019-NOV-30, it is possible to build against OS X 10.14.6 via https://github.com/sowson/valgrind and https://github.com/LouisBrunner/valgrind-macos

However, there are many test failures (see the LouisBrunner link), noise during runs, and SEGVs when running against non-trivial programs: installing is, well, installing. YMMV.

Solution 10 - Macos

(works on mojave 10.14.6)

brew install --HEAD https://raw.githubusercontent.com/sowson/valgrind/master/valgrind.rb

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKatherine ElyonView Question on Stackoverflow
Solution 1 - Macosalex.mView Answer on Stackoverflow
Solution 2 - MacosPiotr SowaView Answer on Stackoverflow
Solution 3 - MacosMeta.x.gdbView Answer on Stackoverflow
Solution 4 - Macosuser10467334View Answer on Stackoverflow
Solution 5 - MacosheinerView Answer on Stackoverflow
Solution 6 - MacosHenriBrgView Answer on Stackoverflow
Solution 7 - MacosbiocybermanView Answer on Stackoverflow
Solution 8 - MacosByProcyxView Answer on Stackoverflow
Solution 9 - Macosuser2864740View Answer on Stackoverflow
Solution 10 - MacoshchorfiView Answer on Stackoverflow