【问题标题】:Don't see line numbers using Valgrind inside Ubuntu (Vagrant+Virtualbox)在 Ubuntu 中看不到使用 Valgrind 的行号(Vagrant+Virtualbox)
【发布时间】:2014-03-18 21:37:14
【问题描述】:

我目前正在阅读“Learn C The Hard Way”-book。在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

我发现关于 SO 的以下 3 个问题(部分)描述了相同的问题,但答案和解决方案对我不起作用:

到目前为止我所尝试的:

  • 已添加libc6-dbg
  • 安装了 gcc 并尝试使用它而不是 cc 进行编译。
  • --track-origins=yes 添加到valgrind-命令
  • 添加(后来删除)使用-static-oO 标志编译

所以我不确定从这里去哪里?我可以尝试手动安装 gcc 的最新版本(而不是 v3.7),尽管这看起来相当困难。

编辑:
@abligh 的答案似乎是正确的。我用万花筒做了这个: 在左侧,您会看到 valgrind --track-origins=yes ./ex4 的结果,在右侧看到 valgrind ./ex4 的结果。

我想我仍然需要了解有关 c 及其工具的分配。

【问题讨论】:

  • 编译程序时会传递哪些选项给 gcc? -g 在里面吗?
  • 是的...一切都在我的仓库中。您可以签出 Makefile here、vagrantfile 和其余的 c 文件。

标签: c ubuntu valgrind vagrant line-numbers


【解决方案1】:

我认为您只是在输出中缺少它们。

这是您的一些输出(从 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

【讨论】:

  • 我认为你是对的!该死,我认为那些十六进制不应该在那里。谢谢!我会尽可能接受你的回答。编辑:哦,我知道我可以立即接受它^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2014-07-29
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
相关资源
最近更新 更多