c - 在 Ubuntu 中使用 Valgrind 看不到行号(Vagrant+Virtualbox)

标签 c ubuntu valgrind vagrant line-numbers

我目前正在阅读和休闲“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 的问题(部分)描述了相同的问题,但答案和解决方案对我不起作用:

到目前为止我尝试过的:

  • 已添加libc6-dbg
  • 已安装 gcc并尝试用它代替 cc 进行编译.
  • 已添加--track-origins=yesvalgrind -命令
  • 添加(后来删除)使用 -static 进行编译和-oO标志

所以我不知道从这里去哪里?我可以尝试安装最新的(而不是 v3.7) gcc手动虽然看起来相当困难。

编辑:
@abligh 答案似乎是正确的。我用万花筒做了这个: enter image description here 在左侧您可以看到结果: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/

相关文章:

c - Valgrind - 在 C 中实现的 "readline"函数中大小 1​​ 的无效读取

c - 大小 1 的读取无效

c - 发送/接收奇怪的数据

c - 以十六进制格式获取文件 - 我的输出与 xxd 命令输出

c - 解决简单 C 程序中的段错误

linux - 如何使用 linux 从一台计算机访问另一台计算机上的命令提示符

python - 如何使 Windmill IDE 出现在 Ubuntu 上的 https 站点上?

javascript - 当\js是未知命令时,如何在mysql-shell中切换到JS模式?

c - 使 valgrind 在未初始化的值上快速失败

c - 无法使用SSE进行优化