我正在Learn C The Hard Way跟进,我正在Exercise 4: Introducing Valgrind。我在Mac OS X Yosemite上,在撰写本文时,没有稳定的Valgrind for Yosemite版本。我找到了Yosemite and Valgrind并使用了最高投票回答brew install --HEAD valgrind
的说明。这个安装了Valgrind和我能够跟随Zed的练习。但是,当我“修复”应用程序时,我仍然遇到错误。
要仔细检查,我回到Exercise 3,这应该没有错误,但我仍然在Valgrind中出错。这是代码,然后是输出:
ex3.c
#include <stdio.h>
int main()
{
int age = 10;
int height = 72;
printf("I am %d years old.\n", age);
printf("I am %d inches tall.\n", height);
return 0;
}
在iTerm中:
ransom:learn-c-the-hard-way ben$ rm -f ex3
ransom:learn-c-the-hard-way ben$ make ex3
cc -Wall -g ex3.c -o ex3
ransom:learn-c-the-hard-way ben$ valgrind ./ex3
==8795== Memcheck, a memory error detector
==8795== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8795== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==8795== Command: ./ex3
==8795==
==8795== Conditional jump or move depends on uninitialised value(s)
==8795== at 0x1003FBC3F: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==8795== by 0x1001EFB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x1001F9FE5: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x10021F9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x10021FC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x1001F5B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x1001F39D7: printf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x100000F2D: main (ex3.c:8)
==8795==
==8795== Conditional jump or move depends on uninitialised value(s)
==8795== at 0x1003FBC47: _platform_memchr$VARIANT$Haswell (in /usr/lib/system/libsystem_platform.dylib)
==8795== by 0x1001EFB96: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x1001F9FE5: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x10021F9AE: __v2printf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x10021FC80: __xvprintf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x1001F5B71: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x1001F39D7: printf (in /usr/lib/system/libsystem_c.dylib)
==8795== by 0x100000F2D: main (ex3.c:8)
==8795==
I am 10 years old.
I am 72 inches tall.
==8795==
==8795== HEAP SUMMARY:
==8795== in use at exit: 38,888 bytes in 426 blocks
==8795== total heap usage: 506 allocs, 80 frees, 45,016 bytes allocated
==8795==
==8795== LEAK SUMMARY:
==8795== definitely lost: 0 bytes in 0 blocks
==8795== indirectly lost: 0 bytes in 0 blocks
==8795== possibly lost: 0 bytes in 0 blocks
==8795== still reachable: 4,096 bytes in 1 blocks
==8795== suppressed: 34,792 bytes in 425 blocks
==8795== Rerun with --leak-check=full to see details of leaked memory
==8795==
==8795== For counts of detected and suppressed errors, rerun with: -v
==8795== Use --track-origins=yes to see where uninitialised values come from
==8795== ERROR SUMMARY: 4 errors from 2 contexts (suppressed: 0 from 0)
它说我Conditional jump or move depends on uninitialized value(s)
上的ex3.c:8
,但height
变量在第6行初始化。
我的猜测是这个问题与Valgrind在Yosemite上出现问题并且错误是假的,但我对C很新,而且我很确定代码是正确的,我也是不知道是否有遗漏的东西。
Valgrind或我的代码有问题吗?
答案 0 :(得分:8)
此特定报告对Valgrind来说是误报。 Yosemite上的Valgrind还没有完全覆盖系统库中的所有极端情况以及这些库使用的优化。
此处的提示是函数名称_platform_memchr $ VARIANT $ Haswell,即此错误的存在将取决于您的系统硬件,在这种情况下,您是否拥有基于Intel Haswell的CPU。
如果您可以根据Valgrind的http://valgrind.org/support/bug_reports.html报告此错误,那将是很好的,以便在下一个稳定的Valgrind发布之前修复它。
完全披露:我是Valgrind开发人员之一,他们提供了支持OS X 10.10的补丁
答案 1 :(得分:1)
您可以使用参数运行valgrind以忽略库代码。
然后所有那些(你应该忽略的)库错误消息都会消失。
来自valgrind.org 页面:http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress
错误检查工具可检测系统中的许多问题 库,例如C库,它预装了你的 OS。您无法轻松解决这些问题,但您不希望看到这些错误 (是的,有很多!)所以Valgrind读取错误列表 在启动时抑制。默认抑制文件由。创建 系统构建时./configure脚本。
您可以随意修改和添加到抑制文件,或者, 更好,写自己的。允许多个抑制文件。这个 如果您的项目的一部分包含您不能或不能的错误,则非常有用 想要修复,但你不想一直想起它们。
注意:到目前为止,添加抑制的最简单方法是使用 Core Command-line Options中描述的
--gen-suppressions=yes
选项。这会自动生成抑制。但是,为了获得最佳效果, 您可能想要手动编辑--gen-suppressions=yes
的输出 在这种情况下,最好仔细阅读本节。非常具体地描述要抑制的每个错误 无意中最小化抑制指令的可能性 抑制了一堆你想看到的类似错误。该 抑制机制旨在实现精确而灵活 要压制的错误规范。
答案 2 :(得分:1)
Valgrind绝对是优胜美地的实验。但是,我正在以不同的方式(更正确?)运行您的示例。我也使用svn版本,可能比你更新,因为我在测试之前更新了它。另一个区别是我自己建造了它,没有使用brew。
==14456== Memcheck, a memory error detector
==14456== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==14456== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==14456== Command: ./t1
==14456==
I am 10 years old.
I am 72 inches tall.
==14456==
==14456== HEAP SUMMARY:
==14456== in use at exit: 38,396 bytes in 418 blocks
==14456== total heap usage: 503 allocs, 85 frees, 44,692 bytes allocated
==14456==
==14456== LEAK SUMMARY:
==14456== definitely lost: 0 bytes in 0 blocks
==14456== indirectly lost: 0 bytes in 0 blocks
==14456== possibly lost: 0 bytes in 0 blocks
==14456== still reachable: 4,096 bytes in 1 blocks
==14456== suppressed: 34,300 bytes in 417 blocks
==14456== Rerun with --leak-check=full to see details of leaked memory
==14456==
==14456== For counts of detected and suppressed errors, rerun with: -v
==14456== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
<强>更新强>
这就是我构建valgrind的方法(默认选项):
./autogen.sh
./configure
make
sudo make install
这是libc(libSystem metalibrary)版本,我的示例二进制文件链接到:
$ otool -L t1
t1:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
这是我的铿锵版:
$ clang -v
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
答案 3 :(得分:1)
好吧,我可以添加另一个&#34;我在Yosemite&#34;上运行我的自制valgrind
并不会发生这种情况。该二进制文件的日期为2014-11-25,版本为&#34; Valgrind-3.11.0.SVN&#34;。运行测试代码,我得到输出:
$ valgrind --suppressions=suppressions ./ex3
==40416== Memcheck, a memory error detector
==40416== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==40416== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==40416== Command: ./ex3
==40416==
--40416-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--40416-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--40416-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
I am 10 years old.
I am 72 inches tall.
==40416==
==40416== HEAP SUMMARY:
==40416== in use at exit: 39,086 bytes in 428 blocks
==40416== total heap usage: 511 allocs, 83 frees, 45,358 bytes allocated
==40416==
==40416== LEAK SUMMARY:
==40416== definitely lost: 0 bytes in 0 blocks
==40416== indirectly lost: 0 bytes in 0 blocks
==40416== possibly lost: 0 bytes in 0 blocks
==40416== still reachable: 25,940 bytes in 308 blocks
==40416== suppressed: 13,146 bytes in 120 blocks
==40416== Rerun with --leak-check=full to see details of leaked memory
==40416==
==40416== For counts of detected and suppressed errors, rerun with: -v
==40416== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$
我的抑制文件列出了Yosemite运行时的18个泄漏(并且没有访问错误)(如果有帮助,我很乐意分享;它随着时间的推移而被{{1}创建} 选项)。我正在运行一款带有英特尔酷睿i7的老式(2011年初,17英寸)MacBook Pro。
我不喜欢&#39; Unknown mach_msg&#39;消息,但它们总是出现给我,他们不会明显地停止--gen-suppressions=yes
工作(它为我发现了真正的问题,并且没有报告应该工作的代码上的错误)。
我认为你所看到的问题出现在系统库中,并且抑制这两个消息是合理的,但是不得不这样做(很像是不得不抑制这么多的o / s泄漏) )。