Valgrind尽pipe使用-g标志(在Ubuntu 11.10 / VirtualBox上)仍然没有显示行号,

我正在关注“学习艰苦之路”,特别是关于Valgrind的一章 。 本章给你一个故意错误的程序来展示Valgrind如何工作。

当我在Valgrind下运行练习时,我的堆栈跟踪中没有find行号,只是错误的“(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()调用函数,并且该函数包含一个错误(例如一个未初始化的variables),那么我得到一个跟踪在main()调用该函数的地方。 但main()错误仍未指定。 看到这个粘贴的例子。

7 Solutions collect form web for “Valgrind尽pipe使用-g标志(在Ubuntu 11.10 / VirtualBox上)仍然没有显示行号,”

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

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

根据这个消息你应该运行./ex4像这样:

 valgrind --track-origins=yes ./ex4 

为了避免Valgrind无法finddebugging信息的问题,可以使用静态链接:

 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的输出并不理想。 它标识包含未初始化variables的堆栈帧(函数),但不会打印variables的名称。

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

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

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

尝试安装libc6-dbg软件包。

你应该用“-g”来编译它。 gcc -g test.c -o test然后valgrind –track-originins = yes –leak-check = full ./test

请注意,仅为Mac OS X运行带有–dsymutil = yes解决scheme的valgrind。

根据文件:

dsymutil = no | yes [no]只有在Mac OS X上运行Valgrind时,此选项才是相关的。

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

尝试gcc不cc

cc不提供行号,但gcc做

我追查这个问题,其他答案都没有工作。 我的输出显示正确的符号,但行号不存在。

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

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

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

  • Valgrind检测到仍然可达泄漏
  • 正确的方法来初始化C ++结构
  • 在Mac OS X Mountain Lion和Mavericks上是否有任何替代valgrind的方法来检测C / C ++应用程序的内存泄漏?
  • cpp - valgrind - 大小为8的无效读取
  • 你用什么工具在Linux上开发C ++应用程序?
  • 这Valgrind警告是什么意思? - 警告设置地址范围烫发