Valgrind没有显示行号,尽管-g标志(在Ubuntu 11.10/VirtualBox上)

djb*_*djb 64 c valgrind

我正在关注'学习C艰难之路',特别是关于Valgrind的章节.本章给出了一个故意错误的程序来展示Valgrind的工作原理.

当我在Valgrind下运行练习时,我没有在我的堆栈跟踪中获得行号,只是'(在主要下面)'的错误.

肯定用-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)
Run Code Online (Sandbox Code Playgroud)

我在VirtualBox VM中使用Ubuntu 11.10.

感谢您的任何帮助.

更新

似乎如果我调用一个函数main()并且该函数包含一个错误(例如一个未初始化的变量),那么我找到一个函数被调用的地方main().但是,其中的错误main()仍未指定.有关示例,请参阅此粘贴.

小智 57

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

==5190== Use --track-origins=yes to see where uninitialised values come from
Run Code Online (Sandbox Code Playgroud)

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

valgrind --track-origins=yes ./ex4
Run Code Online (Sandbox Code Playgroud)

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

gcc -static -g  -o ex4  ex4.c 
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

档案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  }
Run Code Online (Sandbox Code Playgroud)

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

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

  • 即使你有阅读课,你仍然无法解决你的问题.这个答案没有解决任何问题.Valgrind在教程中的输出给出了使用未初始化变量的行.这与通过--track-origin = yes给出的输出有很大的不同......并且由于最初的问题是如何获得与Ubuntu上的教程相同的输出,问题应该仍然是开放的...顺便说一下,同样的问题正在运行使用valgrind 3.7.0在Ubuntu 11.10上(不是通过VM). (17认同)

Jas*_*ney 16

我也正在使用-g旗帜进行编译,但仍未获得行号.删除.dSYM我的应用程序的目录,并使用该--dsymutil=yes选项运行valgrind后,我终于得到了行号.