【问题标题】:Valgrind not showing line numbers in spite of -g flag (on Ubuntu 11.10/VirtualBox)尽管有 -g 标志,Valgrind 不显示行号(在 Ubuntu 11.10/VirtualBox 上)
【发布时间】:2016-12-01 08:57:24
【问题描述】:

我正在关注“Learn C the Hard Way”,特别是 the chapter on Valgrind。本章提供了一个故意错误的程序来展示 Valgrind 的工作原理。

当我在 Valgrind 下运行练习时,我的堆栈跟踪中没有行号,只有 '(below main)' 用于错误。

肯定使用 -g 标志进行编译。

我的 Valgrind 输出如下:

djb@twin:~/projects/Learning/C$ valgrind ./ex4
==5190== Memcheck, a memory error detector
==5190== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==5190== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==5190== Command: ./ex4
==5190== 
==5190== Use of uninitialised value of size 4
==5190==    at 0x4078B2B: _itoa_word (_itoa.c:195)
==5190==    by 0x407CE55: vfprintf (vfprintf.c:1619)
==5190==    by 0x40831DE: printf (printf.c:35)
==5190==    by 0x4052112: (below main) (libc-start.c:226)
==5190== 
==5190== Conditional jump or move depends on uninitialised value(s)
==5190==    at 0x4078B33: _itoa_word (_itoa.c:195)
==5190==    by 0x407CE55: vfprintf (vfprintf.c:1619)
==5190==    by 0x40831DE: printf (printf.c:35)
==5190==    by 0x4052112: (below main) (libc-start.c:226)
==5190== 
==5190== Conditional jump or move depends on uninitialised value(s)
==5190==    at 0x407CC10: vfprintf (vfprintf.c:1619)
==5190==    by 0x40831DE: printf (printf.c:35)
==5190==    by 0x4052112: (below main) (libc-start.c:226)
==5190== 
==5190== Conditional jump or move depends on uninitialised value(s)
==5190==    at 0x407C742: vfprintf (vfprintf.c:1619)
==5190==    by 0x40831DE: printf (printf.c:35)
==5190==    by 0x4052112: (below main) (libc-start.c:226)
==5190== 
I am 0 years old.
I am 68882420 inches tall.
==5190== 
==5190== HEAP SUMMARY:
==5190==     in use at exit: 0 bytes in 0 blocks
==5190==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==5190== 
==5190== All heap blocks were freed -- no leaks are possible
==5190== 
==5190== For counts of detected and suppressed errors, rerun with: -v
==5190== Use --track-origins=yes to see where uninitialised values come from
==5190== ERROR SUMMARY: 22 errors from 4 contexts (suppressed: 11 from 6)

我在 VirtualBox 虚拟机中使用 Ubuntu 11.10。

感谢您的帮助。

更新

似乎如果我从main() 调用一个函数并且该函数包含一个错误(例如一个未初始化的变量),那么我确实会跟踪到该函数在@ 中被调用的位置987654329@。但是,main() 中的错误仍未指定。示例见this paste

【问题讨论】:

  • 请创建一个新的空目录,并将您在pastebin.com/A6bK2hdw 找到的内容放入该目录;这是一个尝试重现问题的 bash 脚本。当我运行它时,我得到的输出就像你在 pastebin.com/JncWz2GF 看到的那样,这就是你的教程说你应该得到的。自己运行。如果你得到正确的输出,然后确定你做它的方式与这个 shell 脚本有什么不同。如果你运行这个 shell 脚本并得到不正确的输出,如上所示,回来讨论吧。
  • 嗨@Bill,感谢您的帮助。在运行您的脚本时,我得到相同的输出以及有关时钟偏差的警告。 http://pastebin.com/fjfPrLts
  • 我遇到了同样的问题,同样的操作系统,同样的 VirtualBox 虚拟机。我试过 valgrind 3.6.1 和 3.7.0 都没有使用 -g 选项显示行号。
  • 我猜这个问题与 VirtualBox 本身有关。我不熟悉VirtualBox。我建议您将 VirtualBox 标签添加到您的问题中,看看是否有什么东西浮出水面。如果这没有帮助,请查看forums.virtualbox.org 的人员是否可以提供帮助。希望我能提供更多帮助。对不起。
  • 刚刚在 Mac OSX 上测试了与我的 Ubuntu/VBox 设置相同的版本,它在 Mac 上完美运行。我认为 Bill 可能是对的,它要么是 VBox 的东西,要么是 Ubuntu 的东西。

标签: c valgrind


【解决方案1】:

您在问题中提供的输出包含以下行:

==5190== Use --track-origins=yes to see where uninitialised values come from

根据这条消息,您应该像这样运行./ex4

valgrind --track-origins=yes ./ex4

为避免 Valgrind 无法找到调试信息的一些问题,可以使用静态链接:

gcc -static -g  -o ex4  ex4.c 

