16

I'm trying to compile a program with debugging symbols so that valgrind will give me line numbers. I have found that if I compile a simple test program in one go (with -g) then it contains the symbols. However, if I compile in two passes (i.e. compile then link) then it does not contain the debugging symbols.

Here's the compile command for the single pass case:

g++ -g file.c -o file

And for two passes

g++ -g -c file.c -o file.o
g++ -g file.o -o file

The actual program looks like this and contains a simple Invalid Write

int main(){
    int* x = new int[10];
    x[10]=1;

}

If I compile with one pass then valgrind gives the following (note the line number at the end)

==24114== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
==24114==    at 0xB823: malloc (vg_replace_malloc.c:266)
==24114==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24114==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24114==    by 0x100000F09: main (file.c:3)

whereas if I compile in two passes I get this (with no line number):

==24135== 40 bytes in 1 blocks are definitely lost in loss record 2 of 9
==24135==    at 0xB823: malloc (vg_replace_malloc.c:266)
==24135==    by 0x5768D: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24135==    by 0x576DA: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==24135==    by 0x100000F09: main (in ./file)

Any insight on this would be much appreciated. I am using gcc version 4.2.1 on OS X 10.7.3

ks1322
  • 33,961
  • 14
  • 109
  • 164
Peter Cogan
  • 865
  • 1
  • 11
  • 19
  • 1
    Does Clang exhibit the same behaviour? – rubenvb Mar 23 '12 at 11:19
  • 1
    I guess something must be broken on your toolchain. On ubuntu (gcc 4.6.1) valgrind (3.6.1) shows line numbers for both compilation methods. – dbrank0 Mar 23 '12 at 11:39
  • 1
    can you debug second method using gdb step by step? – ks1322 Mar 23 '12 at 11:50
  • Thanks for the suggestions. Debugging the executable produced with the second method step by step with gdb indicates that lines numbers are actually there! But for some reason valgrind doesn't print them. Am currently setting up an ubuntu virtual machine to check there. Don't know how to compile with clang...will try to figure that out – Peter Cogan Mar 23 '12 at 12:57
  • OK I also can't reproduce on Ubuntu - must be something weird about valgrind (am using latest version). Thanks again for the help & suggestions – Peter Cogan Mar 23 '12 at 13:09
  • 10
    Final remark - it was indeed an OS X specific 'feature' to do with the way OS X links debug information. Valgrind helps the user circumvent the problem with the command --dsymutil=yes. You can read more about it here: http://tinyurl.com/6lwaez5 Credit to Dave Goodell who sent me the solution on valgrind users forum. – Peter Cogan Mar 23 '12 at 14:01
  • 10
    Probably best to post that as an answer, rather than a comment. I'd have found it sooner that way. – ams Mar 23 '12 at 17:25

2 Answers2

8

Final remark - it was indeed an OS X specific 'feature' to do with the way OS X links debug information. Valgrind helps the user circumvent the problem with the command --dsymutil=yes.

You can read more about it here: http://valgrind.org/docs/manual/manual-core.html#manual-core.erropts

Credit to Dave Goodell who sent me the solution on valgrind users forum.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Peter Cogan
  • 865
  • 1
  • 11
  • 19
2

Just for marking this question as "answered" (so it's not needlessly opened and read by others).

=> Answer is found as the comment from "user1288111" to the initial question.

SDwarfs
  • 3,189
  • 5
  • 31
  • 53