1

I am just a beginner in the usage of valgrind.I have ubuntu opened as a part of vmWare and i just made a c program which should show valgrind errors and had run the valgrind on the a.out ,but i am unable to see the line numbers to be visible on the output: The command used is :

valgrind --leak-check=full --track-origins=yes ./a.out 

for the C program as shown below:

  #include <stdlib.h>

  #define ARRAY_SIZE      (5)

  typedef char TEST_TYPE;


  void invalid_write(TEST_TYPE* array, int size)
  {
     array[size] = 5;
  }


 int main(void)
 {
    TEST_TYPE static_array[ARRAY_SIZE];
    TEST_TYPE* dynamic_array = NULL;
    TEST_TYPE* p = NULL;
    TEST_TYPE i;


    dynamic_array = (TEST_TYPE*)malloc(ARRAY_SIZE * sizeof(TEST_TYPE));
   /* ERROR 1 : Writing out of array boundaries (heap overrun) */
    invalid_write(dynamic_array, ARRAY_SIZE);
 }

with the output as shown below:

==6801== Memcheck, a memory error detector
==6801== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6801== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6801== Command: ./a.out
==6801== 
==6801== Invalid write of size 1
==6801==    at 0x80483ED: invalid_write (in /home/jci/a.out)
==6801==    by 0x804842E: main (in /home/jci/a.out)
==6801==  Address 0x419702d is 0 bytes after a block of size 5 alloc'd
==6801==    at 0x4026444: malloc (vg_replace_malloc.c:263)
==6801==    by 0x8048416: main (in /home/jci/a.out)
==6801== 
==6801== 
==6801== HEAP SUMMARY:
==6801==     in use at exit: 5 bytes in 1 blocks
==6801==   total heap usage: 1 allocs, 0 frees, 5 bytes allocated
==6801== 
==6801== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6801==    at 0x4026444: malloc (vg_replace_malloc.c:263)
==6801==    by 0x8048416: main (in /home/jci/a.out)
==6801== 
==6801== LEAK SUMMARY:
==6801==    definitely lost: 5 bytes in 1 blocks
==6801==    indirectly lost: 0 bytes in 0 blocks
==6801==      possibly lost: 0 bytes in 0 blocks
==6801==    still reachable: 0 bytes in 0 blocks
==6801==         suppressed: 0 bytes in 0 blocks
==6801== 
==6801== For counts of detected and suppressed errors, rerun with: -v
==6801== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from 6)

How am i supposed to get the line numbers of these errors so that we could exactly pin point the problem? Currently the valgrind version used is 3.7.0.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Maddy
  • 503
  • 4
  • 12
  • 21
  • Show us the command you used to compile the code. – Graham Borland Mar 22 '13 at 11:51
  • 2
    Did you compile the program with debug information? Use the `-g` flag to GCC for that. – Some programmer dude Mar 22 '13 at 11:52
  • cc b.c is the command and the valgrind command used was valgrind --leak-check=full --track-origins=yes ./a.out – Maddy Mar 22 '13 at 11:52
  • 2
    At the risk of sounding like a broken record, [please don't cast the return value of `malloc()` and friends, in C](http://stackoverflow.com/a/605858/28169). Thanks. – unwind Mar 22 '13 at 11:52
  • @unwind I wonder why stackoverflow hasn't implemented an s/\([^)]+\) *malloc/alert("There is no need to cast malloc in C");/ yet, providing the "C" tag is included in the question... – autistic Mar 22 '13 at 13:12

2 Answers2

4

You need to build the program with debugging info, for gcc you should be able to do something like this:

gcc -g -O0 -Wall sourcefile.c

Valgrind will then show you the line numbers and function names from your source.

harald
  • 5,976
  • 1
  • 24
  • 41
  • 1
    I might add that it is often wise to disable compiler optimization, as well, since it can make the relationships to source lines very confusing at times... CFLAGS="-g -O0 -Wall" is a good idea when trying to debug and/or use valgrind (presuming you're using a gcc like compiler) – K Scott Piel Mar 22 '13 at 12:17
0

You can use addr2line tool for that purpose.

addr2line --exe a.out 8048416

I assume you have used -g flag when building objects:

gcc -c -g my_source1.c -o mysource1.o
gcc -c -g my_source2.c -o mysource2.o
gcc mysource1.o mysource2.o -o myapp

Or:

gcc -g my_source1.c my_source2.c -o myapp
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
  • 1
    "I assume you have used `-g`" -- if he had, Valgrind would have shown him line numbers without use of `addr2line`. Since Valgrind didn't, one has to assume that he had not; making `addr2line` also useless. – Employed Russian Mar 24 '13 at 18:58
  • @EmployedRussian addr2line can be used after getting run results. Even if the original executable had no debug symbols, the tool can be used with a new build. Not always, but in many cases it is possible – Valeri Atamaniouk Mar 24 '13 at 19:28