如何使用 Homebrew 在 macOS Mojave(10.14) 上安装 Valgrind?

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

我尝试使用 brew install Valgrind 安装 Valgrind 并得到:

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.

我试过了brew install --HEAD Valgrind

相反,在成功安装依赖项 autoconfautomakelibtool 后,当它尝试安装 valgrind 时,出现配置错误:

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)

我的 OS 是 macOS Mojave(10.14),这是否意味着我目前无法使用 Homebrew 安装正常运行的 Valgrind?

我遇到了同样的问题。似乎 valgrind 与最新的 macOS(10.14 Mojave)不兼容。尝试以 High Sierra 方式安装它 (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/)。

对于 macOs 来说不是一个合适的解决方案,但暂时,我创建了一个 docker 图像。在为 macOS 安装 docker 后,这是启动 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

基于 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

如果出现以下错误:No rule to make target '/usr/include/mach/mach_vm.defs’,您将需要 运行 xcode-select --install。如果您还没有安装 Xcode,您可能需要从应用商店安装它。完成后,您需要编辑 coregrind/Makefile:

搜索:

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

仔细检查以下文件夹是否存在后,在每一行前加上:

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

最终结果应该是:

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

现在再次 运行 make 并且应该找到包含。但这并不一定意味着它会编译。我收到以下错误:

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

解决此问题的方法是添加以下行:

#define __private_extern__ extern

到以下文件:

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

最后,您需要祈祷没有其他错误出现:

$ make
$ make install

你可以按照 alex.m 的回答来获取 valgrind,但是如果你在 int main() { return 0; } 程序中使用它,你会得到很多奇怪的错误,以及不存在的分配/免费。

对于 'hide' 这些烦人的错误,您可以按照以下步骤操作(它更像是一种解决方法,而不是真正的修复)(基于 this wiki page 和对 Valgrind 源代码的一些研究):

  • 首先,创建并编译一个 int main() { return 0; } 程序。
  • 执行以下命令(创建包含错误抑制的文件):

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

  • 复制并粘贴 $YOUR_VALGRIND_INSTALLATION_PATH$/lib/valgrind/default.supp 文件末尾的 minimal.supp 内容

大功告成!奇怪的和不存在的错误将被忽略。 如果您还想删除不存在的 allocs、frees 等,您可以直接编辑 Valgrind 的源代码。或者只使用 heapusage 进行泄漏跟踪

您可以在以下位置使用适用于 macOS 10.14.5 Mojave 的 Valgrind 实验版:

https://github.com/sowson/valgrind

使用它的命令是:

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

它仍处于实验阶段,需要一些工作,但对于简单的项目来说已经可以了...享受吧!

补充:我发现这个在我的 OSX 10.14

上对我有用
brew install --HEAD https://raw.githubusercontent.com/LouisBrunner/valgrind-macos/master/valgrind.rb

一个正在努力 OSX 正确的分支。在我们修复真正的 valgrind 版本之前,可以帮助我们渡过难关。

我刚刚找到了在 Mac (Mojave 10.14.6) 上使用 VALGRIND 的有效解决方案。只需 运行 这个命令:

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

(来自 https://github.com/LouisBrunner/valgrind-macos

希望对你有用。

自 2019 年 11 月 30 日起,可以通过 https://github.com/sowson/valgrind and https://github.com/LouisBrunner/valgrind-macos

针对 OS X 10.14.6 构建

但是,有很多测试失败(参见 LouisBrunner link),运行期间的噪音,以及 运行 针对非平凡程序时的 SEGV : 安装 就是安装。 YMMV.

(适用于 mojave 10.14.6)

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

正如其他人所提到的,Louis Brunner 在 https://github.com/LouisBrunner/valgrind-macos 维护着一个工作版本。

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

这对我适用于 MacOS 10.15.7 Catalina。