我目前正在阅读和休闲“Learn C The Hard Way”书。上exercise 4我必须安装Valgrind 。我首先在运行 Maverick 的 Macbook 上本地执行此操作,但收到一条警告,指出 Valgrind 可能无法 100% 工作。
所以现在我在 Ubuntu 12.04 盒子上尝试使用 Vagrant(使用 VirtualBox)。您可以在我的 github repo 上查看确切的 Vagrantfile(设置)和练习文件。 .
问题:
我没有看到行号,而是得到类似 0x40052B
的内容.
我通过执行以下操作编译了文件:
$ make clean # Just to be sure
$ make
$ valgrind --track-origins=yes ./ex4
我将结果粘贴到pastebin here .
我发现以下 3 个关于 SO 的问题(部分)描述了相同的问题,但答案和解决方案对我不起作用:
- Valgrind not showing line numbers in spite of -g flag (on Ubuntu 11.10/VirtualBox)
- How do you get Valgrind to show line errors?
- Valgrind does not show line-numbers
到目前为止我尝试过的:
- 已添加
libc6-dbg
- 已安装
gcc
并尝试用它代替cc
进行编译. - 已添加
--track-origins=yes
到valgrind
-命令 - 添加(后来删除)使用
-static
进行编译和-oO
标志
所以我不知道从这里去哪里?我可以尝试安装最新的(而不是 v3.7) gcc
手动虽然看起来相当困难。
编辑:
@abligh 答案似乎是正确的。我用万花筒做了这个:
在左侧您可以看到结果:valgrind --track-origins=yes ./ex4
右侧是 valgrind ./ex4
的结果.
我想我还需要学习分配c
它是工具。
最佳答案
我认为您只是在输出中丢失了它们。
这是您的一些输出(从 Pastebin 复制):
==16314== by 0x40052B: main (ex4.c:9)
^^--- LINE NUMBER
==16314== Uninitialised value was created by a stack allocation
==16314== at 0x4004F4: main (ex4.c:4)
^^--- LINE NUMBER
虽然我认为你的调用检查内存泄漏是错误的。我编写了一个非常简单的程序来泄漏一项:
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char **argv)
{
void *leak;
leak = malloc (1);
printf ("Leaked %p\n", leak);
exit (0);
}
并使用您的Makefile
编译它:
gcc -Wall -g test.c -o test
运行命令:
$ valgrind --track-origins=yes ./test
==26506== Memcheck, a memory error detector
==26506== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==26506== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==26506== Command: ./test
==26506==
Leaked 0x51f2040
==26506==
==26506== HEAP SUMMARY:
==26506== in use at exit: 1 bytes in 1 blocks
==26506== total heap usage: 1 allocs, 0 frees, 1 bytes allocated
==26506==
==26506== LEAK SUMMARY:
==26506== definitely lost: 0 bytes in 0 blocks
==26506== indirectly lost: 0 bytes in 0 blocks
==26506== possibly lost: 0 bytes in 0 blocks
==26506== still reachable: 1 bytes in 1 blocks
==26506== suppressed: 0 bytes in 0 blocks
==26506== Rerun with --leak-check=full to see details of leaked memory
==26506==
==26506== For counts of detected and suppressed errors, rerun with: -v
==26506== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
但是以我通常调用它的方式调用它:
$ valgrind --leak-check=full --show-reachable=yes ./test
==26524== Memcheck, a memory error detector
==26524== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==26524== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==26524== Command: ./test
==26524==
Leaked 0x51f2040
==26524==
==26524== HEAP SUMMARY:
==26524== in use at exit: 1 bytes in 1 blocks
==26524== total heap usage: 1 allocs, 0 frees, 1 bytes allocated
==26524==
==26524== 1 bytes in 1 blocks are still reachable in loss record 1 of 1
==26524== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26524== by 0x40059C: main (test.c:8)
==26524==
==26524== LEAK SUMMARY:
==26524== definitely lost: 0 bytes in 0 blocks
==26524== indirectly lost: 0 bytes in 0 blocks
==26524== possibly lost: 0 bytes in 0 blocks
==26524== still reachable: 1 bytes in 1 blocks
==26524== suppressed: 0 bytes in 0 blocks
==26524==
==26524== For counts of detected and suppressed errors, rerun with: -v
==26524== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
请注意,行号再次位于括号中,例如
==26524== by 0x40059C: main (test.c:8)
^^^^^^^^ <- FILENAME AND LINE NUMBER
关于c - 在 Ubuntu 中使用 Valgrind 看不到行号(Vagrant+Virtualbox),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22491596/