Valgrind 的输出将包含类似Uninitialised value was created by a stack allocation:

的消息
==17673== Memcheck, a memory error detector
==17673== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==17673== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==17673== Command: ./ex4
...
==17673== Use of uninitialised value of size 4
==17673==    at 0x805CA7B: _itoa_word (in /home/user/ex4)
==17673==    by 0x8049D5F: printf (in /home/user/ex4)
==17673==    by 0x8048ECD: main (ex4.c:8)
==17673==  Uninitialised value was created by a stack allocation
==17673==    at 0x8048EFA: bad_function (ex4.c:17)
...
==17673== Use of uninitialised value of size 4
==17673==    at 0x805CA7B: _itoa_word (in /home/user/ex4)
==17673==    by 0x8049D5F: printf (in /home/user/ex4)
==17673==    by 0x80490BE: (below main) (in /home/user/ex4)
==17673==  Uninitialised value was created by a stack allocation
==17673==    at 0x8048EBE: main (ex4.c:4)
...
I am -1094375076 years old.
...
I am -1094369310 inches tall.
...
==17673== 
==17673== HEAP SUMMARY:
==17673==     in use at exit: 0 bytes in 0 blocks
==17673==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==17673== 
==17673== All heap blocks were freed -- no leaks are possible
==17673== 
==17673== For counts of detected and suppressed errors, rerun with: -v
==17673== ERROR SUMMARY: 83 errors from 21 contexts (suppressed: 0 from 0)

文件ex4.c

 1  #include <stdio.h>
 2
 3  int main()
 4  {
 5          int age = 10;
 6          int height;
 7
 8          bad_function();
 9
10          printf("I am %d years old.\n");
11          printf("I am %d inches tall.\n", height);
12
13          return 0;
14  }
15
16  int bad_function() 
17  {
18          int x;
19          printf("%d\n", x);
20  }

Valgrind 的输出并不理想。它标识了包含未初始化变量的堆栈帧(函数),但它不打印变量的名称。

在 VirtualBox 下运行 Linux 对 Valgrind 没有影响。

【讨论】:

  • 即使你上过阅读课,你仍然无法解决你的问题。这个答案不能解决任何问题。 Valgrind 在教程中的输出给出了使用未初始化变量的行。这与通过 --track-origins=yes... 给出的输出非常不同,并且由于最初的问题是如何获得与 Ubuntu 上的教程相同的输出,所以问题应该仍然是开放的...顺便说一句,完全相同的问题正在运行在 Ubuntu 11.10(不通过 VM)上使用 valgrind 3.7.0。
【解决方案2】:

我也在使用-g 标志进行编译,但仍然没有得到行号。在为我的应用删除 .dSYM 目录并使用 --dsymutil=yes 选项运行 valgrind 后,我终于得到了行号。

【讨论】:

  • 这仅适用于 OS X。
  • valgrind --track-origins=yes --dsymutil=yes ./FILE_NAME 在 Ubuntu 中可以解决问题,我使用 -g 标志编译
【解决方案3】:

在许多发行版中,glibc 的默认版本不包含调试符号。

尝试安装 libc6-dbg 包。

【讨论】:

    【解决方案4】:

    你应该用 "-g" 编译它。 gcc -g test.c -o 测试 接着 valgrind --track-origins=yes --leak-check=full ./test

    【讨论】:

    • "我肯定使用 -g 标志进行编译。" -- 引用自 OP。
    【解决方案5】:

    请注意,使用 --dsymutil=yes 解决方案运行 valgrind 仅适用于 Mac OS X。

    根据文档:

    --dsymutil=no|yes [no] 此选项仅在 Mac OS X 上运行 Valgrind 时相关。

    Mac OS X 使用延迟调试信息 (debuginfo) 链接方案。 当包含 debuginfo 的目标文件链接到 .dylib 或 可执行文件,调试信息不​​会复制到最终文件中。反而, 调试信息必须通过运行 dsymutil 手动链接, 系统提供的实用程序,在可执行文件或 .dylib 上。所结果的 组合的 debuginfo 放在可执行文件旁边的目录中 或 .dylib,但扩展名为 .dSYM。

    【讨论】:

      【解决方案6】:

      我追查了这个问题,但其他答案都没有奏效。我的输出显示了正确的符号,但没有出现行号。

      在我的情况下,这是由于有问题的库使用 .zdebug 压缩的行号信息,而我使用的 valgrind 版本太旧,还没有所需的补丁 [0]。

      解决方案是将 valgrind 升级到最新版本。

      [0]https://bugs.kde.org/show_bug.cgi?id=303877

      【讨论】:

        【解决方案7】:

        试试 gcc 而不是 cc

        cc 不提供行号,但 gcc 提供

        【讨论】:

        • cc 只是系统 C 编译器的符号链接,无论如何它很可能是 GCC。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        • 2012-01-19
        • 2013-04-09
        • 1970-01-01
        相关资源
        最近更新 更多